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"
30 const QString CALL_SERVICE_NAME = "com.nokia.csd";
31 const QString CALL_SERVICE_PATH = "/com/nokia/csd/call";
32 const QString CALL_SERVICE_INTERFACE = "com.nokia.csd.Call";
33 const QString CALL_SERVICE_INSTANCE_NAME = "com.nokia.csd.Call.Instance";
34 const QString CALL_SIGNAL_INCOMING = "Coming";
35 const QString CALL_SIGNAL_RELEASE = "Release";
36 const QString CALL_SIGNAL_TERMINATED = "Terminated";
39 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
41 CallListener::CallListener(): eniro_(0),
42 closeConnection_(false), initialized_(false), box_(0), label_(0),
43 retries_(-1), site_(Eniro::FI), timer_(0), autoconnect_(false)
47 CallListener::~CallListener()
53 bool CallListener::begin()
55 if(Settings::instance()->getConnectionType() == Settings::ALWAYS_ASK)
57 qDebug() << "Bad connection settings, unable to start";
61 systemBus_.connect(CALL_SERVICE_NAME,
63 CALL_SERVICE_INTERFACE,
66 SLOT(incomingCall(QDBusObjectPath, QString)));
68 systemBus_.connect(CALL_SERVICE_NAME,
70 CALL_SERVICE_INTERFACE,
73 SLOT(callTerminate()));
75 site_ = Eniro::stringToSite(Settings::instance()->get("site"));
77 qDebug() << "Starting...";
83 void CallListener::end()
85 systemBus_.disconnect(CALL_SERVICE_NAME,
87 CALL_SERVICE_INTERFACE,
90 SLOT(incomingCall(QDBusObjectPath, QString)));
92 systemBus_.disconnect(CALL_SERVICE_NAME,
94 CALL_SERVICE_INTERFACE,
97 SLOT(callTerminate()));
103 void CallListener::search(Eniro::SearchDetails const& details)
105 qDebug() << "Search called";
109 Eniro::Result result;
111 if(Cache::instance().findItem(details.query, result))
114 qDebug() << "Found from cache";
116 showDelayedResult(createResult(result.name,
118 result.city), BANNER_DELAY);
123 currentSearch_ = details.query;
125 if(!handleConnection())
127 qDebug() << "Unable to connect";
131 showDelayedResult(tr("Searching..."), BANNER_DELAY);
133 qDebug() << "Starting to search...";
135 eniro_->search(details);
140 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
141 Eniro::SearchDetails const& details,
144 // If box is not visible, the call must have been terminated already
145 if(!initialized_ || !box_->isVisible())
154 qDebug() << "Error: " << eniro_->errorString();
156 if(retries_ < SEARCH_RETRIES && retries_ >= 0)
159 eniro_->search(Eniro::SearchDetails(currentSearch_));
166 Eniro::Error error = eniro_->error();
171 errorString = tr("Request timed out");
174 errorString = eniro_->errorString();
178 showError(tr("Searching failed:") + " " + errorString + ".");
185 if(results.size() == 0)
187 message = tr("Phone number was not found");
192 message = createResult(results.at(0).name, results.at(0).street,
195 Eniro::Result result = results.at(0);
196 result.number = details.query;
197 Cache::instance().addItem(result);
206 ConnectionManager cm;
208 closeConnection_ = false;
212 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
214 QString result = "<b>" + name + "</b>";
216 if(!street.isEmpty() || !city.isEmpty())
220 if(!street.isEmpty())
222 result += street + ", ";
231 void CallListener::showResult(QString const& text)
238 label_->setText("<font color='black'>" + text + "</font>");
240 if(box_->isVisible())
248 void CallListener::incomingCall(QDBusObjectPath path, QString number)
250 qDebug() << "Incoming: " << number;
254 if(!cm.numberExists(number))
256 qDebug() << "Number doesn't exist";
258 systemBus_.connect(CALL_SERVICE_NAME,
260 CALL_SERVICE_INSTANCE_NAME,
261 CALL_SIGNAL_TERMINATED,
263 SLOT(callTerminate()));
265 search(Eniro::SearchDetails(number));
269 qDebug() << "Number exists";
273 void CallListener::callTerminate()
275 if(box_ && box_->isVisible())
282 ConnectionManager cm;
284 closeConnection_ = false;
290 void CallListener::showDelayedResult(QString const& text, int delay)
292 timedMessage_ = text;
293 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
296 void CallListener::showTimedMessage()
298 if(timedMessage_.size() == 0 || !initialized_)
303 showResult(timedMessage_);
308 void CallListener::searchInit()
310 qDebug() << "Initializing search...";
314 qDebug() << "Already initialized";
318 eniro_ = new Eniro(site_);
319 eniro_->setMaxResults(1);
320 eniro_->setFindNumber(false);
321 eniro_->setTimeout(REQUEST_TIMEOUT);
323 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
324 Eniro::SearchDetails const&, bool)),
325 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
326 Eniro::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()
337 initialized_ = false;
339 qDebug() << "Closing search...";
343 disconnect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
344 Eniro::SearchDetails const&, bool)),
345 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
346 Eniro::SearchDetails const&, bool)));
356 bool CallListener::handleConnection()
363 ConnectionManager cm;
367 closeConnection_ = false;
371 closeConnection_ = true;
373 Settings::ConnectionType configType = Settings::instance()->getConnectionType();
375 if(configType == Settings::ALWAYS_ASK)
377 showError(tr("Automatic connecting is not allowed by settings."));
381 showDelayedResult(tr("Connecting..."), BANNER_DELAY);
383 ConnectionManager::Connection best;
385 ConnectionManager::ConnectionType lookupType = ConnectionManager::NO_TYPE;
390 lookupType = ConnectionManager::WLAN;
393 lookupType = ConnectionManager::GPRS;
396 lookupType = ConnectionManager::NO_TYPE;
402 while(cretries < CONNECTION_LOOKUP_RETRIES)
409 if(cm.getBestConnection(best, lookupType))
414 qDebug() << "No connections found, retrying...";
418 sleep(WAIT_BETWEEN_RETRIES);
422 if(cretries >= CONNECTION_LOOKUP_RETRIES)
424 showError(tr("No available 3G or WLAN networks found."));
430 while(retries < CONNECT_RETRIES)
437 sleep(WAIT_BETWEEN_RETRIES);
439 if(cm.connect(best.id))
443 else if(cm.error() == ConnectionManager::INVALID_IAP)
445 showError(tr("Selected access point doesn't exist."));
449 qDebug() << "Unable to connect, retrying...";
454 if(initialized_ && retries >= CONNECT_RETRIES)
456 showError(tr("Unable to connect to network."));
463 void CallListener::showError(QString const& msg)
465 qDebug() << "Error: " << msg;
466 box_->setTimeout(ERROR_BANNER_TIMEOUT);
470 void CallListener::timerEvent(QTimerEvent* event)
477 void CallListener::sleep(int ms)
484 timer_ = startTimer(ms);
488 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);