|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 #include <e32base.h> |
|
17 #include <e32property.h> |
|
18 #include <e32debug.h> |
|
19 #include "TPropertyManager.h" |
|
20 |
|
21 _LIT(KSeparator, "|"); // Char used to separate arguments |
|
22 |
|
23 static TInt DefineProperty(TUid aCategory, TUint aKey, TInt aAttr,TInt aPreallocated) |
|
24 { |
|
25 _LIT_SECURE_ID(mySid,0X10204FC5); |
|
26 |
|
27 TSecurityPolicy readPolicy = TSecurityPolicy::EAlwaysPass; |
|
28 TSecurityPolicy writePolicy = TSecurityPolicy(mySid); |
|
29 |
|
30 TInt err = RProperty::Define(aCategory, aKey, aAttr, readPolicy, writePolicy, aPreallocated); |
|
31 |
|
32 //If the variable is already defined then return KErrNone as this is fine, |
|
33 if(err == KErrAlreadyExists) |
|
34 { |
|
35 err = KErrNone; |
|
36 } |
|
37 |
|
38 RDebug::Print(_L("Property Manager - Define Property Cat = %d Key = %d err = %d\n"), aCategory,aKey, err); |
|
39 |
|
40 return err; |
|
41 } |
|
42 |
|
43 static TInt DeleteProperty(TUid aCategory, TUint aKey) |
|
44 { |
|
45 TInt err = RProperty::Delete(aCategory, aKey); |
|
46 |
|
47 //If the variable is not found then return KErrNone as this is fine |
|
48 if(err == KErrNotFound) |
|
49 { |
|
50 err = KErrNone; |
|
51 } |
|
52 |
|
53 RDebug::Print(_L("Property Manager - Delete Property Cat = %d Key = %d err = %d\n"), aCategory,aKey, err); |
|
54 |
|
55 return err; |
|
56 } |
|
57 |
|
58 static TInt SetProperty(TUid aCategory, TUint aKey, TInt aValue) |
|
59 { |
|
60 TInt err = RProperty::Set(aCategory, aKey, aValue); |
|
61 RDebug::Print(_L("Property Manager - Set Property Cat = %d Key = %d Value = %d err = %d\n"), aCategory,aKey,aValue,err); |
|
62 return err; |
|
63 } |
|
64 |
|
65 static TInt SetProperty(TUid aCategory, TUint aKey, const TDesC8 &aValue) |
|
66 { |
|
67 TInt err = RProperty::Set(aCategory, aKey, aValue); |
|
68 RDebug::Print(_L("Property Manager - Set Property Cat = %d Key = %d Value = %S err = %d\n"), aCategory,aKey,&aValue,err); |
|
69 return err; |
|
70 } |
|
71 |
|
72 static TInt SetProperty(TUid aCategory, TUint aKey, const TDesC16 &aValue) |
|
73 { |
|
74 TInt err = RProperty::Set(aCategory, aKey, aValue); |
|
75 RDebug::Print(_L("Property Manager - Set Property Cat = %d Key = %d Value = %S err = %d\n"), aCategory,aKey,&aValue,err); |
|
76 return err; |
|
77 } |
|
78 |
|
79 TInt GetNumber(const TPtrC& aText, TInt& aLength) |
|
80 { |
|
81 TInt total = 0; |
|
82 aLength = 0; |
|
83 TInt textLength = aText.Length(); |
|
84 |
|
85 //Loop through each character and verify that it is a valid digit |
|
86 //before converting to a decimal representation and adding to the total |
|
87 while (aLength < textLength) |
|
88 { |
|
89 TChar currentChar = aText[aLength]; |
|
90 if (currentChar < '0' || currentChar > '9') |
|
91 { |
|
92 return total; |
|
93 } |
|
94 |
|
95 TInt digit = 0; |
|
96 TUint charValue = currentChar; |
|
97 |
|
98 //Convert from character value to decimal |
|
99 digit = charValue - 0x30; //(offset for western digit characters |
|
100 total = (total * 10) + digit; |
|
101 aLength++; |
|
102 } |
|
103 return total; |
|
104 } |
|
105 |
|
106 static TInt ParseArgs(TDesC& aArgs, TInt& aStartIndex) |
|
107 { |
|
108 TInt length; |
|
109 TInt value; |
|
110 |
|
111 //Get a substrng of aArgs begining at aStartIndex |
|
112 TPtrC string = aArgs.Mid(aStartIndex,aArgs.Length() - aStartIndex); |
|
113 |
|
114 //Find the position of the first separator |
|
115 TInt pos = string.Find(KSeparator); |
|
116 |
|
117 if(pos > 0) |
|
118 { |
|
119 //Get the number that exists in the substring before the separator |
|
120 value = GetNumber(string.Mid(0,pos),length); |
|
121 } |
|
122 //If a separator is not found, then this must be the last number in aArgs |
|
123 else |
|
124 { |
|
125 value = GetNumber(string,length); |
|
126 } |
|
127 |
|
128 //Update the start index to the character following the separator |
|
129 aStartIndex += (pos +1); |
|
130 |
|
131 return value; |
|
132 } |
|
133 |
|
134 static TInt ProcessCommandLine(TDesC& aArgs) |
|
135 { |
|
136 TInt error; |
|
137 TInt pos = 0; |
|
138 TInt length = aArgs.Length(); |
|
139 |
|
140 //Pull out the common elements of all commands - Op, Cat & Key |
|
141 PropertyManager::TOperation operation = PropertyManager::TOperation(ParseArgs(aArgs,pos)); |
|
142 TInt category = ParseArgs(aArgs,pos); |
|
143 TUid categoryUid = TUid::Uid(category); |
|
144 TUint key = ParseArgs(aArgs,pos); |
|
145 |
|
146 TInt attr; |
|
147 TInt preallocated; |
|
148 TInt intVal; |
|
149 TBuf8<64> value; |
|
150 |
|
151 //Handle each operation separately to pull out the remaining arguments |
|
152 //and process the request. |
|
153 switch(operation) |
|
154 { |
|
155 case PropertyManager::EDefineProperty: |
|
156 attr = ParseArgs(aArgs,pos); |
|
157 preallocated = ParseArgs(aArgs,pos); |
|
158 error = DefineProperty(categoryUid,key,attr,preallocated); |
|
159 break; |
|
160 |
|
161 case PropertyManager::EDeleteProperty: |
|
162 error = DeleteProperty(categoryUid,key); |
|
163 break; |
|
164 |
|
165 case PropertyManager::ESetPropertyInt: |
|
166 intVal = ParseArgs(aArgs,pos); |
|
167 error = SetProperty(categoryUid,key,intVal); |
|
168 break; |
|
169 |
|
170 case PropertyManager::ESetPropertyDes8: |
|
171 value.Copy(aArgs.Mid(pos,(aArgs.Length()-pos))); |
|
172 error = SetProperty(categoryUid,key,value); |
|
173 break; |
|
174 |
|
175 case PropertyManager::ESetPropertyDes16: |
|
176 error = SetProperty(categoryUid,key,aArgs.Mid(pos,(aArgs.Length()-pos))); |
|
177 break; |
|
178 |
|
179 default: |
|
180 error = KErrArgument; |
|
181 break; |
|
182 } |
|
183 |
|
184 return error; |
|
185 } |
|
186 |
|
187 GLDEF_C TInt E32Main() |
|
188 { |
|
189 |
|
190 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
191 |
|
192 TBuf<64> args; |
|
193 User::CommandLine(args); |
|
194 |
|
195 TInt error = ProcessCommandLine(args); |
|
196 |
|
197 delete cleanup; |
|
198 return error; |
|
199 |
|
200 } |
|
201 |