Move "download all" work to a thread. Add property to know when is it running.
authorIvan Frade <ivan.frade@gmail.com>
Mon, 4 Jul 2011 12:23:32 +0000 (15:23 +0300)
committerIvan Frade <ivan.frade@gmail.com>
Mon, 4 Jul 2011 12:23:32 +0000 (15:23 +0300)
src/qml/controller.py

index 53b99ce..b96e284 100644 (file)
@@ -36,6 +36,22 @@ class DownloadThread (QtCore.QThread):
             counter += 1
 
 
+class MassiveDownloadsThread (QtCore.QThread):
+
+    def __init__ (self, albumModel):
+        QtCore.QThread.__init__ (self)
+        self.downloader = MussorgskyAlbumArt ()
+        self.albumModel = albumModel
+
+    def run (self):
+        print "Download one cover per-album in a thread"
+        import time
+        time.sleep (4)
+        for albumItem in self.albumModel.get_albums ():
+            if albumItem.require_download:
+                self.downloader.get_album_art (albumItem)
+            
+
 class MussorgskyController (QtCore.QObject):
 
     def __init__ (self, rootContext):
@@ -43,37 +59,31 @@ class MussorgskyController (QtCore.QObject):
         self.download = None
         self.ctx = rootContext 
         self.tracker = TrackerBackend ()
+        self.__is_downloading = False
 
-#    @QtCore.Slot (QtCore.QObject, QtCore.QObject, int)
-#    def coverSelected (self, coverObject, albumModel, index):
-#        """
-#        The user has clicked in one cover!
-#        """
-#        albumObject = albumModel.getAlbumInRow (index)
-#        
-#        print "Selected cover", albumObject.title
-#        filename = getCoverArtFileName (albumObject.title)
-#        thumbnail = getCoverArtThumbFileName (albumObject.title)
-#
-#        coverObject.save (filename, thumbnail)
-#
-#        albumModel.updateThumb (index, filename)
-#        #albumObject.album_art = thumbnail
-#        albumObject.album_art_changed.emit ()
-
-    #@QtCore.Slot (QtCore.QObject)
-    #def resetAlternatives (self, coversModel):
-    #    print "Reseting alternatives", coversModel
-    #    QtGui.QPixmapCache.clear ()
-    #    coversModel.resetAlternatives ()
+    def _is_downloading (self):
+        return self.__is_downloading
+
+    def _set_is_downloading (self, value):
+        if (value != self.__is_downloading):
+            self.__is_downloading = value
+            self.is_downloading_changed.emit ()
+
+    is_downloading_changed = QtCore.Signal ()
+    is_downloading = QtCore.Property (bool, _is_downloading, notify=is_downloading_changed)
 
     @QtCore.Slot (QtCore.QObject)
     def download_all (self, albumModel):
-        print "well, we will do as soon as possible"
-        downloader = MussorgskyAlbumArt ()
-        for albumItem in albumModel.get_albums ():
-            if albumItem.require_download:
-                downloader.get_album_art (albumItem)
+        self._set_is_downloading (True)
+        self.download_all_thread = MassiveDownloadsThread (albumModel)
+        self.download_all_thread.finished.connect (self.download_all_finished)
+        self.download_all_thread.start ()
+        print "now we are downloading..."
+
+    @QtCore.Slot ()
+    def download_all_finished (self):
+        self._set_is_downloading (False)
+        
 
     @QtCore.Slot (QtCore.QObject)
     def get_options_for (self, albumItem):
@@ -108,3 +118,4 @@ class MussorgskyController (QtCore.QObject):
         for album_title, album_artist in self.tracker.get_all_albums ():
             results.append (AlbumItem (album_title, album_artist))
         return results
+