Update in Thread mit Callback
This commit is contained in:
parent
3935ef57b5
commit
0753d69968
2 changed files with 38 additions and 16 deletions
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
from mpd import MPDClient
|
from mpd import MPDClient
|
||||||
import os
|
import os
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,6 +19,7 @@ class MPDCoverGrid:
|
||||||
self._host = host
|
self._host = host
|
||||||
self._port = port
|
self._port = port
|
||||||
self._password = password
|
self._password = password
|
||||||
|
self.updateCallback = None
|
||||||
|
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
|
@ -44,14 +46,26 @@ class MPDCoverGrid:
|
||||||
return self.albums
|
return self.albums
|
||||||
|
|
||||||
|
|
||||||
|
def connectUpdate(self, updateCallback):
|
||||||
|
self.updateCallback = updateCallback
|
||||||
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
|
Thread(target=self._update, args=()).start()
|
||||||
|
|
||||||
|
|
||||||
|
def _update(self):
|
||||||
for song in self.client.listallinfo():
|
for song in self.client.listallinfo():
|
||||||
try:
|
try:
|
||||||
|
new = False
|
||||||
if song['album'] not in self.albums:
|
if song['album'] not in self.albums:
|
||||||
self.albums[song['album']] = MCGAlbum(song['artist'], song['album'], os.path.dirname(song['file']))
|
self.albums[song['album']] = MCGAlbum(song['artist'], song['album'], os.path.dirname(song['file']))
|
||||||
|
new = True
|
||||||
|
|
||||||
album = self.albums[song['album']]
|
album = self.albums[song['album']]
|
||||||
album.addTrack(song['title'])
|
album.addTrack(song['title'])
|
||||||
|
if new and self.updateCallback is not None:
|
||||||
|
self.updateCallback(album)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
from gi.repository import Gtk, Gdk, GdkPixbuf
|
from gi.repository import Gtk, Gdk, GdkPixbuf, GObject
|
||||||
from MPDCoverGrid import MPDCoverGrid
|
from MPDCoverGrid import MPDCoverGrid
|
||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
|
@ -17,7 +17,9 @@ class MPDCoverGridGTK(Gtk.Window):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
Gtk.Window.__init__(self, title="MPDCoverGridGTK")
|
Gtk.Window.__init__(self, title="MPDCoverGridGTK")
|
||||||
self.set_default_size(400, 400)
|
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
|
# GridModel
|
||||||
self.coverGridModel = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str)
|
self.coverGridModel = Gtk.ListStore(GdkPixbuf.Pixbuf, str, str)
|
||||||
|
@ -41,12 +43,7 @@ class MPDCoverGridGTK(Gtk.Window):
|
||||||
self.add(coverGridScroll)
|
self.add(coverGridScroll)
|
||||||
|
|
||||||
self._initClient()
|
self._initClient()
|
||||||
|
self.mcg.connectUpdate(self.updateCallback)
|
||||||
|
|
||||||
def __del__(self):
|
|
||||||
#if self.mcg is not None:
|
|
||||||
#self.mcg.disconnect()
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def _initClient(self):
|
def _initClient(self):
|
||||||
|
@ -54,17 +51,29 @@ class MPDCoverGridGTK(Gtk.Window):
|
||||||
self.mcg.connect()
|
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):
|
def update(self):
|
||||||
if self.mcg is None:
|
if self.mcg is None:
|
||||||
return
|
return
|
||||||
|
self.mcg.update()
|
||||||
|
|
||||||
for title, album in self.mcg.getAlbums().items():
|
|
||||||
if album.getCover() is not None:
|
def updateCallback(self, album):
|
||||||
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(album.getCover(), self.size, self.size)
|
if album.getCover() is not None:
|
||||||
if pixbuf is not None:
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(album.getCover(), self.size, self.size)
|
||||||
self.coverGridModel.append([pixbuf, album.getTitle(), ' von '.join([album.getTitle(), album.getArtist()])])
|
if pixbuf is not None:
|
||||||
else:
|
self.coverGridModel.append([pixbuf, album.getTitle(), ' von '.join([album.getTitle(), album.getArtist()])])
|
||||||
print("pixbuf none: "+album.getTitle())
|
else:
|
||||||
|
print("pixbuf none: "+album.getTitle())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -72,6 +81,5 @@ class MPDCoverGridGTK(Gtk.Window):
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
mcgg = MPDCoverGridGTK()
|
mcgg = MPDCoverGridGTK()
|
||||||
mcgg.show_all()
|
mcgg.show_all()
|
||||||
mcgg.update()
|
|
||||||
Gtk.main()
|
Gtk.main()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue