Set Windows launch at startup

This commit is contained in:
JonnyWong16 2020-04-25 12:08:14 -07:00
parent 4b5f880ccb
commit 7bb9c6c915
No known key found for this signature in database
GPG key ID: B1F1F9807184697A
6 changed files with 149 additions and 65 deletions

View file

@ -36,7 +36,8 @@ import tzlocal
import plexpy import plexpy
from plexpy import config, database, helpers, logger, webstart from plexpy import config, database, helpers, logger, webstart
if os.name == 'nt':
from plexpy import windows
# Register signals, such as CTRL + C # Register signals, such as CTRL + C
signal.signal(signal.SIGINT, plexpy.sig_handler) signal.signal(signal.SIGINT, plexpy.sig_handler)
@ -240,8 +241,10 @@ def main():
webstart.start() webstart.start()
# Windows system tray icon # Windows system tray icon
if os.name == 'nt' and plexpy.CONFIG.WIN_SYS_TRAY: if os.name == 'nt':
plexpy.win_system_tray() windows.set_startup()
if plexpy.CONFIG.WIN_SYS_TRAY:
windows.win_system_tray()
logger.info("Tautulli is ready!") logger.info("Tautulli is ready!")

View file

@ -455,6 +455,12 @@
</label> </label>
<p class="help-block">Show Tautulli shortcut in the system tray.</p> <p class="help-block">Show Tautulli shortcut in the system tray.</p>
</div> </div>
<div class="checkbox">
<label>
<input type="checkbox" name="launch_startup" id="launch_startup" value="1" ${config['launch_startup']}> Launch at System Startup
</label>
<p class="help-block">Start Tautulli automatically after Windows Login.</p>
</div>
% endif % endif
<div class="checkbox"> <div class="checkbox">
<label> <label>

View file

@ -415,56 +415,6 @@ def launch_browser(host, port, root):
logger.error("Could not launch browser: %s" % e) logger.error("Could not launch browser: %s" % e)
def win_system_tray():
from systray import SysTrayIcon
def tray_open(sysTrayIcon):
launch_browser(CONFIG.HTTP_HOST, HTTP_PORT, HTTP_ROOT)
def tray_check_update(sysTrayIcon):
versioncheck.check_update()
def tray_update(sysTrayIcon):
global SIGNAL
global WIN_SYS_TRAY_ICON
if UPDATE_AVAILABLE:
SIGNAL = 'update'
else:
hover_text = common.PRODUCT + ' - No Update Available'
WIN_SYS_TRAY_ICON.update(hover_text=hover_text)
def tray_restart(sysTrayIcon):
global SIGNAL
SIGNAL = 'restart'
def tray_quit(sysTrayIcon):
global SIGNAL
SIGNAL = 'shutdown'
if UPDATE_AVAILABLE:
icon = os.path.join(PROG_DIR, 'data/interfaces/', CONFIG.INTERFACE, 'images/logo_tray-update.ico')
hover_text = common.PRODUCT + ' - Update Available!'
else:
icon = os.path.join(PROG_DIR, 'data/interfaces/', CONFIG.INTERFACE, 'images/logo_tray.ico')
hover_text = common.PRODUCT
menu_options = (('Open Tautulli', None, tray_open, 'default'),
('', None, 'separator', None),
('Check for Updates', None, tray_check_update, None),
('Update', None, tray_update, None),
('Restart', None, tray_restart, None))
logger.info("Launching system tray icon.")
global WIN_SYS_TRAY_ICON
try:
WIN_SYS_TRAY_ICON = SysTrayIcon(icon, hover_text, menu_options, on_quit=tray_quit)
WIN_SYS_TRAY_ICON.start()
except Exception as e:
logger.error("Unable to launch system tray icon: %s." % e)
WIN_SYS_TRAY_ICON = None
def initialize_scheduler(): def initialize_scheduler():
""" """
Start the scheduled background tasks. Re-schedule if interval settings changed. Start the scheduled background tasks. Re-schedule if interval settings changed.

View file

@ -293,6 +293,7 @@ _CONFIG_DEFINITIONS = {
'JOIN_ON_NEWDEVICE': (int, 'Join', 0), 'JOIN_ON_NEWDEVICE': (int, 'Join', 0),
'JOURNAL_MODE': (str, 'Advanced', 'WAL'), 'JOURNAL_MODE': (str, 'Advanced', 'WAL'),
'LAUNCH_BROWSER': (int, 'General', 1), 'LAUNCH_BROWSER': (int, 'General', 1),
'LAUNCH_STARTUP': (int, 'General', 1),
'LOG_BLACKLIST': (int, 'General', 1), 'LOG_BLACKLIST': (int, 'General', 1),
'LOG_DIR': (str, 'General', ''), 'LOG_DIR': (str, 'General', ''),
'LOGGING_IGNORE_INTERVAL': (int, 'Monitoring', 120), 'LOGGING_IGNORE_INTERVAL': (int, 'Monitoring', 120),

View file

@ -66,6 +66,7 @@ if plexpy.PYTHON2:
import versioncheck import versioncheck
import web_socket import web_socket
import webstart import webstart
import windows
from api2 import API2 from api2 import API2
from helpers import checked, addtoapi, get_ip, create_https_certificates, build_datatables_json, sanitize_out from helpers import checked, addtoapi, get_ip, create_https_certificates, build_datatables_json, sanitize_out
from session import get_session_info, get_session_user_id, allow_session_user, allow_session_library from session import get_session_info, get_session_user_id, allow_session_user, allow_session_library
@ -95,6 +96,7 @@ else:
from plexpy import versioncheck from plexpy import versioncheck
from plexpy import web_socket from plexpy import web_socket
from plexpy import webstart from plexpy import webstart
from plexpy import windows
from plexpy.api2 import API2 from plexpy.api2 import API2
from plexpy.helpers import checked, addtoapi, get_ip, create_https_certificates, build_datatables_json, sanitize_out from plexpy.helpers import checked, addtoapi, get_ip, create_https_certificates, build_datatables_json, sanitize_out
from plexpy.session import get_session_info, get_session_user_id, allow_session_user, allow_session_library from plexpy.session import get_session_info, get_session_user_id, allow_session_user, allow_session_library
@ -2939,6 +2941,7 @@ class WebInterface(object):
"http_proxy": checked(plexpy.CONFIG.HTTP_PROXY), "http_proxy": checked(plexpy.CONFIG.HTTP_PROXY),
"http_plex_admin": checked(plexpy.CONFIG.HTTP_PLEX_ADMIN), "http_plex_admin": checked(plexpy.CONFIG.HTTP_PLEX_ADMIN),
"launch_browser": checked(plexpy.CONFIG.LAUNCH_BROWSER), "launch_browser": checked(plexpy.CONFIG.LAUNCH_BROWSER),
"launch_startup": checked(plexpy.CONFIG.LAUNCH_STARTUP),
"enable_https": checked(plexpy.CONFIG.ENABLE_HTTPS), "enable_https": checked(plexpy.CONFIG.ENABLE_HTTPS),
"https_create_cert": checked(plexpy.CONFIG.HTTPS_CREATE_CERT), "https_create_cert": checked(plexpy.CONFIG.HTTPS_CREATE_CERT),
"https_cert": plexpy.CONFIG.HTTPS_CERT, "https_cert": plexpy.CONFIG.HTTPS_CERT,
@ -3042,6 +3045,7 @@ class WebInterface(object):
# Check if we should refresh our data # Check if we should refresh our data
first_run = False first_run = False
startup_changed = False
server_changed = False server_changed = False
reschedule = False reschedule = False
https_changed = False https_changed = False
@ -3053,7 +3057,8 @@ class WebInterface(object):
first_run = True first_run = True
checked_configs = [ checked_configs = [
"launch_browser", "enable_https", "https_create_cert", "api_enabled", "freeze_db", "check_github", "launch_browser", "launch_startup", "enable_https", "https_create_cert",
"api_enabled", "freeze_db", "check_github",
"grouping_global_history", "grouping_user_history", "grouping_charts", "group_history_tables", "grouping_global_history", "grouping_user_history", "grouping_charts", "group_history_tables",
"pms_url_manual", "week_start_monday", "pms_url_manual", "week_start_monday",
"refresh_libraries_on_startup", "refresh_users_on_startup", "refresh_libraries_on_startup", "refresh_users_on_startup",
@ -3102,6 +3107,10 @@ class WebInterface(object):
kwargs[plain_config] = kwargs[use_config] kwargs[plain_config] = kwargs[use_config]
del kwargs[use_config] del kwargs[use_config]
if kwargs.get('launch_startup') != plexpy.CONFIG.LAUNCH_STARTUP or \
kwargs.get('launch_browser') != plexpy.CONFIG.LAUNCH_BROWSER:
startup_changed = True
# If we change any monitoring settings, make sure we reschedule tasks. # If we change any monitoring settings, make sure we reschedule tasks.
if kwargs.get('check_github') != plexpy.CONFIG.CHECK_GITHUB or \ if kwargs.get('check_github') != plexpy.CONFIG.CHECK_GITHUB or \
kwargs.get('refresh_libraries_interval') != str(plexpy.CONFIG.REFRESH_LIBRARIES_INTERVAL) or \ kwargs.get('refresh_libraries_interval') != str(plexpy.CONFIG.REFRESH_LIBRARIES_INTERVAL) or \
@ -3168,6 +3177,11 @@ class WebInterface(object):
# Write the config # Write the config
plexpy.CONFIG.write() plexpy.CONFIG.write()
# Enable or disable system startup
if startup_changed:
if os.name == 'nt':
windows.set_startup()
# Get new server URLs for SSL communications and get new server friendly name # Get new server URLs for SSL communications and get new server friendly name
if server_changed: if server_changed:
plextv.get_server_resources() plextv.get_server_resources()

110
plexpy/windows.py Normal file
View file

@ -0,0 +1,110 @@
# -*- coding: utf-8 -*-
# This file is part of Tautulli.
#
# Tautulli is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Tautulli is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Tautulli. If not, see <http://www.gnu.org/licenses/>.
import os
import shlex
import sys
import winreg
import plexpy
if plexpy.PYTHON2:
import common
import logger
import versioncheck
else:
from plexpy import common
from plexpy import logger
from plexpy import versioncheck
def win_system_tray():
from systray import SysTrayIcon
def tray_open(sysTrayIcon):
plexpy.launch_browser(plexpy.CONFIG.HTTP_HOST, plexpy.HTTP_PORT, plexpy.HTTP_ROOT)
def tray_check_update(sysTrayIcon):
versioncheck.check_update()
def tray_update(sysTrayIcon):
if plexpy.UPDATE_AVAILABLE:
plexpy.SIGNAL = 'update'
else:
hover_text = common.PRODUCT + ' - No Update Available'
plexpy.WIN_SYS_TRAY_ICON.update(hover_text=hover_text)
def tray_restart(sysTrayIcon):
plexpy.SIGNAL = 'restart'
def tray_quit(sysTrayIcon):
plexpy.SIGNAL = 'shutdown'
if plexpy.UPDATE_AVAILABLE:
icon = os.path.join(plexpy.PROG_DIR, 'data/interfaces/', plexpy.CONFIG.INTERFACE, 'images/logo_tray-update.ico')
hover_text = common.PRODUCT + ' - Update Available!'
else:
icon = os.path.join(plexpy.PROG_DIR, 'data/interfaces/', plexpy.CONFIG.INTERFACE, 'images/logo_tray.ico')
hover_text = common.PRODUCT
menu_options = (('Open Tautulli', None, tray_open, 'default'),
('', None, 'separator', None),
('Check for Updates', None, tray_check_update, None),
('Update', None, tray_update, None),
('Restart', None, tray_restart, None))
logger.info("Launching system tray icon.")
try:
plexpy.WIN_SYS_TRAY_ICON = SysTrayIcon(icon, hover_text, menu_options, on_quit=tray_quit)
plexpy.WIN_SYS_TRAY_ICON.start()
except Exception as e:
logger.error("Unable to launch system tray icon: %s." % e)
plexpy.WIN_SYS_TRAY_ICON = None
def set_startup():
startup_reg_path = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
exe = sys.executable
if plexpy.FROZEN:
args = [exe]
else:
args = [exe, plexpy.FULL_PATH]
if not plexpy.CONFIG.LAUNCH_BROWSER:
args += ['--nolaunch']
cmd = ' '.join(shlex.quote(arg) for arg in args).replace('python.exe', 'pythonw.exe').replace("'", '"')
if plexpy.CONFIG.LAUNCH_STARTUP:
try:
winreg.CreateKey(winreg.HKEY_CURRENT_USER, startup_reg_path)
registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, startup_reg_path, 0, winreg.KEY_WRITE)
winreg.SetValueEx(registry_key, common.PRODUCT, 0, winreg.REG_SZ, cmd)
winreg.CloseKey(registry_key)
return True
except WindowsError:
return False
else:
try:
registry_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, startup_reg_path, 0, winreg.KEY_ALL_ACCESS)
winreg.DeleteValue(registry_key, common.PRODUCT)
winreg.CloseKey(registry_key)
return True
except WindowsError:
return False