1 /* gbookmarkfile.c: parsing and building desktop bookmarks
3 * Copyright (C) 2005-2006 Emmanuele Bassi
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
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.
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/>.
23 #include "gbookmarkfile.h"
36 #include "gdatetime.h"
38 #include "gfileutils.h"
45 #include "gmessages.h"
50 #include "gstrfuncs.h"
56 * SECTION:gbookmarkfile
57 * @title: Bookmark file parser
58 * @short_description: parses files containing bookmarks
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
65 * [Desktop Bookmark Specification](http://www.gnome.org/~ebassi/bookmark-spec).
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.
84 * Here is an example of a bookmark file:
85 * [bookmarks.xbel](https://gitlab.gnome.org/GNOME/glib/-/blob/HEAD/glib/tests/bookmarks.xbel)
87 * A bookmark file might contain more than one bookmark; each bookmark
88 * is accessed through its URI.
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().
97 * The #GBookmarkFile parser was added in GLib 2.12.
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"
106 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
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"
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 */
128 #define XBEL_YES_VALUE "yes"
129 #define XBEL_NO_VALUE "no"
131 /* Desktop bookmark spec entities */
132 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
134 #define BOOKMARK_NAMESPACE_NAME "bookmark"
135 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
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"
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"
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"
159 typedef struct _BookmarkAppInfo BookmarkAppInfo;
160 typedef struct _BookmarkMetadata BookmarkMetadata;
161 typedef struct _BookmarkItem BookmarkItem;
162 typedef struct _ParseData ParseData;
164 struct _BookmarkAppInfo
171 GDateTime *stamp; /* (owned) */
174 struct _BookmarkMetadata
181 GHashTable *apps_by_name;
186 guint is_private : 1;
196 GDateTime *added; /* (owned) */
197 GDateTime *modified; /* (owned) */
198 GDateTime *visited; /* (owned) */
200 BookmarkMetadata *metadata;
203 struct _GBookmarkFile
208 /* we store our items in a list and keep a copy inside
209 * a hash table for faster lookup performances
212 GHashTable *items_by_uri;
215 /* parser state machine */
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,
242 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
245 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
247 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
251 static gboolean timestamp_from_iso8601 (const gchar *iso_date,
252 GDateTime **out_date_time,
255 /********************************
258 * Application metadata storage *
259 ********************************/
260 static BookmarkAppInfo *
261 bookmark_app_info_new (const gchar *name)
263 BookmarkAppInfo *retval;
265 g_warn_if_fail (name != NULL);
267 retval = g_slice_new (BookmarkAppInfo);
269 retval->name = g_strdup (name);
272 retval->stamp = NULL;
278 bookmark_app_info_free (BookmarkAppInfo *app_info)
283 g_free (app_info->name);
284 g_free (app_info->exec);
285 g_clear_pointer (&app_info->stamp, g_date_time_unref);
287 g_slice_free (BookmarkAppInfo, app_info);
290 static BookmarkAppInfo *
291 bookmark_app_info_copy (BookmarkAppInfo *app_info)
293 BookmarkAppInfo *copy;
298 copy = bookmark_app_info_new (app_info->name);
299 copy->count = app_info->count;
300 copy->exec = g_strdup (app_info->exec);
303 copy->stamp = g_date_time_ref (app_info->stamp);
309 bookmark_app_info_dump (BookmarkAppInfo *app_info)
312 gchar *name, *exec, *modified, *count;
314 g_warn_if_fail (app_info != NULL);
316 if (app_info->count == 0)
319 name = g_markup_escape_text (app_info->name, -1);
320 exec = g_markup_escape_text (app_info->exec, -1);
321 count = g_strdup_printf ("%u", app_info->count);
327 tmp = g_date_time_format_iso8601 (app_info->stamp);
328 modified = g_strconcat (" " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", tmp, "\"",
334 modified = g_strdup ("");
337 retval = g_strconcat (" "
338 "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
339 " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
340 " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\"",
342 " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
354 /***********************
358 ***********************/
359 static BookmarkMetadata *
360 bookmark_metadata_new (void)
362 BookmarkMetadata *retval;
364 retval = g_slice_new (BookmarkMetadata);
366 retval->mime_type = NULL;
368 retval->groups = NULL;
370 retval->applications = NULL;
371 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
376 retval->is_private = FALSE;
378 retval->icon_href = NULL;
379 retval->icon_mime = NULL;
385 bookmark_metadata_free (BookmarkMetadata *metadata)
390 g_free (metadata->mime_type);
392 g_list_free_full (metadata->groups, g_free);
393 g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
395 g_hash_table_destroy (metadata->apps_by_name);
397 g_free (metadata->icon_href);
398 g_free (metadata->icon_mime);
400 g_slice_free (BookmarkMetadata, metadata);
403 static BookmarkMetadata *
404 bookmark_metadata_copy (BookmarkMetadata *metadata)
406 BookmarkMetadata *copy;
412 copy = bookmark_metadata_new ();
413 copy->is_private = metadata->is_private;
414 copy->mime_type = g_strdup (metadata->mime_type);
415 copy->icon_href = g_strdup (metadata->icon_href);
416 copy->icon_mime = g_strdup (metadata->icon_mime);
418 copy->groups = g_list_copy_deep (metadata->groups, (GCopyFunc) g_strdup, NULL);
420 g_list_copy_deep (metadata->applications, (GCopyFunc) bookmark_app_info_copy, NULL);
422 for (l = copy->applications; l; l = l->next)
424 BookmarkAppInfo *app_info = l->data;
425 g_hash_table_insert (copy->apps_by_name, app_info->name, app_info);
428 g_assert (g_hash_table_size (copy->apps_by_name) ==
429 g_hash_table_size (metadata->apps_by_name));
435 bookmark_metadata_dump (BookmarkMetadata *metadata)
440 if (!metadata->applications)
443 retval = g_string_sized_new (1024);
445 /* metadata container */
446 g_string_append (retval,
448 "<" XBEL_METADATA_ELEMENT
449 " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
453 if (metadata->mime_type) {
454 buffer = g_strconcat (" "
455 "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
456 MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
458 g_string_append (retval, buffer);
462 if (metadata->groups)
466 /* open groups container */
467 g_string_append (retval,
469 "<" BOOKMARK_NAMESPACE_NAME
470 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
472 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
476 group_name = g_markup_escape_text ((gchar *) l->data, -1);
477 buffer = g_strconcat (" "
478 "<" BOOKMARK_NAMESPACE_NAME
479 ":" BOOKMARK_GROUP_ELEMENT ">",
481 "</" BOOKMARK_NAMESPACE_NAME
482 ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
483 g_string_append (retval, buffer);
489 /* close groups container */
490 g_string_append (retval,
492 "</" BOOKMARK_NAMESPACE_NAME
493 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
496 if (metadata->applications)
500 /* open applications container */
501 g_string_append (retval,
503 "<" BOOKMARK_NAMESPACE_NAME
504 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
506 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
508 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
511 g_warn_if_fail (app_info != NULL);
513 app_data = bookmark_app_info_dump (app_info);
517 retval = g_string_append (retval, app_data);
523 /* close applications container */
524 g_string_append (retval,
526 "</" BOOKMARK_NAMESPACE_NAME
527 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
531 if (metadata->icon_href)
533 if (!metadata->icon_mime)
534 metadata->icon_mime = g_strdup ("application/octet-stream");
536 buffer = g_strconcat (" "
537 "<" BOOKMARK_NAMESPACE_NAME
538 ":" BOOKMARK_ICON_ELEMENT
539 " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
540 "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
541 g_string_append (retval, buffer);
547 if (metadata->is_private)
548 g_string_append (retval,
550 "<" BOOKMARK_NAMESPACE_NAME
551 ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
553 /* close metadata container */
554 g_string_append (retval,
556 "</" XBEL_METADATA_ELEMENT ">\n");
558 return g_string_free (retval, FALSE);
561 /******************************************************
564 * Storage for a single bookmark item inside the list *
565 ******************************************************/
566 static BookmarkItem *
567 bookmark_item_new (const gchar *uri)
571 g_warn_if_fail (uri != NULL);
573 item = g_slice_new (BookmarkItem);
574 item->uri = g_strdup (uri);
577 item->description = NULL;
580 item->modified = NULL;
581 item->visited = NULL;
583 item->metadata = NULL;
589 bookmark_item_free (BookmarkItem *item)
595 g_free (item->title);
596 g_free (item->description);
599 bookmark_metadata_free (item->metadata);
601 g_clear_pointer (&item->added, g_date_time_unref);
602 g_clear_pointer (&item->modified, g_date_time_unref);
603 g_clear_pointer (&item->visited, g_date_time_unref);
605 g_slice_free (BookmarkItem, item);
608 static BookmarkItem *
609 bookmark_item_copy (BookmarkItem *item)
616 copy = bookmark_item_new (item->uri);
618 copy->title = g_strdup (item->title);
619 copy->description = g_strdup (item->description);
621 copy->metadata = bookmark_metadata_copy (item->metadata);
624 copy->added = g_date_time_ref (item->added);
626 copy->modified = g_date_time_ref (item->modified);
628 copy->visited = g_date_time_ref (item->visited);
634 bookmark_item_touch_modified (BookmarkItem *item)
636 g_clear_pointer (&item->modified, g_date_time_unref);
637 item->modified = g_date_time_new_now_utc ();
641 bookmark_item_dump (BookmarkItem *item)
646 /* at this point, we must have at least a registered application; if we don't
647 * we don't screw up the bookmark file, and just skip this item
649 if (!item->metadata || !item->metadata->applications)
651 g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri);
655 retval = g_string_sized_new (4096);
657 g_string_append (retval, " <" XBEL_BOOKMARK_ELEMENT " ");
659 escaped_uri = g_markup_escape_text (item->uri, -1);
661 g_string_append (retval, XBEL_HREF_ATTRIBUTE "=\"");
662 g_string_append (retval, escaped_uri);
663 g_string_append (retval , "\" ");
665 g_free (escaped_uri);
671 added = g_date_time_format_iso8601 (item->added);
672 g_string_append (retval, XBEL_ADDED_ATTRIBUTE "=\"");
673 g_string_append (retval, added);
674 g_string_append (retval, "\" ");
682 modified = g_date_time_format_iso8601 (item->modified);
683 g_string_append (retval, XBEL_MODIFIED_ATTRIBUTE "=\"");
684 g_string_append (retval, modified);
685 g_string_append (retval, "\" ");
693 visited = g_date_time_format_iso8601 (item->visited);
694 g_string_append (retval, XBEL_VISITED_ATTRIBUTE "=\"");
695 g_string_append (retval, visited);
696 g_string_append (retval, "\" ");
700 if (retval->str[retval->len - 1] == ' ')
701 g_string_truncate (retval, retval->len - 1);
702 g_string_append (retval, ">\n");
706 gchar *escaped_title;
708 escaped_title = g_markup_escape_text (item->title, -1);
709 g_string_append (retval, " " "<" XBEL_TITLE_ELEMENT ">");
710 g_string_append (retval, escaped_title);
711 g_string_append (retval, "</" XBEL_TITLE_ELEMENT ">\n");
713 g_free (escaped_title);
716 if (item->description)
720 escaped_desc = g_markup_escape_text (item->description, -1);
721 g_string_append (retval, " " "<" XBEL_DESC_ELEMENT ">");
722 g_string_append (retval, escaped_desc);
723 g_string_append (retval, "</" XBEL_DESC_ELEMENT ">\n");
725 g_free (escaped_desc);
732 metadata = bookmark_metadata_dump (item->metadata);
735 g_string_append (retval, " " "<" XBEL_INFO_ELEMENT ">\n");
736 g_string_append (retval, metadata);
737 g_string_append (retval, " " "</" XBEL_INFO_ELEMENT ">\n");
743 g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
745 return g_string_free (retval, FALSE);
748 static BookmarkAppInfo *
749 bookmark_item_lookup_app_info (BookmarkItem *item,
750 const gchar *app_name)
752 g_warn_if_fail (item != NULL && app_name != NULL);
757 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
760 /*************************
762 *************************/
765 g_bookmark_file_init (GBookmarkFile *bookmark)
767 bookmark->title = NULL;
768 bookmark->description = NULL;
770 bookmark->items = NULL;
771 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
778 g_bookmark_file_clear (GBookmarkFile *bookmark)
780 g_free (bookmark->title);
781 g_free (bookmark->description);
783 g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
784 bookmark->items = NULL;
786 g_clear_pointer (&bookmark->items_by_uri, g_hash_table_unref);
793 GHashTable *namespaces;
795 GBookmarkFile *bookmark_file;
796 BookmarkItem *current_item;
800 parse_data_new (void)
804 retval = g_new (ParseData, 1);
806 retval->state = STATE_STARTED;
807 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
808 (GDestroyNotify) g_free,
809 (GDestroyNotify) g_free);
810 retval->bookmark_file = NULL;
811 retval->current_item = NULL;
817 parse_data_free (ParseData *parse_data)
819 g_hash_table_destroy (parse_data->namespaces);
824 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
827 parse_bookmark_element (GMarkupParseContext *context,
828 ParseData *parse_data,
829 const gchar **attribute_names,
830 const gchar **attribute_values,
833 const gchar *uri, *added, *modified, *visited;
839 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
842 uri = added = modified = visited = NULL;
843 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
845 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
846 uri = attribute_values[i];
847 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
848 added = attribute_values[i];
849 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
850 modified = attribute_values[i];
851 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
852 visited = attribute_values[i];
855 /* bookmark is defined by the XBEL spec, so we need
856 * to error out if the element has different or
859 g_set_error (error, G_MARKUP_ERROR,
860 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
861 _("Unexpected attribute “%s” for element “%s”"),
863 XBEL_BOOKMARK_ELEMENT);
870 g_set_error (error, G_MARKUP_ERROR,
871 G_MARKUP_ERROR_INVALID_CONTENT,
872 _("Attribute “%s” of element “%s” not found"),
874 XBEL_BOOKMARK_ELEMENT);
878 g_warn_if_fail (parse_data->current_item == NULL);
880 item = bookmark_item_new (uri);
882 if (added != NULL && !timestamp_from_iso8601 (added, &item->added, error))
884 bookmark_item_free (item);
888 if (modified != NULL && !timestamp_from_iso8601 (modified, &item->modified, error))
890 bookmark_item_free (item);
894 if (visited != NULL && !timestamp_from_iso8601 (visited, &item->visited, error))
896 bookmark_item_free (item);
901 g_bookmark_file_add_item (parse_data->bookmark_file,
906 bookmark_item_free (item);
908 g_propagate_error (error, add_error);
913 parse_data->current_item = item;
917 parse_application_element (GMarkupParseContext *context,
918 ParseData *parse_data,
919 const gchar **attribute_names,
920 const gchar **attribute_values,
923 const gchar *name, *exec, *count, *stamp, *modified;
929 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
932 name = exec = count = stamp = modified = NULL;
933 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
935 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
936 name = attribute_values[i];
937 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
938 exec = attribute_values[i];
939 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
940 count = attribute_values[i];
941 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
942 stamp = attribute_values[i];
943 else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
944 modified = attribute_values[i];
947 /* the "name" and "exec" attributes are mandatory */
950 g_set_error (error, G_MARKUP_ERROR,
951 G_MARKUP_ERROR_INVALID_CONTENT,
952 _("Attribute “%s” of element “%s” not found"),
953 BOOKMARK_NAME_ATTRIBUTE,
954 BOOKMARK_APPLICATION_ELEMENT);
960 g_set_error (error, G_MARKUP_ERROR,
961 G_MARKUP_ERROR_INVALID_CONTENT,
962 _("Attribute “%s” of element “%s” not found"),
963 BOOKMARK_EXEC_ATTRIBUTE,
964 BOOKMARK_APPLICATION_ELEMENT);
968 g_warn_if_fail (parse_data->current_item != NULL);
969 item = parse_data->current_item;
971 ai = bookmark_item_lookup_app_info (item, name);
974 ai = bookmark_app_info_new (name);
977 item->metadata = bookmark_metadata_new ();
979 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
980 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
984 ai->exec = g_strdup (exec);
987 ai->count = atoi (count);
991 g_clear_pointer (&ai->stamp, g_date_time_unref);
992 if (modified != NULL)
994 if (!timestamp_from_iso8601 (modified, &ai->stamp, error))
999 /* the timestamp attribute has been deprecated but we still parse
1000 * it for backward compatibility
1003 ai->stamp = g_date_time_new_from_unix_utc (atol (stamp));
1005 ai->stamp = g_date_time_new_now_utc ();
1010 parse_mime_type_element (GMarkupParseContext *context,
1011 ParseData *parse_data,
1012 const gchar **attribute_names,
1013 const gchar **attribute_values,
1021 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
1025 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
1027 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
1028 type = attribute_values[i];
1032 type = "application/octet-stream";
1034 g_warn_if_fail (parse_data->current_item != NULL);
1035 item = parse_data->current_item;
1037 if (!item->metadata)
1038 item->metadata = bookmark_metadata_new ();
1040 g_free (item->metadata->mime_type);
1041 item->metadata->mime_type = g_strdup (type);
1045 parse_icon_element (GMarkupParseContext *context,
1046 ParseData *parse_data,
1047 const gchar **attribute_names,
1048 const gchar **attribute_values,
1057 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
1062 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
1064 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
1065 href = attribute_values[i];
1066 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
1067 type = attribute_values[i];
1070 /* the "href" attribute is mandatory */
1073 g_set_error (error, G_MARKUP_ERROR,
1074 G_MARKUP_ERROR_INVALID_CONTENT,
1075 _("Attribute “%s” of element “%s” not found"),
1076 BOOKMARK_HREF_ATTRIBUTE,
1077 BOOKMARK_ICON_ELEMENT);
1082 type = "application/octet-stream";
1084 g_warn_if_fail (parse_data->current_item != NULL);
1085 item = parse_data->current_item;
1087 if (!item->metadata)
1088 item->metadata = bookmark_metadata_new ();
1090 g_free (item->metadata->icon_href);
1091 g_free (item->metadata->icon_mime);
1092 item->metadata->icon_href = g_strdup (href);
1093 item->metadata->icon_mime = g_strdup (type);
1096 /* scans through the attributes of an element for the "xmlns" pragma, and
1097 * adds any resulting namespace declaration to a per-parser hashtable, using
1098 * the namespace name as a key for the namespace URI; if no key was found,
1099 * the namespace is considered as default, and stored under the "default" key.
1101 * FIXME: this works on the assumption that the generator of the XBEL file
1102 * is either this code or is smart enough to place the namespace declarations
1103 * inside the main root node or inside the metadata node and does not redefine
1104 * a namespace inside an inner node; this does *not* conform to the
1105 * XML-NS standard, although is a close approximation. In order to make this
1106 * conformant to the XML-NS specification we should use a per-element
1107 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
1110 map_namespace_to_name (ParseData *parse_data,
1111 const gchar **attribute_names,
1112 const gchar **attribute_values)
1117 g_warn_if_fail (parse_data != NULL);
1119 if (!attribute_names || !attribute_names[0])
1123 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1125 if (g_str_has_prefix (attr, "xmlns"))
1127 gchar *namespace_name, *namespace_uri;
1130 p = g_utf8_strchr (attr, -1, ':');
1132 p = g_utf8_next_char (p);
1136 namespace_name = g_strdup (p);
1137 namespace_uri = g_strdup (attribute_values[i]);
1139 g_hash_table_replace (parse_data->namespaces,
1146 /* checks whether @element_full is equal to @element.
1148 * if @namespace is set, it tries to resolve the namespace to a known URI,
1149 * and if found is prepended to the element name, from which is separated
1150 * using the character specified in the @sep parameter.
1153 is_element_full (ParseData *parse_data,
1154 const gchar *element_full,
1155 const gchar *namespace,
1156 const gchar *element,
1159 gchar *ns_uri, *ns_name;
1160 const gchar *p, *element_name;
1163 g_warn_if_fail (parse_data != NULL);
1164 g_warn_if_fail (element_full != NULL);
1169 /* no namespace requested: dumb element compare */
1171 return (0 == strcmp (element_full, element));
1173 /* search for namespace separator; if none found, assume we are under the
1174 * default namespace, and set ns_name to our "default" marker; if no default
1175 * namespace has been set, just do a plain comparison between @full_element
1178 p = g_utf8_strchr (element_full, -1, ':');
1181 ns_name = g_strndup (element_full, p - element_full);
1182 element_name = g_utf8_next_char (p);
1186 ns_name = g_strdup ("default");
1187 element_name = element_full;
1190 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1193 /* no default namespace found */
1196 return (0 == strcmp (element_full, element));
1199 retval = (0 == strcmp (ns_uri, namespace) &&
1200 0 == strcmp (element_name, element));
1207 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1208 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1210 static const gchar *
1211 parser_state_to_element_name (ParserState state)
1216 case STATE_FINISHED:
1217 return "(top-level)";
1219 return XBEL_ROOT_ELEMENT;
1220 case STATE_BOOKMARK:
1221 return XBEL_BOOKMARK_ELEMENT;
1223 return XBEL_TITLE_ELEMENT;
1225 return XBEL_DESC_ELEMENT;
1227 return XBEL_INFO_ELEMENT;
1228 case STATE_METADATA:
1229 return XBEL_METADATA_ELEMENT;
1230 case STATE_APPLICATIONS:
1231 return BOOKMARK_APPLICATIONS_ELEMENT;
1232 case STATE_APPLICATION:
1233 return BOOKMARK_APPLICATION_ELEMENT;
1235 return BOOKMARK_GROUPS_ELEMENT;
1237 return BOOKMARK_GROUP_ELEMENT;
1239 return MIME_TYPE_ELEMENT;
1241 return BOOKMARK_ICON_ELEMENT;
1243 g_assert_not_reached ();
1248 start_element_raw_cb (GMarkupParseContext *context,
1249 const gchar *element_name,
1250 const gchar **attribute_names,
1251 const gchar **attribute_values,
1255 ParseData *parse_data = (ParseData *) user_data;
1257 /* we must check for namespace declarations first
1259 * XXX - we could speed up things by checking for namespace declarations
1260 * only on the root node, where they usually are; this would probably break
1261 * on streams not produced by us or by "smart" generators
1263 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1265 switch (parse_data->state)
1268 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1274 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1276 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1277 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1278 parse_data->state = STATE_ROOT;
1282 g_set_error (error, G_MARKUP_ERROR,
1283 G_MARKUP_ERROR_INVALID_CONTENT,
1284 _("Unexpected tag “%s”, tag “%s” expected"),
1285 element_name, XBEL_ROOT_ELEMENT);
1288 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1289 parse_data->state = STATE_TITLE;
1290 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1291 parse_data->state = STATE_DESC;
1292 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1294 GError *inner_error = NULL;
1296 parse_data->state = STATE_BOOKMARK;
1298 parse_bookmark_element (context,
1304 g_propagate_error (error, inner_error);
1307 g_set_error (error, G_MARKUP_ERROR,
1308 G_MARKUP_ERROR_INVALID_CONTENT,
1309 _("Unexpected tag “%s” inside “%s”"),
1313 case STATE_BOOKMARK:
1314 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1315 parse_data->state = STATE_TITLE;
1316 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1317 parse_data->state = STATE_DESC;
1318 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1319 parse_data->state = STATE_INFO;
1321 g_set_error (error, G_MARKUP_ERROR,
1322 G_MARKUP_ERROR_INVALID_CONTENT,
1323 _("Unexpected tag “%s” inside “%s”"),
1325 XBEL_BOOKMARK_ELEMENT);
1328 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1334 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1336 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1337 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1339 parse_data->state = STATE_METADATA;
1341 if (!parse_data->current_item->metadata)
1342 parse_data->current_item->metadata = bookmark_metadata_new ();
1347 g_set_error (error, G_MARKUP_ERROR,
1348 G_MARKUP_ERROR_INVALID_CONTENT,
1349 _("Unexpected tag “%s”, tag “%s” expected"),
1351 XBEL_METADATA_ELEMENT);
1353 case STATE_METADATA:
1354 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1355 parse_data->state = STATE_APPLICATIONS;
1356 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1357 parse_data->state = STATE_GROUPS;
1358 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1359 parse_data->current_item->metadata->is_private = TRUE;
1360 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1362 GError *inner_error = NULL;
1364 parse_data->state = STATE_ICON;
1366 parse_icon_element (context,
1372 g_propagate_error (error, inner_error);
1374 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1376 GError *inner_error = NULL;
1378 parse_data->state = STATE_MIME;
1380 parse_mime_type_element (context,
1386 g_propagate_error (error, inner_error);
1389 g_set_error (error, G_MARKUP_ERROR,
1390 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1391 _("Unexpected tag “%s” inside “%s”"),
1393 XBEL_METADATA_ELEMENT);
1395 case STATE_APPLICATIONS:
1396 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1398 GError *inner_error = NULL;
1400 parse_data->state = STATE_APPLICATION;
1402 parse_application_element (context,
1408 g_propagate_error (error, inner_error);
1411 g_set_error (error, G_MARKUP_ERROR,
1412 G_MARKUP_ERROR_INVALID_CONTENT,
1413 _("Unexpected tag “%s”, tag “%s” expected"),
1415 BOOKMARK_APPLICATION_ELEMENT);
1418 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1419 parse_data->state = STATE_GROUP;
1421 g_set_error (error, G_MARKUP_ERROR,
1422 G_MARKUP_ERROR_INVALID_CONTENT,
1423 _("Unexpected tag “%s”, tag “%s” expected"),
1425 BOOKMARK_GROUP_ELEMENT);
1430 case STATE_APPLICATION:
1434 case STATE_FINISHED:
1435 g_set_error (error, G_MARKUP_ERROR,
1436 G_MARKUP_ERROR_INVALID_CONTENT,
1437 _("Unexpected tag “%s” inside “%s”"),
1439 parser_state_to_element_name (parse_data->state));
1443 g_assert_not_reached ();
1449 end_element_raw_cb (GMarkupParseContext *context,
1450 const gchar *element_name,
1454 ParseData *parse_data = (ParseData *) user_data;
1456 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1457 parse_data->state = STATE_FINISHED;
1458 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1460 parse_data->current_item = NULL;
1462 parse_data->state = STATE_ROOT;
1464 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1465 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1466 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1468 if (parse_data->current_item)
1469 parse_data->state = STATE_BOOKMARK;
1471 parse_data->state = STATE_ROOT;
1473 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1474 parse_data->state = STATE_INFO;
1475 else if (IS_ELEMENT_NS (parse_data, element_name,
1476 BOOKMARK_NAMESPACE_URI,
1477 BOOKMARK_APPLICATION_ELEMENT))
1478 parse_data->state = STATE_APPLICATIONS;
1479 else if (IS_ELEMENT_NS (parse_data, element_name,
1480 BOOKMARK_NAMESPACE_URI,
1481 BOOKMARK_GROUP_ELEMENT))
1482 parse_data->state = STATE_GROUPS;
1483 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1484 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1485 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1486 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1487 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1488 parse_data->state = STATE_METADATA;
1492 text_raw_cb (GMarkupParseContext *context,
1498 ParseData *parse_data = (ParseData *) user_data;
1501 payload = g_strndup (text, length);
1503 switch (parse_data->state)
1506 if (parse_data->current_item)
1508 g_free (parse_data->current_item->title);
1509 parse_data->current_item->title = g_strdup (payload);
1513 g_free (parse_data->bookmark_file->title);
1514 parse_data->bookmark_file->title = g_strdup (payload);
1518 if (parse_data->current_item)
1520 g_free (parse_data->current_item->description);
1521 parse_data->current_item->description = g_strdup (payload);
1525 g_free (parse_data->bookmark_file->description);
1526 parse_data->bookmark_file->description = g_strdup (payload);
1533 g_warn_if_fail (parse_data->current_item != NULL);
1535 if (!parse_data->current_item->metadata)
1536 parse_data->current_item->metadata = bookmark_metadata_new ();
1538 groups = parse_data->current_item->metadata->groups;
1539 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1543 case STATE_BOOKMARK:
1545 case STATE_METADATA:
1546 case STATE_APPLICATIONS:
1547 case STATE_APPLICATION:
1553 g_warn_if_reached ();
1560 static const GMarkupParser markup_parser =
1562 start_element_raw_cb, /* start_element */
1563 end_element_raw_cb, /* end_element */
1564 text_raw_cb, /* text */
1565 NULL, /* passthrough */
1570 g_bookmark_file_parse (GBookmarkFile *bookmark,
1571 const gchar *buffer,
1575 GMarkupParseContext *context;
1576 ParseData *parse_data;
1577 GError *parse_error, *end_error;
1580 g_warn_if_fail (bookmark != NULL);
1588 if (length == (gsize) -1)
1589 length = strlen (buffer);
1591 parse_data = parse_data_new ();
1592 parse_data->bookmark_file = bookmark;
1594 context = g_markup_parse_context_new (&markup_parser,
1595 G_MARKUP_DEFAULT_FLAGS,
1597 (GDestroyNotify) parse_data_free);
1599 retval = g_markup_parse_context_parse (context,
1604 g_propagate_error (error, parse_error);
1607 retval = g_markup_parse_context_end_parse (context, &end_error);
1609 g_propagate_error (error, end_error);
1612 g_markup_parse_context_free (context);
1618 g_bookmark_file_dump (GBookmarkFile *bookmark,
1626 retval = g_string_sized_new (4096);
1628 g_string_append (retval,
1629 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1631 /* XXX - do we really need the doctype? */
1632 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1633 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1634 " \"" XBEL_DTD_URI "\">\n"
1636 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1637 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1638 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1640 if (bookmark->title)
1642 gchar *escaped_title;
1644 escaped_title = g_markup_escape_text (bookmark->title, -1);
1646 buffer = g_strconcat (" "
1647 "<" XBEL_TITLE_ELEMENT ">",
1649 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1651 g_string_append (retval, buffer);
1654 g_free (escaped_title);
1657 if (bookmark->description)
1659 gchar *escaped_desc;
1661 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1663 buffer = g_strconcat (" "
1664 "<" XBEL_DESC_ELEMENT ">",
1666 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1667 g_string_append (retval, buffer);
1670 g_free (escaped_desc);
1673 if (!bookmark->items)
1676 retval = g_string_append (retval, "\n");
1678 /* the items are stored in reverse order */
1679 for (l = g_list_last (bookmark->items);
1683 BookmarkItem *item = (BookmarkItem *) l->data;
1686 item_dump = bookmark_item_dump (item);
1690 retval = g_string_append (retval, item_dump);
1696 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1699 *length = retval->len;
1701 return g_string_free (retval, FALSE);
1709 timestamp_from_iso8601 (const gchar *iso_date,
1710 GDateTime **out_date_time,
1713 GDateTime *dt = g_date_time_new_from_iso8601 (iso_date, NULL);
1716 g_set_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_READ,
1717 _("Invalid date/time ‘%s’ in bookmark file"), iso_date);
1721 *out_date_time = g_steal_pointer (&dt);
1725 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1727 /********************
1729 ********************/
1732 * g_bookmark_file_new: (constructor)
1734 * Creates a new empty #GBookmarkFile object.
1736 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1737 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1740 * Returns: an empty #GBookmarkFile
1745 g_bookmark_file_new (void)
1747 GBookmarkFile *bookmark;
1749 bookmark = g_new (GBookmarkFile, 1);
1751 g_bookmark_file_init (bookmark);
1757 * g_bookmark_file_copy:
1758 * @bookmark: A #GBookmarkFile
1760 * Deeply copies a @bookmark #GBookmarkFile object to a new one.
1762 * Returns: (transfer full): the copy of @bookmark. Use
1763 * g_bookmark_free() when finished using it.
1768 g_bookmark_file_copy (GBookmarkFile *bookmark)
1770 GBookmarkFile *copy;
1773 g_return_val_if_fail (bookmark != NULL, NULL);
1775 copy = g_bookmark_file_new ();
1776 copy->title = g_strdup (bookmark->title);
1777 copy->description = g_strdup (bookmark->description);
1778 copy->items = g_list_copy_deep (bookmark->items, (GCopyFunc) bookmark_item_copy, NULL);
1780 for (l = copy->items; l; l = l->next)
1782 BookmarkItem *item = l->data;
1783 g_hash_table_insert (copy->items_by_uri, item->uri, item);
1786 g_assert (g_hash_table_size (copy->items_by_uri) ==
1787 g_hash_table_size (bookmark->items_by_uri));
1793 * g_bookmark_file_free:
1794 * @bookmark: a #GBookmarkFile
1796 * Frees a #GBookmarkFile.
1801 g_bookmark_file_free (GBookmarkFile *bookmark)
1806 g_bookmark_file_clear (bookmark);
1812 * g_bookmark_file_load_from_data:
1813 * @bookmark: an empty #GBookmarkFile struct
1814 * @data: (array length=length) (element-type guint8): desktop bookmarks
1816 * @length: the length of @data in bytes
1817 * @error: return location for a #GError, or %NULL
1819 * Loads a bookmark file from memory into an empty #GBookmarkFile
1820 * structure. If the object cannot be created then @error is set to a
1821 * #GBookmarkFileError.
1823 * Returns: %TRUE if a desktop bookmark could be loaded.
1828 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1833 GError *parse_error;
1836 g_return_val_if_fail (bookmark != NULL, FALSE);
1838 if (length == (gsize) -1)
1839 length = strlen (data);
1841 if (bookmark->items)
1843 g_bookmark_file_clear (bookmark);
1844 g_bookmark_file_init (bookmark);
1848 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1851 g_propagate_error (error, parse_error);
1857 * g_bookmark_file_load_from_file:
1858 * @bookmark: an empty #GBookmarkFile struct
1859 * @filename: (type filename): the path of a filename to load, in the
1860 * GLib file name encoding
1861 * @error: return location for a #GError, or %NULL
1863 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1864 * If the file could not be loaded then @error is set to either a #GFileError
1865 * or #GBookmarkFileError.
1867 * Returns: %TRUE if a desktop bookmark file could be loaded
1872 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1873 const gchar *filename,
1876 gboolean ret = FALSE;
1877 gchar *buffer = NULL;
1880 g_return_val_if_fail (bookmark != NULL, FALSE);
1881 g_return_val_if_fail (filename != NULL, FALSE);
1883 if (!g_file_get_contents (filename, &buffer, &len, error))
1886 if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1896 /* Iterates through all the directories in *dirs trying to
1897 * find file. When it successfully locates file, returns a
1898 * string its absolute path. It also leaves the unchecked
1899 * directories in *dirs. You should free the returned string
1901 * Adapted from gkeyfile.c
1904 find_file_in_data_dirs (const gchar *file,
1908 gchar **data_dirs, *data_dir, *path;
1917 while (data_dirs && (data_dir = *data_dirs) && !path)
1919 gchar *candidate_file, *sub_dir;
1921 candidate_file = (gchar *) file;
1922 sub_dir = g_strdup ("");
1923 while (candidate_file != NULL && !path)
1927 path = g_build_filename (data_dir, sub_dir,
1928 candidate_file, NULL);
1930 candidate_file = strchr (candidate_file, '-');
1932 if (candidate_file == NULL)
1938 sub_dir = g_strndup (file, candidate_file - file - 1);
1940 for (p = sub_dir; *p != '\0'; p++)
1943 *p = G_DIR_SEPARATOR;
1954 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1955 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1956 _("No valid bookmark file found in data dirs"));
1966 * g_bookmark_file_load_from_data_dirs:
1967 * @bookmark: a #GBookmarkFile
1968 * @file: (type filename): a relative path to a filename to open and parse
1969 * @full_path: (out) (optional) (type filename): return location for a string
1970 * containing the full path of the file, or %NULL
1971 * @error: return location for a #GError, or %NULL
1973 * This function looks for a desktop bookmark file named @file in the
1974 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1975 * loads the file into @bookmark and returns the file's full path in
1976 * @full_path. If the file could not be loaded then @error is
1977 * set to either a #GFileError or #GBookmarkFileError.
1979 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1984 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1989 GError *file_error = NULL;
1990 gchar **all_data_dirs, **data_dirs;
1991 const gchar *user_data_dir;
1992 const gchar * const * system_data_dirs;
1995 gboolean found_file;
1997 g_return_val_if_fail (bookmark != NULL, FALSE);
1998 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
2000 user_data_dir = g_get_user_data_dir ();
2001 system_data_dirs = g_get_system_data_dirs ();
2002 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
2005 all_data_dirs[i++] = g_strdup (user_data_dir);
2008 while (system_data_dirs[j] != NULL)
2009 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
2012 data_dirs = all_data_dirs;
2014 while (*data_dirs != NULL && !found_file)
2016 g_free (output_path);
2018 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
2022 g_propagate_error (error, file_error);
2026 found_file = g_bookmark_file_load_from_file (bookmark,
2031 g_propagate_error (error, file_error);
2036 if (found_file && full_path)
2037 *full_path = output_path;
2039 g_free (output_path);
2041 g_strfreev (all_data_dirs);
2048 * g_bookmark_file_to_data:
2049 * @bookmark: a #GBookmarkFile
2050 * @length: (out) (optional): return location for the length of the returned string, or %NULL
2051 * @error: return location for a #GError, or %NULL
2053 * This function outputs @bookmark as a string.
2055 * Returns: (transfer full) (array length=length) (element-type guint8):
2056 * a newly allocated string holding the contents of the #GBookmarkFile
2061 g_bookmark_file_to_data (GBookmarkFile *bookmark,
2065 GError *write_error = NULL;
2068 g_return_val_if_fail (bookmark != NULL, NULL);
2070 retval = g_bookmark_file_dump (bookmark, length, &write_error);
2073 g_propagate_error (error, write_error);
2082 * g_bookmark_file_to_file:
2083 * @bookmark: a #GBookmarkFile
2084 * @filename: (type filename): path of the output file
2085 * @error: return location for a #GError, or %NULL
2087 * This function outputs @bookmark into a file. The write process is
2088 * guaranteed to be atomic by using g_file_set_contents() internally.
2090 * Returns: %TRUE if the file was successfully written.
2095 g_bookmark_file_to_file (GBookmarkFile *bookmark,
2096 const gchar *filename,
2100 GError *data_error, *write_error;
2104 g_return_val_if_fail (bookmark != NULL, FALSE);
2105 g_return_val_if_fail (filename != NULL, FALSE);
2108 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
2111 g_propagate_error (error, data_error);
2117 g_file_set_contents (filename, data, len, &write_error);
2120 g_propagate_error (error, write_error);
2132 static BookmarkItem *
2133 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
2136 g_warn_if_fail (bookmark != NULL && uri != NULL);
2138 return g_hash_table_lookup (bookmark->items_by_uri, uri);
2141 /* this function adds a new item to the list */
2143 g_bookmark_file_add_item (GBookmarkFile *bookmark,
2147 g_warn_if_fail (bookmark != NULL);
2148 g_warn_if_fail (item != NULL);
2150 /* this should never happen; and if it does, then we are
2151 * screwing up something big time.
2153 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2155 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2156 G_BOOKMARK_FILE_ERROR_INVALID_URI,
2157 _("A bookmark for URI “%s” already exists"),
2162 bookmark->items = g_list_prepend (bookmark->items, item);
2164 g_hash_table_replace (bookmark->items_by_uri,
2168 if (item->added == NULL)
2169 item->added = g_date_time_new_now_utc ();
2171 if (item->modified == NULL)
2172 item->modified = g_date_time_new_now_utc ();
2174 if (item->visited == NULL)
2175 item->visited = g_date_time_new_now_utc ();
2179 * g_bookmark_file_remove_item:
2180 * @bookmark: a #GBookmarkFile
2182 * @error: return location for a #GError, or %NULL
2184 * Removes the bookmark for @uri from the bookmark file @bookmark.
2186 * Returns: %TRUE if the bookmark was removed successfully.
2191 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2197 g_return_val_if_fail (bookmark != NULL, FALSE);
2198 g_return_val_if_fail (uri != NULL, FALSE);
2200 item = g_bookmark_file_lookup_item (bookmark, uri);
2204 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2205 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2206 _("No bookmark found for URI “%s”"),
2211 bookmark->items = g_list_remove (bookmark->items, item);
2212 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2214 bookmark_item_free (item);
2220 * g_bookmark_file_has_item:
2221 * @bookmark: a #GBookmarkFile
2224 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2226 * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2231 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2234 g_return_val_if_fail (bookmark != NULL, FALSE);
2235 g_return_val_if_fail (uri != NULL, FALSE);
2237 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2241 * g_bookmark_file_get_uris:
2242 * @bookmark: a #GBookmarkFile
2243 * @length: (out) (optional): return location for the number of returned URIs, or %NULL
2245 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2246 * The array of returned URIs will be %NULL-terminated, so @length may
2247 * optionally be %NULL.
2249 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2250 * Use g_strfreev() to free it.
2255 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2262 g_return_val_if_fail (bookmark != NULL, NULL);
2264 n_items = g_list_length (bookmark->items);
2265 uris = g_new0 (gchar *, n_items + 1);
2267 /* the items are stored in reverse order, so we walk the list backward */
2268 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2270 BookmarkItem *item = (BookmarkItem *) l->data;
2272 g_warn_if_fail (item != NULL);
2274 uris[i++] = g_strdup (item->uri);
2285 * g_bookmark_file_set_title:
2286 * @bookmark: a #GBookmarkFile
2287 * @uri: (nullable): a valid URI or %NULL
2288 * @title: a UTF-8 encoded string
2290 * Sets @title as the title of the bookmark for @uri inside the
2291 * bookmark file @bookmark.
2293 * If @uri is %NULL, the title of @bookmark is set.
2295 * If a bookmark for @uri cannot be found then it is created.
2300 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2304 g_return_if_fail (bookmark != NULL);
2308 g_free (bookmark->title);
2309 bookmark->title = g_strdup (title);
2315 item = g_bookmark_file_lookup_item (bookmark, uri);
2318 item = bookmark_item_new (uri);
2319 g_bookmark_file_add_item (bookmark, item, NULL);
2322 g_free (item->title);
2323 item->title = g_strdup (title);
2325 bookmark_item_touch_modified (item);
2330 * g_bookmark_file_get_title:
2331 * @bookmark: a #GBookmarkFile
2332 * @uri: (nullable): a valid URI or %NULL
2333 * @error: return location for a #GError, or %NULL
2335 * Returns the title of the bookmark for @uri.
2337 * If @uri is %NULL, the title of @bookmark is returned.
2339 * In the event the URI cannot be found, %NULL is returned and
2340 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2342 * Returns: (transfer full): a newly allocated string or %NULL if the specified
2343 * URI cannot be found.
2348 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2354 g_return_val_if_fail (bookmark != NULL, NULL);
2357 return g_strdup (bookmark->title);
2359 item = g_bookmark_file_lookup_item (bookmark, uri);
2362 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2363 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2364 _("No bookmark found for URI “%s”"),
2369 return g_strdup (item->title);
2373 * g_bookmark_file_set_description:
2374 * @bookmark: a #GBookmarkFile
2375 * @uri: (nullable): a valid URI or %NULL
2376 * @description: a string
2378 * Sets @description as the description of the bookmark for @uri.
2380 * If @uri is %NULL, the description of @bookmark is set.
2382 * If a bookmark for @uri cannot be found then it is created.
2387 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2389 const gchar *description)
2391 g_return_if_fail (bookmark != NULL);
2395 g_free (bookmark->description);
2396 bookmark->description = g_strdup (description);
2402 item = g_bookmark_file_lookup_item (bookmark, uri);
2405 item = bookmark_item_new (uri);
2406 g_bookmark_file_add_item (bookmark, item, NULL);
2409 g_free (item->description);
2410 item->description = g_strdup (description);
2412 bookmark_item_touch_modified (item);
2417 * g_bookmark_file_get_description:
2418 * @bookmark: a #GBookmarkFile
2420 * @error: return location for a #GError, or %NULL
2422 * Retrieves the description of the bookmark for @uri.
2424 * In the event the URI cannot be found, %NULL is returned and
2425 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2427 * Returns: (transfer full): a newly allocated string or %NULL if the specified
2428 * URI cannot be found.
2433 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2439 g_return_val_if_fail (bookmark != NULL, NULL);
2442 return g_strdup (bookmark->description);
2444 item = g_bookmark_file_lookup_item (bookmark, uri);
2447 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2448 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2449 _("No bookmark found for URI “%s”"),
2454 return g_strdup (item->description);
2458 * g_bookmark_file_set_mime_type:
2459 * @bookmark: a #GBookmarkFile
2461 * @mime_type: a MIME type
2463 * Sets @mime_type as the MIME type of the bookmark for @uri.
2465 * If a bookmark for @uri cannot be found then it is created.
2470 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2472 const gchar *mime_type)
2476 g_return_if_fail (bookmark != NULL);
2477 g_return_if_fail (uri != NULL);
2478 g_return_if_fail (mime_type != NULL);
2480 item = g_bookmark_file_lookup_item (bookmark, uri);
2483 item = bookmark_item_new (uri);
2484 g_bookmark_file_add_item (bookmark, item, NULL);
2487 if (!item->metadata)
2488 item->metadata = bookmark_metadata_new ();
2490 g_free (item->metadata->mime_type);
2492 item->metadata->mime_type = g_strdup (mime_type);
2493 bookmark_item_touch_modified (item);
2497 * g_bookmark_file_get_mime_type:
2498 * @bookmark: a #GBookmarkFile
2500 * @error: return location for a #GError, or %NULL
2502 * Retrieves the MIME type of the resource pointed by @uri.
2504 * In the event the URI cannot be found, %NULL is returned and
2505 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2506 * event that the MIME type cannot be found, %NULL is returned and
2507 * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2509 * Returns: (transfer full): a newly allocated string or %NULL if the specified
2510 * URI cannot be found.
2515 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2521 g_return_val_if_fail (bookmark != NULL, NULL);
2522 g_return_val_if_fail (uri != NULL, NULL);
2524 item = g_bookmark_file_lookup_item (bookmark, uri);
2527 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2528 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2529 _("No bookmark found for URI “%s”"),
2534 if (!item->metadata)
2536 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2537 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2538 _("No MIME type defined in the bookmark for URI “%s”"),
2543 return g_strdup (item->metadata->mime_type);
2547 * g_bookmark_file_set_is_private:
2548 * @bookmark: a #GBookmarkFile
2550 * @is_private: %TRUE if the bookmark should be marked as private
2552 * Sets the private flag of the bookmark for @uri.
2554 * If a bookmark for @uri cannot be found then it is created.
2559 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2561 gboolean is_private)
2565 g_return_if_fail (bookmark != NULL);
2566 g_return_if_fail (uri != NULL);
2568 item = g_bookmark_file_lookup_item (bookmark, uri);
2571 item = bookmark_item_new (uri);
2572 g_bookmark_file_add_item (bookmark, item, NULL);
2575 if (!item->metadata)
2576 item->metadata = bookmark_metadata_new ();
2578 item->metadata->is_private = (is_private == TRUE);
2579 bookmark_item_touch_modified (item);
2583 * g_bookmark_file_get_is_private:
2584 * @bookmark: a #GBookmarkFile
2586 * @error: return location for a #GError, or %NULL
2588 * Gets whether the private flag of the bookmark for @uri is set.
2590 * In the event the URI cannot be found, %FALSE is returned and
2591 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2592 * event that the private flag cannot be found, %FALSE is returned and
2593 * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2595 * Returns: %TRUE if the private flag is set, %FALSE otherwise.
2600 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2606 g_return_val_if_fail (bookmark != NULL, FALSE);
2607 g_return_val_if_fail (uri != NULL, FALSE);
2609 item = g_bookmark_file_lookup_item (bookmark, uri);
2612 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2613 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2614 _("No bookmark found for URI “%s”"),
2619 if (!item->metadata)
2621 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2622 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2623 _("No private flag has been defined in bookmark for URI “%s”"),
2628 return item->metadata->is_private;
2632 * g_bookmark_file_set_added:
2633 * @bookmark: a #GBookmarkFile
2635 * @added: a timestamp or -1 to use the current time
2637 * Sets the time the bookmark for @uri was added into @bookmark.
2639 * If no bookmark for @uri is found then it is created.
2642 * Deprecated: 2.66: Use g_bookmark_file_set_added_date_time() instead, as
2643 * `time_t` is deprecated due to the year 2038 problem.
2646 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2650 GDateTime *added_dt = (added != (time_t) -1) ? g_date_time_new_from_unix_utc (added) : g_date_time_new_now_utc ();
2651 g_bookmark_file_set_added_date_time (bookmark, uri, added_dt);
2652 g_date_time_unref (added_dt);
2656 * g_bookmark_file_set_added_date_time:
2657 * @bookmark: a #GBookmarkFile
2659 * @added: a #GDateTime
2661 * Sets the time the bookmark for @uri was added into @bookmark.
2663 * If no bookmark for @uri is found then it is created.
2668 g_bookmark_file_set_added_date_time (GBookmarkFile *bookmark,
2674 g_return_if_fail (bookmark != NULL);
2675 g_return_if_fail (uri != NULL);
2676 g_return_if_fail (added != NULL);
2678 item = g_bookmark_file_lookup_item (bookmark, uri);
2681 item = bookmark_item_new (uri);
2682 g_bookmark_file_add_item (bookmark, item, NULL);
2685 g_clear_pointer (&item->added, g_date_time_unref);
2686 item->added = g_date_time_ref (added);
2687 g_clear_pointer (&item->modified, g_date_time_unref);
2688 item->modified = g_date_time_ref (added);
2692 * g_bookmark_file_get_added:
2693 * @bookmark: a #GBookmarkFile
2695 * @error: return location for a #GError, or %NULL
2697 * Gets the time the bookmark for @uri was added to @bookmark
2699 * In the event the URI cannot be found, -1 is returned and
2700 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2702 * Returns: a timestamp
2705 * Deprecated: 2.66: Use g_bookmark_file_get_added_date_time() instead, as
2706 * `time_t` is deprecated due to the year 2038 problem.
2709 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2713 GDateTime *added = g_bookmark_file_get_added_date_time (bookmark, uri, error);
2714 return (added != NULL) ? g_date_time_to_unix (added) : (time_t) -1;
2718 * g_bookmark_file_get_added_date_time:
2719 * @bookmark: a #GBookmarkFile
2721 * @error: return location for a #GError, or %NULL
2723 * Gets the time the bookmark for @uri was added to @bookmark
2725 * In the event the URI cannot be found, %NULL is returned and
2726 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2728 * Returns: (transfer none): a #GDateTime
2733 g_bookmark_file_get_added_date_time (GBookmarkFile *bookmark,
2739 g_return_val_if_fail (bookmark != NULL, NULL);
2740 g_return_val_if_fail (uri != NULL, NULL);
2741 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2743 item = g_bookmark_file_lookup_item (bookmark, uri);
2746 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2747 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2748 _("No bookmark found for URI “%s”"),
2757 * g_bookmark_file_set_modified:
2758 * @bookmark: a #GBookmarkFile
2760 * @modified: a timestamp or -1 to use the current time
2762 * Sets the last time the bookmark for @uri was last modified.
2764 * If no bookmark for @uri is found then it is created.
2766 * The "modified" time should only be set when the bookmark's meta-data
2767 * was actually changed. Every function of #GBookmarkFile that
2768 * modifies a bookmark also changes the modification time, except for
2769 * g_bookmark_file_set_visited_date_time().
2772 * Deprecated: 2.66: Use g_bookmark_file_set_modified_date_time() instead, as
2773 * `time_t` is deprecated due to the year 2038 problem.
2776 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2780 GDateTime *modified_dt = (modified != (time_t) -1) ? g_date_time_new_from_unix_utc (modified) : g_date_time_new_now_utc ();
2781 g_bookmark_file_set_modified_date_time (bookmark, uri, modified_dt);
2782 g_date_time_unref (modified_dt);
2786 * g_bookmark_file_set_modified_date_time:
2787 * @bookmark: a #GBookmarkFile
2789 * @modified: a #GDateTime
2791 * Sets the last time the bookmark for @uri was last modified.
2793 * If no bookmark for @uri is found then it is created.
2795 * The "modified" time should only be set when the bookmark's meta-data
2796 * was actually changed. Every function of #GBookmarkFile that
2797 * modifies a bookmark also changes the modification time, except for
2798 * g_bookmark_file_set_visited_date_time().
2803 g_bookmark_file_set_modified_date_time (GBookmarkFile *bookmark,
2805 GDateTime *modified)
2809 g_return_if_fail (bookmark != NULL);
2810 g_return_if_fail (uri != NULL);
2811 g_return_if_fail (modified != NULL);
2813 item = g_bookmark_file_lookup_item (bookmark, uri);
2816 item = bookmark_item_new (uri);
2817 g_bookmark_file_add_item (bookmark, item, NULL);
2820 g_clear_pointer (&item->modified, g_date_time_unref);
2821 item->modified = g_date_time_ref (modified);
2825 * g_bookmark_file_get_modified:
2826 * @bookmark: a #GBookmarkFile
2828 * @error: return location for a #GError, or %NULL
2830 * Gets the time when the bookmark for @uri was last modified.
2832 * In the event the URI cannot be found, -1 is returned and
2833 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2835 * Returns: a timestamp
2838 * Deprecated: 2.66: Use g_bookmark_file_get_modified_date_time() instead, as
2839 * `time_t` is deprecated due to the year 2038 problem.
2842 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2846 GDateTime *modified = g_bookmark_file_get_modified_date_time (bookmark, uri, error);
2847 return (modified != NULL) ? g_date_time_to_unix (modified) : (time_t) -1;
2851 * g_bookmark_file_get_modified_date_time:
2852 * @bookmark: a #GBookmarkFile
2854 * @error: return location for a #GError, or %NULL
2856 * Gets the time when the bookmark for @uri was last modified.
2858 * In the event the URI cannot be found, %NULL is returned and
2859 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2861 * Returns: (transfer none): a #GDateTime
2866 g_bookmark_file_get_modified_date_time (GBookmarkFile *bookmark,
2872 g_return_val_if_fail (bookmark != NULL, NULL);
2873 g_return_val_if_fail (uri != NULL, NULL);
2874 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
2876 item = g_bookmark_file_lookup_item (bookmark, uri);
2879 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2880 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2881 _("No bookmark found for URI “%s”"),
2886 return item->modified;
2890 * g_bookmark_file_set_visited:
2891 * @bookmark: a #GBookmarkFile
2893 * @visited: a timestamp or -1 to use the current time
2895 * Sets the time the bookmark for @uri was last visited.
2897 * If no bookmark for @uri is found then it is created.
2899 * The "visited" time should only be set if the bookmark was launched,
2900 * either using the command line retrieved by g_bookmark_file_get_application_info()
2901 * or by the default application for the bookmark's MIME type, retrieved
2902 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2903 * does not affect the "modified" time.
2906 * Deprecated: 2.66: Use g_bookmark_file_set_visited_date_time() instead, as
2907 * `time_t` is deprecated due to the year 2038 problem.
2910 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2914 GDateTime *visited_dt = (visited != (time_t) -1) ? g_date_time_new_from_unix_utc (visited) : g_date_time_new_now_utc ();
2915 g_bookmark_file_set_visited_date_time (bookmark, uri, visited_dt);
2916 g_date_time_unref (visited_dt);
2920 * g_bookmark_file_set_visited_date_time:
2921 * @bookmark: a #GBookmarkFile
2923 * @visited: a #GDateTime
2925 * Sets the time the bookmark for @uri was last visited.
2927 * If no bookmark for @uri is found then it is created.
2929 * The "visited" time should only be set if the bookmark was launched,
2930 * either using the command line retrieved by g_bookmark_file_get_application_info()
2931 * or by the default application for the bookmark's MIME type, retrieved
2932 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2933 * does not affect the "modified" time.
2938 g_bookmark_file_set_visited_date_time (GBookmarkFile *bookmark,
2944 g_return_if_fail (bookmark != NULL);
2945 g_return_if_fail (uri != NULL);
2946 g_return_if_fail (visited != NULL);
2948 item = g_bookmark_file_lookup_item (bookmark, uri);
2951 item = bookmark_item_new (uri);
2952 g_bookmark_file_add_item (bookmark, item, NULL);
2955 g_clear_pointer (&item->visited, g_date_time_unref);
2956 item->visited = g_date_time_ref (visited);
2960 * g_bookmark_file_get_visited:
2961 * @bookmark: a #GBookmarkFile
2963 * @error: return location for a #GError, or %NULL
2965 * Gets the time the bookmark for @uri was last visited.
2967 * In the event the URI cannot be found, -1 is returned and
2968 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2970 * Returns: a timestamp.
2973 * Deprecated: 2.66: Use g_bookmark_file_get_visited_date_time() instead, as
2974 * `time_t` is deprecated due to the year 2038 problem.
2977 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2981 GDateTime *visited = g_bookmark_file_get_visited_date_time (bookmark, uri, error);
2982 return (visited != NULL) ? g_date_time_to_unix (visited) : (time_t) -1;
2986 * g_bookmark_file_get_visited_date_time:
2987 * @bookmark: a #GBookmarkFile
2989 * @error: return location for a #GError, or %NULL
2991 * Gets the time the bookmark for @uri was last visited.
2993 * In the event the URI cannot be found, %NULL is returned and
2994 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2996 * Returns: (transfer none): a #GDateTime
3001 g_bookmark_file_get_visited_date_time (GBookmarkFile *bookmark,
3007 g_return_val_if_fail (bookmark != NULL, NULL);
3008 g_return_val_if_fail (uri != NULL, NULL);
3009 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
3011 item = g_bookmark_file_lookup_item (bookmark, uri);
3014 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3015 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3016 _("No bookmark found for URI “%s”"),
3021 return item->visited;
3025 * g_bookmark_file_has_group:
3026 * @bookmark: a #GBookmarkFile
3028 * @group: the group name to be searched
3029 * @error: return location for a #GError, or %NULL
3031 * Checks whether @group appears in the list of groups to which
3032 * the bookmark for @uri belongs to.
3034 * In the event the URI cannot be found, %FALSE is returned and
3035 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3037 * Returns: %TRUE if @group was found.
3042 g_bookmark_file_has_group (GBookmarkFile *bookmark,
3050 g_return_val_if_fail (bookmark != NULL, FALSE);
3051 g_return_val_if_fail (uri != NULL, FALSE);
3053 item = g_bookmark_file_lookup_item (bookmark, uri);
3056 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3057 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3058 _("No bookmark found for URI “%s”"),
3063 if (!item->metadata)
3066 for (l = item->metadata->groups; l != NULL; l = l->next)
3068 if (strcmp (l->data, group) == 0)
3077 * g_bookmark_file_add_group:
3078 * @bookmark: a #GBookmarkFile
3080 * @group: the group name to be added
3082 * Adds @group to the list of groups to which the bookmark for @uri
3085 * If no bookmark for @uri is found then it is created.
3090 g_bookmark_file_add_group (GBookmarkFile *bookmark,
3096 g_return_if_fail (bookmark != NULL);
3097 g_return_if_fail (uri != NULL);
3098 g_return_if_fail (group != NULL && group[0] != '\0');
3100 item = g_bookmark_file_lookup_item (bookmark, uri);
3103 item = bookmark_item_new (uri);
3104 g_bookmark_file_add_item (bookmark, item, NULL);
3107 if (!item->metadata)
3108 item->metadata = bookmark_metadata_new ();
3110 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
3112 item->metadata->groups = g_list_prepend (item->metadata->groups,
3115 bookmark_item_touch_modified (item);
3120 * g_bookmark_file_remove_group:
3121 * @bookmark: a #GBookmarkFile
3123 * @group: the group name to be removed
3124 * @error: return location for a #GError, or %NULL
3126 * Removes @group from the list of groups to which the bookmark
3127 * for @uri belongs to.
3129 * In the event the URI cannot be found, %FALSE is returned and
3130 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3131 * In the event no group was defined, %FALSE is returned and
3132 * @error is set to %G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
3134 * Returns: %TRUE if @group was successfully removed.
3139 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
3147 g_return_val_if_fail (bookmark != NULL, FALSE);
3148 g_return_val_if_fail (uri != NULL, FALSE);
3150 item = g_bookmark_file_lookup_item (bookmark, uri);
3153 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3154 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3155 _("No bookmark found for URI “%s”"),
3160 if (!item->metadata)
3162 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3163 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
3164 _("No groups set in bookmark for URI “%s”"),
3169 for (l = item->metadata->groups; l != NULL; l = l->next)
3171 if (strcmp (l->data, group) == 0)
3173 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
3177 bookmark_item_touch_modified (item);
3187 * g_bookmark_file_set_groups:
3188 * @bookmark: a #GBookmarkFile
3189 * @uri: an item's URI
3190 * @groups: (nullable) (array length=length) (element-type utf8): an array of
3191 * group names, or %NULL to remove all groups
3192 * @length: number of group name values in @groups
3194 * Sets a list of group names for the item with URI @uri. Each previously
3195 * set group name list is removed.
3197 * If @uri cannot be found then an item for it is created.
3202 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
3204 const gchar **groups,
3210 g_return_if_fail (bookmark != NULL);
3211 g_return_if_fail (uri != NULL);
3212 g_return_if_fail (groups != NULL);
3214 item = g_bookmark_file_lookup_item (bookmark, uri);
3217 item = bookmark_item_new (uri);
3218 g_bookmark_file_add_item (bookmark, item, NULL);
3221 if (!item->metadata)
3222 item->metadata = bookmark_metadata_new ();
3224 g_list_free_full (item->metadata->groups, g_free);
3225 item->metadata->groups = NULL;
3229 for (i = 0; i < length && groups[i] != NULL; i++)
3230 item->metadata->groups = g_list_append (item->metadata->groups,
3231 g_strdup (groups[i]));
3234 bookmark_item_touch_modified (item);
3238 * g_bookmark_file_get_groups:
3239 * @bookmark: a #GBookmarkFile
3241 * @length: (out) (optional): return location for the length of the returned string, or %NULL
3242 * @error: return location for a #GError, or %NULL
3244 * Retrieves the list of group names of the bookmark for @uri.
3246 * In the event the URI cannot be found, %NULL is returned and
3247 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3249 * The returned array is %NULL terminated, so @length may optionally
3252 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
3253 * Use g_strfreev() to free it.
3258 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
3268 g_return_val_if_fail (bookmark != NULL, NULL);
3269 g_return_val_if_fail (uri != NULL, NULL);
3271 item = g_bookmark_file_lookup_item (bookmark, uri);
3274 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3275 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3276 _("No bookmark found for URI “%s”"),
3281 if (!item->metadata)
3289 len = g_list_length (item->metadata->groups);
3290 retval = g_new0 (gchar *, len + 1);
3291 for (l = g_list_last (item->metadata->groups), i = 0;
3295 gchar *group_name = (gchar *) l->data;
3297 g_warn_if_fail (group_name != NULL);
3299 retval[i++] = g_strdup (group_name);
3310 * g_bookmark_file_add_application:
3311 * @bookmark: a #GBookmarkFile
3313 * @name: (nullable): the name of the application registering the bookmark
3315 * @exec: (nullable): command line to be used to launch the bookmark or %NULL
3317 * Adds the application with @name and @exec to the list of
3318 * applications that have registered a bookmark for @uri into
3321 * Every bookmark inside a #GBookmarkFile must have at least an
3322 * application registered. Each application must provide a name, a
3323 * command line useful for launching the bookmark, the number of times
3324 * the bookmark has been registered by the application and the last
3325 * time the application registered this bookmark.
3327 * If @name is %NULL, the name of the application will be the
3328 * same returned by g_get_application_name(); if @exec is %NULL, the
3329 * command line will be a composition of the program name as
3330 * returned by g_get_prgname() and the "\%u" modifier, which will be
3331 * expanded to the bookmark's URI.
3333 * This function will automatically take care of updating the
3334 * registrations count and timestamping in case an application
3335 * with the same @name had already registered a bookmark for
3336 * @uri inside @bookmark.
3338 * If no bookmark for @uri is found, one is created.
3343 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3349 gchar *app_name, *app_exec;
3352 g_return_if_fail (bookmark != NULL);
3353 g_return_if_fail (uri != NULL);
3355 item = g_bookmark_file_lookup_item (bookmark, uri);
3358 item = bookmark_item_new (uri);
3359 g_bookmark_file_add_item (bookmark, item, NULL);
3362 if (name && name[0] != '\0')
3363 app_name = g_strdup (name);
3365 app_name = g_strdup (g_get_application_name ());
3367 if (exec && exec[0] != '\0')
3368 app_exec = g_strdup (exec);
3370 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3372 stamp = g_date_time_new_now_utc ();
3374 g_bookmark_file_set_application_info (bookmark, uri,
3381 g_date_time_unref (stamp);
3387 * g_bookmark_file_remove_application:
3388 * @bookmark: a #GBookmarkFile
3390 * @name: the name of the application
3391 * @error: return location for a #GError or %NULL
3393 * Removes application registered with @name from the list of applications
3394 * that have registered a bookmark for @uri inside @bookmark.
3396 * In the event the URI cannot be found, %FALSE is returned and
3397 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3398 * In the event that no application with name @app_name has registered
3399 * a bookmark for @uri, %FALSE is returned and error is set to
3400 * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3402 * Returns: %TRUE if the application was successfully removed.
3407 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3415 g_return_val_if_fail (bookmark != NULL, FALSE);
3416 g_return_val_if_fail (uri != NULL, FALSE);
3417 g_return_val_if_fail (name != NULL, FALSE);
3420 retval = g_bookmark_file_set_application_info (bookmark, uri,
3428 g_propagate_error (error, set_error);
3437 * g_bookmark_file_has_application:
3438 * @bookmark: a #GBookmarkFile
3440 * @name: the name of the application
3441 * @error: return location for a #GError or %NULL
3443 * Checks whether the bookmark for @uri inside @bookmark has been
3444 * registered by application @name.
3446 * In the event the URI cannot be found, %FALSE is returned and
3447 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3449 * Returns: %TRUE if the application @name was found
3454 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3461 g_return_val_if_fail (bookmark != NULL, FALSE);
3462 g_return_val_if_fail (uri != NULL, FALSE);
3463 g_return_val_if_fail (name != NULL, FALSE);
3465 item = g_bookmark_file_lookup_item (bookmark, uri);
3468 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3469 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3470 _("No bookmark found for URI “%s”"),
3475 return (NULL != bookmark_item_lookup_app_info (item, name));
3479 * g_bookmark_file_set_app_info:
3480 * @bookmark: a #GBookmarkFile
3482 * @name: an application's name
3483 * @exec: an application's command line
3484 * @count: the number of registrations done for this application
3485 * @stamp: the time of the last registration for this application
3486 * @error: return location for a #GError or %NULL
3488 * Sets the meta-data of application @name inside the list of
3489 * applications that have registered a bookmark for @uri inside
3492 * You should rarely use this function; use g_bookmark_file_add_application()
3493 * and g_bookmark_file_remove_application() instead.
3495 * @name can be any UTF-8 encoded string used to identify an
3497 * @exec can have one of these two modifiers: "\%f", which will
3498 * be expanded as the local file name retrieved from the bookmark's
3499 * URI; "\%u", which will be expanded as the bookmark's URI.
3500 * The expansion is done automatically when retrieving the stored
3501 * command line using the g_bookmark_file_get_application_info() function.
3502 * @count is the number of times the application has registered the
3503 * bookmark; if is < 0, the current registration count will be increased
3504 * by one, if is 0, the application with @name will be removed from
3505 * the list of registered applications.
3506 * @stamp is the Unix time of the last registration; if it is -1, the
3507 * current time will be used.
3509 * If you try to remove an application by setting its registration count to
3510 * zero, and no bookmark for @uri is found, %FALSE is returned and
3511 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3512 * in the event that no application @name has registered a bookmark
3513 * for @uri, %FALSE is returned and error is set to
3514 * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3515 * for @uri is found, one is created.
3517 * Returns: %TRUE if the application's meta-data was successfully
3521 * Deprecated: 2.66: Use g_bookmark_file_set_application_info() instead, as
3522 * `time_t` is deprecated due to the year 2038 problem.
3525 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3533 GDateTime *stamp_dt = (stamp != (time_t) -1) ? g_date_time_new_from_unix_utc (stamp) : g_date_time_new_now_utc ();
3535 retval = g_bookmark_file_set_application_info (bookmark, uri, name, exec, count,
3537 g_date_time_unref (stamp_dt);
3542 * g_bookmark_file_set_application_info:
3543 * @bookmark: a #GBookmarkFile
3545 * @name: an application's name
3546 * @exec: an application's command line
3547 * @count: the number of registrations done for this application
3548 * @stamp: (nullable): the time of the last registration for this application,
3549 * which may be %NULL if @count is 0
3550 * @error: return location for a #GError or %NULL
3552 * Sets the meta-data of application @name inside the list of
3553 * applications that have registered a bookmark for @uri inside
3556 * You should rarely use this function; use g_bookmark_file_add_application()
3557 * and g_bookmark_file_remove_application() instead.
3559 * @name can be any UTF-8 encoded string used to identify an
3561 * @exec can have one of these two modifiers: "\%f", which will
3562 * be expanded as the local file name retrieved from the bookmark's
3563 * URI; "\%u", which will be expanded as the bookmark's URI.
3564 * The expansion is done automatically when retrieving the stored
3565 * command line using the g_bookmark_file_get_application_info() function.
3566 * @count is the number of times the application has registered the
3567 * bookmark; if is < 0, the current registration count will be increased
3568 * by one, if is 0, the application with @name will be removed from
3569 * the list of registered applications.
3570 * @stamp is the Unix time of the last registration.
3572 * If you try to remove an application by setting its registration count to
3573 * zero, and no bookmark for @uri is found, %FALSE is returned and
3574 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3575 * in the event that no application @name has registered a bookmark
3576 * for @uri, %FALSE is returned and error is set to
3577 * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3578 * for @uri is found, one is created.
3580 * Returns: %TRUE if the application's meta-data was successfully
3586 g_bookmark_file_set_application_info (GBookmarkFile *bookmark,
3595 BookmarkAppInfo *ai;
3597 g_return_val_if_fail (bookmark != NULL, FALSE);
3598 g_return_val_if_fail (uri != NULL, FALSE);
3599 g_return_val_if_fail (name != NULL, FALSE);
3600 g_return_val_if_fail (exec != NULL, FALSE);
3601 g_return_val_if_fail (count == 0 || stamp != NULL, FALSE);
3602 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3604 item = g_bookmark_file_lookup_item (bookmark, uri);
3609 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3610 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3611 _("No bookmark found for URI “%s”"),
3617 item = bookmark_item_new (uri);
3618 g_bookmark_file_add_item (bookmark, item, NULL);
3622 if (!item->metadata)
3623 item->metadata = bookmark_metadata_new ();
3625 ai = bookmark_item_lookup_app_info (item, name);
3630 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3631 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3632 _("No application with name “%s” registered a bookmark for “%s”"),
3639 ai = bookmark_app_info_new (name);
3641 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3642 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3648 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3649 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3650 bookmark_app_info_free (ai);
3652 bookmark_item_touch_modified (item);
3661 g_clear_pointer (&ai->stamp, g_date_time_unref);
3662 ai->stamp = g_date_time_ref (stamp);
3664 if (exec && exec[0] != '\0')
3667 ai->exec = g_shell_quote (exec);
3670 bookmark_item_touch_modified (item);
3675 /* expands the application's command line */
3677 expand_exec_line (const gchar *exec_fmt,
3683 exec = g_string_sized_new (512);
3684 while ((ch = *exec_fmt++) != '\0')
3688 exec = g_string_append_c (exec, ch);
3699 g_string_append (exec, uri);
3704 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3707 g_string_append (exec, file);
3712 g_string_free (exec, TRUE);
3719 exec = g_string_append_c (exec, ch);
3725 return g_string_free (exec, FALSE);
3729 * g_bookmark_file_get_app_info:
3730 * @bookmark: a #GBookmarkFile
3732 * @name: an application's name
3733 * @exec: (out) (optional): return location for the command line of the application, or %NULL
3734 * @count: (out) (optional): return location for the registration count, or %NULL
3735 * @stamp: (out) (optional): return location for the last registration time, or %NULL
3736 * @error: return location for a #GError, or %NULL
3738 * Gets the registration information of @app_name for the bookmark for
3739 * @uri. See g_bookmark_file_set_application_info() for more information about
3740 * the returned data.
3742 * The string returned in @app_exec must be freed.
3744 * In the event the URI cannot be found, %FALSE is returned and
3745 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3746 * event that no application with name @app_name has registered a bookmark
3747 * for @uri, %FALSE is returned and error is set to
3748 * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3749 * the command line fails, an error of the %G_SHELL_ERROR domain is
3750 * set and %FALSE is returned.
3752 * Returns: %TRUE on success.
3755 * Deprecated: 2.66: Use g_bookmark_file_get_application_info() instead, as
3756 * `time_t` is deprecated due to the year 2038 problem.
3759 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3767 GDateTime *stamp_dt = NULL;
3770 retval = g_bookmark_file_get_application_info (bookmark, uri, name, exec, count, &stamp_dt, error);
3775 *stamp = g_date_time_to_unix (stamp_dt);
3781 * g_bookmark_file_get_application_info:
3782 * @bookmark: a #GBookmarkFile
3784 * @name: an application's name
3785 * @exec: (out) (optional): return location for the command line of the application, or %NULL
3786 * @count: (out) (optional): return location for the registration count, or %NULL
3787 * @stamp: (out) (optional) (transfer none): return location for the last registration time, or %NULL
3788 * @error: return location for a #GError, or %NULL
3790 * Gets the registration information of @app_name for the bookmark for
3791 * @uri. See g_bookmark_file_set_application_info() for more information about
3792 * the returned data.
3794 * The string returned in @app_exec must be freed.
3796 * In the event the URI cannot be found, %FALSE is returned and
3797 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3798 * event that no application with name @app_name has registered a bookmark
3799 * for @uri, %FALSE is returned and error is set to
3800 * %G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3801 * the command line fails, an error of the %G_SHELL_ERROR domain is
3802 * set and %FALSE is returned.
3804 * Returns: %TRUE on success.
3809 g_bookmark_file_get_application_info (GBookmarkFile *bookmark,
3813 unsigned int *count,
3818 BookmarkAppInfo *ai;
3820 g_return_val_if_fail (bookmark != NULL, FALSE);
3821 g_return_val_if_fail (uri != NULL, FALSE);
3822 g_return_val_if_fail (name != NULL, FALSE);
3823 g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
3825 item = g_bookmark_file_lookup_item (bookmark, uri);
3828 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3829 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3830 _("No bookmark found for URI “%s”"),
3835 ai = bookmark_item_lookup_app_info (item, name);
3838 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3839 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3840 _("No application with name “%s” registered a bookmark for “%s”"),
3848 GError *unquote_error = NULL;
3849 gchar *command_line;
3851 command_line = g_shell_unquote (ai->exec, &unquote_error);
3854 g_propagate_error (error, unquote_error);
3858 *exec = expand_exec_line (command_line, uri);
3861 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3862 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3863 _("Failed to expand exec line “%s” with URI “%s”"),
3865 g_free (command_line);
3870 g_free (command_line);
3883 * g_bookmark_file_get_applications:
3884 * @bookmark: a #GBookmarkFile
3886 * @length: (out) (optional): return location of the length of the returned list, or %NULL
3887 * @error: return location for a #GError, or %NULL
3889 * Retrieves the names of the applications that have registered the
3890 * bookmark for @uri.
3892 * In the event the URI cannot be found, %NULL is returned and
3893 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3895 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3896 * Use g_strfreev() to free it.
3901 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3911 g_return_val_if_fail (bookmark != NULL, NULL);
3912 g_return_val_if_fail (uri != NULL, NULL);
3914 item = g_bookmark_file_lookup_item (bookmark, uri);
3917 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3918 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3919 _("No bookmark found for URI “%s”"),
3924 if (!item->metadata)
3932 n_apps = g_list_length (item->metadata->applications);
3933 apps = g_new0 (gchar *, n_apps + 1);
3935 for (l = g_list_last (item->metadata->applications), i = 0;
3939 BookmarkAppInfo *ai;
3941 ai = (BookmarkAppInfo *) l->data;
3943 g_warn_if_fail (ai != NULL);
3944 g_warn_if_fail (ai->name != NULL);
3946 apps[i++] = g_strdup (ai->name);
3957 * g_bookmark_file_get_size:
3958 * @bookmark: a #GBookmarkFile
3960 * Gets the number of bookmarks inside @bookmark.
3962 * Returns: the number of bookmarks
3967 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3969 g_return_val_if_fail (bookmark != NULL, 0);
3971 return g_list_length (bookmark->items);
3975 * g_bookmark_file_move_item:
3976 * @bookmark: a #GBookmarkFile
3977 * @old_uri: a valid URI
3978 * @new_uri: (nullable): a valid URI, or %NULL
3979 * @error: return location for a #GError or %NULL
3981 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3982 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3983 * %NULL, then the bookmark is removed.
3985 * In the event the URI cannot be found, %FALSE is returned and
3986 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3988 * Returns: %TRUE if the URI was successfully changed
3993 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3994 const gchar *old_uri,
3995 const gchar *new_uri,
4000 g_return_val_if_fail (bookmark != NULL, FALSE);
4001 g_return_val_if_fail (old_uri != NULL, FALSE);
4003 item = g_bookmark_file_lookup_item (bookmark, old_uri);
4006 g_set_error (error, G_BOOKMARK_FILE_ERROR,
4007 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
4008 _("No bookmark found for URI “%s”"),
4013 if (new_uri && new_uri[0] != '\0')
4015 if (g_strcmp0 (old_uri, new_uri) == 0)
4018 if (g_bookmark_file_has_item (bookmark, new_uri))
4020 if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
4024 g_hash_table_steal (bookmark->items_by_uri, item->uri);
4027 item->uri = g_strdup (new_uri);
4028 bookmark_item_touch_modified (item);
4030 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
4036 if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
4044 * g_bookmark_file_set_icon:
4045 * @bookmark: a #GBookmarkFile
4047 * @href: (nullable): the URI of the icon for the bookmark, or %NULL
4048 * @mime_type: the MIME type of the icon for the bookmark
4050 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
4051 * the currently set icon. @href can either be a full URL for the icon
4052 * file or the icon name following the Icon Naming specification.
4054 * If no bookmark for @uri is found one is created.
4059 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
4062 const gchar *mime_type)
4066 g_return_if_fail (bookmark != NULL);
4067 g_return_if_fail (uri != NULL);
4069 item = g_bookmark_file_lookup_item (bookmark, uri);
4072 item = bookmark_item_new (uri);
4073 g_bookmark_file_add_item (bookmark, item, NULL);
4076 if (!item->metadata)
4077 item->metadata = bookmark_metadata_new ();
4079 g_free (item->metadata->icon_href);
4080 g_free (item->metadata->icon_mime);
4082 item->metadata->icon_href = g_strdup (href);
4084 if (mime_type && mime_type[0] != '\0')
4085 item->metadata->icon_mime = g_strdup (mime_type);
4087 item->metadata->icon_mime = g_strdup ("application/octet-stream");
4089 bookmark_item_touch_modified (item);
4093 * g_bookmark_file_get_icon:
4094 * @bookmark: a #GBookmarkFile
4096 * @href: (out) (optional): return location for the icon's location or %NULL
4097 * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL
4098 * @error: return location for a #GError or %NULL
4100 * Gets the icon of the bookmark for @uri.
4102 * In the event the URI cannot be found, %FALSE is returned and
4103 * @error is set to %G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
4105 * Returns: %TRUE if the icon for the bookmark for the URI was found.
4106 * You should free the returned strings.
4111 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
4119 g_return_val_if_fail (bookmark != NULL, FALSE);
4120 g_return_val_if_fail (uri != NULL, FALSE);
4122 item = g_bookmark_file_lookup_item (bookmark, uri);
4125 g_set_error (error, G_BOOKMARK_FILE_ERROR,
4126 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
4127 _("No bookmark found for URI “%s”"),
4132 if ((!item->metadata) || (!item->metadata->icon_href))
4136 *href = g_strdup (item->metadata->icon_href);
4139 *mime_type = g_strdup (item->metadata->icon_mime);