3 TribeStatuBar - functions for the SwarmPlayer status bar
\r
5 Written by Riccardo Petrocco
\r
6 see LICENSE.txt for license information
\r
9 // TODO make async requests using ajax
11 var TribeStatusBar = {
12 // Install a timeout handler to install the interval routine
16 this.refreshInformation();
17 window.setInterval(this.refreshInformation, 1000);
18 this.tribeChannel = null;
22 // Called periodically to refresh traffic information
23 refreshInformation: function()
26 var httpRequest = null;
27 var fullUrl = "http://127.0.0.1:6877/webUI?&{%22method%22:%22get_speed_info%22}";
30 function infoReceived()
33 var tribePanel = document.getElementById('tribestatusbar');
34 var output = httpRequest.responseText;
39 var resp = JSON.parse(output);
43 if (tribePanel.src != "chrome://tribe/skin/swarmplugin.png") {
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"
51 tribePanel.label = "Down: " + parseInt(resp.downspeed) + " KB/s, Up: " + parseInt(resp.upspeed) + " KB/s";
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');
65 function openAndReuseTab()
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");
72 // Check each browser instance for our URL
74 while (!found && browserEnumerator.hasMoreElements()) {
75 var browserWin = browserEnumerator.getNext();
76 var tabbrowser = browserWin.gBrowser;
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) {
84 // The URL is already opened. Select this tab.
85 tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[index];
87 // Focus *this* browser-window
96 // Our URL isn't open. Open it now.
98 var recentWindow = wm.getMostRecentWindow("navigator:browser");
100 // Use an existing browser window
101 recentWindow.delayedOpenTab(url, null, null, null, null);
104 // No browser windows are open, so open a new one.
114 TribeStatusBar.startBG();
118 function restoreBar()
120 var tribePanel = document.getElementById('tribestatusbar');
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"
128 TribeStatusBar.tribeChannel = null;
134 function reqTimeout()
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.
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()
160 aMsg = ("*** StatusBar : " + err.description);
\r
161 Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(aMsg);
\r
166 startBG: function() {
168 if (this.tribeChannel == null) {
169 var tribeChannel = Components.classes['@p2pnext.org/tribe/channel;1'].getService().wrappedJSObject;
171 this.tribeChannel = tribeChannel;
175 if (!tribeChannel.init) {
176 tribeChannel.startBackgroundDaemon();
184 window.addEventListener("load", function(e) { TribeStatusBar.startup(); }, false);