-
Notifications
You must be signed in to change notification settings - Fork 704
/
Copy pathfile_picker_windows.dart
272 lines (236 loc) · 9.22 KB
/
file_picker_windows.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import 'dart:ffi';
import 'dart:math';
import 'dart:typed_data';
import 'package:ffi/ffi.dart';
import 'package:file_picker/file_picker.dart';
import 'package:file_picker/src/utils.dart';
import 'package:file_picker/src/windows/file_picker_windows_ffi_types.dart';
import 'package:path/path.dart';
FilePicker filePickerWithFFI() => FilePickerWindows();
class FilePickerWindows extends FilePicker {
@override
Future<FilePickerResult?> pickFiles({
String? dialogTitle,
FileType type = FileType.any,
List<String>? allowedExtensions,
Function(FilePickerStatus)? onFileLoading,
bool allowCompression = true,
bool allowMultiple = false,
bool withData = false,
bool withReadStream = false,
}) async {
final comdlg32 = DynamicLibrary.open('comdlg32.dll');
final getOpenFileNameW =
comdlg32.lookupFunction<GetOpenFileNameW, GetOpenFileNameWDart>(
'GetOpenFileNameW');
final Pointer<OPENFILENAMEW> openFileNameW = _instantiateOpenFileNameW(
allowMultiple: allowMultiple,
allowedExtensions: allowedExtensions,
dialogTitle: dialogTitle,
type: type,
);
final result = getOpenFileNameW(openFileNameW);
FilePickerResult? returnValue;
if (result == 1) {
final filePaths = _extractSelectedFilesFromOpenFileNameW(
openFileNameW.ref,
);
final platformFiles = await filePathsToPlatformFiles(
filePaths,
withReadStream,
withData,
);
returnValue = FilePickerResult(platformFiles);
}
_freeMemory(openFileNameW);
return returnValue;
}
@override
Future<String?> getDirectoryPath({
String? dialogTitle,
}) {
final pathIdPointer = _pickDirectory(dialogTitle ?? defaultDialogTitle);
if (pathIdPointer == null) {
return Future.value(null);
}
return Future.value(
_getPathFromItemIdentifierList(pathIdPointer),
);
}
@override
Future<String?> saveFile({
String? dialogTitle,
String? fileName,
FileType type = FileType.any,
List<String>? allowedExtensions,
}) async {
final comdlg32 = DynamicLibrary.open('comdlg32.dll');
final getSaveFileNameW =
comdlg32.lookupFunction<GetSaveFileNameW, GetSaveFileNameWDart>(
'GetSaveFileNameW');
final Pointer<OPENFILENAMEW> openFileNameW = _instantiateOpenFileNameW(
allowedExtensions: allowedExtensions,
defaultFileName: fileName,
dialogTitle: dialogTitle,
type: type,
);
final result = getSaveFileNameW(openFileNameW);
String? returnValue;
if (result == 1) {
final filePaths = _extractSelectedFilesFromOpenFileNameW(
openFileNameW.ref,
);
returnValue = filePaths.first;
}
_freeMemory(openFileNameW);
return returnValue;
}
String fileTypeToFileFilter(FileType type, List<String>? allowedExtensions) {
switch (type) {
case FileType.any:
return 'All Files (*.*)\x00*.*\x00\x00';
case FileType.audio:
return 'Audios (*.aac,*.midi,*.mp3,*.ogg,*.wav)\x00*.aac;*.midi;*.mp3;*.ogg;*.wav\x00\x00';
case FileType.custom:
return 'Files (*.${allowedExtensions!.join(',*.')})\x00*.${allowedExtensions.join(';*.')}\x00\x00';
case FileType.image:
return 'Images (*.bmp,*.gif,*.jpeg,*.jpg,*.png)\x00*.bmp;*.gif;*.jpeg;*.jpg;*.png\x00\x00';
case FileType.media:
return 'Videos (*.avi,*.flv,*.mkv,*.mov,*.mp4,*.mpeg,*.webm,*.wmv)\x00*.avi;*.flv;*.mkv;*.mov;*.mp4;*.mpeg;*.webm;*.wmv\x00Images (*.bmp,*.gif,*.jpeg,*.jpg,*.png)\x00*.bmp;*.gif;*.jpeg;*.jpg;*.png\x00\x00';
case FileType.video:
return 'Videos (*.avi,*.flv,*.mkv,*.mov,*.mp4,*.mpeg,*.webm,*.wmv)\x00*.avi;*.flv;*.mkv;*.mov;*.mp4;*.mpeg;*.webm;*.wmv\x00\x00';
default:
throw Exception('unknown file type');
}
}
/// Uses the Win32 API to display a dialog box that enables the user to select a folder.
///
/// Returns a PIDL that specifies the location of the selected folder relative to the root of the
/// namespace. Returns null, if the user clicked on the "Cancel" button in the dialog box.
Pointer? _pickDirectory(String dialogTitle) {
final shell32 = DynamicLibrary.open('shell32.dll');
final shBrowseForFolderW =
shell32.lookupFunction<SHBrowseForFolderW, SHBrowseForFolderW>(
'SHBrowseForFolderW');
final Pointer<BROWSEINFOA> browseInfo = calloc<BROWSEINFOA>();
browseInfo.ref.hwndOwner = nullptr;
browseInfo.ref.pidlRoot = nullptr;
browseInfo.ref.pszDisplayName = calloc.allocate<Utf16>(maximumPathLength);
browseInfo.ref.lpszTitle = dialogTitle.toNativeUtf16();
browseInfo.ref.ulFlags =
bifEditBox | bifNewDialogStyle | bifReturnOnlyFsDirs;
final Pointer<NativeType> itemIdentifierList =
shBrowseForFolderW(browseInfo);
calloc.free(browseInfo.ref.pszDisplayName);
calloc.free(browseInfo.ref.lpszTitle);
calloc.free(browseInfo);
if (itemIdentifierList == nullptr) {
return null;
}
return itemIdentifierList;
}
/// Uses the Win32 API to convert an item identifier list to a file system path.
///
/// [lpItem] must contain the address of an item identifier list that specifies a
/// file or directory location relative to the root of the namespace (the desktop).
/// Returns the file system path as a [String]. Throws an exception, if the
/// conversion wasn't successful.
String _getPathFromItemIdentifierList(Pointer lpItem) {
final shell32 = DynamicLibrary.open('shell32.dll');
final shGetPathFromIDListW =
shell32.lookupFunction<SHGetPathFromIDListW, SHGetPathFromIDListWDart>(
'SHGetPathFromIDListW');
final Pointer<Utf16> pszPath = calloc.allocate<Utf16>(maximumPathLength);
final int result = shGetPathFromIDListW(lpItem, pszPath);
if (result == 0x00000000) {
throw Exception(
'Failed to convert item identifier list to a file system path.');
}
final path = pszPath.toDartString();
calloc.free(pszPath);
return path;
}
/// Extracts the list of selected files from the Win32 API struct [OPENFILENAMEW].
///
/// After the user has closed the file picker dialog, Win32 API sets the property
/// [lpstrFile] of [OPENFILENAMEW] to the user's selection. This property contains
/// a string terminated by two [null] characters. If the user has selected only one
/// file, then the returned string contains the absolute file path, e. g.
/// `C:\Users\John\file1.jpg\x00\x00`. If the user has selected more than one file,
/// then the returned string contains the directory of the selected files, followed
/// by a [null] character, followed by the file names each separated by a [null]
/// character, e.g. `C:\Users\John\x00file1.jpg\x00file2.jpg\x00file3.jpg\x00\x00`.
List<String> _extractSelectedFilesFromOpenFileNameW(
OPENFILENAMEW openFileNameW,
) {
final List<String> filePaths = [];
final buffer = StringBuffer();
int i = 0;
bool lastCharWasNull = false;
while (true) {
final char = openFileNameW.lpstrFile.cast<Uint16>().elementAt(i).value;
if (char == 0) {
if (lastCharWasNull) {
break;
} else {
filePaths.add(buffer.toString());
buffer.clear();
lastCharWasNull = true;
}
} else {
lastCharWasNull = false;
buffer.writeCharCode(char);
}
i++;
}
if (filePaths.length > 1) {
final String directoryPath = filePaths.removeAt(0);
return filePaths
.map<String>((filePath) => join(directoryPath, filePath))
.toList();
}
return filePaths;
}
Pointer<OPENFILENAMEW> _instantiateOpenFileNameW({
bool allowMultiple = false,
String? dialogTitle,
String? defaultFileName,
List<String>? allowedExtensions,
FileType type = FileType.any,
}) {
final lpstrFileBufferSize = 20 * maximumPathLength;
final Pointer<OPENFILENAMEW> openFileNameW = calloc<OPENFILENAMEW>();
openFileNameW.ref.lStructSize = sizeOf<OPENFILENAMEW>();
openFileNameW.ref.lpstrTitle =
(dialogTitle ?? defaultDialogTitle).toNativeUtf16();
openFileNameW.ref.lpstrFile = calloc.allocate<Utf16>(lpstrFileBufferSize);
openFileNameW.ref.lpstrFilter =
fileTypeToFileFilter(type, allowedExtensions).toNativeUtf16();
openFileNameW.ref.nMaxFile = lpstrFileBufferSize;
openFileNameW.ref.lpstrInitialDir = ''.toNativeUtf16();
openFileNameW.ref.flags = ofnExplorer | ofnFileMustExist | ofnHideReadOnly;
if (allowMultiple) {
openFileNameW.ref.flags |= ofnAllowMultiSelect;
}
if (defaultFileName != null) {
final Uint16List nativeString = openFileNameW.ref.lpstrFile
.cast<Uint16>()
.asTypedList(maximumPathLength);
final safeName = defaultFileName.substring(
0,
min(maximumPathLength - 1, defaultFileName.length),
);
final units = safeName.codeUnits;
nativeString.setRange(0, units.length, units);
nativeString[units.length] = 0;
}
return openFileNameW;
}
void _freeMemory(Pointer<OPENFILENAMEW> openFileNameW) {
calloc.free(openFileNameW.ref.lpstrTitle);
calloc.free(openFileNameW.ref.lpstrFile);
calloc.free(openFileNameW.ref.lpstrFilter);
calloc.free(openFileNameW.ref.lpstrInitialDir);
calloc.free(openFileNameW);
}
}