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