One more fix to connection handling.
[jenirok] / src / common / eniro.cpp
index 0958893..46fda30 100644 (file)
@@ -45,7 +45,8 @@ namespace
     static const QString INVALID_LOGIN_STRING = "Invalid login details";
     static const QString TIMEOUT_STRING = "Request timed out";
     static const QString PERSON_REGEXP = "<td class=\"hTd2\">(.*)<b>(.*)</td>";
-    static const QString YELLOW_REGEXP = "<td class=\"hTd2\">(.*)<span class=\"gray\"\\}>(.*)</td>";
+    static const QString YELLOW_REGEXP = "<td class=\"hTd2\">(.*)<span class=\"gray\">(.*)</td>";
+    static const QString SINGLE_REGEXP = "<div class=\"header\">(.*)</div>(.*)<div class=\"callRow\">(.*)(<div class=\"block\">|</p>(.*)<br/>|</p>(.*)<br />)";
     static const QString NUMBER_REGEXP = "<div class=\"callRow\">(.*)</div>";
     static const QString LOGIN_CHECK = "<input class=\"inpTxt\" id=\"loginformUsername\"";
 }
@@ -141,7 +142,7 @@ void Eniro::search(SearchDetails const& details)
     SearchType type = details.type;
 
     // Only logged in users can use other than person search
-    if(!loggedIn_)
+    if(!loggedIn_ && site_ == FI)
     {
         type = PERSONS;
     }
@@ -149,7 +150,7 @@ void Eniro::search(SearchDetails const& details)
     QUrl url = createUrl(details.query, details.location);
     QString what;
 
-    if(loggedIn_)
+    if(loggedIn_ || site_ != FI)
     {
         switch(type)
         {
@@ -163,6 +164,7 @@ void Eniro::search(SearchDetails const& details)
 
         default:
             what = "moball";
+            break;
         }
 
     }
@@ -190,9 +192,8 @@ void Eniro::search(SearchDetails const& details)
 
 }
 
-void Eniro::handleHttpData(int id, QString const& data)
+void Eniro::handleHttpData(int id, QByteArray const& data)
 {
-    qDebug() << "Handle:" << id;
     searchMap::const_iterator searchIt;
     numberMap::const_iterator numberIt;
 
@@ -200,7 +201,6 @@ void Eniro::handleHttpData(int id, QString const& data)
     if((searchIt = pendingSearches_.find(id)) !=
         pendingSearches_.end())
     {
-        qDebug() << "Load results";
         // Load results from html data
         loadResults(id, data);
     }
@@ -257,7 +257,6 @@ void Eniro::handleHttpError(int id)
         pendingLoginRequests_.end())
     {
         emit loginStatus(false);
-
     }
 }
 
@@ -265,21 +264,8 @@ void Eniro::handleHttpError(int id)
 void Eniro::loadResults(int id, QString const& httpData)
 {
     searchMap::iterator it = pendingSearches_.find(id);
-    QString expr;
 
-    switch(it.value()->details.type)
-    {
-    case YELLOW_PAGES:
-        expr = YELLOW_REGEXP;
-        break;
-    case PERSONS:
-        expr = PERSON_REGEXP;
-        break;
-    default:
-        return;
-    }
-
-    QRegExp rx(expr);
+    QRegExp rx("((" + YELLOW_REGEXP + ")|(" + PERSON_REGEXP + ")|(" + SINGLE_REGEXP + "))");
     rx.setMinimal(true);
 
     bool requestsPending = false;
@@ -291,8 +277,10 @@ void Eniro::loadResults(int id, QString const& httpData)
     {
         pos += rx.matchedLength();
 
-        data = rx.cap(2);
+        data = rx.cap(1);
+
         data = stripTags(data);
+
         QStringList rows = data.split('\n');
 
         for(int i = 0; i < rows.size(); i++)
@@ -315,6 +303,19 @@ void Eniro::loadResults(int id, QString const& httpData)
 
         Result result;
 
+        switch(site_)
+        {
+        case FI:
+            result.country = "Finland";
+            break;
+        case SE:
+            result.country = "Sweden";
+            break;
+        case DK:
+            result.country = "Denmark";
+            break;
+        }
+
         int size = rows.size();
 
         switch(size)
@@ -329,9 +330,18 @@ void Eniro::loadResults(int id, QString const& httpData)
             break;
 
         case 3:
-            result.name = rows[0];
-            result.street = rows[1];
-            result.city = rows[2];
+            if(isPhoneNumber(rows[1]))
+            {
+                result.name = rows[0];
+                result.number = cleanUpNumber(rows[1]);
+                result.city = rows[2];
+            }
+            else
+            {
+                result.name = rows[0];
+                result.street = rows[1];
+                result.city = rows[2];
+            }
             break;
 
         case 4:
@@ -343,6 +353,35 @@ void Eniro::loadResults(int id, QString const& httpData)
             break;
 
         default:
+            bool ok = false;
+
+            for(int a = 0; a < size && a < 8; a++)
+            {
+                if(isPhoneNumber(rows[a]))
+                {
+                    result.name = rows[0];
+                    result.number = cleanUpNumber(rows[a]);
+
+                    for(int i = a + 1; i < size && i < 8; i++)
+                    {
+                        if(!isPhoneNumber(rows[i]) && size > i + 1 && isStreet(rows[i]))
+                        {
+                            result.street = rows[i];
+                            result.city = rows[i+1];
+                            ok = true;
+                            break;
+                        }
+                    }
+
+                }
+
+            }
+
+            if(ok)
+            {
+                break;
+            }
+
             continue;
 
         }
@@ -351,9 +390,9 @@ void Eniro::loadResults(int id, QString const& httpData)
 
         unsigned int foundResults = ++(it.value()->numbersTotal);
 
-        // If phone number searh is enabled, we have to make another
+        // If phone number search is enabled, we have to make another
         // request to find it out
-        if(getFindNumber() && size < 4 && loggedIn_ &&
+        if(getFindNumber() && size < 4 && (loggedIn_ || site_ != FI) &&
                 it.value()->details.type != YELLOW_PAGES)
         {
             requestsPending = true;
@@ -476,7 +515,7 @@ QUrl Eniro::createUrl(QString const& query, QString const& location)
     {
         url.addQueryItem("hpp", QString::number(maxResults));
     }
-    if(loggedIn_)
+    if(loggedIn_ && site_ == FI)
     {
         url.addQueryItem("login_name", username_);
         url.addQueryItem("login_password", password_);
@@ -515,7 +554,6 @@ void Eniro::emitRequestFinished(int key, SearchData* data, bool error)
 QMap <Eniro::Site, Eniro::SiteDetails> Eniro::getSites()
 {
     QMap <Site, SiteDetails> sites;
-    SiteDetails details;
 
     for(int i = 0; i < SITE_COUNT; i++)
     {
@@ -544,3 +582,17 @@ Eniro::Site Eniro::stringToSite(QString const& str)
 
     return site;
 }
+
+bool Eniro::isStreet(QString const& str)
+{
+    static QRegExp number("([0-9]+)");
+    int a = number.indexIn(str);
+    int b = str.indexOf(" ");
+
+    if((a == -1 && b == -1) || (a != -1 && b != -1))
+    {
+        return true;
+    }
+
+    return false;
+}