Architecture changed to allow easier addition of new phone books. Norwegian phonebook...
[jenirok] / src / common / source.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 "source.h"
21 #include "eniro.h"
22 #include "mobil1881.h"
23
24 namespace
25 {
26     static const QString SOURCE_NAMES[Source::SOURCE_COUNT] =
27     {
28          "Eniro (FI, SE, DK)",
29          "1881 Mobil (NO)"
30     };
31
32     static const QString SOURCE_IDS[Source::SOURCE_COUNT] =
33     {
34          "eniro",
35          "1881mobil"
36     };
37
38 }
39
40 // Regexp used to remove everything except numbers from string
41 QRegExp Source::numberCleaner_ = QRegExp("([^0-9]+)");
42
43 // Removes html tags from string
44 QRegExp Source::tagStripper_ = QRegExp("<([^>]+)>");
45
46 Source* Source::getSource(Source::SourceId id, QObject* parent)
47 {
48     switch(id)
49     {
50     case ENIRO:
51         return new Eniro(parent);
52         break;
53     case MOBIL1881:
54         return new Mobil1881(parent);
55         break;
56     default:
57         qDebug() << "Unknown source:" << id;
58     }
59
60     return 0;
61 }
62
63 Source::SourceId Source::stringToId(QString const& str)
64 {
65     for(int i = 0; i < SOURCE_COUNT; i++)
66     {
67         if(SOURCE_IDS[i] == str || SOURCE_NAMES[i] == str)
68         {
69             return static_cast<SourceId>(i);
70         }
71     }
72
73     return ENIRO;
74 }
75
76 void Source::getSources(QList<SourceDetails>& list)
77 {
78     for(int i = 0; i < SOURCE_COUNT; i++)
79     {
80         SourceDetails details;
81         details.type = static_cast<SourceId>(i);
82         details.name = SOURCE_NAMES[i];
83         details.id = SOURCE_IDS[i];
84         list.push_back(details);
85     }
86 }
87
88 Source::Source(QObject* parent): QObject(parent),
89 maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(false),
90 error_(NO_ERROR), loggedIn_(false)
91 {
92     connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool)));
93 }
94
95 Source::~Source()
96 {
97     abort();
98 }
99
100 void Source::abort()
101 {
102     http_.abort();
103 }
104
105 void Source::setMaxResults(unsigned int results)
106 {
107     maxResults_ = results;
108 }
109
110 unsigned int Source::getMaxResults() const
111 {
112     return maxResults_;
113 }
114
115 void Source::setTimeout(unsigned int timeout)
116 {
117     timeout_ = timeout;
118     resetTimeout();
119 }
120
121 unsigned int Source::getTimeout() const
122 {
123     return timeout_;
124 }
125
126 void Source::resetTimeout()
127 {
128     if(timerId_)
129     {
130         killTimer(timerId_);
131     }
132     if(timeout_)
133     {
134         timerId_ = startTimer(timeout_);
135     }
136 }
137
138 void Source::timerEvent(QTimerEvent* t)
139 {
140     Q_UNUSED(t);
141 }
142
143 void Source::setFindNumber(bool value)
144 {
145     findNumber_ = value;
146 }
147
148 bool Source::getFindNumber() const
149 {
150     return findNumber_;
151 }
152
153 Source::Error Source::error() const
154 {
155     return error_;
156 }
157
158 const QString& Source::errorString() const
159 {
160     return errorString_;
161 }
162
163 void Source::setError(Source::Error error, QString const& errorString)
164 {
165     error_ = error;
166     errorString_ = errorString;
167 }
168
169 void Source::httpReady(int id, bool error)
170 {
171
172     if(error)
173     {
174         if(http_.error() == QHttp::Aborted)
175         {
176             return;
177         }
178
179         qDebug() << "Error: " << http_.errorString();
180         handleHttpError(id);
181     }
182     else
183     {
184         QString result(http_.readAll());
185         handleHttpData(id, result);
186     }
187 }
188
189 QString Source::ucFirst(QString& str)
190 {
191     if (str.size() < 1) {
192         return "";
193     }
194
195     QStringList tokens = str.split(" ");
196     QList<QString>::iterator tokItr;
197
198     for (tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr)
199     {
200         (*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1);
201     }
202
203     return tokens.join(" ");
204 }
205
206 QString& Source::cleanUpNumber(QString& number)
207 {
208     return number.replace(numberCleaner_, "");
209 }
210
211 QString& Source::stripTags(QString& string)
212 {
213     return string.replace(tagStripper_, "");
214 }
215
216 void Source::fixUrl(QUrl& url)
217 {
218     QByteArray path = url.encodedQuery().replace('+', "%2B");
219     url.setEncodedQuery(path);
220 }
221
222 Source::SearchDetails::SearchDetails(QString const& q,
223                                      QString const& loc,
224                                      SearchType t)
225 {
226     query = q;
227     location = loc;
228     type = t;
229 }