Added automatic retries to call listener.
[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), box_(0), label_(0), retries_(-1)
41 {
42 }
43
44 CallListener::~CallListener()
45 {
46     end();
47 }
48
49 void CallListener::begin()
50 {
51     systemBus_.connect(CALL_SERVICE_NAME,
52                        CALL_SERVICE_PATH,
53                        CALL_SERVICE_INTERFACE,
54                        CALL_SIGNAL_INCOMING,
55                        this,
56                        SLOT(incomingCall(QDBusObjectPath, QString)));
57
58     systemBus_.connect(CALL_SERVICE_NAME,
59                        CALL_SERVICE_PATH,
60                        CALL_SERVICE_INTERFACE,
61                        CALL_SIGNAL_RELEASE,
62                        this,
63                        SLOT(callTerminate()));
64
65     contactManager_ = new ContactManager;
66
67     eniro_ = new Eniro(Eniro::stringToSite(Settings::instance()->get("site")));
68
69     eniro_->setMaxResults(1);
70     eniro_->setFindNumber(false);
71     eniro_->setTimeout(REQUEST_TIMEOUT);
72
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)));
77
78     box_ = new InformationBox();
79     label_ = new QLabel("", box_);
80     label_->setMargin(8);
81     box_->setWidget(label_);
82
83     qDebug() << "Starting...";
84
85 }
86
87 void CallListener::end()
88 {
89     systemBus_.disconnect(CALL_SERVICE_NAME,
90                           CALL_SERVICE_PATH,
91                           CALL_SERVICE_INTERFACE,
92                           CALL_SIGNAL_INCOMING,
93                           this,
94                           SLOT(incomingCall(QDBusObjectPath, QString)));
95
96     systemBus_.disconnect(CALL_SERVICE_NAME,
97                           CALL_SERVICE_PATH,
98                           CALL_SERVICE_INTERFACE,
99                           CALL_SIGNAL_RELEASE,
100                           this,
101                           SLOT(callTerminate()));
102
103     delete eniro_;
104     eniro_ = 0;
105     delete box_;
106     box_ = 0;
107     delete label_;
108     label_ = 0;
109 }
110
111 void CallListener::search(Eniro::SearchDetails const& details)
112 {
113         qDebug() << "Search called";
114
115     Eniro::Result result;
116
117     if(Cache::instance().findItem(details.query, result))
118     {
119
120         showDelayedResult(createResult(result.name,
121                                   result.street,
122                                   result.city), BANNER_DELAY);
123     }
124     else
125     {
126         showResult(tr("Searching..."));
127         retries_ = 0;
128         currentSearch_ = details;
129         eniro_->search(details);
130     }
131
132 }
133
134 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
135                                    Eniro::SearchDetails const& details,
136                                    bool error)
137 {
138     qDebug() << "Found: " << results.size();
139
140     // If box is not visible, the call must have been terminated already
141     if(!box_->isVisible())
142     {
143         return;
144     }
145
146     QString message;
147
148     if(error)
149     {
150         qDebug() << "Error: " << eniro_->errorString();
151
152         if(retries_ < NUMBER_OF_RETRIES && retries_ >= 0)
153         {
154                 retries_++;
155                 eniro_->search(currentSearch_);
156                 return;
157         }
158         else
159         {
160                 message = tr("Search failed:") + " " + eniro_->errorString() + ".";
161         }
162     }
163     else if(results.size() == 0)
164     {
165         message = tr("Phone number was not found");
166     }
167     else
168     {
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);
173     }
174
175     retries_ = -1;
176
177     timedMessage_ = message;
178
179     // Show banner after small delay
180     showDelayedResult(message, BANNER_DELAY);
181
182 }
183
184 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
185 {
186     QString result = "<b>" + name + "</b>";
187
188     if(!street.isEmpty() || !city.isEmpty())
189     {
190         result += "<br>";
191
192         if(!street.isEmpty())
193         {
194             result += street + ", ";
195         }
196
197         result += city;
198     }
199
200     return result;
201 }
202
203 void CallListener::showResult(QString const& text)
204 {
205     label_->setText("<font color='black'>" + text + "</font>");
206
207     if(box_->isVisible())
208     {
209         box_->hide();
210     }
211     box_->show();
212 }
213
214 void CallListener::incomingCall(QDBusObjectPath path, QString number)
215 {
216     qDebug() << "Incoming: " << number;
217
218     if(!contactManager_->numberExists(number))
219     {
220         qDebug() << "Number doesn't exist";
221
222         systemBus_.connect(CALL_SERVICE_NAME,
223                            path.path(),
224                            CALL_SERVICE_INSTANCE_NAME,
225                            CALL_SIGNAL_TERMINATED,
226                            this,
227                            SLOT(callTerminate()));
228
229         qDebug() << "Going to search";
230
231         search(Eniro::SearchDetails(number));
232     }
233     else
234     {
235         qDebug() << "Number exists";
236     }
237 }
238
239 void CallListener::callTerminate()
240 {
241         if(box_->isVisible())
242         {
243                 box_->hide();
244         }
245 }
246
247 void CallListener::showDelayedResult(QString const& text, int delay)
248 {
249         timedMessage_ = text;
250         QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
251 }
252
253 void CallListener::showTimedMessage()
254 {
255         if(timedMessage_.size() == 0)
256         {
257                 return;
258         }
259
260         showResult(timedMessage_);
261
262         timedMessage_ = "";
263 }