~jr/bzr-explorer/unstable

« back to all changes in this revision

Viewing changes to thirdparty/send2trash/plat_osx.py

  • Committer: Jonathan Riddell
  • Date: 2011-06-10 13:30:56 UTC
  • mfrom: (361.1.1 bzr-explorer-unstable)
  • Revision ID: jriddell@canonical.com-20110610133056-zrr83g9yoq4ogjby
merge in 1.1.3 from upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
 
2
 
 
3
# This software is licensed under the "BSD" License as described in the "LICENSE" file, 
 
4
# which should be included with this package. The terms are also available at 
 
5
# http://www.hardcoded.net/licenses/bsd_license
 
6
 
 
7
from ctypes import cdll, byref, Structure, c_char, c_char_p
 
8
from ctypes.util import find_library
 
9
 
 
10
Foundation = cdll.LoadLibrary(find_library(u'Foundation'))
 
11
CoreServices = cdll.LoadLibrary(find_library(u'CoreServices'))
 
12
 
 
13
GetMacOSStatusCommentString = Foundation.GetMacOSStatusCommentString
 
14
GetMacOSStatusCommentString.restype = c_char_p
 
15
FSPathMakeRefWithOptions = CoreServices.FSPathMakeRefWithOptions
 
16
FSMoveObjectToTrashSync = CoreServices.FSMoveObjectToTrashSync
 
17
 
 
18
kFSPathMakeRefDefaultOptions = 0
 
19
kFSPathMakeRefDoNotFollowLeafSymlink = 0x01
 
20
 
 
21
kFSFileOperationDefaultOptions = 0
 
22
kFSFileOperationOverwrite = 0x01
 
23
kFSFileOperationSkipSourcePermissionErrors = 0x02
 
24
kFSFileOperationDoNotMoveAcrossVolumes = 0x04
 
25
kFSFileOperationSkipPreflight = 0x08
 
26
 
 
27
class FSRef(Structure):
 
28
    _fields_ = [(u'hidden', c_char * 80)]
 
29
 
 
30
def check_op_result(op_result):
 
31
    if op_result:
 
32
        msg = GetMacOSStatusCommentString(op_result).decode(u'utf-8')
 
33
        raise OSError(msg)
 
34
 
 
35
def send2trash(path):
 
36
    if not isinstance(path, str):
 
37
        path = path.encode(u'utf-8')
 
38
    fp = FSRef()
 
39
    opts = kFSPathMakeRefDoNotFollowLeafSymlink
 
40
    op_result = FSPathMakeRefWithOptions(path, opts, byref(fp), None)
 
41
    check_op_result(op_result)
 
42
    opts = kFSFileOperationDefaultOptions
 
43
    op_result = FSMoveObjectToTrashSync(byref(fp), None, opts)
 
44
    check_op_result(op_result)