Small fix to prevent daemon from closing itself.
[jenirok] / src / daemon / calllistener.cpp
index 4a2f1c5..5e2dd75 100644 (file)
@@ -48,6 +48,7 @@ retries_(-1), timer_(0)
 CallListener::~CallListener()
 {
     end();
+    DB::removeDatabase();
 }
 
 bool CallListener::begin()
@@ -77,8 +78,6 @@ bool CallListener::begin()
                        this,
                        SLOT(callTerminate()));
 
-    findGprsId();
-
     qDebug() << "Starting...";
 
     return true;
@@ -147,6 +146,13 @@ void CallListener::requestFinished(QVector <Source::Result> const& results,
                                    Source::SearchDetails const& details,
                                    bool error)
 {
+    if(closeConnection_)
+    {
+        closeConnection_ = false;
+        ConnectionManager cm;
+        cm.disconnect(true);
+    }
+
     // If box is not visible, the call must have been terminated already
     if(!initialized_ || !box_->isVisible())
     {
@@ -206,13 +212,6 @@ void CallListener::requestFinished(QVector <Source::Result> const& results,
 
     retries_ = -1;
     currentSearch_ = "";
-
-    if(closeConnection_)
-    {
-        closeConnection_ = false;
-        ConnectionManager cm;
-        cm.disconnect(true);
-    }
 }
 
 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
@@ -281,19 +280,7 @@ void CallListener::callTerminate()
         box_->hide();
     }
 
-    if(closeConnection_)
-    {
-        closeConnection_ = false;
-        ConnectionManager cm;
-        cm.disconnect(true);
-    }
-
     searchClose();
-
-    if(gprsId_.isEmpty())
-    {
-        findGprsId();
-    }
 }
 
 void CallListener::showDelayedResult(QString const& text, int delay)
@@ -347,6 +334,11 @@ void CallListener::searchInit()
 
 void CallListener::searchClose()
 {
+    if(!initialized_)
+    {
+        return;
+    }
+
     initialized_ = false;
 
     qDebug() << "Closing search...";
@@ -364,6 +356,13 @@ void CallListener::searchClose()
     delete box_;
     box_ = 0;
     label_ = 0;
+
+    if(closeConnection_)
+    {
+        closeConnection_ = false;
+        ConnectionManager cm;
+        cm.disconnect(true);
+    }
 }
 
 bool CallListener::handleConnection()
@@ -411,9 +410,14 @@ bool CallListener::handleConnection()
     }
 
     int cretries = 0;
+    int scans = 0;
+    bool found = false;
+    int maxScans = GPRS_SCANS;
 
-    bool tryGprs = (!gprsId_.isEmpty() &&
-                   (configType == Settings::ANY || configType == Settings::GPRS));
+    if(lookupType != ConnectionManager::GPRS)
+    {
+        maxScans = WLAN_SCANS;
+    }
 
     while(cretries < CONNECTION_LOOKUP_RETRIES)
     {
@@ -422,26 +426,32 @@ bool CallListener::handleConnection()
             return false;
         }
 
-        if(cm.getBestConnection(best, lookupType))
+        if(scans < maxScans)
         {
-            break;
+            if(cm.getBestConnection(best, lookupType))
+            {
+                found = true;
+            }
+
+            scans++;
         }
 
-        // ICD doesn't always find gprs connection during call, so
-        // try to use gprs anyway.
-        if(tryGprs && is3g())
+        // If there is only gprs connection available,
+        // make sure that we are on 3g network
+        if(found && (best.type != ConnectionManager::GPRS || is3g()))
         {
-            best.id = gprsId_;
-            qDebug() << "Trying gprs";
             break;
         }
 
+        if(found)
+        {
+            sleep(WAIT_BETWEEN_RETRIES);
+        }
+
         qDebug() << "No connections found, retrying...";
 
         cretries++;
 
-        sleep(WAIT_BETWEEN_RETRIES);
-
     }
 
     if(cretries >= CONNECTION_LOOKUP_RETRIES)
@@ -459,6 +469,8 @@ bool CallListener::handleConnection()
             return false;
         }
 
+        qDebug() << "Connecting to " << best.name;
+
         if(cm.connect(best.id))
         {
             break;
@@ -469,16 +481,26 @@ bool CallListener::handleConnection()
             return false;
         }
 
-        sleep(WAIT_BETWEEN_RETRIES);
+        retries++;
 
         qDebug() << "Unable to connect, retrying...";
-        retries++;
+
+        if(retries < CONNECT_RETRIES)
+        {
+            sendRetrySignal(best.id, initialized_);
+        }
 
     }
 
-    if(initialized_ && retries >= CONNECT_RETRIES)
+    if(retries >= CONNECT_RETRIES)
     {
-        showError(tr("Unable to connect to network."));
+        sendRetrySignal(best.id, false);
+
+        if(initialized_)
+        {
+            showError(tr("Unable to connect to network."));
+        }
+
         return false;
     }
 
@@ -488,6 +510,12 @@ bool CallListener::handleConnection()
 void CallListener::showError(QString const& msg, int timeout)
 {
     qDebug() << "Error: " << msg;
+
+    if(!initialized_ || !box_)
+    {
+        return;
+    }
+
     box_->setTimeout(ERROR_BANNER_TIMEOUT);
 
     if(timeout)
@@ -500,41 +528,6 @@ void CallListener::showError(QString const& msg, int timeout)
     }
 }
 
-void CallListener::timerEvent(QTimerEvent* event)
-{
-    Q_UNUSED(event);
-    killTimer(timer_);
-    timer_ = 0;
-}
-
-void CallListener::sleep(int ms)
-{
-    if(timer_)
-    {
-        killTimer(timer_);
-    }
-
-    timer_ = startTimer(ms);
-
-    while(timer_)
-    {
-        QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
-    }
-}
-
-void CallListener::findGprsId()
-{
-    ConnectionManager cm;
-
-    QList<ConnectionManager::Connection> connections;
-
-    if(cm.scanConnections(connections, ConnectionManager::GPRS) &&
-       connections.size() > 0)
-    {
-        gprsId_ = connections.at(0).id;
-    }
-}
-
 bool CallListener::is3g()
 {
     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
@@ -552,15 +545,48 @@ bool CallListener::is3g()
 
     uint status = rep.arguments().value(6).toUInt();
 
-    if(status & 0x10) // 3.5G (HSUPA)
+    if(status & 0x10 || status & 0x08)
     {
         return true;
     }
-    else if(status & 0x08) // 3G (HSDPA)
+
+    return false;
+}
+
+void CallListener::sendRetrySignal(QString const& iap, bool retry)
+{
+    QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
+                                                  "com.nokia.icd_ui",
+                                                  "retry");
+
+    QList<QVariant> arguments;
+    arguments.append(QVariant(iap));
+    arguments.append(QVariant(retry));
+    msg.setArguments(arguments);
+
+    QDBusConnection::systemBus().send(msg);
+
+    qDebug() << "Retry signal sent";
+}
+
+void CallListener::timerEvent(QTimerEvent* event)
+{
+    Q_UNUSED(event);
+    killTimer(timer_);
+    timer_ = 0;
+}
+
+void CallListener::sleep(int ms)
+{
+    if(timer_)
     {
-        return true;
+        killTimer(timer_);
     }
 
-    // Something else
-    return false;
+    timer_ = startTimer(ms);
+
+    while(timer_)
+    {
+        QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
+    }
 }