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