Increased the maximum number of search results.
[jenirok] / src / common / eniro.cpp
index 20af69c..b28760c 100644 (file)
@@ -42,7 +42,8 @@ namespace
          "dk"
     };
 
-    static const QString INVALID_LOGIN_STRING = "Invalid login details";
+    static const QString INVALID_LOGIN_STRING = QObject::tr("Invalid login details");
+    static const QString TIMEOUT_STRING = QObject::tr("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 NUMBER_REGEXP = "<div class=\"callRow\">(.*)</div>";
@@ -57,8 +58,8 @@ QRegExp Eniro::tagStripper_ = QRegExp("<([^>]+)>");
 
 Eniro::Eniro(Site site, QObject *parent): QObject(parent), site_(site),
 username_(""), password_(""), loggedIn_(false), error_(NO_ERROR),
-errorString_(""), maxResults_(10), findNumber_(true),
-pendingSearches_(), pendingNumberRequests_()
+errorString_(""), maxResults_(DEFAULT_MAX_RESULTS), timeout_(0), timerId_(0),
+findNumber_(true), pendingSearches_(), pendingNumberRequests_()
 {
     connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool)));
 }
@@ -113,6 +114,51 @@ void Eniro::setSite(Eniro::Site site)
     site_ = site;
 }
 
+void Eniro::setTimeout(unsigned int ms)
+{
+    timeout_ = ms;
+    resetTimeout();
+}
+
+void Eniro::resetTimeout()
+{
+    if(timerId_)
+    {
+        killTimer(timerId_);
+    }
+    if(timeout_)
+    {
+        timerId_ = startTimer(timeout_);
+    }
+}
+
+void Eniro::timerEvent(QTimerEvent* t)
+{
+    if(t->timerId() == timerId_)
+    {
+        int currentId = http_.currentId();
+
+        if(currentId)
+        {
+            searchMap::const_iterator it = pendingSearches_.find(currentId);
+
+            if(it != pendingSearches_.end())
+            {
+                QVector <Eniro::Result> results = it.value()->results;
+                SearchDetails details = it.value()->details;
+
+                abort();
+
+                error_ = TIMEOUT;
+                errorString_ = TIMEOUT_STRING;
+
+                emit requestFinished(results, details, true);
+            }
+        }
+
+    }
+}
+
 void Eniro::login(QString const& username,
                   QString const& password)
 {
@@ -141,6 +187,8 @@ void Eniro::testLogin()
 
 bool Eniro::search(SearchDetails const& details)
 {
+    resetTimeout();
+
     SearchType type = details.type;
 
     // Only logged in users can use other than person search
@@ -388,7 +436,7 @@ void Eniro::loadResults(int id, QString const& httpData)
         }
     }
 
-    // If number there were no results or no phone numbers needed to
+    // If there were no results or no phone numbers needed to
     // be fetched, the whole request is ready
     if(it.value()->numbersTotal == 0 || !requestsPending)
     {