Back to index

lightning-sunbird  0.9+nobinonly
nsPrintProgress.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  *   Rod Spears <rods@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsPrintProgress.h"
00041 
00042 #include "nsIBaseWindow.h"
00043 #include "nsISupportsArray.h"
00044 #include "nsXPCOM.h"
00045 #include "nsISupportsPrimitives.h"
00046 #include "nsIComponentManager.h"
00047 
00048 NS_IMPL_THREADSAFE_ADDREF(nsPrintProgress)
00049 NS_IMPL_THREADSAFE_RELEASE(nsPrintProgress)
00050 
00051 NS_INTERFACE_MAP_BEGIN(nsPrintProgress)
00052    NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrintStatusFeedback)
00053    NS_INTERFACE_MAP_ENTRY(nsIPrintProgress)
00054    NS_INTERFACE_MAP_ENTRY(nsIPrintStatusFeedback)
00055    NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
00056 NS_INTERFACE_MAP_END_THREADSAFE
00057 
00058 
00059 nsPrintProgress::nsPrintProgress()
00060 {
00061   m_closeProgress = PR_FALSE;
00062   m_processCanceled = PR_FALSE;
00063   m_pendingStateFlags = -1;
00064   m_pendingStateValue = 0;
00065 }
00066 
00067 nsPrintProgress::~nsPrintProgress()
00068 {
00069   (void)ReleaseListeners();
00070 }
00071 
00072 /* void openProgressDialog (in nsIDOMWindowInternal parent, in string dialogURL, in nsISupports parameters); */
00073 NS_IMETHODIMP nsPrintProgress::OpenProgressDialog(nsIDOMWindowInternal *parent,
00074                                                   const char *dialogURL,
00075                                                   nsISupports *parameters, 
00076                                                   nsIObserver *openDialogObserver,
00077                                                   PRBool *notifyOnOpen)
00078 {
00079   *notifyOnOpen = PR_TRUE;
00080   m_observer = openDialogObserver;
00081   nsresult rv = NS_ERROR_FAILURE;
00082   
00083   if (m_dialog)
00084     return NS_ERROR_ALREADY_INITIALIZED;
00085   
00086   if (!dialogURL || !*dialogURL)
00087     return NS_ERROR_INVALID_ARG;
00088 
00089   if (parent)
00090   {
00091     // Set up window.arguments[0]...
00092     nsCOMPtr<nsISupportsArray> array;
00093     rv = NS_NewISupportsArray(getter_AddRefs(array));
00094     NS_ENSURE_SUCCESS(rv, rv);
00095 
00096     nsCOMPtr<nsISupportsInterfacePointer> ifptr =
00097       do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
00098     NS_ENSURE_SUCCESS(rv, rv);
00099     
00100     ifptr->SetData(NS_STATIC_CAST(nsIPrintProgress*, this));
00101     ifptr->SetDataIID(&NS_GET_IID(nsIPrintProgress));
00102 
00103     array->AppendElement(ifptr);
00104 
00105     array->AppendElement(parameters);
00106 
00107     // Open the dialog.
00108     nsCOMPtr<nsIDOMWindow> newWindow;
00109     rv = parent->OpenDialog(NS_ConvertASCIItoUCS2(dialogURL),
00110                             NS_LITERAL_STRING("_blank"),
00111                             NS_LITERAL_STRING("chrome,titlebar,dependent,centerscreen"),
00112                             array, getter_AddRefs(newWindow));
00113   }
00114 
00115   return rv;
00116 }
00117 
00118 /* void closeProgressDialog (in boolean forceClose); */
00119 NS_IMETHODIMP nsPrintProgress::CloseProgressDialog(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 nsPrintProgress::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 nsPrintProgress::GetProcessCanceledByUser(PRBool *aProcessCanceledByUser)
00139 {
00140   NS_ENSURE_ARG_POINTER(aProcessCanceledByUser);
00141   *aProcessCanceledByUser = m_processCanceled;
00142   return NS_OK;
00143 }
00144 NS_IMETHODIMP nsPrintProgress::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 nsPrintProgress::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 nsPrintProgress::UnregisterListener(nsIWebProgressListener *listener)
00180 {
00181   if (m_listenerList && listener)
00182     m_listenerList->RemoveElement(listener);
00183   
00184   return NS_OK;
00185 }
00186 
00187 /* void doneIniting (); */
00188 NS_IMETHODIMP nsPrintProgress::DoneIniting()
00189 {
00190   if (m_observer) {
00191     m_observer->Observe(nsnull, nsnull, nsnull);
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 nsPrintProgress::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00198 {
00199   nsresult rv = NS_OK;
00200 
00201   m_pendingStateFlags = aStateFlags;
00202   m_pendingStateValue = aStatus;
00203   
00204   if (m_listenerList)
00205   {
00206     PRUint32 count;
00207     PRInt32 i;
00208 
00209     rv = m_listenerList->Count(&count);
00210     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00211     if (NS_FAILED(rv))
00212       return rv;
00213   
00214     nsCOMPtr<nsISupports> aSupports;
00215     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00216     for (i = count - 1; i >= 0; i --)
00217     {
00218       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00219       aProgressListener = do_QueryInterface(aSupports);
00220       if (aProgressListener)
00221         aProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00222     }
00223   }
00224   
00225   return rv;
00226 }
00227 
00228 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00229 NS_IMETHODIMP nsPrintProgress::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00230 {
00231   nsresult rv = NS_OK;
00232 
00233   if (m_listenerList)
00234   {
00235     PRUint32 count;
00236     PRInt32 i;
00237 
00238     rv = m_listenerList->Count(&count);
00239     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00240     if (NS_FAILED(rv))
00241       return rv;
00242   
00243     nsCOMPtr<nsISupports> aSupports;
00244     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00245     for (i = count - 1; i >= 0; i --)
00246     {
00247       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00248       aProgressListener = do_QueryInterface(aSupports);
00249       if (aProgressListener)
00250         aProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
00251     }
00252   }
00253   
00254   return rv;
00255 }
00256 
00257 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00258 NS_IMETHODIMP nsPrintProgress::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location)
00259 {
00260     return NS_ERROR_NOT_IMPLEMENTED;
00261 }
00262 
00263 /* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
00264 NS_IMETHODIMP nsPrintProgress::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00265 {
00266   nsresult rv = NS_OK;
00267 
00268   if (aMessage && *aMessage)
00269   m_pendingStatus = aMessage;
00270   if (m_listenerList)
00271   {
00272     PRUint32 count;
00273     PRInt32 i;
00274 
00275     rv = m_listenerList->Count(&count);
00276     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00277     if (NS_FAILED(rv))
00278       return rv;
00279   
00280     nsCOMPtr<nsISupports> aSupports;
00281     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00282     for (i = count - 1; i >= 0; i --)
00283     {
00284       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00285       aProgressListener = do_QueryInterface(aSupports);
00286       if (aProgressListener)
00287         aProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00288     }
00289   }
00290   
00291   return rv;
00292 }
00293 
00294 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00295 NS_IMETHODIMP nsPrintProgress::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00296 {
00297     return NS_OK;
00298 }
00299 
00300 nsresult nsPrintProgress::ReleaseListeners()
00301 {
00302   nsresult rv = NS_OK;
00303 
00304   if (m_listenerList)
00305   {
00306     PRUint32 count;
00307     PRInt32 i;
00308 
00309     rv = m_listenerList->Count(&count);
00310     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00311     if (NS_SUCCEEDED(rv))    
00312       for (i = count - 1; i >= 0; i --)
00313         m_listenerList->RemoveElementAt(i);
00314   }
00315   
00316   return rv;
00317 }
00318 
00319 NS_IMETHODIMP nsPrintProgress::ShowStatusString(const PRUnichar *status)
00320 {
00321   return OnStatusChange(nsnull, nsnull, NS_OK, status);
00322 }
00323 
00324 /* void startMeteors (); */
00325 NS_IMETHODIMP nsPrintProgress::StartMeteors()
00326 {
00327   return NS_ERROR_NOT_IMPLEMENTED;
00328 }
00329 
00330 /* void stopMeteors (); */
00331 NS_IMETHODIMP nsPrintProgress::StopMeteors()
00332 {
00333   return NS_ERROR_NOT_IMPLEMENTED;
00334 }
00335 
00336 /* void showProgress (in long percent); */
00337 NS_IMETHODIMP nsPrintProgress::ShowProgress(PRInt32 percent)
00338 {
00339   return NS_ERROR_NOT_IMPLEMENTED;
00340 }
00341 
00342 /* [noscript] void setDocShell (in nsIDocShell shell, in nsIDOMWindowInternal window); */
00343 NS_IMETHODIMP nsPrintProgress::SetDocShell(nsIDocShell *shell, nsIDOMWindowInternal *window)
00344 {
00345   return NS_ERROR_NOT_IMPLEMENTED;
00346 }
00347 
00348 /* void closeWindow (); */
00349 NS_IMETHODIMP nsPrintProgress::CloseWindow()
00350 {
00351   return NS_ERROR_NOT_IMPLEMENTED;
00352 }
00353