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.

264 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. if( this.refreshInterval == 0 )
  144. this.refresh();
  145. }
  146. Ext.extend( Ext.ux.MumbleChannelViewer, Ext.tree.TreePanel, {
  147. setAutoRefresh: function(){
  148. if( this.autoRefreshId != 0 ){
  149. clearTimeout( this.autoRefreshId );
  150. }
  151. if( this.cbAutoRefresh.getValue() ){
  152. this.refreshInterval = this.nfAutoRefreshInterval.getValue() * 1000;
  153. this.autoRefresh();
  154. }
  155. else{
  156. this.refreshInterval = 0;
  157. }
  158. },
  159. autoRefresh: function(){
  160. this.refresh();
  161. if( this.refreshInterval > 0 ){
  162. this.autoRefreshId = this.autoRefresh.defer( this.refreshInterval, this );
  163. }
  164. },
  165. refresh: function(){
  166. var conn = new Ext.data.Connection();
  167. conn.request({
  168. url: this.source_url,
  169. scope: this,
  170. success: function( resp, opt ){
  171. var respdata = Ext.decode( resp.responseText );
  172. var root = {
  173. text: respdata.name,
  174. nodeType: 'async',
  175. id: "mumbroot",
  176. leaf: false,
  177. icon: this.imageurl+'/mumble.16x16.png',
  178. children: [],
  179. chandata: respdata.root,
  180. imageurl: this.imageurl
  181. };
  182. tree = this;
  183. function populateNode( node, json ){
  184. var subchan_users = 0;
  185. json.channels.sort(cmp_channels);
  186. for( var i = 0; i < json.channels.length; i++ ){
  187. var child = {
  188. text: json.channels[i].name,
  189. id: ("channel_" + json.channels[i].id),
  190. nodeType: 'async',
  191. allowDrag: true,
  192. allowDrop: true,
  193. draggable: true,
  194. icon: tree.imageurl+'/channel.png',
  195. children: [],
  196. uiProvider: Ext.ux.MumbleChannelNodeUI,
  197. chandata: json.channels[i],
  198. imageurl: tree.imageurl
  199. };
  200. node.children.push( child );
  201. subchan_users += populateNode( child, json.channels[i] );
  202. }
  203. json.users.sort(cmp_names);
  204. for( var i = 0; i < json.users.length; i++ ){
  205. var child = {
  206. text: json.users[i].name,
  207. id: ("user_" + json.users[i].session),
  208. nodeType: 'async',
  209. leaf: true,
  210. allowDrag: true,
  211. draggable: true,
  212. uiProvider: Ext.ux.MumbleUserNodeUI,
  213. userdata: json.users[i],
  214. imageurl: tree.imageurl
  215. };
  216. if( json.users[i].idlesecs <= tree.idleInterval )
  217. child.icon = tree.imageurl+'/talking_on.png';
  218. else
  219. child.icon = tree.imageurl+'/talking_off.png';
  220. node.leaf = false;
  221. node.children.push( child );
  222. }
  223. if( json.id == 0 || json.users.length > 0 || subchan_users )
  224. node.expanded = true;
  225. return subchan_users + json.users.length;
  226. }
  227. populateNode( root, respdata.root );
  228. this.setRootNode( root );
  229. },
  230. failure: function( resp, opt ){
  231. if( resp.isTimeout === true )
  232. // Ignore, happens from time to time
  233. return;
  234. if( this.refreshInterval > 0 )
  235. this.cbAutoRefresh.setValue(false);
  236. Ext.Msg.show({
  237. title: gettext("Update error"),
  238. msg: gettext("Querying the server failed, so the channel viewer has not been updated."),
  239. icon: Ext.MessageBox.ERROR,
  240. buttons: Ext.MessageBox.OK
  241. });
  242. },
  243. });
  244. },
  245. } );
  246. Ext.reg( 'mumblechannelviewer', Ext.ux.MumbleChannelViewer );