From 3646611a90648d796f1d1a97200b7b3f859ae760 Mon Sep 17 00:00:00 2001
From: Andrei <EndrIIMail@gmail.com>
Date: Sun, 4 Mar 2018 23:19:15 +0300
Subject: [PATCH] added server list

---
 SoundBand/CurentPlayList.qml   | 26 --------------
 SoundBand/MainPage.qml         |  8 -----
 SoundBand/PlayListEditPane.qml | 37 --------------------
 SoundBand/PlayListsControl.qml | 31 ----------------
 SoundBand/ServerListPage.qml   | 64 +++++++++++++++++++++++++++++++---
 SoundBand/app.cpp              | 14 +++++++-
 SoundBand/app.h                |  3 ++
 SoundBand/serverlistmodel.cpp  |  4 +--
 SoundBand/syncengine.cpp       | 12 ++++++-
 SoundBand/syncengine.h         |  5 +++
 sync/sync.cpp                  |  2 +-
 11 files changed, 95 insertions(+), 111 deletions(-)

diff --git a/SoundBand/CurentPlayList.qml b/SoundBand/CurentPlayList.qml
index 903004a..eb092f2 100644
--- a/SoundBand/CurentPlayList.qml
+++ b/SoundBand/CurentPlayList.qml
@@ -2,32 +2,6 @@ import QtQuick 2.4
 import QtQuick.Controls 2.0
 
 Item{
-    function addItem(obj){
-        model.append(obj);
-    }
-
-    function removeItem(obj){
-        model.remove(obj);
-    }
-
-    function clear(){
-        model.clear()
-    }
-
-    function init(){
-        var listOfSong = [];
-        listOfSong = syncEngine.curentPlayList(mainPage.curentPlayListName);
-
-        for(var i = 0; i < listOfSong.length; i++){
-            var temp = Qt.createComponent("SongDelegate.qml");
-            if(temp.status === Component.Ready){
-                var obj = temp.createObject();
-                var songName = listOfSong[i];
-                obj.init(syncEngine.songImageByName(songName), songName);
-                parent.addItem(obj);
-            }
-        }
-    }
 
     ListView {
         id: listView
diff --git a/SoundBand/MainPage.qml b/SoundBand/MainPage.qml
index 1824927..60a2a0a 100644
--- a/SoundBand/MainPage.qml
+++ b/SoundBand/MainPage.qml
@@ -1,6 +1,5 @@
 import QtQuick 2.7
 import QtQuick.Controls 2.0
-import SyncEngine 1.0
 
 Page {
     id: mainPage
@@ -10,12 +9,6 @@ Page {
     property string curentPlayListName: "all"
 
     function onLoaded(){
-        playListsControl.init();
-        curentPlayList.init();
-    }
-
-    SyncEngine{
-        id: syncEngine
 
     }
 
@@ -46,7 +39,6 @@ Page {
             PlayListsControl{
                 id:playListsControl;
 
-
             }
 
         }
diff --git a/SoundBand/PlayListEditPane.qml b/SoundBand/PlayListEditPane.qml
index c7a62ae..d03e897 100644
--- a/SoundBand/PlayListEditPane.qml
+++ b/SoundBand/PlayListEditPane.qml
@@ -12,43 +12,6 @@ Rectangle {
     signal select();
     color: Utils.backgroundColor()
 
-    function addItem(obj){
-        model.append(obj);
-    }
-
-    function removeItem(obj){
-        model.remove(obj);
-    }
-
-    function clear(){
-        model.clear()
-    }
-
-    function view(curentPlayListName){
-        name = curentPlayListName;
-        playListPane.visible = true;
-        var added = [], all = [];
-        added = syncEngine.getPlayList(name);
-        all = syncEngine.getPlayList("");
-        for(var i = 0; i < all.lenght; i++){
-
-            var temp = Qt.createComponent("SongDelegateSelection.qml");
-            if(temp.status === Component.Ready){
-                var obj = temp.createObject();
-                obj.init("/image/res/logo.png", all[i]);
-
-                if(added.indexOf(all[i]) >= 0){
-                    obj.isSelected = true;
-                }
-
-                addItem(obj);
-            }
-
-
-        }
-
-    }
-
     Base.BaseText{
         id:namePalyList
         height: 30
diff --git a/SoundBand/PlayListsControl.qml b/SoundBand/PlayListsControl.qml
index 24a8f1e..fdfb56c 100644
--- a/SoundBand/PlayListsControl.qml
+++ b/SoundBand/PlayListsControl.qml
@@ -5,33 +5,6 @@ import "./base" as Base
 
 Item {
     id: playListsControl
-    function addItem(obj){
-        model.append(obj);
-    }
-
-    function removeItem(obj){
-        model.remove(obj);
-    }
-
-    function clear(){
-        model.clear()
-    }
-
-    function init(){
-        var playlists = [];
-        playlists = syncEngine.allPlayLists();
-
-        switch_pane(playListsControlSource);
-
-        for(var i = 0; i < playlists.length; i++){
-            var temp = Qt.createComponent("PlayListDelegate.qml");
-            if(temp.status === Component.Ready){
-                var obj = temp.createObject();
-                obj.init(playlists[i], i);
-                addItem(obj);
-            }
-        }
-    }
 
     function switch_pane(object){
         for(var i = 0; i < playListsControl.children.length; i++){
@@ -72,7 +45,6 @@ Item {
 
                 onClicked: {
                     editPlayList.visible = true;
-                    editPlayList.name = model.get(playLists.selectedItem).text;
                 }
 
             }
@@ -128,9 +100,6 @@ Item {
         showDotAndDotDot: false
         nameFilters: "*.mp3"
         onFilesSelected: {
-            for(var i = 0; i< selectedFiles.length; i++){
-                syncEngine.play(selectedFiles[i]);
-            }
             switch_pane(editPlayList);
           //  messageDialog.text = "Cannot open file "+ currentFolder() + "/" + fileName
           //  messageDialog.open()
diff --git a/SoundBand/ServerListPage.qml b/SoundBand/ServerListPage.qml
index e68ec25..08558b9 100644
--- a/SoundBand/ServerListPage.qml
+++ b/SoundBand/ServerListPage.qml
@@ -1,29 +1,85 @@
 import QtQuick 2.7
 import QtQuick.Controls 2.0
+import QtQuick.Window 2.0
+
 import "./base" as Base
+import "base/utils.js" as Utils
 
 
 Item {
 
+    readonly property real rowHeight: Utils.dp(Screen.pixelDensity, 36)
+    readonly property real rowWidth: parent.width;
+    function onItemClick() {
+    }
+
     Button{
         id:listen
         width: parent.width
-        text: qsTr("Add")
+        text: qsTr("Refresh")
         anchors.top:parent.top
         anchors.left: parent.left
 
+        onClicked: {
+            syncEngine.scan();
+        }
+
     }
 
     ListView {
         id: listView
 
-        model: ListModel {
+        model: serverListModel;
 
+        Component {
+            id: serverDelegate
+
+            Item {
+                height: rowHeight
+                width: listView.width
+                id: item
+
+                Rectangle {
+                    color: Qt.rgba(0,0,0,0)
+                    id: rectangle;
+                    anchors.fill: item
+
+                    MouseArea {
+                        anchors.fill: rectangle
+                        onClicked: {
+                            indicator.active = onItemClick()
+                        }
+                    }
+
+                    Text {
+                        id: serverNameText
+                        height: width
+                        anchors.left: image.right
+                        anchors.top: rectangle.top
+                        anchors.bottom: rectangle.bottom
+                        anchors.right: rectangle.right
+                        text: serverName !== undefined ? serverName : ""
+                        horizontalAlignment: Text.AlignHCenter
+                        verticalAlignment: Text.AlignVCenter
+                    }
+
+                    Image {
+                        id: image
+                        height: buttonHeight
+                        width: height
+                        anchors.left: rectangle.left
+                        anchors.leftMargin: textmargin
+                        anchors.verticalCenter: rectangle.verticalCenter
+                        source: "/icons/res/folder.png"
+                    }
+                }
+            }
         }
+        delegate: serverDelegate
 
-        delegate: ServerDelegate{
+        ScrollIndicator.horizontal: ScrollIndicator { }
+        ScrollIndicator.vertical: ScrollIndicator { }
 
-        }
         anchors.top:listen.bottom
         anchors.left: parent.left
         anchors.right: parent.right
diff --git a/SoundBand/app.cpp b/SoundBand/app.cpp
index 13901b2..02184ad 100644
--- a/SoundBand/app.cpp
+++ b/SoundBand/app.cpp
@@ -1,7 +1,10 @@
 #include "app.h"
 #include <QQmlApplicationEngine>
+#include <QQmlContext>
 #include "syncengine.h"
 #include "imageprovider.h"
+#include "serverlistmodel.h"
+
 
 App::App(QObject* ptr):
     QObject(ptr)
@@ -9,10 +12,18 @@ App::App(QObject* ptr):
     qmlEngine = new QQmlApplicationEngine();
     syncEngine = new SyncEngine();
     imageProvider = new ImageProvider(syncEngine);
+    serverListModel = new ServerListModel();
+    serverListModel->setSource(syncEngine);
 }
 
 bool App::run(){
-    qmlRegisterType<SyncEngine>("SyncEngine",1,0,"SyncEngine");
+
+
+    qmlEngine->addImageProvider(QLatin1String("collection"), imageProvider);
+
+    QQmlContext *ctxt = qmlEngine->rootContext();
+    ctxt->setContextProperty("syncEngine", syncEngine);
+    ctxt->setContextProperty("serverListModel", serverListModel);
 
     qmlEngine->load(QUrl(QStringLiteral("qrc:/main.qml")));
     if (qmlEngine->rootObjects().isEmpty())
@@ -22,6 +33,7 @@ bool App::run(){
 }
 
 App::~App(){
+    delete serverListModel;
     delete syncEngine;
     delete qmlEngine;
 }
diff --git a/SoundBand/app.h b/SoundBand/app.h
index 75e3787..16da38c 100644
--- a/SoundBand/app.h
+++ b/SoundBand/app.h
@@ -7,6 +7,8 @@
 class SyncEngine;
 class QQmlApplicationEngine;
 class ImageProvider;
+class ServerListModel;
+
 
 /**
  * @brief The App class
@@ -18,6 +20,7 @@ private:
     SyncEngine *syncEngine;
     QQmlApplicationEngine *qmlEngine;
     ImageProvider *imageProvider;
+    ServerListModel *serverListModel;
 
 public:
     explicit App(QObject *ptr = nullptr);
diff --git a/SoundBand/serverlistmodel.cpp b/SoundBand/serverlistmodel.cpp
index 2adfddc..62a8d8f 100644
--- a/SoundBand/serverlistmodel.cpp
+++ b/SoundBand/serverlistmodel.cpp
@@ -18,7 +18,7 @@ void ServerListModel::setSource(const SyncEngine *engine){
 
 QHash<int, QByteArray> ServerListModel::roleNames()const{
     QHash<int, QByteArray> roles;
-    roles[nameRole] = "name";
+    roles[nameRole] = "serverName";
     return roles;
 }
 
@@ -30,7 +30,7 @@ void ServerListModel::onPlayListsChanged(){
 
 bool ServerListModel::canFetchMore(const QModelIndex & /* index */) const
 {
-    if (itemCount < servers->size())
+    if (servers && itemCount < servers->size())
         return true;
     else
         return false;
diff --git a/SoundBand/syncengine.cpp b/SoundBand/syncengine.cpp
index 577b183..d918f84 100644
--- a/SoundBand/syncengine.cpp
+++ b/SoundBand/syncengine.cpp
@@ -131,11 +131,21 @@ bool SyncEngine::listen(int index){
     }
 }
 
- const QList<ETcpSocket*>* SyncEngine::getServerList() const{
+const QList<ETcpSocket*>* SyncEngine::getServerList() const{
     return &sync->getServersList();
 
 }
 
+void SyncEngine::scan(){
+    try{
+        sync->scan();
+    }catch(BaseException e){
+
+        _lastError = e.what();
+        emit error();
+    }
+}
+
 int SyncEngine::repeat()const{
     return _repeat;
 }
diff --git a/SoundBand/syncengine.h b/SoundBand/syncengine.h
index 3885fc0..3b99c90 100644
--- a/SoundBand/syncengine.h
+++ b/SoundBand/syncengine.h
@@ -148,6 +148,11 @@ public slots:
      */
     const QList<ETcpSocket *> *getServerList() const;
 
+    /**
+     * @brief scan
+     */
+    void scan();
+
 signals:
 
     /**
diff --git a/sync/sync.cpp b/sync/sync.cpp
index 496497c..03d9179 100644
--- a/sync/sync.cpp
+++ b/sync/sync.cpp
@@ -384,7 +384,7 @@ void Sync::packageRender(ETcpSocket *socket){
 
 void Sync::rescan(bool deep){
     package pac;
-    if(!createPackage(t_what,pac)){
+    if(!createPackage(t_what, pac)){
         throw CreatePackageExaption();
         return;
     }