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