X-Git-Url: http://git.maemo.org/git/?p=jenirok;a=blobdiff_plain;f=src%2Fdaemon%2Fcalllistener.cpp;h=bdc6529ed930f2f54653e518ab34ff81ccc323b1;hp=4a2f1c505af930104d9ebc7acde89fac855c5e25;hb=7c78a6f936682395aa101b2429c118934bb0f1d8;hpb=b7a9e838eaf1539637b2bf46a1907d7aa604bcd9 diff --git a/src/daemon/calllistener.cpp b/src/daemon/calllistener.cpp index 4a2f1c5..bdc6529 100644 --- a/src/daemon/calllistener.cpp +++ b/src/daemon/calllistener.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "calllistener.h" #include "settings.h" #include "cache.h" @@ -35,19 +36,21 @@ 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(): source_(0), closeConnection_(false), initialized_(false), box_(0), label_(0), -retries_(-1), timer_(0) +retries_(-1), timer_(0), currentCall_(0), ignoreFirstZero_(false) { } CallListener::~CallListener() { end(); + DB::removeDatabase(); } bool CallListener::begin() @@ -59,10 +62,14 @@ bool CallListener::begin() } sourceId_ = Source::stringToId(Settings::instance()->get("source")); + QMap tmpConfig; SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_); - config->getConfig(sourceConfig_); + 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, @@ -77,8 +84,6 @@ bool CallListener::begin() this, SLOT(callTerminate())); - findGprsId(); - qDebug() << "Starting..."; return true; @@ -108,7 +113,15 @@ void CallListener::end() 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(); @@ -122,11 +135,12 @@ void CallListener::search(Source::SearchDetails const& details) showDelayedResult(createResult(result.name, result.street, result.city), BANNER_DELAY); + + currentCall_->result = result; } else { retries_ = 0; - currentSearch_ = details.query; if(!handleConnection()) { @@ -136,8 +150,6 @@ void CallListener::search(Source::SearchDetails const& details) showDelayedResult(tr("Searching..."), BANNER_DELAY); - qDebug() << "Starting to search..."; - source_->search(details); } @@ -147,8 +159,17 @@ 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; } @@ -162,7 +183,7 @@ void CallListener::requestFinished(QVector const& results, if(retries_ < SEARCH_RETRIES && retries_ >= 0) { retries_++; - source_->search(Source::SearchDetails(currentSearch_)); + source_->search(Source::SearchDetails(currentCall_->number, "", Source::BOTH)); return; } else @@ -201,18 +222,11 @@ void CallListener::requestFinished(QVector const& results, Source::Result result = results.at(0); result.number = details.query; Cache::instance().addItem(result); + currentCall_->result = results.at(0); } } retries_ = -1; - currentSearch_ = ""; - - if(closeConnection_) - { - closeConnection_ = false; - ConnectionManager cm; - cm.disconnect(true); - } } QString CallListener::createResult(QString const& name, QString const& street, QString const& city) @@ -253,11 +267,24 @@ void CallListener::showResult(QString const& text) void CallListener::incomingCall(QDBusObjectPath path, QString 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(), @@ -266,11 +293,18 @@ void CallListener::incomingCall(QDBusObjectPath path, QString number) this, SLOT(callTerminate())); - search(Source::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; } } @@ -281,18 +315,24 @@ void CallListener::callTerminate() box_->hide(); } - if(closeConnection_) + if(currentCall_) { - closeConnection_ = false; - ConnectionManager cm; - cm.disconnect(true); + 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(gprsId_.isEmpty()) + if(currentCall_) { - findGprsId(); + currentCall_->answered = true; } } @@ -347,6 +387,11 @@ void CallListener::searchInit() void CallListener::searchClose() { + if(!initialized_) + { + return; + } + initialized_ = false; qDebug() << "Closing search..."; @@ -364,6 +409,21 @@ void CallListener::searchClose() 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() @@ -377,6 +437,7 @@ bool CallListener::handleConnection() if(cm.isConnected()) { + cm.connect(); closeConnection_ = false; return true; } @@ -411,9 +472,14 @@ bool CallListener::handleConnection() } int cretries = 0; + int scans = 0; + bool found = false; + int maxScans = GPRS_SCANS; - bool tryGprs = (!gprsId_.isEmpty() && - (configType == Settings::ANY || configType == Settings::GPRS)); + if(lookupType != ConnectionManager::GPRS) + { + maxScans = WLAN_SCANS; + } while(cretries < CONNECTION_LOOKUP_RETRIES) { @@ -422,26 +488,32 @@ bool CallListener::handleConnection() return false; } - if(cm.getBestConnection(best, lookupType)) + if(scans < maxScans) { - break; + if(cm.getBestConnection(best, lookupType)) + { + found = true; + } + + scans++; } - // ICD doesn't always find gprs connection during call, so - // try to use gprs anyway. - if(tryGprs && is3g()) + // If there is only gprs connection available, + // make sure that we are on 3g network + if(found && (best.type != ConnectionManager::GPRS || is3g())) { - best.id = gprsId_; - qDebug() << "Trying gprs"; break; } + if(found) + { + sleep(WAIT_BETWEEN_RETRIES); + } + qDebug() << "No connections found, retrying..."; cretries++; - sleep(WAIT_BETWEEN_RETRIES); - } if(cretries >= CONNECTION_LOOKUP_RETRIES) @@ -459,6 +531,8 @@ bool CallListener::handleConnection() return false; } + qDebug() << "Connecting to " << best.name << " (" << best.id << ")"; + if(cm.connect(best.id)) { break; @@ -469,16 +543,27 @@ bool CallListener::handleConnection() return false; } - sleep(WAIT_BETWEEN_RETRIES); + retries++; qDebug() << "Unable to connect, retrying..."; - retries++; + + 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; } @@ -488,6 +573,12 @@ bool CallListener::handleConnection() void CallListener::showError(QString const& msg, int timeout) { qDebug() << "Error: " << msg; + + if(!initialized_ || !box_) + { + return; + } + box_->setTimeout(ERROR_BANNER_TIMEOUT); if(timeout) @@ -500,41 +591,6 @@ void CallListener::showError(QString const& msg, int timeout) } } -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); - } -} - -void CallListener::findGprsId() -{ - ConnectionManager cm; - - QList connections; - - if(cm.scanConnections(connections, ConnectionManager::GPRS) && - connections.size() > 0) - { - gprsId_ = connections.at(0).id; - } -} - bool CallListener::is3g() { QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net", @@ -552,15 +608,48 @@ bool CallListener::is3g() uint status = rep.arguments().value(6).toUInt(); - if(status & 0x10) // 3.5G (HSUPA) + if(status & 0x10 || status & 0x08) { return true; } - else if(status & 0x08) // 3G (HSDPA) + + 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) +{ + Q_UNUSED(event); + killTimer(timer_); + timer_ = 0; +} + +void CallListener::sleep(int ms) +{ + if(timer_) { - return true; + killTimer(timer_); } - // Something else - return false; + timer_ = startTimer(ms); + + while(timer_) + { + QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents); + } }