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 "dastelefonbuch.h"
23 DasTelefonbuch::DasTelefonbuch(QObject* parent): Source(parent)
27 DasTelefonbuch::~DasTelefonbuch()
32 void DasTelefonbuch::abort()
36 for(int i = 0; i < pendingSearches_.size(); i++)
38 delete pendingSearches_[i];
39 pendingSearches_[i] = 0;
42 pendingSearches_.clear();
45 void DasTelefonbuch::search(Source::SearchDetails const& details)
49 SearchData* newData = new SearchData;
50 newData->details = details;
51 newData->currentPage = 1;
52 newData->finishedSearches = 0;
54 if(details.type == Source::BOTH)
56 newData->totalSearches = 2;
57 Source::SearchDetails tmpDetails = details;
58 tmpDetails.type = Source::PERSONS;
59 int id1 = sendQuery(tmpDetails, 1);
60 tmpDetails.type = Source::YELLOW_PAGES;
61 int id2 = sendQuery(tmpDetails, 1);
62 newData->searchIds.insert(id1);
63 newData->searchIds.insert(id2);
67 newData->totalSearches = 1;
68 int id = sendQuery(details, 1);
69 newData->searchIds.insert(id);
72 pendingSearches_.push_back(newData);
75 void DasTelefonbuch::handleHttpData(int id, QByteArray const& data)
77 QString strData(data);
79 for(int i = 0; i < pendingSearches_.size(); i++)
81 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
82 pendingSearches_.at(i)->searchIds.end())
84 addNumbers(pendingSearches_.at(i), strData, i);
90 void DasTelefonbuch::handleHttpError(int id)
92 for(int i = 0; i < pendingSearches_.size(); i++)
94 if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
95 pendingSearches_.at(i)->searchIds.end())
98 setError(Source::CONNECTION_FAILURE, http_.errorString());
99 emitRequestFinished(pendingSearches_.at(i), true, i);
105 void DasTelefonbuch::addNumbers(SearchData* searchData,
109 if(data.indexOf("<span>1 Gesamttreffer</span>") > 0)
111 addOnlyNumber(searchData, data, index);
116 QRegExp rx("<table.*class=\"bg-01 entry(.*)</table>");
117 QRegExp name("<div class=\"long hide\">(.*)</a>");
118 QRegExp number("<td class=\"col4\">(.*)</td>");
119 QRegExp address("<td class=\"col2\">(.*)</td>.*<td class=\"col3\">(.*)</td>");
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 /* TODO: Paging not implemented yet
178 if(data.indexOf("Neste") > 0)
180 searchData->currentPage++;
181 int id = sendQuery(searchData->details, searchData->currentPage);
182 searchData->searchIds.insert(id);
188 else if(searchData->finishedSearches >= searchData->totalSearches)
190 emitRequestFinished(searchData, false, index);
196 void DasTelefonbuch::addOnlyNumber(SearchData* searchData,
200 QRegExp name("<div class=\"long hide\">(.*) (.*)</a>");
201 QRegExp number("<td class=\"col4\">(.*)</td>");
202 QRegExp address("<td class=\"col2\">(.*)</td>.*<td class=\"col3\">(.*)</td>");
203 name.setMinimal(true);
204 number.setMinimal(true);
205 address.setMinimal(true);
207 Source::Result result;
214 if(name.indexIn(data) != -1)
216 nameStr = name.cap(2).simplified() + name.cap(1).simplified();
217 nameStr.replace("\n","");
220 if(number.indexIn(data) != -1)
222 numberStr = number.cap(1);
225 if(address.indexIn(data) != -1)
227 streetStr = address.cap(1);
228 cityStr = address.cap(2);
231 if(formatResult(nameStr, numberStr, streetStr,
234 searchData->results.push_back(result);
235 emit resultAvailable(result, searchData->details);
238 emitRequestFinished(searchData, false, index);
241 bool DasTelefonbuch::formatResult(QString& name, QString& number,
242 QString& street, QString& city,
243 Source::Result& result)
245 name = stripTags(name);
246 name = htmlEntityDecode(name);
247 result.name = name.trimmed();
248 number = stripTags(number);
249 number = cleanUpNumber(number);
250 result.number = number.trimmed();
251 street = stripTags(street);
252 street = htmlEntityDecode(street);
253 city = stripTags(city);
254 city = htmlEntityDecode(city);
255 result.street = street.trimmed();
256 result.city = city.trimmed();
257 result.country = "Germany";
259 if(!result.name.isEmpty() && (!getFindNumber() || !result.number.isEmpty()))
266 void DasTelefonbuch::emitRequestFinished(SearchData* data,
267 bool error, int index)
269 QVector<Source::Result> results = data->results;
270 Source::SearchDetails details = data->details;
272 emit requestFinished(results, details, error);
273 delete pendingSearches_[index];
274 pendingSearches_[index] = 0;
275 pendingSearches_.removeAt(index);
278 int DasTelefonbuch::sendQuery(Source::SearchDetails const& details,
283 QUrl url("http://www.dastelefonbuch.de/");
285 //Pretending we are a Firefox-Plugin allows a simpler query-String
286 url.addQueryItem("sourceid","Mozilla-search");
287 //But we will need to give a proper User-Agent-String...see below
289 url.addQueryItem("cmd","search");
291 QString query = details.query;
293 if(!details.location.isEmpty())
295 query += "+" + details.location;
298 //Query is added as "kw"
299 url.addQueryItem("kw", query);
301 /* TODO No differentiation between personal and professional entries yet
302 if(details.type == Source::YELLOW_PAGES)
304 url.addQueryItem("t", "c");
308 url.addQueryItem("t", "p");
312 /* TODO No multi-page results yet.
315 url.addQueryItem("p", QString::number(page));
321 //Remember, we are firefox, therefore setting User-Agent here...
322 QHttpRequestHeader header("GET", url.encodedPath()+ '?' + url.encodedQuery());
324 header.setValue("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10");
325 header.setValue("Host", url.encodedHost());
327 http_.setHost(url.host(), url.port(80));
329 return http_.request(header);
332 void DasTelefonbuch::getSearchTypes(QList<SearchType>& types) const
335 types.push_back(BOTH);