~canonical-dx-team/ubuntu/maverick/gtk+2.0/menuproxy

« back to all changes in this revision

Viewing changes to gtk/gtkrecentmanager.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2007-05-04 12:24:25 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20070504122425-0m8midgzrp40y8w2
Tags: 2.10.12-1ubuntu1
* Sync with Debian
* New upstream version:
  Fixed bugs:
  - 379414 file chooser warnings when changing path in the entry
  - 418585 GtkFileChooserDefault sizing code is not DPI independent
  - 419568 Crash in search if start with special letter
  - 435062 build dies with icon cache validation
  - 379399 Segfault to call gtk_print_operation_run twice.
  - 387889 cups backend has problems when there are too many printers
  - 418531 invalid read to gtkicontheme.c gtk_icon_theme_lookup_icon...
  - 423916 crash in color scheme code
  - 424042 Segmentation fault while quickly pressing Alt+arrows
  - 415260 Protect against negative indices when setting values in G...
  - 419171 XGetVisualInfo() may not set nxvisuals
  - 128852 Gdk cursors don't look good on win32
  - 344657 Ctrl-H doesn't toggle "Show Hidden Files" setting
  - 345345 PrintOperation::paginate is not emitted for class handler
  - 347567 GtkPrintOperation::end-print is not emitted if it's cance...
  - 369112 gtk_ui_manager_add_ui should accept unnamed separator
  - 392015 Selected menu item invisible on Windows Vista
  - 399253 MS-Windows Theme Bottom Tab placement rendering glitches
  - 399425 gtk_input_dialog_fill_axes() adds child to gtkscrolledwin...
  - 403251 [patch] little memory leak in GtkPrintJob
  - 403267 [patch] memory leak in GtkPageSetupUnixDialog
  - 403470 MS-Windows Theme tab placement other than on top leaks a ...
  - 404506 Windows system fonts that have multi-byte font names cann...
  - 405089 Incorrect window placement for GtkEventBox private window
  - 405515 Minor leak in gtkfilesystemmodel.c
  - 405539 gdk_pixbuf_save() for PNG saver can return FALSE without ...
  - 415681 gdk_window_clear_area includes an extra line and column o...
  - 418219 GtkRecentChooser should apply filter before sorting and c...
  - 418403 Scroll to printer after selecting it from settings
  - 421985 _gtk_print_operation_platform_backend_launch_preview
  - 421990 gtk_print_job_get_surface
  - 421993 gtk_print_operation_init
  - 423064 Conditional jump or move depends on uninitialised value(s...
  - 423722 Fix printing header in gtk-demo
  - 424168 gtk_print_operation_run on async preview
  - 425655 Don't install gtk+-unix-print-2.0.pc on non-UNIX platforms
  - 425786 GDK segfaults if XineramaQueryScreens fails
  - 428665 Lpr Backend gets stuck in infinite loop during gtk_enumer...
  - 429902 GtkPrintOperation leaks cairo contextes
  - 431997 First delay of GdkPixbufAnimationIter is wrong
  - 433242 Inconsistent scroll arrow position calculations
  - 433972 Placing gtk.Expander inside a gtk.TextView() changes gtk....
  - 434261 _gtk_toolbar_elide_underscores incorrectly handles some s...
  - 383354 ctrl-L should make 'Location' entry disappear
  - 418673 gtk_recent_manager_add_item
  - 429732 gtk_accel_group_finalize accesses invalid memory
  - 435028 WM_CLIENT_LEADER is wrong on the leader_window
  - 431067 Background of the header window is not updated
  - 338843 add recent files support inside the ui manager
  - 148535 add drop shadow to menus, tooltips, etc. under Windows XP
* debian/control.in:
  - Conflicts on ubuntulooks (<= 0.9.11-1)
* debian/patches/15_default-fallback-icon-theme.patch:
  - patch from Debian, fallback on gnome icon theme

Show diffs side-by-side

added added

removed removed

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