Imported Upstream version 2.74.3
[platform/upstream/glib.git] / glib / gbookmarkfile.c
1 /* gbookmarkfile.c: parsing and building desktop bookmarks
2  *
3  * Copyright (C) 2005-2006 Emmanuele Bassi
4  *
5  * SPDX-License-Identifier: LGPL-2.1-or-later
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "config.h"
22
23 #include "gbookmarkfile.h"
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <locale.h>
31 #include <time.h>
32 #include <stdarg.h>
33
34 #include "gconvert.h"
35 #include "gdataset.h"
36 #include "gdatetime.h"
37 #include "gerror.h"
38 #include "gfileutils.h"
39 #include "ghash.h"
40 #include "glibintl.h"
41 #include "glist.h"
42 #include "gmain.h"
43 #include "gmarkup.h"
44 #include "gmem.h"
45 #include "gmessages.h"
46 #include "gshell.h"
47 #include "gslice.h"
48 #include "gstdio.h"
49 #include "gstring.h"
50 #include "gstrfuncs.h"
51 #include "gtimer.h"
52 #include "gutils.h"
53
54
55 /**
56  * SECTION:bookmarkfile
57  * @title: Bookmark file parser
58  * @short_description: parses files containing bookmarks
59  *
60  * GBookmarkFile lets you parse, edit or create files containing bookmarks
61  * to URI, along with some meta-data about the resource pointed by the URI
62  * like its MIME type, the application that is registering the bookmark and
63  * the icon that should be used to represent the bookmark. The data is stored
64  * using the
65  * [Desktop Bookmark Specification](http://www.gnome.org/~ebassi/bookmark-spec).
66  *
67  * The syntax of the bookmark files is described in detail inside the
68  * Desktop Bookmark Specification, here is a quick summary: bookmark
69  * files use a sub-class of the XML Bookmark Exchange Language
70  * specification, consisting of valid UTF-8 encoded XML, under the
71  * <xbel> root element; each bookmark is stored inside a
72  * <bookmark> element, using its URI: no relative paths can
73  * be used inside a bookmark file. The bookmark may have a user defined
74  * title and description, to be used instead of the URI. Under the
75  * <metadata> element, with its owner attribute set to
76  * `http://freedesktop.org`, is stored the meta-data about a resource
77  * pointed by its URI. The meta-data consists of the resource's MIME
78  * type; the applications that have registered a bookmark; the groups
79  * to which a bookmark belongs to; a visibility flag, used to set the
80  * bookmark as "private" to the applications and groups that has it
81  * registered; the URI and MIME type of an icon, to be used when
82  * displaying the bookmark inside a GUI.
83  *
84  * Here is an example of a bookmark file:
85  * [bookmarks.xbel](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/glib/tests/bookmarks.xbel)
86  *
87  * A bookmark file might contain more than one bookmark; each bookmark
88  * is accessed through its URI.
89  *
90  * The important caveat of bookmark files is that when you add a new
91  * bookmark you must also add the application that is registering it, using
92  * g_bookmark_file_add_application() or g_bookmark_file_set_application_info().
93  * If a bookmark has no applications then it won't be dumped when creating
94  * the on disk representation, using g_bookmark_file_to_data() or
95  * g_bookmark_file_to_file().
96  *
97  * The #GBookmarkFile parser was added in GLib 2.12.
98  */
99
100 /* XBEL 1.0 standard entities */
101 #define XBEL_VERSION            "1.0"
102 #define XBEL_DTD_NICK           "xbel"
103 #define XBEL_DTD_SYSTEM         "+//IDN python.org//DTD XML Bookmark " \
104                                 "Exchange Language 1.0//EN//XML"
105
106 #define XBEL_DTD_URI            "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
107
108 #define XBEL_ROOT_ELEMENT       "xbel"
109 #define XBEL_FOLDER_ELEMENT     "folder"        /* unused */
110 #define XBEL_BOOKMARK_ELEMENT   "bookmark"
111 #define XBEL_ALIAS_ELEMENT      "alias"         /* unused */
112 #define XBEL_SEPARATOR_ELEMENT  "separator"     /* unused */
113 #define XBEL_TITLE_ELEMENT      "title"
114 #define XBEL_DESC_ELEMENT       "desc"
115 #define XBEL_INFO_ELEMENT       "info"
116 #define XBEL_METADATA_ELEMENT   "metadata"
117
118 #define XBEL_VERSION_ATTRIBUTE  "version"
119 #define XBEL_FOLDED_ATTRIBUTE   "folded"        /* unused */
120 #define XBEL_OWNER_ATTRIBUTE    "owner"
121 #define XBEL_ADDED_ATTRIBUTE    "added"
122 #define XBEL_VISITED_ATTRIBUTE  "visited"
123 #define XBEL_MODIFIED_ATTRIBUTE "modified"
124 #define XBEL_ID_ATTRIBUTE       "id"
125 #define XBEL_HREF_ATTRIBUTE     "href"
126 #define XBEL_REF_ATTRIBUTE      "ref"           /* unused */
127
128 #define XBEL_YES_VALUE          "yes"
129 #define XBEL_NO_VALUE           "no"
130
131 /* Desktop bookmark spec entities */
132 #define BOOKMARK_METADATA_OWNER         "http://freedesktop.org"
133
134 #define BOOKMARK_NAMESPACE_NAME         "bookmark"
135 #define BOOKMARK_NAMESPACE_URI          "http://www.freedesktop.org/standards/desktop-bookmarks"
136
137 #define BOOKMARK_GROUPS_ELEMENT         "groups"
138 #define BOOKMARK_GROUP_ELEMENT          "group"
139 #define BOOKMARK_APPLICATIONS_ELEMENT   "applications"
140 #define BOOKMARK_APPLICATION_ELEMENT    "application"
141 #define BOOKMARK_ICON_ELEMENT           "icon"
142 #define BOOKMARK_PRIVATE_ELEMENT        "private"
143
144 #define BOOKMARK_NAME_ATTRIBUTE         "name"
145 #define BOOKMARK_EXEC_ATTRIBUTE         "exec"
146 #define BOOKMARK_COUNT_ATTRIBUTE        "count"
147 #define BOOKMARK_TIMESTAMP_ATTRIBUTE    "timestamp"     /* deprecated by "modified" */
148 #define BOOKMARK_MODIFIED_ATTRIBUTE     "modified"
149 #define BOOKMARK_HREF_ATTRIBUTE         "href"
150 #define BOOKMARK_TYPE_ATTRIBUTE         "type"
151
152 /* Shared MIME Info entities */
153 #define MIME_NAMESPACE_NAME             "mime"
154 #define MIME_NAMESPACE_URI              "http://www.freedesktop.org/standards/shared-mime-info"
155 #define MIME_TYPE_ELEMENT               "mime-type"
156 #define MIME_TYPE_ATTRIBUTE             "type"
157
158
159 typedef struct _BookmarkAppInfo  BookmarkAppInfo;
160 typedef struct _BookmarkMetadata BookmarkMetadata;
161 typedef struct _BookmarkItem     BookmarkItem;
162 typedef struct _ParseData        ParseData;
163
164 struct _BookmarkAppInfo
165 {
166   gchar *name;
167   gchar *exec;
168
169   guint count;
170
171   GDateTime *stamp;  /* (owned) */
172 };
173
174 struct _BookmarkMetadata
175 {
176   gchar *mime_type;
177
178   GList *groups;
179
180   GList *applications;
181   GHashTable *apps_by_name;
182
183   gchar *icon_href;
184   gchar *icon_mime;
185
186   guint is_private : 1;
187 };
188
189 struct _BookmarkItem
190 {
191   gchar *uri;
192
193   gchar *title;
194   gchar *description;
195
196   GDateTime *added;  /* (owned) */
197   GDateTime *modified;  /* (owned) */
198   GDateTime *visited;  /* (owned) */
199
200   BookmarkMetadata *metadata;
201 };
202
203 struct _GBookmarkFile
204 {
205   gchar *title;
206   gchar *description;
207
208   /* we store our items in a list and keep a copy inside
209    * a hash table for faster lookup performances
210    */
211   GList *items;
212   GHashTable *items_by_uri;
213 };
214
215 /* parser state machine */
216 typedef enum
217 {
218   STATE_STARTED        = 0,
219
220   STATE_ROOT,
221   STATE_BOOKMARK,
222   STATE_TITLE,
223   STATE_DESC,
224   STATE_INFO,
225   STATE_METADATA,
226   STATE_APPLICATIONS,
227   STATE_APPLICATION,
228   STATE_GROUPS,
229   STATE_GROUP,
230   STATE_MIME,
231   STATE_ICON,
232
233   STATE_FINISHED
234 } ParserState;
235
236 static void          g_bookmark_file_init        (GBookmarkFile  *bookmark);
237 static void          g_bookmark_file_clear       (GBookmarkFile  *bookmark);
238 static gboolean      g_bookmark_file_parse       (GBookmarkFile  *bookmark,
239                                                   const gchar    *buffer,
240                                                   gsize           length,
241                                                   GError        **error);
242 static gchar *       g_bookmark_file_dump        (GBookmarkFile  *bookmark,
243                                                   gsize          *length,
244                                                   GError        **error);
245 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile  *bookmark,
246                                                   const gchar    *uri);
247 static void          g_bookmark_file_add_item    (GBookmarkFile  *bookmark,
248                                                   BookmarkItem   *item,
249                                                   GError        **error);
250
251 static gboolean timestamp_from_iso8601 (const gchar  *iso_date,
252                                         GDateTime   **out_date_time,
253                                         GError      **error);
254
255 /********************************
256  * BookmarkAppInfo              *
257  *                              *
258  * Application metadata storage *
259  ********************************/
260 static BookmarkAppInfo *
261 bookmark_app_info_new (const gchar *name)
262 {
263   BookmarkAppInfo *retval;
264
265   g_warn_if_fail (name != NULL);
266
267   retval = g_slice_new (BookmarkAppInfo);
268
269   retval->name = g_strdup (name);
270   retval->exec = NULL;
271   retval->count = 0;
272   retval->stamp = NULL;
273
274   return retval;
275 }
276
277 static void
278 bookmark_app_info_free (BookmarkAppInfo *app_info)
279 {
280   if (!app_info)
281     return;
282
283   g_free (app_info->name);
284   g_free (app_info->exec);
285   g_clear_pointer (&app_info->stamp, g_date_time_unref);
286
287   g_slice_free (BookmarkAppInfo, app_info);
288 }
289
290 static gchar *
291 bookmark_app_info_dump (BookmarkAppInfo *app_info)
292 {
293   gchar *retval;
294   gchar *name, *exec, *modified, *count;
295
296   g_warn_if_fail (app_info != NULL);
297
298   if (app_info->count == 0)
299     return NULL;
300
301   name = g_markup_escape_text (app_info->name, -1);
302   exec = g_markup_escape_text (app_info->exec, -1);
303   modified = g_date_time_format_iso8601 (app_info->stamp);
304   count = g_strdup_printf ("%u", app_info->count);
305
306   retval = g_strconcat ("          "
307                         "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
308                         " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
309                         " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\""
310                         " " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", modified, "\""
311                         " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
312                         NULL);
313
314   g_free (name);
315   g_free (exec);
316   g_free (modified);
317   g_free (count);
318
319   return retval;
320 }
321
322
323 /***********************
324  * BookmarkMetadata    *
325  *                     *
326  * Metadata storage    *
327  ***********************/
328 static BookmarkMetadata *
329 bookmark_metadata_new (void)
330 {
331   BookmarkMetadata *retval;
332
333   retval = g_slice_new (BookmarkMetadata);
334
335   retval->mime_type = NULL;
336
337   retval->groups = NULL;
338
339   retval->applications = NULL;
340   retval->apps_by_name = g_hash_table_new_full (g_str_hash,
341                                                 g_str_equal,
342                                                 NULL,
343                                                 NULL);
344
345   retval->is_private = FALSE;
346
347   retval->icon_href = NULL;
348   retval->icon_mime = NULL;
349
350   return retval;
351 }
352
353 static void
354 bookmark_metadata_free (BookmarkMetadata *metadata)
355 {
356   if (!metadata)
357     return;
358
359   g_free (metadata->mime_type);
360
361   g_list_free_full (metadata->groups, g_free);
362   g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
363
364   g_hash_table_destroy (metadata->apps_by_name);
365
366   g_free (metadata->icon_href);
367   g_free (metadata->icon_mime);
368
369   g_slice_free (BookmarkMetadata, metadata);
370 }
371
372 static gchar *
373 bookmark_metadata_dump (BookmarkMetadata *metadata)
374 {
375   GString *retval;
376   gchar *buffer;
377
378   if (!metadata->applications)
379     return NULL;
380
381   retval = g_string_sized_new (1024);
382
383   /* metadata container */
384   g_string_append (retval,
385                    "      "
386                    "<" XBEL_METADATA_ELEMENT
387                    " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
388                    "\">\n");
389
390   /* mime type */
391   if (metadata->mime_type) {
392     buffer = g_strconcat ("        "
393                           "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
394                           MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
395                           NULL);
396     g_string_append (retval, buffer);
397     g_free (buffer);
398   }
399
400   if (metadata->groups)
401     {
402       GList *l;
403
404       /* open groups container */
405       g_string_append (retval,
406                        "        "
407                        "<" BOOKMARK_NAMESPACE_NAME
408                        ":" BOOKMARK_GROUPS_ELEMENT ">\n");
409
410       for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
411         {
412           gchar *group_name;
413
414           group_name = g_markup_escape_text ((gchar *) l->data, -1);
415           buffer = g_strconcat ("          "
416                                 "<" BOOKMARK_NAMESPACE_NAME
417                                 ":" BOOKMARK_GROUP_ELEMENT ">",
418                                 group_name,
419                                 "</" BOOKMARK_NAMESPACE_NAME
420                                 ":"  BOOKMARK_GROUP_ELEMENT ">\n", NULL);
421           g_string_append (retval, buffer);
422
423           g_free (buffer);
424           g_free (group_name);
425         }
426
427       /* close groups container */
428       g_string_append (retval,
429                        "        "
430                        "</" BOOKMARK_NAMESPACE_NAME
431                        ":" BOOKMARK_GROUPS_ELEMENT ">\n");
432     }
433
434   if (metadata->applications)
435     {
436       GList *l;
437
438       /* open applications container */
439       g_string_append (retval,
440                        "        "
441                        "<" BOOKMARK_NAMESPACE_NAME
442                        ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
443
444       for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
445         {
446           BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
447           gchar *app_data;
448
449           g_warn_if_fail (app_info != NULL);
450
451           app_data = bookmark_app_info_dump (app_info);
452
453           if (app_data)
454             {
455               retval = g_string_append (retval, app_data);
456
457               g_free (app_data);
458             }
459         }
460
461       /* close applications container */
462       g_string_append (retval,
463                        "        "
464                        "</" BOOKMARK_NAMESPACE_NAME
465                        ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
466     }
467
468   /* icon */
469   if (metadata->icon_href)
470     {
471       if (!metadata->icon_mime)
472         metadata->icon_mime = g_strdup ("application/octet-stream");
473
474       buffer = g_strconcat ("       "
475                             "<" BOOKMARK_NAMESPACE_NAME
476                             ":" BOOKMARK_ICON_ELEMENT
477                             " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
478                             "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
479       g_string_append (retval, buffer);
480
481       g_free (buffer);
482     }
483
484   /* private hint */
485   if (metadata->is_private)
486     g_string_append (retval,
487                      "        "
488                      "<" BOOKMARK_NAMESPACE_NAME
489                      ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
490
491   /* close metadata container */
492   g_string_append (retval,
493                    "      "
494                    "</" XBEL_METADATA_ELEMENT ">\n");
495
496   return g_string_free (retval, FALSE);
497 }
498
499 /******************************************************
500  * BookmarkItem                                       *
501  *                                                    *
502  * Storage for a single bookmark item inside the list *
503  ******************************************************/
504 static BookmarkItem *
505 bookmark_item_new (const gchar *uri)
506 {
507   BookmarkItem *item;
508
509   g_warn_if_fail (uri != NULL);
510
511   item = g_slice_new (BookmarkItem);
512   item->uri = g_strdup (uri);
513
514   item->title = NULL;
515   item->description = NULL;
516
517   item->added = NULL;
518   item->modified = NULL;
519   item->visited = NULL;
520
521   item->metadata = NULL;
522
523   return item;
524 }
525
526 static void
527 bookmark_item_free (BookmarkItem *item)
528 {
529   if (!item)
530     return;
531
532   g_free (item->uri);
533   g_free (item->title);
534   g_free (item->description);
535
536   if (item->metadata)
537     bookmark_metadata_free (item->metadata);
538
539   g_clear_pointer (&item->added, g_date_time_unref);
540   g_clear_pointer (&item->modified, g_date_time_unref);
541   g_clear_pointer (&item->visited, g_date_time_unref);
542
543   g_slice_free (BookmarkItem, item);
544 }
545
546 static void
547 bookmark_item_touch_modified (BookmarkItem *item)
548 {
549   g_clear_pointer (&item->modified, g_date_time_unref);
550   item->modified = g_date_time_new_now_utc ();
551 }
552
553 static gchar *
554 bookmark_item_dump (BookmarkItem *item)
555 {
556   GString *retval;
557   gchar *escaped_uri;
558
559   /* at this point, we must have at least a registered application; if we don't
560    * we don't screw up the bookmark file, and just skip this item
561    */
562   if (!item->metadata || !item->metadata->applications)
563     {
564       g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri);
565       return NULL;
566     }
567
568   retval = g_string_sized_new (4096);
569
570   g_string_append (retval, "  <" XBEL_BOOKMARK_ELEMENT " ");
571
572   escaped_uri = g_markup_escape_text (item->uri, -1);
573
574   g_string_append (retval, XBEL_HREF_ATTRIBUTE "=\"");
575   g_string_append (retval, escaped_uri);
576   g_string_append (retval , "\" ");
577
578   g_free (escaped_uri);
579
580   if (item->added)
581     {
582       char *added;
583
584       added = g_date_time_format_iso8601 (item->added);
585       g_string_append (retval, XBEL_ADDED_ATTRIBUTE "=\"");
586       g_string_append (retval, added);
587       g_string_append (retval, "\" ");
588       g_free (added);
589     }
590
591   if (item->modified)
592     {
593       char *modified;
594
595       modified = g_date_time_format_iso8601 (item->modified);
596       g_string_append (retval, XBEL_MODIFIED_ATTRIBUTE "=\"");
597       g_string_append (retval, modified);
598       g_string_append (retval, "\" ");
599       g_free (modified);
600     }
601
602   if (item->visited)
603     {
604       char *visited;
605
606       visited = g_date_time_format_iso8601 (item->visited);
607       g_string_append (retval, XBEL_VISITED_ATTRIBUTE "=\"");
608       g_string_append (retval, visited);
609       g_string_append (retval, "\" ");
610       g_free (visited);
611     }
612
613   if (retval->str[retval->len - 1] == ' ')
614     g_string_truncate (retval, retval->len - 1);
615   g_string_append (retval, ">\n");
616
617   if (item->title)
618     {
619       gchar *escaped_title;
620
621       escaped_title = g_markup_escape_text (item->title, -1);
622       g_string_append (retval, "    " "<" XBEL_TITLE_ELEMENT ">");
623       g_string_append (retval, escaped_title);
624       g_string_append (retval, "</" XBEL_TITLE_ELEMENT ">\n");
625
626       g_free (escaped_title);
627     }
628
629   if (item->description)
630     {
631       gchar *escaped_desc;
632
633       escaped_desc = g_markup_escape_text (item->description, -1);
634       g_string_append (retval, "    " "<" XBEL_DESC_ELEMENT ">");
635       g_string_append (retval, escaped_desc);
636       g_string_append (retval, "</" XBEL_DESC_ELEMENT ">\n");
637
638       g_free (escaped_desc);
639     }
640
641   if (item->metadata)
642     {
643       gchar *metadata;
644
645       metadata = bookmark_metadata_dump (item->metadata);
646       if (metadata)
647         {
648           g_string_append (retval, "    " "<" XBEL_INFO_ELEMENT ">\n");
649           g_string_append (retval, metadata);
650           g_string_append (retval, "    " "</" XBEL_INFO_ELEMENT ">\n");
651
652           g_free (metadata);
653         }
654     }
655
656   g_string_append (retval, "  </" XBEL_BOOKMARK_ELEMENT ">\n");
657
658   return g_string_free (retval, FALSE);
659 }
660
661 static BookmarkAppInfo *
662 bookmark_item_lookup_app_info (BookmarkItem *item,
663                                const gchar  *app_name)
664 {
665   g_warn_if_fail (item != NULL && app_name != NULL);
666
667   if (!item->metadata)
668     return NULL;
669
670   return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
671 }
672
673 /*************************
674  *    GBookmarkFile    *
675  *************************/
676
677 static void
678 g_bookmark_file_init (GBookmarkFile *bookmark)
679 {
680   bookmark->title = NULL;
681   bookmark->description = NULL;
682
683   bookmark->items = NULL;
684   bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
685                                                   g_str_equal,
686                                                   NULL,
687                                                   NULL);
688 }
689
690 static void
691 g_bookmark_file_clear (GBookmarkFile *bookmark)
692 {
693   g_free (bookmark->title);
694   g_free (bookmark->description);
695
696   g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
697   bookmark->items = NULL;
698
699   if (bookmark->items_by_uri)
700     {
701       g_hash_table_destroy (bookmark->items_by_uri);
702
703       bookmark->items_by_uri = NULL;
704     }
705 }
706
707 struct _ParseData
708 {
709   ParserState state;
710
711   GHashTable *namespaces;
712
713   GBookmarkFile *bookmark_file;
714   BookmarkItem *current_item;
715 };
716
717 static ParseData *
718 parse_data_new (void)
719 {
720   ParseData *retval;
721
722   retval = g_new (ParseData, 1);
723
724   retval->state = STATE_STARTED;
725   retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
726                                               (GDestroyNotify) g_free,
727                                               (GDestroyNotify) g_free);
728   retval->bookmark_file = NULL;
729   retval->current_item = NULL;
730
731   return retval;
732 }
733
734 static void
735 parse_data_free (ParseData *parse_data)
736 {
737   g_hash_table_destroy (parse_data->namespaces);
738
739   g_free (parse_data);
740 }
741
742 #define IS_ATTRIBUTE(s,a)       ((0 == strcmp ((s), (a))))
743
744 static void
745 parse_bookmark_element (GMarkupParseContext  *context,
746                         ParseData            *parse_data,
747                         const gchar         **attribute_names,
748                         const gchar         **attribute_values,
749                         GError              **error)
750 {
751   const gchar *uri, *added, *modified, *visited;
752   const gchar *attr;
753   gint i;
754   BookmarkItem *item;
755   GError *add_error;
756
757   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
758
759   i = 0;
760   uri = added = modified = visited = NULL;
761   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
762     {
763       if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
764         uri = attribute_values[i];
765       else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
766         added = attribute_values[i];
767       else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
768         modified = attribute_values[i];
769       else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
770         visited = attribute_values[i];
771       else
772         {
773           /* bookmark is defined by the XBEL spec, so we need
774            * to error out if the element has different or
775            * missing attributes
776            */
777           g_set_error (error, G_MARKUP_ERROR,
778                        G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
779                        _("Unexpected attribute “%s” for element “%s”"),
780                        attr,
781                        XBEL_BOOKMARK_ELEMENT);
782           return;
783         }
784     }
785
786   if (!uri)
787     {
788       g_set_error (error, G_MARKUP_ERROR,
789                    G_MARKUP_ERROR_INVALID_CONTENT,
790                    _("Attribute “%s” of element “%s” not found"),
791                    XBEL_HREF_ATTRIBUTE,
792                    XBEL_BOOKMARK_ELEMENT);
793       return;
794     }
795
796   g_warn_if_fail (parse_data->current_item == NULL);
797
798   item = bookmark_item_new (uri);
799
800   if (added != NULL && !timestamp_from_iso8601 (added, &item->added, error))
801     {
802       bookmark_item_free (item);
803       return;
804     }
805
806   if (modified != NULL && !timestamp_from_iso8601 (modified, &item->modified, error))
807     {
808       bookmark_item_free (item);
809       return;
810     }
811
812   if (visited != NULL && !timestamp_from_iso8601 (visited, &item->visited, error))
813     {
814       bookmark_item_free (item);
815       return;
816     }
817
818   add_error = NULL;
819   g_bookmark_file_add_item (parse_data->bookmark_file,
820                             item,
821                             &add_error);
822   if (add_error)
823     {
824       bookmark_item_free (item);
825
826       g_propagate_error (error, add_error);
827
828       return;
829     }
830
831   parse_data->current_item = item;
832 }
833
834 static void
835 parse_application_element (GMarkupParseContext  *context,
836                            ParseData            *parse_data,
837                            const gchar         **attribute_names,
838                            const gchar         **attribute_values,
839                            GError              **error)
840 {
841   const gchar *name, *exec, *count, *stamp, *modified;
842   const gchar *attr;
843   gint i;
844   BookmarkItem *item;
845   BookmarkAppInfo *ai;
846
847   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
848
849   i = 0;
850   name = exec = count = stamp = modified = NULL;
851   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
852     {
853       if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
854         name = attribute_values[i];
855       else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
856         exec = attribute_values[i];
857       else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
858         count = attribute_values[i];
859       else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
860         stamp = attribute_values[i];
861       else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
862         modified = attribute_values[i];
863     }
864
865   /* the "name" and "exec" attributes are mandatory */
866   if (!name)
867     {
868       g_set_error (error, G_MARKUP_ERROR,
869                    G_MARKUP_ERROR_INVALID_CONTENT,
870                    _("Attribute “%s” of element “%s” not found"),
871                    BOOKMARK_NAME_ATTRIBUTE,
872                    BOOKMARK_APPLICATION_ELEMENT);
873       return;
874     }
875
876   if (!exec)
877     {
878       g_set_error (error, G_MARKUP_ERROR,
879                    G_MARKUP_ERROR_INVALID_CONTENT,
880                    _("Attribute “%s” of element “%s” not found"),
881                    BOOKMARK_EXEC_ATTRIBUTE,
882                    BOOKMARK_APPLICATION_ELEMENT);
883       return;
884     }
885
886   g_warn_if_fail (parse_data->current_item != NULL);
887   item = parse_data->current_item;
888
889   ai = bookmark_item_lookup_app_info (item, name);
890   if (!ai)
891     {
892       ai = bookmark_app_info_new (name);
893
894       if (!item->metadata)
895         item->metadata = bookmark_metadata_new ();
896
897       item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
898       g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
899     }
900
901   g_free (ai->exec);
902   ai->exec = g_strdup (exec);
903
904   if (count)
905     ai->count = atoi (count);
906   else
907     ai->count = 1;
908
909   g_clear_pointer (&ai->stamp, g_date_time_unref);
910   if (modified != NULL)
911     {
912       if (!timestamp_from_iso8601 (modified, &ai->stamp, error))
913         return;
914     }
915   else
916     {
917       /* the timestamp attribute has been deprecated but we still parse
918        * it for backward compatibility
919        */
920       if (stamp)
921         ai->stamp = g_date_time_new_from_unix_utc (atol (stamp));
922       else
923         ai->stamp = g_date_time_new_now_utc ();
924     }
925 }
926
927 static void
928 parse_mime_type_element (GMarkupParseContext  *context,
929                          ParseData            *parse_data,
930                          const gchar         **attribute_names,
931                          const gchar         **attribute_values,
932                          GError              **error)
933 {
934   const gchar *type;
935   const gchar *attr;
936   gint i;
937   BookmarkItem *item;
938
939   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
940
941   i = 0;
942   type = NULL;
943   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
944     {
945       if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
946         type = attribute_values[i];
947     }
948
949   if (!type)
950     type = "application/octet-stream";
951
952   g_warn_if_fail (parse_data->current_item != NULL);
953   item = parse_data->current_item;
954
955   if (!item->metadata)
956     item->metadata = bookmark_metadata_new ();
957
958   g_free (item->metadata->mime_type);
959   item->metadata->mime_type = g_strdup (type);
960 }
961
962 static void
963 parse_icon_element (GMarkupParseContext  *context,
964                     ParseData            *parse_data,
965                     const gchar         **attribute_names,
966                     const gchar         **attribute_values,
967                     GError              **error)
968 {
969   const gchar *href;
970   const gchar *type;
971   const gchar *attr;
972   gint i;
973   BookmarkItem *item;
974
975   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
976
977   i = 0;
978   href = NULL;
979   type = NULL;
980   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
981     {
982       if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
983         href = attribute_values[i];
984       else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
985         type = attribute_values[i];
986     }
987
988   /* the "href" attribute is mandatory */
989   if (!href)
990     {
991       g_set_error (error, G_MARKUP_ERROR,
992                    G_MARKUP_ERROR_INVALID_CONTENT,
993                    _("Attribute “%s” of element “%s” not found"),
994                    BOOKMARK_HREF_ATTRIBUTE,
995                    BOOKMARK_ICON_ELEMENT);
996       return;
997     }
998
999   if (!type)
1000     type = "application/octet-stream";
1001
1002   g_warn_if_fail (parse_data->current_item != NULL);
1003   item = parse_data->current_item;
1004
1005   if (!item->metadata)
1006     item->metadata = bookmark_metadata_new ();
1007
1008   g_free (item->metadata->icon_href);
1009   g_free (item->metadata->icon_mime);
1010   item->metadata->icon_href = g_strdup (href);
1011   item->metadata->icon_mime = g_strdup (type);
1012 }
1013
1014 /* scans through the attributes of an element for the "xmlns" pragma, and
1015  * adds any resulting namespace declaration to a per-parser hashtable, using
1016  * the namespace name as a key for the namespace URI; if no key was found,
1017  * the namespace is considered as default, and stored under the "default" key.
1018  *
1019  * FIXME: this works on the assumption that the generator of the XBEL file
1020  * is either this code or is smart enough to place the namespace declarations
1021  * inside the main root node or inside the metadata node and does not redefine
1022  * a namespace inside an inner node; this does *not* conform to the
1023  * XML-NS standard, although is a close approximation.  In order to make this
1024  * conformant to the XML-NS specification we should use a per-element
1025  * namespace table inside GMarkup and ask it to resolve the namespaces for us.
1026  */
1027 static void
1028 map_namespace_to_name (ParseData    *parse_data,
1029                        const gchar **attribute_names,
1030                        const gchar **attribute_values)
1031 {
1032   const gchar *attr;
1033   gint i;
1034
1035   g_warn_if_fail (parse_data != NULL);
1036
1037   if (!attribute_names || !attribute_names[0])
1038     return;
1039
1040   i = 0;
1041   for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1042     {
1043       if (g_str_has_prefix (attr, "xmlns"))
1044         {
1045           gchar *namespace_name, *namespace_uri;
1046           gchar *p;
1047
1048           p = g_utf8_strchr (attr, -1, ':');
1049           if (p)
1050             p = g_utf8_next_char (p);
1051           else
1052             p = "default";
1053
1054           namespace_name = g_strdup (p);
1055           namespace_uri = g_strdup (attribute_values[i]);
1056
1057           g_hash_table_replace (parse_data->namespaces,
1058                                 namespace_name,
1059                                 namespace_uri);
1060         }
1061      }
1062 }
1063
1064 /* checks whether @element_full is equal to @element.
1065  *
1066  * if @namespace is set, it tries to resolve the namespace to a known URI,
1067  * and if found is prepended to the element name, from which is separated
1068  * using the character specified in the @sep parameter.
1069  */
1070 static gboolean
1071 is_element_full (ParseData   *parse_data,
1072                  const gchar *element_full,
1073                  const gchar *namespace,
1074                  const gchar *element,
1075                  const gchar  sep)
1076 {
1077   gchar *ns_uri, *ns_name;
1078   const gchar *p, *element_name;
1079   gboolean retval;
1080
1081   g_warn_if_fail (parse_data != NULL);
1082   g_warn_if_fail (element_full != NULL);
1083
1084   if (!element)
1085     return FALSE;
1086
1087   /* no namespace requested: dumb element compare */
1088   if (!namespace)
1089     return (0 == strcmp (element_full, element));
1090
1091   /* search for namespace separator; if none found, assume we are under the
1092    * default namespace, and set ns_name to our "default" marker; if no default
1093    * namespace has been set, just do a plain comparison between @full_element
1094    * and @element.
1095    */
1096   p = g_utf8_strchr (element_full, -1, ':');
1097   if (p)
1098     {
1099       ns_name = g_strndup (element_full, p - element_full);
1100       element_name = g_utf8_next_char (p);
1101     }
1102   else
1103     {
1104       ns_name = g_strdup ("default");
1105       element_name = element_full;
1106     }
1107
1108   ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1109   if (!ns_uri)
1110     {
1111       /* no default namespace found */
1112       g_free (ns_name);
1113
1114       return (0 == strcmp (element_full, element));
1115     }
1116
1117   retval = (0 == strcmp (ns_uri, namespace) &&
1118             0 == strcmp (element_name, element));
1119
1120   g_free (ns_name);
1121
1122   return retval;
1123 }
1124
1125 #define IS_ELEMENT(p,s,e)       (is_element_full ((p), (s), NULL, (e), '\0'))
1126 #define IS_ELEMENT_NS(p,s,n,e)  (is_element_full ((p), (s), (n), (e), '|'))
1127
1128 static const gchar *
1129 parser_state_to_element_name (ParserState state)
1130 {
1131   switch (state)
1132     {
1133     case STATE_STARTED:
1134     case STATE_FINISHED:
1135       return "(top-level)";
1136     case STATE_ROOT:
1137       return XBEL_ROOT_ELEMENT;
1138     case STATE_BOOKMARK:
1139       return XBEL_BOOKMARK_ELEMENT;
1140     case STATE_TITLE:
1141       return XBEL_TITLE_ELEMENT;
1142     case STATE_DESC:
1143       return XBEL_DESC_ELEMENT;
1144     case STATE_INFO:
1145       return XBEL_INFO_ELEMENT;
1146     case STATE_METADATA:
1147       return XBEL_METADATA_ELEMENT;
1148     case STATE_APPLICATIONS:
1149       return BOOKMARK_APPLICATIONS_ELEMENT;
1150     case STATE_APPLICATION:
1151       return BOOKMARK_APPLICATION_ELEMENT;
1152     case STATE_GROUPS:
1153       return BOOKMARK_GROUPS_ELEMENT;
1154     case STATE_GROUP:
1155       return BOOKMARK_GROUP_ELEMENT;
1156     case STATE_MIME:
1157       return MIME_TYPE_ELEMENT;
1158     case STATE_ICON:
1159       return BOOKMARK_ICON_ELEMENT;
1160     default:
1161       g_assert_not_reached ();
1162     }
1163 }
1164
1165 static void
1166 start_element_raw_cb (GMarkupParseContext *context,
1167                       const gchar         *element_name,
1168                       const gchar        **attribute_names,
1169                       const gchar        **attribute_values,
1170                       gpointer             user_data,
1171                       GError             **error)
1172 {
1173   ParseData *parse_data = (ParseData *) user_data;
1174
1175   /* we must check for namespace declarations first
1176    *
1177    * XXX - we could speed up things by checking for namespace declarations
1178    * only on the root node, where they usually are; this would probably break
1179    * on streams not produced by us or by "smart" generators
1180    */
1181   map_namespace_to_name (parse_data, attribute_names, attribute_values);
1182
1183   switch (parse_data->state)
1184     {
1185     case STATE_STARTED:
1186       if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1187         {
1188           const gchar *attr;
1189           gint i;
1190
1191           i = 0;
1192           for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1193             {
1194               if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1195                   (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1196                 parse_data->state = STATE_ROOT;
1197             }
1198         }
1199       else
1200         g_set_error (error, G_MARKUP_ERROR,
1201                      G_MARKUP_ERROR_INVALID_CONTENT,
1202                      _("Unexpected tag “%s”, tag “%s” expected"),
1203                      element_name, XBEL_ROOT_ELEMENT);
1204       break;
1205     case STATE_ROOT:
1206       if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1207         parse_data->state = STATE_TITLE;
1208       else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1209         parse_data->state = STATE_DESC;
1210       else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1211         {
1212           GError *inner_error = NULL;
1213
1214           parse_data->state = STATE_BOOKMARK;
1215
1216           parse_bookmark_element (context,
1217                                   parse_data,
1218                                   attribute_names,
1219                                   attribute_values,
1220                                   &inner_error);
1221           if (inner_error)
1222             g_propagate_error (error, inner_error);
1223         }
1224       else
1225         g_set_error (error, G_MARKUP_ERROR,
1226                      G_MARKUP_ERROR_INVALID_CONTENT,
1227                      _("Unexpected tag “%s” inside “%s”"),
1228                      element_name,
1229                      XBEL_ROOT_ELEMENT);
1230       break;
1231     case STATE_BOOKMARK:
1232       if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1233         parse_data->state = STATE_TITLE;
1234       else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1235         parse_data->state = STATE_DESC;
1236       else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1237         parse_data->state = STATE_INFO;
1238       else
1239         g_set_error (error, G_MARKUP_ERROR,
1240                      G_MARKUP_ERROR_INVALID_CONTENT,
1241                      _("Unexpected tag “%s” inside “%s”"),
1242                      element_name,
1243                      XBEL_BOOKMARK_ELEMENT);
1244       break;
1245     case STATE_INFO:
1246       if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1247         {
1248           const gchar *attr;
1249           gint i;
1250
1251           i = 0;
1252           for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1253             {
1254               if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1255                   (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1256                 {
1257                   parse_data->state = STATE_METADATA;
1258
1259                   if (!parse_data->current_item->metadata)
1260                     parse_data->current_item->metadata = bookmark_metadata_new ();
1261                 }
1262             }
1263         }
1264       else
1265         g_set_error (error, G_MARKUP_ERROR,
1266                      G_MARKUP_ERROR_INVALID_CONTENT,
1267                      _("Unexpected tag “%s”, tag “%s” expected"),
1268                      element_name,
1269                      XBEL_METADATA_ELEMENT);
1270       break;
1271     case STATE_METADATA:
1272       if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1273         parse_data->state = STATE_APPLICATIONS;
1274       else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1275         parse_data->state = STATE_GROUPS;
1276       else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1277         parse_data->current_item->metadata->is_private = TRUE;
1278       else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1279         {
1280           GError *inner_error = NULL;
1281
1282           parse_data->state = STATE_ICON;
1283
1284           parse_icon_element (context,
1285                               parse_data,
1286                               attribute_names,
1287                               attribute_values,
1288                               &inner_error);
1289           if (inner_error)
1290             g_propagate_error (error, inner_error);
1291         }
1292       else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1293         {
1294           GError *inner_error = NULL;
1295
1296           parse_data->state = STATE_MIME;
1297
1298           parse_mime_type_element (context,
1299                                    parse_data,
1300                                    attribute_names,
1301                                    attribute_values,
1302                                    &inner_error);
1303           if (inner_error)
1304             g_propagate_error (error, inner_error);
1305         }
1306       else
1307         g_set_error (error, G_MARKUP_ERROR,
1308                      G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1309                      _("Unexpected tag “%s” inside “%s”"),
1310                      element_name,
1311                      XBEL_METADATA_ELEMENT);
1312       break;
1313     case STATE_APPLICATIONS:
1314       if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1315         {
1316           GError *inner_error = NULL;
1317
1318           parse_data->state = STATE_APPLICATION;
1319
1320           parse_application_element (context,
1321                                      parse_data,
1322                                      attribute_names,
1323                                      attribute_values,
1324                                      &inner_error);
1325           if (inner_error)
1326             g_propagate_error (error, inner_error);
1327         }
1328       else
1329         g_set_error (error, G_MARKUP_ERROR,
1330                      G_MARKUP_ERROR_INVALID_CONTENT,
1331                      _("Unexpected tag “%s”, tag “%s” expected"),
1332                      element_name,
1333                      BOOKMARK_APPLICATION_ELEMENT);
1334       break;
1335     case STATE_GROUPS:
1336       if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1337         parse_data->state = STATE_GROUP;
1338       else
1339         g_set_error (error, G_MARKUP_ERROR,
1340                      G_MARKUP_ERROR_INVALID_CONTENT,
1341                      _("Unexpected tag “%s”, tag “%s” expected"),
1342                      element_name,
1343                      BOOKMARK_GROUP_ELEMENT);
1344       break;
1345
1346     case STATE_TITLE:
1347     case STATE_DESC:
1348     case STATE_APPLICATION:
1349     case STATE_GROUP:
1350     case STATE_MIME:
1351     case STATE_ICON:
1352     case STATE_FINISHED:
1353       g_set_error (error, G_MARKUP_ERROR,
1354                    G_MARKUP_ERROR_INVALID_CONTENT,
1355                    _("Unexpected tag “%s” inside “%s”"),
1356                    element_name,
1357                    parser_state_to_element_name (parse_data->state));
1358       break;
1359
1360     default:
1361       g_assert_not_reached ();
1362       break;
1363     }
1364 }
1365
1366 static void
1367 end_element_raw_cb (GMarkupParseContext *context,
1368                     const gchar         *element_name,
1369                     gpointer             user_data,
1370                     GError             **error)
1371 {
1372   ParseData *parse_data = (ParseData *) user_data;
1373
1374   if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1375     parse_data->state = STATE_FINISHED;
1376   else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1377     {
1378       parse_data->current_item = NULL;
1379
1380       parse_data->state = STATE_ROOT;
1381     }
1382   else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1383            (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1384            (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1385     {
1386       if (parse_data->current_item)
1387         parse_data->state = STATE_BOOKMARK;
1388       else
1389         parse_data->state = STATE_ROOT;
1390     }
1391   else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1392     parse_data->state = STATE_INFO;
1393   else if (IS_ELEMENT_NS (parse_data, element_name,
1394                           BOOKMARK_NAMESPACE_URI,
1395                           BOOKMARK_APPLICATION_ELEMENT))
1396     parse_data->state = STATE_APPLICATIONS;
1397   else if (IS_ELEMENT_NS (parse_data, element_name,
1398                           BOOKMARK_NAMESPACE_URI,
1399                           BOOKMARK_GROUP_ELEMENT))
1400     parse_data->state = STATE_GROUPS;
1401   else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1402            (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1403            (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1404            (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1405            (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1406     parse_data->state = STATE_METADATA;
1407 }
1408
1409 static void
1410 text_raw_cb (GMarkupParseContext *context,
1411              const gchar         *text,
1412              gsize                length,
1413              gpointer             user_data,
1414              GError             **error)
1415 {
1416   ParseData *parse_data = (ParseData *) user_data;
1417   gchar *payload;
1418
1419   payload = g_strndup (text, length);
1420
1421   switch (parse_data->state)
1422     {
1423     case STATE_TITLE:
1424       if (parse_data->current_item)
1425         {
1426           g_free (parse_data->current_item->title);
1427           parse_data->current_item->title = g_strdup (payload);
1428         }
1429       else
1430         {
1431           g_free (parse_data->bookmark_file->title);
1432           parse_data->bookmark_file->title = g_strdup (payload);
1433         }
1434       break;
1435     case STATE_DESC:
1436       if (parse_data->current_item)
1437         {
1438           g_free (parse_data->current_item->description);
1439           parse_data->current_item->description = g_strdup (payload);
1440         }
1441       else
1442         {
1443           g_free (parse_data->bookmark_file->description);
1444           parse_data->bookmark_file->description = g_strdup (payload);
1445         }
1446       break;
1447     case STATE_GROUP:
1448       {
1449       GList *groups;
1450
1451       g_warn_if_fail (parse_data->current_item != NULL);
1452
1453       if (!parse_data->current_item->metadata)
1454         parse_data->current_item->metadata = bookmark_metadata_new ();
1455
1456       groups = parse_data->current_item->metadata->groups;
1457       parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1458       }
1459       break;
1460     case STATE_ROOT:
1461     case STATE_BOOKMARK:
1462     case STATE_INFO:
1463     case STATE_METADATA:
1464     case STATE_APPLICATIONS:
1465     case STATE_APPLICATION:
1466     case STATE_GROUPS:
1467     case STATE_MIME:
1468     case STATE_ICON:
1469       break;
1470     default:
1471       g_warn_if_reached ();
1472       break;
1473     }
1474
1475   g_free (payload);
1476 }
1477
1478 static const GMarkupParser markup_parser =
1479 {
1480   start_element_raw_cb, /* start_element */
1481   end_element_raw_cb,   /* end_element */
1482   text_raw_cb,          /* text */
1483   NULL,                 /* passthrough */
1484   NULL
1485 };
1486
1487 static gboolean
1488 g_bookmark_file_parse (GBookmarkFile  *bookmark,
1489                          const gchar  *buffer,
1490                          gsize         length,
1491                          GError       **error)
1492 {
1493   GMarkupParseContext *context;
1494   ParseData *parse_data;
1495   GError *parse_error, *end_error;
1496   gboolean retval;
1497
1498   g_warn_if_fail (bookmark != NULL);
1499
1500   if (!buffer)
1501     return FALSE;
1502
1503   parse_error = NULL;
1504   end_error = NULL;
1505
1506   if (length == (gsize) -1)
1507     length = strlen (buffer);
1508
1509   parse_data = parse_data_new ();
1510   parse_data->bookmark_file = bookmark;
1511
1512   context = g_markup_parse_context_new (&markup_parser,
1513                                         G_MARKUP_DEFAULT_FLAGS,
1514                                         parse_data,
1515                                         (GDestroyNotify) parse_data_free);
1516
1517   retval = g_markup_parse_context_parse (context,
1518                                          buffer,
1519                                          length,
1520                                          &parse_error);
1521   if (!retval)
1522     g_propagate_error (error, parse_error);
1523   else
1524    {
1525      retval = g_markup_parse_context_end_parse (context, &end_error);
1526       if (!retval)
1527         g_propagate_error (error, end_error);
1528    }
1529
1530   g_markup_parse_context_free (context);
1531
1532   return retval;
1533 }
1534
1535 static gchar *
1536 g_bookmark_file_dump (GBookmarkFile  *bookmark,
1537                       gsize          *length,
1538                       GError        **error)
1539 {
1540   GString *retval;
1541   gchar *buffer;
1542   GList *l;
1543
1544   retval = g_string_sized_new (4096);
1545
1546   g_string_append (retval,
1547                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1548 #if 0
1549                    /* XXX - do we really need the doctype? */
1550                    "<!DOCTYPE " XBEL_DTD_NICK "\n"
1551                    "  PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1552                    "         \"" XBEL_DTD_URI "\">\n"
1553 #endif
1554                    "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1555                    "      xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1556                    "      xmlns:" MIME_NAMESPACE_NAME     "=\"" MIME_NAMESPACE_URI "\"\n>");
1557
1558   if (bookmark->title)
1559     {
1560       gchar *escaped_title;
1561
1562       escaped_title = g_markup_escape_text (bookmark->title, -1);
1563
1564       buffer = g_strconcat ("  "
1565                             "<" XBEL_TITLE_ELEMENT ">",
1566                             escaped_title,
1567                             "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1568
1569       g_string_append (retval, buffer);
1570
1571       g_free (buffer);
1572       g_free (escaped_title);
1573     }
1574
1575   if (bookmark->description)
1576     {
1577       gchar *escaped_desc;
1578
1579       escaped_desc = g_markup_escape_text (bookmark->description, -1);
1580
1581       buffer = g_strconcat ("  "
1582                             "<" XBEL_DESC_ELEMENT ">",
1583                             escaped_desc,
1584                             "</" XBEL_DESC_ELEMENT ">\n", NULL);
1585       g_string_append (retval, buffer);
1586
1587       g_free (buffer);
1588       g_free (escaped_desc);
1589     }
1590
1591   if (!bookmark->items)
1592     goto out;
1593   else
1594     retval = g_string_append (retval, "\n");
1595
1596   /* the items are stored in reverse order */
1597   for (l = g_list_last (bookmark->items);
1598        l != NULL;
1599        l = l->prev)
1600     {
1601       BookmarkItem *item = (BookmarkItem *) l->data;
1602       gchar *item_dump;
1603
1604       item_dump = bookmark_item_dump (item);
1605       if (!item_dump)
1606         continue;
1607
1608       retval = g_string_append (retval, item_dump);
1609
1610       g_free (item_dump);
1611     }
1612
1613 out:
1614   g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1615
1616   if (length)
1617     *length = retval->len;
1618
1619   return g_string_free (retval, FALSE);
1620 }
1621
1622 /**************
1623  *    Misc    *
1624  **************/
1625
1626 static gboolean
1627 timestamp_from_iso8601 (const gchar  *iso_date,
1628                         GDateTime   **out_date_time,
1629                         GError      **error)
1630 {
1631   GDateTime *dt = g_date_time_new_from_iso8601 (iso_date, NULL);
1632   if (dt == NULL)
1633     {
1634       g_set_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_READ,
1635                    _("Invalid date/time ‘%s’ in bookmark file"), iso_date);
1636       return FALSE;
1637     }
1638
1639   *out_date_time = g_steal_pointer (&dt);
1640   return TRUE;
1641 }
1642
1643 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1644
1645 /********************
1646  *    Public API    *
1647  ********************/
1648
1649 /**
1650  * g_bookmark_file_new: (constructor)
1651  *
1652  * Creates a new empty #GBookmarkFile object.
1653  *
1654  * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1655  * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1656  * file.
1657  *
1658  * Returns: an empty #GBookmarkFile
1659  *
1660  * Since: 2.12
1661  */
1662 GBookmarkFile *
1663 g_bookmark_file_new (void)
1664 {
1665   GBookmarkFile *bookmark;
1666
1667   bookmark = g_new (GBookmarkFile, 1);
1668
1669   g_bookmark_file_init (bookmark);
1670
1671   return bookmark;
1672 }
1673
1674 /**
1675  * g_bookmark_file_free:
1676  * @bookmark: a #GBookmarkFile
1677  *
1678  * Frees a #GBookmarkFile.
1679  *
1680  * Since: 2.12
1681  */
1682 void
1683 g_bookmark_file_free (GBookmarkFile *bookmark)
1684 {
1685   if (!bookmark)
1686     return;
1687
1688   g_bookmark_file_clear (bookmark);
1689
1690   g_free (bookmark);
1691 }
1692
1693 /**
1694  * g_bookmark_file_load_from_data:
1695  * @bookmark: an empty #GBookmarkFile struct
1696  * @data: (array length=length) (element-type guint8): desktop bookmarks
1697  *    loaded in memory
1698  * @length: the length of @data in bytes
1699  * @error: return location for a #GError, or %NULL
1700  *
1701  * Loads a bookmark file from memory into an empty #GBookmarkFile
1702  * structure.  If the object cannot be created then @error is set to a
1703  * #GBookmarkFileError.
1704  *
1705  * Returns: %TRUE if a desktop bookmark could be loaded.
1706  *
1707  * Since: 2.12
1708  */
1709 gboolean
1710 g_bookmark_file_load_from_data (GBookmarkFile  *bookmark,
1711                                 const gchar    *data,
1712                                 gsize           length,
1713                                 GError        **error)
1714 {
1715   GError *parse_error;
1716   gboolean retval;
1717
1718   g_return_val_if_fail (bookmark != NULL, FALSE);
1719
1720   if (length == (gsize) -1)
1721     length = strlen (data);
1722
1723   if (bookmark->items)
1724     {
1725       g_bookmark_file_clear (bookmark);
1726       g_bookmark_file_init (bookmark);
1727     }
1728
1729   parse_error = NULL;
1730   retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1731
1732   if (!retval)
1733     g_propagate_error (error, parse_error);
1734
1735   return retval;
1736 }
1737
1738 /**
1739  * g_bookmark_file_load_from_file:
1740  * @bookmark: an empty #GBookmarkFile struct
1741  * @filename: (type filename): the path of a filename to load, in the
1742  *     GLib file name encoding
1743  * @error: return location for a #GError, or %NULL
1744  *
1745  * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1746  * If the file could not be loaded then @error is set to either a #GFileError
1747  * or #GBookmarkFileError.
1748  *
1749  * Returns: %TRUE if a desktop bookmark file could be loaded
1750  *
1751  * Since: 2.12
1752  */
1753 gboolean
1754 g_bookmark_file_load_from_file (GBookmarkFile  *bookmark,
1755                                 const gchar    *filename,
1756                                 GError        **error)
1757 {
1758   gboolean ret = FALSE;
1759   gchar *buffer = NULL;
1760   gsize len;
1761
1762   g_return_val_if_fail (bookmark != NULL, FALSE);
1763   g_return_val_if_fail (filename != NULL, FALSE);
1764
1765   if (!g_file_get_contents (filename, &buffer, &len, error))
1766     goto out;
1767
1768   if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1769     goto out;
1770
1771   ret = TRUE;
1772  out:
1773   g_free (buffer);
1774   return ret;
1775 }
1776
1777
1778 /* Iterates through all the directories in *dirs trying to
1779  * find file.  When it successfully locates file, returns a
1780  * string its absolute path.  It also leaves the unchecked
1781  * directories in *dirs.  You should free the returned string
1782  *
1783  * Adapted from gkeyfile.c
1784  */
1785 static gchar *
1786 find_file_in_data_dirs (const gchar   *file,
1787                         gchar       ***dirs,
1788                         GError       **error)
1789 {
1790   gchar **data_dirs, *data_dir, *path;
1791
1792   path = NULL;
1793
1794   if (dirs == NULL)
1795     return NULL;
1796
1797   data_dirs = *dirs;
1798   path = NULL;
1799   while (data_dirs && (data_dir = *data_dirs) && !path)
1800     {
1801       gchar *candidate_file, *sub_dir;
1802
1803       candidate_file = (gchar *) file;
1804       sub_dir = g_strdup ("");
1805       while (candidate_file != NULL && !path)
1806         {
1807           gchar *p;
1808
1809           path = g_build_filename (data_dir, sub_dir,
1810                                    candidate_file, NULL);
1811
1812           candidate_file = strchr (candidate_file, '-');
1813
1814           if (candidate_file == NULL)
1815             break;
1816
1817           candidate_file++;
1818
1819           g_free (sub_dir);
1820           sub_dir = g_strndup (file, candidate_file - file - 1);
1821
1822           for (p = sub_dir; *p != '\0'; p++)
1823             {
1824               if (*p == '-')
1825                 *p = G_DIR_SEPARATOR;
1826             }
1827         }
1828       g_free (sub_dir);
1829       data_dirs++;
1830     }
1831
1832   *dirs = data_dirs;
1833
1834   if (!path)
1835     {
1836       g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1837                            G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1838                            _("No valid bookmark file found in data dirs"));
1839
1840       return NULL;
1841     }
1842
1843   return path;
1844 }
1845
1846
1847 /**
1848  * g_bookmark_file_load_from_data_dirs:
1849  * @bookmark: a #GBookmarkFile
1850  * @file: (type filename): a relative path to a filename to open and parse
1851  * @full_path: (out) (optional) (type filename): return location for a string
1852  *    containing the full path of the file, or %NULL
1853  * @error: return location for a #GError, or %NULL
1854  *
1855  * This function looks for a desktop bookmark file named @file in the
1856  * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1857  * loads the file into @bookmark and returns the file's full path in
1858  * @full_path.  If the file could not be loaded then @error is
1859  * set to either a #GFileError or #GBookmarkFileError.
1860  *
1861  * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1862  *
1863  * Since: 2.12
1864  */
1865 gboolean
1866 g_bookmark_file_load_from_data_dirs (GBookmarkFile  *bookmark,
1867                                      const gchar    *file,
1868                                      gchar         **full_path,
1869                                      GError        **error)
1870 {
1871   GError *file_error = NULL;
1872   gchar **all_data_dirs, **data_dirs;
1873   const gchar *user_data_dir;
1874   const gchar * const * system_data_dirs;
1875   gsize i, j;
1876   gchar *output_path;
1877   gboolean found_file;
1878
1879   g_return_val_if_fail (bookmark != NULL, FALSE);
1880   g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1881
1882   user_data_dir = g_get_user_data_dir ();
1883   system_data_dirs = g_get_system_data_dirs ();
1884   all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1885
1886   i = 0;
1887   all_data_dirs[i++] = g_strdup (user_data_dir);
1888
1889   j = 0;
1890   while (system_data_dirs[j] != NULL)
1891     all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1892
1893   found_file = FALSE;
1894   data_dirs = all_data_dirs;
1895   output_path = NULL;
1896   while (*data_dirs != NULL && !found_file)
1897     {
1898       g_free (output_path);
1899
1900       output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1901
1902       if (file_error)
1903         {
1904           g_propagate_error (error, file_error);
1905           break;
1906         }
1907
1908       found_file = g_bookmark_file_load_from_file (bookmark,
1909                                                    output_path,
1910                                                    &file_error);
1911       if (file_error)
1912         {
1913           g_propagate_error (error, file_error);
1914           break;
1915         }
1916     }
1917
1918   if (found_file && full_path)
1919     *full_path = output_path;
1920   else
1921     g_free (output_path);
1922
1923   g_strfreev (all_data_dirs);
1924
1925   return found_file;
1926 }
1927
1928
1929 /**
1930  * g_bookmark_file_to_data:
1931  * @bookmark: a #GBookmarkFile
1932  * @length: (out) (optional): return location for the length of the returned string, or %NULL
1933  * @error: return location for a #GError, or %NULL
1934  *
1935  * This function outputs @bookmark as a string.
1936  *
1937  * Returns: (transfer full) (array length=length) (element-type guint8):
1938  *   a newly allocated string holding the contents of the #GBookmarkFile
1939  *
1940  * Since: 2.12
1941  */
1942 gchar *
1943 g_bookmark_file_to_data (GBookmarkFile  *bookmark,
1944                          gsize          *length,
1945                          GError        **error)
1946 {
1947   GError *write_error = NULL;
1948   gchar *retval;
1949
1950   g_return_val_if_fail (bookmark != NULL, NULL);
1951
1952   retval = g_bookmark_file_dump (bookmark, length, &write_error);
1953   if (write_error)
1954     {
1955       g_propagate_error (error, write_error);
1956
1957       return NULL;
1958     }
1959
1960   return retval;
1961 }
1962
1963 /**
1964  * g_bookmark_file_to_file:
1965  * @bookmark: a #GBookmarkFile
1966  * @filename: (type filename): path of the output file
1967  * @error: return location for a #GError, or %NULL
1968  *
1969  * This function outputs @bookmark into a file.  The write process is
1970  * guaranteed to be atomic by using g_file_set_contents() internally.
1971  *
1972  * Returns: %TRUE if the file was successfully written.
1973  *
1974  * Since: 2.12
1975  */
1976 gboolean
1977 g_bookmark_file_to_file (GBookmarkFile  *bookmark,
1978                          const gchar    *filename,
1979                          GError        **error)
1980 {
1981   gchar *data;
1982   GError *data_error, *write_error;
1983   gsize len;
1984   gboolean retval;
1985
1986   g_return_val_if_fail (bookmark != NULL, FALSE);
1987   g_return_val_if_fail (filename != NULL, FALSE);
1988
1989   data_error = NULL;
1990   data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1991   if (data_error)
1992     {
1993       g_propagate_error (error, data_error);
1994
1995       return FALSE;
1996     }
1997
1998   write_error = NULL;
1999   g_file_set_contents (filename, data, len, &write_error);
2000   if (write_error)
2001     {
2002       g_propagate_error (error, write_error);
2003
2004       retval = FALSE;
2005     }
2006   else
2007     retval = TRUE;
2008
2009   g_free (data);
2010
2011   return retval;
2012 }
2013
2014 static BookmarkItem *
2015 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
2016                              const gchar   *uri)
2017 {
2018   g_warn_if_fail (bookmark != NULL && uri != NULL);
2019
2020   return g_hash_table_lookup (bookmark->items_by_uri, uri);
2021 }
2022
2023 /* this function adds a new item to the list */
2024 static void
2025 g_bookmark_file_add_item (GBookmarkFile  *bookmark,
2026                           BookmarkItem   *item,
2027                           GError        **error)
2028 {
2029   g_warn_if_fail (bookmark != NULL);
2030   g_warn_if_fail (item != NULL);
2031
2032   /* this should never happen; and if it does, then we are
2033    * screwing up something big time.
2034    */
2035   if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2036     {
2037       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2038                    G_BOOKMARK_FILE_ERROR_INVALID_URI,
2039                    _("A bookmark for URI “%s” already exists"),
2040                    item->uri);
2041       return;
2042     }
2043
2044   bookmark->items = g_list_prepend (bookmark->items, item);
2045
2046   g_hash_table_replace (bookmark->items_by_uri,
2047                         item->uri,
2048                         item);
2049
2050   if (item->added == NULL)
2051     item->added = g_date_time_new_now_utc ();
2052
2053   if (item->modified == NULL)
2054     item->modified = g_date_time_new_now_utc ();
2055
2056   if (item->visited == NULL)
2057     item->visited = g_date_time_new_now_utc ();
2058 }
2059
2060 /**
2061  * g_bookmark_file_remove_item:
2062  * @bookmark: a #GBookmarkFile
2063  * @uri: a valid URI
2064  * @error: return location for a #GError, or %NULL
2065  *
2066  * Removes the bookmark for @uri from the bookmark file @bookmark.
2067  *
2068  * Returns: %TRUE if the bookmark was removed successfully.
2069  *
2070  * Since: 2.12
2071  */
2072 gboolean
2073 g_bookmark_file_remove_item (GBookmarkFile  *bookmark,
2074                              const gchar    *uri,
2075                              GError        **error)
2076 {
2077   BookmarkItem *item;
2078
2079   g_return_val_if_fail (bookmark != NULL, FALSE);
2080   g_return_val_if_fail (uri != NULL, FALSE);
2081
2082   item = g_bookmark_file_lookup_item (bookmark, uri);
2083
2084   if (!item)
2085     {
2086       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2087                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2088                    _("No bookmark found for URI “%s”"),
2089                    uri);
2090       return FALSE;
2091     }
2092
2093   bookmark->items = g_list_remove (bookmark->items, item);
2094   g_hash_table_remove (bookmark->items_by_uri, item->uri);
2095
2096   bookmark_item_free (item);
2097
2098   return TRUE;
2099 }
2100
2101 /**
2102  * g_bookmark_file_has_item:
2103  * @bookmark: a #GBookmarkFile
2104  * @uri: a valid URI
2105  *
2106  * Looks whether the desktop bookmark has an item with its URI set to @uri.
2107  *
2108  * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2109  *
2110  * Since: 2.12
2111  */
2112 gboolean
2113 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2114                           const gchar   *uri)
2115 {
2116   g_return_val_if_fail (bookmark != NULL, FALSE);
2117   g_return_val_if_fail (uri != NULL, FALSE);
2118
2119   return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2120 }
2121
2122 /**
2123  * g_bookmark_file_get_uris:
2124  * @bookmark: a #GBookmarkFile
2125  * @length: (out) (optional): return location for the number of returned URIs, or %NULL
2126  *
2127  * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2128  * The array of returned URIs will be %NULL-terminated, so @length may
2129  * optionally be %NULL.
2130  *
2131  * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2132  *   Use g_strfreev() to free it.
2133  *
2134  * Since: 2.12
2135  */
2136 gchar **
2137 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2138                           gsize         *length)
2139 {
2140   GList *l;
2141   gchar **uris;
2142   gsize i, n_items;
2143
2144   g_return_val_if_fail (bookmark != NULL, NULL);
2145
2146   n_items = g_list_length (bookmark->items);
2147   uris = g_new0 (gchar *, n_items + 1);
2148
2149   /* the items are stored in reverse order, so we walk the list backward */
2150   for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2151     {
2152       BookmarkItem *item = (BookmarkItem *) l->data;
2153
2154       g_warn_if_fail (item != NULL);
2155
2156       uris[i++] = g_strdup (item->uri);
2157     }
2158   uris[i] = NULL;
2159
2160   if (length)
2161     *length = i;
2162
2163   return uris;
2164 }
2165
2166 /**
2167  * g_bookmark_file_set_title:
2168  * @bookmark: a #GBookmarkFile
2169  * @uri: (nullable): a valid URI or %NULL
2170  * @title: a UTF-8 encoded string
2171  *
2172  * Sets @title as the title of the bookmark for @uri inside the
2173  * bookmark file @bookmark.
2174  *
2175  * If @uri is %NULL, the title of @bookmark is set.
2176  *
2177  * If a bookmark for @uri cannot be found then it is created.
2178  *
2179  * Since: 2.12
2180  */
2181 void
2182 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2183                            const gchar   *uri,
2184                            const gchar   *title)
2185 {
2186   g_return_if_fail (bookmark != NULL);
2187
2188   if (!uri)
2189     {
2190       g_free (bookmark->title);
2191       bookmark->title = g_strdup (title);
2192     }
2193   else
2194     {
2195       BookmarkItem *item;
2196
2197       item = g_bookmark_file_lookup_item (bookmark, uri);
2198       if (!item)
2199         {
2200           item = bookmark_item_new (uri);
2201           g_bookmark_file_add_item (bookmark, item, NULL);
2202         }
2203
2204       g_free (item->title);
2205       item->title = g_strdup (title);
2206
2207       bookmark_item_touch_modified (item);
2208     }
2209 }
2210
2211 /**
2212  * g_bookmark_file_get_title:
2213  * @bookmark: a #GBookmarkFile
2214  * @uri: (nullable): a valid URI or %NULL
2215  * @error: return location for a #GError, or %NULL
2216  *
2217  * Returns the title of the bookmark for @uri.
2218  *
2219  * If @uri is %NULL, the title of @bookmark is returned.
2220  *
2221  * In the event the URI cannot be found, %NULL is returned and
2222  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2223  *
2224  * Returns: (transfer full): a newly allocated string or %NULL if the specified
2225  *   URI cannot be found.
2226  *
2227  * Since: 2.12
2228  */
2229 gchar *
2230 g_bookmark_file_get_title (GBookmarkFile  *bookmark,
2231                            const gchar    *uri,
2232                            GError        **error)
2233 {
2234   BookmarkItem *item;
2235
2236   g_return_val_if_fail (bookmark != NULL, NULL);
2237
2238   if (!uri)
2239     return g_strdup (bookmark->title);
2240
2241   item = g_bookmark_file_lookup_item (bookmark, uri);
2242   if (!item)
2243     {
2244       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2245                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2246                    _("No bookmark found for URI “%s”"),
2247                    uri);
2248       return NULL;
2249     }
2250
2251   return g_strdup (item->title);
2252 }
2253
2254 /**
2255  * g_bookmark_file_set_description:
2256  * @bookmark: a #GBookmarkFile
2257  * @uri: (nullable): a valid URI or %NULL
2258  * @description: a string
2259  *
2260  * Sets @description as the description of the bookmark for @uri.
2261  *
2262  * If @uri is %NULL, the description of @bookmark is set.
2263  *
2264  * If a bookmark for @uri cannot be found then it is created.
2265  *
2266  * Since: 2.12
2267  */
2268 void
2269 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2270                                  const gchar   *uri,
2271                                  const gchar   *description)
2272 {
2273   g_return_if_fail (bookmark != NULL);
2274
2275   if (!uri)
2276     {
2277       g_free (bookmark->description);
2278       bookmark->description = g_strdup (description);
2279     }
2280   else
2281     {
2282       BookmarkItem *item;
2283
2284       item = g_bookmark_file_lookup_item (bookmark, uri);
2285       if (!item)
2286         {
2287           item = bookmark_item_new (uri);
2288           g_bookmark_file_add_item (bookmark, item, NULL);
2289         }
2290
2291       g_free (item->description);
2292       item->description = g_strdup (description);
2293
2294       bookmark_item_touch_modified (item);
2295     }
2296 }
2297
2298 /**
2299  * g_bookmark_file_get_description:
2300  * @bookmark: a #GBookmarkFile
2301  * @uri: a valid URI
2302  * @error: return location for a #GError, or %NULL
2303  *
2304  * Retrieves the description of the bookmark for @uri.
2305  *
2306  * In the event the URI cannot be found, %NULL is returned and
2307  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2308  *
2309  * Returns: (transfer full): a newly allocated string or %NULL if the specified
2310  *   URI cannot be found.
2311  *
2312  * Since: 2.12
2313  */
2314 gchar *
2315 g_bookmark_file_get_description (GBookmarkFile  *bookmark,
2316                                  const gchar    *uri,
2317                                  GError        **error)
2318 {
2319   BookmarkItem *item;
2320
2321   g_return_val_if_fail (bookmark != NULL, NULL);
2322
2323   if (!uri)
2324     return g_strdup (bookmark->description);
2325
2326   item = g_bookmark_file_lookup_item (bookmark, uri);
2327   if (!item)
2328     {
2329       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2330                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2331                    _("No bookmark found for URI “%s”"),
2332                    uri);
2333       return NULL;
2334     }
2335
2336   return g_strdup (item->description);
2337 }
2338
2339 /**
2340  * g_bookmark_file_set_mime_type:
2341  * @bookmark: a #GBookmarkFile
2342  * @uri: a valid URI
2343  * @mime_type: a MIME type
2344  *
2345  * Sets @mime_type as the MIME type of the bookmark for @uri.
2346  *
2347  * If a bookmark for @uri cannot be found then it is created.
2348  *
2349  * Since: 2.12
2350  */
2351 void
2352 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2353                                const gchar   *uri,
2354                                const gchar   *mime_type)
2355 {
2356   BookmarkItem *item;
2357
2358   g_return_if_fail (bookmark != NULL);
2359   g_return_if_fail (uri != NULL);
2360   g_return_if_fail (mime_type != NULL);
2361
2362   item = g_bookmark_file_lookup_item (bookmark, uri);
2363   if (!item)
2364     {
2365       item = bookmark_item_new (uri);
2366       g_bookmark_file_add_item (bookmark, item, NULL);
2367     }
2368
2369   if (!item->metadata)
2370     item->metadata = bookmark_metadata_new ();
2371
2372   g_free (item->metadata->mime_type);
2373
2374   item->metadata->mime_type = g_strdup (mime_type);
2375   bookmark_item_touch_modified (item);
2376 }
2377
2378 /**
2379  * g_bookmark_file_get_mime_type:
2380  * @bookmark: a #GBookmarkFile
2381  * @uri: a valid URI
2382  * @error: return location for a #GError, or %NULL
2383  *
2384  * Retrieves the MIME type of the resource pointed by @uri.
2385  *
2386  * In the event the URI cannot be found, %NULL is returned and
2387  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
2388  * event that the MIME type cannot be found, %NULL is returned and
2389  * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2390  *
2391  * Returns: (transfer full): a newly allocated string or %NULL if the specified
2392  *   URI cannot be found.
2393  *
2394  * Since: 2.12
2395  */
2396 gchar *
2397 g_bookmark_file_get_mime_type (GBookmarkFile  *bookmark,
2398                                const gchar    *uri,
2399                                GError        **error)
2400 {
2401   BookmarkItem *item;
2402
2403   g_return_val_if_fail (bookmark != NULL, NULL);
2404   g_return_val_if_fail (uri != NULL, NULL);
2405
2406   item = g_bookmark_file_lookup_item (bookmark, uri);
2407   if (!item)
2408     {
2409       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2410                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2411                    _("No bookmark found for URI “%s”"),
2412                    uri);
2413       return NULL;
2414     }
2415
2416   if (!item->metadata)
2417     {
2418       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2419                    G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2420                    _("No MIME type defined in the bookmark for URI “%s”"),
2421                    uri);
2422       return NULL;
2423     }
2424
2425   return g_strdup (item->metadata->mime_type);
2426 }
2427
2428 /**
2429  * g_bookmark_file_set_is_private:
2430  * @bookmark: a #GBookmarkFile
2431  * @uri: a valid URI
2432  * @is_private: %TRUE if the bookmark should be marked as private
2433  *
2434  * Sets the private flag of the bookmark for @uri.
2435  *
2436  * If a bookmark for @uri cannot be found then it is created.
2437  *
2438  * Since: 2.12
2439  */
2440 void
2441 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2442                                 const gchar   *uri,
2443                                 gboolean       is_private)
2444 {
2445   BookmarkItem *item;
2446
2447   g_return_if_fail (bookmark != NULL);
2448   g_return_if_fail (uri != NULL);
2449
2450   item = g_bookmark_file_lookup_item (bookmark, uri);
2451   if (!item)
2452     {
2453       item = bookmark_item_new (uri);
2454       g_bookmark_file_add_item (bookmark, item, NULL);
2455     }
2456
2457   if (!item->metadata)
2458     item->metadata = bookmark_metadata_new ();
2459
2460   item->metadata->is_private = (is_private == TRUE);
2461   bookmark_item_touch_modified (item);
2462 }
2463
2464 /**
2465  * g_bookmark_file_get_is_private:
2466  * @bookmark: a #GBookmarkFile
2467  * @uri: a valid URI
2468  * @error: return location for a #GError, or %NULL
2469  *
2470  * Gets whether the private flag of the bookmark for @uri is set.
2471  *
2472  * In the event the URI cannot be found, %FALSE is returned and
2473  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
2474  * event that the private flag cannot be found, %FALSE is returned and
2475  * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2476  *
2477  * Returns: %TRUE if the private flag is set, %FALSE otherwise.
2478  *
2479  * Since: 2.12
2480  */
2481 gboolean
2482 g_bookmark_file_get_is_private (GBookmarkFile  *bookmark,
2483                                 const gchar    *uri,
2484                                 GError        **error)
2485 {
2486   BookmarkItem *item;
2487
2488   g_return_val_if_fail (bookmark != NULL, FALSE);
2489   g_return_val_if_fail (uri != NULL, FALSE);
2490
2491   item = g_bookmark_file_lookup_item (bookmark, uri);
2492   if (!item)
2493     {
2494       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2495                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2496                    _("No bookmark found for URI “%s”"),
2497                    uri);
2498       return FALSE;
2499     }
2500
2501   if (!item->metadata)
2502     {
2503       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2504                    G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2505                    _("No private flag has been defined in bookmark for URI “%s”"),
2506                     uri);
2507       return FALSE;
2508     }
2509
2510   return item->metadata->is_private;
2511 }
2512
2513 /**
2514  * g_bookmark_file_set_added:
2515  * @bookmark: a #GBookmarkFile
2516  * @uri: a valid URI
2517  * @added: a timestamp or -1 to use the current time
2518  *
2519  * Sets the time the bookmark for @uri was added into @bookmark.
2520  *
2521  * If no bookmark for @uri is found then it is created.
2522  *
2523  * Since: 2.12
2524  * Deprecated: 2.66: Use g_bookmark_file_set_added_date_time() instead, as
2525  *    `time_t` is deprecated due to the year 2038 problem.
2526  */
2527 void
2528 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2529                            const gchar   *uri,
2530                            time_t         added)
2531 {
2532   GDateTime *added_dt = (added != (time_t) -1) ? g_date_time_new_from_unix_utc (added) : g_date_time_new_now_utc ();
2533   g_bookmark_file_set_added_date_time (bookmark, uri, added_dt);
2534   g_date_time_unref (added_dt);
2535 }
2536
2537 /**
2538  * g_bookmark_file_set_added_date_time:
2539  * @bookmark: a #GBookmarkFile
2540  * @uri: a valid URI
2541  * @added: a #GDateTime
2542  *
2543  * Sets the time the bookmark for @uri was added into @bookmark.
2544  *
2545  * If no bookmark for @uri is found then it is created.
2546  *
2547  * Since: 2.66
2548  */
2549 void
2550 g_bookmark_file_set_added_date_time (GBookmarkFile *bookmark,
2551                                      const char    *uri,
2552                                      GDateTime     *added)
2553 {
2554   BookmarkItem *item;
2555
2556   g_return_if_fail (bookmark != NULL);
2557   g_return_if_fail (uri != NULL);
2558   g_return_if_fail (added != NULL);
2559
2560   item = g_bookmark_file_lookup_item (bookmark, uri);
2561   if (!item)
2562     {
2563       item = bookmark_item_new (uri);
2564       g_bookmark_file_add_item (bookmark, item, NULL);
2565     }
2566
2567   g_clear_pointer (&item->added, g_date_time_unref);
2568   item->added = g_date_time_ref (added);
2569   g_clear_pointer (&item->modified, g_date_time_unref);
2570   item->modified = g_date_time_ref (added);
2571 }
2572
2573 /**
2574  * g_bookmark_file_get_added:
2575  * @bookmark: a #GBookmarkFile
2576  * @uri: a valid URI
2577  * @error: return location for a #GError, or %NULL
2578  *
2579  * Gets the time the bookmark for @uri was added to @bookmark
2580  *
2581  * In the event the URI cannot be found, -1 is returned and
2582  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2583  *
2584  * Returns: a timestamp
2585  *
2586  * Since: 2.12
2587  * Deprecated: 2.66: Use g_bookmark_file_get_added_date_time() instead, as
2588  *    `time_t` is deprecated due to the year 2038 problem.
2589  */
2590 time_t
2591 g_bookmark_file_get_added (GBookmarkFile  *bookmark,
2592                            const gchar    *uri,
2593                            GError        **error)
2594 {
2595   GDateTime *added = g_bookmark_file_get_added_date_time (bookmark, uri, error);
2596   return (added != NULL) ? g_date_time_to_unix (added) : (time_t) -1;
2597 }
2598
2599 /**
2600  * g_bookmark_file_get_added_date_time:
2601  * @bookmark: a #GBookmarkFile
2602  * @uri: a valid URI
2603  * @error: return location for a #GError, or %NULL
2604  *
2605  * Gets the time the bookmark for @uri was added to @bookmark
2606  *
2607  * In the event the URI cannot be found, %NULL is returned and
2608  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2609  *
2610  * Returns: (transfer none): a #GDateTime
2611  *
2612  * Since: 2.66
2613  */
2614 GDateTime *
2615 g_bookmark_file_get_added_date_time (GBookmarkFile  *bookmark,
2616                                      const char     *uri,
2617                                      GError        **error)
2618 {
2619   BookmarkItem *item;
2620
2621   g_return_val_if_fail (bookmark != NULL, NULL);
2622   g_return_val_if_fail (uri != NULL, NULL);
2623   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2624
2625   item = g_bookmark_file_lookup_item (bookmark, uri);
2626   if (!item)
2627     {
2628       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2629                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2630                    _("No bookmark found for URI “%s”"),
2631                    uri);
2632       return NULL;
2633     }
2634
2635   return item->added;
2636 }
2637
2638 /**
2639  * g_bookmark_file_set_modified:
2640  * @bookmark: a #GBookmarkFile
2641  * @uri: a valid URI
2642  * @modified: a timestamp or -1 to use the current time
2643  *
2644  * Sets the last time the bookmark for @uri was last modified.
2645  *
2646  * If no bookmark for @uri is found then it is created.
2647  *
2648  * The "modified" time should only be set when the bookmark's meta-data
2649  * was actually changed.  Every function of #GBookmarkFile that
2650  * modifies a bookmark also changes the modification time, except for
2651  * g_bookmark_file_set_visited_date_time().
2652  *
2653  * Since: 2.12
2654  * Deprecated: 2.66: Use g_bookmark_file_set_modified_date_time() instead, as
2655  *    `time_t` is deprecated due to the year 2038 problem.
2656  */
2657 void
2658 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2659                               const gchar   *uri,
2660                               time_t         modified)
2661 {
2662   GDateTime *modified_dt = (modified != (time_t) -1) ? g_date_time_new_from_unix_utc (modified) : g_date_time_new_now_utc ();
2663   g_bookmark_file_set_modified_date_time (bookmark, uri, modified_dt);
2664   g_date_time_unref (modified_dt);
2665 }
2666
2667 /**
2668  * g_bookmark_file_set_modified_date_time:
2669  * @bookmark: a #GBookmarkFile
2670  * @uri: a valid URI
2671  * @modified: a #GDateTime
2672  *
2673  * Sets the last time the bookmark for @uri was last modified.
2674  *
2675  * If no bookmark for @uri is found then it is created.
2676  *
2677  * The "modified" time should only be set when the bookmark's meta-data
2678  * was actually changed.  Every function of #GBookmarkFile that
2679  * modifies a bookmark also changes the modification time, except for
2680  * g_bookmark_file_set_visited_date_time().
2681  *
2682  * Since: 2.66
2683  */
2684 void
2685 g_bookmark_file_set_modified_date_time (GBookmarkFile *bookmark,
2686                                         const char    *uri,
2687                                         GDateTime     *modified)
2688 {
2689   BookmarkItem *item;
2690
2691   g_return_if_fail (bookmark != NULL);
2692   g_return_if_fail (uri != NULL);
2693   g_return_if_fail (modified != NULL);
2694
2695   item = g_bookmark_file_lookup_item (bookmark, uri);
2696   if (!item)
2697     {
2698       item = bookmark_item_new (uri);
2699       g_bookmark_file_add_item (bookmark, item, NULL);
2700     }
2701
2702   g_clear_pointer (&item->modified, g_date_time_unref);
2703   item->modified = g_date_time_ref (modified);
2704 }
2705
2706 /**
2707  * g_bookmark_file_get_modified:
2708  * @bookmark: a #GBookmarkFile
2709  * @uri: a valid URI
2710  * @error: return location for a #GError, or %NULL
2711  *
2712  * Gets the time when the bookmark for @uri was last modified.
2713  *
2714  * In the event the URI cannot be found, -1 is returned and
2715  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2716  *
2717  * Returns: a timestamp
2718  *
2719  * Since: 2.12
2720  * Deprecated: 2.66: Use g_bookmark_file_get_modified_date_time() instead, as
2721  *    `time_t` is deprecated due to the year 2038 problem.
2722  */
2723 time_t
2724 g_bookmark_file_get_modified (GBookmarkFile  *bookmark,
2725                               const gchar    *uri,
2726                               GError        **error)
2727 {
2728   GDateTime *modified = g_bookmark_file_get_modified_date_time (bookmark, uri, error);
2729   return (modified != NULL) ? g_date_time_to_unix (modified) : (time_t) -1;
2730 }
2731
2732 /**
2733  * g_bookmark_file_get_modified_date_time:
2734  * @bookmark: a #GBookmarkFile
2735  * @uri: a valid URI
2736  * @error: return location for a #GError, or %NULL
2737  *
2738  * Gets the time when the bookmark for @uri was last modified.
2739  *
2740  * In the event the URI cannot be found, %NULL is returned and
2741  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2742  *
2743  * Returns: (transfer none): a #GDateTime
2744  *
2745  * Since: 2.66
2746  */
2747 GDateTime *
2748 g_bookmark_file_get_modified_date_time (GBookmarkFile  *bookmark,
2749                                         const char     *uri,
2750                                         GError        **error)
2751 {
2752   BookmarkItem *item;
2753
2754   g_return_val_if_fail (bookmark != NULL, NULL);
2755   g_return_val_if_fail (uri != NULL, NULL);
2756   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2757
2758   item = g_bookmark_file_lookup_item (bookmark, uri);
2759   if (!item)
2760     {
2761       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2762                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2763                    _("No bookmark found for URI “%s”"),
2764                    uri);
2765       return NULL;
2766     }
2767
2768   return item->modified;
2769 }
2770
2771 /**
2772  * g_bookmark_file_set_visited:
2773  * @bookmark: a #GBookmarkFile
2774  * @uri: a valid URI
2775  * @visited: a timestamp or -1 to use the current time
2776  *
2777  * Sets the time the bookmark for @uri was last visited.
2778  *
2779  * If no bookmark for @uri is found then it is created.
2780  *
2781  * The "visited" time should only be set if the bookmark was launched,
2782  * either using the command line retrieved by g_bookmark_file_get_application_info()
2783  * or by the default application for the bookmark's MIME type, retrieved
2784  * using g_bookmark_file_get_mime_type().  Changing the "visited" time
2785  * does not affect the "modified" time.
2786  *
2787  * Since: 2.12
2788  * Deprecated: 2.66: Use g_bookmark_file_set_visited_date_time() instead, as
2789  *    `time_t` is deprecated due to the year 2038 problem.
2790  */
2791 void
2792 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2793                              const gchar   *uri,
2794                              time_t         visited)
2795 {
2796   GDateTime *visited_dt = (visited != (time_t) -1) ? g_date_time_new_from_unix_utc (visited) : g_date_time_new_now_utc ();
2797   g_bookmark_file_set_visited_date_time (bookmark, uri, visited_dt);
2798   g_date_time_unref (visited_dt);
2799 }
2800
2801 /**
2802  * g_bookmark_file_set_visited_date_time:
2803  * @bookmark: a #GBookmarkFile
2804  * @uri: a valid URI
2805  * @visited: a #GDateTime
2806  *
2807  * Sets the time the bookmark for @uri was last visited.
2808  *
2809  * If no bookmark for @uri is found then it is created.
2810  *
2811  * The "visited" time should only be set if the bookmark was launched,
2812  * either using the command line retrieved by g_bookmark_file_get_application_info()
2813  * or by the default application for the bookmark's MIME type, retrieved
2814  * using g_bookmark_file_get_mime_type().  Changing the "visited" time
2815  * does not affect the "modified" time.
2816  *
2817  * Since: 2.66
2818  */
2819 void
2820 g_bookmark_file_set_visited_date_time (GBookmarkFile *bookmark,
2821                                        const char    *uri,
2822                                        GDateTime     *visited)
2823 {
2824   BookmarkItem *item;
2825
2826   g_return_if_fail (bookmark != NULL);
2827   g_return_if_fail (uri != NULL);
2828   g_return_if_fail (visited != NULL);
2829
2830   item = g_bookmark_file_lookup_item (bookmark, uri);
2831   if (!item)
2832     {
2833       item = bookmark_item_new (uri);
2834       g_bookmark_file_add_item (bookmark, item, NULL);
2835     }
2836
2837   g_clear_pointer (&item->visited, g_date_time_unref);
2838   item->visited = g_date_time_ref (visited);
2839 }
2840
2841 /**
2842  * g_bookmark_file_get_visited:
2843  * @bookmark: a #GBookmarkFile
2844  * @uri: a valid URI
2845  * @error: return location for a #GError, or %NULL
2846  *
2847  * Gets the time the bookmark for @uri was last visited.
2848  *
2849  * In the event the URI cannot be found, -1 is returned and
2850  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2851  *
2852  * Returns: a timestamp.
2853  *
2854  * Since: 2.12
2855  * Deprecated: 2.66: Use g_bookmark_file_get_visited_date_time() instead, as
2856  *    `time_t` is deprecated due to the year 2038 problem.
2857  */
2858 time_t
2859 g_bookmark_file_get_visited (GBookmarkFile  *bookmark,
2860                              const gchar    *uri,
2861                              GError        **error)
2862 {
2863   GDateTime *visited = g_bookmark_file_get_visited_date_time (bookmark, uri, error);
2864   return (visited != NULL) ? g_date_time_to_unix (visited) : (time_t) -1;
2865 }
2866
2867 /**
2868  * g_bookmark_file_get_visited_date_time:
2869  * @bookmark: a #GBookmarkFile
2870  * @uri: a valid URI
2871  * @error: return location for a #GError, or %NULL
2872  *
2873  * Gets the time the bookmark for @uri was last visited.
2874  *
2875  * In the event the URI cannot be found, %NULL is returned and
2876  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2877  *
2878  * Returns: (transfer none): a #GDateTime
2879  *
2880  * Since: 2.66
2881  */
2882 GDateTime *
2883 g_bookmark_file_get_visited_date_time (GBookmarkFile  *bookmark,
2884                                        const char     *uri,
2885                                        GError        **error)
2886 {
2887   BookmarkItem *item;
2888
2889   g_return_val_if_fail (bookmark != NULL, NULL);
2890   g_return_val_if_fail (uri != NULL, NULL);
2891   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2892
2893   item = g_bookmark_file_lookup_item (bookmark, uri);
2894   if (!item)
2895     {
2896       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2897                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2898                    _("No bookmark found for URI “%s”"),
2899                    uri);
2900       return NULL;
2901     }
2902
2903   return item->visited;
2904 }
2905
2906 /**
2907  * g_bookmark_file_has_group:
2908  * @bookmark: a #GBookmarkFile
2909  * @uri: a valid URI
2910  * @group: the group name to be searched
2911  * @error: return location for a #GError, or %NULL
2912  *
2913  * Checks whether @group appears in the list of groups to which
2914  * the bookmark for @uri belongs to.
2915  *
2916  * In the event the URI cannot be found, %FALSE is returned and
2917  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2918  *
2919  * Returns: %TRUE if @group was found.
2920  *
2921  * Since: 2.12
2922  */
2923 gboolean
2924 g_bookmark_file_has_group (GBookmarkFile  *bookmark,
2925                            const gchar    *uri,
2926                            const gchar    *group,
2927                            GError        **error)
2928 {
2929   BookmarkItem *item;
2930   GList *l;
2931
2932   g_return_val_if_fail (bookmark != NULL, FALSE);
2933   g_return_val_if_fail (uri != NULL, FALSE);
2934
2935   item = g_bookmark_file_lookup_item (bookmark, uri);
2936   if (!item)
2937     {
2938       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2939                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2940                    _("No bookmark found for URI “%s”"),
2941                    uri);
2942       return FALSE;
2943     }
2944
2945   if (!item->metadata)
2946     return FALSE;
2947
2948   for (l = item->metadata->groups; l != NULL; l = l->next)
2949     {
2950       if (strcmp (l->data, group) == 0)
2951         return TRUE;
2952     }
2953
2954   return FALSE;
2955
2956 }
2957
2958 /**
2959  * g_bookmark_file_add_group:
2960  * @bookmark: a #GBookmarkFile
2961  * @uri: a valid URI
2962  * @group: the group name to be added
2963  *
2964  * Adds @group to the list of groups to which the bookmark for @uri
2965  * belongs to.
2966  *
2967  * If no bookmark for @uri is found then it is created.
2968  *
2969  * Since: 2.12
2970  */
2971 void
2972 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2973                            const gchar   *uri,
2974                            const gchar   *group)
2975 {
2976   BookmarkItem *item;
2977
2978   g_return_if_fail (bookmark != NULL);
2979   g_return_if_fail (uri != NULL);
2980   g_return_if_fail (group != NULL && group[0] != '\0');
2981
2982   item = g_bookmark_file_lookup_item (bookmark, uri);
2983   if (!item)
2984     {
2985       item = bookmark_item_new (uri);
2986       g_bookmark_file_add_item (bookmark, item, NULL);
2987     }
2988
2989   if (!item->metadata)
2990     item->metadata = bookmark_metadata_new ();
2991
2992   if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2993     {
2994       item->metadata->groups = g_list_prepend (item->metadata->groups,
2995                                                g_strdup (group));
2996
2997       bookmark_item_touch_modified (item);
2998     }
2999 }
3000
3001 /**
3002  * g_bookmark_file_remove_group:
3003  * @bookmark: a #GBookmarkFile
3004  * @uri: a valid URI
3005  * @group: the group name to be removed
3006  * @error: return location for a #GError, or %NULL
3007  *
3008  * Removes @group from the list of groups to which the bookmark
3009  * for @uri belongs to.
3010  *
3011  * In the event the URI cannot be found, %FALSE is returned and
3012  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3013  * In the event no group was defined, %FALSE is returned and
3014  * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
3015  *
3016  * Returns: %TRUE if @group was successfully removed.
3017  *
3018  * Since: 2.12
3019  */
3020 gboolean
3021 g_bookmark_file_remove_group (GBookmarkFile  *bookmark,
3022                               const gchar    *uri,
3023                               const gchar    *group,
3024                               GError        **error)
3025 {
3026   BookmarkItem *item;
3027   GList *l;
3028
3029   g_return_val_if_fail (bookmark != NULL, FALSE);
3030   g_return_val_if_fail (uri != NULL, FALSE);
3031
3032   item = g_bookmark_file_lookup_item (bookmark, uri);
3033   if (!item)
3034     {
3035       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3036                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3037                    _("No bookmark found for URI “%s”"),
3038                    uri);
3039       return FALSE;
3040     }
3041
3042   if (!item->metadata)
3043     {
3044       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3045                    G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
3046                    _("No groups set in bookmark for URI “%s”"),
3047                    uri);
3048       return FALSE;
3049     }
3050
3051   for (l = item->metadata->groups; l != NULL; l = l->next)
3052     {
3053       if (strcmp (l->data, group) == 0)
3054         {
3055           item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
3056           g_free (l->data);
3057           g_list_free_1 (l);
3058
3059           bookmark_item_touch_modified (item);
3060
3061           return TRUE;
3062         }
3063     }
3064
3065   return FALSE;
3066 }
3067
3068 /**
3069  * g_bookmark_file_set_groups:
3070  * @bookmark: a #GBookmarkFile
3071  * @uri: an item's URI
3072  * @groups: (nullable) (array length=length) (element-type utf8): an array of
3073  *    group names, or %NULL to remove all groups
3074  * @length: number of group name values in @groups
3075  *
3076  * Sets a list of group names for the item with URI @uri.  Each previously
3077  * set group name list is removed.
3078  *
3079  * If @uri cannot be found then an item for it is created.
3080  *
3081  * Since: 2.12
3082  */
3083 void
3084 g_bookmark_file_set_groups (GBookmarkFile  *bookmark,
3085                             const gchar    *uri,
3086                             const gchar   **groups,
3087                             gsize           length)
3088 {
3089   BookmarkItem *item;
3090   gsize i;
3091
3092   g_return_if_fail (bookmark != NULL);
3093   g_return_if_fail (uri != NULL);
3094   g_return_if_fail (groups != NULL);
3095
3096   item = g_bookmark_file_lookup_item (bookmark, uri);
3097   if (!item)
3098     {
3099       item = bookmark_item_new (uri);
3100       g_bookmark_file_add_item (bookmark, item, NULL);
3101     }
3102
3103   if (!item->metadata)
3104     item->metadata = bookmark_metadata_new ();
3105
3106   g_list_free_full (item->metadata->groups, g_free);
3107   item->metadata->groups = NULL;
3108
3109   if (groups)
3110     {
3111       for (i = 0; i < length && groups[i] != NULL; i++)
3112         item->metadata->groups = g_list_append (item->metadata->groups,
3113                                                 g_strdup (groups[i]));
3114     }
3115
3116   bookmark_item_touch_modified (item);
3117 }
3118
3119 /**
3120  * g_bookmark_file_get_groups:
3121  * @bookmark: a #GBookmarkFile
3122  * @uri: a valid URI
3123  * @length: (out) (optional): return location for the length of the returned string, or %NULL
3124  * @error: return location for a #GError, or %NULL
3125  *
3126  * Retrieves the list of group names of the bookmark for @uri.
3127  *
3128  * In the event the URI cannot be found, %NULL is returned and
3129  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3130  *
3131  * The returned array is %NULL terminated, so @length may optionally
3132  * be %NULL.
3133  *
3134  * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
3135  *   Use g_strfreev() to free it.
3136  *
3137  * Since: 2.12
3138  */
3139 gchar **
3140 g_bookmark_file_get_groups (GBookmarkFile  *bookmark,
3141                             const gchar    *uri,
3142                             gsize          *length,
3143                             GError        **error)
3144 {
3145   BookmarkItem *item;
3146   GList *l;
3147   gsize len, i;
3148   gchar **retval;
3149
3150   g_return_val_if_fail (bookmark != NULL, NULL);
3151   g_return_val_if_fail (uri != NULL, NULL);
3152
3153   item = g_bookmark_file_lookup_item (bookmark, uri);
3154   if (!item)
3155     {
3156       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3157                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3158                    _("No bookmark found for URI “%s”"),
3159                    uri);
3160       return NULL;
3161     }
3162
3163   if (!item->metadata)
3164     {
3165       if (length)
3166         *length = 0;
3167
3168       return NULL;
3169     }
3170
3171   len = g_list_length (item->metadata->groups);
3172   retval = g_new0 (gchar *, len + 1);
3173   for (l = g_list_last (item->metadata->groups), i = 0;
3174        l != NULL;
3175        l = l->prev)
3176     {
3177       gchar *group_name = (gchar *) l->data;
3178
3179       g_warn_if_fail (group_name != NULL);
3180
3181       retval[i++] = g_strdup (group_name);
3182     }
3183   retval[i] = NULL;
3184
3185   if (length)
3186     *length = len;
3187
3188   return retval;
3189 }
3190
3191 /**
3192  * g_bookmark_file_add_application:
3193  * @bookmark: a #GBookmarkFile
3194  * @uri: a valid URI
3195  * @name: (nullable): the name of the application registering the bookmark
3196  *   or %NULL
3197  * @exec: (nullable): command line to be used to launch the bookmark or %NULL
3198  *
3199  * Adds the application with @name and @exec to the list of
3200  * applications that have registered a bookmark for @uri into
3201  * @bookmark.
3202  *
3203  * Every bookmark inside a #GBookmarkFile must have at least an
3204  * application registered.  Each application must provide a name, a
3205  * command line useful for launching the bookmark, the number of times
3206  * the bookmark has been registered by the application and the last
3207  * time the application registered this bookmark.
3208  *
3209  * If @name is %NULL, the name of the application will be the
3210  * same returned by g_get_application_name(); if @exec is %NULL, the
3211  * command line will be a composition of the program name as
3212  * returned by g_get_prgname() and the "\%u" modifier, which will be
3213  * expanded to the bookmark's URI.
3214  *
3215  * This function will automatically take care of updating the
3216  * registrations count and timestamping in case an application
3217  * with the same @name had already registered a bookmark for
3218  * @uri inside @bookmark.
3219  *
3220  * If no bookmark for @uri is found, one is created.
3221  *
3222  * Since: 2.12
3223  */
3224 void
3225 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3226                                  const gchar   *uri,
3227                                  const gchar   *name,
3228                                  const gchar   *exec)
3229 {
3230   BookmarkItem *item;
3231   gchar *app_name, *app_exec;
3232   GDateTime *stamp;
3233
3234   g_return_if_fail (bookmark != NULL);
3235   g_return_if_fail (uri != NULL);
3236
3237   item = g_bookmark_file_lookup_item (bookmark, uri);
3238   if (!item)
3239     {
3240       item = bookmark_item_new (uri);
3241       g_bookmark_file_add_item (bookmark, item, NULL);
3242     }
3243
3244   if (name && name[0] != '\0')
3245     app_name = g_strdup (name);
3246   else
3247     app_name = g_strdup (g_get_application_name ());
3248
3249   if (exec && exec[0] != '\0')
3250     app_exec = g_strdup (exec);
3251   else
3252     app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3253
3254   stamp = g_date_time_new_now_utc ();
3255
3256   g_bookmark_file_set_application_info (bookmark, uri,
3257                                         app_name,
3258                                         app_exec,
3259                                         -1,
3260                                         stamp,
3261                                         NULL);
3262
3263   g_date_time_unref (stamp);
3264   g_free (app_exec);
3265   g_free (app_name);
3266 }
3267
3268 /**
3269  * g_bookmark_file_remove_application:
3270  * @bookmark: a #GBookmarkFile
3271  * @uri: a valid URI
3272  * @name: the name of the application
3273  * @error: return location for a #GError or %NULL
3274  *
3275  * Removes application registered with @name from the list of applications
3276  * that have registered a bookmark for @uri inside @bookmark.
3277  *
3278  * In the event the URI cannot be found, %FALSE is returned and
3279  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3280  * In the event that no application with name @app_name has registered
3281  * a bookmark for @uri,  %FALSE is returned and error is set to
3282  * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3283  *
3284  * Returns: %TRUE if the application was successfully removed.
3285  *
3286  * Since: 2.12
3287  */
3288 gboolean
3289 g_bookmark_file_remove_application (GBookmarkFile  *bookmark,
3290                                     const gchar    *uri,
3291                                     const gchar    *name,
3292                                     GError        **error)
3293 {
3294   GError *set_error;
3295   gboolean retval;
3296
3297   g_return_val_if_fail (bookmark != NULL, FALSE);
3298   g_return_val_if_fail (uri != NULL, FALSE);
3299   g_return_val_if_fail (name != NULL, FALSE);
3300
3301   set_error = NULL;
3302   retval = g_bookmark_file_set_application_info (bookmark, uri,
3303                                                  name,
3304                                                  "",
3305                                                  0,
3306                                                  NULL,
3307                                                  &set_error);
3308   if (set_error)
3309     {
3310       g_propagate_error (error, set_error);
3311
3312       return FALSE;
3313     }
3314
3315   return retval;
3316 }
3317
3318 /**
3319  * g_bookmark_file_has_application:
3320  * @bookmark: a #GBookmarkFile
3321  * @uri: a valid URI
3322  * @name: the name of the application
3323  * @error: return location for a #GError or %NULL
3324  *
3325  * Checks whether the bookmark for @uri inside @bookmark has been
3326  * registered by application @name.
3327  *
3328  * In the event the URI cannot be found, %FALSE is returned and
3329  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3330  *
3331  * Returns: %TRUE if the application @name was found
3332  *
3333  * Since: 2.12
3334  */
3335 gboolean
3336 g_bookmark_file_has_application (GBookmarkFile  *bookmark,
3337                                  const gchar    *uri,
3338                                  const gchar    *name,
3339                                  GError        **error)
3340 {
3341   BookmarkItem *item;
3342
3343   g_return_val_if_fail (bookmark != NULL, FALSE);
3344   g_return_val_if_fail (uri != NULL, FALSE);
3345   g_return_val_if_fail (name != NULL, FALSE);
3346
3347   item = g_bookmark_file_lookup_item (bookmark, uri);
3348   if (!item)
3349     {
3350       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3351                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3352                    _("No bookmark found for URI “%s”"),
3353                    uri);
3354       return FALSE;
3355     }
3356
3357   return (NULL != bookmark_item_lookup_app_info (item, name));
3358 }
3359
3360 /**
3361  * g_bookmark_file_set_app_info:
3362  * @bookmark: a #GBookmarkFile
3363  * @uri: a valid URI
3364  * @name: an application's name
3365  * @exec: an application's command line
3366  * @count: the number of registrations done for this application
3367  * @stamp: the time of the last registration for this application
3368  * @error: return location for a #GError or %NULL
3369  *
3370  * Sets the meta-data of application @name inside the list of
3371  * applications that have registered a bookmark for @uri inside
3372  * @bookmark.
3373  *
3374  * You should rarely use this function; use g_bookmark_file_add_application()
3375  * and g_bookmark_file_remove_application() instead.
3376  *
3377  * @name can be any UTF-8 encoded string used to identify an
3378  * application.
3379  * @exec can have one of these two modifiers: "\%f", which will
3380  * be expanded as the local file name retrieved from the bookmark's
3381  * URI; "\%u", which will be expanded as the bookmark's URI.
3382  * The expansion is done automatically when retrieving the stored
3383  * command line using the g_bookmark_file_get_application_info() function.
3384  * @count is the number of times the application has registered the
3385  * bookmark; if is < 0, the current registration count will be increased
3386  * by one, if is 0, the application with @name will be removed from
3387  * the list of registered applications.
3388  * @stamp is the Unix time of the last registration; if it is -1, the
3389  * current time will be used.
3390  *
3391  * If you try to remove an application by setting its registration count to
3392  * zero, and no bookmark for @uri is found, %FALSE is returned and
3393  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3394  * in the event that no application @name has registered a bookmark
3395  * for @uri,  %FALSE is returned and error is set to
3396  * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.  Otherwise, if no bookmark
3397  * for @uri is found, one is created.
3398  *
3399  * Returns: %TRUE if the application's meta-data was successfully
3400  *   changed.
3401  *
3402  * Since: 2.12
3403  * Deprecated: 2.66: Use g_bookmark_file_set_application_info() instead, as
3404  *    `time_t` is deprecated due to the year 2038 problem.
3405  */
3406 gboolean
3407 g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
3408                               const gchar    *uri,
3409                               const gchar    *name,
3410                               const gchar    *exec,
3411                               gint            count,
3412                               time_t          stamp,
3413                               GError        **error)
3414 {
3415   GDateTime *stamp_dt = (stamp != (time_t) -1) ? g_date_time_new_from_unix_utc (stamp) : g_date_time_new_now_utc ();
3416   gboolean retval;
3417   retval = g_bookmark_file_set_application_info (bookmark, uri, name, exec, count,
3418                                                  stamp_dt, error);
3419   g_date_time_unref (stamp_dt);
3420   return retval;
3421 }
3422
3423 /**
3424  * g_bookmark_file_set_application_info:
3425  * @bookmark: a #GBookmarkFile
3426  * @uri: a valid URI
3427  * @name: an application's name
3428  * @exec: an application's command line
3429  * @count: the number of registrations done for this application
3430  * @stamp: (nullable): the time of the last registration for this application,
3431  *    which may be %NULL if @count is 0
3432  * @error: return location for a #GError or %NULL
3433  *
3434  * Sets the meta-data of application @name inside the list of
3435  * applications that have registered a bookmark for @uri inside
3436  * @bookmark.
3437  *
3438  * You should rarely use this function; use g_bookmark_file_add_application()
3439  * and g_bookmark_file_remove_application() instead.
3440  *
3441  * @name can be any UTF-8 encoded string used to identify an
3442  * application.
3443  * @exec can have one of these two modifiers: "\%f", which will
3444  * be expanded as the local file name retrieved from the bookmark's
3445  * URI; "\%u", which will be expanded as the bookmark's URI.
3446  * The expansion is done automatically when retrieving the stored
3447  * command line using the g_bookmark_file_get_application_info() function.
3448  * @count is the number of times the application has registered the
3449  * bookmark; if is < 0, the current registration count will be increased
3450  * by one, if is 0, the application with @name will be removed from
3451  * the list of registered applications.
3452  * @stamp is the Unix time of the last registration.
3453  *
3454  * If you try to remove an application by setting its registration count to
3455  * zero, and no bookmark for @uri is found, %FALSE is returned and
3456  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3457  * in the event that no application @name has registered a bookmark
3458  * for @uri,  %FALSE is returned and error is set to
3459  * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.  Otherwise, if no bookmark
3460  * for @uri is found, one is created.
3461  *
3462  * Returns: %TRUE if the application's meta-data was successfully
3463  *   changed.
3464  *
3465  * Since: 2.66
3466  */
3467 gboolean
3468 g_bookmark_file_set_application_info (GBookmarkFile  *bookmark,
3469                                       const char     *uri,
3470                                       const char     *name,
3471                                       const char     *exec,
3472                                       int             count,
3473                                       GDateTime      *stamp,
3474                                       GError        **error)
3475 {
3476   BookmarkItem *item;
3477   BookmarkAppInfo *ai;
3478
3479   g_return_val_if_fail (bookmark != NULL, FALSE);
3480   g_return_val_if_fail (uri != NULL, FALSE);
3481   g_return_val_if_fail (name != NULL, FALSE);
3482   g_return_val_if_fail (exec != NULL, FALSE);
3483   g_return_val_if_fail (count == 0 || stamp != NULL, FALSE);
3484   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3485
3486   item = g_bookmark_file_lookup_item (bookmark, uri);
3487   if (!item)
3488     {
3489       if (count == 0)
3490         {
3491           g_set_error (error, G_BOOKMARK_FILE_ERROR,
3492                        G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3493                        _("No bookmark found for URI “%s”"),
3494                        uri);
3495           return FALSE;
3496         }
3497       else
3498         {
3499           item = bookmark_item_new (uri);
3500           g_bookmark_file_add_item (bookmark, item, NULL);
3501         }
3502     }
3503
3504   if (!item->metadata)
3505     item->metadata = bookmark_metadata_new ();
3506
3507   ai = bookmark_item_lookup_app_info (item, name);
3508   if (!ai)
3509     {
3510       if (count == 0)
3511         {
3512           g_set_error (error, G_BOOKMARK_FILE_ERROR,
3513                        G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3514                        _("No application with name “%s” registered a bookmark for “%s”"),
3515                        name,
3516                        uri);
3517           return FALSE;
3518         }
3519       else
3520         {
3521           ai = bookmark_app_info_new (name);
3522
3523           item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3524           g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3525         }
3526     }
3527
3528   if (count == 0)
3529     {
3530       item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3531       g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3532       bookmark_app_info_free (ai);
3533
3534       bookmark_item_touch_modified (item);
3535
3536       return TRUE;
3537     }
3538   else if (count > 0)
3539     ai->count = count;
3540   else
3541     ai->count += 1;
3542
3543   g_clear_pointer (&ai->stamp, g_date_time_unref);
3544   ai->stamp = g_date_time_ref (stamp);
3545
3546   if (exec && exec[0] != '\0')
3547     {
3548       g_free (ai->exec);
3549       ai->exec = g_shell_quote (exec);
3550     }
3551
3552   bookmark_item_touch_modified (item);
3553
3554   return TRUE;
3555 }
3556
3557 /* expands the application's command line */
3558 static gchar *
3559 expand_exec_line (const gchar *exec_fmt,
3560                   const gchar *uri)
3561 {
3562   GString *exec;
3563   gchar ch;
3564
3565   exec = g_string_sized_new (512);
3566   while ((ch = *exec_fmt++) != '\0')
3567    {
3568      if (ch != '%')
3569        {
3570          exec = g_string_append_c (exec, ch);
3571          continue;
3572        }
3573
3574      ch = *exec_fmt++;
3575      switch (ch)
3576        {
3577        case '\0':
3578          goto out;
3579        case 'U':
3580        case 'u':
3581          g_string_append (exec, uri);
3582          break;
3583        case 'F':
3584        case 'f':
3585          {
3586            gchar *file = g_filename_from_uri (uri, NULL, NULL);
3587            if (file)
3588              {
3589                g_string_append (exec, file);
3590                g_free (file);
3591              }
3592            else
3593              {
3594                g_string_free (exec, TRUE);
3595                return NULL;
3596              }
3597          }
3598          break;
3599        case '%':
3600        default:
3601          exec = g_string_append_c (exec, ch);
3602          break;
3603        }
3604    }
3605
3606  out:
3607   return g_string_free (exec, FALSE);
3608 }
3609
3610 /**
3611  * g_bookmark_file_get_app_info:
3612  * @bookmark: a #GBookmarkFile
3613  * @uri: a valid URI
3614  * @name: an application's name
3615  * @exec: (out) (optional): return location for the command line of the application, or %NULL
3616  * @count: (out) (optional): return location for the registration count, or %NULL
3617  * @stamp: (out) (optional): return location for the last registration time, or %NULL
3618  * @error: return location for a #GError, or %NULL
3619  *
3620  * Gets the registration information of @app_name for the bookmark for
3621  * @uri.  See g_bookmark_file_set_application_info() for more information about
3622  * the returned data.
3623  *
3624  * The string returned in @app_exec must be freed.
3625  *
3626  * In the event the URI cannot be found, %FALSE is returned and
3627  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
3628  * event that no application with name @app_name has registered a bookmark
3629  * for @uri,  %FALSE is returned and error is set to
3630  * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3631  * the command line fails, an error of the %G_SHELL_ERROR domain is
3632  * set and %FALSE is returned.
3633  *
3634  * Returns: %TRUE on success.
3635  *
3636  * Since: 2.12
3637  * Deprecated: 2.66: Use g_bookmark_file_get_application_info() instead, as
3638  *    `time_t` is deprecated due to the year 2038 problem.
3639  */
3640 gboolean
3641 g_bookmark_file_get_app_info (GBookmarkFile  *bookmark,
3642                               const gchar    *uri,
3643                               const gchar    *name,
3644                               gchar         **exec,
3645                               guint          *count,
3646                               time_t         *stamp,
3647                               GError        **error)
3648 {
3649   GDateTime *stamp_dt = NULL;
3650   gboolean retval;
3651
3652   retval = g_bookmark_file_get_application_info (bookmark, uri, name, exec, count, &stamp_dt, error);
3653   if (!retval)
3654     return FALSE;
3655
3656   if (stamp != NULL)
3657     *stamp = g_date_time_to_unix (stamp_dt);
3658
3659   return TRUE;
3660 }
3661
3662 /**
3663  * g_bookmark_file_get_application_info:
3664  * @bookmark: a #GBookmarkFile
3665  * @uri: a valid URI
3666  * @name: an application's name
3667  * @exec: (out) (optional): return location for the command line of the application, or %NULL
3668  * @count: (out) (optional): return location for the registration count, or %NULL
3669  * @stamp: (out) (optional) (transfer none): return location for the last registration time, or %NULL
3670  * @error: return location for a #GError, or %NULL
3671  *
3672  * Gets the registration information of @app_name for the bookmark for
3673  * @uri.  See g_bookmark_file_set_application_info() for more information about
3674  * the returned data.
3675  *
3676  * The string returned in @app_exec must be freed.
3677  *
3678  * In the event the URI cannot be found, %FALSE is returned and
3679  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
3680  * event that no application with name @app_name has registered a bookmark
3681  * for @uri,  %FALSE is returned and error is set to
3682  * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3683  * the command line fails, an error of the %G_SHELL_ERROR domain is
3684  * set and %FALSE is returned.
3685  *
3686  * Returns: %TRUE on success.
3687  *
3688  * Since: 2.66
3689  */
3690 gboolean
3691 g_bookmark_file_get_application_info (GBookmarkFile  *bookmark,
3692                                       const char     *uri,
3693                                       const char     *name,
3694                                       char          **exec,
3695                                       unsigned int   *count,
3696                                       GDateTime     **stamp,
3697                                       GError        **error)
3698 {
3699   BookmarkItem *item;
3700   BookmarkAppInfo *ai;
3701
3702   g_return_val_if_fail (bookmark != NULL, FALSE);
3703   g_return_val_if_fail (uri != NULL, FALSE);
3704   g_return_val_if_fail (name != NULL, FALSE);
3705   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3706
3707   item = g_bookmark_file_lookup_item (bookmark, uri);
3708   if (!item)
3709     {
3710       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3711                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3712                    _("No bookmark found for URI “%s”"),
3713                    uri);
3714       return FALSE;
3715     }
3716
3717   ai = bookmark_item_lookup_app_info (item, name);
3718   if (!ai)
3719     {
3720       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3721                    G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3722                    _("No application with name “%s” registered a bookmark for “%s”"),
3723                    name,
3724                    uri);
3725       return FALSE;
3726     }
3727
3728   if (exec)
3729     {
3730       GError *unquote_error = NULL;
3731       gchar *command_line;
3732
3733       command_line = g_shell_unquote (ai->exec, &unquote_error);
3734       if (unquote_error)
3735         {
3736           g_propagate_error (error, unquote_error);
3737           return FALSE;
3738         }
3739
3740       *exec = expand_exec_line (command_line, uri);
3741       if (!*exec)
3742         {
3743           g_set_error (error, G_BOOKMARK_FILE_ERROR,
3744                        G_BOOKMARK_FILE_ERROR_INVALID_URI,
3745                        _("Failed to expand exec line “%s” with URI “%s”"),
3746                      ai->exec, uri);
3747           g_free (command_line);
3748
3749           return FALSE;
3750         }
3751       else
3752         g_free (command_line);
3753     }
3754
3755   if (count)
3756     *count = ai->count;
3757
3758   if (stamp)
3759     *stamp = ai->stamp;
3760
3761   return TRUE;
3762 }
3763
3764 /**
3765  * g_bookmark_file_get_applications:
3766  * @bookmark: a #GBookmarkFile
3767  * @uri: a valid URI
3768  * @length: (out) (optional): return location of the length of the returned list, or %NULL
3769  * @error: return location for a #GError, or %NULL
3770  *
3771  * Retrieves the names of the applications that have registered the
3772  * bookmark for @uri.
3773  *
3774  * In the event the URI cannot be found, %NULL is returned and
3775  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3776  *
3777  * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3778  *   Use g_strfreev() to free it.
3779  *
3780  * Since: 2.12
3781  */
3782 gchar **
3783 g_bookmark_file_get_applications (GBookmarkFile  *bookmark,
3784                                   const gchar    *uri,
3785                                   gsize          *length,
3786                                   GError        **error)
3787 {
3788   BookmarkItem *item;
3789   GList *l;
3790   gchar **apps;
3791   gsize i, n_apps;
3792
3793   g_return_val_if_fail (bookmark != NULL, NULL);
3794   g_return_val_if_fail (uri != NULL, NULL);
3795
3796   item = g_bookmark_file_lookup_item (bookmark, uri);
3797   if (!item)
3798     {
3799       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3800                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3801                    _("No bookmark found for URI “%s”"),
3802                    uri);
3803       return NULL;
3804     }
3805
3806   if (!item->metadata)
3807     {
3808       if (length)
3809         *length = 0;
3810
3811       return NULL;
3812     }
3813
3814   n_apps = g_list_length (item->metadata->applications);
3815   apps = g_new0 (gchar *, n_apps + 1);
3816
3817   for (l = g_list_last (item->metadata->applications), i = 0;
3818        l != NULL;
3819        l = l->prev)
3820     {
3821       BookmarkAppInfo *ai;
3822
3823       ai = (BookmarkAppInfo *) l->data;
3824
3825       g_warn_if_fail (ai != NULL);
3826       g_warn_if_fail (ai->name != NULL);
3827
3828       apps[i++] = g_strdup (ai->name);
3829     }
3830   apps[i] = NULL;
3831
3832   if (length)
3833     *length = i;
3834
3835   return apps;
3836 }
3837
3838 /**
3839  * g_bookmark_file_get_size:
3840  * @bookmark: a #GBookmarkFile
3841  *
3842  * Gets the number of bookmarks inside @bookmark.
3843  *
3844  * Returns: the number of bookmarks
3845  *
3846  * Since: 2.12
3847  */
3848 gint
3849 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3850 {
3851   g_return_val_if_fail (bookmark != NULL, 0);
3852
3853   return g_list_length (bookmark->items);
3854 }
3855
3856 /**
3857  * g_bookmark_file_move_item:
3858  * @bookmark: a #GBookmarkFile
3859  * @old_uri: a valid URI
3860  * @new_uri: (nullable): a valid URI, or %NULL
3861  * @error: return location for a #GError or %NULL
3862  *
3863  * Changes the URI of a bookmark item from @old_uri to @new_uri.  Any
3864  * existing bookmark for @new_uri will be overwritten.  If @new_uri is
3865  * %NULL, then the bookmark is removed.
3866  *
3867  * In the event the URI cannot be found, %FALSE is returned and
3868  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3869  *
3870  * Returns: %TRUE if the URI was successfully changed
3871  *
3872  * Since: 2.12
3873  */
3874 gboolean
3875 g_bookmark_file_move_item (GBookmarkFile  *bookmark,
3876                            const gchar    *old_uri,
3877                            const gchar    *new_uri,
3878                            GError        **error)
3879 {
3880   BookmarkItem *item;
3881
3882   g_return_val_if_fail (bookmark != NULL, FALSE);
3883   g_return_val_if_fail (old_uri != NULL, FALSE);
3884
3885   item = g_bookmark_file_lookup_item (bookmark, old_uri);
3886   if (!item)
3887     {
3888       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3889                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3890                    _("No bookmark found for URI “%s”"),
3891                    old_uri);
3892       return FALSE;
3893     }
3894
3895   if (new_uri && new_uri[0] != '\0')
3896     {
3897       if (g_strcmp0 (old_uri, new_uri) == 0)
3898         return TRUE;
3899
3900       if (g_bookmark_file_has_item (bookmark, new_uri))
3901         {
3902           if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3903             return FALSE;
3904         }
3905
3906       g_hash_table_steal (bookmark->items_by_uri, item->uri);
3907
3908       g_free (item->uri);
3909       item->uri = g_strdup (new_uri);
3910       bookmark_item_touch_modified (item);
3911
3912       g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3913
3914       return TRUE;
3915     }
3916   else
3917     {
3918       if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
3919         return FALSE;
3920
3921       return TRUE;
3922     }
3923 }
3924
3925 /**
3926  * g_bookmark_file_set_icon:
3927  * @bookmark: a #GBookmarkFile
3928  * @uri: a valid URI
3929  * @href: (nullable): the URI of the icon for the bookmark, or %NULL
3930  * @mime_type: the MIME type of the icon for the bookmark
3931  *
3932  * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3933  * the currently set icon. @href can either be a full URL for the icon
3934  * file or the icon name following the Icon Naming specification.
3935  *
3936  * If no bookmark for @uri is found one is created.
3937  *
3938  * Since: 2.12
3939  */
3940 void
3941 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3942                           const gchar   *uri,
3943                           const gchar   *href,
3944                           const gchar   *mime_type)
3945 {
3946   BookmarkItem *item;
3947
3948   g_return_if_fail (bookmark != NULL);
3949   g_return_if_fail (uri != NULL);
3950
3951   item = g_bookmark_file_lookup_item (bookmark, uri);
3952   if (!item)
3953     {
3954       item = bookmark_item_new (uri);
3955       g_bookmark_file_add_item (bookmark, item, NULL);
3956     }
3957
3958   if (!item->metadata)
3959     item->metadata = bookmark_metadata_new ();
3960
3961   g_free (item->metadata->icon_href);
3962   g_free (item->metadata->icon_mime);
3963
3964   item->metadata->icon_href = g_strdup (href);
3965
3966   if (mime_type && mime_type[0] != '\0')
3967     item->metadata->icon_mime = g_strdup (mime_type);
3968   else
3969     item->metadata->icon_mime = g_strdup ("application/octet-stream");
3970
3971   bookmark_item_touch_modified (item);
3972 }
3973
3974 /**
3975  * g_bookmark_file_get_icon:
3976  * @bookmark: a #GBookmarkFile
3977  * @uri: a valid URI
3978  * @href: (out) (optional): return location for the icon's location or %NULL
3979  * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL
3980  * @error: return location for a #GError or %NULL
3981  *
3982  * Gets the icon of the bookmark for @uri.
3983  *
3984  * In the event the URI cannot be found, %FALSE is returned and
3985  * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3986  *
3987  * Returns: %TRUE if the icon for the bookmark for the URI was found.
3988  *   You should free the returned strings.
3989  *
3990  * Since: 2.12
3991  */
3992 gboolean
3993 g_bookmark_file_get_icon (GBookmarkFile  *bookmark,
3994                           const gchar    *uri,
3995                           gchar         **href,
3996                           gchar         **mime_type,
3997                           GError        **error)
3998 {
3999   BookmarkItem *item;
4000
4001   g_return_val_if_fail (bookmark != NULL, FALSE);
4002   g_return_val_if_fail (uri != NULL, FALSE);
4003
4004   item = g_bookmark_file_lookup_item (bookmark, uri);
4005   if (!item)
4006     {
4007       g_set_error (error, G_BOOKMARK_FILE_ERROR,
4008                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
4009                    _("No bookmark found for URI “%s”"),
4010                    uri);
4011       return FALSE;
4012     }
4013
4014   if ((!item->metadata) || (!item->metadata->icon_href))
4015     return FALSE;
4016
4017   if (href)
4018     *href = g_strdup (item->metadata->icon_href);
4019
4020   if (mime_type)
4021     *mime_type = g_strdup (item->metadata->icon_mime);
4022
4023   return TRUE;
4024 }