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>
24 static const QString SITE_URLS[Eniro::SITE_COUNT] =
26 "http://wap.eniro.fi/",
27 "http://wap.eniro.se/",
28 "http://wap.eniro.dk/"
31 static const QString SITE_NAMES[Eniro::SITE_COUNT] =
38 static const QString SITE_IDS[Eniro::SITE_COUNT] =
45 static const QString INVALID_LOGIN_STRING = "Invalid login details";
46 static const QString TIMEOUT_STRING = "Request timed out";
47 static const QString PERSON_REGEXP = "<td class=\"hTd2\">(.*)<b>(.*)</td>";
48 static const QString YELLOW_REGEXP = "<td class=\"hTd2\">(.*)<span class=\"gray\">(.*)</td>";
49 static const QString SINGLE_REGEXP = "<div class=\"header\">(.*)</div>(.*)<div class=\"callRow\">(.*)</div>";
50 static const QString NUMBER_REGEXP = "<div class=\"callRow\">(.*)</div>";
51 static const QString LOGIN_CHECK = "<input class=\"inpTxt\" id=\"loginformUsername\"";
54 Eniro::Eniro(QObject *parent): Source(parent), site_(Eniro::FI),
55 loggedIn_(false), username_(""), password_(""),
56 timerId_(0), pendingSearches_(), pendingNumberRequests_()
68 for(searchMap::iterator sit = pendingSearches_.begin();
69 sit != pendingSearches_.end(); sit++)
78 pendingSearches_.clear();
80 for(numberMap::iterator nit = pendingNumberRequests_.begin();
81 nit != pendingNumberRequests_.end(); nit++)
90 pendingNumberRequests_.clear();
91 pendingLoginRequests_.clear();
94 void Eniro::setSite(Eniro::Site site)
99 void Eniro::timerEvent(QTimerEvent* t)
103 int currentId = http_.currentId();
107 searchMap::const_iterator it = pendingSearches_.find(currentId);
109 if(it != pendingSearches_.end())
111 QVector <Eniro::Result> results = it.value()->results;
112 SearchDetails details = it.value()->details;
116 setError(TIMEOUT, TIMEOUT_STRING);
118 emit requestFinished(results, details, true);
123 void Eniro::login(QString const& username,
124 QString const& password)
126 username_ = username;
127 password_ = password;
138 void Eniro::search(SearchDetails const& details)
142 SearchType type = details.type;
144 // Only logged in users can use other than person search
150 QUrl url = createUrl(details.query, details.location);
176 url.addQueryItem("what", what);
178 http_.setHost(url.host(), url.port(80));
179 int id = http_.get(url.encodedPath() + '?' + url.encodedQuery());
181 QVector <Source::Result> results;
183 // Store search data for later identification
184 SearchData* newData = new SearchData;
185 newData->details = details;
186 newData->results = results;
187 newData->foundNumbers = 0;
188 newData->numbersTotal = 0;
190 // Store request id so that it can be identified later
191 pendingSearches_[id] = newData;
195 void Eniro::handleHttpData(int id, QByteArray const& data)
197 searchMap::const_iterator searchIt;
198 numberMap::const_iterator numberIt;
200 // Check if request is pending search request
201 if((searchIt = pendingSearches_.find(id)) !=
202 pendingSearches_.end())
204 // Load results from html data
205 loadResults(id, data);
208 // Check if request is pending number requests
209 else if((numberIt = pendingNumberRequests_.find(id)) !=
210 pendingNumberRequests_.end())
212 // Load number from html data
213 loadNumber(id, data);
216 // Check for login request
217 else if(pendingLoginRequests_.find(id) !=
218 pendingLoginRequests_.end())
222 // If html source contains LOGIN_CHECK, login failed
223 if(data.indexOf(LOGIN_CHECK) != -1)
228 emit loginStatus(success);
233 void Eniro::handleHttpError(int id)
235 searchMap::const_iterator searchIt;
236 numberMap::const_iterator numberIt;
238 // Check if request is pending search request
239 if((searchIt = pendingSearches_.find(id)) !=
240 pendingSearches_.end())
242 setError(CONNECTION_FAILURE, http_.errorString());
243 emitRequestFinished(id, searchIt.value(), true);
246 // Check if request is pending number requests
247 else if((numberIt = pendingNumberRequests_.find(id)) !=
248 pendingNumberRequests_.end())
250 setError(CONNECTION_FAILURE, http_.errorString());
251 delete pendingNumberRequests_[id];
252 pendingNumberRequests_.remove(id);
255 // Check for login request
256 else if(pendingLoginRequests_.find(id) !=
257 pendingLoginRequests_.end())
259 emit loginStatus(false);
264 // Loads results from html source code
265 void Eniro::loadResults(int id, QString const& httpData)
267 searchMap::iterator it = pendingSearches_.find(id);
269 QRegExp rx("((" + YELLOW_REGEXP + ")|(" + PERSON_REGEXP + ")|(" + SINGLE_REGEXP + "))");
272 bool requestsPending = false;
277 while((pos = rx.indexIn(httpData, pos)) != -1)
279 pos += rx.matchedLength();
283 data = stripTags(data);
284 QStringList rows = data.split('\n');
286 for(int i = 0; i < rows.size(); i++)
288 // Remove white spaces
289 QString trimmed = rows.at(i).trimmed().toLower();
291 // Remove empty strings
292 if(trimmed.isEmpty())
299 // Convert words to uppercase
300 rows[i] = ucFirst(trimmed);
306 int size = rows.size();
311 result.name = rows[0];
315 result.name = rows[0];
316 result.city = rows[1];
320 result.name = rows[0];
321 result.street = rows[1];
322 result.city = rows[2];
326 result.name = rows[0];
327 // Remove slashes and spaces from number
328 result.number = cleanUpNumber(rows[1]);
329 result.street = rows[2];
330 result.city = rows[3];
338 it.value()->results.push_back(result);
340 unsigned int foundResults = ++(it.value()->numbersTotal);
342 // If phone number searh is enabled, we have to make another
343 // request to find it out
344 if(getFindNumber() && size < 4 && loggedIn_ &&
345 it.value()->details.type != YELLOW_PAGES)
347 requestsPending = true;
348 getNumberForResult(id, it.value()->results.size() - 1, it.value()->details);
350 // Otherwise result is ready
353 emit resultAvailable(result, it.value()->details);
356 unsigned int maxResults = getMaxResults();
358 // Stop searching if max results is reached
359 if(maxResults && (foundResults >= maxResults))
365 // If there were no results or no phone numbers needed to
366 // be fetched, the whole request is ready
367 if(it.value()->numbersTotal == 0 || !requestsPending)
371 if(httpData.indexOf(LOGIN_CHECK) != -1)
373 setError(INVALID_LOGIN, INVALID_LOGIN_STRING),
377 emitRequestFinished(it.key(), it.value(), error);
381 // Loads phone number from html source
382 void Eniro::loadNumber(int id, QString const& result)
384 numberMap::iterator numberIt = pendingNumberRequests_.find(id);
386 // Make sure that id exists in pending number requests
387 if(numberIt == pendingNumberRequests_.end() || numberIt.value() == 0)
392 searchMap::iterator searchIt = pendingSearches_.find(numberIt.value()->searchId);
394 if(searchIt == pendingSearches_.end() || searchIt.value() == 0)
399 QRegExp rx(NUMBER_REGEXP);
405 if((pos = rx.indexIn(result, pos)) != -1)
407 QString data = rx.cap(1);
408 data = stripTags(data);
410 QString trimmed = data.trimmed();
412 if(!trimmed.isEmpty())
414 // Remove whitespaces from number
415 searchIt.value()->results[numberIt.value()->index].number = cleanUpNumber(trimmed);
417 emit resultAvailable(searchIt.value()->results[numberIt.value()->index], searchIt.value()->details);
419 unsigned int found = ++searchIt.value()->foundNumbers;
421 // Check if all numbers have been found
422 if(found >= searchIt.value()->numbersTotal)
424 emitRequestFinished(searchIt.key(), searchIt.value(), false);
427 // If number was found, there was no error
434 setError(INVALID_LOGIN, INVALID_LOGIN_STRING);
435 emitRequestFinished(searchIt.key(), searchIt.value(), true);
438 // Remove number request
439 int key = numberIt.key();
441 delete pendingNumberRequests_[key];
442 pendingNumberRequests_[key] = 0;
443 pendingNumberRequests_.remove(key);
447 QUrl Eniro::createUrl(QString const& query, QString const& location)
449 QUrl url(SITE_URLS[site_] + "query");
453 url.addQueryItem("search_word", query);
456 if(!location.isEmpty())
458 url.addQueryItem("geo_area", location);
461 unsigned int maxResults = getMaxResults();
465 url.addQueryItem("hpp", QString::number(maxResults));
469 url.addQueryItem("login_name", username_);
470 url.addQueryItem("login_password", password_);
478 // Creates a new request for phone number retrieval
479 void Eniro::getNumberForResult(int id, int index, SearchDetails const& details)
481 QUrl url = createUrl(details.query, details.location);
482 url.addQueryItem("what", "mobwpinfo");
483 url.addQueryItem("search_number", QString::number(index + 1));
485 http_.setHost(url.host(), url.port(80));
486 int requestId = http_.get(url.encodedPath() + '?' + url.encodedQuery());
487 NumberData* number = new NumberData;
488 number->searchId = id;
489 number->index = index;
490 pendingNumberRequests_[requestId] = number;
494 void Eniro::emitRequestFinished(int key, SearchData* data, bool error)
496 emit requestFinished(data->results, data->details, error);
497 delete pendingSearches_[key];
498 pendingSearches_[key] = 0;
499 pendingSearches_.remove(key);
503 QMap <Eniro::Site, Eniro::SiteDetails> Eniro::getSites()
505 QMap <Site, SiteDetails> sites;
508 for(int i = 0; i < SITE_COUNT; i++)
511 details.name = SITE_NAMES[i];
512 details.id = SITE_IDS[i];
513 sites[static_cast<Site>(i)] = details;
519 Eniro::Site Eniro::stringToSite(QString const& str)
522 QString lower = str.toLower();
524 for(int i = 0; i < SITE_COUNT; i++)
526 if(lower == SITE_NAMES[i] || lower == SITE_IDS[i])
528 site = static_cast <Site> (i);