sonist-gtk/main.py

127 lines
2.6 KiB
Python
Executable File

#!/usr/bin/env python3
import gi, sys, os, threading, time, re
# import dbus
# import dbus.service, dbus.mainloop.glib
from pprint import pprint as print
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, GObject
from window import SonistWindow
from about_app import AboutWindow
from mpd import MPDClient
app_id = 'fun.wkit.sonist'
home_dir = os.getenv('HOME')
def run_async(func):
def wrapper(*args, **kwargs):
thread = threading.Thread(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread
return wrapper
def set_timeout(timeout = 0.5):
def decorator(callback):
def wrapper(*args):
t = threading.Timer(timeout, callback, args=args)
t.start()
return t
return wrapper
return decorator
def get_music_dir():
with open(f'{home_dir}/.mpd/mpd.conf', 'r') as f:
data = f.read()
matches = re.search('music_directory\s*"(.*)"', data).groups()
if len(matches) > 0:
return matches[0]
return '/data/music'
class Application(Gtk.Application):
__gsignals__ = {
'playing': (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
'song_changed': (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
'state_changed': (GObject.SignalFlags.RUN_FIRST, None, (str,))
}
def __init__(self):
Gtk.Application.__init__(self, application_id = app_id)
self.timer = None
self.mpd = MPDClient()
self.music_dir = get_music_dir()
self.connect('window-removed', self.on_window_removed)
self.mpd.connect()
def do_activate(self):
print('hello mpc')
self.set_app_menu(None)
self.set_menubar(None)
self.window = SonistWindow(self)
self.about = AboutWindow()
self.add_window(self.window)
self.window.show_all()
# self.about.show_all()
def on_window_removed(self, app, win):
if len(self.get_windows()) == 0:
if self.timer is not None:
self.timer.cancel()
self.mpd.destroy()
print('朕要休息了~~~')
""" class ApplicationService(dbus.service.Object):
def __init__(self, app):
self.app = app
bus_name = dbus.service.BusName(app_id, bus = dbus.SessionBus())
dbus.service.Object.__init__(self, bus_name, '/')
@dbus.service.method(app_id)
def call_app(self):
self.app.present()
"""
if __name__ == "__main__":
# dbus.mainloop.glib.DBusGMainLoop(set_as_default = True)
# bus = dbus.SessionBus()
# try:
# obj = bus.get_object(app_id, '/')
# obj.call_app()
# sys.exit(0)
# except dbus.DBusException:
# pass
app = Application()
# ApplicationService(app)
app.run(sys.argv)
python + gtk3开发的基于mpd后端的音乐播放器
Python 99.1%
Shell 0.9%