Progress timer
[jamaendo] / jamaui / playerwindow.py
1 #!/usr/bin/env python
2 #
3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
5 #
6 # Jamaendo is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Jamaendo is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Jamaendo.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 # Player code heavily based on http://thpinfo.com/2008/panucci/:
20 #  A resuming media player for Podcasts and Audiobooks
21 #  Copyright (c) 2008-05-26 Thomas Perl <thpinfo.com>
22 #  (based on http://pygstdocs.berlios.de/pygst-tutorial/seeking.html)
23 #
24 import gtk
25 import gobject
26 import hildon
27 from settings import settings
28 from postoffice import postoffice
29 from player import Playlist, the_player
30
31 class PlayerWindow(hildon.StackableWindow):
32     def __init__(self, playlist=None):
33         hildon.StackableWindow.__init__(self)
34         self.set_title("jamaendo")
35
36         self.connect('destroy', self.on_destroy)
37
38         self.playlist = Playlist(playlist)
39         self.player = the_player
40
41         vbox = gtk.VBox()
42
43         hbox = gtk.HBox()
44
45         self.cover = gtk.Image()
46         self.cover.set_from_stock(gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
47
48         vbox2 = gtk.VBox()
49
50         self.playlist_pos = gtk.Label()
51         self.track = gtk.Label()
52         self.progress = hildon.GtkHScale()
53         self.progress.set_draw_value(False)
54         self.progress.set_range(0.0, 1.0)
55         self.artist = gtk.Label()
56         self.album = gtk.Label()
57
58         if self.player.playlist.current_index() > -1:
59             pl = self.player.playlist
60             track = pl.current()
61             self.set_labels(track.name, track.artist_name, track.album_name, pl.current_index(), pl.size())
62         else:
63             self.set_labels('', '', '', 0, 0)
64
65         self._position_timer = None
66
67         vbox2.pack_start(self.track, True)
68         vbox2.pack_start(self.artist, False)
69         vbox2.pack_start(self.album, False)
70         vbox2.pack_start(self.playlist_pos, False)
71         vbox2.pack_start(self.progress, False)
72
73         hbox.pack_start(self.cover, True, True, 0)
74         hbox.pack_start(vbox2, True, True, 0)
75
76         vbox.pack_start(hbox, True, True, 0)
77
78         btns = gtk.HButtonBox()
79         btns.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
80
81         vbox.pack_end(btns, False, True, 0)
82
83         self.add_stock_button(btns, gtk.STOCK_MEDIA_PREVIOUS, self.on_prev)
84         self.add_stock_button(btns, gtk.STOCK_MEDIA_PLAY, self.on_play)
85         self.add_stock_button(btns, gtk.STOCK_MEDIA_PAUSE, self.on_pause)
86         self.add_stock_button(btns, gtk.STOCK_MEDIA_STOP, self.on_stop)
87         self.add_stock_button(btns, gtk.STOCK_MEDIA_NEXT, self.on_next)
88
89         self.volume = hildon.VVolumebar()
90         self.volume.set_property('can-focus', False)
91         self.volume.connect('level_changed', self.volume_changed_hildon)
92         self.volume.connect('mute_toggled', self.mute_toggled)
93         #self.__gui_root.main_window.connect( 'key-press-event',
94         #                                     self.on_key_press )
95         hbox.pack_start(self.volume, False)
96         self.add(vbox)
97
98         postoffice.connect('album-cover', self.set_album_cover)
99
100         print "Created player window, playlist: %s" % (self.playlist)
101
102     def get_album_id(self):
103         if self.playlist and self.playlist.current_index() > -1:
104             return self.playlist.current().album_id
105         return None
106
107     def on_destroy(self, wnd):
108         postoffice.disconnect('album-cover', self.set_album_cover)
109
110     def add_stock_button(self, btns, stock, cb):
111         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
112         btn.set_relief(gtk.RELIEF_NONE)
113         btn.set_image(gtk.image_new_from_stock(stock, gtk.ICON_SIZE_SMALL_TOOLBAR))
114         btn.connect('clicked', cb)
115         btns.add(btn)
116
117     def set_labels(self, track, artist, album, playlist_pos, playlist_size):
118         self.playlist_pos.set_markup('<span size="small">%s/%s songs</span>'%(playlist_pos, playlist_size))
119         self.track.set_markup('<span size="x-large">%s</span>'%(track))
120         self.artist.set_markup('<span size="large">%s</span>'%(artist))
121         self.album.set_markup('<span foreground="#aaaaaa">%s</span>'%(album))
122
123
124     def volume_changed_hildon(self, widget):
125         settings.volume = widget.get_level()/100.0
126
127     def mute_toggled(self, widget):
128         if widget.get_mute():
129             settings.volume = 0
130         else:
131             settings.volume = widget.get_level()/100.0
132
133     def on_position_timeout(self):
134         if the_player.playing():
135             self.set_position(*the_player.get_position_duration())
136         return True
137
138     def start_position_timer(self):
139         if self._position_timer is not None:
140             self.stop_position_timer()
141         self._position_timer = gobject.timeout_add(1000, self.on_position_timeout)
142
143     def stop_position_timer(self):
144         if self._position_timer is not None:
145             gobject.source_remove(self._position_timer)
146             self._position_timer = None
147
148     def clear_position(self):
149         self.progress.set_value(0)
150
151     def set_position(self, time_elapsed, total_time):
152         value = (float(time_elapsed) / float(total_time)) if total_time else 0
153         self.progress.set_value( value )
154
155     def update_state(self):
156         item = self.playlist.current()
157         if item:
158             if not item.name:
159                 item.load()
160             print "current:", item
161             self.set_labels(item.name, item.artist_name, item.album_name,
162                             self.playlist.current_index(), self.playlist.size())
163             postoffice.notify('request-album-cover', int(item.album_id), 300)
164             #jamaendo.get_album_cover_async(album_cover_receiver, int(item.album_id), size=200)
165             #coverfile = jamaendo.get_album_cover(int(item.album_id), size=200)
166             #print "coverfile:", coverfile
167             #self.cover.set_from_file(coverfile)
168
169     def set_album_cover(self, albumid, size, cover):
170         if size == 300:
171             playing = self.get_album_id()
172             if int(playing) == int(albumid):
173                 self.cover.set_from_file(cover)
174
175     def play_tracks(self, tracks):
176         self.playlist = Playlist(tracks)
177         self.clear_position()
178         self.start_position_timer()
179         self.player.play(self.playlist)
180         self.update_state()
181
182     def on_play(self, button):
183         self.player.play(self.playlist)
184         self.start_position_timer()
185         self.update_state()
186     def on_pause(self, button):
187         self.stop_position_timer()
188         self.player.pause()
189     def on_prev(self, button):
190         self.player.prev()
191         self.update_state()
192     def on_next(self, button):
193         self.player.next()
194         self.update_state()
195     def on_stop(self, button):
196         self.stop_position_timer()
197         self.clear_position()
198         self.player.stop()
199
200 def open_playerwindow(tracks=None):
201     player = PlayerWindow(tracks)
202     player.show_all()
203     return player