|
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 "cntgroupdeletepopupmodel.h" |
|
19 |
|
20 #include <qtcontacts.h> |
|
21 #include <hbglobal.h> |
|
22 |
|
23 /*! |
|
24 Constructor |
|
25 */ |
|
26 CntGroupDeletePopupModel::CntGroupDeletePopupModel(QContactManager *manager, QObject *parent) |
|
27 : QAbstractListModel(parent), |
|
28 mContactManager(manager), |
|
29 mFavoriteGroupId(-1) |
|
30 { |
|
31 mDataPointer = new CntGroupPopupListData(); |
|
32 isFavoriteGroupCreated(); |
|
33 } |
|
34 |
|
35 /*! |
|
36 Destructor |
|
37 */ |
|
38 CntGroupDeletePopupModel::~CntGroupDeletePopupModel() |
|
39 { |
|
40 |
|
41 } |
|
42 |
|
43 /*! |
|
44 Initialize groups |
|
45 */ |
|
46 void CntGroupDeletePopupModel::initializeGroupsList() |
|
47 { |
|
48 QContactDetailFilter groupFilter; |
|
49 groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType); |
|
50 groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup))); |
|
51 |
|
52 QList<QContactLocalId> groupContactIds = mContactManager->contactIds(groupFilter); |
|
53 if (!groupContactIds.isEmpty()) |
|
54 { |
|
55 for(int i = 0;i < groupContactIds.count();i++) |
|
56 { |
|
57 QVariantList dataList; |
|
58 |
|
59 // group name |
|
60 QStringList displayList; |
|
61 |
|
62 QContact contact = mContactManager->contact(groupContactIds.at(i)); |
|
63 QContactName contactName = contact.detail<QContactName>(); |
|
64 QString groupName = contactName.customLabel(); |
|
65 if(groupContactIds.at(i) != mFavoriteGroupId ) |
|
66 { |
|
67 if (groupName.isNull()) |
|
68 { |
|
69 QString unnamed(hbTrId("Unnamed")); |
|
70 displayList.append(unnamed); |
|
71 } |
|
72 else |
|
73 { |
|
74 displayList.append(groupName); |
|
75 } |
|
76 |
|
77 dataList.append(displayList); |
|
78 |
|
79 // contact Id for identification |
|
80 dataList.append(groupContactIds.at(i)); |
|
81 |
|
82 mDataPointer->mDataList.append(dataList); |
|
83 } |
|
84 } |
|
85 } |
|
86 } |
|
87 |
|
88 bool CntGroupDeletePopupModel::isFavoriteGroupCreated() |
|
89 { |
|
90 bool favoriteGroupCreated = false; |
|
91 QContactDetailFilter groupFilter; |
|
92 groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType); |
|
93 groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup))); |
|
94 |
|
95 QList<QContactLocalId> groupContactIds = mContactManager->contactIds(groupFilter); |
|
96 |
|
97 if (!groupContactIds.isEmpty()) |
|
98 { |
|
99 for(int i = 0;i < groupContactIds.count();i++) |
|
100 { |
|
101 QContact contact = mContactManager->contact(groupContactIds.at(i)); |
|
102 QContactName contactName = contact.detail<QContactName>(); |
|
103 QString groupName = contactName.customLabel(); |
|
104 if(groupName.compare("Favorites") == 0) |
|
105 { |
|
106 favoriteGroupCreated = true; |
|
107 mFavoriteGroupId = groupContactIds.at(i); |
|
108 break; |
|
109 } |
|
110 } |
|
111 } |
|
112 return favoriteGroupCreated; |
|
113 } |
|
114 |
|
115 int CntGroupDeletePopupModel::favoriteGroupId() |
|
116 { |
|
117 return mFavoriteGroupId; |
|
118 } |
|
119 |
|
120 /*! |
|
121 Returns the number of rows in the model |
|
122 */ |
|
123 int CntGroupDeletePopupModel::rowCount(const QModelIndex &parent) const |
|
124 { |
|
125 Q_UNUSED(parent); |
|
126 return mDataPointer->mDataList.count(); |
|
127 } |
|
128 /*! |
|
129 Returns data for given index with a given role |
|
130 */ |
|
131 QVariant CntGroupDeletePopupModel::data(const QModelIndex &index, int role) const |
|
132 { |
|
133 int row = index.row(); |
|
134 |
|
135 if (row < 0) |
|
136 { |
|
137 return QVariant(); |
|
138 } |
|
139 |
|
140 QVariantList values = mDataPointer->mDataList.at(row); |
|
141 |
|
142 if (role == Qt::DisplayRole) |
|
143 { |
|
144 QStringList list = values[0].toStringList(); |
|
145 return QVariant(list); |
|
146 } |
|
147 |
|
148 else if (role == Qt::UserRole) |
|
149 { |
|
150 int contactId = values[1].toInt(); |
|
151 return QVariant(contactId); |
|
152 } |
|
153 return QVariant(); |
|
154 } |
|
155 |
|
156 |
|
157 QContact CntGroupDeletePopupModel::contact(QModelIndex &index) const |
|
158 { |
|
159 int row = index.row(); |
|
160 QVariantList values = mDataPointer->mDataList.at(row); |
|
161 int groupId = values[1].toInt(); |
|
162 return mContactManager->contact( groupId ); |
|
163 } |