From d93782e7a5ae0fc072d094fd645cf415a34a2244 Mon Sep 17 00:00:00 2001 From: eshe Date: Fri, 20 Aug 2010 19:21:39 +0100 Subject: [PATCH] Some fixes to connection manager. --- src/common/connectionmanager.cpp | 28 ++++++++++++++++++-- src/common/connectionmanager.h | 2 ++ src/common/contactmanager.cpp | 11 +++++--- src/common/mobil1881.cpp | 7 ++++- src/common/source.cpp | 4 ++- src/daemon/calllistener.cpp | 1 - src/gui/resultwindow.cpp | 54 +++++++++++++++++++++++++++++--------- src/gui/resultwindow.h | 6 +++++ 8 files changed, 93 insertions(+), 20 deletions(-) diff --git a/src/common/connectionmanager.cpp b/src/common/connectionmanager.cpp index fb0f395..f7b0f32 100644 --- a/src/common/connectionmanager.cpp +++ b/src/common/connectionmanager.cpp @@ -77,6 +77,12 @@ bool ConnectionManager::connect() icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags)); waitSignal(&connectionReady_); + + if(connected_) + { + sleep(WAIT_AFTER_CONNECT); + } + return connected_; } @@ -121,6 +127,8 @@ bool ConnectionManager::connect(QString const& id) connected_ = true; + sleep(WAIT_AFTER_CONNECT); + return true; } @@ -421,15 +429,31 @@ bool ConnectionManager::waitSignal(bool* ready) killTimer(timer_); + if(timeout_) + { + qDebug() << "Connection request timed out"; + } + return *ready || !timeout_; } +void ConnectionManager::sleep(unsigned int ms) +{ + timeout_ = false; + timer_ = startTimer(ms); + + while(!timeout_) + { + QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents); + } + + killTimer(timer_); +} + void ConnectionManager::timerEvent(QTimerEvent* event) { Q_UNUSED(event); killTimer(timer_); timeout_ = true; timer_ = 0; - - qDebug() << "Connection request timed out"; } diff --git a/src/common/connectionmanager.h b/src/common/connectionmanager.h index 34ec24c..b5a2c3c 100644 --- a/src/common/connectionmanager.h +++ b/src/common/connectionmanager.h @@ -55,6 +55,7 @@ public: bool scanConnections(QList& connections, ConnectionType type = NO_TYPE); Error error() const; static unsigned int const TIMEOUT = 15000; + static unsigned int const WAIT_AFTER_CONNECT = 400; protected: virtual void timerEvent(QTimerEvent* event); @@ -65,6 +66,7 @@ private slots: void scanResult(const QDBusMessage& rep); private: + void sleep(unsigned int ms); bool waitSignal(bool* ready); bool stateReady_; bool connectionReady_; diff --git a/src/common/contactmanager.cpp b/src/common/contactmanager.cpp index f9047b5..fc0c62d 100644 --- a/src/common/contactmanager.cpp +++ b/src/common/contactmanager.cpp @@ -17,6 +17,7 @@ */ #include +#include #include "contactmanager.h" @@ -77,6 +78,7 @@ bool ContactManager::addContact(Contact const& contact) char* firstname = contact.name.firstname.toUtf8().data(); char* surname = contact.name.surname.toUtf8().data(); + e_contact_set(newContact, E_CONTACT_GIVEN_NAME, (gpointer)firstname); e_contact_set(newContact, E_CONTACT_FAMILY_NAME, (gpointer)surname); @@ -123,12 +125,15 @@ void ContactManager::stringToName(QString const& strname, ContactManager::Name& if(ename) { + static QRegExp check("([A-Z]+)"); QString additional = QString::fromUtf8(ename->additional); + QString firstname = QString::fromUtf8(ename->given); + QString surname = QString::fromUtf8(ename->family); - if(additional.isEmpty()) + if(additional.isEmpty() && check.indexIn(firstname, 1) == -1 && check.indexIn(surname, 1) == -1) { - name.firstname = QString::fromUtf8(ename->given); - name.surname = QString::fromUtf8(ename->family); + name.firstname = firstname; + name.surname = surname; } else { diff --git a/src/common/mobil1881.cpp b/src/common/mobil1881.cpp index 6ded070..69327b0 100644 --- a/src/common/mobil1881.cpp +++ b/src/common/mobil1881.cpp @@ -95,7 +95,6 @@ void Mobil1881::handleHttpError(int id) if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) != pendingSearches_.at(i)->searchIds.end()) { - setError(Source::CONNECTION_FAILURE, http_.errorString()); emitRequestFinished(pendingSearches_.at(i), true, i); break; @@ -107,6 +106,12 @@ void Mobil1881::addNumbers(SearchData* searchData, QString const& data, int index) { + if(data.isEmpty()) + { + qDebug() << "Server returned no data"; + qDebug() << "Headers: " << http_.lastResponse().toString(); + } + if(data.indexOf("Last ned vCard") > 0) { addOnlyNumber(searchData, data, index); diff --git a/src/common/source.cpp b/src/common/source.cpp index 5d1315e..0109f4a 100644 --- a/src/common/source.cpp +++ b/src/common/source.cpp @@ -17,6 +17,7 @@ */ #include +#include #include "source.h" #include "eniro.h" #include "mobil1881.h" @@ -181,6 +182,8 @@ void Source::setError(Source::Error error, QString const& errorString) void Source::httpReady(int id, bool error) { + QByteArray result = http_.readAll(); + if(error) { if(http_.error() == QHttp::Aborted) @@ -193,7 +196,6 @@ void Source::httpReady(int id, bool error) } else { - QByteArray result = http_.readAll(); handleHttpData(id, result); } } diff --git a/src/daemon/calllistener.cpp b/src/daemon/calllistener.cpp index e73bf26..c76cfdc 100644 --- a/src/daemon/calllistener.cpp +++ b/src/daemon/calllistener.cpp @@ -526,7 +526,6 @@ bool CallListener::handleConnection() if(cm.connect(best.id)) { - sleep(500); break; } else if(cm.error() == ConnectionManager::INVALID_IAP) diff --git a/src/gui/resultwindow.cpp b/src/gui/resultwindow.cpp index e9da90a..dc1509f 100644 --- a/src/gui/resultwindow.cpp +++ b/src/gui/resultwindow.cpp @@ -30,7 +30,7 @@ #include "sourcecoreconfig.h" ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent), -source_(0), list_(0), connectionManager_(0) +source_(0), list_(0), connectionManager_(0), timer_(0), searching_(false) { setAttribute(Qt::WA_Maemo5StackedWindow); setWindowTitle(tr("Search results")); @@ -56,6 +56,16 @@ void ResultWindow::search(SearchDialog::SearchDetails& details) list_->clear(); } + show(); + setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true); + + if(!connectionManager_) + { + connectionManager_ = new ConnectionManager(); + } + + connectionManager_->connect(); + Source::SourceId id = Source::stringToId(Settings::instance()->get("source")); if(!source_ || id != sourceId_) @@ -90,17 +100,19 @@ void ResultWindow::search(SearchDialog::SearchDetails& details) config->apply(source_); delete config; - show(); - setAttribute(Qt::WA_Maemo5ShowProgressIndicator, true); - - if(!connectionManager_) + if(searching_) { - connectionManager_ = new ConnectionManager(); + source_->abort(); + timer_ = startTimer(SEARCH_INTERVAL); } - connectionManager_->connect(); + while(timer_) + { + QApplication::processEvents(QEventLoop::WaitForMoreEvents); + } - source_->abort(); + list_->clear(); + searching_ = true; source_->search(Source::SearchDetails(details.name, details.location, details.type)); } @@ -108,6 +120,11 @@ void ResultWindow::search(SearchDialog::SearchDetails& details) void ResultWindow::resultAvailable(Source::Result const& result, Source::SearchDetails const& details) { + if(!list_) + { + return; + } + Q_UNUSED(details); if(!result.number.isEmpty()) @@ -180,6 +197,14 @@ void ResultWindow::requestFinished(QVector const& results, setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false); + if(timer_) + { + killTimer(timer_); + } + + timer_ = startTimer(SEARCH_INTERVAL); + searching_ = false; + } void ResultWindow::itemClicked(QListWidgetItem* item) @@ -197,15 +222,20 @@ void ResultWindow::itemClicked(QListWidgetItem* item) void ResultWindow::setVisible(bool visible) { - QMainWindow::setVisible(visible); - if(!visible && source_) { source_->abort(); - - } + + QMainWindow::setVisible(visible); } +void ResultWindow::timerEvent(QTimerEvent* event) +{ + Q_UNUSED(event); + + killTimer(timer_); + timer_ = 0; +} diff --git a/src/gui/resultwindow.h b/src/gui/resultwindow.h index 1fff79a..3391f7d 100644 --- a/src/gui/resultwindow.h +++ b/src/gui/resultwindow.h @@ -35,6 +35,7 @@ public: ResultWindow(QWidget* parent = 0); ~ResultWindow(); static const int REQUEST_TIMEOUT = 30000; + static const int SEARCH_INTERVAL = 2000; signals: void itemSelected(Source::Result const& result); @@ -43,6 +44,9 @@ public slots: void search(SearchDialog::SearchDetails& details); virtual void setVisible(bool visible); +protected: + virtual void timerEvent(QTimerEvent* event); + private slots: void resultAvailable(Source::Result const& result, Source::SearchDetails const& details); void requestFinished(QVector const& results, Source::SearchDetails const& details, bool error); @@ -53,6 +57,8 @@ private: Source* source_; QListWidget* list_; ConnectionManager* connectionManager_; + int timer_; + bool searching_; }; -- 1.7.9.5