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