69327b04b185e3696cd133b4dd4781aab137dd63
[jenirok] / src / common / mobil1881.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 "mobil1881.h"
21
22
23 Mobil1881::Mobil1881(QObject* parent): Source(parent)
24 {
25 }
26
27 Mobil1881::~Mobil1881()
28 {
29     abort();
30 }
31
32 void Mobil1881::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
46 void Mobil1881::search(Source::SearchDetails const& details)
47 {
48     resetTimeout();
49
50     SearchData* newData = new SearchData;
51     newData->details = details;
52     newData->currentPage = 1;
53     newData->finishedSearches = 0;
54
55     if(details.type == Source::BOTH)
56     {
57         newData->totalSearches = 2;
58         Source::SearchDetails tmpDetails = details;
59         tmpDetails.type = Source::PERSONS;
60         int id1 = sendQuery(tmpDetails, 1);
61         tmpDetails.type = Source::YELLOW_PAGES;
62         int id2 = sendQuery(tmpDetails, 1);
63         newData->searchIds.insert(id1);
64         newData->searchIds.insert(id2);
65     }
66     else
67     {
68         newData->totalSearches = 1;
69         int id = sendQuery(details, 1);
70         newData->searchIds.insert(id);
71     }
72
73     pendingSearches_.push_back(newData);
74 }
75
76 void Mobil1881::handleHttpData(int id, QByteArray const& data)
77 {
78     QString decoded = QString::fromUtf8(data.data());
79
80     for(int i = 0; i < pendingSearches_.size(); i++)
81     {
82         if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
83             pendingSearches_.at(i)->searchIds.end())
84         {
85             addNumbers(pendingSearches_.at(i), decoded, i);
86             break;
87         }
88     }
89 }
90
91 void Mobil1881::handleHttpError(int id)
92 {
93     for(int i = 0; i < pendingSearches_.size(); i++)
94     {
95         if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
96             pendingSearches_.at(i)->searchIds.end())
97         {
98             setError(Source::CONNECTION_FAILURE, http_.errorString());
99             emitRequestFinished(pendingSearches_.at(i), true, i);
100             break;
101         }
102     }
103 }
104
105 void Mobil1881::addNumbers(SearchData* searchData,
106                            QString const& data,
107                            int index)
108 {
109     if(data.isEmpty())
110     {
111         qDebug() << "Server returned no data";
112         qDebug() << "Headers: " << http_.lastResponse().toString();
113     }
114
115     if(data.indexOf("<b>Last ned vCard</b>") > 0)
116     {
117         addOnlyNumber(searchData, data, index);
118         return;
119     }
120
121     int pos = 0;
122     static QRegExp rx("<td valign=\"top\" width=\"99%\">(.*)</td>");
123     static QRegExp name("<div class=\"srln\">(.*)</div>");
124     static QRegExp address("<div class=\"srla\">(.*),<br/>(.*)</div>");
125     static QRegExp number("<div class=\"srlp\">(.*)</div>");
126     rx.setMinimal(true);
127     name.setMinimal(true);
128     address.setMinimal(true);
129     number.setMinimal(true);
130
131     int maxResults = getMaxResults();
132
133     while((pos = rx.indexIn(data, pos)) != -1)
134     {
135         pos += rx.matchedLength();
136
137         if(searchData->results.size() >= maxResults)
138         {
139             break;
140         }
141
142         QString part = rx.cap(1);
143         Source::Result result;
144         QString nameStr;
145         QString numberStr;
146         QString streetStr;
147         QString cityStr;
148
149         if(name.indexIn(part) != -1)
150         {
151             nameStr = name.cap(1);
152         }
153
154         if(address.indexIn(part) != -1)
155         {
156             streetStr = address.cap(1);
157             cityStr = address.cap(2);
158         }
159
160         if(number.indexIn(part) != -1)
161         {
162             numberStr = number.cap(1);
163         }
164
165         if(formatResult(nameStr, numberStr, streetStr,
166                         cityStr, result))
167         {
168             emit resultAvailable(result, searchData->details);
169             searchData->results.push_back(result);
170         }
171
172     }
173
174     searchData->finishedSearches++;
175
176     if(searchData->results.size() >= maxResults)
177     {
178         emitRequestFinished(searchData, false, index);
179
180         if(searchData->totalSearches > 1)
181         {
182             abort();
183         }
184     }
185     else
186     {
187         if(data.indexOf("Neste") > 0)
188         {
189             searchData->currentPage++;
190             int id = sendQuery(searchData->details, searchData->currentPage);
191             searchData->searchIds.insert(id);
192         }
193         else if(searchData->finishedSearches >= searchData->totalSearches)
194         {
195             emitRequestFinished(searchData, false, index);
196         }
197     }
198
199 }
200
201 void Mobil1881::addOnlyNumber(SearchData* searchData,
202                               QString const& data,
203                               int index)
204 {
205     static QRegExp name("<div class=\"srsln\">(.*)</div>");
206     static QRegExp number("class=\"srlttxt\"><b>(.*)</b>");
207     static QRegExp address("class=\"srlttxt\"><span>(.*),<br/>(.*)</span>");
208     name.setMinimal(true);
209     number.setMinimal(true);
210     address.setMinimal(true);
211
212     Source::Result result;
213
214     QString nameStr;
215     QString numberStr;
216     QString streetStr;
217     QString cityStr;
218
219     if(name.indexIn(data) != -1)
220     {
221         nameStr = name.cap(1);
222     }
223
224     if(number.indexIn(data) != -1)
225     {
226         numberStr = number.cap(1);
227     }
228
229     if(address.indexIn(data) != -1)
230     {
231         streetStr = address.cap(1);
232         cityStr = address.cap(2);
233     }
234
235     if(formatResult(nameStr, numberStr, streetStr,
236                     cityStr, result))
237     {
238         searchData->results.push_back(result);
239         emit resultAvailable(result, searchData->details);
240     }
241
242     emitRequestFinished(searchData, false, index);
243 }
244
245 bool Mobil1881::formatResult(QString& name, QString& number,
246                              QString& street, QString& city,
247                              Source::Result& result)
248 {
249     name = stripTags(name);
250     name = htmlEntityDecode(name);
251     result.name = name.trimmed();
252     number = stripTags(number);
253     number = cleanUpNumber(number);
254     result.number = number.trimmed();
255     street = stripTags(street);
256     street = htmlEntityDecode(street);
257     city = stripTags(city);
258     city = htmlEntityDecode(city);
259     result.street = street.trimmed();
260     result.city = city.trimmed();
261     result.country = "Norway";
262
263     if(!result.name.isEmpty() && (!getFindNumber() || !result.number.isEmpty()))
264     {
265         return true;
266     }
267
268     return false;
269 }
270
271 void Mobil1881::emitRequestFinished(SearchData* data,
272                                     bool error, int index)
273 {
274     QVector<Source::Result> results = data->results;
275     Source::SearchDetails details = data->details;
276
277     emit requestFinished(results, details, error);
278     delete pendingSearches_[index];
279     pendingSearches_[index] = 0;
280     pendingSearches_.removeAt(index);
281 }
282
283 int Mobil1881::sendQuery(Source::SearchDetails const& details,
284                          int page)
285 {
286     QUrl url("http://wap.1881.no/");
287     url.addQueryItem("i", "4854");
288     url.addQueryItem("showonly", "1");
289     QString query = details.query;
290
291     if(!details.location.isEmpty())
292     {
293         query += " " + details.location;
294     }
295
296     url.addQueryItem("s", query);
297     if(details.type == Source::YELLOW_PAGES)
298     {
299         url.addQueryItem("t", "c");
300     }
301     else
302     {
303         url.addQueryItem("t", "p");
304     }
305
306     if(page > 1)
307     {
308         url.addQueryItem("p", QString::number(page));
309     }
310
311     fixUrl(url);
312
313     http_.setHost(url.host(), url.port(80));
314     return http_.get(url.encodedPath() + '?' + url.encodedQuery());
315 }