def _registerFileAssociation(extension, exePath, associate, mimetype): try: with winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, "SOFTWARE\\Classes\\%s" % associate, 0, winreg.KEY_WRITE) as k: #winreg.SetValueEx(k, None, 0, winreg.REG_SZ, "") with winreg.CreateKeyEx(k, "DefaultIcon", 0, winreg.KEY_WRITE) as k2: winreg.SetValueEx(k2, None, 0, winreg.REG_SZ, "@{exePath},1".format(exePath=exePath)) with winreg.CreateKeyEx(k, "shell\\open\\command", 0, winreg.KEY_WRITE) as k2: winreg.SetValueEx( k2, None, 0, winreg.REG_SZ, u"\"{exePath}\" \"%1\"".format(exePath=exePath)) with winreg.CreateKeyEx(winreg.HKEY_CURRENT_USER, "SOFTWARE\\Classes\\%s" % extension, 0, winreg.KEY_WRITE) as k: winreg.SetValueEx(k, None, 0, winreg.REG_SZ, associate) if mimetype != None: winreg.SetValueEx(k, "Content Type", 0, winreg.REG_SZ, mimetype) k2 = winreg.CreateKeyEx(k, "OpenWithProgids\\%s" % associate, 0, winreg.KEY_WRITE) winreg.CloseKey(k2) shellapi.SHChangeNotify(shellapi.SHCNE_ASSOCCHANGED, shellapi.SHCNF_IDLIST, None, None) return True except WindowsError as e: log = logging.getLogger("%s.fileAssoc" % (constants.LOG_PREFIX)) log.error("file Association faild - %s" % (str(e))) return False
def unregisterAddonFileAssociation(): try: # As per MSDN recomendation, we only need to remove the prog ID. _deleteKeyAndSubkeys(_winreg.HKEY_LOCAL_MACHINE, "Software\\Classes\\%s" % addonHandler.NVDA_ADDON_PROG_ID) except WindowsError: # This is probably the first install, so just ignore the error. return # Notify the shell that a file association has changed: shellapi.SHChangeNotify(shellapi.SHCNE_ASSOCCHANGED, shellapi.SHCNF_IDLIST, None, None)
def _unregisterFileAssociation(associate): try: _deleteKeyAndSubkeys(winreg.HKEY_CURRENT_USER, "Software\\Classes\\%s" % associate) except WindowsError as e: log = logging.getLogger("%s.fileAssoc" % (constants.LOG_PREFIX)) log.error("Unset File Association faild - %s" % str(e)) return False shellapi.SHChangeNotify(shellapi.SHCNE_ASSOCCHANGED, shellapi.SHCNF_IDLIST, None, None) return True
def registerAddonFileAssociation(slaveExe): try: # Create progID for NVDA ad-ons with winreg.CreateKeyEx( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes\\%s" % addonHandler.NVDA_ADDON_PROG_ID, 0, winreg.KEY_WRITE) as k: # Translators: A file extension label for NVDA add-on package. winreg.SetValueEx(k, None, 0, winreg.REG_SZ, _("NVDA add-on package")) with winreg.CreateKeyEx(k, "DefaultIcon", 0, winreg.KEY_WRITE) as k2: winreg.SetValueEx(k2, None, 0, winreg.REG_SZ, "@{slaveExe},1".format(slaveExe=slaveExe)) # Point the open verb to nvda_slave addons_installAddonPackage action with winreg.CreateKeyEx(k, "shell\\open\\command", 0, winreg.KEY_WRITE) as k2: winreg.SetValueEx( k2, None, 0, winreg.REG_SZ, u"\"{slaveExe}\" addons_installAddonPackage \"%1\"".format( slaveExe=slaveExe)) # Now associate addon extension to the created prog id. with winreg.CreateKeyEx( winreg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Classes\\.%s" % addonHandler.BUNDLE_EXTENSION, 0, winreg.KEY_WRITE) as k: winreg.SetValueEx(k, None, 0, winreg.REG_SZ, addonHandler.NVDA_ADDON_PROG_ID) winreg.SetValueEx(k, "Content Type", 0, winreg.REG_SZ, addonHandler.BUNDLE_MIMETYPE) # Add NVDA to the "open With" list k2 = winreg.CreateKeyEx( k, "OpenWithProgids\\%s" % addonHandler.NVDA_ADDON_PROG_ID, 0, winreg.KEY_WRITE) winreg.CloseKey(k2) # Notify the shell that a file association has changed: shellapi.SHChangeNotify(shellapi.SHCNE_ASSOCCHANGED, shellapi.SHCNF_IDLIST, None, None) except WindowsError: log.error("Can not create addon file association.", exc_info=True)