#!/usr/bin/env python3 import gi, sys, os, threading, time, re, json # 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 from utils import run_async from window import SonistWindow from about_app import AboutWindow from preferences import PreferencesWindow from mpd import MPDClient app_id = 'fun.wkit.sonist' home_dir = os.getenv('HOME') 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.lib_dir = f"{os.getenv('SONIST_LIB_DIR')}/" if os.getenv('SONIST_LIB_DIR') else '' print(self.lib_dir) self.music_dir = get_music_dir() self.album_cache_dir = f"{home_dir}/.cache/sonist/album" self.lyric_cache_dir = f"{home_dir}/.cache/sonist/lyric" self.config_dir = f"{home_dir}/.config/sonist" self.config_file = os.path.join(self.config_dir, 'config.json') os.makedirs(self.album_cache_dir, exist_ok = True) os.makedirs(self.lyric_cache_dir, exist_ok = True) os.makedirs(self.config_dir, exist_ok = True) self.config_data = { "host": '127.0.0.1', "port": 6600, "music_directory": self.music_dir, "auto_scan": True } if os.path.isfile(self.config_file): with open(self.config_file, 'r') as f: buff = f.read() self.config_data = json.loads(buff) else: with open(self.config_file, 'w') as f: buff = json.dumps(self.config_data) f.write(buff) self.mpd = MPDClient(self.config_data['host'], self.config_data['port']) 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.preferences = PreferencesWindow(self, self.mpd) self.add_window(self.window) self.window.show_all() self.connect_mpd() def on_window_removed(self, app, win): if len(self.get_windows()) == 0: self.mpd.destroy() print('朕要休息了~~~') def quit_all(self): self.remove_window(self.window) """ 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)