|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the QtCore module of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "cntrelationshipgroup.h" |
|
43 #include <cntitem.h> |
|
44 |
|
45 #include <QDebug> |
|
46 |
|
47 /*! |
|
48 * Conatructor |
|
49 * |
|
50 * \a contactDatabase CContactDatabase with established connection to the database |
|
51 * \a managerUri current contact manager uri |
|
52 */ |
|
53 CntRelationshipGroup::CntRelationshipGroup(CContactDatabase *contactDatabase, const QString &managerUri) : |
|
54 CntAbstractRelationship(contactDatabase, managerUri, QContactRelationship::HasMember) |
|
55 { |
|
56 } |
|
57 |
|
58 /*! |
|
59 * Destructor |
|
60 */ |
|
61 CntRelationshipGroup::~CntRelationshipGroup() |
|
62 { |
|
63 } |
|
64 |
|
65 /*! |
|
66 * Save a single relatonship |
|
67 * |
|
68 * \a affectedContactIds will include the affected contact ids |
|
69 * \a relationship to be saved |
|
70 * \a error t |
|
71 */ |
|
72 bool CntRelationshipGroup::saveRelationshipL(QSet<QContactLocalId> *affectedContactIds, QContactRelationship* relationship, QContactManager::Error* error) |
|
73 { |
|
74 //get the ids of the relationship |
|
75 QScopedPointer<QContactId> groupId(new QContactId(relationship->first())); |
|
76 QScopedPointer<QContactId> contactId(new QContactId(relationship->second())); |
|
77 |
|
78 //cntmodel accepts contact id 0, which is considered an error in qt contacts |
|
79 database()->AddContactToGroupL(TContactItemId(contactId->localId()), TContactItemId(groupId->localId())); |
|
80 |
|
81 //add the group and group member to the list of affected contacts |
|
82 //note if the value already exists in the QSet nothing happens |
|
83 affectedContactIds->insert(groupId->localId()); |
|
84 affectedContactIds->insert(contactId->localId()); |
|
85 |
|
86 *error = QContactManager::NoError; |
|
87 return true; |
|
88 } |
|
89 |
|
90 bool CntRelationshipGroup::removeRelationshipL(QSet<QContactLocalId> *affectedContactIds, const QContactRelationship& relationship, QContactManager::Error* error) |
|
91 { |
|
92 //get the ids of the relationship |
|
93 QScopedPointer<QContactId> groupId(new QContactId(relationship.first())); |
|
94 QScopedPointer<QContactId> contactId(new QContactId(relationship.second())); |
|
95 |
|
96 //read the contacts from the database |
|
97 CContactItem* groupContact = database()->ReadContactLC(groupId->localId()); |
|
98 CContactItem* contact = database()->ReadContactLC(contactId->localId()); |
|
99 |
|
100 //Check if contact is part of the group |
|
101 isGroupMemberL(contact, groupContact->Id()); |
|
102 |
|
103 //remove contact doesn't return an error if the group is not part of the group |
|
104 database()->RemoveContactFromGroupL(contact->Id(), groupContact->Id()); |
|
105 |
|
106 //add the removed group member to the list of affected contacts |
|
107 affectedContactIds->insert(groupId->localId()); |
|
108 affectedContactIds->insert(contactId->localId()); |
|
109 |
|
110 CleanupStack::PopAndDestroy(contact); |
|
111 CleanupStack::PopAndDestroy(groupContact); |
|
112 |
|
113 *error = QContactManager::NoError; |
|
114 return true; |
|
115 } |
|
116 |
|
117 bool CntRelationshipGroup::validateRelationship(const QContactRelationship &relationship, QContactManager::Error* error) |
|
118 { |
|
119 // check that "second" is in this manager |
|
120 if (!relationship.second().managerUri().isEmpty() && relationship.second().managerUri() != managerUri()) { |
|
121 *error = QContactManager::InvalidRelationshipError; |
|
122 return false; |
|
123 } |
|
124 |
|
125 return true; |
|
126 } |
|
127 |
|
128 /* |
|
129 * Function will leave with KErrNotFound if contact is not part of the specific goup |
|
130 * |
|
131 */ |
|
132 void CntRelationshipGroup::isGroupMemberL(const CContactItem* contactItem, const TContactItemId groupId) const |
|
133 { |
|
134 if(contactItem && |
|
135 (contactItem->Type() == KUidContactCard |
|
136 || contactItem->Type() == KUidContactOwnCard |
|
137 || contactItem->Type() == KUidContactGroup |
|
138 || contactItem->Type() == KUidContactICCEntry)) |
|
139 { |
|
140 //cast the contact item to base class for groups and contacts |
|
141 const CContactItemPlusGroup *contact = static_cast<const CContactItemPlusGroup *>(contactItem); |
|
142 |
|
143 const CContactIdArray *idArray = contact->GroupsJoined(); |
|
144 |
|
145 //array is null if contact is not part of any groups |
|
146 if(idArray) |
|
147 { |
|
148 //find the group from the array |
|
149 TInt arrayIndex = idArray->Find(groupId); |
|
150 |
|
151 //if it doesn't exist leave |
|
152 if(arrayIndex == KErrNotFound) |
|
153 { |
|
154 User::Leave(KErrNotFound); |
|
155 } |
|
156 } |
|
157 } |
|
158 else |
|
159 { |
|
160 User::Leave(KErrNotFound); |
|
161 } |
|
162 } |
|
163 |
|
164 //retrieve all the groups that the contact is part of |
|
165 QList<QContactRelationship> CntRelationshipGroup::relationshipsL(const QContactId& participantId, QContactRelationship::Role role, QContactManager::Error* error) |
|
166 { |
|
167 QList<QContactRelationship> returnValue; |
|
168 |
|
169 if (participantId != QContactId()) |
|
170 { |
|
171 //role is a group |
|
172 if(role == QContactRelationship::First || role == QContactRelationship::Either) |
|
173 { |
|
174 fetchGroupMembersL(participantId.localId(), &returnValue); |
|
175 } |
|
176 |
|
177 //role is member of a group |
|
178 if(role == QContactRelationship::Second || role == QContactRelationship::Either) |
|
179 { |
|
180 fetchMemberOfGroupsL(participantId.localId(), &returnValue); |
|
181 } |
|
182 } |
|
183 else |
|
184 { |
|
185 // If participantId is the default-constructed id, role is ignored and |
|
186 // all relationships of the given relationshipType are returned. |
|
187 fetchMembersOfAllGroupsL(&returnValue); |
|
188 } |
|
189 |
|
190 if (returnValue.isEmpty()) |
|
191 *error = QContactManager::DoesNotExistError; |
|
192 |
|
193 return returnValue; |
|
194 } |
|
195 |
|
196 void CntRelationshipGroup::fetchGroupMembersL(const QContactLocalId& participantId, QList<QContactRelationship> *relationships) |
|
197 { |
|
198 //fetch the contact item from the database |
|
199 CContactItem *contactItem = database()->ReadContactLC(TContactItemId(participantId)); |
|
200 |
|
201 //make sure it's a group |
|
202 if(contactItem && contactItem->Type() == KUidContactGroup) |
|
203 { |
|
204 //cast the contact to a group |
|
205 CContactGroup *groupContact = static_cast<CContactGroup*>( contactItem ); |
|
206 |
|
207 //create the relationship |
|
208 QContactRelationship relationship; |
|
209 relationship.setRelationshipType(QContactRelationship::HasMember); |
|
210 |
|
211 //get the group contacts |
|
212 const CContactIdArray *idArray = groupContact->ItemsContained(); |
|
213 |
|
214 QContactId groupId; |
|
215 |
|
216 //loop through all the contacts and add them to the list |
|
217 for(int i = 0; i < idArray->Count(); i++ ) |
|
218 { |
|
219 //set participant id as first id |
|
220 QContactId first; |
|
221 first.setLocalId(participantId); |
|
222 first.setManagerUri(managerUri()); |
|
223 relationship.setFirst(first); |
|
224 |
|
225 //set the manager uri |
|
226 groupId.setManagerUri(managerUri()); |
|
227 |
|
228 groupId.setLocalId(idArray->operator[](i)); |
|
229 relationship.setSecond(groupId); |
|
230 |
|
231 *relationships << relationship; |
|
232 } |
|
233 } |
|
234 |
|
235 CleanupStack::PopAndDestroy(contactItem); |
|
236 } |
|
237 |
|
238 |
|
239 void CntRelationshipGroup::fetchMemberOfGroupsL(const QContactLocalId& participantId, QList<QContactRelationship> *relationships) |
|
240 { |
|
241 CContactItem *contactItem = database()->ReadContactLC(TContactItemId(participantId)); |
|
242 |
|
243 if(contactItem && |
|
244 (contactItem->Type() == KUidContactCard |
|
245 || contactItem->Type() == KUidContactOwnCard |
|
246 || contactItem->Type() == KUidContactGroup |
|
247 || contactItem->Type() == KUidContactICCEntry)) |
|
248 { |
|
249 //cast the contact item to base class for groups and contacts |
|
250 CContactItemPlusGroup *contact = static_cast<CContactItemPlusGroup *>(contactItem); |
|
251 |
|
252 //create the relationship |
|
253 QContactRelationship relationship; |
|
254 relationship.setRelationshipType(QContactRelationship::HasMember); |
|
255 |
|
256 const CContactIdArray *idArray = contact->GroupsJoined(); |
|
257 |
|
258 if(idArray) { |
|
259 QContactId groupId; |
|
260 |
|
261 //loop through all the contacts and add them to the list |
|
262 for(int i = 0; i < idArray->Count(); i++ ) |
|
263 { |
|
264 //set the manager uri |
|
265 groupId.setManagerUri(managerUri()); |
|
266 groupId.setLocalId(idArray->operator[](i)); |
|
267 |
|
268 //set the group as first |
|
269 relationship.setFirst(groupId); |
|
270 |
|
271 //set participant id as member of group |
|
272 QContactId second; |
|
273 second.setLocalId(participantId); |
|
274 second.setManagerUri(managerUri()); |
|
275 relationship.setSecond(second); |
|
276 |
|
277 *relationships << relationship; |
|
278 } |
|
279 } |
|
280 } |
|
281 |
|
282 CleanupStack::PopAndDestroy(contactItem); |
|
283 } |
|
284 |
|
285 void CntRelationshipGroup::fetchMembersOfAllGroupsL(QList<QContactRelationship> *relationships) |
|
286 { |
|
287 CContactIdArray *groupIdList = database()->GetGroupIdListL(); |
|
288 CleanupStack::PushL(groupIdList); |
|
289 |
|
290 int count = groupIdList->Count(); |
|
291 for (int i=0; i<count; i++) |
|
292 { |
|
293 fetchGroupMembersL(QContactLocalId((*groupIdList)[i]), relationships); |
|
294 } |
|
295 |
|
296 CleanupStack::PopAndDestroy(groupIdList); |
|
297 } |