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