#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 "sourcecoreconfig.h"
+#include "db.h"
namespace
{
QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
-CallListener::CallListener(): eniro_(0), contactManager_(0),
-connectionManager_(0), closeConnection_(false), box_(0), label_(0),
-retries_(-1)
+CallListener::CallListener(): source_(0),
+closeConnection_(false), initialized_(false), box_(0), label_(0),
+retries_(-1), 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;
+ }
+
+ sourceId_ = Source::stringToId(Settings::instance()->get("source"));
+ SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
+ config->getConfig(sourceConfig_);
+ delete config;
+
systemBus_.connect(CALL_SERVICE_NAME,
CALL_SERVICE_PATH,
CALL_SERVICE_INTERFACE,
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_);
-
qDebug() << "Starting...";
+ return true;
+
}
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();
+ sourceConfig_.clear();
+
}
-void CallListener::search(Eniro::SearchDetails const& details)
+void CallListener::search(Source::SearchDetails const& details)
{
qDebug() << "Search called";
- Eniro::Result result;
+ searchInit();
+
+ Source::Result result;
if(Cache::instance().findItem(details.query, result))
{
+ qDebug() << "Found from cache";
+
showDelayedResult(createResult(result.name,
- result.street,
- result.city), BANNER_DELAY);
+ result.street,
+ result.city), BANNER_DELAY);
}
else
{
- showResult(tr("Searching..."));
retries_ = 0;
currentSearch_ = details.query;
- if(connectionManager_)
+ if(!handleConnection())
{
- delete connectionManager_;
- connectionManager_ = 0;
+ qDebug() << "Unable to connect";
+ return;
}
- connectionManager_ = new ConnectionManager;
+ showDelayedResult(tr("Searching..."), BANNER_DELAY);
- if(!connectionManager_->isConnected())
- {
- connectionManager_->connect();
- closeConnection_ = true;
- }
- else
- {
- closeConnection_ = false;
- }
+ qDebug() << "Starting to search...";
- eniro_->search(details);
+ source_->search(details);
}
}
-void CallListener::requestFinished(QVector <Eniro::Result> const& results,
- Eniro::SearchDetails const& details,
+void CallListener::requestFinished(QVector <Source::Result> const& results,
+ Source::SearchDetails const& details,
bool error)
{
+ if(closeConnection_)
+ {
+ closeConnection_ = false;
+ ConnectionManager cm;
+ cm.disconnect(true);
+ }
+
// If box is not visible, the call must have been terminated already
- if(!box_->isVisible())
+ if(!initialized_ || !box_->isVisible())
{
return;
}
if(error)
{
- qDebug() << "Error: " << eniro_->errorString();
+ qDebug() << "Error: " << source_->errorString();
- if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
+ if(retries_ < SEARCH_RETRIES && retries_ >= 0)
{
retries_++;
- eniro_->search(Eniro::SearchDetails(currentSearch_));
+ source_->search(Source::SearchDetails(currentSearch_));
return;
}
else
{
- message = tr("Search failed:") + " " + eniro_->errorString() + ".";
+ timedMessage_ = "";
+ QString errorString;
+ Source::Error error = source_->error();
+
+ switch(error)
+ {
+ case Source::TIMEOUT:
+ errorString = tr("Request timed out");
+ break;
+ default:
+ errorString = source_->errorString();
+ break;
+ }
+
+ showError(tr("Searching failed:") + " " + 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);
+ Source::Result result = results.at(0);
+ result.number = details.query;
+ Cache::instance().addItem(result);
+ }
}
retries_ = -1;
currentSearch_ = "";
-
- if(closeConnection_)
- {
- connectionManager_->disconnect();
- closeConnection_ = false;
- }
-
- delete connectionManager_;
- connectionManager_ = 0;
-
- showResult(message);
-
}
QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
void CallListener::showResult(QString const& text)
{
+ if(!initialized_)
+ {
+ return;
+ }
+
label_->setText("<font color='black'>" + text + "</font>");
if(box_->isVisible())
void CallListener::incomingCall(QDBusObjectPath path, QString number)
{
- qDebug() << "Incoming: " << number;
+ ContactManager cm;
- if(!contactManager_->numberExists(number))
+ if(!cm.numberExists(number))
{
qDebug() << "Number doesn't exist";
this,
SLOT(callTerminate()));
- qDebug() << "Going to search";
-
- search(Eniro::SearchDetails(number));
+ search(Source::SearchDetails(number));
}
else
{
void CallListener::callTerminate()
{
- if(box_->isVisible())
+ if(box_ && box_->isVisible())
{
box_->hide();
}
- if(closeConnection_ && connectionManager_)
- {
- connectionManager_->disconnect();
- closeConnection_ = false;
- }
-
- delete connectionManager_;
- connectionManager_ = 0;
+ searchClose();
}
void CallListener::showDelayedResult(QString const& text, int delay)
void CallListener::showTimedMessage()
{
- if(timedMessage_.size() == 0)
+ if(timedMessage_.size() == 0 || !initialized_)
{
return;
}
timedMessage_ = "";
}
+
+void CallListener::searchInit()
+{
+ qDebug() << "Initializing search...";
+
+ if(initialized_)
+ {
+ qDebug() << "Already initialized";
+ return;
+ }
+
+ source_ = Source::getSource(sourceId_);
+ SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
+ config->loadFromConfig(sourceConfig_);
+ config->apply(source_);
+ delete config;
+ source_->setMaxResults(1);
+ source_->setFindNumber(false);
+ source_->setTimeout(REQUEST_TIMEOUT);
+
+ connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
+ Source::SearchDetails const&, bool)),
+ this, SLOT(requestFinished(QVector <Source::Result> const&,
+ Source::SearchDetails const&, bool)));
+ box_ = new InformationBox;
+ label_ = new QLabel("", box_);
+ label_->setMargin(8);
+ label_->setWordWrap(true);
+ box_->setWidget(label_);
+ initialized_ = true;
+}
+
+void CallListener::searchClose()
+{
+ if(!initialized_)
+ {
+ return;
+ }
+
+ initialized_ = false;
+
+ qDebug() << "Closing search...";
+
+ if(source_)
+ {
+ disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
+ Source::SearchDetails const&, bool)),
+ this, SLOT(requestFinished(QVector <Source::Result> const&,
+ Source::SearchDetails const&, bool)));
+ }
+
+ delete source_;
+ source_ = 0;
+ delete box_;
+ box_ = 0;
+ label_ = 0;
+
+ if(closeConnection_)
+ {
+ closeConnection_ = false;
+ ConnectionManager cm;
+ cm.disconnect(true);
+ }
+}
+
+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;
+ int scans = 0;
+ bool found = false;
+ int maxScans = GPRS_SCANS;
+
+ if(lookupType != ConnectionManager::GPRS)
+ {
+ maxScans = WLAN_SCANS;
+ }
+
+ while(cretries < CONNECTION_LOOKUP_RETRIES)
+ {
+ if(!initialized_)
+ {
+ return false;
+ }
+
+ if(scans < maxScans)
+ {
+ if(cm.getBestConnection(best, lookupType))
+ {
+ found = true;
+ }
+
+ scans++;
+ }
+
+ // If there is only gprs connection available,
+ // make sure that we are on 3g network
+ if(found && (best.type != ConnectionManager::GPRS || is3g()))
+ {
+ break;
+ }
+
+ if(found)
+ {
+ sleep(WAIT_BETWEEN_RETRIES);
+ }
+
+ qDebug() << "No connections found, retrying...";
+
+ cretries++;
+
+ }
+
+ 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;
+ }
+
+ qDebug() << "Connecting to " << best.name;
+
+ if(cm.connect(best.id))
+ {
+ break;
+ }
+ else if(cm.error() == ConnectionManager::INVALID_IAP)
+ {
+ showError(tr("Selected access point doesn't exist."));
+ return false;
+ }
+
+ retries++;
+
+ qDebug() << "Unable to connect, retrying...";
+
+ if(retries < CONNECT_RETRIES)
+ {
+ sendRetrySignal(best.id, initialized_);
+ }
+
+ }
+
+ if(retries >= CONNECT_RETRIES)
+ {
+ sendRetrySignal(best.id, false);
+
+ if(initialized_)
+ {
+ showError(tr("Unable to connect to network."));
+ }
+
+ return false;
+ }
+
+ return initialized_;
+}
+
+void CallListener::showError(QString const& msg, int timeout)
+{
+ qDebug() << "Error: " << msg;
+
+ if(!initialized_ || !box_)
+ {
+ return;
+ }
+
+ box_->setTimeout(ERROR_BANNER_TIMEOUT);
+
+ if(timeout)
+ {
+ showDelayedResult(msg, timeout);
+ }
+ else
+ {
+ showResult(msg);
+ }
+}
+
+bool CallListener::is3g()
+{
+ QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
+ "/com/nokia/phone/net",
+ "Phone.Net",
+ "get_registration_status");
+
+ QDBusMessage rep = systemBus_.call(msg);
+
+ if(rep.type() == QDBusMessage::ErrorMessage)
+ {
+ qDebug() << "Unable to get network status";
+ return false;
+ }
+
+ uint status = rep.arguments().value(6).toUInt();
+
+ if(status & 0x10 || status & 0x08)
+ {
+ return true;
+ }
+
+ 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);
+ killTimer(timer_);
+ timer_ = 0;
+}
+
+void CallListener::sleep(int ms)
+{
+ if(timer_)
+ {
+ killTimer(timer_);
+ }
+
+ timer_ = startTimer(ms);
+
+ while(timer_)
+ {
+ QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
+ }
+}