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 <QMaemo5ValueButton>
26 #include <QMaemo5InformationBox>
27 #include <QDebug>
28 #include "settingsdialog.h"
29 #include "settings.h"
30 #include "db.h"
31 #include "daemon.h"
32 #include "cache.h"
34 QMap <Eniro::Site, Eniro::SiteDetails> SettingsDialog::sites_ = Eniro::getSites();
36 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
37 usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0),
38 autostartSelector_(0)
39 {
40     setWindowTitle(tr("Settings"));
42     DB::connect();
44     QVBoxLayout* left = new QVBoxLayout;
45     QHBoxLayout* mainLayout = new QHBoxLayout;
46     QHBoxLayout* username = new QHBoxLayout;
47     QHBoxLayout* password = new QHBoxLayout;
48     QHBoxLayout* cache = new QHBoxLayout;
50     QLabel* usernameLabel = new QLabel(tr("Eniro username"));
51     usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username"));
53     QLabel* passwordLabel = new QLabel(tr("Eniro password"));
54     passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password"));
56     QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)"));
57     cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size"));
58     cacheInput_->setValidator(new QIntValidator(0, 10000, this));
59     QPushButton* cacheResetButton = new QPushButton(tr("Clear"), this);
60     connect(cacheResetButton, SIGNAL(pressed()), this, SLOT(resetCache()));
62     siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
63     QString site = Settings::instance()->get("eniro_site");
64     int i = 0;
65     QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
66     for(it = sites_.begin(); it != sites_.end(); it++)
67     {
68         QString name;
70         switch(it.key())
71         {
72         case Eniro::FI:
73             name = tr("Finnish");
74             break;
75         case Eniro::SE:
76             name = tr("Swedish");
77             break;
78         case Eniro::DK:
79             name = tr("Danish");
80             break;
81         default:
82             qDebug() << "Unknown site";
83             continue;
85         }
86         siteSelector_->addItem(name, it.value().id);
88         if(it.value().id == site)
89         {
90             siteSelector_->setCurrentIndex(i);
91         }
93         i++;
94     }
96     autostartSelector_ = new ButtonSelector(tr("Autostart"), this);
97     QString autostart = Settings::instance()->get("autostart");
98     autostartSelector_->addItem(tr("Enabled"), "1");
99     autostartSelector_->addItem(tr("Disabled"), "0");
100     autostartSelector_->setCurrentIndex(autostart == "1" ? 0 : 1);
102     QPushButton* submitButton = new QPushButton(tr("Save"), this);
103     connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
105     username->addWidget(usernameLabel);
106     username->addWidget(usernameInput_);
107     password->addWidget(passwordLabel);
108     password->addWidget(passwordInput_);
109     cache->addWidget(cacheLabel);
110     cache->addWidget(cacheInput_);
111     cache->addWidget(cacheResetButton);
112     left->addLayout(username);
113     left->addLayout(password);
114     left->addLayout(cache);
115     left->addWidget(siteSelector_);
116     left->addWidget(autostartSelector_);
118     QDialogButtonBox* buttons = new QDialogButtonBox;
119     buttons->setCenterButtons(false);
120     buttons->addButton(submitButton, QDialogButtonBox::AcceptRole);
122     mainLayout->addLayout(left);
123     mainLayout->addWidget(buttons);
125     setLayout(mainLayout);
127     DB::disconnect();
130 void SettingsDialog::saveSettings()
132     DB::connect();
134     Settings::instance()->set("eniro_username", usernameInput_->text());
135     Settings::instance()->set("eniro_password", passwordInput_->text());
136     Settings::instance()->set("cache_size", cacheInput_->text());
137     QString site = siteSelector_->value().toString();
138     Settings::instance()->set("site", site);
139     QString autostart = autostartSelector_->value().toString();
140     Settings::instance()->set("autostart", autostart);
142     DB::disconnect();
144     hide();
146     if(site != currentSite_ && Daemon::isRunning())
147     {
148         QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
149         Daemon::restart();
150         currentSite_ = site;
151     }
153     if(autostart != currentAutostart_)
154     {
155         bool value = false;
157         if(autostart == "1")
158         {
159             value = true;
160         }
162         Daemon::setAutostart(value);
163     }
167 void SettingsDialog::setVisible(bool visible)
169     QDialog::setVisible(visible);
171     if(visible)
172     {
173         currentSite_ = siteSelector_->value().toString();
174         currentAutostart_ = autostartSelector_->value().toString();
175     }
179 void SettingsDialog::resetCache()
181     int ret = Cache::instance().clear();
183     if(ret >= 0)
184     {
185         QMaemo5InformationBox::information(this, tr("%n number(s) were deleted from cache", "", ret));
186     }

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment