diff browser_side/radiocol.py @ 285:4f0c2fea358a

browser_side (plugin radiocol): use the status label to give more information
author souliane <souliane@mailoo.org>
date Tue, 26 Nov 2013 19:51:02 +0100
parents bee4719af9b9
children e76ec07be8e5
line wrap: on
line diff
--- a/browser_side/radiocol.py	Mon Nov 25 21:04:09 2013 +0100
+++ b/browser_side/radiocol.py	Tue Nov 26 19:51:02 2013 +0100
@@ -22,21 +22,17 @@
 import pyjd  # this is dummy in pyjs
 from pyjamas.ui.VerticalPanel import VerticalPanel
 from pyjamas.ui.HorizontalPanel import HorizontalPanel
-from pyjamas.ui.SimplePanel import SimplePanel
 from pyjamas.ui.FlexTable import FlexTable
 from pyjamas.ui.FormPanel import FormPanel
-from pyjamas.ui.NamedFrame import NamedFrame
 from pyjamas.ui.Label import Label
 from pyjamas.ui.Button import Button
 from pyjamas.ui.ClickListener import ClickHandler
-from pyjamas.ui.MouseListener import MouseHandler
 from pyjamas.ui.Hidden import Hidden
 from pyjamas.ui.HTML import HTML
 from pyjamas import Window
 from pyjamas.Timer import Timer
 from __pyjamas__ import JS
 
-from jid import JID
 from tools import html_sanitize
 from tools import FilterFileUpload
 from sat_frontends.tools.misc import DEFAULT_MUC
@@ -79,13 +75,12 @@
 class ControlPanel(FormPanel):
     """Panel used to show controls to add a song, or vote for the current one"""
 
-    def __init__(self, referee):
+    def __init__(self, parent):
         FormPanel.__init__(self)
-        self._timer = Timer(notify=self._timeCb)
         self.setEncoding(FormPanel.ENCODING_MULTIPART)
         self.setMethod(FormPanel.METHOD_POST)
         self.setAction("upload_radiocol")
-        vPanel = VerticalPanel()
+        self._parent = parent
 
         hPanel = HorizontalPanel()
         hPanel.setSpacing(5)
@@ -99,19 +94,30 @@
         hPanel.add(self.upload_btn)
 
         self.status = Label()
+        self.updateStatus()
         hPanel.add(self.status)
 
-        vPanel.add(hPanel)
 
         #We need to know the referee
-        referee_field = Hidden('referee', referee)
-        vPanel.add(referee_field)
+        referee_field = Hidden('referee', self._parent.referee)
+        hPanel.add(referee_field)
 
-        self.add(vPanel)
+        self.add(hPanel)
         self.addFormHandler(self)
 
-    def _timeCb(self, timer):
-        self.status.setText('')
+    def updateStatus(self):
+        # TODO: the status should be different if a song is being played or not
+        queue = self._parent.getQueueSize()
+        queue_data = self._parent.queue_data
+        if queue < queue_data[0]:
+            left = queue_data[0] - queue
+            self.status.setText("[we need %d more song%s]" % (left, "s" if left > 1 else ""))
+        elif queue < queue_data[1]:
+            left = queue_data[1] - queue
+            self.status.setText("[%d available spot%s]" % (left, "s" if left > 1 else ""))
+        elif queue >= queue_data[1]:
+                self.status.setText("[The queue is currently full]")
+        self.status.setStyleName('radiocol_status')
 
     def onBtnClick(self):
         if self.file_upload.check():
@@ -130,16 +136,17 @@
         self.file_upload.setVisible(True)
         self.upload_btn.setEnabled(True)
 
+    def setTemporaryStatus(self, text, style):
+        self.status.setText(text)
+        self.status.setStyleName('radiocol_upload_status_%s' % style)
+        Timer(5000, self.updateStatus)
+
     def onSubmitComplete(self, event):
         result = event.getResults()
         if result == "OK":
-            self.status.setText('[Your song has been added to queue]')
-            self.status.setStyleName('radiocol_upload_status_ok')
-            self._timer.schedule(5000)
+            self.setTemporaryStatus('[Your song has been added to queue]', "ok")
         elif result == "KO":
-            self.status.setText('[Something went wrong during your song upload]')
-            self.status.setStyleName('radiocol_upload_status_ko')
-            self._timer.schedule(5000)
+            self.setTemporaryStatus('[Something went wrong during your song upload]', "ko")
         else:
             Window.alert('Submit error: %s' % result)
             self.status.setText('')
@@ -182,11 +189,12 @@
 
 class RadioColPanel(HorizontalPanel, ClickHandler):
 
-    def __init__(self, parent, referee, player_nick, players=None):
+    def __init__(self, parent, referee, player_nick, players, queue_data):
         HorizontalPanel.__init__(self)
         ClickHandler.__init__(self)
         self._parent = parent
         self.referee = referee
+        self.queue_data = queue_data
         self.setStyleName("radiocolPanel")
         self.setHeight('30%')
 
@@ -198,7 +206,7 @@
         self.right_panel = VerticalPanel()
         self.metadata_panel = MetadataPanel()
         self.right_panel.add(self.metadata_panel)
-        self.control_panel = ControlPanel(self.referee)
+        self.control_panel = ControlPanel(self)
         self.right_panel.add(self.control_panel)
         self.add(self.right_panel)
         #self.right_panel.setBorderWidth(1)
@@ -231,6 +239,9 @@
         next_song = self.next_songs.pop(0)
         self.left_panel.remove(next_song)
 
+    def getQueueSize(self):
+        return len(self.left_panel.getChildren())
+
     def radiocolPreload(self, filename, title, artist, album):
         preloaded = False
         for player in self.players:
@@ -252,6 +263,7 @@
                 player.play()
                 self.popNextSong()
                 self.current_player = player
+                self.control_panel.updateStatus()
                 return
         print("WARNING: Song not found in queue, can't play it. This should not happen")