contactengine/contactsengine.cpp
changeset 19 e4b6ee329501
child 23 51fcdd1558d8
equal deleted inserted replaced
18:5707b75503d5 19:e4b6ee329501
       
     1 #include <QContact>
       
     2 #include <QContactAddress>
       
     3 #include <QContactOrganization>
       
     4 #include <QContactPhoneNumber>
       
     5 
       
     6 #include "contactsengine.h"
       
     7 
       
     8 using namespace QtMobility;
       
     9 
       
    10 ContactsEngine::ContactsEngine(QObject *parent) :
       
    11     QAbstractListModel(parent)
       
    12 {
       
    13     this->m_manager = 0;
       
    14 }
       
    15 ContactsEngine::~ContactsEngine()
       
    16 {
       
    17     QList<QContactManager*> initialisedManagers = m_initialisedManagers.values();
       
    18     while (!initialisedManagers.isEmpty()) {
       
    19         QContactManager *deleteMe = initialisedManagers.takeFirst();
       
    20         delete deleteMe;
       
    21     }
       
    22 }
       
    23 
       
    24 void ContactsEngine::setManager(QString aMgrName)
       
    25 {
       
    26     QString managerUri = m_availableManagers.value(aMgrName);
       
    27 
       
    28     // first, check to see if they reselected the same backend.
       
    29     if (m_manager && m_manager->managerUri() == managerUri)
       
    30         return;
       
    31 
       
    32     // the change is real.  update.
       
    33     if (m_initialisedManagers.contains(managerUri)) {
       
    34         m_manager = m_initialisedManagers.value(managerUri);
       
    35     } else {
       
    36         m_manager = QContactManager::fromUri(managerUri);
       
    37         if (m_manager->error()) {
       
    38             // todo switch to qDebug()
       
    39             // QMessageBox::information(this, tr("Failed!"), QString("Failed to open store!\n(error code %1)").arg(m_manager->error()));
       
    40             delete m_manager;
       
    41             m_manager = 0;
       
    42             return;
       
    43         }
       
    44         m_initialisedManagers.insert(managerUri, m_manager);
       
    45     }
       
    46 
       
    47     // compute a new list of contact names
       
    48     QStringList displayNames;
       
    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 }
       
    58 
       
    59 void ContactsEngine::populateAddresses()
       
    60 {
       
    61     QContact *contact;
       
    62     QContactOrganization *dept;
       
    63     QStringList departmentName;
       
    64     QContactPhoneNumber *number;
       
    65     QContactAddress *address;
       
    66 
       
    67     // this source is open source. Do not put private numbers in here.
       
    68 
       
    69     // emergency numbers
       
    70     // fire Foster City
       
    71     contact = new QContact();
       
    72 
       
    73     dept = new QContactOrganization();
       
    74     departmentName << "Foster City Fire department";
       
    75     dept->setDepartment(departmentName);
       
    76     contact->saveDetail(dept);
       
    77 
       
    78     number = new QContactPhoneNumber();
       
    79     number->setContexts(QContactDetail::ContextWork);
       
    80     number->setNumber("+1-650-286-3350?");
       
    81     contact->saveDetail(number);
       
    82     //fire.setPreferredDetail("DialAction",number); //doesn't compile. Has it been depreciated?
       
    83 
       
    84     // create address detail
       
    85     address = new QContactAddress();
       
    86     address->setCountry("USA");
       
    87     address->setRegion("CA");
       
    88     address->setLocality("Foster City"); // RFC 2426 - defines the difference locality and region.
       
    89     address->setStreet("1040 East Hillsdale Boulevard");
       
    90     address->setPostcode("94404");
       
    91     contact->saveDetail(address);
       
    92 
       
    93     this->m_manager->saveContact(contact);
       
    94     // Note that the caller retains ownership of the detail.
       
    95     delete dept;
       
    96     while (!departmentName.isEmpty())
       
    97         departmentName.takeFirst();
       
    98     delete number;
       
    99     delete address;
       
   100     delete contact;
       
   101 
       
   102     // police - foster city
       
   103     contact = new QContact();
       
   104 
       
   105     dept = new QContactOrganization();
       
   106 
       
   107     departmentName << "Foster City Police department";
       
   108     dept->setDepartment(departmentName);
       
   109     contact->saveDetail(dept);
       
   110 
       
   111     number = new QContactPhoneNumber();
       
   112     number->setContexts(QContactDetail::ContextWork);
       
   113     number->setNumber("+1(650)286-3300?");
       
   114     contact->saveDetail(number);
       
   115 
       
   116     // create address detail
       
   117     address = new QContactAddress();
       
   118     address->setCountry("USA");
       
   119     address->setRegion("CA");
       
   120     address->setLocality("Foster City");
       
   121     address->setStreet("1030 East Hillsdale Boulevard");
       
   122     address->setPostcode("94404");
       
   123     contact->saveDetail(address);
       
   124 
       
   125     this->m_manager->saveContact(contact);
       
   126     delete dept;
       
   127     while (!departmentName.isEmpty())
       
   128         departmentName.takeFirst();
       
   129     delete number;
       
   130     delete address;
       
   131     delete contact;
       
   132 
       
   133     // taxi - Foster City
       
   134     contact = new QContact();
       
   135 
       
   136     dept = new QContactOrganization();
       
   137     departmentName << "Foster City Yellow Cab";
       
   138     dept->setDepartment(departmentName);
       
   139     contact->saveDetail(dept);
       
   140 
       
   141     number = new QContactPhoneNumber();
       
   142     number->setContexts(QContactDetail::ContextWork);
       
   143     number->setNumber("+1(650)367-9999?");
       
   144     contact->saveDetail(number);
       
   145 
       
   146     // create address detail
       
   147     address = new QContactAddress();
       
   148     address->setCountry("USA");
       
   149     address->setRegion("CA");
       
   150     address->setLocality("Foster City");
       
   151     address->setStreet("1055 foster city blvd");
       
   152     address->setPostcode("94404");
       
   153     contact->saveDetail(address);
       
   154 
       
   155     this->m_manager->saveContact(contact);
       
   156     delete dept;
       
   157     while (!departmentName.isEmpty())
       
   158         departmentName.takeFirst();
       
   159     delete number;
       
   160     delete address;
       
   161     delete contact;
       
   162 
       
   163     // fire  - Menlo Park
       
   164     contact = new QContact();
       
   165 
       
   166     dept = new QContactOrganization();
       
   167     departmentName << "Menlo Park fire department";
       
   168     dept->setDepartment(departmentName);
       
   169     contact->saveDetail(dept);
       
   170 
       
   171     number = new QContactPhoneNumber();
       
   172     number->setContexts(QContactDetail::ContextWork);
       
   173     number->setNumber("+1(650)688-8400??");
       
   174     contact->saveDetail(number);
       
   175 
       
   176     // create address detail
       
   177     address = new QContactAddress();
       
   178     address->setCountry("USA");
       
   179     address->setRegion("CA");
       
   180     address->setLocality("Menlo Park");
       
   181     address->setStreet("170 Middlefield Road");
       
   182     address->setPostcode("94025");
       
   183     contact->saveDetail(address);
       
   184 
       
   185     this->m_manager->saveContact(contact);
       
   186     delete dept;
       
   187     while (!departmentName.isEmpty())
       
   188         departmentName.takeFirst();
       
   189     delete number;
       
   190     delete address;
       
   191     delete contact;
       
   192 
       
   193     //701 Laurel Street, Menlo Park, CA? -
       
   194     // police  - Menlo Park
       
   195     contact = new QContact();
       
   196 
       
   197     dept = new QContactOrganization();
       
   198     departmentName << "Menlo Park police department";
       
   199     dept->setDepartment(departmentName);
       
   200     contact->saveDetail(dept);
       
   201 
       
   202     number = new QContactPhoneNumber();
       
   203     number->setContexts(QContactDetail::ContextWork);
       
   204     number->setNumber("+1(650)858-3328???");
       
   205     contact->saveDetail(number);
       
   206 
       
   207     // create address detail
       
   208     address = new QContactAddress();
       
   209     address->setCountry("USA");
       
   210     address->setRegion("CA");
       
   211     address->setLocality("Menlo Park");
       
   212     address->setStreet("701 Laurel Street");
       
   213     address->setPostcode("94025");
       
   214     contact->saveDetail(address);
       
   215 
       
   216     this->m_manager->saveContact(contact);
       
   217     delete dept;
       
   218     while (!departmentName.isEmpty())
       
   219         departmentName.takeFirst();
       
   220     delete number;
       
   221     delete address;
       
   222     delete contact;
       
   223 }
       
   224 
       
   225 
       
   226 
       
   227 QStringList ContactsEngine::dataSources()
       
   228 {
       
   229      QStringList availableManagers = QContactManager::availableManagers();
       
   230      availableManagers.removeAll("invalid");
       
   231      foreach(QString managerName, availableManagers) {
       
   232 
       
   233          QMap<QString, QString> params;
       
   234          QString managerUri = QContactManager::buildUri(managerName, params);
       
   235 
       
   236          // Add some parameters to SIM backend so that we can use
       
   237          // all the stores.
       
   238          if (managerName == "symbiansim") {
       
   239              availableManagers.removeAll("symbiansim");
       
   240 
       
   241              availableManagers.append("symbiansim:adn");
       
   242              params.insert("store", "ADN");
       
   243              managerUri = QContactManager::buildUri(managerName, params);
       
   244              m_availableManagers.insert(availableManagers.last(), managerUri);
       
   245 
       
   246              availableManagers.append("symbiansim:fdn");
       
   247              params.clear();
       
   248              params.insert("store", "FDN");
       
   249              managerUri = QContactManager::buildUri(managerName, params);
       
   250              m_availableManagers.insert(availableManagers.last(), managerUri);
       
   251 
       
   252              availableManagers.append("symbiansim:sdn");
       
   253              params.clear();
       
   254              params.insert("store", "SDN");
       
   255              managerUri = QContactManager::buildUri(managerName, params);
       
   256              m_availableManagers.insert(availableManagers.last(), managerUri);
       
   257          }
       
   258          else {
       
   259              m_availableManagers.insert(managerName, managerUri);
       
   260          }
       
   261      }
       
   262      return (availableManagers);
       
   263 }
       
   264 
       
   265 int ContactsEngine::rowCount(const QModelIndex &parent) const
       
   266  {
       
   267     QList<QContact> allContacts;
       
   268     allContacts = this->m_manager->contacts();
       
   269     // return stringList.count();
       
   270     return allContacts.count();
       
   271  }
       
   272 
       
   273 QVariant ContactsEngine::data(const QModelIndex &index, int role) const
       
   274  {
       
   275     QVariant rc;
       
   276     QList<QContact> allContacts;
       
   277 
       
   278     allContacts = this->m_manager->contacts();
       
   279 
       
   280     if (!index.isValid() || index.row() >= allContacts.size()) {
       
   281          return rc;
       
   282     }
       
   283     if (role == Qt::DisplayRole) {
       
   284         rc = QVariant(allContacts.at(index.row()).displayLabel());
       
   285     }
       
   286     return rc;
       
   287  }