|
1 /* |
|
2 * Copyright (c) 2006-2006 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: Global methods for brandimporter |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <e32cons.h> |
|
21 #include <bacline.h> |
|
22 #include <badesca.h> |
|
23 |
|
24 #include "cbsimportmanager.h" |
|
25 #include "importlogwriter.h" |
|
26 |
|
27 namespace |
|
28 { |
|
29 |
|
30 // CONSTANTS |
|
31 // |
|
32 _LIT( KProcessName, "BSImport" ); |
|
33 _LIT( KTxtTitle, "BrandServer XML importer" ); |
|
34 _LIT( KTxtFormatFailed, "\nimport failed: leave code = %d" ); |
|
35 _LIT( KTxtOK, "\nok"); |
|
36 _LIT( KTxtPressAnyKey, "\n [press any key]" ); |
|
37 _LIT( KInstallFiles, "*.xml"); |
|
38 _LIT( KDirSeparator, "\\" ); |
|
39 |
|
40 // SETTING SWITCHES (use uppercase) |
|
41 // |
|
42 _LIT( KInputDir, "-D" ); |
|
43 |
|
44 } |
|
45 |
|
46 // ======== GLOBAL FUNCTIONS ======== |
|
47 |
|
48 // --------------------------------------------------------------------------- |
|
49 // DeleteRPtrArray |
|
50 // --------------------------------------------------------------------------- |
|
51 // |
|
52 LOCAL_C void DeleteRPtrArray( TAny* aArray ) |
|
53 { |
|
54 RPointerArray<HBufC>* array = static_cast< RPointerArray<HBufC>* >(aArray); |
|
55 TInt count = array->Count(); |
|
56 for( TInt i = 0; i < count; i++ ) |
|
57 { |
|
58 delete (*array)[i]; |
|
59 } |
|
60 array->Reset(); |
|
61 } |
|
62 |
|
63 // --------------------------------------------------------------------------- |
|
64 // ParseInstallableBrandsL |
|
65 // --------------------------------------------------------------------------- |
|
66 // |
|
67 LOCAL_C void ParseInstallableBrandsL( RFs& aFs, |
|
68 RPointerArray<HBufC>& aFiles, |
|
69 const TDesC& aDirectory ) |
|
70 { |
|
71 // list import content |
|
72 TParse file; |
|
73 file.Set( KInstallFiles, &aDirectory, NULL ); |
|
74 |
|
75 CDir* dir = NULL; |
|
76 User::LeaveIfError( |
|
77 aFs.GetDir( file.FullName(), KEntryAttNormal, ESortNone, dir ) ); |
|
78 CleanupStack::PushL( dir ); |
|
79 |
|
80 // create array of files (with full path) |
|
81 TInt count = dir->Count(); |
|
82 for( TInt i = 0; i < count; i++ ) |
|
83 { |
|
84 file.Set( (*dir)[i].iName, &aDirectory, NULL ); |
|
85 aFiles.Append( file.FullName().AllocL() ); |
|
86 } |
|
87 |
|
88 CleanupStack::PopAndDestroy( dir ); |
|
89 } |
|
90 |
|
91 // --------------------------------------------------------------------------- |
|
92 // ParseCommandLineArgumentsL |
|
93 // --------------------------------------------------------------------------- |
|
94 // |
|
95 LOCAL_C void ParseCommandLineArgumentsL( RPointerArray<HBufC>& aFiles, |
|
96 CConsoleBase* aConsole ) |
|
97 { |
|
98 // Get arguments |
|
99 CCommandLineArguments *pCmd = CCommandLineArguments::NewL(); |
|
100 CleanupStack::PushL( pCmd ); |
|
101 |
|
102 // check arguments |
|
103 if( pCmd->Count() == 1 ) |
|
104 { |
|
105 aConsole->Printf( _L("Error: No files to import!\nGive import files as arguments") ); // CSI: 78 # See above |
|
106 aConsole->Printf( KTxtPressAnyKey ); |
|
107 aConsole->Getch(); |
|
108 User::Leave( KErrArgument ); |
|
109 } |
|
110 |
|
111 // open file session |
|
112 // Codescanner warning: use of non-pointer/reference RFs |
|
113 // this code cannot get file server handle from anywhere so it has to be created here |
|
114 RFs fs; // CSI: 76 # See above |
|
115 User::LeaveIfError( fs.Connect() ); |
|
116 CleanupClosePushL( fs ); |
|
117 |
|
118 // Check all arguments for switches |
|
119 // Skip first argument since it only contains the name of the executable |
|
120 TInt i = 0; |
|
121 while( ++i < pCmd->Count() ) |
|
122 { |
|
123 RBuf arg; |
|
124 arg.CreateL( pCmd->Arg( i ) ); |
|
125 arg.CleanupClosePushL(); |
|
126 arg.UpperCase(); |
|
127 |
|
128 // Input directory: "-D [directory]" |
|
129 if ( arg.CompareF( KInputDir ) == 0 ) |
|
130 { |
|
131 if( pCmd->Count() > i + 1 ) |
|
132 { |
|
133 // get all files from given directory |
|
134 TPath path( pCmd->Arg( i + 1 ) ); |
|
135 if( path.Right( 1 ).Compare( KDirSeparator ) != 0 ) |
|
136 { |
|
137 path.Append( KDirSeparator ); |
|
138 } |
|
139 ParseInstallableBrandsL( fs, aFiles, path ); |
|
140 i++; // skip to next argument |
|
141 } |
|
142 else |
|
143 { |
|
144 // no content after -d switch |
|
145 aConsole->Printf( _L("Argument missing after '-d' switch") ); // CSI: 78 # debug print |
|
146 aConsole->Printf( KTxtPressAnyKey ); |
|
147 aConsole->Getch(); |
|
148 User::Leave( KErrArgument ); |
|
149 } |
|
150 } |
|
151 else // argument is just plain file - add it. |
|
152 { |
|
153 aFiles.Append( pCmd->Arg( i ).AllocL() ); |
|
154 } |
|
155 |
|
156 CleanupStack::PopAndDestroy(); // arg |
|
157 } |
|
158 |
|
159 CleanupStack::PopAndDestroy( 2 ); // pCmd, fs |
|
160 } |
|
161 |
|
162 // --------------------------------------------------------------------------- |
|
163 // MainL |
|
164 // --------------------------------------------------------------------------- |
|
165 // |
|
166 LOCAL_C void MainL( CConsoleBase* aConsole ) |
|
167 { |
|
168 // Read settings |
|
169 RPointerArray<HBufC> files; |
|
170 CleanupStack::PushL( TCleanupItem( DeleteRPtrArray, &files ) ); |
|
171 ParseCommandLineArgumentsL( files, aConsole ); |
|
172 |
|
173 // convert PointerArray to MDesCArray |
|
174 TInt count = files.Count(); |
|
175 CDesCArrayFlat* importArray = new (ELeave) CDesCArrayFlat( count ); |
|
176 CleanupStack::PushL( importArray ); |
|
177 for( TInt i = 0; i < count; i++ ) |
|
178 { |
|
179 importArray->AppendL( files[i]->Des() ); |
|
180 } |
|
181 |
|
182 // Do import |
|
183 CBSImportManager* importer = CBSImportManager::NewLC(); |
|
184 importer->ImportFileL( *importArray ); |
|
185 CleanupStack::PopAndDestroy( 3 ); //importer, importarray, files |
|
186 } |
|
187 |
|
188 // --------------------------------------------------------------------------- |
|
189 // doMainL |
|
190 // --------------------------------------------------------------------------- |
|
191 // |
|
192 LOCAL_C TInt doMainL() |
|
193 { |
|
194 CConsoleBase* console = Console::NewL( KTxtTitle, TSize( KConsFullScreen, KConsFullScreen ) ); |
|
195 CleanupStack::PushL( console ); |
|
196 TRAPD( error, MainL( console ) ); |
|
197 if( error ) |
|
198 { |
|
199 IMPORT_DP( D_IMPORT_LIT( "ERROR importing brand: %d" ), error ); |
|
200 console->Printf( KTxtFormatFailed, error ); |
|
201 } |
|
202 else |
|
203 { |
|
204 console->Printf( KTxtOK ); |
|
205 } |
|
206 |
|
207 // Pause for development use. |
|
208 // Disable this when releasing |
|
209 //----------------------------------------------------- |
|
210 console->Printf( KTxtPressAnyKey ); |
|
211 //----------------------------------------------------- |
|
212 |
|
213 CleanupStack::PopAndDestroy(); // console |
|
214 return error; |
|
215 } |
|
216 |
|
217 // --------------------------------------------------------------------------- |
|
218 // Main function of the application executable. |
|
219 // --------------------------------------------------------------------------- |
|
220 // |
|
221 GLDEF_C TInt E32Main() |
|
222 { |
|
223 __UHEAP_MARK; |
|
224 IMPORT_DP_TXT("----- NEW IMPORT SESSION ------( IMPORTER )---"); |
|
225 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
226 TInt returnCode( KErrNone ); |
|
227 User::RenameThread( KProcessName ); |
|
228 TRAPD( error, returnCode = doMainL() ); |
|
229 IMPORT_DP_TXT("-----------------------------------------------"); |
|
230 __ASSERT_ALWAYS( !error, User::Panic( KProcessName, error ) ); |
|
231 delete cleanup; |
|
232 __UHEAP_MARKEND; |
|
233 return returnCode; |
|
234 } |