|
1 /* |
|
2 * Copyright (c) 2005-2007 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: Phonebook 2 assign service field validator. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "Pbk2AssignValidateField.h" |
|
20 |
|
21 // Phonebook 2 |
|
22 #include <CPbk2PresentationContact.h> |
|
23 #include <CPbk2PresentationContactFieldCollection.h> |
|
24 |
|
25 // Virtual Phonebook |
|
26 #include <MVPbkContactStore.h> |
|
27 #include <MVPbkContactStoreProperties.h> |
|
28 #include <MVPbkContactFieldTextData.h> |
|
29 #include <MVPbkFieldType.h> |
|
30 #include <VPbkEng.rsg> |
|
31 |
|
32 |
|
33 /// Unnamed namespace for local definitions |
|
34 namespace { |
|
35 |
|
36 const TInt KOne = 1; |
|
37 |
|
38 /** |
|
39 * Checks does a given file exist. |
|
40 * |
|
41 * @param aFs File server session. |
|
42 * @param aFileName Name of the file to check. |
|
43 * @return ETrue if file exists. |
|
44 */ |
|
45 inline TBool FileExists( RFs& aFs, const TDesC& aFileName ) |
|
46 { |
|
47 TEntry ignore; |
|
48 return ( aFs.Entry( aFileName, ignore ) == KErrNone ); |
|
49 } |
|
50 |
|
51 /** |
|
52 * Returns the amount of fields of given type in given contact. |
|
53 * |
|
54 * @param aContact The contact to inspect. |
|
55 * @param aFieldType The field type to inspect. |
|
56 * @return Amount of fields of given type in the given contact. |
|
57 */ |
|
58 TInt CurrentAmountOfFieldTypeInContact |
|
59 ( CPbk2PresentationContact& aContact, |
|
60 const MVPbkFieldType& aFieldType ) |
|
61 { |
|
62 TInt res = 0; |
|
63 |
|
64 // Count of contact fields |
|
65 const TInt count = aContact.Fields().FieldCount(); |
|
66 |
|
67 const TInt maxMatchPriority = aContact.ContactStore(). |
|
68 StoreProperties().SupportedFields().MaxMatchPriority(); |
|
69 |
|
70 for ( TInt i = 0; i < count; ++i ) |
|
71 { |
|
72 const MVPbkFieldType* type = NULL; |
|
73 const MVPbkStoreContactField& field = aContact.Fields().FieldAt( i ); |
|
74 |
|
75 for ( TInt j = 0; j < maxMatchPriority && !type; ++j ) |
|
76 { |
|
77 type = field.MatchFieldType( j ); |
|
78 } |
|
79 |
|
80 if ( (aFieldType.IsSame( *type ) ) ) |
|
81 { |
|
82 ++res; |
|
83 } |
|
84 } |
|
85 |
|
86 return res; |
|
87 } |
|
88 |
|
89 } /// namespace |
|
90 |
|
91 // -------------------------------------------------------------------------- |
|
92 // Pbk2AssignValidateField::ValidateFieldTypeUsageInContactL |
|
93 // -------------------------------------------------------------------------- |
|
94 // |
|
95 TInt Pbk2AssignValidateField::ValidateFieldTypeUsageInContactL |
|
96 ( CPbk2PresentationContact& aContact, |
|
97 const MVPbkFieldType& aFieldType, RFs& aFsSession, |
|
98 TInt& aFieldIndex ) |
|
99 { |
|
100 TInt ret = KErrNotFound; |
|
101 |
|
102 const MVPbkStoreContactFieldCollection& fields = aContact.Fields(); |
|
103 const MVPbkStoreContactField* field = NULL; |
|
104 const TInt fieldCount = fields.FieldCount(); |
|
105 |
|
106 for ( TInt i( 0 ); i < fieldCount; ++i) |
|
107 { |
|
108 field = &fields.FieldAt(i); |
|
109 const MVPbkFieldType* fieldType = field->BestMatchingFieldType(); |
|
110 |
|
111 if ( fieldType && fieldType->IsSame( aFieldType ) ) |
|
112 { |
|
113 // Field type was found from contact |
|
114 ret = KErrNone; |
|
115 aFieldIndex = aContact.PresentationFields().StoreIndexOfField( i ); |
|
116 break; |
|
117 } |
|
118 } |
|
119 |
|
120 if ( ret == KErrNone ) |
|
121 { |
|
122 // Field was found from contact but if it was a ringing tone or an |
|
123 // image field do a file exist check. If the file those fields are |
|
124 // referencing is not found from the file system, the field is |
|
125 // hidden in Phonebook UI. It is not removed, but hidden because |
|
126 // the file might reside on a removable media and come back later on. |
|
127 // |
|
128 // The logic here is to not to ask overwrite contact field |
|
129 // confirmation from the user if the file does not currently exist. |
|
130 |
|
131 TInt fieldTypeResId = aFieldType.FieldTypeResId(); |
|
132 if ( fieldTypeResId == R_VPBK_FIELD_TYPE_RINGTONE || |
|
133 fieldTypeResId == R_VPBK_FIELD_TYPE_CALLEROBJIMG ) |
|
134 { |
|
135 const MVPbkContactFieldTextData& textData = |
|
136 MVPbkContactFieldTextData::Cast( field->FieldData() ); |
|
137 |
|
138 if ( !FileExists( aFsSession, textData.Text() ) ) |
|
139 { |
|
140 // The file the contact field indicates is not |
|
141 // on the file system, return KErrPathNotFound to indicate |
|
142 // that the contact haves this field but the file refenced |
|
143 // is not found |
|
144 ret = KErrPathNotFound; |
|
145 // Do not change the aFieldIndex value here though. |
|
146 } |
|
147 } |
|
148 } |
|
149 |
|
150 if ( ret == KErrNone ) |
|
151 { |
|
152 // Get maximum number of fields of given type in the contact |
|
153 const TInt max = aContact.MaxNumberOfFieldL( aFieldType ); |
|
154 |
|
155 if ( max != KVPbkStoreContactUnlimitedNumber ) |
|
156 { |
|
157 // Get current amount of fields of given type in the contact |
|
158 TInt current = CurrentAmountOfFieldTypeInContact |
|
159 ( aContact, aFieldType ); |
|
160 |
|
161 if ( current >= max ) |
|
162 { |
|
163 // Return code depends on multiplicity |
|
164 if ( max == KOne ) |
|
165 { |
|
166 ret = KErrAlreadyExists; |
|
167 } |
|
168 else |
|
169 { |
|
170 ret = KErrNotSupported; |
|
171 } |
|
172 } |
|
173 } |
|
174 } |
|
175 |
|
176 return ret; |
|
177 } |
|
178 |
|
179 // End of File |