electron/atom/browser/ui/file_dialog_win.cc

280 lines
9 KiB
C++
Raw Normal View History

// 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
// found in the LICENSE file.
2014-03-16 00:30:26 +00:00
#include "atom/browser/ui/file_dialog.h"
2016-08-26 22:43:40 +00:00
#include <windows.h> // windows.h must be included first
#include <atlbase.h> // atlbase.h must be included before atlapp.h
#include <atlapp.h>
#include <atldlgs.h>
2013-08-01 07:39:53 +00:00
#include <commdlg.h>
#include <shlobj.h>
2014-07-21 14:10:35 +00:00
#include "atom/browser/native_window_views.h"
#include "atom/browser/unresponsive_suppressor.h"
#include "base/files/file_util.h"
#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"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread.h"
2017-01-26 07:10:28 +00:00
#include "base/threading/thread_task_runner_handle.h"
#include "base/win/registry.h"
using WTL::CShellFileOpenDialog;
using WTL::CShellFileSaveDialog;
2013-08-01 07:39:53 +00:00
namespace file_dialog {
DialogSettings::DialogSettings() = default;
DialogSettings::DialogSettings(const DialogSettings&) = default;
DialogSettings::~DialogSettings() = default;
2013-08-01 07:39:53 +00:00
namespace {
// Distinguish directories from regular files.
bool IsDirectory(const base::FilePath& path) {
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;
}
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];
2014-08-06 13:51:36 +00:00
COMDLG_FILTERSPEC spec;
buffer->push_back(base::UTF8ToWide(filter.first));
spec.pszName = buffer->back().c_str();
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();
2014-08-06 13:51:36 +00:00
filterspec->push_back(spec);
}
}
// Generic class to delegate common open/save dialog's behaviours, users need to
// call interface methods via GetPtr().
template <typename T>
class FileDialog {
public:
2017-02-08 01:32:58 +00:00
FileDialog(const DialogSettings& settings, int options) {
std::wstring file_part;
2017-02-08 01:32:58 +00:00
if (!IsDirectory(settings.default_path))
file_part = settings.default_path.BaseName().value();
2014-08-06 13:51:36 +00:00
std::vector<std::wstring> buffer;
std::vector<COMDLG_FILTERSPEC> filterspec;
2017-02-08 01:32:58 +00:00
ConvertFilters(settings.filters, &buffer, &filterspec);
2014-08-06 13:51:36 +00:00
2018-04-18 01:55:30 +00:00
dialog_.reset(new T(file_part.c_str(), options, NULL, filterspec.data(),
filterspec.size()));
2017-02-08 01:32:58 +00:00
if (!settings.title.empty())
GetPtr()->SetTitle(base::UTF8ToUTF16(settings.title).c_str());
2017-02-08 01:32:58 +00:00
if (!settings.button_label.empty())
GetPtr()->SetOkButtonLabel(
base::UTF8ToUTF16(settings.button_label).c_str());
// 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-04-18 01:55:30 +00:00
GetPtr()->SetFileTypeIndex(i + 1);
GetPtr()->SetDefaultExtension(filterspec[i].pszSpec);
break;
}
}
if (settings.default_path.IsAbsolute()) {
SetDefaultFolder(settings.default_path);
}
}
bool Show(atom::NativeWindow* parent_window) {
atom::UnresponsiveSuppressor suppressor;
2018-04-18 01:55:30 +00:00
HWND window = parent_window
? static_cast<atom::NativeWindowViews*>(parent_window)
->GetAcceleratedWidget()
: NULL;
2013-09-19 14:28:18 +00:00
return dialog_->DoModal(window) == IDOK;
}
T* GetDialog() { return dialog_.get(); }
IFileDialog* GetPtr() const { return dialog_->GetPtr(); }
private:
// Set up the initial directory for the dialog.
void SetDefaultFolder(const base::FilePath file_path) {
2018-04-18 01:55:30 +00:00
std::wstring directory = IsDirectory(file_path)
? file_path.value()
: file_path.DirName().value();
ATL::CComPtr<IShellItem> folder_item;
2018-04-18 01:55:30 +00:00
HRESULT hr = SHCreateItemFromParsingName(directory.c_str(), NULL,
IID_PPV_ARGS(&folder_item));
if (SUCCEEDED(hr))
GetPtr()->SetFolder(folder_item);
}
2016-05-23 01:59:39 +00:00
std::unique_ptr<T> dialog_;
DISALLOW_COPY_AND_ASSIGN(FileDialog);
};
struct RunState {
base::Thread* dialog_thread;
2017-01-26 07:10:28 +00:00
scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner;
};
bool CreateDialogThread(RunState* run_state) {
auto thread =
std::make_unique<base::Thread>(ATOM_PRODUCT_NAME "FileDialogThread");
thread->init_com_with_mta(false);
if (!thread->Start())
return false;
run_state->dialog_thread = thread.release();
2017-01-26 07:10:28 +00:00
run_state->ui_task_runner = base::ThreadTaskRunnerHandle::Get();
return true;
}
void RunOpenDialogInNewThread(const RunState& run_state,
2017-02-08 01:32:58 +00:00
const DialogSettings& settings,
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);
}
void RunSaveDialogInNewThread(const RunState& run_state,
2017-02-08 01:32:58 +00:00
const DialogSettings& settings,
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);
}
2013-08-01 07:39:53 +00:00
} // namespace
2017-02-08 01:32:58 +00:00
bool ShowOpenDialog(const DialogSettings& settings,
std::vector<base::FilePath>* paths) {
2013-09-19 14:28:18 +00:00
int 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)
options |= FOS_FORCESHOWHIDDEN;
2017-02-08 01:32:58 +00:00
if (settings.properties & FILE_DIALOG_PROMPT_TO_CREATE)
options |= FOS_CREATEPROMPT;
2013-09-19 14:28:18 +00:00
2017-02-08 01:32:58 +00:00
FileDialog<CShellFileOpenDialog> open_dialog(settings, options);
if (!open_dialog.Show(settings.parent_window))
2013-09-19 14:28:18 +00:00
return false;
ATL::CComPtr<IShellItemArray> items;
2018-04-18 01:55:30 +00:00
HRESULT hr =
static_cast<IFileOpenDialog*>(open_dialog.GetPtr())->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-04-18 01:55:30 +00:00
hr = CShellFileOpenDialog::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;
}
2017-02-08 01:32:58 +00:00
void ShowOpenDialog(const DialogSettings& settings,
const OpenDialogCallback& callback) {
RunState run_state;
if (!CreateDialogThread(&run_state)) {
callback.Run(false, std::vector<base::FilePath>());
return;
}
2017-01-26 07:10:28 +00:00
run_state.dialog_thread->task_runner()->PostTask(
FROM_HERE,
2017-02-08 01:32:58 +00:00
base::Bind(&RunOpenDialogInNewThread, run_state, settings, callback));
}
2018-04-18 01:55:30 +00:00
bool ShowSaveDialog(const DialogSettings& settings, base::FilePath* path) {
FileDialog<CShellFileSaveDialog> save_dialog(
2017-02-08 01:32:58 +00:00
settings, FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT);
if (!save_dialog.Show(settings.parent_window))
return false;
2014-08-06 13:51:36 +00:00
wchar_t buffer[MAX_PATH];
HRESULT hr = save_dialog.GetDialog()->GetFilePath(buffer, MAX_PATH);
if (FAILED(hr))
2013-08-01 07:39:53 +00:00
return false;
*path = base::FilePath(buffer);
2013-08-01 07:39:53 +00:00
return true;
}
2017-02-08 01:32:58 +00:00
void ShowSaveDialog(const DialogSettings& settings,
const SaveDialogCallback& callback) {
RunState run_state;
if (!CreateDialogThread(&run_state)) {
callback.Run(false, base::FilePath());
return;
}
2017-01-26 07:10:28 +00:00
run_state.dialog_thread->task_runner()->PostTask(
FROM_HERE,
2017-02-08 01:32:58 +00:00
base::Bind(&RunSaveDialogInNewThread, run_state, settings, callback));
}
} // namespace file_dialog