runs on-device now; testing on 5800
authorJohn Kern <johnk@symbian.org>
Thu, 12 Aug 2010 10:49:23 -0700
changeset 25 adbe71832e2b
parent 24 2e833c2a6782
child 26 d9f0df25b6a3
runs on-device now; testing on 5800
contactengine/contactengine.pro
contactengine/contactsengine.cpp
contactengine/contactsengine.h
contactengine/mainwindow.cpp
contactengine/mainwindow.h
--- a/contactengine/contactengine.pro	Wed Aug 11 20:03:29 2010 +0100
+++ b/contactengine/contactengine.pro	Thu Aug 12 10:49:23 2010 -0700
@@ -4,7 +4,7 @@
 #
 #-------------------------------------------------
 
-QT       += core gui webkit sql
+QT       += core gui sql
 
 TARGET = contactengine
 TEMPLATE = app
@@ -22,6 +22,7 @@
     dbtools.h \
     database.h
 
+
 FORMS    += mainwindow.ui
 
 CONFIG += mobility
@@ -34,7 +35,7 @@
 
 symbian {
     TARGET.UID3 = 0xec200759
-    # TARGET.CAPABILITY += 
+    TARGET.CAPABILITY += ReadUserData WriteUserData
     TARGET.EPOCSTACKSIZE = 0x14000
     TARGET.EPOCHEAPSIZE = 0x020000 0x800000
 }
--- a/contactengine/contactsengine.cpp	Wed Aug 11 20:03:29 2010 +0100
+++ b/contactengine/contactsengine.cpp	Thu Aug 12 10:49:23 2010 -0700
@@ -4,6 +4,7 @@
 #include <QContactOrganization>
 #include <QContactPhoneNumber>
 
+
 #include "contactsengine.h"
 
 #include <QDebug>
@@ -13,7 +14,7 @@
 ContactsEngine::ContactsEngine(QObject *parent) :
     QAbstractListModel(parent)
 {
-    this->m_manager =0 ;
+    this->m_manager = 0;
 }
 
 ContactsEngine::~ContactsEngine()
@@ -21,31 +22,57 @@
 
 }
 
+//void ContactsEngine::createManager()
+//{
+//    // this->m_manager = new QContactManager("symbian");
+//    // let's try the default.
+//    this->m_manager = new QContactManager();
+//}
+
+void ContactsEngine::createManager()
+{
+    // adapted from http://wiki.forum.nokia.com/index.php/Finding_contact_manager_in_Qt
+
+    // Get list of different contact back-ends
+    QStringList availableManagers = QContactManager::availableManagers();
+
+    QList<QContactLocalId> contactIds;
+    // Try to find contacts from some back-end
+    while (!availableManagers.isEmpty()) {
+        // Get some manager
+        m_manager = new QContactManager(availableManagers.first());
+        availableManagers.removeFirst();
+
+        // Contacts exists?
+        contactIds = m_manager->contactIds();
+        if (!contactIds.isEmpty()) {
+            // Contact found
+            availableManagers.clear();
+            break;
+        }
+        else {
+            // Not found, try the next manager
+            delete m_manager;
+            m_manager = 0;
+        }
+    }
+
+    // Use default if no contact found from any back-end
+    if (!m_manager) {
+        m_manager = new QContactManager();
+    }
+
+    // Show message to the user
+    QString msg = QString("Manager %1 created, that has %2 contacts")
+                  .arg(m_manager->managerName()).arg(contactIds.count());
+
+   // emit errorOccurred(msg);
+}
+
+
 void ContactsEngine::setManager(const QString & aMgrName)
 {
-    QString managerUri = m_availableManagers.value(aMgrName);
-
-    // first, check to see if they reselected the same backend.
-    if (m_manager && m_manager->managerUri() == managerUri)
-        return;
-
-    // the change is real.  update.
-    if (m_initialisedManagers.contains(managerUri)) {
-        m_manager = m_initialisedManagers.value(managerUri);
-    } else {
-        m_manager = QContactManager::fromUri(managerUri);
-        if (m_manager->error()) {
-            // todo switch to qDebug()
-            // QMessageBox::information(this, tr("Failed!"), QString("Failed to open store!\n(error code %1)").arg(m_manager->error()));
-            delete m_manager;
-            m_manager = 0;
-            return;
-        }
-        m_initialisedManagers.insert(managerUri, m_manager);
-    }
-//    qDebug() << "Dump Object: " << endl;
-//    m_manager->dumpObjectInfo(); // from QObject
-    this->dumpContactMgr(); // private helper function
+   // noop
 }
 
 void ContactsEngine::populateAddresses()
@@ -273,7 +300,10 @@
     QStringList mgrs = QContactManager::availableManagers();
     qDebug() << "Enumerate available Contact Managers:" << endl;
     foreach(QString m, mgrs)
+    {
         qDebug() << "\tmgr: " << m << endl;
+        this->dumpContactMgr();
+    }
     qDebug() << endl;
 }
 
@@ -281,10 +311,14 @@
 void  ContactsEngine::dumpContactMgr()
 {
     qDebug() << "Dump Contact Manager:" << endl;
-    qDebug() << "\tname: " << this->m_manager->managerName() << endl;
-    qDebug() << "\tURI: "  << this->m_manager->managerUri() << endl;
-    qDebug() << "\tVersion: "  << this->m_manager->managerVersion() << endl;
-    qDebug() << "\tCount:" << this->m_manager->contacts().count() << endl;
+    if (this->m_manager) {
+        qDebug() << "\tname: " << this->m_manager->managerName() << endl;
+        qDebug() << "\tURI: "  << this->m_manager->managerUri() << endl;
+        qDebug() << "\tVersion: "  << this->m_manager->managerVersion() << endl;
+        qDebug() << "\tCount:" << this->m_manager->contacts().count() << endl;
+    } else {
+        qDebug() << "\t Contact Manager set to zero." << endl;
+    }
     qDebug() << endl;
 }
 
--- a/contactengine/contactsengine.h	Wed Aug 11 20:03:29 2010 +0100
+++ b/contactengine/contactsengine.h	Thu Aug 12 10:49:23 2010 -0700
@@ -2,8 +2,8 @@
 #define CONTACTSENGINE_H
 
 #include <QAbstractListModel>
-
 #include <QContactManager>
+#include <QPointer>
 
 #include "contactsengine.h"
 
@@ -28,18 +28,17 @@
     void populateAddresses();
     void dumpContactMgr(); // use for debugging. Hard to inspect it via Qt Creator.
     void enumerateMgrs();
+    void createManager();
 
 signals:
-    void managerChanged(QStringList containNames);
+    void errorOccurred(QString errMsg); // when a error message
 
 public slots:
     void setManager(const QString &aMgr);
 
 private:
-    QContactManager *m_manager;
     QMap<QString, QString> m_availableManagers;
-    QMap<QString, QContactManager*> m_initialisedManagers;
-
+    QPointer<QContactManager> m_manager;
 };
 
 #endif // CONTACTSENGINE_H
--- a/contactengine/mainwindow.cpp	Wed Aug 11 20:03:29 2010 +0100
+++ b/contactengine/mainwindow.cpp	Thu Aug 12 10:49:23 2010 -0700
@@ -1,4 +1,7 @@
 #include <QComboBox>
+#include <QMessageBox>
+
+#include <QDebug>
 
 #include "mainwindow.h"
 #include "ui_mainwindow.h"
@@ -10,20 +13,24 @@
 {
     ui->setupUi(this);
     this->ce = new ContactsEngine(this);
-  //  this->ce->enumerateMgrs();
-    this->ce->setManager(QString("memory"));
+
+    connect(this->ce, SIGNAL(errorOccurred(QString)),
+            this,SLOT(errorOccurred(QString)));
 
     connect(ui->comboBox, SIGNAL( activated ( const QString &  )),
             this->ce, SLOT(setManager(const QString &) ));
 
+    this->ce->createManager();
+
     ui->comboBox->addItems(this->ce->dataSources());
-    this->ce->populateAddresses();
- //   this->ce->enumerateMgrs();
-  //  this->ce->dumpContactMgr();
     ui->listView->setModel(this->ce);
 }
 
-
+void MainWindow::errorOccurred(QString errMsg)
+{
+    QMessageBox::warning(this,"Err Routed",errMsg);
+    qDebug() << errMsg << endl;
+}
 
 MainWindow::~MainWindow()
 {
--- a/contactengine/mainwindow.h	Wed Aug 11 20:03:29 2010 +0100
+++ b/contactengine/mainwindow.h	Thu Aug 12 10:49:23 2010 -0700
@@ -17,6 +17,7 @@
     ~MainWindow();
 
 public slots:
+    void errorOccurred(QString errMsg);
 
 private:
     Ui::MainWindow *ui;