Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
11 KiB

  1. // kate: space-indent on; indent-width 4; replace-tabs on;
  2. /**
  3. * Copyright © 2010, Michael "Svedrin" Ziegler <diese-addy@funzt-halt.net>
  4. *
  5. * Mumble-Django is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This package is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * Documentation for this channel viewer can be found here:
  16. * http://mumble-django.org/docs/api/channelviewer.html
  17. */
  18. Ext.namespace('Ext.ux');
  19. if( typeof gettext == "undefined" ){
  20. // Cope with Django's jsi18n not being available by adding dummy gettext
  21. gettext = function( text ){
  22. return text;
  23. };
  24. gettext_noop = gettext;
  25. }
  26. Ext.ux.MumbleChannelNodeUI = Ext.extend(Ext.tree.TreeNodeUI, {
  27. renderElements : function(n, a, targetNode, bulkRender){
  28. Ext.ux.MumbleUserNodeUI.superclass.renderElements.call( this, n, a, targetNode, bulkRender );
  29. Ext.DomHelper.applyStyles( this.elNode, 'position: relative' );
  30. var tpl = new Ext.DomHelper.createTemplate(
  31. '<img style="position: absolute; top: 0px; right: {pos}px;" src="{imageurl}/{icon}.png"/>'
  32. );
  33. var icons = []
  34. if( a.chandata.description != "" ) icons.push( "comment_seen" );
  35. var pos = 8;
  36. for( var i = 0; i < icons.length; i++ ){
  37. tpl.append( this.elNode, {'imageurl': a.imageurl, 'icon': icons[i], 'pos': pos} );
  38. pos += 18
  39. }
  40. }
  41. });
  42. Ext.ux.MumbleUserNodeUI = Ext.extend(Ext.tree.TreeNodeUI, {
  43. renderElements : function(n, a, targetNode, bulkRender){
  44. Ext.ux.MumbleUserNodeUI.superclass.renderElements.call( this, n, a, targetNode, bulkRender );
  45. Ext.DomHelper.applyStyles( this.elNode, 'position: relative' );
  46. var tpl = new Ext.DomHelper.createTemplate(
  47. '<img style="position: absolute; top: 0px; right: {pos}px;" src="{imageurl}/{icon}.png"/>'
  48. );
  49. var icons = []
  50. if( a.userdata.userid != -1 ) icons.push( "authenticated" );
  51. if( a.userdata.selfDeaf ) icons.push( "deafened_self" );
  52. if( a.userdata.deaf ) icons.push( "deafened_server" );
  53. if( a.userdata.selfMute ) icons.push( "muted_self" );
  54. if( a.userdata.suppress ) icons.push( "muted_suppressed" );
  55. if( a.userdata.mute ) icons.push( "muted_server" );
  56. if( a.userdata.comment != "" ) icons.push( "comment_seen" );
  57. if( a.userdata.prioritySpeaker ) icons.push( "priority_speaker" );
  58. if( a.userdata.recording ) icons.push( "recording" );
  59. var pos = 8;
  60. for( var i = 0; i < icons.length; i++ ){
  61. tpl.append( this.elNode, {'imageurl': a.imageurl, 'icon': icons[i], 'pos': pos} );
  62. pos += 18
  63. }
  64. }
  65. });
  66. function cmp_channels( left, rite ){
  67. // Compare two channels, first by position, and if that equals, by name.
  68. if( typeof left.position != "undefined" && typeof rite.position != "undefined" ){
  69. byorder = left.position - rite.position;
  70. if( byorder != 0 )
  71. return byorder;
  72. }
  73. return left.name.localeCompare(rite.name);
  74. }
  75. function cmp_names( left, rite ){
  76. return left.name.localeCompare(rite.name);
  77. }
  78. Ext.ux.MumbleChannelViewer = function( config ){
  79. Ext.apply( this, config );
  80. Ext.applyIf( this, {
  81. title: gettext("Channel Viewer"),
  82. refreshInterval: 30000,
  83. idleInterval: 2,
  84. usersAboveChannels: false,
  85. autoScroll: true,
  86. enableDD: false, // Users need to enable this explicitly
  87. root: {
  88. text: gettext("Loading..."),
  89. leaf: true
  90. },
  91. listeners: {}
  92. });
  93. Ext.applyIf( this.listeners, {
  94. dragdrop: function( tree, node, targetdd, ev ){
  95. if( typeof node.attributes.userdata != "undefined" )
  96. tree.fireEvent("moveUser", tree, node.attributes.userdata, targetdd.dragOverData.target.attributes.chandata);
  97. else if( typeof node.attributes.chandata != "undefined" )
  98. tree.fireEvent("moveChannel", tree, node.attributes.chandata, targetdd.dragOverData.target.attributes.chandata);
  99. }
  100. });
  101. Ext.applyIf( this, {
  102. // This stuff needs the above applied already
  103. bbar: [ gettext("Auto-Refresh")+':', {
  104. xtype: "checkbox",
  105. ref: "../cbAutoRefresh",
  106. scope: this,
  107. handler: this.setAutoRefresh,
  108. checked: (this.refreshInterval > 0),
  109. }, {
  110. xtype: "numberfield",
  111. width: 30,
  112. value: this.refreshInterval / 1000,
  113. ref: "../nfAutoRefreshInterval",
  114. scope: this,
  115. selectOnFocus: true,
  116. listeners: {
  117. render: function(c) {
  118. Ext.QuickTips.register({
  119. target: c.getEl(),
  120. text: gettext('Enter the interval in seconds in which the channel viewer should refresh and hit Enter.')
  121. });
  122. },
  123. specialkey: function( field, ev ){
  124. if( ev.getKey() == ev.ENTER ){
  125. this.scope.setAutoRefresh(); // lawl
  126. this.blur();
  127. }
  128. }
  129. },
  130. }, gettext("Seconds"), '->', {
  131. xtype: "button",
  132. text: gettext("Refresh"),
  133. handler: this.refresh,
  134. scope: this
  135. }]
  136. } );
  137. Ext.ux.MumbleChannelViewer.superclass.constructor.call( this );
  138. this.addEvents({
  139. 'moveUser': true,
  140. 'moveChannel': true
  141. });
  142. this.autoRefreshId = 0;
  143. this.setAutoRefresh();
  144. if( this.refreshInterval == 0 )
  145. this.refresh();
  146. }
  147. Ext.extend( Ext.ux.MumbleChannelViewer, Ext.tree.TreePanel, {
  148. setAutoRefresh: function(){
  149. if( this.autoRefreshId != 0 ){
  150. clearTimeout( this.autoRefreshId );
  151. }
  152. if( this.cbAutoRefresh.getValue() ){
  153. this.refreshInterval = this.nfAutoRefreshInterval.getValue() * 1000;
  154. this.autoRefresh();
  155. }
  156. else{
  157. this.refreshInterval = 0;
  158. }
  159. },
  160. autoRefresh: function(){
  161. this.refresh();
  162. if( this.refreshInterval > 0 ){
  163. this.autoRefreshId = this.autoRefresh.defer( this.refreshInterval, this );
  164. }
  165. },
  166. refresh: function(){
  167. var conn = new Ext.data.Connection();
  168. conn.request({
  169. url: this.source_url,
  170. scope: this,
  171. success: function( resp, opt ){
  172. var respdata = Ext.decode( resp.responseText );
  173. var root = {
  174. text: respdata.name,
  175. nodeType: 'async',
  176. id: "mumbroot",
  177. leaf: false,
  178. icon: this.imageurl+'/mumble.16x16.png',
  179. children: [],
  180. chandata: respdata.root,
  181. uiProvider: Ext.ux.MumbleChannelNodeUI,
  182. imageurl: this.imageurl
  183. };
  184. tree = this;
  185. function populateNode( node, json ){
  186. var subchan_users = 0;
  187. var popChannels = function(){
  188. json.channels.sort(cmp_channels);
  189. for( var i = 0; i < json.channels.length; i++ ){
  190. var child = {
  191. text: json.channels[i].name,
  192. id: ("channel_" + json.channels[i].id),
  193. nodeType: 'async',
  194. allowDrag: true,
  195. allowDrop: true,
  196. draggable: true,
  197. icon: tree.imageurl+'/channel.png',
  198. children: [],
  199. uiProvider: Ext.ux.MumbleChannelNodeUI,
  200. chandata: json.channels[i],
  201. imageurl: tree.imageurl
  202. };
  203. node.children.push( child );
  204. subchan_users += populateNode( child, json.channels[i] );
  205. }
  206. }
  207. var popUsers = function(){
  208. json.users.sort(cmp_names);
  209. for( var i = 0; i < json.users.length; i++ ){
  210. var child = {
  211. text: json.users[i].name,
  212. id: ("user_" + json.users[i].session),
  213. nodeType: 'async',
  214. leaf: true,
  215. allowDrag: true,
  216. draggable: true,
  217. uiProvider: Ext.ux.MumbleUserNodeUI,
  218. userdata: json.users[i],
  219. imageurl: tree.imageurl
  220. };
  221. if( json.users[i].idlesecs <= tree.idleInterval )
  222. child.icon = tree.imageurl+'/talking_on.png';
  223. else
  224. child.icon = tree.imageurl+'/talking_off.png';
  225. node.leaf = false;
  226. node.children.push( child );
  227. }
  228. }
  229. if( tree.usersAboveChannels ){
  230. popUsers();
  231. popChannels();
  232. }
  233. else{
  234. popChannels();
  235. popUsers();
  236. }
  237. if( json.id == 0 || json.users.length > 0 || subchan_users )
  238. node.expanded = true;
  239. return subchan_users + json.users.length;
  240. }
  241. populateNode( root, respdata.root );
  242. this.setRootNode( root );
  243. },
  244. failure: function( resp, opt ){
  245. if( resp.isTimeout === true )
  246. // Ignore, happens from time to time
  247. return;
  248. if( this.refreshInterval > 0 )
  249. this.cbAutoRefresh.setValue(false);
  250. Ext.Msg.show({
  251. title: gettext("Update error"),
  252. msg: gettext("Querying the server failed, so the channel viewer has not been updated."),
  253. icon: Ext.MessageBox.ERROR,
  254. buttons: Ext.MessageBox.OK
  255. });
  256. },
  257. });
  258. },
  259. } );
  260. Ext.reg( 'mumblechannelviewer', Ext.ux.MumbleChannelViewer );