examples/ForumNokia/Symbian_OS_Basics_Lab_Exercises_v3_1/Lab_04308.cb1/solution/src/S60ClientServLabContainer.cpp

00001 // Copyright (c) 2006 Nokia Corporation.
00002 
00003 // INCLUDE FILES
00004 #include "S60ClientServLabContainer.h"
00005 
00006 #include <eiklabel.h>  // for example label control
00007 #include <f32file.h>   // for file server usage
00008 
00009 _LIT(KClientServerLabText, "Client/Server Lab");
00010 _LIT(KFileWriterText, "File Writer");
00011 _LIT(KOutputFileSizeText, "Output file size:");
00012 _LIT(KNumBytes, "%d bytes");
00013 _LIT(KDataFilePath, "c:\\system\\apps\\S60ClientServLab\\S60ClientServLab.txt");
00014 _LIT8(KDataEntry, "A line of ASCII text\r\n");
00015 // ================= MEMBER FUNCTIONS =======================
00016 
00017 // ---------------------------------------------------------
00018 // CS60ClientServLabContainer::ConstructL(const TRect& aRect)
00019 // EPOC two phased constructor
00020 // ---------------------------------------------------------
00021 //
00022 void CS60ClientServLabContainer::ConstructL(const TRect& aRect)
00023     {
00024     CreateWindowL();
00025 
00026     iTopLabel = new (ELeave) CEikLabel;
00027     iTopLabel->SetContainerWindowL( *this );
00028     iTopLabel->SetTextL(KClientServerLabText);
00029 
00030     iBottomLabel = new (ELeave) CEikLabel;
00031     iBottomLabel->SetContainerWindowL( *this );
00032     iBottomLabel->SetTextL(KFileWriterText);
00033 
00034     SetRect(aRect);
00035     ActivateL();
00036     }
00037 
00038 // Destructor
00039 CS60ClientServLabContainer::~CS60ClientServLabContainer()
00040     {
00041     delete iTopLabel;
00042     delete iBottomLabel;
00043     }
00044 
00045 // ---------------------------------------------------------
00046 // CS60ClientServLabContainer::SizeChanged()
00047 // Called by framework when the view size is changed
00048 // ---------------------------------------------------------
00049 //
00050 void CS60ClientServLabContainer::SizeChanged()
00051     {
00052     iTopLabel->SetExtent( TPoint(0,0), TSize(Rect().Width(), Rect().Height()/2));
00053     iBottomLabel->SetExtent( TPoint(0,Rect().Height()/2), TSize(Rect().Width(),Rect().Height()/2));
00054     }
00055 
00056 // ---------------------------------------------------------
00057 // CS60ClientServLabContainer::CountComponentControls() const
00058 // ---------------------------------------------------------
00059 //
00060 TInt CS60ClientServLabContainer::CountComponentControls() const
00061     {
00062     return 2; // return nbr of controls inside this container
00063     }
00064 
00065 // ---------------------------------------------------------
00066 // CS60ClientServLabContainer::ComponentControl(TInt aIndex) const
00067 // ---------------------------------------------------------
00068 //
00069 CCoeControl* CS60ClientServLabContainer::ComponentControl(TInt aIndex) const
00070     {
00071     switch ( aIndex )
00072         {
00073         case 0:
00074             return iTopLabel;
00075         case 1:
00076             return iBottomLabel;
00077         default:
00078             return NULL;
00079         }
00080     }
00081 
00082 // ---------------------------------------------------------
00083 // CS60ClientServLabContainer::Draw(const TRect& aRect) const
00084 // ---------------------------------------------------------
00085 //
00086 void CS60ClientServLabContainer::Draw(const TRect& aRect) const
00087     {
00088     CWindowGc& gc = SystemGc();
00089     gc.SetPenStyle(CGraphicsContext::ENullPen);
00090     gc.SetBrushColor(KRgbGray);
00091     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00092     gc.DrawRect(aRect);
00093     }
00094 
00095 // ---------------------------------------------------------
00096 // CS60ClientServLabContainer::HandleControlEventL(
00097 //     CCoeControl* aControl,TCoeEvent aEventType)
00098 // ---------------------------------------------------------
00099 //
00100 void CS60ClientServLabContainer::HandleControlEventL(
00101     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00102     {
00103     }
00104     
00105 // ---------------------------------------------------------
00106 // CS60ClientServLabContainer::WriteFileL()
00107 // ---------------------------------------------------------
00108 //
00109 void CS60ClientServLabContainer::WriteFileL()
00110     {
00111     RFs fs;
00112     User::LeaveIfError(fs.Connect());
00113     CleanupClosePushL(fs);
00114 
00115     TParsePtrC parse(KDataFilePath);
00116     TPtrC pathPtr = parse.DriveAndPath();
00117 
00118     User::LeaveIfError(fs.MkDirAll(pathPtr));
00119 
00120     RFile file;
00121     TInt ret = file.Open(fs, KDataFilePath, EFileWrite | EFileStreamText);
00122 
00123     switch(ret)
00124         {         
00125                 case KErrNotFound:
00126                         User::LeaveIfError(file.Create(fs, KDataFilePath, EFileWrite | EFileStreamText));
00127                         break;
00128                         
00129                 case KErrNone:
00130                         // Do nothing
00131                         break;
00132                         
00133                 default:
00134                         User::Leave(ret);
00135                         break;
00136         }
00137     
00138         CleanupClosePushL(file);
00139     
00140     TInt offset = 0;   
00141         User::LeaveIfError(file.Seek(ESeekEnd, offset));
00142         User::LeaveIfError(file.Write(KDataEntry));
00143         
00144     TInt fileSize;
00145     file.Size(fileSize);
00146         
00147         CleanupStack::PopAndDestroy(2);
00148         
00149     TBuf<20> numBytes;
00150     numBytes.Format(KNumBytes, fileSize);
00151 
00152     iTopLabel->SetTextL(KOutputFileSizeText);
00153     iBottomLabel->SetTextL(numBytes);
00154     }
00155 // End of File  

Generated by  doxygen 1.6.2