From f3c0d032a2bad1d7647c4cb06769c132d450115b Mon Sep 17 00:00:00 2001
From: phasefx <phasefx@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Date: Thu, 27 Jan 2011 20:58:41 +0000
Subject: [PATCH] Make window.print() configurable.  The "Mozilla Print"
 strategy is internally identified as 'webBrowserPrint', and it uses XPCOM for
 printing the contents of an HTML document.  However, in some cases we
 override this with a 'window.print' strategy to workaround bugs.  A problem
 with this has been that an actual bare javascript window.print() does not
 honor any of configured "gPrintSettings".  However, it is informed by (and in
 turn informs) a Mozilla preference called print.print_printer.  Right now
 window.print (if used from within the JSAN util.print library) only gets
 called in when in the Default printer context/role.  So we force
 print.print_printer to equal the configured printer for the Default role. 
 Have I mentioned that I hate printing in Mozilla?

git-svn-id: svn://svn.open-ils.org/ILS/trunk@19316 dcc99617-32d9-48b4-a31d-7c20da2025e4
---
 .../xul/staff_client/chrome/content/util/print.js  | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/Open-ILS/xul/staff_client/chrome/content/util/print.js b/Open-ILS/xul/staff_client/chrome/content/util/print.js
index 3f9e884b66..8030de85da 100644
--- a/Open-ILS/xul/staff_client/chrome/content/util/print.js
+++ b/Open-ILS/xul/staff_client/chrome/content/util/print.js
@@ -333,8 +333,21 @@ util.print.prototype = {
                         obj._NSPrint_custom_print(w,silent,params);
                     break;    
                     case 'window.print':
+                        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+                        var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
+                        var originalPrinter = false;
+                        if (prefs.prefHasUserValue('print.print_printer')) {
+                            // This is for restoring print.print_printer after any print dialog, so that when
+                            // window.print gets used again, it uses the configured printer for the right context
+                            // (which should only be default--window.print is a kludge and is in limited use),
+                            // rather than the printer last used.
+                            originalPrinter = prefs.getCharPref('print.print_printer');
+                        }
                         if (typeof w == 'object') {
                             w.print();
+                            if (originalPrinter) {
+                                prefs.setCharPref('print.print_printer',originalPrinter);
+                            }
                         } else {
                             if (params.content_type == 'text/plain') {
                                 w = window.open('data:text/plain,'+escape(params.msg));
@@ -344,6 +357,9 @@ util.print.prototype = {
                             setTimeout(
                                 function() {
                                     w.print();
+                                    if (originalPrinter) {
+                                        prefs.setCharPref('print.print_printer',originalPrinter);
+                                    }
                                     setTimeout(
                                         function() {
                                             w.close(); 
@@ -580,6 +596,12 @@ util.print.prototype = {
             if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
             if (obj.gPrintSettings) file.set_object(obj.gPrintSettings); 
             file.close();
+            if (this.context == 'default') {
+                // print.print_printer gets used by bare window.print()'s.  We sometimes use window.print for the
+                // WebBrowserPrint strategy to workaround bugs with the NSPrint xpcom, and only in the default context.
+                var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
+                prefs.setCharPref('print.print_printer',obj.gPrintSettings.printerName);
+            }
         } catch(E) {
             this.error.standard_unexpected_error_alert("save_settings()",E);
         }
-- 
2.11.0