# examples/Stdlib/GUIApp/GUIApp.cpp

    00001 // Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
    00002 // All rights reserved.
    00003 // This component and the accompanying materials are made available
    00004 // under the terms of "Eclipse Public License v1.0"
    00005 // which accompanies this distribution, and is available
    00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    00007 //
    00008 // Initial Contributors:
    00009 // Nokia Corporation - initial contribution.
    00010 //
    00011 // Contributors:
    00012 //
    00013 // Description:
    00014 // OVERVIEW
    00015 // This is a GUI application to use the checksum engine.
    00016 // DIALOGS
    00017 // The two dialogs which this application defines:
    00018 // a. Allows the user to select a file for which a checksum is calculated
    00019 // b. Displays the list of all checksums calculated
    00020 //
    00021
    00022 #include "GUIApp.h"
    00023 #include <stdlib.h>
    00024 #include <eikstart.h>
    00025
    00026 CExampleChecksumDialog::CExampleChecksumDialog(TDes* aFileName,TInt aTitleId)
    00027         : iFileName(aFileName),iTitleId(aTitleId)
    00028         {}
    00029
    00030 void CExampleChecksumDialog::PreLayoutDynInitL()
    00031         {
    00032         CEikFileNameSelector* fileNameSelector = (CEikFileNameSelector*)
    00033                 (Control(EEikCidFileNameSel));
    00034     CEikFolderNameSelector* folderNameSelector = (CEikFolderNameSelector*)
    00035                 (Control(EEikCidFolderNameSel));
    00036     CEikDriveNameSelector* driveNameSelector = (CEikDriveNameSelector*)
    00037                 (Control(EEikCidDriveNameSel));
    00038         folderNameSelector->SetFileSelectionObserver(fileNameSelector);
    00039         driveNameSelector->SetFileSelectionObserver(folderNameSelector);
    00040     User::LeaveIfError(EikFileUtils::Parse(*iFileName));
    00041         TParsePtr parse(*iFileName);
    00042         driveNameSelector->SetFullNameL(parse.FullName());
    00043     if (iTitleId)
    00044         SetTitleL(iTitleId);
    00045         }
    00046
    00047 TBool CExampleChecksumDialog::OkToExitL(TInt /*aKeycode*/)
    00048         {
    00049         _LIT(KSpace," ");
    00050
    00051         CEikFileNameSelector* fileNameSelector = (CEikFileNameSelector*)
    00052                 (Control(EEikCidFileNameSel));
    00053         fileNameSelector->ValidateStateL();
    00054         // get the selected filename
    00055         *iFileName = fileNameSelector->FullName();
    00056         // Calculate the checksum
    00057         TUint32 checksum, filesize;
    00058         const TUint16* fn=iFileName->PtrZ();
    00059         int fd = wopen((const wchar_t*)fn, O_RDONLY, 0); // Open file for reading
    00060         if (fd < 0)
    00061                 // Can't open file
    00062                 {
    00063                 iEikonEnv->InfoMsg(R_EXAMPLE_TEXT_ERROR,iFileName);
    00064                 return EFalse;
    00065                 }
    00066         TInt err=crc(fd, &checksum, &filesize); // (filesize not used)
    00067         if (err==0)
    00068                 {
    00069                 // display the checksum in an infomessage
    00070                 iEikonEnv->InfoMsg(R_EXAMPLE_TEXT_CKSUM,checksum);
    00071                 // Append the filename and checksum to the container's descriptor array
    00072                 TDes* fileNameAndCksum=iFileName;
    00073                 // Append a space to separate filename from checksum
    00074                 fileNameAndCksum->Append(KSpace);
    00075                 // Convert the checksum to text then append to the descriptor.
    00076                 fileNameAndCksum->AppendNum(checksum,EDecimal);
    00077                 }
    00078         else iEikonEnv->InfoMsg(R_EXAMPLE_TEXT_CKSUM_ERROR);
    00079                         // can't calculate checksum
    00080         close(fd); // close file
    00081         return ETrue;
    00082         }
    00083
    00084 CExampleListBoxDialog::CExampleListBoxDialog(CDesCArrayFlat* aCksumArray)
    00085                 : iCksumArray(aCksumArray)
    00086         {
    00087         }
    00088
    00089 void CExampleListBoxDialog::PreLayoutDynInitL()
    00090     {
    00091         CEikTextListBox* listbox=(CEikTextListBox*)Control(EExampleChecksumListBox);
    00092         CDesCArray* listboxArray=((CDesCArray*)listbox->Model()->ItemTextArray());
    00093         if (iCksumArray!=NULL)
    00094                 {
    00095                 // append contents of the model's checksum array to listbox for display
    00096                 for (TInt i=0; i<iCksumArray->Count(); i++)
    00097                         listboxArray->AppendL(iCksumArray->operator[](i));
    00098                 }
    00099         CEikScrollBarFrame* scrollbarFrame = listbox->CreateScrollBarFrameL();
    00100         // set horizontal and vertical scroll bars to appear when required
    00101         scrollbarFrame->SetScrollBarVisibilityL(CEikScrollBarFrame::EAuto,
    00102                 CEikScrollBarFrame::EAuto);
    00103         }
    00104
    00105 void CExampleListBoxDialog::SetSizeAndPositionL(const TSize& aSize)
    00106         {
    00107         // Resize the dialog - keep aSize's height, double its width
    00108         TSize dialogSize(((aSize.iWidth)*2), aSize.iHeight);
    00109         SetCornerAndSize(EHCenterVCenter,dialogSize);
    00110         }
    00111
    00112 TBool CExampleListBoxDialog::OkToExitL(TInt /*aKeycode*/)
    00113     {
    00114     return(ETrue);
    00115     }
    00116
    00117
    00119 //
    00120 // -----> CExampleContainer (definition)
    00121 //
    00123 CExampleContainer::CExampleContainer()
    00124         {}
    00125
    00126
    00127 CExampleContainer::~CExampleContainer()
    00128         {
    00129         delete iChecksumArray;
    00130         }
    00131
    00132 void CExampleContainer::ConstructL(const TRect& aRect)
    00133         {
    00134           // Create the window
    00135         CreateWindowL();
    00136
    00137           // granularity 3, ie space for 3 elements added when buffer is reallocated
    00138         iChecksumArray = new (ELeave) CDesCArrayFlat(3);
    00139
    00140           // Set the bounding rectangle of this control (this will result in
    00141           // a call to the SizeChangedL() member function
    00142         SetRect(aRect);
    00143
    00144           // Prepares this control and ALL of its contained controls for drawing
    00145         ActivateL();
    00146         }
    00147
    00148
    00149 void CExampleContainer::Draw(const TRect&) const
    00150         {
    00151         // Draw the container control
    00152         CWindowGc& gc=SystemGc();
    00153         gc.SetPenStyle(CGraphicsContext::ENullPen);
    00154         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
    00155         gc.SetBrushColor(KRgbWhite);
    00156         gc.DrawRect(Rect());
    00157         }
    00158
    00159
    00160 void CExampleContainer::AppendChecksumL(const TDesC& aFilename)
    00161         {
    00162         // Check that the filename and cksum don't already exist in the array
    00163         // If already there, do nothing
    00164         TInt pos; // not interested in value
    00165         if (iChecksumArray->Find(aFilename,pos,ECmpNormal))
    00166                 {
    00167                 TRAPD(error,iChecksumArray->AppendL(aFilename));
    00168                 if (error)
    00169                         {
    00170                         iChecksumArray->Reset();
    00171                         delete iChecksumArray;
    00172                         iChecksumArray=0;
    00173                         User::Leave(error);
    00174                         }
    00175                 }
    00176         }
    00177
    00178 CDesCArrayFlat* CExampleContainer::GetChecksumArray() const
    00179         {
    00180         return iChecksumArray;
    00181         }
    00182
    00183 void CExampleContainer::HandleControlEventL(CCoeControl* /*aControl*/,
    00184                                                                                 TCoeEvent /*aEventType*/
    00185                                                                             )
    00186         {
    00187         }
    00188
    00189
    00191 //
    00192 // -----> CExampleAppUi (implementation)
    00193 //
    00195 void CExampleAppUi::ConstructL()
    00196         {
    00197         // Disable control environment's automatic resource checking
    00198         // If we don't do this when using stdlib, we get a CONE 36 panic
    00199         iCoeEnv->DisableExitChecks(ETrue);
    00200
    00201         // Allow base class (CEikAppUi) to perform necessary construction
    00202         BaseConstructL();
    00203
    00204           // Construct the container control which forms the main view
    00205           // for this application. Construction requires the rectangle
    00206           // available.
    00207         iContainer = new(ELeave) CExampleContainer;
    00208         iContainer->ConstructL(ClientRect());
    00209
    00210           // The container is added to the control stack (for key event
    00211           // handling).
    00212         AddToStackL(iContainer);
    00213         }
    00214
    00215
    00216 CExampleAppUi::~CExampleAppUi()
    00217         {
    00218         RemoveFromStack(iContainer);
    00219           // Delete the container (control)
    00220         delete iContainer;
    00221         }
    00222
    00223 void CExampleAppUi::HandleCommandL(TInt aCommand)
    00224         {
    00225           // Handle the command generated by:
    00226           //   1. menu item selection
    00227           //   2. short-cut key press
    00228           //   3. tool bar item selection
    00229         switch (aCommand)
    00230                 {
    00231         case EExampleCmdChecksum:
    00232                 OnCmdChecksumL();
    00233                 break;
    00234         case EExampleCmdViewChecksums:
    00235                 OnCmdViewChecksumsL();
    00236                 break;
    00237                 // EXIT comand
    00238         case EEikCmdExit:
    00239                 OnCmdExit();
    00240                 break;
    00241         default :
    00242                 break;
    00243                 }
    00244         }
    00245
    00246 void CExampleAppUi::OnCmdChecksumL()
    00247         {
    00248         // Ensure buffer is long enough to hold the max length filename
    00249         // and the max length checksum
    00250
    00251         TDriveUnit sysDrive (RFs::GetSystemDrive());
    00252         TDriveName sysDriveName (sysDrive.Name());
    00253         _LIT(KM,"\\Documents\\");
    00254         TBuf<KMaxFileName+KMaxChecksumLength> fileName(sysDriveName);
    00255         fileName.Append(KM);
    00256
    00257         CEikDialog* dialog = new (ELeave) CExampleChecksumDialog(&fileName);
    00258         if (dialog->ExecuteLD(R_EXAMPLE_CHECKSUM_DIALOG))
    00259                 {
    00260                 // A checksum was calculated, so append the descriptor containing it and
    00261                 // the filename to the container's array
    00262                 iContainer->AppendChecksumL(fileName);
    00263                 }
    00264         }
    00265
    00266 void CExampleAppUi::OnCmdViewChecksumsL()
    00267         {
    00268     CEikDialog* dialog = new(ELeave) CExampleListBoxDialog
    00269                 (iContainer->GetChecksumArray());
    00270     dialog->ExecuteLD(R_EXAMPLE_CHECKSUM_INFO_DIALOG);
    00271         }
    00272
    00273 void CExampleAppUi::OnCmdExit()
    00274         {
    00275         CBaActiveScheduler::Exit();
    00276         }
    00277
    00278 void CExampleAppUi::DrawViewL()
    00279         {
    00280         iContainer->SetRect(ClientRect());
    00281         iContainer->DrawNow();
    00282         }
    00283
    00285 //
    00286 // -----> CExampleDocument (implementation)
    00287 //
    00289 CExampleDocument::CExampleDocument(CEikApplication& aApp)
    00290          : CEikDocument(aApp)
    00291         {}
    00292
    00293 CExampleDocument::~CExampleDocument()
    00294         {
    00295         // release all of the STDLIB resources associated with this thread
    00296         // Satisfies the CONE policy that the heap should be balanced across
    00297         // the lifetime of a CCoeEnv.
    00298         CloseSTDLIB();
    00299         }
    00300
    00301 CExampleDocument* CExampleDocument::NewL(CEikApplication& aApp)
    00302         {
    00303         CExampleDocument* self=new(ELeave) CExampleDocument(aApp);
    00304         return self;
    00305         }
    00306
    00307
    00308 CEikAppUi* CExampleDocument::CreateAppUiL()
    00309         {
    00310     return(new(ELeave) CExampleAppUi);
    00311         }
    00312
    00313
    00315 //
    00316 // -----> CExampleApplication (implementation)
    00317 //
    00319 TUid CExampleApplication::AppDllUid() const
    00320         {
    00321         return(KUidCksumApp);
    00322         }
    00323
    00324
    00325 CApaDocument* CExampleApplication::CreateDocumentL()
    00326         {
    00327         return CExampleDocument::NewL(*this);
    00328         }
    00329
    00330 //
    00331 // EXPORTed functions
    00332 //
    00333
    00334 EXPORT_C CApaApplication* NewApplication()
    00335         {
    00336         return(new CExampleApplication);
    00337         }
    00338
    00339
    00340 GLDEF_C TInt E32Main()
    00341         {
    00342         return EikStart::RunApplication(NewApplication);
    00343         }
    00344