diff --git a/mozilla/mailnews/imap/src/nsImapMoveCoalescer.cpp b/mozilla/mailnews/imap/src/nsImapMoveCoalescer.cpp new file mode 100644 index 00000000000..dd01a555bca --- /dev/null +++ b/mozilla/mailnews/imap/src/nsImapMoveCoalescer.cpp @@ -0,0 +1,110 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1999 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#include "msgCore.h" +#include "nsImapMoveCoalescer.h" +#include "nsMsgKeySet.h" +#include "nsImapService.h" + +static NS_DEFINE_CID(kCImapService, NS_IMAPSERVICE_CID); + +nsImapMoveCoalescer::nsImapMoveCoalescer(nsImapMailFolder *sourceFolder) +{ + m_sourceFolder = sourceFolder; + if (sourceFolder) + NS_ADDREF(sourceFolder); +} + +nsImapMoveCoalescer::~nsImapMoveCoalescer() +{ + NS_IF_RELEASE(m_sourceFolder); + for (PRInt32 i = 0; i < m_sourceKeySets.Count(); i++) + { + nsMsgKeySet *keys = (nsMsgKeySet *) m_sourceKeySets.ElementAt(i); + delete keys; + } +} + +nsresult nsImapMoveCoalescer::AddMove(nsIMsgFolder *folder, nsMsgKey key) +{ + if (!m_destFolders) + NS_NewISupportsArray(getter_AddRefs(m_destFolders)); + if (m_destFolders) + { + PRInt32 folderIndex = m_destFolders->IndexOf(folder); + nsMsgKeySet *keysToAdd; + if (folderIndex >= 0) + { + keysToAdd = (nsMsgKeySet *) m_sourceKeySets.ElementAt(folderIndex); + } + else + { + m_destFolders->AppendElement(folder); + keysToAdd = nsMsgKeySet::Create(); + if (!keysToAdd) + return NS_ERROR_OUT_OF_MEMORY; + + m_sourceKeySets.AppendElement(keysToAdd); + } + if (keysToAdd) + keysToAdd->Add(key); + return NS_OK; + } + else + return NS_ERROR_OUT_OF_MEMORY; + +} + +nsresult nsImapMoveCoalescer::PlaybackMoves(nsIEventQueue *eventQueue) +{ + PRUint32 numFolders; + nsresult rv = NS_OK; + + m_destFolders->Count(&numFolders); + for (PRUint32 i = 0; i < numFolders; i++) + { + nsCOMPtr myISupports = getter_AddRefs(m_destFolders->ElementAt(i)); + nsCOMPtr destFolder(do_QueryInterface(myISupports)); + NS_WITH_SERVICE(nsIImapService, imapService, kCImapService, &rv); + if (NS_SUCCEEDED(rv) && imapService) + { + nsMsgKeySet *keysToAdd = (nsMsgKeySet *) m_sourceKeySets.ElementAt(i); + if (keysToAdd) + { + char *messageIds = keysToAdd->Output(); + if (messageIds) + { + nsCOMPtr myISupports = do_QueryInterface((nsIMsgImapMailFolder *) m_sourceFolder, &rv); + nsCOMPtr urlListener(do_QueryInterface(myISupports)); + rv = imapService->OnlineMessageCopy(eventQueue, + m_sourceFolder, messageIds, + destFolder, PR_TRUE, PR_TRUE, + urlListener, nsnull); + delete [] messageIds; + } + else + { + rv = NS_ERROR_OUT_OF_MEMORY; + break; + } + } + } + } + return rv; +} + diff --git a/mozilla/mailnews/imap/src/nsImapMoveCoalescer.h b/mozilla/mailnews/imap/src/nsImapMoveCoalescer.h new file mode 100644 index 00000000000..f74308bdf5e --- /dev/null +++ b/mozilla/mailnews/imap/src/nsImapMoveCoalescer.h @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998, 1999 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#ifndef _nsImapMoveCoalescer_H +#define _nsImapMoveCoalescer_H + +#include "msgCore.h" +#include "nsImapMailFolder.h" +#include "nsISupportsArray.h" +#include "nsCOMPtr.h" + +// imap move coalescer class - in order to keep nsImapMailFolder from growing like Topsy +// Logically, we want to keep track of an nsMsgKeyArray per nsIMsgFolder, and then +// be able to retrieve them one by one and play back the moves. +// This utility class will be used by both the filter code and the offline playback code, +// to avoid multiple moves to the same folder. + +#include "nsISupportsArray.h" + +class nsImapMoveCoalescer : public nsISupportsArray +{ +public: + nsImapMoveCoalescer(nsImapMailFolder *sourceFolder); + virtual ~nsImapMoveCoalescer(); + + nsresult AddMove(nsIMsgFolder *folder, nsMsgKey key); + nsresult PlaybackMoves(nsIEventQueue *eventQueue); +protected: + // m_sourceKeySets and m_destFolders are parallel arrays. + nsVoidArray m_sourceKeySets; + nsCOMPtr m_destFolders; + nsImapMailFolder *m_sourceFolder; +}; + +#endif // _nsImapMoveCoalescer_H +