examples/sfexamples/FileStore/src/FileStoreAppUi.cpp

00001 /*
00002  * Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:
00028  */ 
00029 
00030 // INCLUDES
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 // CONSTANTS
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 // MEMBER FUNCTIONS
00062 
00063 // --------------------------------------------------------------------------
00064 // Constructor
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 // Destructor
00078 // --------------------------------------------------------------------------
00079 CFileStoreAppUi::~CFileStoreAppUi()
00080     {
00081     if (iMainView)
00082         {
00083         delete iMainView;
00084         iMainView = NULL;
00085         }
00086     iArray.ResetAndDestroy();
00087     }
00088 
00089 // --------------------------------------------------------------------------
00090 // Handles user command.
00091 // --------------------------------------------------------------------------
00092 void CFileStoreAppUi::HandleCommandL(TInt aCommand)
00093         {
00094         switch ( aCommand )
00095                 {
00096 
00097                 // For S60, we need to handle this event, which is normally
00098                 // an event from the right soft key.
00099                 case EAknSoftkeyExit:
00100                 case EEikCmdExit:
00101                         {
00102                         Exit();
00103                         break;
00104                         }
00105                 
00106                 case EFileStoreWriteEmployee:
00107                         {
00108                         // Create a new Employee.
00109                         CEmployee* employee = new (ELeave) CEmployee();
00110                         CleanupStack::PushL(employee);
00111                         employee->SetIdentifier(KIdentifier1);
00112                         employee->SetName(KName1);
00113                         employee->SetPhoneNumber(KPhoneNumber1);
00114                         
00115                         // Add the Employee to the file store.
00116                         WriteEmployeeToStoreL(iCoeEnv->FsSession(), KFileName1, *employee);
00117                         
00118                         CleanupStack::PopAndDestroy(employee);
00119                         
00120                         // Display a message on the main view.
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                         // Read the Employee from the file store.
00134                         ReadEmployeeFromStoreL(iCoeEnv->FsSession(), KFileName1, *employee);
00135                         
00136                         // Display the Employee on the main view.
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); // myEmployee and buffer
00149                         break;
00150                         }
00151                 
00152                 case EFileStoreWriteEmployeeMulti:
00153                         {
00154                         iArray.ResetAndDestroy();
00155                         
00156                         // Add two new Employees.
00157                         CEmployee* employee = new (ELeave) CEmployee();
00158                         employee->SetIdentifier(KIdentifier1);
00159                         employee->SetName(KName1);
00160                         employee->SetPhoneNumber(KPhoneNumber1);
00161                         iArray.Append(employee); // ownership is transferred
00162                         
00163                         employee = new (ELeave) CEmployee();
00164                         employee->SetIdentifier(KIdentifier2);
00165                         employee->SetName(KName2);
00166                         employee->SetPhoneNumber(KPhoneNumber2);
00167                         iArray.Append(employee); // ownership is transferred
00168                         
00169                         // Add the Employees to the file store.
00170                         WriteEmployeesWithMultiStreamsL(iCoeEnv->FsSession(), KFileName2,
00171                                         iArray);
00172                         
00173                         // Display a message on the main view.
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                         // Display all the Employees on the main view.
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                         // Do nothing
00212                         break;
00213                 }
00214         }
00215 
00216 
00217 // --------------------------------------------------------------------------
00218 // Handles screen resolution/size changes.
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         // Create a new file store.
00232         CFileStore* store = CDirectFileStore::ReplaceLC(aFs, aFileName, EFileWrite);
00233         store->SetTypeL(KDirectFileStoreLayoutUid);
00234         
00235         // Create a new stream on the store.
00236         RStoreWriteStream writeStream;
00237         TStreamId rootId = writeStream.CreateLC(*store);
00238         
00239         // Write Employee to the stream.
00240         writeStream << aEmployee;
00241         
00242         // Commit the changes to the stream and close the stream.
00243         writeStream.CommitL();
00244         CleanupStack::PopAndDestroy(&writeStream);
00245         
00246         // Set the root identifier of the store to this stream.
00247         store->SetRootL(rootId);
00248         
00249         // Commit the change to the store and close the store.
00250         store->Commit();
00251         CleanupStack::PopAndDestroy(store);
00252         }
00253 
00254 void CFileStoreAppUi::ReadEmployeeFromStoreL(RFs& aFs, const TDesC& aFileName,
00255                 CEmployee& aEmployee)
00256         {
00257         // Open the store.
00258         CFileStore* store = CDirectFileStore::OpenLC(aFs, aFileName, EFileRead);
00259 
00260         // Get the root stream. In this case, we have only one stream.
00261         RStoreReadStream readStream;
00262         readStream.OpenLC(*store, store->Root());
00263         
00264         // Read the Employee.
00265         readStream >> aEmployee;
00266         
00267         CleanupStack::PopAndDestroy(2, store); // readStream and store
00268         }
00269 
00270 void CFileStoreAppUi::WriteEmployeesWithMultiStreamsL(RFs& aFs,
00271                 const TDesC& aFileName,
00272                 const RPointerArray<CEmployee>& aArray)
00273         {
00274         // Create a new file store.
00275         CFileStore* store = CDirectFileStore::ReplaceLC(aFs, aFileName,
00276                         EFileWrite);
00277         store->SetTypeL(store->Layout());
00278         
00279         //
00280         // Write header information.
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         // Create a new employee to the store.
00294         //
00295         RStoreWriteStream employeeStream;
00296         TStreamId employeesId = employeeStream.CreateLC(*store);
00297         
00298         // Write Employees to the stream.
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         // Create a stream dictionary.
00309         //
00310         CStreamDictionary* dictionary = CStreamDictionary::NewLC();
00311         dictionary->AssignL(KHeaderUid, headerId);
00312         dictionary->AssignL(KEmployeesUid, employeesId);
00313         
00314         // Write stream dictionary in the root stream.
00315         RStoreWriteStream dictionaryStream;
00316         TStreamId rootId = dictionaryStream.CreateLC(*store);
00317         dictionaryStream << *dictionary;
00318         dictionaryStream.CommitL();
00319         
00320         CleanupStack::PopAndDestroy(2, dictionary); // dictionaryStream and dictionary
00321         
00322         // Set the root identifier of the store to this stream.
00323         store->SetRootL(rootId);
00324         
00325         // Commit the change to the store and close the store.
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         // Open the new file store.
00335         CFileStore* store = CDirectFileStore::OpenLC(aFs, aFileName,
00336                         EFileWrite);
00337         
00338         //
00339         // Read stream dictionary
00340         //
00341         CStreamDictionary* dictionary = CStreamDictionary::NewLC();
00342         RStoreReadStream dictionaryStream;
00343         dictionaryStream.OpenLC(*store, store->Root());
00344         dictionaryStream >> *dictionary;
00345         CleanupStack::PopAndDestroy(&dictionaryStream);
00346         
00347         // Get the identifier of each stream in this store.
00348         TStreamId headerId = dictionary->At(KHeaderUid);
00349         TStreamId employeesId = dictionary->At(KEmployeesUid);
00350         CleanupStack::PopAndDestroy(dictionary);
00351         
00352         //
00353         // Read header information.
00354         //
00355         RStoreReadStream headerStream;
00356         headerStream.OpenLC(*store, headerId);
00357         
00358         HBufC* signature = HBufC::NewLC(headerStream, KMaxBuffer);
00359         // If needed, check the signature here.
00360         CleanupStack::PopAndDestroy(signature);
00361         TUint32 version;
00362         headerStream >> version;
00363         TUint32 numberOfEmployees;
00364         headerStream >> numberOfEmployees;
00365         
00366         CleanupStack::PopAndDestroy(&headerStream);
00367         
00368         //
00369         // Read the employees streams.
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); // ownership is transferred
00380                 CleanupStack::Pop(employee);
00381                 }
00382         
00383         CleanupStack::PopAndDestroy(&employeeStream);
00384         
00385         // Commit the change to the store and close the store.
00386         CleanupStack::PopAndDestroy(store);
00387         }
00388 
00389 // End of File

Generated by  doxygen 1.6.2