examples/sfexamples/FileStream/src/FileStreamAppUi.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 <FileStream.rsg>
00033 
00034 #include "FileStreamAppUi.h"
00035 #include "FileStreamMainView.h"
00036 #include "FileStream.hrh"
00037 #include "employee.h"
00038 
00039 // CONSTANTS
00040 _LIT(KInt32FileName,    "c:\\data\\myfile.dat");
00041 _LIT(KEmployeeFileName, "c:\\data\\employee.dat");
00042 
00043 _LIT(KName,        "John Doe");
00044 _LIT(KPhoneNumber, "+1-222-333-444");
00045 
00046 
00047 const TChar KEndOfLine = '\f';
00048 
00049 
00050 const TInt KMaxNumber = 10;  // maximum length of a number
00051 const TInt KMaxBuffer = 100; // maximum length of buffer
00052 
00053 // MEMBER FUNCTIONS
00054 
00055 // --------------------------------------------------------------------------
00056 // Constructor
00057 // --------------------------------------------------------------------------
00058 void CFileStreamAppUi::ConstructL()
00059         {
00060 
00061 
00062         BaseConstructL(EAknEnableSkin);
00063         
00064         iMainView = CFileStreamMainView::NewL(ClientRect());
00065         }
00066         
00067 // --------------------------------------------------------------------------
00068 // Destructor
00069 // --------------------------------------------------------------------------
00070 CFileStreamAppUi::~CFileStreamAppUi()
00071     {
00072     if (iMainView)
00073         {
00074         delete iMainView;
00075         iMainView = NULL;
00076         }
00077     }
00078 
00079 // --------------------------------------------------------------------------
00080 // Handles user command.
00081 // --------------------------------------------------------------------------
00082 void CFileStreamAppUi::HandleCommandL(TInt aCommand)
00083         {
00084         switch ( aCommand )
00085                 {
00086                 // For S60, we need to handle this event, which is normally
00087                 // an event from the right soft key.
00088                 case EAknSoftkeyExit:
00089                 case EEikCmdExit:
00090                         {
00091                         Exit();
00092                         break;
00093                         }
00094                         
00095                 case EFileStreamWriteInt32:
00096                         {
00097                         // Write a TInt32 to the stream.
00098                         const TInt32 x = 12345; // new integer to be written
00099                         WriteInt32ToStreamL(iCoeEnv->FsSession(), KInt32FileName, x);
00100                         
00101                         // Display a message on the main view.
00102                         HBufC* message = iCoeEnv->AllocReadResourceLC(
00103                                         R_FILESTREAM_INT32WRITTEN);
00104                         iMainView->SetTextL(*message);
00105                         CleanupStack::PopAndDestroy(message);
00106                         break;
00107                         }
00108                         
00109                 case EFileStreamReadInt32:
00110                         {
00111                         // Read a TInt32 from the stream.
00112                         TInt32 x = ReadInt32FromStreamL(iCoeEnv->FsSession(),
00113                                         KInt32FileName);
00114                         
00115                         // Display the number on the main view.
00116                         TBuf<KMaxNumber> buffer;
00117                         buffer.AppendNum(x);
00118                         iMainView->SetTextL(buffer);
00119                         break;
00120                         }
00121                         
00122                 case EFileStreamAppendInt32:
00123                         {
00124                         // Write a TInt32 to the stream.
00125                         const TInt32 x = 98765; // new integer to be appended
00126                         AppendInt32ToStreamL(iCoeEnv->FsSession(), KInt32FileName, x);
00127                         
00128                         // Display a message on the main view.
00129                         HBufC* message = iCoeEnv->AllocReadResourceLC(
00130                                         R_FILESTREAM_INT32APPENDED);
00131                         iMainView->SetTextL(*message);
00132                         CleanupStack::PopAndDestroy(message);
00133                         break;
00134                         }
00135                         
00136                 case EFileStreamReadAllInt32:
00137                         {
00138                         // Create an array.
00139                         RArray<TInt> array;
00140                         CleanupClosePushL(array);
00141                         
00142                         // Read all TInt32's from the file.
00143                         ReadAllTInt32FromStreamL(iCoeEnv->FsSession(), KInt32FileName, array);
00144                         
00145                         // Display all TInt32's on the main view.
00146                         RBuf buffer;
00147                         buffer.CreateL(KMaxBuffer);
00148                         CleanupClosePushL(buffer);
00149                         for (TInt i = 0; i < array.Count(); i++)
00150                                 {
00151                                 // CAUTION: This code does not check whether there is enough
00152                                 // space in the buffer. If the buffer is not enough, this code
00153                                 // will panic (USER 11).
00154                                 buffer.AppendNum(array[i]);
00155                                 buffer.Append(KEndOfLine);
00156                                 }
00157                         iMainView->SetTextL(buffer);
00158 
00159                         CleanupStack::PopAndDestroy(2, &array); // buffer and array
00160                         
00161                         break;
00162                         }
00163                 
00164                 case EFileStreamWriteEmployee:
00165                         {
00166                         CEmployee* employee = new (ELeave) CEmployee();
00167                         CleanupStack::PushL(employee);
00168                         
00169                         // Add a new employee.
00170                         employee->SetIdentifier(1);
00171                         employee->SetName(KName);
00172                         employee->SetPhoneNumber(KPhoneNumber);
00173                         WriteEmployeeToStreamL(iCoeEnv->FsSession(), KEmployeeFileName,
00174                                         *employee);
00175                         
00176                         CleanupStack::PopAndDestroy(employee);
00177                         
00178                         // Display a message on the main view.
00179                         HBufC* message = iCoeEnv->AllocReadResourceLC(
00180                                         R_FILESTREAM_EMPLOYEEWRITTEN);
00181                         iMainView->SetTextL(*message);
00182                         CleanupStack::PopAndDestroy(message);
00183                         break;
00184                         }
00185                         
00186                 case EFileStreamReadEmployee:
00187                         {
00188                         CEmployee* employee = new (ELeave) CEmployee();
00189                         CleanupStack::PushL(employee);
00190                         
00191                         // Read an employee.
00192                         ReadEmployeeFromStreamL(iCoeEnv->FsSession(), KEmployeeFileName,
00193                                         *employee);
00194                         
00195                         // Display a message on the main view.
00196                         RBuf buffer;
00197                         buffer.CreateL(KMaxBuffer);
00198                         CleanupClosePushL(buffer);
00199                         buffer.AppendNum(employee->Identifier());
00200                         buffer.Append(KEndOfLine);
00201                         buffer.Append(employee->Name());
00202                         buffer.Append(KEndOfLine);
00203                         buffer.Append(employee->PhoneNumber());
00204                         buffer.Append(KEndOfLine);
00205                         iMainView->SetTextL(buffer);
00206                         
00207                         CleanupStack::PopAndDestroy(2, employee); // buffer and employee
00208                         break;
00209                         }
00210                 
00211                 default:
00212                         // Do nothing
00213                         break;
00214                 }
00215         }
00216 
00217 
00218 
00219 // --------------------------------------------------------------------------
00220 // Handles screen resolution/size changes.
00221 // --------------------------------------------------------------------------
00222 void CFileStreamAppUi::HandleResourceChangeL(TInt aType)
00223         {
00224         CAknAppUi::HandleResourceChangeL(aType);
00225         iMainView->SetRect(ClientRect());
00226         }
00227 
00228 
00229 void CFileStreamAppUi::WriteInt32ToStreamL(RFs& aFs,
00230                 const TDesC& aFileName, TInt32 aValue)
00231         {
00232         // Open the file stream.
00233         RFileWriteStream writeStream;
00234         User::LeaveIfError(writeStream.Replace(aFs, aFileName, EFileWrite));
00235         writeStream.PushL();
00236         
00237         // Write a TInt32 to the stream.
00238         writeStream.WriteInt32L(aValue);
00239         
00240         // Commit the change and close the stream.
00241         writeStream.CommitL();
00242         CleanupStack::Pop(&writeStream);
00243         writeStream.Release();
00244         }
00245 
00246 TInt32 CFileStreamAppUi::ReadInt32FromStreamL(RFs& aFs,
00247                 const TDesC& aFileName)
00248         {
00249         // Open the file stream.
00250         RFileReadStream readStream;
00251         User::LeaveIfError(readStream.Open(aFs, aFileName, EFileRead));
00252         CleanupClosePushL(readStream);
00253 
00254         // Read a TInt32 from the stream.
00255         TInt32 value;
00256         readStream >> value;
00257         
00258         // Close the stream and return the read TInt32.
00259         CleanupStack::PopAndDestroy(&readStream);
00260         return value;
00261         }
00262 
00263 void CFileStreamAppUi::AppendInt32ToStreamL(RFs& aFs,
00264                 const TDesC& aFileName, TInt32 aValue)
00265         {
00266         // Open the file stream.
00267         RFileWriteStream writeStream;
00268         TInt err = KErrNone;
00269         if (BaflUtils::FileExists(iCoeEnv->FsSession(), aFileName))
00270                 {
00271                 err = writeStream.Open(aFs, aFileName, EFileWrite);
00272                 }
00273         else
00274                 {
00275                 err = writeStream.Create(aFs, aFileName, EFileWrite);
00276                 }
00277         User::LeaveIfError(err);
00278         writeStream.PushL();
00279         
00280         // Move the file pointer to the end of file.
00281         writeStream.Sink()->SeekL(MStreamBuf::EWrite, EStreamEnd, 0);
00282         
00283         // Write a TInt32 at the end of file.
00284         writeStream.WriteInt32L(aValue);
00285         
00286         // Commit the change and close the file.
00287         writeStream.CommitL();
00288         CleanupStack::Pop(&writeStream);
00289         writeStream.Release();
00290         }
00291 
00292 void CFileStreamAppUi::ReadAllTInt32FromStreamL(RFs& aFs,
00293                 const TDesC& aFileName, RArray<TInt>& aArray)
00294         {
00295         // Open the file stream.
00296         RFileReadStream readStream;
00297         User::LeaveIfError(readStream.Open(aFs, aFileName, EFileRead));
00298         CleanupClosePushL(readStream);
00299 
00300         // Get the EOF position.
00301         TStreamPos eofPos(readStream.Source()->SizeL());
00302         
00303         // Get the current position of file pointer.
00304         TStreamPos currentPos(0);
00305 
00306         // Read all TInt2's until EOF.
00307         while (currentPos < eofPos)
00308                 {
00309                 TInt32 value;
00310                 readStream >> value;
00311                 aArray.Append(value);
00312                 
00313                 currentPos = readStream.Source()->TellL(MStreamBuf::ERead);
00314                 }
00315         
00316         CleanupStack::PopAndDestroy(&readStream);
00317         }
00318 
00319 void CFileStreamAppUi::WriteEmployeeToStreamL(RFs& aFs, const TDesC& aFileName,
00320                 const CEmployee& aEmployee)
00321         {
00322         // Open the file stream.
00323         RFileWriteStream writeStream;
00324         User::LeaveIfError(writeStream.Replace(aFs, aFileName, EFileWrite));
00325         CleanupClosePushL(writeStream);
00326         
00327         // Write employee to the stream.
00328         writeStream << aEmployee;
00329         
00330         // Commit the change and close the file.
00331         writeStream.CommitL();
00332         CleanupStack::PopAndDestroy(&writeStream);
00333         }
00334 
00335 void CFileStreamAppUi::ReadEmployeeFromStreamL(RFs& aFs, const TDesC& aFileName,
00336                 CEmployee& aEmployee)
00337         {
00338         // Open the file stream.
00339         RFileReadStream readStream;
00340         User::LeaveIfError(readStream.Open(aFs, aFileName, EFileRead));
00341         CleanupClosePushL(readStream);
00342         
00343         // Read a employee from the stream.
00344         readStream >> aEmployee;
00345         
00346         // Close the stream.
00347         CleanupStack::PopAndDestroy(&readStream);
00348         }
00349 
00350 // End of File

Generated by  doxygen 1.6.2