Added missing files
[jamaendo] / jamaui / playerwindow.py
1 import gtk
2 import hildon
3 import jamaendo
4 from player import Playlist, the_player
5
6 class PlayerWindow(hildon.StackableWindow):
7     def __init__(self, playlist=None):
8         hildon.StackableWindow.__init__(self)
9         self.set_title("jamaendo")
10
11         self.connect('hide', self.on_hide)
12         self.connect('destroy', self.on_destroy)
13
14         self.playlist = Playlist(playlist)
15         self.player = the_player
16
17         vbox = gtk.VBox()
18
19         hbox = gtk.HBox()
20
21         self.cover = gtk.Image()
22         self.cover.set_size_request(200, 200)
23         self.cover.set_from_stock(gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
24
25         vbox2 = gtk.VBox()
26
27         self.playlist_pos = gtk.Label()
28         self.track = gtk.Label()
29         self.progress = hildon.GtkHScale()
30         self.artist = gtk.Label()
31         self.album = gtk.Label()
32
33         if self.player.playlist.current_index() > -1:
34             pl = self.player.playlist
35             track = pl.current()
36             self.set_labels(track.name, track.artist_name, track.album_name, pl.current_index(), pl.size())
37         else:
38             self.set_labels('track name', 'artist', 'album', 0, 0)
39
40         vbox2.pack_start(self.track, True)
41         vbox2.pack_start(self.artist, False)
42         vbox2.pack_start(self.album, False)
43         vbox2.pack_start(self.playlist_pos, False)
44         vbox2.pack_start(self.progress, False)
45
46         hbox.pack_start(self.cover, True, True, 0)
47         hbox.pack_start(vbox2, True, True, 0)
48
49         vbox.pack_start(hbox, True, True, 0)
50
51         btns = gtk.HButtonBox()
52         btns.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
53
54         vbox.pack_end(btns, False, True, 0)
55
56         self.add_stock_button(btns, gtk.STOCK_MEDIA_PREVIOUS, self.on_prev)
57         self.add_stock_button(btns, gtk.STOCK_MEDIA_PLAY, self.on_play)
58         self.add_stock_button(btns, gtk.STOCK_MEDIA_PAUSE, self.on_pause)
59         self.add_stock_button(btns, gtk.STOCK_MEDIA_STOP, self.on_stop)
60         self.add_stock_button(btns, gtk.STOCK_MEDIA_NEXT, self.on_next)
61
62         self.add(vbox)
63
64         print "Created player window, playlist: %s" % (self.playlist)
65
66     def on_hide(self, wnd):
67         print "Hiding player window"
68
69     def on_destroy(self, wnd):
70         print "Destroying player window"
71         if self.player:
72             self.player.stop()
73
74     def add_stock_button(self, btns, stock, cb):
75         btn = hildon.GtkButton(gtk.HILDON_SIZE_FINGER_HEIGHT)
76         btn.set_relief(gtk.RELIEF_NONE)
77         btn.set_image(gtk.image_new_from_stock(stock, gtk.ICON_SIZE_SMALL_TOOLBAR))
78         btn.connect('clicked', cb)
79         btns.add(btn)
80
81     def set_labels(self, track, artist, album, playlist_pos, playlist_size):
82         self.playlist_pos.set_markup('<span size="small">%s/%s songs</span>'%(playlist_pos, playlist_size))
83         self.track.set_markup('<span size="x-large">%s</span>'%(track))
84         self.artist.set_markup('<span size="large">%s</span>'%(artist))
85         self.album.set_markup('<span foreground="#aaaaaa">%s</span>'%(album))
86
87     def update_state(self):
88         item = self.playlist.current()
89         if item:
90             if not item.name:
91                 item.load()
92             print "current:", item
93             self.set_labels(item.name, item.artist_name, item.album_name,
94                             self.playlist.current_index(), self.playlist.size())
95             coverfile = jamaendo.get_album_cover(int(item.album_id), size=200)
96             print "coverfile:", coverfile
97             self.cover.set_from_file(coverfile)
98
99     def play_tracks(self, tracks):
100         self.playlist = Playlist(tracks)
101         self.player.play(self.playlist)
102         self.update_state()
103
104     def on_play(self, button):
105         self.player.play(self.playlist)
106         self.update_state()
107     def on_pause(self, button):
108         self.player.pause()
109     def on_prev(self, button):
110         self.player.prev()
111         self.update_state()
112     def on_next(self, button):
113         self.player.next()
114         self.update_state()
115     def on_stop(self, button):
116         self.player.stop()
117
118 def open_playerwindow(tracks):
119     player = PlayerWindow(tracks)
120     player.show_all()
121     return player