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"
29 const QString CALL_SERVICE_NAME = "com.nokia.csd";
30 const QString CALL_SERVICE_PATH = "/com/nokia/csd/call";
31 const QString CALL_SERVICE_INTERFACE = "com.nokia.csd.Call";
32 const QString CALL_SERVICE_INSTANCE_NAME = "com.nokia.csd.Call.Instance";
33 const QString CALL_SIGNAL_INCOMING = "Coming";
34 const QString CALL_SIGNAL_RELEASE = "Release";
35 const QString CALL_SIGNAL_TERMINATED = "Terminated";
38 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
40 CallListener::CallListener(): eniro_(0), contactManager_(0),
41 connectionManager_(0), closeConnection_(false), box_(0), label_(0),
46 CallListener::~CallListener()
51 void CallListener::begin()
53 systemBus_.connect(CALL_SERVICE_NAME,
55 CALL_SERVICE_INTERFACE,
58 SLOT(incomingCall(QDBusObjectPath, QString)));
60 systemBus_.connect(CALL_SERVICE_NAME,
62 CALL_SERVICE_INTERFACE,
65 SLOT(callTerminate()));
67 contactManager_ = new ContactManager;
69 eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
71 eniro_->setMaxResults(1);
72 eniro_->setFindNumber(false);
73 eniro_->setTimeout(REQUEST_TIMEOUT);
75 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
76 Eniro::SearchDetails const&, bool)),
77 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
78 Eniro::SearchDetails const&, bool)));
80 box_ = new InformationBox();
81 label_ = new QLabel("", box_);
83 box_->setWidget(label_);
85 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()));
105 delete contactManager_;
107 delete connectionManager_;
108 connectionManager_ = 0;
117 void CallListener::search(Eniro::SearchDetails const& details)
119 qDebug() << "Search called";
121 Eniro::Result result;
123 if(Cache::instance().findItem(details.query, result))
126 showDelayedResult(createResult(result.name,
128 result.city), BANNER_DELAY);
133 currentSearch_ = details.query;
135 if(connectionManager_)
137 delete connectionManager_;
138 connectionManager_ = 0;
141 connectionManager_ = new ConnectionManager;
143 if(!connectionManager_->isConnected())
145 connectionManager_->connect();
146 closeConnection_ = true;
150 closeConnection_ = false;
153 showDelayedResult(tr("Searching..."), BANNER_DELAY);
154 eniro_->search(details);
159 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
160 Eniro::SearchDetails const& details,
163 // If box is not visible, the call must have been terminated already
164 if(!box_->isVisible())
173 qDebug() << "Error: " << eniro_->errorString();
175 if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
178 eniro_->search(Eniro::SearchDetails(currentSearch_));
184 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
192 if(results.size() == 0)
194 message = tr("Phone number was not found");
199 message = createResult(results.at(0).name, results.at(0).street,
202 Eniro::Result result = results.at(0);
203 result.number = details.query;
204 Cache::instance().addItem(result);
211 if(closeConnection_ && connectionManager_)
213 connectionManager_->disconnect(true);
214 closeConnection_ = false;
217 delete connectionManager_;
218 connectionManager_ = 0;
222 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
224 QString result = "<b>" + name + "</b>";
226 if(!street.isEmpty() || !city.isEmpty())
230 if(!street.isEmpty())
232 result += street + ", ";
241 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;
257 if(!contactManager_->numberExists(number))
259 qDebug() << "Number doesn't exist";
261 systemBus_.connect(CALL_SERVICE_NAME,
263 CALL_SERVICE_INSTANCE_NAME,
264 CALL_SIGNAL_TERMINATED,
266 SLOT(callTerminate()));
268 qDebug() << "Going to search";
270 search(Eniro::SearchDetails(number));
274 qDebug() << "Number exists";
278 void CallListener::callTerminate()
280 if(box_->isVisible())
285 if(closeConnection_ && connectionManager_)
287 connectionManager_->disconnect(true);
288 closeConnection_ = false;
291 delete connectionManager_;
292 connectionManager_ = 0;
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)
308 showResult(timedMessage_);