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)
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"));
71 qDebug() << "Starting...";
75 void CallListener::end()
77 systemBus_.disconnect(CALL_SERVICE_NAME,
79 CALL_SERVICE_INTERFACE,
82 SLOT(incomingCall(QDBusObjectPath, QString)));
84 systemBus_.disconnect(CALL_SERVICE_NAME,
86 CALL_SERVICE_INTERFACE,
89 SLOT(callTerminate()));
95 void CallListener::search(Eniro::SearchDetails const& details)
97 qDebug() << "Search called";
101 Eniro::Result result;
103 if(Cache::instance().findItem(details.query, result))
106 qDebug() << "Found from cache";
108 showDelayedResult(createResult(result.name,
110 result.city), BANNER_DELAY);
115 currentSearch_ = details.query;
117 if(!connectionManager_->isConnected())
119 connectionManager_->connect();
120 closeConnection_ = true;
124 closeConnection_ = false;
127 showDelayedResult(tr("Searching..."), BANNER_DELAY);
129 qDebug() << "Starting to search...";
131 eniro_->search(details);
136 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
137 Eniro::SearchDetails const& details,
140 // If box is not visible, the call must have been terminated already
141 if(!initialized_ || !box_->isVisible())
150 qDebug() << "Error: " << eniro_->errorString();
152 if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
155 eniro_->search(Eniro::SearchDetails(currentSearch_));
161 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
169 if(results.size() == 0)
171 message = tr("Phone number was not found");
176 message = createResult(results.at(0).name, results.at(0).street,
179 Eniro::Result result = results.at(0);
180 result.number = details.query;
181 Cache::instance().addItem(result);
188 if(closeConnection_ && connectionManager_)
190 connectionManager_->disconnect(true);
191 closeConnection_ = false;
195 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
197 QString result = "<b>" + name + "</b>";
199 if(!street.isEmpty() || !city.isEmpty())
203 if(!street.isEmpty())
205 result += street + ", ";
214 void CallListener::showResult(QString const& text)
221 label_->setText("<font color='black'>" + text + "</font>");
223 if(box_->isVisible())
231 void CallListener::incomingCall(QDBusObjectPath path, QString number)
233 qDebug() << "Incoming: " << number;
237 if(!cm.numberExists(number))
239 qDebug() << "Number doesn't exist";
241 systemBus_.connect(CALL_SERVICE_NAME,
243 CALL_SERVICE_INSTANCE_NAME,
244 CALL_SIGNAL_TERMINATED,
246 SLOT(callTerminate()));
248 search(Eniro::SearchDetails(number));
252 qDebug() << "Number exists";
256 void CallListener::callTerminate()
258 if(initialized_ && box_ && box_->isVisible())
263 if(closeConnection_ && connectionManager_)
265 connectionManager_->disconnect(true);
266 closeConnection_ = false;
272 void CallListener::showDelayedResult(QString const& text, int delay)
274 timedMessage_ = text;
275 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
278 void CallListener::showTimedMessage()
280 if(timedMessage_.size() == 0 || !initialized_)
285 showResult(timedMessage_);
290 void CallListener::searchInit()
292 qDebug() << "Initializing search...";
294 connectionManager_ = new ConnectionManager;
296 eniro_ = new Eniro(site_);
297 eniro_->setMaxResults(1);
298 eniro_->setFindNumber(false);
299 eniro_->setTimeout(REQUEST_TIMEOUT);
301 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
302 Eniro::SearchDetails const&, bool)),
303 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
304 Eniro::SearchDetails const&, bool)));
305 box_ = new InformationBox;
306 label_ = new QLabel("", box_);
307 label_->setMargin(8);
308 box_->setWidget(label_);
312 void CallListener::searchClose()
314 initialized_ = false;
316 qDebug() << "Closing search...";
320 disconnect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
321 Eniro::SearchDetails const&, bool)),
322 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
323 Eniro::SearchDetails const&, bool)));
326 delete connectionManager_;
327 connectionManager_ = 0;