Connection handling in daemon improved. Added settings to allow automatic connection...
[jenirok] / src / daemon / calllistener.cpp
index 4f2f4b6..149e441 100644 (file)
@@ -40,7 +40,7 @@ QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
 
 CallListener::CallListener(): eniro_(0), connectionManager_(0),
 closeConnection_(false), initialized_(false), box_(0), label_(0),
-retries_(-1), site_(Eniro::FI)
+retries_(-1), site_(Eniro::FI), autoconnect_(false)
 {
 }
 
@@ -66,6 +66,8 @@ void CallListener::begin()
                        SLOT(callTerminate()));
 
     site_ = Eniro::stringToSite(Settings::instance()->get("site"));
+    connectionName_ = Settings::instance()->get("connection");
+    autoconnect_ = (Settings::instance()->get("autoconnect") == "1");
     Settings::close();
 
     qDebug() << "Starting...";
@@ -116,14 +118,10 @@ void CallListener::search(Eniro::SearchDetails const& details)
 
         showDelayedResult(tr("Searching..."), BANNER_DELAY);
 
-        if(!connectionManager_->isConnected())
+        if(!handleConnection())
         {
-            connectionManager_->connect();
-            closeConnection_ = true;
-        }
-        else
-        {
-            closeConnection_ = false;
+            qDebug() << "Unable to connect";
+            return;
         }
 
         qDebug() << "Starting to search...";
@@ -158,8 +156,7 @@ void CallListener::requestFinished(QVector <Eniro::Result> const& results,
         else
         {
             timedMessage_ = "";
-            message = tr("Search failed:") + " " + eniro_->errorString() + ".";
-            showResult(message);
+            showError(tr("Search failed:") + " " + eniro_->errorString() + ".");
         }
     }
     else
@@ -337,3 +334,100 @@ void CallListener::searchClose()
     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);
+}