Connection handling in daemon improved. Added settings to allow automatic connection...
[jenirok] / src / gui / settingsdialog.cpp
index 25ba29d..f9dd623 100644 (file)
@@ -21,6 +21,8 @@
 #include <QtGui/QVBoxLayout>
 #include <QtGui/QHBoxLayout>
 #include <QtGui/QIntValidator>
+#include <QtGui/QDialogButtonBox>
+#include <QtGui/QTabWidget>
 #include <QMaemo5ValueButton>
 #include <QMaemo5InformationBox>
 #include <QDebug>
 #include "settings.h"
 #include "db.h"
 #include "daemon.h"
+#include "cache.h"
 
-QMap <Eniro::Site, Eniro::SiteDetails> SettingsDialog::sites_ = Eniro::getSites();
+QMap <Eniro::Site, Eniro::SiteDetails> SettingsDialog::sites_;
 
 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
-usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0), autostartSelector_(0)
+usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0),
+autostartSelector_(0)
 {
-       setWindowTitle(tr("Settings"));
-
-       DB::connect();
-
-       QVBoxLayout* left = new QVBoxLayout;
-       QHBoxLayout* mainLayout = new QHBoxLayout;
-       QHBoxLayout* username = new QHBoxLayout;
-       QHBoxLayout* password = new QHBoxLayout;
-       QHBoxLayout* cache = new QHBoxLayout;
-
-       QLabel* usernameLabel = new QLabel(tr("Eniro username"));
-       usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username"));
-
-       QLabel* passwordLabel = new QLabel(tr("Eniro password"));
-       passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password"));
-
-       QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)"));
-       cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size"));
-       cacheInput_->setValidator(new QIntValidator(0, 10000, this));
-
-       siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
-       QString site = Settings::instance()->get("eniro_site");
-       int i = 0;
-       QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
-       for(it = sites_.begin(); it != sites_.end(); it++)
-       {
-               siteSelector_->addItem(it.value().name, it.value().id);
-
-               if(it.value().id == site)
-               {
-                       siteSelector_->setCurrentIndex(i);
-               }
-
-               i++;
-       }
-
-       autostartSelector_ = new ButtonSelector(tr("Autostart"), this);
-       QString autostart = Settings::instance()->get("autostart");
-       autostartSelector_->addItem(tr("Enabled"), "1");
-       autostartSelector_->addItem(tr("Disabled"), "0");
-       autostartSelector_->setCurrentIndex(autostart == "1" ? 0 : 1);
-
-       QPushButton* submitButton = new QPushButton(tr("Save"));
-       connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
-
-       username->addWidget(usernameLabel);
-       username->addWidget(usernameInput_);
-       password->addWidget(passwordLabel);
-       password->addWidget(passwordInput_);
-       cache->addWidget(cacheLabel);
-       cache->addWidget(cacheInput_);
-       left->addLayout(username);
-       left->addLayout(password);
-       left->addLayout(cache);
-       left->addWidget(siteSelector_);
-       left->addWidget(autostartSelector_);
-
-       mainLayout->addLayout(left);
-       mainLayout->addWidget(submitButton);
-
-       setLayout(mainLayout);
-
-       DB::disconnect();
+    setWindowTitle(tr("Settings"));
+
+    DB::connect();
+
+    QVBoxLayout* general = new QVBoxLayout;
+    QVBoxLayout* daemon = new QVBoxLayout;
+    QHBoxLayout* mainLayout = new QHBoxLayout;
+    QHBoxLayout* username = new QHBoxLayout;
+    QHBoxLayout* password = new QHBoxLayout;
+    QHBoxLayout* cache = new QHBoxLayout;
+
+    QLabel* usernameLabel = new QLabel(tr("Eniro username"));
+    usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username"));
+
+    QLabel* passwordLabel = new QLabel(tr("Eniro password"));
+    passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password"));
+    passwordInput_->setEchoMode(QLineEdit::PasswordEchoOnEdit);
+
+    QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)"));
+    cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size"));
+    cacheInput_->setValidator(new QIntValidator(0, 10000, this));
+    QPushButton* cacheResetButton = new QPushButton(tr("Clear"), this);
+    connect(cacheResetButton, SIGNAL(pressed()), this, SLOT(resetCache()));
+
+    siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
+    QString site = Settings::instance()->get("eniro_site");
+    int i = 0;
+
+    if(sites_.empty())
+    {
+       sites_ = Eniro::getSites();
+    }
+
+    QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
+    for(it = sites_.begin(); it != sites_.end(); it++)
+    {
+        QString name;
+
+        switch(it.key())
+        {
+        case Eniro::FI:
+            name = tr("Finnish");
+            break;
+        case Eniro::SE:
+            name = tr("Swedish");
+            break;
+        case Eniro::DK:
+            name = tr("Danish");
+            break;
+        default:
+            qDebug() << "Unknown site";
+            continue;
+
+        }
+        siteSelector_->addItem(name, it.value().id);
+
+        if(it.value().id == site)
+        {
+            siteSelector_->setCurrentIndex(i);
+        }
+
+        i++;
+    }
+
+    autostartSelector_ = new ButtonSelector(tr("Autostart"), this);
+    QString autostart = Settings::instance()->get("autostart");
+    autostartSelector_->addItem(tr("Enabled"), "1");
+    autostartSelector_->addItem(tr("Disabled"), "0");
+    autostartSelector_->setCurrentIndex(autostart == "1" ? 0 : 1);
+
+    autoconnectCheck_ = new QCheckBox(tr("Allow daemon to connect automatically"));
+    QString autoconnect = Settings::instance()->get("autoconnect");
+    autoconnectCheck_->setChecked(autoconnect == "1");
+
+    connectionSelector_ = new ConnectionSelector(tr("Connection to use"), this);
+    QString selectedConnection = Settings::instance()->get("connection");
+
+    if(selectedConnection != "0")
+    {
+        connectionSelector_->addItem(Settings::instance()->get("connection_name"), selectedConnection);
+        connectionSelector_->selectByValue(selectedConnection);
+    }
+
+    QPushButton* submitButton = new QPushButton(tr("Save"), this);
+    connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
+
+    username->addWidget(usernameLabel);
+    username->addWidget(usernameInput_);
+    password->addWidget(passwordLabel);
+    password->addWidget(passwordInput_);
+    cache->addWidget(cacheLabel);
+    cache->addWidget(cacheInput_);
+    cache->addWidget(cacheResetButton);
+    general->addLayout(username);
+    general->addLayout(password);
+    general->addLayout(cache);
+    general->addWidget(siteSelector_);
+
+    daemon->addWidget(autostartSelector_);
+    daemon->addWidget(autoconnectCheck_);
+    daemon->addWidget(connectionSelector_);
+
+    QDialogButtonBox* buttons = new QDialogButtonBox;
+    buttons->setCenterButtons(false);
+    buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
+
+    QTabWidget* tabs = new QTabWidget;
+
+    QWidget* generalTab = new QWidget;
+    generalTab->setLayout(general);
+
+    QWidget* daemonTab = new QWidget;
+    daemonTab->setLayout(daemon);
+
+    tabs->addTab(generalTab, tr("General"));
+    tabs->addTab(daemonTab, tr("Daemon"));
+
+    mainLayout->addWidget(tabs);
+    mainLayout->addWidget(buttons);
+
+    setLayout(mainLayout);
+
+    DB::disconnect();
 }
 
 void SettingsDialog::saveSettings()
 {
-       DB::connect();
+    hide();
+
+    DB::connect();
+
+    Settings::instance()->set("eniro_username", usernameInput_->text());
+    Settings::instance()->set("eniro_password", passwordInput_->text());
+    Settings::instance()->set("cache_size", cacheInput_->text());
+    QString site = siteSelector_->value().toString();
+    Settings::instance()->set("site", site);
+    QString autostart = autostartSelector_->value().toString();
+    Settings::instance()->set("autostart", autostart);
+    bool autoconnect = autoconnectCheck_->isChecked();
+    Settings::instance()->set("autoconnect", autoconnect ? "1" : "0");
+    QString connection = connectionSelector_->value().toString();
+    Settings::instance()->set("connection", connection);
+    Settings::instance()->set("connection_name", connectionSelector_->text());
+
+    DB::disconnect();
+
+    if((site != currentSite_ ||
+        autoconnect != currentAutoconnect_ ||
+        connection != currentConnection_) && Daemon::isRunning())
+    {
+        QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
+        Daemon::restart();
+        currentSite_ = site;
+    }
 
-       Settings::instance()->set("eniro_username", usernameInput_->text());
-       Settings::instance()->set("eniro_password", passwordInput_->text());
-       Settings::instance()->set("cache_size", cacheInput_->text());
-       QString site = siteSelector_->value().toString();
-       Settings::instance()->set("site", site);
-       QString autostart = autostartSelector_->value().toString();
-       Settings::instance()->set("autostart", autostart);
-
-       DB::disconnect();
-
-       hide();
-
-       if(site != currentSite_ && Daemon::isRunning())
-       {
-               QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
-               Daemon::restart();
-               currentSite_ = site;
-       }
-
-       if(autostart != currentAutostart_)
-       {
-               bool value = false;
+}
 
-               if(autostart == "1")
-               {
-                       value = true;
-               }
+void SettingsDialog::setVisible(bool visible)
+{
+    QDialog::setVisible(visible);
 
-               Daemon::setAutostart(value);
-       }
+    if(visible)
+    {
+        currentSite_ = siteSelector_->value().toString();
+        currentConnection_ = connectionSelector_->value().toString();
+        currentAutoconnect_ = autoconnectCheck_->isChecked();
+    }
 
 }
 
-void SettingsDialog::setVisible(bool visible)
+void SettingsDialog::resetCache()
 {
-       QDialog::setVisible(visible);
-
-       if(visible)
-       {
-               currentSite_ = siteSelector_->value().toString();
-               currentAutostart_ = autostartSelector_->value().toString();
-       }
+    int ret = Cache::instance().clear();
 
+    if(ret >= 0)
+    {
+        QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret));
+    }
 }