]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentmanager.c
Do not cache the icon pixbufs but rely on the GtkIconTheme not to hit the
[~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 void
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
1142 /**
1143  * gtk_recent_manager_lookup_item:
1144  * @manager: a #GtkRecentManager
1145  * @uri: a URI
1146  * @error: a return location for a #GError, or %NULL
1147  *
1148  * Searches for a URI inside the recently used resources list, and
1149  * returns a structure containing informations about the resource
1150  * like its MIME type, or its display name.
1151  *
1152  * Return value: a #GtkRecentInfo structure containing information
1153  *   about the resource pointed by @uri, or %NULL if the URI was
1154  *   not registered in the recently used resources list.  Free with
1155  *   gtk_recent_info_unref().
1156  *
1157  * Since: 2.10
1158  */
1159 GtkRecentInfo *
1160 gtk_recent_manager_lookup_item (GtkRecentManager  *manager,
1161                                 const gchar       *uri,
1162                                 GError           **error)
1163 {
1164   GtkRecentManagerPrivate *priv;
1165   GtkRecentInfo *info = NULL;
1166   
1167   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1168   g_return_val_if_fail (uri != NULL, NULL);
1169   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1170   
1171   priv = manager->priv;
1172   if (!priv->recent_items)
1173     {
1174       priv->recent_items = g_bookmark_file_new ();
1175       priv->size = 0;
1176
1177       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1178                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1179                    _("Unable to find an item with URI '%s'"),
1180                    uri);
1181
1182       return NULL;
1183     }
1184   
1185   if (!g_bookmark_file_has_item (priv->recent_items, uri))
1186     {
1187       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1188                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1189                    _("Unable to find an item with URI '%s'"),
1190                    uri);
1191       return NULL;
1192     }
1193   
1194   info = gtk_recent_info_new (uri);
1195   g_return_val_if_fail (info != NULL, NULL);
1196   
1197   /* fill the RecentInfo structure with the data retrieved by our
1198    * parser object from the storage file 
1199    */
1200   build_recent_info (priv->recent_items, info);
1201
1202   return info;
1203 }
1204
1205 /**
1206  * gtk_recent_manager_move_item:
1207  * @manager: a #GtkRecentManager
1208  * @uri: the URI of a recently used resource
1209  * @new_uri: the new URI of the recently used resource, or %NULL to
1210  *    remove the item pointed by @uri in the list
1211  * @error: a return location for a #GError, or %NULL
1212  *
1213  * Changes the location of a recently used resource from @uri to @new_uri.
1214  * 
1215  * Please note that this function will not affect the resource pointed
1216  * by the URIs, but only the URI used in the recently used resources list.
1217  *
1218  * Return value: %TRUE on success.
1219  *
1220  * Since: 2.10
1221  */ 
1222 gboolean
1223 gtk_recent_manager_move_item (GtkRecentManager  *recent_manager,
1224                               const gchar       *uri,
1225                               const gchar       *new_uri,
1226                               GError           **error)
1227 {
1228   GtkRecentManagerPrivate *priv;
1229   GError *move_error;
1230   gboolean res;
1231   
1232   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1233   g_return_val_if_fail (uri != NULL, FALSE);
1234   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1235   
1236   priv = recent_manager->priv;
1237
1238   if (!g_bookmark_file_has_item (priv->recent_items, uri))
1239     {
1240       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1241                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1242                    _("Unable to find an item with URI '%s'"),
1243                    uri);
1244       return FALSE;
1245     }
1246   
1247   move_error = NULL;
1248   res = g_bookmark_file_move_item (priv->recent_items,
1249                                    uri, new_uri,
1250                                    &move_error);
1251   if (move_error)
1252     {
1253       g_propagate_error (error, move_error);
1254       return FALSE;
1255     }
1256   
1257   priv->is_dirty = TRUE;
1258
1259   gtk_recent_manager_changed (recent_manager);
1260   
1261   return TRUE;
1262 }
1263
1264 /**
1265  * gtk_recent_manager_get_items:
1266  * @manager: a #GtkRecentManager
1267  *
1268  * Gets the list of recently used resources.
1269  *
1270  * Return value: a list of newly allocated #GtkRecentInfo objects. Use
1271  *   gtk_recent_info_unref() on each item inside the list, and then
1272  *   free the list itself using g_list_free().
1273  *
1274  * Since: 2.10
1275  */
1276 GList *
1277 gtk_recent_manager_get_items (GtkRecentManager *manager)
1278 {
1279   GtkRecentManagerPrivate *priv;
1280   GList *retval = NULL;
1281   gchar **uris;
1282   gsize uris_len, i;
1283   
1284   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1285   
1286   priv = manager->priv;
1287   if (!priv->recent_items)
1288     return NULL;
1289
1290   if (priv->limit == 0)
1291     return NULL;
1292   
1293   uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1294   for (i = 0; i < uris_len; i++)
1295     {
1296       GtkRecentInfo *info;
1297       
1298       if (priv->limit != -1 && i == priv->limit)
1299         break;
1300       
1301       info = gtk_recent_info_new (uris[i]);
1302       build_recent_info (priv->recent_items, info);
1303       
1304       retval = g_list_prepend (retval, info);
1305     }
1306   
1307   g_strfreev (uris);
1308   
1309   return retval;
1310 }
1311
1312 static void
1313 purge_recent_items_list (GtkRecentManager  *manager,
1314                          GError           **error)
1315 {
1316   GtkRecentManagerPrivate *priv = manager->priv;
1317
1318   if (!priv->recent_items)
1319     return;
1320   
1321   g_bookmark_file_free (priv->recent_items);
1322   priv->recent_items = NULL;
1323       
1324   priv->recent_items = g_bookmark_file_new ();
1325   priv->size = 0;
1326   priv->is_dirty = TRUE;
1327       
1328   /* emit the changed signal, to ensure that the purge is written */
1329   gtk_recent_manager_changed (manager);
1330 }
1331
1332 /**
1333  * gtk_recent_manager_purge_items:
1334  * @manager: a #GtkRecentManager
1335  * @error: a return location for a #GError, or %NULL
1336  *
1337  * Purges every item from the recently used resources list.
1338  *
1339  * Return value: the number of items that have been removed from the
1340  *   recently used resources list.
1341  *
1342  * Since: 2.10
1343  */
1344 gint
1345 gtk_recent_manager_purge_items (GtkRecentManager  *manager,
1346                                 GError           **error)
1347 {
1348   GtkRecentManagerPrivate *priv;
1349   gint count, purged;
1350   
1351   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1352
1353   priv = manager->priv;
1354   if (!priv->recent_items)
1355     return 0;
1356   
1357   count = g_bookmark_file_get_size (priv->recent_items);
1358   if (!count)
1359     return 0;
1360   
1361   purge_recent_items_list (manager, error);
1362   
1363   purged = count - g_bookmark_file_get_size (priv->recent_items);
1364
1365   return purged;
1366 }
1367
1368 static void
1369 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1370 {
1371   g_signal_emit (recent_manager, signal_changed, 0);
1372 }
1373
1374 /*****************
1375  * GtkRecentInfo *
1376  *****************/
1377  
1378 GType
1379 gtk_recent_info_get_type (void)
1380 {
1381   static GType info_type = 0;
1382   
1383   if (!info_type)
1384     info_type = g_boxed_type_register_static (I_("GtkRecentInfo"),
1385                                               (GBoxedCopyFunc) gtk_recent_info_ref,
1386                                               (GBoxedFreeFunc) gtk_recent_info_unref);
1387   return info_type;
1388 }
1389
1390 static GtkRecentInfo *
1391 gtk_recent_info_new (const gchar *uri)
1392 {
1393   GtkRecentInfo *info;
1394
1395   g_assert (uri != NULL);
1396
1397   info = g_new0 (GtkRecentInfo, 1);
1398   info->uri = g_strdup (uri);
1399   
1400   info->applications = NULL;
1401   info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1402   
1403   info->groups = NULL;
1404   
1405   info->ref_count = 1;
1406
1407   return info;
1408 }
1409
1410 static void
1411 gtk_recent_info_free (GtkRecentInfo *recent_info)
1412 {
1413   if (!recent_info)
1414     return;
1415
1416   g_free (recent_info->uri);
1417   g_free (recent_info->display_name);
1418   g_free (recent_info->description);
1419   g_free (recent_info->mime_type);
1420   
1421   if (recent_info->applications)
1422     {
1423       g_slist_foreach (recent_info->applications,
1424                        (GFunc) recent_app_info_free,
1425                        NULL);
1426       g_slist_free (recent_info->applications);
1427       
1428       recent_info->applications = NULL;
1429     }
1430   
1431   if (recent_info->apps_lookup)
1432     g_hash_table_destroy (recent_info->apps_lookup);
1433
1434   if (recent_info->groups)
1435     {
1436       g_slist_foreach (recent_info->groups,
1437                        (GFunc) g_free,
1438                        NULL);
1439       g_slist_free (recent_info->groups);
1440
1441       recent_info->groups = NULL;
1442     }
1443   
1444   if (recent_info->icon)
1445     g_object_unref (recent_info->icon);
1446
1447   g_free (recent_info);
1448 }
1449
1450 /**
1451  * gtk_recent_info_ref:
1452  * @info: a #GtkRecentInfo
1453  *
1454  * Increases the reference count of @recent_info by one.
1455  *
1456  * Return value: the recent info object with its reference count increased
1457  *   by one.
1458  *
1459  * Since: 2.10
1460  */
1461 GtkRecentInfo *
1462 gtk_recent_info_ref (GtkRecentInfo *info)
1463 {
1464   g_return_val_if_fail (info != NULL, NULL);
1465   g_return_val_if_fail (info->ref_count > 0, NULL);
1466   
1467   info->ref_count += 1;
1468     
1469   return info;
1470 }
1471
1472 /**
1473  * gtk_recent_info_unref:
1474  * @info: a #GtkRecentInfo
1475  *
1476  * Decreases the reference count of @info by one.  If the reference
1477  * count reaches zero, @info is deallocated, and the memory freed.
1478  *
1479  * Since: 2.10
1480  */
1481 void
1482 gtk_recent_info_unref (GtkRecentInfo *info)
1483 {
1484   g_return_if_fail (info != NULL);
1485   g_return_if_fail (info->ref_count > 0);
1486
1487   info->ref_count -= 1;
1488   
1489   if (info->ref_count == 0)
1490     gtk_recent_info_free (info);
1491 }
1492
1493 /**
1494  * gtk_recent_info_get_uri:
1495  * @info: a #GtkRecentInfo
1496  *
1497  * Gets the URI of the resource.
1498  *
1499  * Return value: the URI of the resource.  The returned string is
1500  *   owned by the recent manager, and should not be freed.
1501  *
1502  * Since: 2.10
1503  */
1504 G_CONST_RETURN gchar *
1505 gtk_recent_info_get_uri (GtkRecentInfo *info)
1506 {
1507   g_return_val_if_fail (info != NULL, NULL);
1508   
1509   return info->uri;
1510 }
1511
1512 /**
1513  * gtk_recent_info_get_display_name:
1514  * @info: a #GtkRecentInfo
1515  *
1516  * Gets the name of the resource.  If none has been defined, the basename
1517  * of the resource is obtained.
1518  *
1519  * Return value: the display name of the resource.  The returned string
1520  *   is owned by the recent manager, and should not be freed.
1521  *
1522  * Since: 2.10
1523  */
1524 G_CONST_RETURN gchar *
1525 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1526 {
1527   g_return_val_if_fail (info != NULL, NULL);
1528   
1529   if (!info->display_name)
1530     info->display_name = gtk_recent_info_get_short_name (info);
1531   
1532   return info->display_name;
1533 }
1534
1535 /**
1536  * gtk_recent_info_get_description:
1537  * @info: a #GtkRecentInfo
1538  *
1539  * Gets the (short) description of the resource.
1540  *
1541  * Return value: the description of the resource.  The returned string
1542  *   is owned by the recent manager, and should not be freed.
1543  *
1544  * Since: 2.10
1545  **/
1546 G_CONST_RETURN gchar *
1547 gtk_recent_info_get_description (GtkRecentInfo *info)
1548 {
1549   g_return_val_if_fail (info != NULL, NULL);
1550   
1551   return info->description;
1552 }
1553
1554 /**
1555  * gtk_recent_info_get_mime_type:
1556  * @info: a #GtkRecentInfo
1557  *
1558  * Gets the MIME type of the resource.
1559  *
1560  * Return value: the MIME type of the resource.  The returned string
1561  *   is owned by the recent manager, and should not be freed.
1562  *
1563  * Since: 2.10
1564  */
1565 G_CONST_RETURN gchar *
1566 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1567 {
1568   g_return_val_if_fail (info != NULL, NULL);
1569   
1570   if (!info->mime_type)
1571     info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1572   
1573   return info->mime_type;
1574 }
1575
1576 /**
1577  * gtk_recent_info_get_added:
1578  * @info: a #GtkRecentInfo
1579  *
1580  * Gets the timestamp (seconds from system's Epoch) when the resource
1581  * was added to the recently used resources list.
1582  *
1583  * Return value: the number of seconds elapsed from system's Epoch when
1584  *   the resource was added to the list, or -1 on failure.
1585  *
1586  * Since: 2.10
1587  */
1588 time_t
1589 gtk_recent_info_get_added (GtkRecentInfo *info)
1590 {
1591   g_return_val_if_fail (info != NULL, (time_t) -1);
1592   
1593   return info->added;
1594 }
1595
1596 /**
1597  * gtk_recent_info_get_modified:
1598  * @info: a #GtkRecentInfo
1599  *
1600  * Gets the timestamp (seconds from system's Epoch) when the resource
1601  * was last modified.
1602  *
1603  * Return value: the number of seconds elapsed from system's Epoch when
1604  *   the resource was last modified, or -1 on failure.
1605  *
1606  * Since: 2.10
1607  */
1608 time_t
1609 gtk_recent_info_get_modified (GtkRecentInfo *info)
1610 {
1611   g_return_val_if_fail (info != NULL, (time_t) -1);
1612   
1613   return info->modified;
1614 }
1615
1616 /**
1617  * gtk_recent_info_get_visited:
1618  * @info: a #GtkRecentInfo
1619  *
1620  * Gets the timestamp (seconds from system's Epoch) when the resource
1621  * was last visited.
1622  *
1623  * Return value: the number of seconds elapsed from system's Epoch when
1624  *   the resource was last visited, or -1 on failure.
1625  *
1626  * Since: 2.10
1627  */
1628 time_t
1629 gtk_recent_info_get_visited (GtkRecentInfo *info)
1630 {
1631   g_return_val_if_fail (info != NULL, (time_t) -1);
1632   
1633   return info->visited;
1634 }
1635
1636 /**
1637  * gtk_recent_info_get_private_hint:
1638  * @info: a #GtkRecentInfo
1639  *
1640  * Gets the value of the "private" flag.  Resources in the recently used
1641  * list that have this flag set to %TRUE should only be displayed by the
1642  * applications that have registered them.
1643  *
1644  * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1645  *
1646  * Since: 2.10
1647  */
1648 gboolean
1649 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1650 {
1651   g_return_val_if_fail (info != NULL, FALSE);
1652   
1653   return info->is_private;
1654 }
1655
1656
1657 static RecentAppInfo *
1658 recent_app_info_new (const gchar *app_name)
1659 {
1660   RecentAppInfo *app_info;
1661
1662   g_assert (app_name != NULL);
1663   
1664   app_info = g_new0 (RecentAppInfo, 1);
1665   app_info->name = g_strdup (app_name);
1666   app_info->exec = NULL;
1667   app_info->count = 1;
1668   app_info->stamp = time (NULL);
1669   
1670   return app_info;
1671 }
1672
1673 static void
1674 recent_app_info_free (RecentAppInfo *app_info)
1675 {
1676   if (!app_info)
1677     return;
1678   
1679   g_free (app_info->name);
1680   
1681   g_free (app_info->exec);
1682   
1683   g_free (app_info);
1684 }
1685
1686 /**
1687  * gtk_recent_info_get_application_info:
1688  * @info: a #GtkRecentInfo
1689  * @app_name: the name of the application that has registered this item
1690  * @app_exec: return location for the string containing the command line
1691  * @count: return location for the number of times this item was registered
1692  * @time_: return location for the timestamp this item was last registered
1693  *    for this application
1694  *
1695  * Gets the data regarding the application that has registered the resource
1696  * pointed by @info.
1697  *
1698  * If the command line contains any escape characters defined inside the
1699  * storage specification, they will be expanded.
1700  *
1701  * Return value: %TRUE if an application with @app_name has registered this
1702  *   resource inside the recently used list, or %FALSE otherwise.  You should
1703  *   free the returned command line using g_free().
1704  *
1705  * Since: 2.10
1706  */
1707 gboolean
1708 gtk_recent_info_get_application_info (GtkRecentInfo  *info,
1709                                       const gchar    *app_name,
1710                                       gchar         **app_exec,
1711                                       guint          *count,
1712                                       time_t         *time_)
1713 {
1714   RecentAppInfo *ai;
1715   
1716   g_return_val_if_fail (info != NULL, FALSE);
1717   g_return_val_if_fail (app_name != NULL, FALSE);
1718   
1719   ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1720                                               app_name);
1721   if (!ai)
1722     {
1723       g_warning ("No registered application with name '%s' "
1724                  "for item with URI '%s' found",
1725                  app_name,
1726                  info->uri);
1727       return FALSE;
1728     }
1729   
1730   if (app_exec)
1731     *app_exec = ai->exec;
1732   
1733   if (count)
1734     *count = ai->count;
1735   
1736   if (time_)
1737     *time_ = ai->stamp;
1738
1739   return TRUE;
1740 }
1741
1742 /**
1743  * gtk_recent_info_get_applications:
1744  * @info: a #GtkRecentInfo
1745  * @length: return location for the length of the returned list, or %NULL
1746  *
1747  * Retrieves the list of applications that have registered this resource.
1748  *
1749  * Return value: a newly allocated %NULL-terminated array of strings.
1750  *   Use g_strfreev() to free it.
1751  *
1752  * Since: 2.10
1753  */                           
1754 gchar **
1755 gtk_recent_info_get_applications (GtkRecentInfo *info,
1756                                   gsize         *length)
1757 {
1758   GSList *l;
1759   gchar **retval;
1760   gsize n_apps, i;
1761   
1762   g_return_val_if_fail (info != NULL, NULL);
1763   
1764   if (!info->applications)
1765     {
1766       if (length)
1767         *length = 0;
1768       
1769       return NULL;    
1770     }
1771   
1772   n_apps = g_slist_length (info->applications);
1773   
1774   retval = g_new0 (gchar *, n_apps + 1);
1775   
1776   for (l = info->applications, i = 0;
1777        l != NULL;
1778        l = l->next)
1779     {
1780       RecentAppInfo *ai = (RecentAppInfo *) l->data;
1781       
1782       g_assert (ai != NULL);
1783       
1784       retval[i++] = g_strdup (ai->name);
1785     }
1786   retval[i] = NULL;
1787   
1788   if (length)
1789     *length = i;
1790   
1791   return retval;
1792 }
1793
1794 /**
1795  * gtk_recent_info_has_application:
1796  * @info: a #GtkRecentInfo
1797  * @app_name: a string containing an application name
1798  *
1799  * Checks whether an application registered this resource using @app_name.
1800  *
1801  * Return value: %TRUE if an application with name @app_name was found,
1802  *   %FALSE otherwise.
1803  *
1804  * Since: 2.10
1805  */
1806 gboolean
1807 gtk_recent_info_has_application (GtkRecentInfo *info,
1808                                  const gchar   *app_name)
1809 {
1810   g_return_val_if_fail (info != NULL, FALSE);
1811   g_return_val_if_fail (app_name != NULL, FALSE);
1812   
1813   return (NULL != g_hash_table_lookup (info->apps_lookup, app_name));
1814 }
1815
1816 /**
1817  * gtk_recent_info_last_application:
1818  * @info: a #GtkRecentInfo
1819  *
1820  * Gets the name of the last application that have registered the
1821  * recently used resource represented by @info.
1822  *
1823  * Return value: an application name.  Use g_free() to free it.
1824  *
1825  * Since: 2.10
1826  */
1827 gchar *
1828 gtk_recent_info_last_application (GtkRecentInfo  *info)
1829 {
1830   GSList *l;
1831   time_t last_stamp = (time_t) -1;
1832   gchar *name = NULL;
1833   
1834   g_return_val_if_fail (info != NULL, NULL);
1835   
1836   for (l = info->applications; l != NULL; l = l->next)
1837     {
1838       RecentAppInfo *ai = (RecentAppInfo *) l->data;
1839       
1840       if (ai->stamp > last_stamp)
1841         {
1842           name = ai->name;
1843           last_stamp = ai->stamp;
1844         }
1845     }
1846   
1847   return g_strdup (name);
1848 }
1849
1850 static GdkPixbuf *
1851 get_icon_for_mime_type (const char *mime_type,
1852                         gint        pixel_size)
1853 {
1854   GtkIconTheme *icon_theme;
1855   const char *separator;
1856   GString *icon_name;
1857   GdkPixbuf *pixbuf;
1858
1859   separator = strchr (mime_type, '/');
1860   if (!separator)
1861     return NULL;
1862
1863   icon_theme = gtk_icon_theme_get_default ();
1864
1865   /* try with the three icon name variants for MIME types */
1866
1867   /* canonicalize MIME type: foo/x-bar -> foo-x-bar */
1868   icon_name = g_string_new (NULL);
1869   g_string_append_len (icon_name, mime_type, separator - mime_type);
1870   g_string_append_c (icon_name, '-');
1871   g_string_append (icon_name, separator + 1);
1872   pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1873                                      pixel_size,
1874                                      0,
1875                                      NULL);
1876   g_string_free (icon_name, TRUE);
1877   if (pixbuf)
1878     return pixbuf;
1879
1880   /* canonicalize MIME type, and prepend "gnome-mime-" */
1881   icon_name = g_string_new ("gnome-mime-");
1882   g_string_append_len (icon_name, mime_type, separator - mime_type);
1883   g_string_append_c (icon_name, '-');
1884   g_string_append (icon_name, separator + 1);
1885   pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1886                                      pixel_size,
1887                                      0,
1888                                      NULL);
1889   g_string_free (icon_name, TRUE);
1890   if (pixbuf)
1891     return pixbuf;
1892
1893   /* try the MIME family icon */
1894   icon_name = g_string_new ("gnome-mime-");
1895   g_string_append_len (icon_name, mime_type, separator - mime_type);
1896   pixbuf = gtk_icon_theme_load_icon (icon_theme, icon_name->str,
1897                                      pixel_size,
1898                                      0,
1899                                      NULL);
1900   g_string_free (icon_name, TRUE);
1901
1902   return pixbuf;
1903 }
1904
1905 static GdkPixbuf *
1906 get_icon_fallback (const gchar *icon_name,
1907                    gint         size)
1908 {
1909   GtkIconTheme *icon_theme;
1910   GdkPixbuf *retval;
1911
1912   icon_theme = gtk_icon_theme_get_default ();
1913   
1914   retval = gtk_icon_theme_load_icon (icon_theme, icon_name,
1915                                      size,
1916                                      GTK_ICON_LOOKUP_USE_BUILTIN,
1917                                      NULL);
1918   g_assert (retval != NULL);
1919   
1920   return retval; 
1921 }
1922
1923 /**
1924  * gtk_recent_info_get_icon:
1925  * @info: a #GtkRecentInfo
1926  * @size: the size of the icon in pixels
1927  *
1928  * Retrieves the icon of size @size associated to the resource MIME type.
1929  *
1930  * Return value: a #GdkPixbuf containing the icon, or %NULL.
1931  *
1932  * Since: 2.10
1933  */
1934 GdkPixbuf *
1935 gtk_recent_info_get_icon (GtkRecentInfo *info,
1936                           gint           size)
1937 {
1938   GdkPixbuf *retval = NULL;
1939   
1940   g_return_val_if_fail (info != NULL, NULL);
1941   
1942   if (info->mime_type)
1943     retval = get_icon_for_mime_type (info->mime_type, size);
1944
1945   /* this function should never fail */  
1946   if (!retval)
1947     {
1948       if (info->mime_type &&
1949           strcmp (info->mime_type, "x-directory/normal") == 0)
1950         retval = get_icon_fallback (GTK_STOCK_DIRECTORY, size);
1951       else
1952         retval = get_icon_fallback (GTK_STOCK_FILE, size);
1953     }
1954   
1955   return retval;
1956 }
1957
1958 /**
1959  * gtk_recent_info_is_local:
1960  * @info: a #GtkRecentInfo
1961  *
1962  * Checks whether the resource is local or not by looking at the
1963  * scheme of its URI.
1964  *
1965  * Return value: %TRUE if the resource is local.
1966  *
1967  * Since: 2.10
1968  */
1969 gboolean
1970 gtk_recent_info_is_local (GtkRecentInfo *info)
1971 {
1972   g_return_val_if_fail (info != NULL, FALSE);
1973   
1974   return has_case_prefix (info->uri, "file:/");
1975 }
1976
1977 /**
1978  * gtk_recent_info_exists:
1979  * @info: a #GtkRecentInfo
1980  *
1981  * Checks whether the resource pointed by @info still exists.  At
1982  * the moment this check is done only on resources pointing to local files.
1983  *
1984  * Return value: %TRUE if the resource exists
1985  *
1986  * Since: 2.10
1987  */
1988 gboolean
1989 gtk_recent_info_exists (GtkRecentInfo *info)
1990 {
1991   gchar *filename;
1992   struct stat stat_buf;
1993   gboolean retval = FALSE;
1994   
1995   g_return_val_if_fail (info != NULL, FALSE);
1996   
1997   /* we guarantee only local resources */
1998   if (!gtk_recent_info_is_local (info))
1999     return FALSE;
2000   
2001   filename = g_filename_from_uri (info->uri, NULL, NULL);
2002   if (filename)
2003     {
2004       if (stat (filename, &stat_buf) == 0)
2005         retval = TRUE;
2006      
2007       g_free (filename);
2008     }
2009   
2010   return retval;
2011 }
2012
2013 /**
2014  * gtk_recent_info_match:
2015  * @info_a: a #GtkRecentInfo
2016  * @info_b: a #GtkRecentInfo
2017  *
2018  * Checks whether two #GtkRecentInfo structures point to the same
2019  * resource.
2020  *
2021  * Return value: %TRUE if both #GtkRecentInfo structures point to se same
2022  *   resource, %FALSE otherwise.
2023  *
2024  * Since: 2.10
2025  */
2026 gboolean
2027 gtk_recent_info_match (GtkRecentInfo *info_a,
2028                        GtkRecentInfo *info_b)
2029 {
2030   g_return_val_if_fail (info_a != NULL, FALSE);
2031   g_return_val_if_fail (info_b != NULL, FALSE);
2032   
2033   return (0 == strcmp (info_a->uri, info_b->uri));
2034 }
2035
2036 /* taken from gnome-vfs-uri.c */
2037 static const gchar *
2038 get_method_string (const gchar  *substring, 
2039                    gchar       **method_string)
2040 {
2041   const gchar *p;
2042   char *method;
2043         
2044   for (p = substring;
2045        g_ascii_isalnum (*p) || *p == '+' || *p == '-' || *p == '.';
2046        p++)
2047     ;
2048
2049   if (*p == ':'
2050 #ifdef G_OS_WIN32
2051                 &&
2052       !(p == substring + 1 && g_ascii_isalpha (*substring))
2053 #endif
2054                                                            )
2055     {
2056       /* Found toplevel method specification.  */
2057       method = g_strndup (substring, p - substring);
2058       *method_string = g_ascii_strdown (method, -1);
2059       g_free (method);
2060       p++;
2061     }
2062   else
2063     {
2064       *method_string = g_strdup ("file");
2065       p = substring;
2066     }
2067   
2068   return p;
2069 }
2070
2071 /* Stolen from gnome_vfs_make_valid_utf8() */
2072 static char *
2073 make_valid_utf8 (const char *name)
2074 {
2075   GString *string;
2076   const char *remainder, *invalid;
2077   int remaining_bytes, valid_bytes;
2078
2079   string = NULL;
2080   remainder = name;
2081   remaining_bytes = name ? strlen (name) : 0;
2082
2083   while (remaining_bytes != 0)
2084     {
2085       if (g_utf8_validate (remainder, remaining_bytes, &invalid))
2086         break;
2087       
2088       valid_bytes = invalid - remainder;
2089       
2090       if (string == NULL)
2091         string = g_string_sized_new (remaining_bytes);
2092       
2093       g_string_append_len (string, remainder, valid_bytes);
2094       g_string_append_c (string, '?');
2095       
2096       remaining_bytes -= valid_bytes + 1;
2097       remainder = invalid + 1;
2098     }
2099   
2100   if (string == NULL)
2101     return g_strdup (name);
2102
2103   g_string_append (string, remainder);
2104   g_assert (g_utf8_validate (string->str, -1, NULL));
2105
2106   return g_string_free (string, FALSE);
2107 }
2108
2109 static gchar *
2110 get_uri_shortname_for_display (const gchar *uri)
2111 {
2112   gchar *name = NULL;
2113   gboolean validated = FALSE;
2114
2115   if (has_case_prefix (uri, "file:/"))
2116     {
2117       gchar *local_file;
2118       
2119       local_file = g_filename_from_uri (uri, NULL, NULL);
2120       
2121       if (local_file)
2122         {
2123           name = g_filename_display_basename (local_file);
2124           validated = TRUE;
2125         }
2126                 
2127       g_free (local_file);
2128     } 
2129   
2130   if (!name)
2131     {
2132       gchar *method;
2133       gchar *local_file;
2134       const gchar *rest;
2135       
2136       rest = get_method_string (uri, &method);
2137       local_file = g_filename_display_basename (rest);
2138       
2139       name = g_strdup_printf ("%s: %s", method, local_file);
2140       
2141       g_free (local_file);
2142       g_free (method);
2143     }
2144   
2145   g_assert (name != NULL);
2146   
2147   if (!validated && !g_utf8_validate (name, -1, NULL))
2148     {
2149       gchar *utf8_name;
2150       
2151       utf8_name = make_valid_utf8 (name);
2152       g_free (name);
2153       
2154       name = utf8_name;
2155     }
2156
2157   return name;
2158 }
2159
2160 /**
2161  * gtk_recent_info_get_short_name:
2162  * @info: an #GtkRecentInfo
2163  *
2164  * Computes a valid UTF-8 string that can be used as the name of the item in a
2165  * menu or list.  For example, calling this function on an item that refers to
2166  * "file:///foo/bar.txt" will yield "bar.txt".
2167  *
2168  * Return value: A newly-allocated string in UTF-8 encoding; free it with
2169  *   g_free().
2170  *
2171  * Since: 2.10
2172  */
2173 gchar *
2174 gtk_recent_info_get_short_name (GtkRecentInfo *info)
2175 {
2176   gchar *short_name;
2177
2178   g_return_val_if_fail (info != NULL, NULL);
2179
2180   if (info->uri == NULL)
2181     return NULL;
2182
2183   short_name = get_uri_shortname_for_display (info->uri);
2184
2185   return short_name;
2186 }
2187
2188 /**
2189  * gtk_recent_info_get_uri_display:
2190  * @info: a #GtkRecentInfo
2191  *
2192  * Gets a displayable version of the resource's URI.  If the resource
2193  * is local, it returns a local path; if the resource is not local,
2194  * it returns the UTF-8 encoded content of gtk_recent_info_get_uri().
2195  *
2196  * Return value: a UTF-8 string containing the resource's URI or %NULL
2197  *
2198  * Since: 2.10
2199  */
2200 gchar *
2201 gtk_recent_info_get_uri_display (GtkRecentInfo *info)
2202 {
2203   gchar *retval;
2204   
2205   g_return_val_if_fail (info != NULL, NULL);
2206
2207   retval = NULL;
2208   if (gtk_recent_info_is_local (info))
2209     {
2210       gchar *filename;
2211
2212       filename = g_filename_from_uri (info->uri, NULL, NULL);
2213       if (!filename)
2214         return NULL;
2215       
2216       retval = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
2217       g_free (filename);
2218     }
2219   else
2220     {
2221       retval = make_valid_utf8 (info->uri);
2222     }
2223
2224   return retval;
2225 }
2226
2227 /**
2228  * gtk_recent_info_get_age:
2229  * @info: a #GtkRecentInfo
2230  *
2231  * Gets the number of days elapsed since the last update of the resource
2232  * pointed by @info.
2233  *
2234  * Return value: a positive integer containing the number of days elapsed
2235  *   since the time this resource was last modified.  
2236  *
2237  * Since: 2.10
2238  */
2239 gint
2240 gtk_recent_info_get_age (GtkRecentInfo *info)
2241 {
2242   time_t now, delta;
2243   gint retval;
2244
2245   g_return_val_if_fail (info != NULL, -1);
2246
2247   now = time (NULL);
2248   
2249   delta = now - info->modified;
2250   
2251   retval = (gint) (delta / (60 * 60 * 24));
2252   
2253   return retval;
2254 }
2255
2256 /**
2257  * gtk_recent_info_get_groups:
2258  * @info: a #GtkRecentInfo
2259  * @length: return location for the number of groups returned, or %NULL
2260  *
2261  * Returns all groups registered for the recently used item @info.  The
2262  * array of returned group names will be %NULL terminated, so length might
2263  * optionally be %NULL.
2264  *
2265  * Return value: a newly allocated %NULL terminated array of strings.  Use
2266  *   g_strfreev() to free it.
2267  *
2268  * Since: 2.10
2269  */
2270 gchar **
2271 gtk_recent_info_get_groups (GtkRecentInfo *info,
2272                             gsize         *length)
2273 {
2274   GSList *l;
2275   gchar **retval;
2276   gsize n_groups, i;
2277   
2278   g_return_val_if_fail (info != NULL, NULL);
2279   
2280   if (!info->groups)
2281     {
2282       if (length)
2283         *length = 0;
2284       
2285       return NULL;
2286     }
2287   
2288   n_groups = g_slist_length (info->groups);
2289   
2290   retval = g_new0 (gchar *, n_groups + 1);
2291   
2292   for (l = info->groups, i = 0;
2293        l != NULL;
2294        l = l->next)
2295     {
2296       gchar *group_name = (gchar *) l->data;
2297       
2298       g_assert (group_name != NULL);
2299       
2300       retval[i++] = g_strdup (group_name);
2301     }
2302   retval[i] = NULL;
2303   
2304   if (length)
2305     *length = i;
2306   
2307   return retval;
2308 }
2309
2310 /**
2311  * gtk_recent_info_has_group:
2312  * @info: a #GtkRecentInfo
2313  * @group_name: name of a group
2314  *
2315  * Checks whether @group_name appears inside the groups registered for the
2316  * recently used item @info.
2317  *
2318  * Return value: %TRUE if the group was found.
2319  *
2320  * Since: 2.10
2321  */
2322 gboolean
2323 gtk_recent_info_has_group (GtkRecentInfo *info,
2324                            const gchar   *group_name)
2325 {
2326   GSList *l;
2327   
2328   g_return_val_if_fail (info != NULL, FALSE);
2329   g_return_val_if_fail (group_name != NULL, FALSE);
2330
2331   if (!info->groups)
2332     return FALSE;
2333
2334   for (l = info->groups; l != NULL; l = l->next)
2335     {
2336       gchar *g = (gchar *) l->data;
2337
2338       if (strcmp (g, group_name) == 0)
2339         return TRUE;
2340     }
2341
2342   return FALSE;
2343 }
2344
2345 /*
2346  * _gtk_recent_manager_sync:
2347  * 
2348  * Private function for synchronising the recent manager singleton.
2349  */
2350 void
2351 _gtk_recent_manager_sync (void)
2352 {
2353   if (recent_manager_singleton)
2354     {
2355       /* force a dump of the contents of the recent manager singleton */
2356       recent_manager_singleton->priv->is_dirty = TRUE;
2357       gtk_recent_manager_real_changed (recent_manager_singleton);
2358     }
2359 }
2360
2361 #define __GTK_RECENT_MANAGER_C__
2362 #include "gtkaliasdef.c"