instrumentation: add next-share/
[cs-p2p-next.git] / instrumentation / next-share / BaseLib / Transport / chrome / content / tribe_status_bar.js
1 \r
2 /*\r
3   TribeStatuBar - functions for the SwarmPlayer status bar\r
4 \r
5   Written by Riccardo Petrocco\r
6   see LICENSE.txt for license information\r
7 */\r
8
9 // TODO make async requests using ajax
10
11 var TribeStatusBar = {
12         // Install a timeout handler to install the interval routine
13
14   startup: function()
15   {
16     this.refreshInformation();
17     window.setInterval(this.refreshInformation, 1000);
18     this.tribeChannel = null;
19   },
20
21
22   // Called periodically to refresh traffic information
23   refreshInformation: function()
24   {
25
26     var httpRequest = null;
27     var fullUrl = "http://127.0.0.1:6877/webUI?&{%22method%22:%22get_speed_info%22}";
28     var tribeBar = this;
29
30     function infoReceived()
31     {
32
33             var tribePanel = document.getElementById('tribestatusbar');
34             var output = httpRequest.responseText;
35
36                 
37             if (output.length)
38             {
39                     var resp = JSON.parse(output);
40
41                     if(resp.success) {
42                       
43                       if (tribePanel.src != "chrome://tribe/skin/swarmplugin.png") {
44                     
45                         tribePanel.src = "chrome://tribe/skin/swarmplugin.png";
46                         //tribePanel.onclick = openWebUI;
47                         tribePanel.onclick = openAndReuseTab;
48                     tribePanel.tooltipText="Click here to access the SwarmPlayer Web Interface"
49                   }
50                   
51                       tribePanel.label = "Down: " + parseInt(resp.downspeed) + " KB/s, Up: " + parseInt(resp.upspeed) + " KB/s";
52         }                               
53                 
54
55             }
56
57     }
58     
59     function openWebUI()
60         {
61           var win = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow('navigator:browser'); 
62           win.openUILinkIn('http://127.0.0.1:6877/webUI', 'tab');
63         }
64         
65     function openAndReuseTab() 
66         {
67           url = "http://127.0.0.1:6877/webUI";
68           var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
69                              .getService(Components.interfaces.nsIWindowMediator);
70           var browserEnumerator = wm.getEnumerator("navigator:browser");
71
72           // Check each browser instance for our URL
73           var found = false;
74           while (!found && browserEnumerator.hasMoreElements()) {
75             var browserWin = browserEnumerator.getNext();
76             var tabbrowser = browserWin.gBrowser;
77
78             // Check each tab of this browser instance
79             var numTabs = tabbrowser.browsers.length;
80             for (var index = 0; index < numTabs; index++) {
81               var currentBrowser = tabbrowser.getBrowserAtIndex(index);
82               if (url == currentBrowser.currentURI.spec) {
83
84                 // The URL is already opened. Select this tab.
85                 tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
86
87                 // Focus *this* browser-window
88                 browserWin.focus();
89
90                 found = true;
91                 break;
92               }
93             }
94           }
95
96           // Our URL isn't open. Open it now.
97           if (!found) {
98             var recentWindow = wm.getMostRecentWindow("navigator:browser");
99             if (recentWindow) {
100               // Use an existing browser window
101               recentWindow.delayedOpenTab(url, null, null, null, null);
102             }
103             else {
104               // No browser windows are open, so open a new one.
105               window.open(url);
106             }
107           }
108       }
109
110     
111     function restartBG()
112     {
113
114       TribeStatusBar.startBG();
115
116     }
117     
118     function restoreBar()
119     {
120             var tribePanel = document.getElementById('tribestatusbar');
121
122       if (tribePanel.src != "chrome://tribe/skin/swarmplugin_grey.png") {    
123           tribePanel.src = "chrome://tribe/skin/swarmplugin_grey.png";
124               tribePanel.onclick=restartBG;
125               tribePanel.label = " ";
126                   tribePanel.tooltipText="SwarmPlayer: Sharing is disabled. Click here to start sharing"
127                     
128                   TribeStatusBar.tribeChannel = null;
129       }
130       
131     }
132
133     //TODO remove
134     function reqTimeout()
135     {
136         httpRequest.abort();
137         return;
138         // Note that at this point you could try to send a notification to the
139         // server that things failed, using the same xhr object.
140     }
141     
142     try 
143     {
144         httpRequest = new XMLHttpRequest();
145         httpRequest.open("GET", fullUrl, true);
146         httpRequest.onload = infoReceived;
147         httpRequest.onerror = restoreBar;
148         httpRequest.send(null);
149         // Timeout to abort in 5 seconds
150         //var reqTimeout = setTimeout(reqTimeout(),1000);
151         setTimeout(function()
152             {
153                 httpRequest.abort();
154                 return;
155             }
156             ,1000);
157     }
158     catch( err )
159     {
160         aMsg = ("*** StatusBar : " + err.description);\r
161         Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);\r
162         dump(aMsg);
163     }
164   },
165   
166   startBG: function() {
167
168     if (this.tribeChannel == null) { 
169       var tribeChannel = Components.classes['@p2pnext.org/tribe/channel;1'].getService().wrappedJSObject;
170                                        
171       this.tribeChannel = tribeChannel;
172                                        
173     }
174     
175     if (!tribeChannel.init) {
176       tribeChannel.startBackgroundDaemon();
177     }
178     
179   },
180   
181 }
182
183
184 window.addEventListener("load", function(e) { TribeStatusBar.startup(); }, false);