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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/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
|
|
|
|
|
2018-06-27 11:09:18 +00:00
|
|
|
#include <atlbase.h> // atlbase.h for CComPtr
|
2018-06-26 20:57:40 +00:00
|
|
|
|
2013-08-01 07:39:53 +00:00
|
|
|
#include <shlobj.h>
|
2018-06-27 11:09:18 +00:00
|
|
|
#include <shobjidl.h>
|
2013-08-01 07:39:53 +00:00
|
|
|
|
2014-07-21 14:10:35 +00:00
|
|
|
#include "atom/browser/native_window_views.h"
|
2016-07-11 06:29:03 +00:00
|
|
|
#include "atom/browser/unresponsive_suppressor.h"
|
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"
|
2014-12-16 19:27:38 +00:00
|
|
|
#include "base/threading/thread.h"
|
2017-01-26 07:10:28 +00:00
|
|
|
#include "base/threading/thread_task_runner_handle.h"
|
2013-08-01 08:17:11 +00:00
|
|
|
#include "base/win/registry.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);
|
|
|
|
for (size_t i = 0; i < filters.size(); ++i) {
|
|
|
|
const Filter& filter = filters[i];
|
2013-08-01 08:17:11 +00:00
|
|
|
|
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);
|
|
|
|
for (size_t j = 0; j < extensions.size(); ++j)
|
|
|
|
extensions[j].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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:27:38 +00:00
|
|
|
struct RunState {
|
|
|
|
base::Thread* dialog_thread;
|
2017-01-26 07:10:28 +00:00
|
|
|
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner;
|
2014-12-16 19:27:38 +00:00
|
|
|
};
|
|
|
|
|
2014-12-16 20:33:17 +00:00
|
|
|
bool CreateDialogThread(RunState* run_state) {
|
2018-06-18 07:32:55 +00:00
|
|
|
auto thread =
|
|
|
|
std::make_unique<base::Thread>(ATOM_PRODUCT_NAME "FileDialogThread");
|
2014-12-16 20:33:17 +00:00
|
|
|
thread->init_com_with_mta(false);
|
|
|
|
if (!thread->Start())
|
|
|
|
return false;
|
|
|
|
|
2015-07-08 06:12:11 +00:00
|
|
|
run_state->dialog_thread = thread.release();
|
2017-01-26 07:10:28 +00:00
|
|
|
run_state->ui_task_runner = base::ThreadTaskRunnerHandle::Get();
|
2014-12-16 20:33:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:27:38 +00:00
|
|
|
void RunOpenDialogInNewThread(const RunState& run_state,
|
2017-02-08 01:32:58 +00:00
|
|
|
const DialogSettings& settings,
|
2014-12-16 19:27:38 +00:00
|
|
|
const OpenDialogCallback& callback) {
|
|
|
|
std::vector<base::FilePath> paths;
|
2017-02-08 01:32:58 +00:00
|
|
|
bool result = ShowOpenDialog(settings, &paths);
|
2017-01-26 07:10:28 +00:00
|
|
|
run_state.ui_task_runner->PostTask(FROM_HERE,
|
2018-04-18 01:55:30 +00:00
|
|
|
base::Bind(callback, result, paths));
|
2017-01-26 07:10:28 +00:00
|
|
|
run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
|
2014-12-16 19:27:38 +00:00
|
|
|
}
|
|
|
|
|
2014-12-16 20:27:26 +00:00
|
|
|
void RunSaveDialogInNewThread(const RunState& run_state,
|
2017-02-08 01:32:58 +00:00
|
|
|
const DialogSettings& settings,
|
2014-12-16 20:27:26 +00:00
|
|
|
const SaveDialogCallback& callback) {
|
|
|
|
base::FilePath path;
|
2017-02-08 01:32:58 +00:00
|
|
|
bool result = ShowSaveDialog(settings, &path);
|
2017-01-26 07:10:28 +00:00
|
|
|
run_state.ui_task_runner->PostTask(FROM_HERE,
|
|
|
|
base::Bind(callback, result, path));
|
|
|
|
run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
|
2014-12-16 20:27:26 +00:00
|
|
|
}
|
|
|
|
|
2013-08-01 07:39:53 +00:00
|
|
|
} // namespace
|
|
|
|
|
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) {
|
2018-06-27 11:09:18 +00:00
|
|
|
assert(pShellItem != NULL);
|
|
|
|
|
|
|
|
LPWSTR lpstrName = NULL;
|
|
|
|
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
|
|
|
hRet = DISP_E_BUFFERTOOSMALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
::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;
|
|
|
|
HRESULT hr = SHCreateItemFromParsingName(directory.c_str(), NULL,
|
|
|
|
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) {
|
|
|
|
atom::UnresponsiveSuppressor suppressor;
|
|
|
|
HWND parent_window =
|
|
|
|
settings.parent_window
|
|
|
|
? static_cast<atom::NativeWindowViews*>(settings.parent_window)
|
|
|
|
->GetAcceleratedWidget()
|
|
|
|
: NULL;
|
|
|
|
|
2018-06-27 18:58:37 +00:00
|
|
|
return dialog->Show(parent_window);
|
2018-06-27 11:09:18 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 18:58:37 +00:00
|
|
|
static void ApplySettings(IFileDialog* dialog,
|
2018-06-27 11:09:18 +00:00
|
|
|
const DialogSettings& settings) {
|
|
|
|
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())
|
2018-06-27 18:58:37 +00:00
|
|
|
dialog->SetTitle(base::UTF8ToUTF16(settings.title).c_str());
|
2018-06-27 11:09:18 +00:00
|
|
|
|
|
|
|
if (!settings.button_label.empty())
|
2018-06-27 18:58:37 +00:00
|
|
|
dialog->SetOkButtonLabel(base::UTF8ToUTF16(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:32:58 +00:00
|
|
|
bool ShowOpenDialog(const DialogSettings& settings,
|
2013-07-05 02:13:09 +00:00
|
|
|
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;
|
2017-02-08 01:32:58 +00:00
|
|
|
if (settings.properties & FILE_DIALOG_OPEN_DIRECTORY)
|
2013-09-19 14:28:18 +00:00
|
|
|
options |= FOS_PICKFOLDERS;
|
2017-02-08 01:32:58 +00:00
|
|
|
if (settings.properties & FILE_DIALOG_MULTI_SELECTIONS)
|
2013-09-19 14:28:18 +00:00
|
|
|
options |= FOS_ALLOWMULTISELECT;
|
2017-02-08 01:32:58 +00:00
|
|
|
if (settings.properties & FILE_DIALOG_SHOW_HIDDEN_FILES)
|
2016-07-11 04:49:51 +00:00
|
|
|
options |= FOS_FORCESHOWHIDDEN;
|
2017-02-08 01:32:58 +00:00
|
|
|
if (settings.properties & FILE_DIALOG_PROMPT_TO_CREATE)
|
2017-02-02 16:30:02 +00:00
|
|
|
options |= FOS_CREATEPROMPT;
|
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];
|
2018-06-27 11:09:18 +00:00
|
|
|
hr = GetFileNameFromShellItem(item, SIGDN_FILESYSPATH, file_name, MAX_PATH);
|
|
|
|
|
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,
|
2013-09-24 06:47:39 +00:00
|
|
|
const OpenDialogCallback& callback) {
|
2014-12-16 20:33:17 +00:00
|
|
|
RunState run_state;
|
|
|
|
if (!CreateDialogThread(&run_state)) {
|
|
|
|
callback.Run(false, std::vector<base::FilePath>());
|
2014-12-16 19:27:38 +00:00
|
|
|
return;
|
2014-12-16 20:33:17 +00:00
|
|
|
}
|
2014-12-16 19:27:38 +00:00
|
|
|
|
2017-01-26 07:10:28 +00:00
|
|
|
run_state.dialog_thread->task_runner()->PostTask(
|
2014-12-16 19:27:38 +00:00
|
|
|
FROM_HERE,
|
2017-02-08 01:32:58 +00:00
|
|
|
base::Bind(&RunOpenDialogInNewThread, run_state, settings, callback));
|
2013-09-24 06:47:39 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
bool ShowSaveDialog(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;
|
|
|
|
|
2018-06-27 18:58:37 +00:00
|
|
|
file_save_dialog->SetOptions(FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST |
|
2018-06-27 11:09:18 +00:00
|
|
|
FOS_OVERWRITEPROMPT);
|
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,
|
2013-09-24 06:47:39 +00:00
|
|
|
const SaveDialogCallback& callback) {
|
2014-12-16 20:33:17 +00:00
|
|
|
RunState run_state;
|
|
|
|
if (!CreateDialogThread(&run_state)) {
|
|
|
|
callback.Run(false, base::FilePath());
|
2014-12-16 20:27:26 +00:00
|
|
|
return;
|
2014-12-16 20:33:17 +00:00
|
|
|
}
|
2014-12-16 20:27:26 +00:00
|
|
|
|
2017-01-26 07:10:28 +00:00
|
|
|
run_state.dialog_thread->task_runner()->PostTask(
|
2014-12-16 20:27:26 +00:00
|
|
|
FROM_HERE,
|
2017-02-08 01:32:58 +00:00
|
|
|
base::Bind(&RunSaveDialogInNewThread, run_state, settings, callback));
|
2013-09-24 06:47:39 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 02:13:09 +00:00
|
|
|
} // namespace file_dialog
|