emailuis/emailui/src/freestyleemailuimailboxdeleter.cpp
changeset 0 8466d47a6819
child 1 12c456ceeff2
equal deleted inserted replaced
-1:000000000000 0:8466d47a6819
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Mailbox Deleter
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // SYSTEM INCLUDE FILES
       
    20 #include <StringLoader.h>
       
    21 #include <AknsUtils.h>
       
    22 #include <AknsConstants.h>
       
    23 #include <avkon.mbg>
       
    24 #include <FreestyleEmailUi.rsg>
       
    25 #include <freestyleemailui.mbg>
       
    26 #include <aknnotewrappers.h>
       
    27 #include <AknIconUtils.h>  
       
    28 
       
    29 // INTERNAL INCLUDE FILES
       
    30 #include "emailtrace.h"
       
    31 #include "freestyleemailuimailboxdeleter.h"
       
    32 #include "FreestyleEmailUiUtilities.h"
       
    33 #include "CFSMailBox.h"
       
    34 #include "FreestyleEmailUiAppui.h"
       
    35 #include "CFSMailClient.h"
       
    36 
       
    37 // CONSTANT VALUES
       
    38 _LIT( KTabCharacter, "\t" ); 
       
    39 
       
    40 
       
    41 CFSEmailUiMailboxDeleter* CFSEmailUiMailboxDeleter::NewL( CFSMailClient& aMailClient, 
       
    42                                                           MFSEmailUiMailboxDeleteObserver& aObserver  )
       
    43     {
       
    44     FUNC_LOG;
       
    45     CFSEmailUiMailboxDeleter* self = CFSEmailUiMailboxDeleter::NewLC( aMailClient, aObserver );
       
    46     CleanupStack::Pop( self );
       
    47     return self;
       
    48     }
       
    49 
       
    50 CFSEmailUiMailboxDeleter* CFSEmailUiMailboxDeleter::NewLC( CFSMailClient& aMailClient, 
       
    51                                                            MFSEmailUiMailboxDeleteObserver& aObserver )
       
    52     {
       
    53     FUNC_LOG;
       
    54     CFSEmailUiMailboxDeleter* self = new (ELeave) CFSEmailUiMailboxDeleter( aMailClient, aObserver );
       
    55     CleanupStack::PushL( self );
       
    56     self->ConstructL();
       
    57     return self;
       
    58     }
       
    59 
       
    60 CFSEmailUiMailboxDeleter::CFSEmailUiMailboxDeleter( CFSMailClient& aMailClient, 
       
    61                                                     MFSEmailUiMailboxDeleteObserver& aObserver )
       
    62     : iMailClient( aMailClient ), iObserver( aObserver )
       
    63     {
       
    64     FUNC_LOG;
       
    65     }
       
    66 
       
    67 void CFSEmailUiMailboxDeleter::ConstructL()
       
    68     {
       
    69     FUNC_LOG;
       
    70     }
       
    71 
       
    72 CFSEmailUiMailboxDeleter::~CFSEmailUiMailboxDeleter()
       
    73     {
       
    74     FUNC_LOG;
       
    75     delete iWaitDialog;
       
    76     }
       
    77 
       
    78 
       
    79 // ---------------------------------------------------------------------------
       
    80 // Deletes the given mailbox or displays a list of available mailboxes and
       
    81 // allows the user to mark multiple mailboxes for deletion if aId is NullId.
       
    82 // ---------------------------------------------------------------------------
       
    83 //
       
    84 
       
    85 void CFSEmailUiMailboxDeleter::DeleteMailboxL()
       
    86     {
       
    87     // Make sure that FSMailServer is running so that the mailboxes will also 
       
    88     // be removed from MCE.
       
    89     TFsEmailUiUtility::EnsureFsMailServerIsRunning( CCoeEnv::Static()->WsSession() );
       
    90 
       
    91     iMailboxesToDelete.Reset();
       
    92 
       
    93     // Get mailbox count
       
    94     RPointerArray<CFSMailBox> mailboxes;
       
    95     CleanupResetAndDestroyClosePushL( mailboxes );
       
    96     
       
    97     TFSMailMsgId id; 
       
    98     User::LeaveIfError( iMailClient.ListMailBoxes( id, mailboxes ) );
       
    99        
       
   100     CFSMailBox* mailBox = NULL;
       
   101     
       
   102     // Filter out mailboxes that can't be deleted.
       
   103     for( TInt i(0); i < mailboxes.Count(); )
       
   104         {
       
   105         mailBox = mailboxes[i];
       
   106         if( !mailBox->HasCapability( EFSMBoxCapaCanBeDeleted ) )
       
   107             {
       
   108             delete mailBox;
       
   109             mailboxes.Remove( i );
       
   110             }
       
   111         else
       
   112             {
       
   113             ++i;
       
   114             }
       
   115         }
       
   116     
       
   117     TBool res( EFalse );    
       
   118     
       
   119     if( mailboxes.Count() == 1 )
       
   120         {
       
   121         res = DeleteSingleMailboxL( mailboxes );
       
   122         }
       
   123     else if( mailboxes.Count() > 1 )
       
   124         {
       
   125         res = DeleteMultipleMailboxesL( mailboxes );
       
   126         }
       
   127 
       
   128     CleanupStack::PopAndDestroy(); // mailboxes
       
   129     
       
   130     if( res && ( iMailboxesToDelete.Count() > 0 ) )
       
   131         {
       
   132         // Start wait note.
       
   133         iWaitDialog = new (ELeave) CAknWaitDialog( 
       
   134                     (REINTERPRET_CAST(CEikDialog**, &iWaitDialog)), EFalse );
       
   135         iWaitDialog->PrepareLC( R_FS_WAIT_NOTE_REMOVING_MAILBOX );
       
   136         iWaitDialog->SetCallback( this );
       
   137         iWaitDialog->RunLD();
       
   138 
       
   139         // Set email indicator off.
       
   140         TFsEmailUiUtility::ToggleEmailIconL(EFalse);
       
   141         
       
   142         // Delete first mailbox in queue.
       
   143         DoDeleteNextMailboxL();        
       
   144         }
       
   145     }
       
   146 
       
   147 // ---------------------------------------------------------------------------
       
   148 // DeleteSingleMailboxL
       
   149 // ---------------------------------------------------------------------------
       
   150 //
       
   151 TBool CFSEmailUiMailboxDeleter::DeleteSingleMailboxL( 
       
   152                                 const RPointerArray<CFSMailBox>& aMailboxes )
       
   153     {
       
   154     CFSMailBox* mailBox = aMailboxes[0];
       
   155 
       
   156     TBool res( ConfirmMailboxDeletionL( 1, mailBox->GetName() ) );
       
   157     
       
   158     if( res )
       
   159         {
       
   160         iMailboxesToDelete.AppendL( mailBox->GetId() );
       
   161         }
       
   162     
       
   163     return res;
       
   164     }
       
   165 // ---------------------------------------------------------------------------
       
   166 // DeleteMultipleMailboxesL
       
   167 // ---------------------------------------------------------------------------
       
   168 //
       
   169 TBool CFSEmailUiMailboxDeleter::DeleteMultipleMailboxesL( 
       
   170                                 const RPointerArray<CFSMailBox>& aMailboxes )
       
   171     {
       
   172     // Create array for user selections 
       
   173     CArrayFixFlat<TInt>* selections = new(ELeave) CArrayFixFlat<TInt>( aMailboxes.Count() );
       
   174     CleanupStack::PushL( selections );    
       
   175 
       
   176     // Setup mailbox selection dialog
       
   177     CAknListQueryDialog* dlg = new(ELeave) CAknListQueryDialog( selections );
       
   178     dlg->PrepareLC( R_DELETE_MAILBOXES_MULTI_SELECTION_QUERY );
       
   179      
       
   180     // Create icon array
       
   181     CArrayPtr<CGulIcon>* mailboxListIconArray = new(ELeave) CArrayPtrFlat<CGulIcon>(1);
       
   182     CleanupStack::PushL( mailboxListIconArray );
       
   183 
       
   184     // Create listbox model
       
   185     CDesCArrayFlat* mailboxListModel = new(ELeave) CDesCArrayFlat(1);
       
   186     CleanupStack::PushL( mailboxListModel );
       
   187 
       
   188     CreateListboxModelAndIconArrayL( *mailboxListModel, *mailboxListIconArray, aMailboxes );
       
   189      
       
   190     dlg->SetOwnershipType( ELbmOwnsItemArray );        
       
   191     dlg->SetItemTextArray( mailboxListModel ); // Takes ownership
       
   192     CleanupStack::Pop( mailboxListModel );
       
   193 
       
   194     dlg->SetIconArrayL( mailboxListIconArray ); // Takes ownership
       
   195     CleanupStack::Pop( mailboxListIconArray ); 
       
   196 
       
   197     // Display mailbox selection dialog.
       
   198     TBool res( dlg->RunLD() );
       
   199 
       
   200     if( res && ( selections->Count() > 0 ) )
       
   201         {
       
   202         res = ConfirmMailboxDeletionL( selections->Count(), 
       
   203                                        aMailboxes[selections->At(0)]->GetName() );
       
   204          
       
   205         if( res )
       
   206             {
       
   207             for( TInt i( 0 ); i < selections->Count(); ++i )
       
   208                 {
       
   209                 iMailboxesToDelete.AppendL( aMailboxes[selections->At(i)]->GetId() );
       
   210                 }
       
   211             }
       
   212         }
       
   213         
       
   214     CleanupStack::PopAndDestroy( selections );
       
   215 
       
   216     return res;
       
   217     }
       
   218 
       
   219 // ---------------------------------------------------------------------------
       
   220 // CreateMarkIconLC
       
   221 // ---------------------------------------------------------------------------
       
   222 //
       
   223 CGulIcon* CFSEmailUiMailboxDeleter::CreateMarkIconLC()
       
   224     {
       
   225     TAknsItemID skinId;
       
   226     skinId.Set( EAknsMajorGeneric, EAknsMinorGenericQgnIndiMarkedAdd );
       
   227     
       
   228     TRgb defaultColour( KRgbBlack );
       
   229     CFbsBitmap* bmap = NULL;
       
   230     CFbsBitmap* mask = NULL;
       
   231 
       
   232     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   233     AknsUtils::GetCachedColor( skin, defaultColour,
       
   234                KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13 );
       
   235     AknsUtils::CreateColorIconLC( skin, 
       
   236                                   skinId,
       
   237                                   KAknsIIDQsnIconColors, 
       
   238                                   EAknsCIQsnIconColorsCG13, 
       
   239                                   bmap, 
       
   240                                   mask,
       
   241                                   AknIconUtils::AvkonIconFileName(), 
       
   242                                   EMbmAvkonQgn_indi_marked_add, 
       
   243                                   EMbmAvkonQgn_indi_marked_add_mask,
       
   244                                   defaultColour );
       
   245     
       
   246     CGulIcon* icon = CGulIcon::NewL( bmap, mask );
       
   247     icon->SetBitmapsOwnedExternally( EFalse );
       
   248     CleanupStack::Pop( 2 ); // Icon owns the bitmaps now   
       
   249     CleanupStack::PushL( icon );
       
   250     return icon;
       
   251     }
       
   252 
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // CreateDefaultMailboxIconLC
       
   256 // ---------------------------------------------------------------------------
       
   257 //
       
   258 CGulIcon* CFSEmailUiMailboxDeleter::CreateDefaultMailboxIconLC()
       
   259     {
       
   260     TFileName iconFileName;
       
   261     TFsEmailUiUtility::GetFullIconFileNameL( iconFileName );
       
   262    
       
   263     CFbsBitmap* bmap = NULL;
       
   264     CFbsBitmap* mask = NULL;
       
   265     AknIconUtils::CreateIconLC( bmap, mask, iconFileName,
       
   266                          EMbmFreestyleemailuiQgn_indi_cmail_drop_inbox,
       
   267                          EMbmFreestyleemailuiQgn_indi_cmail_drop_inbox_mask);
       
   268     CGulIcon* defaultIcon = CGulIcon::NewL( bmap, mask );
       
   269     defaultIcon->SetBitmapsOwnedExternally( EFalse );
       
   270     CleanupStack::Pop( 2 ); // Icon owns the bitmaps now   
       
   271     CleanupStack::PushL( defaultIcon );    
       
   272 
       
   273     return defaultIcon;
       
   274     }
       
   275 
       
   276 // ---------------------------------------------------------------------------
       
   277 // ConfirmMailboxDeletionL
       
   278 // ---------------------------------------------------------------------------
       
   279 //
       
   280 TBool CFSEmailUiMailboxDeleter::ConfirmMailboxDeletionL( TInt aCount, 
       
   281                                                          const TDesC& aMailboxName )
       
   282     {
       
   283     CAknQueryDialog* dlg = new ( ELeave ) CAknQueryDialog();
       
   284     dlg->PrepareLC( R_FSEMAIL_QUERY_DIALOG );
       
   285 
       
   286     HBufC* queryText = NULL;    
       
   287     if( aCount == 1 )
       
   288         {
       
   289         queryText = StringLoader::LoadLC( R_DELETE_MAILBOX_QUERY, aMailboxName );
       
   290         }
       
   291     else if( aCount > 0 )
       
   292         {
       
   293         queryText = StringLoader::LoadLC( R_DELETE_MULTIPLE_MAILBOXES_QUERY, 
       
   294                                           aCount );
       
   295         }
       
   296 
       
   297     dlg->SetPromptL( *queryText );
       
   298     CleanupStack::PopAndDestroy( queryText );
       
   299 
       
   300     return dlg->RunLD();
       
   301     }
       
   302 
       
   303 // ---------------------------------------------------------------------------
       
   304 // CreateListboxModelAndIconArrayL
       
   305 // ---------------------------------------------------------------------------
       
   306 //
       
   307 void CFSEmailUiMailboxDeleter::CreateListboxModelAndIconArrayL( 
       
   308                                     CDesCArrayFlat& aModel,
       
   309                                     CArrayPtr<CGulIcon>& aIconArray, 
       
   310                                     const RPointerArray<CFSMailBox>& aMailboxes )
       
   311     {
       
   312     // Create and add listbox mark icon
       
   313     CGulIcon* markIcon = CreateMarkIconLC();
       
   314     aIconArray.AppendL( markIcon );
       
   315     CleanupStack::Pop( markIcon );
       
   316 
       
   317     // Create and add default (non-branded) mailbox icon
       
   318     CGulIcon* defaultIcon = CreateDefaultMailboxIconLC();
       
   319     aIconArray.AppendL( defaultIcon );
       
   320     CleanupStack::Pop( defaultIcon );
       
   321     
       
   322     // Add branded mailbox icons
       
   323     CFSMailBox* mailBox = NULL;     
       
   324     for( TInt i(0); i < aMailboxes.Count(); ++i )
       
   325         {
       
   326         mailBox = aMailboxes[i];
       
   327          
       
   328         CGulIcon* mbIcon = NULL;
       
   329          
       
   330         TRAPD( err, mbIcon = iMailClient.GetBrandManagerL().GetGraphicL( EFSMailboxIcon, 
       
   331                                                                          mailBox->GetId() ) ); 
       
   332         if ( err == KErrNone && mbIcon )
       
   333             {
       
   334             CleanupStack::PushL( mbIcon );
       
   335             aIconArray.AppendL( mbIcon );
       
   336             CleanupStack::Pop( mbIcon );            
       
   337             }
       
   338         
       
   339         TPtrC name( mailBox->GetName() );
       
   340         HBufC* buf = HBufC::NewLC( name.Length() + 
       
   341                                    KTabCharacter().Length() * 2 + 4 ); // +4 for icon id
       
   342         TPtr bufPtr( buf->Des() );
       
   343 
       
   344         TInt index( mbIcon ? aIconArray.Count() - 1 : 1 ); // Select branded or default icon
       
   345         TBuf<4> indexBuf;
       
   346         indexBuf.Num( index );
       
   347         bufPtr.Append( indexBuf );
       
   348 
       
   349         bufPtr.Append( KTabCharacter );
       
   350         bufPtr.Append( name );
       
   351         bufPtr.Append( KTabCharacter );
       
   352         aModel.AppendL( bufPtr );
       
   353         CleanupStack::PopAndDestroy( buf );
       
   354         }            
       
   355     }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 // RequestResponseL
       
   359 // ---------------------------------------------------------------------------
       
   360 //
       
   361 void CFSEmailUiMailboxDeleter::RequestResponseL( TFSProgress aEvent, 
       
   362                                                  TInt aRequestId )
       
   363     {
       
   364     switch( aEvent.iProgressStatus )
       
   365         {
       
   366         case TFSProgress::EFSStatus_RequestComplete:
       
   367         case TFSProgress::EFSStatus_RequestCancelled:
       
   368             {
       
   369             if( aRequestId == iMailboxDeleteOperationId )
       
   370                 {
       
   371                 if( iMailboxesToDelete.Count() > 0 )
       
   372                     {
       
   373                     // Delete next mailbox in queue.
       
   374                     DoDeleteNextMailboxL();
       
   375                     }
       
   376                 else
       
   377                     {
       
   378                     if( iWaitDialog )
       
   379                         {
       
   380                         iWaitDialog->ProcessFinishedL();
       
   381                         }
       
   382                     // Notify observer that the deletion is complete. 
       
   383                     iObserver.MailboxDeletionComplete();
       
   384                     }
       
   385                 }            
       
   386             break;
       
   387             }
       
   388         default:
       
   389             {
       
   390             break;
       
   391             }
       
   392         }
       
   393     }
       
   394 
       
   395 // ---------------------------------------------------------------------------
       
   396 // DialogDismissedL
       
   397 // ---------------------------------------------------------------------------
       
   398 //
       
   399 void CFSEmailUiMailboxDeleter::DialogDismissedL( TInt /*aButtonId */ )
       
   400     {
       
   401     iWaitDialog = NULL;
       
   402     }
       
   403     
       
   404 // ---------------------------------------------------------------------------
       
   405 // DoDeleteNextMailboxL
       
   406 // ---------------------------------------------------------------------------
       
   407 //
       
   408 void CFSEmailUiMailboxDeleter::DoDeleteNextMailboxL()
       
   409     {
       
   410     TFSMailMsgId nextToDelete = iMailboxesToDelete[0]; 
       
   411     iMailboxesToDelete.Remove( 0 );
       
   412     iMailboxDeleteOperationId = iMailClient.DeleteMailBoxByUidL( nextToDelete, 
       
   413                                                                  *this );
       
   414     }
       
   415 
       
   416 // End of file