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

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

Generated by  doxygen 1.6.2