Back to index

lightning-sunbird  0.9+nobinonly
nsPagePrintTimer.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsPagePrintTimer.h"
00039 #include "nsPrintEngine.h"
00040 #include "nsIContentViewer.h"
00041 #include "nsIServiceManager.h"
00042 
00043 NS_IMPL_ISUPPORTS1(nsPagePrintTimer, nsITimerCallback)
00044 
00045 nsPagePrintTimer::nsPagePrintTimer() :
00046   mDocViewerPrint(nsnull), 
00047   mPresContext(nsnull), 
00048   mPrintSettings(nsnull), 
00049   mDelay(0)
00050 {
00051 }
00052 
00053 nsPagePrintTimer::~nsPagePrintTimer()
00054 {
00055   if (mTimer) {
00056     mTimer->Cancel();
00057   }
00058   mPrintEngine->SetIsPrinting(PR_FALSE); // this will notify the DV also
00059 
00060   nsCOMPtr<nsIContentViewer> cv(do_QueryInterface(mDocViewerPrint));
00061   if (cv) {
00062     cv->Destroy();
00063   }
00064 }
00065 
00066 nsresult 
00067 nsPagePrintTimer::StartTimer(PRBool aUseDelay)
00068 {
00069   nsresult result;
00070   mTimer = do_CreateInstance("@mozilla.org/timer;1", &result);
00071   if (NS_FAILED(result)) {
00072     NS_WARNING("unable to start the timer");
00073   } else {
00074     mTimer->InitWithCallback(this, aUseDelay?mDelay:0, nsITimer::TYPE_ONE_SHOT);
00075   }
00076   return result;
00077 }
00078 
00079 
00080 
00081 // nsITimerCallback
00082 NS_IMETHODIMP
00083 nsPagePrintTimer::Notify(nsITimer *timer)
00084 {
00085   if (mPresContext && mDocViewerPrint) {
00086     PRPackedBool initNewTimer = PR_TRUE;
00087     // Check to see if we are done
00088     // donePrinting will be true if it completed successfully or
00089     // if the printing was cancelled
00090     PRBool inRange;
00091     PRBool donePrinting = mPrintEngine->PrintPage(mPresContext, mPrintSettings, mPrintObj, inRange);
00092     if (donePrinting) {
00093       // now clean up print or print the next webshell
00094       if (mPrintEngine->DonePrintingPages(mPrintObj, NS_OK)) {
00095         initNewTimer = PR_FALSE;
00096       }
00097     }
00098 
00099     Stop();
00100     if (initNewTimer) {
00101       nsresult result = StartTimer(inRange);
00102       if (NS_FAILED(result)) {
00103         donePrinting = PR_TRUE;     // had a failure.. we are finished..
00104         mPrintEngine->SetIsPrinting(PR_FALSE);
00105       }
00106     }
00107   }
00108   return NS_OK;
00109 }
00110 
00111 void 
00112 nsPagePrintTimer::Init(nsPrintEngine*          aPrintEngine,
00113                        nsIDocumentViewerPrint* aDocViewerPrint,
00114                        nsPresContext*         aPresContext,
00115                        nsIPrintSettings*       aPrintSettings,
00116                        nsPrintObject*          aPO,
00117                        PRUint32                aDelay)
00118 {
00119   mPrintEngine     = aPrintEngine;
00120   mDocViewerPrint  = aDocViewerPrint;
00121 
00122   mPresContext     = aPresContext;
00123   mPrintSettings   = aPrintSettings;
00124   mPrintObj        = aPO;
00125   mDelay           = aDelay;
00126 }
00127 
00128 nsresult 
00129 nsPagePrintTimer::Start(nsPrintEngine*          aPrintEngine,
00130                         nsIDocumentViewerPrint* aDocViewerPrint,
00131                         nsPresContext*         aPresContext,
00132                         nsIPrintSettings*       aPrintSettings,
00133                         nsPrintObject*          aPO,
00134                         PRUint32                aDelay)
00135 {
00136   Init(aPrintEngine, aDocViewerPrint, aPresContext, aPrintSettings, aPO, aDelay);
00137   return StartTimer(PR_FALSE);
00138 }
00139 
00140 
00141 void  
00142 nsPagePrintTimer::Stop()
00143 {
00144   if (mTimer) {
00145     mTimer->Cancel();
00146     mTimer = nsnull;
00147   }
00148 }
00149 
00150 nsresult NS_NewPagePrintTimer(nsPagePrintTimer **aResult)
00151 {
00152 
00153   NS_PRECONDITION(aResult, "null param");
00154 
00155   nsPagePrintTimer* result = new nsPagePrintTimer;
00156 
00157   if (!result) {
00158     *aResult = nsnull;
00159     return NS_ERROR_OUT_OF_MEMORY;
00160   }
00161 
00162   NS_ADDREF(result);
00163   *aResult = result;
00164 
00165   return NS_OK;
00166 }
00167