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 <QtSql/QSqlQuery>
22 #include <QtSql/QSqlError>
23 #include "calllistener.h"
26 #include "contactmanager.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), connectionManager_(0),
42 closeConnection_(false), initialized_(false), box_(0), label_(0),
43 retries_(-1), site_(Eniro::FI), autoconnect_(false)
47 CallListener::~CallListener()
52 void CallListener::begin()
54 systemBus_.connect(CALL_SERVICE_NAME,
56 CALL_SERVICE_INTERFACE,
59 SLOT(incomingCall(QDBusObjectPath, QString)));
61 systemBus_.connect(CALL_SERVICE_NAME,
63 CALL_SERVICE_INTERFACE,
66 SLOT(callTerminate()));
68 site_ = Eniro::stringToSite(Settings::instance()->get("site"));
69 connectionName_ = Settings::instance()->get("connection");
70 autoconnect_ = (Settings::instance()->get("autoconnect") == "1");
73 qDebug() << "Starting...";
77 void CallListener::end()
79 systemBus_.disconnect(CALL_SERVICE_NAME,
81 CALL_SERVICE_INTERFACE,
84 SLOT(incomingCall(QDBusObjectPath, QString)));
86 systemBus_.disconnect(CALL_SERVICE_NAME,
88 CALL_SERVICE_INTERFACE,
91 SLOT(callTerminate()));
97 void CallListener::search(Eniro::SearchDetails const& details)
99 qDebug() << "Search called";
103 Eniro::Result result;
105 if(Cache::instance().findItem(details.query, result))
108 qDebug() << "Found from cache";
110 showDelayedResult(createResult(result.name,
112 result.city), BANNER_DELAY);
117 currentSearch_ = details.query;
119 showDelayedResult(tr("Searching..."), BANNER_DELAY);
121 if(!handleConnection())
123 qDebug() << "Unable to connect";
127 qDebug() << "Starting to search...";
129 eniro_->search(details);
134 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
135 Eniro::SearchDetails const& details,
138 // If box is not visible, the call must have been terminated already
139 if(!initialized_ || !box_->isVisible())
148 qDebug() << "Error: " << eniro_->errorString();
150 if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
153 eniro_->search(Eniro::SearchDetails(currentSearch_));
159 showError(tr("Search failed:") + " " + eniro_->errorString() + ".");
166 if(results.size() == 0)
168 message = tr("Phone number was not found");
173 message = createResult(results.at(0).name, results.at(0).street,
176 Eniro::Result result = results.at(0);
177 result.number = details.query;
178 Cache::instance().addItem(result);
185 if(closeConnection_ && connectionManager_)
187 connectionManager_->disconnect(true);
188 closeConnection_ = false;
192 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
194 QString result = "<b>" + name + "</b>";
196 if(!street.isEmpty() || !city.isEmpty())
200 if(!street.isEmpty())
202 result += street + ", ";
211 void CallListener::showResult(QString const& text)
218 label_->setText("<font color='black'>" + text + "</font>");
220 if(box_->isVisible())
228 void CallListener::incomingCall(QDBusObjectPath path, QString number)
230 qDebug() << "Incoming: " << number;
234 if(!cm.numberExists(number))
236 qDebug() << "Number doesn't exist";
238 systemBus_.connect(CALL_SERVICE_NAME,
240 CALL_SERVICE_INSTANCE_NAME,
241 CALL_SIGNAL_TERMINATED,
243 SLOT(callTerminate()));
245 search(Eniro::SearchDetails(number));
249 qDebug() << "Number exists";
253 void CallListener::callTerminate()
255 if(initialized_ && box_ && box_->isVisible())
260 if(closeConnection_ && connectionManager_)
262 connectionManager_->disconnect(true);
263 closeConnection_ = false;
269 void CallListener::showDelayedResult(QString const& text, int delay)
271 timedMessage_ = text;
272 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
275 void CallListener::showTimedMessage()
277 if(timedMessage_.size() == 0 || !initialized_)
282 showResult(timedMessage_);
287 void CallListener::searchInit()
289 qDebug() << "Initializing search...";
293 qDebug() << "Already initialized";
297 connectionManager_ = new ConnectionManager;
299 eniro_ = new Eniro(site_);
300 eniro_->setMaxResults(1);
301 eniro_->setFindNumber(false);
302 eniro_->setTimeout(REQUEST_TIMEOUT);
304 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
305 Eniro::SearchDetails const&, bool)),
306 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
307 Eniro::SearchDetails const&, bool)));
308 box_ = new InformationBox;
309 label_ = new QLabel("", box_);
310 label_->setMargin(8);
311 box_->setWidget(label_);
315 void CallListener::searchClose()
317 initialized_ = false;
319 qDebug() << "Closing search...";
323 disconnect(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)));
329 delete connectionManager_;
330 connectionManager_ = 0;
338 bool CallListener::handleConnection()
340 if(!connectionManager_)
342 qDebug() << "Error: connection manager not initialized";
346 if(connectionManager_->isConnected())
348 closeConnection_ = false;
352 closeConnection_ = true;
359 if(connectionName_.size() != 0 && connectionName_ != "0")
361 conn = connectionName_;
365 ConnectionManager::Connection best;
367 if(!connectionManager_->getBestConnection(best))
369 showError(tr("No network connections found."));
376 while(retries < CONNECT_RETRIES)
378 if(connectionManager_->connect(conn))
382 else if(connectionManager_->error() == ConnectionManager::INVALID_IAP)
384 showError(tr("Selected access point doesn't exist."));
393 while(retries < CONNECT_RETRIES)
395 if(connectionManager_->connect())
401 qDebug() << "Automatic connection failed";
408 if(retries >= CONNECT_RETRIES)
410 ConnectionManager::NetworkMode mode = connectionManager_->getNetworkMode();
412 if(mode != ConnectionManager::NETWORK_3G &&
413 mode != ConnectionManager::NETWORK_3_5G)
415 showError(tr("No 3G or WLAN network available."));
419 showError(tr("Unable to connect to network."));
428 void CallListener::showError(QString const& msg)
430 qDebug() << "Error: " << msg;
431 box_->setTimeout(ERROR_BANNER_TIMEOUT);