~ubuntu-branches/ubuntu/maverick/gimp/maverick-updates

« back to all changes in this revision

Viewing changes to app/file/gimprecentlist.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2005-12-09 19:44:52 UTC
  • Revision ID: james.westby@ubuntu.com-20051209194452-yggpemjlofpjqyf4
Tags: upstream-2.2.9
ImportĀ upstreamĀ versionĀ 2.2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* The GIMP -- an image manipulation program
 
2
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 
3
 *
 
4
 * Recent File Storage,
 
5
 * see http://freedesktop.org/Standards/recent-file-spec/
 
6
 *
 
7
 * This code is taken from libegg and has been adapted to the GIMP needs.
 
8
 * The original author is James Willcox <jwillcox@cs.indiana.edu>,
 
9
 * responsible for bugs in this version is Sven Neumann <sven@gimp.org>.
 
10
 *
 
11
 * This program is free software; you can redistribute it and/or modify
 
12
 * it under the terms of the GNU General Public License as published by
 
13
 * the Free Software Foundation; either version 2 of the License, or
 
14
 * (at your option) any later version.
 
15
 *
 
16
 * This program is distributed in the hope that it will be useful,
 
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 * GNU General Public License for more details.
 
20
 *
 
21
 * You should have received a copy of the GNU General Public License
 
22
 * along with this program; if not, write to the Free Software
 
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
24
 */
 
25
 
 
26
 
 
27
#include "config.h"
 
28
 
 
29
#include <stdlib.h>
 
30
#include <string.h>
 
31
#include <errno.h>
 
32
#include <time.h>
 
33
#ifdef HAVE_UNISTD_H
 
34
#include <unistd.h>
 
35
#endif
 
36
#include <sys/types.h>
 
37
#include <sys/stat.h>
 
38
#include <fcntl.h>
 
39
 
 
40
#include <glib-object.h>
 
41
 
 
42
#ifndef G_OS_WIN32  /* This code doesn't compile on win32 and the use of
 
43
                     * the freedesktop standard doesn't make much sense
 
44
                     * there anyway. If someone wants to contribute a win32
 
45
                     * specific implementation, that would be appreciated.
 
46
                     */
 
47
 
 
48
#include "config/config-types.h"
 
49
 
 
50
#include "config/gimpxmlparser.h"
 
51
 
 
52
#include "gimprecentitem.h"
 
53
#include "gimprecentlist.h"
 
54
 
 
55
 
 
56
#define GIMP_RECENT_LIST_FILE_NAME      ".recently-used"
 
57
#define GIMP_RECENT_LIST_MAX_ITEMS      500
 
58
#define GIMP_RECENT_LIST_GROUP_GIMP     "gimp"
 
59
 
 
60
 
 
61
#define GIMP_RECENT_ITEM_LIST_UNREF(list) \
 
62
        g_list_foreach (list, (GFunc) gimp_recent_item_unref, NULL); \
 
63
        g_list_free (list);
 
64
 
 
65
 
 
66
typedef struct
 
67
{
 
68
  GSList         *states;
 
69
  GList          *items;
 
70
  GimpRecentItem *current_item;
 
71
} ParseInfo;
 
72
 
 
73
typedef enum
 
74
{
 
75
  STATE_START,
 
76
  STATE_RECENT_FILES,
 
77
  STATE_RECENT_ITEM,
 
78
  STATE_URI,
 
79
  STATE_MIME_TYPE,
 
80
  STATE_TIMESTAMP,
 
81
  STATE_PRIVATE,
 
82
  STATE_GROUPS,
 
83
  STATE_GROUP
 
84
} ParseState;
 
85
 
 
86
 
 
87
#define TAG_RECENT_FILES "RecentFiles"
 
88
#define TAG_RECENT_ITEM  "RecentItem"
 
89
#define TAG_URI          "URI"
 
90
#define TAG_MIME_TYPE    "Mime-Type"
 
91
#define TAG_TIMESTAMP    "Timestamp"
 
92
#define TAG_PRIVATE      "Private"
 
93
#define TAG_GROUPS       "Groups"
 
94
#define TAG_GROUP        "Group"
 
95
 
 
96
 
 
97
static void  start_element_handler (GMarkupParseContext  *context,
 
98
                                    const gchar          *element_name,
 
99
                                    const gchar         **attribute_names,
 
100
                                    const gchar         **attribute_values,
 
101
                                    gpointer              user_data,
 
102
                                    GError              **error);
 
103
static void  end_element_handler   (GMarkupParseContext  *context,
 
104
                                    const gchar          *element_name,
 
105
                                    gpointer              user_data,
 
106
                                    GError              **error);
 
107
static void  text_handler          (GMarkupParseContext  *context,
 
108
                                    const gchar          *text,
 
109
                                    gsize                 text_len,
 
110
                                    gpointer              user_data,
 
111
                                    GError              **error);
 
112
 
 
113
static const GMarkupParser markup_parser =
 
114
{
 
115
  start_element_handler,
 
116
  end_element_handler,
 
117
  text_handler,
 
118
  NULL,
 
119
  NULL
 
120
};
 
121
 
 
122
static void
 
123
gimp_recent_list_add_new_groups (GimpRecentItem *item,
 
124
                                 GimpRecentItem *upd_item)
 
125
{
 
126
  const GList *tmp;
 
127
 
 
128
  for (tmp = gimp_recent_item_get_groups (upd_item); tmp; tmp = tmp->next)
 
129
    {
 
130
      const gchar *group = tmp->data;
 
131
 
 
132
      if (! gimp_recent_item_in_group (item, group))
 
133
        gimp_recent_item_add_group (item, group);
 
134
    }
 
135
}
 
136
 
 
137
static gboolean
 
138
gimp_recent_list_update_item (GList          *items,
 
139
                              GimpRecentItem *upd_item)
 
140
{
 
141
  const char *uri = gimp_recent_item_get_uri (upd_item);
 
142
  GList      *tmp;
 
143
 
 
144
  for (tmp = items; tmp; tmp = tmp->next)
 
145
    {
 
146
      GimpRecentItem *item = tmp->data;
 
147
 
 
148
      /*  gnome_vfs_uris_match (gimp_recent_item_get_uri (item), uri)  */
 
149
      if (strcmp (gimp_recent_item_get_uri (item), uri) == 0)
 
150
        {
 
151
          gimp_recent_item_set_timestamp (item, (time_t) -1);
 
152
          gimp_recent_list_add_new_groups (item, upd_item);
 
153
 
 
154
          return TRUE;
 
155
        }
 
156
    }
 
157
 
 
158
  return FALSE;
 
159
}
 
160
 
 
161
static void
 
162
parse_info_init (ParseInfo *info)
 
163
{
 
164
  info->states = g_slist_prepend (NULL, GINT_TO_POINTER (STATE_START));
 
165
  info->items = NULL;
 
166
}
 
167
 
 
168
static void
 
169
parse_info_free (ParseInfo *info)
 
170
{
 
171
  g_slist_free (info->states);
 
172
}
 
173
 
 
174
static void
 
175
push_state (ParseInfo  *info,
 
176
            ParseState  state)
 
177
{
 
178
  info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
 
179
}
 
180
 
 
181
static void
 
182
pop_state (ParseInfo *info)
 
183
{
 
184
  g_return_if_fail (info->states != NULL);
 
185
 
 
186
  info->states = g_slist_remove (info->states, info->states->data);
 
187
}
 
188
 
 
189
static ParseState
 
190
peek_state (ParseInfo *info)
 
191
{
 
192
  g_return_val_if_fail (info->states != NULL, STATE_START);
 
193
 
 
194
  return GPOINTER_TO_INT (info->states->data);
 
195
}
 
196
 
 
197
#define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)
 
198
 
 
199
static void
 
200
start_element_handler (GMarkupParseContext  *context,
 
201
                       const gchar          *element_name,
 
202
                       const gchar         **attribute_names,
 
203
                       const gchar         **attribute_values,
 
204
                       gpointer              user_data,
 
205
                       GError              **error)
 
206
{
 
207
  ParseInfo *info = user_data;
 
208
 
 
209
  if (ELEMENT_IS (TAG_RECENT_FILES))
 
210
    {
 
211
      push_state (info, STATE_RECENT_FILES);
 
212
    }
 
213
  else if (ELEMENT_IS (TAG_RECENT_ITEM))
 
214
    {
 
215
      info->current_item = gimp_recent_item_new ();
 
216
      push_state (info, STATE_RECENT_ITEM);
 
217
    }
 
218
  else if (ELEMENT_IS (TAG_URI))
 
219
    {
 
220
      push_state (info, STATE_URI);
 
221
    }
 
222
  else if (ELEMENT_IS (TAG_MIME_TYPE))
 
223
    {
 
224
      push_state (info, STATE_MIME_TYPE);
 
225
    }
 
226
  else if (ELEMENT_IS (TAG_TIMESTAMP))
 
227
    {
 
228
      push_state (info, STATE_TIMESTAMP);
 
229
    }
 
230
  else if (ELEMENT_IS (TAG_PRIVATE))
 
231
    {
 
232
      push_state (info, STATE_PRIVATE);
 
233
      gimp_recent_item_set_private (info->current_item, TRUE);
 
234
    }
 
235
  else if (ELEMENT_IS (TAG_GROUPS))
 
236
    {
 
237
      push_state (info, STATE_GROUPS);
 
238
    }
 
239
  else if (ELEMENT_IS (TAG_GROUP))
 
240
    {
 
241
      push_state (info, STATE_GROUP);
 
242
    }
 
243
}
 
244
 
 
245
static void
 
246
end_element_handler (GMarkupParseContext  *context,
 
247
                     const gchar          *element_name,
 
248
                     gpointer              user_data,
 
249
                     GError              **error)
 
250
{
 
251
  ParseInfo *info = user_data;
 
252
 
 
253
  switch (peek_state (info))
 
254
    {
 
255
    case STATE_RECENT_ITEM:
 
256
      info->items = g_list_prepend (info->items, info->current_item);
 
257
      break;
 
258
 
 
259
    default:
 
260
      break;
 
261
    }
 
262
 
 
263
  pop_state (info);
 
264
}
 
265
 
 
266
static void
 
267
text_handler (GMarkupParseContext  *context,
 
268
              const gchar          *text,
 
269
              gsize                 text_len,
 
270
              gpointer              user_data,
 
271
              GError              **error)
 
272
{
 
273
  ParseInfo *info = user_data;
 
274
 
 
275
  switch (peek_state (info))
 
276
    {
 
277
    case STATE_START:
 
278
    case STATE_RECENT_FILES:
 
279
    case STATE_RECENT_ITEM:
 
280
    case STATE_PRIVATE:
 
281
    case STATE_GROUPS:
 
282
      break;
 
283
 
 
284
    case STATE_URI:
 
285
      gimp_recent_item_set_uri (info->current_item, text);
 
286
      break;
 
287
 
 
288
    case STATE_MIME_TYPE:
 
289
      gimp_recent_item_set_mime_type (info->current_item, text);
 
290
      break;
 
291
 
 
292
    case STATE_TIMESTAMP:
 
293
      gimp_recent_item_set_timestamp (info->current_item, (time_t) atoi (text));
 
294
      break;
 
295
 
 
296
    case STATE_GROUP:
 
297
      gimp_recent_item_add_group (info->current_item, text);
 
298
      break;
 
299
    }
 
300
}
 
301
 
 
302
static void
 
303
gimp_recent_list_enforce_limit (GList *list,
 
304
                                gint   limit)
 
305
{
 
306
  gint   len;
 
307
  GList *end;
 
308
 
 
309
  /* limit < 0 means unlimited */
 
310
  if (limit <= 0)
 
311
    return;
 
312
 
 
313
  len = g_list_length (list);
 
314
 
 
315
  if (len > limit)
 
316
    {
 
317
      GList *next;
 
318
 
 
319
      end = g_list_nth (list, limit-1);
 
320
      next = end->next;
 
321
 
 
322
      end->next = NULL;
 
323
 
 
324
      GIMP_RECENT_ITEM_LIST_UNREF (next);
 
325
    }
 
326
}
 
327
 
 
328
static GList *
 
329
gimp_recent_list_read (gint fd)
 
330
{
 
331
  GimpXmlParser  *parser;
 
332
  GList          *list;
 
333
  ParseInfo       info;
 
334
  GError         *error = NULL;
 
335
 
 
336
  lseek (fd, 0, SEEK_SET);
 
337
 
 
338
  parse_info_init (&info);
 
339
 
 
340
  parser = gimp_xml_parser_new (&markup_parser, &info);
 
341
 
 
342
  if (! gimp_xml_parser_parse_fd (parser, fd, &error))
 
343
    {
 
344
      g_printerr ("%s", error->message);
 
345
      g_error_free (error);
 
346
    }
 
347
 
 
348
  gimp_xml_parser_free (parser);
 
349
 
 
350
  list = info.items;
 
351
 
 
352
  parse_info_free (&info);
 
353
 
 
354
  return g_list_reverse (list);
 
355
}
 
356
 
 
357
static gboolean
 
358
gimp_recent_list_write_raw (gint         fd,
 
359
                            const gchar *content,
 
360
                            gssize       len)
 
361
{
 
362
  struct stat  sbuf;
 
363
  gssize       remaining = len;
 
364
 
 
365
  lseek (fd, 0, SEEK_SET);
 
366
 
 
367
  if (fstat (fd, &sbuf) < 0)
 
368
    {
 
369
      g_warning ("Couldn't stat XML document.");
 
370
    }
 
371
  else
 
372
    {
 
373
      if ((off_t) len < sbuf.st_size)
 
374
        ftruncate (fd, len);
 
375
    }
 
376
 
 
377
  while (remaining > 0)
 
378
    {
 
379
      gssize  written = write (fd, content, remaining);
 
380
 
 
381
      if (written < 0 && errno != EINTR)
 
382
        return FALSE;
 
383
 
 
384
      remaining -= written;
 
385
    }
 
386
 
 
387
  fsync (fd);
 
388
 
 
389
  return TRUE;
 
390
}
 
391
 
 
392
static gboolean
 
393
gimp_recent_list_write (gint   fd,
 
394
                        GList *list)
 
395
{
 
396
  GString  *string;
 
397
  gboolean  success;
 
398
 
 
399
  string = g_string_new ("<?xml version=\"1.0\"?>\n");
 
400
  string = g_string_append (string, "<" TAG_RECENT_FILES ">\n");
 
401
 
 
402
  while (list)
 
403
    {
 
404
      GimpRecentItem *item = list->data;
 
405
      const GList    *groups;
 
406
      gchar          *uri;
 
407
      const gchar    *mime_type;
 
408
      gchar          *escaped_uri;
 
409
      time_t          timestamp;
 
410
 
 
411
      uri = gimp_recent_item_get_uri_utf8 (item);
 
412
      escaped_uri = g_markup_escape_text (uri, strlen (uri));
 
413
      g_free (uri);
 
414
 
 
415
      mime_type = gimp_recent_item_get_mime_type (item);
 
416
      timestamp = gimp_recent_item_get_timestamp (item);
 
417
 
 
418
      string = g_string_append (string, "  <" TAG_RECENT_ITEM ">\n");
 
419
 
 
420
      g_string_append_printf (string,
 
421
                              "    <" TAG_URI ">%s</" TAG_URI ">\n", escaped_uri);
 
422
 
 
423
      if (mime_type)
 
424
        g_string_append_printf (string,
 
425
                                "    <" TAG_MIME_TYPE ">%s</" TAG_MIME_TYPE ">\n", mime_type);
 
426
      else
 
427
        g_string_append_printf (string,
 
428
                                "    <" TAG_MIME_TYPE "></" TAG_MIME_TYPE ">\n");
 
429
 
 
430
      g_string_append_printf (string,
 
431
                              "    <" TAG_TIMESTAMP ">%d</" TAG_TIMESTAMP ">\n", (gint) timestamp);
 
432
 
 
433
      if (gimp_recent_item_get_private (item))
 
434
        string = g_string_append (string,
 
435
                                  "    <" TAG_PRIVATE "/>\n");
 
436
 
 
437
      groups = gimp_recent_item_get_groups (item);
 
438
 
 
439
      if (groups)
 
440
        {
 
441
          /* write the groups */
 
442
          string = g_string_append (string,
 
443
                                    "    <" TAG_GROUPS ">\n");
 
444
 
 
445
          if (groups == NULL && gimp_recent_item_get_private (item))
 
446
            g_warning ("Item with URI \"%s\" marked as private, but"
 
447
                       " does not belong to any groups.\n", uri);
 
448
 
 
449
          while (groups)
 
450
            {
 
451
              const gchar *group = groups->data;
 
452
              gchar       *escaped_group;
 
453
 
 
454
              escaped_group = g_markup_escape_text (group, strlen(group));
 
455
 
 
456
              g_string_append_printf (string,
 
457
                                      "      <" TAG_GROUP ">%s</" TAG_GROUP ">\n",
 
458
                                      escaped_group);
 
459
 
 
460
              g_free (escaped_group);
 
461
 
 
462
              groups = groups->next;
 
463
            }
 
464
 
 
465
          string = g_string_append (string, "    </" TAG_GROUPS ">\n");
 
466
        }
 
467
 
 
468
      string = g_string_append (string,
 
469
                                "  </" TAG_RECENT_ITEM ">\n");
 
470
 
 
471
      g_free (escaped_uri);
 
472
 
 
473
      list = list->next;
 
474
    }
 
475
 
 
476
  string = g_string_append (string, "</" TAG_RECENT_FILES ">");
 
477
 
 
478
  success = gimp_recent_list_write_raw (fd, string->str, string->len);
 
479
 
 
480
  g_string_free (string, TRUE);
 
481
 
 
482
  return success;
 
483
}
 
484
 
 
485
static gboolean
 
486
gimp_recent_list_lock_file (gint fd)
 
487
{
 
488
  gint  i;
 
489
 
 
490
  /* Attempt to lock the file 5 times,
 
491
   * waiting a random interval (< 1 second)
 
492
   * in between attempts.
 
493
   * We should really be doing asynchronous
 
494
   * locking, but requires substantially larger
 
495
   * changes.
 
496
   */
 
497
 
 
498
  lseek (fd, 0, SEEK_SET);
 
499
 
 
500
  for (i = 0; i < 5; i++)
 
501
    {
 
502
      gint rand_interval;
 
503
 
 
504
      if (lockf (fd, F_TLOCK, 0) == 0)
 
505
        return TRUE;
 
506
 
 
507
      rand_interval = 1 + (gint) (10.0 * rand () / (RAND_MAX + 1.0));
 
508
 
 
509
      g_usleep (100000 * rand_interval);
 
510
    }
 
511
 
 
512
  return FALSE;
 
513
}
 
514
 
 
515
static gboolean
 
516
gimp_recent_list_unlock_file (gint fd)
 
517
{
 
518
  lseek (fd, 0, SEEK_SET);
 
519
 
 
520
  return (lockf (fd, F_ULOCK, 0) == 0) ? TRUE : FALSE;
 
521
}
 
522
 
 
523
static gboolean
 
524
gimp_recent_list_add_item (GimpRecentItem *item)
 
525
{
 
526
  const gchar *home;
 
527
  gchar       *filename;
 
528
  gint         fd;
 
529
  gboolean     success = FALSE;
 
530
  gboolean     created = FALSE;
 
531
  gboolean     updated = FALSE;
 
532
 
 
533
  home = g_get_home_dir ();
 
534
  if (! home)
 
535
    return FALSE;
 
536
 
 
537
  filename = g_build_filename (home, GIMP_RECENT_LIST_FILE_NAME, NULL);
 
538
 
 
539
  fd = open (filename, O_RDWR);
 
540
 
 
541
  if (fd < 0)
 
542
    {
 
543
      fd = creat (filename, S_IRUSR | S_IWUSR);
 
544
      created = TRUE;
 
545
    }
 
546
 
 
547
  g_free (filename);
 
548
 
 
549
  if (fd < 0)
 
550
    return FALSE;
 
551
 
 
552
  if (gimp_recent_list_lock_file (fd))
 
553
    {
 
554
      GList *list = NULL;
 
555
 
 
556
      if (! created)
 
557
        list = gimp_recent_list_read (fd);
 
558
 
 
559
      /* if it's already there, we just update it */
 
560
      updated = gimp_recent_list_update_item (list, item);
 
561
 
 
562
      if (!updated)
 
563
        {
 
564
          list = g_list_prepend (list, item);
 
565
          gimp_recent_list_enforce_limit (list, GIMP_RECENT_LIST_MAX_ITEMS);
 
566
        }
 
567
 
 
568
      /* write new stuff */
 
569
      if (!gimp_recent_list_write (fd, list))
 
570
        g_warning ("Write failed: %s", g_strerror (errno));
 
571
 
 
572
      if (!updated)
 
573
        list = g_list_remove (list, item);
 
574
 
 
575
      GIMP_RECENT_ITEM_LIST_UNREF (list);
 
576
      success = TRUE;
 
577
    }
 
578
  else
 
579
    {
 
580
      g_warning ("Failed to lock:  %s", g_strerror (errno));
 
581
      close (fd);
 
582
      return FALSE;
 
583
    }
 
584
 
 
585
  if (! gimp_recent_list_unlock_file (fd))
 
586
    g_warning ("Failed to unlock: %s", strerror (errno));
 
587
 
 
588
  close (fd);
 
589
 
 
590
  return success;
 
591
}
 
592
 
 
593
/**
 
594
 * gimp_recent_list_add_uri:
 
595
 * @uri:       an URI
 
596
 * @mime_type: a MIME type
 
597
 *
 
598
 * This function adds an item to the list of recently used URIs.
 
599
 * See http://freedesktop.org/Standards/recent-file-spec/.
 
600
 *
 
601
 * On the Win32 platform, this call is unimplemented and will always
 
602
 * fail.
 
603
 *
 
604
 * Returns: %TRUE on success, %FALSE otherwise
 
605
 */
 
606
gboolean
 
607
gimp_recent_list_add_uri (const gchar *uri,
 
608
                          const gchar *mime_type)
 
609
{
 
610
  GimpRecentItem *item;
 
611
  gboolean        success;
 
612
 
 
613
  g_return_val_if_fail (uri != NULL, FALSE);
 
614
 
 
615
  if (! mime_type          ||
 
616
      ! strlen (mime_type) ||
 
617
      ! g_utf8_validate (mime_type, -1, NULL))
 
618
    return FALSE;
 
619
 
 
620
  item = gimp_recent_item_new_from_uri (uri);
 
621
  if (! item)
 
622
    return FALSE;
 
623
 
 
624
  gimp_recent_item_set_mime_type (item, mime_type);
 
625
  gimp_recent_item_set_timestamp (item, -1);
 
626
  gimp_recent_item_add_group (item, GIMP_RECENT_LIST_GROUP_GIMP);
 
627
 
 
628
  success = gimp_recent_list_add_item (item);
 
629
 
 
630
  gimp_recent_item_unref (item);
 
631
 
 
632
  return success;
 
633
}
 
634
 
 
635
#else  /* G_OS_WIN32  */
 
636
 
 
637
gboolean
 
638
gimp_recent_list_add_uri (const gchar *uri,
 
639
                          const gchar *mime_type)
 
640
{
 
641
  return FALSE;
 
642
}
 
643
 
 
644
#endif