emailuis/emailui/src/freestyleemailuimailboxdeleter.cpp
branchRCL_3
changeset 25 3533d4323edc
equal deleted inserted replaced
24:d189ee25cf9d 25:3533d4323edc
       
     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     iIdle = CIdle::NewL (CIdle::EPriorityIdle);
       
    71     }
       
    72 
       
    73 CFSEmailUiMailboxDeleter::~CFSEmailUiMailboxDeleter()
       
    74     {
       
    75     FUNC_LOG;
       
    76     delete iWaitDialog;
       
    77     iMailboxesToDelete.Close();
       
    78     if (iIdle)
       
    79     	{
       
    80     	iIdle->Cancel();
       
    81     	delete iIdle;
       
    82     	}
       
    83     }
       
    84 
       
    85 
       
    86 // ---------------------------------------------------------------------------
       
    87 // Deletes the given mailbox or displays a list of available mailboxes and
       
    88 // allows the user to mark multiple mailboxes for deletion if aId is NullId.
       
    89 // ---------------------------------------------------------------------------
       
    90 //
       
    91 void CFSEmailUiMailboxDeleter::DeleteMailboxL()
       
    92     {
       
    93     iMailboxesToDelete.Reset();
       
    94 
       
    95     // Get the deletable mailboxes.
       
    96     RPointerArray<CFSMailBox> mailboxes;
       
    97     const TInt mailboxCount = GetDeletableMailboxesLC( mailboxes );
       
    98     
       
    99     TBool res( EFalse );    
       
   100     
       
   101     if( mailboxCount == 1 )
       
   102         {
       
   103         res = DeleteSingleMailboxL( mailboxes );
       
   104         }
       
   105     else if( mailboxCount > 1 )
       
   106         {
       
   107         res = DeleteMultipleMailboxesL( mailboxes );
       
   108         }
       
   109 
       
   110     CleanupStack::PopAndDestroy(); // mailboxes
       
   111     
       
   112     if ( res )
       
   113         {
       
   114         DoDeleteSelectedMailboxesL();
       
   115         }
       
   116     }
       
   117 
       
   118 // ---------------------------------------------------------------------------
       
   119 // DeleteMailboxL()
       
   120 // Deletes the given mailbox. Displays also a confirmation dialog.
       
   121 // ---------------------------------------------------------------------------
       
   122 //
       
   123 void CFSEmailUiMailboxDeleter::DeleteMailboxL( const TFSMailMsgId& aMailboxId )
       
   124 	{
       
   125     iMailboxesToDelete.Reset();
       
   126 
       
   127     // Get the deletable mailboxes.
       
   128     RPointerArray<CFSMailBox> mailboxes;
       
   129     const TInt mailboxCount = GetDeletableMailboxesLC( mailboxes );
       
   130 
       
   131     // Make sure that it is allowed to delete the mailbox with the given ID.
       
   132     CFSMailBox* mailbox = NULL;
       
   133 
       
   134     for ( TInt i( 0 ); i < mailboxCount; ++i )
       
   135     	{
       
   136     	mailbox = mailboxes[i];
       
   137 
       
   138     	if ( mailbox && mailbox->GetId() == aMailboxId )
       
   139     		{
       
   140     		// The mailbox can be deleted.
       
   141     		break;
       
   142     		}
       
   143 
       
   144     	mailbox = NULL;
       
   145     	}
       
   146 
       
   147     if ( !mailbox )
       
   148     	{
       
   149     	// Either no mailbox with the given ID exist or it is not allowed to
       
   150     	// be deleted.
       
   151     	// Display an error message?
       
   152     	CleanupStack::PopAndDestroy(); // mailboxes
       
   153     	return;
       
   154     	}
       
   155 
       
   156     TBool response = ConfirmMailboxDeletionL( 1, mailbox->GetName() );
       
   157     CleanupStack::PopAndDestroy(); // mailboxes
       
   158 
       
   159     if ( !response )
       
   160     	{
       
   161     	// User did not confirm the deletion.
       
   162     	return;
       
   163     	}
       
   164     
       
   165     iMailboxesToDelete.AppendL( aMailboxId );
       
   166     DoDeleteSelectedMailboxesL();
       
   167 	}
       
   168 
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // GetDeletableMailboxesLC()
       
   172 // Returns the mailboxes that can be deleted.
       
   173 // ---------------------------------------------------------------------------
       
   174 //
       
   175 TInt CFSEmailUiMailboxDeleter::GetDeletableMailboxesLC(
       
   176 	RPointerArray<CFSMailBox>& aMailboxes )
       
   177 	{
       
   178     // Make sure that FSMailServer is running so that the mailboxes will also
       
   179     // be removed from MCE.
       
   180     TFsEmailUiUtility::EnsureFsMailServerIsRunning(
       
   181     	CCoeEnv::Static()->WsSession() );
       
   182 
       
   183     CleanupResetAndDestroyClosePushL( aMailboxes );
       
   184 
       
   185     // Get the mailboxes.
       
   186     TFSMailMsgId id; 
       
   187     User::LeaveIfError( iMailClient.ListMailBoxes( id, aMailboxes ) );
       
   188 
       
   189     CFSMailBox* mailbox = NULL;
       
   190     
       
   191     // Filter out mailboxes that can't be deleted.
       
   192     const TInt mailboxCount( aMailboxes.Count() );
       
   193 
       
   194     for( TInt i( 0 ); i < mailboxCount; )
       
   195         {
       
   196         mailbox = aMailboxes[i];
       
   197 
       
   198         if( !mailbox->HasCapability( EFSMBoxCapaCanBeDeleted ) )
       
   199             {
       
   200             delete mailbox;
       
   201             aMailboxes.Remove( i );
       
   202             }
       
   203         else
       
   204             {
       
   205             ++i;
       
   206             }
       
   207         }
       
   208 
       
   209     return aMailboxes.Count();
       
   210 	}
       
   211 
       
   212 
       
   213 // ---------------------------------------------------------------------------
       
   214 // DeleteSingleMailboxL
       
   215 // ---------------------------------------------------------------------------
       
   216 //
       
   217 TBool CFSEmailUiMailboxDeleter::DeleteSingleMailboxL( 
       
   218                                 const RPointerArray<CFSMailBox>& aMailboxes )
       
   219     {
       
   220     CFSMailBox* mailBox = aMailboxes[0];
       
   221 
       
   222     TBool res( ConfirmMailboxDeletionL( 1, mailBox->GetName() ) );
       
   223     
       
   224     if( res )
       
   225         {
       
   226         iMailboxesToDelete.AppendL( mailBox->GetId() );
       
   227         }
       
   228     
       
   229     return res;
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------------------------
       
   233 // DeleteMultipleMailboxesL
       
   234 // ---------------------------------------------------------------------------
       
   235 //
       
   236 TBool CFSEmailUiMailboxDeleter::DeleteMultipleMailboxesL( 
       
   237                                 const RPointerArray<CFSMailBox>& aMailboxes )
       
   238     {
       
   239     // Create array for user selections 
       
   240     CArrayFixFlat<TInt>* selections = new(ELeave) CArrayFixFlat<TInt>( aMailboxes.Count() );
       
   241     CleanupStack::PushL( selections );    
       
   242 
       
   243     // Setup mailbox selection dialog
       
   244     CAknListQueryDialog* dlg = new(ELeave) CAknListQueryDialog( selections );
       
   245     dlg->PrepareLC( R_DELETE_MAILBOXES_MULTI_SELECTION_QUERY );
       
   246      
       
   247     // Create icon array
       
   248     CArrayPtr<CGulIcon>* mailboxListIconArray = new(ELeave) CArrayPtrFlat<CGulIcon>(1);
       
   249     CleanupStack::PushL( mailboxListIconArray );
       
   250 
       
   251     // Create listbox model
       
   252     CDesCArrayFlat* mailboxListModel = new(ELeave) CDesCArrayFlat(1);
       
   253     CleanupStack::PushL( mailboxListModel );
       
   254 
       
   255     CreateListboxModelAndIconArrayL( *mailboxListModel, *mailboxListIconArray, aMailboxes );
       
   256      
       
   257     dlg->SetOwnershipType( ELbmOwnsItemArray );        
       
   258     dlg->SetItemTextArray( mailboxListModel ); // Takes ownership
       
   259     CleanupStack::Pop( mailboxListModel );
       
   260 
       
   261     dlg->SetIconArrayL( mailboxListIconArray ); // Takes ownership
       
   262     CleanupStack::Pop( mailboxListIconArray ); 
       
   263 
       
   264     // Display mailbox selection dialog.
       
   265     TBool res( dlg->RunLD() );
       
   266 
       
   267     if( res && ( selections->Count() > 0 ) )
       
   268         {
       
   269         res = ConfirmMailboxDeletionL( selections->Count(), 
       
   270                                        aMailboxes[selections->At(0)]->GetName() );
       
   271          
       
   272         if( res )
       
   273             {
       
   274             for( TInt i( 0 ); i < selections->Count(); ++i )
       
   275                 {
       
   276                 iMailboxesToDelete.AppendL( aMailboxes[selections->At(i)]->GetId() );
       
   277                 }
       
   278             }
       
   279         }
       
   280         
       
   281     CleanupStack::PopAndDestroy( selections );
       
   282 
       
   283     return res;
       
   284     }
       
   285 
       
   286 
       
   287 // ---------------------------------------------------------------------------
       
   288 // DoDeleteSelectedMailboxesL()
       
   289 // Deletes the selected mailboxes i.e. those of which IDs are in the
       
   290 // iMailboxesToDelete array.
       
   291 // ---------------------------------------------------------------------------
       
   292 //
       
   293 void CFSEmailUiMailboxDeleter::DoDeleteSelectedMailboxesL()
       
   294 	{
       
   295     if ( iMailboxesToDelete.Count() < 1 )
       
   296     	{
       
   297     	// Nothing to delete!
       
   298     	return;
       
   299     	}
       
   300     
       
   301     // Start wait note.
       
   302     iWaitDialog = new ( ELeave ) CAknWaitDialog(
       
   303         ( REINTERPRET_CAST( CEikDialog**, &iWaitDialog ) ), ETrue );
       
   304     iWaitDialog->PrepareLC( R_FS_WAIT_NOTE_REMOVING_MAILBOX );
       
   305     iWaitDialog->SetCallback( this );
       
   306     iWaitDialog->RunLD();
       
   307 
       
   308     // Set email indicator off.
       
   309     TFsEmailUiUtility::ToggleEmailIconL( EFalse );
       
   310         
       
   311     // Delete first mailbox in queue.
       
   312 	DoDeleteNextMailboxL();
       
   313 	}
       
   314 
       
   315 
       
   316 // ---------------------------------------------------------------------------
       
   317 // CreateMarkIconLC
       
   318 // ---------------------------------------------------------------------------
       
   319 //
       
   320 CGulIcon* CFSEmailUiMailboxDeleter::CreateMarkIconLC()
       
   321     {
       
   322     TAknsItemID skinId;
       
   323     skinId.Set( EAknsMajorGeneric, EAknsMinorGenericQgnIndiMarkedAdd );
       
   324     
       
   325     TRgb defaultColour( KRgbBlack );
       
   326     CFbsBitmap* bmap = NULL;
       
   327     CFbsBitmap* mask = NULL;
       
   328 
       
   329     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   330     AknsUtils::GetCachedColor( skin, defaultColour,
       
   331                KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13 );
       
   332     AknsUtils::CreateColorIconLC( skin, 
       
   333                                   skinId,
       
   334                                   KAknsIIDQsnIconColors, 
       
   335                                   EAknsCIQsnIconColorsCG13, 
       
   336                                   bmap, 
       
   337                                   mask,
       
   338                                   AknIconUtils::AvkonIconFileName(), 
       
   339                                   EMbmAvkonQgn_indi_marked_add, 
       
   340                                   EMbmAvkonQgn_indi_marked_add_mask,
       
   341                                   defaultColour );
       
   342     
       
   343     CGulIcon* icon = CGulIcon::NewL( bmap, mask );
       
   344     icon->SetBitmapsOwnedExternally( EFalse );
       
   345     CleanupStack::Pop( 2 ); // Icon owns the bitmaps now   
       
   346     CleanupStack::PushL( icon );
       
   347     return icon;
       
   348     }
       
   349 
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 // CreateDefaultMailboxIconLC
       
   353 // ---------------------------------------------------------------------------
       
   354 //
       
   355 CGulIcon* CFSEmailUiMailboxDeleter::CreateDefaultMailboxIconLC()
       
   356     {
       
   357     TFileName iconFileName;
       
   358     TFsEmailUiUtility::GetFullIconFileNameL( iconFileName );
       
   359    
       
   360     CFbsBitmap* bmap = NULL;
       
   361     CFbsBitmap* mask = NULL;
       
   362     AknIconUtils::CreateIconLC( bmap, mask, iconFileName,
       
   363                          EMbmFreestyleemailuiQgn_indi_cmail_drop_inbox,
       
   364                          EMbmFreestyleemailuiQgn_indi_cmail_drop_inbox_mask);
       
   365     CGulIcon* defaultIcon = CGulIcon::NewL( bmap, mask );
       
   366     defaultIcon->SetBitmapsOwnedExternally( EFalse );
       
   367     CleanupStack::Pop( 2 ); // Icon owns the bitmaps now   
       
   368     CleanupStack::PushL( defaultIcon );    
       
   369 
       
   370     return defaultIcon;
       
   371     }
       
   372 
       
   373 // ---------------------------------------------------------------------------
       
   374 // ConfirmMailboxDeletionL
       
   375 // ---------------------------------------------------------------------------
       
   376 //
       
   377 TBool CFSEmailUiMailboxDeleter::ConfirmMailboxDeletionL( TInt aCount, 
       
   378                                                          const TDesC& aMailboxName )
       
   379     {
       
   380     CAknQueryDialog* dlg = new ( ELeave ) CAknQueryDialog();
       
   381     dlg->PrepareLC( R_FSEMAIL_QUERY_DIALOG );
       
   382 
       
   383     HBufC* queryText = NULL;    
       
   384     if( aCount == 1 )
       
   385         {
       
   386         queryText = StringLoader::LoadLC( R_DELETE_MAILBOX_QUERY, aMailboxName );
       
   387         }
       
   388     else if( aCount > 0 )
       
   389         {
       
   390         queryText = StringLoader::LoadLC( R_DELETE_MULTIPLE_MAILBOXES_QUERY, 
       
   391                                           aCount );
       
   392         }
       
   393 
       
   394     dlg->SetPromptL( *queryText );
       
   395     CleanupStack::PopAndDestroy( queryText );
       
   396 
       
   397     return dlg->RunLD();
       
   398     }
       
   399 
       
   400 // ---------------------------------------------------------------------------
       
   401 // CreateListboxModelAndIconArrayL
       
   402 // ---------------------------------------------------------------------------
       
   403 //
       
   404 void CFSEmailUiMailboxDeleter::CreateListboxModelAndIconArrayL( 
       
   405                                     CDesCArrayFlat& aModel,
       
   406                                     CArrayPtr<CGulIcon>& aIconArray, 
       
   407                                     const RPointerArray<CFSMailBox>& aMailboxes )
       
   408     {
       
   409     // Create and add listbox mark icon
       
   410     CGulIcon* markIcon = CreateMarkIconLC();
       
   411     aIconArray.AppendL( markIcon );
       
   412     CleanupStack::Pop( markIcon );
       
   413 
       
   414     // Create and add default (non-branded) mailbox icon
       
   415     CGulIcon* defaultIcon = CreateDefaultMailboxIconLC();
       
   416     aIconArray.AppendL( defaultIcon );
       
   417     CleanupStack::Pop( defaultIcon );
       
   418     
       
   419     // Add branded mailbox icons
       
   420     CFSMailBox* mailBox = NULL;     
       
   421     for( TInt i(0); i < aMailboxes.Count(); ++i )
       
   422         {
       
   423         mailBox = aMailboxes[i];
       
   424          
       
   425         CGulIcon* mbIcon = NULL;
       
   426          
       
   427         TRAPD( err, mbIcon = iMailClient.GetBrandManagerL().GetGraphicL( EFSMailboxIcon, 
       
   428                                                                          mailBox->GetId() ) ); 
       
   429         if ( err == KErrNone && mbIcon )
       
   430             {
       
   431             CleanupStack::PushL( mbIcon );
       
   432             aIconArray.AppendL( mbIcon );
       
   433             CleanupStack::Pop( mbIcon );            
       
   434             }
       
   435         
       
   436         TPtrC name( mailBox->GetName() );
       
   437         HBufC* buf = HBufC::NewLC( name.Length() + 
       
   438                                    KTabCharacter().Length() * 2 + 4 ); // +4 for icon id
       
   439         TPtr bufPtr( buf->Des() );
       
   440 
       
   441         TInt index( mbIcon ? aIconArray.Count() - 1 : 1 ); // Select branded or default icon
       
   442         TBuf<4> indexBuf;
       
   443         indexBuf.Num( index );
       
   444         bufPtr.Append( indexBuf );
       
   445 
       
   446         bufPtr.Append( KTabCharacter );
       
   447         bufPtr.Append( name );
       
   448         bufPtr.Append( KTabCharacter );
       
   449         aModel.AppendL( bufPtr );
       
   450         CleanupStack::PopAndDestroy( buf );
       
   451         }            
       
   452     }
       
   453 
       
   454 // ---------------------------------------------------------------------------
       
   455 // RequestResponseL
       
   456 // ---------------------------------------------------------------------------
       
   457 //
       
   458 void CFSEmailUiMailboxDeleter::RequestResponseL( TFSProgress aEvent, 
       
   459                                                  TInt aRequestId )
       
   460     {
       
   461     switch( aEvent.iProgressStatus )
       
   462         {
       
   463         case TFSProgress::EFSStatus_RequestComplete:
       
   464         case TFSProgress::EFSStatus_RequestCancelled:
       
   465             {
       
   466             if( iMailboxesToDelete.Count() > 0 )
       
   467             	{
       
   468                 // Delete next mailbox in queue.
       
   469                 iIdle->Cancel();
       
   470                 iIdle->Start(TCallBack(IdleCallbackL,this));
       
   471             	}
       
   472             else
       
   473             	{
       
   474                 if( iWaitDialog )
       
   475                     {
       
   476                     iWaitDialog->ProcessFinishedL();
       
   477                     }
       
   478                 // Notify observer that the deletion is complete. 
       
   479                 iObserver.MailboxDeletionComplete();
       
   480             	}          
       
   481             break;
       
   482             }
       
   483         default:
       
   484             {
       
   485             break;
       
   486             }
       
   487         }
       
   488     }
       
   489 
       
   490 // ---------------------------------------------------------------------------
       
   491 // DialogDismissedL
       
   492 // ---------------------------------------------------------------------------
       
   493 //
       
   494 void CFSEmailUiMailboxDeleter::DialogDismissedL( TInt /*aButtonId */ )
       
   495     {
       
   496     iWaitDialog = NULL;
       
   497     }
       
   498     
       
   499 // ---------------------------------------------------------------------------
       
   500 // DoDeleteNextMailboxL
       
   501 // ---------------------------------------------------------------------------
       
   502 //
       
   503 void CFSEmailUiMailboxDeleter::DoDeleteNextMailboxL()
       
   504     {
       
   505     TFSMailMsgId nextToDelete = iMailboxesToDelete[0]; 
       
   506     iMailboxesToDelete.Remove( 0 );
       
   507     iMailboxDeleteOperationId = iMailClient.DeleteMailBoxByUidL( nextToDelete, 
       
   508                                                                  *this );
       
   509     }
       
   510 // ---------------------------------------------------------------------------
       
   511 // IdleCallback
       
   512 // ---------------------------------------------------------------------------
       
   513 //
       
   514 TInt CFSEmailUiMailboxDeleter::IdleCallbackL(TAny* aPtr)
       
   515 	{
       
   516 	TRAPD( leaveErr,
       
   517 		   static_cast<CFSEmailUiMailboxDeleter*>
       
   518 		   (aPtr)->DoDeleteNextMailboxL(); );
       
   519 	return leaveErr;
       
   520 	}
       
   521 
       
   522 // End of file