757a0ffad5bde906f28e3e22bf198d886670a4da
[jenirok] / src / gui / settingsdialog.cpp
1 /*
2  * This file is part of Jenirok.
3  *
4  * Jenirok is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Jenirok is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
16  *
17  */
18
19 #include <QtGui/QLabel>
20 #include <QtGui/QPushButton>
21 #include <QtGui/QVBoxLayout>
22 #include <QtGui/QHBoxLayout>
23 #include <QtGui/QIntValidator>
24 #include <QMaemo5ValueButton>
25 #include <QMaemo5InformationBox>
26 #include <QDebug>
27 #include "settingsdialog.h"
28 #include "settings.h"
29 #include "db.h"
30 #include "daemon.h"
31
32 QMap <Eniro::Site, Eniro::SiteDetails> SettingsDialog::sites_ = Eniro::getSites();
33
34 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
35 usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0),
36 autostartSelector_(0)
37 {
38     setWindowTitle(tr("Settings"));
39
40     DB::connect();
41
42     QVBoxLayout* left = new QVBoxLayout;
43     QHBoxLayout* mainLayout = new QHBoxLayout;
44     QHBoxLayout* username = new QHBoxLayout;
45     QHBoxLayout* password = new QHBoxLayout;
46     QHBoxLayout* cache = new QHBoxLayout;
47
48     QLabel* usernameLabel = new QLabel(tr("Eniro username"));
49     usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username"));
50
51     QLabel* passwordLabel = new QLabel(tr("Eniro password"));
52     passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password"));
53
54     QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)"));
55     cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size"));
56     cacheInput_->setValidator(new QIntValidator(0, 10000, this));
57
58     siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
59     QString site = Settings::instance()->get("eniro_site");
60     int i = 0;
61     QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
62     for(it = sites_.begin(); it != sites_.end(); it++)
63     {
64         QString name;
65
66         switch(it.key())
67         {
68         case Eniro::FI:
69             name = tr("Finnish");
70             break;
71         case Eniro::SE:
72             name = tr("Swedish");
73             break;
74         case Eniro::DK:
75             name = tr("Danish");
76             break;
77         default:
78             qDebug() << "Unknown site";
79             continue;
80
81         }
82         siteSelector_->addItem(name, it.value().id);
83
84         if(it.value().id == site)
85         {
86             siteSelector_->setCurrentIndex(i);
87         }
88
89         i++;
90     }
91
92     autostartSelector_ = new ButtonSelector(tr("Autostart"), this);
93     QString autostart = Settings::instance()->get("autostart");
94     autostartSelector_->addItem(tr("Enabled"), "1");
95     autostartSelector_->addItem(tr("Disabled"), "0");
96     autostartSelector_->setCurrentIndex(autostart == "1" ? 0 : 1);
97
98     QPushButton* submitButton = new QPushButton(tr("Save"));
99     connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
100
101     username->addWidget(usernameLabel);
102     username->addWidget(usernameInput_);
103     password->addWidget(passwordLabel);
104     password->addWidget(passwordInput_);
105     cache->addWidget(cacheLabel);
106     cache->addWidget(cacheInput_);
107     left->addLayout(username);
108     left->addLayout(password);
109     left->addLayout(cache);
110     left->addWidget(siteSelector_);
111     left->addWidget(autostartSelector_);
112
113     mainLayout->addLayout(left);
114     mainLayout->addWidget(submitButton);
115
116     setLayout(mainLayout);
117
118     DB::disconnect();
119 }
120
121 void SettingsDialog::saveSettings()
122 {
123     DB::connect();
124
125     Settings::instance()->set("eniro_username", usernameInput_->text());
126     Settings::instance()->set("eniro_password", passwordInput_->text());
127     Settings::instance()->set("cache_size", cacheInput_->text());
128     QString site = siteSelector_->value().toString();
129     Settings::instance()->set("site", site);
130     QString autostart = autostartSelector_->value().toString();
131     Settings::instance()->set("autostart", autostart);
132
133     DB::disconnect();
134
135     hide();
136
137     if(site != currentSite_ && Daemon::isRunning())
138     {
139         QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
140         Daemon::restart();
141         currentSite_ = site;
142     }
143
144     if(autostart != currentAutostart_)
145     {
146         bool value = false;
147
148         if(autostart == "1")
149         {
150             value = true;
151         }
152
153         Daemon::setAutostart(value);
154     }
155
156 }
157
158 void SettingsDialog::setVisible(bool visible)
159 {
160     QDialog::setVisible(visible);
161
162     if(visible)
163     {
164         currentSite_ = siteSelector_->value().toString();
165         currentAutostart_ = autostartSelector_->value().toString();
166     }
167
168 }