phonebookui/pbkcommonui/src/cntgroupdeletepopupmodel.cpp
changeset 47 7cbcb2896f0e
parent 46 efe85016a067
child 61 d30183af6ca6
--- a/phonebookui/pbkcommonui/src/cntgroupdeletepopupmodel.cpp	Wed Jun 23 18:02:44 2010 +0300
+++ b/phonebookui/pbkcommonui/src/cntgroupdeletepopupmodel.cpp	Tue Jul 06 14:05:47 2010 +0300
@@ -16,21 +16,31 @@
 */
 
 #include "cntgroupdeletepopupmodel.h"
+#include "cntfavourite.h"
 
+#include <thumbnailmanager_qt.h>
 #include <qtcontacts.h>
 #include <hbglobal.h>
-#include <hbiconitem.h>
+#include <hbicon.h>
 
 /*!
 Constructor
 */
 CntGroupDeletePopupModel::CntGroupDeletePopupModel(QContactManager *manager, QObject *parent)
     : QAbstractListModel(parent),
-    mContactManager(manager),
     mFavoriteGroupId(-1)
 {
-    mDataPointer = new CntGroupPopupListData();
-    isFavoriteGroupCreated();
+    d = new CntGroupPopupListData();
+    d->mContactManager = manager;
+    
+    mFavoriteGroupId = CntFavourite::createFavouriteGroup( d->mContactManager );
+    
+    d->mThumbnailManager = new ThumbnailManager(this);
+    d->mThumbnailManager->setMode(ThumbnailManager::Default);
+    d->mThumbnailManager->setQualityPreference(ThumbnailManager::OptimizeForPerformance);
+    d->mThumbnailManager->setThumbnailSize(ThumbnailManager::ThumbnailSmall);
+    connect(d->mThumbnailManager, SIGNAL(thumbnailReady(QPixmap, void *, int, int)),
+             this, SLOT(onIconReady(QPixmap, void *, int, int)));
 }
 
 /*!
@@ -38,7 +48,12 @@
 */
 CntGroupDeletePopupModel::~CntGroupDeletePopupModel()
 {
-    
+    foreach (int id, d->mIconRequests.keys())
+    {
+        d->mThumbnailManager->cancelRequest(id);
+    }
+
+    d->mIconRequests.clear();
 }
 
 /*!
@@ -58,7 +73,7 @@
     QList<QContactSortOrder> groupsOrder;
     groupsOrder.append(sortOrderGroupName);
 
-    QList<QContactLocalId> groupContactIds = mContactManager->contactIds(groupFilter, groupsOrder);
+    QList<QContactLocalId> groupContactIds = d->mContactManager->contactIds(groupFilter, groupsOrder);
 
     if (!groupContactIds.isEmpty())
     {
@@ -69,7 +84,7 @@
             // group name
             QStringList displayList;
             
-            QContact contact = mContactManager->contact(groupContactIds.at(i));
+            QContact contact = d->mContactManager->contact(groupContactIds.at(i));
             QContactName contactName = contact.detail<QContactName>();
             QString groupName = contactName.customLabel();
             if(groupContactIds.at(i) != mFavoriteGroupId )
@@ -89,56 +104,26 @@
                 // contact Id for identification
                 dataList.append(groupContactIds.at(i));
                 
-                // Default if no image for group 
-                bool icon = false;
+                // Default if no image for group
+                dataList.append(HbIcon("qtg_large_custom"));
+                
                 QList<QContactAvatar> details = contact.details<QContactAvatar>();
                 for (int k = 0;k < details.count();k++)
                 {
                    if (details.at(k).imageUrl().isValid())
                    {
-                       dataList.append(QStringList(details.at(k).imageUrl().toString()));
-                       icon = true;
+                       int id = d->mThumbnailManager->getThumbnail(details.at(k).imageUrl().toString());
+                       d->mIconRequests.insert(id, rowCount());
                        break;
                    }
                 }
-                if(!icon)
-                {
-                   dataList.append(QStringList("qtg_large_custom"));
-                }
 
-                mDataPointer->mDataList.append(dataList);
+                d->mDataList.append(dataList);
             }
         }
     }
 }
 
-bool CntGroupDeletePopupModel::isFavoriteGroupCreated()
-{
-    bool favoriteGroupCreated = false;
-    QContactDetailFilter groupFilter;
-    groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
-    groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup)));
-
-    QList<QContactLocalId> groupContactIds = mContactManager->contactIds(groupFilter);
-    
-    if (!groupContactIds.isEmpty())
-    {
-        for(int i = 0;i < groupContactIds.count();i++)
-        {
-            QContact contact = mContactManager->contact(groupContactIds.at(i));
-            QContactName contactName = contact.detail<QContactName>();
-            QString groupName = contactName.customLabel();
-            if(groupName.compare("Favorites") == 0)
-            {
-                favoriteGroupCreated = true;
-                mFavoriteGroupId = groupContactIds.at(i);
-                break;
-            }
-        }
-    }
-    return favoriteGroupCreated;
-}
-
 int CntGroupDeletePopupModel::favoriteGroupId()
 {
     return mFavoriteGroupId;
@@ -150,7 +135,7 @@
 int CntGroupDeletePopupModel::rowCount(const QModelIndex &parent) const
 {
     Q_UNUSED(parent);
-    return mDataPointer->mDataList.count();
+    return d->mDataList.count();
 }
 /*!
 Returns data for given index with a given role
@@ -164,7 +149,7 @@
         return QVariant();
     }
     
-    QVariantList values = mDataPointer->mDataList.at(row);
+    QVariantList values = d->mDataList.at(row);
     
     if (role == Qt::DisplayRole)
     {
@@ -174,13 +159,7 @@
     
     else if (role == Qt::DecorationRole)
     {
-        QVariantList icons;
-        for (int i = 0;i < values[2].toStringList().count();i++)
-        {
-            HbIcon icon(values[2].toStringList().at(i));
-            icons.append(icon);
-        }
-        return QVariant(icons);
+        return values[2];
     }
     
     else if (role == Qt::UserRole)
@@ -195,7 +174,30 @@
 QContact CntGroupDeletePopupModel::contact(QModelIndex &index) const
 {
     int row = index.row();
-    QVariantList values = mDataPointer->mDataList.at(row);
+    QVariantList values = d->mDataList.at(row);
     int groupId = values[1].toInt();
-    return mContactManager->contact( groupId );
+    return d->mContactManager->contact( groupId );
 }
+
+void CntGroupDeletePopupModel::onIconReady(const QPixmap& pixmap, void *data, int id, int error)
+{
+    Q_UNUSED(data);
+    
+    if (error == 0)
+    {
+        int row = d->mIconRequests.take(id);
+        
+        QVariantList values = d->mDataList.at(row);
+        QVariantList newValues;
+        
+        newValues.append(values[0]);
+        newValues.append(values[1]);
+        newValues.append(HbIcon(pixmap));
+        
+        d->mDataList.removeAt(row);
+        
+        d->mDataList.insert(row, newValues);
+        
+        emit dataChanged(index(row, 0), index(row, 0));
+    }
+}