email/imap4mtm/imapsyncmanager/src/cimapopsyncsubs.cpp
changeset 0 72b543305e3a
equal deleted inserted replaced
-1:000000000000 0:72b543305e3a
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 // imapopsyncsubs.cpp
       
    15 // 
       
    16 //
       
    17 
       
    18 #include "cimapsyncmanager.h"
       
    19 #include "cimapopsyncsubs.h"
       
    20 #include "cimapsettings.h"
       
    21 #include "cimapfolder.h"
       
    22 
       
    23 CImapOpSyncSubs* CImapOpSyncSubs::NewL(CMsvServerEntry& aEntry, CImapSession& aSession, CImapSyncManager& aSyncMan,CImapSettings& aImapSettings)
       
    24 	{
       
    25 	CImapOpSyncSubs* self=NewLC(aEntry, aSession, aSyncMan, aImapSettings);
       
    26 	CleanupStack::Pop();
       
    27 
       
    28 	return self;
       
    29 	}
       
    30 
       
    31 CImapOpSyncSubs* CImapOpSyncSubs::NewLC(CMsvServerEntry& aEntry, CImapSession& aSession,CImapSyncManager& aSyncMan,CImapSettings& aImapSettings)
       
    32 	{
       
    33 	CImapOpSyncSubs* self=new (ELeave) CImapOpSyncSubs(aEntry, aSession, aSyncMan, aImapSettings);
       
    34 	CleanupStack::PushL(self);
       
    35 
       
    36 	// Non-trivial constructor
       
    37 	self->ConstructL();
       
    38 
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 CImapOpSyncSubs::~CImapOpSyncSubs()
       
    43 	{
       
    44 	iImapListFolderInfo.ResetAndDestroy();
       
    45 	}
       
    46 
       
    47 void CImapOpSyncSubs::ConstructL()
       
    48 	{
       
    49 	// We're an active object...
       
    50 	CActiveScheduler::Add(this);
       
    51 
       
    52 	}
       
    53 
       
    54 void CImapOpSyncSubs::SynchroniseSubscriptionsL(TRequestStatus& aStatus)
       
    55 	{
       
    56 	// Do we need to know what folders are subscribed remotely?
       
    57 	if (iImapSettings.Synchronise() == EUseLocal && 
       
    58 		(iImapSettings.Subscribe() == EUpdateNeither || iImapSettings.Subscribe() == EUpdateLocal))
       
    59 		{
       
    60 		// Just update any remote subscriptions
       
    61 		iNextSyncAction = CImapSyncManager::EUpdateRemoteSubscription;
       
    62 		Queue(aStatus);
       
    63 		CompleteSelf();
       
    64 		SetActive();
       
    65 		}
       
    66 	else
       
    67 		{
       
    68 		// Update our list of remotely subscribed folders
       
    69 		// First, we need to go through the entire service, resetting all
       
    70 		// the 'remote subscribed' flags.
       
    71 		ResetSubscriptionFlagsL(iServiceId);
       
    72 
       
    73 		// Build a buffer to quote the folder path (it may be necessary)
       
    74 		HBufC16* path=HBufC16::NewLC(iImapSettings.FolderPath().Length()+2);
       
    75 		path->Des().Copy(iImapSettings.FolderPath());
       
    76 		if (iImapSettings.FolderPath().Length())
       
    77 			{
       
    78 			path->Des().Append(iImapSettings.PathSeparator());
       
    79 			}
       
    80 
       
    81 		path->Des().Append(KStarChar);
       
    82 		
       
    83 		// Quote it
       
    84 		CImapFolder::DoQuoteL(path);
       
    85 
       
    86 		// List this tree
       
    87 		iNextSyncAction = CImapSyncManager::EProcessRemoteSubscription;
       
    88 		iServerEntry.SetEntry(iServiceId);
       
    89 		TMsvEmailEntry entry = iServerEntry.Entry();
       
    90 		
       
    91 		iSession.LsubL(iStatus, KNullDesC, path->Des(), iImapListFolderInfo);
       
    92 
       
    93 		Queue(aStatus);
       
    94 		SetActive();
       
    95 		CleanupStack::PopAndDestroy(path);
       
    96 		}
       
    97 	}
       
    98 
       
    99 CImapOpSyncSubs::CImapOpSyncSubs(CMsvServerEntry& aEntry, CImapSession& aSession, CImapSyncManager& aSyncMan,CImapSettings& aImapSettings)
       
   100 : CImapSyncOperation(aEntry, aSession, aSyncMan, aImapSettings, EPriorityStandard)
       
   101 	{
       
   102 	}
       
   103 
       
   104 	
       
   105 /** 
       
   106 Set the current entry context on the Message server
       
   107 
       
   108 @param aId
       
   109 The entry id to set in the Message server
       
   110 @leave KErrNotFound if the entry does not exist or KErrLocked if the entry is locked.
       
   111 */
       
   112 void CImapOpSyncSubs::SetEntryL(const TMsvId aId)
       
   113 	{
       
   114 	User::LeaveIfError(iServerEntry.SetEntry(aId));
       
   115 	}
       
   116 
       
   117 /**
       
   118 Sets the context's index entry to the specified values on the Message server
       
   119 
       
   120 @param  aEntry
       
   121 The new details for the entry.
       
   122 @leave KErrAccessDenied - the entry is read only (deleted entry, standard folder, or locked); 
       
   123 KErrNotSupported - aEntry is invalid or the ID specified in aEntry is not the same as the context ID
       
   124 or no context has been set for the object; 
       
   125 KErrNoMemory - a memory allocation failed. 
       
   126 */
       
   127 void CImapOpSyncSubs::ChangeEntryL(const TMsvEntry& aEntry)
       
   128 	{
       
   129 	User::LeaveIfError(iServerEntry.ChangeEntry(aEntry));
       
   130 	}
       
   131 
       
   132 /**
       
   133 Return a list of all child entries of the currently selected entry node.
       
   134 
       
   135 @param  aSelection
       
   136 The ids for the found child entries will be put into this parameter
       
   137 */
       
   138 void CImapOpSyncSubs::GetChildrenL(CMsvEntrySelection& aSelection)
       
   139 	{
       
   140 	User::LeaveIfError(iServerEntry.GetChildren(aSelection));
       
   141 	}
       
   142 
       
   143 /**
       
   144 Reset subscription flags for all children, and recurse into folders
       
   145 
       
   146 @param aFolder
       
   147 Folder from which to start the flags resetting
       
   148 */
       
   149 void CImapOpSyncSubs::ResetSubscriptionFlagsL(const TMsvId aFolder)
       
   150 	{
       
   151 	// Do this one
       
   152 	SetEntryL(aFolder);
       
   153 	TMsvEmailEntry entry = iServerEntry.Entry();
       
   154 
       
   155 	// A folder or service? If not, return
       
   156 	if (entry.iType!=KUidMsvServiceEntry &&
       
   157 		entry.iType!=KUidMsvFolderEntry)
       
   158 		{
       
   159 		return;
       
   160 		}
       
   161 
       
   162 	// Reset flag if needed
       
   163 	if (entry.Subscribed())
       
   164 		{
       
   165 		// Reset flag and save
       
   166 		entry.SetSubscribed(EFalse);
       
   167 		ChangeEntryL(entry);
       
   168 		}
       
   169 
       
   170 	// Any children?
       
   171 	CMsvEntrySelection *children = new (ELeave) CMsvEntrySelection;
       
   172 	CleanupStack::PushL(children);
       
   173 	GetChildrenL(*children);
       
   174 	if (children->Count())
       
   175 		{
       
   176 		// Do each in turn
       
   177 		for(TInt child = 0; child<children->Count(); ++child)
       
   178 			{
       
   179 			ResetSubscriptionFlagsL((*children)[child]);
       
   180 			}
       
   181 		}
       
   182 	CleanupStack::PopAndDestroy();
       
   183 	}
       
   184 	
       
   185 
       
   186 /**
       
   187 Called when async child completes with >=KErrNone
       
   188 */
       
   189 void CImapOpSyncSubs::DoRunL()
       
   190 	{
       
   191 	// Finish if any server errors.
       
   192 	if (iStatus.Int()!=KErrNone)
       
   193 		{
       
   194 		Complete(iStatus.Int());
       
   195 		}
       
   196 
       
   197 	switch(iNextSyncAction)
       
   198 		{
       
   199 	case CImapSyncManager::EProcessRemoteSubscription:
       
   200 		{
       
   201 		// Call returned from the LSUB command. Now process the list.
       
   202 		// Update folder tree
       
   203 		TInt folderscount = iImapListFolderInfo.Count();
       
   204 		CImapListFolderInfo* folder;
       
   205 		
       
   206 		for(TInt loop = 0;loop < folderscount; ++loop)
       
   207 			{
       
   208 			folder = iImapListFolderInfo[loop];
       
   209 			iSyncMan.AddSubscribedFolderFromInfoL(folder, iSession.LogId());
       
   210 			}
       
   211 		iSyncMan.CreateLocalFolderListL();
       
   212 
       
   213 		// Now do the remote subscription
       
   214 		iNextSyncAction = CImapSyncManager::EUpdateRemoteSubscription;
       
   215 		CompleteSelf();
       
   216 		SetActive();
       
   217 		}
       
   218 		break;
       
   219 	case CImapSyncManager::EUpdateRemoteSubscription:
       
   220 		{
       
   221 		// Process the subscribed and unsubscribed list from the SyncManager.
       
   222 		// This should be done after checking the online subscription list.
       
   223 
       
   224 		RArray<TUint>* unsubscribeList = iSyncMan.GetUnSubscribedFolderToDoList();
       
   225 		RArray<TUint>* subscribeList = iSyncMan.GetSubscribedFolderToDoList();
       
   226 
       
   227 		// Any subscription to do?
       
   228 		if (subscribeList->Count())
       
   229 			{
       
   230 			// Take it off the head
       
   231 			TMsvId folderid = (*subscribeList)[0];
       
   232 			subscribeList->Remove(0);
       
   233 
       
   234 			// Subscribe to it
       
   235 			CImapFolder* tempfolder = iSyncMan.GetTempFolderL(folderid);
       
   236 			CleanupStack::PushL(tempfolder);
       
   237 			iSession.SubscribeL(iStatus, tempfolder->FullFolderPathL());
       
   238 			CleanupStack::PopAndDestroy(tempfolder);
       
   239 			
       
   240 			// set the subscribed flag for the folder.
       
   241 			SetEntryL(folderid);
       
   242 			TMsvEmailEntry entry = iServerEntry.Entry();
       
   243 			entry.SetSubscribed(ETrue);
       
   244 			ChangeEntryL(entry);
       
   245 			
       
   246 			SetActive();
       
   247 			}
       
   248 		// ...or unsubscription?
       
   249 		else if (unsubscribeList->Count())
       
   250 			{
       
   251 			// Take it off the head
       
   252 			TMsvId folderid = (*unsubscribeList)[0];
       
   253 			unsubscribeList->Remove(0);
       
   254 
       
   255 			// Unsubscribe from it
       
   256 			CImapFolder* tempfolder = iSyncMan.GetTempFolderL(folderid);
       
   257 			CleanupStack::PushL(tempfolder);
       
   258 			iSession.UnsubscribeL(iStatus, tempfolder->FullFolderPathL());
       
   259 			CleanupStack::PopAndDestroy(tempfolder);
       
   260 			
       
   261 			// clear the subscribed flag for the folder.
       
   262 			SetEntryL(folderid);
       
   263 			TMsvEmailEntry entry = iServerEntry.Entry();
       
   264 			entry.SetSubscribed(EFalse);
       
   265 			ChangeEntryL(entry);
       
   266 			
       
   267 			SetActive();
       
   268 			}
       
   269 		else
       
   270 			{
       
   271 			// All done.
       
   272 			iNextSyncAction = CImapSyncManager::ENotSyncing;
       
   273 			Complete(iStatus.Int());
       
   274 			}
       
   275 		}
       
   276 		break;
       
   277 	default:
       
   278 	break;
       
   279 		}
       
   280 	}
       
   281 
       
   282 /**
       
   283 Cancels any outstanding asynchronous service requests.
       
   284 */
       
   285 void CImapOpSyncSubs::DoCancel()
       
   286 	{
       
   287 	iSession.Cancel();
       
   288 	CMsgActive::DoCancel();
       
   289 	}
       
   290 
       
   291 /**
       
   292 
       
   293 */
       
   294 void CImapOpSyncSubs::DoComplete(TInt& /*aStatus*/)
       
   295 	{
       
   296 	}
       
   297 
       
   298 void CImapOpSyncSubs::CompleteSelf()
       
   299 	{
       
   300 	TRequestStatus* status = &iStatus;
       
   301 	iStatus = KRequestPending;
       
   302 	User::RequestComplete(status, KErrNone);
       
   303 	}
       
   304 	
       
   305 
       
   306