@@ -30,64 +30,44 @@ std::string GetPromptText(GtkDialog* dialog) {
3030 return std::string ();
3131}
3232
33- std::string GetDescriptionFromMimeType (const std::string& mime_type) {
34- // Check for wild card mime types and return an appropriate description.
35- static const struct {
36- const char * mime_type;
37- const char * label;
38- } kWildCardMimeTypes [] = {
39- {" audio" , " Audio Files" },
40- {" image" , " Image Files" },
41- {" text" , " Text Files" },
42- {" video" , " Video Files" },
43- };
44-
45- for (size_t i = 0 ;
46- i < sizeof (kWildCardMimeTypes ) / sizeof (kWildCardMimeTypes [0 ]); ++i) {
47- if (mime_type == std::string (kWildCardMimeTypes [i].mime_type ) + " /*" )
48- return std::string (kWildCardMimeTypes [i].label );
33+ // Split |str| on |delim| and return the parts.
34+ std::vector<std::string> SplitString (const std::string& str, char delim) {
35+ std::vector<std::string> result;
36+ std::string token;
37+ for (char c : str) {
38+ if (c == delim) {
39+ if (!token.empty ())
40+ result.push_back (token);
41+ token.clear ();
42+ } else {
43+ token += c;
44+ }
4945 }
50-
51- return std::string ();
46+ if (!token.empty ())
47+ result.push_back (token);
48+ return result;
5249}
5350
5451void AddFilters (GtkFileChooser* chooser,
5552 const std::vector<CefString>& accept_filters,
53+ const std::vector<CefString>& accept_extensions,
54+ const std::vector<CefString>& accept_descriptions,
5655 bool include_all_files,
5756 std::vector<GtkFileFilter*>* filters) {
5857 bool has_filter = false ;
5958
60- for (size_t i = 0 ; i < accept_filters.size (); ++i ) {
61- const std::string& filter = accept_filters[i ];
59+ for (size_t j = 0 ; j < accept_filters.size (); ++j ) {
60+ const std::string& filter = accept_filters[j ];
6261 if (filter.empty ())
6362 continue ;
6463
65- std::vector<std::string> extensions;
66- std::string description;
67-
68- size_t sep_index = filter.find (' |' );
69- if (sep_index != std::string::npos) {
70- // Treat as a filter of the form "Filter Name|.ext1;.ext2;.ext3".
71- description = filter.substr (0 , sep_index);
72-
73- const std::string& exts = filter.substr (sep_index + 1 );
74- size_t last = 0 ;
75- size_t size = exts.size ();
76- for (size_t i = 0 ; i <= size; ++i) {
77- if (i == size || exts[i] == ' ;' ) {
78- std::string ext (exts, last, i - last);
79- if (!ext.empty () && ext[0 ] == ' .' )
80- extensions.push_back (ext);
81- last = i + 1 ;
82- }
83- }
84- } else if (filter[0 ] == ' .' ) {
85- // Treat as an extension beginning with the '.' character.
86- extensions.push_back (filter);
87- } else {
88- // Otherwise convert mime type to one or more extensions.
89- description = GetDescriptionFromMimeType (filter);
64+ // Use pre-parsed extensions when available.
65+ std::vector<std::string> extensions =
66+ SplitString (accept_extensions[j], ' ;' );
67+ std::string description = accept_descriptions[j];
9068
69+ if (extensions.empty ()) {
70+ // Fallback: convert MIME type to extensions.
9171 std::vector<CefString> ext;
9272 CefGetExtensionsForMimeType (filter, ext);
9373 for (size_t x = 0 ; x < ext.size (); ++x)
@@ -101,7 +81,9 @@ void AddFilters(GtkFileChooser* chooser,
10181
10282 std::string ext_str;
10383 for (size_t x = 0 ; x < extensions.size (); ++x) {
104- const std::string& pattern = " *" + extensions[x];
84+ std::string pattern = extensions[x];
85+ if (pattern[0 ] != ' *' )
86+ pattern = " *" + pattern;
10587 if (x != 0 )
10688 ext_str += " ;" ;
10789 ext_str += pattern;
@@ -133,34 +115,31 @@ void AddFilters(GtkFileChooser* chooser,
133115
134116} // namespace
135117
136- ClientDialogHandlerGtk::ClientDialogHandlerGtk () : gtk_dialog_(NULL ) {}
118+ ClientDialogHandlerGtk::ClientDialogHandlerGtk () : gtk_dialog_(nullptr ) {}
137119
138120bool ClientDialogHandlerGtk::OnFileDialog (
139121 CefRefPtr<CefBrowser> browser,
140122 FileDialogMode mode,
141123 const CefString& title,
142124 const CefString& default_file_path,
143125 const std::vector<CefString>& accept_filters,
144- int selected_accept_filter,
126+ const std::vector<CefString>& accept_extensions,
127+ const std::vector<CefString>& accept_descriptions,
145128 CefRefPtr<CefFileDialogCallback> callback) {
146129 std::vector<CefString> files;
147130
148131 GtkFileChooserAction action;
149132 const gchar* accept_button;
150133
151- // Remove any modifier flags.
152- FileDialogMode mode_type =
153- static_cast <FileDialogMode>(mode & FILE_DIALOG_TYPE_MASK);
154-
155- if (mode_type == FILE_DIALOG_OPEN || mode_type == FILE_DIALOG_OPEN_MULTIPLE) {
134+ if (mode == FILE_DIALOG_OPEN || mode == FILE_DIALOG_OPEN_MULTIPLE) {
156135 action = GTK_FILE_CHOOSER_ACTION_OPEN;
157- accept_button = GTK_STOCK_OPEN ;
158- } else if (mode_type == FILE_DIALOG_OPEN_FOLDER) {
136+ accept_button = " _Open " ;
137+ } else if (mode == FILE_DIALOG_OPEN_FOLDER) {
159138 action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
160- accept_button = GTK_STOCK_OPEN ;
161- } else if (mode_type == FILE_DIALOG_SAVE) {
139+ accept_button = " _Open " ;
140+ } else if (mode == FILE_DIALOG_SAVE) {
162141 action = GTK_FILE_CHOOSER_ACTION_SAVE;
163- accept_button = GTK_STOCK_SAVE ;
142+ accept_button = " _Save " ;
164143 } else {
165144 NOTREACHED ();
166145 return false ;
@@ -170,7 +149,7 @@ bool ClientDialogHandlerGtk::OnFileDialog(
170149 if (!title.empty ()) {
171150 title_str = title;
172151 } else {
173- switch (mode_type ) {
152+ switch (mode ) {
174153 case FILE_DIALOG_OPEN:
175154 title_str = " Open File" ;
176155 break ;
@@ -193,21 +172,13 @@ bool ClientDialogHandlerGtk::OnFileDialog(
193172 return false ;
194173
195174 GtkWidget* dialog = gtk_file_chooser_dialog_new (
196- title_str.c_str (), GTK_WINDOW (window), action, GTK_STOCK_CANCEL ,
197- GTK_RESPONSE_CANCEL, accept_button, GTK_RESPONSE_ACCEPT, NULL );
175+ title_str.c_str (), GTK_WINDOW (window), action, " _Cancel " ,
176+ GTK_RESPONSE_CANCEL, accept_button, GTK_RESPONSE_ACCEPT, nullptr );
198177
199- if (mode_type == FILE_DIALOG_OPEN_MULTIPLE)
178+ if (mode == FILE_DIALOG_OPEN_MULTIPLE)
200179 gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), TRUE );
201180
202- if (mode_type == FILE_DIALOG_SAVE) {
203- gtk_file_chooser_set_do_overwrite_confirmation (
204- GTK_FILE_CHOOSER (dialog), !!(mode & FILE_DIALOG_OVERWRITEPROMPT_FLAG));
205- }
206-
207- gtk_file_chooser_set_show_hidden (GTK_FILE_CHOOSER (dialog),
208- !(mode & FILE_DIALOG_HIDEREADONLY_FLAG));
209-
210- if (!default_file_path.empty () && mode_type == FILE_DIALOG_SAVE) {
181+ if (!default_file_path.empty () && mode == FILE_DIALOG_SAVE) {
211182 const std::string& file_path = default_file_path;
212183 bool exists = false ;
213184
@@ -227,25 +198,22 @@ bool ClientDialogHandlerGtk::OnFileDialog(
227198 }
228199
229200 std::vector<GtkFileFilter*> filters;
230- AddFilters (GTK_FILE_CHOOSER (dialog), accept_filters, true , &filters);
231- if (selected_accept_filter < static_cast <int >(filters.size ())) {
232- gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (dialog),
233- filters[selected_accept_filter]);
234- }
201+ AddFilters (GTK_FILE_CHOOSER (dialog), accept_filters, accept_extensions,
202+ accept_descriptions, true , &filters);
235203
236204 bool success = false ;
237205
238206 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
239- if (mode_type == FILE_DIALOG_OPEN || mode_type == FILE_DIALOG_OPEN_FOLDER ||
240- mode_type == FILE_DIALOG_SAVE) {
207+ if (mode == FILE_DIALOG_OPEN || mode == FILE_DIALOG_OPEN_FOLDER ||
208+ mode == FILE_DIALOG_SAVE) {
241209 char * filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
242210 files.push_back (std::string (filename));
243211 success = true ;
244- } else if (mode_type == FILE_DIALOG_OPEN_MULTIPLE) {
212+ } else if (mode == FILE_DIALOG_OPEN_MULTIPLE) {
245213 GSList* filenames =
246214 gtk_file_chooser_get_filenames (GTK_FILE_CHOOSER (dialog));
247215 if (filenames) {
248- for (GSList* iter = filenames; iter != NULL ;
216+ for (GSList* iter = filenames; iter != nullptr ;
249217 iter = g_slist_next (iter)) {
250218 std::string path (static_cast <char *>(iter->data ));
251219 g_free (iter->data );
@@ -257,24 +225,10 @@ bool ClientDialogHandlerGtk::OnFileDialog(
257225 }
258226 }
259227
260- int filter_index = selected_accept_filter;
261- if (success) {
262- GtkFileFilter* selected_filter =
263- gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));
264- if (selected_filter != NULL ) {
265- for (size_t x = 0 ; x < filters.size (); ++x) {
266- if (filters[x] == selected_filter) {
267- filter_index = x;
268- break ;
269- }
270- }
271- }
272- }
273-
274228 gtk_widget_destroy (dialog);
275229
276230 if (success)
277- callback->Continue (filter_index, files);
231+ callback->Continue (files);
278232 else
279233 callback->Cancel ();
280234
@@ -316,11 +270,6 @@ bool ClientDialogHandlerGtk::OnJSDialog(CefRefPtr<CefBrowser> browser,
316270
317271 js_dialog_callback_ = callback;
318272
319- if (!origin_url.empty ()) {
320- // title += " - ";
321- // title += CefFormatUrlForSecurityDisplay(origin_url).ToString();
322- }
323-
324273 GtkWindow* window = CefBrowser_GetGtkWindow (browser);
325274 if (!window)
326275 return false ;
@@ -329,12 +278,12 @@ bool ClientDialogHandlerGtk::OnJSDialog(CefRefPtr<CefBrowser> browser,
329278 gtk_message_type, buttons, " %s" ,
330279 message_text.ToString ().c_str ());
331280 g_signal_connect (gtk_dialog_, " delete-event" ,
332- G_CALLBACK (gtk_widget_hide_on_delete), NULL );
281+ G_CALLBACK (gtk_widget_hide_on_delete), nullptr );
333282
334283 gtk_window_set_title (GTK_WINDOW (gtk_dialog_), title.c_str ());
335284
336285 GtkWidget* ok_button = gtk_dialog_add_button (GTK_DIALOG (gtk_dialog_),
337- GTK_STOCK_OK , GTK_RESPONSE_OK);
286+ " _OK " , GTK_RESPONSE_OK);
338287
339288 if (dialog_type != JSDIALOGTYPE_PROMPT)
340289 gtk_widget_grab_focus (ok_button);
@@ -378,8 +327,8 @@ void ClientDialogHandlerGtk::OnResetDialogState(CefRefPtr<CefBrowser> browser) {
378327 if (!gtk_dialog_)
379328 return ;
380329 gtk_widget_destroy (gtk_dialog_);
381- gtk_dialog_ = NULL ;
382- js_dialog_callback_ = NULL ;
330+ gtk_dialog_ = nullptr ;
331+ js_dialog_callback_ = nullptr ;
383332}
384333
385334// static
@@ -401,5 +350,5 @@ void ClientDialogHandlerGtk::OnDialogResponse(GtkDialog* dialog,
401350 NOTREACHED ();
402351 }
403352
404- handler->OnResetDialogState (NULL );
353+ handler->OnResetDialogState (nullptr );
405354}
0 commit comments