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