|
1 /* |
|
2 * Copyright (c) 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: Property object for Alfred |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #include "alf/alfpropertyowner.h" |
|
21 |
|
22 |
|
23 // --------------------------------------------------------------------------- |
|
24 // Constructor |
|
25 // --------------------------------------------------------------------------- |
|
26 // |
|
27 EXPORT_C CAlfPropertyOwner::CAlfPropertyOwner() |
|
28 { |
|
29 |
|
30 } |
|
31 |
|
32 // --------------------------------------------------------------------------- |
|
33 // Destructor |
|
34 // --------------------------------------------------------------------------- |
|
35 // |
|
36 EXPORT_C CAlfPropertyOwner::~CAlfPropertyOwner() |
|
37 { |
|
38 PropertyClear(); |
|
39 } |
|
40 |
|
41 // --------------------------------------------------------------------------- |
|
42 // Remove property |
|
43 // --------------------------------------------------------------------------- |
|
44 // |
|
45 EXPORT_C TBool CAlfPropertyOwner::PropertyRemove(const TDesC8& aName) |
|
46 { |
|
47 TInt index; |
|
48 |
|
49 if (!Find(aName, &index)) |
|
50 { |
|
51 return EFalse; |
|
52 } |
|
53 |
|
54 delete iProperties[index]; |
|
55 iProperties.Remove(index); |
|
56 iProperties.Compress(); |
|
57 |
|
58 return ETrue; |
|
59 } |
|
60 |
|
61 // --------------------------------------------------------------------------- |
|
62 // Clear property |
|
63 // --------------------------------------------------------------------------- |
|
64 // |
|
65 EXPORT_C void CAlfPropertyOwner::PropertyClear() |
|
66 { |
|
67 iProperties.ResetAndDestroy(); |
|
68 } |
|
69 |
|
70 |
|
71 // We have a different registration method for each property type. |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // Set integer property |
|
75 // --------------------------------------------------------------------------- |
|
76 // |
|
77 EXPORT_C TBool CAlfPropertyOwner::PropertySetIntegerL(const TDesC8& aName, TInt aVal, TInt aMin, TInt aMax) |
|
78 { |
|
79 CAlfPropertyInteger* property; |
|
80 TAlfPropertyType type; |
|
81 TInt index; |
|
82 |
|
83 if (aName.Length() == 0) |
|
84 { |
|
85 // Cannot have zero-length name as an argument. |
|
86 return EFalse; |
|
87 } |
|
88 |
|
89 if (Find(aName, &index, &type)) |
|
90 { |
|
91 // Property exists already. |
|
92 if (type != EAlfPropertyTypeInteger) |
|
93 { |
|
94 // Wrong type. We have to exit without doing anything. |
|
95 return EFalse; |
|
96 } |
|
97 |
|
98 // Modify existing property. |
|
99 property = static_cast<CAlfPropertyInteger*>(iProperties[index]); |
|
100 property->Set(TRangedValue(aVal, aMin, aMax)); |
|
101 } |
|
102 else |
|
103 { |
|
104 // Not found, so add new item to the property list. |
|
105 property = new (ELeave) CAlfPropertyInteger(); |
|
106 CleanupStack::PushL(property); |
|
107 property->ConstructL(aName); |
|
108 CleanupStack::Pop(property); |
|
109 |
|
110 property->Set(TRangedValue(aVal, aMin, aMax)); |
|
111 |
|
112 User::LeaveIfError(iProperties.Append(property)); |
|
113 } |
|
114 |
|
115 return ETrue; |
|
116 } |
|
117 |
|
118 // --------------------------------------------------------------------------- |
|
119 // Find integer property |
|
120 // --------------------------------------------------------------------------- |
|
121 // |
|
122 EXPORT_C TBool CAlfPropertyOwner::PropertyFindInteger(const TDesC8& aName, TInt* aValue) const |
|
123 { |
|
124 TInt index; |
|
125 |
|
126 if (!FindOfType(aName, &index, EAlfPropertyTypeInteger)) |
|
127 { |
|
128 return EFalse; |
|
129 } |
|
130 |
|
131 if (aValue) |
|
132 { |
|
133 // Clamp the value. |
|
134 CAlfPropertyInteger* foundVal = static_cast<CAlfPropertyInteger*>(iProperties[index]); |
|
135 |
|
136 *aValue = Min(foundVal->Data().iValue, foundVal->Data().iMax); |
|
137 *aValue = Max(*aValue, foundVal->Data().iMin); |
|
138 } |
|
139 |
|
140 return ETrue; |
|
141 } |
|
142 |
|
143 // --------------------------------------------------------------------------- |
|
144 // Find complete ranged value |
|
145 // --------------------------------------------------------------------------- |
|
146 // |
|
147 TBool CAlfPropertyOwner::PropertyFindInteger(const TDesC8& aName, TRangedValue& aValue) const |
|
148 { |
|
149 TInt index; |
|
150 |
|
151 if (FindOfType(aName, &index, EAlfPropertyTypeInteger)) |
|
152 { |
|
153 CAlfPropertyInteger* foundVal = static_cast<CAlfPropertyInteger*>(iProperties[index]); |
|
154 |
|
155 aValue = foundVal->Data(); |
|
156 return ETrue; |
|
157 } |
|
158 |
|
159 return EFalse; |
|
160 } |
|
161 |
|
162 // --------------------------------------------------------------------------- |
|
163 // Set string property |
|
164 // --------------------------------------------------------------------------- |
|
165 // |
|
166 EXPORT_C TBool CAlfPropertyOwner::PropertySetStringL(const TDesC8& aName, const TDesC& aValue) |
|
167 { |
|
168 CAlfPropertyString* property; |
|
169 TAlfPropertyType type; |
|
170 TInt index; |
|
171 |
|
172 if (aName.Length() == 0) |
|
173 { |
|
174 // Cannot have zero-length name as an argument. |
|
175 return EFalse; |
|
176 } |
|
177 |
|
178 if (Find(aName, &index, &type)) |
|
179 { |
|
180 // Property exists already. |
|
181 if (type != EAlfPropertyTypeString) |
|
182 { |
|
183 // Wrong type. We have to exit without doing anything. |
|
184 return EFalse; |
|
185 } |
|
186 |
|
187 // Modify existing property. |
|
188 property = static_cast<CAlfPropertyString*>(iProperties[index]); |
|
189 property->SetL(aValue); |
|
190 } |
|
191 else |
|
192 { |
|
193 // Not found, so add new item to the property list. |
|
194 property = new (ELeave) CAlfPropertyString(); |
|
195 CleanupStack::PushL(property); |
|
196 property->ConstructL(aName); |
|
197 CleanupStack::Pop(property); |
|
198 |
|
199 property->SetL(aValue); |
|
200 |
|
201 User::LeaveIfError(iProperties.Append(property)); |
|
202 } |
|
203 |
|
204 return ETrue; |
|
205 } |
|
206 |
|
207 |
|
208 // --------------------------------------------------------------------------- |
|
209 // Find string property |
|
210 // --------------------------------------------------------------------------- |
|
211 // |
|
212 EXPORT_C TBool CAlfPropertyOwner::PropertyFindString(const TDesC8& aName, TDesC** aValue) const |
|
213 { |
|
214 TInt index; |
|
215 |
|
216 if (!FindOfType(aName, &index, EAlfPropertyTypeString)) |
|
217 { |
|
218 return EFalse; |
|
219 } |
|
220 |
|
221 CAlfPropertyString* foundVal = static_cast<CAlfPropertyString*>(iProperties[index]); |
|
222 |
|
223 if (aValue) |
|
224 { |
|
225 *aValue = foundVal->Data(); |
|
226 } |
|
227 |
|
228 return ETrue; |
|
229 } |
|
230 |
|
231 |
|
232 // Private methods |
|
233 |
|
234 // --------------------------------------------------------------------------- |
|
235 // Find property |
|
236 // --------------------------------------------------------------------------- |
|
237 // |
|
238 TBool CAlfPropertyOwner::Find(const TDesC8& aName, TInt* aIndex, TAlfPropertyType* aType) const |
|
239 { |
|
240 TIdentityRelation<CAlfProperty> matchName( CAlfProperty::Matches ); |
|
241 |
|
242 if (iProperties.Count() == 0) |
|
243 { |
|
244 // Array is empty, nothing to find. |
|
245 return false; |
|
246 } |
|
247 |
|
248 // Create a temporary property with the name we want to find. |
|
249 CAlfProperty* toFind = new CAlfProperty(); |
|
250 if ( !toFind ) |
|
251 { |
|
252 return EFalse; |
|
253 } |
|
254 |
|
255 TRAPD( err, toFind->ConstructL(aName) ); |
|
256 if ( err != KErrNone ) |
|
257 { |
|
258 delete toFind; |
|
259 return EFalse; |
|
260 } |
|
261 |
|
262 TInt found = iProperties.Find(toFind, matchName); |
|
263 delete toFind; |
|
264 |
|
265 if (found == KErrNotFound) |
|
266 { |
|
267 return EFalse; |
|
268 } |
|
269 |
|
270 CAlfProperty* foundProperty = iProperties[found]; |
|
271 |
|
272 if (aIndex) |
|
273 { |
|
274 *aIndex = found; |
|
275 } |
|
276 |
|
277 if (aType) |
|
278 { |
|
279 *aType = foundProperty->Type(); |
|
280 } |
|
281 |
|
282 return ETrue; |
|
283 } |
|
284 |
|
285 // --------------------------------------------------------------------------- |
|
286 // Find property and check for the correct type |
|
287 // --------------------------------------------------------------------------- |
|
288 // |
|
289 TBool CAlfPropertyOwner::FindOfType(const TDesC8& aName, TInt* aIndex, TAlfPropertyType aRequiredType) const |
|
290 { |
|
291 TAlfPropertyType foundType; |
|
292 |
|
293 if (!Find(aName, aIndex, &foundType)) |
|
294 { |
|
295 return EFalse; |
|
296 } |
|
297 |
|
298 if (foundType != aRequiredType) |
|
299 { |
|
300 return EFalse; |
|
301 } |
|
302 |
|
303 // Return, with find index stored in return parameter. |
|
304 return ETrue; |
|
305 } |
|
306 |
|
307 |
|
308 EXPORT_C TInt CAlfPropertyOwner::CopyProperty(const CAlfPropertyOwner& aOwner, const TDesC8& aPropertyName) |
|
309 { |
|
310 TInt index(0), result(KErrNone); |
|
311 TAlfPropertyType propertyType; |
|
312 |
|
313 if (aOwner.Find(aPropertyName, &index, &propertyType)) |
|
314 { |
|
315 switch (propertyType) |
|
316 { |
|
317 case EAlfPropertyTypeInteger: |
|
318 { |
|
319 TRangedValue value; |
|
320 aOwner.PropertyFindInteger(aPropertyName, value); // we know it exists |
|
321 TRAP(result, PropertySetIntegerL(aPropertyName, value.iValue, value.iMin, value.iMax)) |
|
322 break; |
|
323 } |
|
324 case EAlfPropertyTypeString: |
|
325 { |
|
326 TDesC* string = 0; |
|
327 aOwner.PropertyFindString(aPropertyName, &string); // we know it exists |
|
328 TRAP(result, PropertySetStringL(aPropertyName, *string)) |
|
329 break; |
|
330 } |
|
331 default: |
|
332 result = KErrNotSupported; |
|
333 break; |
|
334 } |
|
335 } |
|
336 else |
|
337 { |
|
338 result = KErrNotFound; |
|
339 } |
|
340 |
|
341 return result; |
|
342 } |
|
343 |
|
344 EXPORT_C void CAlfPropertyOwner::PropertyOwnerExtension(const TUid& /*aExtensionUid*/, TAny** /*aExtensionParams*/) |
|
345 { |
|
346 |
|
347 } |
|
348 |