From 81a16b424f5760f091885d7ec3d2708d91aa2cf9 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Mar 2016 16:28:01 +0900 Subject: [PATCH 1/2] Add extension to filename automatically for GTK+ save dialog --- atom/browser/ui/file_dialog_gtk.cc | 34 +++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/atom/browser/ui/file_dialog_gtk.cc b/atom/browser/ui/file_dialog_gtk.cc index ed79449655..5a82a84939 100644 --- a/atom/browser/ui/file_dialog_gtk.cc +++ b/atom/browser/ui/file_dialog_gtk.cc @@ -39,7 +39,8 @@ class FileChooserDialog { const std::string& title, const base::FilePath& default_path, const Filters& filters) - : dialog_scope_(parent_window) { + : dialog_scope_(parent_window), + filters_(filters) { const char* confirm_text = GTK_STOCK_OK; if (action == GTK_FILE_CHOOSER_ACTION_SAVE) confirm_text = GTK_STOCK_SAVE; @@ -111,7 +112,7 @@ class FileChooserDialog { base::FilePath GetFileName() const { gchar* filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog_)); - base::FilePath path(filename); + base::FilePath path = AddExtensionForFilename(filename); g_free(filename); return path; } @@ -121,7 +122,8 @@ class FileChooserDialog { GSList* filenames = gtk_file_chooser_get_filenames( GTK_FILE_CHOOSER(dialog_)); for (GSList* iter = filenames; iter != NULL; iter = g_slist_next(iter)) { - base::FilePath path(static_cast(iter->data)); + base::FilePath path = AddExtensionForFilename( + static_cast(iter->data)); g_free(iter->data); paths.push_back(path); } @@ -135,11 +137,13 @@ class FileChooserDialog { private: void AddFilters(const Filters& filters); + base::FilePath AddExtensionForFilename(const gchar* filename) const; atom::NativeWindow::DialogScope dialog_scope_; GtkWidget* dialog_; + Filters filters_; SaveDialogCallback save_callback_; OpenDialogCallback open_callback_; @@ -184,6 +188,30 @@ void FileChooserDialog::AddFilters(const Filters& filters) { } } +base::FilePath FileChooserDialog::AddExtensionForFilename( + const gchar* filename) const { + base::FilePath path(filename); + GtkFileFilter* selected_filter = + gtk_file_chooser_get_filter(GTK_FILE_CHOOSER(dialog_)); + if (!selected_filter) + return path; + + GSList* filters = gtk_file_chooser_list_filters(GTK_FILE_CHOOSER(dialog_)); + int i = g_slist_index(filters, selected_filter); + g_slist_free(filters); + if (i >= filters_.size()) + return path; + + const auto& extensions = filters_[i].second; + for (const auto& extension : extensions) { + if (extension == "*" || path.MatchesExtension("." + extension)) + return path; + } + + return path.AddExtension(extensions[0]); +} + + } // namespace bool ShowOpenDialog(atom::NativeWindow* parent_window, From c2797e186446ff456b1f74450f1e5fc1b4aa6836 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 14 Mar 2016 17:08:32 +0900 Subject: [PATCH 2/2] Replace extension with the one in filter --- atom/browser/ui/file_dialog_gtk.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/browser/ui/file_dialog_gtk.cc b/atom/browser/ui/file_dialog_gtk.cc index 5a82a84939..6d19a2eda4 100644 --- a/atom/browser/ui/file_dialog_gtk.cc +++ b/atom/browser/ui/file_dialog_gtk.cc @@ -208,7 +208,7 @@ base::FilePath FileChooserDialog::AddExtensionForFilename( return path; } - return path.AddExtension(extensions[0]); + return path.ReplaceExtension(extensions[0]); }