phonebookui/pbkcommonui/src/cntcollectionlistmodel.cpp
changeset 24 0ba2181d7c28
child 25 76a2435edfd4
equal deleted inserted replaced
0:e686773b3f54 24:0ba2181d7c28
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "cntcollectionlistmodel.h"
       
    19 
       
    20 #include <QIcon>
       
    21 #include <qtcontacts.h>
       
    22 #include <hbglobal.h>
       
    23 
       
    24 /*!
       
    25 Constructor
       
    26 */
       
    27 CntCollectionListModel::CntCollectionListModel(QContactManager *manager, QObject *parent)
       
    28     : QAbstractListModel(parent),
       
    29     mContactManager(manager)
       
    30 {
       
    31     mDataPointer = new CntCollectionListData();
       
    32     doConstruct();
       
    33 }
       
    34 
       
    35 /*!
       
    36 Destructor
       
    37 */
       
    38 CntCollectionListModel::~CntCollectionListModel()
       
    39 {
       
    40     
       
    41 }
       
    42 
       
    43 /*!
       
    44 Returns data for given index with a given role
       
    45 */
       
    46 QVariant CntCollectionListModel::data(const QModelIndex &index, int role) const
       
    47 {
       
    48     int row = index.row();
       
    49     
       
    50     if (row < 0)
       
    51     {
       
    52         return QVariant();
       
    53     }
       
    54     
       
    55     QVariantList values = mDataPointer->mDataList.at(row);
       
    56     
       
    57     if (role == Qt::DisplayRole)
       
    58     {
       
    59         QStringList list = values[0].toStringList();        
       
    60         return QVariant(list);
       
    61     }
       
    62      
       
    63     else if (role == Qt::DecorationRole)
       
    64     {
       
    65         QVariantList icons;
       
    66         QIcon itemIcon(values[1].toString());
       
    67         icons.append(itemIcon);
       
    68         return QVariant(icons);
       
    69     }
       
    70     
       
    71     else if (role == Qt::UserRole)
       
    72     {
       
    73         int contactId = values[2].toInt();
       
    74         return QVariant(contactId);
       
    75     }
       
    76 
       
    77     return QVariant();
       
    78 }
       
    79 
       
    80 /*!
       
    81 Returns the number of rows in the model
       
    82 */
       
    83 int CntCollectionListModel::rowCount(const QModelIndex &parent) const
       
    84 {
       
    85     Q_UNUSED(parent);
       
    86     return mDataPointer->mDataList.count();
       
    87 }
       
    88 
       
    89 /*!
       
    90 Removes given rows from the model
       
    91 */
       
    92 bool CntCollectionListModel::removeRows(int row, int count, const QModelIndex &parent)
       
    93 {
       
    94     Q_UNUSED(count);
       
    95     Q_UNUSED(parent);
       
    96     if (row < 0 || row > rowCount())
       
    97     {
       
    98         return false;
       
    99     }
       
   100     
       
   101     beginRemoveRows(parent, row, row);
       
   102     mDataPointer->mDataList.removeAt(row);
       
   103     endRemoveRows();
       
   104 
       
   105     return true;
       
   106 }
       
   107 
       
   108 /*!
       
   109 Remove given group from the model
       
   110 */
       
   111 void CntCollectionListModel::removeGroup(int localId)
       
   112 {
       
   113     for (int i = 0;i < rowCount();i++)
       
   114     {
       
   115         if (mDataPointer->mDataList.at(i)[2] == localId)
       
   116         {
       
   117             removeRow(i);
       
   118             break;
       
   119         }
       
   120     }
       
   121 }
       
   122 
       
   123 /*!
       
   124 Initialize the data, both static (favorites, online) and user defined groups
       
   125 */
       
   126 void CntCollectionListModel::doConstruct()
       
   127 {
       
   128     initializeStaticGroups();
       
   129     initializeUserGroups();
       
   130 }
       
   131 
       
   132 /*!
       
   133 Initialize static groups
       
   134 */
       
   135 void CntCollectionListModel::initializeStaticGroups()
       
   136 {
       
   137     QVariantList dataList;
       
   138     QStringList displayList;
       
   139     displayList.append(hbTrId("txt_phob_dblist_favorites"));
       
   140     displayList.append(hbTrId("txt_phob_dblist_favorites_val_no_favorites_selecte")); // as this isn't supported yet
       
   141     dataList.append(displayList);
       
   142     
       
   143     dataList.append(":/icons/qtg_large_favourites.svg");
       
   144     
       
   145     dataList.append(-1); // as favorites doesn't really have a contact Id, -1 is used
       
   146     
       
   147     mDataPointer->mDataList.append(dataList);
       
   148 }
       
   149 
       
   150 /*!
       
   151 Initialize user defined groups
       
   152 */
       
   153 void CntCollectionListModel::initializeUserGroups()
       
   154 {
       
   155     QContactDetailFilter groupFilter;
       
   156     groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType);
       
   157     groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup)));
       
   158 
       
   159     QList<QContactLocalId> groupContactIds = mContactManager->contacts(groupFilter);
       
   160     
       
   161     if (!groupContactIds.isEmpty())
       
   162     {
       
   163         for(int i = 0;i < groupContactIds.count();i++)
       
   164         {
       
   165             QVariantList dataList;
       
   166             
       
   167             // group name
       
   168             QStringList displayList;
       
   169             
       
   170             QContact contact = mContactManager->contact(groupContactIds.at(i));
       
   171             QContactName contactName = contact.detail<QContactName>();
       
   172             QString groupName = contactName.customLabel();
       
   173             
       
   174             if (groupName.isNull())
       
   175             {
       
   176                 QString unnamed(hbTrId("Unnamed"));
       
   177                 displayList.append(unnamed);
       
   178             }
       
   179             else
       
   180             {
       
   181                 displayList.append(groupName);
       
   182             }
       
   183             
       
   184             QContactRelationshipFilter rFilter;
       
   185             rFilter.setRelationshipType(QContactRelationship::HasMember);
       
   186             rFilter.setRole(QContactRelationshipFilter::Second);
       
   187             rFilter.setOtherParticipantId(contact.id());
       
   188             
       
   189             // group members and their count
       
   190             QList<QContactLocalId> groupMemberIds = mContactManager->contacts(rFilter);
       
   191             
       
   192             if (!groupMemberIds.isEmpty())
       
   193             {
       
   194                 QStringList nameList;
       
   195                 for(int i = 0;i < groupMemberIds.count();i++)
       
   196                 {
       
   197                     QContact contact = mContactManager->contact(groupMemberIds.at(i));
       
   198                     QString memberName = contact.displayLabel();
       
   199                     nameList << memberName;
       
   200                     if (nameList.join(", ").length() > 30)
       
   201                     {
       
   202                         break;
       
   203                     }
       
   204                 }
       
   205                 QString names = nameList.join(", ");
       
   206                 displayList.append(names);
       
   207                 displayList.append(hbTrId("(%1)").arg(groupMemberIds.count()));
       
   208             }
       
   209             else
       
   210             {
       
   211                 displayList.append(hbTrId("No members selected"));
       
   212             }
       
   213             dataList.append(displayList);
       
   214             
       
   215             // icon, default for now always
       
   216             dataList.append(":/icons/qtg_large_custom.svg");
       
   217             
       
   218             // contact Id for identification
       
   219             dataList.append(groupContactIds.at(i));
       
   220             
       
   221             mDataPointer->mDataList.append(dataList);
       
   222         }
       
   223     }
       
   224 }