Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Jenirok
Summary Tracker SCM Files
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  */
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 <QtGui/QTabWidget>
26 #include <QMaemo5ValueButton>
27 #include <QMaemo5InformationBox>
28 #include <QDebug>
29 #include "settingsdialog.h"
30 #include "settings.h"
31 #include "db.h"
32 #include "daemon.h"
33 #include "cache.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),
39 autostartSelector_(0)
40 {
41     setWindowTitle(tr("Settings"));
43     DB::connect();
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");
67     int i = 0;
69     if(sites_.empty())
70     {
71        sites_ = Eniro::getSites();
72     }
74     QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
75     for(it = sites_.begin(); it != sites_.end(); it++)
76     {
77         QString name;
79         switch(it.key())
80         {
81         case Eniro::FI:
82             name = tr("Finnish");
83             break;
84         case Eniro::SE:
85             name = tr("Swedish");
86             break;
87         case Eniro::DK:
88             name = tr("Danish");
89             break;
90         default:
91             qDebug() << "Unknown site";
92             continue;
94         }
95         siteSelector_->addItem(name, it.value().id);
97         if(it.value().id == site)
98         {
99             siteSelector_->setCurrentIndex(i);
100         }
102         i++;
103     }
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);
153     DB::disconnect();
156 void SettingsDialog::saveSettings()
158     hide();
160     DB::connect();
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());
173     DB::disconnect();
175     if(site != currentSite_ && Daemon::isRunning())
176     {
177         QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
178         Daemon::restart();
179         currentSite_ = site;
180     }
184 void SettingsDialog::setVisible(bool visible)
186     QDialog::setVisible(visible);
188     if(visible)
189     {
190         currentSite_ = siteSelector_->value().toString();
191         currentConnection_ = connectionSelector_->value().toString();
192     }
196 void SettingsDialog::resetCache()
198     int ret = Cache::instance().clear();
200     if(ret >= 0)
201     {
202         QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret));
203     }

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment