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.context = context || 'default';
16 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
17 var key = 'oils.printer.external.cmd.' + this.context;
18 var has_key = prefs.prefHasUserValue(key);
19 this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
24 util.print.prototype = {
26 'reprint_last' : function() {
28 var obj = this; obj.data.init({'via':'stash'});
29 if (!obj.data.last_print) {
31 document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
35 var msg = obj.data.last_print.msg;
36 var params = obj.data.last_print.params; params.no_prompt = false;
37 obj.simple( msg, params );
39 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
43 'html2txt' : function(html) {
44 JSAN.use('util.text');
45 //dump('html2txt, before:\n' + html + '\n');
46 var lines = html.split(/\n/);
48 for (var i = 0; i < lines.length; i++) {
51 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
52 line = util.text.reverse_preserve_string_in_html(line);
53 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
54 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
56 var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
57 for (var j = 0; j < hex_chars.length; j++) {
58 raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
60 return p1 + p3 + raw_chars;
62 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
63 line = line.replace(/<br.*?>/gi,'\r\n');
64 line = line.replace(/<table.*?>/gi,'');
65 line = line.replace(/<tr.*?>/gi,'');
66 line = line.replace(/<hr.*?>/gi,'\r\n');
67 line = line.replace(/<p.*?>/gi,'');
68 line = line.replace(/<block.*?>/gi,'');
69 line = line.replace(/<li.*?>/gi,' * ');
70 line = line.replace(/<.+?>/gi,'');
71 if (line) { new_lines.push(line); }
76 var new_html = new_lines.join('\n');
77 //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
81 'simple' : function(msg,params) {
83 if (!params) params = {};
88 obj.data.last_print = { 'msg' : msg, 'params' : params, 'context' : this.context};
89 obj.data.stash('last_print');
92 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
97 if (params && params.content_type) {
98 content_type = params.content_type;
100 content_type = 'text/html';
105 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
106 obj.data.init({'via':'stash'});
108 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
110 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
112 params.dos_print = true;
114 /* FIXME - this it a kludge.. we're going to sidestep window-based html rendering for printing */
115 /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
116 /* templates aren't guaranteed to be valid xml. The unadulterated msg is still preserved in */
118 if (content_type=='text/html') {
119 w = obj.html2txt(msg);
123 if (! params.no_form_feed) { w = w + '\f'; }
124 obj.NSPrint(w, silent, params);
130 switch(content_type) {
132 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '}; function my_init() { if (typeof go_print == "function") { go_print(); } else { setTimeout( function() { if (typeof go_print == "function") { alert("Please tell the developers that the 2-second go_print workaround executed, and let them know whether this job printed successfully. Thanks!"); go_print(); } else { alert("Please tell the developers that the 2-second go_print workaround did not work. We will try to print one more time; there have been reports of wasted receipt paper at this point. Please check the settings in the print dialog and/or prepare to power off your printer. Thanks!"); window.print(); } }, 2000 ); } /* FIXME - mozilla bug#301560 - xpcom kills it too */ }');
133 var print_url = 'data:text/html,'
134 + '<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head>'
135 + '<body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>';
136 w = obj.win.open(print_url,'receipt_temp','chrome,resizable');
138 w.go_print = function() {
140 obj.NSPrint(w, silent, params);
142 obj.error.standard_unexpected_error_alert("Print Error in util.print.simple. After this dialog we'll try a second print attempt. content_type = " + content_type,E);
145 w.minimize(); w.close();
149 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
154 obj.NSPrint(w, silent, params);
156 obj.error.standard_unexpected_error_alert("Print Error in util.print.simple. After this dialog we'll try a second print attempt. content_type = " + content_type,E);
159 w.minimize(); w.close();
166 this.error.standard_unexpected_error_alert('util.print.simple',E);
170 'tree_list' : function (params) {
172 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
173 dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
179 if (params.header) s += this.template_sub( params.header, cols, params );
181 for (var i = 0; i < params.list.length; i++) {
182 params.row = params.list[i];
184 s += this.template_sub( params.line_item, cols, params );
187 if (params.footer) s += this.template_sub( params.footer, cols, params );
189 if (params.sample_frame) {
190 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
191 params.sample_frame.setAttribute('src','data:text/html,<html id="top"><head><script src="' + window.escape(jsrc) + '"></script></head><body>' + window.escape(s) + '</body></html>');
193 this.simple(s,params);
197 'template_sub' : function( msg, cols, params ) {
200 if (!msg) { dump('template sub called with empty string\n'); return; }
201 JSAN.use('util.date');
204 try{b = s; s = s.replace(/%LINE_NO%/,Number(params.row_idx)+1);}
205 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
207 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
208 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
210 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
211 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
212 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
213 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
214 try{b = s; s = s.replace(/%SHORTNAME%/,params.lib.shortname());}
215 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
216 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
217 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
218 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
219 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
220 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
221 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
222 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
223 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
224 try{b = s; s = s.replace(/%PATRON_ALIAS_OR_FIRSTNAME%/,(params.patron.alias() == '' || params.patron.alias() == null) ? params.patron.first_given_name() : params.patron.alias());}
225 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
226 try{b = s; s = s.replace(/%PATRON_ALIAS%/,(params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias());}
227 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
228 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_name());}
229 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
230 try{b = s; s = s.replace(/%PATRON_LASTNAME%/,params.patron.family_name());}
231 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
232 try{b = s; s = s.replace(/%PATRON_BARCODE%/,typeof params.patron.card() == 'object' ? params.patron.card().barcode() : util.functional.find_id_object_in_list( params.patron.cards(), params.patron.card() ).barcode() ) ;}
233 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
235 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
236 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
237 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
238 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
239 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
240 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
241 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
242 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
243 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
244 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
245 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
246 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
247 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
248 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
249 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
250 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
251 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
252 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
253 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
254 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
257 if (typeof params.row != 'undefined') {
258 if (params.row.length >= 0) {
259 alert('debug - please tell the developers that deprecated template code tried to execute');
260 for (var i = 0; i < cols.length; i++) {
261 var re = new RegExp(cols[i],"g");
262 try{b = s; s=s.replace(re, params.row[i]);}
263 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
266 /* for dump_with_keys */
267 for (var i in params.row) {
268 var re = new RegExp('%'+i+'%',"g");
269 try{b = s; s=s.replace(re, params.row[i]);}
270 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
275 if (typeof params.data != 'undefined') {
276 for (var i in params.data) {
277 var re = new RegExp('%'+i+'%',"g");
278 if (typeof params.data[i] == 'string') {
279 try{b = s; s=s.replace(re, params.data[i]);}
280 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
284 } catch(E) { dump(E+'\n'); }
288 alert('Error in print.js, template_sub(): ' + E);
293 'NSPrint' : function(w,silent,params) {
297 if (!params) params = {};
299 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
300 obj.data.init({'via':'stash'});
302 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
304 dump('params.print_strategy = ' + params.print_strategy
305 + ' || obj.data.print_strategy[' + obj.context + '] = ' + obj.data.print_strategy[obj.context]
306 + ' || obj.data.print_strategy[default] = ' + obj.data.print_strategy['default']
307 + ' => ' + ( params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default'] ) + '\n');
308 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
311 if (typeof w != 'string') {
313 var temp_w = params.msg || w.document.firstChild.innerHTML;
314 if (!params.msg) { params.msg = temp_w; }
315 if (typeof temp_w != 'string') { throw(temp_w); }
316 w = obj.html2txt(temp_w);
318 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
319 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
320 w.getSelection().selectAllChildren(w.document.firstChild);
321 w = w.getSelection().toString();
324 obj._NSPrint_custom_print(w,silent,params);
327 if (typeof w == 'object') {
330 if (params.content_type == 'text/plain') {
331 w = window.open('data:text/plain,'+escape(params.msg));
333 w = window.open('data:text/html,'+escape(params.msg));
347 case 'webBrowserPrint':
349 if (typeof w == 'object') {
350 obj._NSPrint_webBrowserPrint(w,silent,params);
352 if (params.content_type == 'text/plain') {
353 w = window.open('data:text/plain,'+escape(params.msg));
355 w = window.open('data:text/html,'+escape(params.msg));
359 obj._NSPrint_webBrowserPrint(w,silent,params);
373 obj._NSPrint_webBrowserPrint(w,silent,params);
377 alert('Probably not printing: ' + e);
378 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
383 '_NSPrint_custom_print' : function(w,silent,params) {
388 var html = params.msg || w;
390 var txt_file = new util.file('receipt.txt');
391 txt_file.write_content('truncate',text);
392 var text_path = '"' + txt_file._file.path + '"';
395 var html_file = new util.file('receipt.html');
396 html_file.write_content('truncate',html);
397 var html_path = '"' + html_file._file.path + '"';
400 var cmd = params.dos_print ?
401 'copy ' + text_path + ' lpt1 /b\n'
402 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
405 file = new util.file('receipt.bat');
406 file.write_content('truncate+exec',cmd);
408 file = new util.file('receipt.bat');
410 dump('print exec: ' + cmd + '\n');
411 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
412 process.init(file._file);
416 dump('process.run = ' + process.run(true, args, args.length) + '\n');
421 //alert('Probably not printing: ' + e);
422 this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
426 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
429 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
430 var webBrowserPrint = w
431 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
432 .getInterface(Components.interfaces.nsIWebBrowserPrint);
433 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
434 if (webBrowserPrint) {
435 var gPrintSettings = obj.GetPrintSettings();
436 if (silent) gPrintSettings.printSilent = true;
437 else gPrintSettings.printSilent = false;
439 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
441 webBrowserPrint.print(gPrintSettings, null);
442 this.error.sdump('D_PRINT','Should be printing\n');
444 this.error.sdump('D_ERROR','Should not be printing\n');
447 //alert('Probably not printing: ' + e);
448 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
449 // causing an exception to be thrown which we catch here.
450 // Unfortunately this will also consume helpful failures
451 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
455 'GetPrintSettings' : function() {
457 //alert('entering GetPrintSettings');
458 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
459 var pref = Components.classes["@mozilla.org/preferences-service;1"]
460 .getService(Components.interfaces.nsIPrefBranch);
461 //alert('pref = ' + pref);
463 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
464 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
465 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
468 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
469 .getService(Components.interfaces.nsIPrintSettingsService);
470 if (this.gPrintSettingsAreGlobal) {
471 this.gPrintSettings = printService.globalPrintSettings;
472 //alert('called setPrinterDefaultsForSelectedPrinter');
473 this.setPrinterDefaultsForSelectedPrinter(printService);
475 this.gPrintSettings = printService.newPrintSettings;
476 //alert('used printService.newPrintSettings');
479 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
480 //alert("GetPrintSettings() "+e+"\n");
483 return this.gPrintSettings;
486 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
488 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
489 if (this.gPrintSettings.printerName == "") {
490 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
491 //alert('used .defaultPrinterName');
493 //alert('printerName = ' + this.gPrintSettings.printerName);
495 // First get any defaults from the printer
496 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
498 // now augment them with any values from last time
499 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
501 // now augment from our own saved settings if they exist
502 this.load_settings();
505 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
509 'page_settings' : function() {
511 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
512 this.GetPrintSettings();
513 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
514 PO.ShowPrintSetupDialog(this.gPrintSettings);
516 this.error.standard_unexpected_error_alert("page_settings()",E);
520 'load_settings' : function() {
523 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
524 var file = new util.file('gPrintSettings.' + this.context);
525 if (file._file.exists()) {
526 temp = file.get_object(); file.close();
527 for (var i in temp) {
528 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
530 } else if (this.context != 'default') {
531 var file = new util.file('gPrintSettings.default');
532 if (file._file.exists()) {
533 temp = file.get_object(); file.close();
534 for (var i in temp) {
535 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
538 this.gPrintSettings.marginTop = 0;
539 this.gPrintSettings.marginLeft = 0;
540 this.gPrintSettings.marginBottom = 0;
541 this.gPrintSettings.marginRight = 0;
542 this.gPrintSettings.headerStrLeft = '';
543 this.gPrintSettings.headerStrCenter = '';
544 this.gPrintSettings.headerStrRight = '';
545 this.gPrintSettings.footerStrLeft = '';
546 this.gPrintSettings.footerStrCenter = '';
547 this.gPrintSettings.footerStrRight = '';
551 this.error.sdump('D_PRINT',error_msg);
552 this.error.yns_alert(
553 document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
554 document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
555 document.getElementById('offlineStrings').getString('common.ok'),
562 this.error.standard_unexpected_error_alert("load_settings()",E);
566 'save_settings' : function() {
569 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
570 var file = new util.file('gPrintSettings.' + this.context);
571 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
572 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
575 this.error.standard_unexpected_error_alert("save_settings()",E);
580 dump('exiting util/print.js\n');