2023-08-17 00:06:21 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2023-08-25 18:33:01 +08:00
|
|
|
import gi, sys, os, re, json
|
2023-08-21 19:06:22 +08:00
|
|
|
|
2023-08-17 00:06:21 +08:00
|
|
|
gi.require_version('Gtk', '3.0')
|
2023-08-24 17:04:41 +08:00
|
|
|
from gi.repository import Gtk
|
|
|
|
|
|
|
|
from utils import run_async
|
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-25 15:45:56 +08:00
|
|
|
from preferences import PreferencesWindow
|
2023-08-23 20:36:52 +08:00
|
|
|
from mpd import MPDClient
|
2023-08-17 00:06:21 +08:00
|
|
|
|
2023-08-25 18:33:01 +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-24 14:33:20 +08:00
|
|
|
|
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
|
|
|
|
2023-08-17 00:06:21 +08:00
|
|
|
def __init__(self):
|
2023-08-25 18:33:01 +08:00
|
|
|
Gtk.Application.__init__(self, application_id = APP_ID)
|
2023-08-17 00:06:21 +08:00
|
|
|
|
2023-08-22 20:43:40 +08:00
|
|
|
self.music_dir = get_music_dir()
|
2023-08-24 17:04:41 +08:00
|
|
|
self.album_cache_dir = f"{home_dir}/.cache/sonist/album"
|
|
|
|
self.lyric_cache_dir = f"{home_dir}/.cache/sonist/lyric"
|
2023-08-25 15:45:56 +08:00
|
|
|
self.config_dir = f"{home_dir}/.config/sonist"
|
|
|
|
|
|
|
|
self.config_file = os.path.join(self.config_dir, 'config.json')
|
2023-08-22 20:43:40 +08:00
|
|
|
|
2023-08-24 17:04:41 +08:00
|
|
|
os.makedirs(self.album_cache_dir, exist_ok = True)
|
|
|
|
os.makedirs(self.lyric_cache_dir, exist_ok = True)
|
2023-08-25 15:45:56 +08:00
|
|
|
os.makedirs(self.config_dir, exist_ok = True)
|
|
|
|
|
|
|
|
|
|
|
|
self.config_data = {
|
|
|
|
"host": '127.0.0.1',
|
|
|
|
"port": 6600,
|
2023-08-26 22:28:23 +08:00
|
|
|
"music_directory": self.music_dir
|
2023-08-25 15:45:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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'])
|
2023-08-24 17:04:41 +08:00
|
|
|
|
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-24 14:33:20 +08:00
|
|
|
|
|
|
|
@run_async
|
|
|
|
def connect_mpd(self):
|
|
|
|
self.mpd.start()
|
|
|
|
|
2023-08-26 22:28:23 +08:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2023-08-24 14:33:20 +08:00
|
|
|
|
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-25 15:45:56 +08:00
|
|
|
self.preferences = PreferencesWindow(self, self.mpd)
|
2023-08-18 19:04:51 +08:00
|
|
|
self.add_window(self.window)
|
|
|
|
self.window.show_all()
|
2023-08-22 16:19:20 +08:00
|
|
|
|
2023-08-24 14:33:20 +08:00
|
|
|
self.connect_mpd()
|
|
|
|
|
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
|
|
|
self.mpd.destroy()
|
2023-08-18 19:04:51 +08:00
|
|
|
print('朕要休息了~~~')
|
|
|
|
|
2023-08-17 00:06:21 +08:00
|
|
|
|
2023-08-24 17:04:41 +08:00
|
|
|
def quit_all(self):
|
|
|
|
self.remove_window(self.window)
|
2023-08-17 00:06:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app = Application()
|
2023-08-18 19:04:51 +08:00
|
|
|
app.run(sys.argv)
|
|
|
|
|
2023-08-17 00:06:21 +08:00
|
|
|
|