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, QString const& data)
62 for(int i = 0; i < pendingSearches_.size(); i++)
64 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
65 pendingSearches_.at(i)->searchIds.end())
68 addNumbers(pendingSearches_.at(i), data, i);
74 void Mobil1881::handleHttpError(int id)
76 for(int i = 0; i < pendingSearches_.size(); i++)
78 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
79 pendingSearches_.at(i)->searchIds.end())
82 setError(Source::CONNECTION_FAILURE, http_.errorString());
83 emitRequestFinished(pendingSearches_.at(i), true, i);
89 void Mobil1881::addNumbers(SearchData* searchData,
93 if(data.indexOf("<b>Last ned vCard</b>") > 0)
95 addOnlyNumber(searchData, data, index);
100 QRegExp rx("<td valign=\"top\" width=\"99%\">(.*)</td>");
101 QRegExp name("<div class=\"srln\">(.*)</div>");
102 QRegExp address("<div class=\"srla\">(.*),<br/>(.*)</div>");
103 QRegExp number("<div class=\"srlp\">(.*)</div>");
105 name.setMinimal(true);
106 address.setMinimal(true);
107 number.setMinimal(true);
109 int maxResults = getMaxResults();
111 while((pos = rx.indexIn(data, pos)) != -1)
113 pos += rx.matchedLength();
115 if(searchData->results.size() >= maxResults)
120 QString part = rx.cap(1);
121 Source::Result result;
127 if(name.indexIn(part) != -1)
129 nameStr = name.cap(1);
132 if(address.indexIn(part) != -1)
134 streetStr = address.cap(1);
135 cityStr = address.cap(2);
138 if(number.indexIn(part) != -1)
140 numberStr = number.cap(1);
143 if(formatResult(nameStr, numberStr, streetStr,
146 emit resultAvailable(result, searchData->details);
147 searchData->results.push_back(result);
152 if(searchData->results.size() >= maxResults)
154 emitRequestFinished(searchData, false, index);
159 if(data.indexOf("Neste") > 0)
161 searchData->currentPage++;
162 int id = sendQuery(searchData->details, searchData->currentPage);
163 searchData->searchIds.insert(id);
167 emitRequestFinished(searchData, false, index);
173 void Mobil1881::addOnlyNumber(SearchData* searchData,
177 QRegExp name("<div class=\"srsln\">(.*)</div>");
178 QRegExp number("class=\"srlttxt\"><b>(.*)</b>");
179 QRegExp address("class=\"srlttxt\"><span>(.*),<br/>(.*)</span>");
180 name.setMinimal(true);
181 number.setMinimal(true);
182 address.setMinimal(true);
184 Source::Result result;
191 if(name.indexIn(data) != -1)
193 nameStr = name.cap(1);
196 if(number.indexIn(data) != -1)
198 numberStr = number.cap(1);
201 if(address.indexIn(data) != -1)
203 streetStr = address.cap(1);
204 cityStr = address.cap(2);
207 if(formatResult(nameStr, numberStr, streetStr,
210 searchData->results.push_back(result);
211 emit resultAvailable(result, searchData->details);
214 emitRequestFinished(searchData, false, index);
217 bool Mobil1881::formatResult(QString& name, QString& number,
218 QString& street, QString& city,
219 Source::Result& result)
221 name = stripTags(name);
222 result.name = name.trimmed();
223 number = stripTags(number);
224 number = cleanUpNumber(number);
225 result.number = number.trimmed();
226 street = stripTags(street);
227 city = stripTags(city);
228 result.street = street.trimmed();
229 result.city = city.trimmed();
231 if(!result.name.isEmpty() && !result.number.isEmpty())
239 void Mobil1881::emitRequestFinished(SearchData* data,
240 bool error, int index)
242 QVector<Source::Result> results = data->results;
243 Source::SearchDetails details = data->details;
245 emit requestFinished(results, details, error);
246 delete pendingSearches_[index];
247 pendingSearches_[index] = 0;
248 pendingSearches_.removeAt(index);
251 int Mobil1881::sendQuery(Source::SearchDetails const& details,
254 QUrl url("http://wap.1881.no/");
255 url.addQueryItem("i", "4854");
256 url.addQueryItem("showonly", "1");
257 QString query = details.query;
259 if(!details.location.isEmpty())
261 query += details.location;
264 url.addQueryItem("s", query);
265 if(details.type == Source::YELLOW_PAGES)
267 url.addQueryItem("t", "c");
271 url.addQueryItem("t", "p");
276 url.addQueryItem("p", QString::number(page));
281 http_.setHost(url.host(), url.port(80));
282 return http_.get(url.encodedPath() + '?' + url.encodedQuery());