Fix bug 351581 - [Win] Print fails on first attempt but works on second. r=decathlon
authorPhilipp Kewisch <mozilla@kewis.ch>
Fri, 05 Aug 2011 21:02:14 +0200
changeset 8360 696f0ff4a0fbe4b0c73dde0474bfde3f3f76d129
parent 8359 6ade8a6feeec364dee50923cd6ca85339f2fea4b
child 8361 a054da113fa8667d5555a6216028354b600fed2d
push idunknown
push userunknown
push dateunknown
reviewersdecathlon
bugs351581
Fix bug 351581 - [Win] Print fails on first attempt but works on second. r=decathlon
calendar/base/content/dialogs/calendar-print-dialog.js
calendar/base/jar.mn
--- a/calendar/base/content/dialogs/calendar-print-dialog.js
+++ b/calendar/base/content/dialogs/calendar-print-dialog.js
@@ -29,39 +29,46 @@
  *   Michiel van Leeuwen <mvl@exedo.nl>
  *   Matthew Willis <mattwillis@gmail.com>
  *   Martin Schroeder <mschroeder@mozilla.x-home.org>
  *   Joey Minta <jminta@gmail.com>
  *   Diego Mira David <diegomd86@gmail.com>
  *   Eduardo Teruo Katayama <eduardo@ime.usp.br>
  *   Glaucus Augustus Grecco Cardoso <glaucus@ime.usp.br>
  *   Francisco Jose Mulero <fjmulero@gmv.com>
+ *   Philipp Kewisch <mozilla@kewis.ch>
  *
  * Alternatively, the contents of this file may be used under the terms of
  * either the GNU General Public License Version 2 or later (the "GPL"), or
  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  * in which case the provisions of the GPL or the LGPL are applicable instead
  * of those above. If you wish to allow use of your version of this file only
  * under the terms of either the GPL or the LGPL, and not to allow others to
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+/**
+ * Gets the calendar view from the opening window
+ */
 function getCalendarView() {
     let theView = window.opener.currentView();
     if (!theView.startDay) {
         theView = null;
     }
     return theView;
 }
 
+/**
+ * Loads the print dialog, setting up all needed elements.
+ */
 function loadCalendarPrintDialog() {
     // set the datepickers to the currently selected dates
     let theView = getCalendarView();
     if (theView) {
         document.getElementById("start-date-picker").value = theView.startDay.jsDate;
         document.getElementById("end-date-picker").value = theView.endDay.jsDate;
     } else {
         document.getElementById("printCurrentViewRadio").setAttribute("disabled", true);
@@ -125,16 +132,21 @@ function getEventsAndDialogSettings(rece
                     settings.eventList = eventWithDueDate;
                 }
             }
         };
         window.opener.getCompositeCalendar().getItems(getFilter(settings), 0, settings.start, settings.end, listener);
     }
 }
 
+/**
+ * Retrieves a settings object containing info on what to print
+ *
+ * @return      The settings object with all print settings
+ */
 function getWhatToPrintSettings() {
     let tempTitle = document.getElementById("title-field").value;
     let settings = new Object();
     settings.title = (tempTitle || calGetString("calendar", "Untitled"));
     settings.layoutCId = document.getElementById("layout-field").value;
     settings.start = null;
     settings.end = null;
     settings.eventList = [];
@@ -175,16 +187,22 @@ function getWhatToPrintSettings() {
         settings.end.day += 1;
         break ;
     default:
         dump("Error : no case in printDialog.js::printCalendar()");
     }
     return settings;
 }
 
+/**
+ * Sets up the filter for a getItems call based on the javascript settings
+ * object
+ *
+ * @param settings      The settings data to base upon
+ */
 function getFilter(settings) {
     let filter = 0;
     if (settings.printTasks) {
         filter |= Components.interfaces.calICalendar.ITEM_FILTER_TYPE_TODO;
         if (settings.printCompletedTasks) {
             filter |= Components.interfaces.calICalendar.ITEM_FILTER_COMPLETED_ALL;
         } else {
             filter |= Components.interfaces.calICalendar.ITEM_FILTER_COMPLETED_NO;
@@ -246,53 +264,63 @@ function refreshHtml(finishFunc) {
 
             if (finishFunc) {
                 finishFunc();
             }
         }
     );
 }
 
+/**
+ * This is a nsIWebProgressListener that closes the dialog on completion, makes
+ * sure printing works without issues
+ */
+var closeOnComplete = {
+    onStateChange: function onStateChange(aProgress, aRequest, aStateFlags, aStatus) {
+
+        if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) {
+            // The request is complete, close the window.
+            document.documentElement.cancelDialog();
+        }
+    },
+
+    onProgressChange: function() {},
+    onLocationChange: function() {},
+    onStatusChange: function() {},
+    onSecurityChange: function() {}
+};
+
+/**
+ * Prints the document and then closes the window
+ */
 function printAndClose() {
     refreshHtml(
         function finish() {
             var webBrowserPrint = PrintUtils.getWebBrowserPrint();
             var printSettings = PrintUtils.getPrintSettings();
 
             // Evicts "about:blank" header
             printSettings.docURL = " ";
 
             // Start the printing, this is just what PrintUtils does, but we
             // apply our own settings.
             try {
-                webBrowserPrint.print(printSettings, null);
+                webBrowserPrint.print(printSettings, closeOnComplete);
                 if (gPrintSettingsAreGlobal && gSavePrintSettings) {
                     var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
                                           .getService(Components.interfaces.nsIPrintSettingsService);
                     PSSVC.savePrintSettingsToPrefs(printSettings, true,
                                                         printSettings.kInitSaveAll);
                     PSSVC.savePrintSettingsToPrefs(printSettings, false,
                                                    printSettings.kInitSavePrinterName);
                 }
-            } catch (e if e instanceof Components.results.NS_ERROR_ABORT) {
+            } catch (e if e.result == Components.results.NS_ERROR_ABORT) {
                 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
                 // causing an exception to be thrown which we catch here.
             }
-
-            let closeDialog = true;
-#ifdef XP_UNIX
-#ifndef XP_MACOSX
-            closeDialog = false;
-#endif
-#endif
-            // XXX: printing fails "printing failed while in preview"
-            //      if dialog is closed too early on Unix
-            if (closeDialog) {
-                document.getElementById("calendar-new-printwindow").cancelDialog();
-            }
         });
     return false; // leave open
 }
 
 /**
  * Called when once a date has been selected in the datepicker.
  */
 function onDatePick() {
--- a/calendar/base/jar.mn
+++ b/calendar/base/jar.mn
@@ -60,17 +60,17 @@ calendar.jar:
     content/calendar/calendar-event-dialog-attendees.js    (content/dialogs/calendar-event-dialog-attendees.js)
     content/calendar/calendar-invitations-dialog.css       (content/dialogs/calendar-invitations-dialog.css)
     content/calendar/calendar-invitations-dialog.js        (content/dialogs/calendar-invitations-dialog.js)
     content/calendar/calendar-invitations-dialog.xul       (content/dialogs/calendar-invitations-dialog.xul)
     content/calendar/calendar-invitations-list.xml         (content/dialogs/calendar-invitations-list.xml)
 *   content/calendar/calendar-migration-dialog.js          (content/dialogs/calendar-migration-dialog.js)
     content/calendar/calendar-migration-dialog.xul         (content/dialogs/calendar-migration-dialog.xul)
     content/calendar/calendar-occurrence-prompt.xul        (content/dialogs/calendar-occurrence-prompt.xul)
-*   content/calendar/calendar-print-dialog.js              (content/dialogs/calendar-print-dialog.js)
+    content/calendar/calendar-print-dialog.js              (content/dialogs/calendar-print-dialog.js)
     content/calendar/calendar-print-dialog.xul             (content/dialogs/calendar-print-dialog.xul)
     content/calendar/calendar-properties-dialog.xul        (content/dialogs/calendar-properties-dialog.xul)
     content/calendar/calendar-properties-dialog.js         (content/dialogs/calendar-properties-dialog.js)
     content/calendar/calendar-providerUninstall-dialog.xul (content/dialogs/calendar-providerUninstall-dialog.xul)
     content/calendar/calendar-providerUninstall-dialog.js  (content/dialogs/calendar-providerUninstall-dialog.js)
     content/calendar/calendar-subscriptions-dialog.css     (content/dialogs/calendar-subscriptions-dialog.css)
     content/calendar/calendar-subscriptions-dialog.js      (content/dialogs/calendar-subscriptions-dialog.js)
     content/calendar/calendar-subscriptions-dialog.xul     (content/dialogs/calendar-subscriptions-dialog.xul)