2015-12-07 16:17:57 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import os
|
|
|
|
from os.path import dirname, abspath
|
2016-09-29 09:26:58 +00:00
|
|
|
import json
|
2015-12-07 16:17:57 +00:00
|
|
|
import subprocess
|
2016-04-14 10:57:45 +00:00
|
|
|
import sys
|
2015-12-07 16:17:57 +00:00
|
|
|
import webbrowser
|
|
|
|
|
|
|
|
import gi
|
|
|
|
gi.require_version('Gtk', '3.0')
|
|
|
|
from gi.repository import Gtk, GLib
|
|
|
|
|
|
|
|
base = dirname(dirname(dirname(abspath(__file__))))
|
2019-01-25 13:15:59 +00:00
|
|
|
icon = os.path.join(base, 'openmedialibrary/static/svg/oml.svg')
|
2015-12-07 16:17:57 +00:00
|
|
|
title = "Open Media Library"
|
|
|
|
ctl = base + '/ctl'
|
|
|
|
|
2016-01-09 10:31:51 +00:00
|
|
|
|
2019-01-25 11:06:54 +00:00
|
|
|
def check_pid(pid):
|
|
|
|
if not os.path.exists(pid):
|
|
|
|
return False
|
|
|
|
try:
|
|
|
|
with open(pid, 'r') as fd:
|
|
|
|
pid = int(fd.read().strip())
|
|
|
|
os.kill(pid, 0)
|
|
|
|
except OSError:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
|
|
|
|
2019-01-25 13:15:59 +00:00
|
|
|
def preexec(): # Don't forward signals.
|
|
|
|
os.setpgrp()
|
|
|
|
|
|
|
|
|
|
|
|
class OMLStatus:
|
2015-12-07 16:17:57 +00:00
|
|
|
menu = None
|
2016-01-09 10:31:51 +00:00
|
|
|
icon = None
|
2015-12-07 16:17:57 +00:00
|
|
|
|
2016-04-14 10:57:45 +00:00
|
|
|
def __init__(self, autostart=False):
|
|
|
|
self.autostart = autostart
|
2016-09-29 09:26:58 +00:00
|
|
|
self.create_pid()
|
2019-01-25 13:15:59 +00:00
|
|
|
self.win = Gtk.Window()
|
|
|
|
self.win.set_icon_from_file(icon)
|
|
|
|
self.win.set_title(title)
|
|
|
|
#self.win.show_all()
|
|
|
|
self.win.iconify()
|
|
|
|
'''
|
|
|
|
self.icon = Gtk.StatusIcon()
|
|
|
|
self.icon.set_from_file(icon)
|
|
|
|
self.icon.set_title(title)
|
|
|
|
self.icon.connect("activate", self._click)
|
|
|
|
self.icon.connect("popup-menu", self._click)
|
|
|
|
'''
|
2016-09-29 09:26:58 +00:00
|
|
|
subprocess.Popen([ctl, 'start'], close_fds=True, preexec_fn=preexec)
|
2016-04-14 10:57:45 +00:00
|
|
|
if not self.autostart:
|
|
|
|
GLib.timeout_add_seconds(1, self._open, None)
|
2016-01-18 12:17:36 +00:00
|
|
|
GLib.timeout_add_seconds(60, self._check, None)
|
2016-01-09 10:11:42 +00:00
|
|
|
|
2016-01-18 12:17:36 +00:00
|
|
|
def _check(self, *args, **kwargs):
|
|
|
|
pid = os.path.join(base, 'data', 'openmedialibrary.pid')
|
|
|
|
if os.path.exists(pid):
|
|
|
|
GLib.timeout_add_seconds(60, self._check, None)
|
|
|
|
else:
|
|
|
|
self._quit(None)
|
2015-12-07 16:17:57 +00:00
|
|
|
|
2016-09-29 09:26:58 +00:00
|
|
|
def create_pid(self):
|
|
|
|
pid = self.get_pid()
|
|
|
|
if pid:
|
|
|
|
with open(pid, 'w') as fd:
|
|
|
|
fd.write('%s' % os.getpid())
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def remove_pid(cls):
|
|
|
|
pid = cls.get_pid()
|
|
|
|
if pid and os.path.exists(pid):
|
|
|
|
os.unlink(pid)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_pid(cls):
|
|
|
|
run = '/run/user/%s' % os.getuid()
|
|
|
|
if os.path.exists(run):
|
|
|
|
oml = os.path.join(run, 'openmedialibrary')
|
|
|
|
if not os.path.exists(oml):
|
|
|
|
os.makedirs(oml)
|
|
|
|
pid = os.path.join(oml, 'pid')
|
|
|
|
else:
|
|
|
|
pid = None
|
|
|
|
return pid
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def is_running(cls):
|
|
|
|
pid = cls.get_pid()
|
2019-01-25 11:06:54 +00:00
|
|
|
if pid and check_pid(pid):
|
2016-09-29 09:26:58 +00:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-01-09 10:31:51 +00:00
|
|
|
def get_menu(self):
|
|
|
|
menu = Gtk.Menu()
|
2016-01-12 05:03:15 +00:00
|
|
|
t = Gtk.MenuItem(label=title)
|
|
|
|
t.set_sensitive(False)
|
|
|
|
menu.append(t)
|
2016-01-09 14:24:11 +00:00
|
|
|
menu.append(Gtk.SeparatorMenuItem())
|
|
|
|
about = Gtk.MenuItem(label="Launch")
|
2016-01-09 10:31:51 +00:00
|
|
|
about.connect("activate", self._open)
|
|
|
|
menu.append(about)
|
|
|
|
quit = Gtk.MenuItem(label="Quit")
|
|
|
|
quit.connect("activate", self._quit)
|
|
|
|
menu.append(quit)
|
|
|
|
menu.show_all()
|
|
|
|
return menu
|
|
|
|
|
2016-01-05 17:43:52 +00:00
|
|
|
def _click(self, icon, button=None, time=None):
|
2015-12-07 16:17:57 +00:00
|
|
|
if self.menu:
|
|
|
|
self.menu.destroy()
|
|
|
|
self.menu = None
|
|
|
|
else:
|
2016-01-09 10:31:51 +00:00
|
|
|
self.menu = self.get_menu()
|
2015-12-07 16:17:57 +00:00
|
|
|
button = 1
|
|
|
|
time = Gtk.get_current_event_time()
|
2016-01-09 10:31:51 +00:00
|
|
|
self.menu.popup(None, None, self.icon.position_menu, icon, button, time)
|
2015-12-07 16:17:57 +00:00
|
|
|
|
|
|
|
def _quit(self, q):
|
|
|
|
Gtk.main_quit()
|
2016-09-29 09:26:58 +00:00
|
|
|
self.stop()
|
|
|
|
|
|
|
|
def stop(self, *args, **kwargs):
|
|
|
|
p = subprocess.Popen([ctl, 'stop'], close_fds=True)
|
2015-12-07 16:17:57 +00:00
|
|
|
self.menu = None
|
2016-09-29 09:26:58 +00:00
|
|
|
self.remove_pid()
|
2015-12-07 16:17:57 +00:00
|
|
|
|
|
|
|
def _open(self, *args):
|
2016-09-29 09:26:58 +00:00
|
|
|
self.load()
|
|
|
|
self.menu = None
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load(cls):
|
|
|
|
config = os.path.join(base, 'data', 'server.json')
|
|
|
|
default_port = port = 9842
|
|
|
|
if os.path.exists(config):
|
|
|
|
try:
|
|
|
|
with open(config) as fd:
|
|
|
|
config = json.load(fd)
|
|
|
|
port = config.get('port', port)
|
|
|
|
except:
|
|
|
|
pass
|
2015-12-07 16:17:57 +00:00
|
|
|
url = 'file://' + base + '/openmedialibrary/static/html/load.html'
|
2016-09-29 09:26:58 +00:00
|
|
|
if port != default_port:
|
|
|
|
url += '#%s' % port
|
2015-12-07 16:17:57 +00:00
|
|
|
webbrowser.open_new_tab(url)
|
2016-09-29 09:26:58 +00:00
|
|
|
|
2015-12-07 16:17:57 +00:00
|
|
|
|
2016-04-14 10:57:45 +00:00
|
|
|
if __name__ == '__main__':
|
2016-09-29 09:26:58 +00:00
|
|
|
import signal
|
2016-04-14 10:57:45 +00:00
|
|
|
autostart = len(sys.argv) > 1 and sys.argv[1] == '--autostart'
|
2019-01-25 13:15:59 +00:00
|
|
|
if OMLStatus.is_running():
|
|
|
|
OMLStatus.load()
|
2016-09-29 09:26:58 +00:00
|
|
|
else:
|
2019-01-25 13:15:59 +00:00
|
|
|
oml = OMLStatus(autostart)
|
2016-09-29 09:26:58 +00:00
|
|
|
main_loop = GLib.MainLoop()
|
|
|
|
try:
|
|
|
|
main_loop.run()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
oml.stop()
|