One more fix to connection handling.
[jenirok] / src / daemon / calllistener.cpp
index 9110ff7..e73bf26 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,
@@ -106,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();
 
@@ -120,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())
         {
@@ -134,8 +148,6 @@ void CallListener::search(Source::SearchDetails const& details)
 
         showDelayedResult(tr("Searching..."), BANNER_DELAY);
 
-        qDebug() << "Starting to search...";
-
         source_->search(details);
     }
 
@@ -145,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;
     }
@@ -160,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
@@ -199,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)
@@ -251,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(),
@@ -264,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;
     }
 }
 
@@ -279,16 +306,27 @@ 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(currentCall_)
+    {
+        currentCall_->answered = true;
+    }
+}
+
 void CallListener::showDelayedResult(QString const& text, int delay)
 {
     timedMessage_ = text;
@@ -340,6 +378,11 @@ void CallListener::searchInit()
 
 void CallListener::searchClose()
 {
+    if(!initialized_)
+    {
+        return;
+    }
+
     initialized_ = false;
 
     qDebug() << "Closing search...";
@@ -357,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()
@@ -370,6 +428,7 @@ bool CallListener::handleConnection()
 
     if(cm.isConnected())
     {
+        cm.connect();
         closeConnection_ = false;
         return true;
     }
@@ -408,7 +467,7 @@ bool CallListener::handleConnection()
     bool found = false;
     int maxScans = GPRS_SCANS;
 
-    if(lookupType == ConnectionManager::WLAN)
+    if(lookupType != ConnectionManager::GPRS)
     {
         maxScans = WLAN_SCANS;
     }
@@ -463,10 +522,11 @@ bool CallListener::handleConnection()
             return false;
         }
 
-        qDebug() << "Connecting to " << best.name;
+        qDebug() << "Connecting to " << best.name << " (" << best.id << ")";
 
         if(cm.connect(best.id))
         {
+            sleep(500);
             break;
         }
         else if(cm.error() == ConnectionManager::INVALID_IAP)
@@ -475,16 +535,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;
     }
 
@@ -529,19 +600,30 @@ bool CallListener::is3g()
 
     uint status = rep.arguments().value(6).toUInt();
 
-    if(status & 0x10) // 3.5G (HSUPA)
-    {
-        return true;
-    }
-    else if(status & 0x08) // 3G (HSDPA)
+    if(status & 0x10 || status & 0x08)
     {
         return true;
     }
 
-    // Something else
     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);