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