summaryrefslogtreecommitdiff
path: root/nephilim/plugins/AlbumCover.py
diff options
context:
space:
mode:
authorAnton Khirnov <wyskas@gmail.com>2009-04-30 20:27:39 +0200
committerAnton Khirnov <wyskas@gmail.com>2009-04-30 20:27:39 +0200
commit8791853b26f201f0672030831ed6218260698c4b (patch)
treec8008623fccc92150624207fc39bfd5861a57cf5 /nephilim/plugins/AlbumCover.py
parentb0ea0f22913d572acc0cc6db9933dac9b85c3b32 (diff)
Use a separate thread for all fetches of covers.
i don't really like how it's done though, there should be a better way.
Diffstat (limited to 'nephilim/plugins/AlbumCover.py')
-rw-r--r--nephilim/plugins/AlbumCover.py71
1 files changed, 38 insertions, 33 deletions
diff --git a/nephilim/plugins/AlbumCover.py b/nephilim/plugins/AlbumCover.py
index cbee07b..05d2c90 100644
--- a/nephilim/plugins/AlbumCover.py
+++ b/nephilim/plugins/AlbumCover.py
@@ -42,33 +42,32 @@ class wgAlbumCover(QtGui.QLabel):
_cover_filepath = None # for current song should be stored.
_menu = None # popup menu
- _cover_lock = None # mutex for the cover
-
def __init__(self, plugin):
QtGui.QLabel.__init__(self)
self.plugin = plugin
self.setAlignment(QtCore.Qt.AlignCenter)
- self._cover_lock = QtCore.QMutex()
-
# popup menu
self._menu = QtGui.QMenu("album")
refresh = self._menu.addAction('&Refresh cover.')
+ refresh.setObjectName('refresh')
select_file_action = self._menu.addAction('&Select cover file...')
+ select_file_action.setObjectName('select_file_action')
fetch_amazon_action = self._menu.addAction('Fetch from &Amazon.')
+ fetch_amazon_action.setObjectName('fetch_amazon_action')
view_action = self._menu.addAction('&View in a separate window.')
save_action = self._menu.addAction('Save cover &as...')
- self.connect(refresh, QtCore.SIGNAL('triggered()'), self.refresh)
- self.connect(select_file_action, QtCore.SIGNAL('triggered()'), self._fetch_local_manual)
- self.connect(fetch_amazon_action, QtCore.SIGNAL('triggered()'), self._fetch_amazon_manual)
+ self.connect(refresh, QtCore.SIGNAL('triggered()'), self._fetch_cover)
+ self.connect(select_file_action, QtCore.SIGNAL('triggered()'), self._fetch_cover)
+ self.connect(fetch_amazon_action, QtCore.SIGNAL('triggered()'), self._fetch_cover)
self.connect(view_action, QtCore.SIGNAL('triggered()'), self._view_cover)
self.connect(save_action, QtCore.SIGNAL('triggered()'), self._save_cover)
# MPD events
- self.connect(self.plugin.mpclient(), QtCore.SIGNAL('song_changed'), self.refresh)
- self.connect(self.plugin.mpclient(), QtCore.SIGNAL('disconnected'), self.refresh)
- self.connect(self.plugin.mpclient(), QtCore.SIGNAL('state_changed'),self.refresh)
+ self.connect(self.plugin.mpclient(), QtCore.SIGNAL('song_changed'), self._fetch_cover)
+ self.connect(self.plugin.mpclient(), QtCore.SIGNAL('disconnected'), self._fetch_cover)
+ self.connect(self.plugin.mpclient(), QtCore.SIGNAL('state_changed'),self._fetch_cover)
self.connect(self, QtCore.SIGNAL('new_cover_fetched'), self.set_cover)
@@ -80,13 +79,11 @@ class wgAlbumCover(QtGui.QLabel):
"""Set cover for current song, attempt to write it to a file
if write is True and it's globally allowed."""
logging.info('Setting cover')
- self._cover_lock.lock()
if not cover or cover.isNull():
self.cover = None
self.cover_loaded = False
self.setPixmap(QtGui.QPixmap('gfx/no-cd-cover.png'))
self.plugin.emit(QtCore.SIGNAL('cover_changed'), None)
- self._cover_lock.unlock()
return
self.cover = QtGui.QPixmap.fromImage(cover)
@@ -101,7 +98,6 @@ class wgAlbumCover(QtGui.QLabel):
logging.info('Cover saved.')
else:
logging.error('Error saving cover.')
- self._cover_lock.unlock()
class FetchThread(QtCore.QThread):
def __init__(self, parent, fetch_func, song):
@@ -111,15 +107,32 @@ class wgAlbumCover(QtGui.QLabel):
self.song = song
def run(self):
- self.fetch_func(self.song)
+ cover, write = self.fetch_func(self.song)
+ self.parent().emit(QtCore.SIGNAL('new_cover_fetched'), cover, write)
- def refresh(self):
- """Autofetch cover for currently playing song."""
- logging.info("refreshing cover")
+ def _fetch_cover(self):
song = self.plugin.mpclient().current_song()
if not song:
- return self.set_cover(None)
+ return self.emit(QtCore.SIGNAL('new_cover_fetched'), None)
+
+ if self.sender() == self.plugin.mpclient():
+ fetch_func = self._fetch_auto
+ elif self.sender().objectName() == 'refresh':
+ fetch_func = self._fetch_auto
+ elif self.sender().objectName() == 'select_file_action':
+ fetch_func = self._fetch_local_manual
+ elif self.sender().objectName() == 'fetch_amazon_action':
+ fetch_func = self._fetch_amazon_manual
+ else:
+ return logging.error('Unknown sender.')
+ thread = self.FetchThread(self, fetch_func, song)
+ thread.start()
+ def _fetch_auto(self, song):
+ """Autofetch cover for currently playing song."""
+ logging.info("autorefreshing cover")
+
+ # generate filenames
if QtCore.QDir.isAbsolutePath(song.filepath()):
self._cover_dirname = os.path.dirname(song.filepath())
self._cover_filepath = ''
@@ -132,10 +145,7 @@ class wgAlbumCover(QtGui.QLabel):
filebase = self.plugin.settings().value(self.plugin.name() + '/covername').toString()
self._cover_filepath = '%s/%s'%(self._cover_dirname,
expand_tags(filebase, (self.plugin.parent(), song)).replace('/', '_'))
- fetch_thread = self.FetchThread(self, self._fetch_cover, song)
- fetch_thread.start()
- def _fetch_cover(self, song):
write = False
if not QtCore.QFile.exists(self._cover_filepath):
for i in (0, 1):
@@ -153,20 +163,16 @@ class wgAlbumCover(QtGui.QLabel):
else:
cover = QtGui.QImage(self._cover_filepath)
- self.emit(QtCore.SIGNAL('new_cover_fetched'), cover, write)
+ return cover, write
def _fetch_local_manual(self):
- song = self.plugin.mpclient().current_song()
- if not song:
- return
-
file = QtGui.QFileDialog.getOpenFileName(self,
'Select album cover for %s - %s'%(song.artist(), song.album()),
self._cover_dirname, '')
cover = QtGui.QImage(file)
if cover.isNull():
- return
- self.emit(QtCore.SIGNAL('new_cover_fetched'), cover, True)
+ return None, False
+ return cover, True
def _fetch_local(self, song):
logging.info('Trying to guess local cover name.')
@@ -203,12 +209,11 @@ class wgAlbumCover(QtGui.QLabel):
logging.info('No matching cover found')
return None
- def _fetch_amazon_manual(self):
- song = self.plugin.mpclient().current_song()
- if not song:
- return
+ def _fetch_amazon_manual(self, song):
cover = self._fetch_amazon(song)
- self.emit(QtCore.SIGNAL('new_cover_fetched'), cover, True)
+ if not cover:
+ return None, False
+ return cover, True
def _fetch_amazon(self, song):
if not song.artist() or not song.album():