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.

262 lines
10 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. autoScroll: true,
  85. enableDD: false, // Users need to enable this explicitly
  86. root: {
  87. text: gettext("Loading..."),
  88. leaf: true
  89. },
  90. listeners: {}
  91. });
  92. Ext.applyIf( this.listeners, {
  93. dragdrop: function( tree, node, targetdd, ev ){
  94. if( typeof node.attributes.userdata != "undefined" )
  95. tree.fireEvent("moveUser", tree, node.attributes.userdata, targetdd.dragOverData.target.attributes.chandata);
  96. else if( typeof node.attributes.chandata != "undefined" )
  97. tree.fireEvent("moveChannel", tree, node.attributes.chandata, targetdd.dragOverData.target.attributes.chandata);
  98. }
  99. });
  100. Ext.applyIf( this, {
  101. // This stuff needs the above applied already
  102. bbar: [ gettext("Auto-Refresh")+':', {
  103. xtype: "checkbox",
  104. ref: "../cbAutoRefresh",
  105. scope: this,
  106. handler: this.setAutoRefresh,
  107. checked: (this.refreshInterval > 0),
  108. }, {
  109. xtype: "numberfield",
  110. width: 30,
  111. value: this.refreshInterval / 1000,
  112. ref: "../nfAutoRefreshInterval",
  113. scope: this,
  114. selectOnFocus: true,
  115. listeners: {
  116. render: function(c) {
  117. Ext.QuickTips.register({
  118. target: c.getEl(),
  119. text: gettext('Enter the interval in seconds in which the channel viewer should refresh and hit Enter.')
  120. });
  121. },
  122. specialkey: function( field, ev ){
  123. if( ev.getKey() == ev.ENTER ){
  124. this.scope.setAutoRefresh(); // lawl
  125. this.blur();
  126. }
  127. }
  128. },
  129. }, gettext("Seconds"), '->', {
  130. xtype: "button",
  131. text: gettext("Refresh"),
  132. handler: this.refresh,
  133. scope: this
  134. }]
  135. } );
  136. Ext.ux.MumbleChannelViewer.superclass.constructor.call( this );
  137. this.addEvents({
  138. 'moveUser': true,
  139. 'moveChannel': true
  140. });
  141. this.autoRefreshId = 0;
  142. this.setAutoRefresh();
  143. }
  144. Ext.extend( Ext.ux.MumbleChannelViewer, Ext.tree.TreePanel, {
  145. setAutoRefresh: function(){
  146. if( this.autoRefreshId != 0 ){
  147. clearTimeout( this.autoRefreshId );
  148. }
  149. if( this.cbAutoRefresh.getValue() ){
  150. this.refreshInterval = this.nfAutoRefreshInterval.getValue() * 1000;
  151. this.autoRefresh();
  152. }
  153. else{
  154. this.refreshInterval = 0;
  155. }
  156. },
  157. autoRefresh: function(){
  158. this.refresh();
  159. if( this.refreshInterval > 0 ){
  160. this.autoRefreshId = this.autoRefresh.defer( this.refreshInterval, this );
  161. }
  162. },
  163. refresh: function(){
  164. var conn = new Ext.data.Connection();
  165. conn.request({
  166. url: this.source_url,
  167. scope: this,
  168. success: function( resp, opt ){
  169. var respdata = Ext.decode( resp.responseText );
  170. var root = {
  171. text: respdata.name,
  172. nodeType: 'async',
  173. id: "mumbroot",
  174. leaf: false,
  175. icon: this.imageurl+'/mumble.16x16.png',
  176. children: [],
  177. chandata: respdata.root,
  178. imageurl: this.imageurl
  179. };
  180. tree = this;
  181. function populateNode( node, json ){
  182. var subchan_users = 0;
  183. json.channels.sort(cmp_channels);
  184. for( var i = 0; i < json.channels.length; i++ ){
  185. var child = {
  186. text: json.channels[i].name,
  187. id: ("channel_" + json.channels[i].id),
  188. nodeType: 'async',
  189. allowDrag: true,
  190. allowDrop: true,
  191. draggable: true,
  192. icon: tree.imageurl+'/channel.png',
  193. children: [],
  194. uiProvider: Ext.ux.MumbleChannelNodeUI,
  195. chandata: json.channels[i],
  196. imageurl: tree.imageurl
  197. };
  198. node.children.push( child );
  199. subchan_users += populateNode( child, json.channels[i] );
  200. }
  201. json.users.sort(cmp_names);
  202. for( var i = 0; i < json.users.length; i++ ){
  203. var child = {
  204. text: json.users[i].name,
  205. id: ("user_" + json.users[i].session),
  206. nodeType: 'async',
  207. leaf: true,
  208. allowDrag: true,
  209. draggable: true,
  210. uiProvider: Ext.ux.MumbleUserNodeUI,
  211. userdata: json.users[i],
  212. imageurl: tree.imageurl
  213. };
  214. if( json.users[i].idlesecs <= tree.idleInterval )
  215. child.icon = tree.imageurl+'/talking_on.png';
  216. else
  217. child.icon = tree.imageurl+'/talking_off.png';
  218. node.leaf = false;
  219. node.children.push( child );
  220. }
  221. if( json.id == 0 || json.users.length > 0 || subchan_users )
  222. node.expanded = true;
  223. return subchan_users + json.users.length;
  224. }
  225. populateNode( root, respdata.root );
  226. this.setRootNode( root );
  227. },
  228. failure: function( resp, opt ){
  229. if( resp.isTimeout === true )
  230. // Ignore, happens from time to time
  231. return;
  232. if( this.refreshInterval > 0 )
  233. this.cbAutoRefresh.setValue(false);
  234. Ext.Msg.show({
  235. title: gettext("Update error"),
  236. msg: gettext("Querying the server failed, so the channel viewer has not been updated."),
  237. icon: Ext.MessageBox.ERROR,
  238. buttons: Ext.MessageBox.OK
  239. });
  240. },
  241. });
  242. },
  243. } );
  244. Ext.reg( 'mumblechannelviewer', Ext.ux.MumbleChannelViewer );