clArtistLayout.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2008 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 ######################################################################
00022 #                                                                    #
00023 #                     Some default Layouts                           #
00024 #                                                                    #
00025 ######################################################################    
00026 #
00027 # Top Layout - main album browser
00028 #
00029 
00030 import gtk
00031 import hashlib
00032 import os
00033 import urllib
00034 
00035 from modules import clWidgets
00036 from modules import functions
00037 from modules import clThreadedDownload
00038 
00039 class ArtistLayout(gtk.Layout):
00040 
00041     def __init__(self, pyjama):
00042         self.pyjama = pyjama
00043         
00044         gtk.Layout.__init__(self)
00045         self.set_size(700,400)
00046         
00047         # Labels shown in Artist- View
00048         self.ArtistLabels={}
00049 
00050         # Holds artist's ablums
00051         self.albuminfos = {}
00052 
00053         # Labels for Artist- View
00054 
00055         y = 10          # current y position
00056         xspace = 250    # spacing on x- axes
00057         yspace = 20     # spacing on y- axes
00058         self.ArtistInfos = ['name', 'country', 'albums', 'url']
00059         self.ArtistCaptions = [_("Name"), _("Country"), _("#Albums"), _("link")]
00060         for info in xrange(0, len(self.ArtistInfos)):
00061             if self.ArtistInfos[info] == "url":
00062                 self.ArtistLabels[self.ArtistInfos[info]] = clWidgets.MyLinkButton("", _("link")) #str(album['album_name'][:20])
00063                 self.put(self.ArtistLabels[self.ArtistInfos[info]], xspace, y)
00064                 self.ArtistLabels[self.ArtistInfos[info]].show()
00065                 self.ArtistLabels[self.ArtistInfos[info]].connect("clicked", self.on_zumArtist_clicked)
00066             else:
00067                 self.ArtistLabels[self.ArtistInfos[info]] = gtk.Label("")
00068                 self.ArtistLabels[self.ArtistInfos[info]].set_single_line_mode(True)
00069                 self.put(self.ArtistLabels[self.ArtistInfos[info]], xspace, y)
00070                 self.ArtistLabels[self.ArtistInfos[info]].show()
00071             y+=yspace
00072             
00073         self.image_artist = gtk.Image()
00074         self.put(self.image_artist, 30, 10)
00075         self.image_artist.show()
00076 
00077         self.show()
00078         self.pyjama.window.setcolor(self)
00079 
00080         self.pyjama.Events.connect_event("scrolled_window_resized", self.ev_scrolled_window_resized)
00081 
00082     def ev_scrolled_window_resized(self):
00083         #
00084         # Rearrange layout
00085         #
00086         self.arrange_artistdetail()
00087         self.show()
00088 
00089     def draw(self, data1, data2, data3, data4):
00090         #
00091         # Setting label
00092         #
00093         try:
00094             #markup = self.pyjama.window.markuplbCaption.replace("TEXT", _("Showing infos and albums concerning '%s'") % data1[0]['artist_name'])
00095             txt =  _("Showing infos and albums concerning '%s'") % data1[0]['artist_name']
00096         except KeyError:
00097             print ("Artist not in database, yet.")
00098             dia = clWidgets.MyDialog(_('Artist non existant.'),
00099                                 self.pyjama.window.get_toplevel(),
00100                                 gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, 
00101                                 (gtk.STOCK_OK, gtk.RESPONSE_ACCEPT), gtk.STOCK_DIALOG_WARNING, _('This artist is not in the database.\nPerhaps you are using a old local database\nor Jamendo did not update their dump, yet'))
00102             dia.run()
00103             dia.destroy()
00104 #        self.pyjama.window.lbCaption.set_markup(markup)
00105         self.pyjama.window.LayoutInfo.set_text(txt)
00106         self.pyjama.window.LayoutInfo.set_image("personal.png")
00107 
00108         artistinfos = data1
00109         self.toolbar = self.pyjama.layouts.toolbars['artist']
00110 
00111         for albuminfo in self.albuminfos:
00112             self.albuminfos[albuminfo].destroy()
00113             
00114         self.albuminfos = {}
00115 
00116         img = artistinfos[0]['artist_image']
00117         name = artistinfos[0]['artist_name']
00118         artistID = artistinfos[0]['artist_id']
00119         #genre = artistinfos[0]['artist_genre']
00120         country = artistinfos[0]['artist_country']
00121         #link = artistinfos[0]['artist_link']
00122         url = artistinfos[0]['artist_url']
00123         albums = artistinfos[0]['artist_albumcount']
00124 
00125         md5hash = hashlib.md5(img).hexdigest()
00126         fh = os.path.join(self.pyjama.home, "images", md5hash)
00127         if not os.path.exists(fh) and img != "":
00128             try:
00129                 urllib.urlretrieve(img, fh)
00130             except IOError:
00131                 print ("Could not load image")
00132                 return None
00133 
00134         self.toolbar.ibDonate.tag = url
00135         self.toolbar.sbGotoWebArtist.tag = url
00136 
00137         self.image_artist.clear()
00138         if img == "" or img == None:
00139             pixbuf = self.image_artist.render_icon(gtk.STOCK_MISSING_IMAGE, gtk.ICON_SIZE_DIALOG, detail=None)
00140             self.image_artist.set_from_pixbuf(pixbuf)
00141         else:
00142             self.image_artist.set_from_file(fh)
00143         
00144 
00145         content = [functions.decode_htmlentities(name), country, albums,  url]
00146         x = 0
00147         infos = self.ArtistInfos
00148         captions = self.ArtistCaptions
00149         for x in xrange(0, len(infos)):
00150             #self.window.layout['artist'].move(self.labels[infos[x]], xspace, y)
00151             if self.pyjama.nocolor and infos[x] != "url":
00152                 self.ArtistLabels[infos[x]].set_markup("<span><b>%s: %s</b></span>" % (captions[x], content[x]))
00153             elif infos[x] == "url":
00154                     self.ArtistLabels[infos[x]].set_label(url)
00155             else:
00156                 self.ArtistLabels[infos[x]].set_markup("<span foreground=\"white\"><b>%s: %s</b></span>" % (captions[x], content[x]))
00157             #self.labels[infos[x]].set_line_wrap(True)
00158             #y+=yspace
00159             
00160         #### ARTIST'S ALBUMS ####
00161         threads = {}
00162         albums = []
00163         counter = 0
00164         for album in artistinfos:
00165             albums.append(str(artistinfos[counter]['album_id']))
00166             ## todo ##
00167             ## Replace Image-URL with constants!!!! ##
00168 #            artistinfos[counter]['album_image'] =  "http://imgjam.com/albums/%s/covers/1.100.jpg" % artistinfos[counter]['album_id']
00169             artistinfos[counter]['album_image'] = "http://api.jamendo.com/get2/image/album/redirect/?id=%s&imagesize=%i" % (artistinfos[counter]['album_id'], 100)
00170             threads[counter] = clThreadedDownload.Download(self.pyjama, artistinfos[counter]['album_image'], counter)
00171             threads[counter].start()
00172             counter += 1
00173         ## i added another loop
00174         ## so that threaded download
00175         ## won't collide with
00176         ## creating the albuminfos
00177         counter = 0
00178         for album in artistinfos:
00179             self.albuminfos[counter] = clWidgets.AlbumInfo(self.pyjama, artistinfos[counter])
00180             self.albuminfos[counter].show()
00181             self.put(self.albuminfos[counter], 1, 1)
00182             counter += 1
00183 
00184         ### Add Artist's Tracks to List ###
00185 
00186         self.pyjama.window.TVListFrame.get_label_widget().set_markup(_("Tracks of the artist '<b>%s</b>'" % name))
00187 
00188         self.pyjama.window.tvList.clear()
00189         tracks = self.pyjama.db.artisttracks(artistID)
00190         for track in tracks:
00191             results = [name, track.album_name, track.numalbum, track.name, track.license, artistID, track.album_id, track.id]
00192             self.pyjama.window.tvList.add_item(results)
00193 
00194 #        ### this is more than slow since
00195 #        ### every artists' album is requested
00196 #        ### on its own, need multiple cross- joins!!!
00197 #        for album in albums:
00198 #            tracks = self.db.albumtracks(album)
00199 #            for track in tracks:
00200 #                results = [tracks[track]['artist_name'], tracks[track]['album_name'], tracks[track]['numalbum'], tracks[track]['name'], tracks[track]['license'], tracks[track]['artist_id'], tracks[track]['album_id'], tracks[track]['id']]
00201 #                self.window.tvList.add_item(results)
00202 
00203         self.arrange_artistdetail()
00204         self.pyjama.Events.raise_event("showing_artist_page", artistinfos)
00205         self.show()
00206 
00207     def arrange_artistdetail(self):
00208         if self.albuminfos == {}: return None
00209         width = self.pyjama.window.scrolledwindow_width
00210         hspace = 20
00211         vspacer = 20
00212         
00213         imgwidth = 150
00214         imgheight = 160
00215         starty = 200
00216         
00217         y = 0
00218         x = 0
00219         self.hide()
00220         for counter in self.albuminfos:
00221             if ((x+1) * imgwidth) + hspace*(x+1) >= width:
00222                 y += 1
00223                 x = 0
00224             self.move(self.albuminfos[counter], (imgwidth * x) + hspace*(x+1), (vspacer*(y+1) + (y * imgheight))+starty)
00225             x += 1
00226         height = (y+1) * (imgheight + vspacer) +starty
00227 
00228         self.set_size(width-20,height+vspacer)
00229 
00230     def on_zumArtist_clicked(self, ev):
00231         url = self.ArtistLabels['url'].get_text()
00232         self.pyjama.Events.raise_event("open_url",  url )
00233 
00234 
00235     #
00236     # Actually this toolbar is just a hbox...
00237     #
00238     class ToolBar(gtk.HBox):
00239         def __init__(self, pyjama):
00240             gtk.HBox.__init__(self)
00241             self.pyjama = pyjama
00242             self.layout = self.pyjama.layouts.layouts['artist']
00243 
00244             self.sbGotoWebArtist = clWidgets.StockButton(gtk.STOCK_NETWORK, gtk.ICON_SIZE_LARGE_TOOLBAR)
00245             self.sbGotoWebArtist.set_tooltip_text(_("Goto to artist's page on jamendo"))
00246             self.sbGotoWebArtist.set_size_request(50,50)
00247             self.sbGotoWebArtist.show()
00248             self.pack_end(self.sbGotoWebArtist, False, True, 2)
00249             self.sbGotoWebArtist.connect("clicked", self.on_sbGotoWebArtist_clicked)        
00250 
00251             self.ibDonate = clWidgets.ImageButton("%s/images/money.png"  % functions.install_dir(), 26, 26)
00252             self.ibDonate.set_size_request(50,50)
00253             self.ibDonate.set_tooltip_text(_("Support this artist"))
00254             self.ibDonate.show()
00255             self.pack_end(self.ibDonate, False, True, 2)
00256             self.ibDonate.connect("clicked", self.on_ibDonate_clicked)
00257 
00258         def on_sbGotoWebArtist_clicked(self, ev):
00259             self.pyjama.Events.raise_event("open_url",  self.sbGotoWebArtist.tag)
00260 
00261         def on_ibDonate_clicked(self, ev):
00262             url = self.ibDonate.tag + "/donate"
00263             self.pyjama.Events.raise_event("open_url", url)        
00264 

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8