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 g_set_error (error, G_MARKUP_ERROR,
732 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
733 _("Unexpected attribute '%s' for element '%s'"),
735 XBEL_BOOKMARK_ELEMENT);
742 g_set_error (error, G_MARKUP_ERROR,
743 G_MARKUP_ERROR_INVALID_CONTENT,
744 _("Attribute '%s' of element '%s' not found"),
746 XBEL_BOOKMARK_ELEMENT);
750 g_warn_if_fail (parse_data->current_item == NULL);
752 item = bookmark_item_new (uri);
755 item->added = timestamp_from_iso8601 (added);
758 item->modified = timestamp_from_iso8601 (modified);
761 item->visited = timestamp_from_iso8601 (visited);
764 g_bookmark_file_add_item (parse_data->bookmark_file,
769 bookmark_item_free (item);
771 g_propagate_error (error, add_error);
776 parse_data->current_item = item;
780 parse_application_element (GMarkupParseContext *context,
781 ParseData *parse_data,
782 const gchar **attribute_names,
783 const gchar **attribute_values,
786 const gchar *name, *exec, *count, *stamp, *modified;
792 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
795 name = exec = count = stamp = modified = NULL;
796 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
798 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
799 name = attribute_values[i];
800 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
801 exec = attribute_values[i];
802 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
803 count = attribute_values[i];
804 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
805 stamp = attribute_values[i];
806 else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
807 modified = attribute_values[i];
810 g_set_error (error, G_MARKUP_ERROR,
811 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
812 _("Unexpected attribute '%s' for element '%s'"),
814 BOOKMARK_APPLICATION_ELEMENT);
821 g_set_error (error, G_MARKUP_ERROR,
822 G_MARKUP_ERROR_INVALID_CONTENT,
823 _("Attribute '%s' of element '%s' not found"),
824 BOOKMARK_NAME_ATTRIBUTE,
825 BOOKMARK_APPLICATION_ELEMENT);
831 g_set_error (error, G_MARKUP_ERROR,
832 G_MARKUP_ERROR_INVALID_CONTENT,
833 _("Attribute '%s' of element '%s' not found"),
834 BOOKMARK_EXEC_ATTRIBUTE,
835 BOOKMARK_APPLICATION_ELEMENT);
839 g_warn_if_fail (parse_data->current_item != NULL);
840 item = parse_data->current_item;
842 ai = bookmark_item_lookup_app_info (item, name);
845 ai = bookmark_app_info_new (name);
848 item->metadata = bookmark_metadata_new ();
850 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
851 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
854 ai->exec = g_strdup (exec);
857 ai->count = atoi (count);
862 ai->stamp = timestamp_from_iso8601 (modified);
865 /* the timestamp attribute has been deprecated but we still parse
866 * it for backward compatibility
869 ai->stamp = (time_t) atol (stamp);
871 ai->stamp = time (NULL);
876 parse_mime_type_element (GMarkupParseContext *context,
877 ParseData *parse_data,
878 const gchar **attribute_names,
879 const gchar **attribute_values,
887 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
891 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
893 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
894 type = attribute_values[i];
897 g_set_error (error, G_MARKUP_ERROR,
898 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
899 _("Unexpected attribute '%s' for element '%s'"),
907 type = "application/octet-stream";
909 g_warn_if_fail (parse_data->current_item != NULL);
910 item = parse_data->current_item;
913 item->metadata = bookmark_metadata_new ();
915 item->metadata->mime_type = g_strdup (type);
919 parse_icon_element (GMarkupParseContext *context,
920 ParseData *parse_data,
921 const gchar **attribute_names,
922 const gchar **attribute_values,
931 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
936 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
938 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
939 href = attribute_values[i];
940 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
941 type = attribute_values[i];
944 g_set_error (error, G_MARKUP_ERROR,
945 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
946 _("Unexpected attribute '%s' for element '%s'"),
948 BOOKMARK_ICON_ELEMENT);
955 g_set_error (error, G_MARKUP_ERROR,
956 G_MARKUP_ERROR_INVALID_CONTENT,
957 _("Attribute '%s' of element '%s' not found"),
958 BOOKMARK_HREF_ATTRIBUTE,
959 BOOKMARK_ICON_ELEMENT);
964 type = "application/octet-stream";
966 g_warn_if_fail (parse_data->current_item != NULL);
967 item = parse_data->current_item;
970 item->metadata = bookmark_metadata_new ();
972 item->metadata->icon_href = g_strdup (href);
973 item->metadata->icon_mime = g_strdup (type);
976 /* scans through the attributes of an element for the "xmlns" pragma, and
977 * adds any resulting namespace declaration to a per-parser hashtable, using
978 * the namespace name as a key for the namespace URI; if no key was found,
979 * the namespace is considered as default, and stored under the "default" key.
981 * FIXME: this works on the assumption that the generator of the XBEL file
982 * is either this code or is smart enough to place the namespace declarations
983 * inside the main root node or inside the metadata node and does not redefine
984 * a namespace inside an inner node; this does *not* conform to the
985 * XML-NS standard, although is a close approximation. In order to make this
986 * conformant to the XML-NS specification we should use a per-element
987 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
990 map_namespace_to_name (ParseData *parse_data,
991 const gchar **attribute_names,
992 const gchar **attribute_values)
997 g_warn_if_fail (parse_data != NULL);
999 if (!attribute_names || !attribute_names[0])
1003 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1005 if (g_str_has_prefix (attr, "xmlns"))
1007 gchar *namespace_name, *namespace_uri;
1010 p = g_utf8_strchr (attr, -1, ':');
1012 p = g_utf8_next_char (p);
1016 namespace_name = g_strdup (p);
1017 namespace_uri = g_strdup (attribute_values[i]);
1019 g_hash_table_replace (parse_data->namespaces,
1026 /* checks whether @element_full is equal to @element.
1028 * if @namespace is set, it tries to resolve the namespace to a known URI,
1029 * and if found is prepended to the element name, from which is separated
1030 * using the character specified in the @sep parameter.
1033 is_element_full (ParseData *parse_data,
1034 const gchar *element_full,
1035 const gchar *namespace,
1036 const gchar *element,
1039 gchar *ns_uri, *ns_name;
1040 const gchar *p, *element_name;
1043 g_warn_if_fail (parse_data != NULL);
1044 g_warn_if_fail (element_full != NULL);
1049 /* no namespace requested: dumb element compare */
1051 return (0 == strcmp (element_full, element));
1053 /* search for namespace separator; if none found, assume we are under the
1054 * default namespace, and set ns_name to our "default" marker; if no default
1055 * namespace has been set, just do a plain comparison between @full_element
1058 p = g_utf8_strchr (element_full, -1, ':');
1061 ns_name = g_strndup (element_full, p - element_full);
1062 element_name = g_utf8_next_char (p);
1066 ns_name = g_strdup ("default");
1067 element_name = element_full;
1070 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1073 /* no default namespace found */
1076 return (0 == strcmp (element_full, element));
1079 retval = (0 == strcmp (ns_uri, namespace) &&
1080 0 == strcmp (element_name, element));
1087 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1088 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1091 start_element_raw_cb (GMarkupParseContext *context,
1092 const gchar *element_name,
1093 const gchar **attribute_names,
1094 const gchar **attribute_values,
1098 ParseData *parse_data = (ParseData *) user_data;
1100 /* we must check for namespace declarations first
1102 * XXX - we could speed up things by checking for namespace declarations
1103 * only on the root node, where they usually are; this would probably break
1104 * on streams not produced by us or by "smart" generators
1106 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1108 switch (parse_data->state)
1111 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1117 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1119 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1120 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1121 parse_data->state = STATE_ROOT;
1125 g_set_error (error, G_MARKUP_ERROR,
1126 G_MARKUP_ERROR_INVALID_CONTENT,
1127 _("Unexpected tag '%s', tag '%s' expected"),
1128 element_name, XBEL_ROOT_ELEMENT);
1131 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1132 parse_data->state = STATE_TITLE;
1133 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1134 parse_data->state = STATE_DESC;
1135 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1137 GError *inner_error = NULL;
1139 parse_data->state = STATE_BOOKMARK;
1141 parse_bookmark_element (context,
1147 g_propagate_error (error, inner_error);
1150 g_set_error (error, G_MARKUP_ERROR,
1151 G_MARKUP_ERROR_INVALID_CONTENT,
1152 _("Unexpected tag '%s' inside '%s'"),
1156 case STATE_BOOKMARK:
1157 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1158 parse_data->state = STATE_TITLE;
1159 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1160 parse_data->state = STATE_DESC;
1161 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1162 parse_data->state = STATE_INFO;
1164 g_set_error (error, G_MARKUP_ERROR,
1165 G_MARKUP_ERROR_INVALID_CONTENT,
1166 _("Unexpected tag '%s' inside '%s'"),
1168 XBEL_BOOKMARK_ELEMENT);
1171 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1177 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1179 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1180 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1182 parse_data->state = STATE_METADATA;
1184 if (!parse_data->current_item->metadata)
1185 parse_data->current_item->metadata = bookmark_metadata_new ();
1190 g_set_error (error, G_MARKUP_ERROR,
1191 G_MARKUP_ERROR_INVALID_CONTENT,
1192 _("Unexpected tag '%s', tag '%s' expected"),
1194 XBEL_METADATA_ELEMENT);
1196 case STATE_METADATA:
1197 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1198 parse_data->state = STATE_APPLICATIONS;
1199 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1200 parse_data->state = STATE_GROUPS;
1201 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1202 parse_data->current_item->metadata->is_private = TRUE;
1203 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1205 GError *inner_error = NULL;
1207 parse_data->state = STATE_ICON;
1209 parse_icon_element (context,
1215 g_propagate_error (error, inner_error);
1217 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1219 GError *inner_error = NULL;
1221 parse_data->state = STATE_MIME;
1223 parse_mime_type_element (context,
1229 g_propagate_error (error, inner_error);
1232 g_set_error (error, G_MARKUP_ERROR,
1233 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1234 _("Unexpected tag '%s' inside '%s'"),
1236 XBEL_METADATA_ELEMENT);
1238 case STATE_APPLICATIONS:
1239 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1241 GError *inner_error = NULL;
1243 parse_data->state = STATE_APPLICATION;
1245 parse_application_element (context,
1251 g_propagate_error (error, inner_error);
1254 g_set_error (error, G_MARKUP_ERROR,
1255 G_MARKUP_ERROR_INVALID_CONTENT,
1256 _("Unexpected tag '%s', tag '%s' expected"),
1258 BOOKMARK_APPLICATION_ELEMENT);
1261 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1262 parse_data->state = STATE_GROUP;
1264 g_set_error (error, G_MARKUP_ERROR,
1265 G_MARKUP_ERROR_INVALID_CONTENT,
1266 _("Unexpected tag '%s', tag '%s' expected"),
1268 BOOKMARK_GROUP_ELEMENT);
1271 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1273 GError *inner_error = NULL;
1275 parse_icon_element (context,
1281 g_propagate_error (error, inner_error);
1284 g_set_error (error, G_MARKUP_ERROR,
1285 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1286 _("Unexpected tag '%s' inside '%s'"),
1288 XBEL_METADATA_ELEMENT);
1291 g_warn_if_reached ();
1297 end_element_raw_cb (GMarkupParseContext *context,
1298 const gchar *element_name,
1302 ParseData *parse_data = (ParseData *) user_data;
1304 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1305 parse_data->state = STATE_FINISHED;
1306 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1308 parse_data->current_item = NULL;
1310 parse_data->state = STATE_ROOT;
1312 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1313 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1314 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1316 if (parse_data->current_item)
1317 parse_data->state = STATE_BOOKMARK;
1319 parse_data->state = STATE_ROOT;
1321 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1322 parse_data->state = STATE_INFO;
1323 else if (IS_ELEMENT_NS (parse_data, element_name,
1324 BOOKMARK_NAMESPACE_URI,
1325 BOOKMARK_APPLICATION_ELEMENT))
1326 parse_data->state = STATE_APPLICATIONS;
1327 else if (IS_ELEMENT_NS (parse_data, element_name,
1328 BOOKMARK_NAMESPACE_URI,
1329 BOOKMARK_GROUP_ELEMENT))
1330 parse_data->state = STATE_GROUPS;
1331 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1332 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1333 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1334 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1335 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1336 parse_data->state = STATE_METADATA;
1340 text_raw_cb (GMarkupParseContext *context,
1346 ParseData *parse_data = (ParseData *) user_data;
1349 payload = g_strndup (text, length);
1351 switch (parse_data->state)
1354 if (parse_data->current_item)
1356 g_free (parse_data->current_item->title);
1357 parse_data->current_item->title = g_strdup (payload);
1361 g_free (parse_data->bookmark_file->title);
1362 parse_data->bookmark_file->title = g_strdup (payload);
1366 if (parse_data->current_item)
1368 g_free (parse_data->current_item->description);
1369 parse_data->current_item->description = g_strdup (payload);
1373 g_free (parse_data->bookmark_file->description);
1374 parse_data->bookmark_file->description = g_strdup (payload);
1381 g_warn_if_fail (parse_data->current_item != NULL);
1383 if (!parse_data->current_item->metadata)
1384 parse_data->current_item->metadata = bookmark_metadata_new ();
1386 groups = parse_data->current_item->metadata->groups;
1387 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1391 case STATE_BOOKMARK:
1393 case STATE_METADATA:
1394 case STATE_APPLICATIONS:
1395 case STATE_APPLICATION:
1401 g_warn_if_reached ();
1408 static const GMarkupParser markup_parser =
1410 start_element_raw_cb, /* start_element */
1411 end_element_raw_cb, /* end_element */
1412 text_raw_cb, /* text */
1413 NULL, /* passthrough */
1418 g_bookmark_file_parse (GBookmarkFile *bookmark,
1419 const gchar *buffer,
1423 GMarkupParseContext *context;
1424 ParseData *parse_data;
1425 GError *parse_error, *end_error;
1428 g_warn_if_fail (bookmark != NULL);
1434 length = strlen (buffer);
1436 parse_data = parse_data_new ();
1437 parse_data->bookmark_file = bookmark;
1439 context = g_markup_parse_context_new (&markup_parser,
1442 (GDestroyNotify) parse_data_free);
1445 retval = g_markup_parse_context_parse (context,
1451 g_propagate_error (error, parse_error);
1457 retval = g_markup_parse_context_end_parse (context, &end_error);
1460 g_propagate_error (error, end_error);
1465 g_markup_parse_context_free (context);
1471 g_bookmark_file_dump (GBookmarkFile *bookmark,
1479 retval = g_string_sized_new (4096);
1481 g_string_append (retval,
1482 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1484 /* XXX - do we really need the doctype? */
1485 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1486 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1487 " \"" XBEL_DTD_URI "\">\n"
1489 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1490 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1491 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1493 if (bookmark->title)
1495 gchar *escaped_title;
1497 escaped_title = g_markup_escape_text (bookmark->title, -1);
1499 buffer = g_strconcat (" "
1500 "<" XBEL_TITLE_ELEMENT ">",
1502 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1504 g_string_append (retval, buffer);
1507 g_free (escaped_title);
1510 if (bookmark->description)
1512 gchar *escaped_desc;
1514 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1516 buffer = g_strconcat (" "
1517 "<" XBEL_DESC_ELEMENT ">",
1519 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1520 g_string_append (retval, buffer);
1523 g_free (escaped_desc);
1526 if (!bookmark->items)
1529 retval = g_string_append (retval, "\n");
1531 /* the items are stored in reverse order */
1532 for (l = g_list_last (bookmark->items);
1536 BookmarkItem *item = (BookmarkItem *) l->data;
1539 item_dump = bookmark_item_dump (item);
1543 retval = g_string_append (retval, item_dump);
1549 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1552 *length = retval->len;
1554 return g_string_free (retval, FALSE);
1561 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1562 * should free the returned string.
1565 timestamp_to_iso8601 (time_t timestamp)
1569 if (timestamp == (time_t) -1)
1570 g_get_current_time (&stamp);
1573 stamp.tv_sec = timestamp;
1577 return g_time_val_to_iso8601 (&stamp);
1581 timestamp_from_iso8601 (const gchar *iso_date)
1585 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1588 return (time_t) stamp.tv_sec;
1594 g_bookmark_file_error_quark (void)
1596 return g_quark_from_static_string ("g-bookmark-file-error-quark");
1601 /********************
1603 ********************/
1606 * g_bookmark_file_new:
1608 * Creates a new empty #GBookmarkFile object.
1610 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1611 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1614 * Return value: an empty #GBookmarkFile
1619 g_bookmark_file_new (void)
1621 GBookmarkFile *bookmark;
1623 bookmark = g_new (GBookmarkFile, 1);
1625 g_bookmark_file_init (bookmark);
1631 * g_bookmark_file_free:
1632 * @bookmark: a #GBookmarkFile
1634 * Frees a #GBookmarkFile.
1639 g_bookmark_file_free (GBookmarkFile *bookmark)
1644 g_bookmark_file_clear (bookmark);
1650 * g_bookmark_file_load_from_data:
1651 * @bookmark: an empty #GBookmarkFile struct
1652 * @data: desktop bookmarks loaded in memory
1653 * @length: the length of @data in bytes
1654 * @error: return location for a #GError, or %NULL
1656 * Loads a bookmark file from memory into an empty #GBookmarkFile
1657 * structure. If the object cannot be created then @error is set to a
1658 * #GBookmarkFileError.
1660 * Return value: %TRUE if a desktop bookmark could be loaded.
1665 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1670 GError *parse_error;
1673 g_return_val_if_fail (bookmark != NULL, FALSE);
1674 g_return_val_if_fail (data != NULL, FALSE);
1675 g_return_val_if_fail (length != 0, FALSE);
1677 if (length == (gsize) -1)
1678 length = strlen (data);
1680 if (bookmark->items)
1682 g_bookmark_file_clear (bookmark);
1683 g_bookmark_file_init (bookmark);
1687 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1690 g_propagate_error (error, parse_error);
1699 * g_bookmark_file_load_from_file:
1700 * @bookmark: an empty #GBookmarkFile struct
1701 * @filename: the path of a filename to load, in the GLib file name encoding
1702 * @error: return location for a #GError, or %NULL
1704 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1705 * If the file could not be loaded then @error is set to either a #GFileError
1706 * or #GBookmarkFileError.
1708 * Return value: %TRUE if a desktop bookmark file could be loaded
1713 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1714 const gchar *filename,
1722 g_return_val_if_fail (bookmark != NULL, FALSE);
1723 g_return_val_if_fail (filename != NULL, FALSE);
1726 g_file_get_contents (filename, &buffer, &len, &read_error);
1729 g_propagate_error (error, read_error);
1735 retval = g_bookmark_file_load_from_data (bookmark,
1741 g_propagate_error (error, read_error);
1754 /* Iterates through all the directories in *dirs trying to
1755 * find file. When it successfully locates file, returns a
1756 * string its absolute path. It also leaves the unchecked
1757 * directories in *dirs. You should free the returned string
1759 * Adapted from gkeyfile.c
1762 find_file_in_data_dirs (const gchar *file,
1766 gchar **data_dirs, *data_dir, *path;
1775 while (data_dirs && (data_dir = *data_dirs) && !path)
1777 gchar *candidate_file, *sub_dir;
1779 candidate_file = (gchar *) file;
1780 sub_dir = g_strdup ("");
1781 while (candidate_file != NULL && !path)
1785 path = g_build_filename (data_dir, sub_dir,
1786 candidate_file, NULL);
1788 candidate_file = strchr (candidate_file, '-');
1790 if (candidate_file == NULL)
1796 sub_dir = g_strndup (file, candidate_file - file - 1);
1798 for (p = sub_dir; *p != '\0'; p++)
1801 *p = G_DIR_SEPARATOR;
1812 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1813 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1814 _("No valid bookmark file found in data dirs"));
1824 * g_bookmark_file_load_from_data_dirs:
1825 * @bookmark: a #GBookmarkFile
1826 * @file: a relative path to a filename to open and parse
1827 * @full_path: return location for a string containing the full path
1828 * of the file, or %NULL
1829 * @error: return location for a #GError, or %NULL
1831 * This function looks for a desktop bookmark file named @file in the
1832 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1833 * loads the file into @bookmark and returns the file's full path in
1834 * @full_path. If the file could not be loaded then an %error is
1835 * set to either a #GFileError or #GBookmarkFileError.
1837 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1842 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1847 GError *file_error = NULL;
1848 gchar **all_data_dirs, **data_dirs;
1849 const gchar *user_data_dir;
1850 const gchar * const * system_data_dirs;
1853 gboolean found_file;
1855 g_return_val_if_fail (bookmark != NULL, FALSE);
1856 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1858 user_data_dir = g_get_user_data_dir ();
1859 system_data_dirs = g_get_system_data_dirs ();
1860 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1863 all_data_dirs[i++] = g_strdup (user_data_dir);
1866 while (system_data_dirs[j] != NULL)
1867 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1870 data_dirs = all_data_dirs;
1872 while (*data_dirs != NULL && !found_file)
1874 g_free (output_path);
1876 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1880 g_propagate_error (error, file_error);
1884 found_file = g_bookmark_file_load_from_file (bookmark,
1889 g_propagate_error (error, file_error);
1894 if (found_file && full_path)
1895 *full_path = output_path;
1897 g_free (output_path);
1899 g_strfreev (all_data_dirs);
1906 * g_bookmark_file_to_data:
1907 * @bookmark: a #GBookmarkFile
1908 * @length: return location for the length of the returned string, or %NULL
1909 * @error: return location for a #GError, or %NULL
1911 * This function outputs @bookmark as a string.
1913 * Return value: a newly allocated string holding
1914 * the contents of the #GBookmarkFile
1919 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1923 GError *write_error = NULL;
1926 g_return_val_if_fail (bookmark != NULL, NULL);
1928 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1931 g_propagate_error (error, write_error);
1940 * g_bookmark_file_to_file:
1941 * @bookmark: a #GBookmarkFile
1942 * @filename: path of the output file
1943 * @error: return location for a #GError, or %NULL
1945 * This function outputs @bookmark into a file. The write process is
1946 * guaranteed to be atomic by using g_file_set_contents() internally.
1948 * Return value: %TRUE if the file was successfully written.
1953 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1954 const gchar *filename,
1958 GError *data_error, *write_error;
1962 g_return_val_if_fail (bookmark != NULL, FALSE);
1963 g_return_val_if_fail (filename != NULL, FALSE);
1966 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1969 g_propagate_error (error, data_error);
1975 g_file_set_contents (filename, data, len, &write_error);
1978 g_propagate_error (error, write_error);
1990 static BookmarkItem *
1991 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1994 g_warn_if_fail (bookmark != NULL && uri != NULL);
1996 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1999 /* this function adds a new item to the list */
2001 g_bookmark_file_add_item (GBookmarkFile *bookmark,
2005 g_warn_if_fail (bookmark != NULL);
2006 g_warn_if_fail (item != NULL);
2008 /* this should never happen; and if it does, then we are
2009 * screwing up something big time.
2011 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2013 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2014 G_BOOKMARK_FILE_ERROR_INVALID_URI,
2015 _("A bookmark for URI '%s' already exists"),
2020 bookmark->items = g_list_prepend (bookmark->items, item);
2022 g_hash_table_replace (bookmark->items_by_uri,
2026 if (item->added == (time_t) -1)
2027 item->added = time (NULL);
2029 if (item->modified == (time_t) -1)
2030 item->modified = time (NULL);
2034 * g_bookmark_file_remove_item:
2035 * @bookmark: a #GBookmarkFile
2037 * @error: return location for a #GError, or %NULL
2039 * Removes the bookmark for @uri from the bookmark file @bookmark.
2041 * Return value: %TRUE if the bookmark was removed successfully.
2046 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2052 g_return_val_if_fail (bookmark != NULL, FALSE);
2053 g_return_val_if_fail (uri != NULL, FALSE);
2055 item = g_bookmark_file_lookup_item (bookmark, uri);
2059 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2060 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2061 _("No bookmark found for URI '%s'"),
2066 bookmark->items = g_list_remove (bookmark->items, item);
2067 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2069 bookmark_item_free (item);
2075 * g_bookmark_file_has_item:
2076 * @bookmark: a #GBookmarkFile
2079 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2081 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2086 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2089 g_return_val_if_fail (bookmark != NULL, FALSE);
2090 g_return_val_if_fail (uri != NULL, FALSE);
2092 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2096 * g_bookmark_file_get_uris:
2097 * @bookmark: a #GBookmarkFile
2098 * @length: return location for the number of returned URIs, or %NULL
2100 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2101 * The array of returned URIs will be %NULL-terminated, so @length may
2102 * optionally be %NULL.
2104 * Return value: a newly allocated %NULL-terminated array of strings.
2105 * Use g_strfreev() to free it.
2110 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2117 g_return_val_if_fail (bookmark != NULL, NULL);
2119 n_items = g_list_length (bookmark->items);
2120 uris = g_new0 (gchar *, n_items + 1);
2122 /* the items are stored in reverse order, so we walk the list backward */
2123 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2125 BookmarkItem *item = (BookmarkItem *) l->data;
2127 g_warn_if_fail (item != NULL);
2129 uris[i++] = g_strdup (item->uri);
2140 * g_bookmark_file_set_title:
2141 * @bookmark: a #GBookmarkFile
2142 * @uri: a valid URI or %NULL
2143 * @title: a UTF-8 encoded string
2145 * Sets @title as the title of the bookmark for @uri inside the
2146 * bookmark file @bookmark.
2148 * If @uri is %NULL, the title of @bookmark is set.
2150 * If a bookmark for @uri cannot be found then it is created.
2155 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2159 g_return_if_fail (bookmark != NULL);
2163 g_free (bookmark->title);
2164 bookmark->title = g_strdup (title);
2170 item = g_bookmark_file_lookup_item (bookmark, uri);
2173 item = bookmark_item_new (uri);
2174 g_bookmark_file_add_item (bookmark, item, NULL);
2177 g_free (item->title);
2178 item->title = g_strdup (title);
2180 item->modified = time (NULL);
2185 * g_bookmark_file_get_title:
2186 * @bookmark: a #GBookmarkFile
2187 * @uri: a valid URI or %NULL
2188 * @error: return location for a #GError, or %NULL
2190 * Returns the title of the bookmark for @uri.
2192 * If @uri is %NULL, the title of @bookmark is returned.
2194 * In the event the URI cannot be found, %NULL is returned and
2195 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2197 * Return value: a newly allocated string or %NULL if the specified
2198 * URI cannot be found.
2203 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2209 g_return_val_if_fail (bookmark != NULL, NULL);
2212 return g_strdup (bookmark->title);
2214 item = g_bookmark_file_lookup_item (bookmark, uri);
2217 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2218 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2219 _("No bookmark found for URI '%s'"),
2224 return g_strdup (item->title);
2228 * g_bookmark_file_set_description:
2229 * @bookmark: a #GBookmarkFile
2230 * @uri: a valid URI or %NULL
2231 * @description: a string
2233 * Sets @description as the description of the bookmark for @uri.
2235 * If @uri is %NULL, the description of @bookmark is set.
2237 * If a bookmark for @uri cannot be found then it is created.
2242 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2244 const gchar *description)
2246 g_return_if_fail (bookmark != NULL);
2250 g_free (bookmark->description);
2251 bookmark->description = g_strdup (description);
2257 item = g_bookmark_file_lookup_item (bookmark, uri);
2260 item = bookmark_item_new (uri);
2261 g_bookmark_file_add_item (bookmark, item, NULL);
2264 g_free (item->description);
2265 item->description = g_strdup (description);
2267 item->modified = time (NULL);
2272 * g_bookmark_file_get_description:
2273 * @bookmark: a #GBookmarkFile
2275 * @error: return location for a #GError, or %NULL
2277 * Retrieves the description of the bookmark for @uri.
2279 * In the event the URI cannot be found, %NULL is returned and
2280 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2282 * Return value: a newly allocated string or %NULL if the specified
2283 * URI cannot be found.
2288 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2294 g_return_val_if_fail (bookmark != NULL, NULL);
2297 return g_strdup (bookmark->description);
2299 item = g_bookmark_file_lookup_item (bookmark, uri);
2302 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2303 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2304 _("No bookmark found for URI '%s'"),
2309 return g_strdup (item->description);
2313 * g_bookmark_file_set_mime_type:
2314 * @bookmark: a #GBookmarkFile
2316 * @mime_type: a MIME type
2318 * Sets @mime_type as the MIME type of the bookmark for @uri.
2320 * If a bookmark for @uri cannot be found then it is created.
2325 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2327 const gchar *mime_type)
2331 g_return_if_fail (bookmark != NULL);
2332 g_return_if_fail (uri != NULL);
2333 g_return_if_fail (mime_type != NULL);
2335 item = g_bookmark_file_lookup_item (bookmark, uri);
2338 item = bookmark_item_new (uri);
2339 g_bookmark_file_add_item (bookmark, item, NULL);
2342 if (!item->metadata)
2343 item->metadata = bookmark_metadata_new ();
2345 g_free (item->metadata->mime_type);
2347 item->metadata->mime_type = g_strdup (mime_type);
2348 item->modified = time (NULL);
2352 * g_bookmark_file_get_mime_type:
2353 * @bookmark: a #GBookmarkFile
2355 * @error: return location for a #GError, or %NULL
2357 * Retrieves the MIME type of the resource pointed by @uri.
2359 * In the event the URI cannot be found, %NULL is returned and
2360 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2361 * event that the MIME type cannot be found, %NULL is returned and
2362 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2364 * Return value: a newly allocated string or %NULL if the specified
2365 * URI cannot be found.
2370 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2376 g_return_val_if_fail (bookmark != NULL, NULL);
2377 g_return_val_if_fail (uri != NULL, NULL);
2379 item = g_bookmark_file_lookup_item (bookmark, uri);
2382 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2383 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2384 _("No bookmark found for URI '%s'"),
2389 if (!item->metadata)
2391 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2392 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2393 _("No MIME type defined in the bookmark for URI '%s'"),
2398 return g_strdup (item->metadata->mime_type);
2402 * g_bookmark_file_set_is_private:
2403 * @bookmark: a #GBookmarkFile
2405 * @is_private: %TRUE if the bookmark should be marked as private
2407 * Sets the private flag of the bookmark for @uri.
2409 * If a bookmark for @uri cannot be found then it is created.
2414 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2416 gboolean is_private)
2420 g_return_if_fail (bookmark != NULL);
2421 g_return_if_fail (uri != NULL);
2423 item = g_bookmark_file_lookup_item (bookmark, uri);
2426 item = bookmark_item_new (uri);
2427 g_bookmark_file_add_item (bookmark, item, NULL);
2430 if (!item->metadata)
2431 item->metadata = bookmark_metadata_new ();
2433 item->metadata->is_private = (is_private == TRUE);
2434 item->modified = time (NULL);
2438 * g_bookmark_file_get_is_private:
2439 * @bookmark: a #GBookmarkFile
2441 * @error: return location for a #GError, or %NULL
2443 * Gets whether the private flag of the bookmark for @uri is set.
2445 * In the event the URI cannot be found, %FALSE is returned and
2446 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2447 * event that the private flag cannot be found, %FALSE is returned and
2448 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2450 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2455 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2461 g_return_val_if_fail (bookmark != NULL, FALSE);
2462 g_return_val_if_fail (uri != NULL, FALSE);
2464 item = g_bookmark_file_lookup_item (bookmark, uri);
2467 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2468 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2469 _("No bookmark found for URI '%s'"),
2474 if (!item->metadata)
2476 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2477 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2478 _("No private flag has been defined in bookmark for URI '%s'"),
2483 return item->metadata->is_private;
2487 * g_bookmark_file_set_added:
2488 * @bookmark: a #GBookmarkFile
2490 * @added: a timestamp or -1 to use the current time
2492 * Sets the time the bookmark for @uri was added into @bookmark.
2494 * If no bookmark for @uri is found then it is created.
2499 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2505 g_return_if_fail (bookmark != NULL);
2506 g_return_if_fail (uri != NULL);
2508 item = g_bookmark_file_lookup_item (bookmark, uri);
2511 item = bookmark_item_new (uri);
2512 g_bookmark_file_add_item (bookmark, item, NULL);
2515 if (added == (time_t) -1)
2518 item->added = added;
2519 item->modified = added;
2523 * g_bookmark_file_get_added:
2524 * @bookmark: a #GBookmarkFile
2526 * @error: return location for a #GError, or %NULL
2528 * Gets the time the bookmark for @uri was added to @bookmark
2530 * In the event the URI cannot be found, -1 is returned and
2531 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2533 * Return value: a timestamp
2538 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2544 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2545 g_return_val_if_fail (uri != NULL, (time_t) -1);
2547 item = g_bookmark_file_lookup_item (bookmark, uri);
2550 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2551 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2552 _("No bookmark found for URI '%s'"),
2561 * g_bookmark_file_set_modified:
2562 * @bookmark: a #GBookmarkFile
2564 * @modified: a timestamp or -1 to use the current time
2566 * Sets the last time the bookmark for @uri was last modified.
2568 * If no bookmark for @uri is found then it is created.
2570 * The "modified" time should only be set when the bookmark's meta-data
2571 * was actually changed. Every function of #GBookmarkFile that
2572 * modifies a bookmark also changes the modification time, except for
2573 * g_bookmark_file_set_visited().
2578 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2584 g_return_if_fail (bookmark != NULL);
2585 g_return_if_fail (uri != NULL);
2587 item = g_bookmark_file_lookup_item (bookmark, uri);
2590 item = bookmark_item_new (uri);
2591 g_bookmark_file_add_item (bookmark, item, NULL);
2594 if (modified == (time_t) -1)
2597 item->modified = modified;
2601 * g_bookmark_file_get_modified:
2602 * @bookmark: a #GBookmarkFile
2604 * @error: return location for a #GError, or %NULL
2606 * Gets the time when the bookmark for @uri was last modified.
2608 * In the event the URI cannot be found, -1 is returned and
2609 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2611 * Return value: a timestamp
2616 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2622 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2623 g_return_val_if_fail (uri != NULL, (time_t) -1);
2625 item = g_bookmark_file_lookup_item (bookmark, uri);
2628 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2629 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2630 _("No bookmark found for URI '%s'"),
2635 return item->modified;
2639 * g_bookmark_file_set_visited:
2640 * @bookmark: a #GBookmarkFile
2642 * @visited: a timestamp or -1 to use the current time
2644 * Sets the time the bookmark for @uri was last visited.
2646 * If no bookmark for @uri is found then it is created.
2648 * The "visited" time should only be set if the bookmark was launched,
2649 * either using the command line retrieved by g_bookmark_file_get_app_info()
2650 * or by the default application for the bookmark's MIME type, retrieved
2651 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2652 * does not affect the "modified" time.
2657 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2663 g_return_if_fail (bookmark != NULL);
2664 g_return_if_fail (uri != NULL);
2666 item = g_bookmark_file_lookup_item (bookmark, uri);
2669 item = bookmark_item_new (uri);
2670 g_bookmark_file_add_item (bookmark, item, NULL);
2673 if (visited == (time_t) -1)
2676 item->visited = visited;
2680 * g_bookmark_file_get_visited:
2681 * @bookmark: a #GBookmarkFile
2683 * @error: return location for a #GError, or %NULL
2685 * Gets the time the bookmark for @uri was last visited.
2687 * In the event the URI cannot be found, -1 is returned and
2688 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2690 * Return value: a timestamp.
2695 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2701 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2702 g_return_val_if_fail (uri != NULL, (time_t) -1);
2704 item = g_bookmark_file_lookup_item (bookmark, uri);
2707 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2708 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2709 _("No bookmark found for URI '%s'"),
2714 return item->visited;
2718 * g_bookmark_file_has_group:
2719 * @bookmark: a #GBookmarkFile
2721 * @group: the group name to be searched
2722 * @error: return location for a #GError, or %NULL
2724 * Checks whether @group appears in the list of groups to which
2725 * the bookmark for @uri belongs to.
2727 * In the event the URI cannot be found, %FALSE is returned and
2728 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2730 * Return value: %TRUE if @group was found.
2735 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2743 g_return_val_if_fail (bookmark != NULL, FALSE);
2744 g_return_val_if_fail (uri != NULL, FALSE);
2746 item = g_bookmark_file_lookup_item (bookmark, uri);
2749 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2750 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2751 _("No bookmark found for URI '%s'"),
2756 if (!item->metadata)
2759 for (l = item->metadata->groups; l != NULL; l = l->next)
2761 if (strcmp (l->data, group) == 0)
2770 * g_bookmark_file_add_group:
2771 * @bookmark: a #GBookmarkFile
2773 * @group: the group name to be added
2775 * Adds @group to the list of groups to which the bookmark for @uri
2778 * If no bookmark for @uri is found then it is created.
2783 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2789 g_return_if_fail (bookmark != NULL);
2790 g_return_if_fail (uri != NULL);
2791 g_return_if_fail (group != NULL && group[0] != '\0');
2793 item = g_bookmark_file_lookup_item (bookmark, uri);
2796 item = bookmark_item_new (uri);
2797 g_bookmark_file_add_item (bookmark, item, NULL);
2800 if (!item->metadata)
2801 item->metadata = bookmark_metadata_new ();
2803 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2805 item->metadata->groups = g_list_prepend (item->metadata->groups,
2808 item->modified = time (NULL);
2813 * g_bookmark_file_remove_group:
2814 * @bookmark: a #GBookmarkFile
2816 * @group: the group name to be removed
2817 * @error: return location for a #GError, or %NULL
2819 * Removes @group from the list of groups to which the bookmark
2820 * for @uri belongs to.
2822 * In the event the URI cannot be found, %FALSE is returned and
2823 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2824 * In the event no group was defined, %FALSE is returned and
2825 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2827 * Return value: %TRUE if @group was successfully removed.
2832 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2840 g_return_val_if_fail (bookmark != NULL, FALSE);
2841 g_return_val_if_fail (uri != NULL, FALSE);
2843 item = g_bookmark_file_lookup_item (bookmark, uri);
2846 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2847 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2848 _("No bookmark found for URI '%s'"),
2853 if (!item->metadata)
2855 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2856 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2857 _("No groups set in bookmark for URI '%s'"),
2862 for (l = item->metadata->groups; l != NULL; l = l->next)
2864 if (strcmp (l->data, group) == 0)
2866 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2870 item->modified = time (NULL);
2880 * g_bookmark_file_set_groups:
2881 * @bookmark: a #GBookmarkFile
2882 * @uri: an item's URI
2883 * @groups: an array of group names, or %NULL to remove all groups
2884 * @length: number of group name values in @groups
2886 * Sets a list of group names for the item with URI @uri. Each previously
2887 * set group name list is removed.
2889 * If @uri cannot be found then an item for it is created.
2894 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2896 const gchar **groups,
2902 g_return_if_fail (bookmark != NULL);
2903 g_return_if_fail (uri != NULL);
2904 g_return_if_fail (groups != NULL);
2906 item = g_bookmark_file_lookup_item (bookmark, uri);
2909 item = bookmark_item_new (uri);
2910 g_bookmark_file_add_item (bookmark, item, NULL);
2913 if (!item->metadata)
2914 item->metadata = bookmark_metadata_new ();
2916 if (item->metadata->groups != NULL)
2918 g_list_foreach (item->metadata->groups,
2921 g_list_free (item->metadata->groups);
2922 item->metadata->groups = NULL;
2927 for (i = 0; groups[i] != NULL && i < length; i++)
2928 item->metadata->groups = g_list_append (item->metadata->groups,
2929 g_strdup (groups[i]));
2932 item->modified = time (NULL);
2936 * g_bookmark_file_get_groups:
2937 * @bookmark: a #GBookmarkFile
2939 * @length: return location for the length of the returned string, or %NULL
2940 * @error: return location for a #GError, or %NULL
2942 * Retrieves the list of group names of the bookmark for @uri.
2944 * In the event the URI cannot be found, %NULL is returned and
2945 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2947 * The returned array is %NULL terminated, so @length may optionally
2950 * Return value: a newly allocated %NULL-terminated array of group names.
2951 * Use g_strfreev() to free it.
2956 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2966 g_return_val_if_fail (bookmark != NULL, NULL);
2967 g_return_val_if_fail (uri != NULL, NULL);
2969 item = g_bookmark_file_lookup_item (bookmark, uri);
2972 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2973 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2974 _("No bookmark found for URI '%s'"),
2979 if (!item->metadata)
2987 len = g_list_length (item->metadata->groups);
2988 retval = g_new0 (gchar *, len + 1);
2989 for (l = g_list_last (item->metadata->groups), i = 0;
2993 gchar *group_name = (gchar *) l->data;
2995 g_warn_if_fail (group_name != NULL);
2997 retval[i++] = g_strdup (group_name);
3008 * g_bookmark_file_add_application:
3009 * @bookmark: a #GBookmarkFile
3011 * @name: the name of the application registering the bookmark
3013 * @exec: command line to be used to launch the bookmark or %NULL
3015 * Adds the application with @name and @exec to the list of
3016 * applications that have registered a bookmark for @uri into
3019 * Every bookmark inside a #GBookmarkFile must have at least an
3020 * application registered. Each application must provide a name, a
3021 * command line useful for launching the bookmark, the number of times
3022 * the bookmark has been registered by the application and the last
3023 * time the application registered this bookmark.
3025 * If @name is %NULL, the name of the application will be the
3026 * same returned by g_get_application(); if @exec is %NULL, the
3027 * command line will be a composition of the program name as
3028 * returned by g_get_prgname() and the "%u" modifier, which will be
3029 * expanded to the bookmark's URI.
3031 * This function will automatically take care of updating the
3032 * registrations count and timestamping in case an application
3033 * with the same @name had already registered a bookmark for
3034 * @uri inside @bookmark.
3036 * If no bookmark for @uri is found, one is created.
3041 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3047 gchar *app_name, *app_exec;
3049 g_return_if_fail (bookmark != NULL);
3050 g_return_if_fail (uri != NULL);
3052 item = g_bookmark_file_lookup_item (bookmark, uri);
3055 item = bookmark_item_new (uri);
3056 g_bookmark_file_add_item (bookmark, item, NULL);
3059 if (name && name[0] != '\0')
3060 app_name = g_strdup (name);
3062 app_name = g_strdup (g_get_application_name ());
3064 if (exec && exec[0] != '\0')
3065 app_exec = g_strdup (exec);
3067 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3069 g_bookmark_file_set_app_info (bookmark, uri,
3081 * g_bookmark_file_remove_application:
3082 * @bookmark: a #GBookmarkFile
3084 * @name: the name of the application
3085 * @error: return location for a #GError or %NULL
3087 * Removes application registered with @name from the list of applications
3088 * that have registered a bookmark for @uri inside @bookmark.
3090 * In the event the URI cannot be found, %FALSE is returned and
3091 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3092 * In the event that no application with name @app_name has registered
3093 * a bookmark for @uri, %FALSE is returned and error is set to
3094 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3096 * Return value: %TRUE if the application was successfully removed.
3101 g_bookmark_file_remove_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);
3114 retval = g_bookmark_file_set_app_info (bookmark, uri,
3122 g_propagate_error (error, set_error);
3131 * g_bookmark_file_has_application:
3132 * @bookmark: a #GBookmarkFile
3134 * @name: the name of the application
3135 * @error: return location for a #GError or %NULL
3137 * Checks whether the bookmark for @uri inside @bookmark has been
3138 * registered by application @name.
3140 * In the event the URI cannot be found, %FALSE is returned and
3141 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3143 * Return value: %TRUE if the application @name was found
3148 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3155 g_return_val_if_fail (bookmark != NULL, FALSE);
3156 g_return_val_if_fail (uri != NULL, FALSE);
3157 g_return_val_if_fail (name != NULL, FALSE);
3159 item = g_bookmark_file_lookup_item (bookmark, uri);
3162 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3163 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3164 _("No bookmark found for URI '%s'"),
3169 return (NULL != bookmark_item_lookup_app_info (item, name));
3173 * g_bookmark_file_set_app_info:
3174 * @bookmark: a #GBookmarkFile
3176 * @name: an application's name
3177 * @exec: an application's command line
3178 * @count: the number of registrations done for this application
3179 * @stamp: the time of the last registration for this application
3180 * @error: return location for a #GError or %NULL
3182 * Sets the meta-data of application @name inside the list of
3183 * applications that have registered a bookmark for @uri inside
3186 * You should rarely use this function; use g_bookmark_file_add_application()
3187 * and g_bookmark_file_remove_application() instead.
3189 * @name can be any UTF-8 encoded string used to identify an
3191 * @exec can have one of these two modifiers: "%f", which will
3192 * be expanded as the local file name retrieved from the bookmark's
3193 * URI; "%u", which will be expanded as the bookmark's URI.
3194 * The expansion is done automatically when retrieving the stored
3195 * command line using the g_bookmark_file_get_app_info() function.
3196 * @count is the number of times the application has registered the
3197 * bookmark; if is < 0, the current registration count will be increased
3198 * by one, if is 0, the application with @name will be removed from
3199 * the list of registered applications.
3200 * @stamp is the Unix time of the last registration; if it is -1, the
3201 * current time will be used.
3203 * If you try to remove an application by setting its registration count to
3204 * zero, and no bookmark for @uri is found, %FALSE is returned and
3205 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3206 * in the event that no application @name has registered a bookmark
3207 * for @uri, %FALSE is returned and error is set to
3208 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3209 * for @uri is found, one is created.
3211 * Return value: %TRUE if the application's meta-data was successfully
3217 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3226 BookmarkAppInfo *ai;
3228 g_return_val_if_fail (bookmark != NULL, FALSE);
3229 g_return_val_if_fail (uri != NULL, FALSE);
3230 g_return_val_if_fail (name != NULL, FALSE);
3231 g_return_val_if_fail (exec != NULL, FALSE);
3233 item = g_bookmark_file_lookup_item (bookmark, uri);
3238 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3239 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3240 _("No bookmark found for URI '%s'"),
3246 item = bookmark_item_new (uri);
3247 g_bookmark_file_add_item (bookmark, item, NULL);
3251 ai = bookmark_item_lookup_app_info (item, name);
3256 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3257 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3258 _("No application with name '%s' registered a bookmark for '%s'"),
3265 ai = bookmark_app_info_new (name);
3267 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3268 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3274 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3275 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3276 bookmark_app_info_free (ai);
3278 item->modified = time (NULL);
3287 if (stamp != (time_t) -1)
3290 ai->stamp = time (NULL);
3292 if (exec && exec[0] != '\0')
3295 ai->exec = g_shell_quote (exec);
3298 item->modified = time (NULL);
3303 /* expands the application's command line */
3305 expand_exec_line (const gchar *exec_fmt,
3311 exec = g_string_sized_new (512);
3312 while ((ch = *exec_fmt++) != '\0')
3316 exec = g_string_append_c (exec, ch);
3327 g_string_append (exec, uri);
3332 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3335 g_string_append (exec, file);
3340 g_string_free (exec, TRUE);
3347 exec = g_string_append_c (exec, ch);
3353 return g_string_free (exec, FALSE);
3357 * g_bookmark_file_get_app_info:
3358 * @bookmark: a #GBookmarkFile
3360 * @name: an application's name
3361 * @exec: location for the command line of the application, or %NULL
3362 * @count: return location for the registration count, or %NULL
3363 * @stamp: return location for the last registration time, or %NULL
3364 * @error: return location for a #GError, or %NULL
3366 * Gets the registration informations of @app_name for the bookmark for
3367 * @uri. See g_bookmark_file_set_app_info() for more informations about
3368 * the returned data.
3370 * The string returned in @app_exec must be freed.
3372 * In the event the URI cannot be found, %FALSE is returned and
3373 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3374 * event that no application with name @app_name has registered a bookmark
3375 * for @uri, %FALSE is returned and error is set to
3376 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3377 * the command line fails, an error of the #G_SHELL_ERROR domain is
3378 * set and %FALSE is returned.
3380 * Return value: %TRUE on success.
3385 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3394 BookmarkAppInfo *ai;
3396 g_return_val_if_fail (bookmark != NULL, FALSE);
3397 g_return_val_if_fail (uri != NULL, FALSE);
3398 g_return_val_if_fail (name != NULL, FALSE);
3400 item = g_bookmark_file_lookup_item (bookmark, uri);
3403 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3404 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3405 _("No bookmark found for URI '%s'"),
3410 ai = bookmark_item_lookup_app_info (item, name);
3413 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3414 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3415 _("No application with name '%s' registered a bookmark for '%s'"),
3423 GError *unquote_error = NULL;
3424 gchar *command_line;
3426 command_line = g_shell_unquote (ai->exec, &unquote_error);
3429 g_propagate_error (error, unquote_error);
3433 *exec = expand_exec_line (command_line, uri);
3436 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3437 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3438 _("Failed to expand exec line '%s' with URI '%s'"),
3440 g_free (command_line);
3445 g_free (command_line);
3458 * g_bookmark_file_get_applications:
3459 * @bookmark: a #GBookmarkFile
3461 * @length: return location of the length of the returned list, or %NULL
3462 * @error: return location for a #GError, or %NULL
3464 * Retrieves the names of the applications that have registered the
3465 * bookmark for @uri.
3467 * In the event the URI cannot be found, %NULL is returned and
3468 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3470 * Return value: a newly allocated %NULL-terminated array of strings.
3471 * Use g_strfreev() to free it.
3476 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3486 g_return_val_if_fail (bookmark != NULL, NULL);
3487 g_return_val_if_fail (uri != NULL, NULL);
3489 item = g_bookmark_file_lookup_item (bookmark, uri);
3492 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3493 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3494 _("No bookmark found for URI '%s'"),
3499 if (!item->metadata)
3507 n_apps = g_list_length (item->metadata->applications);
3508 apps = g_new0 (gchar *, n_apps + 1);
3510 for (l = g_list_last (item->metadata->applications), i = 0;
3514 BookmarkAppInfo *ai;
3516 ai = (BookmarkAppInfo *) l->data;
3518 g_warn_if_fail (ai != NULL);
3519 g_warn_if_fail (ai->name != NULL);
3521 apps[i++] = g_strdup (ai->name);
3532 * g_bookmark_file_get_size:
3533 * @bookmark: a #GBookmarkFile
3535 * Gets the number of bookmarks inside @bookmark.
3537 * Return value: the number of bookmarks
3542 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3544 g_return_val_if_fail (bookmark != NULL, 0);
3546 return g_list_length (bookmark->items);
3550 * g_bookmark_file_move_item:
3551 * @bookmark: a #GBookmarkFile
3552 * @old_uri: a valid URI
3553 * @new_uri: a valid URI, or %NULL
3554 * @error: return location for a #GError or %NULL
3556 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3557 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3558 * %NULL, then the bookmark is removed.
3560 * In the event the URI cannot be found, %FALSE is returned and
3561 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3563 * Return value: %TRUE if the URI was successfully changed
3568 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3569 const gchar *old_uri,
3570 const gchar *new_uri,
3574 GError *remove_error;
3576 g_return_val_if_fail (bookmark != NULL, FALSE);
3577 g_return_val_if_fail (old_uri != NULL, FALSE);
3579 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3582 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3583 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3584 _("No bookmark found for URI '%s'"),
3589 if (new_uri && new_uri[0] != '\0')
3591 if (g_bookmark_file_has_item (bookmark, new_uri))
3593 remove_error = NULL;
3594 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3597 g_propagate_error (error, remove_error);
3603 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3606 item->uri = g_strdup (new_uri);
3607 item->modified = time (NULL);
3609 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3615 remove_error = NULL;
3616 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3619 g_propagate_error (error, remove_error);
3629 * g_bookmark_file_set_icon:
3630 * @bookmark: a #GBookmarkFile
3632 * @href: the URI of the icon for the bookmark, or %NULL
3633 * @mime_type: the MIME type of the icon for the bookmark
3635 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3636 * the currently set icon. @href can either be a full URL for the icon
3637 * file or the icon name following the Icon Naming specification.
3639 * If no bookmark for @uri is found one is created.
3644 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3647 const gchar *mime_type)
3651 g_return_if_fail (bookmark != NULL);
3652 g_return_if_fail (uri != NULL);
3654 item = g_bookmark_file_lookup_item (bookmark, uri);
3657 item = bookmark_item_new (uri);
3658 g_bookmark_file_add_item (bookmark, item, NULL);
3661 if (!item->metadata)
3662 item->metadata = bookmark_metadata_new ();
3664 g_free (item->metadata->icon_href);
3665 g_free (item->metadata->icon_mime);
3667 item->metadata->icon_href = g_strdup (href);
3669 if (mime_type && mime_type[0] != '\0')
3670 item->metadata->icon_mime = g_strdup (mime_type);
3672 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3674 item->modified = time (NULL);
3678 * g_bookmark_file_get_icon:
3679 * @bookmark: a #GBookmarkFile
3681 * @href: return location for the icon's location or %NULL
3682 * @mime_type: return location for the icon's MIME type or %NULL
3683 * @error: return location for a #GError or %NULL
3685 * Gets the icon of the bookmark for @uri.
3687 * In the event the URI cannot be found, %FALSE is returned and
3688 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3690 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3691 * You should free the returned strings.
3696 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3704 g_return_val_if_fail (bookmark != NULL, FALSE);
3705 g_return_val_if_fail (uri != NULL, FALSE);
3707 item = g_bookmark_file_lookup_item (bookmark, uri);
3710 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3711 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3712 _("No bookmark found for URI '%s'"),
3717 if ((!item->metadata) || (!item->metadata->icon_href))
3721 *href = g_strdup (item->metadata->icon_href);
3724 *mime_type = g_strdup (item->metadata->icon_mime);
3729 #define __G_BOOKMARK_FILE_C__
3730 #include "galiasdef.c"