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