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 int id = sendQuery(details, 1);
52 SearchData* newData = new SearchData;
53 newData->details = details;
54 newData->searchIds.insert(id);
55 newData->currentPage = 1;
57 pendingSearches_.push_back(newData);
60 void Mobil1881::handleHttpData(int id, QByteArray const& data)
62 QString decoded = QString::fromUtf8(data.data());
64 for(int i = 0; i < pendingSearches_.size(); i++)
66 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
67 pendingSearches_.at(i)->searchIds.end())
69 addNumbers(pendingSearches_.at(i), decoded, i);
75 void Mobil1881::handleHttpError(int id)
77 for(int i = 0; i < pendingSearches_.size(); i++)
79 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
80 pendingSearches_.at(i)->searchIds.end())
83 setError(Source::CONNECTION_FAILURE, http_.errorString());
84 emitRequestFinished(pendingSearches_.at(i), true, i);
90 void Mobil1881::addNumbers(SearchData* searchData,
94 if(data.indexOf("<b>Last ned vCard</b>") > 0)
96 addOnlyNumber(searchData, data, index);
101 QRegExp rx("<td valign=\"top\" width=\"99%\">(.*)</td>");
102 QRegExp name("<div class=\"srln\">(.*)</div>");
103 QRegExp address("<div class=\"srla\">(.*),<br/>(.*)</div>");
104 QRegExp number("<div class=\"srlp\">(.*)</div>");
106 name.setMinimal(true);
107 address.setMinimal(true);
108 number.setMinimal(true);
110 int maxResults = getMaxResults();
112 while((pos = rx.indexIn(data, pos)) != -1)
114 pos += rx.matchedLength();
116 if(searchData->results.size() >= maxResults)
121 QString part = rx.cap(1);
122 Source::Result result;
128 if(name.indexIn(part) != -1)
130 nameStr = name.cap(1);
133 if(address.indexIn(part) != -1)
135 streetStr = address.cap(1);
136 cityStr = address.cap(2);
139 if(number.indexIn(part) != -1)
141 numberStr = number.cap(1);
144 if(formatResult(nameStr, numberStr, streetStr,
147 emit resultAvailable(result, searchData->details);
148 searchData->results.push_back(result);
153 if(searchData->results.size() >= maxResults)
155 emitRequestFinished(searchData, false, index);
160 if(data.indexOf("Neste") > 0)
162 searchData->currentPage++;
163 int id = sendQuery(searchData->details, searchData->currentPage);
164 searchData->searchIds.insert(id);
168 emitRequestFinished(searchData, false, index);
174 void Mobil1881::addOnlyNumber(SearchData* searchData,
178 QRegExp name("<div class=\"srsln\">(.*)</div>");
179 QRegExp number("class=\"srlttxt\"><b>(.*)</b>");
180 QRegExp address("class=\"srlttxt\"><span>(.*),<br/>(.*)</span>");
181 name.setMinimal(true);
182 number.setMinimal(true);
183 address.setMinimal(true);
185 Source::Result result;
192 if(name.indexIn(data) != -1)
194 nameStr = name.cap(1);
197 if(number.indexIn(data) != -1)
199 numberStr = number.cap(1);
202 if(address.indexIn(data) != -1)
204 streetStr = address.cap(1);
205 cityStr = address.cap(2);
208 if(formatResult(nameStr, numberStr, streetStr,
211 searchData->results.push_back(result);
212 emit resultAvailable(result, searchData->details);
215 emitRequestFinished(searchData, false, index);
218 bool Mobil1881::formatResult(QString& name, QString& number,
219 QString& street, QString& city,
220 Source::Result& result)
222 name = stripTags(name);
223 name = htmlEntityDecode(name);
224 result.name = name.trimmed();
225 number = stripTags(number);
226 number = cleanUpNumber(number);
227 result.number = number.trimmed();
228 street = stripTags(street);
229 street = htmlEntityDecode(street);
230 city = stripTags(city);
231 city = htmlEntityDecode(city);
232 result.street = street.trimmed();
233 result.city = city.trimmed();
235 if(!result.name.isEmpty() && (!getFindNumber() || !result.number.isEmpty()))
243 void Mobil1881::emitRequestFinished(SearchData* data,
244 bool error, int index)
246 QVector<Source::Result> results = data->results;
247 Source::SearchDetails details = data->details;
249 emit requestFinished(results, details, error);
250 delete pendingSearches_[index];
251 pendingSearches_[index] = 0;
252 pendingSearches_.removeAt(index);
255 int Mobil1881::sendQuery(Source::SearchDetails const& details,
258 QUrl url("http://wap.1881.no/");
259 url.addQueryItem("i", "4854");
260 url.addQueryItem("showonly", "1");
261 QString query = details.query;
263 if(!details.location.isEmpty())
265 query += " " + details.location;
268 url.addQueryItem("s", query);
269 if(details.type == Source::YELLOW_PAGES)
271 url.addQueryItem("t", "c");
275 url.addQueryItem("t", "p");
280 url.addQueryItem("p", QString::number(page));
285 http_.setHost(url.host(), url.port(80));
286 return http_.get(url.encodedPath() + '?' + url.encodedQuery());