X-Git-Url: http://git.maemo.org/git/?p=jenirok;a=blobdiff_plain;f=src%2Fdaemon%2Fcalllistener.cpp;h=bdc6529ed930f2f54653e518ab34ff81ccc323b1;hp=6c25bfc758bd7ef361b31eb1fc3a8b60a1460237;hb=7c78a6f936682395aa101b2429c118934bb0f1d8;hpb=9127dc5e47aee7fa6659f45ae60a9394612f27b1 diff --git a/src/daemon/calllistener.cpp b/src/daemon/calllistener.cpp index 6c25bfc..bdc6529 100644 --- a/src/daemon/calllistener.cpp +++ b/src/daemon/calllistener.cpp @@ -18,11 +18,13 @@ #include #include +#include #include "calllistener.h" #include "settings.h" #include "cache.h" #include "contactmanager.h" #include "connectionmanager.h" +#include "sourcecoreconfig.h" #include "db.h" namespace @@ -34,13 +36,14 @@ namespace const QString CALL_SIGNAL_INCOMING = "Coming"; const QString CALL_SIGNAL_RELEASE = "Release"; const QString CALL_SIGNAL_TERMINATED = "Terminated"; + const QString CALL_SIGNAL_ANSWERED = "AudioConnect"; } QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus(); -CallListener::CallListener(): eniro_(0), +CallListener::CallListener(): source_(0), closeConnection_(false), initialized_(false), box_(0), label_(0), -retries_(-1), site_(Eniro::FI), timer_(0) +retries_(-1), timer_(0), currentCall_(0), ignoreFirstZero_(false) { } @@ -58,6 +61,15 @@ bool CallListener::begin() return false; } + sourceId_ = Source::stringToId(Settings::instance()->get("source")); + QMap tmpConfig; + SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_); + config->getConfig(tmpConfig); + sourceConfig_ = tmpConfig; + delete config; + + ignoreFirstZero_ = (Settings::instance()->get("ignore_first_zero") == "1"); + systemBus_.connect(CALL_SERVICE_NAME, CALL_SERVICE_PATH, CALL_SERVICE_INTERFACE, @@ -72,8 +84,6 @@ bool CallListener::begin() this, SLOT(callTerminate())); - site_ = Eniro::stringToSite(Settings::instance()->get("site")); - qDebug() << "Starting..."; return true; @@ -97,16 +107,25 @@ void CallListener::end() SLOT(callTerminate())); searchClose(); + sourceConfig_.clear(); } -void CallListener::search(Eniro::SearchDetails const& details) +void CallListener::search(Source::SearchDetails const& details) { - qDebug() << "Search called"; + if(currentCall_) + { + delete currentCall_; + } + + currentCall_ = new CallDetails; + currentCall_->number = details.query; + currentCall_->time = QDateTime::currentDateTime().toTime_t(); + currentCall_->answered = false; searchInit(); - Eniro::Result result; + Source::Result result; if(Cache::instance().findItem(details.query, result)) { @@ -116,11 +135,12 @@ void CallListener::search(Eniro::SearchDetails const& details) showDelayedResult(createResult(result.name, result.street, result.city), BANNER_DELAY); + + currentCall_->result = result; } else { retries_ = 0; - currentSearch_ = details.query; if(!handleConnection()) { @@ -130,19 +150,26 @@ void CallListener::search(Eniro::SearchDetails const& details) showDelayedResult(tr("Searching..."), BANNER_DELAY); - qDebug() << "Starting to search..."; - - eniro_->search(details); + source_->search(details); } } -void CallListener::requestFinished(QVector const& results, - Eniro::SearchDetails const& details, +void CallListener::requestFinished(QVector const& results, + Source::SearchDetails const& details, bool error) { + /*if(closeConnection_) + { + closeConnection_ = false; + ConnectionManager cm; + cm.disconnect(true); + }*/ + + qDebug() << "Request finished"; + // If box is not visible, the call must have been terminated already - if(!initialized_ || !box_->isVisible()) + if(!initialized_ || !box_->isVisible() || !currentCall_) { return; } @@ -151,27 +178,27 @@ void CallListener::requestFinished(QVector const& results, if(error) { - qDebug() << "Error: " << eniro_->errorString(); + qDebug() << "Error: " << source_->errorString(); if(retries_ < SEARCH_RETRIES && retries_ >= 0) { retries_++; - eniro_->search(Eniro::SearchDetails(currentSearch_)); + source_->search(Source::SearchDetails(currentCall_->number, "", Source::BOTH)); return; } else { timedMessage_ = ""; QString errorString; - Eniro::Error error = eniro_->error(); + Source::Error error = source_->error(); switch(error) { - case Eniro::TIMEOUT: + case Source::TIMEOUT: errorString = tr("Request timed out"); break; default: - errorString = eniro_->errorString(); + errorString = source_->errorString(); break; } @@ -192,21 +219,14 @@ void CallListener::requestFinished(QVector const& results, message = createResult(results.at(0).name, results.at(0).street, results.at(0).city); showResult(message); - Eniro::Result result = results.at(0); + Source::Result result = results.at(0); result.number = details.query; Cache::instance().addItem(result); + currentCall_->result = results.at(0); } } retries_ = -1; - currentSearch_ = ""; - - if(closeConnection_) - { - ConnectionManager cm; - cm.disconnect(true); - closeConnection_ = false; - } } QString CallListener::createResult(QString const& name, QString const& street, QString const& city) @@ -247,13 +267,24 @@ void CallListener::showResult(QString const& text) void CallListener::incomingCall(QDBusObjectPath path, QString number) { - qDebug() << "Incoming: " << number; + if(number.isEmpty()) + { + qDebug() << "Unknown caller without number"; + return; + } + + // If the call has come through some kind of switch board + // there might be a leading zero added + if(ignoreFirstZero_) + { + number = number.replace(QRegExp("^00"), "0"); + } ContactManager cm; if(!cm.numberExists(number)) { - qDebug() << "Number doesn't exist"; + qDebug() << "Number doesn't exist: " << number; systemBus_.connect(CALL_SERVICE_NAME, path.path(), @@ -262,11 +293,18 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number) this, SLOT(callTerminate())); - search(Eniro::SearchDetails(number)); + systemBus_.connect(CALL_SERVICE_NAME, + path.path(), + CALL_SERVICE_INSTANCE_NAME, + CALL_SIGNAL_ANSWERED, + this, + SLOT(handleAnswer())); + + search(Source::SearchDetails(number, "", Source::BOTH)); } else { - qDebug() << "Number exists"; + qDebug() << "Number exists: " << number; } } @@ -277,16 +315,27 @@ void CallListener::callTerminate() box_->hide(); } - if(closeConnection_) + if(currentCall_) { - ConnectionManager cm; - cm.disconnect(true); - closeConnection_ = false; + currentCall_->result.number = currentCall_->number; + Cache::instance().logItem(currentCall_->result, !currentCall_->answered, currentCall_->time); + delete currentCall_; + currentCall_ = 0; } searchClose(); } +void CallListener::handleAnswer() +{ + qDebug() << "Answered"; + + if(currentCall_) + { + currentCall_->answered = true; + } +} + void CallListener::showDelayedResult(QString const& text, int delay) { timedMessage_ = text; @@ -315,15 +364,19 @@ void CallListener::searchInit() return; } - eniro_ = new Eniro(site_); - eniro_->setMaxResults(1); - eniro_->setFindNumber(false); - eniro_->setTimeout(REQUEST_TIMEOUT); - - connect(eniro_, SIGNAL(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool)), - this, SLOT(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool))); + 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 const&, + Source::SearchDetails const&, bool)), + this, SLOT(requestFinished(QVector const&, + Source::SearchDetails const&, bool))); box_ = new InformationBox; label_ = new QLabel("", box_); label_->setMargin(8); @@ -334,23 +387,43 @@ void CallListener::searchInit() void CallListener::searchClose() { + if(!initialized_) + { + return; + } + initialized_ = false; qDebug() << "Closing search..."; - if(eniro_) + if(source_) { - disconnect(eniro_, SIGNAL(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool)), - this, SLOT(requestFinished(QVector const&, - Eniro::SearchDetails const&, bool))); + disconnect(source_, SIGNAL(requestFinished(QVector const&, + Source::SearchDetails const&, bool)), + this, SLOT(requestFinished(QVector const&, + Source::SearchDetails const&, bool))); } - delete eniro_; - eniro_ = 0; + delete source_; + source_ = 0; delete box_; box_ = 0; label_ = 0; + + if(closeConnection_) + { + QTimer::singleShot(500, this, SLOT(closeConnection())); + } +} + +void CallListener::closeConnection() +{ + if(closeConnection_) + { + closeConnection_ = false; + ConnectionManager cm; + cm.disconnect(true); + } } bool CallListener::handleConnection() @@ -364,6 +437,7 @@ bool CallListener::handleConnection() if(cm.isConnected()) { + cm.connect(); closeConnection_ = false; return true; } @@ -374,7 +448,7 @@ bool CallListener::handleConnection() if(configType == Settings::ALWAYS_ASK) { - showError(tr("Automatic connecting is not allowed by settings.")); + showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY); return false; } @@ -398,6 +472,14 @@ bool CallListener::handleConnection() } 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) { @@ -406,17 +488,32 @@ bool CallListener::handleConnection() return false; } - if(cm.getBestConnection(best, lookupType)) + 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++; - sleep(WAIT_BETWEEN_RETRIES); - } if(cretries >= CONNECTION_LOOKUP_RETRIES) @@ -434,7 +531,7 @@ bool CallListener::handleConnection() return false; } - sleep(WAIT_BETWEEN_RETRIES); + qDebug() << "Connecting to " << best.name << " (" << best.id << ")"; if(cm.connect(best.id)) { @@ -446,25 +543,93 @@ bool CallListener::handleConnection() return false; } - qDebug() << "Unable to connect, retrying..."; retries++; + qDebug() << "Unable to connect, retrying..."; + + if(retries < CONNECT_RETRIES) + { + sendRetrySignal(best.id, false); + sleep(WAIT_BETWEEN_RETRIES); + } + } - if(initialized_ && retries >= CONNECT_RETRIES) + if(retries >= CONNECT_RETRIES) { - showError(tr("Unable to connect to network.")); + sendRetrySignal(best.id, false); + + if(initialized_) + { + showError(tr("Unable to connect to network.")); + } + return false; } return initialized_; } -void CallListener::showError(QString const& msg) +void CallListener::showError(QString const& msg, int timeout) { qDebug() << "Error: " << msg; + + if(!initialized_ || !box_) + { + return; + } + box_->setTimeout(ERROR_BANNER_TIMEOUT); - showResult(msg); + + 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 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)