Commits

Damián Nohales  committed d490bb2

Important fixes on cover download.

- The program now only allows N concurrents cover downloads.
- Now cover download are cancellable if the download not started.
- When a song is deleted from the playlist, the cover download is canceled.
- Add a timeout for the cover download.
- Fixed application hung when user quits application with cover downloads in progress.

  • Participants
  • Parent commits a09c618

Comments (0)

Files changed (3)

File lib/PlayList.py

         # Change song local ID to fix the search by song
         song = song.clone()
         
-        song_string = "<span font_size='small'>%s</span>\n<span font_size='small' fgcolor='#555555'>%s</span>" % (self.get_sliced_string(glib.markup_escape_text(song.get_title()), 25),
-                                                                              self.get_sliced_string(glib.markup_escape_text(song.get_artist()), 20))
-        #if len(song_string) > 14:
-        #    song_string = song_string[:13] + "..."
-        tooltip = _("<b>Title:</b> {title}").format(title = glib.markup_escape_text(song.get_title())) + "\n"
-        tooltip += _("<b>Artist:</b> {artist}").format(artist = glib.markup_escape_text(song.get_artist())) + "\n"
-        tooltip += _("<b>Album:</b> {album}").format(album = glib.markup_escape_text(song.get_album())) + "\n"
-        tooltip += _("<b>Year:</b> {year}").format(year = song.get_year())
+        song_string = "<span font_size='small'>{title}</span>\n<span font_size='small' fgcolor='#555555'>{artist}</span>".format(
+            title = self.get_sliced_string(glib.markup_escape_text(song.get_title()), 25),
+            artist = self.get_sliced_string(glib.markup_escape_text(song.get_artist()), 20)
+        )
+        
+        tooltip = _("<b>Title:</b> {title}\n<b>Artist:</b> {artist}\n<b>Album:</b> {album}\n<b>Year:</b> {year}").format(
+            title = glib.markup_escape_text(song.get_title()),
+            artist = glib.markup_escape_text(song.get_artist()),
+            album = glib.markup_escape_text(song.get_album()),
+            year = song.get_year()
+        )
         
         initial_pixbuf = None
         must_download = None

File lib/SharkDown.py

 from PlayList import PlayList
 from DownloadList import DownloadList
 from Song import Song
+from Song import SongCoverThread
 import guihelpers
 import pickle
 import gtk
             self.save_playlist("%s/.gsharkdown/playlist.pkl" % os.environ.get("HOME"))
             self.save_downqueue("%s/.gsharkdown/downqueue.pkl" % os.environ.get("HOME"))
             self.downloads.stop_all_downloads()
+            SongCoverThread.cancel_downloads.set()
             gtk.main_quit()
 
     def on_copy_song(self, widget, data = None):
         selection = self.playlist.get_selected_rows()
         if len(selection) > 0:
             for i in selection:
+                self.playlist.get_song(i).cancel_cover_download()
                 self.playlist.get_model().remove(self.playlist.get_model().get_iter(i))
 
             if len(self.playlist) > 0:
         Removes all items from the playlist
         """
         self.stop_play()
-        self.playlist.clear()
+        self.playlist.get_view().select_all()
+        self.on_remove_from_playlist(self.button_playlist_delete)
 
     def on_toggle_repeat(self, widget, data = None):
         """
         self.filename = None
         self.state = Song.STATE_NOT_STARTED
         self.download_thread = None
+        self.cover_thread = None
         self.last_error = None
         
         try:
         Download the album cover, the object will emit the 'cover-downloaded'
         signal when the download finish.
         """
-        thread = SongCoverThread(self)
-        thread.start()
+        self.cover_thread = SongCoverThread(self)
+        self.cover_thread.start()
+        
+    def cancel_cover_download(self):
+        if self.cover_thread != None:
+            self.cover_thread.canceled.set()
         
     def start_download(self, restart = False, speed = None):
         """
     it when download ends. Should not be used directly, use Song instead.
     """
     
+    concurrent_downloads = threading.Semaphore(5)
+    cancel_downloads = threading.Event()
+    
     def __init__(self, song):
         threading.Thread.__init__(self)
         self.song = song
+        self.canceled = threading.Event()
 
     def run(self):
         cover_url = self.song.get_cover_filename()
-
+        
+        SongCoverThread.concurrent_downloads.acquire()
+        
+        if SongCoverThread.cancel_downloads.is_set() or self.canceled.is_set():
+            SongCoverThread.concurrent_downloads.release()
+            return
+            
         try:
             url = "http://beta.grooveshark.com/static/amazonart/s%s"
-            response = urllib2.urlopen(url % cover_url)
+            response = urllib2.urlopen(url % cover_url, None, 10)
             loader = gtk.gdk.PixbufLoader()
             loader.write(response.read())
             loader.close()
         except Exception, e:
             print "Error while downloading cover: ", e
             self.song.set_cover_missed_pixbuf()
+        
+        SongCoverThread.concurrent_downloads.release()
 
-        self.song.emit("cover-downloaded")
+        gobject.idle_add( self.song.emit, "cover-downloaded" )
         
 
 class DownloadThread(threading.Thread):