2 * This file is part of Jenirok.
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.
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.
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/>.
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"
34 #include "buttonselector.h"
35 #include "connectionselector.h"
36 #include "sourceguiconfig.h"
38 QList<Source::SourceDetails> SettingsDialog::sources_;
40 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
41 sourceConfig_(0), cacheInput_(0), sourceSelector_(0),
42 autostartSelector_(0), connectionSelector_(0), tabs_(0)
44 setWindowTitle(tr("Settings"));
46 QVBoxLayout* general = new QVBoxLayout;
47 QVBoxLayout* daemon = new QVBoxLayout;
48 QHBoxLayout* mainLayout = new QHBoxLayout;
50 QHBoxLayout* cache = new QHBoxLayout;
52 currentSource_ = Settings::instance()->get("source");
53 Source::SourceId sourceId = Source::stringToId(currentSource_);
54 sourceConfig_ = SourceGuiConfig::getGuiConfig(sourceId, this);
56 Q_ASSERT(sourceConfig_ != 0);
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()));
64 languageSelector_ = new ButtonSelector(tr("Language"), this);
65 languageSelector_->addItem(tr("Automatic"), "");
67 selectedLanguage_ = Settings::instance()->get("language");
69 QList<Settings::Language> langs;
70 Settings::getLanguages(langs);
72 for(int i = 0; i < langs.size(); i++)
74 languageSelector_->addItem(langs.at(i).name, langs.at(i).id);
76 if(langs.at(i).id == selectedLanguage_)
78 languageSelector_->setCurrentIndex(i + 1);
82 sourceSelector_ = new ButtonSelector(tr("Phonebook"), this);
84 if(sources_.isEmpty())
86 Source::getSources(sources_);
89 for(int i = 0; i < sources_.size(); i++)
91 sourceSelector_->addItem(sources_.at(i).name, sources_.at(i).id);
93 if(sources_.at(i).id == currentSource_)
95 sourceSelector_->setCurrentIndex(i);
99 connect(sourceSelector_, SIGNAL(selected(unsigned int, QString const&, QVariant const&)),
100 this, SLOT(onSourceSelected(unsigned int, QString const&, QVariant const&)));
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);
108 connectionSelector_ = new ConnectionSelector(tr("Connect automatically on"), this);
109 QString selectedConnection = Settings::instance()->get("connection");
110 connectionSelector_->selectByValue(selectedConnection);
112 QPushButton* submitButton = new QPushButton(tr("Save"), this);
113 connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
115 cache->addWidget(cacheLabel);
116 cache->addWidget(cacheInput_);
117 cache->addWidget(cacheResetButton);
118 general->addLayout(cache);
119 general->addWidget(languageSelector_);
120 general->addWidget(sourceSelector_);
122 daemon->addWidget(autostartSelector_);
123 daemon->addWidget(connectionSelector_);
125 QDialogButtonBox* buttons = new QDialogButtonBox;
126 buttons->setCenterButtons(false);
127 buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
129 tabs_ = new QTabWidget;
131 QWidget* generalTab = new QWidget;
132 generalTab->setLayout(general);
134 QWidget* daemonTab = new QWidget;
135 daemonTab->setLayout(daemon);
137 tabs_->addTab(generalTab, tr("General"));
138 tabs_->addTab(daemonTab, tr("Daemon"));
139 tabs_->addTab(sourceConfig_, tr("Phonebook settings"));
141 mainLayout->addWidget(tabs_);
142 mainLayout->addWidget(buttons);
144 setLayout(mainLayout);
148 void SettingsDialog::saveSettings()
152 Settings::instance()->startEdit();
154 Settings::instance()->set("cache_size", cacheInput_->text());
155 QString source = sourceSelector_->value().toString();
156 Settings::instance()->set("source", source);
157 QString autostart = autostartSelector_->value().toString();
158 Settings::instance()->set("autostart", autostart);
159 QString connection = connectionSelector_->value().toString();
160 Settings::instance()->set("connection", connection);
161 Settings::instance()->set("connection_name", connectionSelector_->text());
162 QString language = languageSelector_->value().toString();
163 Settings::instance()->set("language", language);
164 sourceConfig_->save();
166 Settings::instance()->endEdit();
168 if(Daemon::isRunning())
170 QMaemo5InformationBox::information(this, tr("Restarting daemon..."), 1500);
174 if(language != selectedLanguage_)
176 QMaemo5InformationBox::information(this, tr("You need to restart Jenirok for language change to take effect."));
177 selectedLanguage_ = language;
182 void SettingsDialog::resetCache()
184 int ret = Cache::instance().clear();
188 QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret));
192 void SettingsDialog::onSourceSelected(unsigned int index,
194 QVariant const& value)
199 QString source = value.toString();
201 if(source != currentSource_)
203 int tabId = tabs_->indexOf(sourceConfig_);
207 tabs_->removeTab(tabId);
210 delete sourceConfig_;
211 Source::SourceId sourceId = Source::stringToId(value.toString());
212 sourceConfig_ = SourceGuiConfig::getGuiConfig(sourceId, this);
213 Q_ASSERT(sourceConfig_ != 0);
214 tabs_->addTab(sourceConfig_, tr("Phonebook settings"));
215 currentSource_ = source;