Added call log and German translation.
[jenirok] / src / common / dastelefonbuch.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 "dastelefonbuch.h"
21
22
23 DasTelefonbuch::DasTelefonbuch(QObject* parent): Source(parent)
24 {
25 }
26
27 DasTelefonbuch::~DasTelefonbuch()
28 {
29     abort();
30 }
31
32 void DasTelefonbuch::abort()
33 {
34     Source::abort();
35
36     for(int i = 0; i < pendingSearches_.size(); i++)
37     {
38         delete pendingSearches_[i];
39         pendingSearches_[i] = 0;
40     }
41
42     pendingSearches_.clear();
43 }
44
45 void DasTelefonbuch::search(Source::SearchDetails const& details)
46 {
47     resetTimeout();
48
49     SearchData* newData = new SearchData;
50     newData->details = details;
51     newData->currentPage = 1;
52     newData->finishedSearches = 0;
53
54     if(details.type == Source::BOTH)
55     {
56         newData->totalSearches = 2;
57         Source::SearchDetails tmpDetails = details;
58         tmpDetails.type = Source::PERSONS;
59         int id1 = sendQuery(tmpDetails, 1);
60         tmpDetails.type = Source::YELLOW_PAGES;
61         int id2 = sendQuery(tmpDetails, 1);
62         newData->searchIds.insert(id1);
63         newData->searchIds.insert(id2);
64     }
65     else
66     {
67         newData->totalSearches = 1;
68         int id = sendQuery(details, 1);
69         newData->searchIds.insert(id);
70     }
71
72     pendingSearches_.push_back(newData);
73 }
74
75 void DasTelefonbuch::handleHttpData(int id, QByteArray const& data)
76 {
77     QString strData(data);
78
79     for(int i = 0; i < pendingSearches_.size(); i++)
80     {
81         if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
82             pendingSearches_.at(i)->searchIds.end())
83         {
84             addNumbers(pendingSearches_.at(i), strData, i);
85             break;
86         }
87     }
88 }
89
90 void DasTelefonbuch::handleHttpError(int id)
91 {
92     for(int i = 0; i < pendingSearches_.size(); i++)
93     {
94         if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
95             pendingSearches_.at(i)->searchIds.end())
96         {
97
98             setError(Source::CONNECTION_FAILURE, http_.errorString());
99             emitRequestFinished(pendingSearches_.at(i), true, i);
100             break;
101         }
102     }
103 }
104
105 void DasTelefonbuch::addNumbers(SearchData* searchData,
106                            QString const& data,
107                            int index)
108 {
109     if(data.indexOf("<span>1&nbsp;Gesamttreffer</span>") > 0)
110     {
111         addOnlyNumber(searchData, data, index);
112         return;
113     }
114
115     int pos = 0;
116     QRegExp rx("<table.*class=\"bg-01 entry(.*)</table>");
117     QRegExp name("<div class=\"long hide\">(.*)</a>");
118     QRegExp number("<td class=\"col4\">(.*)</td>");
119     QRegExp address("<td class=\"col2\">(.*)</td>.*<td class=\"col3\">(.*)</td>");
120
121     rx.setMinimal(true);
122     name.setMinimal(true);
123     address.setMinimal(true);
124     number.setMinimal(true);
125
126     int maxResults = getMaxResults();
127
128     while((pos = rx.indexIn(data, pos)) != -1)
129     {
130         pos += rx.matchedLength();
131
132         if(searchData->results.size() >= maxResults)
133         {
134             break;
135         }
136
137         QString part = rx.cap(1);
138         Source::Result result;
139         QString nameStr;
140         QString numberStr;
141         QString streetStr;
142         QString cityStr;
143
144         if(name.indexIn(part) != -1)
145         {
146             nameStr = name.cap(1);
147         }
148
149         if(address.indexIn(part) != -1)
150         {
151             streetStr = address.cap(1);
152             cityStr = address.cap(2);
153         }
154
155         if(number.indexIn(part) != -1)
156         {
157             numberStr = number.cap(1);
158         }
159
160         if(formatResult(nameStr, numberStr, streetStr,
161                         cityStr, result))
162         {
163             emit resultAvailable(result, searchData->details);
164             searchData->results.push_back(result);
165         }
166
167     }
168
169     searchData->finishedSearches++;
170
171     if(searchData->results.size() >= maxResults)
172     {
173         emitRequestFinished(searchData, false, index);
174     }
175     else
176     {
177         /* TODO: Paging not implemented yet
178         if(data.indexOf("Neste") > 0)
179         {
180             searchData->currentPage++;
181             int id = sendQuery(searchData->details, searchData->currentPage);
182             searchData->searchIds.insert(id);
183         }
184         */
185         if (false)
186         {
187         }
188         else if(searchData->finishedSearches >= searchData->totalSearches)
189         {
190             emitRequestFinished(searchData, false, index);
191         }
192     }
193
194 }
195
196 void DasTelefonbuch::addOnlyNumber(SearchData* searchData,
197                               QString const& data,
198                               int index)
199 {
200     QRegExp name("<div class=\"long hide\">(.*) (.*)</a>");
201     QRegExp number("<td class=\"col4\">(.*)</td>");
202     QRegExp address("<td class=\"col2\">(.*)</td>.*<td class=\"col3\">(.*)</td>");
203     name.setMinimal(true);
204     number.setMinimal(true);
205     address.setMinimal(true);
206
207     Source::Result result;
208
209     QString nameStr;
210     QString numberStr;
211     QString streetStr;
212     QString cityStr;
213
214     if(name.indexIn(data) != -1)
215     {
216         nameStr = name.cap(2).simplified() + name.cap(1).simplified();
217         nameStr.replace("\n","");
218     }
219
220     if(number.indexIn(data) != -1)
221     {
222         numberStr = number.cap(1);
223     }
224
225     if(address.indexIn(data) != -1)
226     {
227         streetStr = address.cap(1);
228         cityStr = address.cap(2);
229     }
230
231     if(formatResult(nameStr, numberStr, streetStr,
232                     cityStr, result))
233     {
234         searchData->results.push_back(result);
235         emit resultAvailable(result, searchData->details);
236     }
237
238     emitRequestFinished(searchData, false, index);
239 }
240
241 bool DasTelefonbuch::formatResult(QString& name, QString& number,
242                              QString& street, QString& city,
243                              Source::Result& result)
244 {
245     name = stripTags(name);
246     name = htmlEntityDecode(name);
247     result.name = name.trimmed();
248     number = stripTags(number);
249     number = cleanUpNumber(number);
250     result.number = number.trimmed();
251     street = stripTags(street);
252     street = htmlEntityDecode(street);
253     city = stripTags(city);
254     city = htmlEntityDecode(city);
255     result.street = street.trimmed();
256     result.city = city.trimmed();
257
258     if(!result.name.isEmpty() && (!getFindNumber() || !result.number.isEmpty()))
259     {
260         return true;
261     }
262     return false;
263 }
264
265 void DasTelefonbuch::emitRequestFinished(SearchData* data,
266                                     bool error, int index)
267 {
268     QVector<Source::Result> results = data->results;
269     Source::SearchDetails details = data->details;
270
271     emit requestFinished(results, details, error);
272     delete pendingSearches_[index];
273     pendingSearches_[index] = 0;
274     pendingSearches_.removeAt(index);
275 }
276
277 int DasTelefonbuch::sendQuery(Source::SearchDetails const& details,
278                          int page)
279 {
280     Q_UNUSED(page);
281
282     QUrl url("http://www.dastelefonbuch.de/");
283
284     //Pretending we are a Firefox-Plugin allows a simpler query-String
285     url.addQueryItem("sourceid","Mozilla-search");
286     //But we will need to give a proper User-Agent-String...see below
287
288     url.addQueryItem("cmd","search");
289
290     QString query = details.query;
291
292     if(!details.location.isEmpty())
293     {
294         query += "+" + details.location;
295     }
296
297     //Query is added as "kw"
298     url.addQueryItem("kw", query);
299
300     /* TODO No differentiation between personal and professional entries yet
301     if(details.type == Source::YELLOW_PAGES)
302     {
303         url.addQueryItem("t", "c");
304     }
305     else
306     {
307         url.addQueryItem("t", "p");
308     }
309    */
310
311     /* TODO No multi-page results yet.
312     if(page > 1)
313     {
314         url.addQueryItem("p", QString::number(page));
315     }
316     */
317
318     fixUrl(url);
319
320     //Remember, we are firefox, therefore setting User-Agent here...
321     QHttpRequestHeader header("GET", url.encodedPath()+ '?' + url.encodedQuery());
322
323     header.setValue("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10");
324     header.setValue("Host", url.encodedHost());
325
326     http_.setHost(url.host(), url.port(80));
327
328     return http_.request(header);
329 }
330
331 void DasTelefonbuch::getSearchTypes(QList<SearchType>& types) const
332 {
333     types.clear();
334     types.push_back(BOTH);
335 }