01df4967c9b2dcba122a5ca9edcc4ed300b5925d
[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 import logging
31
32 log = logging.getLogger(__name__)
33
34 class PlayerWindow(hildon.StackableWindow):
35     def __init__(self):
36         hildon.StackableWindow.__init__(self)
37         self.set_title("jamaendo")
38
39         self.connect('destroy', self.on_destroy)
40
41         self.player = the_player
42         self.playlist = the_player.playlist
43
44         vbox = gtk.VBox()
45
46         hbox = gtk.HBox()
47
48         self.cover = gtk.Image()
49         self.cover.set_from_stock(gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
50
51         vbox2 = gtk.VBox()
52
53         self.playlist_pos = gtk.Label()
54         self.track = gtk.Label()
55         self.progress = hildon.GtkHScale()
56         self.progress.set_draw_value(False)
57         self.progress.set_range(0.0, 1.0)
58         self.artist = gtk.Label()
59         self.album = gtk.Label()
60
61         self.set_labels('', '', '', 0, 0)
62
63         self._position_timer = None
64
65         vbox2.pack_start(self.track, True)
66         vbox2.pack_start(self.artist, False)
67         vbox2.pack_start(self.album, False)
68         vbox2.pack_start(self.playlist_pos, False)
69         vbox2.pack_start(self.progress, False)
70
71         hbox.pack_start(self.cover, True, True, 0)
72         hbox.pack_start(vbox2, True, True, 0)
73
74         vbox.pack_start(hbox, True, True, 0)
75
76         btns = gtk.HButtonBox()
77         btns.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
78
79         vbox.pack_end(btns, False, True, 0)
80
81         self.add_stock_button(btns, gtk.STOCK_MEDIA_PREVIOUS, self.on_prev)
82         self.add_play_button(btns)
83         self.add_stock_button(btns, gtk.STOCK_MEDIA_STOP, self.on_stop)
84         self.add_stock_button(btns, gtk.STOCK_MEDIA_NEXT, self.on_next)
85
86         self.volume = hildon.VVolumebar()
87         self.volume.set_property('can-focus', False)
88         self.volume.connect('level_changed', self.volume_changed_hildon)
89         self.volume.connect('mute_toggled', self.mute_toggled)
90         #self.__gui_root.main_window.connect( 'key-press-event',
91         #                                     self.on_key_press )
92         hbox.pack_start(self.volume, False)
93         self.add(vbox)
94
95         postoffice.connect('album-cover', self, self.set_album_cover)
96
97         #print "Created player window, playlist: %s" % (self.playlist)
98
99         self.update_state()
100         self.update_play_button()
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         self.stop_position_timer()
109         postoffice.disconnect('album-cover', self)
110
111     def add_stock_button(self, btns, stock, cb):
112         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
113         btn.set_relief(gtk.RELIEF_NONE)
114         sz = gtk.ICON_SIZE_BUTTON
115         btn.set_image(gtk.image_new_from_stock(stock, sz))
116         btn.connect('clicked', cb)
117         btns.add(btn)
118
119     def add_play_button(self, btns):
120         sz = gtk.ICON_SIZE_BUTTON
121         self.playimg = gtk.image_new_from_stock(gtk.STOCK_MEDIA_PLAY, sz)
122         self.pauseimg = gtk.image_new_from_stock(gtk.STOCK_MEDIA_PAUSE, sz)
123         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
124         btn.set_relief(gtk.RELIEF_NONE)
125         if self.player.playing():
126             btn.set_image(self.pauseimg)
127             btn.set_data('state', 'pause')
128         else:
129             btn.set_image(self.playimg)
130             btn.set_data('state', 'play')
131         btn.connect('clicked', self.on_play)
132         btns.add(btn)
133         self.playbtn = btn
134
135     def update_play_button(self):
136         if self.player.playing():
137             self.playbtn.set_image(self.pauseimg)
138             self.playbtn.set_data('state', 'pause')
139         else:
140             self.playbtn.set_image(self.playimg)
141             self.playbtn.set_data('state', 'play')
142
143     def set_labels(self, track, artist, album, playlist_pos, playlist_size):
144         self.playlist_pos.set_markup('<span size="small">%s/%s songs</span>'%(playlist_pos, playlist_size))
145         self.track.set_markup('<span size="x-large">%s</span>'%(track))
146         self.artist.set_markup('<span size="large">%s</span>'%(artist))
147         self.album.set_markup('<span foreground="#aaaaaa">%s</span>'%(album))
148
149
150     def volume_changed_hildon(self, widget):
151         settings.volume = widget.get_level()/100.0
152
153     def mute_toggled(self, widget):
154         if widget.get_mute():
155             settings.volume = 0
156         else:
157             settings.volume = widget.get_level()/100.0
158
159     def on_position_timeout(self):
160         if the_player.playing():
161             self.set_song_position(*the_player.get_position_duration())
162         else:
163             log.debug("position timeout, but not playing")
164         return True
165
166     def start_position_timer(self):
167         if self._position_timer is not None:
168             self.stop_position_timer()
169         self._position_timer = gobject.timeout_add(1000, self.on_position_timeout)
170
171     def stop_position_timer(self):
172         if self._position_timer is not None:
173             gobject.source_remove(self._position_timer)
174             self._position_timer = None
175
176     def clear_position(self):
177         self.progress.set_value(0)
178
179     def set_song_position(self, time_elapsed, total_time):
180         value = (float(time_elapsed) / float(total_time)) if total_time else 0
181         self.progress.set_value( value )
182
183     def update_state(self):
184         item = self.playlist.current()
185         if item:
186             if not item.name:
187                 item.load()
188             #print "current:", item
189             self.set_labels(item.name, item.artist_name, item.album_name,
190                             self.playlist.current_index(), self.playlist.size())
191             postoffice.notify('request-album-cover', int(item.album_id), 300)
192             #jamaendo.get_album_cover_async(album_cover_receiver, int(item.album_id), size=200)
193             #coverfile = jamaendo.get_album_cover(int(item.album_id), size=200)
194             #print "coverfile:", coverfile
195             #self.cover.set_from_file(coverfile)
196
197     def set_album_cover(self, albumid, size, cover):
198         if size == 300:
199             playing = self.get_album_id()
200             if int(playing) == int(albumid):
201                 self.cover.set_from_file(cover)
202
203     def play_tracks(self, tracks):
204         self.stop_position_timer()
205         self.clear_position()
206         self.playlist = Playlist(tracks)
207         self.player.stop()
208         self.player.play(self.playlist)
209         self.update_state()
210         self.update_play_button()
211
212     def on_play(self, button):
213         if not self.player.playing():
214             self.player.play(self.playlist)
215             self.start_position_timer()
216             self.update_state()
217             self.update_play_button()
218         else:
219             self.stop_position_timer()
220             self.player.pause()
221             self.update_state()
222             self.update_play_button()
223     def on_prev(self, button):
224         self.player.prev()
225         self.update_state()
226     def on_next(self, button):
227         self.player.next()
228         self.update_state()
229     def on_stop(self, button):
230         self.stop_position_timer()
231         self.clear_position()
232         self.player.stop()
233         self.update_play_button()
234
235 def open_playerwindow():
236     player = PlayerWindow()
237     player.show_all()
238     return player