tiny color change
[jamaendo] / jamaui / showalbum.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 cgi
26 import hildon
27 import jamaendo
28 from player import Playlist
29 from playerwindow import open_playerwindow
30 from settings import settings
31 from postoffice import postoffice
32 import util
33 import logging
34 from albumlist import TrackList
35 from playlists import add_to_playlist
36
37 import webbrowser
38
39 log = logging.getLogger(__name__)
40
41 class ShowAlbum(hildon.StackableWindow):
42     def __init__(self, album):
43         hildon.StackableWindow.__init__(self)
44         self.set_title(album.artist_name)
45         self.album = album
46
47         self.connect('destroy', self.on_destroy)
48
49         top_hbox = gtk.HBox()
50         vbox1 = gtk.VBox()
51         self.cover = gtk.Image()
52         tmp = util.find_resource('album.png')
53         if tmp:
54             self.cover.set_from_file(tmp)
55         self.bbox = gtk.HButtonBox()
56         self.bbox.set_property('layout-style', gtk.BUTTONBOX_SPREAD)
57         self.goto_artist = self.make_imagebutton('artist', self.on_goto_artist)
58         self.download = self.make_imagebutton('download', self.on_download)
59         self.favorite = self.make_imagebutton('favorite', self.on_favorite)
60         self.license = self.make_imagebutton('license', self.on_license)
61
62         vbox2 = gtk.VBox()
63         self.albumname = gtk.Label()
64         self.albumname.set_markup('<big>%s</big>'%(cgi.escape(album.name)))
65         self.trackarea = hildon.PannableArea()
66
67         self.tracks = TrackList(numbers=True)
68         self.tracks.connect('row-activated', self.row_activated)
69
70         self.tracklist = jamaendo.get_tracks(album.ID)
71         for track in self.tracklist:
72             self.tracks.add_track(track)
73
74         top_hbox.pack_start(vbox1, False)
75         top_hbox.pack_start(vbox2, True)
76         vbox1.pack_start(self.cover, True)
77         vbox1.pack_start(self.bbox, False)
78         self.bbox.add(self.goto_artist)
79         self.bbox.add(self.download)
80         self.bbox.add(self.favorite)
81         self.bbox.add(self.license)
82         vbox2.pack_start(self.albumname, False)
83         vbox2.pack_start(self.trackarea, True)
84         self.trackarea.add(self.tracks)
85
86         self.add(top_hbox)
87
88         postoffice.connect('album-cover', self, self.on_album_cover)
89         postoffice.notify('request-album-cover', self.album.ID, 300)
90
91         self.create_menu()
92
93     def create_menu(self):
94         def on_player(*args):
95             from playerwindow import open_playerwindow
96             open_playerwindow()
97         self.menu = hildon.AppMenu()
98         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
99         player.set_label("Open player")
100         player.connect("clicked", on_player)
101         self.menu.append(player)
102         player = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
103         player.set_label("Add to playlist")
104         player.connect("clicked", self.on_add_to_playlist)
105         self.menu.append(player)
106         self.menu.show_all()
107         self.set_app_menu(self.menu)
108
109     def on_destroy(self, wnd):
110         postoffice.disconnect('album-cover', self)
111
112     def on_album_cover(self, albumid, size, cover):
113         if albumid == self.album.ID and size == 300:
114             self.cover.set_from_file(cover)
115
116
117     def on_add_to_playlist(self, button, user_data=None):
118         add_to_playlist(self, self.tracklist)
119
120     def make_imagebutton(self, name, cb):
121         btn = hildon.GtkButton(gtk.HILDON_SIZE_AUTO)
122         btn.set_relief(gtk.RELIEF_NONE)
123         img = util.find_resource('icon_%s.png'%(name))
124         if img:
125             btn.set_image(gtk.image_new_from_file(img))
126         else:
127             btn.set_image(gtk.image_new_from_stock(gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_SMALL_TOOLBAR))
128         btn.set_border_width(0)
129         btn.connect('clicked', cb)
130         return btn
131
132     def on_goto_artist(self, btn):
133         artist = jamaendo.get_artist(int(self.album.artist_id))
134         self.open_item(artist)
135
136     def on_download(self, btn):
137         banner = hildon.hildon_banner_show_information(self, '', "Opening in web browser")
138         banner.set_timeout(2000)
139         url = self.album.torrent_url()
140         webbrowser.open_new(url)
141
142     def on_favorite(self, btn):
143         settings.favorite(self.album)
144         banner = hildon.hildon_banner_show_information(self, '', "Favorite added")
145         banner.set_timeout(2000)
146
147
148     def on_license(self, btn):
149         banner = hildon.hildon_banner_show_information(self, '', "Opening in web browser")
150         banner.set_timeout(2000)
151         url = self.album.license_url
152         webbrowser.open_new(url)
153
154     def on_play(self, btn):
155         self.open_item(self.album)
156
157     def row_activated(self, treeview, path, view_column):
158         _id = self.tracks.get_track_id(path)
159         playlist = Playlist(self.tracklist)
160         playlist.jump_to(_id)
161         wnd = open_playerwindow()
162         wnd.play_tracks(playlist)
163
164     def open_item(self, item):
165         if isinstance(item, jamaendo.Album):
166             tracks = jamaendo.get_tracks(item.ID)
167             if tracks:
168                 wnd = open_playerwindow()
169                 wnd.play_tracks(tracks)
170         elif isinstance(item, jamaendo.Artist):
171             from showartist import ShowArtist
172             wnd = ShowArtist(item)
173             wnd.show_all()
174         elif isinstance(item, jamaendo.Track):
175             wnd = open_playerwindow()
176             wnd.play_tracks([item])