File: gimprecentlist.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (644 lines) | stat: -rw-r--r-- 15,850 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Recent File Storage,
 * see http://freedesktop.org/Standards/recent-file-spec/
 *
 * This code is taken from libegg and has been adapted to the GIMP needs.
 * The original author is James Willcox <jwillcox@cs.indiana.edu>,
 * responsible for bugs in this version is Sven Neumann <sven@gimp.org>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */


#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <glib-object.h>

#ifndef G_OS_WIN32  /* This code doesn't compile on win32 and the use of
                     * the freedesktop standard doesn't make much sense
                     * there anyway. If someone wants to contribute a win32
                     * specific implementation, that would be appreciated.
                     */

#include "config/config-types.h"

#include "config/gimpxmlparser.h"

#include "gimprecentitem.h"
#include "gimprecentlist.h"


#define GIMP_RECENT_LIST_FILE_NAME      ".recently-used"
#define GIMP_RECENT_LIST_MAX_ITEMS      500
#define GIMP_RECENT_LIST_GROUP_GIMP     "gimp"


#define GIMP_RECENT_ITEM_LIST_UNREF(list) \
	g_list_foreach (list, (GFunc) gimp_recent_item_unref, NULL); \
	g_list_free (list);


typedef struct
{
  GSList         *states;
  GList          *items;
  GimpRecentItem *current_item;
} ParseInfo;

typedef enum
{
  STATE_START,
  STATE_RECENT_FILES,
  STATE_RECENT_ITEM,
  STATE_URI,
  STATE_MIME_TYPE,
  STATE_TIMESTAMP,
  STATE_PRIVATE,
  STATE_GROUPS,
  STATE_GROUP
} ParseState;


#define TAG_RECENT_FILES "RecentFiles"
#define TAG_RECENT_ITEM  "RecentItem"
#define TAG_URI          "URI"
#define TAG_MIME_TYPE    "Mime-Type"
#define TAG_TIMESTAMP    "Timestamp"
#define TAG_PRIVATE      "Private"
#define TAG_GROUPS       "Groups"
#define TAG_GROUP        "Group"


static void  start_element_handler (GMarkupParseContext  *context,
                                    const gchar          *element_name,
                                    const gchar         **attribute_names,
                                    const gchar         **attribute_values,
                                    gpointer              user_data,
                                    GError              **error);
static void  end_element_handler   (GMarkupParseContext  *context,
                                    const gchar          *element_name,
                                    gpointer              user_data,
                                    GError              **error);
static void  text_handler          (GMarkupParseContext  *context,
                                    const gchar          *text,
                                    gsize                 text_len,
                                    gpointer              user_data,
                                    GError              **error);

static const GMarkupParser markup_parser =
{
  start_element_handler,
  end_element_handler,
  text_handler,
  NULL,
  NULL
};

static void
gimp_recent_list_add_new_groups (GimpRecentItem *item,
				 GimpRecentItem *upd_item)
{
  const GList *tmp;

  for (tmp = gimp_recent_item_get_groups (upd_item); tmp; tmp = tmp->next)
    {
      const gchar *group = tmp->data;

      if (! gimp_recent_item_in_group (item, group))
        gimp_recent_item_add_group (item, group);
    }
}

static gboolean
gimp_recent_list_update_item (GList          *items,
                              GimpRecentItem *upd_item)
{
  const char *uri = gimp_recent_item_get_uri (upd_item);
  GList      *tmp;

  for (tmp = items; tmp; tmp = tmp->next)
    {
      GimpRecentItem *item = tmp->data;

      /*  gnome_vfs_uris_match (gimp_recent_item_get_uri (item), uri)  */
      if (strcmp (gimp_recent_item_get_uri (item), uri) == 0)
        {
          gimp_recent_item_set_timestamp (item, (time_t) -1);
          gimp_recent_list_add_new_groups (item, upd_item);

          return TRUE;
        }
    }

  return FALSE;
}

static void
parse_info_init (ParseInfo *info)
{
  info->states = g_slist_prepend (NULL, GINT_TO_POINTER (STATE_START));
  info->items = NULL;
}

static void
parse_info_free (ParseInfo *info)
{
  g_slist_free (info->states);
}

static void
push_state (ParseInfo  *info,
            ParseState  state)
{
  info->states = g_slist_prepend (info->states, GINT_TO_POINTER (state));
}

static void
pop_state (ParseInfo *info)
{
  g_return_if_fail (info->states != NULL);

  info->states = g_slist_remove (info->states, info->states->data);
}

static ParseState
peek_state (ParseInfo *info)
{
  g_return_val_if_fail (info->states != NULL, STATE_START);

  return GPOINTER_TO_INT (info->states->data);
}

#define ELEMENT_IS(name) (strcmp (element_name, (name)) == 0)

static void
start_element_handler (GMarkupParseContext  *context,
                       const gchar          *element_name,
                       const gchar         **attribute_names,
                       const gchar         **attribute_values,
                       gpointer              user_data,
                       GError              **error)
{
  ParseInfo *info = user_data;

  if (ELEMENT_IS (TAG_RECENT_FILES))
    {
      push_state (info, STATE_RECENT_FILES);
    }
  else if (ELEMENT_IS (TAG_RECENT_ITEM))
    {
      info->current_item = gimp_recent_item_new ();
      push_state (info, STATE_RECENT_ITEM);
    }
  else if (ELEMENT_IS (TAG_URI))
    {
      push_state (info, STATE_URI);
    }
  else if (ELEMENT_IS (TAG_MIME_TYPE))
    {
      push_state (info, STATE_MIME_TYPE);
    }
  else if (ELEMENT_IS (TAG_TIMESTAMP))
    {
      push_state (info, STATE_TIMESTAMP);
    }
  else if (ELEMENT_IS (TAG_PRIVATE))
    {
      push_state (info, STATE_PRIVATE);
      gimp_recent_item_set_private (info->current_item, TRUE);
    }
  else if (ELEMENT_IS (TAG_GROUPS))
    {
      push_state (info, STATE_GROUPS);
    }
  else if (ELEMENT_IS (TAG_GROUP))
    {
      push_state (info, STATE_GROUP);
    }
}

static void
end_element_handler (GMarkupParseContext  *context,
                     const gchar          *element_name,
                     gpointer              user_data,
                     GError              **error)
{
  ParseInfo *info = user_data;

  switch (peek_state (info))
    {
    case STATE_RECENT_ITEM:
      info->items = g_list_prepend (info->items, info->current_item);
      break;

    default:
      break;
    }

  pop_state (info);
}

static void
text_handler (GMarkupParseContext  *context,
              const gchar          *text,
              gsize                 text_len,
              gpointer              user_data,
              GError              **error)
{
  ParseInfo *info = user_data;

  switch (peek_state (info))
    {
    case STATE_START:
    case STATE_RECENT_FILES:
    case STATE_RECENT_ITEM:
    case STATE_PRIVATE:
    case STATE_GROUPS:
      break;

    case STATE_URI:
      gimp_recent_item_set_uri (info->current_item, text);
      break;

    case STATE_MIME_TYPE:
      gimp_recent_item_set_mime_type (info->current_item, text);
      break;

    case STATE_TIMESTAMP:
      gimp_recent_item_set_timestamp (info->current_item, (time_t) atoi (text));
      break;

    case STATE_GROUP:
      gimp_recent_item_add_group (info->current_item, text);
      break;
    }
}

static void
gimp_recent_list_enforce_limit (GList *list,
                                gint   limit)
{
  gint   len;
  GList *end;

  /* limit < 0 means unlimited */
  if (limit <= 0)
    return;

  len = g_list_length (list);

  if (len > limit)
    {
      GList *next;

      end = g_list_nth (list, limit-1);
      next = end->next;

      end->next = NULL;

      GIMP_RECENT_ITEM_LIST_UNREF (next);
    }
}

static GList *
gimp_recent_list_read (gint fd)
{
  GimpXmlParser  *parser;
  GList          *list;
  ParseInfo       info;
  GError         *error = NULL;

  lseek (fd, 0, SEEK_SET);

  parse_info_init (&info);

  parser = gimp_xml_parser_new (&markup_parser, &info);

  if (! gimp_xml_parser_parse_fd (parser, fd, &error))
    {
      g_printerr ("%s", error->message);
      g_error_free (error);
    }

  gimp_xml_parser_free (parser);

  list = info.items;

  parse_info_free (&info);

  return g_list_reverse (list);
}

static gboolean
gimp_recent_list_write_raw (gint         fd,
                            const gchar *content,
                            gssize       len)
{
  struct stat  sbuf;
  gssize       remaining = len;

  lseek (fd, 0, SEEK_SET);

  if (fstat (fd, &sbuf) < 0)
    {
      g_warning ("Couldn't stat XML document.");
    }
  else
    {
      if ((off_t) len < sbuf.st_size)
        ftruncate (fd, len);
    }

  while (remaining > 0)
    {
      gssize  written = write (fd, content, remaining);

      if (written < 0 && errno != EINTR)
        return FALSE;

      remaining -= written;
    }

  fsync (fd);

  return TRUE;
}

static gboolean
gimp_recent_list_write (gint   fd,
                        GList *list)
{
  GString  *string;
  gboolean  success;

  string = g_string_new ("<?xml version=\"1.0\"?>\n");
  string = g_string_append (string, "<" TAG_RECENT_FILES ">\n");

  while (list)
    {
      GimpRecentItem *item = list->data;
      const GList    *groups;
      gchar          *uri;
      const gchar    *mime_type;
      gchar          *escaped_uri;
      time_t          timestamp;

      uri = gimp_recent_item_get_uri_utf8 (item);
      escaped_uri = g_markup_escape_text (uri, strlen (uri));
      g_free (uri);

      mime_type = gimp_recent_item_get_mime_type (item);
      timestamp = gimp_recent_item_get_timestamp (item);

      string = g_string_append (string, "  <" TAG_RECENT_ITEM ">\n");

      g_string_append_printf (string,
                              "    <" TAG_URI ">%s</" TAG_URI ">\n", escaped_uri);

      if (mime_type)
        g_string_append_printf (string,
				"    <" TAG_MIME_TYPE ">%s</" TAG_MIME_TYPE ">\n", mime_type);
      else
        g_string_append_printf (string,
				"    <" TAG_MIME_TYPE "></" TAG_MIME_TYPE ">\n");

      g_string_append_printf (string,
                              "    <" TAG_TIMESTAMP ">%d</" TAG_TIMESTAMP ">\n", (gint) timestamp);

      if (gimp_recent_item_get_private (item))
        string = g_string_append (string,
                                  "    <" TAG_PRIVATE "/>\n");

      groups = gimp_recent_item_get_groups (item);

      if (groups)
        {
          /* write the groups */
          string = g_string_append (string,
                                    "    <" TAG_GROUPS ">\n");

          if (groups == NULL && gimp_recent_item_get_private (item))
            g_warning ("Item with URI \"%s\" marked as private, but"
                       " does not belong to any groups.\n", uri);

          while (groups)
            {
              const gchar *group = groups->data;
              gchar       *escaped_group;

              escaped_group = g_markup_escape_text (group, strlen(group));

              g_string_append_printf (string,
                                      "      <" TAG_GROUP ">%s</" TAG_GROUP ">\n",
                                      escaped_group);

              g_free (escaped_group);

              groups = groups->next;
            }

          string = g_string_append (string, "    </" TAG_GROUPS ">\n");
        }

      string = g_string_append (string,
				"  </" TAG_RECENT_ITEM ">\n");

      g_free (escaped_uri);

      list = list->next;
    }

  string = g_string_append (string, "</" TAG_RECENT_FILES ">");

  success = gimp_recent_list_write_raw (fd, string->str, string->len);

  g_string_free (string, TRUE);

  return success;
}

static gboolean
gimp_recent_list_lock_file (gint fd)
{
  gint	i;

  /* Attempt to lock the file 5 times,
   * waiting a random interval (< 1 second)
   * in between attempts.
   * We should really be doing asynchronous
   * locking, but requires substantially larger
   * changes.
   */

  lseek (fd, 0, SEEK_SET);

  for (i = 0; i < 5; i++)
    {
      gint rand_interval;

      if (lockf (fd, F_TLOCK, 0) == 0)
        return TRUE;

      rand_interval = 1 + (gint) (10.0 * rand () / (RAND_MAX + 1.0));

      g_usleep (100000 * rand_interval);
    }

  return FALSE;
}

static gboolean
gimp_recent_list_unlock_file (gint fd)
{
  lseek (fd, 0, SEEK_SET);

  return (lockf (fd, F_ULOCK, 0) == 0) ? TRUE : FALSE;
}

static gboolean
gimp_recent_list_add_item (GimpRecentItem *item)
{
  const gchar *home;
  gchar       *filename;
  gint         fd;
  gboolean     success = FALSE;
  gboolean     created = FALSE;
  gboolean     updated = FALSE;

  home = g_get_home_dir ();
  if (! home)
    return FALSE;

  filename = g_build_filename (home, GIMP_RECENT_LIST_FILE_NAME, NULL);

  fd = open (filename, O_RDWR);

  if (fd < 0)
    {
      fd = creat (filename, S_IRUSR | S_IWUSR);
      created = TRUE;
    }

  g_free (filename);

  if (fd < 0)
    return FALSE;

  if (gimp_recent_list_lock_file (fd))
    {
      GList *list = NULL;

      if (! created)
        list = gimp_recent_list_read (fd);

      /* if it's already there, we just update it */
      updated = gimp_recent_list_update_item (list, item);

      if (!updated)
        {
          list = g_list_prepend (list, item);
          gimp_recent_list_enforce_limit (list, GIMP_RECENT_LIST_MAX_ITEMS);
        }

      /* write new stuff */
      if (!gimp_recent_list_write (fd, list))
        g_warning ("Write failed: %s", g_strerror (errno));

      if (!updated)
        list = g_list_remove (list, item);

      GIMP_RECENT_ITEM_LIST_UNREF (list);
      success = TRUE;
    }
  else
    {
      g_warning ("Failed to lock:  %s", g_strerror (errno));
      close (fd);
      return FALSE;
    }

  if (! gimp_recent_list_unlock_file (fd))
    g_warning ("Failed to unlock: %s", strerror (errno));

  close (fd);

  return success;
}

/**
 * gimp_recent_list_add_uri:
 * @uri:       an URI
 * @mime_type: a MIME type
 *
 * This function adds an item to the list of recently used URIs.
 * See http://freedesktop.org/Standards/recent-file-spec/.
 *
 * On the Win32 platform, this call is unimplemented and will always
 * fail.
 *
 * Returns: %TRUE on success, %FALSE otherwise
 */
gboolean
gimp_recent_list_add_uri (const gchar *uri,
                          const gchar *mime_type)
{
  GimpRecentItem *item;
  gboolean        success;

  g_return_val_if_fail (uri != NULL, FALSE);

  if (! mime_type          ||
      ! strlen (mime_type) ||
      ! g_utf8_validate (mime_type, -1, NULL))
    return FALSE;

  item = gimp_recent_item_new_from_uri (uri);
  if (! item)
    return FALSE;

  gimp_recent_item_set_mime_type (item, mime_type);
  gimp_recent_item_set_timestamp (item, -1);
  gimp_recent_item_add_group (item, GIMP_RECENT_LIST_GROUP_GIMP);

  success = gimp_recent_list_add_item (item);

  gimp_recent_item_unref (item);

  return success;
}

#else  /* G_OS_WIN32  */

gboolean
gimp_recent_list_add_uri (const gchar *uri,
                          const gchar *mime_type)
{
  return FALSE;
}

#endif