Set all columns in the model so the view is updated
[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             if (artist.find ('|') != -1):
63                 real_artist = "Various artists"
64             else:
65                 real_artist = artist
66             self.current_label.set_markup ("<small>Trying: %s - %s</small>" % (escape_html(real_artist),
67                                                                                    escape_html(album)))
68             
69             try:
70                 while (gtk.events_pending()):
71                     gtk.main_iteration()
72
73                 if (self.cancel):
74                     break
75                 
76                 (image, thumb) = self.downloader.get_album_art (real_artist, album)
77                 if thumb:
78                         pixbuf = gtk.gdk.pixbuf_new_from_file_at_size (thumb, 64, 64)
79                         artist_albums_model.set_value (it, 1, pixbuf)
80             except Exception, e:
81                 print "Error processing %s - %s" % (artist, album)
82                 print str(e)
83                 thumb = None
84             
85             self.set_title ("Downloading album art (%d/%d)" % (current, TOTAL))
86             self.previous_label.set_markup ("<b>%s - %s</b>" % (escape_html(real_artist), escape_html(album)))
87               
88             if (thumb):
89                 self.album_art.set_from_file (thumb)
90             else:
91                 self.album_art.set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
92
93             current += 1
94             it = artist_albums_model.iter_next (it)
95
96         
97     def handle_response (self, widget, response_id):
98         if (response_id == gtk.RESPONSE_DELETE_EVENT):
99             print "Cancel the work!"
100         self.cancel = True
101         self.destroy ()
102
103 if __name__ == "__main__":
104
105     import time
106     import random
107     class MockDownloader:
108         def __init__ (self):
109             self.alt = [("../hendrix.jpeg", "../hendrix-thumb.jpeg"),
110                         ("../hoover.jpeg", "../hoover-thumb.jpeg"),
111                         ("../backbeat.jpeg", "../backbeat-thumb.jpeg"),
112                         ("../dylan.jpeg", "../dylan-thumb.jpeg")]
113             self.counter = 0
114         def get_album_art (self, artist, album, force=False):
115             time.sleep (3)
116             return  self.alt [random.randint (0, len (self.alt)-1)]
117
118     PAIRS_store = gtk.ListStore (str, gtk.gdk.Pixbuf, str, str)
119     for i in range (0, 100):
120         PAIRS_store.append (("blablabal", None, "Artist %d" % i, "Album %d" %i))
121
122     def clicked_button (self):
123         aadd = MussorgskyAlbumArtDownloadDialog (w, MockDownloader ())
124         aadd.show_all ()
125         aadd.do_the_job (PAIRS_store)
126         
127     w = gtk.Window ()
128     box = gtk.VBox ()
129
130     button = gtk.Button ("click")
131     button.connect ("clicked", clicked_button)
132     box.add (button)
133
134     w.add (box)
135     w.show_all ()
136
137
138     gtk.main ()