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 Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 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 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 #include "gbookmarkfile.h"
36 #include "gfileutils.h"
43 #include "gmessages.h"
48 #include "gstrfuncs.h"
54 * SECTION:bookmarkfile
55 * @title: Bookmark file parser
56 * @short_description: parses files containing bookmarks
58 * GBookmarkFile lets you parse, edit or create files containing bookmarks
59 * to URI, along with some meta-data about the resource pointed by the URI
60 * like its MIME type, the application that is registering the bookmark and
61 * the icon that should be used to represent the bookmark. The data is stored
63 * [Desktop Bookmark Specification](http://www.gnome.org/~ebassi/bookmark-spec).
65 * The syntax of the bookmark files is described in detail inside the
66 * Desktop Bookmark Specification, here is a quick summary: bookmark
67 * files use a sub-class of the XML Bookmark Exchange Language
68 * specification, consisting of valid UTF-8 encoded XML, under the
69 * <xbel> root element; each bookmark is stored inside a
70 * <bookmark> element, using its URI: no relative paths can
71 * be used inside a bookmark file. The bookmark may have a user defined
72 * title and description, to be used instead of the URI. Under the
73 * <metadata> element, with its owner attribute set to
74 * `http://freedesktop.org`, is stored the meta-data about a resource
75 * pointed by its URI. The meta-data consists of the resource's MIME
76 * type; the applications that have registered a bookmark; the groups
77 * to which a bookmark belongs to; a visibility flag, used to set the
78 * bookmark as "private" to the applications and groups that has it
79 * registered; the URI and MIME type of an icon, to be used when
80 * displaying the bookmark inside a GUI.
82 * Here is an example of a bookmark file:
83 * [bookmarks.xbel](https://git.gnome.org/browse/glib/tree/glib/tests/bookmarks.xbel)
85 * A bookmark file might contain more than one bookmark; each bookmark
86 * is accessed through its URI.
88 * The important caveat of bookmark files is that when you add a new
89 * bookmark you must also add the application that is registering it, using
90 * g_bookmark_file_add_application() or g_bookmark_file_set_app_info().
91 * If a bookmark has no applications then it won't be dumped when creating
92 * the on disk representation, using g_bookmark_file_to_data() or
93 * g_bookmark_file_to_file().
95 * The #GBookmarkFile parser was added in GLib 2.12.
98 /* XBEL 1.0 standard entities */
99 #define XBEL_VERSION "1.0"
100 #define XBEL_DTD_NICK "xbel"
101 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
102 "Exchange Language 1.0//EN//XML"
104 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
106 #define XBEL_ROOT_ELEMENT "xbel"
107 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
108 #define XBEL_BOOKMARK_ELEMENT "bookmark"
109 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
110 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
111 #define XBEL_TITLE_ELEMENT "title"
112 #define XBEL_DESC_ELEMENT "desc"
113 #define XBEL_INFO_ELEMENT "info"
114 #define XBEL_METADATA_ELEMENT "metadata"
116 #define XBEL_VERSION_ATTRIBUTE "version"
117 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
118 #define XBEL_OWNER_ATTRIBUTE "owner"
119 #define XBEL_ADDED_ATTRIBUTE "added"
120 #define XBEL_VISITED_ATTRIBUTE "visited"
121 #define XBEL_MODIFIED_ATTRIBUTE "modified"
122 #define XBEL_ID_ATTRIBUTE "id"
123 #define XBEL_HREF_ATTRIBUTE "href"
124 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
126 #define XBEL_YES_VALUE "yes"
127 #define XBEL_NO_VALUE "no"
129 /* Desktop bookmark spec entities */
130 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
132 #define BOOKMARK_NAMESPACE_NAME "bookmark"
133 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
135 #define BOOKMARK_GROUPS_ELEMENT "groups"
136 #define BOOKMARK_GROUP_ELEMENT "group"
137 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
138 #define BOOKMARK_APPLICATION_ELEMENT "application"
139 #define BOOKMARK_ICON_ELEMENT "icon"
140 #define BOOKMARK_PRIVATE_ELEMENT "private"
142 #define BOOKMARK_NAME_ATTRIBUTE "name"
143 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
144 #define BOOKMARK_COUNT_ATTRIBUTE "count"
145 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp" /* deprecated by "modified" */
146 #define BOOKMARK_MODIFIED_ATTRIBUTE "modified"
147 #define BOOKMARK_HREF_ATTRIBUTE "href"
148 #define BOOKMARK_TYPE_ATTRIBUTE "type"
150 /* Shared MIME Info entities */
151 #define MIME_NAMESPACE_NAME "mime"
152 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
153 #define MIME_TYPE_ELEMENT "mime-type"
154 #define MIME_TYPE_ATTRIBUTE "type"
157 typedef struct _BookmarkAppInfo BookmarkAppInfo;
158 typedef struct _BookmarkMetadata BookmarkMetadata;
159 typedef struct _BookmarkItem BookmarkItem;
160 typedef struct _ParseData ParseData;
162 struct _BookmarkAppInfo
172 struct _BookmarkMetadata
179 GHashTable *apps_by_name;
184 guint is_private : 1;
198 BookmarkMetadata *metadata;
201 struct _GBookmarkFile
206 /* we store our items in a list and keep a copy inside
207 * an hash table for faster lookup performances
210 GHashTable *items_by_uri;
213 /* parser state machine */
234 static void g_bookmark_file_init (GBookmarkFile *bookmark);
235 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
236 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
240 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
243 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
245 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
249 static time_t timestamp_from_iso8601 (const gchar *iso_date);
250 static gchar * timestamp_to_iso8601 (time_t timestamp);
252 /********************************
255 * Application metadata storage *
256 ********************************/
257 static BookmarkAppInfo *
258 bookmark_app_info_new (const gchar *name)
260 BookmarkAppInfo *retval;
262 g_warn_if_fail (name != NULL);
264 retval = g_slice_new (BookmarkAppInfo);
266 retval->name = g_strdup (name);
275 bookmark_app_info_free (BookmarkAppInfo *app_info)
280 g_free (app_info->name);
281 g_free (app_info->exec);
283 g_slice_free (BookmarkAppInfo, app_info);
287 bookmark_app_info_dump (BookmarkAppInfo *app_info)
290 gchar *name, *exec, *modified, *count;
292 g_warn_if_fail (app_info != NULL);
294 if (app_info->count == 0)
297 name = g_markup_escape_text (app_info->name, -1);
298 exec = g_markup_escape_text (app_info->exec, -1);
299 modified = timestamp_to_iso8601 (app_info->stamp);
300 count = g_strdup_printf ("%u", app_info->count);
302 retval = g_strconcat (" "
303 "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
304 " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
305 " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\""
306 " " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", modified, "\""
307 " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
319 /***********************
323 ***********************/
324 static BookmarkMetadata *
325 bookmark_metadata_new (void)
327 BookmarkMetadata *retval;
329 retval = g_slice_new (BookmarkMetadata);
331 retval->mime_type = NULL;
333 retval->groups = NULL;
335 retval->applications = NULL;
336 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
341 retval->is_private = FALSE;
343 retval->icon_href = NULL;
344 retval->icon_mime = NULL;
350 bookmark_metadata_free (BookmarkMetadata *metadata)
355 g_free (metadata->mime_type);
357 g_list_free_full (metadata->groups, g_free);
358 g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
360 g_hash_table_destroy (metadata->apps_by_name);
362 g_free (metadata->icon_href);
363 g_free (metadata->icon_mime);
365 g_slice_free (BookmarkMetadata, metadata);
369 bookmark_metadata_dump (BookmarkMetadata *metadata)
374 if (!metadata->applications)
377 retval = g_string_sized_new (1024);
379 /* metadata container */
380 g_string_append (retval,
382 "<" XBEL_METADATA_ELEMENT
383 " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
387 if (metadata->mime_type) {
388 buffer = g_strconcat (" "
389 "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
390 MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
392 g_string_append (retval, buffer);
396 if (metadata->groups)
400 /* open groups container */
401 g_string_append (retval,
403 "<" BOOKMARK_NAMESPACE_NAME
404 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
406 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
410 group_name = g_markup_escape_text ((gchar *) l->data, -1);
411 buffer = g_strconcat (" "
412 "<" BOOKMARK_NAMESPACE_NAME
413 ":" BOOKMARK_GROUP_ELEMENT ">",
415 "</" BOOKMARK_NAMESPACE_NAME
416 ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
417 g_string_append (retval, buffer);
423 /* close groups container */
424 g_string_append (retval,
426 "</" BOOKMARK_NAMESPACE_NAME
427 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
430 if (metadata->applications)
434 /* open applications container */
435 g_string_append (retval,
437 "<" BOOKMARK_NAMESPACE_NAME
438 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
440 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
442 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
445 g_warn_if_fail (app_info != NULL);
447 app_data = bookmark_app_info_dump (app_info);
451 retval = g_string_append (retval, app_data);
457 /* close applications container */
458 g_string_append (retval,
460 "</" BOOKMARK_NAMESPACE_NAME
461 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
465 if (metadata->icon_href)
467 if (!metadata->icon_mime)
468 metadata->icon_mime = g_strdup ("application/octet-stream");
470 buffer = g_strconcat (" "
471 "<" BOOKMARK_NAMESPACE_NAME
472 ":" BOOKMARK_ICON_ELEMENT
473 " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
474 "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
475 g_string_append (retval, buffer);
481 if (metadata->is_private)
482 g_string_append (retval,
484 "<" BOOKMARK_NAMESPACE_NAME
485 ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
487 /* close metadata container */
488 g_string_append (retval,
490 "</" XBEL_METADATA_ELEMENT ">\n");
492 return g_string_free (retval, FALSE);
495 /******************************************************
498 * Storage for a single bookmark item inside the list *
499 ******************************************************/
500 static BookmarkItem *
501 bookmark_item_new (const gchar *uri)
505 g_warn_if_fail (uri != NULL);
507 item = g_slice_new (BookmarkItem);
508 item->uri = g_strdup (uri);
511 item->description = NULL;
513 item->added = (time_t) -1;
514 item->modified = (time_t) -1;
515 item->visited = (time_t) -1;
517 item->metadata = NULL;
523 bookmark_item_free (BookmarkItem *item)
529 g_free (item->title);
530 g_free (item->description);
533 bookmark_metadata_free (item->metadata);
535 g_slice_free (BookmarkItem, item);
539 bookmark_item_dump (BookmarkItem *item)
542 gchar *added, *visited, *modified;
546 /* at this point, we must have at least a registered application; if we don't
547 * we don't screw up the bookmark file, and just skip this item
549 if (!item->metadata || !item->metadata->applications)
551 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
555 retval = g_string_sized_new (4096);
557 added = timestamp_to_iso8601 (item->added);
558 modified = timestamp_to_iso8601 (item->modified);
559 visited = timestamp_to_iso8601 (item->visited);
561 escaped_uri = g_markup_escape_text (item->uri, -1);
563 buffer = g_strconcat (" <"
564 XBEL_BOOKMARK_ELEMENT
566 XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
567 XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
568 XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
569 XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
572 g_string_append (retval, buffer);
574 g_free (escaped_uri);
582 gchar *escaped_title;
584 escaped_title = g_markup_escape_text (item->title, -1);
585 buffer = g_strconcat (" "
586 "<" XBEL_TITLE_ELEMENT ">",
588 "</" XBEL_TITLE_ELEMENT ">\n",
590 g_string_append (retval, buffer);
592 g_free (escaped_title);
596 if (item->description)
600 escaped_desc = g_markup_escape_text (item->description, -1);
601 buffer = g_strconcat (" "
602 "<" XBEL_DESC_ELEMENT ">",
604 "</" XBEL_DESC_ELEMENT ">\n",
606 g_string_append (retval, buffer);
608 g_free (escaped_desc);
616 metadata = bookmark_metadata_dump (item->metadata);
619 buffer = g_strconcat (" "
620 "<" XBEL_INFO_ELEMENT ">\n",
623 "</" XBEL_INFO_ELEMENT ">\n",
625 retval = g_string_append (retval, buffer);
632 g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
634 return g_string_free (retval, FALSE);
637 static BookmarkAppInfo *
638 bookmark_item_lookup_app_info (BookmarkItem *item,
639 const gchar *app_name)
641 g_warn_if_fail (item != NULL && app_name != NULL);
646 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
649 /*************************
651 *************************/
654 g_bookmark_file_init (GBookmarkFile *bookmark)
656 bookmark->title = NULL;
657 bookmark->description = NULL;
659 bookmark->items = NULL;
660 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
667 g_bookmark_file_clear (GBookmarkFile *bookmark)
669 g_free (bookmark->title);
670 g_free (bookmark->description);
672 g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
673 bookmark->items = NULL;
675 if (bookmark->items_by_uri)
677 g_hash_table_destroy (bookmark->items_by_uri);
679 bookmark->items_by_uri = NULL;
687 GHashTable *namespaces;
689 GBookmarkFile *bookmark_file;
690 BookmarkItem *current_item;
694 parse_data_new (void)
698 retval = g_new (ParseData, 1);
700 retval->state = STATE_STARTED;
701 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
702 (GDestroyNotify) g_free,
703 (GDestroyNotify) g_free);
704 retval->bookmark_file = NULL;
705 retval->current_item = NULL;
711 parse_data_free (ParseData *parse_data)
713 g_hash_table_destroy (parse_data->namespaces);
718 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
721 parse_bookmark_element (GMarkupParseContext *context,
722 ParseData *parse_data,
723 const gchar **attribute_names,
724 const gchar **attribute_values,
727 const gchar *uri, *added, *modified, *visited;
733 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
736 uri = added = modified = visited = NULL;
737 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
739 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
740 uri = attribute_values[i];
741 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
742 added = attribute_values[i];
743 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
744 modified = attribute_values[i];
745 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
746 visited = attribute_values[i];
749 /* bookmark is defined by the XBEL spec, so we need
750 * to error out if the element has different or
753 g_set_error (error, G_MARKUP_ERROR,
754 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
755 _("Unexpected attribute '%s' for element '%s'"),
757 XBEL_BOOKMARK_ELEMENT);
764 g_set_error (error, G_MARKUP_ERROR,
765 G_MARKUP_ERROR_INVALID_CONTENT,
766 _("Attribute '%s' of element '%s' not found"),
768 XBEL_BOOKMARK_ELEMENT);
772 g_warn_if_fail (parse_data->current_item == NULL);
774 item = bookmark_item_new (uri);
777 item->added = timestamp_from_iso8601 (added);
780 item->modified = timestamp_from_iso8601 (modified);
783 item->visited = timestamp_from_iso8601 (visited);
786 g_bookmark_file_add_item (parse_data->bookmark_file,
791 bookmark_item_free (item);
793 g_propagate_error (error, add_error);
798 parse_data->current_item = item;
802 parse_application_element (GMarkupParseContext *context,
803 ParseData *parse_data,
804 const gchar **attribute_names,
805 const gchar **attribute_values,
808 const gchar *name, *exec, *count, *stamp, *modified;
814 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
817 name = exec = count = stamp = modified = NULL;
818 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
820 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
821 name = attribute_values[i];
822 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
823 exec = attribute_values[i];
824 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
825 count = attribute_values[i];
826 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
827 stamp = attribute_values[i];
828 else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
829 modified = attribute_values[i];
832 /* the "name" and "exec" attributes are mandatory */
835 g_set_error (error, G_MARKUP_ERROR,
836 G_MARKUP_ERROR_INVALID_CONTENT,
837 _("Attribute '%s' of element '%s' not found"),
838 BOOKMARK_NAME_ATTRIBUTE,
839 BOOKMARK_APPLICATION_ELEMENT);
845 g_set_error (error, G_MARKUP_ERROR,
846 G_MARKUP_ERROR_INVALID_CONTENT,
847 _("Attribute '%s' of element '%s' not found"),
848 BOOKMARK_EXEC_ATTRIBUTE,
849 BOOKMARK_APPLICATION_ELEMENT);
853 g_warn_if_fail (parse_data->current_item != NULL);
854 item = parse_data->current_item;
856 ai = bookmark_item_lookup_app_info (item, name);
859 ai = bookmark_app_info_new (name);
862 item->metadata = bookmark_metadata_new ();
864 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
865 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
868 ai->exec = g_strdup (exec);
871 ai->count = atoi (count);
876 ai->stamp = timestamp_from_iso8601 (modified);
879 /* the timestamp attribute has been deprecated but we still parse
880 * it for backward compatibility
883 ai->stamp = (time_t) atol (stamp);
885 ai->stamp = time (NULL);
890 parse_mime_type_element (GMarkupParseContext *context,
891 ParseData *parse_data,
892 const gchar **attribute_names,
893 const gchar **attribute_values,
901 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
905 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
907 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
908 type = attribute_values[i];
912 type = "application/octet-stream";
914 g_warn_if_fail (parse_data->current_item != NULL);
915 item = parse_data->current_item;
918 item->metadata = bookmark_metadata_new ();
920 item->metadata->mime_type = g_strdup (type);
924 parse_icon_element (GMarkupParseContext *context,
925 ParseData *parse_data,
926 const gchar **attribute_names,
927 const gchar **attribute_values,
936 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
941 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
943 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
944 href = attribute_values[i];
945 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
946 type = attribute_values[i];
949 /* the "href" attribute is mandatory */
952 g_set_error (error, G_MARKUP_ERROR,
953 G_MARKUP_ERROR_INVALID_CONTENT,
954 _("Attribute '%s' of element '%s' not found"),
955 BOOKMARK_HREF_ATTRIBUTE,
956 BOOKMARK_ICON_ELEMENT);
961 type = "application/octet-stream";
963 g_warn_if_fail (parse_data->current_item != NULL);
964 item = parse_data->current_item;
967 item->metadata = bookmark_metadata_new ();
969 item->metadata->icon_href = g_strdup (href);
970 item->metadata->icon_mime = g_strdup (type);
973 /* scans through the attributes of an element for the "xmlns" pragma, and
974 * adds any resulting namespace declaration to a per-parser hashtable, using
975 * the namespace name as a key for the namespace URI; if no key was found,
976 * the namespace is considered as default, and stored under the "default" key.
978 * FIXME: this works on the assumption that the generator of the XBEL file
979 * is either this code or is smart enough to place the namespace declarations
980 * inside the main root node or inside the metadata node and does not redefine
981 * a namespace inside an inner node; this does *not* conform to the
982 * XML-NS standard, although is a close approximation. In order to make this
983 * conformant to the XML-NS specification we should use a per-element
984 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
987 map_namespace_to_name (ParseData *parse_data,
988 const gchar **attribute_names,
989 const gchar **attribute_values)
994 g_warn_if_fail (parse_data != NULL);
996 if (!attribute_names || !attribute_names[0])
1000 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1002 if (g_str_has_prefix (attr, "xmlns"))
1004 gchar *namespace_name, *namespace_uri;
1007 p = g_utf8_strchr (attr, -1, ':');
1009 p = g_utf8_next_char (p);
1013 namespace_name = g_strdup (p);
1014 namespace_uri = g_strdup (attribute_values[i]);
1016 g_hash_table_replace (parse_data->namespaces,
1023 /* checks whether @element_full is equal to @element.
1025 * if @namespace is set, it tries to resolve the namespace to a known URI,
1026 * and if found is prepended to the element name, from which is separated
1027 * using the character specified in the @sep parameter.
1030 is_element_full (ParseData *parse_data,
1031 const gchar *element_full,
1032 const gchar *namespace,
1033 const gchar *element,
1036 gchar *ns_uri, *ns_name;
1037 const gchar *p, *element_name;
1040 g_warn_if_fail (parse_data != NULL);
1041 g_warn_if_fail (element_full != NULL);
1046 /* no namespace requested: dumb element compare */
1048 return (0 == strcmp (element_full, element));
1050 /* search for namespace separator; if none found, assume we are under the
1051 * default namespace, and set ns_name to our "default" marker; if no default
1052 * namespace has been set, just do a plain comparison between @full_element
1055 p = g_utf8_strchr (element_full, -1, ':');
1058 ns_name = g_strndup (element_full, p - element_full);
1059 element_name = g_utf8_next_char (p);
1063 ns_name = g_strdup ("default");
1064 element_name = element_full;
1067 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1070 /* no default namespace found */
1073 return (0 == strcmp (element_full, element));
1076 retval = (0 == strcmp (ns_uri, namespace) &&
1077 0 == strcmp (element_name, element));
1084 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1085 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1088 start_element_raw_cb (GMarkupParseContext *context,
1089 const gchar *element_name,
1090 const gchar **attribute_names,
1091 const gchar **attribute_values,
1095 ParseData *parse_data = (ParseData *) user_data;
1097 /* we must check for namespace declarations first
1099 * XXX - we could speed up things by checking for namespace declarations
1100 * only on the root node, where they usually are; this would probably break
1101 * on streams not produced by us or by "smart" generators
1103 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1105 switch (parse_data->state)
1108 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1114 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1116 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1117 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1118 parse_data->state = STATE_ROOT;
1122 g_set_error (error, G_MARKUP_ERROR,
1123 G_MARKUP_ERROR_INVALID_CONTENT,
1124 _("Unexpected tag '%s', tag '%s' expected"),
1125 element_name, XBEL_ROOT_ELEMENT);
1128 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1129 parse_data->state = STATE_TITLE;
1130 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1131 parse_data->state = STATE_DESC;
1132 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1134 GError *inner_error = NULL;
1136 parse_data->state = STATE_BOOKMARK;
1138 parse_bookmark_element (context,
1144 g_propagate_error (error, inner_error);
1147 g_set_error (error, G_MARKUP_ERROR,
1148 G_MARKUP_ERROR_INVALID_CONTENT,
1149 _("Unexpected tag '%s' inside '%s'"),
1153 case STATE_BOOKMARK:
1154 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1155 parse_data->state = STATE_TITLE;
1156 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1157 parse_data->state = STATE_DESC;
1158 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1159 parse_data->state = STATE_INFO;
1161 g_set_error (error, G_MARKUP_ERROR,
1162 G_MARKUP_ERROR_INVALID_CONTENT,
1163 _("Unexpected tag '%s' inside '%s'"),
1165 XBEL_BOOKMARK_ELEMENT);
1168 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1174 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1176 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1177 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1179 parse_data->state = STATE_METADATA;
1181 if (!parse_data->current_item->metadata)
1182 parse_data->current_item->metadata = bookmark_metadata_new ();
1187 g_set_error (error, G_MARKUP_ERROR,
1188 G_MARKUP_ERROR_INVALID_CONTENT,
1189 _("Unexpected tag '%s', tag '%s' expected"),
1191 XBEL_METADATA_ELEMENT);
1193 case STATE_METADATA:
1194 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1195 parse_data->state = STATE_APPLICATIONS;
1196 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1197 parse_data->state = STATE_GROUPS;
1198 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1199 parse_data->current_item->metadata->is_private = TRUE;
1200 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1202 GError *inner_error = NULL;
1204 parse_data->state = STATE_ICON;
1206 parse_icon_element (context,
1212 g_propagate_error (error, inner_error);
1214 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1216 GError *inner_error = NULL;
1218 parse_data->state = STATE_MIME;
1220 parse_mime_type_element (context,
1226 g_propagate_error (error, inner_error);
1229 g_set_error (error, G_MARKUP_ERROR,
1230 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1231 _("Unexpected tag '%s' inside '%s'"),
1233 XBEL_METADATA_ELEMENT);
1235 case STATE_APPLICATIONS:
1236 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1238 GError *inner_error = NULL;
1240 parse_data->state = STATE_APPLICATION;
1242 parse_application_element (context,
1248 g_propagate_error (error, inner_error);
1251 g_set_error (error, G_MARKUP_ERROR,
1252 G_MARKUP_ERROR_INVALID_CONTENT,
1253 _("Unexpected tag '%s', tag '%s' expected"),
1255 BOOKMARK_APPLICATION_ELEMENT);
1258 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1259 parse_data->state = STATE_GROUP;
1261 g_set_error (error, G_MARKUP_ERROR,
1262 G_MARKUP_ERROR_INVALID_CONTENT,
1263 _("Unexpected tag '%s', tag '%s' expected"),
1265 BOOKMARK_GROUP_ELEMENT);
1268 g_warn_if_reached ();
1274 end_element_raw_cb (GMarkupParseContext *context,
1275 const gchar *element_name,
1279 ParseData *parse_data = (ParseData *) user_data;
1281 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1282 parse_data->state = STATE_FINISHED;
1283 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1285 parse_data->current_item = NULL;
1287 parse_data->state = STATE_ROOT;
1289 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1290 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1291 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1293 if (parse_data->current_item)
1294 parse_data->state = STATE_BOOKMARK;
1296 parse_data->state = STATE_ROOT;
1298 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1299 parse_data->state = STATE_INFO;
1300 else if (IS_ELEMENT_NS (parse_data, element_name,
1301 BOOKMARK_NAMESPACE_URI,
1302 BOOKMARK_APPLICATION_ELEMENT))
1303 parse_data->state = STATE_APPLICATIONS;
1304 else if (IS_ELEMENT_NS (parse_data, element_name,
1305 BOOKMARK_NAMESPACE_URI,
1306 BOOKMARK_GROUP_ELEMENT))
1307 parse_data->state = STATE_GROUPS;
1308 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1309 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1310 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1311 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1312 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1313 parse_data->state = STATE_METADATA;
1317 text_raw_cb (GMarkupParseContext *context,
1323 ParseData *parse_data = (ParseData *) user_data;
1326 payload = g_strndup (text, length);
1328 switch (parse_data->state)
1331 if (parse_data->current_item)
1333 g_free (parse_data->current_item->title);
1334 parse_data->current_item->title = g_strdup (payload);
1338 g_free (parse_data->bookmark_file->title);
1339 parse_data->bookmark_file->title = g_strdup (payload);
1343 if (parse_data->current_item)
1345 g_free (parse_data->current_item->description);
1346 parse_data->current_item->description = g_strdup (payload);
1350 g_free (parse_data->bookmark_file->description);
1351 parse_data->bookmark_file->description = g_strdup (payload);
1358 g_warn_if_fail (parse_data->current_item != NULL);
1360 if (!parse_data->current_item->metadata)
1361 parse_data->current_item->metadata = bookmark_metadata_new ();
1363 groups = parse_data->current_item->metadata->groups;
1364 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1368 case STATE_BOOKMARK:
1370 case STATE_METADATA:
1371 case STATE_APPLICATIONS:
1372 case STATE_APPLICATION:
1378 g_warn_if_reached ();
1385 static const GMarkupParser markup_parser =
1387 start_element_raw_cb, /* start_element */
1388 end_element_raw_cb, /* end_element */
1389 text_raw_cb, /* text */
1390 NULL, /* passthrough */
1395 g_bookmark_file_parse (GBookmarkFile *bookmark,
1396 const gchar *buffer,
1400 GMarkupParseContext *context;
1401 ParseData *parse_data;
1402 GError *parse_error, *end_error;
1405 g_warn_if_fail (bookmark != NULL);
1413 if (length == (gsize) -1)
1414 length = strlen (buffer);
1416 parse_data = parse_data_new ();
1417 parse_data->bookmark_file = bookmark;
1419 context = g_markup_parse_context_new (&markup_parser,
1422 (GDestroyNotify) parse_data_free);
1424 retval = g_markup_parse_context_parse (context,
1429 g_propagate_error (error, parse_error);
1432 retval = g_markup_parse_context_end_parse (context, &end_error);
1434 g_propagate_error (error, end_error);
1437 g_markup_parse_context_free (context);
1443 g_bookmark_file_dump (GBookmarkFile *bookmark,
1451 retval = g_string_sized_new (4096);
1453 g_string_append (retval,
1454 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1456 /* XXX - do we really need the doctype? */
1457 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1458 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1459 " \"" XBEL_DTD_URI "\">\n"
1461 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1462 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1463 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1465 if (bookmark->title)
1467 gchar *escaped_title;
1469 escaped_title = g_markup_escape_text (bookmark->title, -1);
1471 buffer = g_strconcat (" "
1472 "<" XBEL_TITLE_ELEMENT ">",
1474 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1476 g_string_append (retval, buffer);
1479 g_free (escaped_title);
1482 if (bookmark->description)
1484 gchar *escaped_desc;
1486 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1488 buffer = g_strconcat (" "
1489 "<" XBEL_DESC_ELEMENT ">",
1491 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1492 g_string_append (retval, buffer);
1495 g_free (escaped_desc);
1498 if (!bookmark->items)
1501 retval = g_string_append (retval, "\n");
1503 /* the items are stored in reverse order */
1504 for (l = g_list_last (bookmark->items);
1508 BookmarkItem *item = (BookmarkItem *) l->data;
1511 item_dump = bookmark_item_dump (item);
1515 retval = g_string_append (retval, item_dump);
1521 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1524 *length = retval->len;
1526 return g_string_free (retval, FALSE);
1533 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1534 * should free the returned string.
1537 timestamp_to_iso8601 (time_t timestamp)
1541 if (timestamp == (time_t) -1)
1542 g_get_current_time (&stamp);
1545 stamp.tv_sec = timestamp;
1549 return g_time_val_to_iso8601 (&stamp);
1553 timestamp_from_iso8601 (const gchar *iso_date)
1557 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1560 return (time_t) stamp.tv_sec;
1563 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1565 /********************
1567 ********************/
1570 * g_bookmark_file_new:
1572 * Creates a new empty #GBookmarkFile object.
1574 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1575 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1578 * Returns: an empty #GBookmarkFile
1583 g_bookmark_file_new (void)
1585 GBookmarkFile *bookmark;
1587 bookmark = g_new (GBookmarkFile, 1);
1589 g_bookmark_file_init (bookmark);
1595 * g_bookmark_file_free:
1596 * @bookmark: a #GBookmarkFile
1598 * Frees a #GBookmarkFile.
1603 g_bookmark_file_free (GBookmarkFile *bookmark)
1608 g_bookmark_file_clear (bookmark);
1614 * g_bookmark_file_load_from_data:
1615 * @bookmark: an empty #GBookmarkFile struct
1616 * @data: desktop bookmarks loaded in memory
1617 * @length: the length of @data in bytes
1618 * @error: return location for a #GError, or %NULL
1620 * Loads a bookmark file from memory into an empty #GBookmarkFile
1621 * structure. If the object cannot be created then @error is set to a
1622 * #GBookmarkFileError.
1624 * Returns: %TRUE if a desktop bookmark could be loaded.
1629 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1634 GError *parse_error;
1637 g_return_val_if_fail (bookmark != NULL, FALSE);
1639 if (length == (gsize) -1)
1640 length = strlen (data);
1642 if (bookmark->items)
1644 g_bookmark_file_clear (bookmark);
1645 g_bookmark_file_init (bookmark);
1649 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1652 g_propagate_error (error, parse_error);
1658 * g_bookmark_file_load_from_file:
1659 * @bookmark: an empty #GBookmarkFile struct
1660 * @filename: (type filename): the path of a filename to load, in the
1661 * GLib file name encoding
1662 * @error: return location for a #GError, or %NULL
1664 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1665 * If the file could not be loaded then @error is set to either a #GFileError
1666 * or #GBookmarkFileError.
1668 * Returns: %TRUE if a desktop bookmark file could be loaded
1673 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1674 const gchar *filename,
1677 gboolean ret = FALSE;
1678 gchar *buffer = NULL;
1681 g_return_val_if_fail (bookmark != NULL, FALSE);
1682 g_return_val_if_fail (filename != NULL, FALSE);
1684 if (!g_file_get_contents (filename, &buffer, &len, error))
1687 if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1697 /* Iterates through all the directories in *dirs trying to
1698 * find file. When it successfully locates file, returns a
1699 * string its absolute path. It also leaves the unchecked
1700 * directories in *dirs. You should free the returned string
1702 * Adapted from gkeyfile.c
1705 find_file_in_data_dirs (const gchar *file,
1709 gchar **data_dirs, *data_dir, *path;
1718 while (data_dirs && (data_dir = *data_dirs) && !path)
1720 gchar *candidate_file, *sub_dir;
1722 candidate_file = (gchar *) file;
1723 sub_dir = g_strdup ("");
1724 while (candidate_file != NULL && !path)
1728 path = g_build_filename (data_dir, sub_dir,
1729 candidate_file, NULL);
1731 candidate_file = strchr (candidate_file, '-');
1733 if (candidate_file == NULL)
1739 sub_dir = g_strndup (file, candidate_file - file - 1);
1741 for (p = sub_dir; *p != '\0'; p++)
1744 *p = G_DIR_SEPARATOR;
1755 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1756 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1757 _("No valid bookmark file found in data dirs"));
1767 * g_bookmark_file_load_from_data_dirs:
1768 * @bookmark: a #GBookmarkFile
1769 * @file: (type filename): a relative path to a filename to open and parse
1770 * @full_path: (type filename) (allow-none): return location for a string
1771 * containing the full path of the file, or %NULL
1772 * @error: return location for a #GError, or %NULL
1774 * This function looks for a desktop bookmark file named @file in the
1775 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1776 * loads the file into @bookmark and returns the file's full path in
1777 * @full_path. If the file could not be loaded then an %error is
1778 * set to either a #GFileError or #GBookmarkFileError.
1780 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1785 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1790 GError *file_error = NULL;
1791 gchar **all_data_dirs, **data_dirs;
1792 const gchar *user_data_dir;
1793 const gchar * const * system_data_dirs;
1796 gboolean found_file;
1798 g_return_val_if_fail (bookmark != NULL, FALSE);
1799 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1801 user_data_dir = g_get_user_data_dir ();
1802 system_data_dirs = g_get_system_data_dirs ();
1803 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1806 all_data_dirs[i++] = g_strdup (user_data_dir);
1809 while (system_data_dirs[j] != NULL)
1810 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1813 data_dirs = all_data_dirs;
1815 while (*data_dirs != NULL && !found_file)
1817 g_free (output_path);
1819 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1823 g_propagate_error (error, file_error);
1827 found_file = g_bookmark_file_load_from_file (bookmark,
1832 g_propagate_error (error, file_error);
1837 if (found_file && full_path)
1838 *full_path = output_path;
1840 g_free (output_path);
1842 g_strfreev (all_data_dirs);
1849 * g_bookmark_file_to_data:
1850 * @bookmark: a #GBookmarkFile
1851 * @length: (allow-none) (out): return location for the length of the returned string, or %NULL
1852 * @error: return location for a #GError, or %NULL
1854 * This function outputs @bookmark as a string.
1856 * Returns: a newly allocated string holding
1857 * the contents of the #GBookmarkFile
1862 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1866 GError *write_error = NULL;
1869 g_return_val_if_fail (bookmark != NULL, NULL);
1871 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1874 g_propagate_error (error, write_error);
1883 * g_bookmark_file_to_file:
1884 * @bookmark: a #GBookmarkFile
1885 * @filename: (type filename): path of the output file
1886 * @error: return location for a #GError, or %NULL
1888 * This function outputs @bookmark into a file. The write process is
1889 * guaranteed to be atomic by using g_file_set_contents() internally.
1891 * Returns: %TRUE if the file was successfully written.
1896 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1897 const gchar *filename,
1901 GError *data_error, *write_error;
1905 g_return_val_if_fail (bookmark != NULL, FALSE);
1906 g_return_val_if_fail (filename != NULL, FALSE);
1909 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1912 g_propagate_error (error, data_error);
1918 g_file_set_contents (filename, data, len, &write_error);
1921 g_propagate_error (error, write_error);
1933 static BookmarkItem *
1934 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1937 g_warn_if_fail (bookmark != NULL && uri != NULL);
1939 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1942 /* this function adds a new item to the list */
1944 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1948 g_warn_if_fail (bookmark != NULL);
1949 g_warn_if_fail (item != NULL);
1951 /* this should never happen; and if it does, then we are
1952 * screwing up something big time.
1954 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1956 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1957 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1958 _("A bookmark for URI '%s' already exists"),
1963 bookmark->items = g_list_prepend (bookmark->items, item);
1965 g_hash_table_replace (bookmark->items_by_uri,
1969 if (item->added == (time_t) -1)
1970 item->added = time (NULL);
1972 if (item->modified == (time_t) -1)
1973 item->modified = time (NULL);
1977 * g_bookmark_file_remove_item:
1978 * @bookmark: a #GBookmarkFile
1980 * @error: return location for a #GError, or %NULL
1982 * Removes the bookmark for @uri from the bookmark file @bookmark.
1984 * Returns: %TRUE if the bookmark was removed successfully.
1989 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
1995 g_return_val_if_fail (bookmark != NULL, FALSE);
1996 g_return_val_if_fail (uri != NULL, FALSE);
1998 item = g_bookmark_file_lookup_item (bookmark, uri);
2002 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2003 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2004 _("No bookmark found for URI '%s'"),
2009 bookmark->items = g_list_remove (bookmark->items, item);
2010 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2012 bookmark_item_free (item);
2018 * g_bookmark_file_has_item:
2019 * @bookmark: a #GBookmarkFile
2022 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2024 * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2029 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2032 g_return_val_if_fail (bookmark != NULL, FALSE);
2033 g_return_val_if_fail (uri != NULL, FALSE);
2035 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2039 * g_bookmark_file_get_uris:
2040 * @bookmark: a #GBookmarkFile
2041 * @length: (allow-none) (out): return location for the number of returned URIs, or %NULL
2043 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2044 * The array of returned URIs will be %NULL-terminated, so @length may
2045 * optionally be %NULL.
2047 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2048 * Use g_strfreev() to free it.
2053 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2060 g_return_val_if_fail (bookmark != NULL, NULL);
2062 n_items = g_list_length (bookmark->items);
2063 uris = g_new0 (gchar *, n_items + 1);
2065 /* the items are stored in reverse order, so we walk the list backward */
2066 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2068 BookmarkItem *item = (BookmarkItem *) l->data;
2070 g_warn_if_fail (item != NULL);
2072 uris[i++] = g_strdup (item->uri);
2083 * g_bookmark_file_set_title:
2084 * @bookmark: a #GBookmarkFile
2085 * @uri: (allow-none): a valid URI or %NULL
2086 * @title: a UTF-8 encoded string
2088 * Sets @title as the title of the bookmark for @uri inside the
2089 * bookmark file @bookmark.
2091 * If @uri is %NULL, the title of @bookmark is set.
2093 * If a bookmark for @uri cannot be found then it is created.
2098 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2102 g_return_if_fail (bookmark != NULL);
2106 g_free (bookmark->title);
2107 bookmark->title = g_strdup (title);
2113 item = g_bookmark_file_lookup_item (bookmark, uri);
2116 item = bookmark_item_new (uri);
2117 g_bookmark_file_add_item (bookmark, item, NULL);
2120 g_free (item->title);
2121 item->title = g_strdup (title);
2123 item->modified = time (NULL);
2128 * g_bookmark_file_get_title:
2129 * @bookmark: a #GBookmarkFile
2130 * @uri: (allow-none): a valid URI or %NULL
2131 * @error: return location for a #GError, or %NULL
2133 * Returns the title of the bookmark for @uri.
2135 * If @uri is %NULL, the title of @bookmark is returned.
2137 * In the event the URI cannot be found, %NULL is returned and
2138 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2140 * Returns: a newly allocated string or %NULL if the specified
2141 * URI cannot be found.
2146 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2152 g_return_val_if_fail (bookmark != NULL, NULL);
2155 return g_strdup (bookmark->title);
2157 item = g_bookmark_file_lookup_item (bookmark, uri);
2160 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2161 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2162 _("No bookmark found for URI '%s'"),
2167 return g_strdup (item->title);
2171 * g_bookmark_file_set_description:
2172 * @bookmark: a #GBookmarkFile
2173 * @uri: (allow-none): a valid URI or %NULL
2174 * @description: a string
2176 * Sets @description as the description of the bookmark for @uri.
2178 * If @uri is %NULL, the description of @bookmark is set.
2180 * If a bookmark for @uri cannot be found then it is created.
2185 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2187 const gchar *description)
2189 g_return_if_fail (bookmark != NULL);
2193 g_free (bookmark->description);
2194 bookmark->description = g_strdup (description);
2200 item = g_bookmark_file_lookup_item (bookmark, uri);
2203 item = bookmark_item_new (uri);
2204 g_bookmark_file_add_item (bookmark, item, NULL);
2207 g_free (item->description);
2208 item->description = g_strdup (description);
2210 item->modified = time (NULL);
2215 * g_bookmark_file_get_description:
2216 * @bookmark: a #GBookmarkFile
2218 * @error: return location for a #GError, or %NULL
2220 * Retrieves the description of the bookmark for @uri.
2222 * In the event the URI cannot be found, %NULL is returned and
2223 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2225 * Returns: a newly allocated string or %NULL if the specified
2226 * URI cannot be found.
2231 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2237 g_return_val_if_fail (bookmark != NULL, NULL);
2240 return g_strdup (bookmark->description);
2242 item = g_bookmark_file_lookup_item (bookmark, uri);
2245 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2246 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2247 _("No bookmark found for URI '%s'"),
2252 return g_strdup (item->description);
2256 * g_bookmark_file_set_mime_type:
2257 * @bookmark: a #GBookmarkFile
2259 * @mime_type: a MIME type
2261 * Sets @mime_type as the MIME type of the bookmark for @uri.
2263 * If a bookmark for @uri cannot be found then it is created.
2268 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2270 const gchar *mime_type)
2274 g_return_if_fail (bookmark != NULL);
2275 g_return_if_fail (uri != NULL);
2276 g_return_if_fail (mime_type != NULL);
2278 item = g_bookmark_file_lookup_item (bookmark, uri);
2281 item = bookmark_item_new (uri);
2282 g_bookmark_file_add_item (bookmark, item, NULL);
2285 if (!item->metadata)
2286 item->metadata = bookmark_metadata_new ();
2288 g_free (item->metadata->mime_type);
2290 item->metadata->mime_type = g_strdup (mime_type);
2291 item->modified = time (NULL);
2295 * g_bookmark_file_get_mime_type:
2296 * @bookmark: a #GBookmarkFile
2298 * @error: return location for a #GError, or %NULL
2300 * Retrieves the MIME type of the resource pointed by @uri.
2302 * In the event the URI cannot be found, %NULL is returned and
2303 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2304 * event that the MIME type cannot be found, %NULL is returned and
2305 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2307 * Returns: a newly allocated string or %NULL if the specified
2308 * URI cannot be found.
2313 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2319 g_return_val_if_fail (bookmark != NULL, NULL);
2320 g_return_val_if_fail (uri != NULL, NULL);
2322 item = g_bookmark_file_lookup_item (bookmark, uri);
2325 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2326 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2327 _("No bookmark found for URI '%s'"),
2332 if (!item->metadata)
2334 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2335 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2336 _("No MIME type defined in the bookmark for URI '%s'"),
2341 return g_strdup (item->metadata->mime_type);
2345 * g_bookmark_file_set_is_private:
2346 * @bookmark: a #GBookmarkFile
2348 * @is_private: %TRUE if the bookmark should be marked as private
2350 * Sets the private flag of the bookmark for @uri.
2352 * If a bookmark for @uri cannot be found then it is created.
2357 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2359 gboolean is_private)
2363 g_return_if_fail (bookmark != NULL);
2364 g_return_if_fail (uri != NULL);
2366 item = g_bookmark_file_lookup_item (bookmark, uri);
2369 item = bookmark_item_new (uri);
2370 g_bookmark_file_add_item (bookmark, item, NULL);
2373 if (!item->metadata)
2374 item->metadata = bookmark_metadata_new ();
2376 item->metadata->is_private = (is_private == TRUE);
2377 item->modified = time (NULL);
2381 * g_bookmark_file_get_is_private:
2382 * @bookmark: a #GBookmarkFile
2384 * @error: return location for a #GError, or %NULL
2386 * Gets whether the private flag of the bookmark for @uri is set.
2388 * In the event the URI cannot be found, %FALSE is returned and
2389 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2390 * event that the private flag cannot be found, %FALSE is returned and
2391 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2393 * Returns: %TRUE if the private flag is set, %FALSE otherwise.
2398 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2404 g_return_val_if_fail (bookmark != NULL, FALSE);
2405 g_return_val_if_fail (uri != NULL, FALSE);
2407 item = g_bookmark_file_lookup_item (bookmark, uri);
2410 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2411 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2412 _("No bookmark found for URI '%s'"),
2417 if (!item->metadata)
2419 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2420 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2421 _("No private flag has been defined in bookmark for URI '%s'"),
2426 return item->metadata->is_private;
2430 * g_bookmark_file_set_added:
2431 * @bookmark: a #GBookmarkFile
2433 * @added: a timestamp or -1 to use the current time
2435 * Sets the time the bookmark for @uri was added into @bookmark.
2437 * If no bookmark for @uri is found then it is created.
2442 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2448 g_return_if_fail (bookmark != NULL);
2449 g_return_if_fail (uri != NULL);
2451 item = g_bookmark_file_lookup_item (bookmark, uri);
2454 item = bookmark_item_new (uri);
2455 g_bookmark_file_add_item (bookmark, item, NULL);
2458 if (added == (time_t) -1)
2461 item->added = added;
2462 item->modified = added;
2466 * g_bookmark_file_get_added:
2467 * @bookmark: a #GBookmarkFile
2469 * @error: return location for a #GError, or %NULL
2471 * Gets the time the bookmark for @uri was added to @bookmark
2473 * In the event the URI cannot be found, -1 is returned and
2474 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2476 * Returns: a timestamp
2481 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2487 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2488 g_return_val_if_fail (uri != NULL, (time_t) -1);
2490 item = g_bookmark_file_lookup_item (bookmark, uri);
2493 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2494 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2495 _("No bookmark found for URI '%s'"),
2504 * g_bookmark_file_set_modified:
2505 * @bookmark: a #GBookmarkFile
2507 * @modified: a timestamp or -1 to use the current time
2509 * Sets the last time the bookmark for @uri was last modified.
2511 * If no bookmark for @uri is found then it is created.
2513 * The "modified" time should only be set when the bookmark's meta-data
2514 * was actually changed. Every function of #GBookmarkFile that
2515 * modifies a bookmark also changes the modification time, except for
2516 * g_bookmark_file_set_visited().
2521 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2527 g_return_if_fail (bookmark != NULL);
2528 g_return_if_fail (uri != NULL);
2530 item = g_bookmark_file_lookup_item (bookmark, uri);
2533 item = bookmark_item_new (uri);
2534 g_bookmark_file_add_item (bookmark, item, NULL);
2537 if (modified == (time_t) -1)
2540 item->modified = modified;
2544 * g_bookmark_file_get_modified:
2545 * @bookmark: a #GBookmarkFile
2547 * @error: return location for a #GError, or %NULL
2549 * Gets the time when the bookmark for @uri was last modified.
2551 * In the event the URI cannot be found, -1 is returned and
2552 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2554 * Returns: a timestamp
2559 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2565 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2566 g_return_val_if_fail (uri != NULL, (time_t) -1);
2568 item = g_bookmark_file_lookup_item (bookmark, uri);
2571 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2572 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2573 _("No bookmark found for URI '%s'"),
2578 return item->modified;
2582 * g_bookmark_file_set_visited:
2583 * @bookmark: a #GBookmarkFile
2585 * @visited: a timestamp or -1 to use the current time
2587 * Sets the time the bookmark for @uri was last visited.
2589 * If no bookmark for @uri is found then it is created.
2591 * The "visited" time should only be set if the bookmark was launched,
2592 * either using the command line retrieved by g_bookmark_file_get_app_info()
2593 * or by the default application for the bookmark's MIME type, retrieved
2594 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2595 * does not affect the "modified" time.
2600 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2606 g_return_if_fail (bookmark != NULL);
2607 g_return_if_fail (uri != NULL);
2609 item = g_bookmark_file_lookup_item (bookmark, uri);
2612 item = bookmark_item_new (uri);
2613 g_bookmark_file_add_item (bookmark, item, NULL);
2616 if (visited == (time_t) -1)
2619 item->visited = visited;
2623 * g_bookmark_file_get_visited:
2624 * @bookmark: a #GBookmarkFile
2626 * @error: return location for a #GError, or %NULL
2628 * Gets the time the bookmark for @uri was last visited.
2630 * In the event the URI cannot be found, -1 is returned and
2631 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2633 * Returns: a timestamp.
2638 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2644 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2645 g_return_val_if_fail (uri != NULL, (time_t) -1);
2647 item = g_bookmark_file_lookup_item (bookmark, uri);
2650 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2651 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2652 _("No bookmark found for URI '%s'"),
2657 return item->visited;
2661 * g_bookmark_file_has_group:
2662 * @bookmark: a #GBookmarkFile
2664 * @group: the group name to be searched
2665 * @error: return location for a #GError, or %NULL
2667 * Checks whether @group appears in the list of groups to which
2668 * the bookmark for @uri belongs to.
2670 * In the event the URI cannot be found, %FALSE is returned and
2671 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2673 * Returns: %TRUE if @group was found.
2678 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2686 g_return_val_if_fail (bookmark != NULL, FALSE);
2687 g_return_val_if_fail (uri != NULL, FALSE);
2689 item = g_bookmark_file_lookup_item (bookmark, uri);
2692 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2693 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2694 _("No bookmark found for URI '%s'"),
2699 if (!item->metadata)
2702 for (l = item->metadata->groups; l != NULL; l = l->next)
2704 if (strcmp (l->data, group) == 0)
2713 * g_bookmark_file_add_group:
2714 * @bookmark: a #GBookmarkFile
2716 * @group: the group name to be added
2718 * Adds @group to the list of groups to which the bookmark for @uri
2721 * If no bookmark for @uri is found then it is created.
2726 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2732 g_return_if_fail (bookmark != NULL);
2733 g_return_if_fail (uri != NULL);
2734 g_return_if_fail (group != NULL && group[0] != '\0');
2736 item = g_bookmark_file_lookup_item (bookmark, uri);
2739 item = bookmark_item_new (uri);
2740 g_bookmark_file_add_item (bookmark, item, NULL);
2743 if (!item->metadata)
2744 item->metadata = bookmark_metadata_new ();
2746 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2748 item->metadata->groups = g_list_prepend (item->metadata->groups,
2751 item->modified = time (NULL);
2756 * g_bookmark_file_remove_group:
2757 * @bookmark: a #GBookmarkFile
2759 * @group: the group name to be removed
2760 * @error: return location for a #GError, or %NULL
2762 * Removes @group from the list of groups to which the bookmark
2763 * for @uri belongs to.
2765 * In the event the URI cannot be found, %FALSE is returned and
2766 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2767 * In the event no group was defined, %FALSE is returned and
2768 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2770 * Returns: %TRUE if @group was successfully removed.
2775 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2783 g_return_val_if_fail (bookmark != NULL, FALSE);
2784 g_return_val_if_fail (uri != NULL, FALSE);
2786 item = g_bookmark_file_lookup_item (bookmark, uri);
2789 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2790 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2791 _("No bookmark found for URI '%s'"),
2796 if (!item->metadata)
2798 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2799 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2800 _("No groups set in bookmark for URI '%s'"),
2805 for (l = item->metadata->groups; l != NULL; l = l->next)
2807 if (strcmp (l->data, group) == 0)
2809 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2813 item->modified = time (NULL);
2823 * g_bookmark_file_set_groups:
2824 * @bookmark: a #GBookmarkFile
2825 * @uri: an item's URI
2826 * @groups: (allow-none): an array of group names, or %NULL to remove all groups
2827 * @length: number of group name values in @groups
2829 * Sets a list of group names for the item with URI @uri. Each previously
2830 * set group name list is removed.
2832 * If @uri cannot be found then an item for it is created.
2837 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2839 const gchar **groups,
2845 g_return_if_fail (bookmark != NULL);
2846 g_return_if_fail (uri != NULL);
2847 g_return_if_fail (groups != NULL);
2849 item = g_bookmark_file_lookup_item (bookmark, uri);
2852 item = bookmark_item_new (uri);
2853 g_bookmark_file_add_item (bookmark, item, NULL);
2856 if (!item->metadata)
2857 item->metadata = bookmark_metadata_new ();
2859 g_list_free_full (item->metadata->groups, g_free);
2860 item->metadata->groups = NULL;
2864 for (i = 0; groups[i] != NULL && i < length; i++)
2865 item->metadata->groups = g_list_append (item->metadata->groups,
2866 g_strdup (groups[i]));
2869 item->modified = time (NULL);
2873 * g_bookmark_file_get_groups:
2874 * @bookmark: a #GBookmarkFile
2876 * @length: (allow-none) (out): return location for the length of the returned string, or %NULL
2877 * @error: return location for a #GError, or %NULL
2879 * Retrieves the list of group names of the bookmark for @uri.
2881 * In the event the URI cannot be found, %NULL is returned and
2882 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2884 * The returned array is %NULL terminated, so @length may optionally
2887 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
2888 * Use g_strfreev() to free it.
2893 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2903 g_return_val_if_fail (bookmark != NULL, NULL);
2904 g_return_val_if_fail (uri != NULL, NULL);
2906 item = g_bookmark_file_lookup_item (bookmark, uri);
2909 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2910 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2911 _("No bookmark found for URI '%s'"),
2916 if (!item->metadata)
2924 len = g_list_length (item->metadata->groups);
2925 retval = g_new0 (gchar *, len + 1);
2926 for (l = g_list_last (item->metadata->groups), i = 0;
2930 gchar *group_name = (gchar *) l->data;
2932 g_warn_if_fail (group_name != NULL);
2934 retval[i++] = g_strdup (group_name);
2945 * g_bookmark_file_add_application:
2946 * @bookmark: a #GBookmarkFile
2948 * @name: (allow-none): the name of the application registering the bookmark
2950 * @exec: (allow-none): command line to be used to launch the bookmark or %NULL
2952 * Adds the application with @name and @exec to the list of
2953 * applications that have registered a bookmark for @uri into
2956 * Every bookmark inside a #GBookmarkFile must have at least an
2957 * application registered. Each application must provide a name, a
2958 * command line useful for launching the bookmark, the number of times
2959 * the bookmark has been registered by the application and the last
2960 * time the application registered this bookmark.
2962 * If @name is %NULL, the name of the application will be the
2963 * same returned by g_get_application_name(); if @exec is %NULL, the
2964 * command line will be a composition of the program name as
2965 * returned by g_get_prgname() and the "\%u" modifier, which will be
2966 * expanded to the bookmark's URI.
2968 * This function will automatically take care of updating the
2969 * registrations count and timestamping in case an application
2970 * with the same @name had already registered a bookmark for
2971 * @uri inside @bookmark.
2973 * If no bookmark for @uri is found, one is created.
2978 g_bookmark_file_add_application (GBookmarkFile *bookmark,
2984 gchar *app_name, *app_exec;
2986 g_return_if_fail (bookmark != NULL);
2987 g_return_if_fail (uri != NULL);
2989 item = g_bookmark_file_lookup_item (bookmark, uri);
2992 item = bookmark_item_new (uri);
2993 g_bookmark_file_add_item (bookmark, item, NULL);
2996 if (name && name[0] != '\0')
2997 app_name = g_strdup (name);
2999 app_name = g_strdup (g_get_application_name ());
3001 if (exec && exec[0] != '\0')
3002 app_exec = g_strdup (exec);
3004 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3006 g_bookmark_file_set_app_info (bookmark, uri,
3018 * g_bookmark_file_remove_application:
3019 * @bookmark: a #GBookmarkFile
3021 * @name: the name of the application
3022 * @error: return location for a #GError or %NULL
3024 * Removes application registered with @name from the list of applications
3025 * that have registered a bookmark for @uri inside @bookmark.
3027 * In the event the URI cannot be found, %FALSE is returned and
3028 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3029 * In the event that no application with name @app_name has registered
3030 * a bookmark for @uri, %FALSE is returned and error is set to
3031 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3033 * Returns: %TRUE if the application was successfully removed.
3038 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3046 g_return_val_if_fail (bookmark != NULL, FALSE);
3047 g_return_val_if_fail (uri != NULL, FALSE);
3048 g_return_val_if_fail (name != NULL, FALSE);
3051 retval = g_bookmark_file_set_app_info (bookmark, uri,
3059 g_propagate_error (error, set_error);
3068 * g_bookmark_file_has_application:
3069 * @bookmark: a #GBookmarkFile
3071 * @name: the name of the application
3072 * @error: return location for a #GError or %NULL
3074 * Checks whether the bookmark for @uri inside @bookmark has been
3075 * registered by application @name.
3077 * In the event the URI cannot be found, %FALSE is returned and
3078 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3080 * Returns: %TRUE if the application @name was found
3085 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3092 g_return_val_if_fail (bookmark != NULL, FALSE);
3093 g_return_val_if_fail (uri != NULL, FALSE);
3094 g_return_val_if_fail (name != NULL, FALSE);
3096 item = g_bookmark_file_lookup_item (bookmark, uri);
3099 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3100 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3101 _("No bookmark found for URI '%s'"),
3106 return (NULL != bookmark_item_lookup_app_info (item, name));
3110 * g_bookmark_file_set_app_info:
3111 * @bookmark: a #GBookmarkFile
3113 * @name: an application's name
3114 * @exec: an application's command line
3115 * @count: the number of registrations done for this application
3116 * @stamp: the time of the last registration for this application
3117 * @error: return location for a #GError or %NULL
3119 * Sets the meta-data of application @name inside the list of
3120 * applications that have registered a bookmark for @uri inside
3123 * You should rarely use this function; use g_bookmark_file_add_application()
3124 * and g_bookmark_file_remove_application() instead.
3126 * @name can be any UTF-8 encoded string used to identify an
3128 * @exec can have one of these two modifiers: "\%f", which will
3129 * be expanded as the local file name retrieved from the bookmark's
3130 * URI; "\%u", which will be expanded as the bookmark's URI.
3131 * The expansion is done automatically when retrieving the stored
3132 * command line using the g_bookmark_file_get_app_info() function.
3133 * @count is the number of times the application has registered the
3134 * bookmark; if is < 0, the current registration count will be increased
3135 * by one, if is 0, the application with @name will be removed from
3136 * the list of registered applications.
3137 * @stamp is the Unix time of the last registration; if it is -1, the
3138 * current time will be used.
3140 * If you try to remove an application by setting its registration count to
3141 * zero, and no bookmark for @uri is found, %FALSE is returned and
3142 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3143 * in the event that no application @name has registered a bookmark
3144 * for @uri, %FALSE is returned and error is set to
3145 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3146 * for @uri is found, one is created.
3148 * Returns: %TRUE if the application's meta-data was successfully
3154 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3163 BookmarkAppInfo *ai;
3165 g_return_val_if_fail (bookmark != NULL, FALSE);
3166 g_return_val_if_fail (uri != NULL, FALSE);
3167 g_return_val_if_fail (name != NULL, FALSE);
3168 g_return_val_if_fail (exec != NULL, FALSE);
3170 item = g_bookmark_file_lookup_item (bookmark, uri);
3175 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3176 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3177 _("No bookmark found for URI '%s'"),
3183 item = bookmark_item_new (uri);
3184 g_bookmark_file_add_item (bookmark, item, NULL);
3188 if (!item->metadata)
3189 item->metadata = bookmark_metadata_new ();
3191 ai = bookmark_item_lookup_app_info (item, name);
3196 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3197 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3198 _("No application with name '%s' registered a bookmark for '%s'"),
3205 ai = bookmark_app_info_new (name);
3207 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3208 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3214 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3215 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3216 bookmark_app_info_free (ai);
3218 item->modified = time (NULL);
3227 if (stamp != (time_t) -1)
3230 ai->stamp = time (NULL);
3232 if (exec && exec[0] != '\0')
3235 ai->exec = g_shell_quote (exec);
3238 item->modified = time (NULL);
3243 /* expands the application's command line */
3245 expand_exec_line (const gchar *exec_fmt,
3251 exec = g_string_sized_new (512);
3252 while ((ch = *exec_fmt++) != '\0')
3256 exec = g_string_append_c (exec, ch);
3267 g_string_append (exec, uri);
3272 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3275 g_string_append (exec, file);
3280 g_string_free (exec, TRUE);
3287 exec = g_string_append_c (exec, ch);
3293 return g_string_free (exec, FALSE);
3297 * g_bookmark_file_get_app_info:
3298 * @bookmark: a #GBookmarkFile
3300 * @name: an application's name
3301 * @exec: (allow-none) (out): return location for the command line of the application, or %NULL
3302 * @count: (allow-none) (out): return location for the registration count, or %NULL
3303 * @stamp: (allow-none) (out): return location for the last registration time, or %NULL
3304 * @error: return location for a #GError, or %NULL
3306 * Gets the registration informations of @app_name for the bookmark for
3307 * @uri. See g_bookmark_file_set_app_info() for more informations about
3308 * the returned data.
3310 * The string returned in @app_exec must be freed.
3312 * In the event the URI cannot be found, %FALSE is returned and
3313 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3314 * event that no application with name @app_name has registered a bookmark
3315 * for @uri, %FALSE is returned and error is set to
3316 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3317 * the command line fails, an error of the #G_SHELL_ERROR domain is
3318 * set and %FALSE is returned.
3320 * Returns: %TRUE on success.
3325 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3334 BookmarkAppInfo *ai;
3336 g_return_val_if_fail (bookmark != NULL, FALSE);
3337 g_return_val_if_fail (uri != NULL, FALSE);
3338 g_return_val_if_fail (name != NULL, FALSE);
3340 item = g_bookmark_file_lookup_item (bookmark, uri);
3343 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3344 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3345 _("No bookmark found for URI '%s'"),
3350 ai = bookmark_item_lookup_app_info (item, name);
3353 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3354 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3355 _("No application with name '%s' registered a bookmark for '%s'"),
3363 GError *unquote_error = NULL;
3364 gchar *command_line;
3366 command_line = g_shell_unquote (ai->exec, &unquote_error);
3369 g_propagate_error (error, unquote_error);
3373 *exec = expand_exec_line (command_line, uri);
3376 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3377 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3378 _("Failed to expand exec line '%s' with URI '%s'"),
3380 g_free (command_line);
3385 g_free (command_line);
3398 * g_bookmark_file_get_applications:
3399 * @bookmark: a #GBookmarkFile
3401 * @length: (allow-none) (out): return location of the length of the returned list, or %NULL
3402 * @error: return location for a #GError, or %NULL
3404 * Retrieves the names of the applications that have registered the
3405 * bookmark for @uri.
3407 * In the event the URI cannot be found, %NULL is returned and
3408 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3410 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3411 * Use g_strfreev() to free it.
3416 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3426 g_return_val_if_fail (bookmark != NULL, NULL);
3427 g_return_val_if_fail (uri != NULL, NULL);
3429 item = g_bookmark_file_lookup_item (bookmark, uri);
3432 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3433 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3434 _("No bookmark found for URI '%s'"),
3439 if (!item->metadata)
3447 n_apps = g_list_length (item->metadata->applications);
3448 apps = g_new0 (gchar *, n_apps + 1);
3450 for (l = g_list_last (item->metadata->applications), i = 0;
3454 BookmarkAppInfo *ai;
3456 ai = (BookmarkAppInfo *) l->data;
3458 g_warn_if_fail (ai != NULL);
3459 g_warn_if_fail (ai->name != NULL);
3461 apps[i++] = g_strdup (ai->name);
3472 * g_bookmark_file_get_size:
3473 * @bookmark: a #GBookmarkFile
3475 * Gets the number of bookmarks inside @bookmark.
3477 * Returns: the number of bookmarks
3482 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3484 g_return_val_if_fail (bookmark != NULL, 0);
3486 return g_list_length (bookmark->items);
3490 * g_bookmark_file_move_item:
3491 * @bookmark: a #GBookmarkFile
3492 * @old_uri: a valid URI
3493 * @new_uri: (allow-none): a valid URI, or %NULL
3494 * @error: return location for a #GError or %NULL
3496 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3497 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3498 * %NULL, then the bookmark is removed.
3500 * In the event the URI cannot be found, %FALSE is returned and
3501 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3503 * Returns: %TRUE if the URI was successfully changed
3508 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3509 const gchar *old_uri,
3510 const gchar *new_uri,
3515 g_return_val_if_fail (bookmark != NULL, FALSE);
3516 g_return_val_if_fail (old_uri != NULL, FALSE);
3518 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3521 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3522 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3523 _("No bookmark found for URI '%s'"),
3528 if (new_uri && new_uri[0] != '\0')
3530 if (g_bookmark_file_has_item (bookmark, new_uri))
3532 if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3536 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3539 item->uri = g_strdup (new_uri);
3540 item->modified = time (NULL);
3542 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3548 if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
3556 * g_bookmark_file_set_icon:
3557 * @bookmark: a #GBookmarkFile
3559 * @href: (allow-none): the URI of the icon for the bookmark, or %NULL
3560 * @mime_type: the MIME type of the icon for the bookmark
3562 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3563 * the currently set icon. @href can either be a full URL for the icon
3564 * file or the icon name following the Icon Naming specification.
3566 * If no bookmark for @uri is found one is created.
3571 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3574 const gchar *mime_type)
3578 g_return_if_fail (bookmark != NULL);
3579 g_return_if_fail (uri != NULL);
3581 item = g_bookmark_file_lookup_item (bookmark, uri);
3584 item = bookmark_item_new (uri);
3585 g_bookmark_file_add_item (bookmark, item, NULL);
3588 if (!item->metadata)
3589 item->metadata = bookmark_metadata_new ();
3591 g_free (item->metadata->icon_href);
3592 g_free (item->metadata->icon_mime);
3594 item->metadata->icon_href = g_strdup (href);
3596 if (mime_type && mime_type[0] != '\0')
3597 item->metadata->icon_mime = g_strdup (mime_type);
3599 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3601 item->modified = time (NULL);
3605 * g_bookmark_file_get_icon:
3606 * @bookmark: a #GBookmarkFile
3608 * @href: (allow-none) (out): return location for the icon's location or %NULL
3609 * @mime_type: (allow-none) (out): return location for the icon's MIME type or %NULL
3610 * @error: return location for a #GError or %NULL
3612 * Gets the icon of the bookmark for @uri.
3614 * In the event the URI cannot be found, %FALSE is returned and
3615 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3617 * Returns: %TRUE if the icon for the bookmark for the URI was found.
3618 * You should free the returned strings.
3623 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3631 g_return_val_if_fail (bookmark != NULL, FALSE);
3632 g_return_val_if_fail (uri != NULL, FALSE);
3634 item = g_bookmark_file_lookup_item (bookmark, uri);
3637 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3638 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3639 _("No bookmark found for URI '%s'"),
3644 if ((!item->metadata) || (!item->metadata->icon_href))
3648 *href = g_strdup (item->metadata->icon_href);
3651 *mime_type = g_strdup (item->metadata->icon_mime);