6ded070a983e29cd0f99082fb240fe189a040ec0
[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
99             setError(Source::CONNECTION_FAILURE, http_.errorString());
100             emitRequestFinished(pendingSearches_.at(i), true, i);
101             break;
102         }
103     }
104 }
105
106 void Mobil1881::addNumbers(SearchData* searchData,
107                            QString const& data,
108                            int index)
109 {
110     if(data.indexOf("<b>Last ned vCard</b>") > 0)
111     {
112         addOnlyNumber(searchData, data, index);
113         return;
114     }
115
116     int pos = 0;
117     static QRegExp rx("<td valign=\"top\" width=\"99%\">(.*)</td>");
118     static QRegExp name("<div class=\"srln\">(.*)</div>");
119     static QRegExp address("<div class=\"srla\">(.*),<br/>(.*)</div>");
120     static QRegExp number("<div class=\"srlp\">(.*)</div>");
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         if(searchData->totalSearches > 1)
176         {
177             abort();
178         }
179     }
180     else
181     {
182         if(data.indexOf("Neste") > 0)
183         {
184             searchData->currentPage++;
185             int id = sendQuery(searchData->details, searchData->currentPage);
186             searchData->searchIds.insert(id);
187         }
188         else if(searchData->finishedSearches >= searchData->totalSearches)
189         {
190             emitRequestFinished(searchData, false, index);
191         }
192     }
193
194 }
195
196 void Mobil1881::addOnlyNumber(SearchData* searchData,
197                               QString const& data,
198                               int index)
199 {
200     static QRegExp name("<div class=\"srsln\">(.*)</div>");
201     static QRegExp number("class=\"srlttxt\"><b>(.*)</b>");
202     static QRegExp address("class=\"srlttxt\"><span>(.*),<br/>(.*)</span>");
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(1);
217     }
218
219     if(number.indexIn(data) != -1)
220     {
221         numberStr = number.cap(1);
222     }
223
224     if(address.indexIn(data) != -1)
225     {
226         streetStr = address.cap(1);
227         cityStr = address.cap(2);
228     }
229
230     if(formatResult(nameStr, numberStr, streetStr,
231                     cityStr, result))
232     {
233         searchData->results.push_back(result);
234         emit resultAvailable(result, searchData->details);
235     }
236
237     emitRequestFinished(searchData, false, index);
238 }
239
240 bool Mobil1881::formatResult(QString& name, QString& number,
241                              QString& street, QString& city,
242                              Source::Result& result)
243 {
244     name = stripTags(name);
245     name = htmlEntityDecode(name);
246     result.name = name.trimmed();
247     number = stripTags(number);
248     number = cleanUpNumber(number);
249     result.number = number.trimmed();
250     street = stripTags(street);
251     street = htmlEntityDecode(street);
252     city = stripTags(city);
253     city = htmlEntityDecode(city);
254     result.street = street.trimmed();
255     result.city = city.trimmed();
256     result.country = "Norway";
257
258     if(!result.name.isEmpty() && (!getFindNumber() || !result.number.isEmpty()))
259     {
260         return true;
261     }
262
263     return false;
264 }
265
266 void Mobil1881::emitRequestFinished(SearchData* data,
267                                     bool error, int index)
268 {
269     QVector<Source::Result> results = data->results;
270     Source::SearchDetails details = data->details;
271
272     emit requestFinished(results, details, error);
273     delete pendingSearches_[index];
274     pendingSearches_[index] = 0;
275     pendingSearches_.removeAt(index);
276 }
277
278 int Mobil1881::sendQuery(Source::SearchDetails const& details,
279                          int page)
280 {
281     QUrl url("http://wap.1881.no/");
282     url.addQueryItem("i", "4854");
283     url.addQueryItem("showonly", "1");
284     QString query = details.query;
285
286     if(!details.location.isEmpty())
287     {
288         query += " " + details.location;
289     }
290
291     url.addQueryItem("s", query);
292     if(details.type == Source::YELLOW_PAGES)
293     {
294         url.addQueryItem("t", "c");
295     }
296     else
297     {
298         url.addQueryItem("t", "p");
299     }
300
301     if(page > 1)
302     {
303         url.addQueryItem("p", QString::number(page));
304     }
305
306     fixUrl(url);
307
308     http_.setHost(url.host(), url.port(80));
309     return http_.get(url.encodedPath() + '?' + url.encodedQuery());
310 }