From 04606a9f97b37d3563f365f48258f3fe9eeb4f6a Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 13 Jul 2015 13:51:18 +0800 Subject: [PATCH 1/2] Enable 'accept' attribute in 'input' label. --- atom/browser/web_dialog_helper.cc | 51 +++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/atom/browser/web_dialog_helper.cc b/atom/browser/web_dialog_helper.cc index f6f233453582..bbf16b5fc23c 100644 --- a/atom/browser/web_dialog_helper.cc +++ b/atom/browser/web_dialog_helper.cc @@ -4,17 +4,61 @@ #include "atom/browser/web_dialog_helper.h" +#include #include #include "atom/browser/ui/file_dialog.h" #include "base/bind.h" #include "base/files/file_enumerator.h" +#include "base/files/file_path.h" #include "base/strings/utf_string_conversions.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" #include "content/public/common/file_chooser_file_info.h" +#include "net/base/mime_util.h" #include "ui/shell_dialogs/selected_file_info.h" +namespace { + +file_dialog::Filters GetFileTypesFromAcceptType( + const std::vector& accept_types) { + file_dialog::Filters filters; + if (accept_types.empty()) + return filters; + + std::vector extensions; + + for (const auto& accept_type : accept_types) { + std::string ascii_type = base::UTF16ToASCII(accept_type); + if (ascii_type[0] == '.') { + // If the type starts with a period it is assumed to be a file extension, + // like `.txt`, // so we just have to add it to the list. + base::FilePath::StringType extension( + ascii_type.begin(), ascii_type.end()); + // Skip the first character. + extensions.push_back(extension.substr(1)); + } else { + if (ascii_type == "image/*" || ascii_type == "audio/*" || + ascii_type == "video/*") { + // For MIME Type + net::GetExtensionsForMimeType(ascii_type, &extensions); + } + } + } + + filters.push_back(file_dialog::Filter()); + for (const auto& extension : extensions) { +#if defined(OS_WIN) + filters[0].second.push_back(base::UTF16ToASCII(extension)); +#else + filters[0].second.push_back(extension); +#endif + } + return filters; +} + +} // namespace + namespace atom { WebDialogHelper::WebDialogHelper(NativeWindow* window) @@ -25,15 +69,18 @@ WebDialogHelper::WebDialogHelper(NativeWindow* window) WebDialogHelper::~WebDialogHelper() { } + void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, const content::FileChooserParams& params) { std::vector result; + file_dialog::Filters filters = GetFileTypesFromAcceptType( + params.accept_types); if (params.mode == content::FileChooserParams::Save) { base::FilePath path; if (file_dialog::ShowSaveDialog(window_, base::UTF16ToUTF8(params.title), params.default_file_name, - file_dialog::Filters(), + filters, &path)) { content::FileChooserFileInfo info; info.file_path = path; @@ -59,7 +106,7 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, if (file_dialog::ShowOpenDialog(window_, base::UTF16ToUTF8(params.title), params.default_file_name, - file_dialog::Filters(), + filters, flags, &paths)) { for (auto& path : paths) { From 21e5054facefa4c54b0927df7248560069ee00dd Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 13 Jul 2015 16:19:30 +0800 Subject: [PATCH 2/2] Make remember last used directory. --- atom/browser/web_dialog_helper.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/atom/browser/web_dialog_helper.cc b/atom/browser/web_dialog_helper.cc index bbf16b5fc23c..93cb32cba7c7 100644 --- a/atom/browser/web_dialog_helper.cc +++ b/atom/browser/web_dialog_helper.cc @@ -7,10 +7,13 @@ #include #include +#include "atom/browser/atom_browser_context.h" +#include "atom/browser/native_window.h" #include "atom/browser/ui/file_dialog.h" #include "base/bind.h" #include "base/files/file_enumerator.h" #include "base/files/file_path.h" +#include "base/prefs/pref_service.h" #include "base/strings/utf_string_conversions.h" #include "content/public/browser/render_view_host.h" #include "content/public/browser/web_contents.h" @@ -20,6 +23,8 @@ namespace { +const char kSelectFileLastDirectory[] = "selectfile.last_directory"; + file_dialog::Filters GetFileTypesFromAcceptType( const std::vector& accept_types) { file_dialog::Filters filters; @@ -103,9 +108,13 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, } std::vector paths; + AtomBrowserContext* browser_context = static_cast( + window_->web_contents()->GetBrowserContext()); + base::FilePath default_file_path = browser_context->prefs()->GetFilePath( + kSelectFileLastDirectory).Append(params.default_file_name); if (file_dialog::ShowOpenDialog(window_, base::UTF16ToUTF8(params.title), - params.default_file_name, + default_file_path, filters, flags, &paths)) { @@ -115,6 +124,10 @@ void WebDialogHelper::RunFileChooser(content::WebContents* web_contents, info.display_name = path.BaseName().value(); result.push_back(info); } + if (!paths.empty()) { + browser_context->prefs()->SetFilePath(kSelectFileLastDirectory, + paths[0].DirName()); + } } }