1 /* GTK - The GIMP Toolkit
2 * gtkrecentchooser.c - Abstract interface for recent file selectors GUIs
4 * Copyright (C) 2006, Emmanuele Bassi
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
24 #include "gtkrecentchooser.h"
25 #include "gtkrecentchooserprivate.h"
26 #include "gtkrecentmanager.h"
28 #include "gtktypebuiltins.h"
29 #include "gtkprivate.h"
30 #include "gtkmarshalers.h"
42 static void gtk_recent_chooser_class_init (gpointer g_iface);
44 static guint chooser_signals[LAST_SIGNAL] = { 0, };
47 gtk_recent_chooser_get_type (void)
49 static GType chooser_type = 0;
53 static const GTypeInfo chooser_info =
55 sizeof (GtkRecentChooserIface),
57 NULL, /* base_finalize */
58 (GClassInitFunc) gtk_recent_chooser_class_init,
61 chooser_type = g_type_register_static (G_TYPE_INTERFACE,
62 I_("GtkRecentChooser"),
65 g_type_interface_add_prerequisite (chooser_type, GTK_TYPE_OBJECT);
72 gtk_recent_chooser_class_init (gpointer g_iface)
74 GType iface_type = G_TYPE_FROM_INTERFACE (g_iface);
77 * GtkRecentChooser::selection-changed
78 * @chooser: the object which received the signal
80 * This signal is emitted when there is a change in the set of
81 * selected recently used resources. This can happen when a user
82 * modifies the selection with the mouse or the keyboard, or when
83 * explicitely calling functions to change the selection.
87 chooser_signals[SELECTION_CHANGED] =
88 g_signal_new (I_("selection-changed"),
91 G_STRUCT_OFFSET (GtkRecentChooserIface, selection_changed),
93 g_cclosure_marshal_VOID__VOID,
97 * GtkRecentChooser::item-activated
98 * @chooser: the object which received the signal
100 * This signal is emitted when the user "activates" a recent item
101 * in the recent chooser. This can happen by double-clicking on an item
102 * in the recently used resources list, or by pressing
103 * <keycap>Enter</keycap>.
107 chooser_signals[ITEM_ACTIVATED] =
108 g_signal_new (I_("item-activated"),
111 G_STRUCT_OFFSET (GtkRecentChooserIface, item_activated),
113 g_cclosure_marshal_VOID__VOID,
116 g_object_interface_install_property (g_iface,
117 g_param_spec_object ("recent-manager",
118 P_("Recent Manager"),
119 P_("The RecentManager object to use"),
120 GTK_TYPE_RECENT_MANAGER,
121 G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
122 g_object_interface_install_property (g_iface,
123 g_param_spec_boolean ("show-private",
125 P_("Whether the private items should be displayed"),
128 g_object_interface_install_property (g_iface,
129 g_param_spec_boolean ("show-tips",
131 P_("Whether there should be a tooltip on the item"),
134 g_object_interface_install_property (g_iface,
135 g_param_spec_boolean ("show-icons",
137 P_("Whether there should be an icon near the item"),
140 g_object_interface_install_property (g_iface,
141 g_param_spec_boolean ("show-not-found",
142 P_("Show Not Found"),
143 P_("Whether the items pointing to unavailable resources should be displayed"),
146 g_object_interface_install_property (g_iface,
147 g_param_spec_boolean ("select-multiple",
148 P_("Select Multiple"),
149 P_("Whether to allow multiple items to be selected"),
152 g_object_interface_install_property (g_iface,
153 g_param_spec_boolean ("local-only",
155 P_("Whether the selected resource(s) should be limited to local file: URIs"),
158 g_object_interface_install_property (g_iface,
159 g_param_spec_int ("limit",
161 P_("The maximum number of items to be displayed"),
166 g_object_interface_install_property (g_iface,
167 g_param_spec_enum ("sort-type",
169 P_("The sorting order of the items displayed"),
170 GTK_TYPE_RECENT_SORT_TYPE,
171 GTK_RECENT_SORT_NONE,
173 g_object_interface_install_property (g_iface,
174 g_param_spec_object ("filter",
176 P_("The current filter for selecting which resources are displayed"),
177 GTK_TYPE_RECENT_FILTER,
182 gtk_recent_chooser_error_quark (void)
184 static GQuark error_quark = 0;
186 error_quark = g_quark_from_static_string ("gtk-recent-chooser-error-quark");
191 * _gtk_recent_chooser_get_recent_manager:
192 * @chooser: a #GtkRecentChooser
194 * Gets the #GtkRecentManager used by @chooser.
196 * Return value: the recent manager for @chooser.
201 _gtk_recent_chooser_get_recent_manager (GtkRecentChooser *chooser)
203 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
205 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->get_recent_manager (chooser);
209 * gtk_recent_chooser_set_show_private:
210 * @chooser: a #GtkRecentChooser
211 * @show_private: %TRUE to show private items, %FALSE otherwise
213 * Whether to show recently used resources marked registered as private.
218 gtk_recent_chooser_set_show_private (GtkRecentChooser *chooser,
219 gboolean show_private)
221 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
223 g_object_set (chooser, "show-private", show_private, NULL);
227 * gtk_recent_chooser_get_show_private:
228 * @chooser: a #GtkRecentChooser
230 * Returns whether @chooser should display recently used resources
231 * registered as private.
233 * Return value: %TRUE if the recent chooser should show private items,
239 gtk_recent_chooser_get_show_private (GtkRecentChooser *chooser)
241 gboolean show_private;
243 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
245 g_object_get (chooser, "show-private", &show_private, NULL);
251 * gtk_recent_chooser_set_show_not_found:
252 * @chooser: a #GtkRecentChooser
253 * @show_not_found: whether to show the local items we didn't find
255 * Sets whether @chooser should display the recently used resources that
256 * it didn't find. This only applies to local resources.
261 gtk_recent_chooser_set_show_not_found (GtkRecentChooser *chooser,
262 gboolean show_not_found)
264 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
266 g_object_set (chooser, "show-not-found", show_not_found, NULL);
270 * gtk_recent_chooser_get_show_not_found:
271 * @chooser: a #GtkRecentChooser
273 * Retrieves whether @chooser should show the recently used resources that
276 * Return value: %TRUE if the resources not found should be displayed, and
282 gtk_recent_chooser_get_show_not_found (GtkRecentChooser *chooser)
284 gboolean show_not_found;
286 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
288 g_object_get (chooser, "show-not-found", &show_not_found, NULL);
290 return show_not_found;
294 * gtk_recent_chooser_set_show_icons:
295 * @chooser: a #GtkRecentChooser
296 * @show_icons: whether to show an icon near the resource
298 * Sets whether @chooser should show an icon near the resource when
304 gtk_recent_chooser_set_show_icons (GtkRecentChooser *chooser,
307 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
309 g_object_set (chooser, "show-icons", show_icons, NULL);
313 * gtk_recent_chooser_get_show_icons:
314 * @chooser: a #GtkRecentChooser
316 * Retrieves whether @chooser should show an icon near the resource.
318 * Return value: %TRUE if the icons should be displayed, %FALSE otherwise.
323 gtk_recent_chooser_get_show_icons (GtkRecentChooser *chooser)
327 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
329 g_object_get (chooser, "show-icons", &show_icons, NULL);
335 * gtk_recent_chooser_set_select_multiple:
336 * @chooser: a #GtkRecentChooser
337 * @select_multiple: %TRUE if @chooser can select more than one item
339 * Sets whether @chooser can select multiple items.
344 gtk_recent_chooser_set_select_multiple (GtkRecentChooser *chooser,
345 gboolean select_multiple)
347 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
349 g_object_set (chooser, "select-multiple", select_multiple, NULL);
353 * gtk_recent_chooser_get_select_multiple:
354 * @chooser: a #GtkRecentChooser
356 * Gets whether @chooser can select multiple items.
358 * Return value: %TRUE if @chooser can select more than one item.
363 gtk_recent_chooser_get_select_multiple (GtkRecentChooser *chooser)
365 gboolean select_multiple;
367 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
369 g_object_get (chooser, "select-multiple", &select_multiple, NULL);
371 return select_multiple;
375 * gtk_recent_chooser_set_local_only:
376 * @chooser: a #GtkRecentChooser
377 * @local_only: %TRUE if only local files can be shown
379 * Sets whether only local resources, that is resources using the file:// URI
380 * scheme, should be shown in the recently used resources selector. If
381 * @local_only is %TRUE (the default) then the shown resources are guaranteed
382 * to be accessible through the operating system native file system.
387 gtk_recent_chooser_set_local_only (GtkRecentChooser *chooser,
390 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
392 g_object_set (chooser, "local-only", local_only, NULL);
396 * gtk_recent_chooser_get_local_only:
397 * @chooser: a #GtkRecentChooser
399 * Gets whether only local resources should be shown in the recently used
400 * resources selector. See gtk_recent_chooser_set_local_only()
402 * Return value: %TRUE if only local resources should be shown.
407 gtk_recent_chooser_get_local_only (GtkRecentChooser *chooser)
411 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
413 g_object_get (chooser, "local-only", &local_only, NULL);
419 * gtk_recent_chooser_set_limit:
420 * @chooser: a #GtkRecentChooser
421 * @limit: a positive integer, or -1 for all items
423 * Sets the number of items that should be returned by
424 * gtk_recent_chooser_get_items() and gtk_recent_chooser_get_uris().
429 gtk_recent_chooser_set_limit (GtkRecentChooser *chooser,
432 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
434 g_object_set (chooser, "limit", limit, NULL);
438 * gtk_recent_chooser_get_limit:
439 * @chooser: a #GtkRecentChooser
441 * Gets the number of items returned by gtk_recent_chooser_get_items()
442 * and gtk_recent_chooser_get_uris().
444 * Return value: A positive integer, or -1 meaning that all items are
450 gtk_recent_chooser_get_limit (GtkRecentChooser *chooser)
454 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), 10);
456 g_object_get (chooser, "limit", &limit, NULL);
462 * gtk_recent_chooser_set_show_tips:
463 * @chooser: a #GtkRecentChooser
464 * @show_tips: %TRUE if tooltips should be shown
466 * Sets whether to show a tooltips on the widget.
471 gtk_recent_chooser_set_show_tips (GtkRecentChooser *chooser,
474 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
476 g_object_set (chooser, "show-tips", show_tips, NULL);
480 * gtk_recent_chooser_get_show_tips:
481 * @chooser: a #GtkRecentChooser
483 * Gets whether @chooser should display tooltips.
485 * Return value: %TRUE if the recent chooser should show tooltips,
491 gtk_recent_chooser_get_show_tips (GtkRecentChooser *chooser)
495 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
497 g_object_get (chooser, "show-tips", &show_tips, NULL);
503 * gtk_recent_chooser_set_show_numbers:
504 * @chooser: a #GtkRecentChooser
505 * @show_private: %TRUE to show numbers, %FALSE otherwise
507 * Whether to show recently used resources prepended by a unique number.
512 gtk_recent_chooser_set_show_numbers (GtkRecentChooser *chooser,
513 gboolean show_numbers)
515 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
517 g_object_set (chooser, "show-numbers", show_numbers, NULL);
521 * gtk_recent_chooser_get_show_numbers:
522 * @chooser: a #GtkRecentChooser
524 * Returns whether @chooser should display recently used resources
525 * prepended by a unique number.
527 * Return value: %TRUE if the recent chooser should show display numbers,
533 gtk_recent_chooser_get_show_numbers (GtkRecentChooser *chooser)
535 gboolean show_numbers;
537 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
539 g_object_get (chooser, "show-numbers", &show_numbers, NULL);
546 * gtk_recent_chooser_set_sort_type:
547 * @chooser: a #GtkRecentChooser
548 * @sort_type: sort order that the chooser should use
550 * Changes the sorting order of the recently used resources list displayed by
556 gtk_recent_chooser_set_sort_type (GtkRecentChooser *chooser,
557 GtkRecentSortType sort_type)
559 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
561 g_object_set (chooser, "sort-type", sort_type, NULL);
565 * gtk_recent_chooser_get_sort_type:
566 * @chooser: a #GtkRecentChooser
568 * Gets the value set by gtk_recent_chooser_set_sort_type().
570 * Return value: the sorting order of the @chooser.
575 gtk_recent_chooser_get_sort_type (GtkRecentChooser *chooser)
577 GtkRecentSortType sort_type;
579 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), GTK_RECENT_SORT_NONE);
581 g_object_get (chooser, "sort-type", &sort_type, NULL);
587 * gtk_recent_chooser_set_sort_func:
588 * @chooser: a #GtkRecentChooser
589 * @sort_func: the comparison function
590 * @sort_data: user data to pass to @sort_func, or %NULL
591 * @destroy_data: destroy notifier for @sort_data, or %NULL
593 * Sets the comparison function used when sorting to be @sort_func. If
594 * the @chooser has the sort type set to #GTK_RECENT_SORT_CUSTOM then
595 * the chooser will sort using this function.
597 * To the comparison function will be passed two #GtkRecentInfo structs and
598 * @sort_data; @sort_func should return a positive integer if the first
599 * item comes before the second, zero if the two items are equal and
600 * a negative integer if the first item comes after the second.
605 gtk_recent_chooser_set_sort_func (GtkRecentChooser *chooser,
606 GtkRecentSortFunc sort_func,
608 GDestroyNotify data_destroy)
610 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
612 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->set_sort_func (chooser,
619 * gtk_recent_chooser_set_current_uri:
620 * @chooser: a #GtkRecentChooser
622 * @error: return location for a #GError, or %NULL
624 * Sets @uri as the current URI for @chooser.
626 * Return value: %TRUE if the URI was found.
631 gtk_recent_chooser_set_current_uri (GtkRecentChooser *chooser,
635 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
637 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->set_current_uri (chooser, uri, error);
641 * gtk_recent_chooser_get_current_uri:
642 * @chooser: a #GtkRecentChooser
644 * Gets the URI currently selected by @chooser.
646 * Return value: a newly allocated string holding a URI.
651 gtk_recent_chooser_get_current_uri (GtkRecentChooser *chooser)
653 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
655 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->get_current_uri (chooser);
659 * gtk_recent_chooser_get_current_item:
660 * @chooser: a #GtkRecentChooser
662 * Gets the #GtkRecentInfo currently selected by @chooser.
664 * Return value: a #GtkRecentInfo. Use gtk_recent_info_unref() when
665 * when you have finished using it.
670 gtk_recent_chooser_get_current_item (GtkRecentChooser *chooser)
672 GtkRecentManager *manager;
673 GtkRecentInfo *retval;
676 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
678 uri = gtk_recent_chooser_get_current_uri (chooser);
682 manager = _gtk_recent_chooser_get_recent_manager (chooser);
683 retval = gtk_recent_manager_lookup_item (manager, uri, NULL);
690 * gtk_recent_chooser_select_uri:
691 * @chooser: a #GtkRecentChooser
693 * @error: return location for a #GError, or %NULL
695 * Selects @uri inside @chooser.
697 * Return value: %TRUE if @uri was found.
702 gtk_recent_chooser_select_uri (GtkRecentChooser *chooser,
706 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), FALSE);
708 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->select_uri (chooser, uri, error);
712 * gtk_recent_chooser_unselect_uri:
713 * @chooser: a #GtkRecentChooser
716 * Unselects @uri inside @chooser.
721 gtk_recent_chooser_unselect_uri (GtkRecentChooser *chooser,
724 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
726 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->unselect_uri (chooser, uri);
730 * gtk_recent_chooser_select_all:
731 * @chooser: a #GtkRecentChooser
733 * Selects all the items inside @chooser, if the @chooser supports
734 * multiple selection.
739 gtk_recent_chooser_select_all (GtkRecentChooser *chooser)
741 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
743 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->select_all (chooser);
747 * gtk_recent_chooser_unselect_all:
748 * @chooser: a #GtkRecentChooser
750 * Unselects all the items inside @chooser.
755 gtk_recent_chooser_unselect_all (GtkRecentChooser *chooser)
757 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
759 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->unselect_all (chooser);
763 * gtk_recent_chooser_get_items:
764 * @chooser: a #GtkRecentChooser
766 * Gets the list of recently used resources in form of #GtkRecentInfo objects.
768 * The return value of this function is affected by the "sort-type" and
769 * "limit" properties of @chooser.
771 * Return value: A newly allocated list of #GtkRecentInfo objects. You should
772 * use gtk_recent_info_unref() on every item of the list, and then free
773 * the list itself using g_list_free().
778 gtk_recent_chooser_get_items (GtkRecentChooser *chooser)
780 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
782 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->get_items (chooser);
786 * gtk_recent_chooser_get_uris:
787 * @chooser: a #GtkRecentChooser
788 * @length: return location for a the length of the URI list, or %NULL
790 * Gets the URI of the recently used resources.
792 * The return value of this function is affected by the "sort-type" and "limit"
793 * properties of @chooser.
795 * Since the returned array is %NULL terminated, @length may be %NULL.
797 * Return value: A newly allocated, %NULL terminated array of strings. Use
798 * g_strfreev() to free it.
803 gtk_recent_chooser_get_uris (GtkRecentChooser *chooser,
810 items = gtk_recent_chooser_get_items (chooser);
814 n_items = g_list_length (items);
815 retval = g_new0 (gchar *, n_items + 1);
817 for (l = items, i = 0; l != NULL; l = l->next)
819 GtkRecentInfo *info = (GtkRecentInfo *) l->data;
822 g_assert (info != NULL);
824 uri = gtk_recent_info_get_uri (info);
825 g_assert (uri != NULL);
827 retval[i++] = g_strdup (uri);
834 g_list_foreach (items,
835 (GFunc) gtk_recent_info_unref,
843 * gtk_recent_chooser_add_filter:
844 * @chooser: a #GtkRecentChooser
845 * @filter: a #GtkRecentFilter
847 * Adds @filter to the list of #GtkRecentFilter objects held by @chooser.
849 * If no previous filter objects were defined, this function will call
850 * gtk_recent_chooser_set_filter().
855 gtk_recent_chooser_add_filter (GtkRecentChooser *chooser,
856 GtkRecentFilter *filter)
858 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
860 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->add_filter (chooser, filter);
864 * gtk_recent_chooser_remove_filter:
865 * @chooser: a #GtkRecentChooser
866 * @filter: a #GtkRecentFilter
868 * Removes @filter from the list of #GtkRecentFilter objects held by @chooser.
873 gtk_recent_chooser_remove_filter (GtkRecentChooser *chooser,
874 GtkRecentFilter *filter)
876 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
878 GTK_RECENT_CHOOSER_GET_IFACE (chooser)->remove_filter (chooser, filter);
882 * gtk_recent_chooser_list_filters:
883 * @chooser: a #GtkRecentChooser
885 * Gets the #GtkRecentFilter objects held by @chooser.
887 * Return value: A singly linked list of #GtkRecentFilter objects. You
888 * should just free the returned list using g_slist_free().
893 gtk_recent_chooser_list_filters (GtkRecentChooser *chooser)
895 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
897 return GTK_RECENT_CHOOSER_GET_IFACE (chooser)->list_filters (chooser);
901 * gtk_recent_chooser_set_filter:
902 * @chooser: a #GtkRecentChooser
903 * @filter: a #GtkRecentFilter
905 * Sets @filter as the current #GtkRecentFilter object used by @chooser
906 * to affect the displayed recently used resources.
911 gtk_recent_chooser_set_filter (GtkRecentChooser *chooser,
912 GtkRecentFilter *filter)
914 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
916 g_object_set (G_OBJECT (chooser), "filter", filter, NULL);
920 * gtk_recent_chooser_get_filter:
921 * @chooser: a #GtkRecentChooser
923 * Gets the #GtkRecentFilter object currently used by @chooser to affect
924 * the display of the recently used resources.
926 * Return value: a #GtkRecentFilter object.
931 gtk_recent_chooser_get_filter (GtkRecentChooser *chooser)
933 GtkRecentFilter *filter;
935 g_return_val_if_fail (GTK_IS_RECENT_CHOOSER (chooser), NULL);
937 g_object_get (G_OBJECT (chooser), "filter", &filter, NULL);
939 /* we need this hack because g_object_get() increases the refcount
940 * of the returned object; see also gtk_file_chooser_get_filter()
941 * inside gtkfilechooser.c
944 g_object_unref (filter);
950 _gtk_recent_chooser_item_activated (GtkRecentChooser *chooser)
952 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
954 g_signal_emit (chooser, chooser_signals[ITEM_ACTIVATED], 0);
958 _gtk_recent_chooser_selection_changed (GtkRecentChooser *chooser)
960 g_return_if_fail (GTK_IS_RECENT_CHOOSER (chooser));
962 g_signal_emit (chooser, chooser_signals[SELECTION_CHANGED], 0);
965 #define __GTK_RECENT_CHOOSER_C__
966 #include "gtkaliasdef.c"