2 * This file is part of Jenirok.
4 * Jenirok is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Jenirok is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Jenirok. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtCore/QDebug>
20 #include <QtCore/QTimer>
21 #include <QtCore/QDateTime>
22 #include "calllistener.h"
25 #include "contactmanager.h"
26 #include "connectionmanager.h"
27 #include "sourcecoreconfig.h"
32 const QString CALL_SERVICE_NAME = "com.nokia.csd";
33 const QString CALL_SERVICE_PATH = "/com/nokia/csd/call";
34 const QString CALL_SERVICE_INTERFACE = "com.nokia.csd.Call";
35 const QString CALL_SERVICE_INSTANCE_NAME = "com.nokia.csd.Call.Instance";
36 const QString CALL_SIGNAL_INCOMING = "Coming";
37 const QString CALL_SIGNAL_RELEASE = "Release";
38 const QString CALL_SIGNAL_TERMINATED = "Terminated";
39 const QString CALL_SIGNAL_ANSWERED = "AudioConnect";
42 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
44 CallListener::CallListener(): source_(0),
45 closeConnection_(false), initialized_(false), box_(0), label_(0),
46 retries_(-1), timer_(0), currentCall_(0)
50 CallListener::~CallListener()
56 bool CallListener::begin()
58 if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
60 qDebug() << "Bad connection settings, unable to start";
64 sourceId_ = Source::stringToId(Settings::instance()->get("source"));
65 QMap<QString, QString> tmpConfig;
66 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
67 config->getConfig(tmpConfig);
68 sourceConfig_ = tmpConfig;
71 systemBus_.connect(CALL_SERVICE_NAME,
73 CALL_SERVICE_INTERFACE,
76 SLOT(incomingCall(QDBusObjectPath, QString)));
78 systemBus_.connect(CALL_SERVICE_NAME,
80 CALL_SERVICE_INTERFACE,
83 SLOT(callTerminate()));
85 qDebug() << "Starting...";
91 void CallListener::end()
93 systemBus_.disconnect(CALL_SERVICE_NAME,
95 CALL_SERVICE_INTERFACE,
98 SLOT(incomingCall(QDBusObjectPath, QString)));
100 systemBus_.disconnect(CALL_SERVICE_NAME,
102 CALL_SERVICE_INTERFACE,
105 SLOT(callTerminate()));
108 sourceConfig_.clear();
112 void CallListener::search(Source::SearchDetails const& details)
119 currentCall_ = new CallDetails;
120 currentCall_->number = details.query;
121 currentCall_->time = QDateTime::currentDateTime().toTime_t();
122 currentCall_->answered = false;
126 Source::Result result;
128 if(Cache::instance().findItem(details.query, result))
131 qDebug() << "Found from cache";
133 showDelayedResult(createResult(result.name,
135 result.city), BANNER_DELAY);
137 currentCall_->result = result;
143 if(!handleConnection())
145 qDebug() << "Unable to connect";
149 showDelayedResult(tr("Searching..."), BANNER_DELAY);
151 source_->search(details);
156 void CallListener::requestFinished(QVector <Source::Result> const& results,
157 Source::SearchDetails const& details,
160 /*if(closeConnection_)
162 closeConnection_ = false;
163 ConnectionManager cm;
167 // If box is not visible, the call must have been terminated already
168 if(!initialized_ || !box_->isVisible() || !currentCall_)
177 qDebug() << "Error: " << source_->errorString();
179 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
182 source_->search(Source::SearchDetails(currentCall_->number, "", Source::BOTH));
189 Source::Error error = source_->error();
193 case Source::TIMEOUT:
194 errorString = tr("Request timed out");
197 errorString = source_->errorString();
201 showError(tr("Searching failed:") + " " + errorString + ".");
208 if(results.size() == 0)
210 message = tr("Phone number was not found");
215 message = createResult(results.at(0).name, results.at(0).street,
218 Source::Result result = results.at(0);
219 result.number = details.query;
220 Cache::instance().addItem(result);
221 currentCall_->result = results.at(0);
228 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
230 QString result = "<b>" + name + "</b>";
232 if(!street.isEmpty() || !city.isEmpty())
236 if(!street.isEmpty())
238 result += street + ", ";
247 void CallListener::showResult(QString const& text)
254 label_->setText("<font color='black'>" + text + "</font>");
256 if(box_->isVisible())
264 void CallListener::incomingCall(QDBusObjectPath path, QString number)
268 qDebug() << "Unknown caller without number";
274 if(!cm.numberExists(number))
276 qDebug() << "Number doesn't exist: " << number;
278 systemBus_.connect(CALL_SERVICE_NAME,
280 CALL_SERVICE_INSTANCE_NAME,
281 CALL_SIGNAL_TERMINATED,
283 SLOT(callTerminate()));
285 systemBus_.connect(CALL_SERVICE_NAME,
287 CALL_SERVICE_INSTANCE_NAME,
288 CALL_SIGNAL_ANSWERED,
290 SLOT(handleAnswer()));
292 search(Source::SearchDetails(number, "", Source::BOTH));
296 qDebug() << "Number exists: " << number;
300 void CallListener::callTerminate()
302 if(box_ && box_->isVisible())
309 currentCall_->result.number = currentCall_->number;
310 Cache::instance().logItem(currentCall_->result, !currentCall_->answered, currentCall_->time);
318 void CallListener::handleAnswer()
320 qDebug() << "Answered";
324 currentCall_->answered = true;
328 void CallListener::showDelayedResult(QString const& text, int delay)
330 timedMessage_ = text;
331 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
334 void CallListener::showTimedMessage()
336 if(timedMessage_.size() == 0 || !initialized_)
341 showResult(timedMessage_);
346 void CallListener::searchInit()
348 qDebug() << "Initializing search...";
352 qDebug() << "Already initialized";
356 source_ = Source::getSource(sourceId_);
357 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
358 config->loadFromConfig(sourceConfig_);
359 config->apply(source_);
361 source_->setMaxResults(1);
362 source_->setFindNumber(false);
363 source_->setTimeout(REQUEST_TIMEOUT);
365 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
366 Source::SearchDetails const&, bool)),
367 this, SLOT(requestFinished(QVector <Source::Result> const&,
368 Source::SearchDetails const&, bool)));
369 box_ = new InformationBox;
370 label_ = new QLabel("", box_);
371 label_->setMargin(8);
372 label_->setWordWrap(true);
373 box_->setWidget(label_);
377 void CallListener::searchClose()
384 initialized_ = false;
386 qDebug() << "Closing search...";
390 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
391 Source::SearchDetails const&, bool)),
392 this, SLOT(requestFinished(QVector <Source::Result> const&,
393 Source::SearchDetails const&, bool)));
404 QTimer::singleShot(500, this, SLOT(closeConnection()));
408 void CallListener::closeConnection()
412 closeConnection_ = false;
413 ConnectionManager cm;
418 bool CallListener::handleConnection()
425 ConnectionManager cm;
429 closeConnection_ = false;
433 closeConnection_ = true;
435 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
437 if(configType == Settings::ALWAYS_ASK)
439 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
443 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
445 ConnectionManager::Connection best;
447 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
452 lookupType = ConnectionManager::WLAN;
455 lookupType = ConnectionManager::GPRS;
458 lookupType = ConnectionManager::NO_TYPE;
465 int maxScans = GPRS_SCANS;
467 if(lookupType != ConnectionManager::GPRS)
469 maxScans = WLAN_SCANS;
472 while(cretries < CONNECTION_LOOKUP_RETRIES)
481 if(cm.getBestConnection(best, lookupType))
489 // If there is only gprs connection available,
490 // make sure that we are on 3g network
491 if(found && (best.type != ConnectionManager::GPRS || is3g()))
498 sleep(WAIT_BETWEEN_RETRIES);
501 qDebug() << "No connections found, retrying...";
507 if(cretries >= CONNECTION_LOOKUP_RETRIES)
509 showError(tr("No available 3G or WLAN networks found."));
515 while(retries < CONNECT_RETRIES)
522 qDebug() << "Connecting to " << best.name << " (" << best.id << ")";
524 if(cm.connect(best.id))
529 else if(cm.error() == ConnectionManager::INVALID_IAP)
531 showError(tr("Selected access point doesn't exist."));
537 qDebug() << "Unable to connect, retrying...";
539 if(retries < CONNECT_RETRIES)
541 sendRetrySignal(best.id, false);
542 sleep(WAIT_BETWEEN_RETRIES);
547 if(retries >= CONNECT_RETRIES)
549 sendRetrySignal(best.id, false);
553 showError(tr("Unable to connect to network."));
562 void CallListener::showError(QString const& msg, int timeout)
564 qDebug() << "Error: " << msg;
566 if(!initialized_ || !box_)
571 box_->setTimeout(ERROR_BANNER_TIMEOUT);
575 showDelayedResult(msg, timeout);
583 bool CallListener::is3g()
585 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
586 "/com/nokia/phone/net",
588 "get_registration_status");
590 QDBusMessage rep = systemBus_.call(msg);
592 if(rep.type() == QDBusMessage::ErrorMessage)
594 qDebug() << "Unable to get network status";
598 uint status = rep.arguments().value(6).toUInt();
600 if(status & 0x10 || status & 0x08)
608 void CallListener::sendRetrySignal(QString const& iap, bool retry)
610 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
614 QList<QVariant> arguments;
615 arguments.append(QVariant(iap));
616 arguments.append(QVariant(retry));
617 msg.setArguments(arguments);
619 QDBusConnection::systemBus().send(msg);
621 qDebug() << "Retry signal sent";
624 void CallListener::timerEvent(QTimerEvent* event)
631 void CallListener::sleep(int ms)
638 timer_ = startTimer(ms);
642 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);