+
+void CallListener::searchInit()
+{
+ qDebug() << "Initializing search...";
+
+ if(initialized_)
+ {
+ qDebug() << "Already initialized";
+ return;
+ }
+
+ eniro_ = new Eniro(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);
+ label_->setWordWrap(true);
+ box_->setWidget(label_);
+ initialized_ = true;
+}
+
+void CallListener::searchClose()
+{
+ initialized_ = false;
+
+ qDebug() << "Closing search...";
+
+ if(eniro_)
+ {
+ disconnect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
+ Eniro::SearchDetails const&, bool)),
+ this, SLOT(requestFinished(QVector <Eniro::Result> const&,
+ Eniro::SearchDetails const&, bool)));
+ }
+
+ 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."));
+ 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)
+{
+ qDebug() << "Error: " << msg;
+ box_->setTimeout(ERROR_BANNER_TIMEOUT);
+ 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);
+ }
+}