3 # This file is part of Jamaendo.
4 # Copyright (c) 2010 Kristoffer Gronlund
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.
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.
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/>.
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)
27 from settings import settings
28 from postoffice import postoffice
29 from player import Playlist, the_player
32 log = logging.getLogger(__name__)
34 class PlayerWindow(hildon.StackableWindow):
35 def __init__(self, playlist=None):
36 hildon.StackableWindow.__init__(self)
37 self.set_title("jamaendo")
39 self.connect('destroy', self.on_destroy)
41 self.playlist = Playlist(playlist)
42 self.player = the_player
48 self.cover = gtk.Image()
49 self.cover.set_from_stock(gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
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()
61 if self.player.playlist.current_index() > -1:
62 pl = self.player.playlist
64 self.set_labels(track.name, track.artist_name, track.album_name, pl.current_index(), pl.size())
66 self.set_labels('', '', '', 0, 0)
68 self._position_timer = None
70 vbox2.pack_start(self.track, True)
71 vbox2.pack_start(self.artist, False)
72 vbox2.pack_start(self.album, False)
73 vbox2.pack_start(self.playlist_pos, False)
74 vbox2.pack_start(self.progress, False)
76 hbox.pack_start(self.cover, True, True, 0)
77 hbox.pack_start(vbox2, True, True, 0)
79 vbox.pack_start(hbox, True, True, 0)
81 btns = gtk.HButtonBox()
82 btns.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
84 vbox.pack_end(btns, False, True, 0)
86 self.add_stock_button(btns, gtk.STOCK_MEDIA_PREVIOUS, self.on_prev)
87 self.add_stock_button(btns, gtk.STOCK_MEDIA_PLAY, self.on_play)
88 self.add_stock_button(btns, gtk.STOCK_MEDIA_PAUSE, self.on_pause)
89 self.add_stock_button(btns, gtk.STOCK_MEDIA_STOP, self.on_stop)
90 self.add_stock_button(btns, gtk.STOCK_MEDIA_NEXT, self.on_next)
92 self.volume = hildon.VVolumebar()
93 self.volume.set_property('can-focus', False)
94 self.volume.connect('level_changed', self.volume_changed_hildon)
95 self.volume.connect('mute_toggled', self.mute_toggled)
96 #self.__gui_root.main_window.connect( 'key-press-event',
98 hbox.pack_start(self.volume, False)
101 postoffice.connect('album-cover', self.set_album_cover)
103 #print "Created player window, playlist: %s" % (self.playlist)
105 def get_album_id(self):
106 if self.playlist and self.playlist.current_index() > -1:
107 return self.playlist.current().album_id
110 def on_destroy(self, wnd):
111 postoffice.disconnect('album-cover', self.set_album_cover)
113 def add_stock_button(self, btns, stock, cb):
114 btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
115 btn.set_relief(gtk.RELIEF_NONE)
116 btn.set_image(gtk.image_new_from_stock(stock, gtk.ICON_SIZE_SMALL_TOOLBAR))
117 btn.connect('clicked', cb)
120 def set_labels(self, track, artist, album, playlist_pos, playlist_size):
121 self.playlist_pos.set_markup('<span size="small">%s/%s songs</span>'%(playlist_pos, playlist_size))
122 self.track.set_markup('<span size="x-large">%s</span>'%(track))
123 self.artist.set_markup('<span size="large">%s</span>'%(artist))
124 self.album.set_markup('<span foreground="#aaaaaa">%s</span>'%(album))
127 def volume_changed_hildon(self, widget):
128 settings.volume = widget.get_level()/100.0
130 def mute_toggled(self, widget):
131 if widget.get_mute():
134 settings.volume = widget.get_level()/100.0
136 def on_position_timeout(self):
137 if the_player.playing():
138 self.set_song_position(*the_player.get_position_duration())
140 log.debug("position timeout, but not playing")
143 def start_position_timer(self):
144 if self._position_timer is not None:
145 self.stop_position_timer()
146 self._position_timer = gobject.timeout_add(1000, self.on_position_timeout)
148 def stop_position_timer(self):
149 if self._position_timer is not None:
150 gobject.source_remove(self._position_timer)
151 self._position_timer = None
153 def clear_position(self):
154 self.progress.set_value(0)
156 def set_song_position(self, time_elapsed, total_time):
157 value = (float(time_elapsed) / float(total_time)) if total_time else 0
158 self.progress.set_value( value )
160 def update_state(self):
161 item = self.playlist.current()
165 #print "current:", item
166 self.set_labels(item.name, item.artist_name, item.album_name,
167 self.playlist.current_index(), self.playlist.size())
168 postoffice.notify('request-album-cover', int(item.album_id), 300)
169 #jamaendo.get_album_cover_async(album_cover_receiver, int(item.album_id), size=200)
170 #coverfile = jamaendo.get_album_cover(int(item.album_id), size=200)
171 #print "coverfile:", coverfile
172 #self.cover.set_from_file(coverfile)
174 def set_album_cover(self, albumid, size, cover):
176 playing = self.get_album_id()
177 if int(playing) == int(albumid):
178 self.cover.set_from_file(cover)
180 def play_tracks(self, tracks):
181 self.playlist = Playlist(tracks)
182 self.clear_position()
183 self.start_position_timer()
184 self.player.play(self.playlist)
187 def on_play(self, button):
188 self.player.play(self.playlist)
189 self.start_position_timer()
191 def on_pause(self, button):
192 self.stop_position_timer()
194 def on_prev(self, button):
197 def on_next(self, button):
200 def on_stop(self, button):
201 self.stop_position_timer()
202 self.clear_position()
205 def open_playerwindow(tracks=None):
206 player = PlayerWindow(tracks)