2014-10-31 12:17:05 -06:00
|
|
|
// Copyright (c) 2013 GitHub, Inc.
|
2014-04-25 03:49:37 -06:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2013-07-04 20:13:09 -06:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/browser/ui/file_dialog.h"
|
2013-07-04 20:13:09 -06:00
|
|
|
|
2016-08-26 16:43:40 -06:00
|
|
|
#include <windows.h> // windows.h must be included first
|
|
|
|
|
2021-03-22 16:49:43 -06:00
|
|
|
#include "base/win/shlwapi.h" // NOLINT(build/include_order)
|
|
|
|
|
2021-03-24 11:53:07 -06:00
|
|
|
// atlbase.h for CComPtr
|
|
|
|
#include <atlbase.h> // NOLINT(build/include_order)
|
2018-06-26 14:57:40 -06:00
|
|
|
|
2021-03-24 11:53:07 -06:00
|
|
|
#include <shlobj.h> // NOLINT(build/include_order)
|
|
|
|
#include <shobjidl.h> // NOLINT(build/include_order)
|
2013-08-01 01:39:53 -06:00
|
|
|
|
2015-01-09 18:45:50 -07:00
|
|
|
#include "base/files/file_util.h"
|
2013-08-01 02:17:11 -06:00
|
|
|
#include "base/i18n/case_conversion.h"
|
|
|
|
#include "base/strings/string_split.h"
|
2016-08-26 16:30:02 -06:00
|
|
|
#include "base/strings/string_util.h"
|
2013-12-16 23:01:40 -07:00
|
|
|
#include "base/strings/utf_string_conversions.h"
|
2013-08-01 02:17:11 -06:00
|
|
|
#include "base/win/registry.h"
|
2019-06-19 14:46:59 -06:00
|
|
|
#include "shell/browser/native_window_views.h"
|
2020-05-12 11:37:51 -06:00
|
|
|
#include "shell/browser/ui/win/dialog_thread.h"
|
2019-09-05 23:52:54 -06:00
|
|
|
#include "shell/common/gin_converters/file_path_converter.h"
|
2018-06-26 14:57:40 -06:00
|
|
|
|
2013-07-04 20:13:09 -06:00
|
|
|
namespace file_dialog {
|
|
|
|
|
2018-04-17 17:37:22 -06:00
|
|
|
DialogSettings::DialogSettings() = default;
|
2018-06-25 14:30:00 -06:00
|
|
|
DialogSettings::DialogSettings(const DialogSettings&) = default;
|
2018-04-17 17:37:22 -06:00
|
|
|
DialogSettings::~DialogSettings() = default;
|
|
|
|
|
2013-08-01 01:39:53 -06:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Distinguish directories from regular files.
|
|
|
|
bool IsDirectory(const base::FilePath& path) {
|
2014-07-10 22:19:01 -06:00
|
|
|
base::File::Info file_info;
|
2018-04-17 19:55:30 -06:00
|
|
|
return base::GetFileInfo(path, &file_info) ? file_info.is_directory
|
|
|
|
: path.EndsWithSeparator();
|
2013-08-01 01:39:53 -06:00
|
|
|
}
|
|
|
|
|
2014-08-06 07:51:36 -06:00
|
|
|
void ConvertFilters(const Filters& filters,
|
|
|
|
std::vector<std::wstring>* buffer,
|
|
|
|
std::vector<COMDLG_FILTERSPEC>* filterspec) {
|
|
|
|
if (filters.empty()) {
|
2018-04-17 19:55:30 -06:00
|
|
|
COMDLG_FILTERSPEC spec = {L"All Files (*.*)", L"*.*"};
|
2014-08-06 07:51:36 -06:00
|
|
|
filterspec->push_back(spec);
|
|
|
|
return;
|
2013-08-01 02:17:11 -06:00
|
|
|
}
|
|
|
|
|
2014-08-06 07:51:36 -06:00
|
|
|
buffer->reserve(filters.size() * 2);
|
2019-09-13 08:26:59 -06:00
|
|
|
for (const Filter& filter : filters) {
|
2014-08-06 07:51:36 -06:00
|
|
|
COMDLG_FILTERSPEC spec;
|
|
|
|
buffer->push_back(base::UTF8ToWide(filter.first));
|
|
|
|
spec.pszName = buffer->back().c_str();
|
2013-09-18 06:28:56 -06:00
|
|
|
|
2014-08-06 07:51:36 -06:00
|
|
|
std::vector<std::string> extensions(filter.second);
|
2019-09-13 08:26:59 -06:00
|
|
|
for (std::string& extension : extensions)
|
|
|
|
extension.insert(0, "*.");
|
2015-12-07 11:02:06 -07:00
|
|
|
buffer->push_back(base::UTF8ToWide(base::JoinString(extensions, ";")));
|
2014-08-06 07:51:36 -06:00
|
|
|
spec.pszSpec = buffer->back().c_str();
|
2013-08-01 02:17:11 -06:00
|
|
|
|
2014-08-06 07:51:36 -06:00
|
|
|
filterspec->push_back(spec);
|
2013-08-01 02:17:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 05:09:18 -06:00
|
|
|
static HRESULT GetFileNameFromShellItem(IShellItem* pShellItem,
|
|
|
|
SIGDN type,
|
|
|
|
LPWSTR lpstr,
|
2018-06-27 08:28:12 -06:00
|
|
|
size_t cchLength) {
|
2023-10-03 13:26:35 -06:00
|
|
|
assert(pShellItem != nullptr);
|
2018-06-27 05:09:18 -06:00
|
|
|
|
2023-10-03 13:26:35 -06:00
|
|
|
LPWSTR lpstrName = nullptr;
|
2018-06-27 05:09:18 -06:00
|
|
|
HRESULT hRet = pShellItem->GetDisplayName(type, &lpstrName);
|
|
|
|
|
|
|
|
if (SUCCEEDED(hRet)) {
|
|
|
|
if (wcslen(lpstrName) < cchLength) {
|
|
|
|
wcscpy_s(lpstr, cchLength, lpstrName);
|
|
|
|
} else {
|
2018-06-27 08:28:12 -06:00
|
|
|
NOTREACHED();
|
2018-06-27 05:09:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
::CoTaskMemFree(lpstrName);
|
|
|
|
}
|
|
|
|
|
|
|
|
return hRet;
|
|
|
|
}
|
|
|
|
|
2018-06-27 12:58:37 -06:00
|
|
|
static void SetDefaultFolder(IFileDialog* dialog,
|
2018-06-27 05:09:18 -06:00
|
|
|
const base::FilePath file_path) {
|
|
|
|
std::wstring directory =
|
|
|
|
IsDirectory(file_path) ? file_path.value() : file_path.DirName().value();
|
|
|
|
|
|
|
|
ATL::CComPtr<IShellItem> folder_item;
|
2023-10-03 13:26:35 -06:00
|
|
|
HRESULT hr = SHCreateItemFromParsingName(directory.c_str(), nullptr,
|
2018-06-27 05:09:18 -06:00
|
|
|
IID_PPV_ARGS(&folder_item));
|
|
|
|
if (SUCCEEDED(hr))
|
2018-06-27 12:58:37 -06:00
|
|
|
dialog->SetFolder(folder_item);
|
2018-06-27 05:09:18 -06:00
|
|
|
}
|
|
|
|
|
2018-06-27 12:58:37 -06:00
|
|
|
static HRESULT ShowFileDialog(IFileDialog* dialog,
|
2018-06-27 05:09:18 -06:00
|
|
|
const DialogSettings& settings) {
|
|
|
|
HWND parent_window =
|
|
|
|
settings.parent_window
|
2019-06-19 15:23:04 -06:00
|
|
|
? static_cast<electron::NativeWindowViews*>(settings.parent_window)
|
2018-06-27 05:09:18 -06:00
|
|
|
->GetAcceleratedWidget()
|
2023-10-03 13:26:35 -06:00
|
|
|
: nullptr;
|
2018-06-27 05:09:18 -06:00
|
|
|
|
2018-06-27 12:58:37 -06:00
|
|
|
return dialog->Show(parent_window);
|
2018-06-27 05:09:18 -06:00
|
|
|
}
|
|
|
|
|
2018-09-19 05:10:26 -06:00
|
|
|
static void ApplySettings(IFileDialog* dialog, const DialogSettings& settings) {
|
2018-06-27 05:09:18 -06:00
|
|
|
std::wstring file_part;
|
|
|
|
|
|
|
|
if (!IsDirectory(settings.default_path))
|
|
|
|
file_part = settings.default_path.BaseName().value();
|
|
|
|
|
2018-06-27 12:58:37 -06:00
|
|
|
dialog->SetFileName(file_part.c_str());
|
2018-06-27 05:09:18 -06:00
|
|
|
|
|
|
|
if (!settings.title.empty())
|
2021-03-18 13:55:51 -06:00
|
|
|
dialog->SetTitle(base::UTF8ToWide(settings.title).c_str());
|
2018-06-27 05:09:18 -06:00
|
|
|
|
|
|
|
if (!settings.button_label.empty())
|
2021-03-18 13:55:51 -06:00
|
|
|
dialog->SetOkButtonLabel(base::UTF8ToWide(settings.button_label).c_str());
|
2018-06-27 05:09:18 -06:00
|
|
|
|
|
|
|
std::vector<std::wstring> buffer;
|
|
|
|
std::vector<COMDLG_FILTERSPEC> filterspec;
|
|
|
|
ConvertFilters(settings.filters, &buffer, &filterspec);
|
|
|
|
|
|
|
|
if (!filterspec.empty()) {
|
2018-06-27 12:58:37 -06:00
|
|
|
dialog->SetFileTypes(filterspec.size(), filterspec.data());
|
2018-06-27 05:09:18 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// By default, *.* will be added to the file name if file type is "*.*". In
|
|
|
|
// Electron, we disable it to make a better experience.
|
|
|
|
//
|
|
|
|
// From MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/
|
|
|
|
// bb775970(v=vs.85).aspx
|
|
|
|
//
|
|
|
|
// If SetDefaultExtension is not called, the dialog will not update
|
|
|
|
// automatically when user choose a new file type in the file dialog.
|
|
|
|
//
|
|
|
|
// We set file extension to the first none-wildcard extension to make
|
|
|
|
// sure the dialog will update file extension automatically.
|
|
|
|
for (size_t i = 0; i < filterspec.size(); ++i) {
|
|
|
|
if (std::wstring(filterspec[i].pszSpec) != L"*.*") {
|
|
|
|
// SetFileTypeIndex is regarded as one-based index.
|
2018-06-27 12:58:37 -06:00
|
|
|
dialog->SetFileTypeIndex(i + 1);
|
|
|
|
dialog->SetDefaultExtension(filterspec[i].pszSpec);
|
2018-06-27 05:09:18 -06:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings.default_path.IsAbsolute()) {
|
2018-06-27 12:58:37 -06:00
|
|
|
SetDefaultFolder(dialog, settings.default_path);
|
2018-06-27 05:09:18 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 11:37:51 -06:00
|
|
|
} // namespace
|
|
|
|
|
2019-03-05 06:54:48 -07:00
|
|
|
bool ShowOpenDialogSync(const DialogSettings& settings,
|
|
|
|
std::vector<base::FilePath>* paths) {
|
2018-06-27 12:58:37 -06:00
|
|
|
ATL::CComPtr<IFileOpenDialog> file_open_dialog;
|
|
|
|
HRESULT hr = file_open_dialog.CoCreateInstance(CLSID_FileOpenDialog);
|
2018-06-27 05:09:18 -06:00
|
|
|
|
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
DWORD options = FOS_FORCEFILESYSTEM | FOS_FILEMUSTEXIST;
|
2019-08-13 14:40:07 -06:00
|
|
|
if (settings.properties & OPEN_DIALOG_OPEN_DIRECTORY)
|
2013-09-19 08:28:18 -06:00
|
|
|
options |= FOS_PICKFOLDERS;
|
2019-08-13 14:40:07 -06:00
|
|
|
if (settings.properties & OPEN_DIALOG_MULTI_SELECTIONS)
|
2013-09-19 08:28:18 -06:00
|
|
|
options |= FOS_ALLOWMULTISELECT;
|
2019-08-13 14:40:07 -06:00
|
|
|
if (settings.properties & OPEN_DIALOG_SHOW_HIDDEN_FILES)
|
2016-07-10 22:49:51 -06:00
|
|
|
options |= FOS_FORCESHOWHIDDEN;
|
2019-08-13 14:40:07 -06:00
|
|
|
if (settings.properties & OPEN_DIALOG_PROMPT_TO_CREATE)
|
2017-02-02 09:30:02 -07:00
|
|
|
options |= FOS_CREATEPROMPT;
|
2019-08-13 09:48:22 -06:00
|
|
|
if (settings.properties & FILE_DIALOG_DONT_ADD_TO_RECENT)
|
|
|
|
options |= FOS_DONTADDTORECENT;
|
2018-06-27 12:58:37 -06:00
|
|
|
file_open_dialog->SetOptions(options);
|
2013-09-19 08:28:18 -06:00
|
|
|
|
2018-06-27 12:58:37 -06:00
|
|
|
ApplySettings(file_open_dialog, settings);
|
|
|
|
hr = ShowFileDialog(file_open_dialog, settings);
|
2018-06-27 05:09:18 -06:00
|
|
|
if (FAILED(hr))
|
2013-09-19 08:28:18 -06:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ATL::CComPtr<IShellItemArray> items;
|
2018-06-27 12:58:37 -06:00
|
|
|
hr = file_open_dialog->GetResults(&items);
|
2013-09-19 08:28:18 -06:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ATL::CComPtr<IShellItem> item;
|
|
|
|
DWORD count = 0;
|
|
|
|
hr = items->GetCount(&count);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
paths->reserve(count);
|
|
|
|
for (DWORD i = 0; i < count; ++i) {
|
|
|
|
hr = items->GetItemAt(i, &item);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
wchar_t file_name[MAX_PATH];
|
2019-05-07 07:19:14 -06:00
|
|
|
hr = GetFileNameFromShellItem(item, SIGDN_FILESYSPATH, file_name,
|
2022-03-24 19:39:03 -06:00
|
|
|
std::size(file_name));
|
2018-06-27 05:09:18 -06:00
|
|
|
|
2013-09-19 08:28:18 -06:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
paths->push_back(base::FilePath(file_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-07-04 20:13:09 -06:00
|
|
|
}
|
|
|
|
|
2017-02-07 18:32:58 -07:00
|
|
|
void ShowOpenDialog(const DialogSettings& settings,
|
2019-11-01 00:10:32 -06:00
|
|
|
gin_helper::Promise<gin_helper::Dictionary> promise) {
|
2020-05-12 11:37:51 -06:00
|
|
|
auto done = [](gin_helper::Promise<gin_helper::Dictionary> promise,
|
|
|
|
bool success, std::vector<base::FilePath> result) {
|
|
|
|
v8::HandleScope handle_scope(promise.isolate());
|
2023-10-10 04:45:44 -06:00
|
|
|
auto dict = gin::Dictionary::CreateEmpty(promise.isolate());
|
2020-05-12 11:37:51 -06:00
|
|
|
dict.Set("canceled", !success);
|
|
|
|
dict.Set("filePaths", result);
|
2019-11-01 00:10:32 -06:00
|
|
|
promise.Resolve(dict);
|
2020-05-12 11:37:51 -06:00
|
|
|
};
|
|
|
|
dialog_thread::Run(base::BindOnce(ShowOpenDialogSync, settings),
|
|
|
|
base::BindOnce(done, std::move(promise)));
|
2013-09-24 00:47:39 -06:00
|
|
|
}
|
|
|
|
|
2019-03-05 14:48:20 -07:00
|
|
|
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
|
2018-06-27 12:58:37 -06:00
|
|
|
ATL::CComPtr<IFileSaveDialog> file_save_dialog;
|
|
|
|
HRESULT hr = file_save_dialog.CoCreateInstance(CLSID_FileSaveDialog);
|
2018-06-27 05:09:18 -06:00
|
|
|
if (FAILED(hr))
|
2013-09-18 06:28:56 -06:00
|
|
|
return false;
|
|
|
|
|
2019-08-13 14:40:07 -06:00
|
|
|
DWORD options = FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT;
|
|
|
|
if (settings.properties & SAVE_DIALOG_SHOW_HIDDEN_FILES)
|
|
|
|
options |= FOS_FORCESHOWHIDDEN;
|
|
|
|
if (settings.properties & SAVE_DIALOG_DONT_ADD_TO_RECENT)
|
|
|
|
options |= FOS_DONTADDTORECENT;
|
|
|
|
|
|
|
|
file_save_dialog->SetOptions(options);
|
2018-06-27 12:58:37 -06:00
|
|
|
ApplySettings(file_save_dialog, settings);
|
|
|
|
hr = ShowFileDialog(file_save_dialog, settings);
|
2018-06-27 05:09:18 -06:00
|
|
|
|
2013-09-18 06:28:56 -06:00
|
|
|
if (FAILED(hr))
|
2013-08-01 01:39:53 -06:00
|
|
|
return false;
|
|
|
|
|
2018-06-27 05:09:18 -06:00
|
|
|
CComPtr<IShellItem> pItem;
|
2018-06-27 12:58:37 -06:00
|
|
|
hr = file_save_dialog->GetResult(&pItem);
|
2018-06-27 05:09:18 -06:00
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
PWSTR result_path = nullptr;
|
|
|
|
hr = pItem->GetDisplayName(SIGDN_FILESYSPATH, &result_path);
|
|
|
|
if (!SUCCEEDED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
*path = base::FilePath(result_path);
|
|
|
|
CoTaskMemFree(result_path);
|
|
|
|
|
2013-08-01 01:39:53 -06:00
|
|
|
return true;
|
2013-07-04 20:13:09 -06:00
|
|
|
}
|
|
|
|
|
2017-02-07 18:32:58 -07:00
|
|
|
void ShowSaveDialog(const DialogSettings& settings,
|
2019-11-01 00:10:32 -06:00
|
|
|
gin_helper::Promise<gin_helper::Dictionary> promise) {
|
2020-05-12 11:37:51 -06:00
|
|
|
auto done = [](gin_helper::Promise<gin_helper::Dictionary> promise,
|
|
|
|
bool success, base::FilePath result) {
|
|
|
|
v8::HandleScope handle_scope(promise.isolate());
|
2023-10-10 04:45:44 -06:00
|
|
|
auto dict = gin::Dictionary::CreateEmpty(promise.isolate());
|
2020-05-12 11:37:51 -06:00
|
|
|
dict.Set("canceled", !success);
|
|
|
|
dict.Set("filePath", result);
|
2019-11-01 00:10:32 -06:00
|
|
|
promise.Resolve(dict);
|
2020-05-12 11:37:51 -06:00
|
|
|
};
|
|
|
|
dialog_thread::Run(base::BindOnce(ShowSaveDialogSync, settings),
|
|
|
|
base::BindOnce(done, std::move(promise)));
|
2013-09-24 00:47:39 -06:00
|
|
|
}
|
|
|
|
|
2013-07-04 20:13:09 -06:00
|
|
|
} // namespace file_dialog
|