1237b7c1bc2716f3408b5545ad13a98f634d86a9
[jenirok] / src / daemon / calllistener.cpp
1 /*
2  * This file is part of Jenirok.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  */
18
19 #include <QtCore/QDebug>
20 #include <QtCore/QTimer>
21 #include <QtSql/QSqlQuery>
22 #include <QtSql/QSqlError>
23 #include "calllistener.h"
24 #include "settings.h"
25 #include "cache.h"
26
27 namespace
28 {
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";
36 }
37
38 QDBusConnection CallListener::systemBus_ = QDBusConnection::systemBus();
39
40 CallListener::CallListener(): eniro_(0), contactManager_(0),
41 connectionManager_(0), closeConnection_(false), box_(0), label_(0),
42 retries_(-1)
43 {
44 }
45
46 CallListener::~CallListener()
47 {
48     end();
49 }
50
51 void CallListener::begin()
52 {
53     systemBus_.connect(CALL_SERVICE_NAME,
54                        CALL_SERVICE_PATH,
55                        CALL_SERVICE_INTERFACE,
56                        CALL_SIGNAL_INCOMING,
57                        this,
58                        SLOT(incomingCall(QDBusObjectPath, QString)));
59
60     systemBus_.connect(CALL_SERVICE_NAME,
61                        CALL_SERVICE_PATH,
62                        CALL_SERVICE_INTERFACE,
63                        CALL_SIGNAL_RELEASE,
64                        this,
65                        SLOT(callTerminate()));
66
67     contactManager_ = new ContactManager;
68     connectionManager_ = new ConnectionManager;
69
70     eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
71
72     eniro_->setMaxResults(1);
73     eniro_->setFindNumber(false);
74     eniro_->setTimeout(REQUEST_TIMEOUT);
75
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)));
80
81     box_ = new InformationBox();
82     label_ = new QLabel("", box_);
83     label_->setMargin(8);
84     box_->setWidget(label_);
85
86     qDebug() << "Starting...";
87
88 }
89
90 void CallListener::end()
91 {
92     systemBus_.disconnect(CALL_SERVICE_NAME,
93                           CALL_SERVICE_PATH,
94                           CALL_SERVICE_INTERFACE,
95                           CALL_SIGNAL_INCOMING,
96                           this,
97                           SLOT(incomingCall(QDBusObjectPath, QString)));
98
99     systemBus_.disconnect(CALL_SERVICE_NAME,
100                           CALL_SERVICE_PATH,
101                           CALL_SERVICE_INTERFACE,
102                           CALL_SIGNAL_RELEASE,
103                           this,
104                           SLOT(callTerminate()));
105
106     delete contactManager_;
107     contactManager_ = 0;
108     delete connectionManager_;
109     connectionManager_ = 0;
110     delete eniro_;
111     eniro_ = 0;
112     delete box_;
113     box_ = 0;
114     delete label_;
115     label_ = 0;
116 }
117
118 void CallListener::search(Eniro::SearchDetails const& details)
119 {
120     qDebug() << "Search called";
121
122     Eniro::Result result;
123
124     if(Cache::instance().findItem(details.query, result))
125     {
126
127         showDelayedResult(createResult(result.name,
128                                   result.street,
129                                   result.city), BANNER_DELAY);
130     }
131     else
132     {
133         showResult(tr("Searching..."));
134         retries_ = 0;
135         currentSearch_ = details;
136
137         if(!connectionManager_->isConnected())
138         {
139             connectionManager_->connect();
140             closeConnection_ = true;
141         }
142         else
143         {
144             closeConnection_ = false;
145         }
146
147         eniro_->search(details);
148     }
149
150 }
151
152 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
153                                    Eniro::SearchDetails const& details,
154                                    bool error)
155 {
156     qDebug() << "Found: " << results.size();
157
158     // If box is not visible, the call must have been terminated already
159     if(!box_->isVisible())
160     {
161         return;
162     }
163
164     QString message;
165
166     if(error)
167     {
168         qDebug() << "Error: " << eniro_->errorString();
169
170         if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
171         {
172             retries_++;
173             eniro_->search(currentSearch_);
174             return;
175         }
176         else
177         {
178             message = tr("Search failed:") + " " + eniro_->errorString() + ".";
179         }
180     }
181     else if(results.size() == 0)
182     {
183         message = tr("Phone number was not found");
184     }
185     else
186     {
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);
191     }
192
193     retries_ = -1;
194
195     if(closeConnection_)
196     {
197         connectionManager_->disconnect();
198         closeConnection_ = false;
199     }
200
201     timedMessage_ = message;
202
203     showResult(message);
204
205 }
206
207 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
208 {
209     QString result = "<b>" + name + "</b>";
210
211     if(!street.isEmpty() || !city.isEmpty())
212     {
213         result += "<br>";
214
215         if(!street.isEmpty())
216         {
217             result += street + ", ";
218         }
219
220         result += city;
221     }
222
223     return result;
224 }
225
226 void CallListener::showResult(QString const& text)
227 {
228     label_->setText("<font color='black'>" + text + "</font>");
229
230     if(box_->isVisible())
231     {
232         box_->hide();
233     }
234     box_->show();
235 }
236
237 void CallListener::incomingCall(QDBusObjectPath path, QString number)
238 {
239     qDebug() << "Incoming: " << number;
240
241     if(!contactManager_->numberExists(number))
242     {
243         qDebug() << "Number doesn't exist";
244
245         systemBus_.connect(CALL_SERVICE_NAME,
246                            path.path(),
247                            CALL_SERVICE_INSTANCE_NAME,
248                            CALL_SIGNAL_TERMINATED,
249                            this,
250                            SLOT(callTerminate()));
251
252         qDebug() << "Going to search";
253
254         search(Eniro::SearchDetails(number));
255     }
256     else
257     {
258         qDebug() << "Number exists";
259     }
260 }
261
262 void CallListener::callTerminate()
263 {
264     if(box_->isVisible())
265     {
266         box_->hide();
267     }
268
269     if(closeConnection_)
270     {
271         connectionManager_->disconnect();
272         closeConnection_ = false;
273     }
274 }
275
276 void CallListener::showDelayedResult(QString const& text, int delay)
277 {
278     timedMessage_ = text;
279     QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
280 }
281
282 void CallListener::showTimedMessage()
283 {
284     if(timedMessage_.size() == 0)
285     {
286         return;
287     }
288
289     showResult(timedMessage_);
290
291     timedMessage_ = "";
292 }