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 if(!has_key && this.context != 'default') {
20 key = 'oils.printer.external.cmd.default';
21 has_key = prefs.prefHasUserValue(key);
23 this.oils_printer_external_cmd = has_key ? prefs.getCharPref(key) : '';
26 if (prefs.prefHasUserValue('print.always_print_silent')) {
27 if (! prefs.getBoolPref('print.always_print_silent')) {
28 prefs.clearUserPref('print.always_print_silent');
32 dump('Error in print.js trying to clear print.always_print_silent\n');
38 util.print.prototype = {
40 'reprint_last' : function() {
42 var obj = this; obj.data.init({'via':'stash'});
43 if (!obj.data.last_print) {
45 document.getElementById('offlineStrings').getString('printing.nothing_to_reprint')
49 var msg = obj.data.last_print.msg;
50 var params = obj.data.last_print.params; params.no_prompt = false;
51 obj.simple( msg, params );
53 this.error.standard_unexpected_error_alert('util.print.reprint_last',E);
57 'html2txt' : function(html) {
58 JSAN.use('util.text');
59 //dump('html2txt, before:\n' + html + '\n');
60 var lines = html.split(/\n/);
62 for (var i = 0; i < lines.length; i++) {
65 // This undoes the util.text.preserve_string_in_html call that spine_label.js does
66 line = util.text.reverse_preserve_string_in_html(line);
67 // This looks for @hex attributes containing 2-digit hex codes, and converts them into real characters
68 line = line.replace(/(<.+?)hex=['"](.+?)['"](.*?>)/gi, function(str,p1,p2,p3,offset,s) {
70 var hex_chars = p2.match(/[0-9,a-f,A-F][0-9,a-f,A-F]/g);
71 for (var j = 0; j < hex_chars.length; j++) {
72 raw_chars += String.fromCharCode( parseInt(hex_chars[j],16) );
74 return p1 + p3 + raw_chars;
76 line = line.replace(/<head.*?>.*?<\/head>/gi, '');
77 line = line.replace(/<br.*?>/gi,'\r\n');
78 line = line.replace(/<table.*?>/gi,'');
79 line = line.replace(/<tr.*?>/gi,'');
80 line = line.replace(/<hr.*?>/gi,'\r\n');
81 line = line.replace(/<p.*?>/gi,'');
82 line = line.replace(/<block.*?>/gi,'');
83 line = line.replace(/<li.*?>/gi,' * ');
84 line = line.replace(/<.+?>/gi,'');
85 line = line.replace(/</gi,'<');
86 line = line.replace(/>/gi,'>');
87 line = line.replace(/&/gi,'&');
88 if (line) { new_lines.push(line); }
93 var new_html = new_lines.join('\n');
94 //dump('html2txt, after:\n' + new_html + '\nhtml2txt, done.\n');
98 'escape_html' : function(data) {
99 return data.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>');
102 'simple' : function(msg,params) {
104 if (!params) params = {};
109 obj.data.last_print = { 'msg' : msg, 'params' : params, 'context' : this.context};
110 obj.data.stash('last_print');
113 if ( params && params.no_prompt && (params.no_prompt == true || params.no_prompt == 'true') ) {
118 if (params && params.content_type) {
119 content_type = params.content_type;
121 content_type = 'text/html';
126 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
127 obj.data.init({'via':'stash'});
129 if (typeof obj.data.print_strategy == 'undefined') {
130 obj.data.print_strategy = {};
131 obj.data.stash('print_strategy');
134 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
136 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
138 params.dos_print = true;
140 /* FIXME - this it a kludge.. we're going to sidestep window-based html rendering for printing */
141 /* I'm using regexps to mangle the html receipt templates; it'd be nice to use xsl but the */
142 /* templates aren't guaranteed to be valid xml. The unadulterated msg is still preserved in */
144 if (content_type=='text/html') {
145 w = obj.html2txt(msg);
149 if (! params.no_form_feed) { w = w + '\f'; }
150 obj.NSPrint(w, silent, params);
156 switch(content_type) {
158 var jsrc = 'data:text/javascript,' + window.escape('var params = window.arguments[0]; window.go_print = window.arguments[1];');
159 var print_url = 'data:text/html,'
160 + '<html id="top"><head><script src="/xul/server/main/JSAN.js"></script><script src="' + window.escape(jsrc) + '"></script></head>'
161 + '<body onload="try{go_print();}catch(E){alert(E);}">' + window.escape(msg) + '</body></html>';
162 w = obj.win.openDialog(print_url,'receipt_temp','chrome,resizable,minimizable', null, { "data" : params.data, "list" : params.list}, function() {
164 obj.NSPrint(w, silent, params);
166 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);
169 w.minimize(); w.close();
174 w = obj.win.open('data:' + content_type + ',' + window.escape(msg),'receipt_temp','chrome,resizable');
179 obj.NSPrint(w, silent, params);
181 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);
184 w.minimize(); w.close();
191 this.error.standard_unexpected_error_alert('util.print.simple',E);
195 'tree_list' : function (params) {
197 dump('print.tree_list.params.list = \n' + this.error.pretty_print(js2JSON(params.list)) + '\n');
198 dump('print.tree_list.params.data = \n' + this.error.pretty_print(js2JSON(params.data)) + '\n');
204 if (params.header) s += this.template_sub( params.header, cols, params );
206 // Pre-templating sort
207 // %SORT(field[ AS type][ ASC|DESC][,...])%
208 var sort_blocks = params.line_item.match(/%SORT\([^)]+\)%/g);
210 for(var i = 0; i < sort_blocks.length; i++) {
211 sort_blocks[i] = sort_blocks[i].substring(6,sort_blocks[i].length-2);
213 sort_blocks = sort_blocks.join(',').split(/\s*,\s*/); // Supports %SORT(a,b)% and %SORT(a)% %SORT(b)% methods
214 for(var i = 0; i < sort_blocks.length; i++) {
215 sort_blocks[i] = sort_blocks[i].match(/([^ ]+)(?:\s+AS\s+([^ ]+))?(?:\s+(ASC|DESC))?/);
216 sort_blocks[i].shift(); // Removes the "full match" entry
219 function sorter(a, b) {
221 for(var i = 0; i < sort_blocks.length && return_val == 0; i++) {
222 var sort = sort_blocks[i];
223 var a_test = a[sort[0]];
224 var b_test = b[sort[0]];
225 sort[1] = sort[1] || '';
226 sort[2] = sort[2] || 'ASC';
227 switch(sort[1].toUpperCase()) {
229 a_test = new Date(a_test);
230 b_test = new Date(b_test);
233 a_test = parseInt(a_test);
234 b_test = parseInt(b_test);
238 a_test = parseFloat(a_test);
239 b_test = parseFloat(b_test);
242 a_test = a_test.toLowerCase();
243 b_test = b_test.toLowerCase();
246 a_test = a_test.toUpperCase();
247 b_test = b_test.toUpperCase();
250 if(a_test > b_test) return_val = 1;
251 if(a_test < b_test) return_val = -1;
252 if(sort[2] == 'DESC') return_val *= -1;
256 params.list.sort(sorter);
257 params.line_item = params.line_item.replace(/%SORT\([^)]*\)%/g,'');
260 for (var i = 0; i < params.list.length; i++) {
261 params.row = params.list[i];
263 s += this.template_sub( params.line_item, cols, params );
266 if (params.footer) s += this.template_sub( params.footer, cols, params );
268 if (params.sample_frame) {
269 var jsrc = 'data:text/javascript,' + window.escape('var params = { "data" : ' + js2JSON(params.data) + ', "list" : ' + js2JSON(params.list) + '};');
270 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>');
272 this.simple(s,params);
276 'template_sub' : function( msg, cols, params ) {
279 if (!msg) { dump('template sub called with empty string\n'); return; }
280 JSAN.use('util.date');
284 // Note that we keep track of already included settings
285 // This ensures that we don't infinite loop through includes
288 var include_patt=/%INCLUDE\(\s*([^)]*?)\s*\)%/;
290 while(match = include_patt.exec(s)) {
291 if(match[1] == '' || included[match[1]]) {
292 s = s.replace(match[0], '');
294 included[match[1]] = true;
295 s = s.replace(new RegExp("%INCLUDE\\(\\s*" + match[1].replace(/([.?*+^$[\]\\(){}-])/g, "\\$1") + "\\s*\\)%","g"), obj.data.hash.aous['circ.staff_client.receipt.' + match[1]] || '');
298 } catch(E) { dump(E+'\n'); }
300 try{b = s; s = s.replace(/%LINE_NO%/g,Number(params.row_idx)+1);}
301 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
303 try{b = s; s = s.replace(/%patron_barcode%/g,this.escape_html(params.patron_barcode));}
304 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
306 try{b = s; s = s.replace(/%LIBRARY%/g,this.escape_html(params.lib.name()));}
307 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
308 try{b = s; s = s.replace(/%PINES_CODE%/g,this.escape_html(params.lib.shortname()));}
309 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
310 try{b = s; s = s.replace(/%SHORTNAME%/g,this.escape_html(params.lib.shortname()));}
311 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
312 try{b = s; s = s.replace(/%STAFF_FIRSTNAME%/g,this.escape_html(params.staff.first_given_name()));}
313 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
314 try{b = s; s = s.replace(/%STAFF_LASTNAME%/g,this.escape_html(params.staff.family_name()));}
315 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
316 try{b = s; s = s.replace(/%STAFF_BARCODE%/g,this.escape_html(params.staff.barcode)); }
317 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
318 try{b = s; s = s.replace(/%STAFF_PROFILE%/g,this.escape_html(obj.data.hash.pgt[ params.staff.profile() ].name() )); }
319 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
320 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()));}
321 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
322 try{b = s; s = s.replace(/%PATRON_ALIAS%/g,this.escape_html((params.patron.alias() == '' || params.patron.alias() == null) ? '' : params.patron.alias()));}
323 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
324 try{b = s; s = s.replace(/%PATRON_FIRSTNAME%/g,this.escape_html(params.patron.first_given_name()));}
325 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
326 try{b = s; s = s.replace(/%PATRON_LASTNAME%/g,this.escape_html(params.patron.family_name()));}
327 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
328 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() )) ;}
329 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
331 try{b = s; s=s.replace(/%TODAY%/g,(new Date()));}
332 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
333 try{b = s; s=s.replace(/%TODAY_m%/g,(util.date.formatted_date(new Date(),'%m')));}
334 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
335 try{b = s; s=s.replace(/%TODAY_TRIM%/g,(util.date.formatted_date(new Date(),'')));}
336 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
337 try{b = s; s=s.replace(/%TODAY_d%/g,(util.date.formatted_date(new Date(),'%d')));}
338 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
339 try{b = s; s=s.replace(/%TODAY_Y%/g,(util.date.formatted_date(new Date(),'%Y')));}
340 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
341 try{b = s; s=s.replace(/%TODAY_H%/g,(util.date.formatted_date(new Date(),'%H')));}
342 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
343 try{b = s; s=s.replace(/%TODAY_I%/g,(util.date.formatted_date(new Date(),'%I')));}
344 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
345 try{b = s; s=s.replace(/%TODAY_M%/g,(util.date.formatted_date(new Date(),'%M')));}
346 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
347 try{b = s; s=s.replace(/%TODAY_D%/g,(util.date.formatted_date(new Date(),'%D')));}
348 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
349 try{b = s; s=s.replace(/%TODAY_F%/g,(util.date.formatted_date(new Date(),'%F')));}
350 catch(E){s = b; this.error.sdump('D_WARN','string = <' + s + '> error = ' + js2JSON(E)+'\n');}
353 if (typeof params.row != 'undefined') {
354 if (params.row.length >= 0) {
355 alert('debug - please tell the developers that deprecated template code tried to execute');
356 for (var i = 0; i < cols.length; i++) {
357 var re = new RegExp(cols[i],"g");
358 try{b = s; s=s.replace(re, this.escape_html(params.row[i]));}
359 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 1 string = <' + s + '>',E);}
362 /* for dump_with_keys */
363 for (var i in params.row) {
364 var re = new RegExp('%'+i+'%',"g");
365 try{b = s; s=s.replace(re, this.escape_html(params.row[i].toString()));}
366 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 2 string = <' + s + '>',E);}
371 if (typeof params.data != 'undefined') {
372 for (var i in params.data) {
373 var re = new RegExp('%'+i+'%',"g");
374 if (typeof params.data[i] == 'string' || typeof params.data[i] == 'number') {
375 try{b = s; s=s.replace(re, (typeof params.data[i] == 'string' ? this.escape_html(params.data[i]) : params.data[i]));}
376 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
378 /* likely a null, print as an empty string */
379 try{b = s; s=s.replace(re, '');}
380 catch(E){s = b; this.error.standard_unexpected_error_alert('print.js, template_sub(): 3 string = <' + s + '>',E);}
384 } catch(E) { dump(E+'\n'); }
389 var date_format_patt=/%DATE_FORMAT\(\s*([^,]*?)\s*,\s*([^)]*?)\s*\)%/;
390 while(match = date_format_patt.exec(s)) {
391 if(match[1] == '' || match[2] == '')
392 s = s.replace(match[0], '');
394 s = s.replace(match[0], util.date.formatted_date(match[1], match[2]));
396 } catch(E) { dump(E+'\n'); }
401 // Pre-trim inside of substrings, and only inside of them
402 // This keeps the trim commands from being truncated
403 var substr_trim_patt=/(%SUBSTR\(-?\d+,?\s*(-?\d+)?\)%.*?)(\s*%-TRIM%|%TRIM-%\s*)(.*?%SUBSTR_END%)/;
404 while(match = substr_trim_patt.exec(s))
405 s = s.replace(match[0], match[1] + match[4]);
406 // Then do the substrings themselves
407 var substr_patt=/%SUBSTR\((-?\d+),?\s*(-?\d+)?\)%(.*?)%SUBSTR_END%/;
408 while(match = substr_patt.exec(s)) {
409 var substring_start = parseInt(match[1]);
410 if(substring_start < 0) substring_start = match[3].length + substring_start;
411 var substring_length = parseInt(match[2]);
412 if(substring_length > 0)
413 s = s.replace(match[0], match[3].substring(substring_start, substring_start + substring_length));
414 else if(substring_length < 0)
415 s = s.replace(match[0], match[3].substring(substring_start + substring_length, substring_start));
417 s = s.replace(match[0], match[3].substring(substring_start));
419 } catch(E) { dump(E+'\n'); }
421 // Cleanup unwanted whitespace
423 s = s.replace(/%TRIM-%\s*/g,'');
424 s = s.replace(/\s*%-TRIM%/g,'');
425 } catch(E) { dump(E+'\n'); }
429 alert('Error in print.js, template_sub(): ' + E);
434 'NSPrint' : function(w,silent,params) {
438 if (!params) params = {};
440 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
441 obj.data.init({'via':'stash'});
443 if (params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
445 dump('params.print_strategy = ' + params.print_strategy
446 + ' || obj.data.print_strategy[' + obj.context + '] = ' + obj.data.print_strategy[obj.context]
447 + ' || obj.data.print_strategy[default] = ' + obj.data.print_strategy['default']
448 + ' => ' + ( params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default'] ) + '\n');
449 switch(params.print_strategy || obj.data.print_strategy[obj.context] || obj.data.print_strategy['default']) {
451 params.dos_print = true;
453 if (typeof w != 'string') {
455 var temp_w = params.msg || w.document.firstChild.innerHTML;
456 if (!params.msg) { params.msg = temp_w; }
457 if (typeof temp_w != 'string') { throw(temp_w); }
458 w = obj.html2txt(temp_w);
460 dump('util.print: Could not use w.document.firstChild.innerHTML with ' + w + ': ' + E + '\n');
461 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
462 w.getSelection().selectAllChildren(w.document.firstChild);
463 w = w.getSelection().toString();
466 obj._NSPrint_custom_print(w,silent,params);
469 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
470 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
471 var originalPrinter = false;
472 if (prefs.prefHasUserValue('print.print_printer')) {
473 // This is for restoring print.print_printer after any print dialog, so that when
474 // window.print gets used again, it uses the configured printer for the right context
475 // (which should only be default--window.print is a kludge and is in limited use),
476 // rather than the printer last used.
477 originalPrinter = prefs.getCharPref('print.print_printer');
479 if (typeof w == 'object') {
481 if (originalPrinter) {
482 prefs.setCharPref('print.print_printer',originalPrinter);
485 if (params.content_type == 'text/plain') {
486 w = window.open('data:text/plain,'+escape(params.msg));
488 w = window.open('data:text/html,'+escape(params.msg));
493 if (originalPrinter) {
494 prefs.setCharPref('print.print_printer',originalPrinter);
505 case 'webBrowserPrint':
507 if (typeof w == 'object') {
508 obj._NSPrint_webBrowserPrint(w,silent,params);
510 if (params.content_type == 'text/plain') {
511 w = window.open('data:text/plain,'+escape(params.msg));
513 w = window.open('data:text/html,'+escape(params.msg));
517 obj._NSPrint_webBrowserPrint(w,silent,params);
531 obj._NSPrint_webBrowserPrint(w,silent,params);
535 alert('Probably not printing: ' + e);
536 this.error.sdump('D_ERROR','PRINT EXCEPTION: ' + js2JSON(e) + '\n');
541 '_NSPrint_custom_print' : function(w,silent,params) {
546 var html = params.msg || w;
548 var txt_file = new util.file('receipt.txt');
549 txt_file.write_content('truncate',text);
550 var text_path = '"' + txt_file._file.path + '"';
553 var html_file = new util.file('receipt.html');
554 html_file.write_content('truncate',html);
555 var html_path = '"' + html_file._file.path + '"';
558 var cmd = params.dos_print ?
559 'copy ' + text_path + ' lpt1 /b\n'
560 : obj.oils_printer_external_cmd.replace('%receipt.txt%',text_path).replace('%receipt.html%',html_path)
563 file = new util.file('receipt.bat');
564 file.write_content('truncate+exec',cmd);
566 file = new util.file('receipt.bat');
568 dump('print exec: ' + cmd + '\n');
569 var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
570 process.init(file._file);
574 dump('process.run = ' + process.run(true, args, args.length) + '\n');
579 //alert('Probably not printing: ' + e);
580 this.error.sdump('D_ERROR','_NSPrint_custom_print PRINT EXCEPTION: ' + js2JSON(e) + '\n');
584 '_NSPrint_webBrowserPrint' : function(w,silent,params) {
587 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
588 var webBrowserPrint = w
589 .QueryInterface(Components.interfaces.nsIInterfaceRequestor)
590 .getInterface(Components.interfaces.nsIWebBrowserPrint);
591 this.error.sdump('D_PRINT','webBrowserPrint = ' + webBrowserPrint);
592 if (webBrowserPrint) {
593 var gPrintSettings = obj.GetPrintSettings();
594 if (silent) gPrintSettings.printSilent = true;
595 else gPrintSettings.printSilent = false;
597 if (params.marginLeft) gPrintSettings.marginLeft = params.marginLeft;
599 webBrowserPrint.print(gPrintSettings, null);
600 this.error.sdump('D_PRINT','Should be printing\n');
602 this.error.sdump('D_ERROR','Should not be printing\n');
605 //alert('Probably not printing: ' + e);
606 // Pressing cancel is expressed as an NS_ERROR_ABORT return value,
607 // causing an exception to be thrown which we catch here.
608 // Unfortunately this will also consume helpful failures
609 this.error.sdump('D_ERROR','_NSPrint_webBrowserPrint PRINT EXCEPTION: ' + js2JSON(e) + '\n');
613 'GetPrintSettings' : function() {
615 //alert('entering GetPrintSettings');
616 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
617 var pref = Components.classes["@mozilla.org/preferences-service;1"]
618 .getService(Components.interfaces.nsIPrefBranch);
619 //alert('pref = ' + pref);
621 this.gPrintSettingsAreGlobal = pref.getBoolPref("print.use_global_printsettings", false);
622 this.gSavePrintSettings = pref.getBoolPref("print.save_print_settings", false);
623 //alert('gPrintSettingsAreGlobal = ' + this.gPrintSettingsAreGlobal + ' gSavePrintSettings = ' + this.gSavePrintSettings);
626 var printService = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
627 .getService(Components.interfaces.nsIPrintSettingsService);
628 if (this.gPrintSettingsAreGlobal) {
629 this.gPrintSettings = printService.globalPrintSettings;
630 //alert('called setPrinterDefaultsForSelectedPrinter');
631 this.setPrinterDefaultsForSelectedPrinter(printService);
633 this.gPrintSettings = printService.newPrintSettings;
634 //alert('used printService.newPrintSettings');
637 this.error.sdump('D_ERROR',"GetPrintSettings() "+e+"\n");
638 //alert("GetPrintSettings() "+e+"\n");
641 return this.gPrintSettings;
644 'setPrinterDefaultsForSelectedPrinter' : function (aPrintService) {
646 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
647 if (this.gPrintSettings.printerName == "") {
648 this.gPrintSettings.printerName = aPrintService.defaultPrinterName;
649 //alert('used .defaultPrinterName');
651 //alert('printerName = ' + this.gPrintSettings.printerName);
653 // First get any defaults from the printer
654 aPrintService.initPrintSettingsFromPrinter(this.gPrintSettings.printerName, this.gPrintSettings);
656 // now augment them with any values from last time
657 aPrintService.initPrintSettingsFromPrefs(this.gPrintSettings, true, this.gPrintSettings.kInitSaveAll);
659 // now augment from our own saved settings if they exist
660 this.load_settings();
663 this.error.sdump('D_ERROR',"setPrinterDefaultsForSelectedPrinter() "+E+"\n");
667 'page_settings' : function() {
669 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
670 this.GetPrintSettings();
671 var PO = Components.classes["@mozilla.org/gfx/printsettings-service;1"].getService(Components.interfaces.nsIPrintOptions);
672 PO.ShowPrintSetupDialog(this.gPrintSettings);
674 this.error.standard_unexpected_error_alert("page_settings()",E);
678 'load_settings' : function() {
681 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
682 var file = new util.file('gPrintSettings.' + this.context);
683 if (file._file.exists()) {
684 temp = file.get_object(); file.close();
685 for (var i in temp) {
686 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
688 } else if (this.context != 'default') {
689 var file = new util.file('gPrintSettings.default');
690 if (file._file.exists()) {
691 temp = file.get_object(); file.close();
692 for (var i in temp) {
693 try { this.gPrintSettings[i] = temp[i]; } catch(E) { error_msg += 'Error trying to set gPrintSettings.'+i+'='+temp[i]+' : ' + js2JSON(E) + '\n'; }
696 this.gPrintSettings.marginTop = 0;
697 this.gPrintSettings.marginLeft = 0;
698 this.gPrintSettings.marginBottom = 0;
699 this.gPrintSettings.marginRight = 0;
700 this.gPrintSettings.headerStrLeft = '';
701 this.gPrintSettings.headerStrCenter = '';
702 this.gPrintSettings.headerStrRight = '';
703 this.gPrintSettings.footerStrLeft = '';
704 this.gPrintSettings.footerStrCenter = '';
705 this.gPrintSettings.footerStrRight = '';
709 this.error.sdump('D_PRINT',error_msg);
710 this.error.yns_alert(
711 document.getElementById('offlineStrings').getString('load_printer_settings_error_description'),
712 document.getElementById('offlineStrings').getString('load_printer_settings_error_title'),
713 document.getElementById('offlineStrings').getString('common.ok'),
720 this.error.standard_unexpected_error_alert("load_settings()",E);
724 'save_settings' : function() {
727 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
728 var file = new util.file('gPrintSettings.' + this.context);
729 if (typeof obj.gPrintSettings == 'undefined') obj.GetPrintSettings();
730 if (obj.gPrintSettings) file.set_object(obj.gPrintSettings);
732 if (this.context == 'default') {
733 // print.print_printer gets used by bare window.print()'s. We sometimes use window.print for the
734 // WebBrowserPrint strategy to workaround bugs with the NSPrint xpcom, and only in the default context.
735 var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces['nsIPrefBranch']);
736 prefs.setCharPref('print.print_printer',obj.gPrintSettings.printerName);
739 this.error.standard_unexpected_error_alert("save_settings()",E);
744 dump('exiting util/print.js\n');