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