Better cache handling. Added clear cache button to settings.
[jenirok] / src / common / eniro.cpp
index 20af69c..896a788 100644 (file)
@@ -43,6 +43,7 @@ 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 NUMBER_REGEXP = "<div class=\"callRow\">(.*)</div>";
@@ -57,7 +58,7 @@ 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),
+errorString_(""), maxResults_(10), 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