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>
20 #include <QtNetwork/QHttpResponseHeader>
23 #include "mobil1881.h"
24 #include "dastelefonbuch.h"
28 static const QString SOURCE_NAMES[Source::SOURCE_COUNT] =
32 "Das Telefonbuch (DE)"
35 static const QString SOURCE_IDS[Source::SOURCE_COUNT] =
44 // Regexp used to remove everything except numbers from string
45 QRegExp Source::numberCleaner_ = QRegExp("([^0-9]+)");
47 // Removes html tags from string
48 QRegExp Source::tagStripper_ = QRegExp("<([^>]+)>");
50 Source* Source::getSource(Source::SourceId id, QObject* parent)
55 return new Eniro(parent);
58 return new Mobil1881(parent);
61 return new DasTelefonbuch(parent);
64 qDebug() << "Unknown source:" << id;
70 Source::SourceId Source::stringToId(QString const& str)
72 for(int i = 0; i < SOURCE_COUNT; i++)
74 if(SOURCE_IDS[i] == str || SOURCE_NAMES[i] == str)
76 return static_cast<SourceId>(i);
83 void Source::getSources(QList<SourceDetails>& list)
85 for(int i = 0; i < SOURCE_COUNT; i++)
87 SourceDetails details;
88 details.type = static_cast<SourceId>(i);
89 details.name = SOURCE_NAMES[i];
90 details.id = SOURCE_IDS[i];
91 list.push_back(details);
95 Source::Source(QObject* parent): QObject(parent),
96 maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(true),
97 error_(NO_ERROR), loggedIn_(false)
99 connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool)));
112 void Source::setMaxResults(unsigned int results)
114 maxResults_ = results;
117 unsigned int Source::getMaxResults() const
122 void Source::getSearchTypes(QList<SearchType>& types) const
125 types.push_back(PERSONS);
126 types.push_back(YELLOW_PAGES);
129 void Source::setTimeout(unsigned int timeout)
135 unsigned int Source::getTimeout() const
140 void Source::resetTimeout()
148 timerId_ = startTimer(timeout_);
152 void Source::timerEvent(QTimerEvent* t)
157 void Source::setFindNumber(bool value)
162 bool Source::getFindNumber() const
167 Source::Error Source::error() const
172 const QString& Source::errorString() const
177 void Source::setError(Source::Error error, QString const& errorString)
180 errorString_ = errorString;
183 void Source::httpReady(int id, bool error)
185 QByteArray result = http_.readAll();
189 if(http_.error() == QHttp::Aborted)
194 qDebug() << "Error: " << http_.errorString();
199 handleHttpData(id, result);
203 QString Source::ucFirst(QString& str)
205 if (str.size() < 1) {
209 QStringList tokens = str.split(" ");
210 QList<QString>::iterator tokItr;
212 for (tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr)
214 (*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1);
217 return tokens.join(" ");
220 QString& Source::cleanUpNumber(QString& number)
222 return number.replace(numberCleaner_, "");
225 QString& Source::stripTags(QString& string)
227 return string.replace(tagStripper_, "");
230 QString& Source::htmlEntityDecode(QString& string)
232 static const QString entities[] =
304 static const int entityValues[] =
376 static int const COUNT = sizeof(entityValues) / sizeof(entityValues[0]);
378 for(int i = 0; i < COUNT; i++)
380 string = string.replace("&" + entities[i] + ";", QChar(entityValues[i]));
383 static QRegExp entityCleaner("&#([0-9]{1,3});");
384 entityCleaner.setMinimal(true);
388 while((pos = entityCleaner.indexIn(string, pos)) != -1)
390 QString match = entityCleaner.cap(1);
392 int value = match.toInt();
394 if(value >= 1 && value <= 255)
396 string = string.replace(pos, match.length() + 3, QChar(value));
399 pos += entityCleaner.matchedLength();
406 void Source::fixUrl(QUrl& url)
408 QByteArray path = url.encodedQuery().replace('+', "%2B");
409 url.setEncodedQuery(path);
412 bool Source::isPhoneNumber(QString const& string)
414 static QRegExp check("^([0-9 -]{7,25})$");
415 return check.exactMatch(string);
418 Source::SearchDetails::SearchDetails(QString const& q,