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);
320 case 'webBrowserPrint':
321 obj._NSPrint_webBrowserPrint(w,silent,params);
325 obj._NSPrint_webBrowserPrint(w,silent,params);
331 obj._NSPrint_webBrowserPrint(w,silent,params);
335 alert('Probably not printing: ' + e);
336 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
341 '_NSPrint_custom_print' : function(w,silent,params) {
346 var html = params.msg || w;
348 var txt_file = new util.file('receipt.txt');
349 txt_file.write_content('truncate',text);
350 var text_path = '"' + txt_file._file.path + '"';
353 var html_file = new util.file('receipt.html');
354 html_file.write_content('truncate',html);
355 var html_path = '"' + html_file._file.path + '"';
358 var cmd = params.dos_print ?
359 'copy ' + text_path + ' lpt1 /b\n'
360 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
363 file = new util.file('receipt.bat');
364 file.write_content('truncate+exec',cmd);
366 file = new util.file('receipt.bat');
368 dump('print exec: ' + cmd + '\n');
369 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
370 process.init(file._file);
374 dump('process.run = ' + process.run(true, args, args.length) + '\n');
379 //alert('Probably not printing: ' + e);
380 this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
384 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
387 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
388 var webBrowserPrint = w
389 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
390 .getInterface(Components.interfaces.nsIWebBrowserPrint);
391 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
392 if (webBrowserPrint) {
393 var gPrintSettings = obj.GetPrintSettings();
394 if (silent) gPrintSettings.printSilent = true;
395 else gPrintSettings.printSilent = false;
397 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
399 webBrowserPrint.print(gPrintSettings, null);
400 this.error.sdump('D_PRINT','Should be printing\n');
402 this.error.sdump('D_ERROR','Should not be printing\n');
405 //alert('Probably not printing: ' + e);
406 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
407 // causing an exception to be thrown which we catch here.
408 // Unfortunately this will also consume helpful failures
409 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
413 'GetPrintSettings' : function() {
415 //alert('entering GetPrintSettings');
416 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
417 var pref = Components.classes["@mozilla.org/preferences-service;1"]
418 .getService(Components.interfaces.nsIPrefBranch);
419 //alert('pref = ' + pref);
421 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
422 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
423 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
426 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
427 .getService(Components.interfaces.nsIPrintSettingsService);
428 if (this.gPrintSettingsAreGlobal) {
429 this.gPrintSettings = printService.globalPrintSettings;
430 //alert('called setPrinterDefaultsForSelectedPrinter');
431 this.setPrinterDefaultsForSelectedPrinter(printService);
433 this.gPrintSettings = printService.newPrintSettings;
434 //alert('used printService.newPrintSettings');
437 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
438 //alert("GetPrintSettings() "+e+"\n");
441 return this.gPrintSettings;
444 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
446 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
447 if (this.gPrintSettings.printerName == "") {
448 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
449 //alert('used .defaultPrinterName');
451 //alert('printerName = ' + this.gPrintSettings.printerName);
453 // First get any defaults from the printer
454 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
456 // now augment them with any values from last time
457 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
459 // now augment from our own saved settings if they exist
460 this.load_settings();
463 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
467 'page_settings' : function() {
469 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
470 this.GetPrintSettings();
471 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
472 PO.ShowPrintSetupDialog(this.gPrintSettings);
474 this.error.standard_unexpected_error_alert("page_settings()",E);
478 'load_settings' : function() {
481 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
482 var file = new util.file('gPrintSettings');
483 if (file._file.exists()) {
484 temp = file.get_object(); file.close();
485 for (var i in temp) {
486 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
489 this.gPrintSettings.marginTop = 0;
490 this.gPrintSettings.marginLeft = 0;
491 this.gPrintSettings.marginBottom = 0;
492 this.gPrintSettings.marginRight = 0;
493 this.gPrintSettings.headerStrLeft = '';
494 this.gPrintSettings.headerStrCenter = '';
495 this.gPrintSettings.headerStrRight = '';
496 this.gPrintSettings.footerStrLeft = '';
497 this.gPrintSettings.footerStrCenter = '';
498 this.gPrintSettings.footerStrRight = '';
501 this.error.sdump('D_PRINT',error_msg);
502 this.error.yns_alert(
503 document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
504 document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
505 document.getElementById('offlineStrings').getString('common.ok'),
512 this.error.standard_unexpected_error_alert("load_settings()",E);
516 'save_settings' : function() {
519 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
520 var file = new util.file('gPrintSettings');
521 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
522 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
525 this.error.standard_unexpected_error_alert("save_settings()",E);
530 dump('exiting util/print.js\n');