Unnecessary includes removed.
[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 <QtCore/QDebug>
20 #include <QtGui/QLabel>
21 #include <QtGui/QPushButton>
22 #include <QtGui/QVBoxLayout>
23 #include <QtGui/QHBoxLayout>
24 #include <QtGui/QIntValidator>
25 #include <QtGui/QDialogButtonBox>
26 #include <QtGui/QTabWidget>
27 #include <QMaemo5ValueButton>
28 #include <QMaemo5InformationBox>
29 #include "settingsdialog.h"
30 #include "settings.h"
31 #include "db.h"
32 #include "daemon.h"
33 #include "cache.h"
34
35 QMap <Eniro::Site, Eniro::SiteDetails> SettingsDialog::sites_;
36
37 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
38 usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0),
39 autostartSelector_(0)
40 {
41     setWindowTitle(tr("Settings"));
42
43     DB::connect();
44
45     QVBoxLayout* general = new QVBoxLayout;
46     QVBoxLayout* daemon = new QVBoxLayout;
47     QHBoxLayout* mainLayout = new QHBoxLayout;
48     QHBoxLayout* username = new QHBoxLayout;
49     QHBoxLayout* password = new QHBoxLayout;
50     QHBoxLayout* cache = new QHBoxLayout;
51
52     QLabel* usernameLabel = new QLabel(tr("Eniro username"));
53     usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username"));
54
55     QLabel* passwordLabel = new QLabel(tr("Eniro password"));
56     passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password"));
57     passwordInput_->setEchoMode(QLineEdit::PasswordEchoOnEdit);
58
59     QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)"));
60     cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size"));
61     cacheInput_->setValidator(new QIntValidator(0, 10000, this));
62     QPushButton* cacheResetButton = new QPushButton(tr("Clear"), this);
63     connect(cacheResetButton, SIGNAL(pressed()), this, SLOT(resetCache()));
64
65     siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
66     QString site = Settings::instance()->get("eniro_site");
67     int i = 0;
68
69     if(sites_.empty())
70     {
71        sites_ = Eniro::getSites();
72     }
73
74     QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
75     for(it = sites_.begin(); it != sites_.end(); it++)
76     {
77         QString name;
78
79         switch(it.key())
80         {
81         case Eniro::FI:
82             name = tr("Finnish");
83             break;
84         case Eniro::SE:
85             name = tr("Swedish");
86             break;
87         case Eniro::DK:
88             name = tr("Danish");
89             break;
90         default:
91             qDebug() << "Unknown site";
92             continue;
93
94         }
95         siteSelector_->addItem(name, it.value().id);
96
97         if(it.value().id == site)
98         {
99             siteSelector_->setCurrentIndex(i);
100         }
101
102         i++;
103     }
104
105     autostartSelector_ = new ButtonSelector(tr("Autostart"), this);
106     QString autostart = Settings::instance()->get("autostart");
107     autostartSelector_->addItem(tr("Enabled"), "1");
108     autostartSelector_->addItem(tr("Disabled"), "0");
109     autostartSelector_->setCurrentIndex(autostart == "1" ? 0 : 1);
110
111     connectionSelector_ = new ConnectionSelector(tr("Connect automatically on"), this);
112     QString selectedConnection = Settings::instance()->get("connection");
113     connectionSelector_->selectByValue(selectedConnection);
114
115     QPushButton* submitButton = new QPushButton(tr("Save"), this);
116     connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
117
118     username->addWidget(usernameLabel);
119     username->addWidget(usernameInput_);
120     password->addWidget(passwordLabel);
121     password->addWidget(passwordInput_);
122     cache->addWidget(cacheLabel);
123     cache->addWidget(cacheInput_);
124     cache->addWidget(cacheResetButton);
125     general->addLayout(username);
126     general->addLayout(password);
127     general->addLayout(cache);
128     general->addWidget(siteSelector_);
129
130     daemon->addWidget(autostartSelector_);
131     daemon->addWidget(connectionSelector_);
132
133     QDialogButtonBox* buttons = new QDialogButtonBox;
134     buttons->setCenterButtons(false);
135     buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
136
137     QTabWidget* tabs = new QTabWidget;
138
139     QWidget* generalTab = new QWidget;
140     generalTab->setLayout(general);
141
142     QWidget* daemonTab = new QWidget;
143     daemonTab->setLayout(daemon);
144
145     tabs->addTab(generalTab, tr("General"));
146     tabs->addTab(daemonTab, tr("Daemon"));
147
148     mainLayout->addWidget(tabs);
149     mainLayout->addWidget(buttons);
150
151     setLayout(mainLayout);
152
153     DB::disconnect();
154 }
155
156 void SettingsDialog::saveSettings()
157 {
158     hide();
159
160     DB::connect();
161
162     Settings::instance()->set("eniro_username", usernameInput_->text());
163     Settings::instance()->set("eniro_password", passwordInput_->text());
164     Settings::instance()->set("cache_size", cacheInput_->text());
165     QString site = siteSelector_->value().toString();
166     Settings::instance()->set("site", site);
167     QString autostart = autostartSelector_->value().toString();
168     Settings::instance()->set("autostart", autostart);
169     QString connection = connectionSelector_->value().toString();
170     Settings::instance()->set("connection", connection);
171     Settings::instance()->set("connection_name", connectionSelector_->text());
172
173     DB::disconnect();
174
175     if(site != currentSite_ && Daemon::isRunning())
176     {
177         QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
178         Daemon::restart();
179         currentSite_ = site;
180     }
181
182 }
183
184 void SettingsDialog::setVisible(bool visible)
185 {
186     QDialog::setVisible(visible);
187
188     if(visible)
189     {
190         currentSite_ = siteSelector_->value().toString();
191     }
192
193 }
194
195 void SettingsDialog::resetCache()
196 {
197     int ret = Cache::instance().clear();
198
199     if(ret >= 0)
200     {
201         QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret));
202     }
203 }