Back to index

lightning-sunbird  0.9+nobinonly
nsMsgComposeProgress.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; 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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Jean-Francois Ducarroz <ducarroz@netscape.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 "nsMsgComposeProgress.h"
00040 
00041 #include "nsIScriptGlobalObject.h"
00042 #include "nsIBaseWindow.h"
00043 #include "nsIDocShell.h"
00044 #include "nsIDocShellTreeItem.h"
00045 #include "nsIDocShellTreeOwner.h"
00046 #include "nsISupportsArray.h"
00047 #include "nsXPCOM.h"
00048 #include "nsISupportsPrimitives.h"
00049 #include "nsIComponentManager.h"
00050 
00051 NS_IMPL_ISUPPORTS1(nsMsgComposeProgress, nsIMsgComposeProgress)
00052 
00053 nsMsgComposeProgress::nsMsgComposeProgress()
00054 {
00055   m_closeProgress = PR_FALSE;
00056   m_processCanceled = PR_FALSE;
00057   m_pendingStateFlags = -1;
00058   m_pendingStateValue = 0;
00059 }
00060 
00061 nsMsgComposeProgress::~nsMsgComposeProgress()
00062 {
00063   (void)ReleaseListeners();
00064 }
00065 
00066 /* void OpenProgress (in nsIDOMWindowInternal parent, in wstring subject, in boolean itsASaveOperation); */
00067 NS_IMETHODIMP
00068 nsMsgComposeProgress::OpenProgress(nsIDOMWindowInternal *parent,
00069                                    const PRUnichar *subject,
00070                                    PRBool itsASaveOperation)
00071 {
00072   nsresult rv = NS_ERROR_FAILURE;
00073   
00074   if (m_dialog)
00075     return NS_ERROR_ALREADY_INITIALIZED;
00076 
00077   if (parent)
00078   {
00079      // Open the dialog.
00080     nsCOMPtr<nsISupportsArray> array;
00081     rv = NS_NewISupportsArray(getter_AddRefs(array));
00082     NS_ENSURE_SUCCESS(rv, rv);
00083 
00084     nsCOMPtr<nsISupportsString> strptr =
00085       do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
00086     NS_ENSURE_SUCCESS(rv, rv);
00087 
00088     strptr->SetData(nsDependentString(subject));
00089 
00090     array->AppendElement(strptr);
00091 
00092     nsCOMPtr<nsISupportsPRBool> boolptr =
00093       do_CreateInstance(NS_SUPPORTS_PRBOOL_CONTRACTID, &rv);
00094     NS_ENSURE_SUCCESS(rv, rv);
00095 
00096     boolptr->SetData(itsASaveOperation);
00097 
00098     array->AppendElement(boolptr);
00099 
00100     nsCOMPtr<nsISupportsInterfacePointer> ifptr =
00101       do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
00102     NS_ENSURE_SUCCESS(rv, rv);
00103 
00104     ifptr->SetData(this);
00105     ifptr->SetDataIID(&NS_GET_IID(nsIMsgComposeProgress));
00106 
00107     array->AppendElement(ifptr);
00108 
00109     nsCOMPtr<nsIDOMWindow> newWindow;
00110     rv = parent->OpenDialog(NS_LITERAL_STRING("chrome://messenger/content/messengercompose/sendProgress.xul"),
00111                             NS_LITERAL_STRING("_blank"),
00112                             NS_LITERAL_STRING("chrome,titlebar,dependent"),
00113                             array, getter_AddRefs(newWindow));
00114   }
00115   return rv;
00116 }
00117 
00118 /* void CloseProgress (); */
00119 NS_IMETHODIMP nsMsgComposeProgress::CloseProgress(PRBool forceClose)
00120 {
00121   m_closeProgress = PR_TRUE;
00122   return OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, forceClose);
00123 }
00124 
00125 /* nsIPrompt GetPrompter (); */
00126 NS_IMETHODIMP nsMsgComposeProgress::GetPrompter(nsIPrompt **_retval)
00127 {
00128   NS_ENSURE_ARG_POINTER(_retval);
00129   *_retval = nsnull;
00130 
00131   if (! m_closeProgress && m_dialog)
00132     return m_dialog->GetPrompter(_retval);
00133     
00134   return NS_ERROR_FAILURE;
00135 }
00136 
00137 /* attribute boolean processCanceledByUser; */
00138 NS_IMETHODIMP nsMsgComposeProgress::GetProcessCanceledByUser(PRBool *aProcessCanceledByUser)
00139 {
00140   NS_ENSURE_ARG_POINTER(aProcessCanceledByUser);
00141   *aProcessCanceledByUser = m_processCanceled;
00142   return NS_OK;
00143 }
00144 NS_IMETHODIMP nsMsgComposeProgress::SetProcessCanceledByUser(PRBool aProcessCanceledByUser)
00145 {
00146   m_processCanceled = aProcessCanceledByUser;
00147   OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, PR_FALSE);
00148   return NS_OK;
00149 }
00150 
00151 /* void RegisterListener (in nsIWebProgressListener listener); */
00152 NS_IMETHODIMP nsMsgComposeProgress::RegisterListener(nsIWebProgressListener * listener)
00153 {
00154   nsresult rv = NS_OK;
00155   
00156   if (!listener) //Nothing to do with a null listener!
00157     return NS_OK;
00158   
00159   if (!m_listenerList)
00160     rv = NS_NewISupportsArray(getter_AddRefs(m_listenerList));
00161   
00162   if (NS_SUCCEEDED(rv) && m_listenerList)
00163   {
00164     m_listenerList->AppendElement(listener);
00165     if (m_closeProgress || m_processCanceled)
00166       listener->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, 0);
00167     else
00168     {
00169       listener->OnStatusChange(nsnull, nsnull, 0, m_pendingStatus.get());
00170       if (m_pendingStateFlags != -1)
00171         listener->OnStateChange(nsnull, nsnull, m_pendingStateFlags, m_pendingStateValue);
00172     }
00173   }
00174     
00175   return NS_OK;
00176 }
00177 
00178 /* void UnregisterListener (in nsIWebProgressListener listener); */
00179 NS_IMETHODIMP nsMsgComposeProgress::UnregisterListener(nsIWebProgressListener *listener)
00180 {
00181   if (m_listenerList && listener)
00182     m_listenerList->RemoveElement(listener);
00183   
00184   return NS_OK;
00185 }
00186 
00187 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aStateFlags, in unsigned long aStatus); */
00188 NS_IMETHODIMP nsMsgComposeProgress::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aStateFlags, PRUint32 aStatus)
00189 {
00190   nsresult rv = NS_OK;
00191 
00192   m_pendingStateFlags = aStateFlags;
00193   m_pendingStateValue = aStatus;
00194   
00195   if (m_listenerList)
00196   {
00197     PRUint32 count = 0;
00198     PRInt32 i;
00199 
00200     rv = m_listenerList->Count(&count);
00201     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00202   
00203     nsCOMPtr<nsISupports> aSupports;
00204     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00205     for (i = count - 1; i >= 0; i --)
00206     {
00207       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00208       aProgressListener = do_QueryInterface(aSupports);
00209       if (aProgressListener)
00210         aProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00211     }
00212   }
00213   
00214   return NS_OK;
00215 }
00216 
00217 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00218 NS_IMETHODIMP nsMsgComposeProgress::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00219 {
00220   nsresult rv = NS_OK;
00221 
00222   if (m_listenerList)
00223   {
00224     PRUint32 count = 0;
00225     PRInt32 i;
00226 
00227     rv = m_listenerList->Count(&count);
00228     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00229   
00230     nsCOMPtr<nsISupports> aSupports;
00231     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00232     for (i = count - 1; i >= 0; i --)
00233     {
00234       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00235       aProgressListener = do_QueryInterface(aSupports);
00236       if (aProgressListener)
00237         aProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
00238     }
00239   }
00240   
00241   return NS_OK;
00242 }
00243 
00244 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00245 NS_IMETHODIMP nsMsgComposeProgress::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00246 {
00247     return NS_OK;
00248 }
00249 
00250 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00251 NS_IMETHODIMP nsMsgComposeProgress::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00252 {
00253   nsresult rv;
00254 
00255   m_pendingStatus = aMessage;
00256   if (m_listenerList)
00257   {
00258     PRUint32 count = 0;
00259     PRInt32 i;
00260 
00261     rv = m_listenerList->Count(&count);
00262     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00263   
00264     nsCOMPtr<nsISupports> aSupports;
00265     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00266     for (i = count - 1; i >= 0; i --)
00267     {
00268       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00269       aProgressListener = do_QueryInterface(aSupports);
00270       if (aProgressListener)
00271         aProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00272     }
00273   }
00274   
00275   return NS_OK;
00276 }
00277 
00278 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00279 NS_IMETHODIMP nsMsgComposeProgress::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00280 {
00281     return NS_OK;
00282 }
00283 
00284 nsresult nsMsgComposeProgress::ReleaseListeners()
00285 {
00286   nsresult rv = NS_OK;
00287 
00288   if (m_listenerList)
00289   {
00290     PRUint32 count;
00291     PRInt32 i;
00292 
00293     rv = m_listenerList->Count(&count);
00294     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00295     if (NS_SUCCEEDED(rv))    
00296       for (i = count - 1; i >= 0; i --)
00297         m_listenerList->RemoveElementAt(i);
00298   }
00299   
00300   return rv;
00301 }
00302