#!/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 # 类型js的settimeout的修饰器 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): def __init__(self): Gtk.Application.__init__(self, application_id = app_id) self.timer = None self.music_dir = get_music_dir() self.mpd = MPDClient() self.connect('window-removed', self.on_window_removed) @run_async def connect_mpd(self): self.mpd.start() 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() self.connect_mpd() 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)