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 <QtSql/QSqlQuery>
21 #include <QtSql/QSqlError>
22 #include "calllistener.h"
28 const QString CALL_SERVICE_NAME = "com.nokia.csd";
29 const QString CALL_SERVICE_PATH = "/com/nokia/csd/call";
30 const QString CALL_SERVICE_INTERFACE = "com.nokia.csd.Call";
31 const QString CALL_SERVICE_INSTANCE_NAME = "com.nokia.csd.Call.Instance";
32 const QString CALL_SIGNAL_INCOMING = "Coming";
33 const QString CALL_SIGNAL_RELEASE = "Release";
34 const QString CALL_SIGNAL_TERMINATED = "Terminated";
37 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
39 CallListener::CallListener(): eniro_(0), contactManager_(0), box_(0), label_(0)
43 CallListener::~CallListener()
48 void CallListener::begin()
50 systemBus_.connect(CALL_SERVICE_NAME,
52 CALL_SERVICE_INTERFACE,
55 SLOT(incomingCall(QDBusObjectPath, QString)));
57 systemBus_.connect(CALL_SERVICE_NAME,
59 CALL_SERVICE_INTERFACE,
62 SLOT(callTerminate()));
64 contactManager_ = new ContactManager;
66 eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
68 eniro_->setMaxResults(1);
69 eniro_->setFindNumber(false);
70 eniro_->setTimeout(REQUEST_TIMEOUT);
72 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
73 Eniro::SearchDetails const&, bool)),
74 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
75 Eniro::SearchDetails const&, bool)));
77 box_ = new InformationBox();
78 label_ = new QLabel("", box_);
80 box_->setWidget(label_);
84 void CallListener::end()
86 systemBus_.disconnect(CALL_SERVICE_NAME,
88 CALL_SERVICE_INTERFACE,
91 SLOT(incomingCall(QDBusObjectPath, QString)));
93 systemBus_.disconnect(CALL_SERVICE_NAME,
95 CALL_SERVICE_INTERFACE,
98 SLOT(callTerminate()));
108 void CallListener::search(Eniro::SearchDetails const& details)
110 Eniro::Result result;
112 if(Cache::instance().findItem(details.query, result))
115 showResult(createResult(result.name,
121 showResult(tr("Searching..."));
122 eniro_->search(details);
127 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
128 Eniro::SearchDetails const& details,
131 qDebug() << "Found: " << results.size();
133 // If box is not visible, the call must have been terminated already
134 if(!box_->isVisible())
143 qDebug() << "Error: " << eniro_->errorString();
144 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
146 else if(results.size() == 0)
148 message = tr("Phone number was not found");
152 message = createResult(results.at(0).name, results.at(0).street, results.at(0).city);
153 Eniro::Result result = results.at(0);
154 result.number = details.query;
155 Cache::instance().addItem(result);
162 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
164 QString result = "<b>" + name + "</b>";
166 if(!street.isEmpty() || !city.isEmpty())
170 if(!street.isEmpty())
172 result += street + ", ";
181 void CallListener::showResult(QString const& text)
183 label_->setText("<font color='black'>" + text + "</font>");
185 if(box_->isVisible())
192 void CallListener::incomingCall(QDBusObjectPath path, QString number)
196 if(!contactManager_->numberExists(number))
199 systemBus_.connect(CALL_SERVICE_NAME,
201 CALL_SERVICE_INSTANCE_NAME,
202 CALL_SIGNAL_TERMINATED,
204 SLOT(callTerminate()));
206 search(Eniro::SearchDetails(number));
210 void CallListener::callTerminate()