X-Git-Url: http://git.maemo.org/git/?p=jenirok;a=blobdiff_plain;f=src%2Fcommon%2Feniro.cpp;h=718064dce2f47f09501b2e59ea984affea1ba6a8;hp=896a788a856801589fe36afa1f288886db66a214;hb=f99718e69b270a89094113a26b22c694b79382f0;hpb=72f0ccb17472f83c1cba569879e185eb7cd60e31 diff --git a/src/common/eniro.cpp b/src/common/eniro.cpp index 896a788..718064d 100644 --- a/src/common/eniro.cpp +++ b/src/common/eniro.cpp @@ -25,7 +25,7 @@ namespace { "http://wap.eniro.fi/", "http://wap.eniro.se/", - "http://wap.eniro.dk/" + "http://m.krak.dk/" }; static const QString SITE_NAMES[Eniro::SITE_COUNT] = @@ -45,33 +45,25 @@ namespace static const QString INVALID_LOGIN_STRING = "Invalid login details"; static const QString TIMEOUT_STRING = "Request timed out"; static const QString PERSON_REGEXP = "(.*)(.*)"; - static const QString YELLOW_REGEXP = "(.*)(.*)"; + static const QString YELLOW_REGEXP = "(.*)(.*)|(.*)(.*)"; + static const QString SINGLE_REGEXP = "
(.*)
(.*)
(.*)(
|

(.*)
|

(.*)
)"; static const QString NUMBER_REGEXP = "
(.*)
"; static const QString LOGIN_CHECK = "]+)>"); - -Eniro::Eniro(Site site, QObject *parent): QObject(parent), site_(site), -username_(""), password_(""), loggedIn_(false), error_(NO_ERROR), -errorString_(""), maxResults_(10), timeout_(0), timerId_(0), findNumber_(true), -pendingSearches_(), pendingNumberRequests_() +Eniro::Eniro(QObject *parent): Source(parent), site_(Eniro::FI), +loggedIn_(false), username_(""), password_(""), +timerId_(0), pendingSearches_(), pendingNumberRequests_() { - connect(&http_, SIGNAL(requestFinished(int, bool)), this, SLOT(httpReady(int, bool))); } Eniro::~Eniro() { - abort(); } void Eniro::abort() { - http_.abort(); + Source::abort(); for(searchMap::iterator sit = pendingSearches_.begin(); sit != pendingSearches_.end(); sit++) @@ -99,63 +91,32 @@ void Eniro::abort() pendingLoginRequests_.clear(); } -void Eniro::setMaxResults(unsigned int value) -{ - maxResults_ = value; -} - -void Eniro::setFindNumber(bool value) -{ - findNumber_ = value; -} - void Eniro::setSite(Eniro::Site site) { site_ = site; } -void Eniro::setTimeout(unsigned int ms) +void Eniro::timerEvent(QTimerEvent* t) { - timeout_ = ms; - resetTimeout(); -} + Q_UNUSED(t); -void Eniro::resetTimeout() -{ - if(timerId_) - { - killTimer(timerId_); - } - if(timeout_) - { - timerId_ = startTimer(timeout_); - } -} + int currentId = http_.currentId(); -void Eniro::timerEvent(QTimerEvent* t) -{ - if(t->timerId() == timerId_) + if(currentId) { - int currentId = http_.currentId(); + searchMap::const_iterator it = pendingSearches_.find(currentId); - if(currentId) + if(it != pendingSearches_.end()) { - searchMap::const_iterator it = pendingSearches_.find(currentId); + QVector results = it.value()->results; + SearchDetails details = it.value()->details; - if(it != pendingSearches_.end()) - { - QVector results = it.value()->results; - SearchDetails details = it.value()->details; + abort(); - abort(); + setError(TIMEOUT, TIMEOUT_STRING); - error_ = TIMEOUT; - errorString_ = TIMEOUT_STRING; - - emit requestFinished(results, details, true); - } + emit requestFinished(results, details, true); } - } } @@ -174,25 +135,14 @@ void Eniro::logout() loggedIn_ = false; } -void Eniro::testLogin() -{ - QUrl url = createUrl("", ""); - - url.addQueryItem("what", "mobwp"); - http_.setHost(url.host(), url.port(80)); - int id = http_.get(url.encodedPath() + '?' + url.encodedQuery()); - - pendingLoginRequests_.insert(id); -} - -bool Eniro::search(SearchDetails const& details) +void Eniro::search(SearchDetails const& details) { resetTimeout(); SearchType type = details.type; // Only logged in users can use other than person search - if(!loggedIn_) + if(!loggedIn_ && site_ == FI) { type = PERSONS; } @@ -200,7 +150,13 @@ bool Eniro::search(SearchDetails const& details) QUrl url = createUrl(details.query, details.location); QString what; - if(loggedIn_) + // We must use full search instead of wap page because wap search is currently not + // working for persons + if(loggedIn_ && type == PERSONS && site_ == FI && getMaxResults() > 1) + { + what = "wp"; + } + else if(loggedIn_ || site_ != FI) { switch(type) { @@ -214,6 +170,7 @@ bool Eniro::search(SearchDetails const& details) default: what = "moball"; + break; } } @@ -227,7 +184,9 @@ bool Eniro::search(SearchDetails const& details) http_.setHost(url.host(), url.port(80)); int id = http_.get(url.encodedPath() + '?' + url.encodedQuery()); - QVector results; + //qDebug() << "Url: " << url.host() << url.encodedPath() << "?" << url.encodedQuery(); + + QVector results; // Store search data for later identification SearchData* newData = new SearchData; @@ -239,26 +198,10 @@ bool Eniro::search(SearchDetails const& details) // Store request id so that it can be identified later pendingSearches_[id] = newData; - return true; -} - -Eniro::Error Eniro::error() const -{ - return error_; } -const QString& Eniro::errorString() const +void Eniro::handleHttpData(int id, QByteArray const& data) { - return errorString_; -} - -void Eniro::httpReady(int id, bool error) -{ - if(error) - { - qDebug() << "Error: " << http_.errorString(); - } - searchMap::const_iterator searchIt; numberMap::const_iterator numberIt; @@ -266,37 +209,30 @@ void Eniro::httpReady(int id, bool error) if((searchIt = pendingSearches_.find(id)) != pendingSearches_.end()) { - if(error) + if(data.isEmpty()) { - error_ = CONNECTION_FAILURE; - errorString_ = http_.errorString(); + setError(CONNECTION_FAILURE, "Server returned empty data"); emitRequestFinished(id, searchIt.value(), true); return; } - QString result(http_.readAll()); - // Load results from html data - loadResults(id, result); + loadResults(id, data); } // Check if request is pending number requests else if((numberIt = pendingNumberRequests_.find(id)) != pendingNumberRequests_.end()) { - if(error) + if(data.isEmpty()) { - error_ = CONNECTION_FAILURE; - errorString_ = http_.errorString(); - delete pendingNumberRequests_[id]; - pendingNumberRequests_.remove(id); + setError(CONNECTION_FAILURE, "Server returned empty data"); + emitRequestFinished(id, searchIt.value(), true); return; } - QString result(http_.readAll()); - // Load number from html data - loadNumber(id, result); + loadNumber(id, data); } // Check for login request @@ -305,17 +241,8 @@ void Eniro::httpReady(int id, bool error) { bool success = true; - if(!error) - { - QString result(http_.readAll()); - - // If html source contains LOGIN_CHECK, login failed - if(result.indexOf(LOGIN_CHECK) != -1) - { - success = false; - } - } - else + // If html source contains LOGIN_CHECK, login failed + if(data.indexOf(LOGIN_CHECK) != -1) { success = false; } @@ -325,25 +252,49 @@ void Eniro::httpReady(int id, bool error) } +void Eniro::handleHttpError(int id) +{ + searchMap::const_iterator searchIt; + numberMap::const_iterator numberIt; + + // Check if request is pending search request + if((searchIt = pendingSearches_.find(id)) != + pendingSearches_.end()) + { + setError(CONNECTION_FAILURE, http_.errorString()); + emitRequestFinished(id, searchIt.value(), true); + } + + // Check if request is pending number requests + else if((numberIt = pendingNumberRequests_.find(id)) != + pendingNumberRequests_.end()) + { + setError(CONNECTION_FAILURE, http_.errorString()); + delete pendingNumberRequests_[id]; + pendingNumberRequests_.remove(id); + } + + // Check for login request + else if(pendingLoginRequests_.find(id) != + pendingLoginRequests_.end()) + { + emit loginStatus(false); + } +} + // Loads results from html source code void Eniro::loadResults(int id, QString const& httpData) { searchMap::iterator it = pendingSearches_.find(id); - QString expr; - switch(it.value()->details.type) + // Finnish person search is not working in wap mode so we have to use different type of loading + if(getMaxResults() > 1 && loggedIn_ && site_ == FI && it.value()->details.type == PERSONS) { - case YELLOW_PAGES: - expr = YELLOW_REGEXP; - break; - case PERSONS: - expr = PERSON_REGEXP; - break; - default: + loadFinnishPersonResults(id, httpData); return; } - QRegExp rx(expr); + QRegExp rx("((" + YELLOW_REGEXP + ")|(" + PERSON_REGEXP + ")|(" + SINGLE_REGEXP + "))"); rx.setMinimal(true); bool requestsPending = false; @@ -355,8 +306,10 @@ void Eniro::loadResults(int id, QString const& httpData) { pos += rx.matchedLength(); - data = rx.cap(2); + data = rx.cap(1); + data = stripTags(data); + QStringList rows = data.split('\n'); for(int i = 0; i < rows.size(); i++) @@ -379,6 +332,19 @@ void Eniro::loadResults(int id, QString const& httpData) Result result; + switch(site_) + { + case FI: + result.country = "Finland"; + break; + case SE: + result.country = "Sweden"; + break; + case DK: + result.country = "Denmark"; + break; + } + int size = rows.size(); switch(size) @@ -393,9 +359,18 @@ void Eniro::loadResults(int id, QString const& httpData) break; case 3: - result.name = rows[0]; - result.street = rows[1]; - result.city = rows[2]; + if(isPhoneNumber(rows[1])) + { + result.name = rows[0]; + result.number = cleanUpNumber(rows[1]); + result.city = rows[2]; + } + else + { + result.name = rows[0]; + result.street = rows[1]; + result.city = rows[2]; + } break; case 4: @@ -407,6 +382,35 @@ void Eniro::loadResults(int id, QString const& httpData) break; default: + bool ok = false; + + for(int a = 0; a < size && a < 8; a++) + { + if(isPhoneNumber(rows[a])) + { + result.name = rows[0]; + result.number = cleanUpNumber(rows[a]); + + for(int i = a + 1; i < size && i < 8; i++) + { + if(!isPhoneNumber(rows[i]) && size > i + 1 && isStreet(rows[i])) + { + result.street = rows[i]; + result.city = rows[i+1]; + ok = true; + break; + } + } + + } + + } + + if(ok) + { + break; + } + continue; } @@ -415,9 +419,9 @@ void Eniro::loadResults(int id, QString const& httpData) unsigned int foundResults = ++(it.value()->numbersTotal); - // If phone number searh is enabled, we have to make another + // If phone number search is enabled, we have to make another // request to find it out - if(findNumber_ && size < 4 && loggedIn_ && + if(getFindNumber() && size < 4 && (loggedIn_ || site_ != FI) && it.value()->details.type != YELLOW_PAGES) { requestsPending = true; @@ -426,17 +430,20 @@ void Eniro::loadResults(int id, QString const& httpData) // Otherwise result is ready else { + it.value()->foundNumbers++; emit resultAvailable(result, it.value()->details); } + unsigned int maxResults = getMaxResults(); + // Stop searching if max results is reached - if(maxResults_ && (foundResults >= maxResults_)) + if(maxResults && (foundResults >= maxResults)) { break; } } - // If number there were no results or no phone numbers needed to + // If there were no results or no phone numbers needed to // be fetched, the whole request is ready if(it.value()->numbersTotal == 0 || !requestsPending) { @@ -444,8 +451,7 @@ void Eniro::loadResults(int id, QString const& httpData) if(httpData.indexOf(LOGIN_CHECK) != -1) { - error_ = INVALID_LOGIN; - errorString_ = INVALID_LOGIN_STRING; + setError(INVALID_LOGIN, INVALID_LOGIN_STRING), error = true; } @@ -453,6 +459,102 @@ void Eniro::loadResults(int id, QString const& httpData) } } +void Eniro::loadFinnishPersonResults(int id, QString const& httpData) +{ + searchMap::iterator it = pendingSearches_.find(id); + + static QRegExp rx("
"); + static QRegExp name("(.*)"); + static QRegExp number("(.*)