Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: ui/base/dialogs/gtk/select_file_dialog_impl_gtk.cc

Issue 11784036: [gtk] Fix file filter bug for <input type=file accept=mime/type>. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: erg@ review Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/libgtk2ui/select_file_dialog_impl_gtk2.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <gtk/gtk.h> 5 #include <gtk/gtk.h>
6 #include <map> 6 #include <map>
7 #include <set> 7 #include <set>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/file_util.h" 10 #include "base/file_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/string_util.h" 13 #include "base/string_util.h"
14 #include "base/sys_string_conversions.h" 14 #include "base/sys_string_conversions.h"
15 #include "base/threading/thread.h" 15 #include "base/threading/thread.h"
16 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
17 #include "base/utf_string_conversions.h" 17 #include "base/utf_string_conversions.h"
18 #include "grit/ui_strings.h" 18 #include "grit/ui_strings.h"
19 #include "ui/base/dialogs/gtk/select_file_dialog_impl.h" 19 #include "ui/base/dialogs/gtk/select_file_dialog_impl.h"
20 #include "ui/base/dialogs/select_file_dialog.h" 20 #include "ui/base/dialogs/select_file_dialog.h"
21 #include "ui/base/gtk/gtk_signal.h" 21 #include "ui/base/gtk/gtk_signal.h"
22 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
23 23
24 namespace { 24 namespace {
25 25
26 // Makes sure that .jpg also shows .JPG. |data| is a std::string* in disguise.
27 gboolean FileFilterCaseInsensitive(const GtkFileFilterInfo* file_info,
28 gpointer data) {
Evan Stade 2013/01/10 01:50:06 just make data a std::string*
Dan Beam 2013/01/10 05:37:08 Done.
29 std::string* file_extension = reinterpret_cast<std::string*>(data);
30 return EndsWith(file_info->filename, *file_extension, false);
31 }
32
33 // Deletes |data| when gtk_file_filter_add_custom() is done with it.
34 void OnFileFilterDataDestroyed(gpointer data) {
35 delete reinterpret_cast<std::string*>(data);
36 }
37
26 // Implementation of SelectFileDialog that shows a Gtk common dialog for 38 // Implementation of SelectFileDialog that shows a Gtk common dialog for
27 // choosing a file or folder. This acts as a modal dialog. 39 // choosing a file or folder. This acts as a modal dialog.
28 class SelectFileDialogImplGTK : public ui::SelectFileDialogImpl { 40 class SelectFileDialogImplGTK : public ui::SelectFileDialogImpl {
29 public: 41 public:
30 explicit SelectFileDialogImplGTK(Listener* listener, 42 explicit SelectFileDialogImplGTK(Listener* listener,
31 ui::SelectFilePolicy* policy); 43 ui::SelectFilePolicy* policy);
32 44
33 protected: 45 protected:
34 virtual ~SelectFileDialogImplGTK(); 46 virtual ~SelectFileDialogImplGTK();
35 47
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) { 232 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) {
221 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { 233 for (size_t i = 0; i < file_types_.extensions.size(); ++i) {
222 GtkFileFilter* filter = NULL; 234 GtkFileFilter* filter = NULL;
223 std::set<std::string> fallback_labels; 235 std::set<std::string> fallback_labels;
224 236
225 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { 237 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) {
226 const std::string& current_extension = file_types_.extensions[i][j]; 238 const std::string& current_extension = file_types_.extensions[i][j];
227 if (!current_extension.empty()) { 239 if (!current_extension.empty()) {
228 if (!filter) 240 if (!filter)
229 filter = gtk_file_filter_new(); 241 filter = gtk_file_filter_new();
230 std::string pattern = "*." + current_extension; 242 // |file_extension| is freed in |OnFileFilterDataDestroyed()|.
231 gtk_file_filter_add_pattern(filter, pattern.c_str()); 243 std::string* file_extension = new std::string("." + current_extension);
Evan Stade 2013/01/10 01:50:06 scoped_ptr for clarity (and .release() it to the f
Dan Beam 2013/01/10 05:37:08 Done.
232 fallback_labels.insert(pattern); 244 gtk_file_filter_add_custom(filter,
245 GTK_FILE_FILTER_FILENAME,
246 &FileFilterCaseInsensitive,
247 reinterpret_cast<gpointer>(file_extension),
Evan Stade 2013/01/10 01:50:06 sure this cast is necessary?
Dan Beam 2013/01/10 05:37:08 Nope, it's not.
248 &OnFileFilterDataDestroyed);
249 fallback_labels.insert(std::string("*").append(*file_extension));
Evan Stade 2013/01/10 01:50:06 you should not use file_extension after you've han
Dan Beam 2013/01/10 05:37:08 Done.
233 } 250 }
234 } 251 }
235 // We didn't find any non-empty extensions to filter on. 252 // We didn't find any non-empty extensions to filter on.
236 if (!filter) 253 if (!filter)
237 continue; 254 continue;
238 255
239 // The description vector may be blank, in which case we are supposed to 256 // The description vector may be blank, in which case we are supposed to
240 // use some sort of default description based on the filter. 257 // use some sort of default description based on the filter.
241 if (i < file_types_.extension_description_overrides.size()) { 258 if (i < file_types_.extension_description_overrides.size()) {
242 gtk_file_filter_set_name(filter, UTF16ToUTF8( 259 gtk_file_filter_set_name(filter, UTF16ToUTF8(
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 } // namespace 570 } // namespace
554 571
555 namespace ui { 572 namespace ui {
556 573
557 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplGTK( 574 SelectFileDialogImpl* SelectFileDialogImpl::NewSelectFileDialogImplGTK(
558 Listener* listener, ui::SelectFilePolicy* policy) { 575 Listener* listener, ui::SelectFilePolicy* policy) {
559 return new SelectFileDialogImplGTK(listener, policy); 576 return new SelectFileDialogImplGTK(listener, policy);
560 } 577 }
561 578
562 } // namespace ui 579 } // namespace ui
OLDNEW
« no previous file with comments | « chrome/browser/ui/libgtk2ui/select_file_dialog_impl_gtk2.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698