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 QMap<QString, QString> tmpConfig;
64 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
65 config->getConfig(tmpConfig);
66 sourceConfig_ = tmpConfig;
69 systemBus_.connect(CALL_SERVICE_NAME,
71 CALL_SERVICE_INTERFACE,
74 SLOT(incomingCall(QDBusObjectPath, QString)));
76 systemBus_.connect(CALL_SERVICE_NAME,
78 CALL_SERVICE_INTERFACE,
81 SLOT(callTerminate()));
83 qDebug() << "Starting...";
89 void CallListener::end()
91 systemBus_.disconnect(CALL_SERVICE_NAME,
93 CALL_SERVICE_INTERFACE,
96 SLOT(incomingCall(QDBusObjectPath, QString)));
98 systemBus_.disconnect(CALL_SERVICE_NAME,
100 CALL_SERVICE_INTERFACE,
103 SLOT(callTerminate()));
106 sourceConfig_.clear();
110 void CallListener::search(Source::SearchDetails const& details)
112 qDebug() << "Search called";
116 Source::Result result;
118 if(Cache::instance().findItem(details.query, result))
121 qDebug() << "Found from cache";
123 showDelayedResult(createResult(result.name,
125 result.city), BANNER_DELAY);
130 currentSearch_ = details.query;
132 if(!handleConnection())
134 qDebug() << "Unable to connect";
138 showDelayedResult(tr("Searching..."), BANNER_DELAY);
140 qDebug() << "Starting to search...";
142 source_->search(details);
147 void CallListener::requestFinished(QVector <Source::Result> const& results,
148 Source::SearchDetails const& details,
153 closeConnection_ = false;
154 ConnectionManager cm;
158 // If box is not visible, the call must have been terminated already
159 if(!initialized_ || !box_->isVisible())
168 qDebug() << "Error: " << source_->errorString();
170 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
173 source_->search(Source::SearchDetails(currentSearch_, "", Source::BOTH));
180 Source::Error error = source_->error();
184 case Source::TIMEOUT:
185 errorString = tr("Request timed out");
188 errorString = source_->errorString();
192 showError(tr("Searching failed:") + " " + errorString + ".");
199 if(results.size() == 0)
201 message = tr("Phone number was not found");
206 message = createResult(results.at(0).name, results.at(0).street,
209 Source::Result result = results.at(0);
210 result.number = details.query;
211 Cache::instance().addItem(result);
219 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
221 QString result = "<b>" + name + "</b>";
223 if(!street.isEmpty() || !city.isEmpty())
227 if(!street.isEmpty())
229 result += street + ", ";
238 void CallListener::showResult(QString const& text)
245 label_->setText("<font color='black'>" + text + "</font>");
247 if(box_->isVisible())
255 void CallListener::incomingCall(QDBusObjectPath path, QString number)
259 if(!cm.numberExists(number))
261 qDebug() << "Number doesn't exist";
263 systemBus_.connect(CALL_SERVICE_NAME,
265 CALL_SERVICE_INSTANCE_NAME,
266 CALL_SIGNAL_TERMINATED,
268 SLOT(callTerminate()));
270 search(Source::SearchDetails(number, "", Source::BOTH));
274 qDebug() << "Number exists";
278 void CallListener::callTerminate()
280 if(box_ && box_->isVisible())
288 void CallListener::showDelayedResult(QString const& text, int delay)
290 timedMessage_ = text;
291 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
294 void CallListener::showTimedMessage()
296 if(timedMessage_.size() == 0 || !initialized_)
301 showResult(timedMessage_);
306 void CallListener::searchInit()
308 qDebug() << "Initializing search...";
312 qDebug() << "Already initialized";
316 source_ = Source::getSource(sourceId_);
317 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
318 config->loadFromConfig(sourceConfig_);
319 config->apply(source_);
321 source_->setMaxResults(1);
322 source_->setFindNumber(false);
323 source_->setTimeout(REQUEST_TIMEOUT);
325 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
326 Source::SearchDetails const&, bool)),
327 this, SLOT(requestFinished(QVector <Source::Result> const&,
328 Source::SearchDetails const&, bool)));
329 box_ = new InformationBox;
330 label_ = new QLabel("", box_);
331 label_->setMargin(8);
332 label_->setWordWrap(true);
333 box_->setWidget(label_);
337 void CallListener::searchClose()
344 initialized_ = false;
346 qDebug() << "Closing search...";
350 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
351 Source::SearchDetails const&, bool)),
352 this, SLOT(requestFinished(QVector <Source::Result> const&,
353 Source::SearchDetails const&, bool)));
364 closeConnection_ = false;
365 ConnectionManager cm;
370 bool CallListener::handleConnection()
377 ConnectionManager cm;
381 closeConnection_ = false;
385 closeConnection_ = true;
387 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
389 if(configType == Settings::ALWAYS_ASK)
391 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
395 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
397 ConnectionManager::Connection best;
399 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
404 lookupType = ConnectionManager::WLAN;
407 lookupType = ConnectionManager::GPRS;
410 lookupType = ConnectionManager::NO_TYPE;
417 int maxScans = GPRS_SCANS;
419 if(lookupType != ConnectionManager::GPRS)
421 maxScans = WLAN_SCANS;
424 while(cretries < CONNECTION_LOOKUP_RETRIES)
433 if(cm.getBestConnection(best, lookupType))
441 // If there is only gprs connection available,
442 // make sure that we are on 3g network
443 if(found && (best.type != ConnectionManager::GPRS || is3g()))
450 sleep(WAIT_BETWEEN_RETRIES);
453 qDebug() << "No connections found, retrying...";
459 if(cretries >= CONNECTION_LOOKUP_RETRIES)
461 showError(tr("No available 3G or WLAN networks found."));
467 while(retries < CONNECT_RETRIES)
474 qDebug() << "Connecting to " << best.name;
476 if(cm.connect(best.id))
480 else if(cm.error() == ConnectionManager::INVALID_IAP)
482 showError(tr("Selected access point doesn't exist."));
488 qDebug() << "Unable to connect, retrying...";
490 if(retries < CONNECT_RETRIES)
492 sendRetrySignal(best.id, initialized_);
497 if(retries >= CONNECT_RETRIES)
499 sendRetrySignal(best.id, false);
503 showError(tr("Unable to connect to network."));
512 void CallListener::showError(QString const& msg, int timeout)
514 qDebug() << "Error: " << msg;
516 if(!initialized_ || !box_)
521 box_->setTimeout(ERROR_BANNER_TIMEOUT);
525 showDelayedResult(msg, timeout);
533 bool CallListener::is3g()
535 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
536 "/com/nokia/phone/net",
538 "get_registration_status");
540 QDBusMessage rep = systemBus_.call(msg);
542 if(rep.type() == QDBusMessage::ErrorMessage)
544 qDebug() << "Unable to get network status";
548 uint status = rep.arguments().value(6).toUInt();
550 if(status & 0x10 || status & 0x08)
558 void CallListener::sendRetrySignal(QString const& iap, bool retry)
560 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
564 QList<QVariant> arguments;
565 arguments.append(QVariant(iap));
566 arguments.append(QVariant(retry));
567 msg.setArguments(arguments);
569 QDBusConnection::systemBus().send(msg);
571 qDebug() << "Retry signal sent";
574 void CallListener::timerEvent(QTimerEvent* event)
581 void CallListener::sleep(int ms)
588 timer_ = startTimer(ms);
592 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);