[svn] Adding xmms selection and last display. immsview
authorfabien
Sun, 01 Feb 2004 10:59:04 -0500
branchimmsview
changeset 2 fc119540f904
parent 1 a445a5e6c775
child 3 d488fff3852f
[svn] Adding xmms selection and last display.
immsview
--- a/immsview	Sat Jan 31 23:40:41 2004 -0500
+++ b/immsview	Sun Feb 01 10:59:04 2004 -0500
@@ -1,6 +1,6 @@
 #!/usr/bin/python
 
-_immsview_version = "$Id: immsview 1683 2004-02-01 04:40:41Z fabien $"
+_immsview_version = "$Id: immsview 1684 2004-02-01 15:59:04Z fabien $"
 
 # Copyright (C) 2004 by Fabien Ninoles
 
@@ -26,7 +26,6 @@
 #   - Columns ordering (should pass to GTK probably for this).
 #   - Rating edition
 # * XMMS:
-#   - current song display
 #   - selecting a song
 #   - getting current playlist
 #   - editing playlist
@@ -40,6 +39,8 @@
 import Tkinter
 import ScrolledText
 import gettext
+import xmms.control
+import time
 
 gettext.bindtextdomain('immsview', '/usr/share/immsview/LANG')
 gettext.textdomain('immsview')
@@ -47,35 +48,79 @@
 
 _dbname = os.environ['HOME'] + '/.imms/imms.db'
 
+def strtime(seconds):
+    secs = abs(round(seconds))
+    minutes = secs / 60;
+    hours = minutes / 60;
+    days = hours / 24;
+    secs = secs % 60;
+    minutes %= 60;
+    hours %= 24;
+
+    if seconds < 0:
+            s = "-"
+    else:
+            s = ""
+    if days >= 1:
+            s += "%dd %dh" % (days, hours)
+    elif hours >= 1:
+            s += "%dh%02d" % (hours, minutes)
+    elif minutes >= 1:
+            s += "%d'%02d\"" % (minutes, secs)
+    else:
+            s += "%d\"" % (secs)
+    return s
+
+
 class IMMSDb:
 	def __init__(self):
 		self.cx = sqlite.connect(_dbname)
 	def commit(self):
 		self.cx.commit()
-	def get_ratings(self, min = 0, max = 250):
+	def _get_ratings(self, min = 0, max = 250):
 		cu = self.cx.cursor()
-		cu.execute('''SELECT Rating.uid, Rating.rating
+		cu.execute('''SELECT Rating.uid, Rating.rating 
 			FROM Rating
 			WHERE Rating.rating >= %d
 			AND Rating.rating <= %d
 			ORDER BY Rating.rating;''' % (min, max))
 		return cu.fetchall()
-	def get_library_uid(self, uid):
+	def _get_library_uid(self, uid):
 		cu = self.cx.cursor()
 		cu.execute('''SELECT Library.path
 			FROM Library
 			WHERE Library.uid = %d;''' % (uid,))
 		return cu.fetchone()
-	def get_library_by_path(self, path):
+	def _get_library_by_path(self, path):
 		cu = self.cx.cursor()
 		cu.execute('''SELECT Library.uid, Library.sid
 			FROM Library
 			WHERE Library.path = '%s';''' % (path))
+        def get_ratings_and_info(self):
+                cu = self.cx.cursor()
+                cu.execute('''SELECT Rating.uid, Rating.rating,
+                                Library.path, Last.last
+                                FROM Rating, Library, Last
+                                WHERE Rating.uid = Library.uid AND
+                                Library.sid = Last.sid
+                                ORDER BY Rating.rating DESC;''')
+                # Better to fetch everything since locking can really mess
+                # things in imms plugin.
+                res = cu.fetchall()
+                self.commit()
+                results = []
+                for tune in res:
+                        tmp = {'rating' : tune[1],
+                               'path' : tune[2],
+                               'last' : int(tune[3])}
+                        results.append(tmp)
+                return results
 
 class IMMSView(Tkinter.Frame):
 	def __init__(self, master = None):
 		Tkinter.Frame.__init__(self, master)
 		self.db = IMMSDb()
+                self.xmms = xmms.control
 		self.create_widgets()
 	def create_widgets(self):
 		lf = Tkinter.Frame(self)
@@ -93,14 +138,28 @@
 		self.lb['xscrollcommand'] = xsb.set
 		
 		self.refresh()
+                
 	def refresh(self):
-		rates = self.db.get_ratings()
+                curtime = time.time()
+                filename = self.xmms.get_playlist_file(
+                        self.xmms.get_playlist_pos())
+		tunes = self.db.get_ratings_and_info()
 		self.lb.delete(0,self.lb.size())
-		for rate in rates:
-			lib = self.db.get_library_uid(rate[0])
-			self.lb.insert(-1, "%4d - %s" % (rate[1], lib[0]))
-		self.db.commit()
-		self.lb.see(0)
+                l = []
+                idx = -1
+                for tune in tunes:
+                        if idx < 0:
+                                if tune['path'] == filename:
+                                        idx = tunes.index(tune)
+                        l.append("%4d: %s [%s]" %
+                                (tune['rating'], tune['path'],
+                                 strtime(curtime - tune['last'])))
+                apply(self.lb.insert, [0]+ l)
+                if idx < 0:
+                        idx = 0
+		self.lb.see(idx)
+                self.lb.select_set(idx)
+
 class IMMSToolbar(Tkinter.Frame):
 	def __init__(self, master = None):
 		Tkinter.Frame.__init__(self, master)