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 "calllistener.h"
24 #include "contactmanager.h"
25 #include "connectionmanager.h"
26 #include "sourcecoreconfig.h"
31 const QString CALL_SERVICE_NAME = "com.nokia.csd";
32 const QString CALL_SERVICE_PATH = "/com/nokia/csd/call";
33 const QString CALL_SERVICE_INTERFACE = "com.nokia.csd.Call";
34 const QString CALL_SERVICE_INSTANCE_NAME = "com.nokia.csd.Call.Instance";
35 const QString CALL_SIGNAL_INCOMING = "Coming";
36 const QString CALL_SIGNAL_RELEASE = "Release";
37 const QString CALL_SIGNAL_TERMINATED = "Terminated";
40 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
42 CallListener::CallListener(): source_(0),
43 closeConnection_(false), initialized_(false), box_(0), label_(0),
44 retries_(-1), timer_(0)
48 CallListener::~CallListener()
54 bool CallListener::begin()
56 if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
58 qDebug() << "Bad connection settings, unable to start";
62 sourceId_ = Source::stringToId(Settings::instance()->get("source"));
63 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
64 config->getConfig(sourceConfig_);
67 systemBus_.connect(CALL_SERVICE_NAME,
69 CALL_SERVICE_INTERFACE,
72 SLOT(incomingCall(QDBusObjectPath, QString)));
74 systemBus_.connect(CALL_SERVICE_NAME,
76 CALL_SERVICE_INTERFACE,
79 SLOT(callTerminate()));
81 qDebug() << "Starting...";
87 void CallListener::end()
89 systemBus_.disconnect(CALL_SERVICE_NAME,
91 CALL_SERVICE_INTERFACE,
94 SLOT(incomingCall(QDBusObjectPath, QString)));
96 systemBus_.disconnect(CALL_SERVICE_NAME,
98 CALL_SERVICE_INTERFACE,
101 SLOT(callTerminate()));
104 sourceConfig_.clear();
108 void CallListener::search(Source::SearchDetails const& details)
110 qDebug() << "Search called";
114 Source::Result result;
116 if(Cache::instance().findItem(details.query, result))
119 qDebug() << "Found from cache";
121 showDelayedResult(createResult(result.name,
123 result.city), BANNER_DELAY);
128 currentSearch_ = details.query;
130 if(!handleConnection())
132 qDebug() << "Unable to connect";
136 showDelayedResult(tr("Searching..."), BANNER_DELAY);
138 qDebug() << "Starting to search...";
140 source_->search(details);
145 void CallListener::requestFinished(QVector <Source::Result> const& results,
146 Source::SearchDetails const& details,
151 closeConnection_ = false;
152 ConnectionManager cm;
156 // If box is not visible, the call must have been terminated already
157 if(!initialized_ || !box_->isVisible())
166 qDebug() << "Error: " << source_->errorString();
168 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
171 source_->search(Source::SearchDetails(currentSearch_));
178 Source::Error error = source_->error();
182 case Source::TIMEOUT:
183 errorString = tr("Request timed out");
186 errorString = source_->errorString();
190 showError(tr("Searching failed:") + " " + errorString + ".");
197 if(results.size() == 0)
199 message = tr("Phone number was not found");
204 message = createResult(results.at(0).name, results.at(0).street,
207 Source::Result result = results.at(0);
208 result.number = details.query;
209 Cache::instance().addItem(result);
217 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
219 QString result = "<b>" + name + "</b>";
221 if(!street.isEmpty() || !city.isEmpty())
225 if(!street.isEmpty())
227 result += street + ", ";
236 void CallListener::showResult(QString const& text)
243 label_->setText("<font color='black'>" + text + "</font>");
245 if(box_->isVisible())
253 void CallListener::incomingCall(QDBusObjectPath path, QString number)
257 if(!cm.numberExists(number))
259 qDebug() << "Number doesn't exist";
261 systemBus_.connect(CALL_SERVICE_NAME,
263 CALL_SERVICE_INSTANCE_NAME,
264 CALL_SIGNAL_TERMINATED,
266 SLOT(callTerminate()));
268 search(Source::SearchDetails(number));
272 qDebug() << "Number exists";
276 void CallListener::callTerminate()
278 if(box_ && box_->isVisible())
286 void CallListener::showDelayedResult(QString const& text, int delay)
288 timedMessage_ = text;
289 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
292 void CallListener::showTimedMessage()
294 if(timedMessage_.size() == 0 || !initialized_)
299 showResult(timedMessage_);
304 void CallListener::searchInit()
306 qDebug() << "Initializing search...";
310 qDebug() << "Already initialized";
314 source_ = Source::getSource(sourceId_);
315 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
316 config->loadFromConfig(sourceConfig_);
317 config->apply(source_);
319 source_->setMaxResults(1);
320 source_->setFindNumber(false);
321 source_->setTimeout(REQUEST_TIMEOUT);
323 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
324 Source::SearchDetails const&, bool)),
325 this, SLOT(requestFinished(QVector <Source::Result> const&,
326 Source::SearchDetails const&, bool)));
327 box_ = new InformationBox;
328 label_ = new QLabel("", box_);
329 label_->setMargin(8);
330 label_->setWordWrap(true);
331 box_->setWidget(label_);
335 void CallListener::searchClose()
342 initialized_ = false;
344 qDebug() << "Closing search...";
348 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
349 Source::SearchDetails const&, bool)),
350 this, SLOT(requestFinished(QVector <Source::Result> const&,
351 Source::SearchDetails const&, bool)));
362 closeConnection_ = false;
363 ConnectionManager cm;
368 bool CallListener::handleConnection()
375 ConnectionManager cm;
379 closeConnection_ = false;
383 closeConnection_ = true;
385 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
387 if(configType == Settings::ALWAYS_ASK)
389 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
393 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
395 ConnectionManager::Connection best;
397 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
402 lookupType = ConnectionManager::WLAN;
405 lookupType = ConnectionManager::GPRS;
408 lookupType = ConnectionManager::NO_TYPE;
415 int maxScans = GPRS_SCANS;
417 if(lookupType != ConnectionManager::GPRS)
419 maxScans = WLAN_SCANS;
422 while(cretries < CONNECTION_LOOKUP_RETRIES)
431 if(cm.getBestConnection(best, lookupType))
439 // If there is only gprs connection available,
440 // make sure that we are on 3g network
441 if(found && (best.type != ConnectionManager::GPRS || is3g()))
448 sleep(WAIT_BETWEEN_RETRIES);
451 qDebug() << "No connections found, retrying...";
457 if(cretries >= CONNECTION_LOOKUP_RETRIES)
459 showError(tr("No available 3G or WLAN networks found."));
465 while(retries < CONNECT_RETRIES)
472 qDebug() << "Connecting to " << best.name;
474 if(cm.connect(best.id))
478 else if(cm.error() == ConnectionManager::INVALID_IAP)
480 showError(tr("Selected access point doesn't exist."));
486 qDebug() << "Unable to connect, retrying...";
488 if(retries < CONNECT_RETRIES)
490 sendRetrySignal(best.id, initialized_);
495 if(retries >= CONNECT_RETRIES)
497 sendRetrySignal(best.id, false);
501 showError(tr("Unable to connect to network."));
510 void CallListener::showError(QString const& msg, int timeout)
512 qDebug() << "Error: " << msg;
514 if(!initialized_ || !box_)
519 box_->setTimeout(ERROR_BANNER_TIMEOUT);
523 showDelayedResult(msg, timeout);
531 bool CallListener::is3g()
533 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
534 "/com/nokia/phone/net",
536 "get_registration_status");
538 QDBusMessage rep = systemBus_.call(msg);
540 if(rep.type() == QDBusMessage::ErrorMessage)
542 qDebug() << "Unable to get network status";
546 uint status = rep.arguments().value(6).toUInt();
548 if(status & 0x10 || status & 0x08)
556 void CallListener::sendRetrySignal(QString const& iap, bool retry)
558 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
562 QList<QVariant> arguments;
563 arguments.append(QVariant(iap));
564 arguments.append(QVariant(retry));
565 msg.setArguments(arguments);
567 QDBusConnection::systemBus().send(msg);
569 qDebug() << "Retry signal sent";
572 void CallListener::timerEvent(QTimerEvent* event)
579 void CallListener::sleep(int ms)
586 timer_ = startTimer(ms);
590 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);