#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
{
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)
+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,
SLOT(callTerminate()));
site_ = Eniro::stringToSite(Settings::instance()->get("site"));
- Settings::close();
qDebug() << "Starting...";
+ return true;
+
}
void CallListener::end()
retries_ = 0;
currentSearch_ = details.query;
- if(!connectionManager_->isConnected())
- {
- connectionManager_->connect();
- closeConnection_ = true;
- }
- else
+ if(!handleConnection())
{
- closeConnection_ = false;
+ qDebug() << "Unable to connect";
+ return;
}
showDelayedResult(tr("Searching..."), BANNER_DELAY);
{
qDebug() << "Error: " << eniro_->errorString();
- if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
+ if(retries_ < SEARCH_RETRIES && retries_ >= 0)
{
retries_++;
eniro_->search(Eniro::SearchDetails(currentSearch_));
else
{
timedMessage_ = "";
- message = tr("Search failed:") + " " + eniro_->errorString() + ".";
- showResult(message);
+ 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
retries_ = -1;
currentSearch_ = "";
- if(closeConnection_ && connectionManager_)
+ if(closeConnection_)
{
- connectionManager_->disconnect(true);
+ ConnectionManager cm;
+ cm.disconnect(true);
closeConnection_ = false;
}
}
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;
}
{
qDebug() << "Initializing search...";
- connectionManager_ = new ConnectionManager;
+ if(initialized_)
+ {
+ qDebug() << "Already initialized";
+ return;
+ }
eniro_ = new Eniro(site_);
eniro_->setMaxResults(1);
box_ = new InformationBox;
label_ = new QLabel("", box_);
label_->setMargin(8);
+ label_->setWordWrap(true);
box_->setWidget(label_);
initialized_ = true;
}
Eniro::SearchDetails const&, bool)));
}
- delete connectionManager_;
- connectionManager_ = 0;
delete eniro_;
eniro_ = 0;
delete box_;
box_ = 0;
+ label_ = 0;
+}
+
+bool CallListener::handleConnection()
+{
+ if(!initialized_)
+ {
+ return false;
+ }
+
+ ConnectionManager cm;
+
+ if(cm.isConnected())
+ {
+ closeConnection_ = false;
+ return true;
+ }
+
+ closeConnection_ = true;
+
+ Settings::ConnectionType configType = Settings::instance()->getConnectionType();
+
+ if(configType == Settings::ALWAYS_ASK)
+ {
+ showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
+ return false;
+ }
+
+ showDelayedResult(tr("Connecting..."), BANNER_DELAY);
+
+ 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_)
+ {
+ return false;
+ }
+
+ if(cm.getBestConnection(best, lookupType))
+ {
+ break;
+ }
+
+ qDebug() << "No connections found, retrying...";
+
+ cretries++;
+
+ sleep(WAIT_BETWEEN_RETRIES);
+
+ }
+
+ if(cretries >= CONNECTION_LOOKUP_RETRIES)
+ {
+ showError(tr("No available 3G or WLAN networks found."));
+ return false;
+ }
+
+ int retries = 0;
+
+ while(retries < CONNECT_RETRIES)
+ {
+ if(!initialized_)
+ {
+ return false;
+ }
+
+ sleep(WAIT_BETWEEN_RETRIES);
+
+ if(cm.connect(best.id))
+ {
+ break;
+ }
+ else if(cm.error() == ConnectionManager::INVALID_IAP)
+ {
+ 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 initialized_;
+}
+
+void CallListener::showError(QString const& msg, int timeout)
+{
+ qDebug() << "Error: " << msg;
+ box_->setTimeout(ERROR_BANNER_TIMEOUT);
+
+ 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);
+ }
}