2019-06-06 11:52:23 -05:00
from __future__ import unicode_literals
from future import standard_library
standard_library . install_aliases ( )
from builtins import str
import winreg
2014-10-27 16:29:04 -06:00
import os
import sys
from platform_utils import paths
2019-06-06 11:52:23 -05:00
RUN_REGKEY = r " SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run "
2014-10-27 16:29:04 -06:00
def is_installed ( app_subkey ) :
2021-06-16 16:18:41 -05:00
""" Checks if the currently running copy is installed or portable variant. Requires the name of the application subkey found under the uninstall section in Windows registry. """
2014-10-27 16:29:04 -06:00
2021-06-16 16:18:41 -05:00
try :
key = winreg . OpenKey ( winreg . HKEY_LOCAL_MACHINE , " SOFTWARE \\ Microsoft \\ Windows \\ CurrentVersion \\ Uninstall \\ %s " % app_subkey )
inst_dir = winreg . QueryValueEx ( key , " InstallLocation " ) [ 0 ]
except WindowsError :
return False
winreg . CloseKey ( key )
try :
return os . stat ( inst_dir ) == os . stat ( paths . app_path ( ) )
except WindowsError :
return False
2014-10-27 16:29:04 -06:00
def getAutoStart ( app_name ) :
2021-06-16 16:18:41 -05:00
""" Queries if the automatic startup should be set for the application or not, depending on it ' s current state. """
2014-10-27 16:29:04 -06:00
2021-06-16 16:18:41 -05:00
try :
key = winreg . OpenKey ( winreg . HKEY_CURRENT_USER , RUN_REGKEY )
val = winreg . QueryValueEx ( key , str ( app_name ) ) [ 0 ]
return os . stat ( val ) == os . stat ( sys . argv [ 0 ] )
except ( WindowsError , OSError ) :
return False
2014-10-27 16:29:04 -06:00
def setAutoStart ( app_name , enable = True ) :
2021-06-16 16:18:41 -05:00
""" Configures automatic startup for the application, if the enable argument is set to True. If set to False, deletes the application AutoStart value. """
if getAutoStart ( app_name ) == enable :
return
key = winreg . OpenKey ( winreg . HKEY_CURRENT_USER , RUN_REGKEY , 0 , winreg . KEY_WRITE )
if enable :
winreg . SetValueEx ( key , str ( app_name ) , None , winreg . REG_SZ , paths . get_executable ( ) )
else :
winreg . DeleteValue ( key , str ( app_name ) )