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