refactored AppSettings to Singleton pattern.
authordruid23 <usr@dru-id.co.uk>
Tue, 7 Sep 2010 00:15:14 +0000 (01:15 +0100)
committerdruid23 <usr@dru-id.co.uk>
Tue, 7 Sep 2010 00:15:14 +0000 (01:15 +0100)
modified:   src/accountdialog.cpp
modified:   src/appsettings.cpp
modified:   src/appsettings.h
modified:   src/newaccountdialog.cpp

src/accountdialog.cpp
src/appsettings.cpp
src/appsettings.h
src/newaccountdialog.cpp

index cc97fec..8089a9a 100644 (file)
@@ -197,8 +197,6 @@ void AccountDialog::use()
 {
     QString currentKey = ui->listWidget->currentItem()->text();
     AppSettings::setCurrentKey(currentKey);
-    //QSettings settings;
-    //settings.setValue("config/currentKey", currentKey);
     load();
     emit accept();
 }
index 23f1598..1a48354 100644 (file)
 #include <QNetworkInterface>
 #include "appsettings.h"
 
+bool AppSettings::_haveCurrentKey            = false;
+QString AppSettings::_currentKey             = "";
+bool AppSettings::_haveCurrentIp             = false;
+QString AppSettings::_currentIp              = "";
+bool AppSettings::_haveConnectionTimeout     = false;
+int  AppSettings::_connectionTimeout         = 0;
+bool AppSettings::_havePingTimeout           = false;
+int  AppSettings::_pingTimeout               = 0;
+bool AppSettings::_haveStatusPollTimeout     = false;
+int  AppSettings::_statusPollTimeout         = 0;
+bool AppSettings::_haveRetrieveArtTimeout    = false;
+int  AppSettings::_retrieveArtTimeout        = 0;
+bool AppSettings::_haveRetryNetworkTimeout   = false;
+int  AppSettings::_retryNetworkTimeout       = 0;
+bool AppSettings::_haveShowUnknownFileTypes  = false;
+bool AppSettings::_showUnknownFileTypes      = false;
+bool AppSettings::_haveShowAlbumArt          = false;
+bool AppSettings::_showAlbumArt              = false;
+bool AppSettings::_haveAlertOnClose          = false;
+bool AppSettings::_alertOnClose              = false;
+bool AppSettings::_haveOrientation           = false;
+Orientation AppSettings::_orientation        = LANDSCAPE;
+
 AppSettings::AppSettings() {
 }
 
@@ -28,15 +51,7 @@ AppSettings::~AppSettings() {
 bool AppSettings::isConnected() {
     QNetworkInterface wlan = QNetworkInterface::interfaceFromName("wlan0");
     QNetworkInterface gprs = QNetworkInterface::interfaceFromName("gprs0");
-
-    if( (wlan.isValid() && wlan.flags().testFlag(QNetworkInterface::IsUp)) || (gprs.isValid() && gprs.flags().testFlag(QNetworkInterface::IsUp)) )
-    {
-        return true;
-    }
-    else
-    {
-        return false;
-    }
+    return ( (wlan.isValid() && wlan.flags().testFlag(QNetworkInterface::IsUp)) || (gprs.isValid() && gprs.flags().testFlag(QNetworkInterface::IsUp)) );
 }
 QStringList AppSettings::getAllAccounts() {
     QStringList accounts;
@@ -47,20 +62,34 @@ QStringList AppSettings::getAllAccounts() {
     return accounts;
 }
 QString AppSettings::getCurrentKey() {
-    QSettings sets;
-    return sets.value("config/currentKey", "").toString();
+    if (!_haveCurrentKey) {
+        QSettings sets;
+        _currentKey = sets.value("config/currentKey", "").toString();
+        _haveCurrentKey = true;
+    }
+    return _currentKey;
 }
-void AppSettings::setCurrentKey(QString key) {
+QString AppSettings::setCurrentKey(QString currentKey) {
     QSettings sets;
-    sets.setValue("config/currentKey", key);
+    sets.setValue("config/currentKey", currentKey);
+    _currentKey = currentKey;
+    _haveCurrentKey = true;
+    return _currentKey;
 }
 QString AppSettings::getCurrentIp() {
-    QSettings sets;
-    return sets.value("account/" + getCurrentKey(), "").toString();
+    if (!_haveCurrentIp) {
+        QSettings sets;
+        _currentIp = sets.value("account/" + getCurrentKey(), "").toString();
+        _haveCurrentIp = true;
+    }
+    return _currentIp;
 }
-void AppSettings::setCurrentIp(QString ip) {
+QString AppSettings::setCurrentIp(QString currentIp) {
     QSettings sets;
-    sets.setValue("account/" + getCurrentKey(), ip);
+    sets.setValue("account/" + getCurrentKey(), currentIp);
+    _currentIp = currentIp;
+    _haveCurrentIp = true;
+    return _currentIp;
 }
 VlcDirectory AppSettings::getHomeDirectory() {
     QSettings sets;
@@ -97,7 +126,7 @@ bool AppSettings::addFavourite(VlcDirectory dir) {
     // should check for existing first otherwise it overwrites
     if (0 < sets.value("config/accounts/" + getCurrentKey() + "/favourites/" + dir.name, "").toString().length()) {
         dir.name = "_" + dir.name;
-        return addFavourite(dir);
+        return addFavourite(dir); // recurse
     }
     sets.setValue("config/accounts/" + getCurrentKey() + "/favourites/" + dir.name, dir.path);
     return true;
@@ -108,36 +137,138 @@ bool AppSettings::deleteFavourite(VlcDirectory dir) {
     return true;
 }
 int AppSettings::getStatusPollTimeout() {
-    return STATUS_POLL_TIMEOUT;
+    if (!_haveStatusPollTimeout) {
+        QSettings sets;
+        _statusPollTimeout = sets.value("config/statusPollTimeout", STATUS_POLL_TIMEOUT).toInt();
+        _haveStatusPollTimeout = true;
+    }
+    return _statusPollTimeout;
+}
+int AppSettings::setStatusPollTimeout(int statusPollTimeout) {
+    QSettings sets;
+    sets.setValue("config/statusPollTimeout", statusPollTimeout);
+    _statusPollTimeout = statusPollTimeout;
+    _haveStatusPollTimeout = true;
+    return _statusPollTimeout;
 }
 int AppSettings::getPingTimeout() {
-    return PING_TIMEOUT;
+    if (!_havePingTimeout) {
+        QSettings sets;
+        _pingTimeout = sets.value("config/pingTimeout", PING_TIMEOUT).toInt();
+        _havePingTimeout = true;
+    }
+    return _pingTimeout;
+}
+int AppSettings::setPingTimeout(int havePingTimeout) {
+    QSettings sets;
+    sets.setValue("config/pingTimeout", havePingTimeout);
+    _havePingTimeout = havePingTimeout;
+    _havePingTimeout = true;
+    return _pingTimeout;
 }
 int AppSettings::getConnectionTimeout() {
-    return CONNECTION_TIMEOUT;
+    if (!_haveConnectionTimeout) {
+        QSettings sets;
+        _connectionTimeout = sets.value("config/connectionTimeout", CONNECTION_TIMEOUT).toInt();
+        _haveConnectionTimeout = true;
+    }
+    return _connectionTimeout;
+}
+int AppSettings::setConnectionTimeout(int connectionTimeout) {
+    QSettings sets;
+    sets.setValue("config/connectionTimeout", connectionTimeout);
+    _connectionTimeout = connectionTimeout;
+    _haveConnectionTimeout = true;
+    return _connectionTimeout;
 }
 int AppSettings::getRetrieveArtTimeout() {
-    return RETRIEVE_ART_TIMEOUT;
+    if (!_haveRetrieveArtTimeout) {
+        QSettings sets;
+        _retrieveArtTimeout = sets.value("config/retrieveArtTimeout", RETRIEVE_ART_TIMEOUT).toInt();
+        _haveRetrieveArtTimeout = true;
+    }
+    return _retrieveArtTimeout;
+}
+int AppSettings::setRetrieveArtTimeout(int retrieveArtTimeout) {
+    QSettings sets;
+    sets.setValue("config/retrieveArtTimeout", retrieveArtTimeout);
+    _retrieveArtTimeout = retrieveArtTimeout;
+    _haveRetrieveArtTimeout = true;
+    return _retrieveArtTimeout;
 }
 int AppSettings::getRetryNetworkTimeout() {
-    return RETRY_NETWORK_TIMEOUT;
+    if (!_haveRetryNetworkTimeout) {
+        QSettings sets;
+        _retryNetworkTimeout = sets.value("config/retryNetworkTimeout", RETRY_NETWORK_TIMEOUT).toInt();
+        _haveRetryNetworkTimeout = true;
+    }
+    return _retryNetworkTimeout;
+}
+int AppSettings::setRetryNetworkTimeout(int retryNetworkTimeout) {
+    QSettings sets;
+    sets.setValue("config/retryNetworkTimeout", retryNetworkTimeout);
+    _retryNetworkTimeout = retryNetworkTimeout;
+    _haveRetryNetworkTimeout = true;
+    return _retryNetworkTimeout;
 }
 bool AppSettings::getShowUnknownFileTypes() {
-    return SHOW_UNKNOWN_FILETYPES;
+    if (!_haveShowUnknownFileTypes) {
+        QSettings sets;
+        _showUnknownFileTypes = sets.value("config/showUnknownFileTypes", SHOW_UNKNOWN_FILETYPES).toBool();
+        _haveShowUnknownFileTypes = true;
+    }
+    return _showUnknownFileTypes;
+}
+bool AppSettings::setShowUnknownFileTypes(bool showUnknownFileTypes) {
+    QSettings sets;
+    sets.setValue("config/showUnknownFileTypes", showUnknownFileTypes);
+    _showUnknownFileTypes = showUnknownFileTypes;
+    _haveShowUnknownFileTypes = true;
+    return _showUnknownFileTypes;
 }
 bool AppSettings::getShowAlbumArt() {
-    return SHOW_ALBUM_ART;
+    if (!_haveShowAlbumArt) {
+        QSettings sets;
+        _showAlbumArt = sets.value("config/showAlbumArt", SHOW_ALBUM_ART).toBool();
+        _haveShowAlbumArt = true;
+    }
+    return _showAlbumArt;
+}
+bool AppSettings::setShowAlbumArt(bool showAlbumArt) {
+    QSettings sets;
+    sets.setValue("config/showAlbumArt", showAlbumArt);
+    _showAlbumArt = showAlbumArt;
+    _haveShowAlbumArt = true;
+    return _showAlbumArt;
 }
 bool AppSettings::getAlertOnClose() {
-    return ALERT_ON_CLOSE;
+    if (!_haveAlertOnClose) {
+        QSettings sets;
+        _alertOnClose = sets.value("config/alertOnClose", ALERT_ON_CLOSE).toBool();
+        _haveAlertOnClose = true;
+    }
+    return _alertOnClose;
 }
-Orientation AppSettings::setOrientation(Orientation orientation) {
+bool AppSettings::setAlertOnClose(bool alertOnClose) {
     QSettings sets;
-    sets.setValue("config/orientation", (int)orientation);
-    return orientation;
+    sets.setValue("config/alertOnClose", alertOnClose);
+    _alertOnClose = alertOnClose;
+    _haveAlertOnClose = true;
+    return _alertOnClose;
 }
 Orientation AppSettings::getOrientation() {
+    if (!_haveOrientation) {
+        QSettings sets;
+        return (Orientation)(sets.value("config/orientation", AUTO_ROTATE).toInt());
+        _haveOrientation = true;
+    }
+    return _orientation;
+}
+Orientation AppSettings::setOrientation(Orientation orientation) {
     QSettings sets;
-    return (Orientation)(sets.value("config/orientation", AUTO_ROTATE).toInt());
+    sets.setValue("config/orientation", (int)orientation);
+    _orientation = orientation;
+    _haveOrientation = true;
+    return _orientation;
 }
 
index 47ff5a0..8280a18 100644 (file)
@@ -63,26 +63,59 @@ public:
     explicit AppSettings();
     ~AppSettings();
     static QString getCurrentKey();
-    static void setCurrentKey(QString key);
+    static QString setCurrentKey(QString);
     static QString getCurrentIp();
-    static void setCurrentIp(QString ip);
+    static QString setCurrentIp(QString);
     static QStringList getAllAccounts();
     static VlcDirectory getHomeDirectory();
     static QList<VlcDirectory>* getFavourites();
-    static bool addFavourite(VlcDirectory dir);
-    static bool deleteFavourite(VlcDirectory dir);
-    static bool setHomeDirectory(VlcDirectory dir);
-    static Orientation setOrientation(Orientation orientation);
-    static Orientation getOrientation();
+    static bool addFavourite(VlcDirectory);
+    static bool deleteFavourite(VlcDirectory);
+    static bool setHomeDirectory(VlcDirectory);
     static bool isConnected();
     static int getStatusPollTimeout();
+    static int setStatusPollTimeout(int);
     static int getPingTimeout();
+    static int setPingTimeout(int);
     static int getConnectionTimeout();
+    static int setConnectionTimeout(int);
     static int getRetrieveArtTimeout();
+    static int setRetrieveArtTimeout(int);
     static int getRetryNetworkTimeout();
+    static int setRetryNetworkTimeout(int);
     static bool getShowUnknownFileTypes();
+    static bool setShowUnknownFileTypes(bool);
     static bool getShowAlbumArt();
+    static bool setShowAlbumArt(bool);
     static bool getAlertOnClose();
+    static bool setAlertOnClose(bool);
+    static Orientation setOrientation(Orientation);
+    static Orientation getOrientation();
+
+private:
+    static bool _haveCurrentKey;
+    static QString _currentKey;
+    static bool _haveCurrentIp;
+    static QString _currentIp;
+    static bool _haveConnectionTimeout;
+    static int _connectionTimeout;
+    static bool _havePingTimeout;
+    static int _pingTimeout;
+    static bool _haveStatusPollTimeout;
+    static int _statusPollTimeout;
+    static bool _haveRetrieveArtTimeout;
+    static int _retrieveArtTimeout;
+    static bool _haveRetryNetworkTimeout;
+    static int _retryNetworkTimeout;
+    static bool _haveShowUnknownFileTypes;
+    static bool _showUnknownFileTypes;
+    static bool _haveShowAlbumArt;
+    static bool _showAlbumArt;
+    static bool _haveAlertOnClose;
+    static bool _alertOnClose;
+    static bool _haveOrientation;
+    static Orientation _orientation;
+
 
 };
 
index 3a3acd3..afcac00 100644 (file)
@@ -19,6 +19,7 @@
 #include <QFormLayout>
 #include <QDebug>
 #include <QSettings>
+#include "appsettings.h"
 //#include <QMaemo5InformationBox>
 
 NewAccountDialog::NewAccountDialog(QWidget *parent)
@@ -55,6 +56,7 @@ void NewAccountDialog::save()
     QString myIp = mIpLineEdit->text();
     QString myPort = mPortLineEdit->text();
 
+    /// TODO create account through appsettings
     QSettings settings;
     settings.beginGroup("account");
     if (!mEditKey.isEmpty()) {