Log In
New Account
  
Home My Page Project Cloud Code Snippets Project Openings Jenirok
Summary Tracker SCM Files
1 /*
2  * This file is part of Jenirok.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  */
19 #include <QtCore/QDebug>
20 #include "eniro.h"
22 namespace
23 {
24     static const QString SITE_URLS[Eniro::SITE_COUNT] =
25     {
26             "http://wap.eniro.fi/",
27             "http://wap.eniro.se/",
28             "http://wap.eniro.dk/"
29     };
31     static const QString SITE_NAMES[Eniro::SITE_COUNT] =
32     {
33          "finnish",
34          "swedish",
35          "danish"
36     };
38     static const QString SITE_IDS[Eniro::SITE_COUNT] =
39     {
40          "fi",
41          "se",
42          "dk"
43     };
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 class=\"block\">|</p>(.*)<br/>|</p>(.*)<br />)";
50     static const QString NUMBER_REGEXP = "<div class=\"callRow\">(.*)</div>";
51     static const QString LOGIN_CHECK = "<input class=\"inpTxt\" id=\"loginformUsername\"";
52 }
54 Eniro::Eniro(QObject *parent): Source(parent), site_(Eniro::FI),
55 loggedIn_(false), username_(""), password_(""),
56 timerId_(0), pendingSearches_(), pendingNumberRequests_()
57 {
58 }
60 Eniro::~Eniro()
61 {
62 }
64 void Eniro::abort()
65 {
66     Source::abort();
68     for(searchMap::iterator sit = pendingSearches_.begin();
69     sit != pendingSearches_.end(); sit++)
70     {
71         if(sit.value() != 0)
72         {
73             delete sit.value();
74             sit.value() = 0;
75         }
76     }
78     pendingSearches_.clear();
80     for(numberMap::iterator nit = pendingNumberRequests_.begin();
81     nit != pendingNumberRequests_.end(); nit++)
82     {
83         if(nit.value() != 0)
84         {
85             delete nit.value();
86             nit.value() = 0;
87         }
88     }
90     pendingNumberRequests_.clear();
91     pendingLoginRequests_.clear();
92 }
94 void Eniro::setSite(Eniro::Site site)
95 {
96     site_ = site;
97 }
99 void Eniro::timerEvent(QTimerEvent* t)
101     Q_UNUSED(t);
103     int currentId = http_.currentId();
105     if(currentId)
106     {
107         searchMap::const_iterator it = pendingSearches_.find(currentId);
109         if(it != pendingSearches_.end())
110         {
111             QVector <Eniro::Result> results = it.value()->results;
112             SearchDetails details = it.value()->details;
114             abort();
116             setError(TIMEOUT, TIMEOUT_STRING);
118             emit requestFinished(results, details, true);
119         }
120     }
123 void Eniro::login(QString const& username,
124                   QString const& password)
126     username_ = username;
127     password_ = password;
128     loggedIn_ = true;
131 void Eniro::logout()
133     username_ = "";
134     password_ = "";
135     loggedIn_ = false;
138 void Eniro::search(SearchDetails const& details)
140     resetTimeout();
142     SearchType type = details.type;
144     // Only logged in users can use other than person search
145     if(!loggedIn_ && site_ == FI)
146     {
147         type = PERSONS;
148     }
150     QUrl url = createUrl(details.query, details.location);
151     QString what;
153     if(loggedIn_ || site_ != FI)
154     {
155         switch(type)
156         {
157         case YELLOW_PAGES:
158             what = "mobcs";
159             break;
161         case PERSONS:
162             what = "mobwp";
163             break;
165         default:
166             what = "moball";
167             break;
168         }
170     }
171     else
172     {
173         what = "moball";
174     }
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())
203     {
204         // Load results from html data
205         loadResults(id, data);
206     }
208     // Check if request is pending number requests
209     else if((numberIt = pendingNumberRequests_.find(id)) !=
210         pendingNumberRequests_.end())
211     {
212         // Load number from html data
213         loadNumber(id, data);
214     }
216     // Check for login request
217     else if(pendingLoginRequests_.find(id) !=
218         pendingLoginRequests_.end())
219     {
220         bool success = true;
222         // If html source contains LOGIN_CHECK, login failed
223         if(data.indexOf(LOGIN_CHECK) != -1)
224         {
225             success = false;
226         }
228         emit loginStatus(success);
229     }
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())
241     {
242         setError(CONNECTION_FAILURE, http_.errorString());
243         emitRequestFinished(id, searchIt.value(), true);
244     }
246     // Check if request is pending number requests
247     else if((numberIt = pendingNumberRequests_.find(id)) !=
248         pendingNumberRequests_.end())
249     {
250         setError(CONNECTION_FAILURE, http_.errorString());
251         delete pendingNumberRequests_[id];
252         pendingNumberRequests_.remove(id);
253     }
255     // Check for login request
256     else if(pendingLoginRequests_.find(id) !=
257         pendingLoginRequests_.end())
258     {
259         emit loginStatus(false);
260     }
263 // Loads results from html source code
264 void Eniro::loadResults(int id, QString const& httpData)
266     searchMap::iterator it = pendingSearches_.find(id);
268     QRegExp rx("((" + YELLOW_REGEXP + ")|(" + PERSON_REGEXP + ")|(" + SINGLE_REGEXP + "))");
269     rx.setMinimal(true);
271     bool requestsPending = false;
272     int pos = 0;
273     QString data;
275     // Find all matches
276     while((pos = rx.indexIn(httpData, pos)) != -1)
277     {
278         pos += rx.matchedLength();
280         data = rx.cap(1);
282         data = stripTags(data);
284         QStringList rows = data.split('\n');
286         for(int i = 0; i < rows.size(); i++)
287         {
288             // Remove white spaces
289             QString trimmed = rows.at(i).trimmed().toLower();
291             // Remove empty strings
292             if(trimmed.isEmpty())
293             {
294                 rows.removeAt(i);
295                 i--;
296             }
297             else
298             {
299                 // Convert words to uppercase
300                 rows[i] = ucFirst(trimmed);
301             }
302         }
304         Result result;
306         switch(site_)
307         {
308         case FI:
309             result.country = "Finland";
310             break;
311         case SE:
312             result.country = "Sweden";
313             break;
314         case DK:
315             result.country = "Denmark";
316             break;
317         }
319         int size = rows.size();
321         switch(size)
322         {
323         case 1:
324             result.name = rows[0];
325             break;
327         case 2:
328             result.name = rows[0];
329             result.city = rows[1];
330             break;
332         case 3:
333             if(isPhoneNumber(rows[1]))
334             {
335                 result.name = rows[0];
336                 result.number = cleanUpNumber(rows[1]);
337                 result.city = rows[2];
338             }
339             else
340             {
341                 result.name = rows[0];
342                 result.street = rows[1];
343                 result.city = rows[2];
344             }
345             break;
347         case 4:
348             result.name = rows[0];
349             // Remove slashes and spaces from number
350             result.number = cleanUpNumber(rows[1]);
351             result.street = rows[2];
352             result.city = rows[3];
353             break;
355         default:
356             bool ok = false;
358             for(int a = 0; a < size && a < 8; a++)
359             {
360                 if(isPhoneNumber(rows[a]))
361                 {
362                     result.name = rows[0];
363                     result.number = cleanUpNumber(rows[a]);
365                     for(int i = a + 1; i < size && i < 8; i++)
366                     {
367                         if(!isPhoneNumber(rows[i]) && size > i + 1 && isStreet(rows[i]))
368                         {
369                             result.street = rows[i];
370                             result.city = rows[i+1];
371                             ok = true;
372                             break;
373                         }
374                     }
376                 }
378             }
380             if(ok)
381             {
382                 break;
383             }
385             continue;
387         }
389         it.value()->results.push_back(result);
391         unsigned int foundResults = ++(it.value()->numbersTotal);
393         // If phone number search is enabled, we have to make another
394         // request to find it out
395         if(getFindNumber() && size < 4 && (loggedIn_ || site_ != FI) &&
396                 it.value()->details.type != YELLOW_PAGES)
397         {
398             requestsPending = true;
399             getNumberForResult(id, it.value()->results.size() - 1, it.value()->details);
400         }
401         // Otherwise result is ready
402         else
403         {
404             emit resultAvailable(result, it.value()->details);
405         }
407         unsigned int maxResults = getMaxResults();
409         // Stop searching if max results is reached
410         if(maxResults && (foundResults >= maxResults))
411         {
412             break;
413         }
414     }
416     // If there were no results or no phone numbers needed to
417     // be fetched, the whole request is ready
418     if(it.value()->numbersTotal == 0 || !requestsPending)
419     {
420         bool error = false;
422         if(httpData.indexOf(LOGIN_CHECK) != -1)
423         {
424             setError(INVALID_LOGIN, INVALID_LOGIN_STRING),
425             error = true;
426         }
428         emitRequestFinished(it.key(), it.value(), error);
429     }
432 // Loads phone number from html source
433 void Eniro::loadNumber(int id, QString const& result)
435     numberMap::iterator numberIt = pendingNumberRequests_.find(id);
437     // Make sure that id exists in pending number requests
438     if(numberIt == pendingNumberRequests_.end() || numberIt.value() == 0)
439     {
440         return;
441     }
443     searchMap::iterator searchIt = pendingSearches_.find(numberIt.value()->searchId);
445     if(searchIt == pendingSearches_.end() || searchIt.value() == 0)
446     {
447         return;
448     }
450     QRegExp rx(NUMBER_REGEXP);
451     rx.setMinimal(true);
453     int pos = 0;
454     bool error = true;
456     if((pos = rx.indexIn(result, pos)) != -1)
457     {
458         QString data = rx.cap(1);
459         data = stripTags(data);
461         QString trimmed = data.trimmed();
463         if(!trimmed.isEmpty())
464         {
465             // Remove whitespaces from number
466             searchIt.value()->results[numberIt.value()->index].number = cleanUpNumber(trimmed);
468             emit resultAvailable(searchIt.value()->results[numberIt.value()->index], searchIt.value()->details);
470             unsigned int found = ++searchIt.value()->foundNumbers;
472             // Check if all numbers have been found
473             if(found >= searchIt.value()->numbersTotal)
474             {
475                 emitRequestFinished(searchIt.key(), searchIt.value(), false);
476             }
478             // If number was found, there was no error
479             error = false;
480         }
481     }
483     if(error)
484     {
485         setError(INVALID_LOGIN, INVALID_LOGIN_STRING);
486         emitRequestFinished(searchIt.key(), searchIt.value(), true);
487     }
489     // Remove number request
490     int key = numberIt.key();
492     delete pendingNumberRequests_[key];
493     pendingNumberRequests_[key] = 0;
494     pendingNumberRequests_.remove(key);
498 QUrl Eniro::createUrl(QString const& query, QString const& location)
500     QUrl url(SITE_URLS[site_] + "query");
502     if(!query.isEmpty())
503     {
504         url.addQueryItem("search_word", query);
505     }
507     if(!location.isEmpty())
508     {
509         url.addQueryItem("geo_area", location);
510     }
512     unsigned int maxResults = getMaxResults();
514     if(maxResults)
515     {
516         url.addQueryItem("hpp", QString::number(maxResults));
517     }
518     if(loggedIn_ && site_ == FI)
519     {
520         url.addQueryItem("login_name", username_);
521         url.addQueryItem("login_password", password_);
522     }
524     fixUrl(url);
526     return url;
529 // Creates a new request for phone number retrieval
530 void Eniro::getNumberForResult(int id, int index, SearchDetails const& details)
532     QUrl url = createUrl(details.query, details.location);
533     url.addQueryItem("what", "mobwpinfo");
534     url.addQueryItem("search_number", QString::number(index + 1));
536     http_.setHost(url.host(), url.port(80));
537     int requestId = http_.get(url.encodedPath() + '?' + url.encodedQuery());
538     NumberData* number = new NumberData;
539     number->searchId = id;
540     number->index = index;
541     pendingNumberRequests_[requestId] = number;
545 void Eniro::emitRequestFinished(int key, SearchData* data, bool error)
547     emit requestFinished(data->results, data->details, error);
548     delete pendingSearches_[key];
549     pendingSearches_[key] = 0;
550     pendingSearches_.remove(key);
554 QMap <Eniro::Site, Eniro::SiteDetails> Eniro::getSites()
556     QMap <Site, SiteDetails> sites;
558     for(int i = 0; i < SITE_COUNT; i++)
559     {
560         SiteDetails details;
561         details.name = SITE_NAMES[i];
562         details.id = SITE_IDS[i];
563         sites[static_cast<Site>(i)] = details;
564     }
566     return sites;
569 Eniro::Site Eniro::stringToSite(QString const& str)
571     Site site = FI;
572     QString lower = str.toLower();
574     for(int i = 0; i < SITE_COUNT; i++)
575     {
576         if(lower == SITE_NAMES[i] || lower == SITE_IDS[i])
577         {
578             site = static_cast <Site> (i);
579             break;
580         }
581     }
583     return site;
586 bool Eniro::isStreet(QString const& str)
588     static QRegExp number("([0-9]+)");
589     int a = number.indexIn(str);
590     int b = str.indexOf(" ");
592     if((a == -1 && b == -1) || (a != -1 && b != -1))
593     {
594         return true;
595     }
597     return false;

Terms of Use    Privacy Policy    Contribution Guidelines    Feedback

Powered By GForge Collaborative Development Environment