]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentmanager.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkrecentmanager.c
1 /* GTK - The GIMP Toolkit
2  * gtkrecentmanager.c: a manager for the recently used resources
3  *
4  * Copyright (C) 2006 Emmanuele Bassi
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library 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.
10  *
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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * SECTION:gtkrecentmanager
22  * @Title: GtkRecentManager
23  * @short_description: Managing recently used files
24  * @See_Also: #GBookmarkFile, #GtkSettings, #GtkRecentChooser
25  *
26  * #GtkRecentManager provides a facility for adding, removing and
27  * looking up recently used files. Each recently used file is
28  * identified by its URI, and has meta-data associated to it, like
29  * the names and command lines of the applications that have
30  * registered it, the number of time each application has registered
31  * the same file, the mime type of the file and whether the file
32  * should be displayed only by the applications that have
33  * registered it.
34  *
35  * <note><para>The recently used files list is per user.</para></note>
36  *
37  * The #GtkRecentManager acts like a database of all the recently
38  * used files. You can create new #GtkRecentManager objects, but
39  * it is more efficient to use the default manager created by GTK+.
40  *
41  * Adding a new recently used file is as simple as:
42  *
43  * |[
44  * GtkRecentManager *manager;
45  *
46  * manager = gtk_recent_manager_get_default ();
47  * gtk_recent_manager_add_item (manager, file_uri);
48  * ]|
49  *
50  * The #GtkRecentManager will try to gather all the needed information
51  * from the file itself through GIO.
52  *
53  * Looking up the meta-data associated with a recently used file
54  * given its URI requires calling gtk_recent_manager_lookup_item():
55  *
56  * |[
57  * GtkRecentManager *manager;
58  * GtkRecentInfo *info;
59  * GError *error = NULL;
60  *
61  * manager = gtk_recent_manager_get_default ();
62  * info = gtk_recent_manager_lookup_item (manager, file_uri, &amp;error);
63  * if (error)
64  *   {
65  *     g_warning ("Could not find the file: &percnt;s", error-&gt;message);
66  *     g_error_free (error);
67  *   }
68  * else
69  *  {
70  *    /&ast; Use the info object &ast;/
71  *    gtk_recent_info_unref (info);
72  *  }
73  * ]|
74  *
75  * In order to retrieve the list of recently used files, you can use
76  * gtk_recent_manager_get_items(), which returns a list of #GtkRecentInfo
77  * structures.
78  *
79  * A #GtkRecentManager is the model used to populate the contents of
80  * one, or more #GtkRecentChooser implementations.
81  *
82  * <note><para>The maximum age of the recently used files list is
83  * controllable through the #GtkSettings:gtk-recent-files-max-age
84  * property.</para></note>
85  *
86  * Recently used files are supported since GTK+ 2.10.
87  */
88
89 #include "config.h"
90
91 #include <sys/types.h>
92 #include <sys/stat.h>
93 #ifdef HAVE_UNISTD_H
94 #include <unistd.h>
95 #endif
96 #include <errno.h>
97 #include <string.h>
98 #include <stdlib.h>
99 #include <glib.h>
100 #include <glib/gstdio.h>
101 #include <gio/gio.h>
102
103 #include "gtkrecentmanager.h"
104 #include "gtkintl.h"
105 #include "gtksettings.h"
106 #include "gtkstock.h"
107 #include "gtkicontheme.h"
108 #include "gtktypebuiltins.h"
109 #include "gtkprivate.h"
110 #include "gtkmarshalers.h"
111
112 /* the file where we store the recently used items */
113 #define GTK_RECENTLY_USED_FILE  "recently-used.xbel"
114
115 /* return all items by default */
116 #define DEFAULT_LIMIT   -1
117
118 /* keep in sync with xdgmime */
119 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
120
121 typedef struct
122 {
123   gchar *name;
124   gchar *exec;
125   
126   guint count;
127   
128   time_t stamp;
129 } RecentAppInfo;
130
131 /**
132  * GtkRecentInfo:
133  *
134  * #GtkRecentInfo is an opaque data structure
135  * whose members can only be accessed using the provided API.
136  *
137  * #GtkRecentInfo constains all the meta-data
138  * associated with an entry in the recently used files list.
139  *
140  * Since: 2.10
141  */
142 struct _GtkRecentInfo
143 {
144   gchar *uri;
145   
146   gchar *display_name;
147   gchar *description;
148   
149   time_t added;
150   time_t modified;
151   time_t visited;
152   
153   gchar *mime_type;
154   
155   GSList *applications;
156   GHashTable *apps_lookup;
157   
158   GSList *groups;
159   
160   gboolean is_private;
161   
162   GdkPixbuf *icon;
163   
164   gint ref_count;
165 };
166
167 struct _GtkRecentManagerPrivate
168 {
169   gchar *filename;
170
171   guint is_dirty : 1;
172   
173   gint size;
174
175   GBookmarkFile *recent_items;
176
177   GFileMonitor *monitor;
178
179   guint changed_timeout;
180   guint changed_age;
181 };
182
183 enum
184 {
185   PROP_0,
186
187   PROP_FILENAME,  
188   PROP_LIMIT,
189   PROP_SIZE
190 };
191
192 static void     gtk_recent_manager_dispose             (GObject           *object);
193 static void     gtk_recent_manager_finalize            (GObject           *object);
194 static void     gtk_recent_manager_set_property        (GObject           *object,
195                                                         guint              prop_id,
196                                                         const GValue      *value,
197                                                         GParamSpec        *pspec);
198 static void     gtk_recent_manager_get_property        (GObject           *object,
199                                                         guint              prop_id,
200                                                         GValue            *value,
201                                                         GParamSpec        *pspec);
202 static void     gtk_recent_manager_add_item_query_info (GObject           *source_object,
203                                                         GAsyncResult      *res,
204                                                         gpointer           user_data);
205 static void     gtk_recent_manager_monitor_changed     (GFileMonitor      *monitor,
206                                                         GFile             *file,
207                                                         GFile             *other_file,
208                                                         GFileMonitorEvent  event_type,
209                                                         gpointer           user_data);
210 static void     gtk_recent_manager_changed             (GtkRecentManager  *manager);
211 static void     gtk_recent_manager_real_changed        (GtkRecentManager  *manager);
212 static void     gtk_recent_manager_set_filename        (GtkRecentManager  *manager,
213                                                         const gchar       *filename);
214 static void     gtk_recent_manager_clamp_to_age        (GtkRecentManager  *manager,
215                                                         gint               age);
216 static void     gtk_recent_manager_enabled_changed     (GtkRecentManager  *manager);
217
218
219 static void build_recent_items_list (GtkRecentManager  *manager);
220 static void purge_recent_items_list (GtkRecentManager  *manager,
221                                      GError           **error);
222
223 static RecentAppInfo *recent_app_info_new  (const gchar   *app_name);
224 static void           recent_app_info_free (RecentAppInfo *app_info);
225
226 static GtkRecentInfo *gtk_recent_info_new  (const gchar   *uri);
227 static void           gtk_recent_info_free (GtkRecentInfo *recent_info);
228
229 static guint signal_changed = 0;
230
231 static GtkRecentManager *recent_manager_singleton = NULL;
232
233 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
234
235 static void
236 filename_warning (const gchar *format, 
237                   const gchar *filename, 
238                   const gchar *message)
239 {
240   gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
241   g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
242   g_free (utf8);
243 }
244
245 /* Test of haystack has the needle prefix, comparing case
246  * insensitive. haystack may be UTF-8, but needle must
247  * contain only lowercase ascii. */
248 static gboolean
249 has_case_prefix (const gchar *haystack, 
250                  const gchar *needle)
251 {
252   const gchar *h, *n;
253
254   /* Eat one character at a time. */
255   h = haystack;
256   n = needle;
257
258   while (*n && *h && *n == g_ascii_tolower (*h))
259     {
260       n++;
261       h++;
262     }
263
264   return *n == '\0';
265 }
266
267 GQuark
268 gtk_recent_manager_error_quark (void)
269 {
270   return g_quark_from_static_string ("gtk-recent-manager-error-quark");
271 }
272
273 static void
274 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
275 {
276   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
277   
278   gobject_class->set_property = gtk_recent_manager_set_property;
279   gobject_class->get_property = gtk_recent_manager_get_property;
280   gobject_class->dispose = gtk_recent_manager_dispose;
281   gobject_class->finalize = gtk_recent_manager_finalize;
282   
283   /**
284    * GtkRecentManager:filename:
285    *
286    * The full path to the file to be used to store and read the recently
287    * used resources list
288    *
289    * Since: 2.10
290    */
291   g_object_class_install_property (gobject_class,
292                                    PROP_FILENAME,
293                                    g_param_spec_string ("filename",
294                                                         P_("Filename"),
295                                                         P_("The full path to the file to be used to store and read the list"),
296                                                         NULL,
297                                                         (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
298
299   /**
300    * GtkRecentManager:size:
301    * 
302    * The size of the recently used resources list.
303    *
304    * Since: 2.10
305    */
306   g_object_class_install_property (gobject_class,
307                                    PROP_SIZE,
308                                    g_param_spec_int ("size",
309                                                      P_("Size"),
310                                                      P_("The size of the recently used resources list"),
311                                                      -1,
312                                                      G_MAXINT,
313                                                      0,
314                                                      G_PARAM_READABLE));
315   
316   /**
317    * GtkRecentManager::changed:
318    * @recent_manager: the recent manager
319    *
320    * Emitted when the current recently used resources manager changes its
321    * contents, either by calling gtk_recent_manager_add_item() or by another
322    * application.
323    *
324    * Since: 2.10
325    */
326   signal_changed =
327     g_signal_new (I_("changed"),
328                   G_TYPE_FROM_CLASS (klass),
329                   G_SIGNAL_RUN_FIRST,
330                   G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
331                   NULL, NULL,
332                   g_cclosure_marshal_VOID__VOID,
333                   G_TYPE_NONE, 0);
334   
335   klass->changed = gtk_recent_manager_real_changed;
336   
337   g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
338 }
339
340 static void
341 gtk_recent_manager_init (GtkRecentManager *manager)
342 {
343   GtkRecentManagerPrivate *priv;
344   GtkSettings *settings;
345
346   manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
347                                                GTK_TYPE_RECENT_MANAGER,
348                                                GtkRecentManagerPrivate);
349   priv = manager->priv;
350
351   priv->size = 0;
352   priv->filename = NULL;
353
354   settings = gtk_settings_get_default ();
355   g_signal_connect_swapped (settings, "notify::gtk-recent-files-enabled",
356                             G_CALLBACK (gtk_recent_manager_enabled_changed), manager);
357 }
358
359 static void
360 gtk_recent_manager_set_property (GObject               *object,
361                                  guint                  prop_id,
362                                  const GValue          *value,
363                                  GParamSpec            *pspec)
364 {
365   GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
366  
367   switch (prop_id)
368     {
369     case PROP_FILENAME:
370       gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
371       break;
372     default:
373       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
374       break;
375     }
376 }
377
378 static void
379 gtk_recent_manager_get_property (GObject               *object,
380                                  guint                  prop_id,
381                                  GValue                *value,
382                                  GParamSpec            *pspec)
383 {
384   GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
385   
386   switch (prop_id)
387     {
388     case PROP_FILENAME:
389       g_value_set_string (value, recent_manager->priv->filename);
390       break;
391     case PROP_SIZE:
392       g_value_set_int (value, recent_manager->priv->size);
393       break;
394     default:
395       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
396       break;
397     }
398
399
400 static void
401 gtk_recent_manager_finalize (GObject *object)
402 {
403   GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
404   GtkRecentManagerPrivate *priv = manager->priv;
405
406   g_free (priv->filename);
407
408   if (priv->recent_items != NULL)
409     g_bookmark_file_free (priv->recent_items);
410
411   G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
412 }
413
414 static void
415 gtk_recent_manager_dispose (GObject *gobject)
416 {
417   GtkRecentManager *manager = GTK_RECENT_MANAGER (gobject);
418   GtkRecentManagerPrivate *priv = manager->priv;
419
420   if (priv->monitor != NULL)
421     {
422       g_signal_handlers_disconnect_by_func (priv->monitor,
423                                             G_CALLBACK (gtk_recent_manager_monitor_changed),
424                                             manager);
425       g_object_unref (priv->monitor);
426       priv->monitor = NULL;
427     }
428
429   if (priv->changed_timeout != 0)
430     {
431       g_source_remove (priv->changed_timeout);
432       priv->changed_timeout = 0;
433       priv->changed_age = 0;
434     }
435
436   if (priv->is_dirty)
437     {
438       g_object_ref (manager);
439       g_signal_emit (manager, signal_changed, 0);
440       g_object_unref (manager);
441     }
442
443   G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (gobject);
444 }
445
446 static void
447 gtk_recent_manager_enabled_changed (GtkRecentManager *manager)
448 {
449   manager->priv->is_dirty = TRUE;
450   gtk_recent_manager_changed (manager);
451 }
452
453 static void
454 gtk_recent_manager_real_changed (GtkRecentManager *manager)
455 {
456   GtkRecentManagerPrivate *priv = manager->priv;
457
458   g_object_freeze_notify (G_OBJECT (manager));
459
460   if (priv->is_dirty)
461     {
462       GError *write_error;
463       
464       /* we are marked as dirty, so we dump the content of our
465        * recently used items list
466        */
467       g_assert (priv->filename != NULL);
468
469       if (!priv->recent_items)
470         {
471           /* if no container object has been defined, we create a new
472            * empty container, and dump it
473            */
474           priv->recent_items = g_bookmark_file_new ();
475           priv->size = 0;
476         }
477       else
478         {
479           GtkSettings *settings = gtk_settings_get_default ();
480           gint age = 30;
481           gboolean enabled;
482
483           g_object_get (G_OBJECT (settings),
484                         "gtk-recent-files-max-age", &age,
485                         "gtk-recent-files-enabled", &enabled,
486                         NULL);
487
488           if (age == 0 || !enabled)
489             {
490               g_bookmark_file_free (priv->recent_items);
491               priv->recent_items = g_bookmark_file_new ();
492             }
493           else if (age > 0)
494             gtk_recent_manager_clamp_to_age (manager, age);
495         }
496
497       write_error = NULL;
498       g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
499       if (write_error)
500         {
501           filename_warning ("Attempting to store changes into `%s', "
502                             "but failed: %s",
503                             priv->filename,
504                             write_error->message);
505           g_error_free (write_error);
506         }
507
508       if (g_chmod (priv->filename, 0600) < 0)
509         {
510           filename_warning ("Attempting to set the permissions of `%s', "
511                             "but failed: %s",
512                             priv->filename,
513                             g_strerror (errno));
514         }
515
516       /* mark us as clean */
517       priv->is_dirty = FALSE;
518     }
519   else
520     {
521       /* we are not marked as dirty, so we have been called
522        * because the recently used resources file has been
523        * changed (and not from us).
524        */
525       build_recent_items_list (manager);
526     }
527
528   g_object_thaw_notify (G_OBJECT (manager));
529 }
530
531 static void
532 gtk_recent_manager_monitor_changed (GFileMonitor      *monitor,
533                                     GFile             *file,
534                                     GFile             *other_file,
535                                     GFileMonitorEvent  event_type,
536                                     gpointer           user_data)
537 {
538   GtkRecentManager *manager = user_data;
539
540   switch (event_type)
541     {
542     case G_FILE_MONITOR_EVENT_CHANGED:
543     case G_FILE_MONITOR_EVENT_CREATED:
544       gdk_threads_enter ();
545       gtk_recent_manager_changed (manager);
546       gdk_threads_leave ();
547       break;
548
549     case G_FILE_MONITOR_EVENT_DELETED:
550       break;
551
552     default:
553       break;
554     }
555 }
556
557 static gchar *
558 get_default_filename (void)
559 {
560   if (g_mkdir_with_parents (g_get_user_data_dir (), 0755) == -1)
561     {
562       int saved_errno = errno;
563
564       g_critical ("Unable to create user data directory '%s' for storing "
565                   "the recently used files list: %s",
566                   g_get_user_data_dir (),
567                   g_strerror (saved_errno));
568
569       return NULL;
570     }
571
572   return g_build_filename (g_get_user_data_dir (),
573                            GTK_RECENTLY_USED_FILE,
574                            NULL);
575 }
576
577 static void
578 gtk_recent_manager_set_filename (GtkRecentManager *manager,
579                                  const gchar      *filename)
580 {
581   GtkRecentManagerPrivate *priv;
582   GFile *file;
583   GError *error;
584   
585   g_assert (GTK_IS_RECENT_MANAGER (manager));
586
587   priv = manager->priv;
588
589   /* if a filename is already set and filename is not NULL, then copy
590    * it and reset the monitor; otherwise, if it's NULL we're being
591    * called from the finalization sequence, so we simply disconnect the
592    * monitoring and return.
593    *
594    * if no filename is set and filename is NULL, use the default.
595    */
596   if (priv->filename)
597     {
598       g_free (priv->filename);
599
600       if (priv->monitor)
601         {
602           g_signal_handlers_disconnect_by_func (priv->monitor,
603                                                 G_CALLBACK (gtk_recent_manager_monitor_changed),
604                                                 manager);
605           g_object_unref (priv->monitor);
606           priv->monitor = NULL;
607         }
608
609       if (!filename || *filename == '\0')
610         return;
611       else
612         priv->filename = g_strdup (filename);
613     }
614   else
615     {
616       if (!filename || *filename == '\0')
617         priv->filename = get_default_filename ();
618       else
619         priv->filename = g_strdup (filename);
620     }
621
622   g_assert (priv->filename != NULL);
623   file = g_file_new_for_path (priv->filename);
624
625   error = NULL;
626   priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
627   if (error)
628     {
629       filename_warning ("Unable to monitor `%s': %s\n"
630                         "The GtkRecentManager will not update its contents "
631                         "if the file is changed from other instances",
632                         priv->filename,
633                         error->message);
634       g_error_free (error);
635     }
636   else
637     g_signal_connect (priv->monitor, "changed",
638                       G_CALLBACK (gtk_recent_manager_monitor_changed),
639                       manager);
640
641   g_object_unref (file);
642
643   priv->is_dirty = FALSE;
644   build_recent_items_list (manager);
645 }
646
647 /* reads the recently used resources file and builds the items list.
648  * we keep the items list inside the parser object, and build the
649  * RecentInfo object only on user's demand to avoid useless replication.
650  * this function resets the dirty bit of the manager.
651  */
652 static void
653 build_recent_items_list (GtkRecentManager *manager)
654 {
655   GtkRecentManagerPrivate *priv = manager->priv;
656   GError *read_error;
657   gint size;
658
659   g_assert (priv->filename != NULL);
660   
661   if (!priv->recent_items)
662     {
663       priv->recent_items = g_bookmark_file_new ();
664       priv->size = 0;
665     }
666
667   /* the file exists, and it's valid (we hope); if not, destroy the container
668    * object and hope for a better result when the next "changed" signal is
669    * fired. */
670   read_error = NULL;
671   g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
672   if (read_error)
673     {
674       /* if the file does not exist we just wait for the first write
675        * operation on this recent manager instance, to avoid creating
676        * empty files and leading to spurious file system events (Sabayon
677        * will not be happy about those)
678        */
679       if (read_error->domain == G_FILE_ERROR &&
680           read_error->code != G_FILE_ERROR_NOENT)
681         filename_warning ("Attempting to read the recently used resources "
682                           "file at `%s', but the parser failed: %s.",
683                           priv->filename,
684                           read_error->message);
685
686       g_bookmark_file_free (priv->recent_items);
687       priv->recent_items = NULL;
688
689       g_error_free (read_error);
690     }
691   else
692     {
693       size = g_bookmark_file_get_size (priv->recent_items);
694       if (priv->size != size)
695         {
696           priv->size = size;
697
698           g_object_notify (G_OBJECT (manager), "size");
699         }
700     }
701
702   priv->is_dirty = FALSE;
703 }
704
705
706 /********************
707  * GtkRecentManager *
708  ********************/
709
710
711 /**
712  * gtk_recent_manager_new:
713  * 
714  * Creates a new recent manager object.  Recent manager objects are used to
715  * handle the list of recently used resources.  A #GtkRecentManager object
716  * monitors the recently used resources list, and emits the "changed" signal
717  * each time something inside the list changes.
718  *
719  * #GtkRecentManager objects are expensive: be sure to create them only when
720  * needed. You should use gtk_recent_manager_get_default() instead.
721  *
722  * Return value: A newly created #GtkRecentManager object.
723  *
724  * Since: 2.10
725  */
726 GtkRecentManager *
727 gtk_recent_manager_new (void)
728 {
729   return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
730 }
731
732 /**
733  * gtk_recent_manager_get_default:
734  *
735  * Gets a unique instance of #GtkRecentManager, that you can share
736  * in your application without caring about memory management.
737  *
738  * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
739  *
740  * Since: 2.10
741  */
742 GtkRecentManager *
743 gtk_recent_manager_get_default (void)
744 {
745   if (G_UNLIKELY (!recent_manager_singleton))
746     recent_manager_singleton = gtk_recent_manager_new ();
747
748   return recent_manager_singleton;
749 }
750
751
752 static void
753 gtk_recent_manager_add_item_query_info (GObject      *source_object,
754                                         GAsyncResult *res,
755                                         gpointer      user_data)
756 {
757   GFile *file = G_FILE (source_object);
758   GtkRecentManager *manager = user_data;
759   GtkRecentData recent_data;
760   GFileInfo *file_info;
761   gchar *uri, *basename;
762
763   uri = g_file_get_uri (file);
764
765   file_info = g_file_query_info_finish (file, res, NULL); /* NULL-GError */
766
767   recent_data.display_name = NULL;
768   recent_data.description = NULL;
769
770   if (file_info)
771     {
772       gchar *content_type;
773
774       content_type = g_file_info_get_attribute_as_string (file_info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
775
776       if (G_LIKELY (content_type))
777         recent_data.mime_type = g_content_type_get_mime_type (content_type);
778       else
779         recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
780
781       g_free (content_type);
782       g_object_unref (file_info);
783     }
784   else
785     {
786       basename = g_file_get_basename (file);
787       recent_data.mime_type = g_content_type_guess (basename, NULL, 0, NULL);
788       g_free (basename);
789     }
790
791   recent_data.app_name = g_strdup (g_get_application_name ());
792   recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
793   recent_data.groups = NULL;
794   recent_data.is_private = FALSE;
795
796   gdk_threads_enter ();
797
798   /* Ignore return value, this can't fail anyway since all required
799    * fields are set */
800   gtk_recent_manager_add_full (manager, uri, &recent_data);
801
802   manager->priv->is_dirty = TRUE;
803   gtk_recent_manager_changed (manager);
804
805   gdk_threads_leave ();
806
807   g_free (recent_data.mime_type);
808   g_free (recent_data.app_name);
809   g_free (recent_data.app_exec);
810
811   g_object_unref (manager);
812   g_free (uri);
813 }
814
815 /**
816  * gtk_recent_manager_add_item:
817  * @manager: a #GtkRecentManager
818  * @uri: a valid URI
819  *
820  * Adds a new resource, pointed by @uri, into the recently used
821  * resources list.
822  *
823  * This function automatically retrieves some of the needed
824  * metadata and setting other metadata to common default values; it
825  * then feeds the data to gtk_recent_manager_add_full().
826  * 
827  * See gtk_recent_manager_add_full() if you want to explicitly
828  * define the metadata for the resource pointed by @uri.
829  *
830  * Return value: %TRUE if the new item was successfully added
831  *   to the recently used resources list
832  *
833  * Since: 2.10
834  */
835 gboolean
836 gtk_recent_manager_add_item (GtkRecentManager  *manager,
837                              const gchar       *uri)
838 {
839   GFile* file;
840   
841   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
842   g_return_val_if_fail (uri != NULL, FALSE);
843
844   file = g_file_new_for_uri (uri);
845
846   g_file_query_info_async (file,
847                            G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
848                            G_PRIORITY_DEFAULT,
849                            G_FILE_QUERY_INFO_NONE,
850                            NULL,
851                            gtk_recent_manager_add_item_query_info,
852                            g_object_ref (manager));
853
854   g_object_unref (file);
855
856   return TRUE;
857 }
858
859 /**
860  * gtk_recent_manager_add_full:
861  * @manager: a #GtkRecentManager
862  * @uri: a valid URI
863  * @recent_data: metadata of the resource
864  *
865  * Adds a new resource, pointed by @uri, into the recently used
866  * resources list, using the metadata specified inside the #GtkRecentData
867  * structure passed in @recent_data.
868  *
869  * The passed URI will be used to identify this resource inside the
870  * list.
871  *
872  * In order to register the new recently used resource, metadata about
873  * the resource must be passed as well as the URI; the metadata is
874  * stored in a #GtkRecentData structure, which must contain the MIME
875  * type of the resource pointed by the URI; the name of the application
876  * that is registering the item, and a command line to be used when
877  * launching the item.
878  *
879  * Optionally, a #GtkRecentData structure might contain a UTF-8 string
880  * to be used when viewing the item instead of the last component of the
881  * URI; a short description of the item; whether the item should be
882  * considered private - that is, should be displayed only by the
883  * applications that have registered it.
884  *
885  * Return value: %TRUE if the new item was successfully added to the
886  * recently used resources list, %FALSE otherwise.
887  *
888  * Since: 2.10
889  */
890 gboolean
891 gtk_recent_manager_add_full (GtkRecentManager     *manager,
892                              const gchar          *uri,
893                              const GtkRecentData  *data)
894 {
895   GtkRecentManagerPrivate *priv;
896   GtkSettings *settings;
897   gboolean enabled;
898   
899   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
900   g_return_val_if_fail (uri != NULL, FALSE);
901   g_return_val_if_fail (data != NULL, FALSE);
902
903   /* sanity checks */
904   if ((data->display_name) &&
905       (!g_utf8_validate (data->display_name, -1, NULL)))
906     {
907       g_warning ("Attempting to add `%s' to the list of recently used "
908                  "resources, but the display name is not a valid UTF-8 "
909                  "encoded string",
910                  uri);
911       return FALSE;
912     }
913   
914   if ((data->description) &&
915       (!g_utf8_validate (data->description, -1, NULL)))
916     {
917       g_warning ("Attempting to add `%s' to the list of recently used "
918                  "resources, but the description is not a valid UTF-8 "
919                  "encoded string",
920                  uri);
921       return FALSE;
922     }
923
924  
925   if (!data->mime_type)
926     {
927       g_warning ("Attempting to add `%s' to the list of recently used "
928                  "resources, but not MIME type was defined",
929                  uri);
930       return FALSE;
931     }
932   
933   if (!data->app_name)
934     {
935       g_warning ("Attempting to add `%s' to the list of recently used "
936                  "resources, but no name of the application that is "
937                  "registering it was defined",
938                  uri);
939       return FALSE;
940     }
941   
942   if (!data->app_exec)
943     {
944       g_warning ("Attempting to add `%s' to the list of recently used "
945                  "resources, but no command line for the application "
946                  "that is registering it was defined",
947                  uri);
948       return FALSE;
949     }
950
951   settings = gtk_settings_get_default ();
952   g_object_get (G_OBJECT (settings), "gtk-recent-files-enabled", &enabled, NULL);
953   if (!enabled)
954     return TRUE;
955
956   priv = manager->priv;
957
958   if (!priv->recent_items)
959     {
960       priv->recent_items = g_bookmark_file_new ();
961       priv->size = 0;
962     }
963
964   if (data->display_name)  
965     g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
966   
967   if (data->description)
968     g_bookmark_file_set_description (priv->recent_items, uri, data->description);
969
970   g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
971   
972   if (data->groups && data->groups[0] != '\0')
973     {
974       gint j;
975       
976       for (j = 0; (data->groups)[j] != NULL; j++)
977         g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
978     }
979   
980   /* register the application; this will take care of updating the
981    * registration count and time in case the application has
982    * already registered the same document inside the list
983    */
984   g_bookmark_file_add_application (priv->recent_items, uri,
985                                    data->app_name,
986                                    data->app_exec);
987   
988   g_bookmark_file_set_is_private (priv->recent_items, uri,
989                                   data->is_private);
990   
991   /* mark us as dirty, so that when emitting the "changed" signal we
992    * will dump our changes
993    */
994   priv->is_dirty = TRUE;
995   gtk_recent_manager_changed (manager);
996   
997   return TRUE;
998 }
999
1000 /**
1001  * gtk_recent_manager_remove_item:
1002  * @manager: a #GtkRecentManager
1003  * @uri: the URI of the item you wish to remove
1004  * @error: (allow-none): return location for a #GError, or %NULL
1005  *
1006  * Removes a resource pointed by @uri from the recently used resources
1007  * list handled by a recent manager.
1008  *
1009  * Return value: %TRUE if the item pointed by @uri has been successfully
1010  *   removed by the recently used resources list, and %FALSE otherwise.
1011  *
1012  * Since: 2.10
1013  */
1014 gboolean
1015 gtk_recent_manager_remove_item (GtkRecentManager  *manager,
1016                                 const gchar       *uri,
1017                                 GError           **error)
1018 {
1019   GtkRecentManagerPrivate *priv;
1020   GError *remove_error = NULL;
1021
1022   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1023   g_return_val_if_fail (uri != NULL, FALSE);
1024   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1025   
1026   priv = manager->priv;
1027   
1028   if (!priv->recent_items)
1029     {
1030       priv->recent_items = g_bookmark_file_new ();
1031       priv->size = 0;
1032
1033       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1034                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1035                    _("Unable to find an item with URI '%s'"),
1036                    uri);
1037
1038       return FALSE;
1039     }
1040
1041   g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
1042   if (remove_error)
1043     {
1044       g_error_free (remove_error);
1045
1046       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1047                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1048                    _("Unable to find an item with URI '%s'"),
1049                    uri);
1050       
1051       return FALSE;
1052     }
1053
1054   priv->is_dirty = TRUE;
1055   gtk_recent_manager_changed (manager);
1056   
1057   return TRUE;
1058 }
1059
1060 /**
1061  * gtk_recent_manager_has_item:
1062  * @manager: a #GtkRecentManager
1063  * @uri: a URI
1064  *
1065  * Checks whether there is a recently used resource registered
1066  * with @uri inside the recent manager.
1067  *
1068  * Return value: %TRUE if the resource was found, %FALSE otherwise.
1069  *
1070  * Since: 2.10
1071  */
1072 gboolean
1073 gtk_recent_manager_has_item (GtkRecentManager *manager,
1074                              const gchar      *uri)
1075 {
1076   GtkRecentManagerPrivate *priv;
1077
1078   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1079   g_return_val_if_fail (uri != NULL, FALSE);
1080
1081   priv = manager->priv;
1082   g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1083
1084   return g_bookmark_file_has_item (priv->recent_items, uri);
1085 }
1086
1087 static void
1088 build_recent_info (GBookmarkFile  *bookmarks,
1089                    GtkRecentInfo  *info)
1090 {
1091   gchar **apps, **groups;
1092   gsize apps_len, groups_len, i;
1093
1094   g_assert (bookmarks != NULL);
1095   g_assert (info != NULL);
1096   
1097   info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1098   info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1099   info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1100     
1101   info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1102   
1103   info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1104   info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1105   info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1106   
1107   groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1108   for (i = 0; i < groups_len; i++)
1109     {
1110       gchar *group_name = g_strdup (groups[i]);
1111       
1112       info->groups = g_slist_append (info->groups, group_name);
1113     }
1114
1115   g_strfreev (groups);
1116   
1117   apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1118   for (i = 0; i < apps_len; i++)
1119     {
1120       gchar *app_name, *app_exec;
1121       guint count;
1122       time_t stamp;
1123       RecentAppInfo *app_info;
1124       gboolean res;
1125       
1126       app_name = apps[i];
1127       
1128       res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1129                                           &app_exec,
1130                                           &count,
1131                                           &stamp,
1132                                           NULL);
1133       if (!res)
1134         continue;
1135       
1136       app_info = recent_app_info_new (app_name);
1137       app_info->exec = app_exec;
1138       app_info->count = count;
1139       app_info->stamp = stamp;
1140       
1141       info->applications = g_slist_prepend (info->applications, app_info);
1142       g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1143     }
1144   
1145   g_strfreev (apps);
1146 }
1147
1148 /**
1149  * gtk_recent_manager_lookup_item:
1150  * @manager: a #GtkRecentManager
1151  * @uri: a URI
1152  * @error: (allow-none): a return location for a #GError, or %NULL
1153  *
1154  * Searches for a URI inside the recently used resources list, and
1155  * returns a structure containing informations about the resource
1156  * like its MIME type, or its display name.
1157  *
1158  * Return value: a #GtkRecentInfo structure containing information
1159  *   about the resource pointed by @uri, or %NULL if the URI was
1160  *   not registered in the recently used resources list.  Free with
1161  *   gtk_recent_info_unref().
1162  *
1163  * Since: 2.10
1164  */
1165 GtkRecentInfo *
1166 gtk_recent_manager_lookup_item (GtkRecentManager  *manager,
1167                                 const gchar       *uri,
1168                                 GError           **error)
1169 {
1170   GtkRecentManagerPrivate *priv;
1171   GtkRecentInfo *info = NULL;
1172   
1173   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1174   g_return_val_if_fail (uri != NULL, NULL);
1175   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1176   
1177   priv = manager->priv;
1178   if (!priv->recent_items)
1179     {
1180       priv->recent_items = g_bookmark_file_new ();
1181       priv->size = 0;
1182
1183       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1184                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1185                    _("Unable to find an item with URI '%s'"),
1186                    uri);
1187
1188       return NULL;
1189     }
1190   
1191   if (!g_bookmark_file_has_item (priv->recent_items, uri))
1192     {
1193       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1194                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1195                    _("Unable to find an item with URI '%s'"),
1196                    uri);
1197       return NULL;
1198     }
1199   
1200   info = gtk_recent_info_new (uri);
1201   g_return_val_if_fail (info != NULL, NULL);
1202   
1203   /* fill the RecentInfo structure with the data retrieved by our
1204    * parser object from the storage file 
1205    */
1206   build_recent_info (priv->recent_items, info);
1207
1208   return info;
1209 }
1210
1211 /**
1212  * gtk_recent_manager_move_item:
1213  * @manager: a #GtkRecentManager
1214  * @uri: the URI of a recently used resource
1215  * @new_uri: (allow-none): the new URI of the recently used resource, or %NULL to
1216  *    remove the item pointed by @uri in the list
1217  * @error: (allow-none): a return location for a #GError, or %NULL
1218  *
1219  * Changes the location of a recently used resource from @uri to @new_uri.
1220  * 
1221  * Please note that this function will not affect the resource pointed
1222  * by the URIs, but only the URI used in the recently used resources list.
1223  *
1224  * Return value: %TRUE on success.
1225  *
1226  * Since: 2.10
1227  */
1228 gboolean
1229 gtk_recent_manager_move_item (GtkRecentManager  *recent_manager,
1230                               const gchar       *uri,
1231                               const gchar       *new_uri,
1232                               GError           **error)
1233 {
1234   GtkRecentManagerPrivate *priv;
1235   GError *move_error;
1236
1237   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1238   g_return_val_if_fail (uri != NULL, FALSE);
1239   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1240
1241   priv = recent_manager->priv;
1242
1243   if (!priv->recent_items)
1244     {
1245       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1246                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1247                    _("Unable to find an item with URI '%s'"),
1248                    uri);
1249       return FALSE;
1250     }
1251
1252   if (!g_bookmark_file_has_item (priv->recent_items, uri))
1253     {
1254       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1255                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1256                    _("Unable to find an item with URI '%s'"),
1257                    uri);
1258       return FALSE;
1259     }
1260
1261   move_error = NULL;
1262   if (!g_bookmark_file_move_item (priv->recent_items,
1263                                   uri,
1264                                   new_uri,
1265                                   &move_error))
1266     {
1267       g_error_free (move_error);
1268
1269       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1270                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1271                    _("Unable to find an item with URI '%s'"),
1272                    uri);
1273       return FALSE;
1274     }
1275
1276   priv->is_dirty = TRUE;
1277   gtk_recent_manager_changed (recent_manager);
1278
1279   return TRUE;
1280 }
1281
1282 /**
1283  * gtk_recent_manager_get_items:
1284  * @manager: a #GtkRecentManager
1285  *
1286  * Gets the list of recently used resources.
1287  *
1288  * Return value:  (element-type GtkRecentInfo) (transfer full): a list of
1289  *   newly allocated #GtkRecentInfo objects. Use
1290  *   gtk_recent_info_unref() on each item inside the list, and then
1291  *   free the list itself using g_list_free().
1292  *
1293  * Since: 2.10
1294  */
1295 GList *
1296 gtk_recent_manager_get_items (GtkRecentManager *manager)
1297 {
1298   GtkRecentManagerPrivate *priv;
1299   GList *retval = NULL;
1300   gchar **uris;
1301   gsize uris_len, i;
1302   
1303   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1304   
1305   priv = manager->priv;
1306   if (!priv->recent_items)
1307     return NULL;
1308
1309   uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1310   for (i = 0; i < uris_len; i++)
1311     {
1312       GtkRecentInfo *info;
1313       
1314       info = gtk_recent_info_new (uris[i]);
1315       build_recent_info (priv->recent_items, info);
1316       
1317       retval = g_list_prepend (retval, info);
1318     }
1319   
1320   g_strfreev (uris);
1321   
1322   return retval;
1323 }
1324
1325 static void
1326 purge_recent_items_list (GtkRecentManager  *manager,
1327                          GError           **error)
1328 {
1329   GtkRecentManagerPrivate *priv = manager->priv;
1330
1331   if (priv->recent_items == NULL)
1332     return;
1333
1334   g_bookmark_file_free (priv->recent_items);
1335   priv->recent_items = g_bookmark_file_new ();
1336   priv->size = 0;
1337
1338   /* emit the changed signal, to ensure that the purge is written */
1339   priv->is_dirty = TRUE;
1340   gtk_recent_manager_changed (manager);
1341 }
1342
1343 /**
1344  * gtk_recent_manager_purge_items:
1345  * @manager: a #GtkRecentManager
1346  * @error: (allow-none): a return location for a #GError, or %NULL
1347  *
1348  * Purges every item from the recently used resources list.
1349  *
1350  * Return value: the number of items that have been removed from the
1351  *   recently used resources list.
1352  *
1353  * Since: 2.10
1354  */
1355 gint
1356 gtk_recent_manager_purge_items (GtkRecentManager  *manager,
1357                                 GError           **error)
1358 {
1359   GtkRecentManagerPrivate *priv;
1360   gint count, purged;
1361   
1362   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1363
1364   priv = manager->priv;
1365   if (!priv->recent_items)
1366     return 0;
1367   
1368   count = g_bookmark_file_get_size (priv->recent_items);
1369   if (!count)
1370     return 0;
1371   
1372   purge_recent_items_list (manager, error);
1373   
1374   purged = count - g_bookmark_file_get_size (priv->recent_items);
1375
1376   return purged;
1377 }
1378
1379 static gboolean
1380 emit_manager_changed (gpointer data)
1381 {
1382   GtkRecentManager *manager = data;
1383
1384   manager->priv->changed_age = 0;
1385   manager->priv->changed_timeout = 0;
1386
1387   g_signal_emit (manager, signal_changed, 0);
1388
1389   return FALSE;
1390 }
1391
1392 static void
1393 gtk_recent_manager_changed (GtkRecentManager *manager)
1394 {
1395   /* coalesce consecutive changes
1396    *
1397    * we schedule a write in 250 msecs immediately; if we get more than one
1398    * request per millisecond before the timeout has a chance to run, we
1399    * schedule an emission immediately.
1400    */
1401   if (manager->priv->changed_timeout == 0)
1402     manager->priv->changed_timeout = gdk_threads_add_timeout (250, emit_manager_changed, manager);
1403   else
1404     {
1405       manager->priv->changed_age += 1;
1406
1407       if (manager->priv->changed_age > 250)
1408         {
1409           g_source_remove (manager->priv->changed_timeout);
1410           g_signal_emit (manager, signal_changed, 0);
1411
1412           manager->priv->changed_age = 0;
1413           manager->priv->changed_timeout = 0;
1414         }
1415     }
1416 }
1417
1418 static void
1419 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1420                                  gint              age)
1421 {
1422   GtkRecentManagerPrivate *priv = manager->priv;
1423   gchar **uris;
1424   gsize n_uris, i;
1425   time_t now;
1426
1427   if (G_UNLIKELY (!priv->recent_items))
1428     return;
1429
1430   now = time (NULL);
1431
1432   uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1433
1434   for (i = 0; i < n_uris; i++)
1435     {
1436       const gchar *uri = uris[i];
1437       time_t modified;
1438       gint item_age;
1439
1440       modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1441       item_age = (gint) ((now - modified) / (60 * 60 * 24));
1442       if (item_age > age)
1443         g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1444     }
1445
1446   g_strfreev (uris);
1447 }
1448
1449 /*****************
1450  * GtkRecentInfo *
1451  *****************/
1452  
1453 G_DEFINE_BOXED_TYPE (GtkRecentInfo, gtk_recent_info,
1454                      gtk_recent_info_ref,
1455                      gtk_recent_info_unref)
1456
1457 static GtkRecentInfo *
1458 gtk_recent_info_new (const gchar *uri)
1459 {
1460   GtkRecentInfo *info;
1461
1462   g_assert (uri != NULL);
1463
1464   info = g_new0 (GtkRecentInfo, 1);
1465   info->uri = g_strdup (uri);
1466   
1467   info->applications = NULL;
1468   info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1469   
1470   info->groups = NULL;
1471   
1472   info->ref_count = 1;
1473
1474   return info;
1475 }
1476
1477 static void
1478 gtk_recent_info_free (GtkRecentInfo *recent_info)
1479 {
1480   if (!recent_info)
1481     return;
1482
1483   g_free (recent_info->uri);
1484   g_free (recent_info->display_name);
1485   g_free (recent_info->description);
1486   g_free (recent_info->mime_type);
1487   
1488   if (recent_info->applications)
1489     {
1490       g_slist_foreach (recent_info->applications,
1491                        (GFunc) recent_app_info_free,
1492                        NULL);
1493       g_slist_free (recent_info->applications);
1494       
1495       recent_info->applications = NULL;
1496     }
1497   
1498   if (recent_info->apps_lookup)
1499     g_hash_table_destroy (recent_info->apps_lookup);
1500
1501   if (recent_info->groups)
1502     {
1503       g_slist_foreach (recent_info->groups,
1504                        (GFunc) g_free,
1505                        NULL);
1506       g_slist_free (recent_info->groups);
1507
1508       recent_info->groups = NULL;
1509     }
1510   
1511   if (recent_info->icon)
1512     g_object_unref (recent_info->icon);
1513
1514   g_free (recent_info);
1515 }
1516
1517 /**
1518  * gtk_recent_info_ref:
1519  * @info: a #GtkRecentInfo
1520  *
1521  * Increases the reference count of @recent_info by one.
1522  *
1523  * Return value: the recent info object with its reference count increased
1524  *   by one.
1525  *
1526  * Since: 2.10
1527  */
1528 GtkRecentInfo *
1529 gtk_recent_info_ref (GtkRecentInfo *info)
1530 {
1531   g_return_val_if_fail (info != NULL, NULL);
1532   g_return_val_if_fail (info->ref_count > 0, NULL);
1533   
1534   info->ref_count += 1;
1535     
1536   return info;
1537 }
1538
1539 /**
1540  * gtk_recent_info_unref:
1541  * @info: a #GtkRecentInfo
1542  *
1543  * Decreases the reference count of @info by one.  If the reference
1544  * count reaches zero, @info is deallocated, and the memory freed.
1545  *
1546  * Since: 2.10
1547  */
1548 void
1549 gtk_recent_info_unref (GtkRecentInfo *info)
1550 {
1551   g_return_if_fail (info != NULL);
1552   g_return_if_fail (info->ref_count > 0);
1553
1554   info->ref_count -= 1;
1555   
1556   if (info->ref_count == 0)
1557     gtk_recent_info_free (info);
1558 }
1559
1560 /**
1561  * gtk_recent_info_get_uri:
1562  * @info: a #GtkRecentInfo
1563  *
1564  * Gets the URI of the resource.
1565  *
1566  * Return value: the URI of the resource.  The returned string is
1567  *   owned by the recent manager, and should not be freed.
1568  *
1569  * Since: 2.10
1570  */
1571 const gchar *
1572 gtk_recent_info_get_uri (GtkRecentInfo *info)
1573 {
1574   g_return_val_if_fail (info != NULL, NULL);
1575   
1576   return info->uri;
1577 }
1578
1579 /**
1580  * gtk_recent_info_get_display_name:
1581  * @info: a #GtkRecentInfo
1582  *
1583  * Gets the name of the resource.  If none has been defined, the basename
1584  * of the resource is obtained.
1585  *
1586  * Return value: the display name of the resource.  The returned string
1587  *   is owned by the recent manager, and should not be freed.
1588  *
1589  * Since: 2.10
1590  */
1591 const gchar *
1592 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1593 {
1594   g_return_val_if_fail (info != NULL, NULL);
1595   
1596   if (!info->display_name)
1597     info->display_name = gtk_recent_info_get_short_name (info);
1598   
1599   return info->display_name;
1600 }
1601
1602 /**
1603  * gtk_recent_info_get_description:
1604  * @info: a #GtkRecentInfo
1605  *
1606  * Gets the (short) description of the resource.
1607  *
1608  * Return value: the description of the resource.  The returned string
1609  *   is owned by the recent manager, and should not be freed.
1610  *
1611  * Since: 2.10
1612  **/
1613 const gchar *
1614 gtk_recent_info_get_description (GtkRecentInfo *info)
1615 {
1616   g_return_val_if_fail (info != NULL, NULL);
1617   
1618   return info->description;
1619 }
1620
1621 /**
1622  * gtk_recent_info_get_mime_type:
1623  * @info: a #GtkRecentInfo
1624  *
1625  * Gets the MIME type of the resource.
1626  *
1627  * Return value: the MIME type of the resource.  The returned string
1628  *   is owned by the recent manager, and should not be freed.
1629  *
1630  * Since: 2.10
1631  */
1632 const gchar *
1633 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1634 {
1635   g_return_val_if_fail (info != NULL, NULL);
1636   
1637   if (!info->mime_type)
1638     info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1639   
1640   return info->mime_type;
1641 }
1642
1643 /**
1644  * gtk_recent_info_get_added:
1645  * @info: a #GtkRecentInfo
1646  *
1647  * Gets the timestamp (seconds from system's Epoch) when the resource
1648  * was added to the recently used resources list.
1649  *
1650  * Return value: the number of seconds elapsed from system's Epoch when
1651  *   the resource was added to the list, or -1 on failure.
1652  *
1653  * Since: 2.10
1654  */
1655 time_t
1656 gtk_recent_info_get_added (GtkRecentInfo *info)
1657 {
1658   g_return_val_if_fail (info != NULL, (time_t) -1);
1659   
1660   return info->added;
1661 }
1662
1663 /**
1664  * gtk_recent_info_get_modified:
1665  * @info: a #GtkRecentInfo
1666  *
1667  * Gets the timestamp (seconds from system's Epoch) when the resource
1668  * was last modified.
1669  *
1670  * Return value: the number of seconds elapsed from system's Epoch when
1671  *   the resource was last modified, or -1 on failure.
1672  *
1673  * Since: 2.10
1674  */
1675 time_t
1676 gtk_recent_info_get_modified (GtkRecentInfo *info)
1677 {
1678   g_return_val_if_fail (info != NULL, (time_t) -1);
1679   
1680   return info->modified;
1681 }
1682
1683 /**
1684  * gtk_recent_info_get_visited:
1685  * @info: a #GtkRecentInfo
1686  *
1687  * Gets the timestamp (seconds from system's Epoch) when the resource
1688  * was last visited.
1689  *
1690  * Return value: the number of seconds elapsed from system's Epoch when
1691  *   the resource was last visited, or -1 on failure.
1692  *
1693  * Since: 2.10
1694  */
1695 time_t
1696 gtk_recent_info_get_visited (GtkRecentInfo *info)
1697 {
1698   g_return_val_if_fail (info != NULL, (time_t) -1);
1699   
1700   return info->visited;
1701 }
1702
1703 /**
1704  * gtk_recent_info_get_private_hint:
1705  * @info: a #GtkRecentInfo
1706  *
1707  * Gets the value of the "private" flag.  Resources in the recently used
1708  * list that have this flag set to %TRUE should only be displayed by the
1709  * applications that have registered them.
1710  *
1711  * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1712  *
1713  * Since: 2.10
1714  */
1715 gboolean
1716 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1717 {
1718   g_return_val_if_fail (info != NULL, FALSE);
1719   
1720   return info->is_private;
1721 }
1722
1723
1724 static RecentAppInfo *
1725 recent_app_info_new (const gchar *app_name)
1726 {
1727   RecentAppInfo *app_info;
1728
1729   g_assert (app_name != NULL);
1730   
1731   app_info = g_slice_new0 (RecentAppInfo);
1732   app_info->name = g_strdup (app_name);
1733   app_info->exec = NULL;
1734   app_info->count = 1;
1735   app_info->stamp = 0; 
1736   
1737   return app_info;
1738 }
1739
1740 static void
1741 recent_app_info_free (RecentAppInfo *app_info)
1742 {
1743   if (!app_info)
1744     return;
1745   
1746   g_free (app_info->name);
1747   g_free (app_info->exec);
1748   
1749   g_slice_free (RecentAppInfo, app_info);
1750 }
1751
1752 /**
1753  * gtk_recent_info_get_application_info:
1754  * @info: a #GtkRecentInfo
1755  * @app_name: the name of the application that has registered this item
1756  * @app_exec: (transfer none) (out): return location for the string containing the command line
1757  * @count: (out): return location for the number of times this item was registered
1758  * @time_: (out): return location for the timestamp this item was last registered
1759  *    for this application
1760  *
1761  * Gets the data regarding the application that has registered the resource
1762  * pointed by @info.
1763  *
1764  * If the command line contains any escape characters defined inside the
1765  * storage specification, they will be expanded.
1766  *
1767  * Return value: %TRUE if an application with @app_name has registered this
1768  *   resource inside the recently used list, or %FALSE otherwise. The
1769  *   @app_exec string is owned by the #GtkRecentInfo and should not be
1770  *   modified or freed
1771  *
1772  * Since: 2.10
1773  */
1774 gboolean
1775 gtk_recent_info_get_application_info (GtkRecentInfo  *info,
1776                                       const gchar    *app_name,
1777                                       const gchar   **app_exec,
1778                                       guint          *count,
1779                                       time_t         *time_)
1780 {
1781   RecentAppInfo *ai;
1782   
1783   g_return_val_if_fail (info != NULL, FALSE);
1784   g_return_val_if_fail (app_name != NULL, FALSE);
1785   
1786   ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1787                                               app_name);
1788   if (!ai)
1789     {
1790       g_warning ("No registered application with name '%s' "
1791                  "for item with URI '%s' found",
1792                  app_name,
1793                  info->uri);
1794       return FALSE;
1795     }
1796   
1797   if (app_exec)
1798     *app_exec = ai->exec;
1799   
1800   if (count)
1801     *count = ai->count;
1802   
1803   if (time_)
1804     *time_ = ai->stamp;
1805
1806   return TRUE;
1807 }
1808
1809 /**
1810  * gtk_recent_info_get_applications:
1811  * @info: a #GtkRecentInfo
1812  * @length: (out) (allow-none): return location for the length of the returned list
1813  *
1814  * Retrieves the list of applications that have registered this resource.
1815  *
1816  * Return value: (array length=length zero-terminated=1) (transfer full):
1817  *     a newly allocated %NULL-terminated array of strings.
1818  *     Use g_strfreev() to free it.
1819  *
1820  * Since: 2.10
1821  */
1822 gchar **
1823 gtk_recent_info_get_applications (GtkRecentInfo *info,
1824                                   gsize         *length)
1825 {
1826   GSList *l;
1827   gchar **retval;
1828   gsize n_apps, i;
1829   
1830   g_return_val_if_fail (info != NULL, NULL);
1831   
1832   if (!info->applications)
1833     {
1834       if (length)
1835         *length = 0;
1836       
1837       return NULL;    
1838     }
1839   
1840   n_apps = g_slist_length (info->applications);
1841   
1842   retval = g_new0 (gchar *, n_apps + 1);
1843   
1844   for (l = info->applications, i = 0;
1845        l != NULL;
1846        l = l->next)
1847     {
1848       RecentAppInfo *ai = (RecentAppInfo *) l->data;
1849       
1850       g_assert (ai != NULL);
1851       
1852       retval[i++] = g_strdup (ai->name);
1853     }
1854   retval[i] = NULL;
1855   
1856   if (length)
1857     *length = i;
1858   
1859   return retval;
1860 }
1861
1862 /**
1863  * gtk_recent_info_has_application:
1864  * @info: a #GtkRecentInfo
1865  * @app_name: a string containing an application name
1866  *
1867  * Checks whether an application registered this resource using @app_name.
1868  *
1869  * Return value: %TRUE if an application with name @app_name was found,
1870  *   %FALSE otherwise.
1871  *
1872  * Since: 2.10
1873  */
1874 gboolean
1875 gtk_recent_info_has_application (GtkRecentInfo *info,
1876                                  const gchar   *app_name)
1877 {
1878   g_return_val_if_fail (info != NULL, FALSE);
1879   g_return_val_if_fail (app_name != NULL, FALSE);
1880   
1881   return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1882 }
1883
1884 /**
1885  * gtk_recent_info_last_application:
1886  * @info: a #GtkRecentInfo
1887  *
1888  * Gets the name of the last application that have registered the
1889  * recently used resource represented by @info.
1890  *
1891  * Return value: an application name.  Use g_free() to free it.
1892  *
1893  * Since: 2.10
1894  */
1895 gchar *
1896 gtk_recent_info_last_application (GtkRecentInfo  *info)
1897 {
1898   GSList *l;
1899   time_t last_stamp = (time_t) -1;
1900   gchar *name = NULL;
1901   
1902   g_return_val_if_fail (info != NULL, NULL);
1903   
1904   for (l = info->applications; l != NULL; l = l->next)
1905     {
1906       RecentAppInfo *ai = (RecentAppInfo *) l->data;
1907       
1908       if (ai->stamp > last_stamp)
1909         {
1910           name = ai->name;
1911           last_stamp = ai->stamp;
1912         }
1913     }
1914   
1915   return g_strdup (name);
1916 }
1917
1918 static GdkPixbuf *
1919 get_icon_for_mime_type (const char *mime_type,
1920                         gint        pixel_size)
1921 {
1922   GtkIconTheme *icon_theme;
1923   char *content_type;
1924   GIcon *icon;
1925   GtkIconInfo *info;
1926   GdkPixbuf *pixbuf;
1927
1928   icon_theme = gtk_icon_theme_get_default ();
1929
1930   content_type = g_content_type_from_mime_type (mime_type);
1931
1932   if (!content_type)
1933     return NULL;
1934
1935   icon = g_content_type_get_icon (content_type);
1936   info = gtk_icon_theme_lookup_by_gicon (icon_theme, 
1937                                          icon, 
1938                                          pixel_size, 
1939                                          GTK_ICON_LOOKUP_USE_BUILTIN);
1940   g_free (content_type);
1941   g_object_unref (icon);
1942
1943   if (!info)
1944     return NULL;
1945
1946   pixbuf = gtk_icon_info_load_icon (info, NULL);
1947   g_object_unref (info);
1948
1949   return pixbuf;
1950 }
1951
1952 static GdkPixbuf *
1953 get_icon_fallback (const gchar *icon_name,
1954                    gint         size)
1955 {
1956   GtkIconTheme *icon_theme;
1957   GdkPixbuf *retval;
1958
1959   icon_theme = gtk_icon_theme_get_default ();
1960   
1961   retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1962                                      size,
1963                                      GTK_ICON_LOOKUP_USE_BUILTIN,
1964                                      NULL);
1965   g_assert (retval != NULL);
1966   
1967   return retval; 
1968 }
1969
1970 /**
1971  * gtk_recent_info_get_icon:
1972  * @info: a #GtkRecentInfo
1973  * @size: the size of the icon in pixels
1974  *
1975  * Retrieves the icon of size @size associated to the resource MIME type.
1976  *
1977  * Return value: (transfer full): a #GdkPixbuf containing the icon,
1978  *     or %NULL. Use g_object_unref() when finished using the icon.
1979  *
1980  * Since: 2.10
1981  */
1982 GdkPixbuf *
1983 gtk_recent_info_get_icon (GtkRecentInfo *info,
1984                           gint           size)
1985 {
1986   GdkPixbuf *retval = NULL;
1987   
1988   g_return_val_if_fail (info != NULL, NULL);
1989   
1990   if (info->mime_type)
1991     retval = get_icon_for_mime_type (info->mime_type, size);
1992
1993   /* this function should never fail */  
1994   if (!retval)
1995     {
1996       if (info->mime_type &&
1997           strcmp (info->mime_type, "x-directory/normal") == 0)
1998         retval = get_icon_fallback ("folder", size);
1999       else
2000         retval = get_icon_fallback ("text-x-generic", size);
2001     }
2002   
2003   return retval;
2004 }
2005
2006 /**
2007  * gtk_recent_info_get_gicon:
2008  * @info: a #GtkRecentInfo
2009  *
2010  * Retrieves the icon associated to the resource MIME type.
2011  *
2012  * Return value: (transfer full): a #GIcon containing the icon, or %NULL. Use
2013  *   g_object_unref() when finished using the icon
2014  *
2015  * Since: 2.22
2016  */
2017 GIcon *
2018 gtk_recent_info_get_gicon (GtkRecentInfo  *info)
2019 {
2020   GIcon *icon = NULL;
2021   gchar *content_type;
2022
2023   g_return_val_if_fail (info != NULL, NULL);
2024
2025   if (info->mime_type != NULL &&
2026       (content_type = g_content_type_from_mime_type (info->mime_type)) != NULL)
2027     {
2028       icon = g_content_type_get_icon (content_type);
2029       g_free (content_type);
2030     }
2031
2032   return icon;
2033 }
2034
2035 /**
2036  * gtk_recent_info_is_local:
2037  * @info: a #GtkRecentInfo
2038  *
2039  * Checks whether the resource is local or not by looking at the
2040  * scheme of its URI.
2041  *
2042  * Return value: %TRUE if the resource is local.
2043  *
2044  * Since: 2.10
2045  */
2046 gboolean
2047 gtk_recent_info_is_local (GtkRecentInfo *info)
2048 {
2049   g_return_val_if_fail (info != NULL, FALSE);
2050   
2051   return has_case_prefix (info->uri, "file:/");
2052 }
2053
2054 /**
2055  * gtk_recent_info_exists:
2056  * @info: a #GtkRecentInfo
2057  *
2058  * Checks whether the resource pointed by @info still exists.  At
2059  * the moment this check is done only on resources pointing to local files.
2060  *
2061  * Return value: %TRUE if the resource exists
2062  *
2063  * Since: 2.10
2064  */
2065 gboolean
2066 gtk_recent_info_exists (GtkRecentInfo *info)
2067 {
2068   gchar *filename;
2069   GStatBuf stat_buf;
2070   gboolean retval = FALSE;
2071   
2072   g_return_val_if_fail (info != NULL, FALSE);
2073   
2074   /* we guarantee only local resources */
2075   if (!gtk_recent_info_is_local (info))
2076     return FALSE;
2077   
2078   filename = g_filename_from_uri (info->uri, NULL, NULL);
2079   if (filename)
2080     {
2081       if (g_stat (filename, &stat_buf) == 0)
2082         retval = TRUE;
2083      
2084       g_free (filename);
2085     }
2086   
2087   return retval;
2088 }
2089
2090 /**
2091  * gtk_recent_info_match:
2092  * @info_a: a #GtkRecentInfo
2093  * @info_b: a #GtkRecentInfo
2094  *
2095  * Checks whether two #GtkRecentInfo structures point to the same
2096  * resource.
2097  *
2098  * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2099  *   resource, %FALSE otherwise.
2100  *
2101  * Since: 2.10
2102  */
2103 gboolean
2104 gtk_recent_info_match (GtkRecentInfo *info_a,
2105                        GtkRecentInfo *info_b)
2106 {
2107   g_return_val_if_fail (info_a != NULL, FALSE);
2108   g_return_val_if_fail (info_b != NULL, FALSE);
2109   
2110   return (0 == strcmp (info_a->uri, info_b->uri));
2111 }
2112
2113 /* taken from gnome-vfs-uri.c */
2114 static const gchar *
2115 get_method_string (const gchar  *substring, 
2116                    gchar       **method_string)
2117 {
2118   const gchar *p;
2119   char *method;
2120         
2121   for (p = substring;
2122        g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2123        p++)
2124     ;
2125
2126   if (*p == ':'
2127 #ifdef G_OS_WIN32
2128                 &&
2129       !(p == substring + 1 && g_ascii_isalpha (*substring))
2130 #endif
2131                                                            )
2132     {
2133       /* Found toplevel method specification.  */
2134       method = g_strndup (substring, p - substring);
2135       *method_string = g_ascii_strdown (method, -1);
2136       g_free (method);
2137       p++;
2138     }
2139   else
2140     {
2141       *method_string = g_strdup ("file");
2142       p = substring;
2143     }
2144   
2145   return p;
2146 }
2147
2148 /* Stolen from gnome_vfs_make_valid_utf8() */
2149 static char *
2150 make_valid_utf8 (const char *name)
2151 {
2152   GString *string;
2153   const char *remainder, *invalid;
2154   int remaining_bytes, valid_bytes;
2155
2156   string = NULL;
2157   remainder = name;
2158   remaining_bytes = name ? strlen (name) : 0;
2159
2160   while (remaining_bytes != 0)
2161     {
2162       if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2163         break;
2164       
2165       valid_bytes = invalid - remainder;
2166       
2167       if (string == NULL)
2168         string = g_string_sized_new (remaining_bytes);
2169       
2170       g_string_append_len (string, remainder, valid_bytes);
2171       g_string_append_c (string, '?');
2172       
2173       remaining_bytes -= valid_bytes + 1;
2174       remainder = invalid + 1;
2175     }
2176   
2177   if (string == NULL)
2178     return g_strdup (name);
2179
2180   g_string_append (string, remainder);
2181   g_assert (g_utf8_validate (string->str, -1, NULL));
2182
2183   return g_string_free (string, FALSE);
2184 }
2185
2186 static gchar *
2187 get_uri_shortname_for_display (const gchar *uri)
2188 {
2189   gchar *name = NULL;
2190   gboolean validated = FALSE;
2191
2192   if (has_case_prefix (uri, "file:/"))
2193     {
2194       gchar *local_file;
2195       
2196       local_file = g_filename_from_uri (uri, NULL, NULL);
2197       
2198       if (local_file)
2199         {
2200           name = g_filename_display_basename (local_file);
2201           validated = TRUE;
2202         }
2203                 
2204       g_free (local_file);
2205     } 
2206   
2207   if (!name)
2208     {
2209       gchar *method;
2210       gchar *local_file;
2211       const gchar *rest;
2212       
2213       rest = get_method_string (uri, &method);
2214       local_file = g_filename_display_basename (rest);
2215       
2216       name = g_strconcat (method, ": ", local_file, NULL);
2217       
2218       g_free (local_file);
2219       g_free (method);
2220     }
2221   
2222   g_assert (name != NULL);
2223   
2224   if (!validated && !g_utf8_validate (name, -1, NULL))
2225     {
2226       gchar *utf8_name;
2227       
2228       utf8_name = make_valid_utf8 (name);
2229       g_free (name);
2230       
2231       name = utf8_name;
2232     }
2233
2234   return name;
2235 }
2236
2237 /**
2238  * gtk_recent_info_get_short_name:
2239  * @info: an #GtkRecentInfo
2240  *
2241  * Computes a valid UTF-8 string that can be used as the name of the item in a
2242  * menu or list.  For example, calling this function on an item that refers to
2243  * "file:///foo/bar.txt" will yield "bar.txt".
2244  *
2245  * Return value: A newly-allocated string in UTF-8 encoding; free it with
2246  *   g_free().
2247  *
2248  * Since: 2.10
2249  */
2250 gchar *
2251 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2252 {
2253   gchar *short_name;
2254
2255   g_return_val_if_fail (info != NULL, NULL);
2256
2257   if (info->uri == NULL)
2258     return NULL;
2259
2260   short_name = get_uri_shortname_for_display (info->uri);
2261
2262   return short_name;
2263 }
2264
2265 /**
2266  * gtk_recent_info_get_uri_display:
2267  * @info: a #GtkRecentInfo
2268  *
2269  * Gets a displayable version of the resource's URI.  If the resource
2270  * is local, it returns a local path; if the resource is not local,
2271  * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2272  *
2273  * Return value: a newly allocated UTF-8 string containing the
2274  *   resource's URI or %NULL. Use g_free() when done using it.
2275  *
2276  * Since: 2.10
2277  */
2278 gchar *
2279 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2280 {
2281   gchar *retval;
2282   
2283   g_return_val_if_fail (info != NULL, NULL);
2284
2285   retval = NULL;
2286   if (gtk_recent_info_is_local (info))
2287     {
2288       gchar *filename;
2289
2290       filename = g_filename_from_uri (info->uri, NULL, NULL);
2291       if (!filename)
2292         return NULL;
2293       
2294       retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2295       g_free (filename);
2296     }
2297   else
2298     {
2299       retval = make_valid_utf8 (info->uri);
2300     }
2301
2302   return retval;
2303 }
2304
2305 /**
2306  * gtk_recent_info_get_age:
2307  * @info: a #GtkRecentInfo
2308  *
2309  * Gets the number of days elapsed since the last update of the resource
2310  * pointed by @info.
2311  *
2312  * Return value: a positive integer containing the number of days elapsed
2313  *   since the time this resource was last modified.  
2314  *
2315  * Since: 2.10
2316  */
2317 gint
2318 gtk_recent_info_get_age (GtkRecentInfo *info)
2319 {
2320   time_t now, delta;
2321   gint retval;
2322
2323   g_return_val_if_fail (info != NULL, -1);
2324
2325   now = time (NULL);
2326   
2327   delta = now - info->modified;
2328   
2329   retval = (gint) (delta / (60 * 60 * 24));
2330   
2331   return retval;
2332 }
2333
2334 /**
2335  * gtk_recent_info_get_groups:
2336  * @info: a #GtkRecentInfo
2337  * @length: (out) (allow-none): return location for the number of groups returned
2338  *
2339  * Returns all groups registered for the recently used item @info.  The
2340  * array of returned group names will be %NULL terminated, so length might
2341  * optionally be %NULL.
2342  *
2343  * Return value:  (array length=length zero-terminated=1) (transfer full):
2344  *     a newly allocated %NULL terminated array of strings.
2345  *     Use g_strfreev() to free it.
2346  *
2347  * Since: 2.10
2348  */
2349 gchar **
2350 gtk_recent_info_get_groups (GtkRecentInfo *info,
2351                             gsize         *length)
2352 {
2353   GSList *l;
2354   gchar **retval;
2355   gsize n_groups, i;
2356   
2357   g_return_val_if_fail (info != NULL, NULL);
2358   
2359   if (!info->groups)
2360     {
2361       if (length)
2362         *length = 0;
2363       
2364       return NULL;
2365     }
2366   
2367   n_groups = g_slist_length (info->groups);
2368   
2369   retval = g_new0 (gchar *, n_groups + 1);
2370   
2371   for (l = info->groups, i = 0;
2372        l != NULL;
2373        l = l->next)
2374     {
2375       gchar *group_name = (gchar *) l->data;
2376       
2377       g_assert (group_name != NULL);
2378       
2379       retval[i++] = g_strdup (group_name);
2380     }
2381   retval[i] = NULL;
2382   
2383   if (length)
2384     *length = i;
2385   
2386   return retval;
2387 }
2388
2389 /**
2390  * gtk_recent_info_has_group:
2391  * @info: a #GtkRecentInfo
2392  * @group_name: name of a group
2393  *
2394  * Checks whether @group_name appears inside the groups registered for the
2395  * recently used item @info.
2396  *
2397  * Return value: %TRUE if the group was found.
2398  *
2399  * Since: 2.10
2400  */
2401 gboolean
2402 gtk_recent_info_has_group (GtkRecentInfo *info,
2403                            const gchar   *group_name)
2404 {
2405   GSList *l;
2406   
2407   g_return_val_if_fail (info != NULL, FALSE);
2408   g_return_val_if_fail (group_name != NULL, FALSE);
2409
2410   if (!info->groups)
2411     return FALSE;
2412
2413   for (l = info->groups; l != NULL; l = l->next)
2414     {
2415       gchar *g = (gchar *) l->data;
2416
2417       if (strcmp (g, group_name) == 0)
2418         return TRUE;
2419     }
2420
2421   return FALSE;
2422 }
2423
2424 /**
2425  * gtk_recent_info_create_app_info:
2426  * @info: a #GtkRecentInfo
2427  * @app_name: (allow-none): the name of the application that should
2428  *   be mapped to a #GAppInfo; if %NULL is used then the default
2429  *   application for the MIME type is used
2430  * @error: (allow-none): return location for a #GError, or %NULL
2431  *
2432  * Creates a #GAppInfo for the specified #GtkRecentInfo
2433  *
2434  * Return value: (transfer full): the newly created #GAppInfo, or %NULL.
2435  *   In case of error, @error will be set either with a
2436  *   %GTK_RECENT_MANAGER_ERROR or a %G_IO_ERROR
2437  */
2438 GAppInfo *
2439 gtk_recent_info_create_app_info (GtkRecentInfo  *info,
2440                                  const gchar    *app_name,
2441                                  GError        **error)
2442 {
2443   RecentAppInfo *ai;
2444   GAppInfo *app_info;
2445   GError *internal_error = NULL;
2446
2447   g_return_val_if_fail (info != NULL, NULL);
2448
2449   if (app_name == NULL || *app_name == '\0')
2450     {
2451       char *content_type;
2452
2453       if (info->mime_type == NULL)
2454         return NULL;
2455
2456       content_type = g_content_type_from_mime_type (info->mime_type);
2457       if (content_type == NULL)
2458         return NULL;
2459
2460       app_info = g_app_info_get_default_for_type (content_type, TRUE);
2461       g_free (content_type);
2462
2463       return app_info;
2464     }
2465
2466   ai = g_hash_table_lookup (info->apps_lookup, app_name);
2467   if (ai == NULL)
2468     {
2469       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
2470                    GTK_RECENT_MANAGER_ERROR_NOT_REGISTERED,
2471                    _("No registered application with name '%s' for item with URI '%s' found"),
2472                    app_name,
2473                    info->uri);
2474       return NULL;
2475     }
2476
2477   internal_error = NULL;
2478   app_info = g_app_info_create_from_commandline (ai->exec, ai->name,
2479                                                  G_APP_INFO_CREATE_NONE,
2480                                                  &internal_error);
2481   if (internal_error != NULL)
2482     {
2483       g_propagate_error (error, internal_error);
2484       return NULL;
2485     }
2486
2487   return app_info;
2488 }
2489
2490 /*
2491  * _gtk_recent_manager_sync:
2492  * 
2493  * Private function for synchronising the recent manager singleton.
2494  */
2495 void
2496 _gtk_recent_manager_sync (void)
2497 {
2498   if (recent_manager_singleton)
2499     {
2500       /* force a dump of the contents of the recent manager singleton */
2501       recent_manager_singleton->priv->is_dirty = TRUE;
2502       gtk_recent_manager_real_changed (recent_manager_singleton);
2503     }
2504 }