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)
114 qDebug() << "Search called";
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 qDebug() << "Starting to search...";
155 source_->search(details);
160 void CallListener::requestFinished(QVector <Source::Result> const& results,
161 Source::SearchDetails const& details,
166 closeConnection_ = false;
167 ConnectionManager cm;
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";
278 if(!cm.numberExists(number))
280 qDebug() << "Number doesn't exist: " << number;
282 systemBus_.connect(CALL_SERVICE_NAME,
284 CALL_SERVICE_INSTANCE_NAME,
285 CALL_SIGNAL_TERMINATED,
287 SLOT(callTerminate()));
289 systemBus_.connect(CALL_SERVICE_NAME,
291 CALL_SERVICE_INSTANCE_NAME,
292 CALL_SIGNAL_ANSWERED,
294 SLOT(handleAnswer()));
296 search(Source::SearchDetails(number, "", Source::BOTH));
300 qDebug() << "Number exists: " << number;
304 void CallListener::callTerminate()
306 if(box_ && box_->isVisible())
313 currentCall_->result.number = currentCall_->number;
314 Cache::instance().logItem(currentCall_->result, !currentCall_->answered, currentCall_->time);
322 void CallListener::handleAnswer()
324 qDebug() << "Answered";
328 currentCall_->answered = true;
332 void CallListener::showDelayedResult(QString const& text, int delay)
334 timedMessage_ = text;
335 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
338 void CallListener::showTimedMessage()
340 if(timedMessage_.size() == 0 || !initialized_)
345 showResult(timedMessage_);
350 void CallListener::searchInit()
352 qDebug() << "Initializing search...";
356 qDebug() << "Already initialized";
360 source_ = Source::getSource(sourceId_);
361 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
362 config->loadFromConfig(sourceConfig_);
363 config->apply(source_);
365 source_->setMaxResults(1);
366 source_->setFindNumber(false);
367 source_->setTimeout(REQUEST_TIMEOUT);
369 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
370 Source::SearchDetails const&, bool)),
371 this, SLOT(requestFinished(QVector <Source::Result> const&,
372 Source::SearchDetails const&, bool)));
373 box_ = new InformationBox;
374 label_ = new QLabel("", box_);
375 label_->setMargin(8);
376 label_->setWordWrap(true);
377 box_->setWidget(label_);
381 void CallListener::searchClose()
388 initialized_ = false;
390 qDebug() << "Closing search...";
394 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
395 Source::SearchDetails const&, bool)),
396 this, SLOT(requestFinished(QVector <Source::Result> const&,
397 Source::SearchDetails const&, bool)));
408 closeConnection_ = false;
409 ConnectionManager cm;
414 bool CallListener::handleConnection()
421 ConnectionManager cm;
425 closeConnection_ = false;
429 closeConnection_ = true;
431 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
433 if(configType == Settings::ALWAYS_ASK)
435 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
439 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
441 ConnectionManager::Connection best;
443 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
448 lookupType = ConnectionManager::WLAN;
451 lookupType = ConnectionManager::GPRS;
454 lookupType = ConnectionManager::NO_TYPE;
461 int maxScans = GPRS_SCANS;
463 if(lookupType != ConnectionManager::GPRS)
465 maxScans = WLAN_SCANS;
468 while(cretries < CONNECTION_LOOKUP_RETRIES)
477 if(cm.getBestConnection(best, lookupType))
485 // If there is only gprs connection available,
486 // make sure that we are on 3g network
487 if(found && (best.type != ConnectionManager::GPRS || is3g()))
494 sleep(WAIT_BETWEEN_RETRIES);
497 qDebug() << "No connections found, retrying...";
503 if(cretries >= CONNECTION_LOOKUP_RETRIES)
505 showError(tr("No available 3G or WLAN networks found."));
511 while(retries < CONNECT_RETRIES)
518 qDebug() << "Connecting to " << best.name;
520 if(cm.connect(best.id))
524 else if(cm.error() == ConnectionManager::INVALID_IAP)
526 showError(tr("Selected access point doesn't exist."));
532 qDebug() << "Unable to connect, retrying...";
534 if(retries < CONNECT_RETRIES)
536 sendRetrySignal(best.id, initialized_);
541 if(retries >= CONNECT_RETRIES)
543 sendRetrySignal(best.id, false);
547 showError(tr("Unable to connect to network."));
556 void CallListener::showError(QString const& msg, int timeout)
558 qDebug() << "Error: " << msg;
560 if(!initialized_ || !box_)
565 box_->setTimeout(ERROR_BANNER_TIMEOUT);
569 showDelayedResult(msg, timeout);
577 bool CallListener::is3g()
579 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
580 "/com/nokia/phone/net",
582 "get_registration_status");
584 QDBusMessage rep = systemBus_.call(msg);
586 if(rep.type() == QDBusMessage::ErrorMessage)
588 qDebug() << "Unable to get network status";
592 uint status = rep.arguments().value(6).toUInt();
594 if(status & 0x10 || status & 0x08)
602 void CallListener::sendRetrySignal(QString const& iap, bool retry)
604 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
608 QList<QVariant> arguments;
609 arguments.append(QVariant(iap));
610 arguments.append(QVariant(retry));
611 msg.setArguments(arguments);
613 QDBusConnection::systemBus().send(msg);
615 qDebug() << "Retry signal sent";
618 void CallListener::timerEvent(QTimerEvent* event)
625 void CallListener::sleep(int ms)
632 timer_ = startTimer(ms);
636 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);