|
1 // Copyright (c) 1997-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 "CHtmlToCrtConverter.h" |
|
17 #include "CHtmlToCrtConvParser.h" |
|
18 #include "CHtmlToCrtConvBuffer.h" |
|
19 #include "CHtmlToCrtConvCharsetConvert.h" |
|
20 #include <txtrich.h> |
|
21 #include <bautils.h> |
|
22 #include <s32file.h> |
|
23 #include <ecom/implementationproxy.h> |
|
24 |
|
25 #ifdef __CHTMLTOCRTCONV_COPY_TO_CLIPBOARD__ |
|
26 #include <baclipb.h> |
|
27 #endif |
|
28 |
|
29 const TUid KUidCHtmlToCrtConverter={0x1000a90e}; |
|
30 |
|
31 CHtmlToCrtConverter::CHtmlToCrtConverter() |
|
32 { |
|
33 } |
|
34 |
|
35 CHtmlToCrtConverter::~CHtmlToCrtConverter() |
|
36 { |
|
37 Cleanup(); |
|
38 } |
|
39 |
|
40 void CHtmlToCrtConverter::ConvertL(const TFileName& aSourceFile, const TFileName& aTargetFile, MConverterUiObserver* aObserver) |
|
41 { |
|
42 iObserver=aObserver; |
|
43 PrepForConversionL(aSourceFile, aTargetFile); |
|
44 while(DoConvertL()) |
|
45 { |
|
46 } |
|
47 } |
|
48 |
|
49 void CHtmlToCrtConverter::ConvertAL(const TFileName& aSourceFile, const TFileName& aTargetFile, MConverterUiObserver* aObserver) |
|
50 { |
|
51 iObserver=aObserver; |
|
52 PrepForConversionL(aSourceFile, aTargetFile); |
|
53 } |
|
54 |
|
55 TBool CHtmlToCrtConverter::DoConvertL() |
|
56 { |
|
57 TBool moreProcessingRequired=ETrue; |
|
58 TBool moreParsingRequired=ETrue; |
|
59 switch(iConverterState) |
|
60 { |
|
61 case EGetCharsetFromPassword: |
|
62 if(iCharsetConvert->GetCharSetFromPasswordL()) |
|
63 { |
|
64 iConverterState=EPrepareForConvertToUnicode; |
|
65 } |
|
66 else |
|
67 iConverterState=EGetCharsetFromMetaTag; |
|
68 break; |
|
69 case EGetCharsetFromMetaTag: |
|
70 if(iCharsetConvert->GetCharSetFromMetaTagL()) |
|
71 { |
|
72 iConverterState=EPrepareForConvertToUnicode; |
|
73 } |
|
74 else |
|
75 iConverterState=EGetCharsetUsingAutoDetect; |
|
76 break; |
|
77 case EGetCharsetUsingAutoDetect: |
|
78 iCharsetConvert->GetCharSetUsingAutoDetectL(); |
|
79 iConverterState=EPrepareForConvertToUnicode; |
|
80 break; |
|
81 case EPrepareForConvertToUnicode: |
|
82 iCharsetConvert->PrepareForConvertToUnicodeL(); |
|
83 iConverterState=EDoOneStepOfParsing; |
|
84 break; |
|
85 case EDoOneStepOfParsing: |
|
86 moreParsingRequired=iParser->DoOneStepL(); |
|
87 if(!moreParsingRequired) |
|
88 { |
|
89 iConverterState=EFinishedParsing; |
|
90 } |
|
91 break; |
|
92 case EFinishedParsing: |
|
93 FinaliseConversionL(); |
|
94 moreProcessingRequired=EFalse; |
|
95 break; |
|
96 default: |
|
97 break; |
|
98 } |
|
99 return moreProcessingRequired; |
|
100 } |
|
101 |
|
102 TUid CHtmlToCrtConverter::Uid() |
|
103 { |
|
104 return KUidCHtmlToCrtConverter; |
|
105 } |
|
106 |
|
107 TInt CHtmlToCrtConverter::Capabilities() |
|
108 { |
|
109 return EConvertsFiles; |
|
110 } |
|
111 |
|
112 void CHtmlToCrtConverter::CancelConvert() |
|
113 { |
|
114 Cleanup(); |
|
115 } |
|
116 |
|
117 void CHtmlToCrtConverter::PrepForConversionL(const TDesC& aSourceFile, const TDesC& aTargetFile) |
|
118 { |
|
119 Cleanup(); |
|
120 iParaFormatLayer=CParaFormatLayer::NewL(); |
|
121 iCharFormatLayer=CCharFormatLayer::NewL(); |
|
122 iText=CRichText::NewL(iParaFormatLayer, iCharFormatLayer); |
|
123 |
|
124 iSourceFileName=aSourceFile.AllocL(); |
|
125 iTargetFileName=aTargetFile.AllocL(); |
|
126 User::LeaveIfError(iFsSession.Connect()); |
|
127 LocateAndOpenResourceFileL(); |
|
128 iCnvCharacterSetConverter=CCnvCharacterSetConverter::NewL(); |
|
129 iBuffer=CHtmlToCrtConvBuffer::NewL(*iSourceFileName, iFsSession, *iCnvCharacterSetConverter); |
|
130 iCharsetConvert=CHtmlToCrtConvCharsetConvert::NewL(*iBuffer, iFsSession, *iCnvCharacterSetConverter, *this, iObserver); |
|
131 iParser=CHtmlToCrtConvParser::NewL(*iText, *iBuffer, *this); |
|
132 if (iObserver) |
|
133 { |
|
134 TEntry entry; |
|
135 User::LeaveIfError(iFsSession.Entry(aSourceFile, entry)); |
|
136 iObserver->MaxSteps((entry.iSize / KCharsProcessedInOneStep), 0); |
|
137 } |
|
138 } |
|
139 |
|
140 void CHtmlToCrtConverter::LocateAndOpenResourceFileL() |
|
141 { |
|
142 _LIT(KResourceFileName, "chtmltocrtconverter.rsc"); |
|
143 _LIT(KPath,"\\resource\\convert\\"); |
|
144 |
|
145 TFileName* resourceFileName=new(ELeave) TFileName(KResourceFileName); |
|
146 CleanupStack::PushL(resourceFileName); |
|
147 BaflUtils::NearestLanguageFile(iFsSession, *resourceFileName); |
|
148 TFileName* fullPathToResourceFile=new(ELeave) TFileName; |
|
149 CleanupStack::PushL(fullPathToResourceFile); |
|
150 |
|
151 #ifdef __CHTMLTOCRTCONV_TESTCODE__ |
|
152 //test code uses TFindFile to scan for resource file |
|
153 TFindFile findFile(iFsSession); |
|
154 User::LeaveIfError(findFile.FindByDir(*resourceFileName, KPath)==KErrNone); |
|
155 (*fullPathToResourceFile)=findFile.File(); |
|
156 #else |
|
157 //use Dll::FileName to get drive letter for converter, then append "\system\convert\'resource file name'" |
|
158 Dll::FileName(*fullPathToResourceFile); |
|
159 fullPathToResourceFile->SetLength(2); |
|
160 fullPathToResourceFile->Append(KPath); |
|
161 fullPathToResourceFile->Append(*resourceFileName); |
|
162 #endif |
|
163 |
|
164 iResourceFile.OpenL(iFsSession, *fullPathToResourceFile); |
|
165 CleanupStack::PopAndDestroy(2);//fullPathToResourceFile, resourceFileName |
|
166 iResourceFile.ConfirmSignatureL(0); |
|
167 } |
|
168 |
|
169 HBufC* CHtmlToCrtConverter::ReadResourceHBufCLC(TInt aResourceId) |
|
170 { |
|
171 HBufC8* resource=iResourceFile.AllocReadLC(aResourceId); |
|
172 TResourceReader resourceReader; |
|
173 resourceReader.SetBuffer(resource); |
|
174 |
|
175 HBufC* resourceText=resourceReader.ReadHBufCL(); |
|
176 CleanupStack::PopAndDestroy(resource); |
|
177 CleanupStack::PushL(resourceText); |
|
178 return resourceText; |
|
179 } |
|
180 |
|
181 TRgb CHtmlToCrtConverter::ReadResourceTRgbLC(TInt aResourceId) |
|
182 { |
|
183 HBufC8* resource=iResourceFile.AllocReadLC(aResourceId); |
|
184 TResourceReader resourceReader; |
|
185 resourceReader.SetBuffer(resource); |
|
186 |
|
187 TRgb resourceColor=0; |
|
188 TInt color=resourceReader.ReadUint8(); |
|
189 resourceColor.SetRed(color); |
|
190 color=resourceReader.ReadUint8(); |
|
191 resourceColor.SetGreen(color); |
|
192 color=resourceReader.ReadUint8(); |
|
193 resourceColor.SetBlue(color); |
|
194 |
|
195 CleanupStack::PopAndDestroy(resource); |
|
196 return resourceColor; |
|
197 } |
|
198 |
|
199 void CHtmlToCrtConverter::Cleanup() |
|
200 { |
|
201 delete iCharsetConvert; |
|
202 iCharsetConvert=NULL; |
|
203 delete iParser; |
|
204 iParser=NULL; |
|
205 delete iBuffer; |
|
206 iBuffer=NULL; |
|
207 delete iCnvCharacterSetConverter; |
|
208 iCnvCharacterSetConverter=NULL; |
|
209 delete iText; |
|
210 iText=NULL; |
|
211 delete iParaFormatLayer; |
|
212 iParaFormatLayer=NULL; |
|
213 delete iCharFormatLayer; |
|
214 iCharFormatLayer=NULL; |
|
215 delete iSourceFileName; |
|
216 iSourceFileName=NULL; |
|
217 delete iTargetFileName; |
|
218 iTargetFileName=NULL; |
|
219 iResourceFile.Close(); |
|
220 iFsSession.Close(); |
|
221 } |
|
222 |
|
223 void CHtmlToCrtConverter::FinaliseConversionL() |
|
224 { |
|
225 TInt error=iFsSession.MkDirAll(*iTargetFileName); |
|
226 if(!(error==KErrNone || error==KErrAlreadyExists)) |
|
227 User::Leave(error); |
|
228 |
|
229 CDirectFileStore* directFileStore=CDirectFileStore::ReplaceLC(iFsSession, *iTargetFileName, EFileWrite); |
|
230 directFileStore->SetTypeL(KDirectFileStoreLayoutUid); |
|
231 |
|
232 CStreamDictionary* dictionary=CStreamDictionary::NewLC(); |
|
233 if (iText->DocumentLength()) |
|
234 { |
|
235 iText->CopyToStoreL(*directFileStore, *dictionary, 0, iText->DocumentLength()); |
|
236 } |
|
237 |
|
238 RStoreWriteStream storeWriteStream; |
|
239 TStreamId streamId=storeWriteStream.CreateLC(*directFileStore); |
|
240 storeWriteStream << *dictionary; |
|
241 storeWriteStream.CommitL(); |
|
242 |
|
243 CleanupStack::PopAndDestroy(2); //storeWriteStream, dictionary |
|
244 directFileStore->SetRootL(streamId); |
|
245 directFileStore->CommitL(); |
|
246 CleanupStack::PopAndDestroy(directFileStore); |
|
247 |
|
248 #ifdef __CHTMLTOCRTCONV_COPY_TO_CLIPBOARD__ |
|
249 CClipboard* clipboard=CClipboard::NewForWritingLC(iFsSession); |
|
250 if (iText->DocumentLength()) |
|
251 { |
|
252 iText->CopyToStoreL((clipboard->Store()), (clipboard->StreamDictionary()), 0, iText->DocumentLength()); |
|
253 } |
|
254 clipboard->CommitL(); |
|
255 CleanupStack::PopAndDestroy(clipboard); |
|
256 #endif |
|
257 } |
|
258 |
|
259 CConverterBase2* CHtmlToCrtConverter::NewL() |
|
260 { |
|
261 CConverterBase2* newConverter=new (ELeave) CHtmlToCrtConverter(); |
|
262 return newConverter; |
|
263 } |
|
264 |
|
265 const TImplementationProxy ImplementationTable[] = |
|
266 { |
|
267 IMPLEMENTATION_PROXY_ENTRY(0x1000a90e,CHtmlToCrtConverter::NewL) |
|
268 }; |
|
269 |
|
270 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount) |
|
271 { |
|
272 aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy); |
|
273 return ImplementationTable; |
|
274 } |
|
275 |