diff --git a/MPDCoverGrid.py b/MPDCoverGrid.py index 8172a8c..13e8360 100644 --- a/MPDCoverGrid.py +++ b/MPDCoverGrid.py @@ -5,6 +5,7 @@ from mpd import MPDClient import os +from threading import Thread @@ -18,6 +19,7 @@ class MPDCoverGrid: self._host = host self._port = port self._password = password + self.updateCallback = None def connect(self): @@ -44,14 +46,26 @@ class MPDCoverGrid: return self.albums + def connectUpdate(self, updateCallback): + self.updateCallback = updateCallback + + def update(self): + Thread(target=self._update, args=()).start() + + + def _update(self): for song in self.client.listallinfo(): try: + new = False if song['album'] not in self.albums: self.albums[song['album']] = MCGAlbum(song['artist'], song['album'], os.path.dirname(song['file'])) + new = True album = self.albums[song['album']] album.addTrack(song['title']) + if new and self.updateCallback is not None: + self.updateCallback(album) except KeyError: pass diff --git a/MPDCoverGridGTK.py b/MPDCoverGridGTK.py index c0a46dd..41dd622 100755 --- a/MPDCoverGridGTK.py +++ b/MPDCoverGridGTK.py @@ -3,7 +3,7 @@ -from gi.repository import Gtk, Gdk, GdkPixbuf +from gi.repository import Gtk, Gdk, GdkPixbuf, GObject from MPDCoverGrid import MPDCoverGrid import inspect @@ -17,7 +17,9 @@ class MPDCoverGridGTK(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="MPDCoverGridGTK") self.set_default_size(400, 400) - self.connect("delete-event", Gtk.main_quit) + self.connect("focus", self.updateSignal) + self.connect("delete-event", self._destroy) + GObject.threads_init() # GridModel self.coverGridModel = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str) @@ -41,12 +43,7 @@ class MPDCoverGridGTK(Gtk.Window): self.add(coverGridScroll) self._initClient() - - - def __del__(self): - #if self.mcg is not None: - #self.mcg.disconnect() - pass + self.mcg.connectUpdate(self.updateCallback) def _initClient(self): @@ -54,17 +51,29 @@ class MPDCoverGridGTK(Gtk.Window): self.mcg.connect() + def _destroy(self, widget, state): + if self.mcg is not None: + self.mcg.disconnect() + Gtk.main_quit() + + + def updateSignal(self, widget, state): + self.update() + + def update(self): if self.mcg is None: return + self.mcg.update() - for title, album in self.mcg.getAlbums().items(): - if album.getCover() is not None: - pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(album.getCover(), self.size, self.size) - if pixbuf is not None: - self.coverGridModel.append([pixbuf, album.getTitle(), ' von '.join([album.getTitle(), album.getArtist()])]) - else: - print("pixbuf none: "+album.getTitle()) + + def updateCallback(self, album): + if album.getCover() is not None: + pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(album.getCover(), self.size, self.size) + if pixbuf is not None: + self.coverGridModel.append([pixbuf, album.getTitle(), ' von '.join([album.getTitle(), album.getArtist()])]) + else: + print("pixbuf none: "+album.getTitle()) @@ -72,6 +81,5 @@ class MPDCoverGridGTK(Gtk.Window): if __name__ == "__main__": mcgg = MPDCoverGridGTK() mcgg.show_all() - mcgg.update() Gtk.main()