contactengine/contactsengine.cpp
changeset 25 adbe71832e2b
parent 23 51fcdd1558d8
child 29 a9f1d0e21384
equal deleted inserted replaced
24:2e833c2a6782 25:adbe71832e2b
     2 #include <QContactAddress>
     2 #include <QContactAddress>
     3 #include <QContactName>
     3 #include <QContactName>
     4 #include <QContactOrganization>
     4 #include <QContactOrganization>
     5 #include <QContactPhoneNumber>
     5 #include <QContactPhoneNumber>
     6 
     6 
       
     7 
     7 #include "contactsengine.h"
     8 #include "contactsengine.h"
     8 
     9 
     9 #include <QDebug>
    10 #include <QDebug>
    10 
    11 
    11 using namespace QtMobility;
    12 using namespace QtMobility;
    12 
    13 
    13 ContactsEngine::ContactsEngine(QObject *parent) :
    14 ContactsEngine::ContactsEngine(QObject *parent) :
    14     QAbstractListModel(parent)
    15     QAbstractListModel(parent)
    15 {
    16 {
    16     this->m_manager =0 ;
    17     this->m_manager = 0;
    17 }
    18 }
    18 
    19 
    19 ContactsEngine::~ContactsEngine()
    20 ContactsEngine::~ContactsEngine()
    20 {
    21 {
    21 
    22 
    22 }
    23 }
    23 
    24 
    24 void ContactsEngine::setManager(const QString & aMgrName)
    25 //void ContactsEngine::createManager()
    25 {
    26 //{
    26     QString managerUri = m_availableManagers.value(aMgrName);
    27 //    // this->m_manager = new QContactManager("symbian");
    27 
    28 //    // let's try the default.
    28     // first, check to see if they reselected the same backend.
    29 //    this->m_manager = new QContactManager();
    29     if (m_manager && m_manager->managerUri() == managerUri)
    30 //}
    30         return;
    31 
    31 
    32 void ContactsEngine::createManager()
    32     // the change is real.  update.
    33 {
    33     if (m_initialisedManagers.contains(managerUri)) {
    34     // adapted from http://wiki.forum.nokia.com/index.php/Finding_contact_manager_in_Qt
    34         m_manager = m_initialisedManagers.value(managerUri);
    35 
    35     } else {
    36     // Get list of different contact back-ends
    36         m_manager = QContactManager::fromUri(managerUri);
    37     QStringList availableManagers = QContactManager::availableManagers();
    37         if (m_manager->error()) {
    38 
    38             // todo switch to qDebug()
    39     QList<QContactLocalId> contactIds;
    39             // QMessageBox::information(this, tr("Failed!"), QString("Failed to open store!\n(error code %1)").arg(m_manager->error()));
    40     // Try to find contacts from some back-end
       
    41     while (!availableManagers.isEmpty()) {
       
    42         // Get some manager
       
    43         m_manager = new QContactManager(availableManagers.first());
       
    44         availableManagers.removeFirst();
       
    45 
       
    46         // Contacts exists?
       
    47         contactIds = m_manager->contactIds();
       
    48         if (!contactIds.isEmpty()) {
       
    49             // Contact found
       
    50             availableManagers.clear();
       
    51             break;
       
    52         }
       
    53         else {
       
    54             // Not found, try the next manager
    40             delete m_manager;
    55             delete m_manager;
    41             m_manager = 0;
    56             m_manager = 0;
    42             return;
       
    43         }
    57         }
    44         m_initialisedManagers.insert(managerUri, m_manager);
    58     }
    45     }
    59 
    46 //    qDebug() << "Dump Object: " << endl;
    60     // Use default if no contact found from any back-end
    47 //    m_manager->dumpObjectInfo(); // from QObject
    61     if (!m_manager) {
    48     this->dumpContactMgr(); // private helper function
    62         m_manager = new QContactManager();
       
    63     }
       
    64 
       
    65     // Show message to the user
       
    66     QString msg = QString("Manager %1 created, that has %2 contacts")
       
    67                   .arg(m_manager->managerName()).arg(contactIds.count());
       
    68 
       
    69    // emit errorOccurred(msg);
       
    70 }
       
    71 
       
    72 
       
    73 void ContactsEngine::setManager(const QString & aMgrName)
       
    74 {
       
    75    // noop
    49 }
    76 }
    50 
    77 
    51 void ContactsEngine::populateAddresses()
    78 void ContactsEngine::populateAddresses()
    52 {
    79 {
    53     QContact *contact;
    80     QContact *contact;
   271 void ContactsEngine::enumerateMgrs()
   298 void ContactsEngine::enumerateMgrs()
   272 {
   299 {
   273     QStringList mgrs = QContactManager::availableManagers();
   300     QStringList mgrs = QContactManager::availableManagers();
   274     qDebug() << "Enumerate available Contact Managers:" << endl;
   301     qDebug() << "Enumerate available Contact Managers:" << endl;
   275     foreach(QString m, mgrs)
   302     foreach(QString m, mgrs)
       
   303     {
   276         qDebug() << "\tmgr: " << m << endl;
   304         qDebug() << "\tmgr: " << m << endl;
       
   305         this->dumpContactMgr();
       
   306     }
   277     qDebug() << endl;
   307     qDebug() << endl;
   278 }
   308 }
   279 
   309 
   280 // dump the current contact manager.
   310 // dump the current contact manager.
   281 void  ContactsEngine::dumpContactMgr()
   311 void  ContactsEngine::dumpContactMgr()
   282 {
   312 {
   283     qDebug() << "Dump Contact Manager:" << endl;
   313     qDebug() << "Dump Contact Manager:" << endl;
   284     qDebug() << "\tname: " << this->m_manager->managerName() << endl;
   314     if (this->m_manager) {
   285     qDebug() << "\tURI: "  << this->m_manager->managerUri() << endl;
   315         qDebug() << "\tname: " << this->m_manager->managerName() << endl;
   286     qDebug() << "\tVersion: "  << this->m_manager->managerVersion() << endl;
   316         qDebug() << "\tURI: "  << this->m_manager->managerUri() << endl;
   287     qDebug() << "\tCount:" << this->m_manager->contacts().count() << endl;
   317         qDebug() << "\tVersion: "  << this->m_manager->managerVersion() << endl;
       
   318         qDebug() << "\tCount:" << this->m_manager->contacts().count() << endl;
       
   319     } else {
       
   320         qDebug() << "\t Contact Manager set to zero." << endl;
       
   321     }
   288     qDebug() << endl;
   322     qDebug() << endl;
   289 }
   323 }
   290 
   324 
   291 QVariant ContactsEngine::data(const QModelIndex &index, int role) const
   325 QVariant ContactsEngine::data(const QModelIndex &index, int role) const
   292  {
   326  {