Some fixes to connection manager.
authoreshe <jessehakanen@gmail.com>
Fri, 20 Aug 2010 18:21:39 +0000 (19:21 +0100)
committereshe <jessehakanen@gmail.com>
Fri, 20 Aug 2010 18:21:39 +0000 (19:21 +0100)
src/common/connectionmanager.cpp
src/common/connectionmanager.h
src/common/contactmanager.cpp
src/common/mobil1881.cpp
src/common/source.cpp
src/daemon/calllistener.cpp
src/gui/resultwindow.cpp
src/gui/resultwindow.h

index fb0f395..f7b0f32 100644 (file)
@@ -77,6 +77,12 @@ bool ConnectionManager::connect()
     icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
 
     waitSignal(&connectionReady_);
     icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
 
     waitSignal(&connectionReady_);
+
+    if(connected_)
+    {
+        sleep(WAIT_AFTER_CONNECT);
+    }
+
     return connected_;
 }
 
     return connected_;
 }
 
@@ -121,6 +127,8 @@ bool ConnectionManager::connect(QString const& id)
 
     connected_ = true;
 
 
     connected_ = true;
 
+    sleep(WAIT_AFTER_CONNECT);
+
     return true;
 }
 
     return true;
 }
 
@@ -421,15 +429,31 @@ bool ConnectionManager::waitSignal(bool* ready)
 
     killTimer(timer_);
 
 
     killTimer(timer_);
 
+    if(timeout_)
+    {
+        qDebug() << "Connection request timed out";
+    }
+
     return *ready || !timeout_;
 }
 
     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;
 void ConnectionManager::timerEvent(QTimerEvent* event)
 {
     Q_UNUSED(event);
     killTimer(timer_);
     timeout_ = true;
     timer_ = 0;
-
-    qDebug() << "Connection request timed out";
 }
 }
index 34ec24c..b5a2c3c 100644 (file)
@@ -55,6 +55,7 @@ public:
     bool scanConnections(QList<Connection>& connections, ConnectionType type = NO_TYPE);
     Error error() const;
     static unsigned int const TIMEOUT = 15000;
     bool scanConnections(QList<Connection>& 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);
 
 protected:
     virtual void timerEvent(QTimerEvent* event);
@@ -65,6 +66,7 @@ private slots:
     void scanResult(const QDBusMessage& rep);
 
 private:
     void scanResult(const QDBusMessage& rep);
 
 private:
+    void sleep(unsigned int ms);
     bool waitSignal(bool* ready);
     bool stateReady_;
     bool connectionReady_;
     bool waitSignal(bool* ready);
     bool stateReady_;
     bool connectionReady_;
index f9047b5..fc0c62d 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <QtCore/QDebug>
  */
 
 #include <QtCore/QDebug>
+#include <QtCore/QRegExp>
 #include "contactmanager.h"
 
 
 #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();
 
     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);
 
     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)
     {
 
     if(ename)
     {
+        static QRegExp check("([A-Z]+)");
         QString additional = QString::fromUtf8(ename->additional);
         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
         {
         }
         else
         {
index 6ded070..69327b0 100644 (file)
@@ -95,7 +95,6 @@ void Mobil1881::handleHttpError(int id)
         if(pendingSearches_.at(i) && pendingSearches_.at(i)->searchIds.find(id) !=
             pendingSearches_.at(i)->searchIds.end())
         {
         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;
             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)
 {
                            QString const& data,
                            int index)
 {
+    if(data.isEmpty())
+    {
+        qDebug() << "Server returned no data";
+        qDebug() << "Headers: " << http_.lastResponse().toString();
+    }
+
     if(data.indexOf("<b>Last ned vCard</b>") > 0)
     {
         addOnlyNumber(searchData, data, index);
     if(data.indexOf("<b>Last ned vCard</b>") > 0)
     {
         addOnlyNumber(searchData, data, index);
index 5d1315e..0109f4a 100644 (file)
@@ -17,6 +17,7 @@
  */
 
 #include <QtCore/QDebug>
  */
 
 #include <QtCore/QDebug>
+#include <QtNetwork/QHttpResponseHeader>
 #include "source.h"
 #include "eniro.h"
 #include "mobil1881.h"
 #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)
 {
 
 void Source::httpReady(int id, bool error)
 {
+    QByteArray result = http_.readAll();
+
     if(error)
     {
         if(http_.error() == QHttp::Aborted)
     if(error)
     {
         if(http_.error() == QHttp::Aborted)
@@ -193,7 +196,6 @@ void Source::httpReady(int id, bool error)
     }
     else
     {
     }
     else
     {
-        QByteArray result = http_.readAll();
         handleHttpData(id, result);
     }
 }
         handleHttpData(id, result);
     }
 }
index e73bf26..c76cfdc 100644 (file)
@@ -526,7 +526,6 @@ bool CallListener::handleConnection()
 
         if(cm.connect(best.id))
         {
 
         if(cm.connect(best.id))
         {
-            sleep(500);
             break;
         }
         else if(cm.error() == ConnectionManager::INVALID_IAP)
             break;
         }
         else if(cm.error() == ConnectionManager::INVALID_IAP)
index e9da90a..dc1509f 100644 (file)
@@ -30,7 +30,7 @@
 #include "sourcecoreconfig.h"
 
 ResultWindow::ResultWindow(QWidget* parent): QMainWindow(parent),
 #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"));
 {
     setAttribute(Qt::WA_Maemo5StackedWindow);
     setWindowTitle(tr("Search results"));
@@ -56,6 +56,16 @@ void ResultWindow::search(SearchDialog::SearchDetails& details)
         list_->clear();
     }
 
         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_)
     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;
 
     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));
 
 }
     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)
 {
 void ResultWindow::resultAvailable(Source::Result const& result,
                                    Source::SearchDetails const& details)
 {
+    if(!list_)
+    {
+        return;
+    }
+
     Q_UNUSED(details);
 
     if(!result.number.isEmpty())
     Q_UNUSED(details);
 
     if(!result.number.isEmpty())
@@ -180,6 +197,14 @@ void ResultWindow::requestFinished(QVector <Source::Result> const& results,
 
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 
 
     setAttribute(Qt::WA_Maemo5ShowProgressIndicator, false);
 
+    if(timer_)
+    {
+        killTimer(timer_);
+    }
+
+    timer_ = startTimer(SEARCH_INTERVAL);
+    searching_ = false;
+
 }
 
 void ResultWindow::itemClicked(QListWidgetItem* item)
 }
 
 void ResultWindow::itemClicked(QListWidgetItem* item)
@@ -197,15 +222,20 @@ void ResultWindow::itemClicked(QListWidgetItem* item)
 
 void ResultWindow::setVisible(bool visible)
 {
 
 void ResultWindow::setVisible(bool visible)
 {
-    QMainWindow::setVisible(visible);
-
     if(!visible && source_)
     {
         source_->abort();
     if(!visible && source_)
     {
         source_->abort();
-
-
     }
     }
+
+    QMainWindow::setVisible(visible);
 }
 
 }
 
+void ResultWindow::timerEvent(QTimerEvent* event)
+{
+    Q_UNUSED(event);
+
+    killTimer(timer_);
+    timer_ = 0;
+}
 
 
 
 
index 1fff79a..3391f7d 100644 (file)
@@ -35,6 +35,7 @@ public:
     ResultWindow(QWidget* parent = 0);
     ~ResultWindow();
     static const int REQUEST_TIMEOUT = 30000;
     ResultWindow(QWidget* parent = 0);
     ~ResultWindow();
     static const int REQUEST_TIMEOUT = 30000;
+    static const int SEARCH_INTERVAL = 2000;
 
 signals:
     void itemSelected(Source::Result const& result);
 
 signals:
     void itemSelected(Source::Result const& result);
@@ -43,6 +44,9 @@ public slots:
     void search(SearchDialog::SearchDetails& details);
     virtual void setVisible(bool visible);
 
     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 <Source::Result> const& results, Source::SearchDetails const& details, bool error);
 private slots:
     void resultAvailable(Source::Result const& result, Source::SearchDetails const& details);
     void requestFinished(QVector <Source::Result> const& results, Source::SearchDetails const& details, bool error);
@@ -53,6 +57,8 @@ private:
     Source* source_;
     QListWidget* list_;
     ConnectionManager* connectionManager_;
     Source* source_;
     QListWidget* list_;
     ConnectionManager* connectionManager_;
+    int timer_;
+    bool searching_;
 
 };
 
 
 };