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;
68 connectionManager_ = new ConnectionManager;
70 eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
72 eniro_->setMaxResults(1);
73 eniro_->setFindNumber(false);
74 eniro_->setTimeout(REQUEST_TIMEOUT);
76 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
77 Eniro::SearchDetails const&, bool)),
78 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
79 Eniro::SearchDetails const&, bool)));
81 box_ = new InformationBox();
82 label_ = new QLabel("", box_);
84 box_->setWidget(label_);
86 qDebug() << "Starting...";
90 void CallListener::end()
92 systemBus_.disconnect(CALL_SERVICE_NAME,
94 CALL_SERVICE_INTERFACE,
97 SLOT(incomingCall(QDBusObjectPath, QString)));
99 systemBus_.disconnect(CALL_SERVICE_NAME,
101 CALL_SERVICE_INTERFACE,
104 SLOT(callTerminate()));
106 delete contactManager_;
108 delete connectionManager_;
109 connectionManager_ = 0;
118 void CallListener::search(Eniro::SearchDetails const& details)
120 qDebug() << "Search called";
122 Eniro::Result result;
124 if(Cache::instance().findItem(details.query, result))
127 showDelayedResult(createResult(result.name,
129 result.city), BANNER_DELAY);
133 showResult(tr("Searching..."));
135 currentSearch_ = details;
137 if(!connectionManager_->isConnected())
139 connectionManager_->connect();
140 closeConnection_ = true;
144 closeConnection_ = false;
147 eniro_->search(details);
152 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
153 Eniro::SearchDetails const& details,
156 qDebug() << "Found: " << results.size();
158 // If box is not visible, the call must have been terminated already
159 if(!box_->isVisible())
168 qDebug() << "Error: " << eniro_->errorString();
170 if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
173 eniro_->search(currentSearch_);
178 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
181 else if(results.size() == 0)
183 message = tr("Phone number was not found");
187 message = createResult(results.at(0).name, results.at(0).street, results.at(0).city);
188 Eniro::Result result = results.at(0);
189 result.number = details.query;
190 Cache::instance().addItem(result);
197 connectionManager_->disconnect();
198 closeConnection_ = false;
201 timedMessage_ = message;
207 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
209 QString result = "<b>" + name + "</b>";
211 if(!street.isEmpty() || !city.isEmpty())
215 if(!street.isEmpty())
217 result += street + ", ";
226 void CallListener::showResult(QString const& text)
228 label_->setText("<font color='black'>" + text + "</font>");
230 if(box_->isVisible())
237 void CallListener::incomingCall(QDBusObjectPath path, QString number)
239 qDebug() << "Incoming: " << number;
241 if(!contactManager_->numberExists(number))
243 qDebug() << "Number doesn't exist";
245 systemBus_.connect(CALL_SERVICE_NAME,
247 CALL_SERVICE_INSTANCE_NAME,
248 CALL_SIGNAL_TERMINATED,
250 SLOT(callTerminate()));
252 qDebug() << "Going to search";
254 search(Eniro::SearchDetails(number));
258 qDebug() << "Number exists";
262 void CallListener::callTerminate()
264 if(box_->isVisible())
271 connectionManager_->disconnect();
272 closeConnection_ = false;
276 void CallListener::showDelayedResult(QString const& text, int delay)
278 timedMessage_ = text;
279 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
282 void CallListener::showTimedMessage()
284 if(timedMessage_.size() == 0)
289 showResult(timedMessage_);