contentstorage/caclient/src/caitemmodel.cpp
changeset 109 e0aa398e6810
parent 103 b99b84bcd2d1
child 115 3ab5c078b490
equal deleted inserted replaced
104:9b022b1f357c 109:e0aa398e6810
   386 QSharedPointer<CaEntry> CaItemModel::entry(const QModelIndex &index) const
   386 QSharedPointer<CaEntry> CaItemModel::entry(const QModelIndex &index) const
   387 {
   387 {
   388     return m_d->entry(index);
   388     return m_d->entry(index);
   389 }
   389 }
   390 
   390 
       
   391 QList<int> CaItemModel::getUninstallingEntriesIds(int componentId)
       
   392 {
       
   393     return m_d->getUninstallingEntriesIds(componentId);
       
   394 }
       
   395 
       
   396 void CaItemModel::updateProgress(int id, int valueOfProgress)
       
   397 {
       
   398     m_d->updateProgress(id, valueOfProgress);
       
   399 }
       
   400 
   391 /*!
   401 /*!
   392  Constructor
   402  Constructor
   393  \param query needed to create model
   403  \param query needed to create model
   394  \param pointer to public implementation object connected
   404  \param pointer to public implementation object connected
   395  */
   405  */
   444 QVariant CaItemModelPrivate::data(const QModelIndex &modelIndex,
   454 QVariant CaItemModelPrivate::data(const QModelIndex &modelIndex,
   445                                   int role) const
   455                                   int role) const
   446 {
   456 {
   447     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::data");
   457     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::data");
   448     QVariant variant;
   458     QVariant variant;
   449     if (modelIndex.isValid()) {
   459     QSharedPointer<CaEntry> pEntry = entry(modelIndex);
       
   460     if (!pEntry.isNull()) {
   450         switch (role) {
   461         switch (role) {
   451         case Qt::DisplayRole:
   462         case Qt::DisplayRole:
   452             variant = displayRole(modelIndex);
   463             variant = displayRole(pEntry.data());
   453             break;
   464             break;
   454         case Qt::DecorationRole:
   465         case Qt::DecorationRole:
   455             variant = QVariant(HbIcon(entry(modelIndex)->makeIcon(mSize)));
   466             variant = QVariant(HbIcon(pEntry->makeIcon(mSize)));
   456             break;
   467             break;
   457         case CaItemModel::IdRole:
   468         case CaItemModel::IdRole:
   458             variant = QVariant(entry(modelIndex)->id());
   469             variant = QVariant(pEntry->id());
   459             break;
   470             break;
   460         case CaItemModel::TypeRole:
   471         case CaItemModel::TypeRole:
   461             variant = QVariant(entry(modelIndex)->entryTypeName());
   472             variant = QVariant(pEntry->entryTypeName());
   462             break;
   473             break;
   463         case CaItemModel::FlagsRole:
   474         case CaItemModel::FlagsRole:
   464             variant = qVariantFromValue(entry(modelIndex)->flags());
   475             variant = qVariantFromValue(pEntry->flags());
   465             break;
   476             break;
   466         case CaItemModel::TextRole:
   477         case CaItemModel::TextRole:
   467             variant = QVariant(entry(modelIndex)->text());
   478             variant = QVariant(pEntry->text());
   468             break;
   479             break;
   469         case CaItemModel::FullTextRole:
   480         case CaItemModel::FullTextRole:
   470             variant = QVariant(entry(modelIndex)->text() + QString(" ")
   481             variant = QVariant(pEntry->text() + QString(" ") + pEntry->description());
   471                 + entry(modelIndex)->description());
       
   472             break;
   482             break;
   473         case CaItemModel::UninstalRole:
   483         case CaItemModel::UninstalRole:
   474       	    variant = QVariant(entry(modelIndex)->attribute(UNINSTALL_PROGRESS_APPLICATION_ATTRIBUTE_NAME).toInt());
   484             variant = QVariant(
   475             break;
   485                 pEntry->attribute(UNINSTALL_PROGRESS_APPLICATION_ATTRIBUTE_NAME).toInt());
       
   486             break;
       
   487         case CaItemModel::CollectionTitleRole:
       
   488             if (!pEntry->attribute(COLLECTION_TITLE_NAME).isNull()) {
       
   489                 variant = QVariant(pEntry->
       
   490                     attribute(COLLECTION_TITLE_NAME).toUtf8());
       
   491             }
       
   492             else {
       
   493                 variant = QVariant(pEntry->text());
       
   494             }
       
   495             break;
       
   496             
   476         default:
   497         default:
   477             variant = QVariant(QVariant::Invalid);
   498             variant = QVariant(QVariant::Invalid);
   478         }
   499         }
   479     }
   500     }
   480     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::data");
   501     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::data");
   557  \param modelIndex index of entry in model
   578  \param modelIndex index of entry in model
   558  \retval pointer to an entry
   579  \retval pointer to an entry
   559  */
   580  */
   560 QSharedPointer<CaEntry> CaItemModelPrivate::entry(const QModelIndex &modelIndex) const
   581 QSharedPointer<CaEntry> CaItemModelPrivate::entry(const QModelIndex &modelIndex) const
   561 {
   582 {
   562     if (modelIndex.column() == 1) {
   583     if (modelIndex.isValid()) {
   563         return mParentEntry;
   584         if (modelIndex.column() == 1) {
   564     } else {
   585             return mParentEntry;
   565         return mEntries.at(modelIndex.row());
   586         }
   566     }
   587         else {
       
   588             int row = modelIndex.row();
       
   589             if (row >= mEntries.count()) {
       
   590                 return QSharedPointer<CaEntry> ();
       
   591             }
       
   592             else {
       
   593                 return mEntries.at(row);
       
   594             }
       
   595         }
       
   596     }
       
   597     return QSharedPointer<CaEntry> ();
   567 }
   598 }
   568 
   599 
   569 /*!
   600 /*!
   570  Disables or enables secondline feature of the model
   601  Disables or enables secondline feature of the model
   571  \param secondLine disables or enables second line
   602  \param secondLine disables or enables second line
   591 /*!
   622 /*!
   592  Returns proper display role for item
   623  Returns proper display role for item
   593  \param modelIndex item index
   624  \param modelIndex item index
   594  \retval QVariant containing display role
   625  \retval QVariant containing display role
   595  */
   626  */
   596 QVariant CaItemModelPrivate::displayRole(const QModelIndex &modelIndex) const
   627 QVariant CaItemModelPrivate::displayRole(const CaEntry* entry) const
   597 {
   628 {
   598     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::displayRole")
   629     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::displayRole")
   599 
   630 
   600     QVariant result;
   631     QVariant result;
   601     if (mSecondLineVisibility) {
   632     if (mSecondLineVisibility) {
   602         if (entry(modelIndex)->description().isEmpty()) {
   633         if (entry->description().isEmpty()) {
   603             result = entry(modelIndex)->text();
   634             result = entry->text();
   604         } else {
   635         } else {
   605             QList<QVariant> text;
   636             QList<QVariant> text;
   606             text << entry(modelIndex)->text();
   637             text << entry->text();
   607             text << entry(modelIndex)->description();
   638             text << entry->description();
   608             result = QVariant(text);
   639             result = QVariant(text);
   609         }
   640         }
   610     } else {
   641     } else {
   611         result = entry(modelIndex)->text();
   642         result = entry->text();
   612     }
   643     }
   613     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::displayRole")
   644     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::displayRole")
   614     return result;
   645     return result;
   615 }
   646 }
   616 
   647 
   670     }
   701     }
   671     return false;
   702     return false;
   672 }
   703 }
   673 
   704 
   674 
   705 
       
   706 QList<int> CaItemModelPrivate::getUninstallingEntriesIds(int componentId)
       
   707 {
       
   708     CaQuery* query = new CaQuery(mQuery);
       
   709     QString compId(QString().setNum(componentId));
       
   710     query->setAttribute(QString("component_id"), compId);
       
   711     QList<int> ids = mService->getEntryIds(*query);
       
   712     delete query;
       
   713     return ids;
       
   714 }
       
   715 
       
   716 void CaItemModelPrivate::updateProgress(int id, int valueOfProgress)
       
   717 {
       
   718     int updateIndex = mEntries.updateProgress(id, valueOfProgress);
       
   719     if (updateIndex >= 0) {
       
   720         emit m_q->dataChanged(
       
   721                 index(updateIndex), index(updateIndex));
       
   722     }        
       
   723 }
   675 /*!
   724 /*!
   676  Updates model with fresh entries and resets model
   725  Updates model with fresh entries and resets model
   677  */
   726  */
   678 void CaItemModelPrivate::updateModel()
   727 void CaItemModelPrivate::updateModel()
   679 {
   728 {
   786 {
   835 {
   787     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::handleAddItems");
   836     CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::handleAddItems");
   788     const int oldItemCount(mEntries.count());
   837     const int oldItemCount(mEntries.count());
   789     if (oldItemCount) {
   838     if (oldItemCount) {
   790         const int newItemCount(itemsList.count());
   839         const int newItemCount(itemsList.count());
       
   840         int i = 0;
       
   841         QList<int> oldList = mEntries.orderedIdList();
       
   842         //we loop through items to find first added
       
   843         while (i < oldList.count()) {
       
   844             if (oldList[i] != itemsList[i]) {
       
   845                 oldList.takeAt(i);
       
   846             } else {
       
   847                 ++i;
       
   848             }
       
   849         }
   791         if (newItemCount == oldItemCount) {
   850         if (newItemCount == oldItemCount) {
   792             // count is the same - check if item order changed
   851             // count is the same - check if item order changed
   793             if (itemsList == mEntries.orderedIdList()) {
   852             if (itemsList == oldList) {
   794                 // assume that if the order has not changed
   853                 // assume that if the order has not changed
   795                 // it had to be the secondary lines
   854                 // it had to be the secondary lines
   796                 updateModel();
   855                 emit m_q->dataChanged(index(0), index(m_q->rowCount()-1));
   797             } else {
   856             } else {
   798                 updateLayout();
   857                 updateLayout();
   799             }
   858             }
   800         } else {
   859         } else {
   801             int i = 0;
       
   802             QList<int> oldList = mEntries.orderedIdList();
       
   803             //we loop through items to find first added
       
   804             while (i < oldList.count()) {
       
   805                 if (oldList[i] != itemsList[i]) {
       
   806                     oldList.takeAt(i);
       
   807                 } else {
       
   808                     ++i;
       
   809                 }
       
   810             }
       
   811             updateModel();
   860             updateModel();
   812             //i is the index of first added item
   861             //i is the index of first added item
   813             emit m_q->scrollTo(i, QAbstractItemView::PositionAtTop);
       
   814         }
   862         }
       
   863         emit m_q->scrollTo(i, QAbstractItemView::PositionAtTop);
   815     } else {
   864     } else {
   816         // items added to an empty list - add all as a single block
   865         // items added to an empty list - add all as a single block
   817         addItemBlock(itemsList);
   866         addItemBlock(itemsList);
   818     }
   867     }
   819     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::handleAddItems");
   868     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::handleAddItems");
   998         handleAddItems(ids);
  1047         handleAddItems(ids);
   999     } else {
  1048     } else {
  1000         removeItems(ids);
  1049         removeItems(ids);
  1001     }
  1050     }
  1002     emitEmpty(previousCount);
  1051     emitEmpty(previousCount);
       
  1052     emitCountChange(previousCount);
  1003     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateModelContent");
  1053     CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateModelContent");
  1004 }
  1054 }
  1005 
  1055 
  1006 /*!
  1056 /*!
  1007  Emits empty signal if model state was changed
  1057  Emits empty signal if model state was changed
  1014     }
  1064     }
  1015     if ( !previousCount && rowCount()) {
  1065     if ( !previousCount && rowCount()) {
  1016         emit m_q->empty(false);
  1066         emit m_q->empty(false);
  1017     }
  1067     }
  1018 }
  1068 }
       
  1069 
       
  1070 /*!
       
  1071  Emits empty signal if count of item in model was change
       
  1072  \param previousCount 
       
  1073  */
       
  1074 void CaItemModelPrivate::emitCountChange(int previousCount)
       
  1075 {
       
  1076     if (previousCount != rowCount()) {
       
  1077         emit m_q->countChange();
       
  1078     }
       
  1079 }
       
  1080