Some fixes to connection manager.
[jenirok] / src / daemon / calllistener.cpp
index 4a2f1c5..c76cfdc 100644 (file)
@@ -18,6 +18,7 @@
 
 #include <QtCore/QDebug>
 #include <QtCore/QTimer>
+#include <QtCore/QDateTime>
 #include "calllistener.h"
 #include "settings.h"
 #include "cache.h"
@@ -35,19 +36,21 @@ namespace
     const QString CALL_SIGNAL_INCOMING = "Coming";
     const QString CALL_SIGNAL_RELEASE = "Release";
     const QString CALL_SIGNAL_TERMINATED = "Terminated";
+    const QString CALL_SIGNAL_ANSWERED = "AudioConnect";
 }
 
 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
 
 CallListener::CallListener(): source_(0),
 closeConnection_(false), initialized_(false), box_(0), label_(0),
-retries_(-1), timer_(0)
+retries_(-1), timer_(0), currentCall_(0)
 {
 }
 
 CallListener::~CallListener()
 {
     end();
+    DB::removeDatabase();
 }
 
 bool CallListener::begin()
@@ -59,8 +62,10 @@ bool CallListener::begin()
     }
 
     sourceId_ = Source::stringToId(Settings::instance()->get("source"));
+    QMap<QString, QString> tmpConfig;
     SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
-    config->getConfig(sourceConfig_);
+    config->getConfig(tmpConfig);
+    sourceConfig_ = tmpConfig;
     delete config;
 
     systemBus_.connect(CALL_SERVICE_NAME,
@@ -77,8 +82,6 @@ bool CallListener::begin()
                        this,
                        SLOT(callTerminate()));
 
-    findGprsId();
-
     qDebug() << "Starting...";
 
     return true;
@@ -108,7 +111,15 @@ void CallListener::end()
 
 void CallListener::search(Source::SearchDetails const& details)
 {
-    qDebug() << "Search called";
+    if(currentCall_)
+    {
+        delete currentCall_;
+    }
+
+    currentCall_ = new CallDetails;
+    currentCall_->number = details.query;
+    currentCall_->time = QDateTime::currentDateTime().toTime_t();
+    currentCall_->answered = false;
 
     searchInit();
 
@@ -122,11 +133,12 @@ void CallListener::search(Source::SearchDetails const& details)
         showDelayedResult(createResult(result.name,
                                        result.street,
                                        result.city), BANNER_DELAY);
+
+        currentCall_->result = result;
     }
     else
     {
         retries_ = 0;
-        currentSearch_ = details.query;
 
         if(!handleConnection())
         {
@@ -136,8 +148,6 @@ void CallListener::search(Source::SearchDetails const& details)
 
         showDelayedResult(tr("Searching..."), BANNER_DELAY);
 
-        qDebug() << "Starting to search...";
-
         source_->search(details);
     }
 
@@ -147,8 +157,17 @@ void CallListener::requestFinished(QVector <Source::Result> const& results,
                                    Source::SearchDetails const& details,
                                    bool error)
 {
+    /*if(closeConnection_)
+    {
+        closeConnection_ = false;
+        ConnectionManager cm;
+        cm.disconnect(true);
+    }*/
+
+    qDebug() << "Request finished";
+
     // If box is not visible, the call must have been terminated already
-    if(!initialized_ || !box_->isVisible())
+    if(!initialized_ || !box_->isVisible() || !currentCall_)
     {
         return;
     }
@@ -162,7 +181,7 @@ void CallListener::requestFinished(QVector <Source::Result> const& results,
         if(retries_ < SEARCH_RETRIES && retries_ >= 0)
         {
             retries_++;
-            source_->search(Source::SearchDetails(currentSearch_));
+            source_->search(Source::SearchDetails(currentCall_->number, "", Source::BOTH));
             return;
         }
         else
@@ -201,18 +220,11 @@ void CallListener::requestFinished(QVector <Source::Result> const& results,
             Source::Result result = results.at(0);
             result.number = details.query;
             Cache::instance().addItem(result);
+            currentCall_->result = results.at(0);
         }
     }
 
     retries_ = -1;
-    currentSearch_ = "";
-
-    if(closeConnection_)
-    {
-        closeConnection_ = false;
-        ConnectionManager cm;
-        cm.disconnect(true);
-    }
 }
 
 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
@@ -253,11 +265,17 @@ void CallListener::showResult(QString const& text)
 
 void CallListener::incomingCall(QDBusObjectPath path, QString number)
 {
+    if(number.isEmpty())
+    {
+        qDebug() << "Unknown caller without number";
+        return;
+    }
+
     ContactManager cm;
 
     if(!cm.numberExists(number))
     {
-        qDebug() << "Number doesn't exist";
+        qDebug() << "Number doesn't exist: " << number;
 
         systemBus_.connect(CALL_SERVICE_NAME,
                            path.path(),
@@ -266,11 +284,18 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number)
                            this,
                            SLOT(callTerminate()));
 
-        search(Source::SearchDetails(number));
+        systemBus_.connect(CALL_SERVICE_NAME,
+                           path.path(),
+                           CALL_SERVICE_INSTANCE_NAME,
+                           CALL_SIGNAL_ANSWERED,
+                           this,
+                           SLOT(handleAnswer()));
+
+        search(Source::SearchDetails(number, "", Source::BOTH));
     }
     else
     {
-        qDebug() << "Number exists";
+        qDebug() << "Number exists: " << number;
     }
 }
 
@@ -281,18 +306,24 @@ void CallListener::callTerminate()
         box_->hide();
     }
 
-    if(closeConnection_)
+    if(currentCall_)
     {
-        closeConnection_ = false;
-        ConnectionManager cm;
-        cm.disconnect(true);
+        currentCall_->result.number = currentCall_->number;
+        Cache::instance().logItem(currentCall_->result, !currentCall_->answered, currentCall_->time);
+        delete currentCall_;
+        currentCall_ = 0;
     }
 
     searchClose();
+}
+
+void CallListener::handleAnswer()
+{
+    qDebug() << "Answered";
 
-    if(gprsId_.isEmpty())
+    if(currentCall_)
     {
-        findGprsId();
+        currentCall_->answered = true;
     }
 }
 
@@ -347,6 +378,11 @@ void CallListener::searchInit()
 
 void CallListener::searchClose()
 {
+    if(!initialized_)
+    {
+        return;
+    }
+
     initialized_ = false;
 
     qDebug() << "Closing search...";
@@ -364,6 +400,21 @@ void CallListener::searchClose()
     delete box_;
     box_ = 0;
     label_ = 0;
+
+    if(closeConnection_)
+    {
+        QTimer::singleShot(500, this, SLOT(closeConnection()));
+    }
+}
+
+void CallListener::closeConnection()
+{
+    if(closeConnection_)
+    {
+        closeConnection_ = false;
+        ConnectionManager cm;
+        cm.disconnect(true);
+    }
 }
 
 bool CallListener::handleConnection()
@@ -377,6 +428,7 @@ bool CallListener::handleConnection()
 
     if(cm.isConnected())
     {
+        cm.connect();
         closeConnection_ = false;
         return true;
     }
@@ -411,9 +463,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 +479,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 +522,8 @@ bool CallListener::handleConnection()
             return false;
         }
 
+        qDebug() << "Connecting to " << best.name << " (" << best.id << ")";
+
         if(cm.connect(best.id))
         {
             break;
@@ -469,16 +534,27 @@ bool CallListener::handleConnection()
             return false;
         }
 
-        sleep(WAIT_BETWEEN_RETRIES);
+        retries++;
 
         qDebug() << "Unable to connect, retrying...";
-        retries++;
+
+        if(retries < CONNECT_RETRIES)
+        {
+            sendRetrySignal(best.id, false);
+            sleep(WAIT_BETWEEN_RETRIES);
+        }
 
     }
 
-    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 +564,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 +582,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 +599,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);
+    }
 }