Some fixes to connection manager.
[jenirok] / src / common / connectionmanager.cpp
index 4f0d99e..f7b0f32 100644 (file)
@@ -77,6 +77,12 @@ bool ConnectionManager::connect()
     icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
 
     waitSignal(&connectionReady_);
+
+    if(connected_)
+    {
+        sleep(WAIT_AFTER_CONNECT);
+    }
+
     return connected_;
 }
 
@@ -121,6 +127,8 @@ bool ConnectionManager::connect(QString const& id)
 
     connected_ = true;
 
+    sleep(WAIT_AFTER_CONNECT);
+
     return true;
 }
 
@@ -192,13 +200,6 @@ bool ConnectionManager::disconnect(bool force)
 {
     if(force)
     {
-        // Do not allow forced disconnection if connection was not started
-        // by this class.
-        if(!connected_)
-        {
-            return false;
-        }
-
         QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
                                                       "com.nokia.icd_ui",
                                                       "disconnect");
@@ -363,7 +364,7 @@ void ConnectionManager::scanResult(const QDBusMessage& rep)
         return;
     }
 
-    if(status != ICD_SCAN_NEW)
+    if(status != ICD_SCAN_NEW && status != ICD_SCAN_NOTIFY)
     {
         return;
     }
@@ -400,6 +401,19 @@ void ConnectionManager::scanResult(const QDBusMessage& rep)
     connection.name = args.value(8).toString();
     connection.strength = args.value(11).toInt();
 
+    for(int i = 0; i < connections_->size(); i++)
+    {
+        if(connections_->at(i).id == connection.id)
+        {
+            if(status == ICD_SCAN_NEW)
+            {
+                connections_->replace(i, connection);
+            }
+
+            return;
+        }
+    }
+
     connections_->push_back(connection);
 }
 
@@ -415,15 +429,31 @@ bool ConnectionManager::waitSignal(bool* ready)
 
     killTimer(timer_);
 
+    if(timeout_)
+    {
+        qDebug() << "Connection request timed out";
+    }
+
     return *ready || !timeout_;
 }
 
+void ConnectionManager::sleep(unsigned int ms)
+{
+    timeout_ = false;
+    timer_ = startTimer(ms);
+
+    while(!timeout_)
+    {
+        QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
+    }
+
+    killTimer(timer_);
+}
+
 void ConnectionManager::timerEvent(QTimerEvent* event)
 {
     Q_UNUSED(event);
     killTimer(timer_);
     timeout_ = true;
     timer_ = 0;
-
-    qDebug() << "Connection request timed out";
 }