]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
printing the contents of iframes is sometimes problematic, especially with complex...
[working/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     JSAN.use('util.functional');
12     JSAN.use('util.file');
13
14     var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
15     var key = 'oils.printer.external.cmd';
16     var has_key = prefs.prefHasUserValue(key);
17     this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
18
19     return this;
20 };
21
22 util.print.prototype = {
23
24     'reprint_last' : function() {
25         try {
26             var obj = this; obj.data.init({'via':'stash'});
27             if (!obj.data.last_print) {
28                 alert(
29                     document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
30                 );
31                 return;
32             }
33             var msg = obj.data.last_print.msg;
34             var params = obj.data.last_print.params; params.no_prompt = false;
35             obj.simple( msg, params );
36         } catch(E) {
37             this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
38         }
39     },
40
41     'html2txt' : function(html) {
42         JSAN.use('util.text');
43         //dump('html2txt, before:\n' + html + '\n');
44         var lines = html.split(/\n/);
45         var new_lines = [];
46         for (var i = 0; i < lines.length; i++) {
47             var line = lines[i];
48             if (line) {
49                 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
50                 line = util.text.reverse_preserve_string_in_html(line);
51                 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
52                 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
53                     var raw_chars = '';
54                     var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
55                     for (var j = 0; j < hex_chars.length; j++) {
56                         raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
57                     }
58                     return p1 + p3 + raw_chars;
59                 });
60                 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
61                 line = line.replace(/<br.*?>/gi,'\r\n');
62                 line = line.replace(/<table.*?>/gi,'');
63                 line = line.replace(/<tr.*?>/gi,'');
64                 line = line.replace(/<hr.*?>/gi,'\r\n');
65                 line = line.replace(/<p.*?>/gi,'');
66                 line = line.replace(/<block.*?>/gi,'');
67                 line = line.replace(/<li.*?>/gi,' * ');
68                 line = line.replace(/<.+?>/gi,'');
69                 if (line) { new_lines.push(line); }
70             } else {
71                 new_lines.push(line);
72             }
73         }
74         var new_html = new_lines.join('\n');
75         //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
76         return new_html;
77     },
78
79     'simple' : function(msg,params) {
80         try {
81             if (!params) params = {};
82             params.msg = msg;
83
84             var obj = this;
85
86             obj.data.last_print = { 'msg' : msg, 'params' : params}; obj.data.stash('last_print');
87
88             var silent = false;
89             if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
90                 silent = true;
91             }
92
93             var content_type;
94             if (params && params.content_type) {
95                 content_type = params.content_type;
96             } else {
97                 content_type = 'text/html';
98             }
99
100             var w;
101
102             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
103             obj.data.init({'via':'stash'});
104
105             if (params.print_strategy || obj.data.print_strategy) {
106
107                 switch(params.print_strategy || obj.data.print_strategy) {
108                     case 'dos.print':
109                         params.dos_print = true;
110                     case 'custom.print':
111                         /* FIXME - this it a kludge.. we're going to sidestep window-based html rendering for printing */
112                         /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
113                         /* templates aren't guaranteed to be valid xml.  The unadulterated msg is still preserved in */
114                         /* params */
115                         if (content_type=='text/html') {
116                             w = obj.html2txt(msg);
117                         } else {
118                             w = msg;
119                         }
120                         if (! params.no_form_feed) { w = w + '\f'; }
121                         obj.NSPrint(w, silent, params);
122                         return;
123                     break;
124                 }
125             }
126
127             switch(content_type) {
128                 case 'text/html' :
129                     var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '}; function my_init() { if (typeof go_print == "function") { go_print(); } else { setTimeout( function() { if (typeof go_print == "function") { alert("Please tell the developers that the 2-second go_print workaround executed, and let them know whether this job printed successfully.  Thanks!"); go_print(); } else { alert("Please tell the developers that the 2-second go_print workaround did not work.  We will try to print one more time; there have been reports of wasted receipt paper at this point.  Please check the settings in the print dialog and/or prepare to power off your printer.  Thanks!"); window.print(); } }, 2000 ); } /* FIXME - mozilla bug#301560 - xpcom kills it too */ }');
130                     w = obj.win.open('data:text/html,<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head><body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>','receipt_temp','chrome,resizable');
131                     w.minimize();
132                     w.go_print = function() { 
133                         try {
134                             obj.NSPrint(w, silent, params);
135                         } catch(E) {
136                             obj.error.standard_unexpected_error_alert("Print Error in util.print.simple.  After this dialog we'll try a second print attempt. content_type = " + content_type,E);
137                             w.print();
138                         }
139                         w.minimize(); w.close();
140                     }
141                 break;
142                 default:
143                     w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
144                     w.minimize();
145                     setTimeout(
146                         function() {
147                             try {
148                                 obj.NSPrint(w, silent, params);
149                             } catch(E) {
150                                 obj.error.standard_unexpected_error_alert("Print Error in util.print.simple.  After this dialog we'll try a second print attempt. content_type = " + content_type,E);
151                                 w.print();
152                             }
153                             w.minimize(); w.close();
154                         }, 1000
155                     );
156                 break;
157             }
158
159         } catch(E) {
160             this.error.standard_unexpected_error_alert('util.print.simple',E);
161         }
162     },
163     
164     'tree_list' : function (params) { 
165         try {
166             dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
167             dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
168         } catch(E) {
169             dump(E+'\n');
170         }
171         var cols = [];
172         var s = '';
173         if (params.header) s += this.template_sub( params.header, cols, params );
174         if (params.list) {
175             for (var i = 0; i < params.list.length; i++) {
176                 params.row = params.list[i];
177                 params.row_idx = i;
178                 s += this.template_sub( params.line_item, cols, params );
179             }
180         }
181         if (params.footer) s += this.template_sub( params.footer, cols, params );
182
183         if (params.sample_frame) {
184             var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
185             params.sample_frame.setAttribute('src','data:text/html,<html id="top"><head><script src="' + window.escape(jsrc) + '"></script></head><body>' + window.escape(s) + '</body></html>');
186         } else {
187             this.simple(s,params);
188         }
189     },
190
191     'template_sub' : function( msg, cols, params ) {
192         try {
193             var obj = this;
194             if (!msg) { dump('template sub called with empty string\n'); return; }
195             JSAN.use('util.date');
196             var s = msg; var b;
197
198             try{b = s; s = s.replace(/%LINE_NO%/,Number(params.row_idx)+1);}
199                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
200
201             try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
202                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
203
204             try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
205                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
206             try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
207                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
208             try{b = s; s = s.replace(/%SHORTNAME%/,params.lib.shortname());}
209                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
210             try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
211                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
212             try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
213                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
214             try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
215                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
216             try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
217                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
218             try{b = s; s = s.replace(/%PATRON_ALIAS_OR_FIRSTNAME%/,(params.patron.alias() == '' || params.patron.alias() == null) ? params.patron.first_given_name() : params.patron.alias());}
219                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
220             try{b = s; s = s.replace(/%PATRON_ALIAS%/,(params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias());}
221                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
222             try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
223                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
224             try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
225                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
226             try{b = s; s = s.replace(/%PATRON_BARCODE%/,typeof params.patron.card() == 'object' ? params.patron.card().barcode() : util.functional.find_id_object_in_list( params.patron.cards(), params.patron.card() ).barcode() ) ;}
227                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
228
229             try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
230                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
231             try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
232                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
233             try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
234                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
235             try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
236                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
237             try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
238                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
239             try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
240                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
241             try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
242                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
243             try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
244                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
245             try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
246                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
247             try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
248                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
249
250             try {
251                 if (typeof params.row != 'undefined') {
252                     if (params.row.length >= 0) {
253                         alert('debug - please tell the developers that deprecated template code tried to execute');
254                         for (var i = 0; i < cols.length; i++) {
255                             var re = new RegExp(cols[i],"g");
256                             try{b = s; s=s.replace(re, params.row[i]);}
257                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
258                         }
259                     } else { 
260                         /* for dump_with_keys */
261                         for (var i in params.row) {
262                             var re = new RegExp('%'+i+'%',"g");
263                             try{b = s; s=s.replace(re, params.row[i]);}
264                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
265                         }
266                     }
267                 }
268
269                 if (typeof params.data != 'undefined') {
270                     for (var i in params.data) {
271                         var re = new RegExp('%'+i+'%',"g");
272                         if (typeof params.data[i] == 'string') {
273                             try{b = s; s=s.replace(re, params.data[i]);}
274                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
275                         }
276                     }
277                 }
278             } catch(E) { dump(E+'\n'); }
279
280             return s;
281         } catch(E) {
282             alert('Error in print.js, template_sub(): ' + E);
283         }
284     },
285
286
287     'NSPrint' : function(w,silent,params) {
288         if (!w) w = window;
289         var obj = this;
290         try {
291             if (!params) params = {};
292
293             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
294             obj.data.init({'via':'stash'});
295
296             if (params.print_strategy || obj.data.print_strategy) {
297
298 dump('params.print_strategy = ' + params.print_strategy + ' || obj.data.print_strategy = ' + obj.data.print_strategy + ' => ' + ( params.print_strategy || obj.data.print_strategy ) + '\n');
299                 switch(params.print_strategy || obj.data.print_strategy) {
300                     case 'dos.print':
301                     case 'custom.print':
302                         if (typeof w != 'string') {
303                             try {
304                                 var temp_w = params.msg || w.document.firstChild.innerHTML;
305                                 if (!params.msg) { params.msg = temp_w; }
306                                 if (typeof temp_w != 'string') { throw(temp_w); }
307                                 w = obj.html2txt(temp_w);
308                             } catch(E) {
309                                 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
310                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
311                                 w.getSelection().selectAllChildren(w.document.firstChild);
312                                 w = w.getSelection().toString();
313                             }
314                         }
315                         obj._NSPrint_custom_print(w,silent,params);
316                     break;    
317                     case 'window.print':
318                         if (! params.msg) {
319                             w.print();
320                         } else {
321                             w = window.open('data:text/plain,'+escape(params.msg));
322                             setTimeout(
323                                 function() {
324                                     w.print();
325                                     setTimeout(
326                                         function() {
327                                             w.close(); 
328                                         }, 2000
329                                     );
330                                 }, 0
331                             );
332                         }
333                     break;    
334                     case 'webBrowserPrint':
335                     default:
336                         if (! params.msg) {
337                             obj._NSPrint_webBrowserPrint(w,silent,params);
338                         } else {
339                             w = window.open('data:text/plain,'+escape(params.msg));
340                             setTimeout(
341                                 function() {
342                                     obj._NSPrint_webBrowserPrint(w,silent,params);
343                                     setTimeout(
344                                         function() {
345                                             w.close(); 
346                                         }, 2000
347                                     );
348                                 }, 0
349                             );
350                         }
351                     break;    
352                 }
353
354             } else {
355                 //w.print();
356                 obj._NSPrint_webBrowserPrint(w,silent,params);
357             }
358
359         } catch (e) {
360             alert('Probably not printing: ' + e);
361             this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
362         }
363
364     },
365
366     '_NSPrint_custom_print' : function(w,silent,params) {
367         var obj = this;
368         try {
369
370             var text = w;
371             var html = params.msg || w;
372
373             var txt_file = new util.file('receipt.txt');
374             txt_file.write_content('truncate',text); 
375             var text_path = '"' + txt_file._file.path + '"';
376             txt_file.close();
377
378             var html_file = new util.file('receipt.html');
379             html_file.write_content('truncate',html); 
380             var html_path = '"' + html_file._file.path + '"';
381             html_file.close();
382             
383             var cmd = params.dos_print ?
384                 'copy ' + text_path + ' lpt1 /b\n'
385                 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
386             ;
387
388             file = new util.file('receipt.bat');
389             file.write_content('truncate+exec',cmd);
390             file.close();
391             file = new util.file('receipt.bat');
392
393             dump('print exec: ' + cmd + '\n');
394             var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
395             process.init(file._file);
396
397             var args = [];
398
399             dump('process.run = ' + process.run(true, args, args.length) + '\n');
400
401             file.close();
402
403         } catch (e) {
404             //alert('Probably not printing: ' + e);
405             this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
406         }
407     },
408
409     '_NSPrint_webBrowserPrint' : function(w,silent,params) {
410         var obj = this;
411         try {
412             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
413             var webBrowserPrint = w
414                 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
415                 .getInterface(Components.interfaces.nsIWebBrowserPrint);
416             this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
417             if (webBrowserPrint) {
418                 var gPrintSettings = obj.GetPrintSettings();
419                 if (silent) gPrintSettings.printSilent = true;
420                 else gPrintSettings.printSilent = false;
421                 if (params) {
422                     if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
423                 }
424                 webBrowserPrint.print(gPrintSettings, null);
425                 this.error.sdump('D_PRINT','Should be printing\n');
426             } else {
427                 this.error.sdump('D_ERROR','Should not be printing\n');
428             }
429         } catch (e) {
430             //alert('Probably not printing: ' + e);
431             // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
432             // causing an exception to be thrown which we catch here.
433             // Unfortunately this will also consume helpful failures
434             this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
435         }
436     },
437
438     'GetPrintSettings' : function() {
439         try {
440             //alert('entering GetPrintSettings');
441             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
442             var pref = Components.classes["@mozilla.org/preferences-service;1"]
443                 .getService(Components.interfaces.nsIPrefBranch);
444             //alert('pref = ' + pref);
445             if (pref) {
446                 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
447                 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
448                 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + '  gSavePrintSettings = ' + this.gSavePrintSettings);
449             }
450  
451             var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
452                 .getService(Components.interfaces.nsIPrintSettingsService);
453             if (this.gPrintSettingsAreGlobal) {
454                 this.gPrintSettings = printService.globalPrintSettings;
455                 //alert('called setPrinterDefaultsForSelectedPrinter');
456                 this.setPrinterDefaultsForSelectedPrinter(printService);
457             } else {
458                 this.gPrintSettings = printService.newPrintSettings;
459                 //alert('used printService.newPrintSettings');
460             }
461         } catch (e) {
462             this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
463             //alert("GetPrintSettings() "+e+"\n");
464         }
465  
466         return this.gPrintSettings;
467     },
468
469     'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
470         try {
471             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
472             if (this.gPrintSettings.printerName == "") {
473                 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
474                 //alert('used .defaultPrinterName');
475             }
476             //alert('printerName = ' + this.gPrintSettings.printerName);
477      
478             // First get any defaults from the printer 
479             aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
480      
481             // now augment them with any values from last time
482             aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
483
484             // now augment from our own saved settings if they exist
485             this.load_settings();
486
487         } catch(E) {
488             this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
489         }
490     },
491
492     'page_settings' : function() {
493         try {
494             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
495             this.GetPrintSettings();
496             var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
497             PO.ShowPrintSetupDialog(this.gPrintSettings);
498         } catch(E) {
499             this.error.standard_unexpected_error_alert("page_settings()",E);
500         }
501     },
502
503     'load_settings' : function() {
504         try {
505             var error_msg = '';
506             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
507             var file = new util.file('gPrintSettings');
508             if (file._file.exists()) {
509                 temp = file.get_object(); file.close();
510                 for (var i in temp) {
511                     try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
512                 }
513             } else {
514                 this.gPrintSettings.marginTop = 0;
515                 this.gPrintSettings.marginLeft = 0;
516                 this.gPrintSettings.marginBottom = 0;
517                 this.gPrintSettings.marginRight = 0;
518                 this.gPrintSettings.headerStrLeft = '';
519                 this.gPrintSettings.headerStrCenter = '';
520                 this.gPrintSettings.headerStrRight = '';
521                 this.gPrintSettings.footerStrLeft = '';
522                 this.gPrintSettings.footerStrCenter = '';
523                 this.gPrintSettings.footerStrRight = '';
524             }
525             if (error_msg) {
526                 this.error.sdump('D_PRINT',error_msg);
527                 this.error.yns_alert(
528                     document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
529                     document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
530                     document.getElementById('offlineStrings').getString('common.ok'),
531                     null,
532                     null,
533                     null
534                 );
535             }
536         } catch(E) {
537             this.error.standard_unexpected_error_alert("load_settings()",E);
538         }
539     },
540
541     'save_settings' : function() {
542         try {
543             var obj = this;
544             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
545             var file = new util.file('gPrintSettings');
546             if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
547             if (obj.gPrintSettings) file.set_object(obj.gPrintSettings); 
548             file.close();
549         } catch(E) {
550             this.error.standard_unexpected_error_alert("save_settings()",E);
551         }
552     }
553 }
554
555 dump('exiting util/print.js\n');