Nokia N97 SDK
Example Applications Guide

AddressBookAppUi.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : AddressBookAppUi.cpp
00004 *  Part of     : AddressBook
00005 *  Interface   : 
00006 *  Description : Application UI class, CAddressBookAppUi - 
00007 *                                responsible for handling user commands
00008 *  Version     : 
00009 *
00010 *  Copyright (c) 2002-2006 Nokia Corporation.
00011 *  This material, including documentation and any related 
00012 *  computer programs, is protected by copyright controlled by 
00013 *  Nokia Corporation.
00014 * ==============================================================================
00015 */
00016 
00017 // INCLUDES
00018 #include "AddressBookAppUi.h"
00019 #include "AddressBookView.h" 
00020 #include <AddressBook.rsg>
00021 #include "AddressBook.hrh"
00022 #include "AddressBookEngine.h"
00023 #include "AddressBookContact.h"
00024 
00025 #include <SenXmlUtils.h>
00026 
00027 #include <cpbkfieldinfo.h> 
00028 #include <cpbkfieldsinfo.h>
00029 
00030 #include <avkon.hrh>
00031 #include <eikmenup.h>
00032 #include <aknnotewrappers.h> 
00033 #include <aknquerydialog.h> 
00034 
00035 // ================= MEMBER FUNCTIONS =======================
00036 
00037 // ----------------------------------------------------------------------------
00038 // CAddressBookAppUi::ConstructL()
00039 // ----------------------------------------------------------------------------
00040 //
00041 void CAddressBookAppUi::ConstructL()
00042     {
00043     BaseConstructL(CAknAppUi::EAknEnableSkin);
00044         iWaitNote = CAknWaitNoteWrapper::NewL();
00045         iReady = EFalse;
00046         iFirstTime = ETrue;
00047         iConnecting = EFalse;
00048         iAbout = EFalse;
00049 
00050         iEngine = CAddressBookEngine::NewL(*this);
00051     iAppContainer = new (ELeave) CAddressBookView(*iEngine);
00052     iAppContainer->SetMopParent( this );
00053     iAppContainer->ConstructL( ClientRect() );
00054     AddToStackL( iAppContainer );
00055     iContactEngine = CPbkContactEngine::NewL(NULL);
00056     }
00057 
00058 // ----------------------------------------------------------------------------
00059 // CAddressBookAppUi::~CAddressBookAppUi()
00060 // Destructor
00061 // Frees reserved resources
00062 // ----------------------------------------------------------------------------
00063 //
00064 CAddressBookAppUi::~CAddressBookAppUi()
00065     {
00066     if (iAppContainer)
00067         {
00068         RemoveFromStack( iAppContainer );
00069         delete iAppContainer;
00070         }
00071         delete iEngine;
00072         delete iContactEngine;
00073         delete iWaitNote;
00074    }
00075 
00076 // ----------------------------------------------------------------------------
00077 // CAddressBookAppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
00078 // This function is called by the EIKON framework just before it displays
00079 // a menu pane. Sets the state of menu items dynamically according
00080 // to the state of application data.
00081 // ----------------------------------------------------------------------------
00082 //
00083 void CAddressBookAppUi::DynInitMenuPaneL(
00084     TInt aResourceId, CEikMenuPane* aMenuPane)
00085     {
00086         if (aResourceId == R_ADDRESSBOOK_MENU)
00087                 {
00088                 TBool connected = iEngine->Connected();
00089                 TBool isContact(EFalse);
00090                 if ((iAppContainer->CurrentContact()) && (!iAbout))
00091                         isContact = ETrue;
00092 
00093                 aMenuPane->SetItemDimmed(EAddressBookCmdAppConnect, connected);
00094                 aMenuPane->SetItemDimmed(EAddressBookCmdAppSearch, !connected);
00095                 aMenuPane->SetItemDimmed(EAddressBookCmdAppAdd, !isContact);
00096                 aMenuPane->SetItemDimmed(EAddressBookCmdAppConnectAbout, iAbout);
00097                 }
00098         }
00099 
00100 // ----------------------------------------------------------------------------
00101 // CAddressBookAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode) 
00102 // takes care of key event handling
00103 // ----------------------------------------------------------------------------
00104 //
00105 TKeyResponse CAddressBookAppUi::HandleKeyEventL(
00106     const TKeyEvent&,TEventCode)
00107     {
00108         return EKeyWasNotConsumed;
00109     }
00110 
00111 // ----------------------------------------------------------------------------
00112 // CAddressBookAppUi::HandleCommandL(TInt aCommand)
00113 // takes care of command handling
00114 // ----------------------------------------------------------------------------
00115 //
00116 void CAddressBookAppUi::HandleCommandL(TInt aCommand)
00117     {
00118     switch ( aCommand )
00119         {
00120         case EAknSoftkeyBack:
00121                         iAppContainer->ChangeCbaL(R_AVKON_SOFTKEYS_OPTIONS_EXIT, EDrawNow);
00122                         iAppContainer->CloseItem();
00123                         iAbout = iAppContainer->ShowAbout(EFalse);
00124                         break;
00125         case EEikCmdExit:
00126                 case EAknSoftkeyExit:
00127             {
00128             Exit();
00129             break;
00130             }
00131                 case EAddressBookCmdAppConnect:
00132                         {
00133                         iReady = EFalse;
00134                         iConnecting = EFalse;
00135                 
00136                         // ---------------------------------------------------------------------
00137                         // ExecuteL(R_CONNECTING_NOTE, *this, ETrue) runs the active scheduler, 
00138                         // that calls CAknWaitNoteWrapper::RunL(),it checks 
00139                         //       1. if is process already done - then calls function IsProcessDone() 
00140                         //                  (it is overridden from interface MAknBackgroundProcess to AddressBookAppUi)
00141                         //       2. if process is not finished yet then RunL() calls StepL() that does one step of processing
00142                         //                      (it is overridden from interface MAknBackgroundProcess to AddressBookAppUi)
00143                         //                      This step establishes connection.
00144                         // After establishing of connection the active scheduler calls 
00145                         // function DialogDismissedL(TInt aButtonId) which is overriden 
00146                         // in AddressBookAppUi from interface MAknBackgroundProcess 
00147                         // ---------------------------------------------------------------------
00148                         //
00149                         iWaitNote->ExecuteL(R_CONNECTING_NOTE, *this, ETrue);
00150                         iAbout = iAppContainer->ShowAbout(EFalse);
00151                         break;
00152                         }
00153         case EAddressBookCmdAppSearch:
00154             {
00155             if (iFirstTime)
00156                                 {
00157                                 _LIT(KInfoLine, "How to Search? Use first name, last name or phone number as criteria for search.");
00158                                 CAknInformationNote* note = new (ELeave) CAknInformationNote(ETrue);
00159                                 note->ExecuteLD(KInfoLine);
00160                                 }
00161                         TBuf<50> searchFor;
00162                         CAknTextQueryDialog* dlg = new(ELeave)CAknTextQueryDialog(searchFor, CAknQueryDialog::ENoTone );
00163                         dlg->SetPredictiveTextInputPermitted(ETrue);
00164                         if (dlg->ExecuteLD( R_MY_DATA_QUERY_DIALOG ) )
00165                                 {
00166                                 iFirstTime = EFalse;
00167                                 iReady = EFalse;
00168                                 iEngine->SendSearchL(searchFor);
00169                                 
00170                                 // ---------------------------------------------------------------------
00171                                 // ExecuteL(R_SEARCHING_NOTE, *this) runs the active scheduler, 
00172                                 // that calls CSenServiceConnectionImpl::RunL() and it calls 
00173                                 // function HandleMessageL(const TDesC8& aMessage)
00174                                 // (it is overridden from interface MSenServiceConsumer to AddressBookAppUi)
00175                                 // for receiving messages from the SendL()(CSenServiceConnection)function
00176                                 // call
00177                                 // ---------------------------------------------------------------------
00178                                 //
00179                                 iWaitNote->ExecuteL(R_SEARCHING_NOTE, *this);
00180                                 iAppContainer->ChangeCbaL(R_AVKON_SOFTKEYS_OPTIONS_EXIT, EDrawNow);
00181                                 iAbout = iAppContainer->ShowAbout(EFalse);
00182                                 }
00183             break;
00184             }
00185                 case EAddressBookCmdAppConnectAbout:
00186                         iAbout = iAppContainer->ShowAbout();
00187                         iAppContainer->ChangeCbaL(R_AVKON_SOFTKEYS_OPTIONS_BACK, EDrawNow);
00188                         break;
00189                 case EAddressBookCmdAppAdd:
00190                         {
00191                         CAddressBookContact* pContact = iAppContainer->CurrentContact();
00192                         if (pContact)
00193                                 {
00194                                 CPbkContactItem* newItem = 
00195                                                                 iContactEngine->CreateEmptyContactL();
00196                                 CleanupStack::PushL(newItem);
00197                                 // Update personal information
00198                                 UpdateFieldL( newItem, EPbkFieldIdPhoneNumberMobile, 
00199                                         pContact->MobilePhoneL());
00200                                 UpdateFieldL( newItem, EPbkFieldIdPhoneNumberGeneral, 
00201                                         pContact->PhoneL());
00202                                 UpdateFieldL( newItem, EPbkFieldIdFirstName, 
00203                                         pContact->FirstName());
00204                                 UpdateFieldL( newItem, EPbkFieldIdLastName, 
00205                                         pContact->LastName());
00206                                 UpdateFieldL( newItem, EPbkFieldIdFaxNumber, 
00207                                         pContact->FaxL());
00208                                 UpdateFieldL( newItem, EPbkFieldIdEmailAddress, 
00209                                         pContact->Email());
00210                                 UpdateFieldL( newItem, EPbkFieldIdJobTitle, 
00211                                         pContact->Title());
00212                                 UpdateFieldL( newItem, EPbkFieldIdCompanyName, 
00213                                         pContact->Organization());
00214                                 UpdateFieldL( newItem, EPbkFieldIdPostalAddress, 
00215                                         pContact->Street());
00216 
00217                                 // add the new contact to the phone book. 
00218                                 iContactEngine->AddNewContactL(*newItem);
00219                                 CleanupStack::PopAndDestroy(newItem);
00220                                 }
00221                         }
00222                         break;
00223         default:
00224             break;
00225         }
00226   }
00227 
00228 // ----------------------------------------------------------------------------
00229 // CAddressBookAppUi::DialogDismissedL(TInt aButtonId)
00230 // It is overridden function from interface MAknBackgroundProcess
00231 // and is called by the active scheduler to dismiss the dialog
00232 // ----------------------------------------------------------------------------
00233 //
00234 void CAddressBookAppUi::DialogDismissedL(TInt aButtonId)
00235         {
00236         if (aButtonId == EAknSoftkeyCancel)
00237                 {
00238                 iEngine->Cancel();
00239                 }
00240         }
00241 
00242 // ----------------------------------------------------------------------------
00243 // CAddressBookAppUi::IsProcessDone() const
00244 // It is overridden function from interface MAknBackgroundProcess
00245 // and is called by the active scheduler to check if is process already done 
00246 // ----------------------------------------------------------------------------
00247 //
00248 TBool CAddressBookAppUi::IsProcessDone() const
00249         {
00250         return iReady;
00251         }
00252 
00253 // ----------------------------------------------------------------------------
00254 // CAddressBookAppUi::ProcessFinished() 
00255 // It is overridden function from interface MAknBackgroundProcess
00256 // and is called by the active scheduler to finish the process 
00257 // ----------------------------------------------------------------------------
00258 //
00259 void CAddressBookAppUi::ProcessFinished()
00260         {
00261         }
00262 
00263 // ----------------------------------------------------------------------------
00264 // CAddressBookAppUi::StepL() 
00265 // It is overridden function from interface MAknBackgroundProcess
00266 // and is called by the active scheduler to do one step of processing
00267 // Purpose this function in AddressBook to run AddressBookEngine::ConnectL() 
00268 // if connection is not established yet
00269 // ----------------------------------------------------------------------------
00270 //
00271 void CAddressBookAppUi::StepL()
00272         {
00273         if (!iConnecting)
00274                 {
00275                 iConnecting = ETrue;    // do this only once
00276                 iEngine->ConnectL();
00277                 }
00278         }
00279 
00280 // ----------------------------------------------------------------------------
00281 // CAddressBookAppUi::ErrorL(TInt aCode)
00282 // It is overridden function from interface MAddressBookEngineObserver
00283 // Checks errors that can happen
00284 // ----------------------------------------------------------------------------
00285 //
00286 void CAddressBookAppUi::ErrorL(TInt aCode)
00287         {
00288         _LIT(KNotReady, "Connection is not ready.");
00289         _LIT(KTooManyMatches, "Too many search results, please refine your search.");
00290         _LIT(KSearchFailed, "Internal error, please try again.");
00291 
00292         iReady = ETrue;
00293         CAknErrorNote* note = new (ELeave) CAknErrorNote(EFalse);
00294         TBuf<70> errorMessage;
00295         switch(aCode) 
00296                 {
00297                 case EConnectionNotReady:
00298                         errorMessage = KNotReady;
00299                         break;
00300                 case ETooManyMatches:
00301                         errorMessage = KTooManyMatches;
00302                         break;
00303                 case ESearchFailed:
00304                         errorMessage = KSearchFailed;
00305                         break;
00306                 default:
00307                         break;
00308                 }
00309         note->ExecuteLD(errorMessage);
00310         }
00311 
00312 // ----------------------------------------------------------------------------
00313 // CAddressBookAppUi::ConnectionReady()
00314 // It is overridden function from interface MAddressBookEngineObserver
00315 // It is called by the CAddressBookEngine::SetStatus(const TInt aStatus)
00316 // to set conection's status as ready
00317 // ----------------------------------------------------------------------------
00318 //
00319 void CAddressBookAppUi::ConnectionReady()
00320         {
00321         iReady = ETrue;
00322         }
00323 
00324 // ----------------------------------------------------------------------------
00325 // CAddressBookAppUi::UpdateFieldL (CPbkContactItem* modifiableItem,
00326 //                                         TPbkFieldId fieldId,
00327 //                                         const TDesC8& newValue)
00328 // Updates field of CPbkContactItem when user select the point of menu "Add to contacts". 
00329 // The change doesn't reflect to database.
00330 // ----------------------------------------------------------------------------
00331 //
00332 void CAddressBookAppUi::UpdateFieldL(CPbkContactItem* modifiableItem,
00333                                       TPbkFieldId fieldId,
00334                                       const TDesC8& newValue)
00335         {
00336     TPbkContactItemField* Field =
00337         modifiableItem->FindField(fieldId);
00338     if( Field )
00339                 {
00340                 HBufC* pValAsUnicode = SenXmlUtils::ToUnicodeLC(newValue);
00341         if(pValAsUnicode)
00342                         {
00343                         Field->TextStorage()->SetTextL(*pValAsUnicode);
00344                         }
00345                 CleanupStack::PopAndDestroy(pValAsUnicode); 
00346                 }
00347         }
00348         
00349 // ----------------------------------------------------------------------------
00350 // CAddressBookAppUi::SearchFinishedL(RPointerArray<CAddressBookContact>& aContactArray) 
00351 // It is overridden function from MAddressBookEngineObserver interface
00352 // Updates bufer containing the contact information and sets status as READY
00353 // ----------------------------------------------------------------------------
00354 //      
00355 void CAddressBookAppUi::SearchFinishedL(RPointerArray<CAddressBookContact>& aContactArray)
00356         {
00357         iAppContainer->UpdateL(aContactArray);
00358         iReady = ETrue;
00359         }
00360 
00361 // End of File  
00362 

© Nokia 2009

Back to top