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