Some error banner delays fixed.
[jenirok] / src / daemon / calllistener.cpp
index 149e441..4307347 100644 (file)
 
 #include <QtCore/QDebug>
 #include <QtCore/QTimer>
-#include <QtSql/QSqlQuery>
-#include <QtSql/QSqlError>
 #include "calllistener.h"
 #include "settings.h"
 #include "cache.h"
 #include "contactmanager.h"
+#include "connectionmanager.h"
+#include "db.h"
 
 namespace
 {
@@ -38,19 +38,26 @@ namespace
 
 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
 
-CallListener::CallListener(): eniro_(0), connectionManager_(0),
+CallListener::CallListener(): eniro_(0),
 closeConnection_(false), initialized_(false), box_(0), label_(0),
-retries_(-1), site_(Eniro::FI), autoconnect_(false)
+retries_(-1), site_(Eniro::FI), timer_(0)
 {
 }
 
 CallListener::~CallListener()
 {
     end();
+    DB::removeDatabase();
 }
 
-void CallListener::begin()
+bool CallListener::begin()
 {
+    if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
+    {
+        qDebug() << "Bad connection settings, unable to start";
+        return false;
+    }
+
     systemBus_.connect(CALL_SERVICE_NAME,
                        CALL_SERVICE_PATH,
                        CALL_SERVICE_INTERFACE,
@@ -66,12 +73,11 @@ 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...";
 
+    return true;
+
 }
 
 void CallListener::end()
@@ -116,14 +122,14 @@ void CallListener::search(Eniro::SearchDetails const& details)
         retries_ = 0;
         currentSearch_ = details.query;
 
-        showDelayedResult(tr("Searching..."), BANNER_DELAY);
-
         if(!handleConnection())
         {
             qDebug() << "Unable to connect";
             return;
         }
 
+        showDelayedResult(tr("Searching..."), BANNER_DELAY);
+
         qDebug() << "Starting to search...";
 
         eniro_->search(details);
@@ -147,7 +153,7 @@ void CallListener::requestFinished(QVector <Eniro::Result> const& results,
     {
         qDebug() << "Error: " << eniro_->errorString();
 
-        if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
+        if(retries_ < SEARCH_RETRIES && retries_ >= 0)
         {
             retries_++;
             eniro_->search(Eniro::SearchDetails(currentSearch_));
@@ -156,7 +162,20 @@ void CallListener::requestFinished(QVector <Eniro::Result> const& results,
         else
         {
             timedMessage_ = "";
-            showError(tr("Search failed:") + " " + eniro_->errorString() + ".");
+            QString errorString;
+            Eniro::Error error = eniro_->error();
+
+            switch(error)
+            {
+            case Eniro::TIMEOUT:
+                errorString = tr("Request timed out");
+                break;
+            default:
+                errorString = eniro_->errorString();
+                break;
+            }
+
+            showError(tr("Searching failed:") + " " + errorString + ".");
         }
     }
     else
@@ -182,9 +201,10 @@ void CallListener::requestFinished(QVector <Eniro::Result> const& results,
     retries_ = -1;
     currentSearch_ = "";
 
-    if(closeConnection_ && connectionManager_)
+    if(closeConnection_)
     {
-        connectionManager_->disconnect(true);
+        ConnectionManager cm;
+        cm.disconnect(true);
         closeConnection_ = false;
     }
 }
@@ -252,14 +272,15 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number)
 
 void CallListener::callTerminate()
 {
-    if(initialized_ && box_ && box_->isVisible())
+    if(box_ && box_->isVisible())
     {
         box_->hide();
     }
 
-    if(closeConnection_ && connectionManager_)
+    if(closeConnection_)
     {
-        connectionManager_->disconnect(true);
+        ConnectionManager cm;
+        cm.disconnect(true);
         closeConnection_ = false;
     }
 
@@ -294,8 +315,6 @@ void CallListener::searchInit()
         return;
     }
 
-    connectionManager_ = new ConnectionManager;
-
     eniro_ = new Eniro(site_);
     eniro_->setMaxResults(1);
     eniro_->setFindNumber(false);
@@ -308,6 +327,7 @@ void CallListener::searchInit()
     box_ = new InformationBox;
     label_ = new QLabel("", box_);
     label_->setMargin(8);
+    label_->setWordWrap(true);
     box_->setWidget(label_);
     initialized_ = true;
 }
@@ -326,8 +346,6 @@ void CallListener::searchClose()
                                                                              Eniro::SearchDetails const&, bool)));
     }
 
-    delete connectionManager_;
-    connectionManager_ = 0;
     delete eniro_;
     eniro_ = 0;
     delete box_;
@@ -337,97 +355,144 @@ void CallListener::searchClose()
 
 bool CallListener::handleConnection()
 {
-    if(!connectionManager_)
+    if(!initialized_)
     {
-        qDebug() << "Error: connection manager not initialized";
         return false;
     }
 
-    if(connectionManager_->isConnected())
+    ConnectionManager cm;
+
+    if(cm.isConnected())
     {
         closeConnection_ = false;
         return true;
     }
 
     closeConnection_ = true;
-    int retries = 0;
 
-    if(autoconnect_)
+    Settings::ConnectionType configType = Settings::instance()->getConnectionType();
+
+    if(configType == Settings::ALWAYS_ASK)
     {
-        QString conn;
+        showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
+        return false;
+    }
+
+    showDelayedResult(tr("Connecting..."), BANNER_DELAY);
 
-        if(connectionName_.size() != 0 && connectionName_ != "0")
+    ConnectionManager::Connection best;
+
+    ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
+
+    switch(configType)
+    {
+    case Settings::WLAN:
+        lookupType = ConnectionManager::WLAN;
+        break;
+    case Settings::GPRS:
+        lookupType = ConnectionManager::GPRS;
+        break;
+    default:
+        lookupType = ConnectionManager::NO_TYPE;
+        break;
+    }
+
+    int cretries = 0;
+
+    while(cretries < CONNECTION_LOOKUP_RETRIES)
+    {
+        if(!initialized_)
         {
-            conn = connectionName_;
+            return false;
         }
-        else
+
+        if(cm.getBestConnection(best, lookupType))
         {
-            ConnectionManager::Connection best;
+            break;
+        }
 
-            if(!connectionManager_->getBestConnection(best))
-            {
-                showError(tr("No network connections found."));
-                return false;
-            }
+        qDebug() << "No connections found, retrying...";
 
-            conn = best.id;
-        }
+        cretries++;
 
-        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;
-            }
+        sleep(WAIT_BETWEEN_RETRIES);
 
-            retries++;
-        }
     }
-    else
-    {
-        while(retries < CONNECT_RETRIES)
-        {
-            if(connectionManager_->connect())
-            {
-                break;
-            }
-            else
-            {
-                qDebug() << "Automatic connection failed";
-            }
 
-            retries++;
-        }
+    if(cretries >= CONNECTION_LOOKUP_RETRIES)
+    {
+        showError(tr("No available 3G or WLAN networks found."));
+        return false;
     }
 
-    if(retries >= CONNECT_RETRIES)
+    int retries = 0;
+
+    while(retries < CONNECT_RETRIES)
     {
-        ConnectionManager::NetworkMode mode = connectionManager_->getNetworkMode();
+        if(!initialized_)
+        {
+            return false;
+        }
+
+        sleep(WAIT_BETWEEN_RETRIES);
 
-        if(mode != ConnectionManager::NETWORK_3G &&
-           mode != ConnectionManager::NETWORK_3_5G)
+        if(cm.connect(best.id))
         {
-            showError(tr("No 3G or WLAN network available."));
+            break;
         }
-        else
+        else if(cm.error() == ConnectionManager::INVALID_IAP)
         {
-            showError(tr("Unable to connect to network."));
+            showError(tr("Selected access point doesn't exist."));
+            return false;
         }
 
+        qDebug() << "Unable to connect, retrying...";
+        retries++;
+
+    }
+
+    if(initialized_ && retries >= CONNECT_RETRIES)
+    {
+        showError(tr("Unable to connect to network."));
         return false;
     }
 
-    return true;
+    return initialized_;
 }
 
-void CallListener::showError(QString const& msg)
+void CallListener::showError(QString const& msg, int timeout)
 {
     qDebug() << "Error: " << msg;
     box_->setTimeout(ERROR_BANNER_TIMEOUT);
-    showResult(msg);
+
+    if(timeout)
+    {
+        showDelayedResult(msg, timeout);
+    }
+    else
+    {
+        showResult(msg);
+    }
+}
+
+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);
+    }
 }