#include <QtCore/QDebug>
#include <QtCore/QTimer>
+#include <QtCore/QDateTime>
#include "calllistener.h"
#include "settings.h"
#include "cache.h"
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)
{
}
CallListener::~CallListener()
{
end();
+ DB::removeDatabase();
}
bool CallListener::begin()
}
sourceId_ = Source::stringToId(Settings::instance()->get("source"));
+ QMap<QString, QString> tmpConfig;
SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
- config->getConfig(sourceConfig_);
+ config->getConfig(tmpConfig);
+ sourceConfig_ = tmpConfig;
delete config;
systemBus_.connect(CALL_SERVICE_NAME,
this,
SLOT(callTerminate()));
- findGprsId();
-
qDebug() << "Starting...";
return true;
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();
showDelayedResult(createResult(result.name,
result.street,
result.city), BANNER_DELAY);
+
+ currentCall_->result = result;
}
else
{
retries_ = 0;
- currentSearch_ = details.query;
if(!handleConnection())
{
showDelayedResult(tr("Searching..."), BANNER_DELAY);
- qDebug() << "Starting to search...";
-
source_->search(details);
}
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;
}
if(retries_ < SEARCH_RETRIES && retries_ >= 0)
{
retries_++;
- source_->search(Source::SearchDetails(currentSearch_));
+ source_->search(Source::SearchDetails(currentCall_->number, "", Source::BOTH));
return;
}
else
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)
void CallListener::incomingCall(QDBusObjectPath path, QString number)
{
+ if(number.isEmpty())
+ {
+ qDebug() << "Unknown caller without number";
+ return;
+ }
+
ContactManager cm;
if(!cm.numberExists(number))
{
- qDebug() << "Number doesn't exist";
+ qDebug() << "Number doesn't exist: " << number;
systemBus_.connect(CALL_SERVICE_NAME,
path.path(),
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;
}
}
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;
}
}
void CallListener::searchClose()
{
+ if(!initialized_)
+ {
+ return;
+ }
+
initialized_ = false;
qDebug() << "Closing search...";
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(cm.isConnected())
{
+ cm.connect();
closeConnection_ = false;
return true;
}
}
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)
{
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)
return false;
}
+ qDebug() << "Connecting to " << best.name << " (" << best.id << ")";
+
if(cm.connect(best.id))
{
+ sleep(500);
break;
}
else if(cm.error() == ConnectionManager::INVALID_IAP)
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;
}
void CallListener::showError(QString const& msg, int timeout)
{
qDebug() << "Error: " << msg;
+
+ if(!initialized_ || !box_)
+ {
+ return;
+ }
+
box_->setTimeout(ERROR_BANNER_TIMEOUT);
if(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<ConnectionManager::Connection> 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",
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<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_)
{
- return true;
+ killTimer(timer_);
}
- // Something else
- return false;
+ timer_ = startTimer(ms);
+
+ while(timer_)
+ {
+ QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
+ }
}