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"
35 QMap <Eniro::Site, Eniro::SiteDetails> SettingsDialog::sites_;
37 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
38 usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0),
41 setWindowTitle(tr("Settings"));
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;
52 QLabel* usernameLabel = new QLabel(tr("Eniro username"));
53 usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username"));
55 QLabel* passwordLabel = new QLabel(tr("Eniro password"));
56 passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password"));
57 passwordInput_->setEchoMode(QLineEdit::PasswordEchoOnEdit);
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()));
65 siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
66 QString site = Settings::instance()->get("eniro_site");
71 sites_ = Eniro::getSites();
74 QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
75 for(it = sites_.begin(); it != sites_.end(); it++)
91 qDebug() << "Unknown site";
95 siteSelector_->addItem(name, it.value().id);
97 if(it.value().id == site)
99 siteSelector_->setCurrentIndex(i);
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);
111 connectionSelector_ = new ConnectionSelector(tr("Connect automatically on"), this);
112 QString selectedConnection = Settings::instance()->get("connection");
113 connectionSelector_->selectByValue(selectedConnection);
115 QPushButton* submitButton = new QPushButton(tr("Save"), this);
116 connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
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_);
130 daemon->addWidget(autostartSelector_);
131 daemon->addWidget(connectionSelector_);
133 QDialogButtonBox* buttons = new QDialogButtonBox;
134 buttons->setCenterButtons(false);
135 buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
137 QTabWidget* tabs = new QTabWidget;
139 QWidget* generalTab = new QWidget;
140 generalTab->setLayout(general);
142 QWidget* daemonTab = new QWidget;
143 daemonTab->setLayout(daemon);
145 tabs->addTab(generalTab, tr("General"));
146 tabs->addTab(daemonTab, tr("Daemon"));
148 mainLayout->addWidget(tabs);
149 mainLayout->addWidget(buttons);
151 setLayout(mainLayout);
156 void SettingsDialog::saveSettings()
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());
175 if(site != currentSite_ && Daemon::isRunning())
177 QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
184 void SettingsDialog::setVisible(bool visible)
186 QDialog::setVisible(visible);
190 currentSite_ = siteSelector_->value().toString();
195 void SettingsDialog::resetCache()
197 int ret = Cache::instance().clear();
201 QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret));