A new status widget.
[python-gnt.git] / example / xmms / xmms_status.py
blob6b34e1e7213bf38ec09293432a16744c979bc09d
1 #!/usr/bin/env python
3 """
4 xmms-status : A status widget for XMMS2.
6 Copyright (C) 2007 Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>
8 This application is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 This application is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with this application; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301
21 USA
22 """
24 import xmmsclient
25 import xmmsclient.glib as xg
26 import xmmsclient.collections as xc
27 import gobject
28 import gnt
29 import os
30 import sys
32 import common
34 __version__ = "0.0.1alpha"
35 __author__ = "Sadrul Habib Chowdhury <sadrul@users.sourceforge.net>"
36 __copyright__ = "Copyright 2007, Sadrul Habib Chowdhury"
37 __license__ = "GPL"
39 reload(sys)
40 sys.setdefaultencoding("utf-8")
42 class XStatus(gnt.Box):
43 """A generic widget displaying the playback status of XMMS2."""
44 def __init__(self, xmms):
45 gnt.Box.__init__(self, False, False)
46 self.set_property('vertical', True)
47 self.set_pad(0)
48 self.xmms = xmms
49 self.current = None
50 self.playtime = ""
52 self.tv = gnt.TextView()
53 self.tv.set_flag(gnt.TEXT_VIEW_NO_SCROLL)
54 self.tv.set_flag(gnt.TEXT_VIEW_TOP_ALIGN)
56 self.tv.append_text_with_flags("Title: ", gnt.TEXT_FLAG_BOLD)
57 self.tv.append_text_with_tag("...", gnt.TEXT_FLAG_NORMAL, "title")
58 self.tv.append_text_with_flags("\nArtist: ", gnt.TEXT_FLAG_BOLD)
59 self.tv.append_text_with_tag("...", gnt.TEXT_FLAG_NORMAL, "artist")
60 self.tv.append_text_with_flags("\nDuration: ", gnt.TEXT_FLAG_BOLD)
61 self.tv.append_text_with_tag(".../...", gnt.TEXT_FLAG_NORMAL, "playtime")
62 self.tv.set_size(-1, 3)
64 self.status = gnt.Label(" " * 15)
65 self.status.set_property("text-flag", gnt.TEXT_FLAG_BOLD);
66 gnt.unset_flag(self.status, gnt.WIDGET_GROW_X)
68 self.progress = gnt.Slider(False, 1, 0)
69 gnt.unset_flag(self.progress, gnt.WIDGET_CAN_TAKE_FOCUS)
71 self.add_widget(self.tv)
72 self.add_widget(common.pack_widget(False, [self.status, self.progress]))
74 # Update playback status
75 self.xmms.broadcast_playback_status(self.update_status)
76 self.xmms.playback_status(self.update_status)
78 # Update playback time
79 self.xmms.signal_playback_playtime(self.update_playtime)
80 self.xmms.playback_playtime(self.update_playtime)
82 # Update song information
83 self.xmms.broadcast_playback_current_id(self.update_song_info)
84 self.xmms.playback_current_id(self.update_song_info)
86 def medialib_entry_changed(result):
87 song = result.value()
88 if song == self.current['id']:
89 self.update_song_info()
90 self.xmms.broadcast_medialib_entry_changed(medialib_entry_changed)
92 def update_status(self, result):
93 val = result.value()
94 status = "..."
95 if val == xmmsclient.PLAYBACK_STATUS_STOP:
96 status = "Stopped"
97 elif val == xmmsclient.PLAYBACK_STATUS_PLAY:
98 status = "Playing"
99 elif val == xmmsclient.PLAYBACK_STATUS_PAUSE:
100 status = "Paused"
101 self.status.set_text(status)
103 def update_playtime(self, result):
104 val = result.value() / 1000
105 self.tv.tag_change("playtime", "%02d:%02d/%s" % (val / 60, val % 60, self.playtime), True)
106 self.tv.draw()
107 self.progress.set_value(val)
108 result.restart()
110 def update_song(self):
111 self.tv.tag_change("title", self.current['title'], True)
112 self.tv.tag_change("artist", self.current['artist'], True)
114 def update_song_info(self, result = None):
115 if result:
116 val = result.value()
117 else:
118 val = self.current['id']
119 def received_song_info(result):
120 self.current = result.value()
121 playtime = int(self.current.get('duration', 0) / 1000)
122 self.progress.set_range(0, playtime)
123 self.playtime = "%02d:%02d" % (playtime / 60, playtime % 60)
124 self.update_song()
125 self.xmms.medialib_get_info(val, received_song_info)
127 def show(self):
128 win = gnt.Window()
129 win.set_pad(0)
130 win.set_title("XMMS2 Status")
131 win.add_widget(self)
132 win.show()
134 def setup_xmms():
135 xmms = xmmsclient.XMMS("pygnt-search")
136 try:
137 xmms.connect(os.getenv("XMMS_PATH"))
138 conn = xg.GLibConnector(xmms)
139 xs = XStatus(xmms)
140 xs.show()
141 except IOError, detail:
142 common.show_error("Connection failed: " + str(detail))
144 if __name__ == '__main__':
145 setup_xmms()
146 gnt.gnt_main()
147 gnt.gnt_quit()