]> Pileus Git - ~andy/gtk/blob - gtk/gtkrecentmanager.c
Split off gtkprivate.h
[~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 /**
22  * SECTION:gtkrecentmanager
23  * @Title: GtkRecentManager
24  * @short_description: Managing recently used files
25  * @See_Also: #GBookmarkFile, #GtkSettings, #GtkRecentChooser
26  *
27  * #GtkRecentManager provides a facility for adding, removing and
28  * looking up recently used files. Each recently used file is
29  * identified by its URI, and has meta-data associated to it, like
30  * the names and command lines of the applications that have
31  * registered it, the number of time each application has registered
32  * the same file, the mime type of the file and whether the file
33  * should be displayed only by the applications that have
34  * registered it.
35  *
36  * <note><para>The recently used files list is per user.</para></note>
37  *
38  * The #GtkRecentManager acts like a database of all the recently
39  * used files. You can create new #GtkRecentManager objects, but
40  * it is more efficient to use the default manager created by GTK+.
41  *
42  * Adding a new recently used file is as simple as:
43  *
44  * |[
45  * GtkRecentManager *manager;
46  *
47  * manager = gtk_recent_manager_get_default ();
48  * gtk_recent_manager_add_item (manager, file_uri);
49  * ]|
50  *
51  * The #GtkRecentManager will try to gather all the needed information
52  * from the file itself through GIO.
53  *
54  * Looking up the meta-data associated with a recently used file
55  * given its URI requires calling gtk_recent_manager_lookup_item():
56  *
57  * |[
58  * GtkRecentManager *manager;
59  * GtkRecentInfo *info;
60  * GError *error = NULL;
61  *
62  * manager = gtk_recent_manager_get_default ();
63  * info = gtk_recent_manager_lookup_item (manager, file_uri, &amp;error);
64  * if (error)
65  *   {
66  *     g_warning ("Could not find the file: &percnt;s", error-&gt;message);
67  *     g_error_free (error);
68  *   }
69  * else
70  *  {
71  *    /&ast; Use the info object &ast;/
72  *    gtk_recent_info_unref (info);
73  *  }
74  * ]|
75  *
76  * In order to retrieve the list of recently used files, you can use
77  * gtk_recent_manager_get_items(), which returns a list of #GtkRecentInfo
78  * structures.
79  *
80  * A #GtkRecentManager is the model used to populate the contents of
81  * one, or more #GtkRecentChooser implementations.
82  *
83  * <note><para>The maximum age of the recently used files list is
84  * controllable through the #GtkSettings:gtk-recent-files-max-age
85  * property.</para></note>
86  *
87  * Recently used files are supported since GTK+ 2.10.
88  */
89
90 #include "config.h"
91
92 #include <sys/types.h>
93 #include <sys/stat.h>
94 #ifdef HAVE_UNISTD_H
95 #include <unistd.h>
96 #endif
97 #include <errno.h>
98 #include <string.h>
99 #include <stdlib.h>
100 #include <glib.h>
101 #include <glib/gstdio.h>
102 #include <gio/gio.h>
103
104 #include "gtkrecentmanager.h"
105 #include "gtkintl.h"
106 #include "gtksettings.h"
107 #include "gtkstock.h"
108 #include "gtkicontheme.h"
109 #include "gtktypebuiltins.h"
110 #include "gtkprivate.h"
111 #include "gtkmarshalers.h"
112
113 /* the file where we store the recently used items */
114 #define GTK_RECENTLY_USED_FILE  ".recently-used.xbel"
115
116 /* return all items by default */
117 #define DEFAULT_LIMIT   -1
118
119 /* keep in sync with xdgmime */
120 #define GTK_RECENT_DEFAULT_MIME "application/octet-stream"
121
122 typedef struct
123 {
124   gchar *name;
125   gchar *exec;
126   
127   guint count;
128   
129   time_t stamp;
130 } RecentAppInfo;
131
132 /**
133  * GtkRecentInfo:
134  *
135  * #GtkRecentInfo is an opaque data structure
136  * whose members can only be accessed using the provided API.
137  *
138  * #GtkRecentInfo constains all the meta-data
139  * associated with an entry in the recently used files list.
140  *
141  * Since: 2.10
142  */
143 struct _GtkRecentInfo
144 {
145   gchar *uri;
146   
147   gchar *display_name;
148   gchar *description;
149   
150   time_t added;
151   time_t modified;
152   time_t visited;
153   
154   gchar *mime_type;
155   
156   GSList *applications;
157   GHashTable *apps_lookup;
158   
159   GSList *groups;
160   
161   gboolean is_private;
162   
163   GdkPixbuf *icon;
164   
165   gint ref_count;
166 };
167
168 struct _GtkRecentManagerPrivate
169 {
170   gchar *filename;
171
172   guint is_dirty : 1;
173   
174   gint size;
175
176   GBookmarkFile *recent_items;
177
178   GFileMonitor *monitor;
179 };
180
181 enum
182 {
183   PROP_0,
184
185   PROP_FILENAME,  
186   PROP_LIMIT,
187   PROP_SIZE
188 };
189
190 static void     gtk_recent_manager_dispose             (GObject           *object);
191 static void     gtk_recent_manager_finalize            (GObject           *object);
192 static void     gtk_recent_manager_set_property        (GObject           *object,
193                                                         guint              prop_id,
194                                                         const GValue      *value,
195                                                         GParamSpec        *pspec);
196 static void     gtk_recent_manager_get_property        (GObject           *object,
197                                                         guint              prop_id,
198                                                         GValue            *value,
199                                                         GParamSpec        *pspec);
200 static void     gtk_recent_manager_add_item_query_info (GObject           *source_object,
201                                                         GAsyncResult      *res,
202                                                         gpointer           user_data);
203 static void     gtk_recent_manager_monitor_changed     (GFileMonitor      *monitor,
204                                                         GFile             *file,
205                                                         GFile             *other_file,
206                                                         GFileMonitorEvent  event_type,
207                                                         gpointer           user_data);
208 static void     gtk_recent_manager_changed             (GtkRecentManager  *manager);
209 static void     gtk_recent_manager_real_changed        (GtkRecentManager  *manager);
210 static void     gtk_recent_manager_set_filename        (GtkRecentManager  *manager,
211                                                         const gchar       *filename);
212 static void     gtk_recent_manager_clamp_to_age        (GtkRecentManager  *manager,
213                                                         gint               age);
214
215
216 static void build_recent_items_list (GtkRecentManager  *manager);
217 static void purge_recent_items_list (GtkRecentManager  *manager,
218                                      GError           **error);
219
220 static RecentAppInfo *recent_app_info_new  (const gchar   *app_name);
221 static void           recent_app_info_free (RecentAppInfo *app_info);
222
223 static GtkRecentInfo *gtk_recent_info_new  (const gchar   *uri);
224 static void           gtk_recent_info_free (GtkRecentInfo *recent_info);
225
226 static guint signal_changed = 0;
227
228 static GtkRecentManager *recent_manager_singleton = NULL;
229
230 G_DEFINE_TYPE (GtkRecentManager, gtk_recent_manager, G_TYPE_OBJECT)
231
232 static void
233 filename_warning (const gchar *format, 
234                   const gchar *filename, 
235                   const gchar *message)
236 {
237   gchar *utf8 = g_filename_to_utf8 (filename, -1, NULL, NULL, NULL);
238   g_warning (format, utf8 ? utf8 : "(invalid filename)", message);
239   g_free (utf8);
240 }
241
242 /* Test of haystack has the needle prefix, comparing case
243  * insensitive. haystack may be UTF-8, but needle must
244  * contain only lowercase ascii. */
245 static gboolean
246 has_case_prefix (const gchar *haystack, 
247                  const gchar *needle)
248 {
249   const gchar *h, *n;
250
251   /* Eat one character at a time. */
252   h = haystack;
253   n = needle;
254
255   while (*n && *h && *n == g_ascii_tolower (*h))
256     {
257       n++;
258       h++;
259     }
260
261   return *n == '\0';
262 }
263
264 GQuark
265 gtk_recent_manager_error_quark (void)
266 {
267   return g_quark_from_static_string ("gtk-recent-manager-error-quark");
268 }
269
270 static void
271 gtk_recent_manager_class_init (GtkRecentManagerClass *klass)
272 {
273   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
274   
275   gobject_class->set_property = gtk_recent_manager_set_property;
276   gobject_class->get_property = gtk_recent_manager_get_property;
277   gobject_class->dispose = gtk_recent_manager_dispose;
278   gobject_class->finalize = gtk_recent_manager_finalize;
279   
280   /**
281    * GtkRecentManager:filename
282    *
283    * The full path to the file to be used to store and read the recently
284    * used resources list
285    *
286    * Since: 2.10
287    */
288   g_object_class_install_property (gobject_class,
289                                    PROP_FILENAME,
290                                    g_param_spec_string ("filename",
291                                                         P_("Filename"),
292                                                         P_("The full path to the file to be used to store and read the list"),
293                                                         NULL,
294                                                         (G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE)));
295
296   /**
297    * GtkRecentManager:size
298    * 
299    * The size of the recently used resources list.
300    *
301    * Since: 2.10
302    */
303   g_object_class_install_property (gobject_class,
304                                    PROP_SIZE,
305                                    g_param_spec_int ("size",
306                                                      P_("Size"),
307                                                      P_("The size of the recently used resources list"),
308                                                      -1,
309                                                      G_MAXINT,
310                                                      0,
311                                                      G_PARAM_READABLE));
312   
313   /**
314    * GtkRecentManager::changed
315    * @recent_manager: the recent manager
316    *
317    * Emitted when the current recently used resources manager changes its
318    * contents, either by calling gtk_recent_manager_add_item() or by another
319    * application.
320    *
321    * Since: 2.10
322    */
323   signal_changed =
324     g_signal_new (I_("changed"),
325                   G_TYPE_FROM_CLASS (klass),
326                   G_SIGNAL_RUN_FIRST,
327                   G_STRUCT_OFFSET (GtkRecentManagerClass, changed),
328                   NULL, NULL,
329                   g_cclosure_marshal_VOID__VOID,
330                   G_TYPE_NONE, 0);
331   
332   klass->changed = gtk_recent_manager_real_changed;
333   
334   g_type_class_add_private (klass, sizeof (GtkRecentManagerPrivate));
335 }
336
337 static void
338 gtk_recent_manager_init (GtkRecentManager *manager)
339 {
340   GtkRecentManagerPrivate *priv;
341
342   manager->priv = G_TYPE_INSTANCE_GET_PRIVATE (manager,
343                                                GTK_TYPE_RECENT_MANAGER,
344                                                GtkRecentManagerPrivate);
345   priv = manager->priv;
346
347   priv->size = 0;
348   priv->filename = NULL;
349 }
350
351 static void
352 gtk_recent_manager_set_property (GObject               *object,
353                                  guint                  prop_id,
354                                  const GValue          *value,
355                                  GParamSpec            *pspec)
356 {
357   GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
358  
359   switch (prop_id)
360     {
361     case PROP_FILENAME:
362       gtk_recent_manager_set_filename (recent_manager, g_value_get_string (value));
363       break;
364     default:
365       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
366       break;
367     }
368 }
369
370 static void
371 gtk_recent_manager_get_property (GObject               *object,
372                                  guint                  prop_id,
373                                  GValue                *value,
374                                  GParamSpec            *pspec)
375 {
376   GtkRecentManager *recent_manager = GTK_RECENT_MANAGER (object);
377   
378   switch (prop_id)
379     {
380     case PROP_FILENAME:
381       g_value_set_string (value, recent_manager->priv->filename);
382       break;
383     case PROP_SIZE:
384       g_value_set_int (value, recent_manager->priv->size);
385       break;
386     default:
387       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
388       break;
389     }
390
391
392 static void
393 gtk_recent_manager_dispose (GObject *object)
394 {
395   GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
396   GtkRecentManagerPrivate *priv = manager->priv;
397
398   if (priv->monitor)
399     {
400       g_signal_handlers_disconnect_by_func (priv->monitor,
401                                             G_CALLBACK (gtk_recent_manager_monitor_changed),
402                                             manager);
403       g_object_unref (priv->monitor);
404       priv->monitor = NULL;
405     }
406
407   G_OBJECT_CLASS (gtk_recent_manager_parent_class)->dispose (object);
408 }
409
410 static void
411 gtk_recent_manager_finalize (GObject *object)
412 {
413   GtkRecentManager *manager = GTK_RECENT_MANAGER (object);
414   GtkRecentManagerPrivate *priv = manager->priv;
415
416   g_free (priv->filename);
417   
418   if (priv->recent_items)
419     g_bookmark_file_free (priv->recent_items);
420
421   G_OBJECT_CLASS (gtk_recent_manager_parent_class)->finalize (object);
422 }
423
424 static void
425 gtk_recent_manager_real_changed (GtkRecentManager *manager)
426 {
427   GtkRecentManagerPrivate *priv = manager->priv;
428
429   g_object_freeze_notify (G_OBJECT (manager));
430
431   if (priv->is_dirty)
432     {
433       GError *write_error;
434       
435       /* we are marked as dirty, so we dump the content of our
436        * recently used items list
437        */
438       g_assert (priv->filename != NULL);
439
440       if (!priv->recent_items)
441         {
442           /* if no container object has been defined, we create a new
443            * empty container, and dump it
444            */
445           priv->recent_items = g_bookmark_file_new ();
446           priv->size = 0;
447         }
448       else
449         {
450           GtkSettings *settings = gtk_settings_get_default ();
451           gint age = 30;
452
453           g_object_get (G_OBJECT (settings), "gtk-recent-files-max-age", &age, NULL);
454           if (age > 0)
455             gtk_recent_manager_clamp_to_age (manager, age);
456           else if (age == 0)
457             {
458               g_bookmark_file_free (priv->recent_items);
459               priv->recent_items = NULL;
460
461               priv->recent_items = g_bookmark_file_new ();
462             }
463         }
464
465       write_error = NULL;
466       g_bookmark_file_to_file (priv->recent_items, priv->filename, &write_error);
467       if (write_error)
468         {
469           filename_warning ("Attempting to store changes into `%s', "
470                             "but failed: %s",
471                             priv->filename,
472                             write_error->message);
473           g_error_free (write_error);
474         }
475
476       if (g_chmod (priv->filename, 0600) < 0)
477         {
478           filename_warning ("Attempting to set the permissions of `%s', "
479                             "but failed: %s",
480                             priv->filename,
481                             g_strerror (errno));
482         }
483
484       /* mark us as clean */
485       priv->is_dirty = FALSE;
486     }
487   else
488     {
489       /* we are not marked as dirty, so we have been called
490        * because the recently used resources file has been
491        * changed (and not from us).
492        */
493       build_recent_items_list (manager);
494     }
495
496   g_object_thaw_notify (G_OBJECT (manager));
497 }
498
499 static void
500 gtk_recent_manager_monitor_changed (GFileMonitor      *monitor,
501                                     GFile             *file,
502                                     GFile             *other_file,
503                                     GFileMonitorEvent  event_type,
504                                     gpointer           user_data)
505 {
506   GtkRecentManager *manager = user_data;
507
508   switch (event_type)
509     {
510     case G_FILE_MONITOR_EVENT_CHANGED:
511     case G_FILE_MONITOR_EVENT_CREATED:
512       gtk_recent_manager_changed (manager);
513       break;
514
515     case G_FILE_MONITOR_EVENT_DELETED:
516       break;
517
518     default:
519       break;
520     }
521 }
522
523 static void
524 gtk_recent_manager_set_filename (GtkRecentManager *manager,
525                                  const gchar      *filename)
526 {
527   GtkRecentManagerPrivate *priv;
528   GFile *file;
529   GError *error;
530   
531   g_assert (GTK_IS_RECENT_MANAGER (manager));
532
533   priv = manager->priv;
534
535   /* if a filename is already set and filename is not NULL, then copy
536    * it and reset the monitor; otherwise, if it's NULL we're being
537    * called from the finalization sequence, so we simply disconnect the
538    * monitoring and return.
539    *
540    * if no filename is set and filename is NULL, use the default.
541    */
542   if (priv->filename)
543     {
544       g_free (priv->filename);
545
546       if (priv->monitor)
547         {
548           g_signal_handlers_disconnect_by_func (priv->monitor,
549                                                 G_CALLBACK (gtk_recent_manager_monitor_changed),
550                                                 manager);
551           g_object_unref (priv->monitor);
552           priv->monitor = NULL;
553         }
554
555       if (!filename || *filename == '\0')
556         return;
557       else
558         priv->filename = g_strdup (filename);
559     }
560   else
561     {
562       if (!filename || *filename == '\0')
563         priv->filename = g_build_filename (g_get_home_dir (),
564                                            GTK_RECENTLY_USED_FILE,
565                                            NULL);
566       else
567         priv->filename = g_strdup (filename);
568     }
569
570   g_assert (priv->filename != NULL);
571   file = g_file_new_for_path (priv->filename);
572
573   error = NULL;
574   priv->monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, &error);
575   if (error)
576     {
577       filename_warning ("Unable to monitor `%s': %s\n"
578                         "The GtkRecentManager will not update its contents "
579                         "if the file is changed from other instances",
580                         priv->filename,
581                         error->message);
582       g_error_free (error);
583     }
584   else
585     g_signal_connect (priv->monitor, "changed",
586                       G_CALLBACK (gtk_recent_manager_monitor_changed),
587                       manager);
588
589   g_object_unref (file);
590
591   priv->is_dirty = FALSE;
592   build_recent_items_list (manager);
593 }
594
595 /* reads the recently used resources file and builds the items list.
596  * we keep the items list inside the parser object, and build the
597  * RecentInfo object only on user's demand to avoid useless replication.
598  * this function resets the dirty bit of the manager.
599  */
600 static void
601 build_recent_items_list (GtkRecentManager *manager)
602 {
603   GtkRecentManagerPrivate *priv = manager->priv;
604   GError *read_error;
605   gint size;
606
607   g_assert (priv->filename != NULL);
608   
609   if (!priv->recent_items)
610     {
611       priv->recent_items = g_bookmark_file_new ();
612       priv->size = 0;
613     }
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   g_bookmark_file_load_from_file (priv->recent_items, priv->filename, &read_error);
620   if (read_error)
621     {
622       /* if the file does not exist we just wait for the first write
623        * operation on this recent manager instance, to avoid creating
624        * empty files and leading to spurious file system events (Sabayon
625        * will not be happy about those)
626        */
627       if (read_error->domain == G_FILE_ERROR &&
628           read_error->code != G_FILE_ERROR_NOENT)
629         filename_warning ("Attempting to read the recently used resources "
630                           "file at `%s', but the parser failed: %s.",
631                           priv->filename,
632                           read_error->message);
633
634       g_bookmark_file_free (priv->recent_items);
635       priv->recent_items = NULL;
636
637       g_error_free (read_error);
638     }
639   else
640     {
641       size = g_bookmark_file_get_size (priv->recent_items);
642       if (priv->size != size)
643         {
644           priv->size = size;
645
646           g_object_notify (G_OBJECT (manager), "size");
647         }
648     }
649
650   priv->is_dirty = FALSE;
651 }
652
653
654 /********************
655  * GtkRecentManager *
656  ********************/
657
658
659 /**
660  * gtk_recent_manager_new:
661  * 
662  * Creates a new recent manager object.  Recent manager objects are used to
663  * handle the list of recently used resources.  A #GtkRecentManager object
664  * monitors the recently used resources list, and emits the "changed" signal
665  * each time something inside the list changes.
666  *
667  * #GtkRecentManager objects are expensive: be sure to create them only when
668  * needed. You should use gtk_recent_manager_get_default() instead.
669  *
670  * Return value: A newly created #GtkRecentManager object.
671  *
672  * Since: 2.10
673  */
674 GtkRecentManager *
675 gtk_recent_manager_new (void)
676 {
677   return g_object_new (GTK_TYPE_RECENT_MANAGER, NULL);
678 }
679
680 /**
681  * gtk_recent_manager_get_default:
682  *
683  * Gets a unique instance of #GtkRecentManager, that you can share
684  * in your application without caring about memory management. The
685  * returned instance will be freed when you application terminates.
686  *
687  * Return value: (transfer none): A unique #GtkRecentManager. Do not ref or unref it.
688  *
689  * Since: 2.10
690  */
691 GtkRecentManager *
692 gtk_recent_manager_get_default (void)
693 {
694   if (G_UNLIKELY (!recent_manager_singleton))
695     recent_manager_singleton = gtk_recent_manager_new ();
696
697   return recent_manager_singleton;
698 }
699
700
701 static void
702 gtk_recent_manager_add_item_query_info (GObject      *source_object,
703                                         GAsyncResult *res,
704                                         gpointer      user_data)
705 {
706   GFile *file = G_FILE (source_object);
707   GtkRecentManager *manager = user_data;
708   GtkRecentData recent_data;
709   GFileInfo *file_info;
710   gchar *uri;
711   GError *error;
712
713   uri = g_file_get_uri (file);
714
715   error = NULL;
716   file_info = g_file_query_info_finish (file, res, &error);
717   if (error)
718     {
719       g_warning ("Unable to retrieve the file info for `%s': %s",
720                  uri,
721                  error->message);
722       g_error_free (error);
723       goto out;
724     }
725
726   recent_data.display_name = NULL;
727   recent_data.description = NULL;
728
729   if (file_info)
730     {
731       gchar *content_type;
732
733       content_type = g_file_info_get_attribute_as_string (file_info, G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE);
734
735       if (G_LIKELY (content_type))
736         recent_data.mime_type = g_content_type_get_mime_type (content_type);
737       else
738         recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
739
740       g_free (content_type);
741       g_object_unref (file_info);
742     }
743   else
744     recent_data.mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
745
746   recent_data.app_name = g_strdup (g_get_application_name ());
747   recent_data.app_exec = g_strjoin (" ", g_get_prgname (), "%u", NULL);
748   recent_data.groups = NULL;
749   recent_data.is_private = FALSE;
750
751   /* Ignore return value, this can't fail anyway since all required
752    * fields are set */
753   gtk_recent_manager_add_full (manager, uri, &recent_data);
754
755   manager->priv->is_dirty = TRUE;
756   gtk_recent_manager_changed (manager);
757
758   g_free (recent_data.mime_type);
759   g_free (recent_data.app_name);
760   g_free (recent_data.app_exec);
761
762 out:
763   g_object_unref (manager);
764   g_free (uri);
765 }
766
767 /**
768  * gtk_recent_manager_add_item:
769  * @manager: a #GtkRecentManager
770  * @uri: a valid URI
771  *
772  * Adds a new resource, pointed by @uri, into the recently used
773  * resources list.
774  *
775  * This function automatically retrieves some of the needed
776  * metadata and setting other metadata to common default values; it
777  * then feeds the data to gtk_recent_manager_add_full().
778  * 
779  * See gtk_recent_manager_add_full() if you want to explicitly
780  * define the metadata for the resource pointed by @uri.
781  *
782  * Return value: %TRUE if the new item was successfully added
783  *   to the recently used resources list
784  *
785  * Since: 2.10
786  */
787 gboolean
788 gtk_recent_manager_add_item (GtkRecentManager  *manager,
789                              const gchar       *uri)
790 {
791   GFile* file;
792   
793   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
794   g_return_val_if_fail (uri != NULL, FALSE);
795
796   file = g_file_new_for_uri (uri);
797
798   g_file_query_info_async (file,
799                            G_FILE_ATTRIBUTE_STANDARD_FAST_CONTENT_TYPE,
800                            G_PRIORITY_DEFAULT,
801                            G_FILE_QUERY_INFO_NONE,
802                            NULL,
803                            gtk_recent_manager_add_item_query_info,
804                            g_object_ref (manager));
805
806   g_object_unref (file);
807
808   return TRUE;
809 }
810
811 /**
812  * gtk_recent_manager_add_full:
813  * @manager: a #GtkRecentManager
814  * @uri: a valid URI
815  * @recent_data: metadata of the resource
816  *
817  * Adds a new resource, pointed by @uri, into the recently used
818  * resources list, using the metadata specified inside the #GtkRecentData
819  * structure passed in @recent_data.
820  *
821  * The passed URI will be used to identify this resource inside the
822  * list.
823  *
824  * In order to register the new recently used resource, metadata about
825  * the resource must be passed as well as the URI; the metadata is
826  * stored in a #GtkRecentData structure, which must contain the MIME
827  * type of the resource pointed by the URI; the name of the application
828  * that is registering the item, and a command line to be used when
829  * launching the item.
830  *
831  * Optionally, a #GtkRecentData structure might contain a UTF-8 string
832  * to be used when viewing the item instead of the last component of the
833  * URI; a short description of the item; whether the item should be
834  * considered private - that is, should be displayed only by the
835  * applications that have registered it.
836  *
837  * Return value: %TRUE if the new item was successfully added to the
838  * recently used resources list, %FALSE otherwise.
839  *
840  * Since: 2.10
841  */
842 gboolean
843 gtk_recent_manager_add_full (GtkRecentManager     *manager,
844                              const gchar          *uri,
845                              const GtkRecentData  *data)
846 {
847   GtkRecentManagerPrivate *priv;
848   
849   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
850   g_return_val_if_fail (uri != NULL, FALSE);
851   g_return_val_if_fail (data != NULL, FALSE);
852
853   /* sanity checks */
854   if ((data->display_name) &&
855       (!g_utf8_validate (data->display_name, -1, NULL)))
856     {
857       g_warning ("Attempting to add `%s' to the list of recently used "
858                  "resources, but the display name is not a valid UTF-8 "
859                  "encoded string",
860                  uri);
861       return FALSE;
862     }
863   
864   if ((data->description) &&
865       (!g_utf8_validate (data->description, -1, NULL)))
866     {
867       g_warning ("Attempting to add `%s' to the list of recently used "
868                  "resources, but the description is not a valid UTF-8 "
869                  "encoded string",
870                  uri);
871       return FALSE;
872     }
873
874  
875   if (!data->mime_type)
876     {
877       g_warning ("Attempting to add `%s' to the list of recently used "
878                  "resources, but not MIME type was defined",
879                  uri);
880       return FALSE;
881     }
882   
883   if (!data->app_name)
884     {
885       g_warning ("Attempting to add `%s' to the list of recently used "
886                  "resources, but no name of the application that is "
887                  "registering it was defined",
888                  uri);
889       return FALSE;
890     }
891   
892   if (!data->app_exec)
893     {
894       g_warning ("Attempting to add `%s' to the list of recently used "
895                  "resources, but no command line for the application "
896                  "that is registering it was defined",
897                  uri);
898       return FALSE;
899     }
900   
901   priv = manager->priv;
902
903   if (!priv->recent_items)
904     {
905       priv->recent_items = g_bookmark_file_new ();
906       priv->size = 0;
907     }
908
909   if (data->display_name)  
910     g_bookmark_file_set_title (priv->recent_items, uri, data->display_name);
911   
912   if (data->description)
913     g_bookmark_file_set_description (priv->recent_items, uri, data->description);
914
915   g_bookmark_file_set_mime_type (priv->recent_items, uri, data->mime_type);
916   
917   if (data->groups && data->groups[0] != '\0')
918     {
919       gint j;
920       
921       for (j = 0; (data->groups)[j] != NULL; j++)
922         g_bookmark_file_add_group (priv->recent_items, uri, (data->groups)[j]);
923     }
924   
925   /* register the application; this will take care of updating the
926    * registration count and time in case the application has
927    * already registered the same document inside the list
928    */
929   g_bookmark_file_add_application (priv->recent_items, uri,
930                                    data->app_name,
931                                    data->app_exec);
932   
933   g_bookmark_file_set_is_private (priv->recent_items, uri,
934                                   data->is_private);
935   
936   /* mark us as dirty, so that when emitting the "changed" signal we
937    * will dump our changes
938    */
939   priv->is_dirty = TRUE;
940   
941   gtk_recent_manager_changed (manager);
942   
943   return TRUE;
944 }
945
946 /**
947  * gtk_recent_manager_remove_item:
948  * @manager: a #GtkRecentManager
949  * @uri: the URI of the item you wish to remove
950  * @error: (allow-none): return location for a #GError, or %NULL
951  *
952  * Removes a resource pointed by @uri from the recently used resources
953  * list handled by a recent manager.
954  *
955  * Return value: %TRUE if the item pointed by @uri has been successfully
956  *   removed by the recently used resources list, and %FALSE otherwise.
957  *
958  * Since: 2.10
959  */
960 gboolean
961 gtk_recent_manager_remove_item (GtkRecentManager  *manager,
962                                 const gchar       *uri,
963                                 GError           **error)
964 {
965   GtkRecentManagerPrivate *priv;
966   GError *remove_error = NULL;
967
968   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
969   g_return_val_if_fail (uri != NULL, FALSE);
970   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
971   
972   priv = manager->priv;
973   
974   if (!priv->recent_items)
975     {
976       priv->recent_items = g_bookmark_file_new ();
977       priv->size = 0;
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   g_bookmark_file_remove_item (priv->recent_items, uri, &remove_error);
988   if (remove_error)
989     {
990       g_error_free (remove_error);
991
992       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
993                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
994                    _("Unable to find an item with URI '%s'"),
995                    uri);
996       
997       return FALSE;
998     }
999
1000   priv->is_dirty = TRUE;
1001
1002   gtk_recent_manager_changed (manager);
1003   
1004   return TRUE;
1005 }
1006
1007 /**
1008  * gtk_recent_manager_has_item:
1009  * @manager: a #GtkRecentManager
1010  * @uri: a URI
1011  *
1012  * Checks whether there is a recently used resource registered
1013  * with @uri inside the recent manager.
1014  *
1015  * Return value: %TRUE if the resource was found, %FALSE otherwise.
1016  *
1017  * Since: 2.10
1018  */
1019 gboolean
1020 gtk_recent_manager_has_item (GtkRecentManager *manager,
1021                              const gchar      *uri)
1022 {
1023   GtkRecentManagerPrivate *priv;
1024
1025   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), FALSE);
1026   g_return_val_if_fail (uri != NULL, FALSE);
1027
1028   priv = manager->priv;
1029   g_return_val_if_fail (priv->recent_items != NULL, FALSE);
1030
1031   return g_bookmark_file_has_item (priv->recent_items, uri);
1032 }
1033
1034 static void
1035 build_recent_info (GBookmarkFile  *bookmarks,
1036                    GtkRecentInfo  *info)
1037 {
1038   gchar **apps, **groups;
1039   gsize apps_len, groups_len, i;
1040
1041   g_assert (bookmarks != NULL);
1042   g_assert (info != NULL);
1043   
1044   info->display_name = g_bookmark_file_get_title (bookmarks, info->uri, NULL);
1045   info->description = g_bookmark_file_get_description (bookmarks, info->uri, NULL);
1046   info->mime_type = g_bookmark_file_get_mime_type (bookmarks, info->uri, NULL);
1047     
1048   info->is_private = g_bookmark_file_get_is_private (bookmarks, info->uri, NULL);
1049   
1050   info->added = g_bookmark_file_get_added (bookmarks, info->uri, NULL);
1051   info->modified = g_bookmark_file_get_modified (bookmarks, info->uri, NULL);
1052   info->visited = g_bookmark_file_get_visited (bookmarks, info->uri, NULL);
1053   
1054   groups = g_bookmark_file_get_groups (bookmarks, info->uri, &groups_len, NULL);
1055   for (i = 0; i < groups_len; i++)
1056     {
1057       gchar *group_name = g_strdup (groups[i]);
1058       
1059       info->groups = g_slist_append (info->groups, group_name);
1060     }
1061
1062   g_strfreev (groups);
1063   
1064   apps = g_bookmark_file_get_applications (bookmarks, info->uri, &apps_len, NULL);
1065   for (i = 0; i < apps_len; i++)
1066     {
1067       gchar *app_name, *app_exec;
1068       guint count;
1069       time_t stamp;
1070       RecentAppInfo *app_info;
1071       gboolean res;
1072       
1073       app_name = apps[i];
1074       
1075       res = g_bookmark_file_get_app_info (bookmarks, info->uri, app_name,
1076                                           &app_exec,
1077                                           &count,
1078                                           &stamp,
1079                                           NULL);
1080       if (!res)
1081         continue;
1082       
1083       app_info = recent_app_info_new (app_name);
1084       app_info->exec = app_exec;
1085       app_info->count = count;
1086       app_info->stamp = stamp;
1087       
1088       info->applications = g_slist_prepend (info->applications, app_info);
1089       g_hash_table_replace (info->apps_lookup, app_info->name, app_info);
1090     }
1091   
1092   g_strfreev (apps);
1093 }
1094
1095 /**
1096  * gtk_recent_manager_lookup_item:
1097  * @manager: a #GtkRecentManager
1098  * @uri: a URI
1099  * @error: (allow-none): a return location for a #GError, or %NULL
1100  *
1101  * Searches for a URI inside the recently used resources list, and
1102  * returns a structure containing informations about the resource
1103  * like its MIME type, or its display name.
1104  *
1105  * Return value: a #GtkRecentInfo structure containing information
1106  *   about the resource pointed by @uri, or %NULL if the URI was
1107  *   not registered in the recently used resources list.  Free with
1108  *   gtk_recent_info_unref().
1109  *
1110  * Since: 2.10
1111  */
1112 GtkRecentInfo *
1113 gtk_recent_manager_lookup_item (GtkRecentManager  *manager,
1114                                 const gchar       *uri,
1115                                 GError           **error)
1116 {
1117   GtkRecentManagerPrivate *priv;
1118   GtkRecentInfo *info = NULL;
1119   
1120   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1121   g_return_val_if_fail (uri != NULL, NULL);
1122   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
1123   
1124   priv = manager->priv;
1125   if (!priv->recent_items)
1126     {
1127       priv->recent_items = g_bookmark_file_new ();
1128       priv->size = 0;
1129
1130       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1131                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1132                    _("Unable to find an item with URI '%s'"),
1133                    uri);
1134
1135       return NULL;
1136     }
1137   
1138   if (!g_bookmark_file_has_item (priv->recent_items, uri))
1139     {
1140       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1141                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1142                    _("Unable to find an item with URI '%s'"),
1143                    uri);
1144       return NULL;
1145     }
1146   
1147   info = gtk_recent_info_new (uri);
1148   g_return_val_if_fail (info != NULL, NULL);
1149   
1150   /* fill the RecentInfo structure with the data retrieved by our
1151    * parser object from the storage file 
1152    */
1153   build_recent_info (priv->recent_items, info);
1154
1155   return info;
1156 }
1157
1158 /**
1159  * gtk_recent_manager_move_item:
1160  * @manager: a #GtkRecentManager
1161  * @uri: the URI of a recently used resource
1162  * @new_uri: (allow-none): the new URI of the recently used resource, or %NULL to
1163  *    remove the item pointed by @uri in the list
1164  * @error: (allow-none): a return location for a #GError, or %NULL
1165  *
1166  * Changes the location of a recently used resource from @uri to @new_uri.
1167  * 
1168  * Please note that this function will not affect the resource pointed
1169  * by the URIs, but only the URI used in the recently used resources list.
1170  *
1171  * Return value: %TRUE on success.
1172  *
1173  * Since: 2.10
1174  */ 
1175 gboolean
1176 gtk_recent_manager_move_item (GtkRecentManager  *recent_manager,
1177                               const gchar       *uri,
1178                               const gchar       *new_uri,
1179                               GError           **error)
1180 {
1181   GtkRecentManagerPrivate *priv;
1182   GError *move_error;
1183   gboolean res;
1184   
1185   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (recent_manager), FALSE);
1186   g_return_val_if_fail (uri != NULL, FALSE);
1187   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
1188   
1189   priv = recent_manager->priv;
1190
1191   if (!priv->recent_items)
1192     {
1193       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1194                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1195                    _("Unable to find an item with URI '%s'"),
1196                    uri);
1197       return FALSE;
1198     }
1199
1200   if (!g_bookmark_file_has_item (priv->recent_items, uri))
1201     {
1202       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1203                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1204                    _("Unable to find an item with URI '%s'"),
1205                    uri);
1206       return FALSE;
1207     }
1208   
1209   move_error = NULL;
1210   res = g_bookmark_file_move_item (priv->recent_items,
1211                                    uri, new_uri,
1212                                    &move_error);
1213   if (move_error)
1214     {
1215       g_error_free (move_error);
1216
1217       g_set_error (error, GTK_RECENT_MANAGER_ERROR,
1218                    GTK_RECENT_MANAGER_ERROR_NOT_FOUND,
1219                    _("Unable to find an item with URI '%s'"),
1220                    uri);
1221       return FALSE;
1222     }
1223   
1224   priv->is_dirty = TRUE;
1225
1226   gtk_recent_manager_changed (recent_manager);
1227   
1228   return TRUE;
1229 }
1230
1231 /**
1232  * gtk_recent_manager_get_items:
1233  * @manager: a #GtkRecentManager
1234  *
1235  * Gets the list of recently used resources.
1236  *
1237  * Return value:  (element-type GtkRecentInfo) (transfer full): a list of
1238  *   newly allocated #GtkRecentInfo objects. Use
1239  *   gtk_recent_info_unref() on each item inside the list, and then
1240  *   free the list itself using g_list_free().
1241  *
1242  * Since: 2.10
1243  */
1244 GList *
1245 gtk_recent_manager_get_items (GtkRecentManager *manager)
1246 {
1247   GtkRecentManagerPrivate *priv;
1248   GList *retval = NULL;
1249   gchar **uris;
1250   gsize uris_len, i;
1251   
1252   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), NULL);
1253   
1254   priv = manager->priv;
1255   if (!priv->recent_items)
1256     return NULL;
1257
1258   uris = g_bookmark_file_get_uris (priv->recent_items, &uris_len);
1259   for (i = 0; i < uris_len; i++)
1260     {
1261       GtkRecentInfo *info;
1262       
1263       info = gtk_recent_info_new (uris[i]);
1264       build_recent_info (priv->recent_items, info);
1265       
1266       retval = g_list_prepend (retval, info);
1267     }
1268   
1269   g_strfreev (uris);
1270   
1271   return retval;
1272 }
1273
1274 static void
1275 purge_recent_items_list (GtkRecentManager  *manager,
1276                          GError           **error)
1277 {
1278   GtkRecentManagerPrivate *priv = manager->priv;
1279
1280   if (!priv->recent_items)
1281     return;
1282   
1283   g_bookmark_file_free (priv->recent_items);
1284   priv->recent_items = NULL;
1285       
1286   priv->recent_items = g_bookmark_file_new ();
1287   priv->size = 0;
1288   priv->is_dirty = TRUE;
1289       
1290   /* emit the changed signal, to ensure that the purge is written */
1291   gtk_recent_manager_changed (manager);
1292 }
1293
1294 /**
1295  * gtk_recent_manager_purge_items:
1296  * @manager: a #GtkRecentManager
1297  * @error: (allow-none): a return location for a #GError, or %NULL
1298  *
1299  * Purges every item from the recently used resources list.
1300  *
1301  * Return value: the number of items that have been removed from the
1302  *   recently used resources list.
1303  *
1304  * Since: 2.10
1305  */
1306 gint
1307 gtk_recent_manager_purge_items (GtkRecentManager  *manager,
1308                                 GError           **error)
1309 {
1310   GtkRecentManagerPrivate *priv;
1311   gint count, purged;
1312   
1313   g_return_val_if_fail (GTK_IS_RECENT_MANAGER (manager), -1);
1314
1315   priv = manager->priv;
1316   if (!priv->recent_items)
1317     return 0;
1318   
1319   count = g_bookmark_file_get_size (priv->recent_items);
1320   if (!count)
1321     return 0;
1322   
1323   purge_recent_items_list (manager, error);
1324   
1325   purged = count - g_bookmark_file_get_size (priv->recent_items);
1326
1327   return purged;
1328 }
1329
1330 static void
1331 gtk_recent_manager_changed (GtkRecentManager *recent_manager)
1332 {
1333   g_signal_emit (recent_manager, signal_changed, 0);
1334 }
1335
1336 static void
1337 gtk_recent_manager_clamp_to_age (GtkRecentManager *manager,
1338                                  gint              age)
1339 {
1340   GtkRecentManagerPrivate *priv = manager->priv;
1341   gchar **uris;
1342   gsize n_uris, i;
1343   time_t now;
1344
1345   if (G_UNLIKELY (!priv->recent_items))
1346     return;
1347
1348   now = time (NULL);
1349
1350   uris = g_bookmark_file_get_uris (priv->recent_items, &n_uris);
1351
1352   for (i = 0; i < n_uris; i++)
1353     {
1354       const gchar *uri = uris[i];
1355       time_t modified;
1356       gint item_age;
1357
1358       modified = g_bookmark_file_get_modified (priv->recent_items, uri, NULL);
1359       item_age = (gint) ((now - modified) / (60 * 60 * 24));
1360       if (item_age > age)
1361         g_bookmark_file_remove_item (priv->recent_items, uri, NULL);
1362     }
1363
1364   g_strfreev (uris);
1365 }
1366
1367 /*****************
1368  * GtkRecentInfo *
1369  *****************/
1370  
1371 G_DEFINE_BOXED_TYPE (GtkRecentInfo, gtk_recent_info,
1372                      gtk_recent_info_ref,
1373                      gtk_recent_info_unref)
1374
1375 static GtkRecentInfo *
1376 gtk_recent_info_new (const gchar *uri)
1377 {
1378   GtkRecentInfo *info;
1379
1380   g_assert (uri != NULL);
1381
1382   info = g_new0 (GtkRecentInfo, 1);
1383   info->uri = g_strdup (uri);
1384   
1385   info->applications = NULL;
1386   info->apps_lookup = g_hash_table_new (g_str_hash, g_str_equal);
1387   
1388   info->groups = NULL;
1389   
1390   info->ref_count = 1;
1391
1392   return info;
1393 }
1394
1395 static void
1396 gtk_recent_info_free (GtkRecentInfo *recent_info)
1397 {
1398   if (!recent_info)
1399     return;
1400
1401   g_free (recent_info->uri);
1402   g_free (recent_info->display_name);
1403   g_free (recent_info->description);
1404   g_free (recent_info->mime_type);
1405   
1406   if (recent_info->applications)
1407     {
1408       g_slist_foreach (recent_info->applications,
1409                        (GFunc) recent_app_info_free,
1410                        NULL);
1411       g_slist_free (recent_info->applications);
1412       
1413       recent_info->applications = NULL;
1414     }
1415   
1416   if (recent_info->apps_lookup)
1417     g_hash_table_destroy (recent_info->apps_lookup);
1418
1419   if (recent_info->groups)
1420     {
1421       g_slist_foreach (recent_info->groups,
1422                        (GFunc) g_free,
1423                        NULL);
1424       g_slist_free (recent_info->groups);
1425
1426       recent_info->groups = NULL;
1427     }
1428   
1429   if (recent_info->icon)
1430     g_object_unref (recent_info->icon);
1431
1432   g_free (recent_info);
1433 }
1434
1435 /**
1436  * gtk_recent_info_ref:
1437  * @info: a #GtkRecentInfo
1438  *
1439  * Increases the reference count of @recent_info by one.
1440  *
1441  * Return value: the recent info object with its reference count increased
1442  *   by one.
1443  *
1444  * Since: 2.10
1445  */
1446 GtkRecentInfo *
1447 gtk_recent_info_ref (GtkRecentInfo *info)
1448 {
1449   g_return_val_if_fail (info != NULL, NULL);
1450   g_return_val_if_fail (info->ref_count > 0, NULL);
1451   
1452   info->ref_count += 1;
1453     
1454   return info;
1455 }
1456
1457 /**
1458  * gtk_recent_info_unref:
1459  * @info: a #GtkRecentInfo
1460  *
1461  * Decreases the reference count of @info by one.  If the reference
1462  * count reaches zero, @info is deallocated, and the memory freed.
1463  *
1464  * Since: 2.10
1465  */
1466 void
1467 gtk_recent_info_unref (GtkRecentInfo *info)
1468 {
1469   g_return_if_fail (info != NULL);
1470   g_return_if_fail (info->ref_count > 0);
1471
1472   info->ref_count -= 1;
1473   
1474   if (info->ref_count == 0)
1475     gtk_recent_info_free (info);
1476 }
1477
1478 /**
1479  * gtk_recent_info_get_uri:
1480  * @info: a #GtkRecentInfo
1481  *
1482  * Gets the URI of the resource.
1483  *
1484  * Return value: the URI of the resource.  The returned string is
1485  *   owned by the recent manager, and should not be freed.
1486  *
1487  * Since: 2.10
1488  */
1489 G_CONST_RETURN gchar *
1490 gtk_recent_info_get_uri (GtkRecentInfo *info)
1491 {
1492   g_return_val_if_fail (info != NULL, NULL);
1493   
1494   return info->uri;
1495 }
1496
1497 /**
1498  * gtk_recent_info_get_display_name:
1499  * @info: a #GtkRecentInfo
1500  *
1501  * Gets the name of the resource.  If none has been defined, the basename
1502  * of the resource is obtained.
1503  *
1504  * Return value: the display name of the resource.  The returned string
1505  *   is owned by the recent manager, and should not be freed.
1506  *
1507  * Since: 2.10
1508  */
1509 G_CONST_RETURN gchar *
1510 gtk_recent_info_get_display_name (GtkRecentInfo *info)
1511 {
1512   g_return_val_if_fail (info != NULL, NULL);
1513   
1514   if (!info->display_name)
1515     info->display_name = gtk_recent_info_get_short_name (info);
1516   
1517   return info->display_name;
1518 }
1519
1520 /**
1521  * gtk_recent_info_get_description:
1522  * @info: a #GtkRecentInfo
1523  *
1524  * Gets the (short) description of the resource.
1525  *
1526  * Return value: the description of the resource.  The returned string
1527  *   is owned by the recent manager, and should not be freed.
1528  *
1529  * Since: 2.10
1530  **/
1531 G_CONST_RETURN gchar *
1532 gtk_recent_info_get_description (GtkRecentInfo *info)
1533 {
1534   g_return_val_if_fail (info != NULL, NULL);
1535   
1536   return info->description;
1537 }
1538
1539 /**
1540  * gtk_recent_info_get_mime_type:
1541  * @info: a #GtkRecentInfo
1542  *
1543  * Gets the MIME type of the resource.
1544  *
1545  * Return value: the MIME type of the resource.  The returned string
1546  *   is owned by the recent manager, and should not be freed.
1547  *
1548  * Since: 2.10
1549  */
1550 G_CONST_RETURN gchar *
1551 gtk_recent_info_get_mime_type (GtkRecentInfo *info)
1552 {
1553   g_return_val_if_fail (info != NULL, NULL);
1554   
1555   if (!info->mime_type)
1556     info->mime_type = g_strdup (GTK_RECENT_DEFAULT_MIME);
1557   
1558   return info->mime_type;
1559 }
1560
1561 /**
1562  * gtk_recent_info_get_added:
1563  * @info: a #GtkRecentInfo
1564  *
1565  * Gets the timestamp (seconds from system's Epoch) when the resource
1566  * was added to the recently used resources list.
1567  *
1568  * Return value: the number of seconds elapsed from system's Epoch when
1569  *   the resource was added to the list, or -1 on failure.
1570  *
1571  * Since: 2.10
1572  */
1573 time_t
1574 gtk_recent_info_get_added (GtkRecentInfo *info)
1575 {
1576   g_return_val_if_fail (info != NULL, (time_t) -1);
1577   
1578   return info->added;
1579 }
1580
1581 /**
1582  * gtk_recent_info_get_modified:
1583  * @info: a #GtkRecentInfo
1584  *
1585  * Gets the timestamp (seconds from system's Epoch) when the resource
1586  * was last modified.
1587  *
1588  * Return value: the number of seconds elapsed from system's Epoch when
1589  *   the resource was last modified, or -1 on failure.
1590  *
1591  * Since: 2.10
1592  */
1593 time_t
1594 gtk_recent_info_get_modified (GtkRecentInfo *info)
1595 {
1596   g_return_val_if_fail (info != NULL, (time_t) -1);
1597   
1598   return info->modified;
1599 }
1600
1601 /**
1602  * gtk_recent_info_get_visited:
1603  * @info: a #GtkRecentInfo
1604  *
1605  * Gets the timestamp (seconds from system's Epoch) when the resource
1606  * was last visited.
1607  *
1608  * Return value: the number of seconds elapsed from system's Epoch when
1609  *   the resource was last visited, or -1 on failure.
1610  *
1611  * Since: 2.10
1612  */
1613 time_t
1614 gtk_recent_info_get_visited (GtkRecentInfo *info)
1615 {
1616   g_return_val_if_fail (info != NULL, (time_t) -1);
1617   
1618   return info->visited;
1619 }
1620
1621 /**
1622  * gtk_recent_info_get_private_hint:
1623  * @info: a #GtkRecentInfo
1624  *
1625  * Gets the value of the "private" flag.  Resources in the recently used
1626  * list that have this flag set to %TRUE should only be displayed by the
1627  * applications that have registered them.
1628  *
1629  * Return value: %TRUE if the private flag was found, %FALSE otherwise.
1630  *
1631  * Since: 2.10
1632  */
1633 gboolean
1634 gtk_recent_info_get_private_hint (GtkRecentInfo *info)
1635 {
1636   g_return_val_if_fail (info != NULL, FALSE);
1637   
1638   return info->is_private;
1639 }
1640
1641
1642 static RecentAppInfo *
1643 recent_app_info_new (const gchar *app_name)
1644 {
1645   RecentAppInfo *app_info;
1646
1647   g_assert (app_name != NULL);
1648   
1649   app_info = g_slice_new0 (RecentAppInfo);
1650   app_info->name = g_strdup (app_name);
1651   app_info->exec = NULL;
1652   app_info->count = 1;
1653   app_info->stamp = 0; 
1654   
1655   return app_info;
1656 }
1657
1658 static void
1659 recent_app_info_free (RecentAppInfo *app_info)
1660 {
1661   if (!app_info)
1662     return;
1663   
1664   g_free (app_info->name);
1665   g_free (app_info->exec);
1666   
1667   g_slice_free (RecentAppInfo, app_info);
1668 }
1669
1670 /**
1671  * gtk_recent_info_get_application_info:
1672  * @info: a #GtkRecentInfo
1673  * @app_name: the name of the application that has registered this item
1674  * @app_exec: (transfer none) (out): return location for the string containing the command line
1675  * @count: (out): return location for the number of times this item was registered
1676  * @time_: (out): return location for the timestamp this item was last registered
1677  *    for this application
1678  *
1679  * Gets the data regarding the application that has registered the resource
1680  * pointed by @info.
1681  *
1682  * If the command line contains any escape characters defined inside the
1683  * storage specification, they will be expanded.
1684  *
1685  * Return value: %TRUE if an application with @app_name has registered this
1686  *   resource inside the recently used list, or %FALSE otherwise. The
1687  *   @app_exec string is owned by the #GtkRecentInfo and should not be
1688  *   modified or freed
1689  *
1690  * Since: 2.10
1691  */
1692 gboolean
1693 gtk_recent_info_get_application_info (GtkRecentInfo  *info,
1694                                       const gchar    *app_name,
1695                                       const gchar   **app_exec,
1696                                       guint          *count,
1697                                       time_t         *time_)
1698 {
1699   RecentAppInfo *ai;
1700   
1701   g_return_val_if_fail (info != NULL, FALSE);
1702   g_return_val_if_fail (app_name != NULL, FALSE);
1703   
1704   ai = (RecentAppInfo *) g_hash_table_lookup (info->apps_lookup,
1705                                               app_name);
1706   if (!ai)
1707     {
1708       g_warning ("No registered application with name '%s' "
1709                  "for item with URI '%s' found",
1710                  app_name,
1711                  info->uri);
1712       return FALSE;
1713     }
1714   
1715   if (app_exec)
1716     *app_exec = ai->exec;
1717   
1718   if (count)
1719     *count = ai->count;
1720   
1721   if (time_)
1722     *time_ = ai->stamp;
1723
1724   return TRUE;
1725 }
1726
1727 /**
1728  * gtk_recent_info_get_applications:
1729  * @info: a #GtkRecentInfo
1730  * @length: (out) (allow-none): return location for the length of the returned list
1731  *
1732  * Retrieves the list of applications that have registered this resource.
1733  *
1734  * Return value: (array length=length zero-terminated=1) (transfer full):
1735  *     a newly allocated %NULL-terminated array of strings.
1736  *     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: (transfer full): a #GdkPixbuf containing the icon,
1896  *     or %NULL. Use 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 ("folder", size);
1917       else
1918         retval = get_icon_fallback ("document-x-generic", 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) (transfer full):
2233  *     a newly allocated %NULL terminated array of strings.
2234  *     Use g_strfreev() to free it.
2235  *
2236  * Since: 2.10
2237  */
2238 gchar **
2239 gtk_recent_info_get_groups (GtkRecentInfo *info,
2240                             gsize         *length)
2241 {
2242   GSList *l;
2243   gchar **retval;
2244   gsize n_groups, i;
2245   
2246   g_return_val_if_fail (info != NULL, NULL);
2247   
2248   if (!info->groups)
2249     {
2250       if (length)
2251         *length = 0;
2252       
2253       return NULL;
2254     }
2255   
2256   n_groups = g_slist_length (info->groups);
2257   
2258   retval = g_new0 (gchar *, n_groups + 1);
2259   
2260   for (l = info->groups, i = 0;
2261        l != NULL;
2262        l = l->next)
2263     {
2264       gchar *group_name = (gchar *) l->data;
2265       
2266       g_assert (group_name != NULL);
2267       
2268       retval[i++] = g_strdup (group_name);
2269     }
2270   retval[i] = NULL;
2271   
2272   if (length)
2273     *length = i;
2274   
2275   return retval;
2276 }
2277
2278 /**
2279  * gtk_recent_info_has_group:
2280  * @info: a #GtkRecentInfo
2281  * @group_name: name of a group
2282  *
2283  * Checks whether @group_name appears inside the groups registered for the
2284  * recently used item @info.
2285  *
2286  * Return value: %TRUE if the group was found.
2287  *
2288  * Since: 2.10
2289  */
2290 gboolean
2291 gtk_recent_info_has_group (GtkRecentInfo *info,
2292                            const gchar   *group_name)
2293 {
2294   GSList *l;
2295   
2296   g_return_val_if_fail (info != NULL, FALSE);
2297   g_return_val_if_fail (group_name != NULL, FALSE);
2298
2299   if (!info->groups)
2300     return FALSE;
2301
2302   for (l = info->groups; l != NULL; l = l->next)
2303     {
2304       gchar *g = (gchar *) l->data;
2305
2306       if (strcmp (g, group_name) == 0)
2307         return TRUE;
2308     }
2309
2310   return FALSE;
2311 }
2312
2313 /*
2314  * _gtk_recent_manager_sync:
2315  * 
2316  * Private function for synchronising the recent manager singleton.
2317  */
2318 void
2319 _gtk_recent_manager_sync (void)
2320 {
2321   if (recent_manager_singleton)
2322     {
2323       /* force a dump of the contents of the recent manager singleton */
2324       recent_manager_singleton->priv->is_dirty = TRUE;
2325       gtk_recent_manager_real_changed (recent_manager_singleton);
2326     }
2327 }