re-introducing sound effects. Need to put them everywhere :)
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / error.js
1 dump('entering util/error.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.error = function () {
5
6         try {
7                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
8                 this.consoleService = Components.classes['@mozilla.org/consoleservice;1']
9                         .getService(Components.interfaces.nsIConsoleService);
10         } catch(E) {
11                 this.consoleDump = false;
12                 dump('util.error constructor: ' + E + '\n');
13         }
14
15         this.sdump_last_time = new Date();
16
17         this.OpenILS = {};
18
19         JSAN.use('util.sound'); this.sound = new util.sound();
20
21         return this;
22 };
23
24 util.error.prototype = {
25
26         'printDebug' : true,
27         'consoleDump' : true,
28         'debugDump' : true,
29         'arg_dump_full' : false,
30
31         'debug' : function(e){
32                 dump('-----------------------------------------\n' 
33                         + e + '\n-----------------------------------------\n' );
34         },
35
36         'sdump_levels' : {
37
38                 'D_NONE' : false, 'D_ALL' : false, 'D_ERROR' : true, 'D_DEBUG' : true, 'D_TRACE' :  true,
39                 'D_WARN' : false,
40                 'D_TRACE_ENTER' :  false, 'D_TRACE_EXIT' :  false, 'D_TIMEOUT' :  false, 'D_FILTER' : false,
41                 'D_CONSTRUCTOR' : false, 'D_FIREFOX' : false, 'D_LEGACY' : false, 'D_DATA' : false,
42
43                 'D_CLAM' : false, 'D_PAGED_TREE' : false, 'D_GRID_LIST' : false, 'D_HTML_TABLE' : false,
44                 'D_TAB' : false, 'D_LIST' : false,
45
46                 'D_AUTH' : true, 'D_OPAC' : true, 'D_CAT' : true,
47
48                 'D_PATRON_SEARCH' : false, 'D_PATRON_SEARCH_FORM' : false, 'D_PATRON_SEARCH_RESULTS' : false,
49
50                 'D_PATRON_DISPLAY' : false, 'D_PATRON_DISPLAY_STATUS' : false, 'D_PATRON_DISPLAY_CONTACT' : false,
51
52                 'D_PATRON_ITEMS' : false, 'D_PATRON_CHECKOUT_ITEMS' : false, 'D_PATRON_HOLDS' : false,
53                 'D_PATRON_BILLS' : false, 'D_PATRON_EDIT' : false,
54
55                 'D_CHECKIN' : false, 'D_CHECKIN_ITEMS' : false,
56
57                 'D_HOLD_CAPTURE' : false, 'D_HOLD_CAPTURE_ITEMS' : false,
58
59                 'D_PATRON_UTILS' : false, 'D_CIRC_UTILS' : false,
60
61                 'D_FILE' : true, 'D_EXPLODE' : false, 'D_FM_UTILS' : false, 'D_PRINT' : true, 'D_SES' : true,
62                 'D_SES_FUNC' : false, 'D_SES_RESULT' : true, 'D_SPAWN' : false, 'D_STRING' : false,
63                 'D_UTIL' : false, 'D_WIN' : false, 'D_WIDGETS' : false
64         },
65
66         'filter_console_init' : function (p) {
67                 this.sdump('D_FILTER',this.arg_dump(arguments,{0:true}));
68
69                 var filterConsoleListener = {
70                         observe: function( msg ) {
71                                 try {
72                                         p.observe_msg( msg );
73                                 } catch(E) {
74                                         alert(E);
75                                 }
76                         },
77                         QueryInterface: function (iid) {
78                                 if (!iid.equals(Components.interfaces.nsIConsoleListener) &&
79                                         !iid.equals(Components.interfaces.nsISupports)) {
80                                                 throw Components.results.NS_ERROR_NO_INTERFACE;
81                                 }
82                                 return this;
83                         }
84                 };
85                 try {
86                         this.consoleService.registerListener(filterConsoleListener);    
87                 } catch(E) {
88                         alert(E);
89                 }
90
91                 this.sdump('D_TRACE_EXIT',this.arg_dump(arguments));
92         },
93
94         'sdump' : function (level,msg) {
95                 try {
96                         var now = new Date();
97                         var message = now.valueOf() + '\tdelta = ' + (now.valueOf() - this.sdump_last_time.valueOf()) + '\t' + level + '\n' + msg;
98                         if (this.sdump_levels['D_NONE']) return null;
99                         if (this.sdump_levels[level]||this.sdump_levels['D_ALL']) {
100                                 this.sdump_last_time = now;
101                                 if (this.debugDump)
102                                         this.debug(message);
103                                 if (this.consoleDump) {
104                                         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
105                                         this.consoleService.logStringMessage(message);
106                                 }
107                         }
108                 } catch(E) {
109                         dump('Calling sdump but ' + E + '\n');
110                 }
111         },
112
113         'arg_dump' : function (args,dump_these) {
114                 var s = '*>*>*> Called function ';
115                 try {
116                         if (!dump_these)
117                                 dump_these = {};
118                         s += args.callee.toString().match(/\w+/g)[1] + ' : ';
119                         for (var i = 0; i < args.length; i++)
120                                 s += typeof(args[i]) + ' ';
121                         s += '\n';
122                         for (var i = 0; i < args.length; i++)
123                                 if (dump_these[i]) {
124
125                                         var arg = args[i];
126                                         //dump('dump_these[i] = ' + dump_these[i] + '  arg = ' + arg + '\n');
127
128                                         if (typeof(dump_these[i])=='string') {
129
130                                                 if (dump_these[i].slice(0,1) == '.') {
131                                                         var cmd = 'arg' + dump_these[i];
132                                                         var result;
133                                                         try {
134                                                                 result = eval( cmd );
135                                                         } catch(E) {
136                                                                 result = cmd + ' ==> ' + E;
137                                                         }
138                                                         s += '\targ #' + i + ': ' + cmd + ' = ' + result;
139                                                 } else {
140                                                         var result;
141                                                         try {
142                                                                 result = eval( dump_these[i] );
143                                                         } catch(E) {
144                                                                 result = dump_these[i] + ' ==> ' + E;
145                                                         }
146                                                         s += '\targ #' + i + ': ' + result;
147                                                 }
148         
149                                         } else {
150                                                 s += '\targ #' + i + ' = ';
151                                                 try {
152                                                         //s += js2JSON( arg );
153                                                         s += arg;
154                                                 } catch(E) {
155                                                         s += arg;
156                                                 }
157                                         }
158         
159                                         s += '\n';
160                                         if (this.arg_dump_full)
161                                                 s += 'Definition: ' + args.callee.toString() + '\n';
162         
163                                 }
164                         return s;
165                 } catch(E) {
166                         return s + '\nDEBUG ME: ' + js2JSON(E) + '\n';
167                 }
168         },
169
170         'handle_error' : function (E,annoy) {
171                 var s = '';
172                 if (instanceOf(E,ex)) {
173                         s += E.err_msg();
174                         //s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
175                         //s += 'This error was anticipated.\n\n';
176                         //s += js2JSON(E).substr(0,200) + '...\n\n';
177                         if (snd_bad) snd_bad();
178                 } else {
179                         s += '\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n';
180                         s += 'This is a bug that we will fix later.\n\n';
181                         try {
182                                 s += js2JSON(E).substr(0,1024) + '\n\n';
183                         } catch(E2) {
184                                 try {
185                                         s += E.substr(0,1024) + '\n\n';
186                                 } catch(E3) {
187                                         s += E + '\n\n';
188                                 }
189                         }
190                         if (snd_really_bad) snd_really_bad();
191                 }
192                 sdump('D_ERROR',s);
193                 if (annoy)
194                         this.s_alert(s);
195                 else
196                         alert(s);
197         },
198
199         's_alert' : function (s) { alert(s); },
200
201         'get_ilsevent' : function(status) {
202                 JSAN.use('OpenILS.data'); 
203                 this.OpenILS.data = new OpenILS.data(); this.OpenILS.data.init({'via':'stash'});
204                 return this.OpenILS.data.entities['ilsevent.'+status];
205         },
206
207         'yns_alert' : function (s,title,b1,b2,b3,c) {
208
209                 /*
210                         s       = Message to display
211                         title   = Text in Title Bar
212                         b1      = Text for button 1
213                         b2      = Text for button 2
214                         b3      = Text for button 3
215                         c       = Text for confirmation checkbox.  null for no confirm
216                 */
217
218                 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
219
220                 this.sound.bad();
221
222                 // get a reference to the prompt service component.
223                 var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
224                         .getService(Components.interfaces.nsIPromptService);
225
226                 // set the buttons that will appear on the dialog. It should be
227                 // a set of constants multiplied by button position constants. In this case,
228                 // three buttons appear, Save, Cancel and a custom button.
229                 //var flags=promptService.BUTTON_TITLE_OK * promptService.BUTTON_POS_0 +
230                 //      promptService.BUTTON_TITLE_CANCEL * promptService.BUTTON_POS_1 +
231                 //      promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_2;
232                 var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
233                         promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
234                         promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_2; 
235
236                 // display the dialog box. The flags set above are passed
237                 // as the fourth argument. The next three arguments are custom labels used for
238                 // the buttons, which are used if BUTTON_TITLE_IS_STRING is assigned to a
239                 // particular button. The last two arguments are for an optional check box.
240                 var check = {};
241                 var rv = promptService.confirmEx(window,title, s, flags, b1, b2, b3, c, check);
242                 if (c && !check.value) {
243                         return this.yns_alert(s,title,b1,b2,b3,c);
244                 }
245                 return rv;
246         },
247 }
248
249 dump('exiting util/error.js\n');