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"
26 static const QString SOURCE_NAMES[Source::SOURCE_COUNT] =
32 static const QString SOURCE_IDS[Source::SOURCE_COUNT] =
40 // Regexp used to remove everything except numbers from string
41 QRegExp Source::numberCleaner_ = QRegExp("([^0-9]+)");
43 // Removes html tags from string
44 QRegExp Source::tagStripper_ = QRegExp("<([^>]+)>");
46 Source* Source::getSource(Source::SourceId id, QObject* parent)
51 return new Eniro(parent);
54 return new Mobil1881(parent);
57 qDebug() << "Unknown source:" << id;
63 Source::SourceId Source::stringToId(QString const& str)
65 for(int i = 0; i < SOURCE_COUNT; i++)
67 if(SOURCE_IDS[i] == str || SOURCE_NAMES[i] == str)
69 return static_cast<SourceId>(i);
76 void Source::getSources(QList<SourceDetails>& list)
78 for(int i = 0; i < SOURCE_COUNT; i++)
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);
88 Source::Source(QObject* parent): QObject(parent),
89 maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(true),
90 error_(NO_ERROR), loggedIn_(false)
92 connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool)));
105 void Source::setMaxResults(unsigned int results)
107 maxResults_ = results;
110 unsigned int Source::getMaxResults() const
115 void Source::setTimeout(unsigned int timeout)
121 unsigned int Source::getTimeout() const
126 void Source::resetTimeout()
134 timerId_ = startTimer(timeout_);
138 void Source::timerEvent(QTimerEvent* t)
143 void Source::setFindNumber(bool value)
148 bool Source::getFindNumber() const
153 Source::Error Source::error() const
158 const QString& Source::errorString() const
163 void Source::setError(Source::Error error, QString const& errorString)
166 errorString_ = errorString;
169 void Source::httpReady(int id, bool error)
174 if(http_.error() == QHttp::Aborted)
179 qDebug() << "Error: " << http_.errorString();
184 QByteArray result = http_.readAll();
185 handleHttpData(id, result);
189 QString Source::ucFirst(QString& str)
191 if (str.size() < 1) {
195 QStringList tokens = str.split(" ");
196 QList<QString>::iterator tokItr;
198 for (tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr)
200 (*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1);
203 return tokens.join(" ");
206 QString& Source::cleanUpNumber(QString& number)
208 return number.replace(numberCleaner_, "");
211 QString& Source::stripTags(QString& string)
213 return string.replace(tagStripper_, "");
216 QString& Source::htmlEntityDecode(QString& string)
218 static const QString entities[] =
290 static const int entityValues[] =
362 static int const COUNT = sizeof(entityValues) / sizeof(entityValues[0]);
364 for(int i = 0; i < COUNT; i++)
366 string = string.replace("&" + entities[i] + ";", QChar(entityValues[i]));
369 static QRegExp entityCleaner("&#([0-9]{1,3});");
370 entityCleaner.setMinimal(true);
374 while((pos = entityCleaner.indexIn(string, pos)) != -1)
376 QString match = entityCleaner.cap(1);
378 int value = match.toInt();
380 if(value >= 1 && value <= 255)
382 string = string.replace(pos, match.length() + 3, QChar(value));
385 pos += entityCleaner.matchedLength();
392 void Source::fixUrl(QUrl& url)
394 QByteArray path = url.encodedQuery().replace('+', "%2B");
395 url.setEncodedQuery(path);
398 bool Source::isPhoneNumber(QString const& string)
400 static QRegExp check("^([0-9 -]{7,25})$");
401 return check.exactMatch(string);
404 Source::SearchDetails::SearchDetails(QString const& q,