Added Das Telefonbuch support.
[jenirok] / src / common / source.cpp
index 4fc513e..21245b9 100644 (file)
 #include "source.h"
 #include "eniro.h"
 #include "mobil1881.h"
+#include "dastelefonbuch.h"
 
 namespace
 {
     static const QString SOURCE_NAMES[Source::SOURCE_COUNT] =
     {
          "Eniro (FI, SE, DK)",
-         "1881 Mobil (NO)"
+         "1881 Mobil (NO)",
+         "Das Telefonbuch (DE)"
     };
 
     static const QString SOURCE_IDS[Source::SOURCE_COUNT] =
     {
          "eniro",
-         "1881mobil"
+         "1881mobil",
+         "dastelefonbuch"
     };
 
 }
@@ -53,6 +56,9 @@ Source* Source::getSource(Source::SourceId id, QObject* parent)
     case MOBIL1881:
         return new Mobil1881(parent);
         break;
+    case DASTELEFONBUCH:
+        return new DasTelefonbuch(parent);
+        break;
     default:
         qDebug() << "Unknown source:" << id;
     }
@@ -86,7 +92,7 @@ void Source::getSources(QList<SourceDetails>& list)
 }
 
 Source::Source(QObject* parent): QObject(parent),
-maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(false),
+maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0), findNumber_(true),
 error_(NO_ERROR), loggedIn_(false)
 {
     connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool)));
@@ -112,6 +118,13 @@ unsigned int Source::getMaxResults() const
     return maxResults_;
 }
 
+void Source::getSearchTypes(QList<SearchType>& types) const
+{
+    types.clear();
+    types.push_back(PERSONS);
+    types.push_back(YELLOW_PAGES);
+}
+
 void Source::setTimeout(unsigned int timeout)
 {
     timeout_ = timeout;
@@ -181,7 +194,7 @@ void Source::httpReady(int id, bool error)
     }
     else
     {
-        QString result(http_.readAll());
+        QByteArray result = http_.readAll();
         handleHttpData(id, result);
     }
 }
@@ -213,12 +226,194 @@ QString& Source::stripTags(QString& string)
     return string.replace(tagStripper_, "");
 }
 
+QString& Source::htmlEntityDecode(QString& string)
+{
+    static const QString entities[] =
+    {
+        "quot",
+        "apos",
+        "amp",
+        "lt",
+        "gt",
+        "nbsp",
+        "Agrave",
+        "Aacute",
+        "Acirc",
+        "Atilde",
+        "Auml",
+        "Aring",
+        "AElig",
+        "Ccedil",
+        "Egrave",
+        "Eacute",
+        "Ecirc",
+        "Euml",
+        "Igrave",
+        "Iacute",
+        "Icirc",
+        "Iuml",
+        "ETH",
+        "Ntilde",
+        "Ograve",
+        "Oacute",
+        "Ocirc",
+        "Otilde",
+        "Ouml",
+        "Oslash",
+        "Ugrave",
+        "Uacute",
+        "Ucirc",
+        "Uuml",
+        "Yacute",
+        "THORN",
+        "szlig",
+        "agrave",
+        "aacute",
+        "acirc",
+        "atilde",
+        "auml",
+        "aring",
+        "aelig",
+        "ccedil",
+        "egrave",
+        "eacute",
+        "ecirc",
+        "euml",
+        "igrave",
+        "iacute",
+        "icirc",
+        "iuml",
+        "eth",
+        "ntilde",
+        "ograve",
+        "oacute",
+        "ocirc",
+        "otilde",
+        "ouml",
+        "oslash",
+        "ugrave",
+        "uacute",
+        "ucirc",
+        "uuml",
+        "yacute",
+        "thorn",
+        "yuml"
+    };
+
+    static const int entityValues[] =
+    {
+        34,
+        39,
+        38,
+        60,
+        62,
+        160,
+        192,
+        193,
+        194,
+        195,
+        196,
+        197,
+        198,
+        199,
+        200,
+        201,
+        202,
+        203,
+        204,
+        205,
+        206,
+        207,
+        208,
+        209,
+        210,
+        211,
+        212,
+        213,
+        214,
+        216,
+        217,
+        218,
+        219,
+        220,
+        221,
+        222,
+        223,
+        224,
+        225,
+        226,
+        227,
+        228,
+        229,
+        230,
+        231,
+        232,
+        233,
+        234,
+        235,
+        236,
+        237,
+        238,
+        239,
+        240,
+        241,
+        242,
+        243,
+        244,
+        245,
+        246,
+        248,
+        249,
+        250,
+        251,
+        252,
+        253,
+        254,
+        255
+    };
+
+    static int const COUNT = sizeof(entityValues) / sizeof(entityValues[0]);
+
+    for(int i = 0; i < COUNT; i++)
+    {
+        string = string.replace("&" + entities[i] + ";", QChar(entityValues[i]));
+    }
+
+    static QRegExp entityCleaner("&#([0-9]{1,3});");
+    entityCleaner.setMinimal(true);
+
+    int pos = 0;
+
+    while((pos = entityCleaner.indexIn(string, pos)) != -1)
+    {
+        QString match = entityCleaner.cap(1);
+
+        int value = match.toInt();
+
+        if(value >= 1 && value <= 255)
+        {
+            string = string.replace(pos, match.length() + 3, QChar(value));
+        }
+
+        pos += entityCleaner.matchedLength();
+    }
+
+
+   return string;
+}
+
 void Source::fixUrl(QUrl& url)
 {
     QByteArray path = url.encodedQuery().replace('+', "%2B");
     url.setEncodedQuery(path);
 }
 
+bool Source::isPhoneNumber(QString const& string)
+{
+    static QRegExp check("^([0-9 -]{7,25})$");
+    return check.exactMatch(string);
+}
+
 Source::SearchDetails::SearchDetails(QString const& q,
                                      QString const& loc,
                                      SearchType t)