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