mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use RAII in winutil
This commit is contained in:
parent
48e26a754d
commit
59264be1f0
@ -375,10 +375,10 @@ winutil_strftime(PyObject *self, PyObject *args)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static char winutil_doc[] = "Defines utility methods to interface with windows.";
|
static char winutil_doc[] = "Defines utility methods to interface with windows.";
|
||||||
extern PyObject *add_to_recent_docs(PyObject *self, PyObject *args);
|
extern PyObject *winutil_add_to_recent_docs(PyObject *self, PyObject *args);
|
||||||
extern PyObject *file_association(PyObject *self, PyObject *args);
|
extern PyObject *winutil_file_association(PyObject *self, PyObject *args);
|
||||||
extern PyObject *friendly_name(PyObject *self, PyObject *args);
|
extern PyObject *winutil_friendly_name(PyObject *self, PyObject *args);
|
||||||
extern PyObject *notify_associations_changed(PyObject *self, PyObject *args);
|
extern PyObject *winutil_notify_associations_changed(PyObject *self, PyObject *args);
|
||||||
|
|
||||||
static PyMethodDef winutil_methods[] = {
|
static PyMethodDef winutil_methods[] = {
|
||||||
{"special_folder_path", winutil_folder_path, METH_VARARGS,
|
{"special_folder_path", winutil_folder_path, METH_VARARGS,
|
||||||
|
@ -29,71 +29,80 @@ py_to_wchar(PyObject *obj, wchar_t **output) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void
|
class wchar_raii {
|
||||||
free_wchar_buffer(wchar_t **buf) {
|
private:
|
||||||
#if PY_MAJOR_VERSION >= 3
|
wchar_t **handle;
|
||||||
PyMem_Free(*buf);
|
// copy and assignment not implemented; prevent their use by
|
||||||
#endif
|
// declaring private.
|
||||||
*buf = NULL;
|
wchar_raii( const wchar_raii & ) ;
|
||||||
}
|
wchar_raii & operator=( const wchar_raii & ) ;
|
||||||
|
|
||||||
|
public:
|
||||||
|
wchar_raii(wchar_t **buf) : handle(*buf) {}
|
||||||
|
|
||||||
|
~wchar_raii() {
|
||||||
|
#if PY_MAJOR_VERSION >= 3
|
||||||
|
PyMem_Free(*handle);
|
||||||
|
#endif
|
||||||
|
*handle = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
wchar_t *ptr() { return *handle; }
|
||||||
|
};
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
add_to_recent_docs(PyObject *self, PyObject *args) {
|
winutil_add_to_recent_docs(PyObject *self, PyObject *args) {
|
||||||
wchar_t *path, *app_id;
|
wchar_t *path_, *app_id_;
|
||||||
if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &path, py_to_wchar, &app_id)) return NULL;
|
if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &path_, py_to_wchar, &app_id_)) return NULL;
|
||||||
if (app_id) {
|
wchar_raii path(path_), app_id(app_id_);
|
||||||
|
if (app_id.ptr()) {
|
||||||
CComPtr<IShellItem> item;
|
CComPtr<IShellItem> item;
|
||||||
HRESULT hr = SHCreateItemFromParsingName(path, NULL, IID_PPV_ARGS(&item));
|
HRESULT hr = SHCreateItemFromParsingName(path.ptr(), NULL, IID_PPV_ARGS(&item));
|
||||||
if (SUCCEEDED(hr)) {
|
if (SUCCEEDED(hr)) {
|
||||||
SHARDAPPIDINFO info;
|
SHARDAPPIDINFO info;
|
||||||
info.psi = item;
|
info.psi = item;
|
||||||
info.pszAppID = app_id;
|
info.pszAppID = app_id.ptr();
|
||||||
SHAddToRecentDocs(SHARD_APPIDINFO, &info);
|
SHAddToRecentDocs(SHARD_APPIDINFO, &info);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
SHAddToRecentDocs(SHARD_PATHW, path);
|
SHAddToRecentDocs(SHARD_PATHW, path.ptr());
|
||||||
}
|
}
|
||||||
free_wchar_buffer(&path); free_wchar_buffer(&app_id);
|
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
file_association(PyObject *self, PyObject *args) {
|
winutil_file_association(PyObject *self, PyObject *args) {
|
||||||
wchar_t *ext, buf[2048];
|
wchar_t *ext_, buf[2048];
|
||||||
DWORD sz = sizeof(buf);
|
DWORD sz = sizeof(buf);
|
||||||
if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &ext)) return NULL;
|
if (!PyArg_ParseTuple(args, "O&", py_to_wchar, &ext_)) return NULL;
|
||||||
HRESULT hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, ext, NULL, buf, &sz);
|
wchar_raii ext(ext_);
|
||||||
free_wchar_buffer(&ext);
|
HRESULT hr = AssocQueryStringW(0, ASSOCSTR_EXECUTABLE, ext.ptr(), NULL, buf, &sz);
|
||||||
if (!SUCCEEDED(hr) || sz < 1) Py_RETURN_NONE;
|
if (!SUCCEEDED(hr) || sz < 1) Py_RETURN_NONE;
|
||||||
return Py_BuildValue("u", buf);
|
return Py_BuildValue("u", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
friendly_name(PyObject *self, PyObject *args) {
|
winutil_friendly_name(PyObject *self, PyObject *args) {
|
||||||
wchar_t *exe, *prog_id, buf[2048], *p;
|
wchar_t *exe_, *prog_id_, buf[2048], *p;
|
||||||
DWORD sz = sizeof(buf);
|
DWORD sz = sizeof(buf);
|
||||||
if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &prog_id, py_to_wchar, &exe)) return NULL;
|
if (!PyArg_ParseTuple(args, "O&O&", py_to_wchar, &prog_id_, py_to_wchar, &exe_)) return NULL;
|
||||||
|
wchar_raii exe(exe_), prog_id(prog_id_);
|
||||||
ASSOCF flags = ASSOCF_REMAPRUNDLL;
|
ASSOCF flags = ASSOCF_REMAPRUNDLL;
|
||||||
if (exe) {
|
if (exe.ptr()) {
|
||||||
p = exe;
|
p = exe.ptr();
|
||||||
flags |= ASSOCF_OPEN_BYEXENAME;
|
flags |= ASSOCF_OPEN_BYEXENAME;
|
||||||
} else p = prog_id;
|
} else p = prog_id.ptr();
|
||||||
if (!p) {
|
if (!p) Py_RETURN_NONE;
|
||||||
free_wchar_buffer(&exe); free_wchar_buffer(&prog_id);
|
|
||||||
Py_RETURN_NONE;
|
|
||||||
}
|
|
||||||
HRESULT hr = AssocQueryStringW(flags, ASSOCSTR_FRIENDLYAPPNAME, p, NULL, buf, &sz);
|
HRESULT hr = AssocQueryStringW(flags, ASSOCSTR_FRIENDLYAPPNAME, p, NULL, buf, &sz);
|
||||||
free_wchar_buffer(&exe); free_wchar_buffer(&prog_id);
|
|
||||||
if (!SUCCEEDED(hr) || sz < 1) Py_RETURN_NONE;
|
if (!SUCCEEDED(hr) || sz < 1) Py_RETURN_NONE;
|
||||||
return Py_BuildValue("u", buf);
|
return Py_BuildValue("u", buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject *
|
PyObject *
|
||||||
notify_associations_changed(PyObject *self, PyObject *args) {
|
winutil_notify_associations_changed(PyObject *self, PyObject *args) {
|
||||||
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_DWORD | SHCNF_FLUSH, NULL, NULL);
|
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_DWORD | SHCNF_FLUSH, NULL, NULL);
|
||||||
Py_RETURN_NONE;
|
Py_RETURN_NONE;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user