2 * This file is part of Jenirok.
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.
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.
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/>.
19 #include <QtCore/QDebug>
20 #include "mobil1881.h"
23 Mobil1881::Mobil1881(QObject* parent): Source(parent)
27 Mobil1881::~Mobil1881()
32 void Mobil1881::abort()
36 for(int i = 0; i < pendingSearches_.size(); i++)
38 delete pendingSearches_[i];
39 pendingSearches_[i] = 0;
42 pendingSearches_.clear();
46 void Mobil1881::search(Source::SearchDetails const& details)
50 SearchData* newData = new SearchData;
51 newData->details = details;
52 newData->currentPage = 1;
53 newData->finishedSearches = 0;
55 if(details.type == Source::BOTH)
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);
68 newData->totalSearches = 1;
69 int id = sendQuery(details, 1);
70 newData->searchIds.insert(id);
73 pendingSearches_.push_back(newData);
76 void Mobil1881::handleHttpData(int id, QByteArray const& data)
78 QString decoded = QString::fromUtf8(data.data());
80 for(int i = 0; i < pendingSearches_.size(); i++)
82 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
83 pendingSearches_.at(i)->searchIds.end())
85 addNumbers(pendingSearches_.at(i), decoded, i);
91 void Mobil1881::handleHttpError(int id)
93 for(int i = 0; i < pendingSearches_.size(); i++)
95 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
96 pendingSearches_.at(i)->searchIds.end())
98 setError(Source::CONNECTION_FAILURE, http_.errorString());
99 emitRequestFinished(pendingSearches_.at(i), true, i);
105 void Mobil1881::addNumbers(SearchData* searchData,
111 qDebug() << "Server returned no data";
112 qDebug() << "Headers: " << http_.lastResponse().toString();
115 if(data.indexOf("<b>Last ned vCard</b>") > 0)
117 addOnlyNumber(searchData, data, index);
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>");
127 name.setMinimal(true);
128 address.setMinimal(true);
129 number.setMinimal(true);
131 int maxResults = getMaxResults();
133 while((pos = rx.indexIn(data, pos)) != -1)
135 pos += rx.matchedLength();
137 if(searchData->results.size() >= maxResults)
142 QString part = rx.cap(1);
143 Source::Result result;
149 if(name.indexIn(part) != -1)
151 nameStr = name.cap(1);
154 if(address.indexIn(part) != -1)
156 streetStr = address.cap(1);
157 cityStr = address.cap(2);
160 if(number.indexIn(part) != -1)
162 numberStr = number.cap(1);
165 if(formatResult(nameStr, numberStr, streetStr,
168 emit resultAvailable(result, searchData->details);
169 searchData->results.push_back(result);
174 searchData->finishedSearches++;
176 if(searchData->results.size() >= maxResults)
178 emitRequestFinished(searchData, false, index);
180 if(searchData->totalSearches > 1)
187 if(data.indexOf("Neste") > 0)
189 searchData->currentPage++;
190 int id = sendQuery(searchData->details, searchData->currentPage);
191 searchData->searchIds.insert(id);
193 else if(searchData->finishedSearches >= searchData->totalSearches)
195 emitRequestFinished(searchData, false, index);
201 void Mobil1881::addOnlyNumber(SearchData* searchData,
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);
212 Source::Result result;
219 if(name.indexIn(data) != -1)
221 nameStr = name.cap(1);
224 if(number.indexIn(data) != -1)
226 numberStr = number.cap(1);
229 if(address.indexIn(data) != -1)
231 streetStr = address.cap(1);
232 cityStr = address.cap(2);
235 if(formatResult(nameStr, numberStr, streetStr,
238 searchData->results.push_back(result);
239 emit resultAvailable(result, searchData->details);
242 emitRequestFinished(searchData, false, index);
245 bool Mobil1881::formatResult(QString& name, QString& number,
246 QString& street, QString& city,
247 Source::Result& result)
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";
263 if(!result.name.isEmpty() && (!getFindNumber() || !result.number.isEmpty()))
271 void Mobil1881::emitRequestFinished(SearchData* data,
272 bool error, int index)
274 QVector<Source::Result> results = data->results;
275 Source::SearchDetails details = data->details;
277 emit requestFinished(results, details, error);
278 delete pendingSearches_[index];
279 pendingSearches_[index] = 0;
280 pendingSearches_.removeAt(index);
283 int Mobil1881::sendQuery(Source::SearchDetails const& details,
286 QUrl url("http://wap.1881.no/");
287 url.addQueryItem("i", "4854");
288 url.addQueryItem("showonly", "1");
289 QString query = details.query;
291 if(!details.location.isEmpty())
293 query += " " + details.location;
296 url.addQueryItem("s", query);
297 if(details.type == Source::YELLOW_PAGES)
299 url.addQueryItem("t", "c");
303 url.addQueryItem("t", "p");
308 url.addQueryItem("p", QString::number(page));
313 http_.setHost(url.host(), url.port(80));
314 return http_.get(url.encodedPath() + '?' + url.encodedQuery());