|
1 /* |
|
2 * Copyright (c) 2009 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: Mifconv icon binary converters class. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "mifconv.h" |
|
20 #include "mifconv_iconbinaryconverter.h" |
|
21 #include "mifconv_util.h" |
|
22 #include "mifconv_exception.h" |
|
23 #include "mifconv_convertermanager.h" |
|
24 #include "mifconv_argumentmanager.h" |
|
25 #include <errno.h> |
|
26 #include <algorithm> |
|
27 |
|
28 const MifConvString SVGTBINENCODE_DEFAULT_PATH(EPOC_TOOLS_PATH); |
|
29 |
|
30 /** |
|
31 * |
|
32 */ |
|
33 MifConvIconBinaryConverter::MifConvIconBinaryConverter() |
|
34 { |
|
35 } |
|
36 |
|
37 /** |
|
38 * |
|
39 */ |
|
40 MifConvIconBinaryConverter::~MifConvIconBinaryConverter() |
|
41 { |
|
42 } |
|
43 |
|
44 /** |
|
45 * |
|
46 */ |
|
47 void MifConvIconBinaryConverter::Init() |
|
48 { |
|
49 } |
|
50 |
|
51 /** |
|
52 * |
|
53 */ |
|
54 void MifConvIconBinaryConverter::CleanupTargetFiles() |
|
55 { |
|
56 } |
|
57 |
|
58 /** |
|
59 * |
|
60 */ |
|
61 void MifConvIconBinaryConverter::AppendFile( const MifConvSourceFile& sourcefile ) |
|
62 { |
|
63 if( MifConvUtil::FileExtension( sourcefile.Filename() ) == SVG_FILE_EXTENSION && |
|
64 MifConvArgumentManager::Instance()->BooleanValue(MifConvDisableSvgCompression) == false) |
|
65 { |
|
66 iSourceFiles.push_back( sourcefile ); |
|
67 |
|
68 // Create temp directory: |
|
69 if( iTempDir.length() == 0 ) |
|
70 { |
|
71 InitTempFile(); |
|
72 } |
|
73 |
|
74 // External SVGTBINENCODE converts .svg files to .svgb files. However, .svgb files |
|
75 // shall be given to mif-converter to get them in mif-file: |
|
76 |
|
77 // Create new string for .svgb file name: |
|
78 MifConvString tmpFile(sourcefile.Filename()); |
|
79 ConvertToBinaryFilename(tmpFile); |
|
80 MifConvString tempBinFilename(iTempDir + MifConvUtil::FilenameWithoutExtension(tmpFile) + "." + SVGB_BINARY_FILE_EXTENSION); |
|
81 |
|
82 // Get converters for .svgb files: |
|
83 MifConvFileConverterList& additionalConverters = MifConvConverterManager::Instance()->GetConverters(tempBinFilename); |
|
84 |
|
85 // Converters for .svg files: |
|
86 MifConvFileConverterList& thisFilesConverters = MifConvConverterManager::Instance()->GetConverters( sourcefile.Filename() ); |
|
87 |
|
88 // Save temporary binary filename for later deleting: |
|
89 iTempFilenames.push_back(tempBinFilename); |
|
90 |
|
91 // Add temporary file to converters: |
|
92 for( MifConvFileConverterList::iterator c = additionalConverters.begin(); c != additionalConverters.end(); ++c ) |
|
93 { |
|
94 // We have to make sure that we don't add same file twice to same converter. So, let's take first a list of |
|
95 // .svg file converters and compare them to the .svgb file converters. Don't add temporary file to converters |
|
96 // that are found from both of the lists. |
|
97 MifConvFileConverterList::iterator c2 = std::find(thisFilesConverters.begin(), thisFilesConverters.end(), *c ); |
|
98 if( c2 == thisFilesConverters.end() ) |
|
99 { |
|
100 // .svgb converter not found from .svg converters -> add temporary file to .svgb converter: |
|
101 MifConvSourceFile svgbFile(sourcefile); |
|
102 svgbFile.SetFilename(tempBinFilename); |
|
103 (*c)->AppendFile(svgbFile); |
|
104 } |
|
105 } |
|
106 } |
|
107 } |
|
108 |
|
109 /** |
|
110 * |
|
111 */ |
|
112 void MifConvIconBinaryConverter::Convert() |
|
113 { |
|
114 if( iSourceFiles.size() > 0 && MifConvArgumentManager::Instance()->BooleanValue(MifConvDisableSvgCompression) == false ) |
|
115 { |
|
116 ConvertToSvgb(); |
|
117 } |
|
118 } |
|
119 |
|
120 /** |
|
121 * |
|
122 */ |
|
123 void MifConvIconBinaryConverter::Cleanup(bool err) |
|
124 { |
|
125 CleanupTempFiles(); |
|
126 if( err ) |
|
127 { |
|
128 CleanupTargetFiles(); |
|
129 } |
|
130 } |
|
131 |
|
132 /** |
|
133 * |
|
134 */ |
|
135 void MifConvIconBinaryConverter::ConvertToSvgb() |
|
136 { |
|
137 RunExtConverter(); |
|
138 } |
|
139 |
|
140 /** |
|
141 * |
|
142 */ |
|
143 void MifConvIconBinaryConverter::InitTempFile() |
|
144 { |
|
145 MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance(); |
|
146 // Construct temp file name |
|
147 // If temp directory is given in command line arguments, use it: |
|
148 iTempDir = MifConvUtil::DefaultTempDirectory(); |
|
149 const MifConvString& tempDirArg = argMgr->StringValue(MifConvTempPathArg); |
|
150 if( tempDirArg.length() > 0 ) |
|
151 { |
|
152 iTempDir = tempDirArg; |
|
153 } |
|
154 |
|
155 if( iTempDir.length() > 0 && iTempDir.at(iTempDir.length()-1) != DIR_SEPARATOR2 ) |
|
156 { |
|
157 iTempDir.append(DIR_SEPARATOR); |
|
158 } |
|
159 |
|
160 // Generate new temp-filename: |
|
161 iTempDir.append(MifConvUtil::TemporaryFilename()); |
|
162 |
|
163 // append tmp at as postfix |
|
164 // this is needed because the generated name can contain a single period '.' |
|
165 // character as the last character which is eaten away when the directory created. |
|
166 iTempDir.append(MifConvString("tmp")); |
|
167 |
|
168 MifConvUtil::EnsurePathExists(iTempDir); |
|
169 |
|
170 iTempDir.append(DIR_SEPARATOR); |
|
171 } |
|
172 |
|
173 /** |
|
174 * |
|
175 */ |
|
176 void MifConvIconBinaryConverter::ConvertToBinaryFilename( MifConvString& input ) |
|
177 { |
|
178 MifConvUtil::ReplaceChar(input, DIR_SEPARATOR2, '_'); |
|
179 MifConvUtil::ReplaceChar(input, INCORRECT_DIR_SEPARATOR2, '_'); |
|
180 MifConvUtil::ReplaceChar(input, ':', '_'); |
|
181 MifConvUtil::ReplaceChar(input, ' ', '_'); |
|
182 } |
|
183 |
|
184 /** |
|
185 * |
|
186 */ |
|
187 void MifConvIconBinaryConverter::RunExtConverter() |
|
188 { |
|
189 MifConvArgumentManager* argMgr = MifConvArgumentManager::Instance(); |
|
190 |
|
191 // Build svgtbinencode command |
|
192 MifConvString extConverterCommand("\""); // Open the " mark |
|
193 MifConvString versionArgument; |
|
194 MifConvString sourceArgument; |
|
195 |
|
196 const MifConvString& extConverterPath = argMgr->StringValue(MifConvSvgencodePathArg); |
|
197 const MifConvString& defaultExtConverterPath = GetDefaultExtConverterPath(); |
|
198 if( extConverterPath.length() > 0 ) |
|
199 { |
|
200 extConverterCommand += extConverterPath; // If the path is given, use it. |
|
201 } |
|
202 else |
|
203 { |
|
204 extConverterCommand += defaultExtConverterPath; // Use default path |
|
205 } |
|
206 |
|
207 // Ensure that the last char of the path is dir-separator: |
|
208 if( extConverterCommand.length() > 1 && extConverterCommand.at(extConverterCommand.length()-1) != DIR_SEPARATOR2 ) |
|
209 extConverterCommand += DIR_SEPARATOR; |
|
210 |
|
211 // Then add SVGTBINENCODE executable call and close the " mark |
|
212 extConverterCommand += SVGTBINENCODE_EXECUTABLE_NAME + MifConvString("\" "); |
|
213 |
|
214 // If SVGTBINENCODE version is given, use it also: |
|
215 const MifConvString& extConverterVersion = argMgr->StringValue(MifConvSvgtVersionArg); |
|
216 if( extConverterVersion.length() > 0 ) |
|
217 { |
|
218 versionArgument = SVGTBINENCODE_OPTION_PREFIX + |
|
219 MifConvString(SVGTBINENCODE_VERSION_PARAMETER) + " " + extConverterVersion; |
|
220 extConverterCommand += versionArgument + " "; |
|
221 } |
|
222 |
|
223 // Run converter for each of the source files: |
|
224 for( MifConvSourceFileList::iterator i = iSourceFiles.begin(); i != iSourceFiles.end(); ++i ) |
|
225 { |
|
226 // Build temp filename by replacing dir separator and ':' chars with '_': |
|
227 MifConvString tmpFileName(i->Filename()); |
|
228 ConvertToBinaryFilename(tmpFileName); |
|
229 |
|
230 // Copy source file to temp directory: |
|
231 MifConvString to(iTempDir + tmpFileName); |
|
232 if( MifConvUtil::CopyFile(i->Filename(), to) == false ) |
|
233 { |
|
234 THROW_ERROR_COMMON("File copy failed: " + to, MifConvString(__FILE__), __LINE__ ); |
|
235 } |
|
236 iTempFilenames.push_back(to); |
|
237 // It seems that system() function does not work if the command consists of two separate parts |
|
238 // enclosed with quotation marks. If the whole string is enclosed with quotation marks then it works... |
|
239 // For example: command '"\epoc32\tools\bmconv" "somefile"' does not work while command |
|
240 // '""\epoc32\tools\bmconv" "somefile""' does. |
|
241 if( system(MifConvString("\""+extConverterCommand+"\""+to+"\"\"").c_str()) < 0 ) |
|
242 { |
|
243 int ernro = errno; // The error number must check straight away before any next system command |
|
244 |
|
245 MifConvString errStr("Executing SVGTBINENCODE failed"); |
|
246 if( ernro ) |
|
247 { |
|
248 errStr += ", system error = " + MifConvUtil::ToString(ernro); // Possible system error. |
|
249 } |
|
250 THROW_ERROR_COMMON(errStr, MifConvString(__FILE__), __LINE__ ); |
|
251 } |
|
252 } |
|
253 } |
|
254 |
|
255 /** |
|
256 * |
|
257 */ |
|
258 void MifConvIconBinaryConverter::CleanupTempFiles() |
|
259 { |
|
260 for( MifConvStringList::iterator i = iTempFilenames.begin(); i != iTempFilenames.end(); ++i ) |
|
261 { |
|
262 if( remove( i->c_str() ) != 0 ) |
|
263 { |
|
264 perror( "Error deleting file (svg conversion)" ); |
|
265 } |
|
266 } |
|
267 if( iTempDir.length() > 0 && MifConvUtil::RemoveDirectory( iTempDir ) != 0 ) |
|
268 { |
|
269 perror( "Error deleting temporary directory (svg conversion)" ); |
|
270 } |
|
271 } |
|
272 |
|
273 /** |
|
274 * |
|
275 */ |
|
276 const MifConvString& MifConvIconBinaryConverter::GetDefaultExtConverterPath() |
|
277 { |
|
278 if( iDefaultExtConverterPath.length() == 0 ) |
|
279 { |
|
280 // Check if the EPOCROOT is given |
|
281 MifConvString epocRoot(MifConvArgumentManager::Instance()->EpocRoot()); |
|
282 if( epocRoot.length() > 0 ) |
|
283 { |
|
284 iDefaultExtConverterPath += epocRoot; |
|
285 } |
|
286 |
|
287 // Ensure that the last char of the path is dir-separator: |
|
288 if( iDefaultExtConverterPath.length() > 0 ) |
|
289 { |
|
290 if( iDefaultExtConverterPath.at(iDefaultExtConverterPath.length()-1) != DIR_SEPARATOR2 ) |
|
291 { |
|
292 iDefaultExtConverterPath += DIR_SEPARATOR; |
|
293 } |
|
294 iDefaultExtConverterPath += SVGTBINENCODE_DEFAULT_PATH; |
|
295 } |
|
296 } |
|
297 |
|
298 return iDefaultExtConverterPath; |
|
299 } |
|
300 |
|
301 |
|
302 |