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