1 /* gbookmarkfile.c: parsing and building desktop bookmarks
3 * Copyright (C) 2005-2006 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "gbookmarkfile.h"
35 #include "gfileutils.h"
42 #include "gmessages.h"
47 #include "gstrfuncs.h"
53 * SECTION:bookmarkfile
54 * @title: Bookmark file parser
55 * @short_description: parses files containing bookmarks
57 * GBookmarkFile lets you parse, edit or create files containing bookmarks
58 * to URI, along with some meta-data about the resource pointed by the URI
59 * like its MIME type, the application that is registering the bookmark and
60 * the icon that should be used to represent the bookmark. The data is stored
62 * [Desktop Bookmark Specification](http://www.gnome.org/~ebassi/bookmark-spec).
64 * The syntax of the bookmark files is described in detail inside the
65 * Desktop Bookmark Specification, here is a quick summary: bookmark
66 * files use a sub-class of the XML Bookmark Exchange Language
67 * specification, consisting of valid UTF-8 encoded XML, under the
68 * <xbel> root element; each bookmark is stored inside a
69 * <bookmark> element, using its URI: no relative paths can
70 * be used inside a bookmark file. The bookmark may have a user defined
71 * title and description, to be used instead of the URI. Under the
72 * <metadata> element, with its owner attribute set to
73 * `http://freedesktop.org`, is stored the meta-data about a resource
74 * pointed by its URI. The meta-data consists of the resource's MIME
75 * type; the applications that have registered a bookmark; the groups
76 * to which a bookmark belongs to; a visibility flag, used to set the
77 * bookmark as "private" to the applications and groups that has it
78 * registered; the URI and MIME type of an icon, to be used when
79 * displaying the bookmark inside a GUI.
81 * Here is an example of a bookmark file:
82 * [bookmarks.xbel](https://git.gnome.org/browse/glib/tree/glib/tests/bookmarks.xbel)
84 * A bookmark file might contain more than one bookmark; each bookmark
85 * is accessed through its URI.
87 * The important caveat of bookmark files is that when you add a new
88 * bookmark you must also add the application that is registering it, using
89 * g_bookmark_file_add_application() or g_bookmark_file_set_app_info().
90 * If a bookmark has no applications then it won't be dumped when creating
91 * the on disk representation, using g_bookmark_file_to_data() or
92 * g_bookmark_file_to_file().
94 * The #GBookmarkFile parser was added in GLib 2.12.
97 /* XBEL 1.0 standard entities */
98 #define XBEL_VERSION "1.0"
99 #define XBEL_DTD_NICK "xbel"
100 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
101 "Exchange Language 1.0//EN//XML"
103 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
105 #define XBEL_ROOT_ELEMENT "xbel"
106 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
107 #define XBEL_BOOKMARK_ELEMENT "bookmark"
108 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
109 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
110 #define XBEL_TITLE_ELEMENT "title"
111 #define XBEL_DESC_ELEMENT "desc"
112 #define XBEL_INFO_ELEMENT "info"
113 #define XBEL_METADATA_ELEMENT "metadata"
115 #define XBEL_VERSION_ATTRIBUTE "version"
116 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
117 #define XBEL_OWNER_ATTRIBUTE "owner"
118 #define XBEL_ADDED_ATTRIBUTE "added"
119 #define XBEL_VISITED_ATTRIBUTE "visited"
120 #define XBEL_MODIFIED_ATTRIBUTE "modified"
121 #define XBEL_ID_ATTRIBUTE "id"
122 #define XBEL_HREF_ATTRIBUTE "href"
123 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
125 #define XBEL_YES_VALUE "yes"
126 #define XBEL_NO_VALUE "no"
128 /* Desktop bookmark spec entities */
129 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
131 #define BOOKMARK_NAMESPACE_NAME "bookmark"
132 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
134 #define BOOKMARK_GROUPS_ELEMENT "groups"
135 #define BOOKMARK_GROUP_ELEMENT "group"
136 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
137 #define BOOKMARK_APPLICATION_ELEMENT "application"
138 #define BOOKMARK_ICON_ELEMENT "icon"
139 #define BOOKMARK_PRIVATE_ELEMENT "private"
141 #define BOOKMARK_NAME_ATTRIBUTE "name"
142 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
143 #define BOOKMARK_COUNT_ATTRIBUTE "count"
144 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp" /* deprecated by "modified" */
145 #define BOOKMARK_MODIFIED_ATTRIBUTE "modified"
146 #define BOOKMARK_HREF_ATTRIBUTE "href"
147 #define BOOKMARK_TYPE_ATTRIBUTE "type"
149 /* Shared MIME Info entities */
150 #define MIME_NAMESPACE_NAME "mime"
151 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
152 #define MIME_TYPE_ELEMENT "mime-type"
153 #define MIME_TYPE_ATTRIBUTE "type"
156 typedef struct _BookmarkAppInfo BookmarkAppInfo;
157 typedef struct _BookmarkMetadata BookmarkMetadata;
158 typedef struct _BookmarkItem BookmarkItem;
159 typedef struct _ParseData ParseData;
161 struct _BookmarkAppInfo
171 struct _BookmarkMetadata
178 GHashTable *apps_by_name;
183 guint is_private : 1;
197 BookmarkMetadata *metadata;
200 struct _GBookmarkFile
205 /* we store our items in a list and keep a copy inside
206 * an hash table for faster lookup performances
209 GHashTable *items_by_uri;
212 /* parser state machine */
233 static void g_bookmark_file_init (GBookmarkFile *bookmark);
234 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
235 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
239 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
242 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
244 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
248 static gboolean timestamp_from_iso8601 (const gchar *iso_date,
249 time_t *out_timestamp,
251 static gchar *timestamp_to_iso8601 (time_t timestamp);
253 /********************************
256 * Application metadata storage *
257 ********************************/
258 static BookmarkAppInfo *
259 bookmark_app_info_new (const gchar *name)
261 BookmarkAppInfo *retval;
263 g_warn_if_fail (name != NULL);
265 retval = g_slice_new (BookmarkAppInfo);
267 retval->name = g_strdup (name);
276 bookmark_app_info_free (BookmarkAppInfo *app_info)
281 g_free (app_info->name);
282 g_free (app_info->exec);
284 g_slice_free (BookmarkAppInfo, app_info);
288 bookmark_app_info_dump (BookmarkAppInfo *app_info)
291 gchar *name, *exec, *modified, *count;
293 g_warn_if_fail (app_info != NULL);
295 if (app_info->count == 0)
298 name = g_markup_escape_text (app_info->name, -1);
299 exec = g_markup_escape_text (app_info->exec, -1);
300 modified = timestamp_to_iso8601 (app_info->stamp);
301 count = g_strdup_printf ("%u", app_info->count);
303 retval = g_strconcat (" "
304 "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
305 " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
306 " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\""
307 " " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", modified, "\""
308 " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
320 /***********************
324 ***********************/
325 static BookmarkMetadata *
326 bookmark_metadata_new (void)
328 BookmarkMetadata *retval;
330 retval = g_slice_new (BookmarkMetadata);
332 retval->mime_type = NULL;
334 retval->groups = NULL;
336 retval->applications = NULL;
337 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
342 retval->is_private = FALSE;
344 retval->icon_href = NULL;
345 retval->icon_mime = NULL;
351 bookmark_metadata_free (BookmarkMetadata *metadata)
356 g_free (metadata->mime_type);
358 g_list_free_full (metadata->groups, g_free);
359 g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
361 g_hash_table_destroy (metadata->apps_by_name);
363 g_free (metadata->icon_href);
364 g_free (metadata->icon_mime);
366 g_slice_free (BookmarkMetadata, metadata);
370 bookmark_metadata_dump (BookmarkMetadata *metadata)
375 if (!metadata->applications)
378 retval = g_string_sized_new (1024);
380 /* metadata container */
381 g_string_append (retval,
383 "<" XBEL_METADATA_ELEMENT
384 " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
388 if (metadata->mime_type) {
389 buffer = g_strconcat (" "
390 "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
391 MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
393 g_string_append (retval, buffer);
397 if (metadata->groups)
401 /* open groups container */
402 g_string_append (retval,
404 "<" BOOKMARK_NAMESPACE_NAME
405 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
407 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
411 group_name = g_markup_escape_text ((gchar *) l->data, -1);
412 buffer = g_strconcat (" "
413 "<" BOOKMARK_NAMESPACE_NAME
414 ":" BOOKMARK_GROUP_ELEMENT ">",
416 "</" BOOKMARK_NAMESPACE_NAME
417 ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
418 g_string_append (retval, buffer);
424 /* close groups container */
425 g_string_append (retval,
427 "</" BOOKMARK_NAMESPACE_NAME
428 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
431 if (metadata->applications)
435 /* open applications container */
436 g_string_append (retval,
438 "<" BOOKMARK_NAMESPACE_NAME
439 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
441 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
443 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
446 g_warn_if_fail (app_info != NULL);
448 app_data = bookmark_app_info_dump (app_info);
452 retval = g_string_append (retval, app_data);
458 /* close applications container */
459 g_string_append (retval,
461 "</" BOOKMARK_NAMESPACE_NAME
462 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
466 if (metadata->icon_href)
468 if (!metadata->icon_mime)
469 metadata->icon_mime = g_strdup ("application/octet-stream");
471 buffer = g_strconcat (" "
472 "<" BOOKMARK_NAMESPACE_NAME
473 ":" BOOKMARK_ICON_ELEMENT
474 " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
475 "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
476 g_string_append (retval, buffer);
482 if (metadata->is_private)
483 g_string_append (retval,
485 "<" BOOKMARK_NAMESPACE_NAME
486 ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
488 /* close metadata container */
489 g_string_append (retval,
491 "</" XBEL_METADATA_ELEMENT ">\n");
493 return g_string_free (retval, FALSE);
496 /******************************************************
499 * Storage for a single bookmark item inside the list *
500 ******************************************************/
501 static BookmarkItem *
502 bookmark_item_new (const gchar *uri)
506 g_warn_if_fail (uri != NULL);
508 item = g_slice_new (BookmarkItem);
509 item->uri = g_strdup (uri);
512 item->description = NULL;
514 item->added = (time_t) -1;
515 item->modified = (time_t) -1;
516 item->visited = (time_t) -1;
518 item->metadata = NULL;
524 bookmark_item_free (BookmarkItem *item)
530 g_free (item->title);
531 g_free (item->description);
534 bookmark_metadata_free (item->metadata);
536 g_slice_free (BookmarkItem, item);
540 bookmark_item_dump (BookmarkItem *item)
543 gchar *added, *visited, *modified;
547 /* at this point, we must have at least a registered application; if we don't
548 * we don't screw up the bookmark file, and just skip this item
550 if (!item->metadata || !item->metadata->applications)
552 g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri);
556 retval = g_string_sized_new (4096);
558 added = timestamp_to_iso8601 (item->added);
559 modified = timestamp_to_iso8601 (item->modified);
560 visited = timestamp_to_iso8601 (item->visited);
562 escaped_uri = g_markup_escape_text (item->uri, -1);
564 buffer = g_strconcat (" <"
565 XBEL_BOOKMARK_ELEMENT
567 XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
568 XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
569 XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
570 XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
573 g_string_append (retval, buffer);
575 g_free (escaped_uri);
583 gchar *escaped_title;
585 escaped_title = g_markup_escape_text (item->title, -1);
586 buffer = g_strconcat (" "
587 "<" XBEL_TITLE_ELEMENT ">",
589 "</" XBEL_TITLE_ELEMENT ">\n",
591 g_string_append (retval, buffer);
593 g_free (escaped_title);
597 if (item->description)
601 escaped_desc = g_markup_escape_text (item->description, -1);
602 buffer = g_strconcat (" "
603 "<" XBEL_DESC_ELEMENT ">",
605 "</" XBEL_DESC_ELEMENT ">\n",
607 g_string_append (retval, buffer);
609 g_free (escaped_desc);
617 metadata = bookmark_metadata_dump (item->metadata);
620 buffer = g_strconcat (" "
621 "<" XBEL_INFO_ELEMENT ">\n",
624 "</" XBEL_INFO_ELEMENT ">\n",
626 retval = g_string_append (retval, buffer);
633 g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
635 return g_string_free (retval, FALSE);
638 static BookmarkAppInfo *
639 bookmark_item_lookup_app_info (BookmarkItem *item,
640 const gchar *app_name)
642 g_warn_if_fail (item != NULL && app_name != NULL);
647 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
650 /*************************
652 *************************/
655 g_bookmark_file_init (GBookmarkFile *bookmark)
657 bookmark->title = NULL;
658 bookmark->description = NULL;
660 bookmark->items = NULL;
661 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
668 g_bookmark_file_clear (GBookmarkFile *bookmark)
670 g_free (bookmark->title);
671 g_free (bookmark->description);
673 g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
674 bookmark->items = NULL;
676 if (bookmark->items_by_uri)
678 g_hash_table_destroy (bookmark->items_by_uri);
680 bookmark->items_by_uri = NULL;
688 GHashTable *namespaces;
690 GBookmarkFile *bookmark_file;
691 BookmarkItem *current_item;
695 parse_data_new (void)
699 retval = g_new (ParseData, 1);
701 retval->state = STATE_STARTED;
702 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
703 (GDestroyNotify) g_free,
704 (GDestroyNotify) g_free);
705 retval->bookmark_file = NULL;
706 retval->current_item = NULL;
712 parse_data_free (ParseData *parse_data)
714 g_hash_table_destroy (parse_data->namespaces);
719 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
722 parse_bookmark_element (GMarkupParseContext *context,
723 ParseData *parse_data,
724 const gchar **attribute_names,
725 const gchar **attribute_values,
728 const gchar *uri, *added, *modified, *visited;
734 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
737 uri = added = modified = visited = NULL;
738 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
740 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
741 uri = attribute_values[i];
742 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
743 added = attribute_values[i];
744 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
745 modified = attribute_values[i];
746 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
747 visited = attribute_values[i];
750 /* bookmark is defined by the XBEL spec, so we need
751 * to error out if the element has different or
754 g_set_error (error, G_MARKUP_ERROR,
755 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
756 _("Unexpected attribute “%s” for element “%s”"),
758 XBEL_BOOKMARK_ELEMENT);
765 g_set_error (error, G_MARKUP_ERROR,
766 G_MARKUP_ERROR_INVALID_CONTENT,
767 _("Attribute “%s” of element “%s” not found"),
769 XBEL_BOOKMARK_ELEMENT);
773 g_warn_if_fail (parse_data->current_item == NULL);
775 item = bookmark_item_new (uri);
777 if (added != NULL && !timestamp_from_iso8601 (added, &item->added, error))
780 if (modified != NULL && !timestamp_from_iso8601 (modified, &item->modified, error))
783 if (visited != NULL && !timestamp_from_iso8601 (visited, &item->visited, error))
787 g_bookmark_file_add_item (parse_data->bookmark_file,
792 bookmark_item_free (item);
794 g_propagate_error (error, add_error);
799 parse_data->current_item = item;
803 parse_application_element (GMarkupParseContext *context,
804 ParseData *parse_data,
805 const gchar **attribute_names,
806 const gchar **attribute_values,
809 const gchar *name, *exec, *count, *stamp, *modified;
815 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
818 name = exec = count = stamp = modified = NULL;
819 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
821 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
822 name = attribute_values[i];
823 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
824 exec = attribute_values[i];
825 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
826 count = attribute_values[i];
827 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
828 stamp = attribute_values[i];
829 else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
830 modified = attribute_values[i];
833 /* the "name" and "exec" attributes are mandatory */
836 g_set_error (error, G_MARKUP_ERROR,
837 G_MARKUP_ERROR_INVALID_CONTENT,
838 _("Attribute “%s” of element “%s” not found"),
839 BOOKMARK_NAME_ATTRIBUTE,
840 BOOKMARK_APPLICATION_ELEMENT);
846 g_set_error (error, G_MARKUP_ERROR,
847 G_MARKUP_ERROR_INVALID_CONTENT,
848 _("Attribute “%s” of element “%s” not found"),
849 BOOKMARK_EXEC_ATTRIBUTE,
850 BOOKMARK_APPLICATION_ELEMENT);
854 g_warn_if_fail (parse_data->current_item != NULL);
855 item = parse_data->current_item;
857 ai = bookmark_item_lookup_app_info (item, name);
860 ai = bookmark_app_info_new (name);
863 item->metadata = bookmark_metadata_new ();
865 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
866 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
870 ai->exec = g_strdup (exec);
873 ai->count = atoi (count);
877 if (modified != NULL)
879 if (!timestamp_from_iso8601 (modified, &ai->stamp, error))
884 /* the timestamp attribute has been deprecated but we still parse
885 * it for backward compatibility
888 ai->stamp = (time_t) atol (stamp);
890 ai->stamp = time (NULL);
895 parse_mime_type_element (GMarkupParseContext *context,
896 ParseData *parse_data,
897 const gchar **attribute_names,
898 const gchar **attribute_values,
906 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
910 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
912 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
913 type = attribute_values[i];
917 type = "application/octet-stream";
919 g_warn_if_fail (parse_data->current_item != NULL);
920 item = parse_data->current_item;
923 item->metadata = bookmark_metadata_new ();
925 g_free (item->metadata->mime_type);
926 item->metadata->mime_type = g_strdup (type);
930 parse_icon_element (GMarkupParseContext *context,
931 ParseData *parse_data,
932 const gchar **attribute_names,
933 const gchar **attribute_values,
942 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
947 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
949 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
950 href = attribute_values[i];
951 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
952 type = attribute_values[i];
955 /* the "href" attribute is mandatory */
958 g_set_error (error, G_MARKUP_ERROR,
959 G_MARKUP_ERROR_INVALID_CONTENT,
960 _("Attribute “%s” of element “%s” not found"),
961 BOOKMARK_HREF_ATTRIBUTE,
962 BOOKMARK_ICON_ELEMENT);
967 type = "application/octet-stream";
969 g_warn_if_fail (parse_data->current_item != NULL);
970 item = parse_data->current_item;
973 item->metadata = bookmark_metadata_new ();
975 g_free (item->metadata->icon_href);
976 g_free (item->metadata->icon_mime);
977 item->metadata->icon_href = g_strdup (href);
978 item->metadata->icon_mime = g_strdup (type);
981 /* scans through the attributes of an element for the "xmlns" pragma, and
982 * adds any resulting namespace declaration to a per-parser hashtable, using
983 * the namespace name as a key for the namespace URI; if no key was found,
984 * the namespace is considered as default, and stored under the "default" key.
986 * FIXME: this works on the assumption that the generator of the XBEL file
987 * is either this code or is smart enough to place the namespace declarations
988 * inside the main root node or inside the metadata node and does not redefine
989 * a namespace inside an inner node; this does *not* conform to the
990 * XML-NS standard, although is a close approximation. In order to make this
991 * conformant to the XML-NS specification we should use a per-element
992 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
995 map_namespace_to_name (ParseData *parse_data,
996 const gchar **attribute_names,
997 const gchar **attribute_values)
1002 g_warn_if_fail (parse_data != NULL);
1004 if (!attribute_names || !attribute_names[0])
1008 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1010 if (g_str_has_prefix (attr, "xmlns"))
1012 gchar *namespace_name, *namespace_uri;
1015 p = g_utf8_strchr (attr, -1, ':');
1017 p = g_utf8_next_char (p);
1021 namespace_name = g_strdup (p);
1022 namespace_uri = g_strdup (attribute_values[i]);
1024 g_hash_table_replace (parse_data->namespaces,
1031 /* checks whether @element_full is equal to @element.
1033 * if @namespace is set, it tries to resolve the namespace to a known URI,
1034 * and if found is prepended to the element name, from which is separated
1035 * using the character specified in the @sep parameter.
1038 is_element_full (ParseData *parse_data,
1039 const gchar *element_full,
1040 const gchar *namespace,
1041 const gchar *element,
1044 gchar *ns_uri, *ns_name;
1045 const gchar *p, *element_name;
1048 g_warn_if_fail (parse_data != NULL);
1049 g_warn_if_fail (element_full != NULL);
1054 /* no namespace requested: dumb element compare */
1056 return (0 == strcmp (element_full, element));
1058 /* search for namespace separator; if none found, assume we are under the
1059 * default namespace, and set ns_name to our "default" marker; if no default
1060 * namespace has been set, just do a plain comparison between @full_element
1063 p = g_utf8_strchr (element_full, -1, ':');
1066 ns_name = g_strndup (element_full, p - element_full);
1067 element_name = g_utf8_next_char (p);
1071 ns_name = g_strdup ("default");
1072 element_name = element_full;
1075 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1078 /* no default namespace found */
1081 return (0 == strcmp (element_full, element));
1084 retval = (0 == strcmp (ns_uri, namespace) &&
1085 0 == strcmp (element_name, element));
1092 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1093 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1095 static const gchar *
1096 parser_state_to_element_name (ParserState state)
1101 case STATE_FINISHED:
1102 return "(top-level)";
1104 return XBEL_ROOT_ELEMENT;
1105 case STATE_BOOKMARK:
1106 return XBEL_BOOKMARK_ELEMENT;
1108 return XBEL_TITLE_ELEMENT;
1110 return XBEL_DESC_ELEMENT;
1112 return XBEL_INFO_ELEMENT;
1113 case STATE_METADATA:
1114 return XBEL_METADATA_ELEMENT;
1115 case STATE_APPLICATIONS:
1116 return BOOKMARK_APPLICATIONS_ELEMENT;
1117 case STATE_APPLICATION:
1118 return BOOKMARK_APPLICATION_ELEMENT;
1120 return BOOKMARK_GROUPS_ELEMENT;
1122 return BOOKMARK_GROUP_ELEMENT;
1124 return MIME_TYPE_ELEMENT;
1126 return BOOKMARK_ICON_ELEMENT;
1128 g_assert_not_reached ();
1133 start_element_raw_cb (GMarkupParseContext *context,
1134 const gchar *element_name,
1135 const gchar **attribute_names,
1136 const gchar **attribute_values,
1140 ParseData *parse_data = (ParseData *) user_data;
1142 /* we must check for namespace declarations first
1144 * XXX - we could speed up things by checking for namespace declarations
1145 * only on the root node, where they usually are; this would probably break
1146 * on streams not produced by us or by "smart" generators
1148 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1150 switch (parse_data->state)
1153 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1159 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1161 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1162 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1163 parse_data->state = STATE_ROOT;
1167 g_set_error (error, G_MARKUP_ERROR,
1168 G_MARKUP_ERROR_INVALID_CONTENT,
1169 _("Unexpected tag “%s”, tag “%s” expected"),
1170 element_name, XBEL_ROOT_ELEMENT);
1173 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1174 parse_data->state = STATE_TITLE;
1175 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1176 parse_data->state = STATE_DESC;
1177 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1179 GError *inner_error = NULL;
1181 parse_data->state = STATE_BOOKMARK;
1183 parse_bookmark_element (context,
1189 g_propagate_error (error, inner_error);
1192 g_set_error (error, G_MARKUP_ERROR,
1193 G_MARKUP_ERROR_INVALID_CONTENT,
1194 _("Unexpected tag “%s” inside “%s”"),
1198 case STATE_BOOKMARK:
1199 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1200 parse_data->state = STATE_TITLE;
1201 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1202 parse_data->state = STATE_DESC;
1203 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1204 parse_data->state = STATE_INFO;
1206 g_set_error (error, G_MARKUP_ERROR,
1207 G_MARKUP_ERROR_INVALID_CONTENT,
1208 _("Unexpected tag “%s” inside “%s”"),
1210 XBEL_BOOKMARK_ELEMENT);
1213 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1219 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1221 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1222 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1224 parse_data->state = STATE_METADATA;
1226 if (!parse_data->current_item->metadata)
1227 parse_data->current_item->metadata = bookmark_metadata_new ();
1232 g_set_error (error, G_MARKUP_ERROR,
1233 G_MARKUP_ERROR_INVALID_CONTENT,
1234 _("Unexpected tag “%s”, tag “%s” expected"),
1236 XBEL_METADATA_ELEMENT);
1238 case STATE_METADATA:
1239 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1240 parse_data->state = STATE_APPLICATIONS;
1241 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1242 parse_data->state = STATE_GROUPS;
1243 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1244 parse_data->current_item->metadata->is_private = TRUE;
1245 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1247 GError *inner_error = NULL;
1249 parse_data->state = STATE_ICON;
1251 parse_icon_element (context,
1257 g_propagate_error (error, inner_error);
1259 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1261 GError *inner_error = NULL;
1263 parse_data->state = STATE_MIME;
1265 parse_mime_type_element (context,
1271 g_propagate_error (error, inner_error);
1274 g_set_error (error, G_MARKUP_ERROR,
1275 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1276 _("Unexpected tag “%s” inside “%s”"),
1278 XBEL_METADATA_ELEMENT);
1280 case STATE_APPLICATIONS:
1281 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1283 GError *inner_error = NULL;
1285 parse_data->state = STATE_APPLICATION;
1287 parse_application_element (context,
1293 g_propagate_error (error, inner_error);
1296 g_set_error (error, G_MARKUP_ERROR,
1297 G_MARKUP_ERROR_INVALID_CONTENT,
1298 _("Unexpected tag “%s”, tag “%s” expected"),
1300 BOOKMARK_APPLICATION_ELEMENT);
1303 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1304 parse_data->state = STATE_GROUP;
1306 g_set_error (error, G_MARKUP_ERROR,
1307 G_MARKUP_ERROR_INVALID_CONTENT,
1308 _("Unexpected tag “%s”, tag “%s” expected"),
1310 BOOKMARK_GROUP_ELEMENT);
1315 case STATE_APPLICATION:
1319 case STATE_FINISHED:
1320 g_set_error (error, G_MARKUP_ERROR,
1321 G_MARKUP_ERROR_INVALID_CONTENT,
1322 _("Unexpected tag “%s” inside “%s”"),
1324 parser_state_to_element_name (parse_data->state));
1328 g_assert_not_reached ();
1334 end_element_raw_cb (GMarkupParseContext *context,
1335 const gchar *element_name,
1339 ParseData *parse_data = (ParseData *) user_data;
1341 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1342 parse_data->state = STATE_FINISHED;
1343 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1345 parse_data->current_item = NULL;
1347 parse_data->state = STATE_ROOT;
1349 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1350 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1351 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1353 if (parse_data->current_item)
1354 parse_data->state = STATE_BOOKMARK;
1356 parse_data->state = STATE_ROOT;
1358 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1359 parse_data->state = STATE_INFO;
1360 else if (IS_ELEMENT_NS (parse_data, element_name,
1361 BOOKMARK_NAMESPACE_URI,
1362 BOOKMARK_APPLICATION_ELEMENT))
1363 parse_data->state = STATE_APPLICATIONS;
1364 else if (IS_ELEMENT_NS (parse_data, element_name,
1365 BOOKMARK_NAMESPACE_URI,
1366 BOOKMARK_GROUP_ELEMENT))
1367 parse_data->state = STATE_GROUPS;
1368 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1369 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1370 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1371 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1372 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1373 parse_data->state = STATE_METADATA;
1377 text_raw_cb (GMarkupParseContext *context,
1383 ParseData *parse_data = (ParseData *) user_data;
1386 payload = g_strndup (text, length);
1388 switch (parse_data->state)
1391 if (parse_data->current_item)
1393 g_free (parse_data->current_item->title);
1394 parse_data->current_item->title = g_strdup (payload);
1398 g_free (parse_data->bookmark_file->title);
1399 parse_data->bookmark_file->title = g_strdup (payload);
1403 if (parse_data->current_item)
1405 g_free (parse_data->current_item->description);
1406 parse_data->current_item->description = g_strdup (payload);
1410 g_free (parse_data->bookmark_file->description);
1411 parse_data->bookmark_file->description = g_strdup (payload);
1418 g_warn_if_fail (parse_data->current_item != NULL);
1420 if (!parse_data->current_item->metadata)
1421 parse_data->current_item->metadata = bookmark_metadata_new ();
1423 groups = parse_data->current_item->metadata->groups;
1424 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1428 case STATE_BOOKMARK:
1430 case STATE_METADATA:
1431 case STATE_APPLICATIONS:
1432 case STATE_APPLICATION:
1438 g_warn_if_reached ();
1445 static const GMarkupParser markup_parser =
1447 start_element_raw_cb, /* start_element */
1448 end_element_raw_cb, /* end_element */
1449 text_raw_cb, /* text */
1450 NULL, /* passthrough */
1455 g_bookmark_file_parse (GBookmarkFile *bookmark,
1456 const gchar *buffer,
1460 GMarkupParseContext *context;
1461 ParseData *parse_data;
1462 GError *parse_error, *end_error;
1465 g_warn_if_fail (bookmark != NULL);
1473 if (length == (gsize) -1)
1474 length = strlen (buffer);
1476 parse_data = parse_data_new ();
1477 parse_data->bookmark_file = bookmark;
1479 context = g_markup_parse_context_new (&markup_parser,
1482 (GDestroyNotify) parse_data_free);
1484 retval = g_markup_parse_context_parse (context,
1489 g_propagate_error (error, parse_error);
1492 retval = g_markup_parse_context_end_parse (context, &end_error);
1494 g_propagate_error (error, end_error);
1497 g_markup_parse_context_free (context);
1503 g_bookmark_file_dump (GBookmarkFile *bookmark,
1511 retval = g_string_sized_new (4096);
1513 g_string_append (retval,
1514 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1516 /* XXX - do we really need the doctype? */
1517 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1518 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1519 " \"" XBEL_DTD_URI "\">\n"
1521 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1522 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1523 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1525 if (bookmark->title)
1527 gchar *escaped_title;
1529 escaped_title = g_markup_escape_text (bookmark->title, -1);
1531 buffer = g_strconcat (" "
1532 "<" XBEL_TITLE_ELEMENT ">",
1534 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1536 g_string_append (retval, buffer);
1539 g_free (escaped_title);
1542 if (bookmark->description)
1544 gchar *escaped_desc;
1546 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1548 buffer = g_strconcat (" "
1549 "<" XBEL_DESC_ELEMENT ">",
1551 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1552 g_string_append (retval, buffer);
1555 g_free (escaped_desc);
1558 if (!bookmark->items)
1561 retval = g_string_append (retval, "\n");
1563 /* the items are stored in reverse order */
1564 for (l = g_list_last (bookmark->items);
1568 BookmarkItem *item = (BookmarkItem *) l->data;
1571 item_dump = bookmark_item_dump (item);
1575 retval = g_string_append (retval, item_dump);
1581 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1584 *length = retval->len;
1586 return g_string_free (retval, FALSE);
1593 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1594 * should free the returned string.
1597 timestamp_to_iso8601 (time_t timestamp)
1599 GDateTime *dt = g_date_time_new_from_unix_utc (timestamp);
1600 gchar *iso8601_string = g_date_time_format_iso8601 (dt);
1601 g_date_time_unref (dt);
1603 return g_steal_pointer (&iso8601_string);
1607 timestamp_from_iso8601 (const gchar *iso_date,
1608 time_t *out_timestamp,
1612 GDateTime *dt = g_date_time_new_from_iso8601 (iso_date, NULL);
1615 g_set_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_READ,
1616 _("Invalid date/time ‘%s’ in bookmark file"), iso_date);
1620 time_val = g_date_time_to_unix (dt);
1621 g_date_time_unref (dt);
1623 *out_timestamp = time_val;
1627 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1629 /********************
1631 ********************/
1634 * g_bookmark_file_new: (constructor)
1636 * Creates a new empty #GBookmarkFile object.
1638 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1639 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1642 * Returns: an empty #GBookmarkFile
1647 g_bookmark_file_new (void)
1649 GBookmarkFile *bookmark;
1651 bookmark = g_new (GBookmarkFile, 1);
1653 g_bookmark_file_init (bookmark);
1659 * g_bookmark_file_free:
1660 * @bookmark: a #GBookmarkFile
1662 * Frees a #GBookmarkFile.
1667 g_bookmark_file_free (GBookmarkFile *bookmark)
1672 g_bookmark_file_clear (bookmark);
1678 * g_bookmark_file_load_from_data:
1679 * @bookmark: an empty #GBookmarkFile struct
1680 * @data: (array length=length) (element-type guint8): desktop bookmarks
1682 * @length: the length of @data in bytes
1683 * @error: return location for a #GError, or %NULL
1685 * Loads a bookmark file from memory into an empty #GBookmarkFile
1686 * structure. If the object cannot be created then @error is set to a
1687 * #GBookmarkFileError.
1689 * Returns: %TRUE if a desktop bookmark could be loaded.
1694 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1699 GError *parse_error;
1702 g_return_val_if_fail (bookmark != NULL, FALSE);
1704 if (length == (gsize) -1)
1705 length = strlen (data);
1707 if (bookmark->items)
1709 g_bookmark_file_clear (bookmark);
1710 g_bookmark_file_init (bookmark);
1714 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1717 g_propagate_error (error, parse_error);
1723 * g_bookmark_file_load_from_file:
1724 * @bookmark: an empty #GBookmarkFile struct
1725 * @filename: (type filename): the path of a filename to load, in the
1726 * GLib file name encoding
1727 * @error: return location for a #GError, or %NULL
1729 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1730 * If the file could not be loaded then @error is set to either a #GFileError
1731 * or #GBookmarkFileError.
1733 * Returns: %TRUE if a desktop bookmark file could be loaded
1738 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1739 const gchar *filename,
1742 gboolean ret = FALSE;
1743 gchar *buffer = NULL;
1746 g_return_val_if_fail (bookmark != NULL, FALSE);
1747 g_return_val_if_fail (filename != NULL, FALSE);
1749 if (!g_file_get_contents (filename, &buffer, &len, error))
1752 if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1762 /* Iterates through all the directories in *dirs trying to
1763 * find file. When it successfully locates file, returns a
1764 * string its absolute path. It also leaves the unchecked
1765 * directories in *dirs. You should free the returned string
1767 * Adapted from gkeyfile.c
1770 find_file_in_data_dirs (const gchar *file,
1774 gchar **data_dirs, *data_dir, *path;
1783 while (data_dirs && (data_dir = *data_dirs) && !path)
1785 gchar *candidate_file, *sub_dir;
1787 candidate_file = (gchar *) file;
1788 sub_dir = g_strdup ("");
1789 while (candidate_file != NULL && !path)
1793 path = g_build_filename (data_dir, sub_dir,
1794 candidate_file, NULL);
1796 candidate_file = strchr (candidate_file, '-');
1798 if (candidate_file == NULL)
1804 sub_dir = g_strndup (file, candidate_file - file - 1);
1806 for (p = sub_dir; *p != '\0'; p++)
1809 *p = G_DIR_SEPARATOR;
1820 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1821 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1822 _("No valid bookmark file found in data dirs"));
1832 * g_bookmark_file_load_from_data_dirs:
1833 * @bookmark: a #GBookmarkFile
1834 * @file: (type filename): a relative path to a filename to open and parse
1835 * @full_path: (out) (optional) (type filename): return location for a string
1836 * containing the full path of the file, or %NULL
1837 * @error: return location for a #GError, or %NULL
1839 * This function looks for a desktop bookmark file named @file in the
1840 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1841 * loads the file into @bookmark and returns the file's full path in
1842 * @full_path. If the file could not be loaded then @error is
1843 * set to either a #GFileError or #GBookmarkFileError.
1845 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1850 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1855 GError *file_error = NULL;
1856 gchar **all_data_dirs, **data_dirs;
1857 const gchar *user_data_dir;
1858 const gchar * const * system_data_dirs;
1861 gboolean found_file;
1863 g_return_val_if_fail (bookmark != NULL, FALSE);
1864 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1866 user_data_dir = g_get_user_data_dir ();
1867 system_data_dirs = g_get_system_data_dirs ();
1868 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1871 all_data_dirs[i++] = g_strdup (user_data_dir);
1874 while (system_data_dirs[j] != NULL)
1875 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1878 data_dirs = all_data_dirs;
1880 while (*data_dirs != NULL && !found_file)
1882 g_free (output_path);
1884 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1888 g_propagate_error (error, file_error);
1892 found_file = g_bookmark_file_load_from_file (bookmark,
1897 g_propagate_error (error, file_error);
1902 if (found_file && full_path)
1903 *full_path = output_path;
1905 g_free (output_path);
1907 g_strfreev (all_data_dirs);
1914 * g_bookmark_file_to_data:
1915 * @bookmark: a #GBookmarkFile
1916 * @length: (out) (optional): return location for the length of the returned string, or %NULL
1917 * @error: return location for a #GError, or %NULL
1919 * This function outputs @bookmark as a string.
1921 * Returns: (array length=length) (element-type guint8):
1922 * a newly allocated string holding the contents of the #GBookmarkFile
1927 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1931 GError *write_error = NULL;
1934 g_return_val_if_fail (bookmark != NULL, NULL);
1936 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1939 g_propagate_error (error, write_error);
1948 * g_bookmark_file_to_file:
1949 * @bookmark: a #GBookmarkFile
1950 * @filename: (type filename): path of the output file
1951 * @error: return location for a #GError, or %NULL
1953 * This function outputs @bookmark into a file. The write process is
1954 * guaranteed to be atomic by using g_file_set_contents() internally.
1956 * Returns: %TRUE if the file was successfully written.
1961 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1962 const gchar *filename,
1966 GError *data_error, *write_error;
1970 g_return_val_if_fail (bookmark != NULL, FALSE);
1971 g_return_val_if_fail (filename != NULL, FALSE);
1974 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1977 g_propagate_error (error, data_error);
1983 g_file_set_contents (filename, data, len, &write_error);
1986 g_propagate_error (error, write_error);
1998 static BookmarkItem *
1999 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
2002 g_warn_if_fail (bookmark != NULL && uri != NULL);
2004 return g_hash_table_lookup (bookmark->items_by_uri, uri);
2007 /* this function adds a new item to the list */
2009 g_bookmark_file_add_item (GBookmarkFile *bookmark,
2013 g_warn_if_fail (bookmark != NULL);
2014 g_warn_if_fail (item != NULL);
2016 /* this should never happen; and if it does, then we are
2017 * screwing up something big time.
2019 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2021 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2022 G_BOOKMARK_FILE_ERROR_INVALID_URI,
2023 _("A bookmark for URI “%s” already exists"),
2028 bookmark->items = g_list_prepend (bookmark->items, item);
2030 g_hash_table_replace (bookmark->items_by_uri,
2034 if (item->added == (time_t) -1)
2035 item->added = time (NULL);
2037 if (item->modified == (time_t) -1)
2038 item->modified = time (NULL);
2042 * g_bookmark_file_remove_item:
2043 * @bookmark: a #GBookmarkFile
2045 * @error: return location for a #GError, or %NULL
2047 * Removes the bookmark for @uri from the bookmark file @bookmark.
2049 * Returns: %TRUE if the bookmark was removed successfully.
2054 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2060 g_return_val_if_fail (bookmark != NULL, FALSE);
2061 g_return_val_if_fail (uri != NULL, FALSE);
2063 item = g_bookmark_file_lookup_item (bookmark, uri);
2067 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2068 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2069 _("No bookmark found for URI “%s”"),
2074 bookmark->items = g_list_remove (bookmark->items, item);
2075 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2077 bookmark_item_free (item);
2083 * g_bookmark_file_has_item:
2084 * @bookmark: a #GBookmarkFile
2087 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2089 * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2094 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2097 g_return_val_if_fail (bookmark != NULL, FALSE);
2098 g_return_val_if_fail (uri != NULL, FALSE);
2100 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2104 * g_bookmark_file_get_uris:
2105 * @bookmark: a #GBookmarkFile
2106 * @length: (out) (optional): return location for the number of returned URIs, or %NULL
2108 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2109 * The array of returned URIs will be %NULL-terminated, so @length may
2110 * optionally be %NULL.
2112 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2113 * Use g_strfreev() to free it.
2118 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2125 g_return_val_if_fail (bookmark != NULL, NULL);
2127 n_items = g_list_length (bookmark->items);
2128 uris = g_new0 (gchar *, n_items + 1);
2130 /* the items are stored in reverse order, so we walk the list backward */
2131 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2133 BookmarkItem *item = (BookmarkItem *) l->data;
2135 g_warn_if_fail (item != NULL);
2137 uris[i++] = g_strdup (item->uri);
2148 * g_bookmark_file_set_title:
2149 * @bookmark: a #GBookmarkFile
2150 * @uri: (nullable): a valid URI or %NULL
2151 * @title: a UTF-8 encoded string
2153 * Sets @title as the title of the bookmark for @uri inside the
2154 * bookmark file @bookmark.
2156 * If @uri is %NULL, the title of @bookmark is set.
2158 * If a bookmark for @uri cannot be found then it is created.
2163 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2167 g_return_if_fail (bookmark != NULL);
2171 g_free (bookmark->title);
2172 bookmark->title = g_strdup (title);
2178 item = g_bookmark_file_lookup_item (bookmark, uri);
2181 item = bookmark_item_new (uri);
2182 g_bookmark_file_add_item (bookmark, item, NULL);
2185 g_free (item->title);
2186 item->title = g_strdup (title);
2188 item->modified = time (NULL);
2193 * g_bookmark_file_get_title:
2194 * @bookmark: a #GBookmarkFile
2195 * @uri: (nullable): a valid URI or %NULL
2196 * @error: return location for a #GError, or %NULL
2198 * Returns the title of the bookmark for @uri.
2200 * If @uri is %NULL, the title of @bookmark is returned.
2202 * In the event the URI cannot be found, %NULL is returned and
2203 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2205 * Returns: a newly allocated string or %NULL if the specified
2206 * URI cannot be found.
2211 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2217 g_return_val_if_fail (bookmark != NULL, NULL);
2220 return g_strdup (bookmark->title);
2222 item = g_bookmark_file_lookup_item (bookmark, uri);
2225 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2226 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2227 _("No bookmark found for URI “%s”"),
2232 return g_strdup (item->title);
2236 * g_bookmark_file_set_description:
2237 * @bookmark: a #GBookmarkFile
2238 * @uri: (nullable): a valid URI or %NULL
2239 * @description: a string
2241 * Sets @description as the description of the bookmark for @uri.
2243 * If @uri is %NULL, the description of @bookmark is set.
2245 * If a bookmark for @uri cannot be found then it is created.
2250 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2252 const gchar *description)
2254 g_return_if_fail (bookmark != NULL);
2258 g_free (bookmark->description);
2259 bookmark->description = g_strdup (description);
2265 item = g_bookmark_file_lookup_item (bookmark, uri);
2268 item = bookmark_item_new (uri);
2269 g_bookmark_file_add_item (bookmark, item, NULL);
2272 g_free (item->description);
2273 item->description = g_strdup (description);
2275 item->modified = time (NULL);
2280 * g_bookmark_file_get_description:
2281 * @bookmark: a #GBookmarkFile
2283 * @error: return location for a #GError, or %NULL
2285 * Retrieves the description of the bookmark for @uri.
2287 * In the event the URI cannot be found, %NULL is returned and
2288 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2290 * Returns: a newly allocated string or %NULL if the specified
2291 * URI cannot be found.
2296 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2302 g_return_val_if_fail (bookmark != NULL, NULL);
2305 return g_strdup (bookmark->description);
2307 item = g_bookmark_file_lookup_item (bookmark, uri);
2310 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2311 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2312 _("No bookmark found for URI “%s”"),
2317 return g_strdup (item->description);
2321 * g_bookmark_file_set_mime_type:
2322 * @bookmark: a #GBookmarkFile
2324 * @mime_type: a MIME type
2326 * Sets @mime_type as the MIME type of the bookmark for @uri.
2328 * If a bookmark for @uri cannot be found then it is created.
2333 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2335 const gchar *mime_type)
2339 g_return_if_fail (bookmark != NULL);
2340 g_return_if_fail (uri != NULL);
2341 g_return_if_fail (mime_type != NULL);
2343 item = g_bookmark_file_lookup_item (bookmark, uri);
2346 item = bookmark_item_new (uri);
2347 g_bookmark_file_add_item (bookmark, item, NULL);
2350 if (!item->metadata)
2351 item->metadata = bookmark_metadata_new ();
2353 g_free (item->metadata->mime_type);
2355 item->metadata->mime_type = g_strdup (mime_type);
2356 item->modified = time (NULL);
2360 * g_bookmark_file_get_mime_type:
2361 * @bookmark: a #GBookmarkFile
2363 * @error: return location for a #GError, or %NULL
2365 * Retrieves the MIME type of the resource pointed by @uri.
2367 * In the event the URI cannot be found, %NULL is returned and
2368 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2369 * event that the MIME type cannot be found, %NULL is returned and
2370 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2372 * Returns: a newly allocated string or %NULL if the specified
2373 * URI cannot be found.
2378 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2384 g_return_val_if_fail (bookmark != NULL, NULL);
2385 g_return_val_if_fail (uri != NULL, NULL);
2387 item = g_bookmark_file_lookup_item (bookmark, uri);
2390 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2391 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2392 _("No bookmark found for URI “%s”"),
2397 if (!item->metadata)
2399 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2400 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2401 _("No MIME type defined in the bookmark for URI “%s”"),
2406 return g_strdup (item->metadata->mime_type);
2410 * g_bookmark_file_set_is_private:
2411 * @bookmark: a #GBookmarkFile
2413 * @is_private: %TRUE if the bookmark should be marked as private
2415 * Sets the private flag of the bookmark for @uri.
2417 * If a bookmark for @uri cannot be found then it is created.
2422 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2424 gboolean is_private)
2428 g_return_if_fail (bookmark != NULL);
2429 g_return_if_fail (uri != NULL);
2431 item = g_bookmark_file_lookup_item (bookmark, uri);
2434 item = bookmark_item_new (uri);
2435 g_bookmark_file_add_item (bookmark, item, NULL);
2438 if (!item->metadata)
2439 item->metadata = bookmark_metadata_new ();
2441 item->metadata->is_private = (is_private == TRUE);
2442 item->modified = time (NULL);
2446 * g_bookmark_file_get_is_private:
2447 * @bookmark: a #GBookmarkFile
2449 * @error: return location for a #GError, or %NULL
2451 * Gets whether the private flag of the bookmark for @uri is set.
2453 * In the event the URI cannot be found, %FALSE is returned and
2454 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2455 * event that the private flag cannot be found, %FALSE is returned and
2456 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2458 * Returns: %TRUE if the private flag is set, %FALSE otherwise.
2463 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2469 g_return_val_if_fail (bookmark != NULL, FALSE);
2470 g_return_val_if_fail (uri != NULL, FALSE);
2472 item = g_bookmark_file_lookup_item (bookmark, uri);
2475 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2476 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2477 _("No bookmark found for URI “%s”"),
2482 if (!item->metadata)
2484 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2485 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2486 _("No private flag has been defined in bookmark for URI “%s”"),
2491 return item->metadata->is_private;
2495 * g_bookmark_file_set_added:
2496 * @bookmark: a #GBookmarkFile
2498 * @added: a timestamp or -1 to use the current time
2500 * Sets the time the bookmark for @uri was added into @bookmark.
2502 * If no bookmark for @uri is found then it is created.
2507 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2513 g_return_if_fail (bookmark != NULL);
2514 g_return_if_fail (uri != NULL);
2516 item = g_bookmark_file_lookup_item (bookmark, uri);
2519 item = bookmark_item_new (uri);
2520 g_bookmark_file_add_item (bookmark, item, NULL);
2523 if (added == (time_t) -1)
2526 item->added = added;
2527 item->modified = added;
2531 * g_bookmark_file_get_added:
2532 * @bookmark: a #GBookmarkFile
2534 * @error: return location for a #GError, or %NULL
2536 * Gets the time the bookmark for @uri was added to @bookmark
2538 * In the event the URI cannot be found, -1 is returned and
2539 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2541 * Returns: a timestamp
2546 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2552 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2553 g_return_val_if_fail (uri != NULL, (time_t) -1);
2555 item = g_bookmark_file_lookup_item (bookmark, uri);
2558 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2559 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2560 _("No bookmark found for URI “%s”"),
2569 * g_bookmark_file_set_modified:
2570 * @bookmark: a #GBookmarkFile
2572 * @modified: a timestamp or -1 to use the current time
2574 * Sets the last time the bookmark for @uri was last modified.
2576 * If no bookmark for @uri is found then it is created.
2578 * The "modified" time should only be set when the bookmark's meta-data
2579 * was actually changed. Every function of #GBookmarkFile that
2580 * modifies a bookmark also changes the modification time, except for
2581 * g_bookmark_file_set_visited().
2586 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2592 g_return_if_fail (bookmark != NULL);
2593 g_return_if_fail (uri != NULL);
2595 item = g_bookmark_file_lookup_item (bookmark, uri);
2598 item = bookmark_item_new (uri);
2599 g_bookmark_file_add_item (bookmark, item, NULL);
2602 if (modified == (time_t) -1)
2605 item->modified = modified;
2609 * g_bookmark_file_get_modified:
2610 * @bookmark: a #GBookmarkFile
2612 * @error: return location for a #GError, or %NULL
2614 * Gets the time when the bookmark for @uri was last modified.
2616 * In the event the URI cannot be found, -1 is returned and
2617 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2619 * Returns: a timestamp
2624 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2630 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2631 g_return_val_if_fail (uri != NULL, (time_t) -1);
2633 item = g_bookmark_file_lookup_item (bookmark, uri);
2636 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2637 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2638 _("No bookmark found for URI “%s”"),
2643 return item->modified;
2647 * g_bookmark_file_set_visited:
2648 * @bookmark: a #GBookmarkFile
2650 * @visited: a timestamp or -1 to use the current time
2652 * Sets the time the bookmark for @uri was last visited.
2654 * If no bookmark for @uri is found then it is created.
2656 * The "visited" time should only be set if the bookmark was launched,
2657 * either using the command line retrieved by g_bookmark_file_get_app_info()
2658 * or by the default application for the bookmark's MIME type, retrieved
2659 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2660 * does not affect the "modified" time.
2665 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2671 g_return_if_fail (bookmark != NULL);
2672 g_return_if_fail (uri != NULL);
2674 item = g_bookmark_file_lookup_item (bookmark, uri);
2677 item = bookmark_item_new (uri);
2678 g_bookmark_file_add_item (bookmark, item, NULL);
2681 if (visited == (time_t) -1)
2684 item->visited = visited;
2688 * g_bookmark_file_get_visited:
2689 * @bookmark: a #GBookmarkFile
2691 * @error: return location for a #GError, or %NULL
2693 * Gets the time the bookmark for @uri was last visited.
2695 * In the event the URI cannot be found, -1 is returned and
2696 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2698 * Returns: a timestamp.
2703 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2709 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2710 g_return_val_if_fail (uri != NULL, (time_t) -1);
2712 item = g_bookmark_file_lookup_item (bookmark, uri);
2715 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2716 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2717 _("No bookmark found for URI “%s”"),
2722 return item->visited;
2726 * g_bookmark_file_has_group:
2727 * @bookmark: a #GBookmarkFile
2729 * @group: the group name to be searched
2730 * @error: return location for a #GError, or %NULL
2732 * Checks whether @group appears in the list of groups to which
2733 * the bookmark for @uri belongs to.
2735 * In the event the URI cannot be found, %FALSE is returned and
2736 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2738 * Returns: %TRUE if @group was found.
2743 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2751 g_return_val_if_fail (bookmark != NULL, FALSE);
2752 g_return_val_if_fail (uri != NULL, FALSE);
2754 item = g_bookmark_file_lookup_item (bookmark, uri);
2757 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2758 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2759 _("No bookmark found for URI “%s”"),
2764 if (!item->metadata)
2767 for (l = item->metadata->groups; l != NULL; l = l->next)
2769 if (strcmp (l->data, group) == 0)
2778 * g_bookmark_file_add_group:
2779 * @bookmark: a #GBookmarkFile
2781 * @group: the group name to be added
2783 * Adds @group to the list of groups to which the bookmark for @uri
2786 * If no bookmark for @uri is found then it is created.
2791 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2797 g_return_if_fail (bookmark != NULL);
2798 g_return_if_fail (uri != NULL);
2799 g_return_if_fail (group != NULL && group[0] != '\0');
2801 item = g_bookmark_file_lookup_item (bookmark, uri);
2804 item = bookmark_item_new (uri);
2805 g_bookmark_file_add_item (bookmark, item, NULL);
2808 if (!item->metadata)
2809 item->metadata = bookmark_metadata_new ();
2811 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2813 item->metadata->groups = g_list_prepend (item->metadata->groups,
2816 item->modified = time (NULL);
2821 * g_bookmark_file_remove_group:
2822 * @bookmark: a #GBookmarkFile
2824 * @group: the group name to be removed
2825 * @error: return location for a #GError, or %NULL
2827 * Removes @group from the list of groups to which the bookmark
2828 * for @uri belongs to.
2830 * In the event the URI cannot be found, %FALSE is returned and
2831 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2832 * In the event no group was defined, %FALSE is returned and
2833 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2835 * Returns: %TRUE if @group was successfully removed.
2840 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2848 g_return_val_if_fail (bookmark != NULL, FALSE);
2849 g_return_val_if_fail (uri != NULL, FALSE);
2851 item = g_bookmark_file_lookup_item (bookmark, uri);
2854 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2855 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2856 _("No bookmark found for URI “%s”"),
2861 if (!item->metadata)
2863 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2864 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2865 _("No groups set in bookmark for URI “%s”"),
2870 for (l = item->metadata->groups; l != NULL; l = l->next)
2872 if (strcmp (l->data, group) == 0)
2874 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2878 item->modified = time (NULL);
2888 * g_bookmark_file_set_groups:
2889 * @bookmark: a #GBookmarkFile
2890 * @uri: an item's URI
2891 * @groups: (nullable) (array length=length) (element-type utf8): an array of
2892 * group names, or %NULL to remove all groups
2893 * @length: number of group name values in @groups
2895 * Sets a list of group names for the item with URI @uri. Each previously
2896 * set group name list is removed.
2898 * If @uri cannot be found then an item for it is created.
2903 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2905 const gchar **groups,
2911 g_return_if_fail (bookmark != NULL);
2912 g_return_if_fail (uri != NULL);
2913 g_return_if_fail (groups != NULL);
2915 item = g_bookmark_file_lookup_item (bookmark, uri);
2918 item = bookmark_item_new (uri);
2919 g_bookmark_file_add_item (bookmark, item, NULL);
2922 if (!item->metadata)
2923 item->metadata = bookmark_metadata_new ();
2925 g_list_free_full (item->metadata->groups, g_free);
2926 item->metadata->groups = NULL;
2930 for (i = 0; i < length && groups[i] != NULL; i++)
2931 item->metadata->groups = g_list_append (item->metadata->groups,
2932 g_strdup (groups[i]));
2935 item->modified = time (NULL);
2939 * g_bookmark_file_get_groups:
2940 * @bookmark: a #GBookmarkFile
2942 * @length: (out) (optional): return location for the length of the returned string, or %NULL
2943 * @error: return location for a #GError, or %NULL
2945 * Retrieves the list of group names of the bookmark for @uri.
2947 * In the event the URI cannot be found, %NULL is returned and
2948 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2950 * The returned array is %NULL terminated, so @length may optionally
2953 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
2954 * Use g_strfreev() to free it.
2959 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2969 g_return_val_if_fail (bookmark != NULL, NULL);
2970 g_return_val_if_fail (uri != NULL, NULL);
2972 item = g_bookmark_file_lookup_item (bookmark, uri);
2975 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2976 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2977 _("No bookmark found for URI “%s”"),
2982 if (!item->metadata)
2990 len = g_list_length (item->metadata->groups);
2991 retval = g_new0 (gchar *, len + 1);
2992 for (l = g_list_last (item->metadata->groups), i = 0;
2996 gchar *group_name = (gchar *) l->data;
2998 g_warn_if_fail (group_name != NULL);
3000 retval[i++] = g_strdup (group_name);
3011 * g_bookmark_file_add_application:
3012 * @bookmark: a #GBookmarkFile
3014 * @name: (nullable): the name of the application registering the bookmark
3016 * @exec: (nullable): command line to be used to launch the bookmark or %NULL
3018 * Adds the application with @name and @exec to the list of
3019 * applications that have registered a bookmark for @uri into
3022 * Every bookmark inside a #GBookmarkFile must have at least an
3023 * application registered. Each application must provide a name, a
3024 * command line useful for launching the bookmark, the number of times
3025 * the bookmark has been registered by the application and the last
3026 * time the application registered this bookmark.
3028 * If @name is %NULL, the name of the application will be the
3029 * same returned by g_get_application_name(); if @exec is %NULL, the
3030 * command line will be a composition of the program name as
3031 * returned by g_get_prgname() and the "\%u" modifier, which will be
3032 * expanded to the bookmark's URI.
3034 * This function will automatically take care of updating the
3035 * registrations count and timestamping in case an application
3036 * with the same @name had already registered a bookmark for
3037 * @uri inside @bookmark.
3039 * If no bookmark for @uri is found, one is created.
3044 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3050 gchar *app_name, *app_exec;
3052 g_return_if_fail (bookmark != NULL);
3053 g_return_if_fail (uri != NULL);
3055 item = g_bookmark_file_lookup_item (bookmark, uri);
3058 item = bookmark_item_new (uri);
3059 g_bookmark_file_add_item (bookmark, item, NULL);
3062 if (name && name[0] != '\0')
3063 app_name = g_strdup (name);
3065 app_name = g_strdup (g_get_application_name ());
3067 if (exec && exec[0] != '\0')
3068 app_exec = g_strdup (exec);
3070 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3072 g_bookmark_file_set_app_info (bookmark, uri,
3084 * g_bookmark_file_remove_application:
3085 * @bookmark: a #GBookmarkFile
3087 * @name: the name of the application
3088 * @error: return location for a #GError or %NULL
3090 * Removes application registered with @name from the list of applications
3091 * that have registered a bookmark for @uri inside @bookmark.
3093 * In the event the URI cannot be found, %FALSE is returned and
3094 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3095 * In the event that no application with name @app_name has registered
3096 * a bookmark for @uri, %FALSE is returned and error is set to
3097 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3099 * Returns: %TRUE if the application was successfully removed.
3104 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3112 g_return_val_if_fail (bookmark != NULL, FALSE);
3113 g_return_val_if_fail (uri != NULL, FALSE);
3114 g_return_val_if_fail (name != NULL, FALSE);
3117 retval = g_bookmark_file_set_app_info (bookmark, uri,
3125 g_propagate_error (error, set_error);
3134 * g_bookmark_file_has_application:
3135 * @bookmark: a #GBookmarkFile
3137 * @name: the name of the application
3138 * @error: return location for a #GError or %NULL
3140 * Checks whether the bookmark for @uri inside @bookmark has been
3141 * registered by application @name.
3143 * In the event the URI cannot be found, %FALSE is returned and
3144 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3146 * Returns: %TRUE if the application @name was found
3151 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3158 g_return_val_if_fail (bookmark != NULL, FALSE);
3159 g_return_val_if_fail (uri != NULL, FALSE);
3160 g_return_val_if_fail (name != NULL, FALSE);
3162 item = g_bookmark_file_lookup_item (bookmark, uri);
3165 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3166 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3167 _("No bookmark found for URI “%s”"),
3172 return (NULL != bookmark_item_lookup_app_info (item, name));
3176 * g_bookmark_file_set_app_info:
3177 * @bookmark: a #GBookmarkFile
3179 * @name: an application's name
3180 * @exec: an application's command line
3181 * @count: the number of registrations done for this application
3182 * @stamp: the time of the last registration for this application
3183 * @error: return location for a #GError or %NULL
3185 * Sets the meta-data of application @name inside the list of
3186 * applications that have registered a bookmark for @uri inside
3189 * You should rarely use this function; use g_bookmark_file_add_application()
3190 * and g_bookmark_file_remove_application() instead.
3192 * @name can be any UTF-8 encoded string used to identify an
3194 * @exec can have one of these two modifiers: "\%f", which will
3195 * be expanded as the local file name retrieved from the bookmark's
3196 * URI; "\%u", which will be expanded as the bookmark's URI.
3197 * The expansion is done automatically when retrieving the stored
3198 * command line using the g_bookmark_file_get_app_info() function.
3199 * @count is the number of times the application has registered the
3200 * bookmark; if is < 0, the current registration count will be increased
3201 * by one, if is 0, the application with @name will be removed from
3202 * the list of registered applications.
3203 * @stamp is the Unix time of the last registration; if it is -1, the
3204 * current time will be used.
3206 * If you try to remove an application by setting its registration count to
3207 * zero, and no bookmark for @uri is found, %FALSE is returned and
3208 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3209 * in the event that no application @name has registered a bookmark
3210 * for @uri, %FALSE is returned and error is set to
3211 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3212 * for @uri is found, one is created.
3214 * Returns: %TRUE if the application's meta-data was successfully
3220 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3229 BookmarkAppInfo *ai;
3231 g_return_val_if_fail (bookmark != NULL, FALSE);
3232 g_return_val_if_fail (uri != NULL, FALSE);
3233 g_return_val_if_fail (name != NULL, FALSE);
3234 g_return_val_if_fail (exec != NULL, FALSE);
3236 item = g_bookmark_file_lookup_item (bookmark, uri);
3241 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3242 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3243 _("No bookmark found for URI “%s”"),
3249 item = bookmark_item_new (uri);
3250 g_bookmark_file_add_item (bookmark, item, NULL);
3254 if (!item->metadata)
3255 item->metadata = bookmark_metadata_new ();
3257 ai = bookmark_item_lookup_app_info (item, name);
3262 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3263 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3264 _("No application with name “%s” registered a bookmark for “%s”"),
3271 ai = bookmark_app_info_new (name);
3273 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3274 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3280 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3281 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3282 bookmark_app_info_free (ai);
3284 item->modified = time (NULL);
3293 if (stamp != (time_t) -1)
3296 ai->stamp = time (NULL);
3298 if (exec && exec[0] != '\0')
3301 ai->exec = g_shell_quote (exec);
3304 item->modified = time (NULL);
3309 /* expands the application's command line */
3311 expand_exec_line (const gchar *exec_fmt,
3317 exec = g_string_sized_new (512);
3318 while ((ch = *exec_fmt++) != '\0')
3322 exec = g_string_append_c (exec, ch);
3333 g_string_append (exec, uri);
3338 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3341 g_string_append (exec, file);
3346 g_string_free (exec, TRUE);
3353 exec = g_string_append_c (exec, ch);
3359 return g_string_free (exec, FALSE);
3363 * g_bookmark_file_get_app_info:
3364 * @bookmark: a #GBookmarkFile
3366 * @name: an application's name
3367 * @exec: (out) (optional): return location for the command line of the application, or %NULL
3368 * @count: (out) (optional): return location for the registration count, or %NULL
3369 * @stamp: (out) (optional): return location for the last registration time, or %NULL
3370 * @error: return location for a #GError, or %NULL
3372 * Gets the registration information of @app_name for the bookmark for
3373 * @uri. See g_bookmark_file_set_app_info() for more information about
3374 * the returned data.
3376 * The string returned in @app_exec must be freed.
3378 * In the event the URI cannot be found, %FALSE is returned and
3379 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3380 * event that no application with name @app_name has registered a bookmark
3381 * for @uri, %FALSE is returned and error is set to
3382 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3383 * the command line fails, an error of the #G_SHELL_ERROR domain is
3384 * set and %FALSE is returned.
3386 * Returns: %TRUE on success.
3391 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3400 BookmarkAppInfo *ai;
3402 g_return_val_if_fail (bookmark != NULL, FALSE);
3403 g_return_val_if_fail (uri != NULL, FALSE);
3404 g_return_val_if_fail (name != NULL, FALSE);
3406 item = g_bookmark_file_lookup_item (bookmark, uri);
3409 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3410 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3411 _("No bookmark found for URI “%s”"),
3416 ai = bookmark_item_lookup_app_info (item, name);
3419 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3420 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3421 _("No application with name “%s” registered a bookmark for “%s”"),
3429 GError *unquote_error = NULL;
3430 gchar *command_line;
3432 command_line = g_shell_unquote (ai->exec, &unquote_error);
3435 g_propagate_error (error, unquote_error);
3439 *exec = expand_exec_line (command_line, uri);
3442 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3443 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3444 _("Failed to expand exec line “%s” with URI “%s”"),
3446 g_free (command_line);
3451 g_free (command_line);
3464 * g_bookmark_file_get_applications:
3465 * @bookmark: a #GBookmarkFile
3467 * @length: (out) (optional): return location of the length of the returned list, or %NULL
3468 * @error: return location for a #GError, or %NULL
3470 * Retrieves the names of the applications that have registered the
3471 * bookmark for @uri.
3473 * In the event the URI cannot be found, %NULL is returned and
3474 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3476 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3477 * Use g_strfreev() to free it.
3482 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3492 g_return_val_if_fail (bookmark != NULL, NULL);
3493 g_return_val_if_fail (uri != NULL, NULL);
3495 item = g_bookmark_file_lookup_item (bookmark, uri);
3498 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3499 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3500 _("No bookmark found for URI “%s”"),
3505 if (!item->metadata)
3513 n_apps = g_list_length (item->metadata->applications);
3514 apps = g_new0 (gchar *, n_apps + 1);
3516 for (l = g_list_last (item->metadata->applications), i = 0;
3520 BookmarkAppInfo *ai;
3522 ai = (BookmarkAppInfo *) l->data;
3524 g_warn_if_fail (ai != NULL);
3525 g_warn_if_fail (ai->name != NULL);
3527 apps[i++] = g_strdup (ai->name);
3538 * g_bookmark_file_get_size:
3539 * @bookmark: a #GBookmarkFile
3541 * Gets the number of bookmarks inside @bookmark.
3543 * Returns: the number of bookmarks
3548 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3550 g_return_val_if_fail (bookmark != NULL, 0);
3552 return g_list_length (bookmark->items);
3556 * g_bookmark_file_move_item:
3557 * @bookmark: a #GBookmarkFile
3558 * @old_uri: a valid URI
3559 * @new_uri: (nullable): a valid URI, or %NULL
3560 * @error: return location for a #GError or %NULL
3562 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3563 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3564 * %NULL, then the bookmark is removed.
3566 * In the event the URI cannot be found, %FALSE is returned and
3567 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3569 * Returns: %TRUE if the URI was successfully changed
3574 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3575 const gchar *old_uri,
3576 const gchar *new_uri,
3581 g_return_val_if_fail (bookmark != NULL, FALSE);
3582 g_return_val_if_fail (old_uri != NULL, FALSE);
3584 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3587 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3588 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3589 _("No bookmark found for URI “%s”"),
3594 if (new_uri && new_uri[0] != '\0')
3596 if (g_strcmp0 (old_uri, new_uri) == 0)
3599 if (g_bookmark_file_has_item (bookmark, new_uri))
3601 if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3605 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3608 item->uri = g_strdup (new_uri);
3609 item->modified = time (NULL);
3611 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3617 if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
3625 * g_bookmark_file_set_icon:
3626 * @bookmark: a #GBookmarkFile
3628 * @href: (nullable): the URI of the icon for the bookmark, or %NULL
3629 * @mime_type: the MIME type of the icon for the bookmark
3631 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3632 * the currently set icon. @href can either be a full URL for the icon
3633 * file or the icon name following the Icon Naming specification.
3635 * If no bookmark for @uri is found one is created.
3640 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3643 const gchar *mime_type)
3647 g_return_if_fail (bookmark != NULL);
3648 g_return_if_fail (uri != NULL);
3650 item = g_bookmark_file_lookup_item (bookmark, uri);
3653 item = bookmark_item_new (uri);
3654 g_bookmark_file_add_item (bookmark, item, NULL);
3657 if (!item->metadata)
3658 item->metadata = bookmark_metadata_new ();
3660 g_free (item->metadata->icon_href);
3661 g_free (item->metadata->icon_mime);
3663 item->metadata->icon_href = g_strdup (href);
3665 if (mime_type && mime_type[0] != '\0')
3666 item->metadata->icon_mime = g_strdup (mime_type);
3668 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3670 item->modified = time (NULL);
3674 * g_bookmark_file_get_icon:
3675 * @bookmark: a #GBookmarkFile
3677 * @href: (out) (optional): return location for the icon's location or %NULL
3678 * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL
3679 * @error: return location for a #GError or %NULL
3681 * Gets the icon of the bookmark for @uri.
3683 * In the event the URI cannot be found, %FALSE is returned and
3684 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3686 * Returns: %TRUE if the icon for the bookmark for the URI was found.
3687 * You should free the returned strings.
3692 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3700 g_return_val_if_fail (bookmark != NULL, FALSE);
3701 g_return_val_if_fail (uri != NULL, FALSE);
3703 item = g_bookmark_file_lookup_item (bookmark, uri);
3706 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3707 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3708 _("No bookmark found for URI “%s”"),
3713 if ((!item->metadata) || (!item->metadata->icon_href))
3717 *href = g_strdup (item->metadata->icon_href);
3720 *mime_type = g_strdup (item->metadata->icon_mime);