2017-08-04 00:52:19 +00:00
|
|
|
# Copyright 2017 Virgil Dupras
|
2010-04-07 06:52:24 +00:00
|
|
|
|
2017-08-04 00:52:19 +00:00
|
|
|
# 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
|
2010-04-07 06:52:24 +00:00
|
|
|
# http://www.hardcoded.net/licenses/bsd_license
|
|
|
|
|
2013-07-19 22:42:32 +00:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-11-01 12:28:22 +00:00
|
|
|
from ctypes import (windll, Structure, byref, c_uint,
|
2019-04-30 16:28:09 +00:00
|
|
|
create_unicode_buffer, addressof,
|
|
|
|
GetLastError, FormatError)
|
2010-10-17 17:00:56 +00:00
|
|
|
from ctypes.wintypes import HWND, UINT, LPCWSTR, BOOL
|
2010-04-06 16:04:32 +00:00
|
|
|
import os.path as op
|
2010-10-17 17:00:56 +00:00
|
|
|
|
2013-07-19 22:42:32 +00:00
|
|
|
from .compat import text_type
|
|
|
|
|
2018-02-16 14:07:05 +00:00
|
|
|
kernel32 = windll.kernel32
|
|
|
|
GetShortPathNameW = kernel32.GetShortPathNameW
|
|
|
|
|
2010-10-17 17:00:56 +00:00
|
|
|
shell32 = windll.shell32
|
|
|
|
SHFileOperationW = shell32.SHFileOperationW
|
|
|
|
|
2018-02-16 14:07:05 +00:00
|
|
|
|
2010-10-17 17:00:56 +00:00
|
|
|
class SHFILEOPSTRUCTW(Structure):
|
|
|
|
_fields_ = [
|
|
|
|
("hwnd", HWND),
|
|
|
|
("wFunc", UINT),
|
|
|
|
("pFrom", LPCWSTR),
|
|
|
|
("pTo", LPCWSTR),
|
|
|
|
("fFlags", c_uint),
|
|
|
|
("fAnyOperationsAborted", BOOL),
|
|
|
|
("hNameMappings", c_uint),
|
|
|
|
("lpszProgressTitle", LPCWSTR),
|
|
|
|
]
|
|
|
|
|
2018-02-16 14:07:05 +00:00
|
|
|
|
2010-10-17 17:00:56 +00:00
|
|
|
FO_MOVE = 1
|
|
|
|
FO_COPY = 2
|
|
|
|
FO_DELETE = 3
|
|
|
|
FO_RENAME = 4
|
|
|
|
|
|
|
|
FOF_MULTIDESTFILES = 1
|
|
|
|
FOF_SILENT = 4
|
|
|
|
FOF_NOCONFIRMATION = 16
|
|
|
|
FOF_ALLOWUNDO = 64
|
|
|
|
FOF_NOERRORUI = 1024
|
2010-04-06 16:04:32 +00:00
|
|
|
|
2018-02-16 14:07:05 +00:00
|
|
|
|
|
|
|
def get_short_path_name(long_name):
|
|
|
|
if not long_name.startswith('\\\\?\\'):
|
|
|
|
long_name = '\\\\?\\' + long_name
|
|
|
|
buf_size = GetShortPathNameW(long_name, None, 0)
|
2019-04-30 16:28:09 +00:00
|
|
|
# FIX: https://github.com/hsoft/send2trash/issues/31
|
|
|
|
# If buffer size is zero, an error has occurred.
|
|
|
|
if not buf_size:
|
|
|
|
err_no = GetLastError()
|
|
|
|
raise WindowsError(err_no, FormatError(err_no), long_name[4:])
|
2018-02-16 14:07:05 +00:00
|
|
|
output = create_unicode_buffer(buf_size)
|
|
|
|
GetShortPathNameW(long_name, output, buf_size)
|
|
|
|
return output.value[4:] # Remove '\\?\' for SHFileOperationW
|
|
|
|
|
|
|
|
|
2010-04-06 16:04:32 +00:00
|
|
|
def send2trash(path):
|
2013-07-19 22:42:32 +00:00
|
|
|
if not isinstance(path, text_type):
|
|
|
|
path = text_type(path, 'mbcs')
|
2010-04-06 16:04:32 +00:00
|
|
|
if not op.isabs(path):
|
|
|
|
path = op.abspath(path)
|
2018-02-16 14:07:05 +00:00
|
|
|
path = get_short_path_name(path)
|
2010-10-17 17:00:56 +00:00
|
|
|
fileop = SHFILEOPSTRUCTW()
|
|
|
|
fileop.hwnd = 0
|
|
|
|
fileop.wFunc = FO_DELETE
|
2017-11-01 12:28:22 +00:00
|
|
|
# FIX: https://github.com/hsoft/send2trash/issues/17
|
|
|
|
# Starting in python 3.6.3 it is no longer possible to use:
|
|
|
|
# LPCWSTR(path + '\0') directly as embedded null characters are no longer
|
|
|
|
# allowed in strings
|
2018-02-16 14:07:05 +00:00
|
|
|
# Workaround
|
2017-11-01 12:28:22 +00:00
|
|
|
# - create buffer of c_wchar[] (LPCWSTR is based on this type)
|
|
|
|
# - buffer is two c_wchar characters longer (double null terminator)
|
|
|
|
# - cast the address of the buffer to a LPCWSTR
|
|
|
|
# NOTE: based on how python allocates memory for these types they should
|
|
|
|
# always be zero, if this is ever not true we can go back to explicitly
|
|
|
|
# setting the last two characters to null using buffer[index] = '\0'.
|
|
|
|
buffer = create_unicode_buffer(path, len(path)+2)
|
|
|
|
fileop.pFrom = LPCWSTR(addressof(buffer))
|
2010-10-17 17:00:56 +00:00
|
|
|
fileop.pTo = None
|
|
|
|
fileop.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
|
|
|
|
fileop.fAnyOperationsAborted = 0
|
|
|
|
fileop.hNameMappings = 0
|
|
|
|
fileop.lpszProgressTitle = None
|
|
|
|
result = SHFileOperationW(byref(fileop))
|
|
|
|
if result:
|
2019-04-30 16:28:09 +00:00
|
|
|
raise WindowsError(result, FormatError(result), path)
|