|
1 /* |
|
2 * Copyright (c) 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: XML importer |
|
15 * |
|
16 */ |
|
17 |
|
18 // System includes |
|
19 #include <s32file.h> // RFileReadStream |
|
20 #include <f32file.h> // RFs |
|
21 #include <cntitem.h> // CContactItem |
|
22 #include <matchdata.h> // CMatchData |
|
23 #include <parser.h> // CParser |
|
24 |
|
25 // User includes |
|
26 #include "PdcXMLImporter.h" // CPdcXmlImporter |
|
27 #include "PdcXMLContentHandler.h" // CPdcXmlContentHandler |
|
28 |
|
29 // Constants |
|
30 _LIT( KXmlWildCard, "*.xml"); // Wildcard pattern for Xml files |
|
31 _LIT8( KXmlMimeType, "text/xml" ); // XML mimie type |
|
32 |
|
33 using namespace Xml; |
|
34 |
|
35 // ======== MEMBER FUNCTIONS ======== |
|
36 |
|
37 // --------------------------------------------------------------------------- |
|
38 // CPdcXmlImporter::NewL |
|
39 // Symbian 1st phase constructor |
|
40 // @param aFs file system |
|
41 // @param aContactStore contacts store |
|
42 // @param aLinkArray links to contacts added. |
|
43 // @return a CPdcXmlImporter object. |
|
44 // --------------------------------------------------------------------------- |
|
45 // |
|
46 CPdcXmlImporter* CPdcXmlImporter::NewL( RFs& aFs, |
|
47 MVPbkContactStore& aContactStore, CVPbkContactLinkArray& aLinkArray ) |
|
48 { |
|
49 CPdcXmlImporter* self = new( ELeave ) CPdcXmlImporter( aFs, |
|
50 aContactStore, aLinkArray ); |
|
51 CleanupStack::PushL( self ); |
|
52 self->ConstructL(); |
|
53 CleanupStack::Pop( self ); |
|
54 return self; |
|
55 } |
|
56 |
|
57 // --------------------------------------------------------------------------- |
|
58 // CPdcXmlImporter::~CPdcXmlImporter |
|
59 // Destructor |
|
60 // --------------------------------------------------------------------------- |
|
61 // |
|
62 CPdcXmlImporter::~CPdcXmlImporter() |
|
63 { |
|
64 Cancel(); |
|
65 |
|
66 iFile.Close(); |
|
67 |
|
68 delete iContentHandler; |
|
69 delete iXmlParser; |
|
70 delete iXmlMatchData; |
|
71 } |
|
72 |
|
73 // --------------------------------------------------------------------------- |
|
74 // CPdcXmlImporter::CPdcXmlImporter |
|
75 // C++ constructor |
|
76 // @param aFs file system |
|
77 // @param aContactStore contacts store |
|
78 // @param aLinkArray links to contacts added. |
|
79 // --------------------------------------------------------------------------- |
|
80 // |
|
81 CPdcXmlImporter::CPdcXmlImporter( RFs& aFs, |
|
82 MVPbkContactStore& aContactStore, CVPbkContactLinkArray& aLinkArray ) |
|
83 : CActive( EPriorityNormal ), |
|
84 iContactStore( aContactStore ), |
|
85 iFs( aFs ), |
|
86 iLinkArray( aLinkArray ) |
|
87 { |
|
88 } |
|
89 |
|
90 // --------------------------------------------------------------------------- |
|
91 // CPdcXmlImporter::ConstructL |
|
92 // Second-phase constructor |
|
93 // --------------------------------------------------------------------------- |
|
94 // |
|
95 void CPdcXmlImporter::ConstructL() |
|
96 { |
|
97 CActiveScheduler::Add( this ); |
|
98 } |
|
99 |
|
100 // --------------------------------------------------------------------------- |
|
101 // CPdcXmlImporter::GetXmlContactsL |
|
102 // Opens an xml file if it exists and starts parseing the contents |
|
103 // @param aFileDirectory directory containing xml file |
|
104 // @param aCallerStatus iStatus of caller |
|
105 // --------------------------------------------------------------------------- |
|
106 // |
|
107 void CPdcXmlImporter::GetXmlContactsL(const TDesC& aFileDirectory, |
|
108 TRequestStatus& aCallerStatus ) |
|
109 { |
|
110 LOGTEXT(_L("CPdcXmlImporter::GetXmlContactsL")); |
|
111 aCallerStatus = KRequestPending; |
|
112 |
|
113 // Is there an XML document in the directory. As there should be only one |
|
114 // XML document if there are more than one, it is only the first one that |
|
115 // is parsed. |
|
116 |
|
117 // Get list of xml files in the directory |
|
118 TFindFile fileFinder( iFs ); |
|
119 |
|
120 CDir* xmlFileList = NULL; |
|
121 TInt error = fileFinder.FindWildByDir( KXmlWildCard, |
|
122 aFileDirectory, xmlFileList); |
|
123 |
|
124 CleanupStack::PushL( xmlFileList ); |
|
125 |
|
126 // Is there a XML file in the directory |
|
127 if ( error == KErrNotFound ) |
|
128 { |
|
129 // If the are no Xml file in the directory, complete the |
|
130 // caller's TRequestStatus |
|
131 TRequestStatus* s = &aCallerStatus; |
|
132 User::RequestComplete( s, KErrNone ); |
|
133 } |
|
134 else |
|
135 { |
|
136 // Create the filename |
|
137 const TDesC& fileName = (*xmlFileList)[0].iName; |
|
138 TParse fileAndPath; |
|
139 fileAndPath.Set( fileName, &aFileDirectory, NULL ); |
|
140 const TDesC& xmlFileName = fileAndPath.FullName(); |
|
141 |
|
142 // Store the callers TRequestStatus |
|
143 iCallerStatus = &aCallerStatus; |
|
144 |
|
145 // Create match data to find the correct xml library |
|
146 iXmlMatchData = CMatchData::NewL(); |
|
147 iXmlMatchData->SetMimeTypeL(KXmlMimeType); |
|
148 iXmlMatchData->SetVariantL(_L8("libxml2")); |
|
149 |
|
150 // Create rge content handler |
|
151 iContentHandler = |
|
152 CPdcXmlContentHandler::NewL( iContactStore, iLinkArray); |
|
153 |
|
154 //transfer the pointer of ContactManager to contenthandler |
|
155 iContentHandler->SetContactManager(iContactManager); |
|
156 |
|
157 // create the xml parser |
|
158 iXmlParser = CParser::NewL( *iXmlMatchData, *iContentHandler ); |
|
159 |
|
160 // Set string table |
|
161 iContentHandler->SetStringPoolL ( iXmlParser->StringPool() ); |
|
162 |
|
163 // Load the file |
|
164 User::LeaveIfError( |
|
165 iFile.Open( iFs, xmlFileName, EFileRead|EFileShareReadersOnly ) ); |
|
166 // Read the first block of data |
|
167 iFile.Read( iXmlData, iStatus ); |
|
168 SetActive(); |
|
169 } |
|
170 |
|
171 CleanupStack::PopAndDestroy( xmlFileList ); |
|
172 } |
|
173 |
|
174 // --------------------------------------------------------------------------- |
|
175 // CPdcXmlImporter::DoCancel |
|
176 // From CActive |
|
177 // Completes the caller's TRequestStatus with KErrCancel |
|
178 // --------------------------------------------------------------------------- |
|
179 // |
|
180 void CPdcXmlImporter::DoCancel() |
|
181 { |
|
182 // If the vCard importer is cancelled, we need to complete the |
|
183 // callers TRequestStatus. |
|
184 User::RequestComplete( iCallerStatus, KErrCancel ); |
|
185 |
|
186 // Cancel the file read operation |
|
187 iFile.ReadCancel(); |
|
188 } |
|
189 |
|
190 // --------------------------------------------------------------------------- |
|
191 // CPdcXmlImporter::RunL |
|
192 // From CActive |
|
193 // When the request is completed, data read is past to the XML parser, and the |
|
194 // next block of data is read asyncronously. If there is no more data to be |
|
195 // read the caller's TRequestStatus is completed. |
|
196 // --------------------------------------------------------------------------- |
|
197 // |
|
198 void CPdcXmlImporter::RunL() |
|
199 { |
|
200 // Leave if there is an error |
|
201 User::LeaveIfError( iStatus.Int() ); |
|
202 |
|
203 if ( iXmlData.Length() ) |
|
204 { |
|
205 // Parse the Xml data |
|
206 iXmlParser->ParseL( iXmlData ); |
|
207 |
|
208 // Read the next block of data |
|
209 iFile.Read( iXmlData, iStatus ); |
|
210 SetActive(); |
|
211 } |
|
212 else |
|
213 { |
|
214 // Reached the end of the file. So complete |
|
215 // the caller's TRequestStatus |
|
216 User::RequestComplete( iCallerStatus, KErrNone ); |
|
217 } |
|
218 } |
|
219 |
|
220 // --------------------------------------------------------------------------- |
|
221 // CPdcXmlImporter::RunError |
|
222 // From CActive |
|
223 // If RunL leaves, the caller's TRequestStatus is completed with the error. |
|
224 // --------------------------------------------------------------------------- |
|
225 // |
|
226 TInt CPdcXmlImporter::RunError( TInt aError ) |
|
227 { |
|
228 LOGTEXT(_L("CPdcXmlImporter::RunError")); |
|
229 // Report the error to the caller of the GetXmlContactsL function by |
|
230 // completing the callers activeObject. |
|
231 User::RequestComplete( iCallerStatus, aError ); |
|
232 return KErrNone; |
|
233 } |
|
234 |
|
235 // --------------------------------------------------------------------------- |
|
236 // CPdcXmlImporter:: |
|
237 // This method get the pointer of contactmanager from CPdcEngine |
|
238 // @param aContactManager |
|
239 // --------------------------------------------------------------------------- |
|
240 // |
|
241 void CPdcXmlImporter::SetContactManager(CVPbkContactManager* aContactManager) |
|
242 { |
|
243 iContactManager = aContactManager; |
|
244 } |
|
245 |
|
246 // End of File |