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())
99 setError(Source::CONNECTION_FAILURE, http_.errorString());
100 emitRequestFinished(pendingSearches_.at(i), true, i);
106 void Mobil1881::addNumbers(SearchData* searchData,
110 if(data.indexOf("<b>Last ned vCard</b>") > 0)
112 addOnlyNumber(searchData, data, index);
117 QRegExp rx("<td valign=\"top\" width=\"99%\">(.*)</td>");
118 QRegExp name("<div class=\"srln\">(.*)</div>");
119 QRegExp address("<div class=\"srla\">(.*),<br/>(.*)</div>");
120 QRegExp number("<div class=\"srlp\">(.*)</div>");
122 name.setMinimal(true);
123 address.setMinimal(true);
124 number.setMinimal(true);
126 int maxResults = getMaxResults();
128 while((pos = rx.indexIn(data, pos)) != -1)
130 pos += rx.matchedLength();
132 if(searchData->results.size() >= maxResults)
137 QString part = rx.cap(1);
138 Source::Result result;
144 if(name.indexIn(part) != -1)
146 nameStr = name.cap(1);
149 if(address.indexIn(part) != -1)
151 streetStr = address.cap(1);
152 cityStr = address.cap(2);
155 if(number.indexIn(part) != -1)
157 numberStr = number.cap(1);
160 if(formatResult(nameStr, numberStr, streetStr,
163 emit resultAvailable(result, searchData->details);
164 searchData->results.push_back(result);
169 searchData->finishedSearches++;
171 if(searchData->results.size() >= maxResults)
173 emitRequestFinished(searchData, false, index);
177 if(data.indexOf("Neste") > 0)
179 searchData->currentPage++;
180 int id = sendQuery(searchData->details, searchData->currentPage);
181 searchData->searchIds.insert(id);
183 else if(searchData->finishedSearches >= searchData->totalSearches)
185 emitRequestFinished(searchData, false, index);
191 void Mobil1881::addOnlyNumber(SearchData* searchData,
195 QRegExp name("<div class=\"srsln\">(.*)</div>");
196 QRegExp number("class=\"srlttxt\"><b>(.*)</b>");
197 QRegExp address("class=\"srlttxt\"><span>(.*),<br/>(.*)</span>");
198 name.setMinimal(true);
199 number.setMinimal(true);
200 address.setMinimal(true);
202 Source::Result result;
209 if(name.indexIn(data) != -1)
211 nameStr = name.cap(1);
214 if(number.indexIn(data) != -1)
216 numberStr = number.cap(1);
219 if(address.indexIn(data) != -1)
221 streetStr = address.cap(1);
222 cityStr = address.cap(2);
225 if(formatResult(nameStr, numberStr, streetStr,
228 searchData->results.push_back(result);
229 emit resultAvailable(result, searchData->details);
232 emitRequestFinished(searchData, false, index);
235 bool Mobil1881::formatResult(QString& name, QString& number,
236 QString& street, QString& city,
237 Source::Result& result)
239 name = stripTags(name);
240 name = htmlEntityDecode(name);
241 result.name = name.trimmed();
242 number = stripTags(number);
243 number = cleanUpNumber(number);
244 result.number = number.trimmed();
245 street = stripTags(street);
246 street = htmlEntityDecode(street);
247 city = stripTags(city);
248 city = htmlEntityDecode(city);
249 result.street = street.trimmed();
250 result.city = city.trimmed();
251 result.country = "Norway";
253 if(!result.name.isEmpty() && (!getFindNumber() || !result.number.isEmpty()))
261 void Mobil1881::emitRequestFinished(SearchData* data,
262 bool error, int index)
264 QVector<Source::Result> results = data->results;
265 Source::SearchDetails details = data->details;
267 emit requestFinished(results, details, error);
268 delete pendingSearches_[index];
269 pendingSearches_[index] = 0;
270 pendingSearches_.removeAt(index);
273 int Mobil1881::sendQuery(Source::SearchDetails const& details,
276 QUrl url("http://wap.1881.no/");
277 url.addQueryItem("i", "4854");
278 url.addQueryItem("showonly", "1");
279 QString query = details.query;
281 if(!details.location.isEmpty())
283 query += " " + details.location;
286 url.addQueryItem("s", query);
287 if(details.type == Source::YELLOW_PAGES)
289 url.addQueryItem("t", "c");
293 url.addQueryItem("t", "p");
298 url.addQueryItem("p", QString::number(page));
303 http_.setHost(url.host(), url.port(80));
304 return http_.get(url.encodedPath() + '?' + url.encodedQuery());