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

00001 /*
00002  * Copyright  2008 Nokia Corporation.
00003  */
00004 
00005 #include <aknviewappui.h>
00006 #include <aknconsts.h>
00007 #include <aknnotewrappers.h>
00008 #include <msvids.h> // Folder Ids
00009 #include <smsexample.rsg>
00010 
00011 #include "SMSExampleListboxView.h"
00012 #include "SMSExampleLogView.h"
00013 #include "SMSExampleMarkableListContainer.h"
00014 #include "SmsExample.hrh"
00015 #include "SMSExampleAppUi.h"
00016 
00017 _LIT(KMessages, " messages");
00018 _LIT(KCopiedMessages, "Copied ");
00019 _LIT(KDeletedMessages, "Deleted ");
00020 _LIT(KMovedMessages, "Moved ");
00021 _LIT(KNoMessagesInFolder, "there are no messages in that source folder!");
00022 
00023 const TInt KMaxMessageCount(100);
00024 
00025 // ----------------------------------------------------------------------------
00026 // CListboxView::NewL()
00027 //
00028 // Symbian OS 2 phase constructor.
00029 // ----------------------------------------------------------------------------
00030 CListboxView* CListboxView::NewL(CSmsEngine* aEngine, CLogView* aView1)
00031     {
00032     CListboxView* self = CListboxView::NewLC(aEngine, aView1);
00033     CleanupStack::Pop(self);
00034 
00035     return self;
00036     }
00037 
00038 // ----------------------------------------------------------------------------
00039 // CListboxView::NewLC()
00040 //
00041 // Symbian OS 2 phase constructor.
00042 // ----------------------------------------------------------------------------
00043 CListboxView* CListboxView::NewLC(CSmsEngine* aEngine, CLogView* aView1)
00044     {
00045     CListboxView* self = new (ELeave) CListboxView(aEngine, aView1);
00046     CleanupStack::PushL(self);
00047     self->ConstructL();
00048     return self;
00049     }
00050 
00051 // ----------------------------------------------------------------------------
00052 // CListboxView::CListboxView(CSmsEngine* aEngine,
00053 //                                    CLogView* aView1)
00054 //
00055 // Constructor.
00056 // ----------------------------------------------------------------------------
00057 CListboxView::CListboxView(CSmsEngine* aEngine, CLogView* aView1):
00058     iEngine(aEngine), iLogView(aView1)
00059     {
00060     iListBoxContainer = 0;
00061     }
00062 
00063 // ----------------------------------------------------------------------------
00064 // CListboxView::~CListboxView()
00065 //
00066 // Destructor frees resources.
00067 // ----------------------------------------------------------------------------
00068 CListboxView::~CListboxView()
00069     {
00070     delete iListBoxContainer;
00071     iListBoxContainer = NULL;
00072 
00073     iEngine = 0;
00074     iLogView = 0;
00075     }
00076 
00077 // ----------------------------------------------------------------------------
00078 // CListboxView::ConstructL()
00079 //
00080 // Constructor.
00081 // ----------------------------------------------------------------------------
00082 void CListboxView::ConstructL()
00083     {
00084     BaseConstructL(R_LISTBOX_MULTIVIEWS_VIEW);
00085     }
00086 
00087 // ----------------------------------------------------------------------------
00088 // CListboxView::Id() const
00089 //
00090 // Return view Id.
00091 // ----------------------------------------------------------------------------
00092 TUid CListboxView::Id() const
00093     {
00094     return TUid::Uid(EListboxViewId);
00095     }
00096 
00097 // ----------------------------------------------------------------------------
00098 // CListboxView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
00099 //                                    TUid /*aCustomMessageId*/,
00100  //                                   const TDesC8& /*aCustomMessage*/)
00101 //
00102 // Activate ListBox view. Create a new Listbox and add it to stack.
00103 // Initialize ListBox with sms messages got from a specific folder.
00104 // See SetFolderID and InitializeListBoxL.
00105 // ----------------------------------------------------------------------------
00106 void CListboxView::DoActivateL(const TVwsViewId& /*aPrevViewId*/,
00107                                     TUid /*aCustomMessageId*/,
00108                                     const TDesC8& /*aCustomMessage*/)
00109     {
00110     iListBoxContainer = CMarkableListContainer::NewL(ClientRect());
00111 
00112     iListBoxContainer->SetMopParent(this);
00113 
00114     AppUi()->AddToStackL(*this, iListBoxContainer);
00115     InitializeListBoxL();
00116     }
00117 
00118 
00119 // ----------------------------------------------------------------------------
00120 // CListboxView::SetFolderID(TMsvId aFolderID)
00121 //
00122 // Set source folder id. Source means all messages in folder which id is
00123 // aFolderID
00124 // ----------------------------------------------------------------------------
00125 void CListboxView::SetFolderID(TMsvId aFolderID)
00126     {
00127     iFolderID = aFolderID;
00128     }
00129 
00130 // ----------------------------------------------------------------------------
00131 // CListboxView::InitializeListBoxL()
00132 //
00133 // Initialize ListBox with SMS messages. Load source folder messages into
00134 // ListBox. Source folder must be set before calling this.
00135 // ----------------------------------------------------------------------------
00136 void CListboxView::InitializeListBoxL()
00137     {
00138 
00139     // Source folder must be set
00140     if ( iFolderID != KMsvGlobalInBoxIndexEntryId &&
00141          iFolderID != KMsvGlobalOutBoxIndexEntryId &&
00142          iFolderID != KMsvDraftEntryId )
00143         {
00144         User::Leave(KErrArgument);
00145         }
00146 
00147     // Listbox takes ownership of the lists. Take messages bodys & addresses
00148     // and set them into ListBox.
00149     CDesCArrayFlat* arrayAddr = 0;
00150     CDesCArrayFlat* arrayMsgBody = 0;
00151     iEngine->GetFolderSMSMessageInformationL(iFolderID,arrayAddr,arrayMsgBody);
00152     iListBoxContainer->SetArraysL( arrayAddr, arrayMsgBody );
00153 
00154     RArray<TMsvId>* array = iEngine->GetMessageIds();
00155     iListBoxContainer->SetIdArray ( array );
00156 
00157     // Check if there are no messages in source folder.
00158     if ( array->Count() == 0 )
00159         {
00160         iLogView->LogEventBeginningL();
00161         iLogView->DrawTextL( KNoMessagesInFolder );
00162 
00163         RArray<TMsvId>* selected = iListBoxContainer->GetSelectedItems();
00164         selected->Reset();
00165         delete selected;
00166         selected = 0;
00167 
00168         AppUi()->ActivateLocalViewL(TUid::Uid(ELogViewId));
00169         }
00170     }
00171 
00172 // ----------------------------------------------------------------------------
00173 // CListboxView::DoDeactivate()
00174 //
00175 // Deactivate this view. Delete ListBox.
00176 // ----------------------------------------------------------------------------
00177 void CListboxView::DoDeactivate()
00178     {
00179     if (iListBoxContainer)
00180         {
00181         AppUi()->RemoveFromStack(iListBoxContainer);
00182         delete iListBoxContainer;
00183         iListBoxContainer = NULL;
00184         }
00185     }
00186 
00187 // ----------------------------------------------------------------------------
00188 // CListboxView::SetListBoxMode(TInt aMode)
00189 //
00190 // ListBox mode for ListBox. See SMSExample.hrh for list of modes.
00191 // ----------------------------------------------------------------------------
00192 void CListboxView::SetListBoxMode(TInt aMode)
00193     {
00194     iMode = aMode;
00195     }
00196 
00197 // ----------------------------------------------------------------------------
00198 // CListboxView::SetTargetFolderID(TMsvId aTargetFolderID)
00199 //
00200 // Set Target folder of ListBox action.
00201 // -----------------------------------------------------------------------------
00202 void CListboxView::SetTargetFolderID(TMsvId aTargetFolderID)
00203     {
00204     iTargetFolderID = aTargetFolderID;
00205     }
00206 
00207 // ----------------------------------------------------------------------------
00208 // CListboxView::HandleClientRectChange()
00209 //
00210 // Set ListBox rectangle.
00211 // ----------------------------------------------------------------------------
00212 void CListboxView::HandleClientRectChange()
00213     {
00214     if ( iListBoxContainer )
00215         {
00216         iListBoxContainer->SetRect( ClientRect() );
00217         }
00218     }
00219 
00220 // ----------------------------------------------------------------------------
00221 // CListboxView::HandleBasedOnModeL()
00222 //
00223 // Do action based on ListBox mode.
00224 // ----------------------------------------------------------------------------
00225 void CListboxView::HandleBasedOnModeL()
00226     {
00227     RArray<TMsvId>* array = iListBoxContainer->GetSelectedItems();
00228 
00229     if ( !array )
00230         {
00231         return;
00232         }
00233 
00234     // Copy from one folder to another
00235     if ( iMode == ESMSExampleModeCopy )
00236         {
00237 
00238         for (TInt i = 0; i < array->Count(); i++)
00239             {
00240             iEngine->CopyMessageL( (*array)[i], iTargetFolderID );
00241             }
00242 
00243         iLogView->LogEventBeginningL();
00244         TBuf<KMaxMessageCount> textBuffer;
00245         textBuffer.Append( KCopiedMessages );
00246         textBuffer.AppendNum( array->Count() );
00247         textBuffer.Append(KMessages);
00248 
00249         iLogView->DrawTextL( textBuffer );
00250         }
00251 
00252     // Delete a group of messages
00253     else if ( iMode == ESMSExampleDelete )
00254         {
00255 
00256         for (TInt i = 0; i < array->Count(); i++)
00257             {
00258             iEngine->DeleteMessageL( (*array)[i] );
00259             }
00260 
00261         iLogView->LogEventBeginningL();
00262         TBuf<KMaxMessageCount> textBuffer;
00263         textBuffer.Append( KDeletedMessages );
00264         textBuffer.AppendNum( array->Count() );
00265         textBuffer.Append(KMessages);
00266 
00267         iLogView->DrawTextL( textBuffer );
00268         }
00269 
00270     // Move a group of messages to another folder
00271     else if ( iMode == ESMSExampleMove )
00272         {
00273 
00274         for (TInt i = 0; i < array->Count(); i++)
00275             {
00276             iEngine->MoveToFolderL( (*array)[i], iTargetFolderID );
00277             }
00278 
00279         iLogView->LogEventBeginningL();
00280         TBuf<KMaxMessageCount> textBuffer;
00281         textBuffer.Append( KMovedMessages );
00282         textBuffer.AppendNum( array->Count() );
00283         textBuffer.Append(KMessages);
00284 
00285         iLogView->DrawTextL( textBuffer );
00286         }
00287 
00288     array->Reset();
00289     delete array;
00290     array = 0;
00291 
00292     AppUi()->ActivateLocalViewL(TUid::Uid(ELogViewId));
00293     iMode = 0;
00294     }
00295 
00296 // ----------------------------------------------------------------------------
00297 // CListboxView::HandleCommandL(TInt aCommand)
00298 //
00299 // Handle commands.
00300 // ----------------------------------------------------------------------------
00301 void CListboxView::HandleCommandL(TInt aCommand)
00302     {
00303     switch ( aCommand )
00304         {
00305      case EAknSoftkeyBack:
00306             {
00307             // This is responsible for destroying selected messages.
00308             RArray<TMsvId>* array = iListBoxContainer->GetSelectedItems();
00309             array->Reset();
00310             delete array;
00311             array = 0;
00312 
00313             // Move back to RichTextEditor view.
00314             AppUi()->ActivateLocalViewL(TUid::Uid(ELogViewId));
00315 
00316             break;
00317             }
00318      case ESMSExampleListBoxSelectAll:
00319             {
00320             // Select all messages.
00321             iListBoxContainer->MarkAllL(ETrue);
00322             break;
00323             }
00324      case ESMSExampleListBoxDeSelectAll:
00325             {
00326             // Deselect all messages.
00327             iListBoxContainer->MarkAllL(EFalse);
00328             break;
00329             }
00330      case ESMSExampleListBoxDone:
00331             {
00332             // Do the action according to ListBox mode.
00333 
00334             //The main actions for SMS manipulation are executed here.
00335             TRAPD(err, HandleBasedOnModeL() );
00336             if( err )
00337                 {
00338                 _LIT(KErrText, "Error in SMS handling");
00339                 CSMSExampleAppUi* appui =
00340                                   static_cast <CSMSExampleAppUi*> (AppUi());
00341                 appui->ShowMessageL( KErrText );
00342                 appui->ShowErrorL(err);
00343                 }
00344             break;
00345             }
00346         default:
00347             {
00348             AppUi()->HandleCommandL( aCommand );
00349             break;
00350             }
00351         }
00352     }
00353 
00354 void CListboxView::SizeChanged()
00355   {
00356   if( iListBoxContainer )
00357       {
00358       iListBoxContainer->SetRect( ClientRect() );
00359       }
00360   }
00361 
00362 // End of File

Generated by  doxygen 1.6.2