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)
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..."));
127 eniro_->search(details);
132 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
133 Eniro::SearchDetails const& details,
136 qDebug() << "Found: " << results.size();
138 // If box is not visible, the call must have been terminated already
139 if(!box_->isVisible())
148 qDebug() << "Error: " << eniro_->errorString();
149 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
151 else if(results.size() == 0)
153 message = tr("Phone number was not found");
157 message = createResult(results.at(0).name, results.at(0).street, results.at(0).city);
158 Eniro::Result result = results.at(0);
159 result.number = details.query;
160 Cache::instance().addItem(result);
163 timedMessage_ = message;
165 // Show banner after small delay
166 showDelayedResult(message, BANNER_DELAY);
170 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
172 QString result = "<b>" + name + "</b>";
174 if(!street.isEmpty() || !city.isEmpty())
178 if(!street.isEmpty())
180 result += street + ", ";
189 void CallListener::showResult(QString const& text)
191 label_->setText("<font color='black'>" + text + "</font>");
193 if(box_->isVisible())
200 void CallListener::incomingCall(QDBusObjectPath path, QString number)
202 qDebug() << "Incoming: " << number;
204 if(!contactManager_->numberExists(number))
206 qDebug() << "Number doesn't exist";
208 systemBus_.connect(CALL_SERVICE_NAME,
210 CALL_SERVICE_INSTANCE_NAME,
211 CALL_SIGNAL_TERMINATED,
213 SLOT(callTerminate()));
215 qDebug() << "Going to search";
217 search(Eniro::SearchDetails(number));
221 qDebug() << "Number exists";
225 void CallListener::callTerminate()
227 if(box_->isVisible())
233 void CallListener::showDelayedResult(QString const& text, int delay)
235 timedMessage_ = text;
236 QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
239 void CallListener::showTimedMessage()
241 if(timedMessage_.size() == 0)
246 showResult(timedMessage_);