Architecture changed to allow easier addition of new phone books. Norwegian phonebook...
[jenirok] / src / common / enirocoreconfig.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 "enirocoreconfig.h"
21 #include "eniro.h"
22 #include "settings.h"
23
24 namespace
25 {
26     QString const COLUMN_PREFIX = "eniro_";
27     QString const USERNAME_COLUMN = COLUMN_PREFIX + "username";
28     QString const PASSWORD_COLUMN = COLUMN_PREFIX + "password";
29     QString const SITE_COLUMN = COLUMN_PREFIX + "site";
30 }
31
32 EniroCoreConfig::EniroCoreConfig(): SourceCoreConfig(), loaded_(false)
33 {
34 }
35
36 EniroCoreConfig::~EniroCoreConfig()
37 {
38 }
39
40 bool EniroCoreConfig::apply(Source* source)
41 {
42     Eniro* eniro = dynamic_cast<Eniro*>(source);
43
44     if(!eniro)
45     {
46         return false;
47     }
48
49     load();
50
51     QString username = config_[USERNAME_COLUMN];
52     QString password = config_[PASSWORD_COLUMN];
53
54     if(!username.isEmpty() && !password.isEmpty())
55     {
56         eniro->login(username, password);
57     }
58
59     eniro->setSite(Eniro::stringToSite(config_[SITE_COLUMN]));
60
61     return true;
62 }
63
64 void EniroCoreConfig::getConfig(QMap<QString, QString>& config)
65 {
66     load();
67
68     config = config_;
69 }
70
71 void EniroCoreConfig::loadFromConfig(QMap<QString, QString> const& config)
72 {
73     config_[USERNAME_COLUMN] = config[USERNAME_COLUMN];
74     config_[PASSWORD_COLUMN] = config[PASSWORD_COLUMN];
75     config_[SITE_COLUMN] = config[SITE_COLUMN];
76
77     loaded_ = true;
78 }
79
80 void EniroCoreConfig::store()
81 {
82     Settings::instance()->set(USERNAME_COLUMN, config_[USERNAME_COLUMN]);
83     Settings::instance()->set(PASSWORD_COLUMN, config_[PASSWORD_COLUMN]);
84     Settings::instance()->set(SITE_COLUMN, config_[SITE_COLUMN]);
85 }
86
87 bool EniroCoreConfig::readyToSearch()
88 {
89     load();
90
91     return !config_[USERNAME_COLUMN].isEmpty() && !config_[PASSWORD_COLUMN].isEmpty();
92 }
93
94 void EniroCoreConfig::setUsername(QString const& username)
95 {
96     config_[USERNAME_COLUMN] = username;
97 }
98
99 void EniroCoreConfig::setPassword(QString const& password)
100 {
101     config_[PASSWORD_COLUMN] = password;
102 }
103
104 void EniroCoreConfig::setSite(QString const& site)
105 {
106     config_[SITE_COLUMN] = site;
107 }
108
109 QString EniroCoreConfig::getUsername() const
110 {
111     return config_[USERNAME_COLUMN];
112 }
113
114 QString EniroCoreConfig::getPassword() const
115 {
116     return config_[PASSWORD_COLUMN];
117 }
118
119 QString EniroCoreConfig::getSite() const
120 {
121     return config_[SITE_COLUMN];
122 }
123
124 void EniroCoreConfig::load()
125 {
126     if(loaded_)
127     {
128         return;
129     }
130
131     Settings::instance()->startEdit();
132
133     config_[USERNAME_COLUMN] = Settings::instance()->get(USERNAME_COLUMN);
134     config_[PASSWORD_COLUMN] = Settings::instance()->get(PASSWORD_COLUMN);
135     config_[SITE_COLUMN] = Settings::instance()->get(SITE_COLUMN);
136
137     Settings::instance()->endEdit();
138
139     loaded_ = true;
140 }