65fe075124da0ad0d8b1d2dea8015a79bc441d2d
[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 hildon
26 import jamaendo
27 from settings import settings
28 from player import Playlist, the_player
29
30 _player_window = None
31
32 def album_cover_receiver(albumid, size, cover):
33     if _player_window:
34         playing = _player_window.get_album_id()
35         if int(playing) == int(albumid):
36             _player_window.set_album_cover(cover)
37
38 class PlayerWindow(hildon.StackableWindow):
39     def __init__(self, playlist=None):
40         hildon.StackableWindow.__init__(self)
41         self.set_title("jamaendo")
42
43         global _player_window
44         _player_window = self
45
46         self.connect('hide', self.on_hide)
47         self.connect('destroy', self.on_destroy)
48
49         self.playlist = Playlist(playlist)
50         self.player = the_player
51
52         vbox = gtk.VBox()
53
54         hbox = gtk.HBox()
55
56         self.cover = gtk.Image()
57         self.cover.set_size_request(200, 200)
58         self.cover.set_from_stock(gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
59
60         vbox2 = gtk.VBox()
61
62         self.playlist_pos = gtk.Label()
63         self.track = gtk.Label()
64         self.progress = hildon.GtkHScale()
65         self.artist = gtk.Label()
66         self.album = gtk.Label()
67
68         if self.player.playlist.current_index() > -1:
69             pl = self.player.playlist
70             track = pl.current()
71             self.set_labels(track.name, track.artist_name, track.album_name, pl.current_index(), pl.size())
72         else:
73             self.set_labels('', '', '', 0, 0)
74
75         vbox2.pack_start(self.track, True)
76         vbox2.pack_start(self.artist, False)
77         vbox2.pack_start(self.album, False)
78         vbox2.pack_start(self.playlist_pos, False)
79         vbox2.pack_start(self.progress, False)
80
81         hbox.pack_start(self.cover, True, True, 0)
82         hbox.pack_start(vbox2, True, True, 0)
83
84         vbox.pack_start(hbox, True, True, 0)
85
86         btns = gtk.HButtonBox()
87         btns.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
88
89         vbox.pack_end(btns, False, True, 0)
90
91         self.add_stock_button(btns, gtk.STOCK_MEDIA_PREVIOUS, self.on_prev)
92         self.add_stock_button(btns, gtk.STOCK_MEDIA_PLAY, self.on_play)
93         self.add_stock_button(btns, gtk.STOCK_MEDIA_PAUSE, self.on_pause)
94         self.add_stock_button(btns, gtk.STOCK_MEDIA_STOP, self.on_stop)
95         self.add_stock_button(btns, gtk.STOCK_MEDIA_NEXT, self.on_next)
96
97         self.volume = hildon.VVolumebar()
98         self.volume.set_property('can-focus', False)
99         self.volume.connect('level_changed', self.volume_changed_hildon)
100         self.volume.connect('mute_toggled', self.mute_toggled)
101         #self.__gui_root.main_window.connect( 'key-press-event',
102         #                                     self.on_key_press )
103         hbox.pack_start(self.volume, False)
104         self.add(vbox)
105
106         print "Created player window, playlist: %s" % (self.playlist)
107
108     def get_album_id(self):
109         if self.playlist and self.playlist.current_index() > -1:
110             return self.playlist.current().album_id
111         return None
112
113     def on_hide(self, wnd):
114         print "Hiding player window"
115
116     def on_destroy(self, wnd):
117         print "Destroying player window"
118         global _player_window
119         _player_window = None
120         if self.player:
121             self.player.stop()
122
123     def add_stock_button(self, btns, stock, cb):
124         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
125         btn.set_relief(gtk.RELIEF_NONE)
126         btn.set_image(gtk.image_new_from_stock(stock, gtk.ICON_SIZE_SMALL_TOOLBAR))
127         btn.connect('clicked', cb)
128         btns.add(btn)
129
130     def set_labels(self, track, artist, album, playlist_pos, playlist_size):
131         self.playlist_pos.set_markup('<span size="small">%s/%s songs</span>'%(playlist_pos, playlist_size))
132         self.track.set_markup('<span size="x-large">%s</span>'%(track))
133         self.artist.set_markup('<span size="large">%s</span>'%(artist))
134         self.album.set_markup('<span foreground="#aaaaaa">%s</span>'%(album))
135
136
137     def volume_changed_hildon(self, widget):
138         settings.volume = widget.get_level()/100.0
139
140     def mute_toggled(self, widget):
141         if widget.get_mute():
142             settings.volume = 0
143         else:
144             settings.volume = widget.get_level()/100.0
145
146     def update_state(self):
147         item = self.playlist.current()
148         if item:
149             if not item.name:
150                 item.load()
151             print "current:", item
152             self.set_labels(item.name, item.artist_name, item.album_name,
153                             self.playlist.current_index(), self.playlist.size())
154             jamaendo.get_album_cover_async(album_cover_receiver, int(item.album_id), size=200)
155             #coverfile = jamaendo.get_album_cover(int(item.album_id), size=200)
156             #print "coverfile:", coverfile
157             #self.cover.set_from_file(coverfile)
158
159     def set_album_cover(self, cover):
160         self.cover.set_from_file(cover)
161
162     def play_tracks(self, tracks):
163         self.playlist = Playlist(tracks)
164         self.player.play(self.playlist)
165         self.update_state()
166
167     def on_play(self, button):
168         self.player.play(self.playlist)
169         self.update_state()
170     def on_pause(self, button):
171         self.player.pause()
172     def on_prev(self, button):
173         self.player.prev()
174         self.update_state()
175     def on_next(self, button):
176         self.player.next()
177         self.update_state()
178     def on_stop(self, button):
179         self.player.stop()
180
181 def open_playerwindow(tracks=None):
182     player = PlayerWindow(tracks)
183     player.show_all()
184     return player