Architecture changed to allow easier addition of new phone books. Norwegian phonebook...
[jenirok] / src / common / source.cpp
diff --git a/src/common/source.cpp b/src/common/source.cpp
new file mode 100644 (file)
index 0000000..4fc513e
--- /dev/null
@@ -0,0 +1,229 @@
+/*
+ * This file is part of Jenirok.
+ *
+ * Jenirok is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Jenirok is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jenirok.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <QtCore/QDebug>
+#include "source.h"
+#include "eniro.h"
+#include "mobil1881.h"
+
+namespace
+{
+    static const QString SOURCE_NAMES[Source::SOURCE_COUNT] =
+    {
+         "Eniro (FI, SE, DK)",
+         "1881 Mobil (NO)"
+    };
+
+    static const QString SOURCE_IDS[Source::SOURCE_COUNT] =
+    {
+         "eniro",
+         "1881mobil"
+    };
+
+}
+
+// Regexp used to remove everything except numbers from string
+QRegExp Source::numberCleaner_ = QRegExp("([^0-9]+)");
+
+// Removes html tags from string
+QRegExp Source::tagStripper_ = QRegExp("<([^>]+)>");
+
+Source* Source::getSource(Source::SourceId id, QObject* parent)
+{
+    switch(id)
+    {
+    case ENIRO:
+        return new Eniro(parent);
+        break;
+    case MOBIL1881:
+        return new Mobil1881(parent);
+        break;
+    default:
+        qDebug() << "Unknown source:" << id;
+    }
+
+    return 0;
+}
+
+Source::SourceId Source::stringToId(QString const& str)
+{
+    for(int i = 0; i < SOURCE_COUNT; i++)
+    {
+        if(SOURCE_IDS[i] == str || SOURCE_NAMES[i] == str)
+        {
+            return static_cast<SourceId>(i);
+        }
+    }
+
+    return ENIRO;
+}
+
+void Source::getSources(QList<SourceDetails>& list)
+{
+    for(int i = 0; i < SOURCE_COUNT; i++)
+    {
+        SourceDetails details;
+        details.type = static_cast<SourceId>(i);
+        details.name = SOURCE_NAMES[i];
+        details.id = SOURCE_IDS[i];
+        list.push_back(details);
+    }
+}
+
+Source::Source(QObject* parent): QObject(parent),
+maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(false),
+error_(NO_ERROR), loggedIn_(false)
+{
+    connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool)));
+}
+
+Source::~Source()
+{
+    abort();
+}
+
+void Source::abort()
+{
+    http_.abort();
+}
+
+void Source::setMaxResults(unsigned int results)
+{
+    maxResults_ = results;
+}
+
+unsigned int Source::getMaxResults() const
+{
+    return maxResults_;
+}
+
+void Source::setTimeout(unsigned int timeout)
+{
+    timeout_ = timeout;
+    resetTimeout();
+}
+
+unsigned int Source::getTimeout() const
+{
+    return timeout_;
+}
+
+void Source::resetTimeout()
+{
+    if(timerId_)
+    {
+        killTimer(timerId_);
+    }
+    if(timeout_)
+    {
+        timerId_ = startTimer(timeout_);
+    }
+}
+
+void Source::timerEvent(QTimerEvent* t)
+{
+    Q_UNUSED(t);
+}
+
+void Source::setFindNumber(bool value)
+{
+    findNumber_ = value;
+}
+
+bool Source::getFindNumber() const
+{
+    return findNumber_;
+}
+
+Source::Error Source::error() const
+{
+    return error_;
+}
+
+const QString& Source::errorString() const
+{
+    return errorString_;
+}
+
+void Source::setError(Source::Error error, QString const& errorString)
+{
+    error_ = error;
+    errorString_ = errorString;
+}
+
+void Source::httpReady(int id, bool error)
+{
+
+    if(error)
+    {
+        if(http_.error() == QHttp::Aborted)
+        {
+            return;
+        }
+
+        qDebug() << "Error: " << http_.errorString();
+        handleHttpError(id);
+    }
+    else
+    {
+        QString result(http_.readAll());
+        handleHttpData(id, result);
+    }
+}
+
+QString Source::ucFirst(QString& str)
+{
+    if (str.size() < 1) {
+        return "";
+    }
+
+    QStringList tokens = str.split(" ");
+    QList<QString>::iterator tokItr;
+
+    for (tokItr = tokens.begin(); tokItr != tokens.end(); ++tokItr)
+    {
+        (*tokItr) = (*tokItr).at(0).toUpper() + (*tokItr).mid(1);
+    }
+
+    return tokens.join(" ");
+}
+
+QString& Source::cleanUpNumber(QString& number)
+{
+    return number.replace(numberCleaner_, "");
+}
+
+QString& Source::stripTags(QString& string)
+{
+    return string.replace(tagStripper_, "");
+}
+
+void Source::fixUrl(QUrl& url)
+{
+    QByteArray path = url.encodedQuery().replace('+', "%2B");
+    url.setEncodedQuery(path);
+}
+
+Source::SearchDetails::SearchDetails(QString const& q,
+                                     QString const& loc,
+                                     SearchType t)
+{
+    query = q;
+    location = loc;
+    type = t;
+}