2010-04-07 06:52:24 +00:00
|
|
|
/* Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
|
|
|
|
|
|
|
|
This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
|
|
|
which should be included with this package. The terms are also available at
|
|
|
|
http://www.hardcoded.net/licenses/bsd_license
|
|
|
|
*/
|
|
|
|
|
2010-04-06 09:28:43 +00:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
#include "Python.h"
|
|
|
|
|
|
|
|
#define WINDOWS_LEAN_AND_MEAN
|
|
|
|
#include "windows.h"
|
|
|
|
#include "shlobj.h"
|
|
|
|
|
2010-04-06 09:41:07 +00:00
|
|
|
/* WARNING: If the filepath is not fully qualified, Windows deletes the file
|
2010-04-06 09:28:43 +00:00
|
|
|
rather than sending it to trash.
|
|
|
|
*/
|
|
|
|
|
2010-04-06 16:04:32 +00:00
|
|
|
static PyObject* send2trash_win_send(PyObject *self, PyObject *args)
|
2010-04-06 09:28:43 +00:00
|
|
|
{
|
|
|
|
SHFILEOPSTRUCTW op;
|
|
|
|
PyObject *filepath;
|
2010-04-06 09:41:07 +00:00
|
|
|
Py_ssize_t len;
|
2010-04-06 09:28:43 +00:00
|
|
|
WCHAR filechars[MAX_PATH+1];
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (!PyArg_ParseTuple(args, "O", &filepath)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PyUnicode_Check(filepath)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "Unicode filename required");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = PyUnicode_GET_SIZE(filepath);
|
2010-04-06 09:41:07 +00:00
|
|
|
memcpy(filechars, PyUnicode_AsUnicode(filepath), sizeof(WCHAR)*len);
|
2010-04-06 09:28:43 +00:00
|
|
|
filechars[len] = '\0';
|
|
|
|
filechars[len+1] = '\0';
|
|
|
|
|
|
|
|
op.hwnd = 0;
|
|
|
|
op.wFunc = FO_DELETE;
|
|
|
|
op.pFrom = (LPCWSTR)&filechars;
|
|
|
|
op.pTo = NULL;
|
|
|
|
op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
|
|
|
|
r = SHFileOperationW(&op);
|
2010-04-06 09:41:07 +00:00
|
|
|
|
|
|
|
if (r != 0) {
|
|
|
|
PyErr_Format(PyExc_OSError, "Couldn't perform operation. Error code: %d", r);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-04-06 09:28:43 +00:00
|
|
|
return Py_None;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef TrashMethods[] = {
|
2010-04-06 16:04:32 +00:00
|
|
|
{"send", send2trash_win_send, METH_VARARGS, ""},
|
2010-04-06 09:28:43 +00:00
|
|
|
{NULL, NULL, 0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
2010-04-06 16:04:32 +00:00
|
|
|
init_send2trash_win(void)
|
2010-04-06 09:28:43 +00:00
|
|
|
{
|
2010-04-06 16:04:32 +00:00
|
|
|
PyObject *m = Py_InitModule("_send2trash_win", TrashMethods);
|
2010-04-06 09:28:43 +00:00
|
|
|
if (m == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|