|
1 /* |
|
2 * Copyright (c) 2006-2007 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: The class implements a long task wrapper for load file |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 // INCLUDE FILES |
|
21 #include <coemain.h> |
|
22 #include <s32file.h> |
|
23 #include <charconv.h> |
|
24 #include "NpdLoadFileAO.h" |
|
25 #include "NpdUtil.h" |
|
26 |
|
27 // CONSTANTS |
|
28 |
|
29 // the max length of the sample text which attempts to determine the character set |
|
30 static const TInt KMaxSampleLengthForAutoDetection = 1024; |
|
31 |
|
32 // the file length load in one step |
|
33 static const TInt KLengthOneStepLoad = 1024 * 10; |
|
34 |
|
35 // when the value is large than this, the auto detect result is available |
|
36 static const TInt KValueAutoDetectCharacterSetAvailable = 50; |
|
37 |
|
38 // ============================= MEMBER FUNCTIONS ============================= |
|
39 |
|
40 // ---------------------------------------------------------------------------- |
|
41 // CNotepadLoadFileAO::CNotepadLoadFileAO() |
|
42 // |
|
43 // ---------------------------------------------------------------------------- |
|
44 CNotepadLoadFileAO::CNotepadLoadFileAO( MNotepadLoadFileObserver* aObserver, |
|
45 CCoeEnv& aCoeEnv, |
|
46 RFile& aFile, |
|
47 TBool aGuessEncoding, |
|
48 TUint aEncoding, |
|
49 CPlainText& aText ): |
|
50 CActive( CActive::EPriorityLow ), |
|
51 iObserver( aObserver ), |
|
52 iCoeEnv( aCoeEnv ), |
|
53 iGuessEncoding( aGuessEncoding ), |
|
54 iEncoding( aEncoding ), |
|
55 iText( aText ) |
|
56 { |
|
57 iFile.Duplicate( aFile ); |
|
58 } |
|
59 |
|
60 // ---------------------------------------------------------------------------- |
|
61 // CNotepadLoadFileAO::~CNotepadLoadFileAO() |
|
62 // |
|
63 // ---------------------------------------------------------------------------- |
|
64 CNotepadLoadFileAO::~CNotepadLoadFileAO() |
|
65 { |
|
66 iFile.Close(); |
|
67 Cancel(); |
|
68 } |
|
69 |
|
70 // ---------------------------------------------------------------------------- |
|
71 // CNotepadLoadFileAO::NewL() |
|
72 // |
|
73 // ---------------------------------------------------------------------------- |
|
74 CNotepadLoadFileAO* CNotepadLoadFileAO::NewL( MNotepadLoadFileObserver* aObserver, |
|
75 CCoeEnv& aCoeEnv, |
|
76 RFile& aFile, |
|
77 TBool aGuessEncoding, |
|
78 TUint aEncoding, |
|
79 CPlainText& aText ) |
|
80 { |
|
81 CNotepadLoadFileAO* self = new (ELeave) CNotepadLoadFileAO(aObserver, |
|
82 aCoeEnv, aFile, aGuessEncoding, aEncoding, aText); |
|
83 |
|
84 CleanupStack::PushL( self ); |
|
85 self->ConstructL(); |
|
86 CleanupStack::Pop( self ); |
|
87 return self; |
|
88 } |
|
89 |
|
90 // ---------------------------------------------------------------------------- |
|
91 // CNotepadLoadFileAO::ConstructL() |
|
92 // |
|
93 // ---------------------------------------------------------------------------- |
|
94 void CNotepadLoadFileAO::ConstructL() |
|
95 { |
|
96 CActiveScheduler::Add( this ); |
|
97 } |
|
98 |
|
99 // ---------------------------------------------------------------------------- |
|
100 // CNotepadLoadFileAO::StartLoadFile() |
|
101 // |
|
102 // ---------------------------------------------------------------------------- |
|
103 TInt CNotepadLoadFileAO::StartLoadFile() |
|
104 { |
|
105 TInt err( KErrAlreadyExists ); |
|
106 if ( !IsActive() ) |
|
107 { |
|
108 err = KErrNone; |
|
109 TRAP ( err, InitImportExportParamL() ) |
|
110 if ( err == KErrNone ) |
|
111 { |
|
112 TRequestStatus *statue = &iStatus; |
|
113 User::RequestComplete( statue, KErrStep ); |
|
114 SetActive(); |
|
115 } |
|
116 } |
|
117 return err; |
|
118 } |
|
119 |
|
120 // ---------------------------------------------------------------------------- |
|
121 // CNotepadLoadFileAO::CancelLoadFile() |
|
122 // |
|
123 // ---------------------------------------------------------------------------- |
|
124 void CNotepadLoadFileAO::CancelLoadFile() |
|
125 { |
|
126 iCancel = ETrue; |
|
127 } |
|
128 |
|
129 |
|
130 // ---------------------------------------------------------------------------- |
|
131 // CNotepadLoadFileAO::RunL() |
|
132 // |
|
133 // ---------------------------------------------------------------------------- |
|
134 void CNotepadLoadFileAO::RunL() |
|
135 { |
|
136 TInt status = iStatus.Int(); |
|
137 if ( status == KErrStep ) |
|
138 { |
|
139 DoStepL(); |
|
140 } |
|
141 iObserver->NotifyCompletedL( status ); |
|
142 } |
|
143 |
|
144 // ---------------------------------------------------------------------------- |
|
145 // CNotepadLoadFileAO::DoCancel() |
|
146 // |
|
147 // ---------------------------------------------------------------------------- |
|
148 void CNotepadLoadFileAO::DoCancel() |
|
149 { |
|
150 iCancel = ETrue; |
|
151 } |
|
152 |
|
153 // ---------------------------------------------------------------------------- |
|
154 // CNotepadLoadFileAO::DoStepL() |
|
155 // |
|
156 // ---------------------------------------------------------------------------- |
|
157 void CNotepadLoadFileAO::DoStepL() |
|
158 { |
|
159 TInt completeCode = KErrStep; |
|
160 // if the load file completed |
|
161 if ( iReadPos >= iFileSize ) |
|
162 { |
|
163 completeCode = KErrNone; |
|
164 } |
|
165 // if the load file canceled |
|
166 else if ( iCancel ) |
|
167 { |
|
168 completeCode = KErrCancel; |
|
169 } |
|
170 // load file for one step |
|
171 else |
|
172 { |
|
173 CPlainText::TImportExportResult result; |
|
174 TInt leaveSize = iFileSize - iReadPos ; |
|
175 iParam.iMaxInputChars = leaveSize > KLengthOneStepLoad ? KLengthOneStepLoad: leaveSize; |
|
176 TInt err = KErrNone; |
|
177 RFile tempFile; |
|
178 tempFile.Duplicate( iFile ); |
|
179 RFileReadStream stream( tempFile,iReadPos ); |
|
180 CleanupClosePushL(stream); |
|
181 TRAP( err, iText.ImportTextL( iText.DocumentLength(), stream, iParam, result) ); |
|
182 stream.Release();// closing of tempfile |
|
183 CleanupStack::PopAndDestroy(); // stream |
|
184 if ( err != KErrNone ) |
|
185 { |
|
186 completeCode = err; |
|
187 } |
|
188 iReadPos += result.iInputChars; |
|
189 } |
|
190 |
|
191 TRequestStatus *statue = &iStatus; |
|
192 User::RequestComplete( statue,completeCode ); |
|
193 SetActive(); |
|
194 } |
|
195 |
|
196 // ---------------------------------------------------------------------------- |
|
197 // CNotepadLoadFileAO::InitImportExportParamL() |
|
198 // |
|
199 // ---------------------------------------------------------------------------- |
|
200 void CNotepadLoadFileAO::InitImportExportParamL() |
|
201 { |
|
202 User::LeaveIfError( iFile.Size( iFileSize ) ); |
|
203 |
|
204 // need to guess the foreign encoding for this file or not |
|
205 if ( !iGuessEncoding ) |
|
206 { |
|
207 iParam.iForeignEncoding = iEncoding; |
|
208 } |
|
209 else |
|
210 { |
|
211 TBuf8<KMaxSampleLengthForAutoDetection> sample; |
|
212 User::LeaveIfError( iFile.Read( sample, iFileSize > KMaxSampleLengthForAutoDetection ? |
|
213 KMaxSampleLengthForAutoDetection : iFileSize ) ); |
|
214 |
|
215 // Creates an array identifying all the character sets for which conversion is available |
|
216 CArrayFix<CCnvCharacterSetConverter::SCharacterSet>* |
|
217 lists = CCnvCharacterSetConverter::CreateArrayOfCharacterSetsAvailableLC( |
|
218 iCoeEnv.FsSession() ); |
|
219 |
|
220 // Attempts to determine the character set of the sample text from those supported on the phone |
|
221 TInt confidence(0); |
|
222 TUint charset(0); |
|
223 CCnvCharacterSetConverter::AutoDetectCharacterSetL( confidence, charset, *lists, sample ); |
|
224 CleanupStack::PopAndDestroy(); |
|
225 |
|
226 // the auto detect result is available |
|
227 if ( confidence > KValueAutoDetectCharacterSetAvailable ) |
|
228 { |
|
229 iParam.iForeignEncoding = charset; |
|
230 } |
|
231 else |
|
232 { |
|
233 // Checks wether the descriptor contains big endian unicode text |
|
234 if ( NotepadUtil::IsBigEndianUnicodeText( sample ) ) |
|
235 { |
|
236 iParam.iForeignEncoding = KCharacterSetIdentifierUnicodeBig; |
|
237 } |
|
238 // Checks wether the descriptor contains little endian unicode text |
|
239 else if ( NotepadUtil::IsLittleEndianUnicodeText( sample ) ) |
|
240 { |
|
241 iParam.iForeignEncoding = KCharacterSetIdentifierUnicodeLittle; |
|
242 } |
|
243 // Checks wether the descriptor contains UTF8 text |
|
244 else if ( NotepadUtil::IsUTF8Text( sample ) ) |
|
245 { |
|
246 iParam.iForeignEncoding = KCharacterSetIdentifierUtf8; |
|
247 } |
|
248 // Checks wether the descriptor contains SHIFT-JIS encoded text |
|
249 else if ( NotepadUtil::IsShiftJisText( sample ) ) |
|
250 { |
|
251 iParam.iForeignEncoding = KCharacterSetIdentifierShiftJis; |
|
252 } |
|
253 // Guess the encoding from the language |
|
254 else |
|
255 { |
|
256 iParam.iForeignEncoding = NotepadUtil::GuessEncodingFromLanguage(); |
|
257 if ( iParam.iForeignEncoding == 0 ) // Variant not found |
|
258 { |
|
259 iParam.iForeignEncoding = KCharacterSetIdentifierAscii; |
|
260 } |
|
261 } |
|
262 } |
|
263 } |
|
264 } |
|
265 |
|
266 |
|
267 // End of File |