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), ignoreFirstZero_(false)
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 ignoreFirstZero_ = (Settings::instance()->get("ignore_first_zero") == "1");
73 systemBus_.connect(CALL_SERVICE_NAME,
75 CALL_SERVICE_INTERFACE,
78 SLOT(incomingCall(QDBusObjectPath, QString)));
80 systemBus_.connect(CALL_SERVICE_NAME,
82 CALL_SERVICE_INTERFACE,
85 SLOT(callTerminate()));
87 qDebug() << "Starting...";
93 void CallListener::end()
95 systemBus_.disconnect(CALL_SERVICE_NAME,
97 CALL_SERVICE_INTERFACE,
100 SLOT(incomingCall(QDBusObjectPath, QString)));
102 systemBus_.disconnect(CALL_SERVICE_NAME,
104 CALL_SERVICE_INTERFACE,
107 SLOT(callTerminate()));
110 sourceConfig_.clear();
114 void CallListener::search(Source::SearchDetails const& details)
121 currentCall_ = new CallDetails;
122 currentCall_->number = details.query;
123 currentCall_->time = QDateTime::currentDateTime().toTime_t();
124 currentCall_->answered = false;
128 Source::Result result;
130 if(Cache::instance().findItem(details.query, result))
133 qDebug() << "Found from cache";
135 showDelayedResult(createResult(result.name,
137 result.city), BANNER_DELAY);
139 currentCall_->result = result;
145 if(!handleConnection())
147 qDebug() << "Unable to connect";
151 showDelayedResult(tr("Searching..."), BANNER_DELAY);
153 source_->search(details);
158 void CallListener::requestFinished(QVector <Source::Result> const& results,
159 Source::SearchDetails const& details,
162 /*if(closeConnection_)
164 closeConnection_ = false;
165 ConnectionManager cm;
169 qDebug() << "Request finished";
171 // If box is not visible, the call must have been terminated already
172 if(!initialized_ || !box_->isVisible() || !currentCall_)
181 qDebug() << "Error: " << source_->errorString();
183 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
186 source_->search(Source::SearchDetails(currentCall_->number, "", Source::BOTH));
193 Source::Error error = source_->error();
197 case Source::TIMEOUT:
198 errorString = tr("Request timed out");
201 errorString = source_->errorString();
205 showError(tr("Searching failed:") + " " + errorString + ".");
212 if(results.size() == 0)
214 message = tr("Phone number was not found");
219 message = createResult(results.at(0).name, results.at(0).street,
222 Source::Result result = results.at(0);
223 result.number = details.query;
224 Cache::instance().addItem(result);
225 currentCall_->result = results.at(0);
232 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
234 QString result = "<b>" + name + "</b>";
236 if(!street.isEmpty() || !city.isEmpty())
240 if(!street.isEmpty())
242 result += street + ", ";
251 void CallListener::showResult(QString const& text)
258 label_->setText("<font color='black'>" + text + "</font>");
260 if(box_->isVisible())
268 void CallListener::incomingCall(QDBusObjectPath path, QString number)
272 qDebug() << "Unknown caller without number";
276 // If the call has come through some kind of switch board
277 // there might be a leading zero added
280 number = number.replace(QRegExp("^00"), "0");
285 if(!cm.numberExists(number))
287 qDebug() << "Number doesn't exist: " << number;
289 systemBus_.connect(CALL_SERVICE_NAME,
291 CALL_SERVICE_INSTANCE_NAME,
292 CALL_SIGNAL_TERMINATED,
294 SLOT(callTerminate()));
296 systemBus_.connect(CALL_SERVICE_NAME,
298 CALL_SERVICE_INSTANCE_NAME,
299 CALL_SIGNAL_ANSWERED,
301 SLOT(handleAnswer()));
303 search(Source::SearchDetails(number, "", Source::BOTH));
307 qDebug() << "Number exists: " << number;
311 void CallListener::callTerminate()
313 if(box_ && box_->isVisible())
320 currentCall_->result.number = currentCall_->number;
321 Cache::instance().logItem(currentCall_->result, !currentCall_->answered, currentCall_->time);
329 void CallListener::handleAnswer()
331 qDebug() << "Answered";
335 currentCall_->answered = true;
339 void CallListener::showDelayedResult(QString const& text, int delay)
341 timedMessage_ = text;
342 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
345 void CallListener::showTimedMessage()
347 if(timedMessage_.size() == 0 || !initialized_)
352 showResult(timedMessage_);
357 void CallListener::searchInit()
359 qDebug() << "Initializing search...";
363 qDebug() << "Already initialized";
367 source_ = Source::getSource(sourceId_);
368 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
369 config->loadFromConfig(sourceConfig_);
370 config->apply(source_);
372 source_->setMaxResults(1);
373 source_->setFindNumber(false);
374 source_->setTimeout(REQUEST_TIMEOUT);
376 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
377 Source::SearchDetails const&, bool)),
378 this, SLOT(requestFinished(QVector <Source::Result> const&,
379 Source::SearchDetails const&, bool)));
380 box_ = new InformationBox;
381 label_ = new QLabel("", box_);
382 label_->setMargin(8);
383 label_->setWordWrap(true);
384 box_->setWidget(label_);
388 void CallListener::searchClose()
395 initialized_ = false;
397 qDebug() << "Closing search...";
401 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
402 Source::SearchDetails const&, bool)),
403 this, SLOT(requestFinished(QVector <Source::Result> const&,
404 Source::SearchDetails const&, bool)));
415 QTimer::singleShot(500, this, SLOT(closeConnection()));
419 void CallListener::closeConnection()
423 closeConnection_ = false;
424 ConnectionManager cm;
429 bool CallListener::handleConnection()
436 ConnectionManager cm;
441 closeConnection_ = false;
445 closeConnection_ = true;
447 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
449 if(configType == Settings::ALWAYS_ASK)
451 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
455 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
457 ConnectionManager::Connection best;
459 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
464 lookupType = ConnectionManager::WLAN;
467 lookupType = ConnectionManager::GPRS;
470 lookupType = ConnectionManager::NO_TYPE;
477 int maxScans = GPRS_SCANS;
479 if(lookupType != ConnectionManager::GPRS)
481 maxScans = WLAN_SCANS;
484 while(cretries < CONNECTION_LOOKUP_RETRIES)
493 if(cm.getBestConnection(best, lookupType))
501 // If there is only gprs connection available,
502 // make sure that we are on 3g network
503 if(found && (best.type != ConnectionManager::GPRS || is3g()))
510 sleep(WAIT_BETWEEN_RETRIES);
513 qDebug() << "No connections found, retrying...";
519 if(cretries >= CONNECTION_LOOKUP_RETRIES)
521 showError(tr("No available 3G or WLAN networks found."));
527 while(retries < CONNECT_RETRIES)
534 qDebug() << "Connecting to " << best.name << " (" << best.id << ")";
536 if(cm.connect(best.id))
540 else if(cm.error() == ConnectionManager::INVALID_IAP)
542 showError(tr("Selected access point doesn't exist."));
548 qDebug() << "Unable to connect, retrying...";
550 if(retries < CONNECT_RETRIES)
552 sendRetrySignal(best.id, false);
553 sleep(WAIT_BETWEEN_RETRIES);
558 if(retries >= CONNECT_RETRIES)
560 sendRetrySignal(best.id, false);
564 showError(tr("Unable to connect to network."));
573 void CallListener::showError(QString const& msg, int timeout)
575 qDebug() << "Error: " << msg;
577 if(!initialized_ || !box_)
582 box_->setTimeout(ERROR_BANNER_TIMEOUT);
586 showDelayedResult(msg, timeout);
594 bool CallListener::is3g()
596 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
597 "/com/nokia/phone/net",
599 "get_registration_status");
601 QDBusMessage rep = systemBus_.call(msg);
603 if(rep.type() == QDBusMessage::ErrorMessage)
605 qDebug() << "Unable to get network status";
609 uint status = rep.arguments().value(6).toUInt();
611 if(status & 0x10 || status & 0x08)
619 void CallListener::sendRetrySignal(QString const& iap, bool retry)
621 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
625 QList<QVariant> arguments;
626 arguments.append(QVariant(iap));
627 arguments.append(QVariant(retry));
628 msg.setArguments(arguments);
630 QDBusConnection::systemBus().send(msg);
632 qDebug() << "Retry signal sent";
635 void CallListener::timerEvent(QTimerEvent* event)
642 void CallListener::sleep(int ms)
649 timer_ = startTimer(ms);
653 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);