+ if(box_ && box_->isVisible())
+ {
+ box_->hide();
+ }
+
+ if(currentCall_)
+ {
+ 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;
+ QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
+}
+
+void CallListener::showTimedMessage()
+{
+ if(timedMessage_.size() == 0 || !initialized_)
+ {
+ return;
+ }
+
+ showResult(timedMessage_);
+
+ 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_)
+ {
+ QTimer::singleShot(500, this, SLOT(closeConnection()));
+ }
+}
+
+void CallListener::closeConnection()
+{
+ if(closeConnection_)
+ {
+ closeConnection_ = false;
+ ConnectionManager cm;
+ cm.disconnect(true);
+ }
+}
+
+bool CallListener::handleConnection()
+{
+ if(!initialized_)
+ {
+ return false;
+ }
+
+ ConnectionManager cm;
+
+ if(cm.isConnected())
+ {
+ cm.connect();
+ 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 << " (" << best.id << ")";
+
+ 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, false);
+ sleep(WAIT_BETWEEN_RETRIES);
+ }
+
+ }
+
+ 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);
+ }