1 dump('entering util/print.js\n');
3 if (typeof util == 'undefined') util = {};
4 util.print = function () {
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 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
15 var key = 'oils.printer.external.cmd';
16 var has_key = prefs.prefHasUserValue(key);
17 this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
22 util.print.prototype = {
24 'reprint_last' : function() {
26 var obj = this; obj.data.init({'via':'stash'});
27 if (!obj.data.last_print) {
29 document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
33 var msg = obj.data.last_print.msg;
34 var params = obj.data.last_print.params; params.no_prompt = false;
35 obj.simple( msg, params );
37 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
41 'html2txt' : function(html) {
42 JSAN.use('util.text');
43 //dump('html2txt, before:\n' + html + '\n');
44 var lines = html.split(/\n/);
46 for (var i = 0; i < lines.length; i++) {
49 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
50 line = util.text.reverse_preserve_string_in_html(line);
51 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
52 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
54 var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
55 for (var j = 0; j < hex_chars.length; j++) {
56 raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
58 return p1 + p3 + raw_chars;
60 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
61 line = line.replace(/<br.*?>/gi,'\r\n');
62 line = line.replace(/<table.*?>/gi,'');
63 line = line.replace(/<tr.*?>/gi,'');
64 line = line.replace(/<hr.*?>/gi,'\r\n');
65 line = line.replace(/<p.*?>/gi,'');
66 line = line.replace(/<block.*?>/gi,'');
67 line = line.replace(/<li.*?>/gi,' * ');
68 line = line.replace(/<.+?>/gi,'');
69 if (line) { new_lines.push(line); }
74 var new_html = new_lines.join('\n');
75 //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
79 'simple' : function(msg,params) {
81 if (!params) params = {};
86 obj.data.last_print = { 'msg' : msg, 'params' : params}; obj.data.stash('last_print');
89 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
94 if (params && params.content_type) {
95 content_type = params.content_type;
97 content_type = 'text/html';
102 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
103 obj.data.init({'via':'stash'});
105 if (params.print_strategy || obj.data.print_strategy) {
107 switch(params.print_strategy || obj.data.print_strategy) {
109 params.dos_print = true;
111 /* FIXME - this it a kludge.. we're going to sidestep window-based html rendering for printing */
112 /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
113 /* templates aren't guaranteed to be valid xml. The unadulterated msg is still preserved in */
115 if (content_type=='text/html') {
116 w = obj.html2txt(msg);
120 if (! params.no_form_feed) { w = w + '\f'; }
121 obj.NSPrint(w, silent, params);
127 switch(content_type) {
129 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 */ }');
130 w = obj.win.open('data:text/html,<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head><body onload="try{my_init();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>','receipt_temp','chrome,resizable');
132 w.go_print = function() {
134 obj.NSPrint(w, silent, params);
136 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);
139 w.minimize(); w.close();
143 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
148 obj.NSPrint(w, silent, params);
150 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);
153 w.minimize(); w.close();
160 this.error.standard_unexpected_error_alert('util.print.simple',E);
164 'tree_list' : function (params) {
166 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
167 dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
173 if (params.header) s += this.template_sub( params.header, cols, params );
175 for (var i = 0; i < params.list.length; i++) {
176 params.row = params.list[i];
178 s += this.template_sub( params.line_item, cols, params );
181 if (params.footer) s += this.template_sub( params.footer, cols, params );
183 if (params.sample_frame) {
184 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
185 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>');
187 this.simple(s,params);
191 'template_sub' : function( msg, cols, params ) {
194 if (!msg) { dump('template sub called with empty string\n'); return; }
195 JSAN.use('util.date');
198 try{b = s; s = s.replace(/%LINE_NO%/,Number(params.row_idx)+1);}
199 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
201 try{b = s; s = s.replace(/%patron_barcode%/,params.patron_barcode);}
202 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
204 try{b = s; s = s.replace(/%LIBRARY%/,params.lib.name());}
205 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
206 try{b = s; s = s.replace(/%PINES_CODE%/,params.lib.shortname());}
207 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
208 try{b = s; s = s.replace(/%SHORTNAME%/,params.lib.shortname());}
209 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
210 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/,params.staff.first_given_name());}
211 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
212 try{b = s; s = s.replace(/%STAFF_LASTNAME%/,params.staff.family_name());}
213 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
214 try{b = s; s = s.replace(/%STAFF_BARCODE%/,params.staff.barcode); }
215 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
216 try{b = s; s = s.replace(/%STAFF_PROFILE%/,obj.data.hash.pgt[ params.staff.profile() ].name() ); }
217 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
218 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());}
219 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
220 try{b = s; s = s.replace(/%PATRON_ALIAS%/,(params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias());}
221 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
222 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/,params.patron.first_given_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_LASTNAME%/,params.patron.family_name());}
225 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
226 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() ) ;}
227 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
229 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
230 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
231 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
232 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
233 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
234 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
235 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
236 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
237 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
238 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
239 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
240 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
241 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
242 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
243 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
244 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
245 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
246 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
247 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
248 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
251 if (typeof params.row != 'undefined') {
252 if (params.row.length >= 0) {
253 alert('debug - please tell the developers that deprecated template code tried to execute');
254 for (var i = 0; i < cols.length; i++) {
255 var re = new RegExp(cols[i],"g");
256 try{b = s; s=s.replace(re, params.row[i]);}
257 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
260 /* for dump_with_keys */
261 for (var i in params.row) {
262 var re = new RegExp('%'+i+'%',"g");
263 try{b = s; s=s.replace(re, params.row[i]);}
264 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
269 if (typeof params.data != 'undefined') {
270 for (var i in params.data) {
271 var re = new RegExp('%'+i+'%',"g");
272 if (typeof params.data[i] == 'string') {
273 try{b = s; s=s.replace(re, params.data[i]);}
274 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
278 } catch(E) { dump(E+'\n'); }
282 alert('Error in print.js, template_sub(): ' + E);
287 'NSPrint' : function(w,silent,params) {
291 if (!params) params = {};
293 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
294 obj.data.init({'via':'stash'});
296 if (params.print_strategy || obj.data.print_strategy) {
298 dump('params.print_strategy = ' + params.print_strategy + ' || obj.data.print_strategy = ' + obj.data.print_strategy + ' => ' + ( params.print_strategy || obj.data.print_strategy ) + '\n');
299 switch(params.print_strategy || obj.data.print_strategy) {
302 if (typeof w != 'string') {
304 var temp_w = params.msg || w.document.firstChild.innerHTML;
305 if (!params.msg) { params.msg = temp_w; }
306 if (typeof temp_w != 'string') { throw(temp_w); }
307 w = obj.html2txt(temp_w);
309 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
310 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
311 w.getSelection().selectAllChildren(w.document.firstChild);
312 w = w.getSelection().toString();
315 obj._NSPrint_custom_print(w,silent,params);
321 w = window.open('data:text/plain,'+escape(params.msg));
334 case 'webBrowserPrint':
337 obj._NSPrint_webBrowserPrint(w,silent,params);
339 w = window.open('data:text/plain,'+escape(params.msg));
342 obj._NSPrint_webBrowserPrint(w,silent,params);
356 obj._NSPrint_webBrowserPrint(w,silent,params);
360 alert('Probably not printing: ' + e);
361 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
366 '_NSPrint_custom_print' : function(w,silent,params) {
371 var html = params.msg || w;
373 var txt_file = new util.file('receipt.txt');
374 txt_file.write_content('truncate',text);
375 var text_path = '"' + txt_file._file.path + '"';
378 var html_file = new util.file('receipt.html');
379 html_file.write_content('truncate',html);
380 var html_path = '"' + html_file._file.path + '"';
383 var cmd = params.dos_print ?
384 'copy ' + text_path + ' lpt1 /b\n'
385 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
388 file = new util.file('receipt.bat');
389 file.write_content('truncate+exec',cmd);
391 file = new util.file('receipt.bat');
393 dump('print exec: ' + cmd + '\n');
394 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
395 process.init(file._file);
399 dump('process.run = ' + process.run(true, args, args.length) + '\n');
404 //alert('Probably not printing: ' + e);
405 this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
409 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
412 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
413 var webBrowserPrint = w
414 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
415 .getInterface(Components.interfaces.nsIWebBrowserPrint);
416 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
417 if (webBrowserPrint) {
418 var gPrintSettings = obj.GetPrintSettings();
419 if (silent) gPrintSettings.printSilent = true;
420 else gPrintSettings.printSilent = false;
422 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
424 webBrowserPrint.print(gPrintSettings, null);
425 this.error.sdump('D_PRINT','Should be printing\n');
427 this.error.sdump('D_ERROR','Should not be printing\n');
430 //alert('Probably not printing: ' + e);
431 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
432 // causing an exception to be thrown which we catch here.
433 // Unfortunately this will also consume helpful failures
434 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
438 'GetPrintSettings' : function() {
440 //alert('entering GetPrintSettings');
441 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
442 var pref = Components.classes["@mozilla.org/preferences-service;1"]
443 .getService(Components.interfaces.nsIPrefBranch);
444 //alert('pref = ' + pref);
446 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
447 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
448 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
451 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
452 .getService(Components.interfaces.nsIPrintSettingsService);
453 if (this.gPrintSettingsAreGlobal) {
454 this.gPrintSettings = printService.globalPrintSettings;
455 //alert('called setPrinterDefaultsForSelectedPrinter');
456 this.setPrinterDefaultsForSelectedPrinter(printService);
458 this.gPrintSettings = printService.newPrintSettings;
459 //alert('used printService.newPrintSettings');
462 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
463 //alert("GetPrintSettings() "+e+"\n");
466 return this.gPrintSettings;
469 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
471 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
472 if (this.gPrintSettings.printerName == "") {
473 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
474 //alert('used .defaultPrinterName');
476 //alert('printerName = ' + this.gPrintSettings.printerName);
478 // First get any defaults from the printer
479 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
481 // now augment them with any values from last time
482 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
484 // now augment from our own saved settings if they exist
485 this.load_settings();
488 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
492 'page_settings' : function() {
494 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
495 this.GetPrintSettings();
496 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
497 PO.ShowPrintSetupDialog(this.gPrintSettings);
499 this.error.standard_unexpected_error_alert("page_settings()",E);
503 'load_settings' : function() {
506 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
507 var file = new util.file('gPrintSettings');
508 if (file._file.exists()) {
509 temp = file.get_object(); file.close();
510 for (var i in temp) {
511 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
514 this.gPrintSettings.marginTop = 0;
515 this.gPrintSettings.marginLeft = 0;
516 this.gPrintSettings.marginBottom = 0;
517 this.gPrintSettings.marginRight = 0;
518 this.gPrintSettings.headerStrLeft = '';
519 this.gPrintSettings.headerStrCenter = '';
520 this.gPrintSettings.headerStrRight = '';
521 this.gPrintSettings.footerStrLeft = '';
522 this.gPrintSettings.footerStrCenter = '';
523 this.gPrintSettings.footerStrRight = '';
526 this.error.sdump('D_PRINT',error_msg);
527 this.error.yns_alert(
528 document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
529 document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
530 document.getElementById('offlineStrings').getString('common.ok'),
537 this.error.standard_unexpected_error_alert("load_settings()",E);
541 'save_settings' : function() {
544 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
545 var file = new util.file('gPrintSettings');
546 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
547 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
550 this.error.standard_unexpected_error_alert("save_settings()",E);
555 dump('exiting util/print.js\n');