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 "calllistener.h"
27 const QString CALL_SERVICE_NAME = "com.nokia.csd";
28 const QString CALL_SERVICE_PATH = "/com/nokia/csd/call";
29 const QString CALL_SERVICE_INTERFACE = "com.nokia.csd.Call";
30 const QString CALL_SERVICE_INSTANCE_NAME = "com.nokia.csd.Call.Instance";
31 const QString CALL_SIGNAL_INCOMING = "Coming";
32 const QString CALL_SIGNAL_RELEASE = "Release";
33 const QString CALL_SIGNAL_TERMINATED = "Terminated";
36 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
38 CallListener::CallListener(): eniro_(0), contactManager_(0), box_(0), label_(0)
42 CallListener::~CallListener()
47 void CallListener::begin()
49 systemBus_.connect(CALL_SERVICE_NAME,
51 CALL_SERVICE_INTERFACE,
54 SLOT(incomingCall(QDBusObjectPath, QString)));
56 systemBus_.connect(CALL_SERVICE_NAME,
58 CALL_SERVICE_INTERFACE,
61 SLOT(callTerminate()));
63 contactManager_ = new ContactManager;
65 eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
67 eniro_->setMaxResults(1);
68 eniro_->setFindNumber(false);
70 connect(eniro_, SIGNAL(requestFinished(QVector <Eniro::Result> const&,
71 Eniro::SearchDetails const&, bool)),
72 this, SLOT(requestFinished(QVector <Eniro::Result> const&,
73 Eniro::SearchDetails const&, bool)));
75 box_ = new InformationBox();
76 label_ = new QLabel("", box_);
77 label_->setMargin(10);
78 box_->setWidget(label_);
82 void CallListener::end()
84 systemBus_.disconnect(CALL_SERVICE_NAME,
86 CALL_SERVICE_INTERFACE,
89 SLOT(incomingCall(QDBusObjectPath, QString)));
91 systemBus_.disconnect(CALL_SERVICE_NAME,
93 CALL_SERVICE_INTERFACE,
96 SLOT(callTerminate()));
106 void CallListener::search(Eniro::SearchDetails const& details)
108 label_->setText(tr("Searching..."));
114 query.prepare("SELECT name, street, city FROM cache WHERE number = :number");
115 query.bindValue(":number", details.query);
117 if(query.exec() && query.next())
119 showResult(createResult(query.value(0).toString(),
120 query.value(1).toString(),
121 query.value(2).toString()));
126 eniro_->search(details);
133 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
134 Eniro::SearchDetails const& details,
137 qDebug() << "Found: " << results.size();
139 // If box is not visible, the call must have been terminated already
140 if(!box_->isVisible())
149 qDebug() << "Error: " << eniro_->errorString();
150 message = tr("Search failed:") + " " + eniro_->errorString();
152 else if(results.size() == 0)
154 message = tr("Phone number was not found");
158 message = createResult(results.at(0).name, results.at(0).street, results.at(0).city);
163 query.prepare("INSERT INTO cache(number, name, street, city) VALUES(:number, :name, :street, :city)");
164 query.bindValue(":number", details.query);
165 query.bindValue(":name", results.at(0).name);
166 query.bindValue(":street", results.at(0).street);
167 query.bindValue(":city", results.at(0).city);
171 qDebug() << "Unable to save cache";
174 QString cacheSize = Settings::instance()->get("cache_size");
176 // Delete old entries from cache
177 if(cacheSize.toInt() > 0)
179 if(!query.exec("DELETE c1 FROM cache AS c1 LEFT JOIN (SELECT id FROM cache ORDER BY id DESC LIMIT " + cacheSize + ") AS c2 ON c1.id = c2.id WHERE c2.id IS NULL"))
181 qDebug() << "Unable to delete old cache entries";
192 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
194 QString result = "<b>" + name + "</b>";
196 if(!street.isEmpty() || !city.isEmpty())
200 if(!street.isEmpty())
202 result += street + ", ";
211 void CallListener::showResult(QString const& text)
213 label_->setText(text);
218 void CallListener::incomingCall(QDBusObjectPath path, QString number)
222 if(!contactManager_->numberExists(number))
225 systemBus_.connect(CALL_SERVICE_NAME,
227 CALL_SERVICE_INSTANCE_NAME,
228 CALL_SIGNAL_TERMINATED,
230 SLOT(callTerminate()));
232 search(Eniro::SearchDetails(number));
236 void CallListener::callTerminate()