Allow cancel between image downloads
[mussorgsky] / src / aa_selection_dialog.py
index aaa1910..6d19fce 100644 (file)
@@ -1,5 +1,6 @@
 import hildon
 import gtk
+import gobject
 from album_art import MussorgskyAlbumArt
 
 class AlbumArtSelectionDialog (gtk.Dialog):
@@ -16,16 +17,20 @@ class AlbumArtSelectionDialog (gtk.Dialog):
         self.artist = artist
         self.album = album
         self.size = size
+        self.paths = []
         self.__create_view (size)
+        self.cancel = False
+        self.connect ("response", self.handle_response)
 
         if (downloader):
             self.downloader = downloader
         else:
             self.downloader = MussorgskyAlbumArt ()
-        self.paths = self.downloader.get_alternatives (album, artist, 4)
+
+        gobject.idle_add (self.__get_alternatives_async)
         self.selection_img = None
         self.selection_thumb = None
-        self.__populate (self.paths)
+        hildon.hildon_gtk_window_set_progress_indicator (self, 1)
 
 
     def __create_view (self, size):
@@ -48,39 +53,61 @@ class AlbumArtSelectionDialog (gtk.Dialog):
 
         self.vbox.add (hbox)
 
-    def __populate (self, paths):
+    def __get_alternatives_async (self):
+        counter = 0
+        for (path, thumb) in self.downloader.get_alternatives (self.album, self.artist, self.size):
+            if (self.cancel):
+                return False
+            self.paths.insert (counter, (path, thumb))
+            print "Setting", thumb, "as image"
+            self.images[counter].set_from_file (thumb)
+            self.event_boxes [counter].set_sensitive (True)
+            counter += 1
+            while (gtk.events_pending()):
+                gtk.main_iteration()
+
+        while (counter < self.size):
+                self.images[counter].set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
+                counter += 1
+                
+        hildon.hildon_gtk_window_set_progress_indicator (self, 0)
 
-        for i in range (0, self.size):
-            if (len(paths) > i):
-                self.images[i].set_from_file (paths[i])
-                self.event_boxes[i].set_sensitive (True)
-            else:
-                self.images[i].set_from_stock (gtk.STOCK_CDROM, gtk.ICON_SIZE_DIALOG)
 
     def click_on_img (self, widget, event, position):
+        img, thumb = self.paths[position]
         self.selection_img, self.selection_thumb = self.downloader.save_alternative (self.artist,
                                                                                      self.album,
-                                                                                     self.paths[position])
+                                                                                     img, thumb)
         self.response (position)
 
     def get_selection (self):
         return (self.selection_img, self.selection_thumb)
 
     
+    def handle_response (self, widget, response_id):
+        self.cancel = True
+        # Return False to continue propagating the signal
+        return False
 
 if __name__ == "__main__":
 
+    import time
     class MockDownloader:
         def __init__ (self):
-            self.alt = ["../hendrix.jpeg", "../hoover.jpeg", "../dylan.jpeg"]
+            self.alt = [("../hendrix.jpeg", "../hendrix-thumb.jpeg"),
+                        ("../hoover.jpeg", "../hoover-thumb.jpeg"),
+                        ("../backbeat.jpeg", "../backbeat-thumb.jpeg"),
+                        ("../dylan.jpeg", "../dylan-thumb.jpeg")]
         def get_alternatives (self, album, artist, amount):
-            return self.alt [0:amount]
-        def save_alternative (self, artist, album, img):
-            return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + img)
+            for a in self.alt:
+                time.sleep (1)
+                yield a
+        def save_alternative (self, artist, album, img, thumb):
+            return ("/home/user/.cache/media-art/" + img, "/home/user/.thumbnails/normal/" + thumb)
                               
 
     def clicked_button (self):
-        aadd = AlbumArtSelectionDialog (w, "rory gallagher", "irish tour", 4, MockDownloader (ALTERNATIVES))
+        aadd = AlbumArtSelectionDialog (w, "rory gallagher", "irish tour", 4, MockDownloader ())
         aadd.show_all ()
         response = aadd.run ()
         if response == gtk.RESPONSE_CLOSE or response == gtk.RESPONSE_DELETE_EVENT or response == gtk.RESPONSE_REJECT: