1 /*
2 * xed-file-chooser-dialog.c
3 * This file is part of xed
4 *
5 * Copyright (C) 2005-2007 - Paolo Maggi
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /*
24 * Modified by the xed Team, 2005-2007. See the AUTHORS file for a
25 * list of people on the xed Team.
26 * See the ChangeLog files for a list of changes.
27 *
28 * $Id$
29 */
30
31 /* TODO: Override set_extra_widget */
32 /* TODO: add encoding property */
33
34 #include <config.h>
35 #include <string.h>
36 #include <glib/gi18n.h>
37
38 #include "xed-file-chooser-dialog.h"
39 #include "xed-encodings-combo-box.h"
40 #include "xed-debug.h"
41 #include "xed-enum-types.h"
42 #include "xed-settings.h"
43
44 #define ALL_FILES _("All Files")
45 #define ALL_TEXT_FILES _("All Text Files")
46
47 struct _XedFileChooserDialogPrivate
48 {
49 GSettings *filter_settings;
50
51 GtkWidget *option_menu;
52 GtkWidget *extra_widget;
53
54 GtkWidget *newline_label;
55 GtkWidget *newline_combo;
56 GtkListStore *newline_store;
57 };
58
G_DEFINE_TYPE_WITH_PRIVATE(XedFileChooserDialog,xed_file_chooser_dialog,GTK_TYPE_FILE_CHOOSER_DIALOG)59 G_DEFINE_TYPE_WITH_PRIVATE (XedFileChooserDialog, xed_file_chooser_dialog, GTK_TYPE_FILE_CHOOSER_DIALOG)
60
61 static void
62 xed_file_chooser_dialog_dispose (GObject *object)
63 {
64 XedFileChooserDialog *dialog = XED_FILE_CHOOSER_DIALOG (object);
65
66 g_clear_object (&dialog->priv->filter_settings);
67
68 G_OBJECT_CLASS (xed_file_chooser_dialog_parent_class)->dispose (object);
69 }
70
71 static void
xed_file_chooser_dialog_class_init(XedFileChooserDialogClass * klass)72 xed_file_chooser_dialog_class_init (XedFileChooserDialogClass *klass)
73 {
74 GObjectClass *object_class = G_OBJECT_CLASS (klass);
75
76 object_class->dispose = xed_file_chooser_dialog_dispose;
77 }
78
79 static void
create_option_menu(XedFileChooserDialog * dialog)80 create_option_menu (XedFileChooserDialog *dialog)
81 {
82 GtkWidget *label;
83 GtkWidget *menu;
84
85 label = gtk_label_new_with_mnemonic (_("C_haracter Encoding:"));
86 gtk_widget_set_halign (label, GTK_ALIGN_START);
87
88 menu = xed_encodings_combo_box_new (
89 gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
90
91 gtk_label_set_mnemonic_widget (GTK_LABEL (label), menu);
92
93 gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget), label, FALSE, TRUE, 0);
94 gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget), menu, TRUE, TRUE, 0);
95
96 gtk_widget_show (label);
97 gtk_widget_show (menu);
98
99 dialog->priv->option_menu = menu;
100 }
101
102 static void
update_newline_visibility(XedFileChooserDialog * dialog)103 update_newline_visibility (XedFileChooserDialog *dialog)
104 {
105 if (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE)
106 {
107 gtk_widget_show (dialog->priv->newline_label);
108 gtk_widget_show (dialog->priv->newline_combo);
109 }
110 else
111 {
112 gtk_widget_hide (dialog->priv->newline_label);
113 gtk_widget_hide (dialog->priv->newline_combo);
114 }
115 }
116
117 static void
newline_combo_append(GtkComboBox * combo,GtkListStore * store,GtkTreeIter * iter,const gchar * label,GtkSourceNewlineType newline_type)118 newline_combo_append (GtkComboBox *combo,
119 GtkListStore *store,
120 GtkTreeIter *iter,
121 const gchar *label,
122 GtkSourceNewlineType newline_type)
123 {
124 gtk_list_store_append (store, iter);
125 gtk_list_store_set (store, iter, 0, label, 1, newline_type, -1);
126
127 if (newline_type == GTK_SOURCE_NEWLINE_TYPE_DEFAULT)
128 {
129 gtk_combo_box_set_active_iter (combo, iter);
130 }
131 }
132
133 static void
create_newline_combo(XedFileChooserDialog * dialog)134 create_newline_combo (XedFileChooserDialog *dialog)
135 {
136 GtkWidget *label, *combo;
137 GtkListStore *store;
138 GtkCellRenderer *renderer;
139 GtkTreeIter iter;
140
141 label = gtk_label_new_with_mnemonic (_("L_ine Ending:"));
142 gtk_widget_set_halign (label, GTK_ALIGN_START);
143
144 store = gtk_list_store_new (2, G_TYPE_STRING, GTK_SOURCE_TYPE_NEWLINE_TYPE);
145 combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (store));
146 renderer = gtk_cell_renderer_text_new ();
147
148 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE);
149
150 gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (combo), renderer, "text", 0);
151
152 newline_combo_append (GTK_COMBO_BOX (combo), store, &iter, _("Unix/Linux"), GTK_SOURCE_NEWLINE_TYPE_LF);
153 newline_combo_append (GTK_COMBO_BOX (combo), store, &iter, _("Mac OS Classic"), GTK_SOURCE_NEWLINE_TYPE_CR);
154 newline_combo_append (GTK_COMBO_BOX (combo), store, &iter, _("Windows"), GTK_SOURCE_NEWLINE_TYPE_CR_LF);
155
156 gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
157
158 gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget), label, FALSE, TRUE, 0);
159 gtk_box_pack_start (GTK_BOX (dialog->priv->extra_widget), combo, TRUE, TRUE, 0);
160
161 dialog->priv->newline_combo = combo;
162 dialog->priv->newline_label = label;
163 dialog->priv->newline_store = store;
164
165 update_newline_visibility (dialog);
166 }
167
168 static void
create_extra_widget(XedFileChooserDialog * dialog)169 create_extra_widget (XedFileChooserDialog *dialog)
170 {
171 dialog->priv->extra_widget = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
172
173 gtk_widget_show (dialog->priv->extra_widget);
174
175 create_option_menu (dialog);
176 create_newline_combo (dialog);
177
178 gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (dialog), dialog->priv->extra_widget);
179
180 }
181
182 static void
action_changed(XedFileChooserDialog * dialog,GParamSpec * pspec,gpointer data)183 action_changed (XedFileChooserDialog *dialog,
184 GParamSpec *pspec,
185 gpointer data)
186 {
187 GtkFileChooserAction action;
188
189 action = gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog));
190
191 switch (action)
192 {
193 case GTK_FILE_CHOOSER_ACTION_OPEN:
194 g_object_set (dialog->priv->option_menu, "save_mode", FALSE, NULL);
195 gtk_widget_show (dialog->priv->option_menu);
196 break;
197 case GTK_FILE_CHOOSER_ACTION_SAVE:
198 g_object_set (dialog->priv->option_menu, "save_mode", TRUE, NULL);
199 gtk_widget_show (dialog->priv->option_menu);
200 break;
201 default:
202 gtk_widget_hide (dialog->priv->option_menu);
203 }
204
205 update_newline_visibility (dialog);
206 }
207
208 static void
filter_changed(XedFileChooserDialog * dialog,GParamSpec * pspec,gpointer data)209 filter_changed (XedFileChooserDialog *dialog,
210 GParamSpec *pspec,
211 gpointer data)
212 {
213 GtkFileFilter *filter;
214
215 filter = gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (dialog));
216 if (filter != NULL)
217 {
218 const gchar *name;
219 gint id = 0;
220
221 name = gtk_file_filter_get_name (filter);
222 g_return_if_fail (name != NULL);
223
224 if (strcmp (name, ALL_TEXT_FILES) == 0)
225 {
226 id = 1;
227 }
228
229 xed_debug_message (DEBUG_COMMANDS, "Active filter: %s (%d)", name, id);
230
231 g_settings_set_int (dialog->priv->filter_settings, XED_SETTINGS_ACTIVE_FILE_FILTER, id);
232 }
233 }
234
235 /* FIXME: use globs too - Paolo (Aug. 27, 2007) */
236 static gboolean
all_text_files_filter(const GtkFileFilterInfo * filter_info,gpointer data)237 all_text_files_filter (const GtkFileFilterInfo *filter_info,
238 gpointer data)
239 {
240 static GSList *known_mime_types = NULL;
241 GSList *mime_types;
242
243 if (known_mime_types == NULL)
244 {
245 GtkSourceLanguageManager *lm;
246 const gchar * const *languages;
247
248 lm = gtk_source_language_manager_get_default ();
249 languages = gtk_source_language_manager_get_language_ids (lm);
250
251 while ((languages != NULL) && (*languages != NULL))
252 {
253 gchar **mime_types;
254 gint i;
255 GtkSourceLanguage *lang;
256
257 lang = gtk_source_language_manager_get_language (lm, *languages);
258 g_return_val_if_fail (GTK_SOURCE_IS_LANGUAGE (lang), FALSE);
259 ++languages;
260
261 mime_types = gtk_source_language_get_mime_types (lang);
262 if (mime_types == NULL)
263 {
264 continue;
265 }
266
267 for (i = 0; mime_types[i] != NULL; i++)
268 {
269 if (!g_content_type_is_a (mime_types[i], "text/plain"))
270 {
271 xed_debug_message (DEBUG_COMMANDS,
272 "Mime-type %s is not related to text/plain",
273 mime_types[i]);
274
275 known_mime_types = g_slist_prepend (known_mime_types, g_strdup (mime_types[i]));
276 }
277 }
278
279 g_strfreev (mime_types);
280 }
281
282 /* known_mime_types always has "text/plain" as first item" */
283 known_mime_types = g_slist_prepend (known_mime_types, g_strdup ("text/plain"));
284 }
285
286 /* known mime_types contains "text/plain" and then the list of mime-types unrelated to "text/plain"
287 * that xed recognizes */
288
289 if (filter_info->mime_type == NULL)
290 {
291 return FALSE;
292 }
293
294 /*
295 * The filter is matching:
296 * - the mime-types beginning with "text/"
297 * - the mime-types inheriting from a known mime-type (note the text/plain is
298 * the first known mime-type)
299 */
300
301 if (strncmp (filter_info->mime_type, "text/", 5) == 0)
302 {
303 return TRUE;
304 }
305
306 mime_types = known_mime_types;
307 while (mime_types != NULL)
308 {
309 if (g_content_type_is_a (filter_info->mime_type, (const gchar*)mime_types->data))
310 {
311 return TRUE;
312 }
313
314 mime_types = g_slist_next (mime_types);
315 }
316
317 return FALSE;
318 }
319
320 static void
xed_file_chooser_dialog_init(XedFileChooserDialog * dialog)321 xed_file_chooser_dialog_init (XedFileChooserDialog *dialog)
322 {
323 dialog->priv = xed_file_chooser_dialog_get_instance_private (dialog);
324
325 dialog->priv->filter_settings = g_settings_new ("org.x.editor.state.file-filter");
326 }
327
328 static GtkWidget *
xed_file_chooser_dialog_new_valist(const gchar * title,GtkWindow * parent,GtkFileChooserAction action,const GtkSourceEncoding * encoding,const gchar * first_button_text,va_list varargs)329 xed_file_chooser_dialog_new_valist (const gchar *title,
330 GtkWindow *parent,
331 GtkFileChooserAction action,
332 const GtkSourceEncoding *encoding,
333 const gchar *first_button_text,
334 va_list varargs)
335 {
336 GtkWidget *result;
337 const char *button_text = first_button_text;
338 gint response_id;
339 GtkFileFilter *filter;
340 gint active_filter;
341
342 g_return_val_if_fail (parent != NULL, NULL);
343
344 result = g_object_new (XED_TYPE_FILE_CHOOSER_DIALOG,
345 "title", title,
346 "local-only", FALSE,
347 "action", action,
348 "select-multiple", action == GTK_FILE_CHOOSER_ACTION_OPEN,
349 NULL);
350
351 create_extra_widget (XED_FILE_CHOOSER_DIALOG (result));
352
353 g_signal_connect (result, "notify::action",
354 G_CALLBACK (action_changed), NULL);
355
356 if (encoding != NULL)
357 {
358 xed_encodings_combo_box_set_selected_encoding (
359 XED_ENCODINGS_COMBO_BOX (XED_FILE_CHOOSER_DIALOG (result)->priv->option_menu), encoding);
360 }
361
362 active_filter = g_settings_get_int (XED_FILE_CHOOSER_DIALOG (result)->priv->filter_settings,
363 XED_SETTINGS_ACTIVE_FILE_FILTER);
364 xed_debug_message (DEBUG_COMMANDS, "Active filter: %d", active_filter);
365
366 /* Filters */
367 filter = gtk_file_filter_new ();
368
369 gtk_file_filter_set_name (filter, ALL_FILES);
370 gtk_file_filter_add_pattern (filter, "*");
371 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (result), filter);
372 gtk_file_chooser_set_action (GTK_FILE_CHOOSER (result), action);
373
374 if (active_filter != 1)
375 {
376 /* Make this filter the default */
377 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (result), filter);
378 }
379
380 filter = gtk_file_filter_new ();
381 gtk_file_filter_set_name (filter, ALL_TEXT_FILES);
382 gtk_file_filter_add_custom (filter, GTK_FILE_FILTER_MIME_TYPE, all_text_files_filter, NULL, NULL);
383 gtk_file_chooser_add_filter (GTK_FILE_CHOOSER (result), filter);
384
385 if (active_filter == 1)
386 {
387 /* Make this filter the default */
388 gtk_file_chooser_set_filter (GTK_FILE_CHOOSER (result), filter);
389 }
390
391 g_signal_connect (result, "notify::filter",
392 G_CALLBACK (filter_changed), NULL);
393
394 gtk_window_set_transient_for (GTK_WINDOW (result), parent);
395 gtk_window_set_destroy_with_parent (GTK_WINDOW (result), TRUE);
396
397 while (button_text)
398 {
399 response_id = va_arg (varargs, gint);
400
401 gtk_dialog_add_button (GTK_DIALOG (result), button_text, response_id);
402 if ((response_id == GTK_RESPONSE_OK) ||
403 (response_id == GTK_RESPONSE_ACCEPT) ||
404 (response_id == GTK_RESPONSE_YES) ||
405 (response_id == GTK_RESPONSE_APPLY))
406 {
407 gtk_dialog_set_default_response (GTK_DIALOG (result), response_id);
408 }
409
410 button_text = va_arg (varargs, const gchar *);
411 }
412
413 return result;
414 }
415
416 /**
417 * xed_file_chooser_dialog_new:
418 * @title: (allow-none): Title of the dialog, or %NULL
419 * @parent: (allow-none): Transient parent of the dialog, or %NULL
420 * @action: Open or save mode for the dialog
421 * @encoding: (allow-none): a #GtkSourceEncoding to pre-select
422 * @first_button_text: (allow-none): stock ID or text to go in
423 * the first button, or %NULL
424 * @...: (allow-none): response ID for the first button, then
425 * additional (button, id) pairs, ending with %NULL
426 *
427 * Creates a new #XedFileChooserDialog. This function is analogous to
428 * gtk_dialog_new_with_buttons().
429 *
430 * Return value: a new #XedFileChooserDialog
431 *
432 **/
433 GtkWidget *
xed_file_chooser_dialog_new(const gchar * title,GtkWindow * parent,GtkFileChooserAction action,const GtkSourceEncoding * encoding,const gchar * first_button_text,...)434 xed_file_chooser_dialog_new (const gchar *title,
435 GtkWindow *parent,
436 GtkFileChooserAction action,
437 const GtkSourceEncoding *encoding,
438 const gchar *first_button_text,
439 ...)
440 {
441 GtkWidget *result;
442 va_list varargs;
443
444 va_start (varargs, first_button_text);
445 result = xed_file_chooser_dialog_new_valist (title, parent, action, encoding, first_button_text, varargs);
446 va_end (varargs);
447
448 return result;
449 }
450
451 void
xed_file_chooser_dialog_set_encoding(XedFileChooserDialog * dialog,const GtkSourceEncoding * encoding)452 xed_file_chooser_dialog_set_encoding (XedFileChooserDialog *dialog,
453 const GtkSourceEncoding *encoding)
454 {
455 g_return_if_fail (XED_IS_FILE_CHOOSER_DIALOG (dialog));
456 g_return_if_fail (XED_IS_ENCODINGS_COMBO_BOX (dialog->priv->option_menu));
457
458 xed_encodings_combo_box_set_selected_encoding (XED_ENCODINGS_COMBO_BOX (dialog->priv->option_menu), encoding);
459 }
460
461 const GtkSourceEncoding *
xed_file_chooser_dialog_get_encoding(XedFileChooserDialog * dialog)462 xed_file_chooser_dialog_get_encoding (XedFileChooserDialog *dialog)
463 {
464 g_return_val_if_fail (XED_IS_FILE_CHOOSER_DIALOG (dialog), NULL);
465 g_return_val_if_fail (XED_IS_ENCODINGS_COMBO_BOX (dialog->priv->option_menu), NULL);
466 g_return_val_if_fail ((gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_OPEN ||
467 gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE), NULL);
468
469 return xed_encodings_combo_box_get_selected_encoding (XED_ENCODINGS_COMBO_BOX (dialog->priv->option_menu));
470 }
471
472 static void
set_enum_combo(GtkComboBox * combo,gint value)473 set_enum_combo (GtkComboBox *combo,
474 gint value)
475 {
476 GtkTreeIter iter;
477 GtkTreeModel *model;
478
479 model = gtk_combo_box_get_model (combo);
480
481 if (!gtk_tree_model_get_iter_first (model, &iter))
482 {
483 return;
484 }
485
486 do
487 {
488 gint nt;
489
490 gtk_tree_model_get (model, &iter, 1, &nt, -1);
491
492 if (value == nt)
493 {
494 gtk_combo_box_set_active_iter (combo, &iter);
495 break;
496 }
497 } while (gtk_tree_model_iter_next (model, &iter));
498 }
499
500 void
xed_file_chooser_dialog_set_newline_type(XedFileChooserDialog * dialog,GtkSourceNewlineType newline_type)501 xed_file_chooser_dialog_set_newline_type (XedFileChooserDialog *dialog,
502 GtkSourceNewlineType newline_type)
503 {
504 g_return_if_fail (XED_IS_FILE_CHOOSER_DIALOG (dialog));
505 g_return_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE);
506
507 set_enum_combo (GTK_COMBO_BOX (dialog->priv->newline_combo), newline_type);
508 }
509
510 GtkSourceNewlineType
xed_file_chooser_dialog_get_newline_type(XedFileChooserDialog * dialog)511 xed_file_chooser_dialog_get_newline_type (XedFileChooserDialog *dialog)
512 {
513 GtkTreeIter iter;
514 GtkSourceNewlineType newline_type;
515
516 g_return_val_if_fail (XED_IS_FILE_CHOOSER_DIALOG (dialog), GTK_SOURCE_NEWLINE_TYPE_DEFAULT);
517 g_return_val_if_fail (gtk_file_chooser_get_action (GTK_FILE_CHOOSER (dialog)) == GTK_FILE_CHOOSER_ACTION_SAVE,
518 GTK_SOURCE_NEWLINE_TYPE_DEFAULT);
519
520 gtk_combo_box_get_active_iter (GTK_COMBO_BOX (dialog->priv->newline_combo), &iter);
521
522 gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->newline_store), &iter, 1, &newline_type, -1);
523
524 return newline_type;
525 }
526