diff --git a/calendar/resources/content/printDialog.js b/calendar/resources/content/printDialog.js index ecbc27a79bd3..a9df7f505a85 100644 --- a/calendar/resources/content/printDialog.js +++ b/calendar/resources/content/printDialog.js @@ -42,20 +42,32 @@ * * ***** END LICENSE BLOCK ***** */ -var gTempFile = null; - var gPrintSettings = null; +function getCalendarView() +{ + var theView = window.opener.currentView(); + if (!theView.startDay) { + theView = null; + } + return theView; +} + function loadCalendarPrintDialog() { // set the datepickers to the currently selected dates - var theView = window.opener.currentView(); - document.getElementById("start-date-picker").value = theView.startDay.jsDate; - document.getElementById("end-date-picker").value = theView.endDay.jsDate; - - if (!theView.getSelectedItems({}).length) { + var 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); + } + if (!theView || !theView.getSelectedItems({}).length) { document.getElementById("selected").setAttribute("disabled", true); } + document.getElementById(theView ? "printCurrentViewRadio" : "custom-range") + .setAttribute("selected", true); // Get a list of formatters var contractids = new Array(); @@ -83,11 +95,6 @@ function loadCalendarPrintDialog() self.focus(); } -function unloadCalendarPrintDialog() -{ - gTempFile.remove(false); -} - /** * Gets the settings from the dialog's UI widgets. * notifies an Object with title, layoutCId, eventList, start, and end @@ -103,7 +110,7 @@ function getEventsAndDialogSettings(receiverFunc) settings.end = null; settings.eventList = null; - var theView = window.opener.currentView(); + var theView = getCalendarView(); switch (document.getElementById("view-field").selectedItem.value) { case 'currentview': case '': //just in case @@ -157,7 +164,7 @@ function getEventsAndDialogSettings(receiverFunc) * updates the HTML in the iframe accordingly. This is also called when a * dialog UI element has changed, since we'll want to refresh the preview. */ -function refreshHtml() +function refreshHtml(finishFunc) { getEventsAndDialogSettings( function getEventsAndDialogSettings_response(settings) { @@ -170,43 +177,55 @@ function refreshHtml() var printformatter = Components.classes[settings.layoutCId] .createInstance(Components.interfaces.calIPrintFormatter); - // Fail-safe check to not init twice, to prevent leaking files - if (gTempFile) { - gTempFile.remove(false); - } - const nsIFile = Components.interfaces.nsIFile; - var dirService = Components.classes["@mozilla.org/file/directory_service;1"] - .getService(Components.interfaces.nsIProperties); - gTempFile = dirService.get("TmpD", nsIFile); - gTempFile.append("calendarPrint.html"); - gTempFile.createUnique(nsIFile.NORMAL_FILE_TYPE, 0600); // 0600 = -rw------- - var ioService = Components.classes["@mozilla.org/network/io-service;1"] - .getService(Components.interfaces.nsIIOService); - var tempUri = ioService.newFileURI(gTempFile); - - var stream = Components.classes["@mozilla.org/network/file-output-stream;1"] - .createInstance(Components.interfaces.nsIFileOutputStream); - + var html = ""; try { - // 0x2A = PR_TRUNCATE, PR_CREATE_FILE, PR_WRONLY - // 0600 = -rw------- - stream.init(gTempFile, 0x2A, 0600, 0); - printformatter.formatToHtml(stream, + var pipe = Components.classes["@mozilla.org/pipe;1"] + .createInstance(Components.interfaces.nsIPipe); + pipe.init(true, true, 0, 0, null); + printformatter.formatToHtml(pipe.outputStream, settings.start, settings.end, settings.eventList.length, settings.eventList, settings.title); - stream.close(); + pipe.outputStream.close(); + // convert byte-array to UTF-8 string: + var convStream = + Components.classes["@mozilla.org/intl/converter-input-stream;1"] + .createInstance(Components.interfaces.nsIConverterInputStream); + convStream.init(pipe.inputStream, "UTF-8", 0, + Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER); + try { + var portion = {}; + while (convStream.readString(-1, portion)) { + html += portion.value; + } + } finally { + convStream.close(); + } } catch (e) { dump("printDialog::refreshHtml:" + e + "\n"); + Components.utils.reportError(e); } + document.getElementById('content').contentDocument.documentElement.innerHTML = html; - document.getElementById("content").contentWindow.location = tempUri.spec; + if (finishFunc) { + finishFunc(); + } } ); } +function printAndClose() +{ + refreshHtml( + function finish() { + PrintUtils.print(); + document.getElementById("calendar-new-printwindow").cancelDialog(); + }); + return false; // leave open +} + /** * Called when once a date has been selected in the datepicker. */ diff --git a/calendar/resources/content/printDialog.xul b/calendar/resources/content/printDialog.xul index 2ee1e08bf9e9..38105af21d84 100644 --- a/calendar/resources/content/printDialog.xul +++ b/calendar/resources/content/printDialog.xul @@ -63,12 +63,11 @@ id="calendar-new-printwindow" title="&calendar.print.window.title;" onload="loadCalendarPrintDialog()" - onunload="unloadCalendarPrintDialog()" buttons="accept,cancel" buttonlabelaccept="&calendar.print.button.label;" buttonaccesskeyaccept="&calendar.print.accesskey;" defaultButton="accept" - ondialogaccept="PrintUtils.print();" + ondialogaccept="return printAndClose();" ondialogcancel="return true;" persist="screenX screenY width height" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" @@ -122,7 +121,7 @@ + value="currentview"/>