PageRenderTime 45ms CodeModel.GetById 14ms app.highlight 26ms RepoModel.GetById 2ms app.codeStats 0ms

/embedding/components/printingui/src/unixshared/nsPrintProgress.cpp

http://github.com/zpao/v8monkey
C++ | 356 lines | 240 code | 59 blank | 57 comment | 35 complexity | b983469c7a4476dd76528127c6a7f1e3 MD5 | raw file
  1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2/* ***** BEGIN LICENSE BLOCK *****
  3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4 *
  5 * The contents of this file are subject to the Mozilla Public License Version
  6 * 1.1 (the "License"); you may not use this file except in compliance with
  7 * the License. You may obtain a copy of the License at
  8 * http://www.mozilla.org/MPL/
  9 *
 10 * Software distributed under the License is distributed on an "AS IS" basis,
 11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12 * for the specific language governing rights and limitations under the
 13 * License.
 14 *
 15 * The Original Code is mozilla.org code.
 16 *
 17 * The Initial Developer of the Original Code is
 18 * Netscape Communications Corporation.
 19 * Portions created by the Initial Developer are Copyright (C) 1998
 20 * the Initial Developer. All Rights Reserved.
 21 *
 22 * Contributor(s):
 23 *   Jean-Francois Ducarroz <ducarroz@netscape.com>
 24 *   Rod Spears <rods@netscape.com>
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either the GNU General Public License Version 2 or later (the "GPL"), or
 28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the MPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39
 40#include "nsPrintProgress.h"
 41
 42#include "nsIBaseWindow.h"
 43#include "nsISupportsArray.h"
 44#include "nsXPCOM.h"
 45#include "nsISupportsPrimitives.h"
 46#include "nsIComponentManager.h"
 47
 48NS_IMPL_THREADSAFE_ADDREF(nsPrintProgress)
 49NS_IMPL_THREADSAFE_RELEASE(nsPrintProgress)
 50
 51NS_INTERFACE_MAP_BEGIN(nsPrintProgress)
 52   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrintStatusFeedback)
 53   NS_INTERFACE_MAP_ENTRY(nsIPrintProgress)
 54   NS_INTERFACE_MAP_ENTRY(nsIPrintStatusFeedback)
 55   NS_INTERFACE_MAP_ENTRY(nsIWebProgressListener)
 56NS_INTERFACE_MAP_END_THREADSAFE
 57
 58
 59nsPrintProgress::nsPrintProgress(nsIPrintSettings* aPrintSettings)
 60{
 61  m_closeProgress = false;
 62  m_processCanceled = false;
 63  m_pendingStateFlags = -1;
 64  m_pendingStateValue = 0;
 65  m_PrintSetting = aPrintSettings;
 66}
 67
 68nsPrintProgress::~nsPrintProgress()
 69{
 70  (void)ReleaseListeners();
 71}
 72
 73/* void openProgressDialog (in nsIDOMWindow parent, in string dialogURL, in nsISupports parameters); */
 74NS_IMETHODIMP nsPrintProgress::OpenProgressDialog(nsIDOMWindow *parent,
 75                                                  const char *dialogURL,
 76                                                  nsISupports *parameters, 
 77                                                  nsIObserver *openDialogObserver,
 78                                                  bool *notifyOnOpen)
 79{
 80  *notifyOnOpen = true;
 81  m_observer = openDialogObserver;
 82  nsresult rv = NS_ERROR_FAILURE;
 83  
 84  if (m_dialog)
 85    return NS_ERROR_ALREADY_INITIALIZED;
 86  
 87  if (!dialogURL || !*dialogURL)
 88    return NS_ERROR_INVALID_ARG;
 89
 90  if (parent)
 91  {
 92    // Set up window.arguments[0]...
 93    nsCOMPtr<nsISupportsArray> array;
 94    rv = NS_NewISupportsArray(getter_AddRefs(array));
 95    NS_ENSURE_SUCCESS(rv, rv);
 96
 97    nsCOMPtr<nsISupportsInterfacePointer> ifptr =
 98      do_CreateInstance(NS_SUPPORTS_INTERFACE_POINTER_CONTRACTID, &rv);
 99    NS_ENSURE_SUCCESS(rv, rv);
100    
101    ifptr->SetData(static_cast<nsIPrintProgress*>(this));
102    ifptr->SetDataIID(&NS_GET_IID(nsIPrintProgress));
103
104    array->AppendElement(ifptr);
105
106    array->AppendElement(parameters);
107
108    // Open the dialog.
109    nsCOMPtr<nsIDOMWindow> newWindow;
110    rv = parent->OpenDialog(NS_ConvertASCIItoUTF16(dialogURL),
111                            NS_LITERAL_STRING("_blank"),
112                            NS_LITERAL_STRING("chrome,titlebar,dependent,centerscreen"),
113                            array, getter_AddRefs(newWindow));
114  }
115
116  return rv;
117}
118
119/* void closeProgressDialog (in boolean forceClose); */
120NS_IMETHODIMP nsPrintProgress::CloseProgressDialog(bool forceClose)
121{
122  m_closeProgress = true;
123  return OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, forceClose);
124}
125
126/* nsIPrompt GetPrompter (); */
127NS_IMETHODIMP nsPrintProgress::GetPrompter(nsIPrompt **_retval)
128{
129  NS_ENSURE_ARG_POINTER(_retval);
130  *_retval = nsnull;
131
132  if (! m_closeProgress && m_dialog)
133    return m_dialog->GetPrompter(_retval);
134    
135  return NS_ERROR_FAILURE;
136}
137
138/* attribute boolean processCanceledByUser; */
139NS_IMETHODIMP nsPrintProgress::GetProcessCanceledByUser(bool *aProcessCanceledByUser)
140{
141  NS_ENSURE_ARG_POINTER(aProcessCanceledByUser);
142  *aProcessCanceledByUser = m_processCanceled;
143  return NS_OK;
144}
145NS_IMETHODIMP nsPrintProgress::SetProcessCanceledByUser(bool aProcessCanceledByUser)
146{
147  if(m_PrintSetting)
148    m_PrintSetting->SetIsCancelled(true);
149  m_processCanceled = aProcessCanceledByUser;
150  OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, false);
151  return NS_OK;
152}
153
154/* void RegisterListener (in nsIWebProgressListener listener); */
155NS_IMETHODIMP nsPrintProgress::RegisterListener(nsIWebProgressListener * listener)
156{
157  nsresult rv = NS_OK;
158  
159  if (!listener) //Nothing to do with a null listener!
160    return NS_OK;
161  
162  if (!m_listenerList)
163    rv = NS_NewISupportsArray(getter_AddRefs(m_listenerList));
164  
165  if (NS_SUCCEEDED(rv) && m_listenerList)
166  {
167    m_listenerList->AppendElement(listener);
168    if (m_closeProgress || m_processCanceled)
169      listener->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, 0);
170    else
171    {
172      listener->OnStatusChange(nsnull, nsnull, 0, m_pendingStatus.get());
173      if (m_pendingStateFlags != -1)
174        listener->OnStateChange(nsnull, nsnull, m_pendingStateFlags, m_pendingStateValue);
175    }
176  }
177    
178  return NS_OK;
179}
180
181/* void UnregisterListener (in nsIWebProgressListener listener); */
182NS_IMETHODIMP nsPrintProgress::UnregisterListener(nsIWebProgressListener *listener)
183{
184  if (m_listenerList && listener)
185    m_listenerList->RemoveElement(listener);
186  
187  return NS_OK;
188}
189
190/* void doneIniting (); */
191NS_IMETHODIMP nsPrintProgress::DoneIniting()
192{
193  if (m_observer) {
194    m_observer->Observe(nsnull, nsnull, nsnull);
195  }
196  return NS_OK;
197}
198
199/* void onStateChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long aStateFlags, in nsresult aStatus); */
200NS_IMETHODIMP nsPrintProgress::OnStateChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 aStateFlags, nsresult aStatus)
201{
202  nsresult rv = NS_OK;
203
204  m_pendingStateFlags = aStateFlags;
205  m_pendingStateValue = aStatus;
206  
207  if (m_listenerList)
208  {
209    PRUint32 count;
210    PRInt32 i;
211
212    rv = m_listenerList->Count(&count);
213    NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
214    if (NS_FAILED(rv))
215      return rv;
216  
217    nsCOMPtr<nsISupports> aSupports;
218    nsCOMPtr<nsIWebProgressListener> aProgressListener;
219    for (i = count - 1; i >= 0; i --)
220    {
221      m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
222      aProgressListener = do_QueryInterface(aSupports);
223      if (aProgressListener)
224        aProgressListener->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus);
225    }
226  }
227  
228  return rv;
229}
230
231/* void onProgressChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in long aCurSelfProgress, in long aMaxSelfProgress, in long aCurTotalProgress, in long aMaxTotalProgress); */
232NS_IMETHODIMP nsPrintProgress::OnProgressChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRInt32 aCurSelfProgress, PRInt32 aMaxSelfProgress, PRInt32 aCurTotalProgress, PRInt32 aMaxTotalProgress)
233{
234  nsresult rv = NS_OK;
235
236  if (m_listenerList)
237  {
238    PRUint32 count;
239    PRInt32 i;
240
241    rv = m_listenerList->Count(&count);
242    NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
243    if (NS_FAILED(rv))
244      return rv;
245  
246    nsCOMPtr<nsISupports> aSupports;
247    nsCOMPtr<nsIWebProgressListener> aProgressListener;
248    for (i = count - 1; i >= 0; i --)
249    {
250      m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
251      aProgressListener = do_QueryInterface(aSupports);
252      if (aProgressListener)
253        aProgressListener->OnProgressChange(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress);
254    }
255  }
256  
257  return rv;
258}
259
260/* void onLocationChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsIURI location, in unsigned long aFlags); */
261NS_IMETHODIMP nsPrintProgress::OnLocationChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsIURI *location, PRUint32 aFlags)
262{
263    return NS_ERROR_NOT_IMPLEMENTED;
264}
265
266/* void onStatusChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in nsresult aStatus, in wstring aMessage); */
267NS_IMETHODIMP nsPrintProgress::OnStatusChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, nsresult aStatus, const PRUnichar *aMessage)
268{
269  nsresult rv = NS_OK;
270
271  if (aMessage && *aMessage)
272  m_pendingStatus = aMessage;
273  if (m_listenerList)
274  {
275    PRUint32 count;
276    PRInt32 i;
277
278    rv = m_listenerList->Count(&count);
279    NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
280    if (NS_FAILED(rv))
281      return rv;
282  
283    nsCOMPtr<nsISupports> aSupports;
284    nsCOMPtr<nsIWebProgressListener> aProgressListener;
285    for (i = count - 1; i >= 0; i --)
286    {
287      m_listenerList->GetElementAt(i, getter_AddRefs(aSupports));
288      aProgressListener = do_QueryInterface(aSupports);
289      if (aProgressListener)
290        aProgressListener->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage);
291    }
292  }
293  
294  return rv;
295}
296
297/* void onSecurityChange (in nsIWebProgress aWebProgress, in nsIRequest aRequest, in unsigned long state); */
298NS_IMETHODIMP nsPrintProgress::OnSecurityChange(nsIWebProgress *aWebProgress, nsIRequest *aRequest, PRUint32 state)
299{
300    return NS_OK;
301}
302
303nsresult nsPrintProgress::ReleaseListeners()
304{
305  nsresult rv = NS_OK;
306
307  if (m_listenerList)
308  {
309    PRUint32 count;
310    PRInt32 i;
311
312    rv = m_listenerList->Count(&count);
313    NS_ASSERTION(NS_SUCCEEDED(rv), "m_listenerList->Count() failed");
314    if (NS_SUCCEEDED(rv))    
315      for (i = count - 1; i >= 0; i --)
316        m_listenerList->RemoveElementAt(i);
317  }
318  
319  return rv;
320}
321
322NS_IMETHODIMP nsPrintProgress::ShowStatusString(const PRUnichar *status)
323{
324  return OnStatusChange(nsnull, nsnull, NS_OK, status);
325}
326
327/* void startMeteors (); */
328NS_IMETHODIMP nsPrintProgress::StartMeteors()
329{
330  return NS_ERROR_NOT_IMPLEMENTED;
331}
332
333/* void stopMeteors (); */
334NS_IMETHODIMP nsPrintProgress::StopMeteors()
335{
336  return NS_ERROR_NOT_IMPLEMENTED;
337}
338
339/* void showProgress (in long percent); */
340NS_IMETHODIMP nsPrintProgress::ShowProgress(PRInt32 percent)
341{
342  return NS_ERROR_NOT_IMPLEMENTED;
343}
344
345/* [noscript] void setDocShell (in nsIDocShell shell, in nsIDOMWindow window); */
346NS_IMETHODIMP nsPrintProgress::SetDocShell(nsIDocShell *shell, nsIDOMWindow *window)
347{
348  return NS_ERROR_NOT_IMPLEMENTED;
349}
350
351/* void closeWindow (); */
352NS_IMETHODIMP nsPrintProgress::CloseWindow()
353{
354  return NS_ERROR_NOT_IMPLEMENTED;
355}
356