2 * This file is part of Jenirok.
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.
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.
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/>.
19 #include <QtCore/QDebug>
22 #include "mobil1881.h"
23 #include "dastelefonbuch.h"
27 static const QString SOURCE_NAMES[Source::SOURCE_COUNT] =
31 "Das Telefonbuch (DE)"
34 static const QString SOURCE_IDS[Source::SOURCE_COUNT] =
43 // Regexp used to remove everything except numbers from string
44 QRegExp Source::numberCleaner_ = QRegExp("([^0-9]+)");
46 // Removes html tags from string
47 QRegExp Source::tagStripper_ = QRegExp("<([^>]+)>");
49 Source* Source::getSource(Source::SourceId id, QObject* parent)
54 return new Eniro(parent);
57 return new Mobil1881(parent);
60 return new DasTelefonbuch(parent);
63 qDebug() << "Unknown source:" << id;
69 Source::SourceId Source::stringToId(QString const& str)
71 for(int i = 0; i < SOURCE_COUNT; i++)
73 if(SOURCE_IDS[i] == str || SOURCE_NAMES[i] == str)
75 return static_cast<SourceId>(i);
82 void Source::getSources(QList<SourceDetails>& list)
84 for(int i = 0; i < SOURCE_COUNT; i++)
86 SourceDetails details;
87 details.type = static_cast<SourceId>(i);
88 details.name = SOURCE_NAMES[i];
89 details.id = SOURCE_IDS[i];
90 list.push_back(details);
94 Source::Source(QObject* parent): QObject(parent),
95 maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(true),
96 error_(NO_ERROR), loggedIn_(false)
98 connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool)));
111 void Source::setMaxResults(unsigned int results)
113 maxResults_ = results;
116 unsigned int Source::getMaxResults() const
121 void Source::getSearchTypes(QList<SearchType>& types) const
124 types.push_back(PERSONS);
125 types.push_back(YELLOW_PAGES);
128 void Source::setTimeout(unsigned int timeout)
134 unsigned int Source::getTimeout() const
139 void Source::resetTimeout()
147 timerId_ = startTimer(timeout_);
151 void Source::timerEvent(QTimerEvent* t)
156 void Source::setFindNumber(bool value)
161 bool Source::getFindNumber() const
166 Source::Error Source::error() const
171 const QString& Source::errorString() const
176 void Source::setError(Source::Error error, QString const& errorString)
179 errorString_ = errorString;
182 void Source::httpReady(int id, bool error)
187 if(http_.error() == QHttp::Aborted)
192 qDebug() << "Error: " << http_.errorString();
197 QByteArray result = http_.readAll();
198 handleHttpData(id, result);
202 QString Source::ucFirst(QString& str)
204 if (str.size() < 1) {
208 QStringList tokens = str.split(" ");
209 QList<QString>::iterator tokItr;
211 for (tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr)
213 (*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1);
216 return tokens.join(" ");
219 QString& Source::cleanUpNumber(QString& number)
221 return number.replace(numberCleaner_, "");
224 QString& Source::stripTags(QString& string)
226 return string.replace(tagStripper_, "");
229 QString& Source::htmlEntityDecode(QString& string)
231 static const QString entities[] =
303 static const int entityValues[] =
375 static int const COUNT = sizeof(entityValues) / sizeof(entityValues[0]);
377 for(int i = 0; i < COUNT; i++)
379 string = string.replace("&" + entities[i] + ";", QChar(entityValues[i]));
382 static QRegExp entityCleaner("&#([0-9]{1,3});");
383 entityCleaner.setMinimal(true);
387 while((pos = entityCleaner.indexIn(string, pos)) != -1)
389 QString match = entityCleaner.cap(1);
391 int value = match.toInt();
393 if(value >= 1 && value <= 255)
395 string = string.replace(pos, match.length() + 3, QChar(value));
398 pos += entityCleaner.matchedLength();
405 void Source::fixUrl(QUrl& url)
407 QByteArray path = url.encodedQuery().replace('+', "%2B");
408 url.setEncodedQuery(path);
411 bool Source::isPhoneNumber(QString const& string)
413 static QRegExp check("^([0-9 -]{7,25})$");
414 return check.exactMatch(string);
417 Source::SearchDetails::SearchDetails(QString const& q,