]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/print.js
previous changes to better support the embedded browser Print button broke printing...
[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                     var print_url = 'data:text/html,'
131                         + '<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head>'
132                         + '<body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>';
133                     w = obj.win.open(print_url,'receipt_temp','chrome,resizable');
134                     w.minimize();
135                     w.go_print = function() { 
136                         try {
137                             obj.NSPrint(w, silent, params);
138                         } catch(E) {
139                             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);
140                             w.print();
141                         }
142                         w.minimize(); w.close();
143                     }
144                 break;
145                 default:
146                     w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
147                     w.minimize();
148                     setTimeout(
149                         function() {
150                             try {
151                                 obj.NSPrint(w, silent, params);
152                             } catch(E) {
153                                 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);
154                                 w.print();
155                             }
156                             w.minimize(); w.close();
157                         }, 1000
158                     );
159                 break;
160             }
161
162         } catch(E) {
163             this.error.standard_unexpected_error_alert('util.print.simple',E);
164         }
165     },
166     
167     'tree_list' : function (params) { 
168         try {
169             dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
170             dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
171         } catch(E) {
172             dump(E+'\n');
173         }
174         var cols = [];
175         var s = '';
176         if (params.header) s += this.template_sub( params.header, cols, params );
177         if (params.list) {
178             for (var i = 0; i < params.list.length; i++) {
179                 params.row = params.list[i];
180                 params.row_idx = i;
181                 s += this.template_sub( params.line_item, cols, params );
182             }
183         }
184         if (params.footer) s += this.template_sub( params.footer, cols, params );
185
186         if (params.sample_frame) {
187             var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
188             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>');
189         } else {
190             this.simple(s,params);
191         }
192     },
193
194     'template_sub' : function( msg, cols, params ) {
195         try {
196             var obj = this;
197             if (!msg) { dump('template sub called with empty string\n'); return; }
198             JSAN.use('util.date');
199             var s = msg; var b;
200
201             try{b = s; s = s.replace(/%LINE_NO%/,Number(params.row_idx)+1);}
202                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
203
204             try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
205                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
206
207             try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
208                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
209             try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
210                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
211             try{b = s; s = s.replace(/%SHORTNAME%/,params.lib.shortname());}
212                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
213             try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
214                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
215             try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
216                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
217             try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
218                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
219             try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
220                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
221             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());}
222                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
223             try{b = s; s = s.replace(/%PATRON_ALIAS%/,(params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias());}
224                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
225             try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
226                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
227             try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
228                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
229             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() ) ;}
230                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
231
232             try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
233                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
234             try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
235                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
236             try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
237                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
238             try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
239                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
240             try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
241                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
242             try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
243                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
244             try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
245                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
246             try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
247                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
248             try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
249                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
250             try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
251                 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
252
253             try {
254                 if (typeof params.row != 'undefined') {
255                     if (params.row.length >= 0) {
256                         alert('debug - please tell the developers that deprecated template code tried to execute');
257                         for (var i = 0; i < cols.length; i++) {
258                             var re = new RegExp(cols[i],"g");
259                             try{b = s; s=s.replace(re, params.row[i]);}
260                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
261                         }
262                     } else { 
263                         /* for dump_with_keys */
264                         for (var i in params.row) {
265                             var re = new RegExp('%'+i+'%',"g");
266                             try{b = s; s=s.replace(re, params.row[i]);}
267                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
268                         }
269                     }
270                 }
271
272                 if (typeof params.data != 'undefined') {
273                     for (var i in params.data) {
274                         var re = new RegExp('%'+i+'%',"g");
275                         if (typeof params.data[i] == 'string') {
276                             try{b = s; s=s.replace(re, params.data[i]);}
277                                 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
278                         }
279                     }
280                 }
281             } catch(E) { dump(E+'\n'); }
282
283             return s;
284         } catch(E) {
285             alert('Error in print.js, template_sub(): ' + E);
286         }
287     },
288
289
290     'NSPrint' : function(w,silent,params) {
291         if (!w) w = window;
292         var obj = this;
293         try {
294             if (!params) params = {};
295
296             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
297             obj.data.init({'via':'stash'});
298
299             if (params.print_strategy || obj.data.print_strategy) {
300
301 dump('params.print_strategy = ' + params.print_strategy + ' || obj.data.print_strategy = ' + obj.data.print_strategy + ' => ' + ( params.print_strategy || obj.data.print_strategy ) + '\n');
302                 switch(params.print_strategy || obj.data.print_strategy) {
303                     case 'dos.print':
304                     case 'custom.print':
305                         if (typeof w != 'string') {
306                             try {
307                                 var temp_w = params.msg || w.document.firstChild.innerHTML;
308                                 if (!params.msg) { params.msg = temp_w; }
309                                 if (typeof temp_w != 'string') { throw(temp_w); }
310                                 w = obj.html2txt(temp_w);
311                             } catch(E) {
312                                 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
313                                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
314                                 w.getSelection().selectAllChildren(w.document.firstChild);
315                                 w = w.getSelection().toString();
316                             }
317                         }
318                         obj._NSPrint_custom_print(w,silent,params);
319                     break;    
320                     case 'window.print':
321                         if (typeof w == 'object') {
322                             w.print();
323                         } else {
324                             if (params.content_type == 'text/plain') {
325                                 w = window.open('data:text/plain,'+escape(params.msg));
326                             } else {
327                                 w = window.open('data:text/html,'+escape(params.msg));
328                             }
329                             setTimeout(
330                                 function() {
331                                     w.print();
332                                     setTimeout(
333                                         function() {
334                                             w.close(); 
335                                         }, 2000
336                                     );
337                                 }, 0
338                             );
339                         }
340                     break;    
341                     case 'webBrowserPrint':
342                     default:
343                         if (typeof w == 'object') {
344                             obj._NSPrint_webBrowserPrint(w,silent,params);
345                         } else {
346                             if (params.content_type == 'text/plain') {
347                                 w = window.open('data:text/plain,'+escape(params.msg));
348                             } else {
349                                 w = window.open('data:text/html,'+escape(params.msg));
350                             }
351                             setTimeout(
352                                 function() {
353                                     obj._NSPrint_webBrowserPrint(w,silent,params);
354                                     setTimeout(
355                                         function() {
356                                             w.close(); 
357                                         }, 2000
358                                     );
359                                 }, 0
360                             );
361                         }
362                     break;    
363                 }
364
365             } else {
366                 //w.print();
367                 obj._NSPrint_webBrowserPrint(w,silent,params);
368             }
369
370         } catch (e) {
371             alert('Probably not printing: ' + e);
372             this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
373         }
374
375     },
376
377     '_NSPrint_custom_print' : function(w,silent,params) {
378         var obj = this;
379         try {
380
381             var text = w;
382             var html = params.msg || w;
383
384             var txt_file = new util.file('receipt.txt');
385             txt_file.write_content('truncate',text); 
386             var text_path = '"' + txt_file._file.path + '"';
387             txt_file.close();
388
389             var html_file = new util.file('receipt.html');
390             html_file.write_content('truncate',html); 
391             var html_path = '"' + html_file._file.path + '"';
392             html_file.close();
393             
394             var cmd = params.dos_print ?
395                 'copy ' + text_path + ' lpt1 /b\n'
396                 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
397             ;
398
399             file = new util.file('receipt.bat');
400             file.write_content('truncate+exec',cmd);
401             file.close();
402             file = new util.file('receipt.bat');
403
404             dump('print exec: ' + cmd + '\n');
405             var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
406             process.init(file._file);
407
408             var args = [];
409
410             dump('process.run = ' + process.run(true, args, args.length) + '\n');
411
412             file.close();
413
414         } catch (e) {
415             //alert('Probably not printing: ' + e);
416             this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
417         }
418     },
419
420     '_NSPrint_webBrowserPrint' : function(w,silent,params) {
421         var obj = this;
422         try {
423             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
424             var webBrowserPrint = w
425                 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
426                 .getInterface(Components.interfaces.nsIWebBrowserPrint);
427             this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
428             if (webBrowserPrint) {
429                 var gPrintSettings = obj.GetPrintSettings();
430                 if (silent) gPrintSettings.printSilent = true;
431                 else gPrintSettings.printSilent = false;
432                 if (params) {
433                     if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
434                 }
435                 webBrowserPrint.print(gPrintSettings, null);
436                 this.error.sdump('D_PRINT','Should be printing\n');
437             } else {
438                 this.error.sdump('D_ERROR','Should not be printing\n');
439             }
440         } catch (e) {
441             //alert('Probably not printing: ' + e);
442             // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
443             // causing an exception to be thrown which we catch here.
444             // Unfortunately this will also consume helpful failures
445             this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
446         }
447     },
448
449     'GetPrintSettings' : function() {
450         try {
451             //alert('entering GetPrintSettings');
452             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
453             var pref = Components.classes["@mozilla.org/preferences-service;1"]
454                 .getService(Components.interfaces.nsIPrefBranch);
455             //alert('pref = ' + pref);
456             if (pref) {
457                 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
458                 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
459                 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + '  gSavePrintSettings = ' + this.gSavePrintSettings);
460             }
461  
462             var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
463                 .getService(Components.interfaces.nsIPrintSettingsService);
464             if (this.gPrintSettingsAreGlobal) {
465                 this.gPrintSettings = printService.globalPrintSettings;
466                 //alert('called setPrinterDefaultsForSelectedPrinter');
467                 this.setPrinterDefaultsForSelectedPrinter(printService);
468             } else {
469                 this.gPrintSettings = printService.newPrintSettings;
470                 //alert('used printService.newPrintSettings');
471             }
472         } catch (e) {
473             this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
474             //alert("GetPrintSettings() "+e+"\n");
475         }
476  
477         return this.gPrintSettings;
478     },
479
480     'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
481         try {
482             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
483             if (this.gPrintSettings.printerName == "") {
484                 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
485                 //alert('used .defaultPrinterName');
486             }
487             //alert('printerName = ' + this.gPrintSettings.printerName);
488      
489             // First get any defaults from the printer 
490             aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
491      
492             // now augment them with any values from last time
493             aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
494
495             // now augment from our own saved settings if they exist
496             this.load_settings();
497
498         } catch(E) {
499             this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
500         }
501     },
502
503     'page_settings' : function() {
504         try {
505             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
506             this.GetPrintSettings();
507             var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
508             PO.ShowPrintSetupDialog(this.gPrintSettings);
509         } catch(E) {
510             this.error.standard_unexpected_error_alert("page_settings()",E);
511         }
512     },
513
514     'load_settings' : function() {
515         try {
516             var error_msg = '';
517             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
518             var file = new util.file('gPrintSettings');
519             if (file._file.exists()) {
520                 temp = file.get_object(); file.close();
521                 for (var i in temp) {
522                     try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
523                 }
524             } else {
525                 this.gPrintSettings.marginTop = 0;
526                 this.gPrintSettings.marginLeft = 0;
527                 this.gPrintSettings.marginBottom = 0;
528                 this.gPrintSettings.marginRight = 0;
529                 this.gPrintSettings.headerStrLeft = '';
530                 this.gPrintSettings.headerStrCenter = '';
531                 this.gPrintSettings.headerStrRight = '';
532                 this.gPrintSettings.footerStrLeft = '';
533                 this.gPrintSettings.footerStrCenter = '';
534                 this.gPrintSettings.footerStrRight = '';
535             }
536             if (error_msg) {
537                 this.error.sdump('D_PRINT',error_msg);
538                 this.error.yns_alert(
539                     document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
540                     document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
541                     document.getElementById('offlineStrings').getString('common.ok'),
542                     null,
543                     null,
544                     null
545                 );
546             }
547         } catch(E) {
548             this.error.standard_unexpected_error_alert("load_settings()",E);
549         }
550     },
551
552     'save_settings' : function() {
553         try {
554             var obj = this;
555             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
556             var file = new util.file('gPrintSettings');
557             if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
558             if (obj.gPrintSettings) file.set_object(obj.gPrintSettings); 
559             file.close();
560         } catch(E) {
561             this.error.standard_unexpected_error_alert("save_settings()",E);
562         }
563     }
564 }
565
566 dump('exiting util/print.js\n');