00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <bautils.h>
00032 #include <FileStore.rsg>
00033
00034 #include "FileStoreAppUi.h"
00035 #include "FileStoreMainView.h"
00036 #include "FileStore.hrh"
00037 #include "Employee.h"
00038
00039
00040 _LIT(KFileName1, "c:\\data\\filestore1.dat");
00041 _LIT(KFileName2, "c:\\data\\filestore2.dat");
00042
00043 const TInt KIdentifier1 = 1;
00044 _LIT(KName1, "John Doe");
00045 _LIT(KPhoneNumber1, "+1-222-333-4444");
00046
00047 const TInt KIdentifier2 = 2;
00048 _LIT(KName2, "Jane Roe");
00049 _LIT(KPhoneNumber2, "+1-234-567-8900");
00050
00051 const TUid KHeaderUid = TUid::Uid(0x01);
00052 const TUid KEmployeesUid = TUid::Uid(0x02);
00053
00054 _LIT(KSignature, "QRECIPES");
00055 const TUint32 KVersion = 1;
00056
00057 const TInt KMaxBuffer = 100;
00058 const TChar KEndOfLine = '\f';
00059
00060
00061
00062
00063
00064
00065
00066 void CFileStoreAppUi::ConstructL()
00067 {
00068
00069 BaseConstructL(EAknEnableSkin);
00070
00071 iMainView = CFileStoreMainView::NewL(ClientRect());
00072
00073 BaflUtils::EnsurePathExistsL(iCoeEnv->FsSession(), KFileName1);
00074 }
00075
00076
00077
00078
00079 CFileStoreAppUi::~CFileStoreAppUi()
00080 {
00081 if (iMainView)
00082 {
00083 delete iMainView;
00084 iMainView = NULL;
00085 }
00086 iArray.ResetAndDestroy();
00087 }
00088
00089
00090
00091
00092 void CFileStoreAppUi::HandleCommandL(TInt aCommand)
00093 {
00094 switch ( aCommand )
00095 {
00096
00097
00098
00099 case EAknSoftkeyExit:
00100 case EEikCmdExit:
00101 {
00102 Exit();
00103 break;
00104 }
00105
00106 case EFileStoreWriteEmployee:
00107 {
00108
00109 CEmployee* employee = new (ELeave) CEmployee();
00110 CleanupStack::PushL(employee);
00111 employee->SetIdentifier(KIdentifier1);
00112 employee->SetName(KName1);
00113 employee->SetPhoneNumber(KPhoneNumber1);
00114
00115
00116 WriteEmployeeToStoreL(iCoeEnv->FsSession(), KFileName1, *employee);
00117
00118 CleanupStack::PopAndDestroy(employee);
00119
00120
00121 HBufC* message = iCoeEnv->AllocReadResourceLC(
00122 R_FILESTORE_EMPLOYEEWRITTEN);
00123 iMainView->SetTextL(*message);
00124 CleanupStack::PopAndDestroy(message);
00125 break;
00126 }
00127
00128 case EFileStoreReadEmployee:
00129 {
00130 CEmployee* employee = new (ELeave) CEmployee();
00131 CleanupStack::PushL(employee);
00132
00133
00134 ReadEmployeeFromStoreL(iCoeEnv->FsSession(), KFileName1, *employee);
00135
00136
00137 RBuf buffer;
00138 buffer.CreateL(KMaxBuffer);
00139 CleanupClosePushL(buffer);
00140 buffer.AppendNum(employee->Identifier());
00141 buffer.Append(KEndOfLine);
00142 buffer.Append(employee->Name());
00143 buffer.Append(KEndOfLine);
00144 buffer.Append(employee->PhoneNumber());
00145 buffer.Append(KEndOfLine);
00146 iMainView->SetTextL(buffer);
00147
00148 CleanupStack::PopAndDestroy(2, employee);
00149 break;
00150 }
00151
00152 case EFileStoreWriteEmployeeMulti:
00153 {
00154 iArray.ResetAndDestroy();
00155
00156
00157 CEmployee* employee = new (ELeave) CEmployee();
00158 employee->SetIdentifier(KIdentifier1);
00159 employee->SetName(KName1);
00160 employee->SetPhoneNumber(KPhoneNumber1);
00161 iArray.Append(employee);
00162
00163 employee = new (ELeave) CEmployee();
00164 employee->SetIdentifier(KIdentifier2);
00165 employee->SetName(KName2);
00166 employee->SetPhoneNumber(KPhoneNumber2);
00167 iArray.Append(employee);
00168
00169
00170 WriteEmployeesWithMultiStreamsL(iCoeEnv->FsSession(), KFileName2,
00171 iArray);
00172
00173
00174 HBufC* message = iCoeEnv->AllocReadResourceLC(
00175 R_FILESTORE_EMPLOYEEWRITTENMULTI);
00176 iMainView->SetTextL(*message);
00177 CleanupStack::PopAndDestroy(message);
00178 break;
00179 }
00180
00181 case EFileStoreReadEmployeeMulti:
00182 {
00183 iArray.ResetAndDestroy();
00184
00185 ReadEmployeesWithMultiStreamsL(iCoeEnv->FsSession(), KFileName2,
00186 iArray);
00187
00188
00189 RBuf buffer;
00190 buffer.CreateL(KMaxBuffer * iArray.Count());
00191 CleanupClosePushL(buffer);
00192 for (TInt i = 0; i < iArray.Count(); i++)
00193 {
00194 const CEmployee* employee = iArray[i];
00195 buffer.AppendNum(employee->Identifier());
00196 buffer.Append(KEndOfLine);
00197 buffer.Append(employee->Name());
00198 buffer.Append(KEndOfLine);
00199 buffer.Append(employee->PhoneNumber());
00200 buffer.Append(KEndOfLine);
00201 buffer.Append(KEndOfLine);
00202 }
00203
00204 iMainView->SetTextL(buffer);
00205
00206 CleanupStack::PopAndDestroy(&buffer);
00207 break;
00208 }
00209
00210 default:
00211
00212 break;
00213 }
00214 }
00215
00216
00217
00218
00219
00220 void CFileStoreAppUi::HandleResourceChangeL(TInt aType)
00221 {
00222 CAknAppUi::HandleResourceChangeL(aType);
00223 iMainView->SetRect(ClientRect());
00224 }
00225
00226
00227
00228 void CFileStoreAppUi::WriteEmployeeToStoreL(RFs& aFs, const TDesC& aFileName,
00229 const CEmployee& aEmployee)
00230 {
00231
00232 CFileStore* store = CDirectFileStore::ReplaceLC(aFs, aFileName, EFileWrite);
00233 store->SetTypeL(KDirectFileStoreLayoutUid);
00234
00235
00236 RStoreWriteStream writeStream;
00237 TStreamId rootId = writeStream.CreateLC(*store);
00238
00239
00240 writeStream << aEmployee;
00241
00242
00243 writeStream.CommitL();
00244 CleanupStack::PopAndDestroy(&writeStream);
00245
00246
00247 store->SetRootL(rootId);
00248
00249
00250 store->Commit();
00251 CleanupStack::PopAndDestroy(store);
00252 }
00253
00254 void CFileStoreAppUi::ReadEmployeeFromStoreL(RFs& aFs, const TDesC& aFileName,
00255 CEmployee& aEmployee)
00256 {
00257
00258 CFileStore* store = CDirectFileStore::OpenLC(aFs, aFileName, EFileRead);
00259
00260
00261 RStoreReadStream readStream;
00262 readStream.OpenLC(*store, store->Root());
00263
00264
00265 readStream >> aEmployee;
00266
00267 CleanupStack::PopAndDestroy(2, store);
00268 }
00269
00270 void CFileStoreAppUi::WriteEmployeesWithMultiStreamsL(RFs& aFs,
00271 const TDesC& aFileName,
00272 const RPointerArray<CEmployee>& aArray)
00273 {
00274
00275 CFileStore* store = CDirectFileStore::ReplaceLC(aFs, aFileName,
00276 EFileWrite);
00277 store->SetTypeL(store->Layout());
00278
00279
00280
00281
00282 RStoreWriteStream headerStream;
00283 TStreamId headerId = headerStream.CreateLC(*store);
00284
00285 headerStream << KSignature;
00286 headerStream << KVersion;
00287 headerStream << (TUint32) aArray.Count();
00288
00289 headerStream.CommitL();
00290 CleanupStack::PopAndDestroy(&headerStream);
00291
00292
00293
00294
00295 RStoreWriteStream employeeStream;
00296 TStreamId employeesId = employeeStream.CreateLC(*store);
00297
00298
00299 for (TInt i = 0; i < aArray.Count(); i++)
00300 {
00301 employeeStream << *aArray[i];
00302 }
00303
00304 employeeStream.CommitL();
00305 CleanupStack::PopAndDestroy(&employeeStream);
00306
00307
00308
00309
00310 CStreamDictionary* dictionary = CStreamDictionary::NewLC();
00311 dictionary->AssignL(KHeaderUid, headerId);
00312 dictionary->AssignL(KEmployeesUid, employeesId);
00313
00314
00315 RStoreWriteStream dictionaryStream;
00316 TStreamId rootId = dictionaryStream.CreateLC(*store);
00317 dictionaryStream << *dictionary;
00318 dictionaryStream.CommitL();
00319
00320 CleanupStack::PopAndDestroy(2, dictionary);
00321
00322
00323 store->SetRootL(rootId);
00324
00325
00326 store->Commit();
00327 CleanupStack::PopAndDestroy(store);
00328 }
00329
00330 void CFileStoreAppUi::ReadEmployeesWithMultiStreamsL(RFs& aFs,
00331 const TDesC& aFileName,
00332 RPointerArray<CEmployee>& aArray)
00333 {
00334
00335 CFileStore* store = CDirectFileStore::OpenLC(aFs, aFileName,
00336 EFileWrite);
00337
00338
00339
00340
00341 CStreamDictionary* dictionary = CStreamDictionary::NewLC();
00342 RStoreReadStream dictionaryStream;
00343 dictionaryStream.OpenLC(*store, store->Root());
00344 dictionaryStream >> *dictionary;
00345 CleanupStack::PopAndDestroy(&dictionaryStream);
00346
00347
00348 TStreamId headerId = dictionary->At(KHeaderUid);
00349 TStreamId employeesId = dictionary->At(KEmployeesUid);
00350 CleanupStack::PopAndDestroy(dictionary);
00351
00352
00353
00354
00355 RStoreReadStream headerStream;
00356 headerStream.OpenLC(*store, headerId);
00357
00358 HBufC* signature = HBufC::NewLC(headerStream, KMaxBuffer);
00359
00360 CleanupStack::PopAndDestroy(signature);
00361 TUint32 version;
00362 headerStream >> version;
00363 TUint32 numberOfEmployees;
00364 headerStream >> numberOfEmployees;
00365
00366 CleanupStack::PopAndDestroy(&headerStream);
00367
00368
00369
00370
00371 RStoreReadStream employeeStream;
00372 employeeStream.OpenLC(*store, employeesId);
00373
00374 for (TUint32 i = 0; i < numberOfEmployees; i++)
00375 {
00376 CEmployee* employee = new (ELeave) CEmployee();
00377 CleanupStack::PushL(employee);
00378 employeeStream >> *employee;
00379 aArray.Append(employee);
00380 CleanupStack::Pop(employee);
00381 }
00382
00383 CleanupStack::PopAndDestroy(&employeeStream);
00384
00385
00386 CleanupStack::PopAndDestroy(store);
00387 }
00388
00389