#include "calllistener.h"
#include "settings.h"
#include "cache.h"
+#include "contactmanager.h"
namespace
{
QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
-CallListener::CallListener(): eniro_(0), contactManager_(0), box_(0), label_(0)
+CallListener::CallListener(): eniro_(0), connectionManager_(0),
+closeConnection_(false), initialized_(false), box_(0), label_(0),
+retries_(-1), site_(Eniro::FI), autoconnect_(false)
{
}
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_);
+ site_ = Eniro::stringToSite(Settings::instance()->get("site"));
+ connectionName_ = Settings::instance()->get("connection");
+ autoconnect_ = (Settings::instance()->get("autoconnect") == "1");
+ Settings::close();
qDebug() << "Starting...";
this,
SLOT(callTerminate()));
- delete eniro_;
- eniro_ = 0;
- delete box_;
- box_ = 0;
- delete label_;
- label_ = 0;
+ searchClose();
+
}
void CallListener::search(Eniro::SearchDetails const& details)
{
- qDebug() << "Search called";
+ qDebug() << "Search called";
+
+ searchInit();
Eniro::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;
+
+ showDelayedResult(tr("Searching..."), BANNER_DELAY);
+
+ if(!handleConnection())
+ {
+ qDebug() << "Unable to connect";
+ return;
+ }
+
+ qDebug() << "Starting to search...";
+
eniro_->search(details);
}
Eniro::SearchDetails const& details,
bool error)
{
- qDebug() << "Found: " << results.size();
-
// 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();
- message = tr("Search failed:") + " " + eniro_->errorString() + ".";
- }
- else if(results.size() == 0)
- {
- message = tr("Phone number was not found");
+
+ if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
+ {
+ retries_++;
+ eniro_->search(Eniro::SearchDetails(currentSearch_));
+ return;
+ }
+ else
+ {
+ timedMessage_ = "";
+ showError(tr("Search failed:") + " " + eniro_->errorString() + ".");
+ }
}
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_ = "";
- timedMessage_ = message;
+ 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);
+ Eniro::Result result = results.at(0);
+ result.number = details.query;
+ Cache::instance().addItem(result);
+ }
+ }
- // Show banner after small delay
- showDelayedResult(message, BANNER_DELAY);
+ retries_ = -1;
+ currentSearch_ = "";
+ if(closeConnection_ && connectionManager_)
+ {
+ connectionManager_->disconnect(true);
+ closeConnection_ = false;
+ }
}
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())
{
box_->hide();
}
+
box_->show();
}
{
qDebug() << "Incoming: " << number;
- if(!contactManager_->numberExists(number))
+ ContactManager cm;
+
+ if(!cm.numberExists(number))
{
- qDebug() << "Number doesn't exist";
+ qDebug() << "Number doesn't exist";
systemBus_.connect(CALL_SERVICE_NAME,
path.path(),
this,
SLOT(callTerminate()));
- qDebug() << "Going to search";
-
search(Eniro::SearchDetails(number));
}
else
{
- qDebug() << "Number exists";
+ qDebug() << "Number exists";
}
}
void CallListener::callTerminate()
{
- if(box_->isVisible())
- {
- box_->hide();
- }
+ if(initialized_ && box_ && box_->isVisible())
+ {
+ box_->hide();
+ }
+
+ if(closeConnection_ && connectionManager_)
+ {
+ connectionManager_->disconnect(true);
+ closeConnection_ = false;
+ }
+
+ searchClose();
}
void CallListener::showDelayedResult(QString const& text, int delay)
{
- timedMessage_ = text;
- QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
+ timedMessage_ = text;
+ QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
}
void CallListener::showTimedMessage()
{
- if(timedMessage_.size() == 0)
- {
- return;
- }
+ if(timedMessage_.size() == 0 || !initialized_)
+ {
+ return;
+ }
+
+ showResult(timedMessage_);
+
+ timedMessage_ = "";
+}
+
+void CallListener::searchInit()
+{
+ qDebug() << "Initializing search...";
+
+ if(initialized_)
+ {
+ qDebug() << "Already initialized";
+ return;
+ }
+
+ connectionManager_ = new ConnectionManager;
+
+ 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);
+ box_->setWidget(label_);
+ initialized_ = true;
+}
+
+void CallListener::searchClose()
+{
+ initialized_ = false;
+
+ qDebug() << "Closing search...";
- showResult(timedMessage_);
+ 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)));
+ }
- timedMessage_ = "";
+ delete connectionManager_;
+ connectionManager_ = 0;
+ delete eniro_;
+ eniro_ = 0;
+ delete box_;
+ box_ = 0;
+ label_ = 0;
+}
+
+bool CallListener::handleConnection()
+{
+ if(!connectionManager_)
+ {
+ qDebug() << "Error: connection manager not initialized";
+ return false;
+ }
+
+ if(connectionManager_->isConnected())
+ {
+ closeConnection_ = false;
+ return true;
+ }
+
+ closeConnection_ = true;
+ int retries = 0;
+
+ if(autoconnect_)
+ {
+ QString conn;
+
+ if(connectionName_.size() != 0 && connectionName_ != "0")
+ {
+ conn = connectionName_;
+ }
+ else
+ {
+ ConnectionManager::Connection best;
+
+ if(!connectionManager_->getBestConnection(best))
+ {
+ showError(tr("No network connections found."));
+ return false;
+ }
+
+ conn = best.id;
+ }
+
+ while(retries < CONNECT_RETRIES)
+ {
+ if(connectionManager_->connect(conn))
+ {
+ break;
+ }
+ else if(connectionManager_->error() == ConnectionManager::INVALID_IAP)
+ {
+ showError(tr("Selected access point doesn't exist."));
+ return false;
+ }
+
+ retries++;
+ }
+ }
+ else
+ {
+ while(retries < CONNECT_RETRIES)
+ {
+ if(connectionManager_->connect())
+ {
+ break;
+ }
+ else
+ {
+ qDebug() << "Automatic connection failed";
+ }
+
+ retries++;
+ }
+ }
+
+ if(retries >= CONNECT_RETRIES)
+ {
+ ConnectionManager::NetworkMode mode = connectionManager_->getNetworkMode();
+
+ if(mode != ConnectionManager::NETWORK_3G &&
+ mode != ConnectionManager::NETWORK_3_5G)
+ {
+ showError(tr("No 3G or WLAN network available."));
+ }
+ else
+ {
+ showError(tr("Unable to connect to network."));
+ }
+
+ return false;
+ }
+
+ return true;
+}
+
+void CallListener::showError(QString const& msg)
+{
+ qDebug() << "Error: " << msg;
+ box_->setTimeout(ERROR_BANNER_TIMEOUT);
+ showResult(msg);
}