phonebookui/pbkcommonui/src/cntcollectionlistmodel.cpp
changeset 59 a642906a277a
parent 47 7cbcb2896f0e
child 61 d30183af6ca6
equal deleted inserted replaced
47:7cbcb2896f0e 59:a642906a277a
   106     if ( p.isNull() )
   106     if ( p.isNull() )
   107         return QVariant();
   107         return QVariant();
   108     
   108     
   109     if (!p->fetched)
   109     if (!p->fetched)
   110     {
   110     {
   111         mThread->scheduleJob(row, p->id);
   111         mThread->scheduleJob(p->id);
   112         p->fetched = true;
   112         p->fetched = true;
   113     }
   113     }
   114     
   114     
   115     switch( role )
   115     switch( role )
   116     {       
   116     {       
   161 
   161 
   162     return true;
   162     return true;
   163 }
   163 }
   164 
   164 
   165 /*!
   165 /*!
       
   166     Adds a row to the model. This is called from addGroup(int localId), so no
       
   167     real logic in here.
       
   168 
       
   169     \param row the first row which is added
       
   170     \param count amount of rows to be added (not supported)
       
   171     \param parent not used as all items share the same parent
       
   172     \return success of the operation
       
   173 */
       
   174 bool CntCollectionListModel::insertRows(int row, int count, const QModelIndex &parent)
       
   175 {
       
   176     Q_UNUSED(count);
       
   177     Q_UNUSED(parent);
       
   178     
       
   179     if ( !validateRowIndex(row) )
       
   180     {
       
   181         return false;
       
   182     }
       
   183     
       
   184     beginInsertRows(parent, row, row);
       
   185 
       
   186     endInsertRows();
       
   187 
       
   188     return true;
       
   189 }
       
   190 
       
   191 /*!
   166     Remove given group from the model. Ignore plugin groups.
   192     Remove given group from the model. Ignore plugin groups.
   167 
   193 
   168     \param localId QContactLocalId of the group that should be removed
   194     \param localId QContactLocalId of the group that should be removed
   169 */
   195 */
   170 void CntCollectionListModel::removeGroup(int localId)
   196 void CntCollectionListModel::removeGroup(int localId)
   175     {
   201     {
   176         if (!d->mList.at(i)->isPlugin && d->mList.at(i)->id == localId)
   202         if (!d->mList.at(i)->isPlugin && d->mList.at(i)->id == localId)
   177         {
   203         {
   178             removeRow(i);
   204             removeRow(i);
   179             break;
   205             break;
       
   206         }
       
   207     }
       
   208     
       
   209     CNT_EXIT
       
   210 }
       
   211 
       
   212 /*!
       
   213     Add a new group to the model. This also takes care of putting it in the
       
   214     correct row.
       
   215 
       
   216     \param localId QContactLocalId of the group that should be added
       
   217 */
       
   218 void CntCollectionListModel::addGroup(int localId)
       
   219 {
       
   220     CNT_ENTRY
       
   221     
       
   222     QContactDetailFilter groupFilter;
       
   223     groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   224     groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup)));
       
   225 
       
   226     QContactSortOrder sortOrderGroupName;
       
   227     sortOrderGroupName.setDetailDefinitionName(QContactName::DefinitionName,
       
   228         QContactName::FieldCustomLabel);
       
   229     sortOrderGroupName.setCaseSensitivity(Qt::CaseInsensitive);
       
   230 
       
   231     QList<QContactSortOrder> groupsOrder;
       
   232     groupsOrder.append(sortOrderGroupName);
       
   233 
       
   234     QList<QContactLocalId> groupContactIds = d->mContactManager->contactIds(groupFilter, groupsOrder);
       
   235     
       
   236     int row = 1;
       
   237     
       
   238     for (int i = 0;i < groupContactIds.count();i++)
       
   239     {
       
   240         if (groupContactIds.at(i) == localId)
       
   241         {
       
   242             row += d->mExtensions.count();
       
   243             
       
   244             CollectionItemPointer item(new CntCollectionItem());
       
   245             
       
   246             QContactFetchHint noRelationshipsFetchHint;
       
   247             noRelationshipsFetchHint.setOptimizationHints(QContactFetchHint::NoRelationships);
       
   248             
       
   249             QContact contact = d->mContactManager->contact(groupContactIds.at(i), noRelationshipsFetchHint);
       
   250             QContactName contactName = contact.detail<QContactName>();
       
   251             QString groupName = contactName.customLabel();
       
   252             
       
   253             if (groupName.isNull())
       
   254             {
       
   255                 item->groupName = hbTrId("txt_phob_dblist_unnamed");
       
   256             }
       
   257             else
       
   258             {
       
   259                 item->groupName = groupName;
       
   260             }
       
   261 
       
   262             item->icon = HbIcon("qtg_large_custom");
       
   263             
       
   264             QList<QContactAvatar> details = contact.details<QContactAvatar>();
       
   265             for (int k = 0;k < details.count();k++)
       
   266             {
       
   267                 if (details.at(k).imageUrl().isValid())
       
   268                 {
       
   269                     int id = d->mThumbnailManager->getThumbnail(details.at(k).imageUrl().toString());
       
   270                     d->mIconRequests.insert(id, groupContactIds.at(i));
       
   271                     break;
       
   272                 }
       
   273             }
       
   274             
       
   275             // contact Id for identification
       
   276             item->id = groupContactIds.at(i);
       
   277             item->secondLineText = " ";
       
   278             item->memberCount = -1;
       
   279 
       
   280             d->mList.insert(row, item);
       
   281             insertRow(row);
       
   282             break;
       
   283         }
       
   284         else if (groupContactIds.at(i) != d->mFavoriteGroupId)
       
   285         {
       
   286             row++;
   180         }
   287         }
   181     }
   288     }
   182     
   289     
   183     CNT_EXIT
   290     CNT_EXIT
   184 }
   291 }
   242             break;
   349             break;
   243         }
   350         }
   244     }
   351     }
   245     
   352     
   246     CNT_EXIT
   353     CNT_EXIT
       
   354 }
       
   355 
       
   356 /*!
       
   357     Get the index for a given group id.
       
   358 
       
   359     \param localId the id of the group
       
   360     \return QModelIndex of the group
       
   361 */
       
   362 QModelIndex CntCollectionListModel::indexOfGroup(int localId)
       
   363 {
       
   364     for (int i = 0;i < d->mList.count();i++)
       
   365     {
       
   366         if (d->mList.at(i)->id == localId && !d->mList.at(i)->isPlugin)
       
   367         {
       
   368             return index(i);
       
   369         }
       
   370     }
       
   371     
       
   372     return QModelIndex();
   247 }
   373 }
   248 
   374 
   249 /*!
   375 /*!
   250     Initialize different groups.
   376     Initialize different groups.
   251 */
   377 */
   293     for(int i = 0;i < d->mExtensionManager.pluginCount();i++)
   419     for(int i = 0;i < d->mExtensionManager.pluginCount();i++)
   294     {
   420     {
   295         CntUiGroupSupplier* groupSupplier = d->mExtensionManager.pluginAt(i)->groupSupplier();
   421         CntUiGroupSupplier* groupSupplier = d->mExtensionManager.pluginAt(i)->groupSupplier();
   296         if (groupSupplier)
   422         if (groupSupplier)
   297         {
   423         {
   298             for(int j = 0;j < groupSupplier->groupCount();j++)
   424             if (groupSupplier->isAsynch())
   299             {
   425             {
   300                 const CntUiExtensionGroup& group = groupSupplier->groupAt(j);
   426                 connect(groupSupplier, SIGNAL(groupsReady()), this, SLOT(extensionGroupsReady()));
   301                 
   427             }
   302                 CollectionItemPointer item(new CntCollectionItem());
   428             else
   303                 
   429             {
   304                 item->groupName = group.name();
   430                 for (int j = 0;j < groupSupplier->groupCount();j++)
   305                 item->secondLineText = group.extraText();
   431                 {
   306                 item->memberCount = group.memberCount();
   432                     const CntUiExtensionGroup& group = groupSupplier->groupAt(j);
   307                 item->icon = HbIcon(group.groupIcon());
   433                     
   308                 item->secondaryIcon = HbIcon(group.extraIcon());
   434                     CollectionItemPointer item(new CntCollectionItem());
   309                 
   435                     
   310                 item->id = group.serviceId();
   436                     item->groupName = group.name();
   311                 item->isPlugin = true;
   437                     item->secondLineText = group.extraText();
   312                 item->fetched = true;
   438                     item->memberCount = group.memberCount();
   313                 
   439                     item->icon = HbIcon(group.groupIcon());
   314                 d->mExtensions.insert(rowCount(), groupSupplier);
   440                     item->secondaryIcon = HbIcon(group.extraIcon());
   315                 d->mList.append(item);
   441                     
       
   442                     item->id = group.serviceId();
       
   443                     item->isPlugin = true;
       
   444                     item->fetched = true;
       
   445                     
       
   446                     d->mExtensions.insert(rowCount(), groupSupplier);
       
   447                     d->mList.append(item);
       
   448                 }
   316             }
   449             }
   317         }
   450         }
   318     }
   451     }
   319     
   452     
   320     CNT_EXIT
   453     CNT_EXIT
   369             for (int k = 0;k < details.count();k++)
   502             for (int k = 0;k < details.count();k++)
   370             {
   503             {
   371                 if (details.at(k).imageUrl().isValid())
   504                 if (details.at(k).imageUrl().isValid())
   372                 {
   505                 {
   373                     int id = d->mThumbnailManager->getThumbnail(details.at(k).imageUrl().toString());
   506                     int id = d->mThumbnailManager->getThumbnail(details.at(k).imageUrl().toString());
   374                     d->mIconRequests.insert(id, rowCount());
   507                     d->mIconRequests.insert(id, groupContactIds.at(i));
   375                     break;
   508                     break;
   376                 }
   509                 }
   377             }
   510             }
   378             
   511             
   379             // contact Id for identification
   512             // contact Id for identification
   448 
   581 
   449     \param row the row receiving the updated information
   582     \param row the row receiving the updated information
   450     \param secondRowText text to be shown in the second row
   583     \param secondRowText text to be shown in the second row
   451     \param memberCount the amount of members the group has (shown in text-3 in HbListViewItem)
   584     \param memberCount the amount of members the group has (shown in text-3 in HbListViewItem)
   452 */
   585 */
   453 void CntCollectionListModel::informationUpdated(int row, const QString& secondRowText, int memberCount)
   586 void CntCollectionListModel::informationUpdated(int id, const QString& secondRowText, int memberCount)
   454 {
   587 {
   455     CNT_ENTRY
   588     CNT_ENTRY
   456     
   589     
   457     CollectionItemPointer item = d->mList.at(row);
   590     for (int i = 0;i < d->mList.count();i++)
   458     
   591     {
   459     item->secondLineText = secondRowText;
   592         if (d->mList.at(i)->id == id && !d->mList.at(i)->isPlugin)
   460     item->memberCount = memberCount;
   593         {
   461     
   594             CollectionItemPointer item = d->mList.at(i);
   462     int idx = d->mList.indexOf(item);
   595             
   463     emit dataChanged(index(idx, 0), index(idx, 0));
   596             item->secondLineText = secondRowText;
       
   597             item->memberCount = memberCount;
       
   598             
       
   599             int idx = d->mList.indexOf(item);
       
   600             emit dataChanged(index(idx, 0), index(idx, 0));
       
   601             
       
   602             break;
       
   603         }
       
   604     }
   464     
   605     
   465     CNT_EXIT
   606     CNT_EXIT
   466 }
   607 }
   467 
   608 
   468 /*!
   609 /*!
   480     
   621     
   481     Q_UNUSED(data);
   622     Q_UNUSED(data);
   482     
   623     
   483     if (error == 0)
   624     if (error == 0)
   484     {
   625     {
   485         int row = d->mIconRequests.take(id);
   626         int localId = d->mIconRequests.take(id);
   486         
   627         
   487         CollectionItemPointer item = d->mList.at(row);
   628         for (int i = 0;i < d->mList.count();i++)
       
   629         {
       
   630             if (d->mList.at(i)->id == localId && !d->mList.at(i)->isPlugin)
       
   631             {
       
   632                 CollectionItemPointer item = d->mList.at(i);
       
   633                 
       
   634                 item->icon = HbIcon(pixmap);
       
   635                 
       
   636                 int idx = d->mList.indexOf(item);
       
   637                 emit dataChanged(index(idx, 0), index(idx, 0));
       
   638                 
       
   639                 break;
       
   640             }
       
   641         }
       
   642     }
       
   643 
       
   644     CNT_EXIT
       
   645 }
       
   646 
       
   647 /*!
       
   648     Slot to handle asynchronously initiated extension groups. As
       
   649     these are added right after the static favorites group, this takes
       
   650     also care of updating the mapping between row and CntUiGroupSuppliers
       
   651     in case there was already some synchronously loaded extension groups.
       
   652 */
       
   653 void CntCollectionListModel::extensionGroupsReady()
       
   654 {
       
   655     CntUiGroupSupplier* groupSupplier = static_cast<CntUiGroupSupplier*>(sender());
       
   656     
       
   657     int addedCount = groupSupplier->groupCount();
       
   658     
       
   659     if (addedCount > 0)
       
   660     {
       
   661         QList<int> rowList = d->mExtensions.keys();
       
   662         QList<CntUiGroupSupplier*> supplierList = d->mExtensions.values();
   488         
   663         
   489         item->icon = HbIcon(pixmap);
   664         d->mExtensions.clear();
   490         
   665         
   491         int idx = d->mList.indexOf(item);
   666         for (int i = 0;i < rowList.count();i++)
   492         emit dataChanged(index(idx, 0), index(idx, 0));
   667         {
   493     }
   668             int row = rowList.at(i) + addedCount;
   494 
   669             CntUiGroupSupplier* supplier = supplierList.at(i);
   495     CNT_EXIT
   670             
   496 }
   671             d->mExtensions.insert(row, supplier);
       
   672         }
       
   673         
       
   674         
       
   675         beginInsertRows(QModelIndex(), 1, groupSupplier->groupCount());
       
   676         
       
   677         for (int j = 0;j < groupSupplier->groupCount();j++)
       
   678         {
       
   679             const CntUiExtensionGroup& group = groupSupplier->groupAt(j);
       
   680             
       
   681             CollectionItemPointer item(new CntCollectionItem());
       
   682             
       
   683             item->groupName = group.name();
       
   684             item->secondLineText = group.extraText();
       
   685             item->memberCount = group.memberCount();
       
   686             item->icon = HbIcon(group.groupIcon());
       
   687             item->secondaryIcon = HbIcon(group.extraIcon());
       
   688             
       
   689             item->id = group.serviceId();
       
   690             item->isPlugin = true;
       
   691             item->fetched = true;
       
   692             
       
   693             d->mExtensions.insert(1 + j, groupSupplier);
       
   694             d->mList.insert(1 + j, item);
       
   695         }
       
   696         
       
   697         endInsertRows();
       
   698     }
       
   699 }