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 NUMBER_REGEXP = "<div class=\"callRow\">(.*)</div>";
50 static const QString LOGIN_CHECK = "<input class=\"inpTxt\" id=\"loginformUsername\"";
53 Eniro::Eniro(QObject *parent): Source(parent), site_(Eniro::FI),
54 loggedIn_(false), username_(""), password_(""),
55 timerId_(0), pendingSearches_(), pendingNumberRequests_()
67 for(searchMap::iterator sit = pendingSearches_.begin();
68 sit != pendingSearches_.end(); sit++)
77 pendingSearches_.clear();
79 for(numberMap::iterator nit = pendingNumberRequests_.begin();
80 nit != pendingNumberRequests_.end(); nit++)
89 pendingNumberRequests_.clear();
90 pendingLoginRequests_.clear();
93 void Eniro::setSite(Eniro::Site site)
98 void Eniro::timerEvent(QTimerEvent* t)
102 int currentId = http_.currentId();
106 searchMap::const_iterator it = pendingSearches_.find(currentId);
108 if(it != pendingSearches_.end())
110 QVector <Eniro::Result> results = it.value()->results;
111 SearchDetails details = it.value()->details;
115 setError(TIMEOUT, TIMEOUT_STRING);
117 emit requestFinished(results, details, true);
122 void Eniro::login(QString const& username,
123 QString const& password)
125 username_ = username;
126 password_ = password;
137 void Eniro::search(SearchDetails const& details)
141 SearchType type = details.type;
143 // Only logged in users can use other than person search
149 QUrl url = createUrl(details.query, details.location);
174 url.addQueryItem("what", what);
176 http_.setHost(url.host(), url.port(80));
177 int id = http_.get(url.encodedPath() + '?' + url.encodedQuery());
179 QVector <Source::Result> results;
181 // Store search data for later identification
182 SearchData* newData = new SearchData;
183 newData->details = details;
184 newData->results = results;
185 newData->foundNumbers = 0;
186 newData->numbersTotal = 0;
188 // Store request id so that it can be identified later
189 pendingSearches_[id] = newData;
193 void Eniro::handleHttpData(int id, QString const& data)
195 qDebug() << "Handle:" << id;
196 searchMap::const_iterator searchIt;
197 numberMap::const_iterator numberIt;
199 // Check if request is pending search request
200 if((searchIt = pendingSearches_.find(id)) !=
201 pendingSearches_.end())
203 qDebug() << "Load results";
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);
270 switch(it.value()->details.type)
273 expr = YELLOW_REGEXP;
276 expr = PERSON_REGEXP;
285 bool requestsPending = false;
290 while((pos = rx.indexIn(httpData, pos)) != -1)
292 pos += rx.matchedLength();
295 data = stripTags(data);
296 QStringList rows = data.split('\n');
298 for(int i = 0; i < rows.size(); i++)
300 // Remove white spaces
301 QString trimmed = rows.at(i).trimmed().toLower();
303 // Remove empty strings
304 if(trimmed.isEmpty())
311 // Convert words to uppercase
312 rows[i] = ucFirst(trimmed);
318 int size = rows.size();
323 result.name = rows[0];
327 result.name = rows[0];
328 result.city = rows[1];
332 result.name = rows[0];
333 result.street = rows[1];
334 result.city = rows[2];
338 result.name = rows[0];
339 // Remove slashes and spaces from number
340 result.number = cleanUpNumber(rows[1]);
341 result.street = rows[2];
342 result.city = rows[3];
350 it.value()->results.push_back(result);
352 unsigned int foundResults = ++(it.value()->numbersTotal);
354 // If phone number searh is enabled, we have to make another
355 // request to find it out
356 if(getFindNumber() && size < 4 && loggedIn_ &&
357 it.value()->details.type != YELLOW_PAGES)
359 requestsPending = true;
360 getNumberForResult(id, it.value()->results.size() - 1, it.value()->details);
362 // Otherwise result is ready
365 emit resultAvailable(result, it.value()->details);
368 unsigned int maxResults = getMaxResults();
370 // Stop searching if max results is reached
371 if(maxResults && (foundResults >= maxResults))
377 // If there were no results or no phone numbers needed to
378 // be fetched, the whole request is ready
379 if(it.value()->numbersTotal == 0 || !requestsPending)
383 if(httpData.indexOf(LOGIN_CHECK) != -1)
385 setError(INVALID_LOGIN, INVALID_LOGIN_STRING),
389 emitRequestFinished(it.key(), it.value(), error);
393 // Loads phone number from html source
394 void Eniro::loadNumber(int id, QString const& result)
396 numberMap::iterator numberIt = pendingNumberRequests_.find(id);
398 // Make sure that id exists in pending number requests
399 if(numberIt == pendingNumberRequests_.end() || numberIt.value() == 0)
404 searchMap::iterator searchIt = pendingSearches_.find(numberIt.value()->searchId);
406 if(searchIt == pendingSearches_.end() || searchIt.value() == 0)
411 QRegExp rx(NUMBER_REGEXP);
417 if((pos = rx.indexIn(result, pos)) != -1)
419 QString data = rx.cap(1);
420 data = stripTags(data);
422 QString trimmed = data.trimmed();
424 if(!trimmed.isEmpty())
426 // Remove whitespaces from number
427 searchIt.value()->results[numberIt.value()->index].number = cleanUpNumber(trimmed);
429 emit resultAvailable(searchIt.value()->results[numberIt.value()->index], searchIt.value()->details);
431 unsigned int found = ++searchIt.value()->foundNumbers;
433 // Check if all numbers have been found
434 if(found >= searchIt.value()->numbersTotal)
436 emitRequestFinished(searchIt.key(), searchIt.value(), false);
439 // If number was found, there was no error
446 setError(INVALID_LOGIN, INVALID_LOGIN_STRING);
447 emitRequestFinished(searchIt.key(), searchIt.value(), true);
450 // Remove number request
451 int key = numberIt.key();
453 delete pendingNumberRequests_[key];
454 pendingNumberRequests_[key] = 0;
455 pendingNumberRequests_.remove(key);
459 QUrl Eniro::createUrl(QString const& query, QString const& location)
461 QUrl url(SITE_URLS[site_] + "query");
465 url.addQueryItem("search_word", query);
468 if(!location.isEmpty())
470 url.addQueryItem("geo_area", location);
473 unsigned int maxResults = getMaxResults();
477 url.addQueryItem("hpp", QString::number(maxResults));
481 url.addQueryItem("login_name", username_);
482 url.addQueryItem("login_password", password_);
490 // Creates a new request for phone number retrieval
491 void Eniro::getNumberForResult(int id, int index, SearchDetails const& details)
493 QUrl url = createUrl(details.query, details.location);
494 url.addQueryItem("what", "mobwpinfo");
495 url.addQueryItem("search_number", QString::number(index + 1));
497 http_.setHost(url.host(), url.port(80));
498 int requestId = http_.get(url.encodedPath() + '?' + url.encodedQuery());
499 NumberData* number = new NumberData;
500 number->searchId = id;
501 number->index = index;
502 pendingNumberRequests_[requestId] = number;
506 void Eniro::emitRequestFinished(int key, SearchData* data, bool error)
508 emit requestFinished(data->results, data->details, error);
509 delete pendingSearches_[key];
510 pendingSearches_[key] = 0;
511 pendingSearches_.remove(key);
515 QMap <Eniro::Site, Eniro::SiteDetails> Eniro::getSites()
517 QMap <Site, SiteDetails> sites;
520 for(int i = 0; i < SITE_COUNT; i++)
523 details.name = SITE_NAMES[i];
524 details.id = SITE_IDS[i];
525 sites[static_cast<Site>(i)] = details;
531 Eniro::Site Eniro::stringToSite(QString const& str)
534 QString lower = str.toLower();
536 for(int i = 0; i < SITE_COUNT; i++)
538 if(lower == SITE_NAMES[i] || lower == SITE_IDS[i])
540 site = static_cast <Site> (i);