Codebase list xapp / b997908
xapp-gtk3-module.c: Override GObject's constructed vfunc to inject the favorites uri into GtkPlacesSidebars rather than hooking onto a signal. This is much cleaner, and has the bonus of affecting more than just file dialogs. Michael Webster 3 years ago
1 changed file(s) with 19 addition(s) and 50 deletion(s). Raw diff Collapse all Expand all
88 * The sole purpose of this module currently is to add a 'Favorites'
99 * shortcut to GtkFileChooser dialogs.
1010 *
11 * In gtk_module_init, the XAppFavorites singleton is initialized, and
12 * the 'favorites' uri scheme is added to the default vfs. Ordinarily
13 * non-file:// schemes aren't supported in these dialogs unless their
14 * 'local-only' property is set to FALSE. Since favorites are shortcuts
15 * to locally-available files, we lie to the chooser setup by returning
16 * "/" instead of NULL when g_file_get_path ("favorites:///") is called.
11 * In gtk_module_init, the 'favorites' uri scheme is added to the default
12 * vfs. Ordinarily, non-file:// schemes aren't supported in these dialogs
13 * unless their 'local-only' property is set to FALSE. Since favorites
14 * are shortcuts to locally-available files, we lie to the chooser setup
15 * by returning "/" instead of NULL when g_file_get_path ("favorites:///")
16 * is called.
1717 */
1818
19
20 /* Make sure GCC doesn't warn us about a missing prototype for this
21 * exported function */
2219 void gtk_module_init (gint *argc, gchar ***argv[]);
23
24 static gboolean
25 selection_changed_cb (GSignalInvocationHint *ihint,
26 guint n_param_values,
27 const GValue *param_values,
28 gpointer data)
29 {
30 GtkFileChooser *chooser = GTK_FILE_CHOOSER (g_value_get_object (&param_values[0]));
31 GSList *list, *i;
32 gboolean already_applied = FALSE;
33 list = gtk_file_chooser_list_shortcut_folder_uris (chooser);
34
35 for (i = list; i != NULL; i = i->next)
36 {
37 if (g_strcmp0 ((gchar *) i->data, "favorites:///") == 0)
38 {
39 already_applied = TRUE;
40 break;
41 }
42 }
43
44 g_slist_free_full (list, g_free);
45
46 if (!already_applied)
47 {
48 gtk_file_chooser_add_shortcut_folder_uri (chooser, "favorites:///", NULL);
49 }
50
51 return TRUE;
52 }
20 static void (* original_sidebar_constructed) (GObject *object);
5321
5422 static void
55 add_chooser_hook (GType type)
23 xapp_sidebar_constructed (GObject *object)
5624 {
57 GTypeClass *type_class;
58 guint sigid;
25 GtkPlacesSidebar *sidebar = GTK_PLACES_SIDEBAR (object);
5926
60 type_class = g_type_class_ref (type);
27 (* original_sidebar_constructed) (object);
6128
62 sigid = g_signal_lookup ("selection-changed", type);
63 g_signal_add_emission_hook (sigid, 0, selection_changed_cb, NULL, NULL);
64
65 g_type_class_unref (type_class);
29 GFile *favorites = g_file_new_for_uri ("favorites:///");
30 gtk_places_sidebar_add_shortcut (sidebar, favorites);
31 g_object_unref (favorites);
6632 }
6733
6834 G_MODULE_EXPORT void gtk_module_init (gint *argc, gchar ***argv[]) {
35 GObjectClass *object_class;
36
6937 // This won't instantiate XAppFavorites but will register the uri so
7038 // it can be used by apps (like pix which doesn't use the favorites api,
7139 // but just adds favorites:/// to its sidebar.)
7240 init_favorite_vfs ();
7341
74 add_chooser_hook (GTK_TYPE_FILE_CHOOSER_WIDGET);
75 add_chooser_hook (GTK_TYPE_FILE_CHOOSER_DIALOG);
76 add_chooser_hook (GTK_TYPE_FILE_CHOOSER_BUTTON);
42 object_class = g_type_class_ref (GTK_TYPE_PLACES_SIDEBAR);
43
44 original_sidebar_constructed = object_class->constructed;
45 object_class->constructed = xapp_sidebar_constructed;
7746 }
7847
7948 G_MODULE_EXPORT gchar* g_module_check_init (GModule *module);