2013-07-05 02:13:09 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2013-08-13 08:51:47 +00:00
|
|
|
#include "browser/ui/file_dialog.h"
|
2013-07-05 02:13:09 +00:00
|
|
|
|
2013-09-18 12:28:56 +00:00
|
|
|
#include <atlbase.h>
|
2013-08-01 07:39:53 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <commdlg.h>
|
|
|
|
#include <shlobj.h>
|
|
|
|
|
|
|
|
#include "base/file_util.h"
|
2013-08-01 08:17:11 +00:00
|
|
|
#include "base/i18n/case_conversion.h"
|
2013-08-01 07:39:53 +00:00
|
|
|
#include "base/string_util.h"
|
2013-08-01 08:17:11 +00:00
|
|
|
#include "base/strings/string_split.h"
|
|
|
|
#include "base/utf_string_conversions.h"
|
|
|
|
#include "base/win/registry.h"
|
2013-08-01 07:39:53 +00:00
|
|
|
#include "browser/native_window.h"
|
2013-09-18 12:28:56 +00:00
|
|
|
#include "third_party/wtl/include/atlapp.h"
|
|
|
|
#include "third_party/wtl/include/atldlgs.h"
|
2013-08-01 07:39:53 +00:00
|
|
|
|
2013-07-05 02:13:09 +00:00
|
|
|
namespace file_dialog {
|
|
|
|
|
2013-08-01 07:39:53 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Distinguish directories from regular files.
|
|
|
|
bool IsDirectory(const base::FilePath& path) {
|
|
|
|
base::PlatformFileInfo file_info;
|
|
|
|
return file_util::GetFileInfo(path, &file_info) ?
|
|
|
|
file_info.is_directory : path.EndsWithSeparator();
|
|
|
|
}
|
|
|
|
|
2013-08-01 08:17:11 +00:00
|
|
|
// Get the file type description from the registry. This will be "Text Document"
|
|
|
|
// for .txt files, "JPEG Image" for .jpg files, etc. If the registry doesn't
|
|
|
|
// have an entry for the file type, we return false, true if the description was
|
|
|
|
// found. 'file_ext' must be in form ".txt".
|
|
|
|
static bool GetRegistryDescriptionFromExtension(const std::wstring& file_ext,
|
|
|
|
std::wstring* reg_description) {
|
|
|
|
DCHECK(reg_description);
|
|
|
|
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.
|
2013-09-18 12:28:56 +00:00
|
|
|
void FormatFilterForExtensions(
|
2013-09-18 14:09:07 +00:00
|
|
|
std::vector<std::wstring>* file_ext,
|
2013-09-18 12:28:56 +00:00
|
|
|
std::vector<std::wstring>* ext_desc,
|
2013-09-18 14:09:07 +00:00
|
|
|
bool include_all_files,
|
2013-09-18 12:28:56 +00:00
|
|
|
std::vector<COMDLG_FILTERSPEC>* file_types) {
|
2013-09-18 14:09:07 +00:00
|
|
|
DCHECK(file_ext->size() >= ext_desc->size());
|
2013-08-01 08:17:11 +00:00
|
|
|
|
2013-09-18 14:09:07 +00:00
|
|
|
if (file_ext->empty())
|
2013-08-01 08:17:11 +00:00
|
|
|
include_all_files = true;
|
|
|
|
|
2013-09-18 14:09:07 +00:00
|
|
|
for (size_t i = 0; i < file_ext->size(); ++i) {
|
|
|
|
std::wstring ext = (*file_ext)[i];
|
2013-08-01 08:17:11 +00:00
|
|
|
std::wstring desc;
|
2013-09-18 12:28:56 +00:00
|
|
|
if (i < ext_desc->size())
|
|
|
|
desc = (*ext_desc)[i];
|
2013-08-01 08:17:11 +00:00
|
|
|
|
|
|
|
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.
|
2013-09-18 12:28:56 +00:00
|
|
|
desc = base::i18n::ToUpper(WideToUTF16(ext_name)) + L" File";
|
2013-08-01 08:17:11 +00:00
|
|
|
}
|
2013-09-18 12:28:56 +00:00
|
|
|
desc += L" (*." + ext_name + L")";
|
|
|
|
|
|
|
|
// Store the description.
|
|
|
|
ext_desc->push_back(desc);
|
2013-08-01 08:17:11 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 14:09:07 +00:00
|
|
|
COMDLG_FILTERSPEC spec = { (*ext_desc)[i].c_str(), (*file_ext)[i].c_str() };
|
2013-09-18 12:28:56 +00:00
|
|
|
file_types->push_back(spec);
|
2013-08-01 08:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (include_all_files) {
|
2013-09-18 12:28:56 +00:00
|
|
|
// TODO(zcbenz): Should be localized.
|
2013-09-18 14:09:07 +00:00
|
|
|
ext_desc->push_back(L"All Files (*.*)");
|
|
|
|
file_ext->push_back(L"*.*");
|
|
|
|
|
|
|
|
COMDLG_FILTERSPEC spec = {
|
|
|
|
(*ext_desc)[ext_desc->size() - 1].c_str(),
|
|
|
|
(*file_ext)[file_ext->size() - 1].c_str(),
|
|
|
|
};
|
2013-09-18 12:28:56 +00:00
|
|
|
file_types->push_back(spec);
|
2013-08-01 08:17:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 13:42:14 +00:00
|
|
|
// Generic class to delegate common open/save dialog's behaviours, users need to
|
|
|
|
// call interface methods via GetPtr().
|
|
|
|
template <typename T>
|
|
|
|
class FileDialog {
|
|
|
|
public:
|
|
|
|
FileDialog(const base::FilePath& default_path,
|
|
|
|
const std::string title,
|
2013-09-18 14:27:40 +00:00
|
|
|
int options,
|
2013-09-18 13:42:14 +00:00
|
|
|
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;
|
2013-09-18 14:09:07 +00:00
|
|
|
FormatFilterForExtensions(&file_ext_, &desc_ext_, true, &filters);
|
2013-09-18 13:42:14 +00:00
|
|
|
|
|
|
|
std::wstring file_part;
|
|
|
|
if (!IsDirectory(default_path))
|
|
|
|
file_part = default_path.BaseName().value();
|
|
|
|
|
|
|
|
dialog_.reset(new T(
|
|
|
|
file_part.c_str(),
|
2013-09-18 14:27:40 +00:00
|
|
|
options,
|
2013-09-18 13:42:14 +00:00
|
|
|
NULL,
|
|
|
|
filters.data(),
|
|
|
|
filters.size()));
|
|
|
|
|
|
|
|
if (!title.empty())
|
|
|
|
GetPtr()->SetTitle(UTF8ToUTF16(title).c_str());
|
|
|
|
|
|
|
|
SetDefaultFolder(default_path);
|
|
|
|
}
|
|
|
|
|
2013-09-24 02:13:43 +00:00
|
|
|
bool Show(atom::NativeWindow* parent_window) {
|
|
|
|
HWND window = parent_window ? parent_window->GetNativeWindow() : NULL;
|
2013-09-19 14:28:18 +00:00
|
|
|
return dialog_->DoModal(window) == IDOK;
|
2013-09-18 13:42:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
T* GetDialog() { return dialog_.get(); }
|
|
|
|
|
|
|
|
IFileDialog* GetPtr() const { return dialog_->GetPtr(); }
|
|
|
|
|
2013-09-18 14:09:07 +00:00
|
|
|
const std::vector<std::wstring> file_ext() const { return file_ext_; }
|
|
|
|
|
2013-09-18 13:42:14 +00:00
|
|
|
private:
|
|
|
|
// Set up the initial directory for the dialog.
|
|
|
|
void SetDefaultFolder(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))
|
|
|
|
GetPtr()->SetDefaultFolder(folder_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2013-08-01 07:39:53 +00:00
|
|
|
} // namespace
|
|
|
|
|
2013-09-24 02:13:43 +00:00
|
|
|
bool ShowOpenDialog(atom::NativeWindow* parent_window,
|
|
|
|
const std::string& title,
|
2013-07-05 02:13:09 +00:00
|
|
|
const base::FilePath& default_path,
|
|
|
|
int properties,
|
|
|
|
std::vector<base::FilePath>* paths) {
|
2013-09-19 14:28:18 +00:00
|
|
|
int options = FOS_FORCEFILESYSTEM | FOS_FILEMUSTEXIST;
|
|
|
|
if (properties & FILE_DIALOG_OPEN_DIRECTORY)
|
|
|
|
options |= FOS_PICKFOLDERS;
|
|
|
|
if (properties & FILE_DIALOG_MULTI_SELECTIONS)
|
|
|
|
options |= FOS_ALLOWMULTISELECT;
|
|
|
|
|
|
|
|
FileDialog<CShellFileOpenDialog> open_dialog(
|
|
|
|
default_path,
|
|
|
|
title,
|
|
|
|
options,
|
|
|
|
std::vector<std::wstring>(),
|
|
|
|
std::vector<std::wstring>());
|
2013-09-24 02:13:43 +00:00
|
|
|
if (!open_dialog.Show(parent_window))
|
2013-09-19 14:28:18 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
ATL::CComPtr<IShellItemArray> items;
|
|
|
|
HRESULT hr = static_cast<IFileOpenDialog*>(open_dialog.GetPtr())->GetResults(
|
|
|
|
&items);
|
|
|
|
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];
|
|
|
|
hr = CShellFileOpenDialog::GetFileNameFromShellItem(
|
|
|
|
item, SIGDN_FILESYSPATH, file_name, MAX_PATH);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
paths->push_back(base::FilePath(file_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-07-05 02:13:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 06:47:39 +00:00
|
|
|
void ShowOpenDialog(atom::NativeWindow* parent_window,
|
|
|
|
const std::string& title,
|
|
|
|
const base::FilePath& default_path,
|
|
|
|
int properties,
|
|
|
|
const OpenDialogCallback& callback) {
|
|
|
|
std::vector<base::FilePath> paths;
|
|
|
|
bool result = ShowOpenDialog(parent_window,
|
|
|
|
title,
|
|
|
|
default_path,
|
|
|
|
properties,
|
|
|
|
&paths);
|
|
|
|
callback.Run(result, paths);
|
|
|
|
}
|
|
|
|
|
2013-09-24 02:13:43 +00:00
|
|
|
bool ShowSaveDialog(atom::NativeWindow* parent_window,
|
2013-07-05 02:13:09 +00:00
|
|
|
const std::string& title,
|
|
|
|
const base::FilePath& default_path,
|
|
|
|
base::FilePath* path) {
|
2013-09-18 12:28:56 +00:00
|
|
|
// TODO(zcbenz): Accept custom filters from caller.
|
2013-09-18 13:42:14 +00:00
|
|
|
std::vector<std::wstring> file_ext;
|
2013-09-18 12:28:56 +00:00
|
|
|
std::wstring extension = default_path.Extension();
|
|
|
|
if (!extension.empty())
|
|
|
|
file_ext.push_back(extension.insert(0, L"*"));
|
2013-08-01 07:39:53 +00:00
|
|
|
|
2013-09-18 13:42:14 +00:00
|
|
|
FileDialog<CShellFileSaveDialog> save_dialog(
|
|
|
|
default_path,
|
|
|
|
title,
|
2013-09-18 14:27:40 +00:00
|
|
|
FOS_FORCEFILESYSTEM | FOS_PATHMUSTEXIST | FOS_OVERWRITEPROMPT,
|
2013-09-18 13:42:14 +00:00
|
|
|
file_ext,
|
|
|
|
std::vector<std::wstring>());
|
2013-09-24 02:13:43 +00:00
|
|
|
if (!save_dialog.Show(parent_window))
|
2013-09-18 12:28:56 +00:00
|
|
|
return false;
|
|
|
|
|
2013-09-18 13:42:14 +00:00
|
|
|
wchar_t file_name[MAX_PATH];
|
|
|
|
HRESULT hr = save_dialog.GetDialog()->GetFilePath(file_name, MAX_PATH);
|
2013-09-18 12:28:56 +00:00
|
|
|
if (FAILED(hr))
|
2013-08-01 07:39:53 +00:00
|
|
|
return false;
|
|
|
|
|
2013-09-18 14:09:07 +00:00
|
|
|
// Append extension according to selected filter.
|
|
|
|
UINT filter_index = 1;
|
|
|
|
save_dialog.GetPtr()->GetFileTypeIndex(&filter_index);
|
|
|
|
std::wstring selected_filter = save_dialog.file_ext()[filter_index - 1];
|
|
|
|
if (selected_filter != L"*.*") {
|
|
|
|
std::wstring result = file_name;
|
2013-09-18 14:24:46 +00:00
|
|
|
if (!EndsWith(result, selected_filter.substr(1), false)) {
|
2013-09-18 14:09:07 +00:00
|
|
|
if (result[result.length() - 1] != L'.')
|
|
|
|
result.push_back(L'.');
|
2013-09-18 14:24:46 +00:00
|
|
|
result.append(selected_filter.substr(2));
|
2013-09-18 14:09:07 +00:00
|
|
|
*path = base::FilePath(result);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-18 12:28:56 +00:00
|
|
|
*path = base::FilePath(file_name);
|
2013-08-01 07:39:53 +00:00
|
|
|
return true;
|
2013-07-05 02:13:09 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 06:47:39 +00:00
|
|
|
void ShowSaveDialog(atom::NativeWindow* parent_window,
|
|
|
|
const std::string& title,
|
|
|
|
const base::FilePath& default_path,
|
|
|
|
const SaveDialogCallback& callback) {
|
|
|
|
base::FilePath path;
|
|
|
|
bool result = ShowSaveDialog(parent_window, title, default_path, &path);
|
|
|
|
callback.Run(result, path);
|
|
|
|
}
|
|
|
|
|
2013-07-05 02:13:09 +00:00
|
|
|
} // namespace file_dialog
|