Dont use win32com for notifying the OS of file association changes

This commit is contained in:
Kovid Goyal 2019-06-11 16:17:34 +05:30
parent ed85704799
commit 21fbec2820
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 17 additions and 3 deletions

View File

@ -30,6 +30,9 @@ class TestWinutil(unittest.TestCase):
def test_special_folder_path(self):
self.assertEqual(os.path.expanduser('~'), self.winutil.special_folder_path(self.winutil.CSIDL_PROFILE))
def test_associations_changed(self):
self.assertIsNone(self.winutil.notify_associations_changed())
def find_tests():
return unittest.defaultTestLoader.loadTestsFromTestCase(TestWinutil)

View File

@ -377,6 +377,7 @@ winutil_strftime(PyObject *self, PyObject *args)
static char winutil_doc[] = "Defines utility methods to interface with windows.";
extern PyObject *add_to_recent_docs(PyObject *self, PyObject *args);
extern PyObject *file_association(PyObject *self, PyObject *args);
extern PyObject *notify_associations_changed(PyObject *self, PyObject *args);
static PyMethodDef winutil_methods[] = {
{"special_folder_path", winutil_folder_path, METH_VARARGS,
@ -452,6 +453,10 @@ be a unicode string. Returns unicode strings."
"file_association()\n\nGet the executable associated with the given file extension"
},
{"notify_associations_changed", (PyCFunction)notify_associations_changed, METH_VARARGS,
"notify_associations_changed()\n\nNotify the OS that file associations have changed"
},
{NULL, NULL, 0, NULL}
};

View File

@ -72,4 +72,10 @@ file_association(PyObject *self, PyObject *args) {
return Py_BuildValue("u#", buf, (int)sz);
}
PyObject *
notify_associations_changed(PyObject *self, PyObject *args) {
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_DWORD | SHCNF_FLUSH, NULL, NULL);
Py_RETURN_NONE;
}
}

View File

@ -12,7 +12,7 @@ from threading import Thread
import winerror
from calibre import guess_type, prints
from calibre.constants import is64bit, isportable, isfrozen, __version__, DEBUG
from calibre.constants import is64bit, isportable, isfrozen, __version__, DEBUG, plugins
from calibre.utils.winreg.lib import Key, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE
from polyglot.builtins import iteritems, itervalues
@ -131,8 +131,8 @@ def register():
with Key(r'Software\RegisteredApplications') as key:
key.set(data['name'], capabilities_path)
from win32com.shell import shell, shellcon
shell.SHChangeNotify(shellcon.SHCNE_ASSOCCHANGED, shellcon.SHCNF_DWORD | shellcon.SHCNF_FLUSH, 0, 0)
winutil = plugins['winutil'][0]
winutil.notify_associations_changed()
def unregister():