13 * |
13 * |
14 * Description: |
14 * Description: |
15 * |
15 * |
16 */ |
16 */ |
17 #include "cntfavourite.h" |
17 #include "cntfavourite.h" |
|
18 #include <cntglobal.h> |
|
19 #include <QDebug> |
18 |
20 |
19 QContactLocalId CntFavourite::createFavouriteGroup( QContactManager* aManager ) |
21 int CntFavourite::mCachedSymbianFavouriteId = 0; |
|
22 |
|
23 QContactLocalId CntFavourite::createFavouriteGroup(QContactManager* aManager) |
20 { |
24 { |
21 QContactLocalId favouriteId = CntFavourite::favouriteGroupId(aManager); |
25 QContactLocalId favouriteId = CntFavourite::favouriteGroupId(aManager); |
22 |
26 |
23 if ( favouriteId == 0 ) |
27 if (favouriteId == 0) { |
24 { |
|
25 QContact favouriteGroup; |
28 QContact favouriteGroup; |
26 favouriteGroup.setType( QContactType::TypeGroup ); |
29 favouriteGroup.setType( QContactType::TypeGroup ); |
27 |
30 |
28 QContactName favouriteGroupName; |
31 QContactName favouriteGroupName; |
29 favouriteGroupName.setCustomLabel( QLatin1String(FavouriteGroupName) ); |
32 favouriteGroupName.setCustomLabel( QLatin1String(FavouriteGroupName) ); |
30 |
33 |
31 favouriteGroup.saveDetail( &favouriteGroupName ); |
34 favouriteGroup.saveDetail(&favouriteGroupName); |
32 if ( aManager->saveContact( &favouriteGroup ) ) |
35 if (aManager->saveContact(&favouriteGroup)) { |
33 { |
|
34 favouriteId = favouriteGroup.localId(); |
36 favouriteId = favouriteGroup.localId(); |
|
37 if (aManager->managerUri() == SYMBIAN_BACKEND) { |
|
38 mCachedSymbianFavouriteId = favouriteId; |
|
39 } |
35 } |
40 } |
36 } |
41 } |
|
42 |
37 return favouriteId; |
43 return favouriteId; |
38 } |
44 } |
39 |
45 |
40 QContactLocalId CntFavourite::favouriteGroupId( QContactManager* aManager ) |
46 QContactLocalId CntFavourite::favouriteGroupId(QContactManager* aManager) |
41 { |
47 { |
42 QContactLocalId favouriteId( 0 ); |
48 QContactLocalId favouriteId(0); |
|
49 |
|
50 if (mCachedSymbianFavouriteId != 0 && aManager->managerUri() == SYMBIAN_BACKEND) { |
|
51 favouriteId = mCachedSymbianFavouriteId; |
|
52 } else { |
|
53 QContactDetailFilter groupFilter; |
|
54 groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType); |
|
55 groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup))); |
43 |
56 |
44 QContactDetailFilter groupFilter; |
57 QList<QContactLocalId> groupContactIds = aManager->contactIds( groupFilter ); |
45 groupFilter.setDetailDefinitionName(QContactType::DefinitionName, QContactType::FieldType); |
58 |
46 groupFilter.setValue(QString(QLatin1String(QContactType::TypeGroup))); |
59 for (int i = 0; i < groupContactIds.count(); i++) { |
47 |
60 QContactFetchHint noRelationshipsFetchHint; |
48 QList<QContactLocalId> groupContactIds = aManager->contactIds( groupFilter ); |
61 noRelationshipsFetchHint.setOptimizationHints(QContactFetchHint::NoRelationships); |
49 |
62 QContact contact = aManager->contact(groupContactIds.at(i), noRelationshipsFetchHint); |
50 for(int i = 0;i < groupContactIds.count();i++) |
63 QContactName contactName = contact.detail<QContactName>(); |
51 { |
64 QString groupName = contactName.customLabel(); |
52 QContact contact = aManager->contact(groupContactIds.at(i)); |
65 if (groupName.compare(QLatin1String(FavouriteGroupName)) == 0) { |
53 QContactName contactName = contact.detail<QContactName>(); |
66 favouriteId = groupContactIds.at(i); |
54 QString groupName = contactName.customLabel(); |
67 if (aManager->managerUri() == SYMBIAN_BACKEND) { |
55 if ( groupName.compare(QLatin1String(FavouriteGroupName)) == 0 ) |
68 mCachedSymbianFavouriteId = favouriteId; |
56 { |
69 } |
57 favouriteId = groupContactIds.at(i); |
70 break; |
58 break; |
71 } |
59 } |
72 } |
60 } |
73 } |
61 |
74 |
62 return favouriteId; |
75 return favouriteId; |
63 } |
76 } |
64 |
77 |
65 bool CntFavourite::isMemberOfFavouriteGroup( QContactManager* aManager, QContact* aContact ) |
78 bool CntFavourite::isMemberOfFavouriteGroup(QContactManager* contactManager, QContact* contact) |
66 { |
79 { |
67 bool favouriteGroupContact( false ); |
80 QContactId favouriteId; |
68 QContactLocalId favouriteId = CntFavourite::favouriteGroupId( aManager ); |
81 favouriteId.setManagerUri(contactManager->managerUri()); |
69 if ( favouriteId != 0 ) |
82 favouriteId.setLocalId(CntFavourite::favouriteGroupId(contactManager)); |
70 { |
83 |
71 QContact favoriteGroup = aManager->contact( favouriteId ); |
84 QContactRelationship favouriteMembership; |
72 QContactRelationshipFilter rFilter; |
85 favouriteMembership.setFirst(favouriteId); |
73 rFilter.setRelationshipType( QContactRelationship::HasMember ); |
86 favouriteMembership.setSecond(contact->id()); |
74 rFilter.setRelatedContactRole( QContactRelationship::First ); |
87 favouriteMembership.setRelationshipType(QContactRelationship::HasMember); |
75 rFilter.setRelatedContactId( favoriteGroup.id() ); |
88 |
76 |
89 return contact->relationships().contains(favouriteMembership); |
77 QList<QContactLocalId> contactsLocalIdList = aManager->contactIds( rFilter ); |
|
78 favouriteGroupContact = contactsLocalIdList.contains( aContact->localId() ); |
|
79 } |
|
80 return favouriteGroupContact; |
|
81 } |
90 } |
82 |
91 |
83 void CntFavourite::addContactToFavouriteGroup( QContactManager* aManager, QContactId& aId) |
92 void CntFavourite::addContactToFavouriteGroup( QContactManager* aManager, QContactId& aId) |
84 { |
93 { |
85 QContactLocalId favouriteId = CntFavourite::favouriteGroupId( aManager ); |
94 QContactLocalId favouriteId = CntFavourite::favouriteGroupId( aManager ); |
98 |
107 |
99 // save relationship |
108 // save relationship |
100 aManager->saveRelationship( &relationship ); |
109 aManager->saveRelationship( &relationship ); |
101 } |
110 } |
102 |
111 |
103 void CntFavourite::removeContactFromFavouriteGroup( QContactManager* aManager, QContactId& aId ) |
112 void CntFavourite::removeContactFromFavouriteGroup(QContactManager* aManager, QContactId& aId) |
104 { |
113 { |
105 QContactLocalId favouriteId = CntFavourite::favouriteGroupId( aManager ); |
114 QContactLocalId favouriteId = CntFavourite::favouriteGroupId(aManager); |
106 if ( favouriteId != 0 ) |
115 if (favouriteId != 0) { |
107 { |
116 QContact favoriteGroup = aManager->contact(favouriteId); |
108 QContact favoriteGroup = aManager->contact( favouriteId ); |
|
109 QContactRelationship relationship; |
117 QContactRelationship relationship; |
110 relationship.setRelationshipType( QContactRelationship::HasMember ); |
118 relationship.setRelationshipType(QContactRelationship::HasMember); |
111 relationship.setFirst( favoriteGroup.id() ); |
119 relationship.setFirst(favoriteGroup.id()); |
112 relationship.setSecond( aId ); |
120 relationship.setSecond(aId); |
113 aManager->removeRelationship(relationship); |
121 aManager->removeRelationship(relationship); |
114 } |
122 } |
115 } |
123 } |
|
124 |
116 // End of File |
125 // End of File |