Back to index

lightning-sunbird  0.9+nobinonly
nsMsgProgress.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 "nsMsgProgress.h"
00040 
00041 #include "nsIBaseWindow.h"
00042 #include "nsISupportsArray.h"
00043 #include "nsXPCOM.h"
00044 #include "nsISupportsPrimitives.h"
00045 #include "nsIComponentManager.h"
00046 #include "nsNetError.h"
00047 #include "nsInt64.h"
00048 
00049 NS_IMPL_THREADSAFE_ADDREF(nsMsgProgress)
00050 NS_IMPL_THREADSAFE_RELEASE(nsMsgProgress)
00051 
00052 NS_INTERFACE_MAP_BEGIN(nsMsgProgress)
00053    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIMsgStatusFeedback)
00054    NS_INTERFACE_MAP_ENTRY(nsIMsgProgress)
00055    NS_INTERFACE_MAP_ENTRY(nsIMsgStatusFeedback)
00056    NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
00057    NS_INTERFACE_MAP_ENTRY(nsIProgressEventSink)
00058 NS_INTERFACE_MAP_END_THREADSAFE
00059 
00060 
00061 nsMsgProgress::nsMsgProgress()
00062 {
00063   m_closeProgress = PR_FALSE;
00064   m_processCanceled = PR_FALSE;
00065   m_pendingStateFlags = -1;
00066   m_pendingStateValue = 0;
00067 }
00068 
00069 nsMsgProgress::~nsMsgProgress()
00070 {
00071   (void)ReleaseListeners();
00072 }
00073 
00074 /* void openProgressDialog (in nsIDOMWindowInternal parent, in nsIMsgWindow aMsgWindow,
00075   in string dialogURL, in nsISupports parameters); 
00076 */
00077 NS_IMETHODIMP nsMsgProgress::OpenProgressDialog(nsIDOMWindowInternal *parent, nsIMsgWindow *aMsgWindow,
00078                                                 const char *dialogURL,
00079                                                 nsISupports *parameters)
00080 {
00081   nsresult rv = NS_ERROR_FAILURE;
00082 
00083   if (aMsgWindow)
00084   {
00085     SetMsgWindow(aMsgWindow);
00086     aMsgWindow->SetStatusFeedback(this);
00087   }
00088 
00089   if (m_dialog)
00090     return NS_ERROR_ALREADY_INITIALIZED;
00091   
00092   if (!dialogURL || !*dialogURL)
00093     return NS_ERROR_INVALID_ARG;
00094 
00095   if (parent)
00096   {
00097     // Set up window.arguments[0]...
00098     nsCOMPtr<nsISupportsArray> array;
00099     rv = NS_NewISupportsArray(getter_AddRefs(array));
00100     NS_ENSURE_SUCCESS(rv, rv);
00101 
00102     nsCOMPtr<nsISupportsInterfacePointer> ifptr =
00103       do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
00104     NS_ENSURE_SUCCESS(rv, rv);
00105     
00106     ifptr->SetData(NS_STATIC_CAST(nsIMsgProgress*, this));
00107     ifptr->SetDataIID(&NS_GET_IID(nsIMsgProgress));
00108 
00109     array->AppendElement(ifptr);
00110 
00111     array->AppendElement(parameters);
00112 
00113     // Open the dialog.
00114     nsCOMPtr<nsIDOMWindow> newWindow;
00115     rv = parent->OpenDialog(NS_ConvertASCIItoUCS2(dialogURL),
00116                             NS_LITERAL_STRING("_blank"),
00117                             NS_LITERAL_STRING("chrome,titlebar,dependent"),
00118                             array, getter_AddRefs(newWindow));
00119   }
00120 
00121   return rv;
00122 }
00123 
00124 /* void closeProgressDialog (in boolean forceClose); */
00125 NS_IMETHODIMP nsMsgProgress::CloseProgressDialog(PRBool forceClose)
00126 {
00127   m_closeProgress = PR_TRUE;
00128   return OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, forceClose ? NS_ERROR_FAILURE : NS_OK);
00129 }
00130 
00131 /* nsIPrompt GetPrompter (); */
00132 NS_IMETHODIMP nsMsgProgress::GetPrompter(nsIPrompt **_retval)
00133 {
00134   NS_ENSURE_ARG_POINTER(_retval);
00135   *_retval = nsnull;
00136 
00137   if (! m_closeProgress && m_dialog)
00138     return m_dialog->GetPrompter(_retval);
00139     
00140   return NS_ERROR_FAILURE;
00141 }
00142 
00143 /* attribute boolean processCanceledByUser; */
00144 NS_IMETHODIMP nsMsgProgress::GetProcessCanceledByUser(PRBool *aProcessCanceledByUser)
00145 {
00146   NS_ENSURE_ARG_POINTER(aProcessCanceledByUser);
00147   *aProcessCanceledByUser = m_processCanceled;
00148   return NS_OK;
00149 }
00150 NS_IMETHODIMP nsMsgProgress::SetProcessCanceledByUser(PRBool aProcessCanceledByUser)
00151 {
00152   m_processCanceled = aProcessCanceledByUser;
00153   OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, NS_BINDING_ABORTED);
00154   return NS_OK;
00155 }
00156 
00157 /* void RegisterListener (in nsIWebProgressListener listener); */
00158 NS_IMETHODIMP nsMsgProgress::RegisterListener(nsIWebProgressListener * listener)
00159 {
00160   nsresult rv = NS_OK;
00161   
00162   if (!listener) //Nothing to do with a null listener!
00163     return NS_OK;
00164 
00165   if (this == listener) //Check for self-reference (see bug 271700)
00166     return NS_ERROR_INVALID_ARG;
00167   
00168   if (!m_listenerList)
00169     rv = NS_NewISupportsArray(getter_AddRefs(m_listenerList));
00170   
00171   if (NS_SUCCEEDED(rv) && m_listenerList)
00172   {
00173     m_listenerList->AppendElement(listener);
00174     if (m_closeProgress || m_processCanceled)
00175       listener->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, 0);
00176     else
00177     {
00178       listener->OnStatusChange(nsnull, nsnull, 0, m_pendingStatus.get());
00179       if (m_pendingStateFlags != -1)
00180         listener->OnStateChange(nsnull, nsnull, m_pendingStateFlags, m_pendingStateValue);
00181     }
00182   }
00183     
00184   return NS_OK;
00185 }
00186 
00187 /* void UnregisterListener (in nsIWebProgressListener listener); */
00188 NS_IMETHODIMP nsMsgProgress::UnregisterListener(nsIWebProgressListener *listener)
00189 {
00190   if (m_listenerList && listener)
00191     m_listenerList->RemoveElement(listener);
00192   
00193   return NS_OK;
00194 }
00195 
00196 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
00197 NS_IMETHODIMP nsMsgProgress::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00198 {
00199   nsresult rv;
00200 
00201   m_pendingStateFlags = aStateFlags;
00202   m_pendingStateValue = aStatus;
00203   
00204   nsCOMPtr<nsIMsgWindow> msgWindow (do_QueryReferent(m_msgWindow));
00205   if (aStateFlags == nsIWebProgressListener::STATE_STOP && msgWindow && NS_FAILED(aStatus))
00206   {
00207     msgWindow->StopUrls();
00208     msgWindow->SetStatusFeedback(nsnull);
00209   }
00210 
00211   if (m_listenerList)
00212   {
00213     PRUint32 count = 0;
00214     PRInt32 i;
00215 
00216     rv = m_listenerList->Count(&count);
00217     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00218   
00219     nsCOMPtr<nsISupports> aSupports;
00220     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00221     for (i = count - 1; i >= 0; i --)
00222     {
00223       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00224       aProgressListener = do_QueryInterface(aSupports);
00225       if (aProgressListener)
00226         aProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00227     }
00228   }
00229   
00230   return NS_OK;
00231 }
00232 
00233 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00234 NS_IMETHODIMP nsMsgProgress::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00235 {
00236   nsresult rv;
00237 
00238   if (m_listenerList)
00239   {
00240     PRUint32 count = 0;
00241     PRInt32 i;
00242 
00243     rv = m_listenerList->Count(&count);
00244     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00245   
00246     nsCOMPtr<nsISupports> aSupports;
00247     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00248     for (i = count - 1; i >= 0; i --)
00249     {
00250       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00251       aProgressListener = do_QueryInterface(aSupports);
00252       if (aProgressListener)
00253         aProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
00254     }
00255   }
00256   
00257   return NS_OK;
00258 }
00259 
00260 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00261 NS_IMETHODIMP nsMsgProgress::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00262 {
00263     return NS_OK;
00264 }
00265 
00266 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00267 NS_IMETHODIMP nsMsgProgress::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00268 {
00269   nsresult rv;
00270 
00271   if (aMessage && *aMessage)
00272   m_pendingStatus = aMessage;
00273   if (m_listenerList)
00274   {
00275     PRUint32 count = 0;
00276     PRInt32 i;
00277 
00278     rv = m_listenerList->Count(&count);
00279     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00280   
00281     nsCOMPtr<nsISupports> aSupports;
00282     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00283     for (i = count - 1; i >= 0; i --)
00284     {
00285       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00286       aProgressListener = do_QueryInterface(aSupports);
00287       if (aProgressListener)
00288         aProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00289     }
00290   }
00291   
00292   return NS_OK;
00293 }
00294 
00295 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00296 NS_IMETHODIMP nsMsgProgress::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00297 {
00298     return NS_OK;
00299 }
00300 
00301 nsresult nsMsgProgress::ReleaseListeners()
00302 {
00303   nsresult rv = NS_OK;
00304 
00305   if (m_listenerList)
00306   {
00307     PRUint32 count;
00308     PRInt32 i;
00309 
00310     rv = m_listenerList->Count(&count);
00311     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00312     if (NS_SUCCEEDED(rv))    
00313       for (i = count - 1; i >= 0; i --)
00314         m_listenerList->RemoveElementAt(i);
00315   }
00316   
00317   return rv;
00318 }
00319 
00320 NS_IMETHODIMP nsMsgProgress::ShowStatusString(const PRUnichar *status)
00321 {
00322   return OnStatusChange(nsnull, nsnull, NS_OK, status);
00323 }
00324 
00325 NS_IMETHODIMP nsMsgProgress::SetStatusString(const PRUnichar *status)
00326 {
00327   return OnStatusChange(nsnull, nsnull, NS_OK, status);
00328 }
00329 
00330 /* void startMeteors (); */
00331 NS_IMETHODIMP nsMsgProgress::StartMeteors()
00332 {
00333   return NS_ERROR_NOT_IMPLEMENTED;
00334 }
00335 
00336 /* void stopMeteors (); */
00337 NS_IMETHODIMP nsMsgProgress::StopMeteors()
00338 {
00339   return NS_ERROR_NOT_IMPLEMENTED;
00340 }
00341 
00342 /* void showProgress (in long percent); */
00343 NS_IMETHODIMP nsMsgProgress::ShowProgress(PRInt32 percent)
00344 {
00345   return NS_ERROR_NOT_IMPLEMENTED;
00346 }
00347 
00348 /* [noscript] void setDocShell (in nsIDocShell shell, in nsIDOMWindowInternal window); */
00349 NS_IMETHODIMP nsMsgProgress::SetDocShell(nsIDocShell *shell, nsIDOMWindow *window)
00350 {
00351   return NS_ERROR_NOT_IMPLEMENTED;
00352 }
00353 
00354 /* void closeWindow (); */
00355 NS_IMETHODIMP nsMsgProgress::CloseWindow()
00356 {
00357   return NS_ERROR_NOT_IMPLEMENTED;
00358 }
00359 
00360 NS_IMETHODIMP nsMsgProgress::SetMsgWindow(nsIMsgWindow *aMsgWindow)
00361 {
00362   m_msgWindow = do_GetWeakReference(aMsgWindow);
00363   return NS_OK;
00364 }
00365 
00366 NS_IMETHODIMP nsMsgProgress::GetMsgWindow(nsIMsgWindow **aMsgWindow)
00367 {
00368   NS_ENSURE_ARG_POINTER(aMsgWindow);
00369   
00370   if (m_msgWindow) 
00371     CallQueryReferent(m_msgWindow.get(), aMsgWindow);
00372   else 
00373     *aMsgWindow = nsnull;
00374 
00375   return NS_OK;
00376 }
00377 
00378 NS_IMETHODIMP nsMsgProgress::OnProgress(nsIRequest *request, nsISupports* ctxt, 
00379                                           PRUint64 aProgress, PRUint64 aProgressMax)
00380 {
00381   // XXX: What should the nsIWebProgress be?
00382   // XXX: This truncates 64-bit to 32-bit
00383   return OnProgressChange(nsnull, request, nsUint64(aProgress), nsUint64(aProgressMax), 
00384                           nsUint64(aProgress) /* current total progress */, nsUint64(aProgressMax) /* max total progress */);
00385 }
00386 
00387 NS_IMETHODIMP nsMsgProgress::OnStatus(nsIRequest *request, nsISupports* ctxt, 
00388                                             nsresult aStatus, const PRUnichar* aStatusArg)
00389 {
00390   nsresult rv;
00391   nsCOMPtr<nsIStringBundleService> sbs = do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
00392   if (NS_FAILED(rv)) return rv;
00393   nsXPIDLString str;
00394   rv = sbs->FormatStatusMessage(aStatus, aStatusArg, getter_Copies(str));
00395   if (NS_FAILED(rv)) return rv;
00396   nsAutoString msg(NS_STATIC_CAST(const PRUnichar*, str));
00397   return ShowStatusString(msg.get());
00398 }