First commit
[jenirok] / src / gui / settingsdialog.cpp
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  */
18
19 #include <QtGui/QLabel>
20 #include <QtGui/QPushButton>
21 #include <QtGui/QVBoxLayout>
22 #include <QtGui/QHBoxLayout>
23 #include <QtGui/QIntValidator>
24 #include <QMaemo5ValueButton>
25 #include <QMaemo5InformationBox>
26 #include <QDebug>
27 #include "settingsdialog.h"
28 #include "settings.h"
29 #include "db.h"
30 #include "daemon.h"
31
32 QMap <Eniro::Site, Eniro::SiteDetails> SettingsDialog::sites_ = Eniro::getSites();
33
34 SettingsDialog::SettingsDialog(QWidget* parent): QDialog(parent),
35 usernameInput_(0), passwordInput_(0), cacheInput_(0), siteSelector_(0), autostartSelector_(0)
36 {
37         setWindowTitle(tr("Settings"));
38
39         DB::connect();
40
41         QVBoxLayout* left = new QVBoxLayout;
42         QHBoxLayout* mainLayout = new QHBoxLayout;
43         QHBoxLayout* username = new QHBoxLayout;
44         QHBoxLayout* password = new QHBoxLayout;
45         QHBoxLayout* cache = new QHBoxLayout;
46
47         QLabel* usernameLabel = new QLabel(tr("Eniro username"));
48         usernameInput_ = new QLineEdit(Settings::instance()->get("eniro_username"));
49
50         QLabel* passwordLabel = new QLabel(tr("Eniro password"));
51         passwordInput_ = new QLineEdit(Settings::instance()->get("eniro_password"));
52
53         QLabel* cacheLabel = new QLabel(tr("Cache size (numbers)"));
54         cacheInput_ = new QLineEdit(Settings::instance()->get("cache_size"));
55         cacheInput_->setValidator(new QIntValidator(0, 10000, this));
56
57         siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
58         QString site = Settings::instance()->get("eniro_site");
59         int i = 0;
60         QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
61         for(it = sites_.begin(); it != sites_.end(); it++)
62         {
63                 siteSelector_->addItem(it.value().name, it.value().id);
64
65                 if(it.value().id == site)
66                 {
67                         siteSelector_->setCurrentIndex(i);
68                 }
69
70                 i++;
71         }
72
73         autostartSelector_ = new ButtonSelector(tr("Autostart"), this);
74         QString autostart = Settings::instance()->get("autostart");
75         autostartSelector_->addItem(tr("Enabled"), "1");
76         autostartSelector_->addItem(tr("Disabled"), "0");
77         autostartSelector_->setCurrentIndex(autostart == "1" ? 0 : 1);
78
79         QPushButton* submitButton = new QPushButton(tr("Save"));
80         connect(submitButton, SIGNAL(pressed()), this, SLOT(saveSettings()));
81
82         username->addWidget(usernameLabel);
83         username->addWidget(usernameInput_);
84         password->addWidget(passwordLabel);
85         password->addWidget(passwordInput_);
86         cache->addWidget(cacheLabel);
87         cache->addWidget(cacheInput_);
88         left->addLayout(username);
89         left->addLayout(password);
90         left->addLayout(cache);
91         left->addWidget(siteSelector_);
92         left->addWidget(autostartSelector_);
93
94         mainLayout->addLayout(left);
95         mainLayout->addWidget(submitButton);
96
97         setLayout(mainLayout);
98
99         DB::disconnect();
100 }
101
102 void SettingsDialog::saveSettings()
103 {
104         DB::connect();
105
106         Settings::instance()->set("eniro_username", usernameInput_->text());
107         Settings::instance()->set("eniro_password", passwordInput_->text());
108         Settings::instance()->set("cache_size", cacheInput_->text());
109         QString site = siteSelector_->value().toString();
110         Settings::instance()->set("site", site);
111         QString autostart = autostartSelector_->value().toString();
112         Settings::instance()->set("autostart", autostart);
113
114         DB::disconnect();
115
116         hide();
117
118         if(site != currentSite_ && Daemon::isRunning())
119         {
120                 QMaemo5InformationBox::information(this, tr("Restarting daemon..."));
121                 Daemon::restart();
122                 currentSite_ = site;
123         }
124
125         if(autostart != currentAutostart_)
126         {
127                 bool value = false;
128
129                 if(autostart == "1")
130                 {
131                         value = true;
132                 }
133
134                 Daemon::setAutostart(value);
135         }
136
137 }
138
139 void SettingsDialog::setVisible(bool visible)
140 {
141         QDialog::setVisible(visible);
142
143         if(visible)
144         {
145                 currentSite_ = siteSelector_->value().toString();
146                 currentAutostart_ = autostartSelector_->value().toString();
147         }
148
149 }