Changed search to retry automatically couple of times before failing.
[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[USERNAME_COLUMN] = config_[USERNAME_COLUMN];
69     config[PASSWORD_COLUMN] = config_[PASSWORD_COLUMN];
70     config[SITE_COLUMN] = config_[SITE_COLUMN];
71 }
72
73 void EniroCoreConfig::loadFromConfig(QMap<QString, QString> const& config)
74 {
75     config_[USERNAME_COLUMN] = config[USERNAME_COLUMN];
76     config_[PASSWORD_COLUMN] = config[PASSWORD_COLUMN];
77     config_[SITE_COLUMN] = config[SITE_COLUMN];
78
79     loaded_ = true;
80 }
81
82 void EniroCoreConfig::store()
83 {
84     Settings::instance()->set(USERNAME_COLUMN, config_[USERNAME_COLUMN]);
85     Settings::instance()->set(PASSWORD_COLUMN, config_[PASSWORD_COLUMN]);
86     Settings::instance()->set(SITE_COLUMN, config_[SITE_COLUMN]);
87 }
88
89 bool EniroCoreConfig::readyToSearch()
90 {
91     load();
92
93     if(config_[SITE_COLUMN] != "fi")
94     {
95         return true;
96     }
97
98     return !config_[USERNAME_COLUMN].isEmpty() && !config_[PASSWORD_COLUMN].isEmpty();
99 }
100
101 void EniroCoreConfig::setUsername(QString const& username)
102 {
103     config_[USERNAME_COLUMN] = username;
104 }
105
106 void EniroCoreConfig::setPassword(QString const& password)
107 {
108     config_[PASSWORD_COLUMN] = password;
109 }
110
111 void EniroCoreConfig::setSite(QString const& site)
112 {
113     config_[SITE_COLUMN] = site;
114 }
115
116 QString EniroCoreConfig::getUsername() const
117 {
118     return config_[USERNAME_COLUMN];
119 }
120
121 QString EniroCoreConfig::getPassword() const
122 {
123     return config_[PASSWORD_COLUMN];
124 }
125
126 QString EniroCoreConfig::getSite() const
127 {
128     return config_[SITE_COLUMN];
129 }
130
131 void EniroCoreConfig::load()
132 {
133     if(loaded_)
134     {
135         return;
136     }
137
138     Settings::instance()->startEdit();
139
140     config_[USERNAME_COLUMN] = Settings::instance()->get(USERNAME_COLUMN);
141     config_[PASSWORD_COLUMN] = Settings::instance()->get(PASSWORD_COLUMN);
142     config_[SITE_COLUMN] = Settings::instance()->get(SITE_COLUMN);
143
144     Settings::instance()->endEdit();
145
146     loaded_ = true;
147 }