Some fixes to connection manager.
[jenirok] / src / gui / resultwindow.cpp
index e9da90a..dc1509f 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)
 {
     setAttribute(Qt::WA_Maemo5StackedWindow);
     setWindowTitle(tr("Search results"));
@@ -56,6 +56,16 @@ void ResultWindow::search(SearchDialog::SearchDetails& details)
         list_->clear();
     }
 
+    show();
+    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
+
+    if(!connectionManager_)
+    {
+        connectionManager_ = new ConnectionManager();
+    }
+
+    connectionManager_->connect();
+
     Source::SourceId id = Source::stringToId(Settings::instance()->get("source"));
 
     if(!source_ || id != sourceId_)
@@ -90,17 +100,19 @@ void ResultWindow::search(SearchDialog::SearchDetails& details)
     config->apply(source_);
     delete config;
 
-    show();
-    setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true);
-
-    if(!connectionManager_)
+    if(searching_)
     {
-        connectionManager_ = new ConnectionManager();
+        source_->abort();
+        timer_ = startTimer(SEARCH_INTERVAL);
     }
 
-    connectionManager_->connect();
+    while(timer_)
+    {
+        QApplication::processEvents(QEventLoop::WaitForMoreEvents);
+    }
 
-    source_->abort();
+    list_->clear();
+    searching_ = true;
     source_->search(Source::SearchDetails(details.name, details.location, details.type));
 
 }
@@ -108,6 +120,11 @@ void ResultWindow::search(SearchDialog::SearchDetails& details)
 void ResultWindow::resultAvailable(Source::Result const& result,
                                    Source::SearchDetails const& details)
 {
+    if(!list_)
+    {
+        return;
+    }
+
     Q_UNUSED(details);
 
     if(!result.number.isEmpty())
@@ -180,6 +197,14 @@ void ResultWindow::requestFinished(QVector <Source::Result> const& results,
 
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 
+    if(timer_)
+    {
+        killTimer(timer_);
+    }
+
+    timer_ = startTimer(SEARCH_INTERVAL);
+    searching_ = false;
+
 }
 
 void ResultWindow::itemClicked(QListWidgetItem* item)
@@ -197,15 +222,20 @@ void ResultWindow::itemClicked(QListWidgetItem* item)
 
 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;
+}