⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 s60clientservlabcontainer.cpp

📁 S60培训教材代码
💻 CPP
字号:
/*
* ============================================================================
*  Name     : CS60ClientServLabContainer from S60ClientServLabContainer.h
*  Part of  : S60ClientServLab
*  Created  : 15/07/2003 by Neil Binns
*  Implementation notes:
*     Initial content was generated by Series 60 AppWizard.
*  Version  :
*  Copyright: (c) 2003 Nokia.  All rights reserved.
* ============================================================================
*/

// INCLUDE FILES
#include "S60ClientServLabContainer.h"

#include <eiklabel.h>  // for example label control
#include <f32file.h>   // for file server usage

_LIT(KClientServerLabText, "Client/Server Lab");
_LIT(KFileWriterText, "File Writer");
_LIT(KOutputFileSizeText, "Output file size:");
_LIT(KNumBytes, "%d bytes");
_LIT(KDataFilePath, "c:\\system\\apps\\S60ClientServLab\\S60ClientServLab.txt");
_LIT8(KDataEntry, "A line of ASCII text\r\n");
// ================= MEMBER FUNCTIONS =======================

// ---------------------------------------------------------
// CS60ClientServLabContainer::ConstructL(const TRect& aRect)
// EPOC two phased constructor
// ---------------------------------------------------------
//
void CS60ClientServLabContainer::ConstructL(const TRect& aRect)
    {
    CreateWindowL();

    iTopLabel = new (ELeave) CEikLabel;
    iTopLabel->SetContainerWindowL( *this );
    iTopLabel->SetTextL(KClientServerLabText);

    iBottomLabel = new (ELeave) CEikLabel;
    iBottomLabel->SetContainerWindowL( *this );
    iBottomLabel->SetTextL(KFileWriterText);

    SetRect(aRect);
    ActivateL();
    }

// Destructor
CS60ClientServLabContainer::~CS60ClientServLabContainer()
    {
    delete iTopLabel;
    delete iBottomLabel;
    }

// ---------------------------------------------------------
// CS60ClientServLabContainer::SizeChanged()
// Called by framework when the view size is changed
// ---------------------------------------------------------
//
void CS60ClientServLabContainer::SizeChanged()
    {
    iTopLabel->SetExtent(TPoint(10, 50), TSize(150, 15));
    iBottomLabel->SetExtent(TPoint(10, 80), TSize(150, 15));
    }

// ---------------------------------------------------------
// CS60ClientServLabContainer::CountComponentControls() const
// ---------------------------------------------------------
//
TInt CS60ClientServLabContainer::CountComponentControls() const
    {
    return 2; // return nbr of controls inside this container
    }

// ---------------------------------------------------------
// CS60ClientServLabContainer::ComponentControl(TInt aIndex) const
// ---------------------------------------------------------
//
CCoeControl* CS60ClientServLabContainer::ComponentControl(TInt aIndex) const
    {
    switch ( aIndex )
        {
        case 0:
            return iTopLabel;
        case 1:
            return iBottomLabel;
        default:
            return NULL;
        }
    }

// ---------------------------------------------------------
// CS60ClientServLabContainer::Draw(const TRect& aRect) const
// ---------------------------------------------------------
//
void CS60ClientServLabContainer::Draw(const TRect& aRect) const
    {
    CWindowGc& gc = SystemGc();
    gc.SetPenStyle(CGraphicsContext::ENullPen);
    gc.SetBrushColor(KRgbGray);
    gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    gc.DrawRect(aRect);
    }

// ---------------------------------------------------------
// CS60ClientServLabContainer::HandleControlEventL(
//     CCoeControl* aControl,TCoeEvent aEventType)
// ---------------------------------------------------------
//
void CS60ClientServLabContainer::HandleControlEventL(
    CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
    {
    }

// ---------------------------------------------------------
// CS60ClientServLabContainer::WriteFileL()
// ---------------------------------------------------------
//
void CS60ClientServLabContainer::WriteFileL()
    {

    // STEP 5: Declare a handle of type RFs
    RFs fs;

    // STEP 6: Call Connect() on the handle and Leave if an error 
    //         is returned.        
    User::LeaveIfError(fs.Connect());
        
    // STEP 19: Put the RFs handle on the Cleanup Stack by calling the
    //          global function CleanupClosePushL(<handle>);
    CleanupClosePushL(fs);

    TParsePtrC parse(KDataFilePath);
    TPtrC pathPtr = parse.DriveAndPath();

    // STEP 7: Call MkDirAll() on the RFs handle supplying pathPtr as 
    //         the argument
    fs.MkDirAll(pathPtr);

    RFile file;
    
    // STEP 8: Call file.Open() to open the output file.
    // a)      Pass in the RFs handle as the 1st parameter
    // b)      Pass in the full file path, KDataFilePath as the 2nd parameter
    // c)      Pass in EFileWrite | EFileStreamText as the 3rd parameter
    // d)      Store the TInt return code
    TInt ret = file.Open(fs, KDataFilePath, EFileWrite | EFileStreamText);
    
    // STEP 9: Use a switch statement to test the return code
    // a)      If the return code is KErrNotFound, create the file output 
    //             file by calling:
    //             file.Create(fs, KDataFilePath, EFileWrite | EFileStreamText)
    //             Leave if an error is returned
    // b)      If the return code is KErrNone, do nothing (other than break)
    // c)      In the default case, call User::Leave(ret);
    //          
    switch (ret)
        {
        case KErrNotFound:
            User::LeaveIfError(file.Create(fs, KDataFilePath, EFileWrite | EFileStreamText));
            break;

        case KErrNone:
            // Do nothing - the file was successfully opened
            break;

        default:
            User::Leave(ret);
        }
    
    // STEP 20: Call CleanupClosePushL(file); to put the file handle on
    //          the Cleanup Stack
    CleanupClosePushL(file);

    
    TInt offset = 0;

    // STEP 10: Call file.Seek(ESeekEnd, offset) to move to the end of the file
    //          Leave if there is an error    
    User::LeaveIfError(file.Seek(ESeekEnd, offset));

    // STEP 11: Call file.Write(KDataEntry) to write a line to the file.
    //          Leave if there is an error
    User::LeaveIfError(file.Write(KDataEntry));

    // STEP 26: Comment out the Leave raised in STEP 15
    // STEP 15: Call User::Leave(KErrWrite); to simulate a write error above
    // User::Leave(KErrWrite);

    TInt fileSize;
    file.Size(fileSize);
    // STEP 21: Call CleanupStack::PopAndDestroy(2); to close the RFs and 
    //          RFile handles and remove them from the Cleanup Stack
    CleanupStack::PopAndDestroy(2); // closes file, fs

    // STEP 22: Comment out the code added in STEPS 12 and 13 as this is
    //          now redundant because of STEP 21
    // STEP 12: Call file.Close() to close the file.
    file.Close();

    // STEP 13: call Close() on the RFs handle.
    fs.Close();

    TBuf<20> numBytes;
    numBytes.Format(KNumBytes, fileSize);

    iTopLabel->SetTextL(KOutputFileSizeText);
    iBottomLabel->SetTextL(numBytes);
    }
// End of File  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -