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 qDebug() << "Request finished";
169 // If box is not visible, the call must have been terminated already
170 if(!initialized_ || !box_->isVisible() || !currentCall_)
179 qDebug() << "Error: " << source_->errorString();
181 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
184 source_->search(Source::SearchDetails(currentCall_->number, "", Source::BOTH));
191 Source::Error error = source_->error();
195 case Source::TIMEOUT:
196 errorString = tr("Request timed out");
199 errorString = source_->errorString();
203 showError(tr("Searching failed:") + " " + errorString + ".");
210 if(results.size() == 0)
212 message = tr("Phone number was not found");
217 message = createResult(results.at(0).name, results.at(0).street,
220 Source::Result result = results.at(0);
221 result.number = details.query;
222 Cache::instance().addItem(result);
223 currentCall_->result = results.at(0);
230 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
232 QString result = "<b>" + name + "</b>";
234 if(!street.isEmpty() || !city.isEmpty())
238 if(!street.isEmpty())
240 result += street + ", ";
249 void CallListener::showResult(QString const& text)
256 label_->setText("<font color='black'>" + text + "</font>");
258 if(box_->isVisible())
266 void CallListener::incomingCall(QDBusObjectPath path, QString number)
270 qDebug() << "Unknown caller without number";
276 if(!cm.numberExists(number))
278 qDebug() << "Number doesn't exist: " << number;
280 systemBus_.connect(CALL_SERVICE_NAME,
282 CALL_SERVICE_INSTANCE_NAME,
283 CALL_SIGNAL_TERMINATED,
285 SLOT(callTerminate()));
287 systemBus_.connect(CALL_SERVICE_NAME,
289 CALL_SERVICE_INSTANCE_NAME,
290 CALL_SIGNAL_ANSWERED,
292 SLOT(handleAnswer()));
294 search(Source::SearchDetails(number, "", Source::BOTH));
298 qDebug() << "Number exists: " << number;
302 void CallListener::callTerminate()
304 if(box_ && box_->isVisible())
311 currentCall_->result.number = currentCall_->number;
312 Cache::instance().logItem(currentCall_->result, !currentCall_->answered, currentCall_->time);
320 void CallListener::handleAnswer()
322 qDebug() << "Answered";
326 currentCall_->answered = true;
330 void CallListener::showDelayedResult(QString const& text, int delay)
332 timedMessage_ = text;
333 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
336 void CallListener::showTimedMessage()
338 if(timedMessage_.size() == 0 || !initialized_)
343 showResult(timedMessage_);
348 void CallListener::searchInit()
350 qDebug() << "Initializing search...";
354 qDebug() << "Already initialized";
358 source_ = Source::getSource(sourceId_);
359 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
360 config->loadFromConfig(sourceConfig_);
361 config->apply(source_);
363 source_->setMaxResults(1);
364 source_->setFindNumber(false);
365 source_->setTimeout(REQUEST_TIMEOUT);
367 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
368 Source::SearchDetails const&, bool)),
369 this, SLOT(requestFinished(QVector <Source::Result> const&,
370 Source::SearchDetails const&, bool)));
371 box_ = new InformationBox;
372 label_ = new QLabel("", box_);
373 label_->setMargin(8);
374 label_->setWordWrap(true);
375 box_->setWidget(label_);
379 void CallListener::searchClose()
386 initialized_ = false;
388 qDebug() << "Closing search...";
392 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
393 Source::SearchDetails const&, bool)),
394 this, SLOT(requestFinished(QVector <Source::Result> const&,
395 Source::SearchDetails const&, bool)));
406 QTimer::singleShot(500, this, SLOT(closeConnection()));
410 void CallListener::closeConnection()
414 closeConnection_ = false;
415 ConnectionManager cm;
420 bool CallListener::handleConnection()
427 ConnectionManager cm;
432 closeConnection_ = false;
436 closeConnection_ = true;
438 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
440 if(configType == Settings::ALWAYS_ASK)
442 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
446 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
448 ConnectionManager::Connection best;
450 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
455 lookupType = ConnectionManager::WLAN;
458 lookupType = ConnectionManager::GPRS;
461 lookupType = ConnectionManager::NO_TYPE;
468 int maxScans = GPRS_SCANS;
470 if(lookupType != ConnectionManager::GPRS)
472 maxScans = WLAN_SCANS;
475 while(cretries < CONNECTION_LOOKUP_RETRIES)
484 if(cm.getBestConnection(best, lookupType))
492 // If there is only gprs connection available,
493 // make sure that we are on 3g network
494 if(found && (best.type != ConnectionManager::GPRS || is3g()))
501 sleep(WAIT_BETWEEN_RETRIES);
504 qDebug() << "No connections found, retrying...";
510 if(cretries >= CONNECTION_LOOKUP_RETRIES)
512 showError(tr("No available 3G or WLAN networks found."));
518 while(retries < CONNECT_RETRIES)
525 qDebug() << "Connecting to " << best.name << " (" << best.id << ")";
527 if(cm.connect(best.id))
532 else if(cm.error() == ConnectionManager::INVALID_IAP)
534 showError(tr("Selected access point doesn't exist."));
540 qDebug() << "Unable to connect, retrying...";
542 if(retries < CONNECT_RETRIES)
544 sendRetrySignal(best.id, false);
545 sleep(WAIT_BETWEEN_RETRIES);
550 if(retries >= CONNECT_RETRIES)
552 sendRetrySignal(best.id, false);
556 showError(tr("Unable to connect to network."));
565 void CallListener::showError(QString const& msg, int timeout)
567 qDebug() << "Error: " << msg;
569 if(!initialized_ || !box_)
574 box_->setTimeout(ERROR_BANNER_TIMEOUT);
578 showDelayedResult(msg, timeout);
586 bool CallListener::is3g()
588 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
589 "/com/nokia/phone/net",
591 "get_registration_status");
593 QDBusMessage rep = systemBus_.call(msg);
595 if(rep.type() == QDBusMessage::ErrorMessage)
597 qDebug() << "Unable to get network status";
601 uint status = rep.arguments().value(6).toUInt();
603 if(status & 0x10 || status & 0x08)
611 void CallListener::sendRetrySignal(QString const& iap, bool retry)
613 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
617 QList<QVariant> arguments;
618 arguments.append(QVariant(iap));
619 arguments.append(QVariant(retry));
620 msg.setArguments(arguments);
622 QDBusConnection::systemBus().send(msg);
624 qDebug() << "Retry signal sent";
627 void CallListener::timerEvent(QTimerEvent* event)
634 void CallListener::sleep(int ms)
641 timer_ = startTimer(ms);
645 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);