email/imum/Mtms/Src/ImapFolderSubscription.cpp
branchRCL_3
changeset 60 7fdbb852d323
parent 0 72b543305e3a
equal deleted inserted replaced
57:ebe688cedc25 60:7fdbb852d323
       
     1 /*
       
     2 * Copyright (c) 2006 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: 
       
    15 *		folder subscription dialog
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDES
       
    21 #include <impicmds.h>
       
    22 #include <imum.rsg>
       
    23 #include <MtmStore.h>
       
    24 #include <msvuids.h>
       
    25 #include <mtudcbas.h>
       
    26 #include <mtmuibas.h>
       
    27 #include <miuthdr.h>
       
    28 #include <imapcmds.h>
       
    29 #include <gulicon.h>
       
    30 #include <GULUTIL.H>
       
    31 #include <EIKCLB.H>
       
    32 #include <EIKCLBD.H>
       
    33 #include <EIKTXLBM.H>
       
    34 #include <AknLists.h>
       
    35 #include <AknIconArray.h>                   // CAknIconArray
       
    36 #include <StringLoader.h>
       
    37 #include <eikmenup.h>
       
    38 #include <FeatMgr.h>                        // FeatureManager
       
    39 #include <hlplch.h>                         // For HlpLauncher
       
    40 #include <muiu.mbg>
       
    41 #include <coemain.h>
       
    42 #include <muiuoperationwait.h>
       
    43 #include <aknquerydialog.h>
       
    44 #include <AknInputBlock.h>					// CAknInputBlock
       
    45 #include <aknsutils.h>
       
    46 #include <aknsconstants.h>                  // Skinned icon ids
       
    47 #include <mtudcbas.h>
       
    48 #include "imapfoldersubscription.h"
       
    49 #include "ImumPanic.h"
       
    50 #include "imas.hrh"
       
    51 #include "EmailUtils.H"
       
    52 #include <data_caging_path_literals.hrh>
       
    53 
       
    54 #include <csxhelp/mbxs.hlp.hrh>
       
    55 
       
    56 // CONSTANTS
       
    57 // Correct path is added to literal when it is used.
       
    58 _LIT(KMuiuBitmapFile, "z:muiu.MBM");
       
    59 const TInt KImumFolderArrayVisibleItems = 5;
       
    60 const TInt KImumFolderIconArrayGranularity = 6;
       
    61 const TInt KImumITextLength = 100;
       
    62 
       
    63 
       
    64 // ----------------------------------------------------------------------------
       
    65 // CImapSubscriptionDialog::NewL
       
    66 // ----------------------------------------------------------------------------
       
    67 CImapSubscriptionDialog* CImapSubscriptionDialog::NewL(
       
    68     TMsvId aServiceId,
       
    69     CMsvSession& aSession,
       
    70     TBool& aShouldExit )
       
    71 	{
       
    72 	CImapSubscriptionDialog* self=new(ELeave) CImapSubscriptionDialog(
       
    73         aServiceId,
       
    74         aSession,
       
    75         aShouldExit );
       
    76 
       
    77 	CleanupStack::PushL(self);
       
    78 	self->ConstructL();
       
    79 	CleanupStack::Pop();
       
    80 	return self;
       
    81 	}
       
    82 
       
    83 // ----------------------------------------------------------------------------
       
    84 // CImapSubscriptionDialog::~CImapSubscriptionDialog
       
    85 // ----------------------------------------------------------------------------
       
    86 CImapSubscriptionDialog::~CImapSubscriptionDialog()
       
    87 	{
       
    88 	iSession.RemoveObserver(*this);
       
    89 	if (iMtmStore)
       
    90 		{
       
    91 		iMtmStore->ReleaseMtmUi(KUidMsgTypeIMAP4);
       
    92 		delete iMtmStore;
       
    93 		}
       
    94 	delete iContextEntry;
       
    95     delete iFolderListArray;
       
    96 	}
       
    97 
       
    98 // ----------------------------------------------------------------------------
       
    99 // CImapSubscriptionDialog::CImapSubscriptionDialog
       
   100 // ----------------------------------------------------------------------------
       
   101 CImapSubscriptionDialog::CImapSubscriptionDialog(
       
   102     TMsvId aServiceId,
       
   103     CMsvSession& aSession,
       
   104     TBool& aShouldExit )
       
   105     :
       
   106     iServiceId(aServiceId),
       
   107 	iSession(aSession),
       
   108     iShouldExit( aShouldExit )
       
   109 	{
       
   110 	}
       
   111 
       
   112     // ----------------------------------------------------------------------------
       
   113 // CImapSubscriptionDialog::ConstructL
       
   114 // ----------------------------------------------------------------------------
       
   115 void CImapSubscriptionDialog::ConstructL()
       
   116 	{
       
   117     FeatureManager::InitializeLibL();
       
   118 
       
   119     CAknDialog::ConstructL( R_IMAS_SETTINGS_SUBSCRIBE_MENU );
       
   120     iSubscriptionsChanged = EFalse;
       
   121 
       
   122 	iSession.AddObserverL(*this);
       
   123 	iMtmStore=CMtmStore::NewL(iSession);
       
   124 
       
   125 	iMtmUiData=&(iMtmStore->MtmUiDataL(KUidMsgTypeIMAP4));
       
   126 
       
   127 	iMtmUi=&(iMtmStore->ClaimMtmUiL(KUidMsgTypeIMAP4));
       
   128 	iMtmUi->BaseMtm().SwitchCurrentEntryL(iServiceId);
       
   129 
       
   130 	TMsvSelectionOrdering selectionOrdering(KMsvGroupByType, EMsvSortByDetails, ETrue);
       
   131 	iContextEntry=CMsvEntry::NewL(iSession, iServiceId, selectionOrdering);
       
   132     iFolderListArray = CImapFolderListArray::NewL( iSession, iServiceId );
       
   133     iOpenedFolderId = iServiceId;
       
   134 
       
   135 	}
       
   136 
       
   137 // ----------------------------------------------------------------------------
       
   138 // CImapSubscriptionDialog::PreLayoutDynInitL
       
   139 // ----------------------------------------------------------------------------
       
   140 void CImapSubscriptionDialog::PreLayoutDynInitL()
       
   141 	{
       
   142     TBool refreshed = EFalse;
       
   143     CAknQueryDialog* confDialog = CAknQueryDialog::NewL();
       
   144 	if( confDialog->ExecuteLD( R_IMUM_CONNECT_FOR_UPDATE ) )
       
   145         {
       
   146         refreshed = ETrue;
       
   147         CreateOperationL( ERefreshFolderList );
       
   148         }
       
   149 
       
   150     if( refreshed )
       
   151         {
       
   152         iFolderListArray->RefreshFolderListArrayL();
       
   153         }
       
   154 
       
   155     CEikColumnListBox* listBox =
       
   156         static_cast< CEikColumnListBox* >( Control( EImasCIDFolderListBox ) );
       
   157 
       
   158     listBox->SetListBoxObserver( this );
       
   159 
       
   160     // Create scroll bar
       
   161     listBox->CreateScrollBarFrameL();
       
   162     listBox->ScrollBarFrame()->SetScrollBarVisibilityL(
       
   163         CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
   164 
       
   165 
       
   166     CTextListBoxModel* model = listBox->Model();
       
   167     model->SetItemTextArray( iFolderListArray );
       
   168     listBox->Model()->SetOwnershipType( ELbmDoesNotOwnItemArray );
       
   169 
       
   170     HBufC* emptyText =
       
   171         StringLoader::LoadLC( R_IMAS_SETTINGS_DIALOG_SUBS_EMPTY_TEXT );
       
   172 
       
   173     listBox->View()->SetListEmptyTextL( emptyText->Des() );
       
   174 
       
   175     CArrayPtrFlat<CGulIcon>* icons
       
   176         = new(ELeave) CAknIconArray( KImumFolderIconArrayGranularity );
       
   177 
       
   178     CleanupStack::PushL( icons );
       
   179 
       
   180     // create icon array
       
   181     TFileName fileName;
       
   182 
       
   183     TParse tp;
       
   184     tp.Set( KMuiuBitmapFile, &KDC_APP_BITMAP_DIR, NULL );
       
   185     fileName.Copy( tp.FullName() );
       
   186 
       
   187     AppendIconsL( fileName, icons );
       
   188 
       
   189     reinterpret_cast<CAknSingleGraphicStyleListBox*>( listBox )->
       
   190         ItemDrawer()->ColumnData()->SetIconArray( icons );
       
   191 
       
   192 
       
   193     CleanupStack::Pop( ); // icons
       
   194     CleanupStack::PopAndDestroy();// emptyText
       
   195 
       
   196 	}
       
   197 
       
   198 // ----------------------------------------------------------------------------
       
   199 // CImapSubscriptionDialog::PostLayoutDynInitL
       
   200 // ----------------------------------------------------------------------------
       
   201 void CImapSubscriptionDialog::PostLayoutDynInitL()
       
   202 	{
       
   203     //nothing here
       
   204     }
       
   205 
       
   206 // ----------------------------------------------------------------------------
       
   207 // CImapSubscriptionDialog::SynchroniseFoldersL
       
   208 // ----------------------------------------------------------------------------
       
   209 void CImapSubscriptionDialog::SynchroniseFoldersL()
       
   210 	{
       
   211 	CreateOperationL(ERefreshFolderList);
       
   212 	}
       
   213 
       
   214 // ----------------------------------------------------------------------------
       
   215 // CImapSubscriptionDialog::CreateOperationL
       
   216 // ----------------------------------------------------------------------------
       
   217 void CImapSubscriptionDialog::CreateOperationL(TInt aButtonId)
       
   218 	{
       
   219 	CMsvEntrySelection* mySelection=new (ELeave) CMsvEntrySelection;
       
   220 	CleanupStack::PushL(mySelection);
       
   221 	TBuf8<1> dummyParam;
       
   222 
       
   223     CMsvOperation* op = NULL;
       
   224     CMuiuOperationWait* wait = CMuiuOperationWait::NewLC();
       
   225 
       
   226 	switch(aButtonId)
       
   227 		{
       
   228 	case EFolderSubscribe:
       
   229 	case EFolderUnsubscribe:
       
   230         {
       
   231         //check for child / parent folders for question "want to subscribe/unsub whole tree branch?"
       
   232         //eat softkey commands. If user would leave while subscription process in progress, it will crash.
       
   233         //Only for subscribe operations. We need to be able to cancel folder list update operation
       
   234         CAknInputBlock::NewLC();
       
   235 
       
   236         HandleSubscriptionL( aButtonId, *mySelection );
       
   237 
       
   238         TInt cnt = mySelection->Count();
       
   239         if( cnt > 1 )
       
   240             {
       
   241             //if more than one folder, we need to do a batch of subscribe commands
       
   242             op = iMtmUi->InvokeAsyncFunctionL( KIMAP4MTMStartBatch,
       
   243 			*mySelection, wait->iStatus, dummyParam);
       
   244             wait->Start();
       
   245             delete op;
       
   246             op = NULL;
       
   247 
       
   248             TInt i;
       
   249             for( i=0; i<cnt; i++ )
       
   250                 {
       
   251                 op = iMtmUi->InvokeAsyncFunctionL(
       
   252 			        (aButtonId==EFolderSubscribe?KIMAP4MTMLocalSubscribe:KIMAP4MTMLocalUnsubscribe),
       
   253 			        *mySelection, wait->iStatus, dummyParam);
       
   254                 wait->Start();
       
   255                 delete op;
       
   256                 op = NULL;
       
   257 
       
   258 
       
   259                 if( mySelection->Count() > 1 )
       
   260                     {
       
   261                     //don't delete the last one, otherwise next invokeasyncfunction crashes, there has to
       
   262                     //be something in the passed entry selection.
       
   263                     mySelection->Delete(0);//always delete the first item, so next will be the first
       
   264                     }
       
   265 
       
   266                 }
       
   267             op = iMtmUi->InvokeAsyncFunctionL( KIMAP4MTMEndBatch,
       
   268 			*mySelection, wait->iStatus, dummyParam);
       
   269             }//if
       
   270         else
       
   271             {
       
   272             //for only selected folder, no batch needed
       
   273             op = iMtmUi->InvokeAsyncFunctionL(
       
   274 			    (aButtonId==EFolderSubscribe?KIMAP4MTMLocalSubscribe:KIMAP4MTMLocalUnsubscribe),
       
   275 			    *mySelection, wait->iStatus, dummyParam);
       
   276             }
       
   277 
       
   278         }
       
   279 		break;
       
   280 
       
   281 	case ERefreshFolderList:
       
   282 
       
   283         mySelection->AppendL(iServiceId);
       
   284 		op = iMtmUi->InvokeAsyncFunctionL(
       
   285 			KImpiMTMRefreshFolderList, *mySelection, wait->iStatus ,dummyParam);
       
   286 
       
   287 		break;
       
   288 
       
   289 	default:
       
   290 		break;
       
   291 		};
       
   292 
       
   293     wait->Start();
       
   294     delete op;
       
   295     op = NULL;
       
   296 
       
   297 
       
   298     if( aButtonId == EFolderSubscribe || aButtonId == EFolderUnsubscribe )
       
   299         {
       
   300         CleanupStack::PopAndDestroy(); //CAknInputBlock
       
   301         }
       
   302 
       
   303 	CleanupStack::PopAndDestroy(2);	// CSI: 47 # wait, mySelection
       
   304 	}
       
   305 
       
   306 // ----------------------------------------------------------------------------
       
   307 // CImapSubscriptionDialog::OkToExitL
       
   308 // ----------------------------------------------------------------------------
       
   309 TBool CImapSubscriptionDialog::OkToExitL(TInt aButtonId)
       
   310 	{
       
   311 	TBool exit=EFalse;
       
   312     CEikColumnListBox* listBox = 
       
   313         static_cast<CEikColumnListBox*>( Control( EImasCIDFolderListBox ) );
       
   314     const TInt oldCount = listBox->Model()->NumberOfItems();
       
   315 	switch(aButtonId)
       
   316 		{
       
   317         case EAknSoftkeyOptions:
       
   318             return CAknDialog::OkToExitL( EAknSoftkeyOptions );
       
   319 
       
   320 		case EFolderSubscribe:
       
   321 		case EFolderUnsubscribe:
       
   322             iSubscriptionsChanged = ETrue;
       
   323             CreateOperationL( aButtonId );
       
   324             break;
       
   325 
       
   326         case EAknSoftkeyClose:
       
   327             //invokaa folder update jos ollaan online, tai promptaa connectia?
       
   328             if( iSubscriptionsChanged )
       
   329                 {
       
   330                 //HandleRefreshL();
       
   331                 }
       
   332             exit = ETrue;
       
   333             break;
       
   334 
       
   335         case EFolderOpenFolder:
       
   336             // open folder which has subfolders...
       
   337 	        iContextEntry->SetEntryL(CurrentFolderL());
       
   338             if ( ContextHasChildFolders() )
       
   339                 {
       
   340                 iOpenedFolderId = CurrentFolderL();
       
   341                 iFolderListArray->ChangeFolderL( CurrentFolderL() );
       
   342                 if ( listBox->Model()->NumberOfItems() )
       
   343                     {
       
   344                     listBox->SetTopItemIndex( 0 );
       
   345                     listBox->SetCurrentItemIndex( 0 );
       
   346                     }
       
   347                 if ( oldCount > listBox->Model()->NumberOfItems() )
       
   348                     {
       
   349                     listBox->HandleItemRemovalL();
       
   350                     }
       
   351                 else
       
   352                     {
       
   353                     listBox->HandleItemAdditionL();
       
   354                     }
       
   355                 }
       
   356             break;
       
   357 
       
   358         case EAknSoftkeyBack:
       
   359             {
       
   360             if( iContextEntry->Entry().iType == KUidMsvServiceEntry )
       
   361                 {
       
   362                 //Only exit if current folder is service
       
   363                 exit = ETrue;
       
   364                 break;
       
   365                 }
       
   366 
       
   367             TMsvId folderToBeSelected = iOpenedFolderId;
       
   368 	        iContextEntry->SetEntryL( iOpenedFolderId );
       
   369 
       
   370             iOpenedFolderId = iContextEntry->Entry().Parent();
       
   371             iFolderListArray->ChangeFolderL( iOpenedFolderId );
       
   372 	        iContextEntry->SetEntryL( iOpenedFolderId );
       
   373 
       
   374             if ( oldCount > listBox->Model()->NumberOfItems() )
       
   375                 {
       
   376                 listBox->HandleItemRemovalL();
       
   377                 }
       
   378             else
       
   379                 {
       
   380                 listBox->HandleItemAdditionL();
       
   381                 }
       
   382 
       
   383             TInt loop;
       
   384             for ( loop = iContextEntry->Count()-1; loop >= 0; loop-- )
       
   385                 {
       
   386                 if ( folderToBeSelected == (*iContextEntry)[loop].Id() )
       
   387                     {
       
   388                     break;
       
   389                     }
       
   390                 }
       
   391             if ( loop < 0 )
       
   392                 {
       
   393                 loop = 0;
       
   394                 }
       
   395             listBox->SetTopItemIndex( ( loop > KImumFolderArrayVisibleItems ? loop - KImumFolderArrayVisibleItems : 0 ) );
       
   396             listBox->SetCurrentItemIndex( loop );
       
   397             }
       
   398             break;
       
   399 
       
   400         case EEikCmdExit:
       
   401             exit = ETrue;
       
   402             iShouldExit = ETrue;//pass exit to settings dialog
       
   403             break;
       
   404 
       
   405 	default:
       
   406 		break;
       
   407 		};
       
   408 	listBox->DrawNow();
       
   409 	return exit;
       
   410 	}
       
   411 
       
   412 
       
   413 // ----------------------------------------------------------------------------
       
   414 // CImapSubscriptionDialog::ContextHasChildFolders
       
   415 // ----------------------------------------------------------------------------
       
   416 TBool CImapSubscriptionDialog::ContextHasChildFolders() const
       
   417 	{
       
   418 	TInt index=iContextEntry->Count()-1;
       
   419 	while (index>=0 && (*iContextEntry)[index].iType!=KUidMsvFolderEntry)
       
   420 		index--;
       
   421 
       
   422 	if(index!=-1)
       
   423 		return ETrue;
       
   424 
       
   425 	return EFalse;
       
   426 	}
       
   427 
       
   428 // ----------------------------------------------------------------------------
       
   429 // CImapSubscriptionDialog::HandleSessionEventL
       
   430 // ----------------------------------------------------------------------------
       
   431 void CImapSubscriptionDialog::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* /*aArg2*/, TAny* /*aArg3*/)
       
   432 	{
       
   433 	if(aEvent!=EMsvEntriesChanged)
       
   434 		return;
       
   435 
       
   436     CMsvEntrySelection& sel=*static_cast<CMsvEntrySelection*>(aArg1);
       
   437 	if(sel.Find(CurrentFolderL())!=KErrNotFound)
       
   438         {
       
   439 		CEikColumnListBox* listBox = static_cast<CEikColumnListBox*>(Control(EImasCIDFolderListBox));
       
   440         listBox->DrawNow();
       
   441         }
       
   442 	}
       
   443 
       
   444 
       
   445 // ----------------------------------------------------------------------------
       
   446 // CImapSubscriptionDialog::DisplayFinalProgressDialog
       
   447 // ----------------------------------------------------------------------------
       
   448 void CImapSubscriptionDialog::DisplayFinalProgressDialog(CMsvOperation& aCompletedOperation) const
       
   449 	{
       
   450 	__ASSERT_DEBUG(!aCompletedOperation.IsActive(), Panic(EIMAP4MtmUiOperationStillActive));
       
   451 	iMtmUi->DisplayProgressSummary(aCompletedOperation.FinalProgress());
       
   452 	}
       
   453 
       
   454 // ----------------------------------------------------------------------------
       
   455 // CImapSubscriptionDialog::AppendIconsL
       
   456 // ----------------------------------------------------------------------------
       
   457 void CImapSubscriptionDialog::AppendIconsL(
       
   458     TFileName& aFileName,
       
   459     CArrayPtrFlat<CGulIcon>* aIcons)
       
   460     {
       
   461 
       
   462     MAknsSkinInstance* skins = AknsUtils::SkinInstance();
       
   463     TAknsItemID id;
       
   464     CFbsBitmap* bitmap;
       
   465     CFbsBitmap* bitmapMask;
       
   466     TBool found = ETrue;
       
   467 
       
   468 
       
   469     for( TInt i=EMbmMuiuQgn_prop_folder_small; i<EMbmMuiuQgn_prop_folder_sub_unsubs_new+1; i++ )
       
   470         {
       
   471         CGulIcon* icon = NULL;
       
   472         found = ETrue;
       
   473         switch( i )
       
   474             {
       
   475             case EMbmMuiuQgn_prop_folder_small:
       
   476                 id.Set( KAknsIIDQgnPropFolderSmall );
       
   477                 break;
       
   478             case EMbmMuiuQgn_prop_folder_subs:
       
   479                 id.Set( KAknsIIDQgnPropFolderSubs );
       
   480                 break;
       
   481             case EMbmMuiuQgn_prop_folder_subs_new:
       
   482                 id.Set( KAknsIIDQgnPropFolderSubsNew );
       
   483                 break;
       
   484             case EMbmMuiuQgn_prop_folder_sub_small:
       
   485                 id.Set( KAknsIIDQgnPropFolderSubSmall );
       
   486                 break;
       
   487             case EMbmMuiuQgn_prop_folder_sub_subs:
       
   488                 id.Set( KAknsIIDQgnPropFolderSubSubs );
       
   489                 break;
       
   490             case EMbmMuiuQgn_prop_folder_sub_subs_new:
       
   491                 id.Set( KAknsIIDQgnPropFolderSubSubsNew );
       
   492                 break;
       
   493             case EMbmMuiuQgn_prop_folder_unsubs_new:
       
   494                 id.Set( KAknsIIDQgnPropFolderUnsubsNew );
       
   495                 break;
       
   496             case EMbmMuiuQgn_prop_folder_sub_unsubs_new:
       
   497                 id.Set( KAknsIIDQgnPropFolderSubUnsubsNew );
       
   498                 break;
       
   499             default:
       
   500                 found = EFalse;
       
   501                 break;
       
   502             }
       
   503 
       
   504 
       
   505         if( found )
       
   506             {
       
   507             AknsUtils::CreateIconL( skins, id, bitmap,
       
   508                 bitmapMask, aFileName, i, i + 1 );
       
   509 
       
   510             icon = CGulIcon::NewL( bitmap, bitmapMask );
       
   511 
       
   512             // ownership of bitmap and mask transfered to icon
       
   513             CleanupStack::PushL( icon );
       
   514             aIcons->AppendL( icon );
       
   515             CleanupStack::Pop( icon ); // icon
       
   516             }
       
   517         }
       
   518 
       
   519     }
       
   520 
       
   521 // ----------------------------------------------------------------------------
       
   522 // CImapSubscriptionDialog::HandleListBoxEventL
       
   523 // ----------------------------------------------------------------------------
       
   524 void CImapSubscriptionDialog::HandleListBoxEventL(
       
   525     CEikListBox* /*aListBox*/,
       
   526     TListBoxEvent aEventType )
       
   527     {
       
   528     switch ( aEventType )
       
   529         {
       
   530         case EEventEnterKeyPressed:
       
   531         case EEventItemDoubleClicked:
       
   532             {
       
   533             TMsvId prevEntry = iContextEntry->Entry().Id();
       
   534 
       
   535             CEikColumnListBox* listBox =
       
   536                 static_cast<CEikColumnListBox*>(
       
   537                     Control( EImasCIDFolderListBox ) );
       
   538             iContextEntry->SetEntryL( CurrentFolderL() );
       
   539 
       
   540             if ( listBox->Model()->NumberOfItems() &&
       
   541                  ContextHasChildFolders() )
       
   542                 {
       
   543                 //if has childs, open folder
       
   544                 OkToExitL( EFolderOpenFolder );
       
   545                 }
       
   546             else
       
   547                 {
       
   548                 //if not, change subs state of the folder
       
   549                 const TMsvEmailEntry emailEntry( iContextEntry->Entry() );
       
   550 
       
   551                 if ( MuiuEmailTools::IsInbox( iSession, emailEntry ) )
       
   552                     {
       
   553                     //inbox cannot be changed
       
   554                     iContextEntry->SetEntryL( prevEntry );
       
   555                     return;
       
   556                     }
       
   557 
       
   558                 if ( emailEntry.LocalSubscription() )
       
   559                     {
       
   560                     iSubscriptionsChanged = ETrue;
       
   561         		    CreateOperationL( EFolderUnsubscribe );
       
   562                     }
       
   563                 else
       
   564                     {
       
   565                     iSubscriptionsChanged = ETrue;
       
   566         		    CreateOperationL( EFolderSubscribe );
       
   567                     }
       
   568                 listBox->DrawNow();
       
   569                 iContextEntry->SetEntryL( prevEntry );
       
   570                 }
       
   571             // Don't set prev entry here because we're opening it,
       
   572             // so it will be valid
       
   573             }
       
   574 
       
   575         break;
       
   576             default:
       
   577         break;
       
   578         }
       
   579     }
       
   580 
       
   581 // ----------------------------------------------------------------------------
       
   582 // CImapSubscriptionDialog::CurrentFolderL
       
   583 // ----------------------------------------------------------------------------
       
   584 TMsvId CImapSubscriptionDialog::CurrentFolderL()
       
   585     {
       
   586     CEikColumnListBox* listBox = static_cast<CEikColumnListBox*>(Control(EImasCIDFolderListBox));
       
   587     TInt curIndex = listBox->CurrentItemIndex();
       
   588 
       
   589     return ( curIndex == KErrNotFound ? curIndex : iFolderListArray->Id( curIndex ) );
       
   590     }
       
   591 
       
   592 // ----------------------------------------------------------------------------
       
   593 // CImapSubscriptionDialog::OfferKeyEventL
       
   594 // ----------------------------------------------------------------------------
       
   595 TKeyResponse CImapSubscriptionDialog::OfferKeyEventL(
       
   596     const TKeyEvent& aKeyEvent,
       
   597     TEventCode aType )
       
   598     {
       
   599     return CAknDialog::OfferKeyEventL( aKeyEvent, aType );
       
   600     }
       
   601 
       
   602 
       
   603 
       
   604 // ----------------------------------------------------------------------------
       
   605 // CImapSubscriptionDialog::DynInitMenuPaneL
       
   606 // ----------------------------------------------------------------------------
       
   607 void CImapSubscriptionDialog::DynInitMenuPaneL(
       
   608     TInt aResourceId,
       
   609     CEikMenuPane* aMenuPane )
       
   610     {
       
   611     TMsvId folderId = CurrentFolderL();
       
   612     if ( folderId == KErrNotFound )
       
   613         {
       
   614         if ( aResourceId == R_IMAS_SETTINGS_SUBSCRIBE_MENUPANE )
       
   615             {
       
   616             aMenuPane->SetItemDimmed( EFolderOpenFolder, ETrue );
       
   617             aMenuPane->SetItemDimmed( EFolderSubscribe, ETrue );
       
   618             aMenuPane->SetItemDimmed( EFolderUnsubscribe, ETrue );
       
   619             }
       
   620         }
       
   621     else
       
   622         {
       
   623         TMsvId prevEntry = iContextEntry->Entry().Id();
       
   624         iContextEntry->SetEntryL( folderId );
       
   625 	    const TMsvEmailEntry emailEntry( iContextEntry->Entry() );
       
   626 
       
   627         if ( aResourceId == R_IMAS_SETTINGS_SUBSCRIBE_MENUPANE )
       
   628             {
       
   629             if ( !ContextHasChildFolders() )
       
   630                 {
       
   631                 aMenuPane->SetItemDimmed( EFolderOpenFolder, ETrue );
       
   632                 }
       
   633             if ( MuiuEmailTools::IsInbox( iSession, emailEntry ) )
       
   634                 {
       
   635                 //inbox subscription cannot be changed
       
   636                 aMenuPane->SetItemDimmed( EFolderSubscribe, ETrue );
       
   637                 aMenuPane->SetItemDimmed( EFolderUnsubscribe, ETrue );
       
   638                 }
       
   639             else
       
   640                 {
       
   641                 aMenuPane->SetItemDimmed(
       
   642                     EFolderSubscribe, emailEntry.LocalSubscription() );
       
   643                 aMenuPane->SetItemDimmed(
       
   644                     EFolderUnsubscribe, !emailEntry.LocalSubscription() );
       
   645                 }
       
   646             aMenuPane->SetItemDimmed( EAknCmdHelp,
       
   647                 !FeatureManager::FeatureSupported( KFeatureIdHelp ) );
       
   648             }
       
   649 
       
   650         iContextEntry->SetEntryL( prevEntry );
       
   651         }
       
   652 
       
   653     }
       
   654 
       
   655 // ----------------------------------------------------------------------------
       
   656 // CImapSubscriptionDialog::ProcessCommandL
       
   657 // ----------------------------------------------------------------------------
       
   658 void CImapSubscriptionDialog::ProcessCommandL( TInt aCommandId )
       
   659     {
       
   660     if ( MenuShowing() )
       
   661         {
       
   662         HideMenu();
       
   663         }
       
   664 
       
   665     CAknDialog::ProcessCommandL(aCommandId);
       
   666     switch( aCommandId )
       
   667         {
       
   668         case EFolderOpenFolder:
       
   669             OkToExitL( EFolderOpenFolder );
       
   670             break;
       
   671         case EFolderSubscribe:
       
   672 		case EFolderUnsubscribe:
       
   673             {
       
   674             TMsvEntry tEntry;
       
   675             TMsvId service;
       
   676             iSession.GetEntry( CurrentFolderL(), service, tEntry );
       
   677             const TMsvEmailEntry emailEntry( tEntry );
       
   678 
       
   679 
       
   680             iSubscriptionsChanged = ETrue;
       
   681             CreateOperationL( aCommandId );
       
   682             }
       
   683             break;
       
   684         case ERefreshFolderList:
       
   685             SynchroniseFoldersL();
       
   686             iFolderListArray->RefreshFolderListArrayL();
       
   687             break;
       
   688 
       
   689         case EAknCmdHelp:
       
   690             {
       
   691             LaunchHelpL();
       
   692             break;
       
   693             }
       
   694 
       
   695         case EEikCmdExit:
       
   696             TryExitL( aCommandId );
       
   697             break;
       
   698         default:
       
   699             iEikonEnv->EikAppUi()->HandleCommandL(aCommandId);
       
   700             break;
       
   701         }
       
   702     }
       
   703 
       
   704 // ----------------------------------------------------------------------------
       
   705 // CImapSubscriptionDialog::LaunchHelpL
       
   706 // Launch help using context
       
   707 //
       
   708 // ----------------------------------------------------------------------------
       
   709 void CImapSubscriptionDialog::LaunchHelpL()
       
   710     {
       
   711     CCoeAppUi* appUi = static_cast<CCoeAppUi*>( ControlEnv()->AppUi() );
       
   712     HlpLauncher::LaunchHelpApplicationL( iEikonEnv->WsSession(), appUi->AppHelpContextL() );
       
   713     }
       
   714 
       
   715 // ----------------------------------------------------------------------------
       
   716 // CImapSubscriptionDialog::GetHelpContext
       
   717 // Returns helpcontext as aContext
       
   718 //
       
   719 // ----------------------------------------------------------------------------
       
   720 void CImapSubscriptionDialog::GetHelpContext
       
   721         (TCoeHelpContext& aContext) const
       
   722     {
       
   723     const TUid KUidMce ={0x100058C5};
       
   724     aContext.iMajor = KUidMce;
       
   725     aContext.iContext = KMCE_HLP_SETT_MAILFOLDER_UPD;
       
   726     }                   
       
   727 
       
   728 // ----------------------------------------------------------------------------
       
   729 // CImapSubscriptionDialog::HandleSubscriptionL( TInt aSubType )
       
   730 // ----------------------------------------------------------------------------
       
   731 void CImapSubscriptionDialog::HandleSubscriptionL( TInt aSubType, CMsvEntrySelection& aSelection )
       
   732     {
       
   733     TMsvId current = CurrentFolderL();
       
   734 
       
   735     if( iFolderListArray->ContextHasChildren( current ) )
       
   736         {
       
   737         TMsvSelectionOrdering selectionOrdering(KMsvGroupByType, EMsvSortByDetails, ETrue);
       
   738 
       
   739         //CMsvEntry is used this way because GetContextChildrenL is recursive. Otherwise
       
   740         //CMsvEntry objects would be created several times before the first one is deleted
       
   741         //which consumes lots of memory. Now we just set new CMsvEntry over this one several times.
       
   742         CMsvEntry* entry = CMsvEntry::NewL(iSession, current, selectionOrdering);
       
   743         CleanupStack::PushL( entry );
       
   744         CMsvEntrySelection* sel = iFolderListArray->GetContextChildrenL( current, *entry );
       
   745         CleanupStack::PopAndDestroy();//entry
       
   746 
       
   747         CleanupStack::PushL( sel );
       
   748 
       
   749         TBool allFolders = CheckAreAllFoldersIncludedL( aSubType, sel );
       
   750 
       
   751         CAknQueryDialog* subsQuery = new(ELeave) CAknQueryDialog;
       
   752 
       
   753         switch( aSubType )
       
   754             {
       
   755             case EFolderSubscribe:
       
   756                 {
       
   757                 //subscribe also all sub folders?
       
   758                 subsQuery->PrepareLC( R_IMUM_DO_SUBS_FOR_SUBFOLDERS );
       
   759                 }
       
   760                 break;
       
   761 	        case EFolderUnsubscribe:
       
   762                 {
       
   763                 //unsubscribe also all sub folders?
       
   764                 subsQuery->PrepareLC( R_IMUM_DO_UNSUBS_FOR_SUBFOLDERS );
       
   765                 }
       
   766                 break;
       
   767             default:
       
   768                 User::Leave( KErrNotSupported );
       
   769                 break;
       
   770             }//switch
       
   771 
       
   772 
       
   773         TInt ret = 1;//by default do all subfolders
       
   774 
       
   775         //if all subfolders already subbed / unsubbed, no need for query
       
   776         if( !allFolders )
       
   777             {
       
   778             ret = subsQuery->RunLD();
       
   779             }
       
   780         else
       
   781             {
       
   782             CleanupStack::PopAndDestroy();//subsQuery
       
   783             }
       
   784 
       
   785         if( ret )
       
   786             {
       
   787             //append all sub folders
       
   788 
       
   789             TInt count = sel->Count();
       
   790 
       
   791             //first append current
       
   792             aSelection.AppendL( current );
       
   793 
       
   794             //then the children
       
   795             TInt i;
       
   796             for( i=0; i<count; i++ )
       
   797                 {
       
   798                 aSelection.AppendL( sel->At(i) );
       
   799                 }
       
   800             }
       
   801         else
       
   802             {
       
   803             //only selected
       
   804             aSelection.AppendL( current );
       
   805             }
       
   806 
       
   807         CleanupStack::PopAndDestroy();//sel
       
   808         }//if
       
   809     else
       
   810         {
       
   811         //no children, append only current
       
   812         aSelection.AppendL( current );
       
   813         }
       
   814     }
       
   815 
       
   816 // ----------------------------------------------------------------------------
       
   817 // CImapSubscriptionDialog::CheckAreAllFoldersIncludedL
       
   818 // ----------------------------------------------------------------------------
       
   819 TBool CImapSubscriptionDialog::CheckAreAllFoldersIncludedL( TInt aSubType, CMsvEntrySelection* sel )
       
   820     {
       
   821     TMsvEntry tEntry;
       
   822     TMsvId service;
       
   823 
       
   824     TInt count = sel->Count();
       
   825     TInt i;
       
   826     TInt result = 0;
       
   827 
       
   828     for( i=0; i<count; i++ )
       
   829         {
       
   830         iSession.GetEntry( sel->At(i), service, tEntry );
       
   831         TMsvEmailEntry emailEntry( tEntry );
       
   832 
       
   833         if( aSubType == EFolderSubscribe )
       
   834             {
       
   835             //count all subscribed entries
       
   836             if( emailEntry.LocalSubscription() )
       
   837                 {
       
   838                 result++;
       
   839                 }
       
   840             }
       
   841         else
       
   842             {
       
   843             //count all unsubscribed entries
       
   844             if( !emailEntry.LocalSubscription() )
       
   845                 {
       
   846                 result++;
       
   847                 }
       
   848             }
       
   849         }
       
   850 
       
   851     if( count == result )
       
   852         {
       
   853         return ETrue;
       
   854         }
       
   855 
       
   856     return EFalse;
       
   857 
       
   858     }
       
   859 
       
   860 ///////////////////////////////////////
       
   861 
       
   862 // ----------------------------------------------------------------------------
       
   863 // CImapFolderListArray::NewL
       
   864 // ----------------------------------------------------------------------------
       
   865 CImapFolderListArray* CImapFolderListArray::NewL(
       
   866     CImumInternalApi& aMailboxApi,
       
   867     TMsvId aMailboxId )
       
   868 	{
       
   869 	CImapFolderListArray* self=new (ELeave) CImapFolderListArray(
       
   870         aMsvSession, aMailboxId );
       
   871 	CleanupStack::PushL(self);
       
   872 	self->ConstructL();
       
   873 	CleanupStack::Pop();
       
   874 	return self;
       
   875 	}
       
   876 
       
   877 // ----------------------------------------------------------------------------
       
   878 // CImapFolderListArray::~CImapFolderListArray
       
   879 // ----------------------------------------------------------------------------
       
   880 CImapFolderListArray::~CImapFolderListArray()
       
   881     {
       
   882     FeatureManager::UnInitializeLib();
       
   883 
       
   884     delete iText;
       
   885     delete iEntry;
       
   886     }
       
   887 
       
   888 // ----------------------------------------------------------------------------
       
   889 // CImapFolderListArray::ConstructL
       
   890 // ----------------------------------------------------------------------------
       
   891 void CImapFolderListArray::ConstructL()
       
   892     {
       
   893     RefreshFolderListArrayL();
       
   894 
       
   895     iText = HBufC::NewL( KImumITextLength );
       
   896     }
       
   897 
       
   898 // ----------------------------------------------------------------------------
       
   899 // CImapFolderListArray::CImapFolderListArray
       
   900 // ----------------------------------------------------------------------------
       
   901 CImapFolderListArray::CImapFolderListArray(
       
   902     CImumInternalApi& aMailboxApi,
       
   903     TMsvId aMailboxId)
       
   904     :
       
   905     iSession( aMsvSession ),
       
   906     iMailboxId( aMailboxId )
       
   907     {
       
   908     }
       
   909 
       
   910 // ----------------------------------------------------------------------------
       
   911 // CImapFolderListArray::MdcaPoint
       
   912 // ----------------------------------------------------------------------------
       
   913 TPtrC CImapFolderListArray::MdcaPoint( TInt aIndex ) const
       
   914     {
       
   915 	const TMsvEmailEntry emailEntry( (*iEntry)[aIndex] );
       
   916 
       
   917     TPtr tempText = iText->Des();
       
   918     tempText.Zero();
       
   919 
       
   920     TInt iconIndex = 0;
       
   921     if ( ContextHasChildFolders( emailEntry.Id() ) )
       
   922         {
       
   923         iconIndex = ( emailEntry.LocalSubscription() ? EImumFolderSubfoldersSubbed : EImumFolderSubfoldersUnsubbed );
       
   924         }
       
   925     else
       
   926         {
       
   927         iconIndex = ( emailEntry.LocalSubscription() ? EImumFolderSingleSubbed : EImumFolderSingleUnsubbed );
       
   928         }
       
   929     tempText.AppendNum( iconIndex );
       
   930     tempText.Append( KColumnListSeparator );
       
   931     tempText.Append( emailEntry.iDetails.Left( KImasImailServiceNameLength ) );
       
   932 
       
   933     return tempText;
       
   934     }
       
   935 
       
   936 // ----------------------------------------------------------------------------
       
   937 // CImapFolderListArray::MdcaCount
       
   938 // ----------------------------------------------------------------------------
       
   939 TInt CImapFolderListArray::MdcaCount() const
       
   940     {
       
   941     return iCount;
       
   942     }
       
   943 
       
   944 // ----------------------------------------------------------------------------
       
   945 // CImapFolderListArray::Id
       
   946 // ----------------------------------------------------------------------------
       
   947 TMsvId CImapFolderListArray::Id( TInt aIndex )
       
   948     {
       
   949     return (*iEntry)[aIndex].Id();
       
   950     }
       
   951 
       
   952 // ----------------------------------------------------------------------------
       
   953 // CImapFolderListArray::ChangeFolderL
       
   954 // ----------------------------------------------------------------------------
       
   955 void CImapFolderListArray::ChangeFolderL( TMsvId aFolderId )
       
   956     {
       
   957     iEntry->SetEntryL( aFolderId );
       
   958 
       
   959     CMsvEntrySelection* sel = iEntry->ChildrenWithTypeL( KUidMsvFolderEntry );
       
   960     iCount = sel->Count();
       
   961     delete sel;
       
   962     sel = NULL;
       
   963 
       
   964     }
       
   965 
       
   966 // ----------------------------------------------------------------------------
       
   967 // CImapFolderListArray::ContextHasChildren
       
   968 // ----------------------------------------------------------------------------
       
   969 TBool CImapFolderListArray::ContextHasChildren( TMsvId aId ) const
       
   970     {
       
   971     return ContextHasChildFolders( aId );
       
   972     }
       
   973 
       
   974 // ----------------------------------------------------------------------------
       
   975 // CImapFolderListArray::ContextHasChildFolders
       
   976 // ----------------------------------------------------------------------------
       
   977 TBool CImapFolderListArray::ContextHasChildFolders( TMsvId aId ) const
       
   978 	{
       
   979     TBool hasFolders = EFalse;
       
   980     TRAPD( error, hasFolders = DoContextHasChildFoldersL( aId ) );
       
   981     if ( error != KErrNone )
       
   982         {
       
   983         return EFalse;
       
   984         }
       
   985     return hasFolders;
       
   986     }
       
   987 
       
   988 // ----------------------------------------------------------------------------
       
   989 // CImapFolderListArray::DoContextHasChildFoldersL
       
   990 // ----------------------------------------------------------------------------
       
   991 TBool CImapFolderListArray::DoContextHasChildFoldersL( TMsvId aId ) const
       
   992 	{
       
   993 	TMsvSelectionOrdering selectionOrdering(KMsvGroupByType, EMsvSortByDetails, ETrue);
       
   994 	CMsvEntry* entry = CMsvEntry::NewL(iSession, aId, selectionOrdering);
       
   995     CleanupStack::PushL( entry );
       
   996 	TInt index=entry->Count()-1;
       
   997 	while (index>=0 && (*entry)[index].iType!=KUidMsvFolderEntry)
       
   998         {
       
   999 		index--;
       
  1000         }
       
  1001 
       
  1002     CleanupStack::PopAndDestroy( entry );
       
  1003 
       
  1004 	if(index!=-1)
       
  1005         {
       
  1006 		return ETrue;
       
  1007         }
       
  1008 
       
  1009 	return EFalse;
       
  1010 	}
       
  1011 
       
  1012 // ----------------------------------------------------------------------------
       
  1013 // CImapFolderListArray::GetContextChildrenL
       
  1014 // ----------------------------------------------------------------------------
       
  1015 CMsvEntrySelection* CImapFolderListArray::GetContextChildrenL( TMsvId aId, CMsvEntry& aEntry ) const
       
  1016     {
       
  1017     TMsvSelectionOrdering selectionOrdering(KMsvGroupByType, EMsvSortByDetails, ETrue);
       
  1018     aEntry.SetEntryL( aId );// = CMsvEntry::NewL(iSession, aId, selectionOrdering);
       
  1019     aEntry.SetSortTypeL( selectionOrdering );
       
  1020     CMsvEntrySelection* sel = aEntry.ChildrenWithTypeL( KUidMsvFolderEntry );
       
  1021     CleanupStack::PushL( sel );
       
  1022 
       
  1023     TInt i;
       
  1024     TInt cnt = sel->Count();
       
  1025 
       
  1026     for( i=0; i<cnt; i++ )
       
  1027         {
       
  1028         if( ContextHasChildren( sel->At(i) ) )
       
  1029             {
       
  1030             //Note! recursion
       
  1031             CMsvEntrySelection* selection = GetContextChildrenL( sel->At(i), aEntry );
       
  1032             CleanupStack::PushL( selection );
       
  1033             TInt c = selection->Count();
       
  1034 
       
  1035             TInt j;
       
  1036             for( j=0; j<c; j++ )
       
  1037                 {
       
  1038                 sel->AppendL( selection->At(j) );
       
  1039                 }
       
  1040             CleanupStack::PopAndDestroy();//selection
       
  1041             }
       
  1042         }
       
  1043 
       
  1044 
       
  1045     CleanupStack::Pop();//sel
       
  1046     return sel;
       
  1047     }
       
  1048 
       
  1049 // ----------------------------------------------------------------------------
       
  1050 // CImapFolderListArray::FoldersUpdated
       
  1051 // ----------------------------------------------------------------------------
       
  1052 TBool CImapFolderListArray::FoldersUpdated() const
       
  1053     {
       
  1054     return ( iEntry->Count() > 0 ? ETrue : EFalse );
       
  1055     }
       
  1056 
       
  1057 // ----------------------------------------------------------------------------
       
  1058 // CImapFolderListArray::RefreshFolderListArrayL
       
  1059 // ----------------------------------------------------------------------------
       
  1060 void CImapFolderListArray::RefreshFolderListArrayL()
       
  1061     {
       
  1062     if( iEntry )
       
  1063         {
       
  1064         delete iEntry;
       
  1065         iEntry = NULL;
       
  1066         }
       
  1067 
       
  1068     iEntry = iSession.GetEntryL( iMailboxId );
       
  1069   	const TMsvSelectionOrdering originalOrdering=iEntry->SortType();
       
  1070 	TMsvSelectionOrdering newOrdering=originalOrdering;
       
  1071 	newOrdering.SetShowInvisibleEntries(ETrue);
       
  1072     newOrdering.SetGroupByType(ETrue);
       
  1073     newOrdering.SetSorting( EMsvSortByDetails );
       
  1074 	iEntry->SetSortTypeL(newOrdering);
       
  1075 
       
  1076     CMsvEntrySelection* sel = iEntry->ChildrenWithTypeL( KUidMsvFolderEntry );
       
  1077     iCount = sel->Count();
       
  1078     delete sel;
       
  1079     sel = NULL;
       
  1080     }
       
  1081 
       
  1082 // End of File