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()));
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,
149 // If box is not visible, the call must have been terminated already
150 if(!initialized_ || !box_->isVisible())
159 qDebug() << "Error: " << source_->errorString();
161 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
164 source_->search(Source::SearchDetails(currentSearch_));
171 Source::Error error = source_->error();
175 case Source::TIMEOUT:
176 errorString = tr("Request timed out");
179 errorString = source_->errorString();
183 showError(tr("Searching failed:") + " " + errorString + ".");
190 if(results.size() == 0)
192 message = tr("Phone number was not found");
197 message = createResult(results.at(0).name, results.at(0).street,
200 Source::Result result = results.at(0);
201 result.number = details.query;
202 Cache::instance().addItem(result);
211 ConnectionManager cm;
213 closeConnection_ = false;
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)
255 qDebug() << "Incoming: " << 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));
274 qDebug() << "Number exists";
278 void CallListener::callTerminate()
280 if(box_ && box_->isVisible())
287 ConnectionManager cm;
289 closeConnection_ = false;
295 void CallListener::showDelayedResult(QString const& text, int delay)
297 timedMessage_ = text;
298 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
301 void CallListener::showTimedMessage()
303 if(timedMessage_.size() == 0 || !initialized_)
308 showResult(timedMessage_);
313 void CallListener::searchInit()
315 qDebug() << "Initializing search...";
319 qDebug() << "Already initialized";
323 source_ = Source::getSource(sourceId_);
324 SourceCoreConfig* config = SourceCoreConfig::getCoreConfig(sourceId_);
325 config->loadFromConfig(sourceConfig_);
326 config->apply(source_);
328 source_->setMaxResults(1);
329 source_->setFindNumber(false);
330 source_->setTimeout(REQUEST_TIMEOUT);
332 connect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
333 Source::SearchDetails const&, bool)),
334 this, SLOT(requestFinished(QVector <Source::Result> const&,
335 Source::SearchDetails const&, bool)));
336 box_ = new InformationBox;
337 label_ = new QLabel("", box_);
338 label_->setMargin(8);
339 label_->setWordWrap(true);
340 box_->setWidget(label_);
344 void CallListener::searchClose()
346 initialized_ = false;
348 qDebug() << "Closing search...";
352 disconnect(source_, SIGNAL(requestFinished(QVector <Source::Result> const&,
353 Source::SearchDetails const&, bool)),
354 this, SLOT(requestFinished(QVector <Source::Result> const&,
355 Source::SearchDetails const&, bool)));
365 bool CallListener::handleConnection()
372 ConnectionManager cm;
376 closeConnection_ = false;
380 closeConnection_ = true;
382 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
384 if(configType == Settings::ALWAYS_ASK)
386 showError(tr("Automatic connecting is not allowed by settings."), BANNER_DELAY);
390 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
392 ConnectionManager::Connection best;
394 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
399 lookupType = ConnectionManager::WLAN;
402 lookupType = ConnectionManager::GPRS;
405 lookupType = ConnectionManager::NO_TYPE;
411 while(cretries < CONNECTION_LOOKUP_RETRIES)
418 if(cm.getBestConnection(best, lookupType))
423 qDebug() << "No connections found, retrying...";
427 sleep(WAIT_BETWEEN_RETRIES);
431 if(cretries >= CONNECTION_LOOKUP_RETRIES)
433 showError(tr("No available 3G or WLAN networks found."));
439 while(retries < CONNECT_RETRIES)
446 sleep(WAIT_BETWEEN_RETRIES);
448 if(cm.connect(best.id))
452 else if(cm.error() == ConnectionManager::INVALID_IAP)
454 showError(tr("Selected access point doesn't exist."));
458 qDebug() << "Unable to connect, retrying...";
463 if(initialized_ && retries >= CONNECT_RETRIES)
465 showError(tr("Unable to connect to network."));
472 void CallListener::showError(QString const& msg, int timeout)
474 qDebug() << "Error: " << msg;
475 box_->setTimeout(ERROR_BANNER_TIMEOUT);
479 showDelayedResult(msg, timeout);
487 void CallListener::timerEvent(QTimerEvent* event)
494 void CallListener::sleep(int ms)
501 timer_ = startTimer(ms);
505 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);