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   m_observer = openDialogObserver;
00080 
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     if (NS_SUCCEEDED(rv)) {
00114       *notifyOnOpen = PR_TRUE;
00115     }
00116   }
00117 
00118   return rv;
00119 }
00120 
00121 /* void closeProgressDialog (in boolean forceClose); */
00122 NS_IMETHODIMP nsPrintProgress::CloseProgressDialog(PRBool forceClose)
00123 {
00124   m_closeProgress = PR_TRUE;
00125   return OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, forceClose);
00126 }
00127 
00128 /* nsIPrompt GetPrompter (); */
00129 NS_IMETHODIMP nsPrintProgress::GetPrompter(nsIPrompt **_retval)
00130 {
00131   NS_ENSURE_ARG_POINTER(_retval);
00132   *_retval = nsnull;
00133 
00134   if (! m_closeProgress && m_dialog)
00135     return m_dialog->GetPrompter(_retval);
00136     
00137   return NS_ERROR_FAILURE;
00138 }
00139 
00140 /* attribute boolean processCanceledByUser; */
00141 NS_IMETHODIMP nsPrintProgress::GetProcessCanceledByUser(PRBool *aProcessCanceledByUser)
00142 {
00143   NS_ENSURE_ARG_POINTER(aProcessCanceledByUser);
00144   *aProcessCanceledByUser = m_processCanceled;
00145   return NS_OK;
00146 }
00147 NS_IMETHODIMP nsPrintProgress::SetProcessCanceledByUser(PRBool aProcessCanceledByUser)
00148 {
00149   m_processCanceled = aProcessCanceledByUser;
00150   OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, PR_FALSE);
00151   return NS_OK;
00152 }
00153 
00154 /* void RegisterListener (in nsIWebProgressListener listener); */
00155 NS_IMETHODIMP nsPrintProgress::RegisterListener(nsIWebProgressListener * listener)
00156 {
00157   nsresult rv = NS_OK;
00158   
00159   if (!listener) //Nothing to do with a null listener!
00160     return NS_OK;
00161   
00162   if (!m_listenerList)
00163     rv = NS_NewISupportsArray(getter_AddRefs(m_listenerList));
00164   
00165   if (NS_SUCCEEDED(rv) && m_listenerList)
00166   {
00167     m_listenerList->AppendElement(listener);
00168     if (m_closeProgress || m_processCanceled)
00169       listener->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, 0);
00170     else
00171     {
00172       listener->OnStatusChange(nsnull, nsnull, 0, m_pendingStatus.get());
00173       if (m_pendingStateFlags != -1)
00174         listener->OnStateChange(nsnull, nsnull, m_pendingStateFlags, m_pendingStateValue);
00175     }
00176   }
00177     
00178   return NS_OK;
00179 }
00180 
00181 /* void UnregisterListener (in nsIWebProgressListener listener); */
00182 NS_IMETHODIMP nsPrintProgress::UnregisterListener(nsIWebProgressListener *listener)
00183 {
00184   if (m_listenerList && listener)
00185     m_listenerList->RemoveElement(listener);
00186   
00187   return NS_OK;
00188 }
00189 
00190 /* void doneIniting (); */
00191 NS_IMETHODIMP nsPrintProgress::DoneIniting()
00192 {
00193   if (m_observer) {
00194     m_observer->Observe(nsnull, nsnull, nsnull);
00195   }
00196   return NS_OK;
00197 }
00198 
00199 /* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
00200 NS_IMETHODIMP nsPrintProgress::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
00201 {
00202   nsresult rv = NS_OK;
00203 
00204   m_pendingStateFlags = aStateFlags;
00205   m_pendingStateValue = aStatus;
00206   
00207   if (m_listenerList)
00208   {
00209     PRUint32 count;
00210     PRInt32 i;
00211 
00212     rv = m_listenerList->Count(&count);
00213     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00214     if (NS_FAILED(rv))
00215       return rv;
00216   
00217     nsCOMPtr<nsISupports> aSupports;
00218     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00219     for (i = count - 1; i >= 0; i --)
00220     {
00221       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00222       aProgressListener = do_QueryInterface(aSupports);
00223       if (aProgressListener)
00224         aProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
00225     }
00226   }
00227   
00228   return rv;
00229 }
00230 
00231 /* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
00232 NS_IMETHODIMP nsPrintProgress::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
00233 {
00234   nsresult rv = NS_OK;
00235 
00236   if (m_listenerList)
00237   {
00238     PRUint32 count;
00239     PRInt32 i;
00240 
00241     rv = m_listenerList->Count(&count);
00242     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00243     if (NS_FAILED(rv))
00244       return rv;
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 rv;
00258 }
00259 
00260 /* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location); */
00261 NS_IMETHODIMP nsPrintProgress::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 nsPrintProgress::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
00268 {
00269   nsresult rv = NS_OK;
00270 
00271   if (aMessage && *aMessage)
00272   m_pendingStatus = aMessage;
00273   if (m_listenerList)
00274   {
00275     PRUint32 count;
00276     PRInt32 i;
00277 
00278     rv = m_listenerList->Count(&count);
00279     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00280     if (NS_FAILED(rv))
00281       return rv;
00282   
00283     nsCOMPtr<nsISupports> aSupports;
00284     nsCOMPtr<nsIWebProgressListener> aProgressListener;
00285     for (i = count - 1; i >= 0; i --)
00286     {
00287       m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
00288       aProgressListener = do_QueryInterface(aSupports);
00289       if (aProgressListener)
00290         aProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
00291     }
00292   }
00293   
00294   return rv;
00295 }
00296 
00297 /* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
00298 NS_IMETHODIMP nsPrintProgress::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
00299 {
00300     return NS_OK;
00301 }
00302 
00303 nsresult nsPrintProgress::ReleaseListeners()
00304 {
00305   nsresult rv = NS_OK;
00306 
00307   if (m_listenerList)
00308   {
00309     PRUint32 count;
00310     PRInt32 i;
00311 
00312     rv = m_listenerList->Count(&count);
00313     NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
00314     if (NS_SUCCEEDED(rv))    
00315       for (i = count - 1; i >= 0; i --)
00316         m_listenerList->RemoveElementAt(i);
00317   }
00318   
00319   return rv;
00320 }
00321 
00322 NS_IMETHODIMP nsPrintProgress::ShowStatusString(const PRUnichar *status)
00323 {
00324   return OnStatusChange(nsnull, nsnull, NS_OK, status);
00325 }
00326 
00327 /* void startMeteors (); */
00328 NS_IMETHODIMP nsPrintProgress::StartMeteors()
00329 {
00330   return NS_ERROR_NOT_IMPLEMENTED;
00331 }
00332 
00333 /* void stopMeteors (); */
00334 NS_IMETHODIMP nsPrintProgress::StopMeteors()
00335 {
00336   return NS_ERROR_NOT_IMPLEMENTED;
00337 }
00338 
00339 /* void showProgress (in long percent); */
00340 NS_IMETHODIMP nsPrintProgress::ShowProgress(PRInt32 percent)
00341 {
00342   return NS_ERROR_NOT_IMPLEMENTED;
00343 }
00344 
00345 /* [noscript] void setDocShell (in nsIDocShell shell, in nsIDOMWindowInternal window); */
00346 NS_IMETHODIMP nsPrintProgress::SetDocShell(nsIDocShell *shell, nsIDOMWindowInternal *window)
00347 {
00348   return NS_ERROR_NOT_IMPLEMENTED;
00349 }
00350 
00351 /* void closeWindow (); */
00352 NS_IMETHODIMP nsPrintProgress::CloseWindow()
00353 {
00354   return NS_ERROR_NOT_IMPLEMENTED;
00355 }
00356