examples/ForumNokia/SMSExample/GUI/src/SMSExampleAppui.cpp

00001 /*
00002  * Copyright � 2008 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include "SMSExampleAppui.h"
00007 #include <SMSExample.rsg>
00008 #include "SMSExample.hrh"
00009 #include "SMSExampleLogView.h"
00010 #include "SMSExampleListboxView.h"
00011 #include "SmsExamplePanics.pan"
00012 #include "SMSExampleParser.h"
00013 
00014 #include <AknQueryDialog.h>
00015 #include <msvids.h> // Folder Ids
00016 #include <txtrich.h> // CRichText
00017 #include <smut.h>
00018 #include <AknGlobalNote.h>  //avkon.lib, aknnotify.lib
00019 
00020 #ifdef __WINS__
00021 const TMsvId KObservedFolderId = KMsvDraftEntryId;
00022 #else
00023 const TMsvId KObservedFolderId =  KMsvGlobalInBoxIndexEntryId;
00024 #endif
00025 
00026 const TMsvId KInbox = KMsvGlobalInBoxIndexEntryId;
00027 const TMsvId KOutbox = KMsvGlobalOutBoxIndexEntryId;
00028 const TMsvId KDrafts = KMsvDraftEntryId;
00029 
00030 _LIT(KCouldNotSendSMS, "Message not sent");
00031 _LIT(KSentMessageToAddress, "Sent message to address: ");
00032 _LIT(KNoBodyText, "No body text");
00033 _LIT(KAutomaticDeletedMesssageText, "Deleted arrived message!");
00034 _LIT(KAutomaticDeleteActivatedText, "Automatic delete activated");
00035 _LIT(KAutomaticDeleteDeActivatedText, "Automatic delete deactivated");
00036 _LIT(KSendingMsgTxt,"Sending msg");
00037 _LIT(KErrText,"Error:");
00038 _LIT(KEngineInitializedTxt,"Engine initialized");
00039 _LIT(KMessageSentTxt,"Message sent");
00040 _LIT(KMsgReceivedTxt,"Received message");
00041 
00042 const TInt KDelayTime = 1000*3000;
00043 const TInt KErrMsgLength = 20;
00044 
00045 #define KErrMsgLength 30
00046 
00047 void CSMSExampleAppUi::ConstructL()
00048     {
00049     BaseConstructL(EAknEnableSkin);
00050 
00051     iAutomaticDelete = EFalse;
00052 
00053     iSmsEngine = CSmsEngine::NewL(*this);
00054 
00055     iLogView = CLogView::NewL();
00056     iListboxView = CListboxView::NewL(iSmsEngine, iLogView);
00057 
00058     AddViewL(iLogView); // Transfer ownership to base class
00059     AddViewL(iListboxView); // Transfer ownership to base class
00060 
00061     SetDefaultViewL(*iLogView); // Start with log view.
00062 
00063     // Message body parser
00064     iParser = CSMSExampleParser::NewL();
00065     }
00066 
00067 // Destructor frees reserved resources
00068 CSMSExampleAppUi::~CSMSExampleAppUi()
00069     {
00070     delete iParser;
00071     iParser = NULL;
00072 
00073     delete iSmsEngine;
00074     iSmsEngine = NULL;
00075     }
00076 
00077 //  This function is called by the EIKON framework just before it displays
00078 //  a menu pane. Its default implementation is empty, and by overriding it,
00079 //  the application can set the state of menu items dynamically according
00080 //  to the state of application data.
00081 void CSMSExampleAppUi::DynInitMenuPaneL(
00082     TInt /*aResourceId*/,CEikMenuPane* /*aMenuPane*/)
00083     {
00084     }
00085 
00086 // takes care of key event handling
00087 TKeyResponse CSMSExampleAppUi::HandleKeyEventL(
00088     const TKeyEvent& /*aKeyEvent*/,TEventCode /*aType*/)
00089     {
00090     return EKeyWasNotConsumed;
00091     }
00092 
00093 // Query message recipient from the user.
00094 TInt CSMSExampleAppUi::GetAddressL()
00095     {
00096     CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(iAddress,
00097         CAknQueryDialog::ENoTone);
00098 
00099     return dlg->ExecuteLD(R_MTMS_EXAMPLE_ADDRESS_QUERY);
00100     }
00101 
00102 // Query message body from the user.
00103 TInt CSMSExampleAppUi::QueryMessageL()
00104     {
00105     CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(iMessage,
00106         CAknQueryDialog::ENoTone);
00107 
00108     return dlg->ExecuteLD(R_MTMS_EXAMPLE_MESSAGE_QUERY);
00109     }
00110 
00111 // Takes care of command handling.
00112 void CSMSExampleAppUi::HandleCommandL(TInt aCommand)
00113     {
00114     switch ( aCommand )
00115         {
00116         case EAknSoftkeyExit:
00117         case EEikCmdExit:
00118             {
00119             Exit();
00120             break;
00121             }
00122         // Send a new message
00123         case ESMSExampleCmdSendMessage:
00124             {
00125             // Ask recipient from the user. Return if Cancel was pressed.
00126             if (!GetAddressL())
00127                 {
00128                 return;
00129                 }
00130 
00131             // Query message body. Return if Cancel was pressed.
00132             if (!QueryMessageL())
00133                 {
00134                 return;
00135                 }
00136 
00137             iLogView->LogEventBeginningL();
00138 
00139             iSmsEngine->SendSmsL(iAddress, iMessage);
00140 
00141             break;
00142             }
00143 
00144         //MOVE MESSAGES
00145         case ESMSExampleCmdMoveMessageFromInboxToDrafts:
00146             {
00147             QueryMessagesAndExecuteL( ESMSExampleMove, KInbox, KDrafts );
00148             break;
00149             }
00150         case ESMSExampleCmdMoveMessageFromInboxToOutbox:
00151             {
00152             QueryMessagesAndExecuteL( ESMSExampleMove, KInbox, KOutbox );
00153             break;
00154             }
00155         case ESMSExampleCmdMoveMessageFromDraftsToInbox:
00156             {
00157             QueryMessagesAndExecuteL( ESMSExampleMove, KDrafts, KInbox );
00158             break;
00159             }
00160         case ESMSExampleCmdMoveMessageFromDraftsToOutbox:
00161             {
00162             QueryMessagesAndExecuteL( ESMSExampleMove, KDrafts, KOutbox );
00163             break;
00164             }
00165         case ESMSExampleCmdMoveMessageFromOutboxToInbox:
00166             {
00167             QueryMessagesAndExecuteL( ESMSExampleMove, KOutbox, KInbox );
00168             break;
00169             }
00170         case ESMSExampleCmdMoveMessageFromOutboxToDrafts:
00171             {
00172             QueryMessagesAndExecuteL( ESMSExampleMove, KOutbox, KDrafts );
00173             break;
00174             }
00175 
00176         //COPY MESSAGES
00177         case ESMSExampleCmdCopyMessageFromDraftsToInbox:
00178             {
00179             QueryMessagesAndExecuteL( ESMSExampleModeCopy, KDrafts, KInbox );
00180             break;
00181             }
00182         case ESMSExampleCmdCopyMessageFromDraftsToOutbox:
00183             {
00184             QueryMessagesAndExecuteL( ESMSExampleModeCopy, KDrafts, KOutbox );
00185             break;
00186             }
00187         case ESMSExampleCmdCopyMessageFromInboxToOutbox:
00188             {
00189             QueryMessagesAndExecuteL( ESMSExampleModeCopy, KInbox, KOutbox );
00190             break;
00191             }
00192         case ESMSExampleCmdCopyMessageFromInboxToDrafts:
00193             {
00194             QueryMessagesAndExecuteL( ESMSExampleModeCopy, KInbox, KDrafts );
00195             break;
00196             }
00197         case ESMSExampleCmdCopyMessageFromOutboxToInbox:
00198             {
00199             QueryMessagesAndExecuteL( ESMSExampleModeCopy, KOutbox, KInbox );
00200             break;
00201             }
00202         case ESMSExampleCmdCopyMessageFromOutboxToDrafts:
00203             {
00204             QueryMessagesAndExecuteL( ESMSExampleModeCopy, KOutbox, KDrafts );
00205             break;
00206             }
00207 
00208         // DELETE MESSAGE COMMANDS. Target folder is not needed.
00209         case ESMSExampleCmdDeleteMessageFromDrafts:
00210             {
00211             QueryMessagesAndExecuteL( ESMSExampleDelete, KDrafts, 0 );
00212             break;
00213             }
00214         case ESMSExampleCmdDeleteMessageFromOutbox:
00215             {
00216             QueryMessagesAndExecuteL( ESMSExampleDelete, KOutbox, 0 );
00217             break;
00218             }
00219         case ESMSExampleCmdDeleteMessageFromInbox:
00220             {
00221             QueryMessagesAndExecuteL(ESMSExampleDelete, KInbox, 0 );
00222             break;
00223             }
00224 
00225         // SETTINGS
00226         case ESMSExampleListBoxSetAutomaticDeleteOn:
00227             {
00228             CAknQueryDialog* conf = CAknQueryDialog::NewL(
00229                                             CAknQueryDialog::EWarningTone );
00230 
00231             TInt retCode =
00232             conf->ExecuteLD(R_AUTOMATIC_DELETE_CONFIRMATION_QUERY_DIALOG);
00233 
00234             if ( retCode )
00235                 {
00236                 iLogView->LogEventBeginningL();
00237                 iLogView->DrawTextL( KAutomaticDeleteActivatedText );
00238                 iSmsEngine->SetAutomaticDeletetion(ETrue);
00239                 }
00240             break;
00241             }
00242         case ESMSExampleListBoxSetAutomaticDeleteOff:
00243             {
00244             iLogView->LogEventBeginningL();
00245             iLogView->DrawTextL( KAutomaticDeleteDeActivatedText );
00246             iSmsEngine->SetAutomaticDeletetion(EFalse);
00247             break;
00248             }
00249 
00250         default:
00251             break;
00252         }
00253     }
00254 
00255 // Set ListBox mode and set folders. Operation is executed when the
00256 // ActivateLocalView activates ListBox.
00257 void CSMSExampleAppUi::QueryMessagesAndExecuteL(TInt aMode,
00258                                                TMsvId aOriginFolderID,
00259                                                TMsvId aDestinationFolderID)
00260     {
00261     iListboxView->SetListBoxMode( aMode );
00262     iListboxView->SetFolderID( aOriginFolderID );
00263     iListboxView->SetTargetFolderID( aDestinationFolderID );
00264 
00265     // Change to another view, function takes id of the view as a parameter
00266     // (ActivateLocalViewL  (  TUid  aViewId   ) ).
00267     // enum TMultiViewsViewNumber in SMSExample.hrh
00268     //defines these ids.
00269     ActivateLocalViewL(TUid::Uid(EListboxViewId));
00270     }
00271 
00272 
00273 void CSMSExampleAppUi::ShowErrorL(TInt aErrorCode) const
00274     {
00275     _LIT(KErrMsg,"Error occured:");
00276     TBuf<KErrMsgLength> errorMsg (KErrMsg);
00277     errorMsg.AppendNum(aErrorCode);
00278     ShowMessageL(errorMsg);
00279     }
00280 
00281 void CSMSExampleAppUi::ShowMessageL(const TDesC& aMsg) const
00282     {
00283     CAknGlobalNote* note = CAknGlobalNote::NewLC();
00284     note->ShowNoteL(EAknGlobalConfirmationNote,  aMsg);
00285     CleanupStack::PopAndDestroy(note);
00286     }
00287 
00288 void CSMSExampleAppUi::HandleResourceChangeL(TInt aType)
00289     {
00290     CAknAppUi::HandleResourceChangeL(aType); //call to upper class
00291 
00292     // ADDED FOR SCALABLE UI SUPPORT
00293     // *****************************
00294     if ( aType == KEikDynamicLayoutVariantSwitch )
00295         {
00296         //do re-layout
00297         if( iLogView )
00298             iLogView->SizeChanged();
00299         if( iListboxView )
00300             iListboxView->SizeChanged();
00301         }
00302     //Must not call this if the components are on the control stack
00303     //iListboxView->HandleResourceChangeL(aType);
00304     //iLogView->HandleResourceChangeL(aType);
00305     }
00306 
00307 void CSMSExampleAppUi::SendingMessage()
00308     {
00309     TRAPD(err,iLogView->DrawTextL(KSendingMsgTxt));
00310     }
00311 
00312 void CSMSExampleAppUi::MessageSent()
00313     {
00314     TRAPD(err,iLogView->DrawTextL( KMessageSentTxt ) );
00315     }
00316 
00317 void CSMSExampleAppUi::MessageReceived(const TDesC& aAddress, const TDesC& aMessage)
00318     {
00319     TRAPD(err, HandleReceivedMessageL(aAddress, aMessage) );
00320     if( err )
00321         {
00322         TRAP(err, ShowErrorL(err) );
00323         }
00324     }
00325 
00326 void CSMSExampleAppUi::SmsEngineInitialized()
00327     {
00328     TRAPD(err,iLogView->DrawTextL(KEngineInitializedTxt));
00329     }
00330 
00331 void CSMSExampleAppUi::SmsEngineError(TInt aErrorCode)
00332     {
00333     //if there was some error sending the SMS
00334     //iLogView->DrawTextL( KCouldNotSendSMS );
00335     TBuf<KErrMsgLength> err = KErrText();
00336     err.AppendNum(aErrorCode);
00337     TRAPD(error,iLogView->DrawTextL(err));
00338     }
00339 
00340 //only if automatic deletion is on
00341 void CSMSExampleAppUi::MessageDeleted()
00342     {
00343     TRAPD(err,iLogView->LogEventBeginningL());
00344     TRAP(err,iLogView->DrawTextL( KAutomaticDeletedMesssageText ) );
00345     }
00346 
00347 void CSMSExampleAppUi::HandleReceivedMessageL(const TDesC& aAddress, const TDesC& aMessage)
00348     {
00349     if (aMessage.Length() > 0)
00350         {
00351         iLogView->DrawTextL( KMsgReceivedTxt );
00352         iLogView->DrawTextL( aAddress );
00353 
00354         TPtrC ptr = aMessage.Ptr();
00355         TBuf<KSmsMessageLength> number;
00356 
00357         // Try to find a number that has 10 or more digits
00358         // from message body and underline the found number.
00359         if ( iParser->ParseMessage( ptr, number ))
00360             {
00361             // Search number position in the message body
00362             TInt pos = aMessage.Find( number );
00363 
00364             iLogView->DrawTextWithoutCarriageL(
00365                                       aMessage.Left(pos)
00366                                       );
00367 
00368             iLogView->DrawUnderlinedTextL( aMessage.Mid(pos, number.Length()) );
00369 
00370             iLogView->DrawTextWithoutCarriageL(
00371                       aMessage.Right( aMessage.Length() - pos - number.Length() ));
00372             }
00373         else
00374             {
00375             iLogView->DrawTextL( aMessage );
00376             }
00377         }
00378     else
00379         {
00380         iLogView->DrawTextL( KNoBodyText );
00381         }
00382     }
00383 
00384 // End of File

Generated by  doxygen 1.6.2