Back to index

lightning-sunbird  0.9+nobinonly
nsCopyMessageStreamListener.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCopyMessageStreamListener.h"
00040 #include "nsIMsgMailNewsUrl.h"
00041 #include "nsIMailboxUrl.h"
00042 #include "nsIMsgHdr.h"
00043 #include "nsIRDFService.h"
00044 #include "nsIRDFNode.h"
00045 #include "nsRDFCID.h"
00046 #include "nsIMsgImapMailFolder.h"
00047 #include "nsXPIDLString.h"
00048 #include "nsIMsgMessageService.h"
00049 #include "nsMsgUtils.h"
00050 #include "netCore.h"
00051 
00052 NS_IMPL_THREADSAFE_ADDREF(nsCopyMessageStreamListener)
00053 NS_IMPL_THREADSAFE_RELEASE(nsCopyMessageStreamListener)
00054 
00055 NS_INTERFACE_MAP_BEGIN(nsCopyMessageStreamListener)
00056    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamListener)
00057    NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
00058    NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
00059    NS_INTERFACE_MAP_ENTRY(nsICopyMessageStreamListener)
00060 NS_INTERFACE_MAP_END_THREADSAFE
00061 
00062 static nsresult GetMessage(nsIURI *aURL, nsIMsgDBHdr **message)
00063 {
00064   NS_ENSURE_ARG_POINTER(message);
00065 
00066        nsCOMPtr<nsIMsgMessageUrl> uriURL;
00067        nsresult rv;
00068 
00069        //Need to get message we are about to copy
00070        uriURL = do_QueryInterface(aURL, &rv);
00071        if(NS_FAILED(rv))
00072               return rv;
00073 
00074   // get the uri.  first try and use the original message spec
00075   // if that fails, use the spec of nsIURI that we're called with
00076   nsXPIDLCString uri;
00077   rv = uriURL->GetOriginalSpec(getter_Copies(uri));
00078   if (NS_FAILED(rv) || uri.IsEmpty()) {
00079     rv = uriURL->GetUri(getter_Copies(uri));
00080          NS_ENSURE_SUCCESS(rv,rv);
00081   }
00082 
00083   nsCOMPtr <nsIMsgMessageService> msgMessageService;
00084   rv = GetMessageServiceFromURI(uri, getter_AddRefs(msgMessageService));
00085   NS_ENSURE_SUCCESS(rv,rv);
00086   if (!msgMessageService) 
00087     return NS_ERROR_FAILURE;
00088 
00089   rv = msgMessageService->MessageURIToMsgHdr(uri.get(), message);
00090   return rv; 
00091 }
00092 
00093 nsCopyMessageStreamListener::nsCopyMessageStreamListener()
00094 {
00095 }
00096 
00097 nsCopyMessageStreamListener::~nsCopyMessageStreamListener()
00098 {
00099        //All member variables are nsCOMPtr's.
00100 }
00101 
00102 NS_IMETHODIMP nsCopyMessageStreamListener::Init(nsIMsgFolder *srcFolder, nsICopyMessageListener *destination, nsISupports *listenerData)
00103 {
00104        mSrcFolder = srcFolder;
00105        mDestination = destination;
00106        mListenerData = listenerData;
00107        return NS_OK;
00108 }
00109 
00110 NS_IMETHODIMP nsCopyMessageStreamListener::StartMessage()
00111 {
00112        if (mDestination)
00113               mDestination->StartMessage();
00114 
00115        return NS_OK;
00116 }
00117 
00118 NS_IMETHODIMP nsCopyMessageStreamListener::EndMessage(nsMsgKey key)
00119 {
00120        if (mDestination)
00121               mDestination->EndMessage(key);
00122 
00123        return NS_OK;
00124 }
00125 
00126 
00127 NS_IMETHODIMP nsCopyMessageStreamListener::OnDataAvailable(nsIRequest * /* request */, nsISupports *ctxt, nsIInputStream *aIStream, PRUint32 sourceOffset, PRUint32 aLength)
00128 {
00129        nsresult rv;
00130        rv = mDestination->CopyData(aIStream, aLength);
00131        return rv;
00132 }
00133 
00134 NS_IMETHODIMP nsCopyMessageStreamListener::OnStartRequest(nsIRequest * request, nsISupports *ctxt)
00135 {
00136        nsCOMPtr<nsIMsgDBHdr> message;
00137        nsresult rv = NS_OK;
00138        nsCOMPtr<nsIURI> uri = do_QueryInterface(ctxt, &rv);
00139 
00140   NS_ASSERTION(NS_SUCCEEDED(rv), "ahah...someone didn't pass in the expected context!!!");
00141        
00142        if (NS_SUCCEEDED(rv))
00143               rv = GetMessage(uri, getter_AddRefs(message));
00144        if(NS_SUCCEEDED(rv))
00145               rv = mDestination->BeginCopy(message);
00146 
00147   NS_ENSURE_SUCCESS(rv, rv);
00148        return rv;
00149 }
00150 
00151 NS_IMETHODIMP nsCopyMessageStreamListener::EndCopy(nsISupports *url, nsresult aStatus)
00152 {
00153        nsresult rv = NS_OK;
00154        nsCOMPtr<nsIURI> uri = do_QueryInterface(url, &rv);
00155 
00156        if (NS_FAILED(rv)) return rv;
00157        PRBool copySucceeded = (aStatus == NS_BINDING_SUCCEEDED);
00158        rv = mDestination->EndCopy(copySucceeded);
00159        //If this is a move and we finished the copy, delete the old message.
00160        if(NS_SUCCEEDED(rv))
00161        {
00162               PRBool moveMessage = PR_FALSE;
00163 
00164               nsCOMPtr<nsIMsgMailNewsUrl> mailURL(do_QueryInterface(uri));
00165               if(mailURL)
00166                      rv = mailURL->IsUrlType(nsIMsgMailNewsUrl::eMove, &moveMessage);
00167 
00168               if(NS_FAILED(rv))
00169                      moveMessage = PR_FALSE;
00170 
00171               // OK, this is wrong if we're moving to an imap folder, for example. This really says that
00172               // we were able to pull the message from the source, NOT that we were able to
00173               // put it in the destination!
00174               if(moveMessage)
00175               {
00176                      // don't do this if we're moving to an imap folder - that's handled elsewhere.
00177                      nsCOMPtr <nsIMsgImapMailFolder> destImap = do_QueryInterface(mDestination);
00178                      if (!destImap)
00179                      {
00180         // if the destination is a local folder, it will handle the delete from the source in EndMove
00181 //                          rv = DeleteMessage(uri, mSrcFolder);
00182 //                          if(NS_SUCCEEDED(rv))
00183                                    rv = mDestination->EndMove(copySucceeded);
00184                      }
00185               }
00186        }
00187        //Even if the above actions failed we probably still want to return NS_OK.  There should probably
00188        //be some error dialog if either the copy or delete failed.
00189        return NS_OK;
00190 }
00191 
00192 NS_IMETHODIMP nsCopyMessageStreamListener::OnStopRequest(nsIRequest* request, nsISupports *ctxt, nsresult aStatus)
00193 {
00194   return EndCopy(ctxt, aStatus);
00195 }
00196