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