1 /* |
|
2 * Copyright (c) 2007-2008 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: Utility class for data model |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 //Includes |
|
20 #include "mul/mulmodelutility.h" |
|
21 |
|
22 #include <e32cmn.h> |
|
23 #include <mul/mulvisualitem.h> |
|
24 #include <mul/imulvarianttype.h> |
|
25 #include <mul/mulvarianttype.h> |
|
26 #include <stdexcept> // for standard exceptions |
|
27 #include <osn/ustring.h> |
|
28 #include <utf.h> |
|
29 |
|
30 //#include "mulactionitem.h" |
|
31 |
|
32 namespace Alf |
|
33 { |
|
34 |
|
35 // --------------------------------------------------------------------------- |
|
36 // CreateVariantType |
|
37 // --------------------------------------------------------------------------- |
|
38 // |
|
39 OSN_EXPORT IAlfVariantType* MulModelUtility::CreateVariantType( IAlfVariantType& aValue ) |
|
40 { |
|
41 /* IAlfVariantType* data = new (EMM) AlfVariantType(); |
|
42 data->set(aValue); |
|
43 return data;*/ |
|
44 switch( aValue.type() ) |
|
45 { |
|
46 case IAlfVariantType::EBool: |
|
47 { |
|
48 return new AlfVariantType( aValue.boolean() ); |
|
49 } |
|
50 case IAlfVariantType::EInt: |
|
51 { |
|
52 return new AlfVariantType( aValue.integer() ); |
|
53 } |
|
54 case IAlfVariantType::EUint: |
|
55 { |
|
56 return new AlfVariantType( aValue.uinteger() ); |
|
57 } |
|
58 case IAlfVariantType::EReal: |
|
59 { |
|
60 return new AlfVariantType( aValue.real() ); |
|
61 } |
|
62 case IAlfVariantType::EString: |
|
63 { |
|
64 return new AlfVariantType( aValue.string() ); |
|
65 } |
|
66 case IAlfVariantType::EContainer: |
|
67 { |
|
68 return new AlfVariantType( aValue.container() ); |
|
69 } |
|
70 case IAlfVariantType::EMap: |
|
71 { |
|
72 return new AlfVariantType( aValue.map() ); |
|
73 } |
|
74 case IAlfVariantType::EBranch: |
|
75 { |
|
76 return new AlfVariantType( aValue.branch() ); |
|
77 } |
|
78 case IAlfVariantType::ECustomData: |
|
79 default : |
|
80 { |
|
81 return NULL; |
|
82 } |
|
83 } |
|
84 } |
|
85 |
|
86 // --------------------------------------------------------------------------- |
|
87 // ConvertVisualItemToMap |
|
88 // --------------------------------------------------------------------------- |
|
89 // |
|
90 OSN_EXPORT std::auto_ptr<IAlfMap> MulModelUtility::ConvertVisualItemToMap( MulVisualItem& /*aVisualItem*/ ) |
|
91 { |
|
92 auto_ptr<IAlfMap> map( new ( EMM ) AlfMap()); |
|
93 |
|
94 return map; |
|
95 } |
|
96 |
|
97 // --------------------------------------------------------------------------- |
|
98 // ConvertMapToVisualItem |
|
99 // --------------------------------------------------------------------------- |
|
100 // |
|
101 OSN_EXPORT std::auto_ptr<MulVisualItem> MulModelUtility::ConvertMapToVisualItem( IAlfMap& /*aMap*/ ) |
|
102 { |
|
103 auto_ptr<MulVisualItem> visualItem( new ( EMM ) MulVisualItem()); |
|
104 // for( int i = 0 ; i < aMap.count() ; ++i ) |
|
105 // { |
|
106 // const UString& attributeName = aMap.name(i); |
|
107 // IAlfVariantType* origValue = aMap.item( attributeName ); |
|
108 // std::auto_ptr< IMulVariantType> value ( new (EMM) MulVariantType() ); |
|
109 // value->set(*origValue); |
|
110 // visualItem->SetAttribute( attributeName , value.get() ); |
|
111 // value.release(); |
|
112 // } |
|
113 return visualItem; |
|
114 } |
|
115 |
|
116 // --------------------------------------------------------------------------- |
|
117 // CheckNamePrefix |
|
118 // --------------------------------------------------------------------------- |
|
119 // |
|
120 bool MulModelUtility::CheckNamePrefix( UString aName ) |
|
121 { |
|
122 _LIT8(KMulPrefix,"mul_"); |
|
123 TPtrC8 debugString1 = ((TUint8*)aName.getUtf8()); |
|
124 int index = debugString1.Find(KMulPrefix); |
|
125 if( index == KErrNotFound ) |
|
126 { |
|
127 return false; |
|
128 } |
|
129 else |
|
130 { |
|
131 return true; |
|
132 } |
|
133 } |
|
134 |
|
135 |
|
136 |
|
137 // --------------------------------------------------------------------------- |
|
138 // UStringToAlfString |
|
139 // --------------------------------------------------------------------------- |
|
140 // |
|
141 OSN_EXPORT UString MulModelUtility::UStringToAlfString( const UString& aUString ) |
|
142 { |
|
143 if(aUString.isEmpty()) |
|
144 { |
|
145 return UString(""); |
|
146 } |
|
147 else |
|
148 { |
|
149 return UString( aUString.getUtf8() ); |
|
150 } |
|
151 } |
|
152 |
|
153 // --------------------------------------------------------------------------- |
|
154 // AlfStringToUString |
|
155 // --------------------------------------------------------------------------- |
|
156 // |
|
157 OSN_EXPORT UString MulModelUtility::AlfStringToUString( const UString& aAlfString ) |
|
158 { |
|
159 return UString( aAlfString.getUtf8() ); |
|
160 } |
|
161 |
|
162 // --------------------------------------------------------------------------- |
|
163 // IntToAlfString |
|
164 // --------------------------------------------------------------------------- |
|
165 // |
|
166 OSN_EXPORT UString MulModelUtility::IntToUString( int aNumber ) |
|
167 { |
|
168 TBuf8<20> buf; |
|
169 buf.AppendNum(aNumber); |
|
170 return UString( (char*)buf.PtrZ()); |
|
171 } |
|
172 |
|
173 // --------------------------------------------------------------------------- |
|
174 // ConvertTDesToUString |
|
175 // --------------------------------------------------------------------------- |
|
176 // |
|
177 OSN_EXPORT void MulModelUtility::ConvertTDesToUString (const TDesC& aDes, UString& aString) |
|
178 { |
|
179 HBufC8* utf8Buffer( NULL ); |
|
180 TRAPD( error, utf8Buffer = CnvUtfConverter::ConvertFromUnicodeToUtf8L( aDes ) ); |
|
181 if ( !utf8Buffer || error != KErrNone ) |
|
182 { |
|
183 delete utf8Buffer; |
|
184 throw std::exception(); |
|
185 } |
|
186 |
|
187 TPtrC8 ptr = utf8Buffer->Des(); |
|
188 std::auto_ptr<osncore::UString> ret; |
|
189 try |
|
190 { |
|
191 if ( ptr.Length() ) |
|
192 { |
|
193 ret.reset( new osncore::UString( ( osncore::Utf8* ) &ptr[0], ptr.Length() )); |
|
194 } |
|
195 else |
|
196 { |
|
197 ret.reset( new osncore::UString( "" ) ); |
|
198 } |
|
199 } |
|
200 catch ( std::exception e ) |
|
201 { |
|
202 delete utf8Buffer; |
|
203 throw e; |
|
204 } |
|
205 |
|
206 delete utf8Buffer; |
|
207 utf8Buffer = NULL; |
|
208 aString = UString(*ret); |
|
209 } |
|
210 |
|
211 // --------------------------------------------------------------------------- |
|
212 // ConvertTDes8ToUString |
|
213 // --------------------------------------------------------------------------- |
|
214 // |
|
215 OSN_EXPORT void MulModelUtility::ConvertTDes8ToUString (const TDesC8& aDes, UString& aString) |
|
216 { |
|
217 TPtrC8 ptr = aDes; |
|
218 std::auto_ptr<osncore::UString> ret; |
|
219 try |
|
220 { |
|
221 if ( ptr.Length() ) |
|
222 { |
|
223 ret.reset( new osncore::UString( ( osncore::Utf8* ) &ptr[0], ptr.Length() )); |
|
224 } |
|
225 else |
|
226 { |
|
227 ret.reset( new osncore::UString( "" ) ); |
|
228 } |
|
229 } |
|
230 catch ( std::exception e ) |
|
231 { |
|
232 throw e; |
|
233 } |
|
234 |
|
235 aString = UString(*ret); |
|
236 } |
|
237 |
|
238 // --------------------------------------------------------------------------- |
|
239 // ConvertUStringToTDes |
|
240 // --------------------------------------------------------------------------- |
|
241 // |
|
242 OSN_EXPORT void MulModelUtility::ConvertUStringToTDes (const UString& aString, TDes& aDes) |
|
243 { |
|
244 HBufC* unicodeBuffer = NULL; |
|
245 if ( !aString.isEmpty() ) |
|
246 { |
|
247 TRAPD( error, unicodeBuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(TPtrC8((TUint8*)aString.getUtf8())) ); |
|
248 if ( !unicodeBuffer || error != KErrNone ) |
|
249 { |
|
250 delete unicodeBuffer; |
|
251 throw std::exception(); |
|
252 } |
|
253 aDes.Copy(*unicodeBuffer); |
|
254 delete unicodeBuffer; |
|
255 unicodeBuffer = NULL; |
|
256 } |
|
257 else |
|
258 { |
|
259 aDes.Zero(); |
|
260 } |
|
261 } |
|
262 |
|
263 // --------------------------------------------------------------------------- |
|
264 // ConvertUStringToTDes8 |
|
265 // --------------------------------------------------------------------------- |
|
266 // |
|
267 OSN_EXPORT void MulModelUtility::ConvertUStringToTDes8(const UString& aString, TDes8& aDes) |
|
268 { |
|
269 /// @see comments in MulModelUtility::ConvertUStringToTDes |
|
270 HBufC* unicodeBuffer = NULL; |
|
271 if ( !aString.isEmpty() ) |
|
272 { |
|
273 TRAPD( error, unicodeBuffer = CnvUtfConverter::ConvertToUnicodeFromUtf8L(TPtrC8((TUint8*)aString.getUtf8())) ); |
|
274 if ( !unicodeBuffer || error != KErrNone ) |
|
275 { |
|
276 delete unicodeBuffer; |
|
277 throw std::exception(); |
|
278 } |
|
279 aDes.Copy(*unicodeBuffer); |
|
280 delete unicodeBuffer; |
|
281 unicodeBuffer = NULL; |
|
282 } |
|
283 else |
|
284 { |
|
285 aDes.Zero(); |
|
286 } |
|
287 } |
|
288 |
|
289 // --------------------------------------------------------------------------- |
|
290 // CreateVisualItem |
|
291 // --------------------------------------------------------------------------- |
|
292 // |
|
293 OSN_EXPORT std::auto_ptr<MulVisualItem> MulModelUtility::CreateVisualItem( mulwidget::TLogicalTemplate aDefaultTemplate ) |
|
294 { |
|
295 std::auto_ptr<MulVisualItem> visualItem( new (EMM) MulVisualItem() ); |
|
296 visualItem->SetAttribute( mulvisualitem::KAttributeTemplate , aDefaultTemplate); |
|
297 return visualItem; |
|
298 } |
|
299 |
|
300 } // namespace Alf |
|
301 |
|
302 //End of file |
|