]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/xul/staff_client/chrome/content/util/sound.js
Merge remote branch 'working/user/shadowspar/ttopac-altcleanup' into template-toolkit...
[Evergreen.git] / Open-ILS / xul / staff_client / chrome / content / util / sound.js
1 dump('entering util/sound.js\n');
2
3 if (typeof util == 'undefined') util = {};
4 util.sound = function (params) {
5
6     try {
7
8         if (!params) { params = {}; }
9
10         this.sig = (new Date()).getMinutes() + ':' + (new Date()).getSeconds() + '.' + (new Date()).getMilliseconds()/1000;
11         if (params.sig) { this.sig += ' ' + params.sig; }
12
13         /* We're going to turn this guy into a singleton, at least for a given window, and look for it in xulG */
14         if (! window.xulG) { window.xulG = {}; }
15         if (window.xulG._sound && !params.reuse_queue_from_this_snd_obj) { 
16             dump('SOUND('+this.sig+'): reusing sound from ' + window.xulG._sound.origin + '('+xulG._sound.sig+') for ' + location.pathname + '\n');
17             return window.xulG._sound; 
18         } else {
19             dump('SOUND('+this.sig+'): instantiating new sound for ' + location.pathname + '\n');
20         }
21
22         /* So we can queue up sounds and put a pause between them instead of having them trample over each other */
23         /* Limitation: interval only gets set once for a singleton */
24         if (params.interval || params.reuse_queue_from_this_snd_obj) {
25             this._queue = true;
26             if (params.reuse_queue_from_this_snd_obj) {
27                 this._funcs = params.reuse_queue_from_this_snd_obj._funcs || [];
28             } else {
29                 this._funcs = [];
30             }
31             JSAN.use('util.exec');
32             this._exec = new util.exec();
33             var delay = params.interval;
34             if (!delay) { delay = _sound_delay_interval; /* define this in server/skin/custom.js */ }
35             if (!delay) { delay = 2000; }
36             var intervalId = this._exec.timer( this._funcs, delay );
37             dump('SOUND('+this.sig+'): starting timer with intervalId = ' + intervalId + '\n');
38         }
39
40         netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
41         var SOUNDContractID = "@mozilla.org/sound;1";
42         var SOUNDIID        = Components.interfaces.nsISound;
43         this.SOUND          = Components.classes[SOUNDContractID].createInstance(SOUNDIID);
44         this.SOUND.init(); // not necessary, but helps avoid delays?
45
46         this.origin = location.pathname;
47
48         window.xulG._sound = this;
49         return this;
50
51     } catch(E) {
52         dump('error in util.sound constructor: ' + E + '\n');
53         return this;
54     }
55 };
56
57 util.sound.prototype = {
58
59     'xp_url_init' : function (aURL) {
60         try {
61             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
62             var URLContractID   = "@mozilla.org/network/standard-url;1";
63             var URLIID          = Components.classes[URLContractID].createInstance( );
64             var URL             = URLIID.QueryInterface(Components.interfaces.nsIURL);
65             if (aURL) {
66                 URL.spec = aURL;
67             }
68             return URL;
69         } catch(E) {
70             alert('xp_url_init(): ' + E);
71         }
72     },
73
74     'play_url' : function(url) {
75
76         if (!url) { return; /* sound of silence */ }
77
78         var obj = this;
79         try {
80             netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
81             JSAN.use('OpenILS.data'); var data = new OpenILS.data(); data.init({'via':'stash'});
82             var url2 = obj.xp_url_init( data.server + url );
83             if (typeof data.no_sound == 'undefined' || data.no_sound == false || data.no_sound == 'false') {
84
85                 if (obj._queue) {
86                     dump('SOUND('+obj.sig+'): queueing file = ' + url + '\n');
87                     obj._funcs.push( function() { 
88                         dump('SOUND('+obj.sig+'): playing file = ' + url + '\n');
89                         obj.SOUND.play( url2 ); 
90                     } );
91                 } else {
92                     dump('SOUND('+obj.sig+'): playing file = ' + url + '\n');
93                     obj.SOUND.play( url2 );
94                 }
95             }
96         } catch(E) {
97             try { if (data.no_sound == 'undefined' || data.no_sound == false || data.no_sound == 'false') obj.SOUND.beep(); } catch(F) { 
98                 dump('beep(): ' + F + '\n');
99             }
100             dump('play_url(): ' + E + '\n');
101         }
102     },
103
104     'event' : function event(evt) {
105         var key = 'AUDIO_' + arguments.callee.name + '_' + evt.textcode;
106         dump('SOUND('+this.sig+'): key = ' + key + '\n');
107         this.play_url( urls[key] );
108     },
109
110     'special' : function special(e) {
111         var key = 'AUDIO_' + arguments.callee.name + '_' + e;
112         dump('SOUND('+this.sig+'): key = ' + key + '\n');
113         this.play_url( urls[key] );
114     },
115
116     'good' : function good(e){
117         var key = 'AUDIO_' + arguments.callee.name;
118         dump('SOUND('+this.sig+'): key = ' + key + '\n');
119         this.play_url( urls[key] );
120     },
121
122     'bad' : function bad(e){
123         var key = 'AUDIO_' + arguments.callee.name;
124         dump('SOUND('+this.sig+'): key = ' + key + '\n');
125         this.play_url( urls[key] );
126     },
127
128     'horrible' : function horrible(e){
129         var key = 'AUDIO_' + arguments.callee.name;
130         dump('SOUND('+this.sig+'): key = ' + key + '\n');
131         this.play_url( urls[key] );
132     },
133
134     'circ_good' : function circ_good(e){
135         var key = 'AUDIO_' + arguments.callee.name;
136         dump('SOUND('+this.sig+'): key = ' + key + '\n');
137         this.play_url( urls[key] );
138     },
139
140     'circ_bad' : function circ_bad(e){
141         var key = 'AUDIO_' + arguments.callee.name;
142         dump('SOUND('+this.sig+'): key = ' + key + '\n');
143         this.play_url( urls[key] );
144     }
145 }
146
147 dump('exiting util/sound.js\n');