Changed search to retry automatically couple of times before failing.
[jenirok] / src / gui / resultwindow.cpp
index 2640bbe..1bd7cb4 100644 (file)
@@ -30,7 +30,7 @@
 #include "sourcecoreconfig.h"
 
 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
-source_(0), list_(0), connectionManager_(0)
+source_(0), list_(0), connectionManager_(0), timer_(0), searching_(false), retries_(0)
 {
     setAttribute(Qt::WA_Maemo5StackedWindow);
     setWindowTitle(tr("Search results"));
@@ -56,11 +56,29 @@ void ResultWindow::search(SearchDialog::SearchDetails& details)
         list_->clear();
     }
 
-    Source::SourceId sourceId = Source::stringToId(Settings::instance()->get("source"));
+    show();
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+
+    if(!connectionManager_)
+    {
+        connectionManager_ = new ConnectionManager();
+    }
+
+    connectionManager_->connect();
+
+    Source::SourceId id = Source::stringToId(Settings::instance()->get("source"));
 
-    if(!source_)
+    if(!source_ || id != sourceId_)
     {
-        source_ = Source::getSource(sourceId);
+        sourceId_ = id;
+
+        if(source_)
+        {
+            delete source_;
+            source_ = 0;
+        }
+
+        source_ = Source::getSource(sourceId_);
         Q_ASSERT(source_ != 0);
         source_->setTimeout(REQUEST_TIMEOUT);
 
@@ -75,46 +93,40 @@ void ResultWindow::search(SearchDialog::SearchDetails& details)
                                                                           Source::SearchDetails const&, bool)));
     }
 
-    SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId);
+    SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
 
     Q_ASSERT(config != 0);
 
     config->apply(source_);
     delete config;
 
-    Source::SearchType type;
-
-    switch(details.type)
+    if(searching_)
     {
-    case 0:
-        type = Source::PERSONS;
-        break;
-    case 1:
-        type = Source::YELLOW_PAGES;
-        break;
-    default:
-        qDebug() << "Unknown search type: " << details.type;
-        return;
+        source_->abort();
+        timer_ = startTimer(SEARCH_INTERVAL);
     }
 
-    show();
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
-
-    if(!connectionManager_)
+    while(timer_)
     {
-        connectionManager_ = new ConnectionManager();
+        QApplication::processEvents(QEventLoop::WaitForMoreEvents);
     }
 
-    connectionManager_->connect();
-
-    source_->abort();
-    source_->search(Source::SearchDetails(details.name, details.location, type));
+    list_->clear();
+    searching_ = true;
+    retries_ = 0;
+    currentSearch_ = Source::SearchDetails(details.name, details.location, details.type);
+    source_->search(currentSearch_);
 
 }
 
 void ResultWindow::resultAvailable(Source::Result const& result,
                                    Source::SearchDetails const& details)
 {
+    if(!list_)
+    {
+        return;
+    }
+
     Q_UNUSED(details);
 
     if(!result.number.isEmpty())
@@ -140,6 +152,7 @@ void ResultWindow::resultAvailable(Source::Result const& result,
     data["street"] = QVariant(result.street);
     data["city"] = QVariant(result.city);
     data["number"] = QVariant(result.number);
+    data["country"] = QVariant(result.country);
 
     item->setData(Qt::UserRole, QVariant(data));
 
@@ -154,6 +167,15 @@ void ResultWindow::requestFinished(QVector <Source::Result> const& results,
 
     if(error)
     {
+        if(retries_ < RETRIES)
+        {
+            qDebug() << "Searching failed, retrying...";
+            retries_++;
+            list_->clear();
+            source_->search(currentSearch_);
+            return;
+        }
+
         QString errorString;
         Source::Error error = source_->error();
 
@@ -186,6 +208,8 @@ void ResultWindow::requestFinished(QVector <Source::Result> const& results,
 
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 
+    searching_ = false;
+
 }
 
 void ResultWindow::itemClicked(QListWidgetItem* item)
@@ -196,19 +220,27 @@ void ResultWindow::itemClicked(QListWidgetItem* item)
     details.street = data["street"].toString();
     details.city = data["city"].toString();
     details.number = data["number"].toString();
+    details.country = data["country"].toString();
 
     emit itemSelected(details);
 }
 
 void ResultWindow::setVisible(bool visible)
 {
-    QMainWindow::setVisible(visible);
-
     if(!visible && source_)
     {
         source_->abort();
     }
+
+    QMainWindow::setVisible(visible);
 }
 
+void ResultWindow::timerEvent(QTimerEvent* event)
+{
+    Q_UNUSED(event);
+
+    killTimer(timer_);
+    timer_ = 0;
+}