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), box_(0), label_(0), retries_(-1)
44 CallListener::~CallListener()
49 void CallListener::begin()
51 systemBus_.connect(CALL_SERVICE_NAME,
53 CALL_SERVICE_INTERFACE,
56 SLOT(incomingCall(QDBusObjectPath, QString)));
58 systemBus_.connect(CALL_SERVICE_NAME,
60 CALL_SERVICE_INTERFACE,
63 SLOT(callTerminate()));
65 contactManager_ = new ContactManager;
67 eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
69 eniro_->setMaxResults(1);
70 eniro_->setFindNumber(false);
71 eniro_->setTimeout(REQUEST_TIMEOUT);
73 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
74 Eniro::SearchDetails const&, bool)),
75 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
76 Eniro::SearchDetails const&, bool)));
78 box_ = new InformationBox();
79 label_ = new QLabel("", box_);
81 box_->setWidget(label_);
83 qDebug() << "Starting...";
87 void CallListener::end()
89 systemBus_.disconnect(CALL_SERVICE_NAME,
91 CALL_SERVICE_INTERFACE,
94 SLOT(incomingCall(QDBusObjectPath, QString)));
96 systemBus_.disconnect(CALL_SERVICE_NAME,
98 CALL_SERVICE_INTERFACE,
101 SLOT(callTerminate()));
111 void CallListener::search(Eniro::SearchDetails const& details)
113 qDebug() << "Search called";
115 Eniro::Result result;
117 if(Cache::instance().findItem(details.query, result))
120 showDelayedResult(createResult(result.name,
122 result.city), BANNER_DELAY);
126 showResult(tr("Searching..."));
128 currentSearch_ = details;
129 eniro_->search(details);
134 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
135 Eniro::SearchDetails const& details,
138 qDebug() << "Found: " << results.size();
140 // If box is not visible, the call must have been terminated already
141 if(!box_->isVisible())
150 qDebug() << "Error: " << eniro_->errorString();
152 if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
155 eniro_->search(currentSearch_);
160 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
163 else if(results.size() == 0)
165 message = tr("Phone number was not found");
169 message = createResult(results.at(0).name, results.at(0).street, results.at(0).city);
170 Eniro::Result result = results.at(0);
171 result.number = details.query;
172 Cache::instance().addItem(result);
177 timedMessage_ = message;
179 // Show banner after small delay
180 showDelayedResult(message, BANNER_DELAY);
184 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
186 QString result = "<b>" + name + "</b>";
188 if(!street.isEmpty() || !city.isEmpty())
192 if(!street.isEmpty())
194 result += street + ", ";
203 void CallListener::showResult(QString const& text)
205 label_->setText("<font color='black'>" + text + "</font>");
207 if(box_->isVisible())
214 void CallListener::incomingCall(QDBusObjectPath path, QString number)
216 qDebug() << "Incoming: " << number;
218 if(!contactManager_->numberExists(number))
220 qDebug() << "Number doesn't exist";
222 systemBus_.connect(CALL_SERVICE_NAME,
224 CALL_SERVICE_INSTANCE_NAME,
225 CALL_SIGNAL_TERMINATED,
227 SLOT(callTerminate()));
229 qDebug() << "Going to search";
231 search(Eniro::SearchDetails(number));
235 qDebug() << "Number exists";
239 void CallListener::callTerminate()
241 if(box_->isVisible())
247 void CallListener::showDelayedResult(QString const& text, int delay)
249 timedMessage_ = text;
250 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
253 void CallListener::showTimedMessage()
255 if(timedMessage_.size() == 0)
260 showResult(timedMessage_);