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 * <ulink url="http://www.gnome.org/~ebassi/bookmark-spec">Desktop Bookmark
64 * Specification</ulink>.
66 * The syntax of the bookmark files is described in detail inside the Desktop
67 * Bookmark Specification, here is a quick summary: bookmark files use a
68 * sub-class of the <ulink url="">XML Bookmark Exchange Language</ulink>
69 * specification, consisting of valid UTF-8 encoded XML, under the
70 * <literal>xbel</literal> root element; each bookmark is stored inside a
71 * <literal>bookmark</literal> element, using its URI: no relative paths can
72 * be used inside a bookmark file. The bookmark may have a user defined title
73 * and description, to be used instead of the URI. Under the
74 * <literal>metadata</literal> element, with its <literal>owner</literal>
75 * attribute set to <literal>http://freedesktop.org</literal>, is stored the
76 * meta-data about a resource pointed by its URI. The meta-data consists of
77 * the resource's MIME type; the applications that have registered a bookmark;
78 * the groups to which a bookmark belongs to; a visibility flag, used to set
79 * the bookmark as "private" to the applications and groups that has it
80 * registered; the URI and MIME type of an icon, to be used when displaying
81 * the bookmark inside a GUI.
83 * <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../glib/tests/bookmarks.xbel"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>
86 * A bookmark file might contain more than one bookmark; each bookmark
87 * is accessed through its URI.
89 * The important caveat of bookmark files is that when you add a new
90 * bookmark you must also add the application that is registering it, using
91 * g_bookmark_file_add_application() or g_bookmark_file_set_app_info().
92 * If a bookmark has no applications then it won't be dumped when creating
93 * the on disk representation, using g_bookmark_file_to_data() or
94 * g_bookmark_file_to_file().
96 * The #GBookmarkFile parser was added in GLib 2.12.
99 /* XBEL 1.0 standard entities */
100 #define XBEL_VERSION "1.0"
101 #define XBEL_DTD_NICK "xbel"
102 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
103 "Exchange Language 1.0//EN//XML"
105 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
107 #define XBEL_ROOT_ELEMENT "xbel"
108 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
109 #define XBEL_BOOKMARK_ELEMENT "bookmark"
110 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
111 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
112 #define XBEL_TITLE_ELEMENT "title"
113 #define XBEL_DESC_ELEMENT "desc"
114 #define XBEL_INFO_ELEMENT "info"
115 #define XBEL_METADATA_ELEMENT "metadata"
117 #define XBEL_VERSION_ATTRIBUTE "version"
118 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
119 #define XBEL_OWNER_ATTRIBUTE "owner"
120 #define XBEL_ADDED_ATTRIBUTE "added"
121 #define XBEL_VISITED_ATTRIBUTE "visited"
122 #define XBEL_MODIFIED_ATTRIBUTE "modified"
123 #define XBEL_ID_ATTRIBUTE "id"
124 #define XBEL_HREF_ATTRIBUTE "href"
125 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
127 #define XBEL_YES_VALUE "yes"
128 #define XBEL_NO_VALUE "no"
130 /* Desktop bookmark spec entities */
131 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
133 #define BOOKMARK_NAMESPACE_NAME "bookmark"
134 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
136 #define BOOKMARK_GROUPS_ELEMENT "groups"
137 #define BOOKMARK_GROUP_ELEMENT "group"
138 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
139 #define BOOKMARK_APPLICATION_ELEMENT "application"
140 #define BOOKMARK_ICON_ELEMENT "icon"
141 #define BOOKMARK_PRIVATE_ELEMENT "private"
143 #define BOOKMARK_NAME_ATTRIBUTE "name"
144 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
145 #define BOOKMARK_COUNT_ATTRIBUTE "count"
146 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp" /* deprecated by "modified" */
147 #define BOOKMARK_MODIFIED_ATTRIBUTE "modified"
148 #define BOOKMARK_HREF_ATTRIBUTE "href"
149 #define BOOKMARK_TYPE_ATTRIBUTE "type"
151 /* Shared MIME Info entities */
152 #define MIME_NAMESPACE_NAME "mime"
153 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
154 #define MIME_TYPE_ELEMENT "mime-type"
155 #define MIME_TYPE_ATTRIBUTE "type"
158 typedef struct _BookmarkAppInfo BookmarkAppInfo;
159 typedef struct _BookmarkMetadata BookmarkMetadata;
160 typedef struct _BookmarkItem BookmarkItem;
161 typedef struct _ParseData ParseData;
163 struct _BookmarkAppInfo
173 struct _BookmarkMetadata
180 GHashTable *apps_by_name;
185 guint is_private : 1;
199 BookmarkMetadata *metadata;
202 struct _GBookmarkFile
207 /* we store our items in a list and keep a copy inside
208 * an hash table for faster lookup performances
211 GHashTable *items_by_uri;
214 /* parser state machine */
235 static void g_bookmark_file_init (GBookmarkFile *bookmark);
236 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
237 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
241 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
244 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
246 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
250 static time_t timestamp_from_iso8601 (const gchar *iso_date);
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.\n", 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);
778 item->added = timestamp_from_iso8601 (added);
781 item->modified = timestamp_from_iso8601 (modified);
784 item->visited = timestamp_from_iso8601 (visited);
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);
869 ai->exec = g_strdup (exec);
872 ai->count = atoi (count);
877 ai->stamp = timestamp_from_iso8601 (modified);
880 /* the timestamp attribute has been deprecated but we still parse
881 * it for backward compatibility
884 ai->stamp = (time_t) atol (stamp);
886 ai->stamp = time (NULL);
891 parse_mime_type_element (GMarkupParseContext *context,
892 ParseData *parse_data,
893 const gchar **attribute_names,
894 const gchar **attribute_values,
902 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
906 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
908 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
909 type = attribute_values[i];
913 type = "application/octet-stream";
915 g_warn_if_fail (parse_data->current_item != NULL);
916 item = parse_data->current_item;
919 item->metadata = bookmark_metadata_new ();
921 item->metadata->mime_type = g_strdup (type);
925 parse_icon_element (GMarkupParseContext *context,
926 ParseData *parse_data,
927 const gchar **attribute_names,
928 const gchar **attribute_values,
937 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
942 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
944 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
945 href = attribute_values[i];
946 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
947 type = attribute_values[i];
950 /* the "href" attribute is mandatory */
953 g_set_error (error, G_MARKUP_ERROR,
954 G_MARKUP_ERROR_INVALID_CONTENT,
955 _("Attribute '%s' of element '%s' not found"),
956 BOOKMARK_HREF_ATTRIBUTE,
957 BOOKMARK_ICON_ELEMENT);
962 type = "application/octet-stream";
964 g_warn_if_fail (parse_data->current_item != NULL);
965 item = parse_data->current_item;
968 item->metadata = bookmark_metadata_new ();
970 item->metadata->icon_href = g_strdup (href);
971 item->metadata->icon_mime = g_strdup (type);
974 /* scans through the attributes of an element for the "xmlns" pragma, and
975 * adds any resulting namespace declaration to a per-parser hashtable, using
976 * the namespace name as a key for the namespace URI; if no key was found,
977 * the namespace is considered as default, and stored under the "default" key.
979 * FIXME: this works on the assumption that the generator of the XBEL file
980 * is either this code or is smart enough to place the namespace declarations
981 * inside the main root node or inside the metadata node and does not redefine
982 * a namespace inside an inner node; this does *not* conform to the
983 * XML-NS standard, although is a close approximation. In order to make this
984 * conformant to the XML-NS specification we should use a per-element
985 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
988 map_namespace_to_name (ParseData *parse_data,
989 const gchar **attribute_names,
990 const gchar **attribute_values)
995 g_warn_if_fail (parse_data != NULL);
997 if (!attribute_names || !attribute_names[0])
1001 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1003 if (g_str_has_prefix (attr, "xmlns"))
1005 gchar *namespace_name, *namespace_uri;
1008 p = g_utf8_strchr (attr, -1, ':');
1010 p = g_utf8_next_char (p);
1014 namespace_name = g_strdup (p);
1015 namespace_uri = g_strdup (attribute_values[i]);
1017 g_hash_table_replace (parse_data->namespaces,
1024 /* checks whether @element_full is equal to @element.
1026 * if @namespace is set, it tries to resolve the namespace to a known URI,
1027 * and if found is prepended to the element name, from which is separated
1028 * using the character specified in the @sep parameter.
1031 is_element_full (ParseData *parse_data,
1032 const gchar *element_full,
1033 const gchar *namespace,
1034 const gchar *element,
1037 gchar *ns_uri, *ns_name;
1038 const gchar *p, *element_name;
1041 g_warn_if_fail (parse_data != NULL);
1042 g_warn_if_fail (element_full != NULL);
1047 /* no namespace requested: dumb element compare */
1049 return (0 == strcmp (element_full, element));
1051 /* search for namespace separator; if none found, assume we are under the
1052 * default namespace, and set ns_name to our "default" marker; if no default
1053 * namespace has been set, just do a plain comparison between @full_element
1056 p = g_utf8_strchr (element_full, -1, ':');
1059 ns_name = g_strndup (element_full, p - element_full);
1060 element_name = g_utf8_next_char (p);
1064 ns_name = g_strdup ("default");
1065 element_name = element_full;
1068 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1071 /* no default namespace found */
1074 return (0 == strcmp (element_full, element));
1077 retval = (0 == strcmp (ns_uri, namespace) &&
1078 0 == strcmp (element_name, element));
1085 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1086 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1089 start_element_raw_cb (GMarkupParseContext *context,
1090 const gchar *element_name,
1091 const gchar **attribute_names,
1092 const gchar **attribute_values,
1096 ParseData *parse_data = (ParseData *) user_data;
1098 /* we must check for namespace declarations first
1100 * XXX - we could speed up things by checking for namespace declarations
1101 * only on the root node, where they usually are; this would probably break
1102 * on streams not produced by us or by "smart" generators
1104 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1106 switch (parse_data->state)
1109 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1115 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1117 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1118 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1119 parse_data->state = STATE_ROOT;
1123 g_set_error (error, G_MARKUP_ERROR,
1124 G_MARKUP_ERROR_INVALID_CONTENT,
1125 _("Unexpected tag '%s', tag '%s' expected"),
1126 element_name, XBEL_ROOT_ELEMENT);
1129 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1130 parse_data->state = STATE_TITLE;
1131 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1132 parse_data->state = STATE_DESC;
1133 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1135 GError *inner_error = NULL;
1137 parse_data->state = STATE_BOOKMARK;
1139 parse_bookmark_element (context,
1145 g_propagate_error (error, inner_error);
1148 g_set_error (error, G_MARKUP_ERROR,
1149 G_MARKUP_ERROR_INVALID_CONTENT,
1150 _("Unexpected tag '%s' inside '%s'"),
1154 case STATE_BOOKMARK:
1155 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1156 parse_data->state = STATE_TITLE;
1157 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1158 parse_data->state = STATE_DESC;
1159 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1160 parse_data->state = STATE_INFO;
1162 g_set_error (error, G_MARKUP_ERROR,
1163 G_MARKUP_ERROR_INVALID_CONTENT,
1164 _("Unexpected tag '%s' inside '%s'"),
1166 XBEL_BOOKMARK_ELEMENT);
1169 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1175 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1177 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1178 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1180 parse_data->state = STATE_METADATA;
1182 if (!parse_data->current_item->metadata)
1183 parse_data->current_item->metadata = bookmark_metadata_new ();
1188 g_set_error (error, G_MARKUP_ERROR,
1189 G_MARKUP_ERROR_INVALID_CONTENT,
1190 _("Unexpected tag '%s', tag '%s' expected"),
1192 XBEL_METADATA_ELEMENT);
1194 case STATE_METADATA:
1195 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1196 parse_data->state = STATE_APPLICATIONS;
1197 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1198 parse_data->state = STATE_GROUPS;
1199 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1200 parse_data->current_item->metadata->is_private = TRUE;
1201 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1203 GError *inner_error = NULL;
1205 parse_data->state = STATE_ICON;
1207 parse_icon_element (context,
1213 g_propagate_error (error, inner_error);
1215 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1217 GError *inner_error = NULL;
1219 parse_data->state = STATE_MIME;
1221 parse_mime_type_element (context,
1227 g_propagate_error (error, inner_error);
1230 g_set_error (error, G_MARKUP_ERROR,
1231 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1232 _("Unexpected tag '%s' inside '%s'"),
1234 XBEL_METADATA_ELEMENT);
1236 case STATE_APPLICATIONS:
1237 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1239 GError *inner_error = NULL;
1241 parse_data->state = STATE_APPLICATION;
1243 parse_application_element (context,
1249 g_propagate_error (error, inner_error);
1252 g_set_error (error, G_MARKUP_ERROR,
1253 G_MARKUP_ERROR_INVALID_CONTENT,
1254 _("Unexpected tag '%s', tag '%s' expected"),
1256 BOOKMARK_APPLICATION_ELEMENT);
1259 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1260 parse_data->state = STATE_GROUP;
1262 g_set_error (error, G_MARKUP_ERROR,
1263 G_MARKUP_ERROR_INVALID_CONTENT,
1264 _("Unexpected tag '%s', tag '%s' expected"),
1266 BOOKMARK_GROUP_ELEMENT);
1269 g_warn_if_reached ();
1275 end_element_raw_cb (GMarkupParseContext *context,
1276 const gchar *element_name,
1280 ParseData *parse_data = (ParseData *) user_data;
1282 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1283 parse_data->state = STATE_FINISHED;
1284 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1286 parse_data->current_item = NULL;
1288 parse_data->state = STATE_ROOT;
1290 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1291 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1292 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1294 if (parse_data->current_item)
1295 parse_data->state = STATE_BOOKMARK;
1297 parse_data->state = STATE_ROOT;
1299 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1300 parse_data->state = STATE_INFO;
1301 else if (IS_ELEMENT_NS (parse_data, element_name,
1302 BOOKMARK_NAMESPACE_URI,
1303 BOOKMARK_APPLICATION_ELEMENT))
1304 parse_data->state = STATE_APPLICATIONS;
1305 else if (IS_ELEMENT_NS (parse_data, element_name,
1306 BOOKMARK_NAMESPACE_URI,
1307 BOOKMARK_GROUP_ELEMENT))
1308 parse_data->state = STATE_GROUPS;
1309 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1310 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1311 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1312 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1313 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1314 parse_data->state = STATE_METADATA;
1318 text_raw_cb (GMarkupParseContext *context,
1324 ParseData *parse_data = (ParseData *) user_data;
1327 payload = g_strndup (text, length);
1329 switch (parse_data->state)
1332 if (parse_data->current_item)
1334 g_free (parse_data->current_item->title);
1335 parse_data->current_item->title = g_strdup (payload);
1339 g_free (parse_data->bookmark_file->title);
1340 parse_data->bookmark_file->title = g_strdup (payload);
1344 if (parse_data->current_item)
1346 g_free (parse_data->current_item->description);
1347 parse_data->current_item->description = g_strdup (payload);
1351 g_free (parse_data->bookmark_file->description);
1352 parse_data->bookmark_file->description = g_strdup (payload);
1359 g_warn_if_fail (parse_data->current_item != NULL);
1361 if (!parse_data->current_item->metadata)
1362 parse_data->current_item->metadata = bookmark_metadata_new ();
1364 groups = parse_data->current_item->metadata->groups;
1365 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1369 case STATE_BOOKMARK:
1371 case STATE_METADATA:
1372 case STATE_APPLICATIONS:
1373 case STATE_APPLICATION:
1379 g_warn_if_reached ();
1386 static const GMarkupParser markup_parser =
1388 start_element_raw_cb, /* start_element */
1389 end_element_raw_cb, /* end_element */
1390 text_raw_cb, /* text */
1391 NULL, /* passthrough */
1396 g_bookmark_file_parse (GBookmarkFile *bookmark,
1397 const gchar *buffer,
1401 GMarkupParseContext *context;
1402 ParseData *parse_data;
1403 GError *parse_error, *end_error;
1406 g_warn_if_fail (bookmark != NULL);
1414 if (length == (gsize) -1)
1415 length = strlen (buffer);
1417 parse_data = parse_data_new ();
1418 parse_data->bookmark_file = bookmark;
1420 context = g_markup_parse_context_new (&markup_parser,
1423 (GDestroyNotify) parse_data_free);
1425 retval = g_markup_parse_context_parse (context,
1430 g_propagate_error (error, parse_error);
1433 retval = g_markup_parse_context_end_parse (context, &end_error);
1435 g_propagate_error (error, end_error);
1438 g_markup_parse_context_free (context);
1444 g_bookmark_file_dump (GBookmarkFile *bookmark,
1452 retval = g_string_sized_new (4096);
1454 g_string_append (retval,
1455 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1457 /* XXX - do we really need the doctype? */
1458 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1459 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1460 " \"" XBEL_DTD_URI "\">\n"
1462 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1463 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1464 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1466 if (bookmark->title)
1468 gchar *escaped_title;
1470 escaped_title = g_markup_escape_text (bookmark->title, -1);
1472 buffer = g_strconcat (" "
1473 "<" XBEL_TITLE_ELEMENT ">",
1475 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1477 g_string_append (retval, buffer);
1480 g_free (escaped_title);
1483 if (bookmark->description)
1485 gchar *escaped_desc;
1487 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1489 buffer = g_strconcat (" "
1490 "<" XBEL_DESC_ELEMENT ">",
1492 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1493 g_string_append (retval, buffer);
1496 g_free (escaped_desc);
1499 if (!bookmark->items)
1502 retval = g_string_append (retval, "\n");
1504 /* the items are stored in reverse order */
1505 for (l = g_list_last (bookmark->items);
1509 BookmarkItem *item = (BookmarkItem *) l->data;
1512 item_dump = bookmark_item_dump (item);
1516 retval = g_string_append (retval, item_dump);
1522 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1525 *length = retval->len;
1527 return g_string_free (retval, FALSE);
1534 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1535 * should free the returned string.
1538 timestamp_to_iso8601 (time_t timestamp)
1542 if (timestamp == (time_t) -1)
1543 g_get_current_time (&stamp);
1546 stamp.tv_sec = timestamp;
1550 return g_time_val_to_iso8601 (&stamp);
1554 timestamp_from_iso8601 (const gchar *iso_date)
1558 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1561 return (time_t) stamp.tv_sec;
1564 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1566 /********************
1568 ********************/
1571 * g_bookmark_file_new:
1573 * Creates a new empty #GBookmarkFile object.
1575 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1576 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1579 * Return value: an empty #GBookmarkFile
1584 g_bookmark_file_new (void)
1586 GBookmarkFile *bookmark;
1588 bookmark = g_new (GBookmarkFile, 1);
1590 g_bookmark_file_init (bookmark);
1596 * g_bookmark_file_free:
1597 * @bookmark: a #GBookmarkFile
1599 * Frees a #GBookmarkFile.
1604 g_bookmark_file_free (GBookmarkFile *bookmark)
1609 g_bookmark_file_clear (bookmark);
1615 * g_bookmark_file_load_from_data:
1616 * @bookmark: an empty #GBookmarkFile struct
1617 * @data: desktop bookmarks loaded in memory
1618 * @length: the length of @data in bytes
1619 * @error: return location for a #GError, or %NULL
1621 * Loads a bookmark file from memory into an empty #GBookmarkFile
1622 * structure. If the object cannot be created then @error is set to a
1623 * #GBookmarkFileError.
1625 * Return value: %TRUE if a desktop bookmark could be loaded.
1630 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1635 GError *parse_error;
1638 g_return_val_if_fail (bookmark != NULL, FALSE);
1640 if (length == (gsize) -1)
1641 length = strlen (data);
1643 if (bookmark->items)
1645 g_bookmark_file_clear (bookmark);
1646 g_bookmark_file_init (bookmark);
1650 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1653 g_propagate_error (error, parse_error);
1659 * g_bookmark_file_load_from_file:
1660 * @bookmark: an empty #GBookmarkFile struct
1661 * @filename: the path of a filename to load, in the 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 * Return value: %TRUE if a desktop bookmark file could be loaded
1673 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1674 const gchar *filename,
1682 g_return_val_if_fail (bookmark != NULL, FALSE);
1683 g_return_val_if_fail (filename != NULL, FALSE);
1686 g_file_get_contents (filename, &buffer, &len, &read_error);
1689 g_propagate_error (error, read_error);
1695 retval = g_bookmark_file_load_from_data (bookmark,
1701 g_propagate_error (error, read_error);
1714 /* Iterates through all the directories in *dirs trying to
1715 * find file. When it successfully locates file, returns a
1716 * string its absolute path. It also leaves the unchecked
1717 * directories in *dirs. You should free the returned string
1719 * Adapted from gkeyfile.c
1722 find_file_in_data_dirs (const gchar *file,
1726 gchar **data_dirs, *data_dir, *path;
1735 while (data_dirs && (data_dir = *data_dirs) && !path)
1737 gchar *candidate_file, *sub_dir;
1739 candidate_file = (gchar *) file;
1740 sub_dir = g_strdup ("");
1741 while (candidate_file != NULL && !path)
1745 path = g_build_filename (data_dir, sub_dir,
1746 candidate_file, NULL);
1748 candidate_file = strchr (candidate_file, '-');
1750 if (candidate_file == NULL)
1756 sub_dir = g_strndup (file, candidate_file - file - 1);
1758 for (p = sub_dir; *p != '\0'; p++)
1761 *p = G_DIR_SEPARATOR;
1772 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1773 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1774 _("No valid bookmark file found in data dirs"));
1784 * g_bookmark_file_load_from_data_dirs:
1785 * @bookmark: a #GBookmarkFile
1786 * @file: a relative path to a filename to open and parse
1787 * @full_path: (allow-none): return location for a string containing the full path
1788 * of the file, or %NULL
1789 * @error: return location for a #GError, or %NULL
1791 * This function looks for a desktop bookmark file named @file in the
1792 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1793 * loads the file into @bookmark and returns the file's full path in
1794 * @full_path. If the file could not be loaded then an %error is
1795 * set to either a #GFileError or #GBookmarkFileError.
1797 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
1802 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1807 GError *file_error = NULL;
1808 gchar **all_data_dirs, **data_dirs;
1809 const gchar *user_data_dir;
1810 const gchar * const * system_data_dirs;
1813 gboolean found_file;
1815 g_return_val_if_fail (bookmark != NULL, FALSE);
1816 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1818 user_data_dir = g_get_user_data_dir ();
1819 system_data_dirs = g_get_system_data_dirs ();
1820 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1823 all_data_dirs[i++] = g_strdup (user_data_dir);
1826 while (system_data_dirs[j] != NULL)
1827 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1830 data_dirs = all_data_dirs;
1832 while (*data_dirs != NULL && !found_file)
1834 g_free (output_path);
1836 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1840 g_propagate_error (error, file_error);
1844 found_file = g_bookmark_file_load_from_file (bookmark,
1849 g_propagate_error (error, file_error);
1854 if (found_file && full_path)
1855 *full_path = output_path;
1857 g_free (output_path);
1859 g_strfreev (all_data_dirs);
1866 * g_bookmark_file_to_data:
1867 * @bookmark: a #GBookmarkFile
1868 * @length: (allow-none) (out): return location for the length of the returned string, or %NULL
1869 * @error: return location for a #GError, or %NULL
1871 * This function outputs @bookmark as a string.
1873 * Return value: a newly allocated string holding
1874 * the contents of the #GBookmarkFile
1879 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1883 GError *write_error = NULL;
1886 g_return_val_if_fail (bookmark != NULL, NULL);
1888 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1891 g_propagate_error (error, write_error);
1900 * g_bookmark_file_to_file:
1901 * @bookmark: a #GBookmarkFile
1902 * @filename: path of the output file
1903 * @error: return location for a #GError, or %NULL
1905 * This function outputs @bookmark into a file. The write process is
1906 * guaranteed to be atomic by using g_file_set_contents() internally.
1908 * Return value: %TRUE if the file was successfully written.
1913 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1914 const gchar *filename,
1918 GError *data_error, *write_error;
1922 g_return_val_if_fail (bookmark != NULL, FALSE);
1923 g_return_val_if_fail (filename != NULL, FALSE);
1926 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1929 g_propagate_error (error, data_error);
1935 g_file_set_contents (filename, data, len, &write_error);
1938 g_propagate_error (error, write_error);
1950 static BookmarkItem *
1951 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1954 g_warn_if_fail (bookmark != NULL && uri != NULL);
1956 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1959 /* this function adds a new item to the list */
1961 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1965 g_warn_if_fail (bookmark != NULL);
1966 g_warn_if_fail (item != NULL);
1968 /* this should never happen; and if it does, then we are
1969 * screwing up something big time.
1971 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1973 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1974 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1975 _("A bookmark for URI '%s' already exists"),
1980 bookmark->items = g_list_prepend (bookmark->items, item);
1982 g_hash_table_replace (bookmark->items_by_uri,
1986 if (item->added == (time_t) -1)
1987 item->added = time (NULL);
1989 if (item->modified == (time_t) -1)
1990 item->modified = time (NULL);
1994 * g_bookmark_file_remove_item:
1995 * @bookmark: a #GBookmarkFile
1997 * @error: return location for a #GError, or %NULL
1999 * Removes the bookmark for @uri from the bookmark file @bookmark.
2001 * Return value: %TRUE if the bookmark was removed successfully.
2006 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2012 g_return_val_if_fail (bookmark != NULL, FALSE);
2013 g_return_val_if_fail (uri != NULL, FALSE);
2015 item = g_bookmark_file_lookup_item (bookmark, uri);
2019 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2020 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2021 _("No bookmark found for URI '%s'"),
2026 bookmark->items = g_list_remove (bookmark->items, item);
2027 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2029 bookmark_item_free (item);
2035 * g_bookmark_file_has_item:
2036 * @bookmark: a #GBookmarkFile
2039 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2041 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2046 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2049 g_return_val_if_fail (bookmark != NULL, FALSE);
2050 g_return_val_if_fail (uri != NULL, FALSE);
2052 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2056 * g_bookmark_file_get_uris:
2057 * @bookmark: a #GBookmarkFile
2058 * @length: (allow-none) (out): return location for the number of returned URIs, or %NULL
2060 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2061 * The array of returned URIs will be %NULL-terminated, so @length may
2062 * optionally be %NULL.
2064 * Return value: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2065 * Use g_strfreev() to free it.
2070 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2077 g_return_val_if_fail (bookmark != NULL, NULL);
2079 n_items = g_list_length (bookmark->items);
2080 uris = g_new0 (gchar *, n_items + 1);
2082 /* the items are stored in reverse order, so we walk the list backward */
2083 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2085 BookmarkItem *item = (BookmarkItem *) l->data;
2087 g_warn_if_fail (item != NULL);
2089 uris[i++] = g_strdup (item->uri);
2100 * g_bookmark_file_set_title:
2101 * @bookmark: a #GBookmarkFile
2102 * @uri: (allow-none): a valid URI or %NULL
2103 * @title: a UTF-8 encoded string
2105 * Sets @title as the title of the bookmark for @uri inside the
2106 * bookmark file @bookmark.
2108 * If @uri is %NULL, the title of @bookmark is set.
2110 * If a bookmark for @uri cannot be found then it is created.
2115 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2119 g_return_if_fail (bookmark != NULL);
2123 g_free (bookmark->title);
2124 bookmark->title = g_strdup (title);
2130 item = g_bookmark_file_lookup_item (bookmark, uri);
2133 item = bookmark_item_new (uri);
2134 g_bookmark_file_add_item (bookmark, item, NULL);
2137 g_free (item->title);
2138 item->title = g_strdup (title);
2140 item->modified = time (NULL);
2145 * g_bookmark_file_get_title:
2146 * @bookmark: a #GBookmarkFile
2147 * @uri: (allow-none): a valid URI or %NULL
2148 * @error: return location for a #GError, or %NULL
2150 * Returns the title of the bookmark for @uri.
2152 * If @uri is %NULL, the title of @bookmark is returned.
2154 * In the event the URI cannot be found, %NULL is returned and
2155 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2157 * Return value: a newly allocated string or %NULL if the specified
2158 * URI cannot be found.
2163 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2169 g_return_val_if_fail (bookmark != NULL, NULL);
2172 return g_strdup (bookmark->title);
2174 item = g_bookmark_file_lookup_item (bookmark, uri);
2177 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2178 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2179 _("No bookmark found for URI '%s'"),
2184 return g_strdup (item->title);
2188 * g_bookmark_file_set_description:
2189 * @bookmark: a #GBookmarkFile
2190 * @uri: (allow-none): a valid URI or %NULL
2191 * @description: a string
2193 * Sets @description as the description of the bookmark for @uri.
2195 * If @uri is %NULL, the description of @bookmark is set.
2197 * If a bookmark for @uri cannot be found then it is created.
2202 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2204 const gchar *description)
2206 g_return_if_fail (bookmark != NULL);
2210 g_free (bookmark->description);
2211 bookmark->description = g_strdup (description);
2217 item = g_bookmark_file_lookup_item (bookmark, uri);
2220 item = bookmark_item_new (uri);
2221 g_bookmark_file_add_item (bookmark, item, NULL);
2224 g_free (item->description);
2225 item->description = g_strdup (description);
2227 item->modified = time (NULL);
2232 * g_bookmark_file_get_description:
2233 * @bookmark: a #GBookmarkFile
2235 * @error: return location for a #GError, or %NULL
2237 * Retrieves the description of the bookmark for @uri.
2239 * In the event the URI cannot be found, %NULL is returned and
2240 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2242 * Return value: a newly allocated string or %NULL if the specified
2243 * URI cannot be found.
2248 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2254 g_return_val_if_fail (bookmark != NULL, NULL);
2257 return g_strdup (bookmark->description);
2259 item = g_bookmark_file_lookup_item (bookmark, uri);
2262 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2263 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2264 _("No bookmark found for URI '%s'"),
2269 return g_strdup (item->description);
2273 * g_bookmark_file_set_mime_type:
2274 * @bookmark: a #GBookmarkFile
2276 * @mime_type: a MIME type
2278 * Sets @mime_type as the MIME type of the bookmark for @uri.
2280 * If a bookmark for @uri cannot be found then it is created.
2285 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2287 const gchar *mime_type)
2291 g_return_if_fail (bookmark != NULL);
2292 g_return_if_fail (uri != NULL);
2293 g_return_if_fail (mime_type != NULL);
2295 item = g_bookmark_file_lookup_item (bookmark, uri);
2298 item = bookmark_item_new (uri);
2299 g_bookmark_file_add_item (bookmark, item, NULL);
2302 if (!item->metadata)
2303 item->metadata = bookmark_metadata_new ();
2305 g_free (item->metadata->mime_type);
2307 item->metadata->mime_type = g_strdup (mime_type);
2308 item->modified = time (NULL);
2312 * g_bookmark_file_get_mime_type:
2313 * @bookmark: a #GBookmarkFile
2315 * @error: return location for a #GError, or %NULL
2317 * Retrieves the MIME type of the resource pointed by @uri.
2319 * In the event the URI cannot be found, %NULL is returned and
2320 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2321 * event that the MIME type cannot be found, %NULL is returned and
2322 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2324 * Return value: a newly allocated string or %NULL if the specified
2325 * URI cannot be found.
2330 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2336 g_return_val_if_fail (bookmark != NULL, NULL);
2337 g_return_val_if_fail (uri != NULL, NULL);
2339 item = g_bookmark_file_lookup_item (bookmark, uri);
2342 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2343 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2344 _("No bookmark found for URI '%s'"),
2349 if (!item->metadata)
2351 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2352 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2353 _("No MIME type defined in the bookmark for URI '%s'"),
2358 return g_strdup (item->metadata->mime_type);
2362 * g_bookmark_file_set_is_private:
2363 * @bookmark: a #GBookmarkFile
2365 * @is_private: %TRUE if the bookmark should be marked as private
2367 * Sets the private flag of the bookmark for @uri.
2369 * If a bookmark for @uri cannot be found then it is created.
2374 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2376 gboolean is_private)
2380 g_return_if_fail (bookmark != NULL);
2381 g_return_if_fail (uri != NULL);
2383 item = g_bookmark_file_lookup_item (bookmark, uri);
2386 item = bookmark_item_new (uri);
2387 g_bookmark_file_add_item (bookmark, item, NULL);
2390 if (!item->metadata)
2391 item->metadata = bookmark_metadata_new ();
2393 item->metadata->is_private = (is_private == TRUE);
2394 item->modified = time (NULL);
2398 * g_bookmark_file_get_is_private:
2399 * @bookmark: a #GBookmarkFile
2401 * @error: return location for a #GError, or %NULL
2403 * Gets whether the private flag of the bookmark for @uri is set.
2405 * In the event the URI cannot be found, %FALSE is returned and
2406 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2407 * event that the private flag cannot be found, %FALSE is returned and
2408 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2410 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2415 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2421 g_return_val_if_fail (bookmark != NULL, FALSE);
2422 g_return_val_if_fail (uri != NULL, FALSE);
2424 item = g_bookmark_file_lookup_item (bookmark, uri);
2427 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2428 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2429 _("No bookmark found for URI '%s'"),
2434 if (!item->metadata)
2436 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2437 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2438 _("No private flag has been defined in bookmark for URI '%s'"),
2443 return item->metadata->is_private;
2447 * g_bookmark_file_set_added:
2448 * @bookmark: a #GBookmarkFile
2450 * @added: a timestamp or -1 to use the current time
2452 * Sets the time the bookmark for @uri was added into @bookmark.
2454 * If no bookmark for @uri is found then it is created.
2459 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2465 g_return_if_fail (bookmark != NULL);
2466 g_return_if_fail (uri != NULL);
2468 item = g_bookmark_file_lookup_item (bookmark, uri);
2471 item = bookmark_item_new (uri);
2472 g_bookmark_file_add_item (bookmark, item, NULL);
2475 if (added == (time_t) -1)
2478 item->added = added;
2479 item->modified = added;
2483 * g_bookmark_file_get_added:
2484 * @bookmark: a #GBookmarkFile
2486 * @error: return location for a #GError, or %NULL
2488 * Gets the time the bookmark for @uri was added to @bookmark
2490 * In the event the URI cannot be found, -1 is returned and
2491 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2493 * Return value: a timestamp
2498 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2504 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2505 g_return_val_if_fail (uri != NULL, (time_t) -1);
2507 item = g_bookmark_file_lookup_item (bookmark, uri);
2510 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2511 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2512 _("No bookmark found for URI '%s'"),
2521 * g_bookmark_file_set_modified:
2522 * @bookmark: a #GBookmarkFile
2524 * @modified: a timestamp or -1 to use the current time
2526 * Sets the last time the bookmark for @uri was last modified.
2528 * If no bookmark for @uri is found then it is created.
2530 * The "modified" time should only be set when the bookmark's meta-data
2531 * was actually changed. Every function of #GBookmarkFile that
2532 * modifies a bookmark also changes the modification time, except for
2533 * g_bookmark_file_set_visited().
2538 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2544 g_return_if_fail (bookmark != NULL);
2545 g_return_if_fail (uri != NULL);
2547 item = g_bookmark_file_lookup_item (bookmark, uri);
2550 item = bookmark_item_new (uri);
2551 g_bookmark_file_add_item (bookmark, item, NULL);
2554 if (modified == (time_t) -1)
2557 item->modified = modified;
2561 * g_bookmark_file_get_modified:
2562 * @bookmark: a #GBookmarkFile
2564 * @error: return location for a #GError, or %NULL
2566 * Gets the time when the bookmark for @uri was last modified.
2568 * In the event the URI cannot be found, -1 is returned and
2569 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2571 * Return value: a timestamp
2576 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2582 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2583 g_return_val_if_fail (uri != NULL, (time_t) -1);
2585 item = g_bookmark_file_lookup_item (bookmark, uri);
2588 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2589 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2590 _("No bookmark found for URI '%s'"),
2595 return item->modified;
2599 * g_bookmark_file_set_visited:
2600 * @bookmark: a #GBookmarkFile
2602 * @visited: a timestamp or -1 to use the current time
2604 * Sets the time the bookmark for @uri was last visited.
2606 * If no bookmark for @uri is found then it is created.
2608 * The "visited" time should only be set if the bookmark was launched,
2609 * either using the command line retrieved by g_bookmark_file_get_app_info()
2610 * or by the default application for the bookmark's MIME type, retrieved
2611 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2612 * does not affect the "modified" time.
2617 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2623 g_return_if_fail (bookmark != NULL);
2624 g_return_if_fail (uri != NULL);
2626 item = g_bookmark_file_lookup_item (bookmark, uri);
2629 item = bookmark_item_new (uri);
2630 g_bookmark_file_add_item (bookmark, item, NULL);
2633 if (visited == (time_t) -1)
2636 item->visited = visited;
2640 * g_bookmark_file_get_visited:
2641 * @bookmark: a #GBookmarkFile
2643 * @error: return location for a #GError, or %NULL
2645 * Gets the time the bookmark for @uri was last visited.
2647 * In the event the URI cannot be found, -1 is returned and
2648 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2650 * Return value: a timestamp.
2655 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2661 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2662 g_return_val_if_fail (uri != NULL, (time_t) -1);
2664 item = g_bookmark_file_lookup_item (bookmark, uri);
2667 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2668 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2669 _("No bookmark found for URI '%s'"),
2674 return item->visited;
2678 * g_bookmark_file_has_group:
2679 * @bookmark: a #GBookmarkFile
2681 * @group: the group name to be searched
2682 * @error: return location for a #GError, or %NULL
2684 * Checks whether @group appears in the list of groups to which
2685 * the bookmark for @uri belongs to.
2687 * In the event the URI cannot be found, %FALSE is returned and
2688 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2690 * Return value: %TRUE if @group was found.
2695 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2703 g_return_val_if_fail (bookmark != NULL, FALSE);
2704 g_return_val_if_fail (uri != NULL, FALSE);
2706 item = g_bookmark_file_lookup_item (bookmark, uri);
2709 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2710 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2711 _("No bookmark found for URI '%s'"),
2716 if (!item->metadata)
2719 for (l = item->metadata->groups; l != NULL; l = l->next)
2721 if (strcmp (l->data, group) == 0)
2730 * g_bookmark_file_add_group:
2731 * @bookmark: a #GBookmarkFile
2733 * @group: the group name to be added
2735 * Adds @group to the list of groups to which the bookmark for @uri
2738 * If no bookmark for @uri is found then it is created.
2743 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2749 g_return_if_fail (bookmark != NULL);
2750 g_return_if_fail (uri != NULL);
2751 g_return_if_fail (group != NULL && group[0] != '\0');
2753 item = g_bookmark_file_lookup_item (bookmark, uri);
2756 item = bookmark_item_new (uri);
2757 g_bookmark_file_add_item (bookmark, item, NULL);
2760 if (!item->metadata)
2761 item->metadata = bookmark_metadata_new ();
2763 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2765 item->metadata->groups = g_list_prepend (item->metadata->groups,
2768 item->modified = time (NULL);
2773 * g_bookmark_file_remove_group:
2774 * @bookmark: a #GBookmarkFile
2776 * @group: the group name to be removed
2777 * @error: return location for a #GError, or %NULL
2779 * Removes @group from the list of groups to which the bookmark
2780 * for @uri belongs to.
2782 * In the event the URI cannot be found, %FALSE is returned and
2783 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2784 * In the event no group was defined, %FALSE is returned and
2785 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2787 * Return value: %TRUE if @group was successfully removed.
2792 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2800 g_return_val_if_fail (bookmark != NULL, FALSE);
2801 g_return_val_if_fail (uri != NULL, FALSE);
2803 item = g_bookmark_file_lookup_item (bookmark, uri);
2806 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2807 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2808 _("No bookmark found for URI '%s'"),
2813 if (!item->metadata)
2815 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2816 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2817 _("No groups set in bookmark for URI '%s'"),
2822 for (l = item->metadata->groups; l != NULL; l = l->next)
2824 if (strcmp (l->data, group) == 0)
2826 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2830 item->modified = time (NULL);
2840 * g_bookmark_file_set_groups:
2841 * @bookmark: a #GBookmarkFile
2842 * @uri: an item's URI
2843 * @groups: (allow-none): an array of group names, or %NULL to remove all groups
2844 * @length: number of group name values in @groups
2846 * Sets a list of group names for the item with URI @uri. Each previously
2847 * set group name list is removed.
2849 * If @uri cannot be found then an item for it is created.
2854 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2856 const gchar **groups,
2862 g_return_if_fail (bookmark != NULL);
2863 g_return_if_fail (uri != NULL);
2864 g_return_if_fail (groups != NULL);
2866 item = g_bookmark_file_lookup_item (bookmark, uri);
2869 item = bookmark_item_new (uri);
2870 g_bookmark_file_add_item (bookmark, item, NULL);
2873 if (!item->metadata)
2874 item->metadata = bookmark_metadata_new ();
2876 g_list_free_full (item->metadata->groups, g_free);
2877 item->metadata->groups = NULL;
2881 for (i = 0; groups[i] != NULL && i < length; i++)
2882 item->metadata->groups = g_list_append (item->metadata->groups,
2883 g_strdup (groups[i]));
2886 item->modified = time (NULL);
2890 * g_bookmark_file_get_groups:
2891 * @bookmark: a #GBookmarkFile
2893 * @length: (allow-none) (out): return location for the length of the returned string, or %NULL
2894 * @error: return location for a #GError, or %NULL
2896 * Retrieves the list of group names of the bookmark for @uri.
2898 * In the event the URI cannot be found, %NULL is returned and
2899 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2901 * The returned array is %NULL terminated, so @length may optionally
2904 * Return value: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
2905 * Use g_strfreev() to free it.
2910 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2920 g_return_val_if_fail (bookmark != NULL, NULL);
2921 g_return_val_if_fail (uri != NULL, NULL);
2923 item = g_bookmark_file_lookup_item (bookmark, uri);
2926 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2927 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2928 _("No bookmark found for URI '%s'"),
2933 if (!item->metadata)
2941 len = g_list_length (item->metadata->groups);
2942 retval = g_new0 (gchar *, len + 1);
2943 for (l = g_list_last (item->metadata->groups), i = 0;
2947 gchar *group_name = (gchar *) l->data;
2949 g_warn_if_fail (group_name != NULL);
2951 retval[i++] = g_strdup (group_name);
2962 * g_bookmark_file_add_application:
2963 * @bookmark: a #GBookmarkFile
2965 * @name: (allow-none): the name of the application registering the bookmark
2967 * @exec: (allow-none): command line to be used to launch the bookmark or %NULL
2969 * Adds the application with @name and @exec to the list of
2970 * applications that have registered a bookmark for @uri into
2973 * Every bookmark inside a #GBookmarkFile must have at least an
2974 * application registered. Each application must provide a name, a
2975 * command line useful for launching the bookmark, the number of times
2976 * the bookmark has been registered by the application and the last
2977 * time the application registered this bookmark.
2979 * If @name is %NULL, the name of the application will be the
2980 * same returned by g_get_application_name(); if @exec is %NULL, the
2981 * command line will be a composition of the program name as
2982 * returned by g_get_prgname() and the "\%u" modifier, which will be
2983 * expanded to the bookmark's URI.
2985 * This function will automatically take care of updating the
2986 * registrations count and timestamping in case an application
2987 * with the same @name had already registered a bookmark for
2988 * @uri inside @bookmark.
2990 * If no bookmark for @uri is found, one is created.
2995 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3001 gchar *app_name, *app_exec;
3003 g_return_if_fail (bookmark != NULL);
3004 g_return_if_fail (uri != NULL);
3006 item = g_bookmark_file_lookup_item (bookmark, uri);
3009 item = bookmark_item_new (uri);
3010 g_bookmark_file_add_item (bookmark, item, NULL);
3013 if (name && name[0] != '\0')
3014 app_name = g_strdup (name);
3016 app_name = g_strdup (g_get_application_name ());
3018 if (exec && exec[0] != '\0')
3019 app_exec = g_strdup (exec);
3021 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3023 g_bookmark_file_set_app_info (bookmark, uri,
3035 * g_bookmark_file_remove_application:
3036 * @bookmark: a #GBookmarkFile
3038 * @name: the name of the application
3039 * @error: return location for a #GError or %NULL
3041 * Removes application registered with @name from the list of applications
3042 * that have registered a bookmark for @uri inside @bookmark.
3044 * In the event the URI cannot be found, %FALSE is returned and
3045 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3046 * In the event that no application with name @app_name has registered
3047 * a bookmark for @uri, %FALSE is returned and error is set to
3048 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3050 * Return value: %TRUE if the application was successfully removed.
3055 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3063 g_return_val_if_fail (bookmark != NULL, FALSE);
3064 g_return_val_if_fail (uri != NULL, FALSE);
3065 g_return_val_if_fail (name != NULL, FALSE);
3068 retval = g_bookmark_file_set_app_info (bookmark, uri,
3076 g_propagate_error (error, set_error);
3085 * g_bookmark_file_has_application:
3086 * @bookmark: a #GBookmarkFile
3088 * @name: the name of the application
3089 * @error: return location for a #GError or %NULL
3091 * Checks whether the bookmark for @uri inside @bookmark has been
3092 * registered by application @name.
3094 * In the event the URI cannot be found, %FALSE is returned and
3095 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3097 * Return value: %TRUE if the application @name was found
3102 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3109 g_return_val_if_fail (bookmark != NULL, FALSE);
3110 g_return_val_if_fail (uri != NULL, FALSE);
3111 g_return_val_if_fail (name != NULL, FALSE);
3113 item = g_bookmark_file_lookup_item (bookmark, uri);
3116 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3117 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3118 _("No bookmark found for URI '%s'"),
3123 return (NULL != bookmark_item_lookup_app_info (item, name));
3127 * g_bookmark_file_set_app_info:
3128 * @bookmark: a #GBookmarkFile
3130 * @name: an application's name
3131 * @exec: an application's command line
3132 * @count: the number of registrations done for this application
3133 * @stamp: the time of the last registration for this application
3134 * @error: return location for a #GError or %NULL
3136 * Sets the meta-data of application @name inside the list of
3137 * applications that have registered a bookmark for @uri inside
3140 * You should rarely use this function; use g_bookmark_file_add_application()
3141 * and g_bookmark_file_remove_application() instead.
3143 * @name can be any UTF-8 encoded string used to identify an
3145 * @exec can have one of these two modifiers: "\%f", which will
3146 * be expanded as the local file name retrieved from the bookmark's
3147 * URI; "\%u", which will be expanded as the bookmark's URI.
3148 * The expansion is done automatically when retrieving the stored
3149 * command line using the g_bookmark_file_get_app_info() function.
3150 * @count is the number of times the application has registered the
3151 * bookmark; if is < 0, the current registration count will be increased
3152 * by one, if is 0, the application with @name will be removed from
3153 * the list of registered applications.
3154 * @stamp is the Unix time of the last registration; if it is -1, the
3155 * current time will be used.
3157 * If you try to remove an application by setting its registration count to
3158 * zero, and no bookmark for @uri is found, %FALSE is returned and
3159 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3160 * in the event that no application @name has registered a bookmark
3161 * for @uri, %FALSE is returned and error is set to
3162 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3163 * for @uri is found, one is created.
3165 * Return value: %TRUE if the application's meta-data was successfully
3171 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3180 BookmarkAppInfo *ai;
3182 g_return_val_if_fail (bookmark != NULL, FALSE);
3183 g_return_val_if_fail (uri != NULL, FALSE);
3184 g_return_val_if_fail (name != NULL, FALSE);
3185 g_return_val_if_fail (exec != NULL, FALSE);
3187 item = g_bookmark_file_lookup_item (bookmark, uri);
3192 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3193 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3194 _("No bookmark found for URI '%s'"),
3200 item = bookmark_item_new (uri);
3201 g_bookmark_file_add_item (bookmark, item, NULL);
3205 if (!item->metadata)
3206 item->metadata = bookmark_metadata_new ();
3208 ai = bookmark_item_lookup_app_info (item, name);
3213 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3214 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3215 _("No application with name '%s' registered a bookmark for '%s'"),
3222 ai = bookmark_app_info_new (name);
3224 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3225 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3231 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3232 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3233 bookmark_app_info_free (ai);
3235 item->modified = time (NULL);
3244 if (stamp != (time_t) -1)
3247 ai->stamp = time (NULL);
3249 if (exec && exec[0] != '\0')
3252 ai->exec = g_shell_quote (exec);
3255 item->modified = time (NULL);
3260 /* expands the application's command line */
3262 expand_exec_line (const gchar *exec_fmt,
3268 exec = g_string_sized_new (512);
3269 while ((ch = *exec_fmt++) != '\0')
3273 exec = g_string_append_c (exec, ch);
3284 g_string_append (exec, uri);
3289 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3292 g_string_append (exec, file);
3297 g_string_free (exec, TRUE);
3304 exec = g_string_append_c (exec, ch);
3310 return g_string_free (exec, FALSE);
3314 * g_bookmark_file_get_app_info:
3315 * @bookmark: a #GBookmarkFile
3317 * @name: an application's name
3318 * @exec: (allow-none) (out): return location for the command line of the application, or %NULL
3319 * @count: (allow-none) (out): return location for the registration count, or %NULL
3320 * @stamp: (allow-none) (out): return location for the last registration time, or %NULL
3321 * @error: return location for a #GError, or %NULL
3323 * Gets the registration informations of @app_name for the bookmark for
3324 * @uri. See g_bookmark_file_set_app_info() for more informations about
3325 * the returned data.
3327 * The string returned in @app_exec must be freed.
3329 * In the event the URI cannot be found, %FALSE is returned and
3330 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3331 * event that no application with name @app_name has registered a bookmark
3332 * for @uri, %FALSE is returned and error is set to
3333 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3334 * the command line fails, an error of the #G_SHELL_ERROR domain is
3335 * set and %FALSE is returned.
3337 * Return value: %TRUE on success.
3342 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3351 BookmarkAppInfo *ai;
3353 g_return_val_if_fail (bookmark != NULL, FALSE);
3354 g_return_val_if_fail (uri != NULL, FALSE);
3355 g_return_val_if_fail (name != NULL, FALSE);
3357 item = g_bookmark_file_lookup_item (bookmark, uri);
3360 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3361 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3362 _("No bookmark found for URI '%s'"),
3367 ai = bookmark_item_lookup_app_info (item, name);
3370 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3371 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3372 _("No application with name '%s' registered a bookmark for '%s'"),
3380 GError *unquote_error = NULL;
3381 gchar *command_line;
3383 command_line = g_shell_unquote (ai->exec, &unquote_error);
3386 g_propagate_error (error, unquote_error);
3390 *exec = expand_exec_line (command_line, uri);
3393 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3394 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3395 _("Failed to expand exec line '%s' with URI '%s'"),
3397 g_free (command_line);
3402 g_free (command_line);
3415 * g_bookmark_file_get_applications:
3416 * @bookmark: a #GBookmarkFile
3418 * @length: (allow-none) (out): return location of the length of the returned list, or %NULL
3419 * @error: return location for a #GError, or %NULL
3421 * Retrieves the names of the applications that have registered the
3422 * bookmark for @uri.
3424 * In the event the URI cannot be found, %NULL is returned and
3425 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3427 * Return value: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3428 * Use g_strfreev() to free it.
3433 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3443 g_return_val_if_fail (bookmark != NULL, NULL);
3444 g_return_val_if_fail (uri != NULL, NULL);
3446 item = g_bookmark_file_lookup_item (bookmark, uri);
3449 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3450 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3451 _("No bookmark found for URI '%s'"),
3456 if (!item->metadata)
3464 n_apps = g_list_length (item->metadata->applications);
3465 apps = g_new0 (gchar *, n_apps + 1);
3467 for (l = g_list_last (item->metadata->applications), i = 0;
3471 BookmarkAppInfo *ai;
3473 ai = (BookmarkAppInfo *) l->data;
3475 g_warn_if_fail (ai != NULL);
3476 g_warn_if_fail (ai->name != NULL);
3478 apps[i++] = g_strdup (ai->name);
3489 * g_bookmark_file_get_size:
3490 * @bookmark: a #GBookmarkFile
3492 * Gets the number of bookmarks inside @bookmark.
3494 * Return value: the number of bookmarks
3499 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3501 g_return_val_if_fail (bookmark != NULL, 0);
3503 return g_list_length (bookmark->items);
3507 * g_bookmark_file_move_item:
3508 * @bookmark: a #GBookmarkFile
3509 * @old_uri: a valid URI
3510 * @new_uri: (allow-none): a valid URI, or %NULL
3511 * @error: return location for a #GError or %NULL
3513 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3514 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3515 * %NULL, then the bookmark is removed.
3517 * In the event the URI cannot be found, %FALSE is returned and
3518 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3520 * Return value: %TRUE if the URI was successfully changed
3525 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3526 const gchar *old_uri,
3527 const gchar *new_uri,
3532 g_return_val_if_fail (bookmark != NULL, FALSE);
3533 g_return_val_if_fail (old_uri != NULL, FALSE);
3535 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3538 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3539 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3540 _("No bookmark found for URI '%s'"),
3545 if (new_uri && new_uri[0] != '\0')
3547 if (g_bookmark_file_has_item (bookmark, new_uri))
3549 if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3553 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3556 item->uri = g_strdup (new_uri);
3557 item->modified = time (NULL);
3559 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3565 if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
3573 * g_bookmark_file_set_icon:
3574 * @bookmark: a #GBookmarkFile
3576 * @href: (allow-none): the URI of the icon for the bookmark, or %NULL
3577 * @mime_type: the MIME type of the icon for the bookmark
3579 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3580 * the currently set icon. @href can either be a full URL for the icon
3581 * file or the icon name following the Icon Naming specification.
3583 * If no bookmark for @uri is found one is created.
3588 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3591 const gchar *mime_type)
3595 g_return_if_fail (bookmark != NULL);
3596 g_return_if_fail (uri != NULL);
3598 item = g_bookmark_file_lookup_item (bookmark, uri);
3601 item = bookmark_item_new (uri);
3602 g_bookmark_file_add_item (bookmark, item, NULL);
3605 if (!item->metadata)
3606 item->metadata = bookmark_metadata_new ();
3608 g_free (item->metadata->icon_href);
3609 g_free (item->metadata->icon_mime);
3611 item->metadata->icon_href = g_strdup (href);
3613 if (mime_type && mime_type[0] != '\0')
3614 item->metadata->icon_mime = g_strdup (mime_type);
3616 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3618 item->modified = time (NULL);
3622 * g_bookmark_file_get_icon:
3623 * @bookmark: a #GBookmarkFile
3625 * @href: (allow-none) (out): return location for the icon's location or %NULL
3626 * @mime_type: (allow-none) (out): return location for the icon's MIME type or %NULL
3627 * @error: return location for a #GError or %NULL
3629 * Gets the icon of the bookmark for @uri.
3631 * In the event the URI cannot be found, %FALSE is returned and
3632 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3634 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3635 * You should free the returned strings.
3640 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3648 g_return_val_if_fail (bookmark != NULL, FALSE);
3649 g_return_val_if_fail (uri != NULL, FALSE);
3651 item = g_bookmark_file_lookup_item (bookmark, uri);
3654 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3655 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3656 _("No bookmark found for URI '%s'"),
3661 if ((!item->metadata) || (!item->metadata->icon_href))
3665 *href = g_strdup (item->metadata->icon_href);
3668 *mime_type = g_strdup (item->metadata->icon_mime);