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     autoconnectCheck_ = new QCheckBox(tr("Allow daemon to connect automatically"));
112     QString autoconnect = Settings::instance()->get("autoconnect");
113     autoconnectCheck_->setChecked(autoconnect == "1");
115     connectionSelector_ = new ConnectionSelector(tr("Connection to use"), this);
116     QString selectedConnection = Settings::instance()->get("connection");
118     if(selectedConnection != "0")
119     {
120         connectionSelector_->addItem(Settings::instance()->get("connection_name"), selectedConnection);
121         connectionSelector_->selectByValue(selectedConnection);
122     }
124     QPushButton* submitButton = new QPushButton(tr("Save"), this);
125     connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
127     username->addWidget(usernameLabel);
128     username->addWidget(usernameInput_);
129     password->addWidget(passwordLabel);
130     password->addWidget(passwordInput_);
131     cache->addWidget(cacheLabel);
132     cache->addWidget(cacheInput_);
133     cache->addWidget(cacheResetButton);
134     general->addLayout(username);
135     general->addLayout(password);
136     general->addLayout(cache);
137     general->addWidget(siteSelector_);
139     daemon->addWidget(autostartSelector_);
140     daemon->addWidget(autoconnectCheck_);
141     daemon->addWidget(connectionSelector_);
143     QDialogButtonBox* buttons = new QDialogButtonBox;
144     buttons->setCenterButtons(false);
145     buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
147     QTabWidget* tabs = new QTabWidget;
149     QWidget* generalTab = new QWidget;
150     generalTab->setLayout(general);
152     QWidget* daemonTab = new QWidget;
153     daemonTab->setLayout(daemon);
155     tabs->addTab(generalTab, tr("General"));
156     tabs->addTab(daemonTab, tr("Daemon"));
158     mainLayout->addWidget(tabs);
159     mainLayout->addWidget(buttons);
161     setLayout(mainLayout);
163     DB::disconnect();
166 void SettingsDialog::saveSettings()
168     hide();
170     DB::connect();
172     Settings::instance()->set("eniro_username", usernameInput_->text());
173     Settings::instance()->set("eniro_password", passwordInput_->text());
174     Settings::instance()->set("cache_size", cacheInput_->text());
175     QString site = siteSelector_->value().toString();
176     Settings::instance()->set("site", site);
177     QString autostart = autostartSelector_->value().toString();
178     Settings::instance()->set("autostart", autostart);
179     bool autoconnect = autoconnectCheck_->isChecked();
180     Settings::instance()->set("autoconnect", autoconnect ? "1" : "0");
181     QString connection = connectionSelector_->value().toString();
182     Settings::instance()->set("connection", connection);
183     Settings::instance()->set("connection_name", connectionSelector_->text());
185     DB::disconnect();
187     if((site != currentSite_ ||
188         autoconnect != currentAutoconnect_ ||
189         connection != currentConnection_) && Daemon::isRunning())
190     {
191         QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
192         Daemon::restart();
193         currentSite_ = site;
194     }
198 void SettingsDialog::setVisible(bool visible)
200     QDialog::setVisible(visible);
202     if(visible)
203     {
204         currentSite_ = siteSelector_->value().toString();
205         currentConnection_ = connectionSelector_->value().toString();
206         currentAutoconnect_ = autoconnectCheck_->isChecked();
207     }
211 void SettingsDialog::resetCache()
213     int ret = Cache::instance().clear();
215     if(ret >= 0)
216     {
217         QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret));
218     }

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment