1 dump('entering util/print.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.print = function (context) {
6 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
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');
14 this.set_context(context, true);
17 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
19 if (prefs.prefHasUserValue('print.always_print_silent')) {
20 if (! prefs.getBoolPref('print.always_print_silent')) {
21 prefs.clearUserPref('print.always_print_silent');
25 dump('Error in print.js trying to clear print.always_print_silent\n');
31 util.print.prototype = {
33 'set_context' : function(context, set_default) {
34 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
36 this.context = context || 'default';
37 if(set_default) this.default_context = this.context;
39 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
40 var key = 'oils.printer.external.cmd.' + this.context;
41 var has_key = prefs.prefHasUserValue(key);
42 if(!has_key && this.context != 'default') {
43 key = 'oils.printer.external.cmd.default';
44 has_key = prefs.prefHasUserValue(key);
46 this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
49 'reprint_last' : function() {
51 var obj = this; obj.data.init({'via':'stash'});
52 if (!obj.data.last_print) {
54 document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
58 if(obj.data.last_print.context) this.set_context(obj.data.last_print.context);
59 var msg = obj.data.last_print.msg;
60 var params = obj.data.last_print.params; params.no_prompt = false;
61 obj.simple( msg, params );
63 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
65 if(this.context != this.default_context) this.set_context(this.default_context);
68 'html2txt' : function(html) {
69 JSAN.use('util.text');
70 //dump('html2txt, before:\n' + html + '\n');
71 var lines = html.split(/\n/);
73 for (var i = 0; i < lines.length; i++) {
76 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
77 line = util.text.reverse_preserve_string_in_html(line);
78 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
79 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
81 var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
82 for (var j = 0; j < hex_chars.length; j++) {
83 raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
85 return p1 + p3 + raw_chars;
87 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
88 line = line.replace(/<br.*?>/gi,'\r\n');
89 line = line.replace(/<table.*?>/gi,'');
90 line = line.replace(/<tr.*?>/gi,'');
91 line = line.replace(/<hr.*?>/gi,'\r\n');
92 line = line.replace(/<p.*?>/gi,'');
93 line = line.replace(/<block.*?>/gi,'');
94 line = line.replace(/<li.*?>/gi,' * ');
95 line = line.replace(/<.+?>/gi,'');
96 line = line.replace(/</gi,'<');
97 line = line.replace(/>/gi,'>');
98 line = line.replace(/&/gi,'&');
99 if (line) { new_lines.push(line); }
101 new_lines.push(line);
104 var new_html = new_lines.join('\n');
105 //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
109 'escape_html' : function(data) {
110 if (typeof data == 'object') { return ''; }
111 return data.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
114 'simple' : function(msg,params) {
116 if (!params) params = {};
121 obj.data.last_print = { 'msg' : msg, 'params' : params, 'context' : this.context};
122 obj.data.stash('last_print');
125 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
130 if (params && params.content_type) {
131 content_type = params.content_type;
133 content_type = 'text/html';
138 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
139 obj.data.init({'via':'stash'});
141 if (typeof obj.data.print_strategy == 'undefined') {
142 obj.data.print_strategy = {};
143 obj.data.stash('print_strategy');
146 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
148 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
150 params.dos_print = true;
152 /* FIXME - this it a kludge.. we're going to sidestep window-based html rendering for printing */
153 /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
154 /* templates aren't guaranteed to be valid xml. The unadulterated msg is still preserved in */
156 if (content_type=='text/html') {
157 w = obj.html2txt(msg);
161 if (! params.no_form_feed) { w = w + '\f'; }
162 obj.NSPrint(w, silent, params);
168 switch(content_type) {
173 var my_prefix = '/xul/server/';
174 if(window.location.protocol == "chrome:") {
175 // Likely in offline interface
176 my_prefix = 'chrome://open_ils_staff_client/content/';
178 if(xulG && xulG.url_prefix) {
179 my_prefix = xulG.url_prefix(my_prefix);
182 var print_url = 'data:text/html,<html id="top"><head>'
183 + '<script src="' + my_prefix + 'util/print_win.js"></script>'
184 + '<script src="' + my_prefix + 'util/print_custom.js"></script>';
185 if(this.data.hash.aous['print.custom_js_file']) {
186 print_url += '<script src="' + this.data.hash.aous['print.custom_js_file'] + '"></script>';
188 print_url += '</head><body onload="try{print_init(\'' + params.type + '\');}catch(E){alert(E);}">' + window.escape(msg.replace(/<script[^>]*>.*?<\/script>/gi,'')) + '</body></html>';
189 obj.win.openDialog(print_url,'receipt_temp','chrome,resizable,modal', null, { "data" : params.data, "list" : params.list}, function(w) {
191 obj.NSPrint(w, silent, params);
193 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);
200 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
205 obj.NSPrint(w, silent, params);
207 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);
210 w.minimize(); w.close();
217 this.error.standard_unexpected_error_alert('util.print.simple',E);
221 'tree_list' : function (params) {
223 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
224 dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
230 if (params.context) this.set_context(params.context);
231 if (params.header) s += this.template_sub( params.header, cols, params );
233 // Pre-templating sort
234 // %SORT(field[ AS type][ ASC|DESC][,...])%
235 var sort_blocks = params.line_item.match(/%SORT\([^)]+\)%/g);
237 for(var i = 0; i < sort_blocks.length; i++) {
238 sort_blocks[i] = sort_blocks[i].substring(6,sort_blocks[i].length-2);
240 sort_blocks = sort_blocks.join(',').split(/\s*,\s*/); // Supports %SORT(a,b)% and %SORT(a)% %SORT(b)% methods
241 for(var i = 0; i < sort_blocks.length; i++) {
242 sort_blocks[i] = sort_blocks[i].match(/([^ ]+)(?:\s+AS\s+([^ ]+))?(?:\s+(ASC|DESC))?/);
243 sort_blocks[i].shift(); // Removes the "full match" entry
246 function sorter(a, b) {
248 for(var i = 0; i < sort_blocks.length && return_val == 0; i++) {
249 var sort = sort_blocks[i];
250 var a_test = a[sort[0]];
251 var b_test = b[sort[0]];
252 sort[1] = sort[1] || '';
253 sort[2] = sort[2] || 'ASC';
254 switch(sort[1].toUpperCase()) {
256 a_test = new Date(a_test);
257 b_test = new Date(b_test);
260 a_test = parseInt(a_test);
261 b_test = parseInt(b_test);
265 a_test = parseFloat(a_test);
266 b_test = parseFloat(b_test);
269 a_test = a_test.toLowerCase();
270 b_test = b_test.toLowerCase();
273 a_test = a_test.toUpperCase();
274 b_test = b_test.toUpperCase();
277 if(a_test > b_test) return_val = 1;
278 if(a_test < b_test) return_val = -1;
279 if(sort[2] == 'DESC') return_val *= -1;
283 params.list.sort(sorter);
284 params.line_item = params.line_item.replace(/%SORT\([^)]*\)%/g,'');
287 for (var i = 0; i < params.list.length; i++) {
288 params.row = params.list[i];
290 s += this.template_sub( params.line_item, cols, params );
293 if (params.footer) s += this.template_sub( params.footer, cols, params );
295 // Sanity check, no javascript in templates
296 // Note: [\s\S] is a workaround for . not including newlines.
297 s=s.replace(/<script[^>]*>[\s\S]*?<\/script[^>]*>/gi,'')
298 s=s.replace(/onload\s*=\s*"[^"]*"/gi,'');
299 s=s.replace(/onload\s*=\s*'[^']*'/gi,'');
301 if (params.sample_frame) {
302 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
303 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>');
305 this.simple(s,params);
307 if(this.context != this.default_context) this.set_context(this.default_context);
310 'template_sub' : function( msg, cols, params ) {
313 if (!msg) { dump('template sub called with empty string\n'); return; }
314 JSAN.use('util.date');
318 // Note that we keep track of already included settings
319 // This ensures that we don't infinite loop through includes
322 var include_patt=/%INCLUDE\(\s*([^)]*?)\s*\)%/;
324 while(match = include_patt.exec(s)) {
325 if(match[1] == '' || included[match[1]]) {
326 s = s.replace(match[0], '');
328 included[match[1]] = true;
329 s = s.replace(new RegExp("%INCLUDE\\(\\s*" + match[1].replace(/([.?*+^$[\]\\(){}-])/g, "\\$1") + "\\s*\\)%","g"), obj.data.hash.aous['circ.staff_client.receipt.' + match[1]] || '');
332 } catch(E) { dump(E+'\n'); }
334 try{b = s; s = s.replace(/%LINE_NO%/g,Number(params.row_idx)+1);}
335 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
337 try{b = s; s = s.replace(/%patron_barcode%/g,this.escape_html(params.patron_barcode));}
338 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
340 try{b = s; s = s.replace(/%LIBRARY%/g,this.escape_html(params.lib.name()));}
341 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
342 try{b = s; s = s.replace(/%PINES_CODE%/g,this.escape_html(params.lib.shortname()));}
343 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
344 try{b = s; s = s.replace(/%SHORTNAME%/g,this.escape_html(params.lib.shortname()));}
345 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
346 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/g,this.escape_html(params.staff.first_given_name()));}
347 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
348 try{b = s; s = s.replace(/%STAFF_LASTNAME%/g,this.escape_html(params.staff.family_name()));}
349 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
350 try{b = s; s = s.replace(/%STAFF_BARCODE%/g,this.escape_html(params.staff.barcode)); }
351 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
352 try{b = s; s = s.replace(/%STAFF_PROFILE%/g,this.escape_html(obj.data.hash.pgt[ params.staff.profile() ].name() )); }
353 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
354 try{b = s; s = s.replace(/%PATRON_ALIAS_OR_FIRSTNAME%/g,this.escape_html((params.patron.alias() == '' || params.patron.alias() == null) ? params.patron.first_given_name() : params.patron.alias()));}
355 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
356 try{b = s; s = s.replace(/%PATRON_ALIAS%/g,this.escape_html((params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias()));}
357 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
358 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/g,this.escape_html(params.patron.first_given_name()));}
359 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
360 try{b = s; s = s.replace(/%PATRON_LASTNAME%/g,this.escape_html(params.patron.family_name()));}
361 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
362 try{b = s; s = s.replace(/%PATRON_BARCODE%/g,this.escape_html(typeof params.patron.card() == 'object' ? params.patron.card().barcode() : util.functional.find_id_object_in_list( params.patron.cards(), params.patron.card() ).barcode() )) ;}
363 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
365 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
366 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
367 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
368 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
369 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
370 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
371 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
372 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
373 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
374 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
375 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
376 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
377 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
378 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
379 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
380 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
381 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
382 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
383 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
384 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
387 if (typeof params.row != 'undefined') {
388 if (params.row.length >= 0) {
389 alert('debug - please tell the developers that deprecated template code tried to execute');
390 for (var i = 0; i < cols.length; i++) {
391 var re = new RegExp(cols[i],"g");
392 try{b = s; s=s.replace(re, this.escape_html(params.row[i]));}
393 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
396 /* for dump_with_keys */
397 for (var i in params.row) {
398 var re = new RegExp('%'+i+'%',"g");
399 try{b = s; s=s.replace(re, this.escape_html(params.row[i].toString()));}
400 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
405 if (typeof params.data != 'undefined') {
406 for (var i in params.data) {
407 var re = new RegExp('%'+i+'%',"g");
408 if (typeof params.data[i] == 'string' || typeof params.data[i] == 'number') {
409 try{b = s; s=s.replace(re, (typeof params.data[i] == 'string' ? this.escape_html(params.data[i]) : params.data[i]));}
410 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
412 /* likely a null, print as an empty string */
413 try{b = s; s=s.replace(re, '');}
414 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
418 } catch(E) { dump(E+'\n'); }
423 var date_format_patt=/%DATE_FORMAT\(\s*([^,]*?)\s*,\s*([^)]*?)\s*\)%/;
424 while(match = date_format_patt.exec(s)) {
425 if(match[1] == '' || match[2] == '')
426 s = s.replace(match[0], '');
428 s = s.replace(match[0], util.date.formatted_date(match[1], match[2]));
430 } catch(E) { dump(E+'\n'); }
435 // Pre-trim inside of substrings, and only inside of them
436 // This keeps the trim commands from being truncated
437 var substr_trim_patt=/(%SUBSTR\(-?\d+,?\s*(-?\d+)?\)%.*?)(\s*%-TRIM%|%TRIM-%\s*)(.*?%SUBSTR_END%)/;
438 while(match = substr_trim_patt.exec(s))
439 s = s.replace(match[0], match[1] + match[4]);
440 // Then do the substrings themselves
441 var substr_patt=/%SUBSTR\((-?\d+),?\s*(-?\d+)?\)%(.*?)%SUBSTR_END%/;
442 while(match = substr_patt.exec(s)) {
443 var substring_start = parseInt(match[1]);
444 if(substring_start < 0) substring_start = match[3].length + substring_start;
445 var substring_length = parseInt(match[2]);
446 if(substring_length > 0)
447 s = s.replace(match[0], match[3].substring(substring_start, substring_start + substring_length));
448 else if(substring_length < 0)
449 s = s.replace(match[0], match[3].substring(substring_start + substring_length, substring_start));
451 s = s.replace(match[0], match[3].substring(substring_start));
453 } catch(E) { dump(E+'\n'); }
455 // Cleanup unwanted whitespace
457 s = s.replace(/%TRIM-%\s*/g,'');
458 s = s.replace(/\s*%-TRIM%/g,'');
459 } catch(E) { dump(E+'\n'); }
463 alert('Error in print.js, template_sub(): ' + E);
468 'NSPrint' : function(w,silent,params) {
472 if (!params) params = {};
474 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
475 obj.data.init({'via':'stash'});
477 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
479 dump('params.print_strategy = ' + params.print_strategy
480 + ' || obj.data.print_strategy[' + obj.context + '] = ' + obj.data.print_strategy[obj.context]
481 + ' || obj.data.print_strategy[default] = ' + obj.data.print_strategy['default']
482 + ' => ' + ( params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default'] ) + '\n');
483 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
485 params.dos_print = true;
487 if (typeof w != 'string') {
489 var temp_w = params.msg || w.document.firstChild.innerHTML;
490 if (!params.msg) { params.msg = temp_w; }
491 if (typeof temp_w != 'string') { throw(temp_w); }
492 w = obj.html2txt(temp_w);
494 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
495 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
496 w.getSelection().selectAllChildren(w.document.firstChild);
497 w = w.getSelection().toString();
500 obj._NSPrint_custom_print(w,silent,params);
503 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
504 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
505 var originalPrinter = false;
506 if (prefs.prefHasUserValue('print.print_printer')) {
507 // This is for restoring print.print_printer after any print dialog, so that when
508 // window.print gets used again, it uses the configured printer for the right context
509 // (which should only be default--window.print is a kludge and is in limited use),
510 // rather than the printer last used.
511 originalPrinter = prefs.getCharPref('print.print_printer');
513 if (typeof w == 'object') {
515 if (originalPrinter) {
516 prefs.setCharPref('print.print_printer',originalPrinter);
519 if (params.content_type == 'text/plain') {
520 w = window.open('data:text/plain,'+escape(params.msg));
522 w = window.open('data:text/html,'+escape(params.msg));
527 if (originalPrinter) {
528 prefs.setCharPref('print.print_printer',originalPrinter);
539 case 'webBrowserPrint':
541 if (typeof w == 'object') {
542 obj._NSPrint_webBrowserPrint(w,silent,params);
544 if (params.content_type == 'text/plain') {
545 w = window.open('data:text/plain,'+escape(params.msg));
547 w = window.open('data:text/html,'+escape(params.msg));
551 obj._NSPrint_webBrowserPrint(w,silent,params);
565 obj._NSPrint_webBrowserPrint(w,silent,params);
569 alert('Probably not printing: ' + e);
570 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
575 '_NSPrint_custom_print' : function(w,silent,params) {
580 var html = params.msg || w;
582 var txt_file = new util.file('receipt.txt');
583 txt_file.write_content('truncate',text);
584 var text_path = '"' + txt_file._file.path + '"';
587 var html_file = new util.file('receipt.html');
588 html_file.write_content('truncate',html);
589 var html_path = '"' + html_file._file.path + '"';
592 var cmd = params.dos_print ?
593 'copy ' + text_path + ' lpt1 /b\n'
594 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
597 file = new util.file('receipt.bat');
598 file.write_content('truncate+exec',cmd);
600 file = new util.file('receipt.bat');
602 dump('print exec: ' + cmd + '\n');
603 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
604 process.init(file._file);
608 dump('process.run = ' + process.run(true, args, args.length) + '\n');
613 //alert('Probably not printing: ' + e);
614 this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
618 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
621 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
622 var webBrowserPrint = w
623 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
624 .getInterface(Components.interfaces.nsIWebBrowserPrint);
625 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
626 if (webBrowserPrint) {
627 var gPrintSettings = obj.GetPrintSettings();
628 if (silent) gPrintSettings.printSilent = true;
629 else gPrintSettings.printSilent = false;
631 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
633 webBrowserPrint.print(gPrintSettings, null);
634 this.error.sdump('D_PRINT','Should be printing\n');
636 this.error.sdump('D_ERROR','Should not be printing\n');
639 //alert('Probably not printing: ' + e);
640 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
641 // causing an exception to be thrown which we catch here.
642 // Unfortunately this will also consume helpful failures
643 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
647 'GetPrintSettings' : function() {
649 //alert('entering GetPrintSettings');
650 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
651 var pref = Components.classes["@mozilla.org/preferences-service;1"]
652 .getService(Components.interfaces.nsIPrefBranch);
653 //alert('pref = ' + pref);
655 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
656 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
657 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
660 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
661 .getService(Components.interfaces.nsIPrintSettingsService);
662 if (this.gPrintSettingsAreGlobal) {
663 this.gPrintSettings = printService.globalPrintSettings;
664 //alert('called setPrinterDefaultsForSelectedPrinter');
665 this.setPrinterDefaultsForSelectedPrinter(printService);
667 this.gPrintSettings = printService.newPrintSettings;
668 //alert('used printService.newPrintSettings');
671 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
672 //alert("GetPrintSettings() "+e+"\n");
675 return this.gPrintSettings;
678 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
680 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
681 if (this.gPrintSettings.printerName == "") {
682 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
683 //alert('used .defaultPrinterName');
685 //alert('printerName = ' + this.gPrintSettings.printerName);
687 // First get any defaults from the printer
688 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
690 // now augment them with any values from last time
691 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
693 // now augment from our own saved settings if they exist
694 this.load_settings();
697 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
701 'page_settings' : function() {
703 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
704 this.GetPrintSettings();
705 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
706 PO.ShowPrintSetupDialog(this.gPrintSettings);
708 this.error.standard_unexpected_error_alert("page_settings()",E);
712 'load_settings' : function() {
715 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
716 var file = new util.file('gPrintSettings.' + this.context);
717 if (file._file.exists()) {
718 temp = file.get_object(); file.close();
719 for (var i in temp) {
720 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
722 } else if (this.context != 'default') {
723 var file = new util.file('gPrintSettings.default');
724 if (file._file.exists()) {
725 temp = file.get_object(); file.close();
726 for (var i in temp) {
727 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
730 this.gPrintSettings.marginTop = 0;
731 this.gPrintSettings.marginLeft = 0;
732 this.gPrintSettings.marginBottom = 0;
733 this.gPrintSettings.marginRight = 0;
734 this.gPrintSettings.headerStrLeft = '';
735 this.gPrintSettings.headerStrCenter = '';
736 this.gPrintSettings.headerStrRight = '';
737 this.gPrintSettings.footerStrLeft = '';
738 this.gPrintSettings.footerStrCenter = '';
739 this.gPrintSettings.footerStrRight = '';
743 this.error.sdump('D_PRINT',error_msg);
744 this.error.yns_alert(
745 document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
746 document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
747 document.getElementById('offlineStrings').getString('common.ok'),
754 this.error.standard_unexpected_error_alert("load_settings()",E);
758 'save_settings' : function() {
761 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
762 var file = new util.file('gPrintSettings.' + this.context);
763 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
764 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
766 if (this.context == 'default') {
767 // print.print_printer gets used by bare window.print()'s. We sometimes use window.print for the
768 // WebBrowserPrint strategy to workaround bugs with the NSPrint xpcom, and only in the default context.
769 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
770 prefs.setCharPref('print.print_printer',obj.gPrintSettings.printerName);
773 this.error.standard_unexpected_error_alert("save_settings()",E);
778 dump('exiting util/print.js\n');