sonist-gtk/main.py

127 lines
2.6 KiB
Python
Raw Normal View History

2023-08-17 00:06:21 +08:00
#!/usr/bin/env python3
2023-08-22 20:43:40 +08:00
import gi, sys, os, threading, time, re
2023-08-17 00:06:21 +08:00
# import dbus
# import dbus.service, dbus.mainloop.glib
from pprint import pprint as print
2023-08-21 19:06:22 +08:00
2023-08-17 00:06:21 +08:00
gi.require_version('Gtk', '3.0')
2023-08-21 19:06:22 +08:00
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf, GObject
2023-08-17 00:06:21 +08:00
from window import SonistWindow
2023-08-22 18:52:18 +08:00
from about_app import AboutWindow
2023-08-17 20:51:59 +08:00
2023-08-23 20:36:52 +08:00
from mpd import MPDClient
2023-08-17 00:06:21 +08:00
app_id = 'fun.wkit.sonist'
2023-08-22 20:43:40 +08:00
home_dir = os.getenv('HOME')
2023-08-17 00:06:21 +08:00
2023-08-18 19:04:51 +08:00
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
2023-08-18 00:09:43 +08:00
2023-08-23 20:36:52 +08:00
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
2023-08-22 20:43:40 +08:00
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'
2023-08-17 00:06:21 +08:00
class Application(Gtk.Application):
2023-08-21 19:06:22 +08:00
__gsignals__ = {
'playing': (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
'song_changed': (GObject.SignalFlags.RUN_FIRST, None, (bool,)),
'state_changed': (GObject.SignalFlags.RUN_FIRST, None, (str,))
}
2023-08-17 00:06:21 +08:00
def __init__(self):
Gtk.Application.__init__(self, application_id = app_id)
2023-08-23 20:36:52 +08:00
self.timer = None
2023-08-21 19:06:22 +08:00
2023-08-18 19:04:51 +08:00
self.mpd = MPDClient()
2023-08-22 20:43:40 +08:00
self.music_dir = get_music_dir()
2023-08-18 19:04:51 +08:00
self.connect('window-removed', self.on_window_removed)
2023-08-17 00:06:21 +08:00
2023-08-23 20:36:52 +08:00
self.mpd.connect()
2023-08-17 00:06:21 +08:00
def do_activate(self):
print('hello mpc')
2023-08-17 20:51:59 +08:00
self.set_app_menu(None)
self.set_menubar(None)
2023-08-18 19:04:51 +08:00
self.window = SonistWindow(self)
2023-08-22 18:52:18 +08:00
self.about = AboutWindow()
2023-08-18 19:04:51 +08:00
self.add_window(self.window)
self.window.show_all()
2023-08-22 20:43:40 +08:00
# self.about.show_all()
2023-08-18 19:04:51 +08:00
def on_window_removed(self, app, win):
if len(self.get_windows()) == 0:
2023-08-23 20:36:52 +08:00
if self.timer is not None:
self.timer.cancel()
self.mpd.destroy()
2023-08-18 19:04:51 +08:00
print('朕要休息了~~~')
2023-08-17 00:06:21 +08:00
""" 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)
2023-08-18 19:04:51 +08:00
app.run(sys.argv)
2023-08-17 00:06:21 +08:00