phonebookengines/cntlistmodel/src/cntlistmodel.cpp
changeset 65 ae724a111993
parent 47 7cbcb2896f0e
child 72 6abfb1094884
equal deleted inserted replaced
59:a642906a277a 65:ae724a111993
    72     CNT_ENTRY
    72     CNT_ENTRY
    73     m_defaultIcon = HbIcon("qtg_large_avatar");
    73     m_defaultIcon = HbIcon("qtg_large_avatar");
    74     m_defaultMyCardIcon = HbIcon("qtg_large_avatar_mycard");
    74     m_defaultMyCardIcon = HbIcon("qtg_large_avatar_mycard");
    75 
    75 
    76     d = new CntListModelData(contactFilter, showMyCard);
    76     d = new CntListModelData(contactFilter, showMyCard);
       
    77 
    77     d->m_contactManager = manager;
    78     d->m_contactManager = manager;
    78     d->m_cache = CntCache::instance();
    79     d->m_cache = CntCache::instance();
       
    80 
    79     connect(d->m_cache, SIGNAL(contactInfoUpdated(QContactLocalId)), this, SLOT(handleContactInfoUpdated(QContactLocalId)));
    81     connect(d->m_cache, SIGNAL(contactInfoUpdated(QContactLocalId)), this, SLOT(handleContactInfoUpdated(QContactLocalId)));
    80     d->m_myCardId = d->m_contactManager->selfContactId();     
    82     d->m_myCardId = d->m_contactManager->selfContactId();     
       
    83 
    81     if (doConstruct() != QContactManager::NoError) {
    84     if (doConstruct() != QContactManager::NoError) {
    82         throw("exception");
    85         throw("exception");
    83     }
    86     }
    84     CNT_EXIT
    87     CNT_EXIT
    85 }
    88 }
   118         }
   121         }
   119         d->m_currentRow = row;
   122         d->m_currentRow = row;
   120     }
   123     }
   121     
   124     
   122     if (role == Qt::DisplayRole) {
   125     if (role == Qt::DisplayRole) {
   123         return dataForDisplayRole(row);
   126         return dataForRole(row, role);
   124     }
   127     }
   125     else if (role == Hb::IndexFeedbackRole) {
   128     else if (role == Hb::IndexFeedbackRole) {
   126         if (row == 0 && (d->m_myCardId == d->m_contactIds[0] || dummyMyCardId == d->m_contactIds[0])) {
   129         if (row == 0 && (d->m_myCardId == d->m_contactIds[0] || dummyMyCardId == d->m_contactIds[0])) {
   127             // do not include MyCard in index feedback
   130             // do not include MyCard in index feedback
   128             return QVariant();
   131             return QVariant();
   129         }
   132         }
   130         else {
   133         else {
   131             return dataForDisplayRole(row).toStringList().at(0).toUpper();
   134             return dataForRole(row, role).toStringList().at(0).toUpper();
   132         }
   135         }
   133     }
   136     }
   134     else if (role == Qt::BackgroundRole) {
   137     else if (role == Qt::BackgroundRole) {
   135         if (d->m_myCardId == d->m_contactIds[row] || dummyMyCardId == d->m_contactIds[row]) {
   138         if (d->m_myCardId == d->m_contactIds[row] || dummyMyCardId == d->m_contactIds[row]) {
   136             return HbFrameBackground("qtg_fr_list_parent_normal", HbFrameDrawer::NinePieces);
   139             return HbFrameBackground("qtg_fr_list_parent_normal", HbFrameDrawer::NinePieces);
   197 {
   200 {
   198     return contact(index.row());
   201     return contact(index.row());
   199 }
   202 }
   200 
   203 
   201 /*!
   204 /*!
       
   205  * Returns the id for the contact at the requested row.
       
   206  *
       
   207  * \param index Index for the sought contact entry in this model.
       
   208  * \return The id for the contact, 0 if invalid index.
       
   209  *
       
   210  */
       
   211 QContactLocalId CntListModel::contactId(const QModelIndex &index) const
       
   212 {
       
   213     CNT_ENTRY
       
   214 
       
   215     if (!validRowId(index.row())) {
       
   216         return 0;
       
   217     }
       
   218 
       
   219     CNT_EXIT
       
   220     return d->m_contactIds[index.row()];
       
   221 }
       
   222 
       
   223 /*!
   202  * Return an index that points to the row relating to the supplied contact.
   224  * Return an index that points to the row relating to the supplied contact.
   203  *  E.g. if the contact is at row 7, the index with the following properties
   225  *  E.g. if the contact is at row 7, the index with the following properties
   204  *  is returned:
   226  *  is returned:
   205  *      index.row() == 7
   227  *      index.row() == 7
   206  
   228  
   208  * \return a QModelIndex with the row set to match that of the contact.
   230  * \return a QModelIndex with the row set to match that of the contact.
   209  */
   231  */
   210 QModelIndex CntListModel::indexOfContact(const QContact &contact) const
   232 QModelIndex CntListModel::indexOfContact(const QContact &contact) const
   211 {
   233 {
   212     return createIndex(rowId(contact.localId()), 0);
   234     return createIndex(rowId(contact.localId()), 0);
       
   235 }
       
   236 
       
   237 /*!
       
   238  * Return an index that points to the row relating to the supplied contact id.
       
   239  *  E.g. if the contact with this id is at row 7, the index with the following
       
   240  *  properties is returned:
       
   241  *      index.row() == 7
       
   242  
       
   243  * \param contactId The id of the contact for whose row an index is required
       
   244  * \return a QModelIndex with the row set to match that of the contact id.
       
   245  */
       
   246 QModelIndex CntListModel::indexOfContactId(const QContactLocalId &contactId) const
       
   247 {
       
   248     return createIndex(rowId(contactId), 0);
   213 }
   249 }
   214 
   250 
   215 /*!
   251 /*!
   216  * Return the contact manager used by this model.
   252  * Return the contact manager used by this model.
   217  *
   253  *
   373     connect(d->m_contactManager, SIGNAL(relationshipsAdded(const QList<QContactLocalId>&)), this, SLOT(handleAddedRelationship(const QList<QContactLocalId>&)));
   409     connect(d->m_contactManager, SIGNAL(relationshipsAdded(const QList<QContactLocalId>&)), this, SLOT(handleAddedRelationship(const QList<QContactLocalId>&)));
   374     connect(d->m_contactManager, SIGNAL(relationshipsRemoved(const QList<QContactLocalId>&)), this, SLOT(handleRemovedRelationship(const QList<QContactLocalId>&)));
   410     connect(d->m_contactManager, SIGNAL(relationshipsRemoved(const QList<QContactLocalId>&)), this, SLOT(handleRemovedRelationship(const QList<QContactLocalId>&)));
   375     
   411     
   376     // Get current setting how to show an item in the name list and subscribe for changes
   412     // Get current setting how to show an item in the name list and subscribe for changes
   377     d->m_Settings = new XQSettingsManager;
   413     d->m_Settings = new XQSettingsManager;
       
   414 
   378     d->m_NameListRowSettingkey = new XQSettingsKey(XQSettingsKey::TargetCentralRepository,
   415     d->m_NameListRowSettingkey = new XQSettingsKey(XQSettingsKey::TargetCentralRepository,
   379                             KCRCntSettings.iUid,
   416                             KCRCntSettings.iUid,
   380                             KCntNameListRowSetting);
   417                             KCntNameListRowSetting);
   381     d->m_currentRowSetting = d->m_Settings->readItemValue(*d->m_NameListRowSettingkey, XQSettingsManager::TypeInt).toInt();
   418     d->m_currentRowSetting = d->m_Settings->readItemValue(*d->m_NameListRowSettingkey, XQSettingsManager::TypeInt).toInt();
   382     d->m_Settings->startMonitoring(*d->m_NameListRowSettingkey, XQSettingsManager::TypeInt);
   419     d->m_Settings->startMonitoring(*d->m_NameListRowSettingkey, XQSettingsManager::TypeInt);
   383     connect(d->m_Settings, SIGNAL(valueChanged(const XQSettingsKey&, const QVariant&)), this, SLOT(handleRowSettingChanged(const XQSettingsKey&, const QVariant&)));
   420     connect(d->m_Settings, SIGNAL(valueChanged(const XQSettingsKey&, const QVariant&)), this, SLOT(handleRowSettingChanged(const XQSettingsKey&, const QVariant&)));
   384     
   421     
   385     CNT_EXIT
   422     CNT_EXIT
       
   423 
   386     return error;
   424     return error;
   387 }
   425 }
   388 
   426 
   389 /*!
   427 /*!
   390  * Initializes d->m_contactIds to contain contact Ids from the database.
   428  * Initializes d->m_contactIds to contain contact Ids from the database.
   480  *
   518  *
   481  * \param row The row of the item to return data about.
   519  * \param row The row of the item to return data about.
   482  * \param column The column of the item to return data about.
   520  * \param column The column of the item to return data about.
   483  * \return QVariant The data for the specified index.
   521  * \return QVariant The data for the specified index.
   484  */
   522  */
   485 QVariant CntListModel::dataForDisplayRole(int row) const
   523 QVariant CntListModel::dataForRole(int row, int role) const
   486 {
   524 {
   487     CNT_ENTRY
   525     CNT_ENTRY
   488     QStringList list;
   526     QStringList list;
   489     QString name;
   527     QString name;
   490     QString infoText;
   528     QString infoText;
   510             name = hbTrId("txt_phob_list_unnamed");
   548             name = hbTrId("txt_phob_list_unnamed");
   511         }
   549         }
   512         infoText = d->m_currentContact.text();
   550         infoText = d->m_currentContact.text();
   513     }
   551     }
   514     
   552     
   515     list << name;
   553     if ( role == Qt::DisplayRole )
       
   554     {
       
   555         list << d->m_Format->formattedText(name, d->m_filter);
       
   556     }
       
   557     else
       
   558     {
       
   559         list << name;
       
   560     }
   516     
   561     
   517     if (!isNonEmptySelfContact) {
   562     if (!isNonEmptySelfContact) {
   518         if (d->m_currentRowSetting == CntTwoRowsNameAndPhoneNumber) {
   563         if (d->m_currentRowSetting == CntTwoRowsNameAndPhoneNumber) {
   519             //add additional text only if user wants 2 rows in each name list item
   564             //add additional text only if user wants 2 rows in each name list item
   520             list << infoText;
   565             list << infoText;
   640         }
   685         }
   641     }
   686     }
   642     
   687     
   643     // invalidate cached contact
   688     // invalidate cached contact
   644     d->m_currentRow = -1;
   689     d->m_currentRow = -1;
   645         
   690     
   646     bool removeMyCard(false);
       
   647     int myCardRow = -1;
   691     int myCardRow = -1;
   648     if (contactIds.contains(d->m_myCardId)) {
   692     if (contactIds.contains(d->m_myCardId)) {
   649         removeMyCard = true;
   693         myCardRow = rowId(d->m_myCardId);
   650         myCardRow = rowId(d->m_myCardId);    
       
   651         d->m_myCardId = 0;
   694         d->m_myCardId = 0;
   652     }
   695     }
   653     
   696 
   654     int removeRowsCount=removeRows.count();
   697     // remove rows starting from the bottom
   655     // no of rows
   698     qSort(removeRows.begin(), removeRows.end(), qGreater<int>());
   656     for(int j = 0; j < removeRows.count(); j++) {
   699     for (int j = 0; j < removeRows.count(); j++) {
   657         if (removeMyCard 
   700         if (removeRows.at(j) != myCardRow || !d->m_showMyCard) {
   658              && removeRows.at(j) == myCardRow 
       
   659              && d->m_showMyCard) {
       
   660             QModelIndex index = createIndex(0, 0);
       
   661             emit dataChanged(index, index);
       
   662             removeRowsCount--;
       
   663         }
       
   664         else {
       
   665             beginRemoveRows(QModelIndex(), removeRows.at(j), removeRows.at(j));
   701             beginRemoveRows(QModelIndex(), removeRows.at(j), removeRows.at(j));
   666             endRemoveRows();
   702             endRemoveRows();
   667         }
   703         }
   668     }
   704     }
   669     updateContactIdsArray();
   705 
   670     
   706     foreach (QContactLocalId id, contactIds) {
   671     // check row count in new list
   707         d->m_contactIds.removeOne(id);
   672     // if there is a mismatch, probable late events. reset model
   708     }
   673     QList<QContactLocalId> updatedIdList = d->m_contactIds;
   709 
   674     int rowsRemoved = idList.count() - updatedIdList.count();
   710     if (myCardRow != -1 && d->m_showMyCard) {
   675     if (rowsRemoved != removeRowsCount) {
   711         d->m_contactIds.insert(0, dummyMyCardId);
   676         beginResetModel();
   712         QModelIndex index = createIndex(0, 0);
   677         reset();
   713         emit dataChanged(index, index);
   678         endResetModel();
   714     }
   679     }
   715 
   680     
       
   681     CNT_EXIT
   716     CNT_EXIT
   682 }
   717 }
   683 
   718 
   684 /*!
   719 /*!
   685  * Handle my card change. 
   720  * Handle my card change. 
   713 {
   748 {
   714     CNT_ENTRY
   749     CNT_ENTRY
   715 
   750 
   716     if (contactIds.contains(d->m_groupId)) {
   751     if (contactIds.contains(d->m_groupId)) {
   717         foreach (QContactLocalId id, contactIds) {
   752         foreach (QContactLocalId id, contactIds) {
   718             if (!d->m_contactIds.contains(id)) {
   753             if (id != d->m_groupId && !d->m_contactIds.contains(id)) {
   719                 // at least one new contact id has been added to this group,
   754                 // at least one new contact id has been added to this group,
   720                 // so update the model
   755                 // so update the model
   721                 updateRelationships();
   756                 updateRelationships();
   722                 break;
   757                 break;
   723             }
   758             }