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);
132 showResult(tr("Searching..."));
134 currentSearch_ = details.query;
136 if(connectionManager_)
138 delete connectionManager_;
139 connectionManager_ = 0;
142 connectionManager_ = new ConnectionManager;
144 if(!connectionManager_->isConnected())
146 connectionManager_->connect();
147 closeConnection_ = true;
151 closeConnection_ = false;
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_));
183 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
186 else if(results.size() == 0)
188 message = tr("Phone number was not found");
192 message = createResult(results.at(0).name, results.at(0).street, results.at(0).city);
193 Eniro::Result result = results.at(0);
194 result.number = details.query;
195 Cache::instance().addItem(result);
203 connectionManager_->disconnect();
204 closeConnection_ = false;
207 delete connectionManager_;
208 connectionManager_ = 0;
214 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
216 QString result = "<b>" + name + "</b>";
218 if(!street.isEmpty() || !city.isEmpty())
222 if(!street.isEmpty())
224 result += street + ", ";
233 void CallListener::showResult(QString const& text)
235 label_->setText("<font color='black'>" + text + "</font>");
237 if(box_->isVisible())
245 void CallListener::incomingCall(QDBusObjectPath path, QString number)
247 qDebug() << "Incoming: " << number;
249 if(!contactManager_->numberExists(number))
251 qDebug() << "Number doesn't exist";
253 systemBus_.connect(CALL_SERVICE_NAME,
255 CALL_SERVICE_INSTANCE_NAME,
256 CALL_SIGNAL_TERMINATED,
258 SLOT(callTerminate()));
260 qDebug() << "Going to search";
262 search(Eniro::SearchDetails(number));
266 qDebug() << "Number exists";
270 void CallListener::callTerminate()
272 if(box_->isVisible())
277 if(closeConnection_ && connectionManager_)
279 connectionManager_->disconnect();
280 closeConnection_ = false;
283 delete connectionManager_;
284 connectionManager_ = 0;
287 void CallListener::showDelayedResult(QString const& text, int delay)
289 timedMessage_ = text;
290 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
293 void CallListener::showTimedMessage()
295 if(timedMessage_.size() == 0)
300 showResult(timedMessage_);