contactengine/contactsengine.cpp
changeset 23 51fcdd1558d8
parent 19 e4b6ee329501
child 25 adbe71832e2b
equal deleted inserted replaced
22:d5911f90500d 23:51fcdd1558d8
     1 #include <QContact>
     1 #include <QContact>
     2 #include <QContactAddress>
     2 #include <QContactAddress>
       
     3 #include <QContactName>
     3 #include <QContactOrganization>
     4 #include <QContactOrganization>
     4 #include <QContactPhoneNumber>
     5 #include <QContactPhoneNumber>
     5 
     6 
     6 #include "contactsengine.h"
     7 #include "contactsengine.h"
     7 
     8 
       
     9 #include <QDebug>
       
    10 
     8 using namespace QtMobility;
    11 using namespace QtMobility;
     9 
    12 
    10 ContactsEngine::ContactsEngine(QObject *parent) :
    13 ContactsEngine::ContactsEngine(QObject *parent) :
    11     QAbstractListModel(parent)
    14     QAbstractListModel(parent)
    12 {
    15 {
    13     this->m_manager = 0;
    16     this->m_manager =0 ;
    14 }
    17 }
       
    18 
    15 ContactsEngine::~ContactsEngine()
    19 ContactsEngine::~ContactsEngine()
    16 {
    20 {
    17     QList<QContactManager*> initialisedManagers = m_initialisedManagers.values();
    21 
    18     while (!initialisedManagers.isEmpty()) {
    22 }
    19         QContactManager *deleteMe = initialisedManagers.takeFirst();
    23 
    20         delete deleteMe;
    24 void ContactsEngine::setManager(const QString & aMgrName)
    21     }
       
    22 }
       
    23 
       
    24 void ContactsEngine::setManager(QString aMgrName)
       
    25 {
    25 {
    26     QString managerUri = m_availableManagers.value(aMgrName);
    26     QString managerUri = m_availableManagers.value(aMgrName);
    27 
    27 
    28     // first, check to see if they reselected the same backend.
    28     // first, check to see if they reselected the same backend.
    29     if (m_manager && m_manager->managerUri() == managerUri)
    29     if (m_manager && m_manager->managerUri() == managerUri)
    41             m_manager = 0;
    41             m_manager = 0;
    42             return;
    42             return;
    43         }
    43         }
    44         m_initialisedManagers.insert(managerUri, m_manager);
    44         m_initialisedManagers.insert(managerUri, m_manager);
    45     }
    45     }
    46 
    46 //    qDebug() << "Dump Object: " << endl;
    47     // compute a new list of contact names
    47 //    m_manager->dumpObjectInfo(); // from QObject
    48     QStringList displayNames;
    48     this->dumpContactMgr(); // private helper function
    49     QList<QContact> c = this->m_manager->contacts();
       
    50 
       
    51     // signal that the manager has changed.
       
    52 
       
    53     // emit managerChanged(m_manager);
       
    54 
       
    55     // and... rebuild the list.
       
    56     // rebuildList(m_currentFilter);
       
    57 }
    49 }
    58 
    50 
    59 void ContactsEngine::populateAddresses()
    51 void ContactsEngine::populateAddresses()
    60 {
    52 {
    61     QContact *contact;
    53     QContact *contact;
   263 }
   255 }
   264 
   256 
   265 int ContactsEngine::rowCount(const QModelIndex &parent) const
   257 int ContactsEngine::rowCount(const QModelIndex &parent) const
   266  {
   258  {
   267     QList<QContact> allContacts;
   259     QList<QContact> allContacts;
   268     allContacts = this->m_manager->contacts();
   260     int rc = 0;
   269     // return stringList.count();
   261     if (this->m_manager)
   270     return allContacts.count();
   262     {
       
   263         allContacts = this->m_manager->contacts();
       
   264         // return stringList.count();
       
   265         if (!allContacts.isEmpty())
       
   266             rc = allContacts.count();
       
   267     }
       
   268     return rc;
   271  }
   269  }
       
   270 
       
   271 void ContactsEngine::enumerateMgrs()
       
   272 {
       
   273     QStringList mgrs = QContactManager::availableManagers();
       
   274     qDebug() << "Enumerate available Contact Managers:" << endl;
       
   275     foreach(QString m, mgrs)
       
   276         qDebug() << "\tmgr: " << m << endl;
       
   277     qDebug() << endl;
       
   278 }
       
   279 
       
   280 // dump the current contact manager.
       
   281 void  ContactsEngine::dumpContactMgr()
       
   282 {
       
   283     qDebug() << "Dump Contact Manager:" << endl;
       
   284     qDebug() << "\tname: " << this->m_manager->managerName() << endl;
       
   285     qDebug() << "\tURI: "  << this->m_manager->managerUri() << endl;
       
   286     qDebug() << "\tVersion: "  << this->m_manager->managerVersion() << endl;
       
   287     qDebug() << "\tCount:" << this->m_manager->contacts().count() << endl;
       
   288     qDebug() << endl;
       
   289 }
   272 
   290 
   273 QVariant ContactsEngine::data(const QModelIndex &index, int role) const
   291 QVariant ContactsEngine::data(const QModelIndex &index, int role) const
   274  {
   292  {
   275     QVariant rc;
   293     QVariant rc;
   276     QList<QContact> allContacts;
   294     QList<QContact> allContacts;
       
   295     QContact c;
   277 
   296 
   278     allContacts = this->m_manager->contacts();
   297     allContacts = this->m_manager->contacts();
   279 
   298 
   280     if (!index.isValid() || index.row() >= allContacts.size()) {
   299     if (!index.isValid() || index.row() >= allContacts.size()) {
   281          return rc;
   300          return rc;
   282     }
   301     }
   283     if (role == Qt::DisplayRole) {
   302     if (role == Qt::DisplayRole) {
   284         rc = QVariant(allContacts.at(index.row()).displayLabel());
   303         //rc = QVariant(allContacts.at(index.row()).displayLabel());
       
   304         c = allContacts.at(index.row());
       
   305         // organizations do not have first and last names.  So the displayLabel() is empty.
       
   306         QContactDetail cd = c.detail(QContactName::DefinitionName);
       
   307         if (cd.isEmpty()) {
       
   308             rc = QVariant (c.detail(QContactOrganization::DefinitionName).value(QContactOrganization::FieldDepartment) );
       
   309         } else {
       
   310             rc = QVariant ( c.displayLabel());
       
   311         }
   285     }
   312     }
   286     return rc;
   313     return rc;
   287  }
   314  }