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"
32 #include <sys/types.h>
41 #include "gfileutils.h"
49 #include "gmessages.h"
54 #include "gstrfuncs.h"
60 /* XBEL 1.0 standard entities */
61 #define XBEL_VERSION "1.0"
62 #define XBEL_DTD_NICK "xbel"
63 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
64 "Exchange Language 1.0//EN//XML"
66 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
68 #define XBEL_ROOT_ELEMENT "xbel"
69 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
70 #define XBEL_BOOKMARK_ELEMENT "bookmark"
71 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
72 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
73 #define XBEL_TITLE_ELEMENT "title"
74 #define XBEL_DESC_ELEMENT "desc"
75 #define XBEL_INFO_ELEMENT "info"
76 #define XBEL_METADATA_ELEMENT "metadata"
78 #define XBEL_VERSION_ATTRIBUTE "version"
79 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
80 #define XBEL_OWNER_ATTRIBUTE "owner"
81 #define XBEL_ADDED_ATTRIBUTE "added"
82 #define XBEL_VISITED_ATTRIBUTE "visited"
83 #define XBEL_MODIFIED_ATTRIBUTE "modified"
84 #define XBEL_ID_ATTRIBUTE "id"
85 #define XBEL_HREF_ATTRIBUTE "href"
86 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
88 #define XBEL_YES_VALUE "yes"
89 #define XBEL_NO_VALUE "no"
91 /* Desktop bookmark spec entities */
92 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
94 #define BOOKMARK_NAMESPACE_NAME "bookmark"
95 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
97 #define BOOKMARK_GROUPS_ELEMENT "groups"
98 #define BOOKMARK_GROUP_ELEMENT "group"
99 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
100 #define BOOKMARK_APPLICATION_ELEMENT "application"
101 #define BOOKMARK_ICON_ELEMENT "icon"
102 #define BOOKMARK_PRIVATE_ELEMENT "private"
104 #define BOOKMARK_NAME_ATTRIBUTE "name"
105 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
106 #define BOOKMARK_COUNT_ATTRIBUTE "count"
107 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp" /* deprecated by "modified" */
108 #define BOOKMARK_MODIFIED_ATTRIBUTE "modified"
109 #define BOOKMARK_HREF_ATTRIBUTE "href"
110 #define BOOKMARK_TYPE_ATTRIBUTE "type"
112 /* Shared MIME Info entities */
113 #define MIME_NAMESPACE_NAME "mime"
114 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
115 #define MIME_TYPE_ELEMENT "mime-type"
116 #define MIME_TYPE_ATTRIBUTE "type"
119 typedef struct _BookmarkAppInfo BookmarkAppInfo;
120 typedef struct _BookmarkMetadata BookmarkMetadata;
121 typedef struct _BookmarkItem BookmarkItem;
122 typedef struct _ParseData ParseData;
124 struct _BookmarkAppInfo
134 struct _BookmarkMetadata
141 GHashTable *apps_by_name;
146 guint is_private : 1;
160 BookmarkMetadata *metadata;
163 struct _GBookmarkFile
168 /* we store our items in a list and keep a copy inside
169 * an hash table for faster lookup performances
172 GHashTable *items_by_uri;
175 /* parser state machine */
196 static void g_bookmark_file_init (GBookmarkFile *bookmark);
197 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
198 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
202 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
205 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
207 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
211 static time_t timestamp_from_iso8601 (const gchar *iso_date);
212 static gchar * timestamp_to_iso8601 (time_t timestamp);
214 /********************************
217 * Application metadata storage *
218 ********************************/
219 static BookmarkAppInfo *
220 bookmark_app_info_new (const gchar *name)
222 BookmarkAppInfo *retval;
224 g_warn_if_fail (name != NULL);
226 retval = g_slice_new (BookmarkAppInfo);
228 retval->name = g_strdup (name);
237 bookmark_app_info_free (BookmarkAppInfo *app_info)
242 g_free (app_info->name);
243 g_free (app_info->exec);
245 g_slice_free (BookmarkAppInfo, app_info);
249 bookmark_app_info_dump (BookmarkAppInfo *app_info)
252 gchar *name, *exec, *modified, *count;
254 g_warn_if_fail (app_info != NULL);
256 if (app_info->count == 0)
259 name = g_markup_escape_text (app_info->name, -1);
260 exec = g_markup_escape_text (app_info->exec, -1);
261 modified = timestamp_to_iso8601 (app_info->stamp);
262 count = g_strdup_printf ("%u", app_info->count);
264 retval = g_strconcat (" "
265 "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
266 " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
267 " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\""
268 " " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", modified, "\""
269 " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
281 /***********************
285 ***********************/
286 static BookmarkMetadata *
287 bookmark_metadata_new (void)
289 BookmarkMetadata *retval;
291 retval = g_slice_new (BookmarkMetadata);
293 retval->mime_type = NULL;
295 retval->groups = NULL;
297 retval->applications = NULL;
298 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
303 retval->is_private = FALSE;
305 retval->icon_href = NULL;
306 retval->icon_mime = NULL;
312 bookmark_metadata_free (BookmarkMetadata *metadata)
317 g_free (metadata->mime_type);
319 if (metadata->groups)
321 g_list_foreach (metadata->groups,
324 g_list_free (metadata->groups);
327 if (metadata->applications)
329 g_list_foreach (metadata->applications,
330 (GFunc) bookmark_app_info_free,
332 g_list_free (metadata->applications);
335 g_hash_table_destroy (metadata->apps_by_name);
337 g_free (metadata->icon_href);
338 g_free (metadata->icon_mime);
340 g_slice_free (BookmarkMetadata, metadata);
344 bookmark_metadata_dump (BookmarkMetadata *metadata)
349 if (!metadata->applications)
352 retval = g_string_sized_new (1024);
354 /* metadata container */
355 g_string_append (retval,
357 "<" XBEL_METADATA_ELEMENT
358 " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
362 if (metadata->mime_type) {
363 buffer = g_strconcat (" "
364 "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
365 MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
367 g_string_append (retval, buffer);
371 if (metadata->groups)
375 /* open groups container */
376 g_string_append (retval,
378 "<" BOOKMARK_NAMESPACE_NAME
379 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
381 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
385 group_name = g_markup_escape_text ((gchar *) l->data, -1);
386 buffer = g_strconcat (" "
387 "<" BOOKMARK_NAMESPACE_NAME
388 ":" BOOKMARK_GROUP_ELEMENT ">",
390 "</" BOOKMARK_NAMESPACE_NAME
391 ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
392 g_string_append (retval, buffer);
398 /* close groups container */
399 g_string_append (retval,
401 "</" BOOKMARK_NAMESPACE_NAME
402 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
405 if (metadata->applications)
409 /* open applications container */
410 g_string_append (retval,
412 "<" BOOKMARK_NAMESPACE_NAME
413 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
415 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
417 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
420 g_warn_if_fail (app_info != NULL);
422 app_data = bookmark_app_info_dump (app_info);
426 retval = g_string_append (retval, app_data);
432 /* close applications container */
433 g_string_append (retval,
435 "</" BOOKMARK_NAMESPACE_NAME
436 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
440 if (metadata->icon_href)
442 if (!metadata->icon_mime)
443 metadata->icon_mime = g_strdup ("application/octet-stream");
445 buffer = g_strconcat (" "
446 "<" BOOKMARK_NAMESPACE_NAME
447 ":" BOOKMARK_ICON_ELEMENT
448 " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
449 "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
450 g_string_append (retval, buffer);
456 if (metadata->is_private)
457 g_string_append (retval,
459 "<" BOOKMARK_NAMESPACE_NAME
460 ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
462 /* close metadata container */
463 g_string_append (retval,
465 "</" XBEL_METADATA_ELEMENT ">\n");
467 return g_string_free (retval, FALSE);
470 /******************************************************
473 * Storage for a single bookmark item inside the list *
474 ******************************************************/
475 static BookmarkItem *
476 bookmark_item_new (const gchar *uri)
480 g_warn_if_fail (uri != NULL);
482 item = g_slice_new (BookmarkItem);
483 item->uri = g_strdup (uri);
486 item->description = NULL;
488 item->added = (time_t) -1;
489 item->modified = (time_t) -1;
490 item->visited = (time_t) -1;
492 item->metadata = NULL;
498 bookmark_item_free (BookmarkItem *item)
504 g_free (item->title);
505 g_free (item->description);
508 bookmark_metadata_free (item->metadata);
510 g_slice_free (BookmarkItem, item);
514 bookmark_item_dump (BookmarkItem *item)
517 gchar *added, *visited, *modified;
521 /* at this point, we must have at least a registered application; if we don't
522 * we don't screw up the bookmark file, and just skip this item
524 if (!item->metadata || !item->metadata->applications)
526 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
530 retval = g_string_sized_new (4096);
532 added = timestamp_to_iso8601 (item->added);
533 modified = timestamp_to_iso8601 (item->modified);
534 visited = timestamp_to_iso8601 (item->visited);
536 escaped_uri = g_markup_escape_text (item->uri, -1);
538 buffer = g_strconcat (" <"
539 XBEL_BOOKMARK_ELEMENT
541 XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
542 XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
543 XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
544 XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
547 g_string_append (retval, buffer);
549 g_free (escaped_uri);
557 gchar *escaped_title;
559 escaped_title = g_markup_escape_text (item->title, -1);
560 buffer = g_strconcat (" "
561 "<" XBEL_TITLE_ELEMENT ">",
563 "</" XBEL_TITLE_ELEMENT ">\n",
565 g_string_append (retval, buffer);
567 g_free (escaped_title);
571 if (item->description)
575 escaped_desc = g_markup_escape_text (item->description, -1);
576 buffer = g_strconcat (" "
577 "<" XBEL_DESC_ELEMENT ">",
579 "</" XBEL_DESC_ELEMENT ">\n",
581 g_string_append (retval, buffer);
583 g_free (escaped_desc);
591 metadata = bookmark_metadata_dump (item->metadata);
594 buffer = g_strconcat (" "
595 "<" XBEL_INFO_ELEMENT ">\n",
598 "</" XBEL_INFO_ELEMENT ">\n",
600 retval = g_string_append (retval, buffer);
607 g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
609 return g_string_free (retval, FALSE);
612 static BookmarkAppInfo *
613 bookmark_item_lookup_app_info (BookmarkItem *item,
614 const gchar *app_name)
616 g_warn_if_fail (item != NULL && app_name != NULL);
621 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
624 /*************************
626 *************************/
629 g_bookmark_file_init (GBookmarkFile *bookmark)
631 bookmark->title = NULL;
632 bookmark->description = NULL;
634 bookmark->items = NULL;
635 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
642 g_bookmark_file_clear (GBookmarkFile *bookmark)
644 g_free (bookmark->title);
645 g_free (bookmark->description);
649 g_list_foreach (bookmark->items,
650 (GFunc) bookmark_item_free,
652 g_list_free (bookmark->items);
654 bookmark->items = NULL;
657 if (bookmark->items_by_uri)
659 g_hash_table_destroy (bookmark->items_by_uri);
661 bookmark->items_by_uri = NULL;
669 GHashTable *namespaces;
671 GBookmarkFile *bookmark_file;
672 BookmarkItem *current_item;
676 parse_data_new (void)
680 retval = g_new (ParseData, 1);
682 retval->state = STATE_STARTED;
683 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
684 (GDestroyNotify) g_free,
685 (GDestroyNotify) g_free);
686 retval->bookmark_file = NULL;
687 retval->current_item = NULL;
693 parse_data_free (ParseData *parse_data)
695 g_hash_table_destroy (parse_data->namespaces);
700 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
703 parse_bookmark_element (GMarkupParseContext *context,
704 ParseData *parse_data,
705 const gchar **attribute_names,
706 const gchar **attribute_values,
709 const gchar *uri, *added, *modified, *visited;
715 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
718 uri = added = modified = visited = NULL;
719 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
721 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
722 uri = attribute_values[i];
723 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
724 added = attribute_values[i];
725 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
726 modified = attribute_values[i];
727 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
728 visited = attribute_values[i];
731 /* bookmark is defined by the XBEL spec, so we need
732 * to error out if the element has different or
735 g_set_error (error, G_MARKUP_ERROR,
736 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
737 _("Unexpected attribute '%s' for element '%s'"),
739 XBEL_BOOKMARK_ELEMENT);
746 g_set_error (error, G_MARKUP_ERROR,
747 G_MARKUP_ERROR_INVALID_CONTENT,
748 _("Attribute '%s' of element '%s' not found"),
750 XBEL_BOOKMARK_ELEMENT);
754 g_warn_if_fail (parse_data->current_item == NULL);
756 item = bookmark_item_new (uri);
759 item->added = timestamp_from_iso8601 (added);
762 item->modified = timestamp_from_iso8601 (modified);
765 item->visited = timestamp_from_iso8601 (visited);
768 g_bookmark_file_add_item (parse_data->bookmark_file,
773 bookmark_item_free (item);
775 g_propagate_error (error, add_error);
780 parse_data->current_item = item;
784 parse_application_element (GMarkupParseContext *context,
785 ParseData *parse_data,
786 const gchar **attribute_names,
787 const gchar **attribute_values,
790 const gchar *name, *exec, *count, *stamp, *modified;
796 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
799 name = exec = count = stamp = modified = NULL;
800 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
802 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
803 name = attribute_values[i];
804 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
805 exec = attribute_values[i];
806 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
807 count = attribute_values[i];
808 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
809 stamp = attribute_values[i];
810 else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
811 modified = attribute_values[i];
814 /* the "name" and "exec" attributes are mandatory */
817 g_set_error (error, G_MARKUP_ERROR,
818 G_MARKUP_ERROR_INVALID_CONTENT,
819 _("Attribute '%s' of element '%s' not found"),
820 BOOKMARK_NAME_ATTRIBUTE,
821 BOOKMARK_APPLICATION_ELEMENT);
827 g_set_error (error, G_MARKUP_ERROR,
828 G_MARKUP_ERROR_INVALID_CONTENT,
829 _("Attribute '%s' of element '%s' not found"),
830 BOOKMARK_EXEC_ATTRIBUTE,
831 BOOKMARK_APPLICATION_ELEMENT);
835 g_warn_if_fail (parse_data->current_item != NULL);
836 item = parse_data->current_item;
838 ai = bookmark_item_lookup_app_info (item, name);
841 ai = bookmark_app_info_new (name);
844 item->metadata = bookmark_metadata_new ();
846 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
847 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
850 ai->exec = g_strdup (exec);
853 ai->count = atoi (count);
858 ai->stamp = timestamp_from_iso8601 (modified);
861 /* the timestamp attribute has been deprecated but we still parse
862 * it for backward compatibility
865 ai->stamp = (time_t) atol (stamp);
867 ai->stamp = time (NULL);
872 parse_mime_type_element (GMarkupParseContext *context,
873 ParseData *parse_data,
874 const gchar **attribute_names,
875 const gchar **attribute_values,
883 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
887 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
889 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
890 type = attribute_values[i];
894 type = "application/octet-stream";
896 g_warn_if_fail (parse_data->current_item != NULL);
897 item = parse_data->current_item;
900 item->metadata = bookmark_metadata_new ();
902 item->metadata->mime_type = g_strdup (type);
906 parse_icon_element (GMarkupParseContext *context,
907 ParseData *parse_data,
908 const gchar **attribute_names,
909 const gchar **attribute_values,
918 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
923 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
925 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
926 href = attribute_values[i];
927 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
928 type = attribute_values[i];
931 /* the "href" attribute is mandatory */
934 g_set_error (error, G_MARKUP_ERROR,
935 G_MARKUP_ERROR_INVALID_CONTENT,
936 _("Attribute '%s' of element '%s' not found"),
937 BOOKMARK_HREF_ATTRIBUTE,
938 BOOKMARK_ICON_ELEMENT);
943 type = "application/octet-stream";
945 g_warn_if_fail (parse_data->current_item != NULL);
946 item = parse_data->current_item;
949 item->metadata = bookmark_metadata_new ();
951 item->metadata->icon_href = g_strdup (href);
952 item->metadata->icon_mime = g_strdup (type);
955 /* scans through the attributes of an element for the "xmlns" pragma, and
956 * adds any resulting namespace declaration to a per-parser hashtable, using
957 * the namespace name as a key for the namespace URI; if no key was found,
958 * the namespace is considered as default, and stored under the "default" key.
960 * FIXME: this works on the assumption that the generator of the XBEL file
961 * is either this code or is smart enough to place the namespace declarations
962 * inside the main root node or inside the metadata node and does not redefine
963 * a namespace inside an inner node; this does *not* conform to the
964 * XML-NS standard, although is a close approximation. In order to make this
965 * conformant to the XML-NS specification we should use a per-element
966 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
969 map_namespace_to_name (ParseData *parse_data,
970 const gchar **attribute_names,
971 const gchar **attribute_values)
976 g_warn_if_fail (parse_data != NULL);
978 if (!attribute_names || !attribute_names[0])
982 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
984 if (g_str_has_prefix (attr, "xmlns"))
986 gchar *namespace_name, *namespace_uri;
989 p = g_utf8_strchr (attr, -1, ':');
991 p = g_utf8_next_char (p);
995 namespace_name = g_strdup (p);
996 namespace_uri = g_strdup (attribute_values[i]);
998 g_hash_table_replace (parse_data->namespaces,
1005 /* checks whether @element_full is equal to @element.
1007 * if @namespace is set, it tries to resolve the namespace to a known URI,
1008 * and if found is prepended to the element name, from which is separated
1009 * using the character specified in the @sep parameter.
1012 is_element_full (ParseData *parse_data,
1013 const gchar *element_full,
1014 const gchar *namespace,
1015 const gchar *element,
1018 gchar *ns_uri, *ns_name;
1019 const gchar *p, *element_name;
1022 g_warn_if_fail (parse_data != NULL);
1023 g_warn_if_fail (element_full != NULL);
1028 /* no namespace requested: dumb element compare */
1030 return (0 == strcmp (element_full, element));
1032 /* search for namespace separator; if none found, assume we are under the
1033 * default namespace, and set ns_name to our "default" marker; if no default
1034 * namespace has been set, just do a plain comparison between @full_element
1037 p = g_utf8_strchr (element_full, -1, ':');
1040 ns_name = g_strndup (element_full, p - element_full);
1041 element_name = g_utf8_next_char (p);
1045 ns_name = g_strdup ("default");
1046 element_name = element_full;
1049 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1052 /* no default namespace found */
1055 return (0 == strcmp (element_full, element));
1058 retval = (0 == strcmp (ns_uri, namespace) &&
1059 0 == strcmp (element_name, element));
1066 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1067 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1070 start_element_raw_cb (GMarkupParseContext *context,
1071 const gchar *element_name,
1072 const gchar **attribute_names,
1073 const gchar **attribute_values,
1077 ParseData *parse_data = (ParseData *) user_data;
1079 /* we must check for namespace declarations first
1081 * XXX - we could speed up things by checking for namespace declarations
1082 * only on the root node, where they usually are; this would probably break
1083 * on streams not produced by us or by "smart" generators
1085 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1087 switch (parse_data->state)
1090 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1096 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1098 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1099 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1100 parse_data->state = STATE_ROOT;
1104 g_set_error (error, G_MARKUP_ERROR,
1105 G_MARKUP_ERROR_INVALID_CONTENT,
1106 _("Unexpected tag '%s', tag '%s' expected"),
1107 element_name, XBEL_ROOT_ELEMENT);
1110 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1111 parse_data->state = STATE_TITLE;
1112 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1113 parse_data->state = STATE_DESC;
1114 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1116 GError *inner_error = NULL;
1118 parse_data->state = STATE_BOOKMARK;
1120 parse_bookmark_element (context,
1126 g_propagate_error (error, inner_error);
1129 g_set_error (error, G_MARKUP_ERROR,
1130 G_MARKUP_ERROR_INVALID_CONTENT,
1131 _("Unexpected tag '%s' inside '%s'"),
1135 case STATE_BOOKMARK:
1136 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1137 parse_data->state = STATE_TITLE;
1138 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1139 parse_data->state = STATE_DESC;
1140 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1141 parse_data->state = STATE_INFO;
1143 g_set_error (error, G_MARKUP_ERROR,
1144 G_MARKUP_ERROR_INVALID_CONTENT,
1145 _("Unexpected tag '%s' inside '%s'"),
1147 XBEL_BOOKMARK_ELEMENT);
1150 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1156 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1158 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1159 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1161 parse_data->state = STATE_METADATA;
1163 if (!parse_data->current_item->metadata)
1164 parse_data->current_item->metadata = bookmark_metadata_new ();
1169 g_set_error (error, G_MARKUP_ERROR,
1170 G_MARKUP_ERROR_INVALID_CONTENT,
1171 _("Unexpected tag '%s', tag '%s' expected"),
1173 XBEL_METADATA_ELEMENT);
1175 case STATE_METADATA:
1176 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1177 parse_data->state = STATE_APPLICATIONS;
1178 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1179 parse_data->state = STATE_GROUPS;
1180 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1181 parse_data->current_item->metadata->is_private = TRUE;
1182 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1184 GError *inner_error = NULL;
1186 parse_data->state = STATE_ICON;
1188 parse_icon_element (context,
1194 g_propagate_error (error, inner_error);
1196 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1198 GError *inner_error = NULL;
1200 parse_data->state = STATE_MIME;
1202 parse_mime_type_element (context,
1208 g_propagate_error (error, inner_error);
1211 g_set_error (error, G_MARKUP_ERROR,
1212 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1213 _("Unexpected tag '%s' inside '%s'"),
1215 XBEL_METADATA_ELEMENT);
1217 case STATE_APPLICATIONS:
1218 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1220 GError *inner_error = NULL;
1222 parse_data->state = STATE_APPLICATION;
1224 parse_application_element (context,
1230 g_propagate_error (error, inner_error);
1233 g_set_error (error, G_MARKUP_ERROR,
1234 G_MARKUP_ERROR_INVALID_CONTENT,
1235 _("Unexpected tag '%s', tag '%s' expected"),
1237 BOOKMARK_APPLICATION_ELEMENT);
1240 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1241 parse_data->state = STATE_GROUP;
1243 g_set_error (error, G_MARKUP_ERROR,
1244 G_MARKUP_ERROR_INVALID_CONTENT,
1245 _("Unexpected tag '%s', tag '%s' expected"),
1247 BOOKMARK_GROUP_ELEMENT);
1250 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1252 GError *inner_error = NULL;
1254 parse_icon_element (context,
1260 g_propagate_error (error, inner_error);
1263 g_set_error (error, G_MARKUP_ERROR,
1264 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1265 _("Unexpected tag '%s' inside '%s'"),
1267 XBEL_METADATA_ELEMENT);
1270 g_warn_if_reached ();
1276 end_element_raw_cb (GMarkupParseContext *context,
1277 const gchar *element_name,
1281 ParseData *parse_data = (ParseData *) user_data;
1283 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1284 parse_data->state = STATE_FINISHED;
1285 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1287 parse_data->current_item = NULL;
1289 parse_data->state = STATE_ROOT;
1291 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1292 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1293 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1295 if (parse_data->current_item)
1296 parse_data->state = STATE_BOOKMARK;
1298 parse_data->state = STATE_ROOT;
1300 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1301 parse_data->state = STATE_INFO;
1302 else if (IS_ELEMENT_NS (parse_data, element_name,
1303 BOOKMARK_NAMESPACE_URI,
1304 BOOKMARK_APPLICATION_ELEMENT))
1305 parse_data->state = STATE_APPLICATIONS;
1306 else if (IS_ELEMENT_NS (parse_data, element_name,
1307 BOOKMARK_NAMESPACE_URI,
1308 BOOKMARK_GROUP_ELEMENT))
1309 parse_data->state = STATE_GROUPS;
1310 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1311 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1312 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1313 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1314 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1315 parse_data->state = STATE_METADATA;
1319 text_raw_cb (GMarkupParseContext *context,
1325 ParseData *parse_data = (ParseData *) user_data;
1328 payload = g_strndup (text, length);
1330 switch (parse_data->state)
1333 if (parse_data->current_item)
1335 g_free (parse_data->current_item->title);
1336 parse_data->current_item->title = g_strdup (payload);
1340 g_free (parse_data->bookmark_file->title);
1341 parse_data->bookmark_file->title = g_strdup (payload);
1345 if (parse_data->current_item)
1347 g_free (parse_data->current_item->description);
1348 parse_data->current_item->description = g_strdup (payload);
1352 g_free (parse_data->bookmark_file->description);
1353 parse_data->bookmark_file->description = g_strdup (payload);
1360 g_warn_if_fail (parse_data->current_item != NULL);
1362 if (!parse_data->current_item->metadata)
1363 parse_data->current_item->metadata = bookmark_metadata_new ();
1365 groups = parse_data->current_item->metadata->groups;
1366 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1370 case STATE_BOOKMARK:
1372 case STATE_METADATA:
1373 case STATE_APPLICATIONS:
1374 case STATE_APPLICATION:
1380 g_warn_if_reached ();
1387 static const GMarkupParser markup_parser =
1389 start_element_raw_cb, /* start_element */
1390 end_element_raw_cb, /* end_element */
1391 text_raw_cb, /* text */
1392 NULL, /* passthrough */
1397 g_bookmark_file_parse (GBookmarkFile *bookmark,
1398 const gchar *buffer,
1402 GMarkupParseContext *context;
1403 ParseData *parse_data;
1404 GError *parse_error, *end_error;
1407 g_warn_if_fail (bookmark != NULL);
1412 if (length == (gsize) -1)
1413 length = strlen (buffer);
1415 parse_data = parse_data_new ();
1416 parse_data->bookmark_file = bookmark;
1418 context = g_markup_parse_context_new (&markup_parser,
1421 (GDestroyNotify) parse_data_free);
1424 retval = g_markup_parse_context_parse (context,
1430 g_propagate_error (error, parse_error);
1436 retval = g_markup_parse_context_end_parse (context, &end_error);
1439 g_propagate_error (error, end_error);
1444 g_markup_parse_context_free (context);
1450 g_bookmark_file_dump (GBookmarkFile *bookmark,
1458 retval = g_string_sized_new (4096);
1460 g_string_append (retval,
1461 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1463 /* XXX - do we really need the doctype? */
1464 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1465 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1466 " \"" XBEL_DTD_URI "\">\n"
1468 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1469 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1470 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1472 if (bookmark->title)
1474 gchar *escaped_title;
1476 escaped_title = g_markup_escape_text (bookmark->title, -1);
1478 buffer = g_strconcat (" "
1479 "<" XBEL_TITLE_ELEMENT ">",
1481 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1483 g_string_append (retval, buffer);
1486 g_free (escaped_title);
1489 if (bookmark->description)
1491 gchar *escaped_desc;
1493 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1495 buffer = g_strconcat (" "
1496 "<" XBEL_DESC_ELEMENT ">",
1498 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1499 g_string_append (retval, buffer);
1502 g_free (escaped_desc);
1505 if (!bookmark->items)
1508 retval = g_string_append (retval, "\n");
1510 /* the items are stored in reverse order */
1511 for (l = g_list_last (bookmark->items);
1515 BookmarkItem *item = (BookmarkItem *) l->data;
1518 item_dump = bookmark_item_dump (item);
1522 retval = g_string_append (retval, item_dump);
1528 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1531 *length = retval->len;
1533 return g_string_free (retval, FALSE);
1540 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1541 * should free the returned string.
1544 timestamp_to_iso8601 (time_t timestamp)
1548 if (timestamp == (time_t) -1)
1549 g_get_current_time (&stamp);
1552 stamp.tv_sec = timestamp;
1556 return g_time_val_to_iso8601 (&stamp);
1560 timestamp_from_iso8601 (const gchar *iso_date)
1564 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1567 return (time_t) stamp.tv_sec;
1573 g_bookmark_file_error_quark (void)
1575 return g_quark_from_static_string ("g-bookmark-file-error-quark");
1580 /********************
1582 ********************/
1585 * g_bookmark_file_new:
1587 * Creates a new empty #GBookmarkFile object.
1589 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1590 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1593 * Return value: an empty #GBookmarkFile
1598 g_bookmark_file_new (void)
1600 GBookmarkFile *bookmark;
1602 bookmark = g_new (GBookmarkFile, 1);
1604 g_bookmark_file_init (bookmark);
1610 * g_bookmark_file_free:
1611 * @bookmark: a #GBookmarkFile
1613 * Frees a #GBookmarkFile.
1618 g_bookmark_file_free (GBookmarkFile *bookmark)
1623 g_bookmark_file_clear (bookmark);
1629 * g_bookmark_file_load_from_data:
1630 * @bookmark: an empty #GBookmarkFile struct
1631 * @data: desktop bookmarks loaded in memory
1632 * @length: the length of @data in bytes
1633 * @error: return location for a #GError, or %NULL
1635 * Loads a bookmark file from memory into an empty #GBookmarkFile
1636 * structure. If the object cannot be created then @error is set to a
1637 * #GBookmarkFileError.
1639 * Return value: %TRUE if a desktop bookmark could be loaded.
1644 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1649 GError *parse_error;
1652 g_return_val_if_fail (bookmark != NULL, FALSE);
1653 g_return_val_if_fail (data != NULL, FALSE);
1654 g_return_val_if_fail (length != 0, FALSE);
1656 if (length == (gsize) -1)
1657 length = strlen (data);
1659 if (bookmark->items)
1661 g_bookmark_file_clear (bookmark);
1662 g_bookmark_file_init (bookmark);
1666 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1669 g_propagate_error (error, parse_error);
1678 * g_bookmark_file_load_from_file:
1679 * @bookmark: an empty #GBookmarkFile struct
1680 * @filename: the path of a filename to load, in the GLib file name encoding
1681 * @error: return location for a #GError, or %NULL
1683 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1684 * If the file could not be loaded then @error is set to either a #GFileError
1685 * or #GBookmarkFileError.
1687 * Return value: %TRUE if a desktop bookmark file could be loaded
1692 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1693 const gchar *filename,
1701 g_return_val_if_fail (bookmark != NULL, FALSE);
1702 g_return_val_if_fail (filename != NULL, FALSE);
1705 g_file_get_contents (filename, &buffer, &len, &read_error);
1708 g_propagate_error (error, read_error);
1714 retval = g_bookmark_file_load_from_data (bookmark,
1720 g_propagate_error (error, read_error);
1733 /* Iterates through all the directories in *dirs trying to
1734 * find file. When it successfully locates file, returns a
1735 * string its absolute path. It also leaves the unchecked
1736 * directories in *dirs. You should free the returned string
1738 * Adapted from gkeyfile.c
1741 find_file_in_data_dirs (const gchar *file,
1745 gchar **data_dirs, *data_dir, *path;
1754 while (data_dirs && (data_dir = *data_dirs) && !path)
1756 gchar *candidate_file, *sub_dir;
1758 candidate_file = (gchar *) file;
1759 sub_dir = g_strdup ("");
1760 while (candidate_file != NULL && !path)
1764 path = g_build_filename (data_dir, sub_dir,
1765 candidate_file, NULL);
1767 candidate_file = strchr (candidate_file, '-');
1769 if (candidate_file == NULL)
1775 sub_dir = g_strndup (file, candidate_file - file - 1);
1777 for (p = sub_dir; *p != '\0'; p++)
1780 *p = G_DIR_SEPARATOR;
1791 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1792 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1793 _("No valid bookmark file found in data dirs"));
1803 * g_bookmark_file_load_from_data_dirs:
1804 * @bookmark: a #GBookmarkFile
1805 * @file: a relative path to a filename to open and parse
1806 * @full_path: return location for a string containing the full path
1807 * of the file, or %NULL
1808 * @error: return location for a #GError, or %NULL
1810 * This function looks for a desktop bookmark file named @file in the
1811 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1812 * loads the file into @bookmark and returns the file's full path in
1813 * @full_path. If the file could not be loaded then an %error is
1814 * set to either a #GFileError or #GBookmarkFileError.
1816 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1821 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1826 GError *file_error = NULL;
1827 gchar **all_data_dirs, **data_dirs;
1828 const gchar *user_data_dir;
1829 const gchar * const * system_data_dirs;
1832 gboolean found_file;
1834 g_return_val_if_fail (bookmark != NULL, FALSE);
1835 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1837 user_data_dir = g_get_user_data_dir ();
1838 system_data_dirs = g_get_system_data_dirs ();
1839 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1842 all_data_dirs[i++] = g_strdup (user_data_dir);
1845 while (system_data_dirs[j] != NULL)
1846 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1849 data_dirs = all_data_dirs;
1851 while (*data_dirs != NULL && !found_file)
1853 g_free (output_path);
1855 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1859 g_propagate_error (error, file_error);
1863 found_file = g_bookmark_file_load_from_file (bookmark,
1868 g_propagate_error (error, file_error);
1873 if (found_file && full_path)
1874 *full_path = output_path;
1876 g_free (output_path);
1878 g_strfreev (all_data_dirs);
1885 * g_bookmark_file_to_data:
1886 * @bookmark: a #GBookmarkFile
1887 * @length: return location for the length of the returned string, or %NULL
1888 * @error: return location for a #GError, or %NULL
1890 * This function outputs @bookmark as a string.
1892 * Return value: a newly allocated string holding
1893 * the contents of the #GBookmarkFile
1898 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1902 GError *write_error = NULL;
1905 g_return_val_if_fail (bookmark != NULL, NULL);
1907 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1910 g_propagate_error (error, write_error);
1919 * g_bookmark_file_to_file:
1920 * @bookmark: a #GBookmarkFile
1921 * @filename: path of the output file
1922 * @error: return location for a #GError, or %NULL
1924 * This function outputs @bookmark into a file. The write process is
1925 * guaranteed to be atomic by using g_file_set_contents() internally.
1927 * Return value: %TRUE if the file was successfully written.
1932 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1933 const gchar *filename,
1937 GError *data_error, *write_error;
1941 g_return_val_if_fail (bookmark != NULL, FALSE);
1942 g_return_val_if_fail (filename != NULL, FALSE);
1945 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1948 g_propagate_error (error, data_error);
1954 g_file_set_contents (filename, data, len, &write_error);
1957 g_propagate_error (error, write_error);
1969 static BookmarkItem *
1970 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1973 g_warn_if_fail (bookmark != NULL && uri != NULL);
1975 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1978 /* this function adds a new item to the list */
1980 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1984 g_warn_if_fail (bookmark != NULL);
1985 g_warn_if_fail (item != NULL);
1987 /* this should never happen; and if it does, then we are
1988 * screwing up something big time.
1990 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1992 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1993 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1994 _("A bookmark for URI '%s' already exists"),
1999 bookmark->items = g_list_prepend (bookmark->items, item);
2001 g_hash_table_replace (bookmark->items_by_uri,
2005 if (item->added == (time_t) -1)
2006 item->added = time (NULL);
2008 if (item->modified == (time_t) -1)
2009 item->modified = time (NULL);
2013 * g_bookmark_file_remove_item:
2014 * @bookmark: a #GBookmarkFile
2016 * @error: return location for a #GError, or %NULL
2018 * Removes the bookmark for @uri from the bookmark file @bookmark.
2020 * Return value: %TRUE if the bookmark was removed successfully.
2025 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2031 g_return_val_if_fail (bookmark != NULL, FALSE);
2032 g_return_val_if_fail (uri != NULL, FALSE);
2034 item = g_bookmark_file_lookup_item (bookmark, uri);
2038 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2039 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2040 _("No bookmark found for URI '%s'"),
2045 bookmark->items = g_list_remove (bookmark->items, item);
2046 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2048 bookmark_item_free (item);
2054 * g_bookmark_file_has_item:
2055 * @bookmark: a #GBookmarkFile
2058 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2060 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2065 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2068 g_return_val_if_fail (bookmark != NULL, FALSE);
2069 g_return_val_if_fail (uri != NULL, FALSE);
2071 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2075 * g_bookmark_file_get_uris:
2076 * @bookmark: a #GBookmarkFile
2077 * @length: return location for the number of returned URIs, or %NULL
2079 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2080 * The array of returned URIs will be %NULL-terminated, so @length may
2081 * optionally be %NULL.
2083 * Return value: a newly allocated %NULL-terminated array of strings.
2084 * Use g_strfreev() to free it.
2089 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2096 g_return_val_if_fail (bookmark != NULL, NULL);
2098 n_items = g_list_length (bookmark->items);
2099 uris = g_new0 (gchar *, n_items + 1);
2101 /* the items are stored in reverse order, so we walk the list backward */
2102 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2104 BookmarkItem *item = (BookmarkItem *) l->data;
2106 g_warn_if_fail (item != NULL);
2108 uris[i++] = g_strdup (item->uri);
2119 * g_bookmark_file_set_title:
2120 * @bookmark: a #GBookmarkFile
2121 * @uri: a valid URI or %NULL
2122 * @title: a UTF-8 encoded string
2124 * Sets @title as the title of the bookmark for @uri inside the
2125 * bookmark file @bookmark.
2127 * If @uri is %NULL, the title of @bookmark is set.
2129 * If a bookmark for @uri cannot be found then it is created.
2134 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2138 g_return_if_fail (bookmark != NULL);
2142 g_free (bookmark->title);
2143 bookmark->title = g_strdup (title);
2149 item = g_bookmark_file_lookup_item (bookmark, uri);
2152 item = bookmark_item_new (uri);
2153 g_bookmark_file_add_item (bookmark, item, NULL);
2156 g_free (item->title);
2157 item->title = g_strdup (title);
2159 item->modified = time (NULL);
2164 * g_bookmark_file_get_title:
2165 * @bookmark: a #GBookmarkFile
2166 * @uri: a valid URI or %NULL
2167 * @error: return location for a #GError, or %NULL
2169 * Returns the title of the bookmark for @uri.
2171 * If @uri is %NULL, the title of @bookmark is returned.
2173 * In the event the URI cannot be found, %NULL is returned and
2174 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2176 * Return value: a newly allocated string or %NULL if the specified
2177 * URI cannot be found.
2182 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2188 g_return_val_if_fail (bookmark != NULL, NULL);
2191 return g_strdup (bookmark->title);
2193 item = g_bookmark_file_lookup_item (bookmark, uri);
2196 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2197 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2198 _("No bookmark found for URI '%s'"),
2203 return g_strdup (item->title);
2207 * g_bookmark_file_set_description:
2208 * @bookmark: a #GBookmarkFile
2209 * @uri: a valid URI or %NULL
2210 * @description: a string
2212 * Sets @description as the description of the bookmark for @uri.
2214 * If @uri is %NULL, the description of @bookmark is set.
2216 * If a bookmark for @uri cannot be found then it is created.
2221 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2223 const gchar *description)
2225 g_return_if_fail (bookmark != NULL);
2229 g_free (bookmark->description);
2230 bookmark->description = g_strdup (description);
2236 item = g_bookmark_file_lookup_item (bookmark, uri);
2239 item = bookmark_item_new (uri);
2240 g_bookmark_file_add_item (bookmark, item, NULL);
2243 g_free (item->description);
2244 item->description = g_strdup (description);
2246 item->modified = time (NULL);
2251 * g_bookmark_file_get_description:
2252 * @bookmark: a #GBookmarkFile
2254 * @error: return location for a #GError, or %NULL
2256 * Retrieves the description of the bookmark for @uri.
2258 * In the event the URI cannot be found, %NULL is returned and
2259 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2261 * Return value: a newly allocated string or %NULL if the specified
2262 * URI cannot be found.
2267 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2273 g_return_val_if_fail (bookmark != NULL, NULL);
2276 return g_strdup (bookmark->description);
2278 item = g_bookmark_file_lookup_item (bookmark, uri);
2281 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2282 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2283 _("No bookmark found for URI '%s'"),
2288 return g_strdup (item->description);
2292 * g_bookmark_file_set_mime_type:
2293 * @bookmark: a #GBookmarkFile
2295 * @mime_type: a MIME type
2297 * Sets @mime_type as the MIME type of the bookmark for @uri.
2299 * If a bookmark for @uri cannot be found then it is created.
2304 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2306 const gchar *mime_type)
2310 g_return_if_fail (bookmark != NULL);
2311 g_return_if_fail (uri != NULL);
2312 g_return_if_fail (mime_type != NULL);
2314 item = g_bookmark_file_lookup_item (bookmark, uri);
2317 item = bookmark_item_new (uri);
2318 g_bookmark_file_add_item (bookmark, item, NULL);
2321 if (!item->metadata)
2322 item->metadata = bookmark_metadata_new ();
2324 g_free (item->metadata->mime_type);
2326 item->metadata->mime_type = g_strdup (mime_type);
2327 item->modified = time (NULL);
2331 * g_bookmark_file_get_mime_type:
2332 * @bookmark: a #GBookmarkFile
2334 * @error: return location for a #GError, or %NULL
2336 * Retrieves the MIME type of the resource pointed by @uri.
2338 * In the event the URI cannot be found, %NULL is returned and
2339 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2340 * event that the MIME type cannot be found, %NULL is returned and
2341 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2343 * Return value: a newly allocated string or %NULL if the specified
2344 * URI cannot be found.
2349 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2355 g_return_val_if_fail (bookmark != NULL, NULL);
2356 g_return_val_if_fail (uri != NULL, NULL);
2358 item = g_bookmark_file_lookup_item (bookmark, uri);
2361 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2362 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2363 _("No bookmark found for URI '%s'"),
2368 if (!item->metadata)
2370 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2371 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2372 _("No MIME type defined in the bookmark for URI '%s'"),
2377 return g_strdup (item->metadata->mime_type);
2381 * g_bookmark_file_set_is_private:
2382 * @bookmark: a #GBookmarkFile
2384 * @is_private: %TRUE if the bookmark should be marked as private
2386 * Sets the private flag of the bookmark for @uri.
2388 * If a bookmark for @uri cannot be found then it is created.
2393 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2395 gboolean is_private)
2399 g_return_if_fail (bookmark != NULL);
2400 g_return_if_fail (uri != NULL);
2402 item = g_bookmark_file_lookup_item (bookmark, uri);
2405 item = bookmark_item_new (uri);
2406 g_bookmark_file_add_item (bookmark, item, NULL);
2409 if (!item->metadata)
2410 item->metadata = bookmark_metadata_new ();
2412 item->metadata->is_private = (is_private == TRUE);
2413 item->modified = time (NULL);
2417 * g_bookmark_file_get_is_private:
2418 * @bookmark: a #GBookmarkFile
2420 * @error: return location for a #GError, or %NULL
2422 * Gets whether the private flag of the bookmark for @uri is set.
2424 * In the event the URI cannot be found, %FALSE is returned and
2425 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2426 * event that the private flag cannot be found, %FALSE is returned and
2427 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2429 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2434 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2440 g_return_val_if_fail (bookmark != NULL, FALSE);
2441 g_return_val_if_fail (uri != NULL, FALSE);
2443 item = g_bookmark_file_lookup_item (bookmark, uri);
2446 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2447 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2448 _("No bookmark found for URI '%s'"),
2453 if (!item->metadata)
2455 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2456 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2457 _("No private flag has been defined in bookmark for URI '%s'"),
2462 return item->metadata->is_private;
2466 * g_bookmark_file_set_added:
2467 * @bookmark: a #GBookmarkFile
2469 * @added: a timestamp or -1 to use the current time
2471 * Sets the time the bookmark for @uri was added into @bookmark.
2473 * If no bookmark for @uri is found then it is created.
2478 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2484 g_return_if_fail (bookmark != NULL);
2485 g_return_if_fail (uri != NULL);
2487 item = g_bookmark_file_lookup_item (bookmark, uri);
2490 item = bookmark_item_new (uri);
2491 g_bookmark_file_add_item (bookmark, item, NULL);
2494 if (added == (time_t) -1)
2497 item->added = added;
2498 item->modified = added;
2502 * g_bookmark_file_get_added:
2503 * @bookmark: a #GBookmarkFile
2505 * @error: return location for a #GError, or %NULL
2507 * Gets the time the bookmark for @uri was added to @bookmark
2509 * In the event the URI cannot be found, -1 is returned and
2510 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2512 * Return value: a timestamp
2517 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2523 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2524 g_return_val_if_fail (uri != NULL, (time_t) -1);
2526 item = g_bookmark_file_lookup_item (bookmark, uri);
2529 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2530 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2531 _("No bookmark found for URI '%s'"),
2540 * g_bookmark_file_set_modified:
2541 * @bookmark: a #GBookmarkFile
2543 * @modified: a timestamp or -1 to use the current time
2545 * Sets the last time the bookmark for @uri was last modified.
2547 * If no bookmark for @uri is found then it is created.
2549 * The "modified" time should only be set when the bookmark's meta-data
2550 * was actually changed. Every function of #GBookmarkFile that
2551 * modifies a bookmark also changes the modification time, except for
2552 * g_bookmark_file_set_visited().
2557 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2563 g_return_if_fail (bookmark != NULL);
2564 g_return_if_fail (uri != NULL);
2566 item = g_bookmark_file_lookup_item (bookmark, uri);
2569 item = bookmark_item_new (uri);
2570 g_bookmark_file_add_item (bookmark, item, NULL);
2573 if (modified == (time_t) -1)
2576 item->modified = modified;
2580 * g_bookmark_file_get_modified:
2581 * @bookmark: a #GBookmarkFile
2583 * @error: return location for a #GError, or %NULL
2585 * Gets the time when the bookmark for @uri was last modified.
2587 * In the event the URI cannot be found, -1 is returned and
2588 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2590 * Return value: a timestamp
2595 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2601 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2602 g_return_val_if_fail (uri != NULL, (time_t) -1);
2604 item = g_bookmark_file_lookup_item (bookmark, uri);
2607 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2608 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2609 _("No bookmark found for URI '%s'"),
2614 return item->modified;
2618 * g_bookmark_file_set_visited:
2619 * @bookmark: a #GBookmarkFile
2621 * @visited: a timestamp or -1 to use the current time
2623 * Sets the time the bookmark for @uri was last visited.
2625 * If no bookmark for @uri is found then it is created.
2627 * The "visited" time should only be set if the bookmark was launched,
2628 * either using the command line retrieved by g_bookmark_file_get_app_info()
2629 * or by the default application for the bookmark's MIME type, retrieved
2630 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2631 * does not affect the "modified" time.
2636 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2642 g_return_if_fail (bookmark != NULL);
2643 g_return_if_fail (uri != NULL);
2645 item = g_bookmark_file_lookup_item (bookmark, uri);
2648 item = bookmark_item_new (uri);
2649 g_bookmark_file_add_item (bookmark, item, NULL);
2652 if (visited == (time_t) -1)
2655 item->visited = visited;
2659 * g_bookmark_file_get_visited:
2660 * @bookmark: a #GBookmarkFile
2662 * @error: return location for a #GError, or %NULL
2664 * Gets the time the bookmark for @uri was last visited.
2666 * In the event the URI cannot be found, -1 is returned and
2667 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2669 * Return value: a timestamp.
2674 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2680 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2681 g_return_val_if_fail (uri != NULL, (time_t) -1);
2683 item = g_bookmark_file_lookup_item (bookmark, uri);
2686 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2687 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2688 _("No bookmark found for URI '%s'"),
2693 return item->visited;
2697 * g_bookmark_file_has_group:
2698 * @bookmark: a #GBookmarkFile
2700 * @group: the group name to be searched
2701 * @error: return location for a #GError, or %NULL
2703 * Checks whether @group appears in the list of groups to which
2704 * the bookmark for @uri belongs to.
2706 * In the event the URI cannot be found, %FALSE is returned and
2707 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2709 * Return value: %TRUE if @group was found.
2714 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2722 g_return_val_if_fail (bookmark != NULL, FALSE);
2723 g_return_val_if_fail (uri != NULL, FALSE);
2725 item = g_bookmark_file_lookup_item (bookmark, uri);
2728 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2729 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2730 _("No bookmark found for URI '%s'"),
2735 if (!item->metadata)
2738 for (l = item->metadata->groups; l != NULL; l = l->next)
2740 if (strcmp (l->data, group) == 0)
2749 * g_bookmark_file_add_group:
2750 * @bookmark: a #GBookmarkFile
2752 * @group: the group name to be added
2754 * Adds @group to the list of groups to which the bookmark for @uri
2757 * If no bookmark for @uri is found then it is created.
2762 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2768 g_return_if_fail (bookmark != NULL);
2769 g_return_if_fail (uri != NULL);
2770 g_return_if_fail (group != NULL && group[0] != '\0');
2772 item = g_bookmark_file_lookup_item (bookmark, uri);
2775 item = bookmark_item_new (uri);
2776 g_bookmark_file_add_item (bookmark, item, NULL);
2779 if (!item->metadata)
2780 item->metadata = bookmark_metadata_new ();
2782 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2784 item->metadata->groups = g_list_prepend (item->metadata->groups,
2787 item->modified = time (NULL);
2792 * g_bookmark_file_remove_group:
2793 * @bookmark: a #GBookmarkFile
2795 * @group: the group name to be removed
2796 * @error: return location for a #GError, or %NULL
2798 * Removes @group from the list of groups to which the bookmark
2799 * for @uri belongs to.
2801 * In the event the URI cannot be found, %FALSE is returned and
2802 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2803 * In the event no group was defined, %FALSE is returned and
2804 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2806 * Return value: %TRUE if @group was successfully removed.
2811 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2819 g_return_val_if_fail (bookmark != NULL, FALSE);
2820 g_return_val_if_fail (uri != NULL, FALSE);
2822 item = g_bookmark_file_lookup_item (bookmark, uri);
2825 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2826 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2827 _("No bookmark found for URI '%s'"),
2832 if (!item->metadata)
2834 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2835 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2836 _("No groups set in bookmark for URI '%s'"),
2841 for (l = item->metadata->groups; l != NULL; l = l->next)
2843 if (strcmp (l->data, group) == 0)
2845 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2849 item->modified = time (NULL);
2859 * g_bookmark_file_set_groups:
2860 * @bookmark: a #GBookmarkFile
2861 * @uri: an item's URI
2862 * @groups: an array of group names, or %NULL to remove all groups
2863 * @length: number of group name values in @groups
2865 * Sets a list of group names for the item with URI @uri. Each previously
2866 * set group name list is removed.
2868 * If @uri cannot be found then an item for it is created.
2873 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2875 const gchar **groups,
2881 g_return_if_fail (bookmark != NULL);
2882 g_return_if_fail (uri != NULL);
2883 g_return_if_fail (groups != NULL);
2885 item = g_bookmark_file_lookup_item (bookmark, uri);
2888 item = bookmark_item_new (uri);
2889 g_bookmark_file_add_item (bookmark, item, NULL);
2892 if (!item->metadata)
2893 item->metadata = bookmark_metadata_new ();
2895 if (item->metadata->groups != NULL)
2897 g_list_foreach (item->metadata->groups,
2900 g_list_free (item->metadata->groups);
2901 item->metadata->groups = NULL;
2906 for (i = 0; groups[i] != NULL && i < length; i++)
2907 item->metadata->groups = g_list_append (item->metadata->groups,
2908 g_strdup (groups[i]));
2911 item->modified = time (NULL);
2915 * g_bookmark_file_get_groups:
2916 * @bookmark: a #GBookmarkFile
2918 * @length: return location for the length of the returned string, or %NULL
2919 * @error: return location for a #GError, or %NULL
2921 * Retrieves the list of group names of the bookmark for @uri.
2923 * In the event the URI cannot be found, %NULL is returned and
2924 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2926 * The returned array is %NULL terminated, so @length may optionally
2929 * Return value: a newly allocated %NULL-terminated array of group names.
2930 * Use g_strfreev() to free it.
2935 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2945 g_return_val_if_fail (bookmark != NULL, NULL);
2946 g_return_val_if_fail (uri != NULL, NULL);
2948 item = g_bookmark_file_lookup_item (bookmark, uri);
2951 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2952 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2953 _("No bookmark found for URI '%s'"),
2958 if (!item->metadata)
2966 len = g_list_length (item->metadata->groups);
2967 retval = g_new0 (gchar *, len + 1);
2968 for (l = g_list_last (item->metadata->groups), i = 0;
2972 gchar *group_name = (gchar *) l->data;
2974 g_warn_if_fail (group_name != NULL);
2976 retval[i++] = g_strdup (group_name);
2987 * g_bookmark_file_add_application:
2988 * @bookmark: a #GBookmarkFile
2990 * @name: the name of the application registering the bookmark
2992 * @exec: command line to be used to launch the bookmark or %NULL
2994 * Adds the application with @name and @exec to the list of
2995 * applications that have registered a bookmark for @uri into
2998 * Every bookmark inside a #GBookmarkFile must have at least an
2999 * application registered. Each application must provide a name, a
3000 * command line useful for launching the bookmark, the number of times
3001 * the bookmark has been registered by the application and the last
3002 * time the application registered this bookmark.
3004 * If @name is %NULL, the name of the application will be the
3005 * same returned by g_get_application_name(); if @exec is %NULL, the
3006 * command line will be a composition of the program name as
3007 * returned by g_get_prgname() and the "%u" modifier, which will be
3008 * expanded to the bookmark's URI.
3010 * This function will automatically take care of updating the
3011 * registrations count and timestamping in case an application
3012 * with the same @name had already registered a bookmark for
3013 * @uri inside @bookmark.
3015 * If no bookmark for @uri is found, one is created.
3020 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3026 gchar *app_name, *app_exec;
3028 g_return_if_fail (bookmark != NULL);
3029 g_return_if_fail (uri != NULL);
3031 item = g_bookmark_file_lookup_item (bookmark, uri);
3034 item = bookmark_item_new (uri);
3035 g_bookmark_file_add_item (bookmark, item, NULL);
3038 if (name && name[0] != '\0')
3039 app_name = g_strdup (name);
3041 app_name = g_strdup (g_get_application_name ());
3043 if (exec && exec[0] != '\0')
3044 app_exec = g_strdup (exec);
3046 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3048 g_bookmark_file_set_app_info (bookmark, uri,
3060 * g_bookmark_file_remove_application:
3061 * @bookmark: a #GBookmarkFile
3063 * @name: the name of the application
3064 * @error: return location for a #GError or %NULL
3066 * Removes application registered with @name from the list of applications
3067 * that have registered a bookmark for @uri inside @bookmark.
3069 * In the event the URI cannot be found, %FALSE is returned and
3070 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3071 * In the event that no application with name @app_name has registered
3072 * a bookmark for @uri, %FALSE is returned and error is set to
3073 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3075 * Return value: %TRUE if the application was successfully removed.
3080 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3088 g_return_val_if_fail (bookmark != NULL, FALSE);
3089 g_return_val_if_fail (uri != NULL, FALSE);
3090 g_return_val_if_fail (name != NULL, FALSE);
3093 retval = g_bookmark_file_set_app_info (bookmark, uri,
3101 g_propagate_error (error, set_error);
3110 * g_bookmark_file_has_application:
3111 * @bookmark: a #GBookmarkFile
3113 * @name: the name of the application
3114 * @error: return location for a #GError or %NULL
3116 * Checks whether the bookmark for @uri inside @bookmark has been
3117 * registered by application @name.
3119 * In the event the URI cannot be found, %FALSE is returned and
3120 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3122 * Return value: %TRUE if the application @name was found
3127 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3134 g_return_val_if_fail (bookmark != NULL, FALSE);
3135 g_return_val_if_fail (uri != NULL, FALSE);
3136 g_return_val_if_fail (name != NULL, FALSE);
3138 item = g_bookmark_file_lookup_item (bookmark, uri);
3141 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3142 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3143 _("No bookmark found for URI '%s'"),
3148 return (NULL != bookmark_item_lookup_app_info (item, name));
3152 * g_bookmark_file_set_app_info:
3153 * @bookmark: a #GBookmarkFile
3155 * @name: an application's name
3156 * @exec: an application's command line
3157 * @count: the number of registrations done for this application
3158 * @stamp: the time of the last registration for this application
3159 * @error: return location for a #GError or %NULL
3161 * Sets the meta-data of application @name inside the list of
3162 * applications that have registered a bookmark for @uri inside
3165 * You should rarely use this function; use g_bookmark_file_add_application()
3166 * and g_bookmark_file_remove_application() instead.
3168 * @name can be any UTF-8 encoded string used to identify an
3170 * @exec can have one of these two modifiers: "%f", which will
3171 * be expanded as the local file name retrieved from the bookmark's
3172 * URI; "%u", which will be expanded as the bookmark's URI.
3173 * The expansion is done automatically when retrieving the stored
3174 * command line using the g_bookmark_file_get_app_info() function.
3175 * @count is the number of times the application has registered the
3176 * bookmark; if is < 0, the current registration count will be increased
3177 * by one, if is 0, the application with @name will be removed from
3178 * the list of registered applications.
3179 * @stamp is the Unix time of the last registration; if it is -1, the
3180 * current time will be used.
3182 * If you try to remove an application by setting its registration count to
3183 * zero, and no bookmark for @uri is found, %FALSE is returned and
3184 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3185 * in the event that no application @name has registered a bookmark
3186 * for @uri, %FALSE is returned and error is set to
3187 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3188 * for @uri is found, one is created.
3190 * Return value: %TRUE if the application's meta-data was successfully
3196 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3205 BookmarkAppInfo *ai;
3207 g_return_val_if_fail (bookmark != NULL, FALSE);
3208 g_return_val_if_fail (uri != NULL, FALSE);
3209 g_return_val_if_fail (name != NULL, FALSE);
3210 g_return_val_if_fail (exec != NULL, FALSE);
3212 item = g_bookmark_file_lookup_item (bookmark, uri);
3217 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3218 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3219 _("No bookmark found for URI '%s'"),
3225 item = bookmark_item_new (uri);
3226 g_bookmark_file_add_item (bookmark, item, NULL);
3230 ai = bookmark_item_lookup_app_info (item, name);
3235 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3236 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3237 _("No application with name '%s' registered a bookmark for '%s'"),
3244 ai = bookmark_app_info_new (name);
3246 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3247 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3253 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3254 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3255 bookmark_app_info_free (ai);
3257 item->modified = time (NULL);
3266 if (stamp != (time_t) -1)
3269 ai->stamp = time (NULL);
3271 if (exec && exec[0] != '\0')
3274 ai->exec = g_shell_quote (exec);
3277 item->modified = time (NULL);
3282 /* expands the application's command line */
3284 expand_exec_line (const gchar *exec_fmt,
3290 exec = g_string_sized_new (512);
3291 while ((ch = *exec_fmt++) != '\0')
3295 exec = g_string_append_c (exec, ch);
3306 g_string_append (exec, uri);
3311 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3314 g_string_append (exec, file);
3319 g_string_free (exec, TRUE);
3326 exec = g_string_append_c (exec, ch);
3332 return g_string_free (exec, FALSE);
3336 * g_bookmark_file_get_app_info:
3337 * @bookmark: a #GBookmarkFile
3339 * @name: an application's name
3340 * @exec: location for the command line of the application, or %NULL
3341 * @count: return location for the registration count, or %NULL
3342 * @stamp: return location for the last registration time, or %NULL
3343 * @error: return location for a #GError, or %NULL
3345 * Gets the registration informations of @app_name for the bookmark for
3346 * @uri. See g_bookmark_file_set_app_info() for more informations about
3347 * the returned data.
3349 * The string returned in @app_exec must be freed.
3351 * In the event the URI cannot be found, %FALSE is returned and
3352 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3353 * event that no application with name @app_name has registered a bookmark
3354 * for @uri, %FALSE is returned and error is set to
3355 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3356 * the command line fails, an error of the #G_SHELL_ERROR domain is
3357 * set and %FALSE is returned.
3359 * Return value: %TRUE on success.
3364 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3373 BookmarkAppInfo *ai;
3375 g_return_val_if_fail (bookmark != NULL, FALSE);
3376 g_return_val_if_fail (uri != NULL, FALSE);
3377 g_return_val_if_fail (name != NULL, FALSE);
3379 item = g_bookmark_file_lookup_item (bookmark, uri);
3382 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3383 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3384 _("No bookmark found for URI '%s'"),
3389 ai = bookmark_item_lookup_app_info (item, name);
3392 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3393 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3394 _("No application with name '%s' registered a bookmark for '%s'"),
3402 GError *unquote_error = NULL;
3403 gchar *command_line;
3405 command_line = g_shell_unquote (ai->exec, &unquote_error);
3408 g_propagate_error (error, unquote_error);
3412 *exec = expand_exec_line (command_line, uri);
3415 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3416 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3417 _("Failed to expand exec line '%s' with URI '%s'"),
3419 g_free (command_line);
3424 g_free (command_line);
3437 * g_bookmark_file_get_applications:
3438 * @bookmark: a #GBookmarkFile
3440 * @length: return location of the length of the returned list, or %NULL
3441 * @error: return location for a #GError, or %NULL
3443 * Retrieves the names of the applications that have registered the
3444 * bookmark for @uri.
3446 * In the event the URI cannot be found, %NULL is returned and
3447 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3449 * Return value: a newly allocated %NULL-terminated array of strings.
3450 * Use g_strfreev() to free it.
3455 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3465 g_return_val_if_fail (bookmark != NULL, NULL);
3466 g_return_val_if_fail (uri != NULL, NULL);
3468 item = g_bookmark_file_lookup_item (bookmark, uri);
3471 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3472 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3473 _("No bookmark found for URI '%s'"),
3478 if (!item->metadata)
3486 n_apps = g_list_length (item->metadata->applications);
3487 apps = g_new0 (gchar *, n_apps + 1);
3489 for (l = g_list_last (item->metadata->applications), i = 0;
3493 BookmarkAppInfo *ai;
3495 ai = (BookmarkAppInfo *) l->data;
3497 g_warn_if_fail (ai != NULL);
3498 g_warn_if_fail (ai->name != NULL);
3500 apps[i++] = g_strdup (ai->name);
3511 * g_bookmark_file_get_size:
3512 * @bookmark: a #GBookmarkFile
3514 * Gets the number of bookmarks inside @bookmark.
3516 * Return value: the number of bookmarks
3521 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3523 g_return_val_if_fail (bookmark != NULL, 0);
3525 return g_list_length (bookmark->items);
3529 * g_bookmark_file_move_item:
3530 * @bookmark: a #GBookmarkFile
3531 * @old_uri: a valid URI
3532 * @new_uri: a valid URI, or %NULL
3533 * @error: return location for a #GError or %NULL
3535 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3536 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3537 * %NULL, then the bookmark is removed.
3539 * In the event the URI cannot be found, %FALSE is returned and
3540 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3542 * Return value: %TRUE if the URI was successfully changed
3547 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3548 const gchar *old_uri,
3549 const gchar *new_uri,
3553 GError *remove_error;
3555 g_return_val_if_fail (bookmark != NULL, FALSE);
3556 g_return_val_if_fail (old_uri != NULL, FALSE);
3558 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3561 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3562 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3563 _("No bookmark found for URI '%s'"),
3568 if (new_uri && new_uri[0] != '\0')
3570 if (g_bookmark_file_has_item (bookmark, new_uri))
3572 remove_error = NULL;
3573 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3576 g_propagate_error (error, remove_error);
3582 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3585 item->uri = g_strdup (new_uri);
3586 item->modified = time (NULL);
3588 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3594 remove_error = NULL;
3595 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3598 g_propagate_error (error, remove_error);
3608 * g_bookmark_file_set_icon:
3609 * @bookmark: a #GBookmarkFile
3611 * @href: the URI of the icon for the bookmark, or %NULL
3612 * @mime_type: the MIME type of the icon for the bookmark
3614 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3615 * the currently set icon. @href can either be a full URL for the icon
3616 * file or the icon name following the Icon Naming specification.
3618 * If no bookmark for @uri is found one is created.
3623 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3626 const gchar *mime_type)
3630 g_return_if_fail (bookmark != NULL);
3631 g_return_if_fail (uri != NULL);
3633 item = g_bookmark_file_lookup_item (bookmark, uri);
3636 item = bookmark_item_new (uri);
3637 g_bookmark_file_add_item (bookmark, item, NULL);
3640 if (!item->metadata)
3641 item->metadata = bookmark_metadata_new ();
3643 g_free (item->metadata->icon_href);
3644 g_free (item->metadata->icon_mime);
3646 item->metadata->icon_href = g_strdup (href);
3648 if (mime_type && mime_type[0] != '\0')
3649 item->metadata->icon_mime = g_strdup (mime_type);
3651 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3653 item->modified = time (NULL);
3657 * g_bookmark_file_get_icon:
3658 * @bookmark: a #GBookmarkFile
3660 * @href: return location for the icon's location or %NULL
3661 * @mime_type: return location for the icon's MIME type or %NULL
3662 * @error: return location for a #GError or %NULL
3664 * Gets the icon of the bookmark for @uri.
3666 * In the event the URI cannot be found, %FALSE is returned and
3667 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3669 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3670 * You should free the returned strings.
3675 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3683 g_return_val_if_fail (bookmark != NULL, FALSE);
3684 g_return_val_if_fail (uri != NULL, FALSE);
3686 item = g_bookmark_file_lookup_item (bookmark, uri);
3689 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3690 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3691 _("No bookmark found for URI '%s'"),
3696 if ((!item->metadata) || (!item->metadata->icon_href))
3700 *href = g_strdup (item->metadata->icon_href);
3703 *mime_type = g_strdup (item->metadata->icon_mime);
3708 #define __G_BOOKMARK_FILE_C__
3709 #include "galiasdef.c"