Connection handling in daemon improved. Added settings to allow automatic connection...
[jenirok] / src / daemon / calllistener.cpp
index 840d849..149e441 100644 (file)
@@ -23,6 +23,7 @@
 #include "calllistener.h"
 #include "settings.h"
 #include "cache.h"
+#include "contactmanager.h"
 
 namespace
 {
@@ -37,9 +38,9 @@ namespace
 
 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
 
-CallListener::CallListener(): eniro_(0), contactManager_(0),
-connectionManager_(0), closeConnection_(false), box_(0), label_(0),
-retries_(-1)
+CallListener::CallListener(): eniro_(0), connectionManager_(0),
+closeConnection_(false), initialized_(false), box_(0), label_(0),
+retries_(-1), site_(Eniro::FI), autoconnect_(false)
 {
 }
 
@@ -64,23 +65,10 @@ void CallListener::begin()
                        this,
                        SLOT(callTerminate()));
 
-    contactManager_ = new ContactManager;
-
-    eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
-
-    eniro_->setMaxResults(1);
-    eniro_->setFindNumber(false);
-    eniro_->setTimeout(REQUEST_TIMEOUT);
-
-    connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
-                                           Eniro::SearchDetails const&, bool)),
-                                           this, SLOT(requestFinished(QVector <Eniro::Result> const&,
-                                                                      Eniro::SearchDetails const&, bool)));
-
-    box_ = new InformationBox();
-    label_ = new QLabel("", box_);
-    label_->setMargin(8);
-    box_->setWidget(label_);
+    site_ = Eniro::stringToSite(Settings::instance()->get("site"));
+    connectionName_ = Settings::instance()->get("connection");
+    autoconnect_ = (Settings::instance()->get("autoconnect") == "1");
+    Settings::close();
 
     qDebug() << "Starting...";
 
@@ -102,27 +90,23 @@ void CallListener::end()
                           this,
                           SLOT(callTerminate()));
 
-    delete contactManager_;
-    contactManager_ = 0;
-    delete connectionManager_;
-    connectionManager_ = 0;
-    delete eniro_;
-    eniro_ = 0;
-    delete box_;
-    box_ = 0;
-    delete label_;
-    label_ = 0;
+    searchClose();
+
 }
 
 void CallListener::search(Eniro::SearchDetails const& details)
 {
     qDebug() << "Search called";
 
+    searchInit();
+
     Eniro::Result result;
 
     if(Cache::instance().findItem(details.query, result))
     {
 
+        qDebug() << "Found from cache";
+
         showDelayedResult(createResult(result.name,
                                        result.street,
                                        result.city), BANNER_DELAY);
@@ -132,25 +116,16 @@ void CallListener::search(Eniro::SearchDetails const& details)
         retries_ = 0;
         currentSearch_ = details.query;
 
-        if(connectionManager_)
-        {
-            delete connectionManager_;
-            connectionManager_ = 0;
-        }
+        showDelayedResult(tr("Searching..."), BANNER_DELAY);
 
-        connectionManager_ = new ConnectionManager;
-
-        if(!connectionManager_->isConnected())
-        {
-            connectionManager_->connect();
-            closeConnection_ = true;
-        }
-        else
+        if(!handleConnection())
         {
-            closeConnection_ = false;
+            qDebug() << "Unable to connect";
+            return;
         }
 
-        showDelayedResult(tr("Searching..."), 200);
+        qDebug() << "Starting to search...";
+
         eniro_->search(details);
     }
 
@@ -161,7 +136,7 @@ void CallListener::requestFinished(QVector <Eniro::Result> const& results,
                                    bool error)
 {
     // If box is not visible, the call must have been terminated already
-    if(!box_->isVisible())
+    if(!initialized_ || !box_->isVisible())
     {
         return;
     }
@@ -180,37 +155,38 @@ void CallListener::requestFinished(QVector <Eniro::Result> const& results,
         }
         else
         {
-            message = tr("Search failed:") + " " + eniro_->errorString() + ".";
+            timedMessage_ = "";
+            showError(tr("Search failed:") + " " + eniro_->errorString() + ".");
         }
     }
-    else if(results.size() == 0)
-    {
-        message = tr("Phone number was not found");
-    }
     else
     {
-        message = createResult(results.at(0).name, results.at(0).street,
-                               results.at(0).city);
-        Eniro::Result result = results.at(0);
-        result.number = details.query;
-        Cache::instance().addItem(result);
+        timedMessage_ = "";
+
+        if(results.size() == 0)
+        {
+            message = tr("Phone number was not found");
+            showResult(message);
+        }
+        else
+        {
+            message = createResult(results.at(0).name, results.at(0).street,
+                                   results.at(0).city);
+            showResult(message);
+            Eniro::Result result = results.at(0);
+            result.number = details.query;
+            Cache::instance().addItem(result);
+        }
     }
 
     retries_ = -1;
     currentSearch_ = "";
-    timedMessage_ = "";
-
-    showResult(message);
 
     if(closeConnection_ && connectionManager_)
     {
         connectionManager_->disconnect(true);
         closeConnection_ = false;
     }
-
-    delete connectionManager_;
-    connectionManager_ = 0;
-
 }
 
 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
@@ -234,6 +210,11 @@ QString CallListener::createResult(QString const& name, QString const& street, Q
 
 void CallListener::showResult(QString const& text)
 {
+    if(!initialized_)
+    {
+        return;
+    }
+
     label_->setText("<font color='black'>" + text + "</font>");
 
     if(box_->isVisible())
@@ -248,7 +229,9 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number)
 {
     qDebug() << "Incoming: " << number;
 
-    if(!contactManager_->numberExists(number))
+    ContactManager cm;
+
+    if(!cm.numberExists(number))
     {
         qDebug() << "Number doesn't exist";
 
@@ -259,8 +242,6 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number)
                            this,
                            SLOT(callTerminate()));
 
-        qDebug() << "Going to search";
-
         search(Eniro::SearchDetails(number));
     }
     else
@@ -271,7 +252,7 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number)
 
 void CallListener::callTerminate()
 {
-    if(box_->isVisible())
+    if(initialized_ && box_ && box_->isVisible())
     {
         box_->hide();
     }
@@ -282,8 +263,7 @@ void CallListener::callTerminate()
         closeConnection_ = false;
     }
 
-    delete connectionManager_;
-    connectionManager_ = 0;
+    searchClose();
 }
 
 void CallListener::showDelayedResult(QString const& text, int delay)
@@ -294,7 +274,7 @@ void CallListener::showDelayedResult(QString const& text, int delay)
 
 void CallListener::showTimedMessage()
 {
-    if(timedMessage_.size() == 0)
+    if(timedMessage_.size() == 0 || !initialized_)
     {
         return;
     }
@@ -303,3 +283,151 @@ void CallListener::showTimedMessage()
 
     timedMessage_ = "";
 }
+
+void CallListener::searchInit()
+{
+    qDebug() << "Initializing search...";
+
+    if(initialized_)
+    {
+        qDebug() << "Already initialized";
+        return;
+    }
+
+    connectionManager_ = new ConnectionManager;
+
+    eniro_ = new Eniro(site_);
+    eniro_->setMaxResults(1);
+    eniro_->setFindNumber(false);
+    eniro_->setTimeout(REQUEST_TIMEOUT);
+
+    connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
+                                           Eniro::SearchDetails const&, bool)),
+                                           this, SLOT(requestFinished(QVector <Eniro::Result> const&,
+                                                                      Eniro::SearchDetails const&, bool)));
+    box_ = new InformationBox;
+    label_ = new QLabel("", box_);
+    label_->setMargin(8);
+    box_->setWidget(label_);
+    initialized_ = true;
+}
+
+void CallListener::searchClose()
+{
+    initialized_ = false;
+
+    qDebug() << "Closing search...";
+
+    if(eniro_)
+    {
+        disconnect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
+                                                  Eniro::SearchDetails const&, bool)),
+                                                  this, SLOT(requestFinished(QVector <Eniro::Result> const&,
+                                                                             Eniro::SearchDetails const&, bool)));
+    }
+
+    delete connectionManager_;
+    connectionManager_ = 0;
+    delete eniro_;
+    eniro_ = 0;
+    delete box_;
+    box_ = 0;
+    label_ = 0;
+}
+
+bool CallListener::handleConnection()
+{
+    if(!connectionManager_)
+    {
+        qDebug() << "Error: connection manager not initialized";
+        return false;
+    }
+
+    if(connectionManager_->isConnected())
+    {
+        closeConnection_ = false;
+        return true;
+    }
+
+    closeConnection_ = true;
+    int retries = 0;
+
+    if(autoconnect_)
+    {
+        QString conn;
+
+        if(connectionName_.size() != 0 && connectionName_ != "0")
+        {
+            conn = connectionName_;
+        }
+        else
+        {
+            ConnectionManager::Connection best;
+
+            if(!connectionManager_->getBestConnection(best))
+            {
+                showError(tr("No network connections found."));
+                return false;
+            }
+
+            conn = best.id;
+        }
+
+        while(retries < CONNECT_RETRIES)
+        {
+            if(connectionManager_->connect(conn))
+            {
+                break;
+            }
+            else if(connectionManager_->error() == ConnectionManager::INVALID_IAP)
+            {
+                showError(tr("Selected access point doesn't exist."));
+                return false;
+            }
+
+            retries++;
+        }
+    }
+    else
+    {
+        while(retries < CONNECT_RETRIES)
+        {
+            if(connectionManager_->connect())
+            {
+                break;
+            }
+            else
+            {
+                qDebug() << "Automatic connection failed";
+            }
+
+            retries++;
+        }
+    }
+
+    if(retries >= CONNECT_RETRIES)
+    {
+        ConnectionManager::NetworkMode mode = connectionManager_->getNetworkMode();
+
+        if(mode != ConnectionManager::NETWORK_3G &&
+           mode != ConnectionManager::NETWORK_3_5G)
+        {
+            showError(tr("No 3G or WLAN network available."));
+        }
+        else
+        {
+            showError(tr("Unable to connect to network."));
+        }
+
+        return false;
+    }
+
+    return true;
+}
+
+void CallListener::showError(QString const& msg)
+{
+    qDebug() << "Error: " << msg;
+    box_->setTimeout(ERROR_BANNER_TIMEOUT);
+    showResult(msg);
+}