]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
3c218bf8777354bc7e3308f70b9a3eca7a897779
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / print.js
1 dump('entering util/print.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.print = function () {
5
6         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
7
8         JSAN.use('util.error'); this.error = new util.error();
9         JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init( { 'via':'stash' } );
10         JSAN.use('util.window'); this.win = new util.window();
11
12         return this;
13 };
14
15 util.print.prototype = {
16
17         'simple' : function(msg,params) {
18
19                 if (!params) params = {};
20
21                 var obj = this;
22
23                 obj.data.last_print = msg; obj.data.stash('last_print');
24
25                 var silent = false;
26                 if (params && params.no_prompt && params.no_prompt == true) silent = true;
27
28                 var w = obj.win.open('data:text/html,<html>' + window.escape(msg) + '</html>','temp','chrome,resizable');
29
30                 w.minimize();
31
32                 setTimeout(
33                         function() {
34                                 try {
35                                         obj.NSPrint(w, silent, params);
36                                 } catch(E) {
37                                         obj.error.sdump('D_ERROR','util.print.simple: ' + E);
38                                         w.print();
39                                 }
40                                 w.minimize(); w.close();
41                         }, 0
42                 );
43         },
44
45         'NSPrint' : function(w,silent,params) {
46                 if (!w) w = window;
47                 try {
48                         var webBrowserPrint = w
49                                 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
50                                 .getInterface(Components.interfaces.nsIWebBrowserPrint);
51                         this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
52                         if (webBrowserPrint) {
53                                 var gPrintSettings = GetPrintSettings();
54                                 if (silent) gPrintSettings.printSilent = true;
55                                 else gPrintSettings.printSilent = false;
56                                 if (params) {
57                                         gPrintSettings.marginTop = 0;
58                                         gPrintSettings.marginLeft = 0;
59                                         gPrintSettings.marginBottom = 0;
60                                         gPrintSettings.marginRight = 0;
61                                         if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
62                                 }
63                                 gPrintSettings.headerStrLeft = '';
64                                 gPrintSettings.headerStrCenter = '';
65                                 gPrintSettings.headerStrRight = '';
66                                 gPrintSettings.footerStrLeft = '';
67                                 gPrintSettings.footerStrCenter = '';
68                                 gPrintSettings.footerStrRight = '';
69                                 this.error.sdump('D_PRINT','gPrintSettings = ' + js2JSON(gPrintSettings));
70                                 //alert('gPrintSettings = ' + js2JSON(gPrintSettings));
71                                 webBrowserPrint.print(gPrintSettings, null);
72                                 //alert('Should be printing\n');
73                                 this.error.sdump('D_PRINT','Should be printing\n');
74                         } else {
75                                 //alert('Should not be printing\n');
76                                 this.error.sdump('D_PRINT','Should not be printing\n');
77                         }
78                 } catch (e) {
79                         //alert('Probably not printing: ' + e);
80                         // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
81                         // causing an exception to be thrown which we catch here.
82                         // Unfortunately this will also consume helpful failures, so add a
83                         this.error.sdump('D_PRINT','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
84                         // if you need to debug
85                 }
86
87         },
88
89         'GetPrintSettings' : function() {
90                 try {
91                         var pref = Components.classes["@mozilla.org/preferences-service;1"]
92                                 .getService(Components.interfaces.nsIPrefBranch);
93                         if (pref) {
94                                 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
95                                 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
96                         }
97  
98                         var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
99                                 .getService(Components.interfaces.nsIPrintSettingsService);
100                         if (this.gPrintSettingsAreGlobal) {
101                                 this.gPrintSettings = printService.globalPrintSettings;
102                                 this.setPrinterDefaultsForSelectedPrinter(printService);
103                         } else {
104                                 this.gPrintSettings = printService.newPrintSettings;
105                         }
106                 } catch (e) {
107                         this.error.sdump('D_PRINT',"GetPrintSettings() "+e+"\n");
108                         //alert("GetPrintSettings() "+e+"\n");
109                 }
110  
111                 return this.gPrintSettings;
112         },
113
114         'setPrinterDefaultsForSelectedPrint' : function (aPrintService) {
115                 if (this.gPrintSettings.printerName == "") {
116                         this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
117                 }
118  
119                 // First get any defaults from the printer 
120                 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
121  
122                 // now augment them with any values from last time
123                 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
124         }
125 }
126
127 dump('exiting util/print.js\n');