Danish Eniro search fixed.
[jenirok] / src / gui / eniroguiconfig.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 <QtCore/QDebug>
20 #include <QtGui/QVBoxLayout>
21 #include <QtGui/QHBoxLayout>
22 #include <QtGui/QLabel>
23 #include "eniroguiconfig.h"
24 #include "eniro.h"
25
26 EniroGuiConfig::EniroGuiConfig(QWidget* parent):
27 SourceGuiConfig(parent), EniroCoreConfig(), layout_(0), usernameInput_(0),
28 passwordInput_(0), siteSelector_(0), usernameLabel_(0), passwordLabel_(0)
29 {
30     load();
31     loadLayout(getSite());
32 }
33
34 EniroGuiConfig::~EniroGuiConfig()
35 {
36 }
37
38 void EniroGuiConfig::save()
39 {
40     if(usernameInput_ && passwordInput_)
41     {
42         setUsername(usernameInput_->text());
43         setPassword(passwordInput_->text());
44     }
45
46     setSite(siteSelector_->value().toString());
47
48     store();
49 }
50
51 void EniroGuiConfig::loadLayout(QString const& site)
52 {
53     if(layout())
54     {
55         siteSelector_->hide();
56         siteSelector_ = 0;
57
58         if(usernameLabel_ && passwordLabel_)
59         {
60             usernameLabel_->hide();
61             usernameLabel_ = 0;
62             usernameInput_->hide();
63             usernameInput_ = 0;
64             passwordLabel_->hide();
65             passwordLabel_ = 0;
66             passwordInput_->hide();
67             passwordInput_ = 0;
68         }
69
70         delete layout();
71         layout_ = 0;
72
73     }
74
75     layout_ = new QVBoxLayout;
76
77     QMap <Eniro::Site, Eniro::SiteDetails> sites = Eniro::getSites();
78
79     siteSelector_ = new ButtonSelector(tr("Eniro site"), this);
80     int i = 0;
81
82     QMap <Eniro::Site, Eniro::SiteDetails>::const_iterator it;
83     for(it = sites.begin(); it != sites.end(); it++)
84     {
85         QString name;
86
87         switch(it.key())
88         {
89         case Eniro::FI:
90             name = tr("Finnish");
91             break;
92         case Eniro::SE:
93             name = tr("Swedish");
94             break;
95         case Eniro::DK:
96             name = tr("Danish");
97             break;
98         default:
99             qDebug() << "Unknown site";
100             continue;
101
102         }
103         siteSelector_->addItem(name, it.value().id);
104
105         if(it.value().id == site)
106         {
107             siteSelector_->setCurrentIndex(i);
108         }
109
110         i++;
111     }
112
113     connect(siteSelector_, SIGNAL(selected(unsigned int, QString const&, QVariant const&)),
114             this, SLOT(siteChanged(unsigned int, QString const&, QVariant const&)));
115
116     layout_->addWidget(siteSelector_);
117
118     if(site == "fi")
119     {
120         QHBoxLayout* username = new QHBoxLayout;
121         QHBoxLayout* password = new QHBoxLayout;
122
123         usernameLabel_ = new QLabel(tr("Eniro username"));
124         usernameInput_ = new QLineEdit(getUsername());
125
126         passwordLabel_ = new QLabel(tr("Eniro password"));
127         passwordInput_ = new QLineEdit(getPassword());
128         passwordInput_->setEchoMode(QLineEdit::PasswordEchoOnEdit);
129
130         username->addWidget(usernameLabel_);
131         username->addWidget(usernameInput_);
132         password->addWidget(passwordLabel_);
133         password->addWidget(passwordInput_);
134
135         layout_->addLayout(username);
136         layout_->addLayout(password);
137     }
138
139     setLayout(layout_);
140 }
141
142 void EniroGuiConfig::siteChanged(unsigned int index,
143                                  QString const& text,
144                                  QVariant const& value)
145 {
146     Q_UNUSED(index);
147     Q_UNUSED(text);
148     loadLayout(value.toString());
149 }