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