win: Implement the filters option.
This commit is contained in:
parent
47e0a61dd8
commit
fe9f94555b
2 changed files with 53 additions and 148 deletions
|
@ -53,7 +53,7 @@ module.exports =
|
||||||
options ?= title: 'Save'
|
options ?= title: 'Save'
|
||||||
options.title ?= ''
|
options.title ?= ''
|
||||||
options.defaultPath ?= ''
|
options.defaultPath ?= ''
|
||||||
options.filter ?= []
|
options.filters ?= []
|
||||||
|
|
||||||
wrappedCallback =
|
wrappedCallback =
|
||||||
if typeof callback is 'function'
|
if typeof callback is 'function'
|
||||||
|
|
|
@ -30,103 +30,30 @@ bool IsDirectory(const base::FilePath& path) {
|
||||||
file_info.is_directory : path.EndsWithSeparator();
|
file_info.is_directory : path.EndsWithSeparator();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the file type description from the registry. This will be "Text Document"
|
void ConvertFilters(const Filters& filters,
|
||||||
// for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't
|
std::vector<std::wstring>* buffer,
|
||||||
// have an entry for the file type, we return false, true if the description was
|
std::vector<COMDLG_FILTERSPEC>* filterspec) {
|
||||||
// found. 'file_ext' must be in form ".txt".
|
if (filters.empty()) {
|
||||||
static bool GetRegistryDescriptionFromExtension(const std::wstring& file_ext,
|
COMDLG_FILTERSPEC spec = { L"All Files (*.*)", L"*.*" };
|
||||||
std::wstring* reg_description) {
|
filterspec->push_back(spec);
|
||||||
DCHECK(reg_description);
|
return;
|
||||||
base::win::RegKey reg_ext(HKEY_CLASSES_ROOT, file_ext.c_str(), KEY_READ);
|
|
||||||
std::wstring reg_app;
|
|
||||||
if (reg_ext.ReadValue(NULL, ®_app) == ERROR_SUCCESS && !reg_app.empty()) {
|
|
||||||
base::win::RegKey reg_link(HKEY_CLASSES_ROOT, reg_app.c_str(), KEY_READ);
|
|
||||||
if (reg_link.ReadValue(NULL, reg_description) == ERROR_SUCCESS)
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up a filter for a Save/Open dialog, which will consist of |file_ext| file
|
|
||||||
// extensions (internally separated by semicolons), |ext_desc| as the text
|
|
||||||
// descriptions of the |file_ext| types (optional), and (optionally) the default
|
|
||||||
// 'All Files' view. The purpose of the filter is to show only files of a
|
|
||||||
// particular type in a Windows Save/Open dialog box. The resulting filter is
|
|
||||||
// returned. The filters created here are:
|
|
||||||
// 1. only files that have 'file_ext' as their extension
|
|
||||||
// 2. all files (only added if 'include_all_files' is true)
|
|
||||||
// Example:
|
|
||||||
// file_ext: { "*.txt", "*.htm;*.html" }
|
|
||||||
// ext_desc: { "Text Document" }
|
|
||||||
// returned: "Text Document\0*.txt\0HTML Document\0*.htm;*.html\0"
|
|
||||||
// "All Files\0*.*\0\0" (in one big string)
|
|
||||||
// If a description is not provided for a file extension, it will be retrieved
|
|
||||||
// from the registry. If the file extension does not exist in the registry, it
|
|
||||||
// will be omitted from the filter, as it is likely a bogus extension.
|
|
||||||
void FormatFilterForExtensions(
|
|
||||||
std::vector<std::wstring>* file_ext,
|
|
||||||
std::vector<std::wstring>* ext_desc,
|
|
||||||
bool include_all_files,
|
|
||||||
std::vector<COMDLG_FILTERSPEC>* file_types) {
|
|
||||||
DCHECK(file_ext->size() >= ext_desc->size());
|
|
||||||
|
|
||||||
if (file_ext->empty())
|
|
||||||
include_all_files = true;
|
|
||||||
|
|
||||||
for (size_t i = 0; i < file_ext->size(); ++i) {
|
|
||||||
std::wstring ext = (*file_ext)[i];
|
|
||||||
std::wstring desc;
|
|
||||||
if (i < ext_desc->size())
|
|
||||||
desc = (*ext_desc)[i];
|
|
||||||
|
|
||||||
if (ext.empty()) {
|
|
||||||
// Force something reasonable to appear in the dialog box if there is no
|
|
||||||
// extension provided.
|
|
||||||
include_all_files = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (desc.empty()) {
|
|
||||||
DCHECK(ext.find(L'.') != std::wstring::npos);
|
|
||||||
std::wstring first_extension = ext.substr(ext.find(L'.'));
|
|
||||||
size_t first_separator_index = first_extension.find(L';');
|
|
||||||
if (first_separator_index != std::wstring::npos)
|
|
||||||
first_extension = first_extension.substr(0, first_separator_index);
|
|
||||||
|
|
||||||
// Find the extension name without the preceeding '.' character.
|
|
||||||
std::wstring ext_name = first_extension;
|
|
||||||
size_t ext_index = ext_name.find_first_not_of(L'.');
|
|
||||||
if (ext_index != std::wstring::npos)
|
|
||||||
ext_name = ext_name.substr(ext_index);
|
|
||||||
|
|
||||||
if (!GetRegistryDescriptionFromExtension(first_extension, &desc)) {
|
|
||||||
// The extension doesn't exist in the registry. Create a description
|
|
||||||
// based on the unknown extension type (i.e. if the extension is .qqq,
|
|
||||||
// the we create a description "QQQ File (.qqq)").
|
|
||||||
include_all_files = true;
|
|
||||||
// TODO(zcbenz): should be localized.
|
|
||||||
desc = base::i18n::ToUpper(base::WideToUTF16(ext_name)) + L" File";
|
|
||||||
}
|
|
||||||
desc += L" (*." + ext_name + L")";
|
|
||||||
|
|
||||||
// Store the description.
|
|
||||||
ext_desc->push_back(desc);
|
|
||||||
}
|
|
||||||
|
|
||||||
COMDLG_FILTERSPEC spec = { (*ext_desc)[i].c_str(), (*file_ext)[i].c_str() };
|
|
||||||
file_types->push_back(spec);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (include_all_files) {
|
buffer->reserve(filters.size() * 2);
|
||||||
// TODO(zcbenz): Should be localized.
|
for (size_t i = 0; i < filters.size(); ++i) {
|
||||||
ext_desc->push_back(L"All Files (*.*)");
|
const Filter& filter = filters[i];
|
||||||
file_ext->push_back(L"*.*");
|
|
||||||
|
|
||||||
COMDLG_FILTERSPEC spec = {
|
COMDLG_FILTERSPEC spec;
|
||||||
(*ext_desc)[ext_desc->size() - 1].c_str(),
|
buffer->push_back(base::UTF8ToWide(filter.first));
|
||||||
(*file_ext)[file_ext->size() - 1].c_str(),
|
spec.pszName = buffer->back().c_str();
|
||||||
};
|
|
||||||
file_types->push_back(spec);
|
std::vector<std::string> extensions(filter.second);
|
||||||
|
for (size_t j = 0; j < extensions.size(); ++j)
|
||||||
|
extensions[j].insert(0, "*.");
|
||||||
|
buffer->push_back(base::UTF8ToWide(JoinString(extensions, ";")));
|
||||||
|
spec.pszSpec = buffer->back().c_str();
|
||||||
|
|
||||||
|
filterspec->push_back(spec);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,26 +62,18 @@ void FormatFilterForExtensions(
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class FileDialog {
|
class FileDialog {
|
||||||
public:
|
public:
|
||||||
FileDialog(const base::FilePath& default_path,
|
FileDialog(const base::FilePath& default_path, const std::string& title,
|
||||||
const std::string title,
|
const Filters& filters, int options) {
|
||||||
int options,
|
|
||||||
const std::vector<std::wstring>& file_ext,
|
|
||||||
const std::vector<std::wstring>& desc_ext)
|
|
||||||
: file_ext_(file_ext),
|
|
||||||
desc_ext_(desc_ext) {
|
|
||||||
std::vector<COMDLG_FILTERSPEC> filters;
|
|
||||||
FormatFilterForExtensions(&file_ext_, &desc_ext_, true, &filters);
|
|
||||||
|
|
||||||
std::wstring file_part;
|
std::wstring file_part;
|
||||||
if (!IsDirectory(default_path))
|
if (!IsDirectory(default_path))
|
||||||
file_part = default_path.BaseName().value();
|
file_part = default_path.BaseName().value();
|
||||||
|
|
||||||
dialog_.reset(new T(
|
std::vector<std::wstring> buffer;
|
||||||
file_part.c_str(),
|
std::vector<COMDLG_FILTERSPEC> filterspec;
|
||||||
options,
|
ConvertFilters(filters, &buffer, &filterspec);
|
||||||
NULL,
|
|
||||||
filters.data(),
|
dialog_.reset(new T(file_part.c_str(), options, NULL,
|
||||||
filters.size()));
|
filterspec.data(), filterspec.size()));
|
||||||
|
|
||||||
if (!title.empty())
|
if (!title.empty())
|
||||||
GetPtr()->SetTitle(base::UTF8ToUTF16(title).c_str());
|
GetPtr()->SetTitle(base::UTF8ToUTF16(title).c_str());
|
||||||
|
@ -174,8 +93,6 @@ class FileDialog {
|
||||||
|
|
||||||
IFileDialog* GetPtr() const { return dialog_->GetPtr(); }
|
IFileDialog* GetPtr() const { return dialog_->GetPtr(); }
|
||||||
|
|
||||||
const std::vector<std::wstring> file_ext() const { return file_ext_; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Set up the initial directory for the dialog.
|
// Set up the initial directory for the dialog.
|
||||||
void SetDefaultFolder(const base::FilePath file_path) {
|
void SetDefaultFolder(const base::FilePath file_path) {
|
||||||
|
@ -193,10 +110,6 @@ class FileDialog {
|
||||||
|
|
||||||
scoped_ptr<T> dialog_;
|
scoped_ptr<T> dialog_;
|
||||||
|
|
||||||
std::vector<std::wstring> file_ext_;
|
|
||||||
std::vector<std::wstring> desc_ext_;
|
|
||||||
std::vector<COMDLG_FILTERSPEC> filters_;
|
|
||||||
|
|
||||||
DISALLOW_COPY_AND_ASSIGN(FileDialog);
|
DISALLOW_COPY_AND_ASSIGN(FileDialog);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -215,11 +128,7 @@ bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
options |= FOS_ALLOWMULTISELECT;
|
options |= FOS_ALLOWMULTISELECT;
|
||||||
|
|
||||||
FileDialog<CShellFileOpenDialog> open_dialog(
|
FileDialog<CShellFileOpenDialog> open_dialog(
|
||||||
default_path,
|
default_path, title, filters, options);
|
||||||
title,
|
|
||||||
options,
|
|
||||||
std::vector<std::wstring>(),
|
|
||||||
std::vector<std::wstring>());
|
|
||||||
if (!open_dialog.Show(parent_window))
|
if (!open_dialog.Show(parent_window))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
@ -263,8 +172,8 @@ void ShowOpenDialog(atom::NativeWindow* parent_window,
|
||||||
bool result = ShowOpenDialog(parent_window,
|
bool result = ShowOpenDialog(parent_window,
|
||||||
title,
|
title,
|
||||||
default_path,
|
default_path,
|
||||||
properties,
|
|
||||||
filters,
|
filters,
|
||||||
|
properties,
|
||||||
&paths);
|
&paths);
|
||||||
callback.Run(result, paths);
|
callback.Run(result, paths);
|
||||||
}
|
}
|
||||||
|
@ -274,42 +183,38 @@ bool ShowSaveDialog(atom::NativeWindow* parent_window,
|
||||||
const base::FilePath& default_path,
|
const base::FilePath& default_path,
|
||||||
const Filters& filters,
|
const Filters& filters,
|
||||||
base::FilePath* path) {
|
base::FilePath* path) {
|
||||||
// TODO(zcbenz): Accept custom filters from caller.
|
|
||||||
std::vector<std::wstring> file_ext;
|
|
||||||
std::wstring extension = default_path.Extension();
|
|
||||||
if (!extension.empty())
|
|
||||||
file_ext.push_back(extension.insert(0, L"*"));
|
|
||||||
|
|
||||||
FileDialog<CShellFileSaveDialog> save_dialog(
|
FileDialog<CShellFileSaveDialog> save_dialog(
|
||||||
default_path,
|
default_path, title, filters,
|
||||||
title,
|
FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT);
|
||||||
FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT,
|
|
||||||
file_ext,
|
|
||||||
std::vector<std::wstring>());
|
|
||||||
if (!save_dialog.Show(parent_window))
|
if (!save_dialog.Show(parent_window))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
wchar_t file_name[MAX_PATH];
|
wchar_t buffer[MAX_PATH];
|
||||||
HRESULT hr = save_dialog.GetDialog()->GetFilePath(file_name, MAX_PATH);
|
HRESULT hr = save_dialog.GetDialog()->GetFilePath(buffer, MAX_PATH);
|
||||||
if (FAILED(hr))
|
if (FAILED(hr))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
std::string file_name = base::WideToUTF8(std::wstring(buffer));
|
||||||
|
|
||||||
// Append extension according to selected filter.
|
// Append extension according to selected filter.
|
||||||
UINT filter_index = 1;
|
if (!filters.empty()) {
|
||||||
save_dialog.GetPtr()->GetFileTypeIndex(&filter_index);
|
UINT filter_index = 1;
|
||||||
std::wstring selected_filter = save_dialog.file_ext()[filter_index - 1];
|
save_dialog.GetPtr()->GetFileTypeIndex(&filter_index);
|
||||||
if (selected_filter != L"*.*") {
|
const Filter& filter = filters[filter_index - 1];
|
||||||
std::wstring result = file_name;
|
|
||||||
if (!EndsWith(result, selected_filter.substr(1), false)) {
|
bool matched = false;
|
||||||
if (result[result.length() - 1] != L'.')
|
for (size_t i = 0; i < filter.second.size(); ++i) {
|
||||||
result.push_back(L'.');
|
if (EndsWith(file_name, filter.second[i], false)) {
|
||||||
result.append(selected_filter.substr(2));
|
matched = true;
|
||||||
*path = base::FilePath(result);
|
break;;
|
||||||
return true;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!matched && !filter.second.empty())
|
||||||
|
file_name += ("." + filter.second[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
*path = base::FilePath(file_name);
|
*path = base::FilePath(base::UTF8ToUTF16(file_name));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue