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

Side by Side Diff: chrome/browser/ui/gtk/select_file_dialog_impl_gtk.cc

Issue 10414085: Modified the pepper file chooser API to support filtering files by extensions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 6 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/file_select_helper_unittest.cc ('k') | chrome/chrome_tests.gypi » ('j') | 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 "chrome/browser/ui/gtk/select_file_dialog_impl.h" 18 #include "chrome/browser/ui/gtk/select_file_dialog_impl.h"
19 #include "chrome/browser/ui/select_file_dialog.h" 19 #include "chrome/browser/ui/select_file_dialog.h"
20 #include "content/public/browser/browser_thread.h" 20 #include "content/public/browser/browser_thread.h"
21 #include "grit/generated_resources.h" 21 #include "grit/generated_resources.h"
22 #include "net/base/mime_util.h"
23 #include "ui/base/gtk/gtk_signal.h" 22 #include "ui/base/gtk/gtk_signal.h"
24 #include "ui/base/l10n/l10n_util.h" 23 #include "ui/base/l10n/l10n_util.h"
25 24
26 // Implementation of SelectFileDialog that shows a Gtk common dialog for 25 // Implementation of SelectFileDialog that shows a Gtk common dialog for
27 // choosing a file or folder. This acts as a modal dialog. 26 // choosing a file or folder. This acts as a modal dialog.
28 class SelectFileDialogImplGTK : public SelectFileDialogImpl { 27 class SelectFileDialogImplGTK : public SelectFileDialogImpl {
29 public: 28 public:
30 explicit SelectFileDialogImplGTK(Listener* listener); 29 explicit SelectFileDialogImplGTK(Listener* listener);
31 30
32 protected: 31 protected:
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 } 220 }
222 221
223 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) { 222 void SelectFileDialogImplGTK::AddFilters(GtkFileChooser* chooser) {
224 for (size_t i = 0; i < file_types_.extensions.size(); ++i) { 223 for (size_t i = 0; i < file_types_.extensions.size(); ++i) {
225 GtkFileFilter* filter = NULL; 224 GtkFileFilter* filter = NULL;
226 std::set<std::string> fallback_labels; 225 std::set<std::string> fallback_labels;
227 226
228 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) { 227 for (size_t j = 0; j < file_types_.extensions[i].size(); ++j) {
229 const std::string& current_extension = file_types_.extensions[i][j]; 228 const std::string& current_extension = file_types_.extensions[i][j];
230 if (!current_extension.empty()) { 229 if (!current_extension.empty()) {
231 std::string mime_type;
232 bool found_valid_mime_type;
233 {
234 // Allow IO in the file dialog. (http://crbug.com/72637)
235 base::ThreadRestrictions::ScopedAllowIO allow_io;
236 found_valid_mime_type = net::GetMimeTypeFromExtension(
237 current_extension, &mime_type);
238 }
239 if (!filter) 230 if (!filter)
240 filter = gtk_file_filter_new(); 231 filter = gtk_file_filter_new();
241 232 std::string pattern = "*." + current_extension;
242 // Try to add a mime-type filter instead of an extension filter if 233 gtk_file_filter_add_pattern(filter, pattern.c_str());
243 // possible. (http://crbug.com/12347) 234 fallback_labels.insert(pattern);
244 if (found_valid_mime_type) {
245 gtk_file_filter_add_mime_type(filter, mime_type.c_str());
246 fallback_labels.insert(mime_type);
247 } else {
248 std::string pattern = "*." + current_extension;
249 gtk_file_filter_add_pattern(filter, pattern.c_str());
250 fallback_labels.insert(pattern);
251 }
252 } 235 }
253 } 236 }
254 // We didn't find any non-empty extensions to filter on. 237 // We didn't find any non-empty extensions to filter on.
255 if (!filter) 238 if (!filter)
256 continue; 239 continue;
257 240
258 // The description vector may be blank, in which case we are supposed to 241 // The description vector may be blank, in which case we are supposed to
259 // use some sort of default description based on the filter. 242 // use some sort of default description based on the filter.
260 if (i < file_types_.extension_description_overrides.size()) { 243 if (i < file_types_.extension_description_overrides.size()) {
261 gtk_file_filter_set_name(filter, UTF16ToUTF8( 244 gtk_file_filter_set_name(filter, UTF16ToUTF8(
262 file_types_.extension_description_overrides[i]).c_str()); 245 file_types_.extension_description_overrides[i]).c_str());
263 } else { 246 } else {
264 // There is no system default filter description so we use 247 // There is no system default filter description so we use
265 // the MIME types themselves if the description is blank. 248 // the extensions themselves if the description is blank.
266 std::vector<std::string> fallback_labels_vector(fallback_labels.begin(), 249 std::vector<std::string> fallback_labels_vector(fallback_labels.begin(),
267 fallback_labels.end()); 250 fallback_labels.end());
268 std::string fallback_label = JoinString(fallback_labels_vector, ','); 251 std::string fallback_label = JoinString(fallback_labels_vector, ',');
269 gtk_file_filter_set_name(filter, fallback_label.c_str()); 252 gtk_file_filter_set_name(filter, fallback_label.c_str());
270 } 253 }
271 254
272 gtk_file_chooser_add_filter(chooser, filter); 255 gtk_file_chooser_add_filter(chooser, filter);
273 if (i == file_type_index_ - 1) 256 if (i == file_type_index_ - 1)
274 gtk_file_chooser_set_filter(chooser, filter); 257 gtk_file_chooser_set_filter(chooser, filter);
275 } 258 }
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
555 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth, 538 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file_at_size(filename, kPreviewWidth,
556 kPreviewHeight, NULL); 539 kPreviewHeight, NULL);
557 g_free(filename); 540 g_free(filename);
558 if (pixbuf) { 541 if (pixbuf) {
559 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf); 542 gtk_image_set_from_pixbuf(GTK_IMAGE(preview_), pixbuf);
560 g_object_unref(pixbuf); 543 g_object_unref(pixbuf);
561 } 544 }
562 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser), 545 gtk_file_chooser_set_preview_widget_active(GTK_FILE_CHOOSER(chooser),
563 pixbuf ? TRUE : FALSE); 546 pixbuf ? TRUE : FALSE);
564 } 547 }
OLDNEW
« no previous file with comments | « chrome/browser/file_select_helper_unittest.cc ('k') | chrome/chrome_tests.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698