From 0551a18cc9868995bb21d2fc2f2a6829b63b86fa Mon Sep 17 00:00:00 2001 From: j Date: Thu, 14 Apr 2016 12:57:45 +0200 Subject: [PATCH] autostart --- config.json | 1 + ctl | 13 ++++++++++++ oml/gtkstatus.py | 14 ++++++++----- oml/integration.py | 37 ++++++++++++++++++++++++++++++----- oml/user/api.py | 8 ++++++++ static/js/preferencesPanel.js | 6 ++++++ 6 files changed, 69 insertions(+), 10 deletions(-) diff --git a/config.json b/config.json index 9006337..813dff5 100644 --- a/config.json +++ b/config.json @@ -302,6 +302,7 @@ ], "user": { "preferences": { + "autostart": false, "acceptMessage": "", "contact": "", "downloadRate": null, diff --git a/ctl b/ctl index 3a99399..794ec4c 100755 --- a/ctl +++ b/ctl @@ -150,6 +150,19 @@ if [ "$1" == "open" ]; then fi exit 0 fi +if [ "$1" == "autostart" ]; then + if [ $SYSTEM == "Darwin" ]; then + open "/Applications/Open Media Library.app" --args --autostart + fi + if [ $SYSTEM == "Linux" ]; then + if [ ! -e "$PID" ]; then + python3 "$NAME/oml/gtkstatus.py" --autostart + exit $? + fi + fi + exit 0 +fi + if [ "$1" == "ui" ]; then shift python3 "$NAME/oml/ui.py" $@ diff --git a/oml/gtkstatus.py b/oml/gtkstatus.py index fa967bf..8031d0e 100644 --- a/oml/gtkstatus.py +++ b/oml/gtkstatus.py @@ -2,7 +2,7 @@ import os from os.path import dirname, abspath import subprocess -import time +import sys import webbrowser import gi @@ -26,7 +26,8 @@ class OMLIcon: icon = None indicator = None - def __init__(self): + def __init__(self, autostart=False): + self.autostart = autostart if appindicator: self.indicator = appindicator.Indicator.new("OML", icon, appindicator.IndicatorCategory.APPLICATION_STATUS) @@ -40,7 +41,8 @@ class OMLIcon: self.icon.connect("activate", self._click) self.icon.connect("popup-menu", self._click) subprocess.Popen([ctl, 'start']) - GLib.timeout_add_seconds(1, self._open, None) + if not self.autostart: + GLib.timeout_add_seconds(1, self._open, None) GLib.timeout_add_seconds(60, self._check, None) def _check(self, *args, **kwargs): @@ -85,5 +87,7 @@ class OMLIcon: webbrowser.open_new_tab(url) self.menu = None -OMLIcon() -Gtk.main() +if __name__ == '__main__': + autostart = len(sys.argv) > 1 and sys.argv[1] == '--autostart' + OMLIcon(autostart) + Gtk.main() diff --git a/oml/integration.py b/oml/integration.py index aa3a3db..891f831 100644 --- a/oml/integration.py +++ b/oml/integration.py @@ -7,15 +7,16 @@ from utils import run root_dir = os.path.dirname(settings.base_dir) -def install_launcher(): +def install_autostart(): if sys.platform == 'darwin': install_launchd() elif sys.platform.startswith('linux'): - install_xdg() + install_autostart_xdg() else: print('no launcher integration supported for %s' % sys.platform) -def uninstall_launcher(): + +def uninstall_autostart(): if sys.platform == 'darwin': name = 'com.openmedialibrary.loginscript' plist = os.path.expanduser('~/Library/LaunchAgents/%s.plist'%name) @@ -25,12 +26,25 @@ def uninstall_launcher(): os.unlink(plist) elif sys.platform.startswith('linux'): for f in map(os.path.expanduser, [ - '~/.local/share/applications/openmedialibrary.desktop', '~/.config/autostart/openmedialibrary.desktop' ]): if os.path.exists(f): os.unlink(f) +def install_launcher(): + if sys.platform.startswith('linux'): + install_xdg() + else: + print('no launcher integration supported for %s' % sys.platform) + +def uninstall_launcher(): + if sys.platform.startswith('linux'): + for f in map(os.path.expanduser, [ + '~/.local/share/applications/openmedialibrary.desktop', + ]): + if os.path.exists(f): + os.unlink(f) + def install_launchd(): return name = 'com.openmedialibrary.loginscript' @@ -48,7 +62,7 @@ def install_launchd(): ProgramArguments %s/ctl - start + autostart RunAtLoad @@ -72,3 +86,16 @@ Terminal=false Categories=Network;FileTransfer;P2P; ''' % {'base': root_dir}) +def install_autostart_xdg(): + app = os.path.expanduser('~/.config/autostart/openmedialibrary.desktop') + ox.makedirs(os.path.dirname(app)) + with open(app, 'w') as fd: + fd.write('''[Desktop Entry] +Type=Application +Name=Open Media Library +Comment= +Exec=%(base)s/ctl autostart +Icon=%(base)s/openmedialibrary/static/png/oml.png +Terminal=false +X-GNOME-Autostart-enabled=true +''' % {'base': root_dir}) diff --git a/oml/user/api.py b/oml/user/api.py index f1d402c..d306b50 100644 --- a/oml/user/api.py +++ b/oml/user/api.py @@ -66,6 +66,8 @@ def setPreferences(data): data['contact'] != settings.preferences['contact'] change_username = 'username' in data and \ data['username'] != settings.preferences['username'] + change_autostart = 'autostart' in data and \ + data['autostart'] != settings.preferences['autostart'] if 'libraryPath' in data and \ data['libraryPath'] != settings.preferences['libraryPath']: change_path = [settings.preferences['libraryPath'], data['libraryPath']] @@ -82,6 +84,12 @@ def setPreferences(data): Changelog.record(state.user(), 'editcontact', data['contact']) if change_path: state.tasks.queue('changelibrarypath', change_path) + if change_autostart: + import integration + if settings.preferences['autostart']: + integration.install_autostart() + else: + integration.uninstall_autostart() return settings.preferences actions.register(setPreferences, cache=False) diff --git a/static/js/preferencesPanel.js b/static/js/preferencesPanel.js index fda0d2b..5d35cfe 100644 --- a/static/js/preferencesPanel.js +++ b/static/js/preferencesPanel.js @@ -136,6 +136,12 @@ oml.ui.preferencesPanel = function() { } ], advanced: [ + { + id: 'autostart', + title: 'Start Open Media Library at Login', + value: preferences.autostart, + help: 'Launch Open Media Library in the background once you login to your computer.' + }, { id: 'showDebugMenu', title: 'Show Debug Menu',