...
[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 #include "buttonselector.h"
35 #include "connectionselector.h"
36 #include "sourceguiconfig.h"
37
38 QList<Source::SourceDetails> SettingsDialog::sources_;
39
40 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
41 sourceConfig_(0), cacheInput_(0), sourceSelector_(0),
42 autostartSelector_(0), connectionSelector_(0), tabs_(0)
43 {
44     setWindowTitle(tr("Settings"));
45
46     QVBoxLayout* general = new QVBoxLayout;
47     QVBoxLayout* daemon = new QVBoxLayout;
48     QHBoxLayout* mainLayout = new QHBoxLayout;
49
50     QHBoxLayout* cache = new QHBoxLayout;
51
52     currentSource_ = Settings::instance()->get("source");
53     Source::SourceId sourceId = Source::stringToId(currentSource_);
54     sourceConfig_ = SourceGuiConfig::getGuiConfig(sourceId, this);
55
56     Q_ASSERT(sourceConfig_ != 0);
57
58     QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)"));
59     cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size"));
60     cacheInput_->setValidator(new QIntValidator(0, 10000, this));
61     QPushButton* cacheResetButton = new QPushButton(tr("Clear"), this);
62     connect(cacheResetButton, SIGNAL(pressed()), this, SLOT(resetCache()));
63
64     languageSelector_ = new ButtonSelector(tr("Language"), this);
65     languageSelector_->addItem(tr("Automatic"), "");
66
67     selectedLanguage_ = Settings::instance()->get("language");
68
69     QList<Settings::Language> langs;
70     Settings::getLanguages(langs);
71
72     for(int i = 0; i < langs.size(); i++)
73     {
74         languageSelector_->addItem(langs.at(i).name, langs.at(i).id);
75
76         if(langs.at(i).id == selectedLanguage_)
77         {
78             languageSelector_->setCurrentIndex(i + 1);
79         }
80     }
81
82     sourceSelector_ = new ButtonSelector(tr("Phonebook"), this);
83
84     if(sources_.isEmpty())
85     {
86         Source::getSources(sources_);
87     }
88
89     for(int i = 0; i < sources_.size(); i++)
90     {
91         sourceSelector_->addItem(sources_.at(i).name, sources_.at(i).id);
92
93         if(sources_.at(i).id == currentSource_)
94         {
95             sourceSelector_->setCurrentIndex(i);
96         }
97     }
98
99     connect(sourceSelector_, SIGNAL(selected(unsigned int, QString const&, QVariant const&)),
100             this, SLOT(onSourceSelected(unsigned int, QString const&, QVariant const&)));
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     connectionSelector_ = new ConnectionSelector(tr("Connect automatically on"), this);
109     QString selectedConnection = Settings::instance()->get("connection");
110
111     if(connectionSelector_->selectByValue(selectedConnection) &&
112        selectedConnection == "gprs")
113     {
114         connectionSelector_->updateConnections();
115     }
116
117     QPushButton* submitButton = new QPushButton(tr("Save"), this);
118     connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
119
120     cache->addWidget(cacheLabel);
121     cache->addWidget(cacheInput_);
122     cache->addWidget(cacheResetButton);
123     general->addLayout(cache);
124     general->addWidget(languageSelector_);
125     general->addWidget(sourceSelector_);
126
127     daemon->addWidget(autostartSelector_);
128     daemon->addWidget(connectionSelector_);
129
130     QDialogButtonBox* buttons = new QDialogButtonBox;
131     buttons->setCenterButtons(false);
132     buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
133
134     tabs_ = new QTabWidget;
135
136     QWidget* generalTab = new QWidget;
137     generalTab->setLayout(general);
138
139     QWidget* daemonTab = new QWidget;
140     daemonTab->setLayout(daemon);
141
142     tabs_->addTab(generalTab, tr("General"));
143     tabs_->addTab(daemonTab, tr("Daemon"));
144     tabs_->addTab(sourceConfig_, tr("Phonebook settings"));
145
146     mainLayout->addWidget(tabs_);
147     mainLayout->addWidget(buttons);
148
149     setLayout(mainLayout);
150
151 }
152
153 void SettingsDialog::saveSettings()
154 {
155     hide();
156
157     Settings::instance()->startEdit();
158
159     Settings::instance()->set("cache_size", cacheInput_->text());
160     QString source = sourceSelector_->value().toString();
161     Settings::instance()->set("source", source);
162     QString autostart = autostartSelector_->value().toString();
163     Settings::instance()->set("autostart", autostart);
164     QString connection = connectionSelector_->value().toString();
165     Settings::instance()->set("connection", connection);
166     QString language = languageSelector_->value().toString();
167     Settings::instance()->set("language", language);
168     sourceConfig_->save();
169
170     Settings::instance()->endEdit();
171
172     bool infoboxShown = false;
173
174     if(language != selectedLanguage_)
175     {
176         QMaemo5InformationBox::information(this, tr("You need to restart Jenirok for language change to take effect."));
177         selectedLanguage_ = language;
178         infoboxShown = true;
179     }
180
181     if(Daemon::isRunning())
182     {
183         if(!infoboxShown)
184         {
185             QMaemo5InformationBox::information(this, tr("Restarting daemon..."), 1500);
186         }
187
188         Daemon::restart();
189     }
190
191     emit saved();
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 }
204
205 void SettingsDialog::onSourceSelected(unsigned int index,
206                                       QString const& text,
207                                       QVariant const& value)
208 {
209     Q_UNUSED(index);
210     Q_UNUSED(text);
211
212     QString source = value.toString();
213
214     if(source != currentSource_)
215     {
216         int tabId = tabs_->indexOf(sourceConfig_);
217
218         if(tabId >= 0)
219         {
220             tabs_->removeTab(tabId);
221         }
222
223         delete sourceConfig_;
224         Source::SourceId sourceId = Source::stringToId(value.toString());
225         sourceConfig_ = SourceGuiConfig::getGuiConfig(sourceId, this);
226         Q_ASSERT(sourceConfig_ != 0);
227         tabs_->addTab(sourceConfig_, tr("Phonebook settings"));
228         currentSource_ = source;
229     }
230 }