| 1 |
''' |
| 2 |
Windows file chooser |
| 3 |
-------------------- |
| 4 |
''' |
| 5 |
|
| 6 |
from plyer.facades import FileChooser |
| 7 |
from win32com.shell.shell import ( |
| 8 |
SHBrowseForFolder as browse, |
| 9 |
SHGetPathFromIDList as get_path |
| 10 |
) |
| 11 |
from win32com.shell import shellcon |
| 12 |
import win32gui |
| 13 |
import win32con |
| 14 |
import pywintypes |
| 15 |
from os.path import dirname, splitext, join, isdir |
| 16 |
|
| 17 |
|
| 18 |
class Win32FileChooser: |
| 19 |
'''A native implementation of NativeFileChooser using the |
| 20 |
Win32 API on Windows. |
| 21 |
|
| 22 |
Not Implemented features (all dialogs): |
| 23 |
* preview |
| 24 |
* icon |
| 25 |
|
| 26 |
Not implemented features (in directory selection only - it's limited |
| 27 |
by Windows itself): |
| 28 |
* preview |
| 29 |
* window-icon |
| 30 |
|
| 31 |
Known issues: |
| 32 |
* non-existins folders such as: Network, Control Panel, My Computer, Trash, |
| 33 |
Library and likes will raise a COM error. The path does not exist, nor |
| 34 |
a user can open from or save to such path. |
| 35 |
''' |
| 36 |
|
| 37 |
path = None |
| 38 |
multiple = False |
| 39 |
filters = [] |
| 40 |
preview = False |
| 41 |
title = None |
| 42 |
icon = None |
| 43 |
show_hidden = False |
| 44 |
|
| 45 |
def __init__(self, *args, **kwargs): |
| 46 |
self._handle_selection = kwargs.pop( |
| 47 |
'on_selection', self._handle_selection |
| 48 |
) |
| 49 |
|
| 50 |
|
| 51 |
for i in kwargs: |
| 52 |
setattr(self, i, kwargs[i]) |
| 53 |
|
| 54 |
@staticmethod |
| 55 |
def _handle_selection(selection): |
| 56 |
''' |
| 57 |
Dummy placeholder for returning selection from chooser. |
| 58 |
''' |
| 59 |
return selection |
| 60 |
|
| 61 |
def run(self): |
| 62 |
self.selection = [] |
| 63 |
try: |
| 64 |
if self.mode != "dir": |
| 65 |
args = {} |
| 66 |
|
| 67 |
if self.path: |
| 68 |
if isdir(self.path): |
| 69 |
args["InitialDir"] = self.path |
| 70 |
else: |
| 71 |
args["InitialDir"] = dirname(self.path) |
| 72 |
_, ext = splitext(self.path) |
| 73 |
args["File"] = self.path |
| 74 |
args["DefExt"] = ext and ext[1:] |
| 75 |
|
| 76 |
args["Title"] = self.title if self.title else "Pick a file..." |
| 77 |
args["CustomFilter"] = 'Other file types\x00*.*\x00' |
| 78 |
args["FilterIndex"] = 1 |
| 79 |
file = "" |
| 80 |
if "File" in args: |
| 81 |
file = args["File"] |
| 82 |
args["File"] = file + ("\x00" * 4096) |
| 83 |
|
| 84 |
|
| 85 |
filters = "" |
| 86 |
for f in self.filters: |
| 87 |
if type(f) == str: |
| 88 |
filters += (f + "\x00") * 2 |
| 89 |
else: |
| 90 |
filters += f[0] + "\x00" + ";".join(f[1:]) + "\x00" |
| 91 |
args["Filter"] = filters |
| 92 |
|
| 93 |
flags = win32con.OFN_OVERWRITEPROMPT |
| 94 |
flags |= win32con.OFN_HIDEREADONLY |
| 95 |
|
| 96 |
if self.multiple: |
| 97 |
flags |= win32con.OFN_ALLOWMULTISELECT |
| 98 |
flags |= win32con.OFN_EXPLORER |
| 99 |
if self.show_hidden: |
| 100 |
flags |= win32con.OFN_FORCESHOWHIDDEN |
| 101 |
|
| 102 |
args["Flags"] = flags |
| 103 |
|
| 104 |
try: |
| 105 |
if self.mode == "open": |
| 106 |
self.fname, _, _ = win32gui.GetOpenFileNameW(**args) |
| 107 |
elif self.mode == "save": |
| 108 |
self.fname, _, _ = win32gui.GetSaveFileNameW(**args) |
| 109 |
except pywintypes.error as e: |
| 110 |
|
| 111 |
if not e.winerror: |
| 112 |
self._handle_selection(self.selection) |
| 113 |
return self.selection |
| 114 |
raise |
| 115 |
|
| 116 |
if self.fname: |
| 117 |
if self.multiple: |
| 118 |
seq = str(self.fname).split("\x00") |
| 119 |
if len(seq) > 1: |
| 120 |
dir_n, base_n = seq[0], seq[1:] |
| 121 |
self.selection = [ |
| 122 |
join(dir_n, i) for i in base_n |
| 123 |
] |
| 124 |
else: |
| 125 |
self.selection = seq |
| 126 |
else: |
| 127 |
self.selection = str(self.fname).split("\x00") |
| 128 |
|
| 129 |
else: |
| 130 |
BIF_EDITBOX = shellcon.BIF_EDITBOX |
| 131 |
BIF_NEWDIALOGSTYLE = 0x00000040 |
| 132 |
|
| 133 |
pidl, name, images = browse( |
| 134 |
win32gui.GetDesktopWindow(), |
| 135 |
None, |
| 136 |
self.title if self.title else "Pick a folder...", |
| 137 |
BIF_NEWDIALOGSTYLE | BIF_EDITBOX, None, None |
| 138 |
) |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
if pidl: |
| 143 |
self.selection = [str(get_path(pidl).decode('utf-8'))] |
| 144 |
|
| 145 |
except (RuntimeError, pywintypes.error, Exception): |
| 146 |
|
| 147 |
import traceback |
| 148 |
traceback.print_exc() |
| 149 |
self._handle_selection(self.selection) |
| 150 |
return self.selection |
| 151 |
|
| 152 |
|
| 153 |
class WinFileChooser(FileChooser): |
| 154 |
'''FileChooser implementation for Windows, using win3all. |
| 155 |
''' |
| 156 |
|
| 157 |
def _file_selection_dialog(self, **kwargs): |
| 158 |
return Win32FileChooser(**kwargs).run() |
| 159 |
|
| 160 |
|
| 161 |
def instance(): |
| 162 |
return WinFileChooser() |