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 <TextFile.rsg> 00033 00034 #include "TextFileAppUi.h" 00035 #include "TextFileMainView.h" 00036 #include "TextFile.hrh" 00037 00038 // CONSTANTS 00039 const TInt KMaxBuffer = 256; 00040 _LIT(KSourceFileName, "c:\\data\\myfile.txt"); 00041 00042 const TChar KDelimiter = '\f'; 00043 00044 00045 // MEMBER FUNCTIONS 00046 00047 // -------------------------------------------------------------------------- 00048 // Constructor 00049 // -------------------------------------------------------------------------- 00050 void CTextFileAppUi::ConstructL() 00051 { 00052 BaseConstructL(EAknEnableSkin); 00053 00054 iMainView = CTextFileMainView::NewL(ClientRect()); 00055 } 00056 00057 // -------------------------------------------------------------------------- 00058 // Destructor 00059 // -------------------------------------------------------------------------- 00060 CTextFileAppUi::~CTextFileAppUi() 00061 { 00062 if (iMainView) 00063 { 00064 delete iMainView; 00065 iMainView = NULL; 00066 } 00067 iBuffer.Close(); 00068 } 00069 00070 // -------------------------------------------------------------------------- 00071 // Handles user command. 00072 // -------------------------------------------------------------------------- 00073 void CTextFileAppUi::HandleCommandL(TInt aCommand) 00074 { 00075 switch ( aCommand ) 00076 { 00077 // For S60, we need to handle this event, which is normally 00078 // an event from the right soft key. 00079 case EAknSoftkeyExit: 00080 case EEikCmdExit: 00081 { 00082 Exit(); 00083 break; 00084 } 00085 00086 case ETextFileReadTextFile: 00087 { 00088 RFs& fileServer = iCoeEnv->FsSession(); 00089 00090 // Delete the old string first. 00091 iBuffer.Close(); 00092 iBuffer.CreateL(KNullDesC, 0); 00093 00094 // Read the string from the file. 00095 ReadFromTextFileL(fileServer, KSourceFileName); 00096 00097 // Set the text in the main view to the string. 00098 iMainView->SetTextL(iBuffer); 00099 00100 iEikonEnv->InfoWinL( 00101 R_TEXTFILE_CAPTION, R_TEXTFILE_FILEREAD); 00102 break; 00103 } 00104 00105 default: 00106 // Do nothing 00107 break; 00108 } 00109 } 00110 00111 00112 00113 // -------------------------------------------------------------------------- 00114 // Handles screen resolution/size changes. 00115 // -------------------------------------------------------------------------- 00116 void CTextFileAppUi::HandleResourceChangeL(TInt aType) 00117 { 00118 CAknAppUi::HandleResourceChangeL(aType); 00119 iMainView->SetRect(ClientRect()); 00120 } 00121 00122 00123 // -------------------------------------------------------------------------- 00124 // Reads text file to an array. 00125 // -------------------------------------------------------------------------- 00126 void CTextFileAppUi::ReadFromTextFileL(RFs& aFs, const TDesC& aFileName) 00127 { 00128 // Open the file for reading. 00129 RFile file; 00130 User::LeaveIfError(file.Open(aFs, aFileName, EFileRead | EFileStreamText)); 00131 CleanupClosePushL(file); 00132 00133 // Create a new instance of TFileText that point to file variable. 00134 TFileText fileText; 00135 fileText.Set(file); 00136 00137 // Create a buffer to read the text file. 00138 TBuf<KMaxBuffer> buffer; 00139 00140 // Loop to read the buffer. It read the buffer until err is KErrEof, 00141 // which means it reached end of file. 00142 TInt err = KErrNone; 00143 while (err != KErrEof) 00144 { 00145 err = fileText.Read(buffer); 00146 00147 // If the error code is other than KErrNone or KErrEof, 00148 // leave this function. 00149 if ((err != KErrNone) && (err != KErrEof)) 00150 { 00151 User::Leave(err); 00152 } 00153 00154 if (KErrNone == err) 00155 { 00156 // Do something with the buffer. 00157 // In this example, we append the buffer to an array. 00158 iBuffer.ReAllocL(iBuffer.Length() + buffer.Length() + 1); 00159 iBuffer.Append(buffer); 00160 iBuffer.Append(KDelimiter); 00161 } 00162 } 00163 00164 CleanupStack::PopAndDestroy(&file); 00165 } 00166 00167 // End of File