Small fixes to banner delays and daemon's startup parameters.
[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)
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         eniro_->search(details);
128     }
129
130 }
131
132 void CallListener::requestFinished(QVector <Eniro::Result> const& results,
133                                    Eniro::SearchDetails const& details,
134                                    bool error)
135 {
136     qDebug() << "Found: " << results.size();
137
138     // If box is not visible, the call must have been terminated already
139     if(!box_->isVisible())
140     {
141         return;
142     }
143
144     QString message;
145
146     if(error)
147     {
148         qDebug() << "Error: " << eniro_->errorString();
149         message = tr("Search failed:") + " " + eniro_->errorString() + ".";
150     }
151     else if(results.size() == 0)
152     {
153         message = tr("Phone number was not found");
154     }
155     else
156     {
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);
161     }
162
163     timedMessage_ = message;
164
165     // Show banner after small delay
166     showDelayedResult(message, BANNER_DELAY);
167
168 }
169
170 QString CallListener::createResult(QString const& name, QString const& street, QString const& city)
171 {
172     QString result = "<b>" + name + "</b>";
173
174     if(!street.isEmpty() || !city.isEmpty())
175     {
176         result += "<br>";
177
178         if(!street.isEmpty())
179         {
180             result += street + ", ";
181         }
182
183         result += city;
184     }
185
186     return result;
187 }
188
189 void CallListener::showResult(QString const& text)
190 {
191     label_->setText("<font color='black'>" + text + "</font>");
192
193     if(box_->isVisible())
194     {
195         box_->hide();
196     }
197     box_->show();
198 }
199
200 void CallListener::incomingCall(QDBusObjectPath path, QString number)
201 {
202     qDebug() << "Incoming: " << number;
203
204     if(!contactManager_->numberExists(number))
205     {
206         qDebug() << "Number doesn't exist";
207
208         systemBus_.connect(CALL_SERVICE_NAME,
209                            path.path(),
210                            CALL_SERVICE_INSTANCE_NAME,
211                            CALL_SIGNAL_TERMINATED,
212                            this,
213                            SLOT(callTerminate()));
214
215         qDebug() << "Going to search";
216
217         search(Eniro::SearchDetails(number));
218     }
219     else
220     {
221         qDebug() << "Number exists";
222     }
223 }
224
225 void CallListener::callTerminate()
226 {
227         if(box_->isVisible())
228         {
229                 box_->hide();
230         }
231 }
232
233 void CallListener::showDelayedResult(QString const& text, int delay)
234 {
235         timedMessage_ = text;
236         QTimer::singleShot(delay, this, SLOT(showTimedMessage()));
237 }
238
239 void CallListener::showTimedMessage()
240 {
241         if(timedMessage_.size() == 0)
242         {
243                 return;
244         }
245
246         showResult(timedMessage_);
247
248         timedMessage_ = "";
249 }