1 dump('entering util/print.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.print = function (context) {
6 JSAN.use('util.error'); this.error = new util.error();
7 JSAN.use('OpenILS.data'); this.data = new OpenILS.data(); this.data.init( { 'via':'stash' } );
8 JSAN.use('util.window'); this.win = new util.window();
9 JSAN.use('util.functional');
10 JSAN.use('util.file');
12 this.set_context(context, true);
15 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
17 if (prefs.prefHasUserValue('print.always_print_silent')) {
18 if (! prefs.getBoolPref('print.always_print_silent')) {
19 prefs.clearUserPref('print.always_print_silent');
23 dump('Error in print.js trying to clear print.always_print_silent\n');
29 util.print.prototype = {
31 'set_context' : function(context, set_default) {
32 this.context = context || 'default';
33 if(set_default) this.default_context = this.context;
35 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
36 var key = 'oils.printer.external.cmd.' + this.context;
37 var has_key = prefs.prefHasUserValue(key);
38 if(!has_key && this.context != 'default') {
39 key = 'oils.printer.external.cmd.default';
40 has_key = prefs.prefHasUserValue(key);
42 this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
45 'reprint_last' : function() {
47 var obj = this; obj.data.init({'via':'stash'});
48 if (!obj.data.last_print) {
50 document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
54 var last_print = JSON2js(obj.data.last_print);
55 if(last_print.context) this.set_context(last_print.context);
56 var msg = last_print.msg;
57 var params = last_print.params; params.no_prompt = false;
58 obj.simple( msg, params );
60 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
62 if(this.context != this.default_context) this.set_context(this.default_context);
65 'html2txt' : function(html) {
66 JSAN.use('util.text');
67 //dump('html2txt, before:\n' + html + '\n');
68 var lines = html.split(/\n/);
70 for (var i = 0; i < lines.length; i++) {
73 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
74 line = util.text.reverse_preserve_string_in_html(line);
75 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
76 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
78 var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
79 for (var j = 0; j < hex_chars.length; j++) {
80 raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
82 return p1 + p3 + raw_chars;
84 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
85 line = line.replace(/<br.*?>/gi,'\r\n');
86 line = line.replace(/<table.*?>/gi,'');
87 line = line.replace(/<tr.*?>/gi,'');
88 line = line.replace(/<hr.*?>/gi,'\r\n');
89 line = line.replace(/<p.*?>/gi,'');
90 line = line.replace(/<block.*?>/gi,'');
91 line = line.replace(/<li.*?>/gi,' * ');
92 line = line.replace(/<.+?>/gi,'');
93 line = line.replace(/</gi,'<');
94 line = line.replace(/>/gi,'>');
95 line = line.replace(/&/gi,'&');
96 if (line) { new_lines.push(line); }
101 var new_html = new_lines.join('\n');
102 //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
106 'escape_html' : function(data) {
107 if (typeof data == 'object') { return ''; }
108 if (typeof data != 'string') { return data; }
109 return data.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
112 'simple' : function(msg,params) {
114 if (!params) params = {};
119 obj.data.last_print = js2JSON({ 'msg' : msg, 'params' : params, 'context' : this.context});
120 obj.data.stash('last_print');
123 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
128 if (params && params.content_type) {
129 content_type = params.content_type;
131 content_type = 'text/html';
136 obj.data.init({'via':'stash'});
138 if (typeof obj.data.print_strategy == 'undefined') {
139 obj.data.print_strategy = {};
140 obj.data.stash('print_strategy');
143 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
145 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
147 params.dos_print = true;
149 /* FIXME - this is a kludge.. we're going to sidestep window-based html rendering for printing */
150 /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
151 /* templates aren't guaranteed to be valid xml. The unadulterated msg is still preserved in */
153 if (content_type=='text/html') {
154 w = obj.html2txt(msg);
158 if (! params.no_form_feed) { w = w + '\f'; }
159 obj.NSPrint(w, silent, params);
165 switch(content_type) {
170 var my_prefix = '/xul/server/';
171 if(window.location.protocol == "chrome:") {
172 // Likely in offline interface
173 my_prefix = 'chrome://open_ils_staff_client/content/';
175 if(xulG && xulG.url_prefix) {
176 my_prefix = xulG.url_prefix(my_prefix);
179 var print_url = '<html id="top"><head><title>EG</title>'
180 + '<script src="' + my_prefix + 'util/print_win.js"></script>'
181 + '<script src="' + my_prefix + 'util/print_custom.js"></script>';
182 if(this.data.hash.aous['print.custom_js_file']) {
183 print_url += '<script src="' + this.data.hash.aous['print.custom_js_file'] + '"></script>';
185 print_url += '</head><body onload="try{print_init(\'' + params.type + '\');}catch(E){alert(E);}">' + msg.replace(/<script[^>]*>.*?<\/script>/gi,'') + '</body></html>';
186 print_url = 'data:text/html;charset=utf-8,' + encodeURIComponent(print_url);
187 obj.win.openDialog(print_url,'receipt_temp','chrome,resizable,modal', { "data" : params.data, "list" : params.list}, function(w) {
189 obj.NSPrint(w, silent, params);
191 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);
198 w = obj.win.open('data:' + content_type + ',' + window.encodeURIComponent(msg),'receipt_temp','chrome,resizable');
203 obj.NSPrint(w, silent, params);
205 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);
208 w.minimize(); w.close();
215 this.error.standard_unexpected_error_alert('util.print.simple',E);
219 'tree_list' : function (params) {
221 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
222 dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
228 if (params.context) this.set_context(params.context);
229 if (params.header) s += this.template_sub( params.header, cols, params );
231 // Pre-templating sort
232 // %SORT(field[ AS type][ ASC|DESC][,...])%
233 var sort_blocks = params.line_item.match(/%SORT\([^)]+\)%/g);
235 for(var i = 0; i < sort_blocks.length; i++) {
236 sort_blocks[i] = sort_blocks[i].substring(6,sort_blocks[i].length-2);
238 sort_blocks = sort_blocks.join(',').split(/\s*,\s*/); // Supports %SORT(a,b)% and %SORT(a)% %SORT(b)% methods
239 for(var i = 0; i < sort_blocks.length; i++) {
240 sort_blocks[i] = sort_blocks[i].match(/([^ ]+)(?:\s+AS\s+([^ ]+))?(?:\s+(ASC|DESC))?/);
241 sort_blocks[i].shift(); // Removes the "full match" entry
244 function sorter(a, b) {
246 for(var i = 0; i < sort_blocks.length && return_val == 0; i++) {
247 var sort = sort_blocks[i];
248 var a_test = a[sort[0]];
249 var b_test = b[sort[0]];
250 sort[1] = sort[1] || '';
251 sort[2] = sort[2] || 'ASC';
252 switch(sort[1].toUpperCase()) {
254 a_test = new Date(a_test);
255 b_test = new Date(b_test);
258 a_test = parseInt(a_test);
259 b_test = parseInt(b_test);
263 a_test = parseFloat(a_test);
264 b_test = parseFloat(b_test);
267 a_test = a_test.toLowerCase();
268 b_test = b_test.toLowerCase();
271 a_test = a_test.toUpperCase();
272 b_test = b_test.toUpperCase();
275 if(a_test > b_test) return_val = 1;
276 if(a_test < b_test) return_val = -1;
277 if(sort[2] == 'DESC') return_val *= -1;
281 params.list.sort(sorter);
282 params.line_item = params.line_item.replace(/%SORT\([^)]*\)%/g,'');
285 for (var i = 0; i < params.list.length; i++) {
286 params.row = params.list[i];
288 s += this.template_sub( params.line_item, cols, params );
291 if (params.footer) s += this.template_sub( params.footer, cols, params );
293 // Sanity check, no javascript in templates
294 // Note: [\s\S] is a workaround for . not including newlines.
295 s=s.replace(/<script[^>]*>[\s\S]*?<\/script[^>]*>/gi,'')
296 s=s.replace(/onload\s*=\s*"[^"]*"/gi,'');
297 s=s.replace(/onload\s*=\s*'[^']*'/gi,'');
299 if (params.sample_frame) {
300 var jsrc = 'data:text/javascript,' + encodeURIComponent('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
301 params.sample_frame.setAttribute('src','data:text/html;charset=utf-8,' + encodeURIComponent('<html id="top"><head><title>EG</title><script src="' + jsrc + '"></script></head><body>' + s + '</body></html>'));
303 this.simple(s,params);
305 if(this.context != this.default_context) this.set_context(this.default_context);
308 'template_sub' : function( msg, cols, params ) {
311 if (!msg) { dump('template sub called with empty string\n'); return; }
312 JSAN.use('util.date');
316 // Note that we keep track of already included settings
317 // This ensures that we don't infinite loop through includes
320 var include_patt=/%INCLUDE\(\s*([^)]*?)\s*\)%/;
322 while(match = include_patt.exec(s)) {
323 if(match[1] == '' || included[match[1]]) {
324 s = s.replace(match[0], '');
326 included[match[1]] = true;
327 s = s.replace(new RegExp("%INCLUDE\\(\\s*" + match[1].replace(/([.?*+^$[\]\\(){}-])/g, "\\$1") + "\\s*\\)%","g"), obj.data.hash.aous['circ.staff_client.receipt.' + match[1]] || '');
330 } catch(E) { dump(E+'\n'); }
332 try{b = s; s = s.replace(/%LINE_NO%/g,Number(params.row_idx)+1);}
333 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
335 try{b = s; s = s.replace(/%patron_barcode%/g,this.escape_html(params.patron_barcode));}
336 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
338 try{b = s; s = s.replace(/%LIBRARY%/g,this.escape_html(params.lib.name()));}
339 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
340 try{b = s; s = s.replace(/%PINES_CODE%/g,this.escape_html(params.lib.shortname()));}
341 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
342 try{b = s; s = s.replace(/%SHORTNAME%/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(/%STAFF_FIRSTNAME%/g,this.escape_html(params.staff.first_given_name()));}
345 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
346 try{b = s; s = s.replace(/%STAFF_MIDDLENAME%/g,this.escape_html(params.staff.second_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_MIDDLENAME%/g,this.escape_html(params.patron.second_given_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_LASTNAME%/g,this.escape_html(params.patron.family_name()));}
363 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
364 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() )) ;}
365 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
366 try{b = s; s = s.replace(/%PATRON_EXPIRE_DATE%/g,this.escape_html(params.patron.expire_date()));}
367 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
368 try{b = s; s = s.replace(/%PATRON_EXPIRE_DATE_YMD%/g,util.date.formatted_date(params.patron.expire_date(), '%Y-%m-%d'));}
369 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
371 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
372 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
373 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
374 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
375 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
376 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
377 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
378 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
379 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
380 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
381 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
382 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
383 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
384 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
385 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
386 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
387 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
388 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
389 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
390 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
393 if (typeof params.row != 'undefined') {
394 if (params.row.length >= 0) {
395 alert('debug - please tell the developers that deprecated template code tried to execute');
396 for (var i = 0; i < cols.length; i++) {
397 var re = new RegExp(cols[i],"g");
398 try{b = s; s=s.replace(re, this.escape_html(params.row[i]));}
399 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
402 /* for dump_with_keys */
403 for (var i in params.row) {
404 var re = new RegExp('%'+i+'%',"g");
405 try{b = s; s=s.replace(re, this.escape_html(params.row[i].toString()));}
406 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
411 if (typeof params.data != 'undefined') {
412 for (var i in params.data) {
413 var re = new RegExp('%'+i+'%',"g");
414 if (typeof params.data[i] == 'string' || typeof params.data[i] == 'number') {
415 try{b = s; s=s.replace(re, this.escape_html(params.data[i]));}
416 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
418 /* likely a null, print as an empty string */
419 try{b = s; s=s.replace(re, '');}
420 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
424 } catch(E) { dump(E+'\n'); }
429 var date_format_patt=/%DATE_FORMAT\(\s*([^,]*?)\s*,\s*([^)]*?)\s*\)%/;
430 while(match = date_format_patt.exec(s)) {
431 if(match[1] == '' || match[2] == '')
432 s = s.replace(match[0], '');
434 s = s.replace(match[0], util.date.formatted_date(match[1], match[2]));
436 } catch(E) { dump(E+'\n'); }
441 // Pre-trim inside of substrings, and only inside of them
442 // This keeps the trim commands from being truncated
443 var substr_trim_patt=/(%SUBSTR\(-?\d+,?\s*(-?\d+)?\)%.*?)(\s*%-TRIM%|%TRIM-%\s*)(.*?%SUBSTR_END%)/;
444 while(match = substr_trim_patt.exec(s))
445 s = s.replace(match[0], match[1] + match[4]);
446 // Then do the substrings themselves
447 var substr_patt=/%SUBSTR\((-?\d+),?\s*(-?\d+)?\)%(.*?)%SUBSTR_END%/;
448 while(match = substr_patt.exec(s)) {
449 var substring_start = parseInt(match[1]);
450 if(substring_start < 0) substring_start = match[3].length + substring_start;
451 var substring_length = parseInt(match[2]);
452 if(substring_length > 0)
453 s = s.replace(match[0], match[3].substring(substring_start, substring_start + substring_length));
454 else if(substring_length < 0)
455 s = s.replace(match[0], match[3].substring(substring_start + substring_length, substring_start));
457 s = s.replace(match[0], match[3].substring(substring_start));
459 } catch(E) { dump(E+'\n'); }
461 // Cleanup unwanted whitespace
463 s = s.replace(/%TRIM-%\s*/g,'');
464 s = s.replace(/\s*%-TRIM%/g,'');
465 } catch(E) { dump(E+'\n'); }
469 alert('Error in print.js, template_sub(): ' + E);
474 'NSPrint' : function(w,silent,params) {
478 if (!params) params = {};
480 obj.data.init({'via':'stash'});
482 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
484 dump('params.print_strategy = ' + params.print_strategy
485 + ' || obj.data.print_strategy[' + obj.context + '] = ' + obj.data.print_strategy[obj.context]
486 + ' || obj.data.print_strategy[default] = ' + obj.data.print_strategy['default']
487 + ' => ' + ( params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default'] ) + '\n');
488 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
490 params.dos_print = true;
492 if (typeof w != 'string') {
494 var temp_w = params.msg || w.document.firstChild.innerHTML;
495 if (!params.msg) { params.msg = temp_w; }
496 if (typeof temp_w != 'string') { throw(temp_w); }
497 w = obj.html2txt(temp_w);
499 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
500 w.getSelection().selectAllChildren(w.document.firstChild);
501 w = w.getSelection().toString();
504 obj._NSPrint_custom_print(w,silent,params);
507 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
508 var originalPrinter = false;
509 if (prefs.prefHasUserValue('print.print_printer')) {
510 // This is for restoring print.print_printer after any print dialog, so that when
511 // window.print gets used again, it uses the configured printer for the right context
512 // (which should only be default--window.print is a kludge and is in limited use),
513 // rather than the printer last used.
514 originalPrinter = prefs.getCharPref('print.print_printer');
516 if (typeof w == 'object') {
518 if (originalPrinter) {
519 prefs.setCharPref('print.print_printer',originalPrinter);
522 if (params.content_type == 'text/plain') {
523 w = window.open('data:text/plain;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
525 w = window.open('data:text/html;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
530 if (originalPrinter) {
531 prefs.setCharPref('print.print_printer',originalPrinter);
542 case 'webBrowserPrint':
544 if (typeof w == 'object') {
545 obj._NSPrint_webBrowserPrint(w,silent,params);
547 if (params.content_type == 'text/plain') {
548 w = window.open('data:text/plain;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
550 w = window.open('data:text/html;charset=UTF-8,'+encodeURIComponent(params.msg),'','chrome');
554 obj._NSPrint_webBrowserPrint(w,silent,params);
568 obj._NSPrint_webBrowserPrint(w,silent,params);
572 alert('Probably not printing: ' + e);
573 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
578 '_NSPrint_custom_print' : function(w,silent,params) {
583 var html = params.msg || w;
585 var txt_file = new util.file('receipt.txt');
586 txt_file.write_content('truncate',text);
587 var text_path = '"' + txt_file._file.path + '"';
590 var html_file = new util.file('receipt.html');
591 html_file.write_content('truncate',html);
592 var html_path = '"' + html_file._file.path + '"';
595 var cmd = params.dos_print ?
596 'copy ' + text_path + ' lpt1 /b\n'
597 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
600 file = new util.file('receipt.bat');
601 file.write_content('truncate+exec',cmd);
603 file = new util.file('receipt.bat');
605 dump('print exec: ' + cmd + '\n');
606 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
607 process.init(file._file);
611 dump('process.run = ' + process.run(true, args, args.length) + '\n');
616 //alert('Probably not printing: ' + e);
617 this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
621 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
624 var webBrowserPrint = w
625 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
626 .getInterface(Components.interfaces.nsIWebBrowserPrint);
627 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
628 if (webBrowserPrint) {
629 var gPrintSettings = obj.GetPrintSettings();
630 if (silent) gPrintSettings.printSilent = true;
631 else gPrintSettings.printSilent = false;
633 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
635 webBrowserPrint.print(gPrintSettings, null);
636 this.error.sdump('D_PRINT','Should be printing\n');
638 this.error.sdump('D_ERROR','Should not be printing\n');
641 //alert('Probably not printing: ' + e);
642 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
643 // causing an exception to be thrown which we catch here.
644 // Unfortunately this will also consume helpful failures
645 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
649 'GetPrintSettings' : function() {
651 //alert('entering GetPrintSettings');
652 var pref = Components.classes["@mozilla.org/preferences-service;1"]
653 .getService(Components.interfaces.nsIPrefBranch);
654 //alert('pref = ' + pref);
656 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
657 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
658 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
661 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
662 .getService(Components.interfaces.nsIPrintSettingsService);
663 if (this.gPrintSettingsAreGlobal) {
664 this.gPrintSettings = printService.globalPrintSettings;
665 //alert('called setPrinterDefaultsForSelectedPrinter');
666 this.setPrinterDefaultsForSelectedPrinter(printService);
668 this.gPrintSettings = printService.newPrintSettings;
669 //alert('used printService.newPrintSettings');
672 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
673 //alert("GetPrintSettings() "+e+"\n");
676 return this.gPrintSettings;
679 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
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 this.GetPrintSettings();
704 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
705 PO.ShowPrintSetupDialog(this.gPrintSettings);
707 this.error.standard_unexpected_error_alert("page_settings()",E);
711 'load_settings' : function() {
714 var file = new util.file('gPrintSettings.' + this.context);
715 if (file._file.exists()) {
716 temp = file.get_object(); file.close();
717 for (var i in temp) {
718 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
720 } else if (this.context != 'default') {
721 var file = new util.file('gPrintSettings.default');
722 if (file._file.exists()) {
723 temp = file.get_object(); file.close();
724 for (var i in temp) {
725 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
728 this.gPrintSettings.marginTop = 0;
729 this.gPrintSettings.marginLeft = 0;
730 this.gPrintSettings.marginBottom = 0;
731 this.gPrintSettings.marginRight = 0;
732 this.gPrintSettings.headerStrLeft = '';
733 this.gPrintSettings.headerStrCenter = '';
734 this.gPrintSettings.headerStrRight = '';
735 this.gPrintSettings.footerStrLeft = '';
736 this.gPrintSettings.footerStrCenter = '';
737 this.gPrintSettings.footerStrRight = '';
741 this.error.sdump('D_PRINT',error_msg);
742 this.error.yns_alert(
743 document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
744 document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
745 document.getElementById('offlineStrings').getString('common.ok'),
752 this.error.standard_unexpected_error_alert("load_settings()",E);
756 'save_settings' : function() {
759 var file = new util.file('gPrintSettings.' + this.context);
760 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
761 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
763 if (this.context == 'default') {
764 // print.print_printer gets used by bare window.print()'s. We sometimes use window.print for the
765 // WebBrowserPrint strategy to workaround bugs with the NSPrint xpcom, and only in the default context.
766 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
767 prefs.setCharPref('print.print_printer',obj.gPrintSettings.printerName);
770 this.error.standard_unexpected_error_alert("save_settings()",E);
775 dump('exiting util/print.js\n');