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