From b5e6e96c52f37a145a437048fc63bfd9bf424164 Mon Sep 17 00:00:00 2001
From: adamsjensen <adamsjensen@yandex.by>
Date: Tue, 6 Mar 2018 16:12:25 +0300
Subject: [PATCH] added settings

---
 SoundBand/CurentPlayList.qml |  2 +-
 SoundBand/app.cpp            |  7 +++++++
 SoundBand/app.h              |  1 +
 SoundBand/syncengine.cpp     | 10 +++++++++-
 SoundBand/syncengine.h       |  3 +++
 sync/config.h                |  1 +
 sync/mysql.cpp               | 22 +++++++++++++++-------
 sync/sync.cpp                |  1 -
 8 files changed, 37 insertions(+), 10 deletions(-)

diff --git a/SoundBand/CurentPlayList.qml b/SoundBand/CurentPlayList.qml
index 0996e40..7d9f1e1 100644
--- a/SoundBand/CurentPlayList.qml
+++ b/SoundBand/CurentPlayList.qml
@@ -23,7 +23,7 @@ Item{
         width: parent.width
         height: parent.height
 
-        model: playListModel;
+        model: currentPlayListModel;
 
         Component {
             id: playListDelegate
diff --git a/SoundBand/app.cpp b/SoundBand/app.cpp
index 634f8eb..b9bebc1 100644
--- a/SoundBand/app.cpp
+++ b/SoundBand/app.cpp
@@ -22,6 +22,9 @@ App::App(QObject* ptr):
 
     playListModel = new PlayListModel();
     playListModel->setSource(syncEngine);
+
+    currentPlayListModel = new PlayListModel();
+    currentPlayListModel->setSource(syncEngine);
 }
 
 bool App::run(){
@@ -33,6 +36,7 @@ bool App::run(){
     ctxt->setContextProperty("serverListModel", serverListModel);
     ctxt->setContextProperty("playListsModel", playListsModel);
     ctxt->setContextProperty("playListModel", playListModel);
+    ctxt->setContextProperty("currentPlayListModel", currentPlayListModel);
 
     qmlEngine->load(QUrl(QStringLiteral("qrc:/main.qml")));
     if (qmlEngine->rootObjects().isEmpty())
@@ -42,6 +46,9 @@ bool App::run(){
 }
 
 App::~App(){
+    delete playListsModel;
+    delete playListModel;
+    delete currentPlayListModel;
     delete serverListModel;
     delete syncEngine;
     delete qmlEngine;
diff --git a/SoundBand/app.h b/SoundBand/app.h
index 0cc471f..fd3aee1 100644
--- a/SoundBand/app.h
+++ b/SoundBand/app.h
@@ -25,6 +25,7 @@ private:
     ServerListModel *serverListModel;
     PlayListsModel *playListsModel;
     PlayListModel *playListModel;
+    PlayListModel *currentPlayListModel;
 
 
 public:
diff --git a/SoundBand/syncengine.cpp b/SoundBand/syncengine.cpp
index 43b5c99..5be3474 100644
--- a/SoundBand/syncengine.cpp
+++ b/SoundBand/syncengine.cpp
@@ -7,6 +7,9 @@ SyncEngine::SyncEngine()
     sync = new syncLib::Sync();
     sqlApi = sync->getSqlApi();
 
+    sync->updatePlayList(settings.value(CURRENT_PLAYLIST_KEY, ALL_SONGS_LIST).toString());
+
+
     connect(sync, SIGNAL(networkStateChange()), this, SIGNAL(serversCountChanged()));
 }
 
@@ -140,7 +143,12 @@ void SyncEngine::setRepeat(int flag){
 
 bool SyncEngine::setPlayList(const QString& name){
 
-    return sync->updatePlayList(name);
+    if(!sync->updatePlayList(name)){
+        return false;
+    }
+
+    settings.setValue(CURRENT_PLAYLIST_KEY, name);
+    return true;
 }
 
 const QString& SyncEngine::lastError() const{
diff --git a/SoundBand/syncengine.h b/SoundBand/syncengine.h
index 14d3b9b..433c6cd 100644
--- a/SoundBand/syncengine.h
+++ b/SoundBand/syncengine.h
@@ -3,6 +3,7 @@
 #include "../sync/sync.h"
 #include <QObject>
 #include <QPixmap>
+#include <QSettings>
 
 
 /**
@@ -23,6 +24,8 @@ private:
     QString _curentPlayListName;
     QString _lastError;
     Repeat _repeat;
+    QSettings settings;
+
 public:
     SyncEngine();
 
diff --git a/sync/config.h b/sync/config.h
index 6ee32a3..f8f81ec 100644
--- a/sync/config.h
+++ b/sync/config.h
@@ -2,6 +2,7 @@
 #define CONFIG_H
 
 // general otions
+#define CURRENT_PLAYLIST_KEY        "currentPlayList"
 
 // LIB VERSION
 #define MAJOR_VERSION               0
diff --git a/sync/mysql.cpp b/sync/mysql.cpp
index fbd5eb9..4d3d547 100644
--- a/sync/mysql.cpp
+++ b/sync/mysql.cpp
@@ -101,13 +101,15 @@ void MySql::initDB(const QString &database){
             return;
         }
 
-        qyer = QString("CREATE UNIQUE INDEX IF NOT EXISTS iplaylistsdata ON playlistsdata(playlist,song)");
+        qyer = QString("CREATE UNIQUE INDEX IF NOT EXISTS iplaylistsdata ON "
+                       "playlistsdata(playlist,song)");
         if(!qyery->exec(qyer)){
             sqlErrorLog(qyer);
             throw InitDBError();
             delete db;
             return;
         }
+
     }
 }
 
@@ -207,7 +209,8 @@ bool MySql::updateAvailableSongs(QList<SongHeader>& list, const QString& playLis
         qyer = QString("SELECT id,name,size from songs");
     }else{
         qyer = QString("SELECT id,name,size from songs where "
-                       "id = (select song from playlistsdata where playlist='%0')").arg(playList);
+                       "id = (select song from playlistsdata where "
+                       "playlist='%0')").arg(playList);
     }
 
     if(!qyery->exec(qyer)){
@@ -250,7 +253,8 @@ bool MySql::removeSong(const SongHeader &header){
             return false;
         }
     }else if(!header.name.isEmpty() && header.size > 0){
-        QString qyer = QString("DELETE from songs where name='%0' and size=%1").arg(header.name).arg(header.size);
+        QString qyer = QString("DELETE from songs where name='%0'"
+                               " and size=%1").arg(header.name).arg(header.size);
         if(!qyery->exec(qyer)){
             sqlErrorLog(qyer);
             return false;
@@ -265,7 +269,8 @@ bool MySql::addPlayList(const QString &newPlayList, const QString& desc){
     if(newPlayList == ALL_SONGS_LIST)
         return false;
 
-    QString qyer = QString("INSERT INTO playlists(name, description) VALUES('%0', '%1')").arg(newPlayList, desc);
+    QString qyer = QString("INSERT INTO playlists(name, description)"
+                           " VALUES('%0', '%1')").arg(newPlayList, desc);
     if(!qyery->exec(qyer)){
         sqlErrorLog(qyer);
         return false;
@@ -278,14 +283,16 @@ bool MySql::addToPlayList(const SongHeader &header, const QString &newPlaylist){
         return false;
 
     if(header.id > -1){
-        QString qyer = QString("INSERT INTO playlistsdata(song, playlist) VALUES(%0,'%1')").arg(header.id).arg(newPlaylist);
+        QString qyer = QString("INSERT INTO playlistsdata(song, playlist)"
+                               " VALUES(%0,'%1')").arg(header.id).arg(newPlaylist);
         if(!qyery->exec(qyer)){
             sqlErrorLog(qyer);
             return false;
         }
     }else if(!header.name.isEmpty() && header.size > 0){
         QString qyer = QString("INSERT INTO playlistsdata(song, playlist) "
-                               "VALUES((SELECT id from songs where name='%0' and size='%1'),'%2')")
+                               "VALUES((SELECT id from songs where name='%0'"
+                               " and size='%1'),'%2')")
                 .arg(header.name).arg(header.size).arg(newPlaylist);
 
         if(!qyery->exec(qyer)){
@@ -310,7 +317,8 @@ bool MySql::removeFromPlayList(const SongHeader &header, const QString &playList
         }
     }else if(!header.name.isEmpty() && header.size > 0){
         QString qyer = QString("DELETE from playlistsdata where "
-                               "song=(SELECT id from songs where name='%0' and size='%1') and playlist='%2'")
+                               "song=(SELECT id from songs where name='%0' and size='%1')"
+                               " and playlist='%2'")
                 .arg(header.name).arg(header.size).arg(playList);
 
         if(!qyery->exec(qyer)){
diff --git a/sync/sync.cpp b/sync/sync.cpp
index 03d9179..84a7d5a 100644
--- a/sync/sync.cpp
+++ b/sync/sync.cpp
@@ -28,7 +28,6 @@ Sync::Sync(const QString &address, int port, const QString &datadir):
     ping = 0;
 
     sql = new MySql(datadir);
-    sql->updateAvailableSongs(playList);
 
     connect(node, SIGNAL(Message(ETcpSocket*)), SLOT(packageRender(ETcpSocket*)));
     connect(&deepScaner, SIGNAL(scaned(QList<ETcpSocket*>*)), SLOT(deepScaned(QList<ETcpSocket*>*)));