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()
53 bool CallListener::begin()
55 if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
57 qDebug() << "Bad connection settings, unable to start";
61 sourceId_ = Source::stringToId(Settings::instance()->get("source"));
62 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
63 config->getConfig(sourceConfig_);
66 systemBus_.connect(CALL_SERVICE_NAME,
68 CALL_SERVICE_INTERFACE,
71 SLOT(incomingCall(QDBusObjectPath, QString)));
73 systemBus_.connect(CALL_SERVICE_NAME,
75 CALL_SERVICE_INTERFACE,
78 SLOT(callTerminate()));
80 qDebug() << "Starting...";
86 void CallListener::end()
88 systemBus_.disconnect(CALL_SERVICE_NAME,
90 CALL_SERVICE_INTERFACE,
93 SLOT(incomingCall(QDBusObjectPath, QString)));
95 systemBus_.disconnect(CALL_SERVICE_NAME,
97 CALL_SERVICE_INTERFACE,
100 SLOT(callTerminate()));
103 sourceConfig_.clear();
107 void CallListener::search(Source::SearchDetails const& details)
109 qDebug() << "Search called";
113 Source::Result result;
115 if(Cache::instance().findItem(details.query, result))
118 qDebug() << "Found from cache";
120 showDelayedResult(createResult(result.name,
122 result.city), BANNER_DELAY);
127 currentSearch_ = details.query;
129 if(!handleConnection())
131 qDebug() << "Unable to connect";
135 showDelayedResult(tr("Searching..."), BANNER_DELAY);
137 qDebug() << "Starting to search...";
139 source_->search(details);
144 void CallListener::requestFinished(QVector <Source::Result> const& results,
145 Source::SearchDetails const& details,
148 // If box is not visible, the call must have been terminated already
149 if(!initialized_ || !box_->isVisible())
158 qDebug() << "Error: " << source_->errorString();
160 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
163 source_->search(Source::SearchDetails(currentSearch_));
170 Source::Error error = source_->error();
174 case Source::TIMEOUT:
175 errorString = tr("Request timed out");
178 errorString = source_->errorString();
182 showError(tr("Searching failed:") + " " + errorString + ".");
189 if(results.size() == 0)
191 message = tr("Phone number was not found");
196 message = createResult(results.at(0).name, results.at(0).street,
199 Source::Result result = results.at(0);
200 result.number = details.query;
201 Cache::instance().addItem(result);
210 closeConnection_ = false;
211 ConnectionManager cm;
216 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
218 QString result = "<b>" + name + "</b>";
220 if(!street.isEmpty() || !city.isEmpty())
224 if(!street.isEmpty())
226 result += street + ", ";
235 void CallListener::showResult(QString const& text)
242 label_->setText("<font color='black'>" + text + "</font>");
244 if(box_->isVisible())
252 void CallListener::incomingCall(QDBusObjectPath path, QString number)
256 if(!cm.numberExists(number))
258 qDebug() << "Number doesn't exist";
260 systemBus_.connect(CALL_SERVICE_NAME,
262 CALL_SERVICE_INSTANCE_NAME,
263 CALL_SIGNAL_TERMINATED,
265 SLOT(callTerminate()));
267 search(Source::SearchDetails(number));
271 qDebug() << "Number exists";
275 void CallListener::callTerminate()
277 if(box_ && box_->isVisible())
284 closeConnection_ = false;
285 ConnectionManager cm;
292 void CallListener::showDelayedResult(QString const& text, int delay)
294 timedMessage_ = text;
295 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
298 void CallListener::showTimedMessage()
300 if(timedMessage_.size() == 0 || !initialized_)
305 showResult(timedMessage_);
310 void CallListener::searchInit()
312 qDebug() << "Initializing search...";
316 qDebug() << "Already initialized";
320 source_ = Source::getSource(sourceId_);
321 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
322 config->loadFromConfig(sourceConfig_);
323 config->apply(source_);
325 source_->setMaxResults(1);
326 source_->setFindNumber(false);
327 source_->setTimeout(REQUEST_TIMEOUT);
329 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
330 Source::SearchDetails const&, bool)),
331 this, SLOT(requestFinished(QVector <Source::Result> const&,
332 Source::SearchDetails const&, bool)));
333 box_ = new InformationBox;
334 label_ = new QLabel("", box_);
335 label_->setMargin(8);
336 label_->setWordWrap(true);
337 box_->setWidget(label_);
341 void CallListener::searchClose()
343 initialized_ = false;
345 qDebug() << "Closing search...";
349 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
350 Source::SearchDetails const&, bool)),
351 this, SLOT(requestFinished(QVector <Source::Result> const&,
352 Source::SearchDetails const&, bool)));
362 bool CallListener::handleConnection()
369 ConnectionManager cm;
373 closeConnection_ = false;
377 closeConnection_ = true;
379 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
381 if(configType == Settings::ALWAYS_ASK)
383 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
387 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
389 ConnectionManager::Connection best;
391 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
396 lookupType = ConnectionManager::WLAN;
399 lookupType = ConnectionManager::GPRS;
402 lookupType = ConnectionManager::NO_TYPE;
409 int maxScans = GPRS_SCANS;
411 if(lookupType == ConnectionManager::WLAN)
413 maxScans = WLAN_SCANS;
416 while(cretries < CONNECTION_LOOKUP_RETRIES)
425 if(cm.getBestConnection(best, lookupType))
433 // If there is only gprs connection available,
434 // make sure that we are on 3g network
435 if(found && (best.type != ConnectionManager::GPRS || is3g()))
442 sleep(WAIT_BETWEEN_RETRIES);
445 qDebug() << "No connections found, retrying...";
451 if(cretries >= CONNECTION_LOOKUP_RETRIES)
453 showError(tr("No available 3G or WLAN networks found."));
459 while(retries < CONNECT_RETRIES)
466 qDebug() << "Connecting to " << best.name;
468 if(cm.connect(best.id))
472 else if(cm.error() == ConnectionManager::INVALID_IAP)
474 showError(tr("Selected access point doesn't exist."));
478 sleep(WAIT_BETWEEN_RETRIES);
480 qDebug() << "Unable to connect, retrying...";
485 if(initialized_ && retries >= CONNECT_RETRIES)
487 showError(tr("Unable to connect to network."));
494 void CallListener::showError(QString const& msg, int timeout)
496 qDebug() << "Error: " << msg;
498 if(!initialized_ || !box_)
503 box_->setTimeout(ERROR_BANNER_TIMEOUT);
507 showDelayedResult(msg, timeout);
515 bool CallListener::is3g()
517 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
518 "/com/nokia/phone/net",
520 "get_registration_status");
522 QDBusMessage rep = systemBus_.call(msg);
524 if(rep.type() == QDBusMessage::ErrorMessage)
526 qDebug() << "Unable to get network status";
530 uint status = rep.arguments().value(6).toUInt();
532 if(status & 0x10) // 3.5G (HSUPA)
536 else if(status & 0x08) // 3G (HSDPA)
545 void CallListener::timerEvent(QTimerEvent* event)
552 void CallListener::sleep(int ms)
559 timer_ = startTimer(ms);
563 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);