summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Mena Quintero <federico@gnome.org>2011-06-27 15:30:59 -0500
committerFederico Mena Quintero <federico@gnome.org>2011-07-01 19:01:24 -0500
commit904ad6620f4fbf39ab9dbfc43fc9a9819fa4b198 (patch)
treed030094bad41a9e4240c9d50b51703eacafdfe63
parent668790ddee0f8c53e3409932e28c4f6d0077e62e (diff)
downloadgtk+-904ad6620f4fbf39ab9dbfc43fc9a9819fa4b198.tar.gz
Utility function to extract folders from the recently-used list
This extracts the parent folders from the items in the recently-used list. We'll use it in the file chooser to present a list of recently-used folders. Signed-off-by: Federico Mena Quintero <federico@gnome.org>
-rw-r--r--gtk/gtkfilechooserutils.c72
-rw-r--r--gtk/gtkfilechooserutils.h2
2 files changed, 74 insertions, 0 deletions
diff --git a/gtk/gtkfilechooserutils.c b/gtk/gtkfilechooserutils.c
index e0fefcfe03..4e327a9418 100644
--- a/gtk/gtkfilechooserutils.c
+++ b/gtk/gtkfilechooserutils.c
@@ -360,3 +360,75 @@ delegate_confirm_overwrite (GtkFileChooser *chooser,
g_signal_emit_by_name (data, "confirm-overwrite", &conf);
return conf;
}
+
+static gint
+recent_sort_mru (gconstpointer a,
+ gconstpointer b)
+{
+ GtkRecentInfo *info_a = (GtkRecentInfo *) a;
+ GtkRecentInfo *info_b = (GtkRecentInfo *) b;
+
+ return (gtk_recent_info_get_modified (info_b) - gtk_recent_info_get_modified (info_a));
+}
+
+static GFile *
+get_parent_for_uri (const char *uri)
+{
+ GFile *file;
+ GFile *parent;
+
+ file = g_file_new_for_uri (uri);
+ parent = g_file_get_parent (file);
+
+ g_object_unref (file);
+ return parent;
+
+}
+
+/* Extracts the parent folders out of the recent items, and returns
+ * a list of GFile* for those parents in MRU-first order.
+ */
+GList *
+_gtk_file_chooser_list_recent_folders (GtkRecentManager *manager)
+{
+ GList *infos;
+ GList *l;
+ GList *result;
+ GHashTable *folders;
+
+ result = NULL;
+
+ infos = gtk_recent_manager_get_items (manager);
+ infos = g_list_sort (infos, recent_sort_mru);
+
+ folders = g_hash_table_new (g_file_hash, (GEqualFunc) g_file_equal);
+
+ for (l = infos; l; l = l->next)
+ {
+ GtkRecentInfo *info = l->data;
+ const char *uri;
+ GFile *parent;
+
+ uri = gtk_recent_info_get_uri (info);
+ parent = get_parent_for_uri (uri);
+
+ if (parent)
+ {
+ if (!g_hash_table_lookup (folders, parent))
+ {
+ g_hash_table_insert (folders, parent, (gpointer) 1);
+ result = g_list_prepend (result, g_object_ref (parent));
+ }
+
+ g_object_unref (parent);
+ }
+ }
+
+ result = g_list_reverse (result);
+
+ g_hash_table_destroy (folders);
+ g_list_foreach (infos, (GFunc) gtk_recent_info_unref, NULL);
+ g_list_free (infos);
+
+ return result;
+}
diff --git a/gtk/gtkfilechooserutils.h b/gtk/gtkfilechooserutils.h
index 8b80ab66f9..5187cbd99c 100644
--- a/gtk/gtkfilechooserutils.h
+++ b/gtk/gtkfilechooserutils.h
@@ -52,6 +52,8 @@ void _gtk_file_chooser_set_delegate (GtkFileChooser *receiver,
GQuark _gtk_file_chooser_delegate_get_quark (void) G_GNUC_CONST;
+GList *_gtk_file_chooser_list_recent_folders (GtkRecentManager *manager);
+
G_END_DECLS
#endif /* __GTK_FILE_CHOOSER_UTILS_H__ */