examples/sfexamples/BinaryFile/src/BinaryFileAppUi.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 <f32file.h>
00032 #include <bautils.h>
00033 #include <BinaryFile.rsg>
00034 
00035 #include "BinaryFileAppUi.h"
00036 #include "BinaryFileMainView.h"
00037 #include "BinaryFile.hrh"
00038 
00039 // CONSTANTS
00040 const TInt KMaxBuffer = 512;
00041 _LIT(KNewFileName,    "c:\\data\\newfile.dat");
00042 _LIT(KSourceFileName, "c:\\data\\sourcefile.dat");
00043 _LIT(KDestFileName,   "c:\\data\\destfile.dat");
00044 
00045 // MEMBER FUNCTIONS
00046 
00047 // --------------------------------------------------------------------------
00048 // Constructor
00049 // --------------------------------------------------------------------------
00050 void CBinaryFileAppUi::ConstructL()
00051         {
00052         BaseConstructL(EAknEnableSkin); 
00053         iMainView = CBinaryFileMainView::NewL(ClientRect());
00054         BaflUtils::EnsurePathExistsL(iCoeEnv->FsSession(), KNewFileName);
00055         }
00056         
00057 // --------------------------------------------------------------------------
00058 // Destructor
00059 // --------------------------------------------------------------------------
00060 CBinaryFileAppUi::~CBinaryFileAppUi()
00061     {
00062     if (iMainView)
00063         {
00064         delete iMainView;
00065         iMainView = NULL;
00066         }
00067     }
00068 
00069 // --------------------------------------------------------------------------
00070 // Handles user command.
00071 // --------------------------------------------------------------------------
00072 void CBinaryFileAppUi::HandleCommandL(TInt aCommand)
00073         {
00074         switch ( aCommand )
00075                 {
00076                 // For S60, we need to handle this event, which is normally
00077                 // an event from the right soft key.
00078                 case EAknSoftkeyExit:
00079 
00080                 case EEikCmdExit:
00081                         {
00082                         Exit();
00083                         break;
00084                         }
00085                 
00086                 case EBinaryFileWriteToFile:
00087                         {
00088                         RFs& fileServer = iCoeEnv->FsSession();
00089                         
00090                         // Create a buffer to be written to the file.                           
00091                         RBuf8 buffer;
00092                         User::LeaveIfError(buffer.CreateMax(KMaxBuffer));
00093                         CleanupClosePushL(buffer);
00094                                 
00095                         // Fill the buffer with your data.
00096                         // …
00097                         for (TInt i = 0; i < KMaxBuffer; i++)
00098                                 {
00099                                 buffer[i] = i;
00100                                 }
00101 
00102                         // Write buffer to the file.
00103                         WriteDescToFileL(fileServer, KNewFileName, buffer);
00104 
00105                         // Delete the buffer.
00106                         CleanupStack::PopAndDestroy(&buffer);
00107                         
00108                         iEikonEnv->InfoWinL(
00109                                         R_BINARYFILE_CAPTION, R_BINARYFILE_FILEWRITTEN);
00110                         break;
00111                         }
00112                         
00113                 case EBinaryFileReadFromFile:
00114                         {
00115                         RFs& fileServer = iCoeEnv->FsSession();
00116                         
00117                         // Create a new buffer
00118                         RBuf8 buffer;
00119                         User::LeaveIfError(buffer.CreateMax(KMaxBuffer));
00120                         CleanupClosePushL(buffer);
00121                         
00122                         // Read from the file.
00123                         // We have to make sure that the buffer has enough space,
00124                         // otherwise it will panic.
00125                         ReadDescFromFileL(fileServer, KSourceFileName, buffer);
00126                         
00127                         // Write the buffer to a file.
00128                         WriteDescToFileL(fileServer, KDestFileName, buffer);
00129                         
00130                         // Delete the buffer.
00131                         CleanupStack::PopAndDestroy(&buffer);
00132                         
00133                         iEikonEnv->InfoWinL(R_BINARYFILE_CAPTION, R_BINARYFILE_FILEREAD);
00134                         break;
00135                         }
00136                 
00137                 default:
00138                         // Do nothing
00139                         break;
00140                 }
00141         }
00142 
00143         
00144 
00145 
00146 // --------------------------------------------------------------------------
00147 // Handles screen resolution/size changes.
00148 // --------------------------------------------------------------------------
00149 void CBinaryFileAppUi::HandleResourceChangeL(TInt aType)
00150         {
00151         CAknAppUi::HandleResourceChangeL(aType);
00152         iMainView->SetRect(ClientRect());
00153         }
00154 
00155 
00156 
00157 // --------------------------------------------------------------------------
00158 // Writes descriptor to a file.
00159 // --------------------------------------------------------------------------
00160 void CBinaryFileAppUi::WriteDescToFileL(RFs& aFs, const TDesC& aFileName,
00161                 const TDesC8& aBuffer)
00162         {
00163         RFile file;
00164         User::LeaveIfError(file.Replace(aFs, aFileName, EFileWrite));
00165         CleanupClosePushL(file);
00166         
00167         User::LeaveIfError(file.Write(aBuffer, aBuffer.Length()));
00168         
00169         CleanupStack::PopAndDestroy(&file);
00170         }
00171 
00172 // --------------------------------------------------------------------------
00173 // Reads descriptor from a file.
00174 // Note that the descriptor must have sufficient length; otherwise it
00175 // will leave.
00176 // --------------------------------------------------------------------------
00177 void CBinaryFileAppUi::ReadDescFromFileL(RFs& aFs, const TDesC& aFileName,
00178                 TDes8& aBuffer)
00179         {
00180         RFile file;
00181         User::LeaveIfError(file.Open(aFs, aFileName, EFileRead));
00182         CleanupClosePushL(file);
00183         
00184         // Get the file size.
00185         TInt fileSize;
00186         User::LeaveIfError(file.Size(fileSize));
00187         
00188         // If the maximum length of the buffer is less than the file size,
00189         // it means we don't have enough space.
00190         if (aBuffer.MaxLength() < fileSize)
00191                 {
00192                 User::Leave(KErrOverflow);
00193                 }
00194         User::LeaveIfError(file.Read(aBuffer, fileSize));
00195         
00196         CleanupStack::PopAndDestroy(&file); // file
00197         }
00198 
00199 // End of File

Generated by  doxygen 1.6.2