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