eb03119699ef5e417e59e9fdd9ee45238227382b
[mussorgsky] / src / download_dialog.py
1 #!/usr/bin/env python2.5
2 import gtk, gobject
3 from album_art import MussorgskyAlbumArt
4 from utils import escape_html
5
6 class MussorgskyAlbumArtDownloadDialog (gtk.Dialog):
7
8     def __init__ (self, parent):
9         gtk.Dialog.__init__ (self,
10                              "Downloading album art", parent,
11                              gtk.DIALOG_DESTROY_WITH_PARENT,
12                              (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT)
13                         )
14         self.downloader = MussorgskyAlbumArt ()
15         self.set_title ("Downloading album art")
16         self.connect ("response", self.handle_response)
17         self.__create_view ()
18         self.cancel = False
19
20     def __create_view (self):
21
22         hbox = gtk.HBox (homogeneous=False)
23
24         self.album_art = gtk.Image ()
25         self.album_art.set_size_request (124, 124)
26         
27         hbox.pack_start (self.album_art, expand=False, fill=True)
28
29         labels = gtk.VBox ()
30         self.status_label = gtk.Label ("")
31         labels.pack_start (self.status_label)
32         self.current_label = gtk.Label ("")
33         labels.pack_start (self.current_label)
34
35         hbox.pack_start (labels, expand=True, fill=False)
36         
37         self.vbox.add (hbox)
38
39
40     def do_the_job (self, artist_albums_model):
41         """
42         each row: ("Visible text", pixbuf, Artist, Album)
43         """
44         TOTAL = len (artist_albums_model)
45         current = 1
46
47         it = artist_albums_model.get_iter_first ()
48         while (it):
49
50             while (gtk.events_pending()):
51                 gtk.main_iteration()
52
53             if (self.cancel):
54                 break
55
56             artist = artist_albums_model.get_value (it, 2)
57             album = artist_albums_model.get_value (it, 3)
58             
59             try:
60                 (image, thumb) = self.downloader.get_album_art (artist, album)
61                 if thumb:
62                         pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (thumb, 124, 124)
63                         artist_albums_model.set_value (it, 1, pixbuf)
64             except Exception, e:
65                 print "Error processing %s - %s" % (artist, album)
66                 print str(e)
67                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
68                 current += 1
69                 it = artist_albums_model.iter_next (it)
70                 continue
71
72             #self.status_label.set_text ("Retrieved (%d/%d)" % (current, TOTAL))
73             self.set_title ("Downloading album art (%d/%d)" % (current, TOTAL))
74             self.current_label.set_markup ("<b>%s - %s</b>" % (escape_html(artist), escape_html(album)))
75               
76             if (thumb):
77                 self.album_art.set_from_file (thumb)
78             else:
79                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
80
81             current += 1
82             it = artist_albums_model.iter_next (it)
83
84         
85     def handle_response (self, widget, response_id):
86         if (response_id == gtk.RESPONSE_DELETE_EVENT):
87             print "Cancel the work!"
88         self.cancel = True
89         self.destroy ()
90
91 if __name__ == "__main__":
92
93     PAIRS_store = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str)
94     for i in range (0, 100):
95         PAIRS_store.append (("blablabal", None, "Artist %d" % i, "Album %d" %i))
96
97     def clicked_button (self):
98         aadd = MussorgskyAlbumArtDownloadDialog (w)
99         aadd.show_all ()
100         aadd.do_the_job (PAIRS_store)
101         
102     w = gtk.Window ()
103     box = gtk.VBox ()
104
105     button = gtk.Button ("click")
106     button.connect ("clicked", clicked_button)
107     box.add (button)
108
109     w.add (box)
110     w.show_all ()
111
112
113     gtk.main ()