|
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 "cntsavemanager.h" |
|
19 #include "cntdebug.h" |
|
20 |
|
21 /*! |
|
22 Contact saving helper class. Used mainly in edit view for contact saving, |
|
23 but should also be used in other (special) cases a contact needs to be saved. |
|
24 For example "forcing" phonebook to shut down (end key, from task swapper etc..) |
|
25 in detail editors. |
|
26 */ |
|
27 CntSaveManager::CntSaveManager(CntContactType type, QObject* parent) : |
|
28 QObject(parent), |
|
29 mContactType(type) |
|
30 { |
|
31 CNT_ENTRY |
|
32 |
|
33 CNT_EXIT |
|
34 } |
|
35 |
|
36 /*! |
|
37 Destructor |
|
38 */ |
|
39 CntSaveManager::~CntSaveManager() |
|
40 { |
|
41 CNT_ENTRY |
|
42 |
|
43 CNT_EXIT |
|
44 } |
|
45 |
|
46 /*! |
|
47 Saves the given QContact to the given QContactManager. Also takes care of checking |
|
48 if the contact is MyCard or a group and behaves different accordingly. |
|
49 |
|
50 \param aContact the contact to be saved, ownership not taken |
|
51 \param aManager the QContactManager which should be used for saving the contact, ownership not taken |
|
52 \return CntSaveResult enum to describe what was done to the contact (saved, updated etc...) |
|
53 */ |
|
54 CntSaveManager::CntSaveResult CntSaveManager::saveContact(QContact* aContact, QContactManager* aManager) |
|
55 { |
|
56 CNT_ENTRY |
|
57 |
|
58 CntSaveResult result = ENothingDone; |
|
59 bool isSavedContact = aContact->localId() > 0; |
|
60 |
|
61 // if the contact is really changed or a new one |
|
62 if ( (*aContact) != aManager->contact(aContact->localId()) || !isSavedContact ) |
|
63 { |
|
64 int detailCount = aContact->details().count(); |
|
65 |
|
66 // Don't set preferred details for a group |
|
67 if (mContactType != EGroup) |
|
68 { |
|
69 setPreferredDetails( aContact ); |
|
70 } |
|
71 |
|
72 // If its a new contact |
|
73 if ( !isSavedContact ) |
|
74 { |
|
75 // if a new contact has 2 or less details, it's still considered empty |
|
76 if ( detailCount > 2 ) |
|
77 { |
|
78 bool success = aManager->saveContact( aContact ); |
|
79 if ( success && mContactType == EMyCard ) |
|
80 { |
|
81 aManager->setSelfContactId( aContact->localId() ); |
|
82 } |
|
83 |
|
84 result = success ? ESaved : EFailed; |
|
85 } |
|
86 } |
|
87 else |
|
88 { |
|
89 // Contact details has been cleared out, a symbian QContact with |
|
90 // 4 or less details is in reality empty. |
|
91 if ( detailCount <= 4 ) |
|
92 { |
|
93 bool success = aManager->removeContact( aContact->localId() ); |
|
94 |
|
95 result = success ? EDeleted : EFailed; |
|
96 } |
|
97 else |
|
98 { |
|
99 bool success = aManager->saveContact(aContact); |
|
100 |
|
101 result = success ? EUpdated : EFailed; |
|
102 } |
|
103 } |
|
104 } |
|
105 |
|
106 CNT_EXIT_ARGS(result) |
|
107 |
|
108 return result; |
|
109 } |
|
110 |
|
111 /*! |
|
112 Set the preferred call, sms and email details for a contact if possible |
|
113 and needed. |
|
114 |
|
115 \param aContact the contact being handled |
|
116 */ |
|
117 void CntSaveManager::setPreferredDetails( QContact* aContact ) |
|
118 { |
|
119 CNT_ENTRY |
|
120 |
|
121 QList<QContactPhoneNumber> numberList( aContact->details<QContactPhoneNumber>() ); |
|
122 //set preferred number for call if there is only one phone number |
|
123 if ( aContact->preferredDetail("call").isEmpty() && numberList.count() == 1 ) |
|
124 { |
|
125 aContact->setPreferredDetail( "call", numberList.first() ); |
|
126 } |
|
127 //set preferred number for message if there is only one mobile phone number |
|
128 if ( aContact->preferredDetail("message").isEmpty() && numberList.count() >= 1 ) |
|
129 { |
|
130 int mobileNumbers = 0; |
|
131 int mobileNumberIndex = -1; |
|
132 for (int i = 0; i < numberList.count(); i++) |
|
133 { |
|
134 QStringList subTypes = numberList.at(i).subTypes(); |
|
135 if (subTypes.count() && subTypes.first() == QContactPhoneNumber::SubTypeMobile) |
|
136 { |
|
137 mobileNumbers++; |
|
138 mobileNumberIndex = i; |
|
139 } |
|
140 } |
|
141 if ( mobileNumbers == 1 ) |
|
142 { |
|
143 aContact->setPreferredDetail( "message", numberList.at(mobileNumberIndex) ); |
|
144 } |
|
145 } |
|
146 QList<QContactEmailAddress> emailList( aContact->details<QContactEmailAddress>() ); |
|
147 //set preferred number for email if there is only one email address |
|
148 if ( aContact->preferredDetail("email").isEmpty() && emailList.count() == 1 ) |
|
149 { |
|
150 aContact->setPreferredDetail( "email", emailList.first() ); |
|
151 } |
|
152 |
|
153 CNT_EXIT |
|
154 } |