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"
108 #define BOOKMARK_HREF_ATTRIBUTE "href"
109 #define BOOKMARK_TYPE_ATTRIBUTE "type"
111 /* Shared MIME Info entities */
112 #define MIME_NAMESPACE_NAME "mime"
113 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
114 #define MIME_TYPE_ELEMENT "mime-type"
115 #define MIME_TYPE_ATTRIBUTE "type"
118 typedef struct _BookmarkAppInfo BookmarkAppInfo;
119 typedef struct _BookmarkMetadata BookmarkMetadata;
120 typedef struct _BookmarkItem BookmarkItem;
121 typedef struct _ParseData ParseData;
123 struct _BookmarkAppInfo
133 struct _BookmarkMetadata
140 GHashTable *apps_by_name;
145 guint is_private : 1;
159 BookmarkMetadata *metadata;
162 struct _GBookmarkFile
167 /* we store our items in a list and keep a copy inside
168 * an hash table for faster lookup performances
171 GHashTable *items_by_uri;
174 /* parser state machine */
195 static void g_bookmark_file_init (GBookmarkFile *bookmark);
196 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
197 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
201 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
204 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
206 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
210 static time_t timestamp_from_iso8601 (const gchar *iso_date);
211 static gchar * timestamp_to_iso8601 (time_t timestamp);
213 /********************************
216 * Application metadata storage *
217 ********************************/
218 static BookmarkAppInfo *
219 bookmark_app_info_new (const gchar *name)
221 BookmarkAppInfo *retval;
223 g_warn_if_fail (name != NULL);
225 retval = g_slice_new (BookmarkAppInfo);
227 retval->name = g_strdup (name);
230 retval->stamp = time (NULL);
236 bookmark_app_info_free (BookmarkAppInfo *app_info)
241 g_free (app_info->name);
242 g_free (app_info->exec);
244 g_slice_free (BookmarkAppInfo, app_info);
248 bookmark_app_info_dump (BookmarkAppInfo *app_info)
253 g_warn_if_fail (app_info != NULL);
255 if (app_info->count == 0)
258 name = g_markup_escape_text (app_info->name, -1);
259 exec = g_markup_escape_text (app_info->exec, -1);
261 retval = g_strdup_printf (" <%s:%s %s=\"%s\" %s=\"%s\" %s=\"%ld\" %s=\"%u\"/>\n",
262 BOOKMARK_NAMESPACE_NAME,
263 BOOKMARK_APPLICATION_ELEMENT,
264 BOOKMARK_NAME_ATTRIBUTE, name,
265 BOOKMARK_EXEC_ATTRIBUTE, exec,
266 BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
267 BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
276 /***********************
280 ***********************/
281 static BookmarkMetadata *
282 bookmark_metadata_new (void)
284 BookmarkMetadata *retval;
286 retval = g_slice_new (BookmarkMetadata);
288 retval->mime_type = NULL;
290 retval->groups = NULL;
292 retval->applications = NULL;
293 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
298 retval->is_private = FALSE;
300 retval->icon_href = NULL;
301 retval->icon_mime = NULL;
307 bookmark_metadata_free (BookmarkMetadata *metadata)
312 g_free (metadata->mime_type);
314 if (metadata->groups)
316 g_list_foreach (metadata->groups,
319 g_list_free (metadata->groups);
322 if (metadata->applications)
324 g_list_foreach (metadata->applications,
325 (GFunc) bookmark_app_info_free,
327 g_list_free (metadata->applications);
329 g_hash_table_destroy (metadata->apps_by_name);
332 g_free (metadata->icon_href);
333 g_free (metadata->icon_mime);
335 g_slice_free (BookmarkMetadata, metadata);
339 bookmark_metadata_dump (BookmarkMetadata *metadata)
344 if (!metadata->applications)
347 retval = g_string_sized_new (1024);
349 /* metadata container */
350 g_string_append (retval,
352 "<" XBEL_METADATA_ELEMENT
353 " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
357 if (metadata->mime_type) {
358 buffer = g_strconcat (" "
359 "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
360 MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
362 g_string_append (retval, buffer);
366 if (metadata->groups)
370 /* open groups container */
371 g_string_append (retval,
373 "<" BOOKMARK_NAMESPACE_NAME
374 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
376 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
380 group_name = g_markup_escape_text ((gchar *) l->data, -1);
381 buffer = g_strconcat (" "
382 "<" BOOKMARK_NAMESPACE_NAME
383 ":" BOOKMARK_GROUP_ELEMENT ">",
385 "</" BOOKMARK_NAMESPACE_NAME
386 ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
387 g_string_append (retval, buffer);
393 /* close groups container */
394 g_string_append (retval,
396 "</" BOOKMARK_NAMESPACE_NAME
397 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
400 if (metadata->applications)
404 /* open applications container */
405 g_string_append (retval,
407 "<" BOOKMARK_NAMESPACE_NAME
408 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
410 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
412 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
415 g_warn_if_fail (app_info != NULL);
417 app_data = bookmark_app_info_dump (app_info);
421 retval = g_string_append (retval, app_data);
427 /* close applications container */
428 g_string_append (retval,
430 "</" BOOKMARK_NAMESPACE_NAME
431 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
435 if (metadata->icon_href)
437 if (!metadata->icon_mime)
438 metadata->icon_mime = g_strdup ("application/octet-stream");
440 buffer = g_strconcat (" "
441 "<" BOOKMARK_NAMESPACE_NAME
442 ":" BOOKMARK_ICON_ELEMENT
443 " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
444 "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
445 g_string_append (retval, buffer);
451 if (metadata->is_private)
452 g_string_append (retval,
454 "<" BOOKMARK_NAMESPACE_NAME
455 ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
457 /* close metadata container */
458 g_string_append (retval,
460 "</" XBEL_METADATA_ELEMENT ">\n");
462 return g_string_free (retval, FALSE);
465 /******************************************************
468 * Storage for a single bookmark item inside the list *
469 ******************************************************/
470 static BookmarkItem *
471 bookmark_item_new (const gchar *uri)
475 g_warn_if_fail (uri != NULL);
477 item = g_slice_new (BookmarkItem);
478 item->uri = g_strdup (uri);
481 item->description = NULL;
483 item->added = (time_t) -1;
484 item->modified = (time_t) -1;
485 item->visited = (time_t) -1;
487 item->metadata = NULL;
493 bookmark_item_free (BookmarkItem *item)
499 g_free (item->title);
500 g_free (item->description);
503 bookmark_metadata_free (item->metadata);
505 g_slice_free (BookmarkItem, item);
509 bookmark_item_dump (BookmarkItem *item)
512 gchar *added, *visited, *modified;
516 /* at this point, we must have at least a registered application; if we don't
517 * we don't screw up the bookmark file, and just skip this item
519 if (!item->metadata || !item->metadata->applications)
521 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
525 retval = g_string_sized_new (4096);
527 added = timestamp_to_iso8601 (item->added);
528 modified = timestamp_to_iso8601 (item->modified);
529 visited = timestamp_to_iso8601 (item->visited);
531 escaped_uri = g_markup_escape_text (item->uri, -1);
533 buffer = g_strconcat (" <"
534 XBEL_BOOKMARK_ELEMENT
536 XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
537 XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
538 XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
539 XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
542 g_string_append (retval, buffer);
544 g_free (escaped_uri);
552 gchar *escaped_title;
554 escaped_title = g_markup_escape_text (item->title, -1);
555 buffer = g_strconcat (" "
556 "<" XBEL_TITLE_ELEMENT ">",
558 "</" XBEL_TITLE_ELEMENT ">\n",
560 g_string_append (retval, buffer);
562 g_free (escaped_title);
566 if (item->description)
570 escaped_desc = g_markup_escape_text (item->description, -1);
571 buffer = g_strconcat (" "
572 "<" XBEL_DESC_ELEMENT ">",
574 "</" XBEL_DESC_ELEMENT ">\n",
576 g_string_append (retval, buffer);
578 g_free (escaped_desc);
586 metadata = bookmark_metadata_dump (item->metadata);
589 buffer = g_strconcat (" "
590 "<" XBEL_INFO_ELEMENT ">\n",
593 "</" XBEL_INFO_ELEMENT ">\n",
595 retval = g_string_append (retval, buffer);
602 g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
604 return g_string_free (retval, FALSE);
607 static BookmarkAppInfo *
608 bookmark_item_lookup_app_info (BookmarkItem *item,
609 const gchar *app_name)
611 g_warn_if_fail (item != NULL && app_name != NULL);
616 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
619 /*************************
621 *************************/
624 g_bookmark_file_init (GBookmarkFile *bookmark)
626 bookmark->title = NULL;
627 bookmark->description = NULL;
629 bookmark->items = NULL;
630 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
637 g_bookmark_file_clear (GBookmarkFile *bookmark)
639 g_free (bookmark->title);
640 g_free (bookmark->description);
644 g_list_foreach (bookmark->items,
645 (GFunc) bookmark_item_free,
647 g_list_free (bookmark->items);
649 bookmark->items = NULL;
652 if (bookmark->items_by_uri)
654 g_hash_table_destroy (bookmark->items_by_uri);
656 bookmark->items_by_uri = NULL;
664 GHashTable *namespaces;
666 GBookmarkFile *bookmark_file;
667 BookmarkItem *current_item;
671 parse_data_new (void)
675 retval = g_new (ParseData, 1);
677 retval->state = STATE_STARTED;
678 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
679 (GDestroyNotify) g_free,
680 (GDestroyNotify) g_free);
681 retval->bookmark_file = NULL;
682 retval->current_item = NULL;
688 parse_data_free (ParseData *parse_data)
690 g_hash_table_destroy (parse_data->namespaces);
695 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
698 parse_bookmark_element (GMarkupParseContext *context,
699 ParseData *parse_data,
700 const gchar **attribute_names,
701 const gchar **attribute_values,
704 const gchar *uri, *added, *modified, *visited;
710 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
713 uri = added = modified = visited = NULL;
714 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
716 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
717 uri = attribute_values[i];
718 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
719 added = attribute_values[i];
720 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
721 modified = attribute_values[i];
722 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
723 visited = attribute_values[i];
726 g_set_error (error, G_MARKUP_ERROR,
727 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
728 _("Unexpected attribute '%s' for element '%s'"),
730 XBEL_BOOKMARK_ELEMENT);
737 g_set_error (error, G_MARKUP_ERROR,
738 G_MARKUP_ERROR_INVALID_CONTENT,
739 _("Attribute '%s' of element '%s' not found"),
741 XBEL_BOOKMARK_ELEMENT);
745 g_warn_if_fail (parse_data->current_item == NULL);
747 item = bookmark_item_new (uri);
750 item->added = timestamp_from_iso8601 (added);
753 item->modified = timestamp_from_iso8601 (modified);
756 item->visited = timestamp_from_iso8601 (visited);
759 g_bookmark_file_add_item (parse_data->bookmark_file,
764 bookmark_item_free (item);
766 g_propagate_error (error, add_error);
771 parse_data->current_item = item;
775 parse_application_element (GMarkupParseContext *context,
776 ParseData *parse_data,
777 const gchar **attribute_names,
778 const gchar **attribute_values,
781 const gchar *name, *exec, *count, *stamp;
787 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
790 name = exec = count = stamp = NULL;
791 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
793 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
794 name = attribute_values[i];
795 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
796 exec = attribute_values[i];
797 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
798 count = attribute_values[i];
799 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
800 stamp = attribute_values[i];
803 g_set_error (error, G_MARKUP_ERROR,
804 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
805 _("Unexpected attribute '%s' for element '%s'"),
807 BOOKMARK_APPLICATION_ELEMENT);
814 g_set_error (error, G_MARKUP_ERROR,
815 G_MARKUP_ERROR_INVALID_CONTENT,
816 _("Attribute '%s' of element '%s' not found"),
817 BOOKMARK_NAME_ATTRIBUTE,
818 BOOKMARK_APPLICATION_ELEMENT);
824 g_set_error (error, G_MARKUP_ERROR,
825 G_MARKUP_ERROR_INVALID_CONTENT,
826 _("Attribute '%s' of element '%s' not found"),
827 BOOKMARK_EXEC_ATTRIBUTE,
828 BOOKMARK_APPLICATION_ELEMENT);
832 g_warn_if_fail (parse_data->current_item != NULL);
833 item = parse_data->current_item;
835 ai = bookmark_item_lookup_app_info (item, name);
838 ai = bookmark_app_info_new (name);
841 item->metadata = bookmark_metadata_new ();
843 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
844 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
847 ai->exec = g_strdup (exec);
850 ai->count = atoi (count);
855 ai->stamp = (time_t) atol (stamp);
857 ai->stamp = time (NULL);
861 parse_mime_type_element (GMarkupParseContext *context,
862 ParseData *parse_data,
863 const gchar **attribute_names,
864 const gchar **attribute_values,
872 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
876 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
878 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
879 type = attribute_values[i];
882 g_set_error (error, G_MARKUP_ERROR,
883 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
884 _("Unexpected attribute '%s' for element '%s'"),
892 type = "application/octet-stream";
894 g_warn_if_fail (parse_data->current_item != NULL);
895 item = parse_data->current_item;
898 item->metadata = bookmark_metadata_new ();
900 item->metadata->mime_type = g_strdup (type);
904 parse_icon_element (GMarkupParseContext *context,
905 ParseData *parse_data,
906 const gchar **attribute_names,
907 const gchar **attribute_values,
916 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
921 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
923 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
924 href = attribute_values[i];
925 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
926 type = attribute_values[i];
929 g_set_error (error, G_MARKUP_ERROR,
930 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
931 _("Unexpected attribute '%s' for element '%s'"),
933 BOOKMARK_ICON_ELEMENT);
940 g_set_error (error, G_MARKUP_ERROR,
941 G_MARKUP_ERROR_INVALID_CONTENT,
942 _("Attribute '%s' of element '%s' not found"),
943 BOOKMARK_HREF_ATTRIBUTE,
944 BOOKMARK_ICON_ELEMENT);
949 type = "application/octet-stream";
951 g_warn_if_fail (parse_data->current_item != NULL);
952 item = parse_data->current_item;
955 item->metadata = bookmark_metadata_new ();
957 item->metadata->icon_href = g_strdup (href);
958 item->metadata->icon_mime = g_strdup (type);
961 /* scans through the attributes of an element for the "xmlns" pragma, and
962 * adds any resulting namespace declaration to a per-parser hashtable, using
963 * the namespace name as a key for the namespace URI; if no key was found,
964 * the namespace is considered as default, and stored under the "default" key.
966 * FIXME: this works on the assumption that the generator of the XBEL file
967 * is either this code or is smart enough to place the namespace declarations
968 * inside the main root node or inside the metadata node and does not redefine
969 * a namespace inside an inner node; this does *not* conform to the
970 * XML-NS standard, although is a close approximation. In order to make this
971 * conformant to the XML-NS specification we should use a per-element
972 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
975 map_namespace_to_name (ParseData *parse_data,
976 const gchar **attribute_names,
977 const gchar **attribute_values)
982 g_warn_if_fail (parse_data != NULL);
984 if (!attribute_names || !attribute_names[0])
988 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
990 if (g_str_has_prefix (attr, "xmlns"))
992 gchar *namespace_name, *namespace_uri;
995 p = g_utf8_strchr (attr, -1, ':');
997 p = g_utf8_next_char (p);
1001 namespace_name = g_strdup (p);
1002 namespace_uri = g_strdup (attribute_values[i]);
1004 g_hash_table_replace (parse_data->namespaces,
1011 /* checks whether @element_full is equal to @element.
1013 * if @namespace is set, it tries to resolve the namespace to a known URI,
1014 * and if found is prepended to the element name, from which is separated
1015 * using the character specified in the @sep parameter.
1018 is_element_full (ParseData *parse_data,
1019 const gchar *element_full,
1020 const gchar *namespace,
1021 const gchar *element,
1024 gchar *ns_uri, *ns_name;
1025 const gchar *p, *element_name;
1028 g_warn_if_fail (parse_data != NULL);
1029 g_warn_if_fail (element_full != NULL);
1034 /* no namespace requested: dumb element compare */
1036 return (0 == strcmp (element_full, element));
1038 /* search for namespace separator; if none found, assume we are under the
1039 * default namespace, and set ns_name to our "default" marker; if no default
1040 * namespace has been set, just do a plain comparison between @full_element
1043 p = g_utf8_strchr (element_full, -1, ':');
1046 ns_name = g_strndup (element_full, p - element_full);
1047 element_name = g_utf8_next_char (p);
1051 ns_name = g_strdup ("default");
1052 element_name = element_full;
1055 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1058 /* no default namespace found */
1061 return (0 == strcmp (element_full, element));
1064 retval = (0 == strcmp (ns_uri, namespace) &&
1065 0 == strcmp (element_name, element));
1072 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1073 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1076 start_element_raw_cb (GMarkupParseContext *context,
1077 const gchar *element_name,
1078 const gchar **attribute_names,
1079 const gchar **attribute_values,
1083 ParseData *parse_data = (ParseData *) user_data;
1085 /* we must check for namespace declarations first
1087 * XXX - we could speed up things by checking for namespace declarations
1088 * only on the root node, where they usually are; this would probably break
1089 * on streams not produced by us or by "smart" generators
1091 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1093 switch (parse_data->state)
1096 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1102 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1104 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1105 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1106 parse_data->state = STATE_ROOT;
1110 g_set_error (error, G_MARKUP_ERROR,
1111 G_MARKUP_ERROR_INVALID_CONTENT,
1112 _("Unexpected tag '%s', tag '%s' expected"),
1113 element_name, XBEL_ROOT_ELEMENT);
1116 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1117 parse_data->state = STATE_TITLE;
1118 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1119 parse_data->state = STATE_DESC;
1120 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1122 GError *inner_error = NULL;
1124 parse_data->state = STATE_BOOKMARK;
1126 parse_bookmark_element (context,
1132 g_propagate_error (error, inner_error);
1135 g_set_error (error, G_MARKUP_ERROR,
1136 G_MARKUP_ERROR_INVALID_CONTENT,
1137 _("Unexpected tag '%s' inside '%s'"),
1141 case STATE_BOOKMARK:
1142 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1143 parse_data->state = STATE_TITLE;
1144 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1145 parse_data->state = STATE_DESC;
1146 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1147 parse_data->state = STATE_INFO;
1149 g_set_error (error, G_MARKUP_ERROR,
1150 G_MARKUP_ERROR_INVALID_CONTENT,
1151 _("Unexpected tag '%s' inside '%s'"),
1153 XBEL_BOOKMARK_ELEMENT);
1156 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1162 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1164 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1165 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1167 parse_data->state = STATE_METADATA;
1169 if (!parse_data->current_item->metadata)
1170 parse_data->current_item->metadata = bookmark_metadata_new ();
1175 g_set_error (error, G_MARKUP_ERROR,
1176 G_MARKUP_ERROR_INVALID_CONTENT,
1177 _("Unexpected tag '%s', tag '%s' expected"),
1179 XBEL_METADATA_ELEMENT);
1181 case STATE_METADATA:
1182 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1183 parse_data->state = STATE_APPLICATIONS;
1184 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1185 parse_data->state = STATE_GROUPS;
1186 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1187 parse_data->current_item->metadata->is_private = TRUE;
1188 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1190 GError *inner_error = NULL;
1192 parse_data->state = STATE_ICON;
1194 parse_icon_element (context,
1200 g_propagate_error (error, inner_error);
1202 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1204 GError *inner_error = NULL;
1206 parse_data->state = STATE_MIME;
1208 parse_mime_type_element (context,
1214 g_propagate_error (error, inner_error);
1217 g_set_error (error, G_MARKUP_ERROR,
1218 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1219 _("Unexpected tag '%s' inside '%s'"),
1221 XBEL_METADATA_ELEMENT);
1223 case STATE_APPLICATIONS:
1224 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1226 GError *inner_error = NULL;
1228 parse_data->state = STATE_APPLICATION;
1230 parse_application_element (context,
1236 g_propagate_error (error, inner_error);
1239 g_set_error (error, G_MARKUP_ERROR,
1240 G_MARKUP_ERROR_INVALID_CONTENT,
1241 _("Unexpected tag '%s', tag '%s' expected"),
1243 BOOKMARK_APPLICATION_ELEMENT);
1246 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1247 parse_data->state = STATE_GROUP;
1249 g_set_error (error, G_MARKUP_ERROR,
1250 G_MARKUP_ERROR_INVALID_CONTENT,
1251 _("Unexpected tag '%s', tag '%s' expected"),
1253 BOOKMARK_GROUP_ELEMENT);
1256 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1258 GError *inner_error = NULL;
1260 parse_icon_element (context,
1266 g_propagate_error (error, inner_error);
1269 g_set_error (error, G_MARKUP_ERROR,
1270 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1271 _("Unexpected tag '%s' inside '%s'"),
1273 XBEL_METADATA_ELEMENT);
1276 g_warn_if_reached ();
1282 end_element_raw_cb (GMarkupParseContext *context,
1283 const gchar *element_name,
1287 ParseData *parse_data = (ParseData *) user_data;
1289 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1290 parse_data->state = STATE_FINISHED;
1291 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1293 parse_data->current_item = NULL;
1295 parse_data->state = STATE_ROOT;
1297 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1298 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1299 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1301 if (parse_data->current_item)
1302 parse_data->state = STATE_BOOKMARK;
1304 parse_data->state = STATE_ROOT;
1306 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1307 parse_data->state = STATE_INFO;
1308 else if (IS_ELEMENT_NS (parse_data, element_name,
1309 BOOKMARK_NAMESPACE_URI,
1310 BOOKMARK_APPLICATION_ELEMENT))
1311 parse_data->state = STATE_APPLICATIONS;
1312 else if (IS_ELEMENT_NS (parse_data, element_name,
1313 BOOKMARK_NAMESPACE_URI,
1314 BOOKMARK_GROUP_ELEMENT))
1315 parse_data->state = STATE_GROUPS;
1316 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1317 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1318 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1319 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1320 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1321 parse_data->state = STATE_METADATA;
1325 text_raw_cb (GMarkupParseContext *context,
1331 ParseData *parse_data = (ParseData *) user_data;
1334 payload = g_strndup (text, length);
1336 switch (parse_data->state)
1339 if (parse_data->current_item)
1341 g_free (parse_data->current_item->title);
1342 parse_data->current_item->title = g_strdup (payload);
1346 g_free (parse_data->bookmark_file->title);
1347 parse_data->bookmark_file->title = g_strdup (payload);
1351 if (parse_data->current_item)
1353 g_free (parse_data->current_item->description);
1354 parse_data->current_item->description = g_strdup (payload);
1358 g_free (parse_data->bookmark_file->description);
1359 parse_data->bookmark_file->description = g_strdup (payload);
1366 g_warn_if_fail (parse_data->current_item != NULL);
1368 if (!parse_data->current_item->metadata)
1369 parse_data->current_item->metadata = bookmark_metadata_new ();
1371 groups = parse_data->current_item->metadata->groups;
1372 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1376 case STATE_BOOKMARK:
1378 case STATE_METADATA:
1379 case STATE_APPLICATIONS:
1380 case STATE_APPLICATION:
1386 g_warn_if_reached ();
1393 static const GMarkupParser markup_parser =
1395 start_element_raw_cb, /* start_element */
1396 end_element_raw_cb, /* end_element */
1397 text_raw_cb, /* text */
1398 NULL, /* passthrough */
1403 g_bookmark_file_parse (GBookmarkFile *bookmark,
1404 const gchar *buffer,
1408 GMarkupParseContext *context;
1409 ParseData *parse_data;
1410 GError *parse_error, *end_error;
1413 g_warn_if_fail (bookmark != NULL);
1419 length = strlen (buffer);
1421 parse_data = parse_data_new ();
1422 parse_data->bookmark_file = bookmark;
1424 context = g_markup_parse_context_new (&markup_parser,
1427 (GDestroyNotify) parse_data_free);
1430 retval = g_markup_parse_context_parse (context,
1436 g_propagate_error (error, parse_error);
1442 retval = g_markup_parse_context_end_parse (context, &end_error);
1445 g_propagate_error (error, end_error);
1450 g_markup_parse_context_free (context);
1456 g_bookmark_file_dump (GBookmarkFile *bookmark,
1464 retval = g_string_sized_new (4096);
1466 g_string_append (retval,
1467 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1469 /* XXX - do we really need the doctype? */
1470 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1471 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1472 " \"" XBEL_DTD_URI "\">\n"
1474 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1475 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1476 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1478 if (bookmark->title)
1480 gchar *escaped_title;
1482 escaped_title = g_markup_escape_text (bookmark->title, -1);
1484 buffer = g_strconcat (" "
1485 "<" XBEL_TITLE_ELEMENT ">",
1487 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1489 g_string_append (retval, buffer);
1492 g_free (escaped_title);
1495 if (bookmark->description)
1497 gchar *escaped_desc;
1499 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1501 buffer = g_strconcat (" "
1502 "<" XBEL_DESC_ELEMENT ">",
1504 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1505 g_string_append (retval, buffer);
1508 g_free (escaped_desc);
1511 if (!bookmark->items)
1514 retval = g_string_append (retval, "\n");
1516 for (l = g_list_last (bookmark->items);
1520 BookmarkItem *item = (BookmarkItem *) l->data;
1523 item_dump = bookmark_item_dump (item);
1527 retval = g_string_append (retval, item_dump);
1533 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1536 *length = retval->len;
1538 return g_string_free (retval, FALSE);
1545 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1546 * should free the returned string.
1549 timestamp_to_iso8601 (time_t timestamp)
1553 if (timestamp == (time_t) -1)
1554 g_get_current_time (&stamp);
1557 stamp.tv_sec = timestamp;
1561 return g_time_val_to_iso8601 (&stamp);
1565 timestamp_from_iso8601 (const gchar *iso_date)
1569 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1572 return (time_t) stamp.tv_sec;
1578 g_bookmark_file_error_quark (void)
1580 return g_quark_from_static_string ("egg-bookmark-file-error-quark");
1585 /********************
1587 ********************/
1590 * g_bookmark_file_new:
1592 * Creates a new empty #GBookmarkFile object.
1594 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1595 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1598 * Return value: an empty #GBookmarkFile
1603 g_bookmark_file_new (void)
1605 GBookmarkFile *bookmark;
1607 bookmark = g_new (GBookmarkFile, 1);
1609 g_bookmark_file_init (bookmark);
1615 * g_bookmark_file_free:
1616 * @bookmark: a #GBookmarkFile
1618 * Frees a #GBookmarkFile.
1623 g_bookmark_file_free (GBookmarkFile *bookmark)
1628 g_bookmark_file_clear (bookmark);
1634 * g_bookmark_file_load_from_data:
1635 * @bookmark: an empty #GBookmarkFile struct
1636 * @data: desktop bookmarks loaded in memory
1637 * @length: the length of @data in bytes
1638 * @error: return location for a #GError, or %NULL
1640 * Loads a bookmark file from memory into an empty #GBookmarkFile
1641 * structure. If the object cannot be created then @error is set to a
1642 * #GBookmarkFileError.
1644 * Return value: %TRUE if a desktop bookmark could be loaded.
1649 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1654 GError *parse_error;
1657 g_return_val_if_fail (bookmark != NULL, FALSE);
1658 g_return_val_if_fail (data != NULL, FALSE);
1659 g_return_val_if_fail (length != 0, FALSE);
1661 if (length == (gsize) -1)
1662 length = strlen (data);
1664 if (bookmark->items)
1666 g_bookmark_file_clear (bookmark);
1667 g_bookmark_file_init (bookmark);
1671 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1674 g_propagate_error (error, parse_error);
1683 * g_bookmark_file_load_from_file:
1684 * @bookmark: an empty #GBookmarkFile struct
1685 * @filename: the path of a filename to load, in the GLib file name encoding
1686 * @error: return location for a #GError, or %NULL
1688 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1689 * If the file could not be loaded then @error is set to either a #GFileError
1690 * or #GBookmarkFileError.
1692 * Return value: %TRUE if a desktop bookmark file could be loaded
1697 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1698 const gchar *filename,
1706 g_return_val_if_fail (bookmark != NULL, FALSE);
1707 g_return_val_if_fail (filename != NULL, FALSE);
1710 g_file_get_contents (filename, &buffer, &len, &read_error);
1713 g_propagate_error (error, read_error);
1719 retval = g_bookmark_file_load_from_data (bookmark,
1725 g_propagate_error (error, read_error);
1738 /* Iterates through all the directories in *dirs trying to
1739 * find file. When it successfully locates file, returns a
1740 * string its absolute path. It also leaves the unchecked
1741 * directories in *dirs. You should free the returned string
1743 * Adapted from gkeyfile.c
1746 find_file_in_data_dirs (const gchar *file,
1750 gchar **data_dirs, *data_dir, *path;
1759 while (data_dirs && (data_dir = *data_dirs) && !path)
1761 gchar *candidate_file, *sub_dir;
1763 candidate_file = (gchar *) file;
1764 sub_dir = g_strdup ("");
1765 while (candidate_file != NULL && !path)
1769 path = g_build_filename (data_dir, sub_dir,
1770 candidate_file, NULL);
1772 candidate_file = strchr (candidate_file, '-');
1774 if (candidate_file == NULL)
1780 sub_dir = g_strndup (file, candidate_file - file - 1);
1782 for (p = sub_dir; *p != '\0'; p++)
1785 *p = G_DIR_SEPARATOR;
1796 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1797 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1798 _("No valid bookmark file found in data dirs"));
1808 * g_bookmark_file_load_from_data_dirs:
1809 * @bookmark: a #GBookmarkFile
1810 * @file: a relative path to a filename to open and parse
1811 * @full_path: return location for a string containing the full path
1812 * of the file, or %NULL
1813 * @error: return location for a #GError, or %NULL
1815 * This function looks for a desktop bookmark file named @file in the
1816 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1817 * loads the file into @bookmark and returns the file's full path in
1818 * @full_path. If the file could not be loaded then an %error is
1819 * set to either a #GFileError or #GBookmarkFileError.
1821 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1826 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1831 GError *file_error = NULL;
1832 gchar **all_data_dirs, **data_dirs;
1833 const gchar *user_data_dir;
1834 const gchar * const * system_data_dirs;
1837 gboolean found_file;
1839 g_return_val_if_fail (bookmark != NULL, FALSE);
1840 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1842 user_data_dir = g_get_user_data_dir ();
1843 system_data_dirs = g_get_system_data_dirs ();
1844 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1847 all_data_dirs[i++] = g_strdup (user_data_dir);
1850 while (system_data_dirs[j] != NULL)
1851 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1854 data_dirs = all_data_dirs;
1856 while (*data_dirs != NULL && !found_file)
1858 g_free (output_path);
1860 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1864 g_propagate_error (error, file_error);
1868 found_file = g_bookmark_file_load_from_file (bookmark,
1873 g_propagate_error (error, file_error);
1878 if (found_file && full_path)
1879 *full_path = output_path;
1881 g_free (output_path);
1883 g_strfreev (all_data_dirs);
1890 * g_bookmark_file_to_data:
1891 * @bookmark: a #GBookmarkFile
1892 * @length: return location for the length of the returned string, or %NULL
1893 * @error: return location for a #GError, or %NULL
1895 * This function outputs @bookmark as a string.
1897 * Return value: a newly allocated string holding
1898 * the contents of the #GBookmarkFile
1903 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1907 GError *write_error = NULL;
1910 g_return_val_if_fail (bookmark != NULL, NULL);
1912 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1915 g_propagate_error (error, write_error);
1924 * g_bookmark_file_to_file:
1925 * @bookmark: a #GBookmarkFile
1926 * @filename: path of the output file
1927 * @error: return location for a #GError, or %NULL
1929 * This function outputs @bookmark into a file. The write process is
1930 * guaranteed to be atomic by using g_file_set_contents() internally.
1932 * Return value: %TRUE if the file was successfully written.
1937 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1938 const gchar *filename,
1942 GError *data_error, *write_error;
1946 g_return_val_if_fail (bookmark != NULL, FALSE);
1947 g_return_val_if_fail (filename != NULL, FALSE);
1950 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1953 g_propagate_error (error, data_error);
1959 g_file_set_contents (filename, data, len, &write_error);
1962 g_propagate_error (error, write_error);
1974 static BookmarkItem *
1975 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1978 g_warn_if_fail (bookmark != NULL && uri != NULL);
1980 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1983 /* this function adds a new item to the list */
1985 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1989 g_warn_if_fail (bookmark != NULL);
1990 g_warn_if_fail (item != NULL);
1992 /* this should never happen; and if it does, then we are
1993 * screwing up something big time.
1995 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1997 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1998 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1999 _("A bookmark for URI '%s' already exists"),
2004 bookmark->items = g_list_prepend (bookmark->items, item);
2006 g_hash_table_replace (bookmark->items_by_uri,
2010 if (item->added == (time_t) -1)
2011 item->added = time (NULL);
2013 if (item->modified == (time_t) -1)
2014 item->modified = time (NULL);
2018 * g_bookmark_file_remove_item:
2019 * @bookmark: a #GBookmarkFile
2021 * @error: return location for a #GError, or %NULL
2023 * Removes the bookmark for @uri from the bookmark file @bookmark.
2025 * Return value: %TRUE if the bookmark was removed successfully.
2030 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2036 g_return_val_if_fail (bookmark != NULL, FALSE);
2037 g_return_val_if_fail (uri != NULL, FALSE);
2039 item = g_bookmark_file_lookup_item (bookmark, uri);
2043 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2044 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2045 _("No bookmark found for URI '%s'"),
2050 bookmark->items = g_list_remove (bookmark->items, item);
2051 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2053 bookmark_item_free (item);
2059 * g_bookmark_file_has_item:
2060 * @bookmark: a #GBookmarkFile
2063 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2065 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2070 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2073 g_return_val_if_fail (bookmark != NULL, FALSE);
2074 g_return_val_if_fail (uri != NULL, FALSE);
2076 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2080 * g_bookmark_file_get_uris:
2081 * @bookmark: a #GBookmarkFile
2082 * @length: return location for the number of returned URIs, or %NULL
2084 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2085 * The array of returned URIs will be %NULL-terminated, so @length may
2086 * optionally be %NULL.
2088 * Return value: a newly allocated %NULL-terminated array of strings.
2089 * Use g_strfreev() to free it.
2094 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2101 g_return_val_if_fail (bookmark != NULL, NULL);
2103 n_items = g_list_length (bookmark->items);
2104 uris = g_new0 (gchar *, n_items + 1);
2106 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2108 BookmarkItem *item = (BookmarkItem *) l->data;
2110 g_warn_if_fail (item != NULL);
2112 uris[i++] = g_strdup (item->uri);
2123 * g_bookmark_file_set_title:
2124 * @bookmark: a #GBookmarkFile
2125 * @uri: a valid URI or %NULL
2126 * @title: a UTF-8 encoded string
2128 * Sets @title as the title of the bookmark for @uri inside the
2129 * bookmark file @bookmark.
2131 * If @uri is %NULL, the title of @bookmark is set.
2133 * If a bookmark for @uri cannot be found then it is created.
2138 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2142 g_return_if_fail (bookmark != NULL);
2146 g_free (bookmark->title);
2147 bookmark->title = g_strdup (title);
2153 item = g_bookmark_file_lookup_item (bookmark, uri);
2156 item = bookmark_item_new (uri);
2157 g_bookmark_file_add_item (bookmark, item, NULL);
2160 g_free (item->title);
2161 item->title = g_strdup (title);
2163 item->modified = time (NULL);
2168 * g_bookmark_file_get_title:
2169 * @bookmark: a #GBookmarkFile
2170 * @uri: a valid URI or %NULL
2171 * @error: return location for a #GError, or %NULL
2173 * Returns the title of the bookmark for @uri.
2175 * If @uri is %NULL, the title of @bookmark is returned.
2177 * In the event the URI cannot be found, %NULL is returned and
2178 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2180 * Return value: a newly allocated string or %NULL if the specified
2181 * URI cannot be found.
2186 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2192 g_return_val_if_fail (bookmark != NULL, NULL);
2195 return g_strdup (bookmark->title);
2197 item = g_bookmark_file_lookup_item (bookmark, uri);
2200 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2201 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2202 _("No bookmark found for URI '%s'"),
2207 return g_strdup (item->title);
2211 * g_bookmark_file_set_description:
2212 * @bookmark: a #GBookmarkFile
2213 * @uri: a valid URI or %NULL
2214 * @description: a string
2216 * Sets @description as the description of the bookmark for @uri.
2218 * If @uri is %NULL, the description of @bookmark is set.
2220 * If a bookmark for @uri cannot be found then it is created.
2225 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2227 const gchar *description)
2229 g_return_if_fail (bookmark != NULL);
2233 g_free (bookmark->description);
2234 bookmark->description = g_strdup (description);
2240 item = g_bookmark_file_lookup_item (bookmark, uri);
2243 item = bookmark_item_new (uri);
2244 g_bookmark_file_add_item (bookmark, item, NULL);
2247 g_free (item->description);
2248 item->description = g_strdup (description);
2250 item->modified = time (NULL);
2255 * g_bookmark_file_get_description:
2256 * @bookmark: a #GBookmarkFile
2258 * @error: return location for a #GError, or %NULL
2260 * Retrieves the description of the bookmark for @uri.
2262 * In the event the URI cannot be found, %NULL is returned and
2263 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2265 * Return value: a newly allocated string or %NULL if the specified
2266 * URI cannot be found.
2271 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2277 g_return_val_if_fail (bookmark != NULL, NULL);
2280 return g_strdup (bookmark->description);
2282 item = g_bookmark_file_lookup_item (bookmark, uri);
2285 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2286 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2287 _("No bookmark found for URI '%s'"),
2292 return g_strdup (item->description);
2296 * g_bookmark_file_set_mime_type:
2297 * @bookmark: a #GBookmarkFile
2299 * @mime_type: a MIME type
2301 * Sets @mime_type as the MIME type of the bookmark for @uri.
2303 * If a bookmark for @uri cannot be found then it is created.
2308 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2310 const gchar *mime_type)
2314 g_return_if_fail (bookmark != NULL);
2315 g_return_if_fail (uri != NULL);
2316 g_return_if_fail (mime_type != NULL);
2318 item = g_bookmark_file_lookup_item (bookmark, uri);
2321 item = bookmark_item_new (uri);
2322 g_bookmark_file_add_item (bookmark, item, NULL);
2325 if (!item->metadata)
2326 item->metadata = bookmark_metadata_new ();
2328 g_free (item->metadata->mime_type);
2330 item->metadata->mime_type = g_strdup (mime_type);
2331 item->modified = time (NULL);
2335 * g_bookmark_file_get_mime_type:
2336 * @bookmark: a #GBookmarkFile
2338 * @error: return location for a #GError, or %NULL
2340 * Retrieves the MIME type of the resource pointed by @uri.
2342 * In the event the URI cannot be found, %NULL is returned and
2343 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2344 * event that the MIME type cannot be found, %NULL is returned and
2345 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2347 * Return value: a newly allocated string or %NULL if the specified
2348 * URI cannot be found.
2353 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2359 g_return_val_if_fail (bookmark != NULL, NULL);
2360 g_return_val_if_fail (uri != NULL, NULL);
2362 item = g_bookmark_file_lookup_item (bookmark, uri);
2365 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2366 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2367 _("No bookmark found for URI '%s'"),
2372 if (!item->metadata)
2374 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2375 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2376 _("No MIME type defined in the bookmark for URI '%s'"),
2381 return g_strdup (item->metadata->mime_type);
2385 * g_bookmark_file_set_is_private:
2386 * @bookmark: a #GBookmarkFile
2388 * @is_private: %TRUE if the bookmark should be marked as private
2390 * Sets the private flag of the bookmark for @uri.
2392 * If a bookmark for @uri cannot be found then it is created.
2397 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2399 gboolean is_private)
2403 g_return_if_fail (bookmark != NULL);
2404 g_return_if_fail (uri != NULL);
2406 item = g_bookmark_file_lookup_item (bookmark, uri);
2409 item = bookmark_item_new (uri);
2410 g_bookmark_file_add_item (bookmark, item, NULL);
2413 if (!item->metadata)
2414 item->metadata = bookmark_metadata_new ();
2416 item->metadata->is_private = (is_private == TRUE);
2417 item->modified = time (NULL);
2421 * g_bookmark_file_get_is_private:
2422 * @bookmark: a #GBookmarkFile
2424 * @error: return location for a #GError, or %NULL
2426 * Gets whether the private flag of the bookmark for @uri is set.
2428 * In the event the URI cannot be found, %FALSE is returned and
2429 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2430 * event that the private flag cannot be found, %FALSE is returned and
2431 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2433 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2438 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2444 g_return_val_if_fail (bookmark != NULL, FALSE);
2445 g_return_val_if_fail (uri != NULL, FALSE);
2447 item = g_bookmark_file_lookup_item (bookmark, uri);
2450 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2451 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2452 _("No bookmark found for URI '%s'"),
2457 if (!item->metadata)
2459 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2460 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2461 _("No private flag has been defined in bookmark for URI '%s'"),
2466 return item->metadata->is_private;
2470 * g_bookmark_file_set_added:
2471 * @bookmark: a #GBookmarkFile
2473 * @added: a timestamp or -1 to use the current time
2475 * Sets the time the bookmark for @uri was added into @bookmark.
2477 * If no bookmark for @uri is found then it is created.
2482 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2488 g_return_if_fail (bookmark != NULL);
2489 g_return_if_fail (uri != NULL);
2491 item = g_bookmark_file_lookup_item (bookmark, uri);
2494 item = bookmark_item_new (uri);
2495 g_bookmark_file_add_item (bookmark, item, NULL);
2498 if (added == (time_t) -1)
2501 item->added = added;
2502 item->modified = added;
2506 * g_bookmark_file_get_added:
2507 * @bookmark: a #GBookmarkFile
2509 * @error: return location for a #GError, or %NULL
2511 * Gets the time the bookmark for @uri was added to @bookmark
2513 * In the event the URI cannot be found, -1 is returned and
2514 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2516 * Return value: a timestamp
2521 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2527 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2528 g_return_val_if_fail (uri != NULL, (time_t) -1);
2530 item = g_bookmark_file_lookup_item (bookmark, uri);
2533 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2534 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2535 _("No bookmark found for URI '%s'"),
2544 * g_bookmark_file_set_modified:
2545 * @bookmark: a #GBookmarkFile
2547 * @modified: a timestamp or -1 to use the current time
2549 * Sets the last time the bookmark for @uri was last modified.
2551 * If no bookmark for @uri is found then it is created.
2553 * The "modified" time should only be set when the bookmark's meta-data
2554 * was actually changed. Every function of #GBookmarkFile that
2555 * modifies a bookmark also changes the modification time, except for
2556 * g_bookmark_file_set_visited().
2561 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2567 g_return_if_fail (bookmark != NULL);
2568 g_return_if_fail (uri != NULL);
2570 item = g_bookmark_file_lookup_item (bookmark, uri);
2573 item = bookmark_item_new (uri);
2574 g_bookmark_file_add_item (bookmark, item, NULL);
2577 if (modified == (time_t) -1)
2580 item->modified = modified;
2584 * g_bookmark_file_get_modified:
2585 * @bookmark: a #GBookmarkFile
2587 * @error: return location for a #GError, or %NULL
2589 * Gets the time when the bookmark for @uri was last modified.
2591 * In the event the URI cannot be found, -1 is returned and
2592 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2594 * Return value: a timestamp
2599 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2605 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2606 g_return_val_if_fail (uri != NULL, (time_t) -1);
2608 item = g_bookmark_file_lookup_item (bookmark, uri);
2611 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2612 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2613 _("No bookmark found for URI '%s'"),
2618 return item->modified;
2622 * g_bookmark_file_set_visited:
2623 * @bookmark: a #GBookmarkFile
2625 * @visited: a timestamp or -1 to use the current time
2627 * Sets the time the bookmark for @uri was last visited.
2629 * If no bookmark for @uri is found then it is created.
2631 * The "visited" time should only be set if the bookmark was launched,
2632 * either using the command line retrieved by g_bookmark_file_get_app_info()
2633 * or by the default application for the bookmark's MIME type, retrieved
2634 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2635 * does not affect the "modified" time.
2640 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2646 g_return_if_fail (bookmark != NULL);
2647 g_return_if_fail (uri != NULL);
2649 item = g_bookmark_file_lookup_item (bookmark, uri);
2652 item = bookmark_item_new (uri);
2653 g_bookmark_file_add_item (bookmark, item, NULL);
2656 if (visited == (time_t) -1)
2659 item->visited = visited;
2663 * g_bookmark_file_get_visited:
2664 * @bookmark: a #GBookmarkFile
2666 * @error: return location for a #GError, or %NULL
2668 * Gets the time the bookmark for @uri was last visited.
2670 * In the event the URI cannot be found, -1 is returned and
2671 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2673 * Return value: a timestamp.
2678 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2684 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2685 g_return_val_if_fail (uri != NULL, (time_t) -1);
2687 item = g_bookmark_file_lookup_item (bookmark, uri);
2690 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2691 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2692 _("No bookmark found for URI '%s'"),
2697 return item->visited;
2701 * g_bookmark_file_has_group:
2702 * @bookmark: a #GBookmarkFile
2704 * @group: the group name to be searched
2705 * @error: return location for a #GError, or %NULL
2707 * Checks whether @group appears in the list of groups to which
2708 * the bookmark for @uri belongs to.
2710 * In the event the URI cannot be found, %FALSE is returned and
2711 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2713 * Return value: %TRUE if @group was found.
2718 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2726 g_return_val_if_fail (bookmark != NULL, FALSE);
2727 g_return_val_if_fail (uri != NULL, FALSE);
2729 item = g_bookmark_file_lookup_item (bookmark, uri);
2732 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2733 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2734 _("No bookmark found for URI '%s'"),
2739 if (!item->metadata)
2742 for (l = item->metadata->groups; l != NULL; l = l->next)
2744 if (strcmp (l->data, group) == 0)
2753 * g_bookmark_file_add_group:
2754 * @bookmark: a #GBookmarkFile
2756 * @group: the group name to be added
2758 * Adds @group to the list of groups to which the bookmark for @uri
2761 * If no bookmark for @uri is found then it is created.
2766 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2772 g_return_if_fail (bookmark != NULL);
2773 g_return_if_fail (uri != NULL);
2774 g_return_if_fail (group != NULL && group[0] != '\0');
2776 item = g_bookmark_file_lookup_item (bookmark, uri);
2779 item = bookmark_item_new (uri);
2780 g_bookmark_file_add_item (bookmark, item, NULL);
2783 if (!item->metadata)
2784 item->metadata = bookmark_metadata_new ();
2786 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2788 item->metadata->groups = g_list_prepend (item->metadata->groups,
2791 item->modified = time (NULL);
2796 * g_bookmark_file_remove_group:
2797 * @bookmark: a #GBookmarkFile
2799 * @group: the group name to be removed
2800 * @error: return location for a #GError, or %NULL
2802 * Removes @group from the list of groups to which the bookmark
2803 * for @uri belongs to.
2805 * In the event the URI cannot be found, %FALSE is returned and
2806 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2807 * In the event no group was defined, %FALSE is returned and
2808 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2810 * Return value: %TRUE if @group was successfully removed.
2815 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2823 g_return_val_if_fail (bookmark != NULL, FALSE);
2824 g_return_val_if_fail (uri != NULL, FALSE);
2826 item = g_bookmark_file_lookup_item (bookmark, uri);
2829 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2830 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2831 _("No bookmark found for URI '%s'"),
2836 if (!item->metadata)
2838 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2839 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2840 _("No groups set in bookmark for URI '%s'"),
2845 for (l = item->metadata->groups; l != NULL; l = l->next)
2847 if (strcmp (l->data, group) == 0)
2849 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2853 item->modified = time (NULL);
2863 * g_bookmark_file_set_groups:
2864 * @bookmark: a #GBookmarkFile
2865 * @uri: an item's URI
2866 * @groups: an array of group names, or %NULL to remove all groups
2867 * @length: number of group name values in @groups
2869 * Sets a list of group names for the item with URI @uri. Each previously
2870 * set group name list is removed.
2872 * If @uri cannot be found then an item for it is created.
2877 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2879 const gchar **groups,
2885 g_return_if_fail (bookmark != NULL);
2886 g_return_if_fail (uri != NULL);
2887 g_return_if_fail (groups != NULL);
2889 item = g_bookmark_file_lookup_item (bookmark, uri);
2892 item = bookmark_item_new (uri);
2893 g_bookmark_file_add_item (bookmark, item, NULL);
2896 if (!item->metadata)
2897 item->metadata = bookmark_metadata_new ();
2899 if (item->metadata->groups != NULL)
2901 g_list_foreach (item->metadata->groups,
2904 g_list_free (item->metadata->groups);
2905 item->metadata->groups = NULL;
2910 for (i = 0; groups[i] != NULL && i < length; i++)
2911 item->metadata->groups = g_list_append (item->metadata->groups,
2912 g_strdup (groups[i]));
2915 item->modified = time (NULL);
2919 * g_bookmark_file_get_groups:
2920 * @bookmark: a #GBookmarkFile
2922 * @length: return location for the length of the returned string, or %NULL
2923 * @error: return location for a #GError, or %NULL
2925 * Retrieves the list of group names of the bookmark for @uri.
2927 * In the event the URI cannot be found, %NULL is returned and
2928 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2930 * The returned array is %NULL terminated, so @length may optionally
2933 * Return value: a newly allocated %NULL-terminated array of group names.
2934 * Use g_strfreev() to free it.
2939 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2949 g_return_val_if_fail (bookmark != NULL, NULL);
2950 g_return_val_if_fail (uri != NULL, NULL);
2952 item = g_bookmark_file_lookup_item (bookmark, uri);
2955 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2956 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2957 _("No bookmark found for URI '%s'"),
2962 if (!item->metadata)
2970 len = g_list_length (item->metadata->groups);
2971 retval = g_new0 (gchar *, len + 1);
2972 for (l = g_list_last (item->metadata->groups), i = 0;
2976 gchar *group_name = (gchar *) l->data;
2978 g_warn_if_fail (group_name != NULL);
2980 retval[i++] = g_strdup (group_name);
2991 * g_bookmark_file_add_application:
2992 * @bookmark: a #GBookmarkFile
2994 * @name: the name of the application registering the bookmark
2996 * @exec: command line to be used to launch the bookmark or %NULL
2998 * Adds the application with @name and @exec to the list of
2999 * applications that have registered a bookmark for @uri into
3002 * Every bookmark inside a #GBookmarkFile must have at least an
3003 * application registered. Each application must provide a name, a
3004 * command line useful for launching the bookmark, the number of times
3005 * the bookmark has been registered by the application and the last
3006 * time the application registered this bookmark.
3008 * If @name is %NULL, the name of the application will be the
3009 * same returned by g_get_application(); if @exec is %NULL, the
3010 * command line will be a composition of the program name as
3011 * returned by g_get_prgname() and the "%u" modifier, which will be
3012 * expanded to the bookmark's URI.
3014 * This function will automatically take care of updating the
3015 * registrations count and timestamping in case an application
3016 * with the same @name had already registered a bookmark for
3017 * @uri inside @bookmark.
3019 * If no bookmark for @uri is found, one is created.
3024 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3030 gchar *app_name, *app_exec;
3032 g_return_if_fail (bookmark != NULL);
3033 g_return_if_fail (uri != NULL);
3035 item = g_bookmark_file_lookup_item (bookmark, uri);
3038 item = bookmark_item_new (uri);
3039 g_bookmark_file_add_item (bookmark, item, NULL);
3042 if (name && name[0] != '\0')
3043 app_name = g_strdup (name);
3045 app_name = g_strdup (g_get_application_name ());
3047 if (exec && exec[0] != '\0')
3048 app_exec = g_strdup (exec);
3050 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3052 g_bookmark_file_set_app_info (bookmark, uri,
3064 * g_bookmark_file_remove_application:
3065 * @bookmark: a #GBookmarkFile
3067 * @name: the name of the application
3068 * @error: return location for a #GError or %NULL
3070 * Removes application registered with @name from the list of applications
3071 * that have registered a bookmark for @uri inside @bookmark.
3073 * In the event the URI cannot be found, %FALSE is returned and
3074 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3075 * In the event that no application with name @app_name has registered
3076 * a bookmark for @uri, %FALSE is returned and error is set to
3077 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3079 * Return value: %TRUE if the application was successfully removed.
3084 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3092 g_return_val_if_fail (bookmark != NULL, FALSE);
3093 g_return_val_if_fail (uri != NULL, FALSE);
3094 g_return_val_if_fail (name != NULL, FALSE);
3097 retval = g_bookmark_file_set_app_info (bookmark, uri,
3105 g_propagate_error (error, set_error);
3114 * g_bookmark_file_has_application:
3115 * @bookmark: a #GBookmarkFile
3117 * @name: the name of the application
3118 * @error: return location for a #GError or %NULL
3120 * Checks whether the bookmark for @uri inside @bookmark has been
3121 * registered by application @name.
3123 * In the event the URI cannot be found, %FALSE is returned and
3124 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3126 * Return value: %TRUE if the application @name was found
3131 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3138 g_return_val_if_fail (bookmark != NULL, FALSE);
3139 g_return_val_if_fail (uri != NULL, FALSE);
3140 g_return_val_if_fail (name != NULL, FALSE);
3142 item = g_bookmark_file_lookup_item (bookmark, uri);
3145 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3146 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3147 _("No bookmark found for URI '%s'"),
3152 return (NULL != bookmark_item_lookup_app_info (item, name));
3156 * g_bookmark_file_set_app_info:
3157 * @bookmark: a #GBookmarkFile
3159 * @name: an application's name
3160 * @exec: an application's command line
3161 * @count: the number of registrations done for this application
3162 * @stamp: the time of the last registration for this application
3163 * @error: return location for a #GError or %NULL
3165 * Sets the meta-data of application @name inside the list of
3166 * applications that have registered a bookmark for @uri inside
3169 * You should rarely use this function; use g_bookmark_file_add_application()
3170 * and g_bookmark_file_remove_application() instead.
3172 * @name can be any UTF-8 encoded string used to identify an
3174 * @exec can have one of these two modifiers: "%f", which will
3175 * be expanded as the local file name retrieved from the bookmark's
3176 * URI; "%u", which will be expanded as the bookmark's URI.
3177 * The expansion is done automatically when retrieving the stored
3178 * command line using the g_bookmark_file_get_app_info() function.
3179 * @count is the number of times the application has registered the
3180 * bookmark; if is < 0, the current registration count will be increased
3181 * by one, if is 0, the application with @name will be removed from
3182 * the list of registered applications.
3183 * @stamp is the Unix time of the last registration; if it is -1, the
3184 * current time will be used.
3186 * If you try to remove an application by setting its registration count to
3187 * zero, and no bookmark for @uri is found, %FALSE is returned and
3188 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3189 * in the event that no application @name has registered a bookmark
3190 * for @uri, %FALSE is returned and error is set to
3191 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3192 * for @uri is found, one is created.
3194 * Return value: %TRUE if the application's meta-data was successfully
3200 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3209 BookmarkAppInfo *ai;
3211 g_return_val_if_fail (bookmark != NULL, FALSE);
3212 g_return_val_if_fail (uri != NULL, FALSE);
3213 g_return_val_if_fail (name != NULL, FALSE);
3214 g_return_val_if_fail (exec != NULL, FALSE);
3216 item = g_bookmark_file_lookup_item (bookmark, uri);
3221 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3222 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3223 _("No bookmark found for URI '%s'"),
3229 item = bookmark_item_new (uri);
3230 g_bookmark_file_add_item (bookmark, item, NULL);
3234 ai = bookmark_item_lookup_app_info (item, name);
3239 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3240 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3241 _("No application with name '%s' registered a bookmark for '%s'"),
3248 ai = bookmark_app_info_new (name);
3250 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3251 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3257 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3258 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3259 bookmark_app_info_free (ai);
3261 item->modified = time (NULL);
3270 if (stamp != (time_t) -1)
3273 ai->stamp = time (NULL);
3275 if (exec && exec[0] != '\0')
3278 ai->exec = g_shell_quote (exec);
3281 item->modified = time (NULL);
3286 /* expands the application's command line */
3288 expand_exec_line (const gchar *exec_fmt,
3294 exec = g_string_sized_new (512);
3295 while ((ch = *exec_fmt++) != '\0')
3299 exec = g_string_append_c (exec, ch);
3310 g_string_append (exec, uri);
3315 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3318 g_string_append (exec, file);
3323 g_string_free (exec, TRUE);
3330 exec = g_string_append_c (exec, ch);
3336 return g_string_free (exec, FALSE);
3340 * g_bookmark_file_get_app_info:
3341 * @bookmark: a #GBookmarkFile
3343 * @name: an application's name
3344 * @exec: location for the command line of the application, or %NULL
3345 * @count: return location for the registration count, or %NULL
3346 * @stamp: return location for the last registration time, or %NULL
3347 * @error: return location for a #GError, or %NULL
3349 * Gets the registration informations of @app_name for the bookmark for
3350 * @uri. See g_bookmark_file_set_app_info() for more informations about
3351 * the returned data.
3353 * The string returned in @app_exec must be freed.
3355 * In the event the URI cannot be found, %FALSE is returned and
3356 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3357 * event that no application with name @app_name has registered a bookmark
3358 * for @uri, %FALSE is returned and error is set to
3359 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3360 * the command line fails, an error of the #G_SHELL_ERROR domain is
3361 * set and %FALSE is returned.
3363 * Return value: %TRUE on success.
3368 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3377 BookmarkAppInfo *ai;
3379 g_return_val_if_fail (bookmark != NULL, FALSE);
3380 g_return_val_if_fail (uri != NULL, FALSE);
3381 g_return_val_if_fail (name != NULL, FALSE);
3383 item = g_bookmark_file_lookup_item (bookmark, uri);
3386 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3387 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3388 _("No bookmark found for URI '%s'"),
3393 ai = bookmark_item_lookup_app_info (item, name);
3396 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3397 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3398 _("No application with name '%s' registered a bookmark for '%s'"),
3406 GError *unquote_error = NULL;
3407 gchar *command_line;
3409 command_line = g_shell_unquote (ai->exec, &unquote_error);
3412 g_propagate_error (error, unquote_error);
3416 *exec = expand_exec_line (command_line, uri);
3419 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3420 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3421 _("Failed to expand exec line '%s' with URI '%s'"),
3423 g_free (command_line);
3428 g_free (command_line);
3441 * g_bookmark_file_get_applications:
3442 * @bookmark: a #GBookmarkFile
3444 * @length: return location of the length of the returned list, or %NULL
3445 * @error: return location for a #GError, or %NULL
3447 * Retrieves the names of the applications that have registered the
3448 * bookmark for @uri.
3450 * In the event the URI cannot be found, %NULL is returned and
3451 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3453 * Return value: a newly allocated %NULL-terminated array of strings.
3454 * Use g_strfreev() to free it.
3459 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3469 g_return_val_if_fail (bookmark != NULL, NULL);
3470 g_return_val_if_fail (uri != NULL, NULL);
3472 item = g_bookmark_file_lookup_item (bookmark, uri);
3475 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3476 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3477 _("No bookmark found for URI '%s'"),
3482 if (!item->metadata)
3490 n_apps = g_list_length (item->metadata->applications);
3491 apps = g_new0 (gchar *, n_apps + 1);
3493 for (l = g_list_last (item->metadata->applications), i = 0;
3497 BookmarkAppInfo *ai;
3499 ai = (BookmarkAppInfo *) l->data;
3501 g_warn_if_fail (ai != NULL);
3502 g_warn_if_fail (ai->name != NULL);
3504 apps[i++] = g_strdup (ai->name);
3515 * g_bookmark_file_get_size:
3516 * @bookmark: a #GBookmarkFile
3518 * Gets the number of bookmarks inside @bookmark.
3520 * Return value: the number of bookmarks
3525 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3527 g_return_val_if_fail (bookmark != NULL, 0);
3529 return g_list_length (bookmark->items);
3533 * g_bookmark_file_move_item:
3534 * @bookmark: a #GBookmarkFile
3535 * @old_uri: a valid URI
3536 * @new_uri: a valid URI, or %NULL
3537 * @error: return location for a #GError or %NULL
3539 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3540 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3541 * %NULL, then the bookmark is removed.
3543 * In the event the URI cannot be found, %FALSE is returned and
3544 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3546 * Return value: %TRUE if the URI was successfully changed
3551 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3552 const gchar *old_uri,
3553 const gchar *new_uri,
3557 GError *remove_error;
3559 g_return_val_if_fail (bookmark != NULL, FALSE);
3560 g_return_val_if_fail (old_uri != NULL, FALSE);
3562 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3565 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3566 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3567 _("No bookmark found for URI '%s'"),
3572 if (new_uri && new_uri[0] != '\0')
3574 if (g_bookmark_file_has_item (bookmark, new_uri))
3576 remove_error = NULL;
3577 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3580 g_propagate_error (error, remove_error);
3586 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3589 item->uri = g_strdup (new_uri);
3590 item->modified = time (NULL);
3592 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3598 remove_error = NULL;
3599 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3602 g_propagate_error (error, remove_error);
3612 * g_bookmark_file_set_icon:
3613 * @bookmark: a #GBookmarkFile
3615 * @href: the URI of the icon for the bookmark, or %NULL
3616 * @mime_type: the MIME type of the icon for the bookmark
3618 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3619 * the currently set icon.
3621 * If no bookmark for @uri is found it is created.
3626 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3629 const gchar *mime_type)
3633 g_return_if_fail (bookmark != NULL);
3634 g_return_if_fail (uri != NULL);
3636 item = g_bookmark_file_lookup_item (bookmark, uri);
3639 item = bookmark_item_new (uri);
3640 g_bookmark_file_add_item (bookmark, item, NULL);
3643 if (!item->metadata)
3644 item->metadata = bookmark_metadata_new ();
3646 g_free (item->metadata->icon_href);
3647 g_free (item->metadata->icon_mime);
3649 item->metadata->icon_href = g_strdup (href);
3651 if (mime_type && mime_type[0] != '\0')
3652 item->metadata->icon_mime = g_strdup (mime_type);
3654 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3656 item->modified = time (NULL);
3660 * g_bookmark_file_get_icon:
3661 * @bookmark: a #GBookmarkFile
3663 * @href: return location for the icon's location or %NULL
3664 * @mime_type: return location for the icon's MIME type or %NULL
3665 * @error: return location for a #GError or %NULL
3667 * Gets the icon of the bookmark for @uri.
3669 * In the event the URI cannot be found, %FALSE is returned and
3670 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3672 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3673 * You should free the returned strings.
3678 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3686 g_return_val_if_fail (bookmark != NULL, FALSE);
3687 g_return_val_if_fail (uri != NULL, FALSE);
3689 item = g_bookmark_file_lookup_item (bookmark, uri);
3692 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3693 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3694 _("No bookmark found for URI '%s'"),
3699 if ((!item->metadata) || (!item->metadata->icon_href))
3703 *href = g_strdup (item->metadata->icon_href);
3706 *mime_type = g_strdup (item->metadata->icon_mime);
3711 #define __G_BOOKMARK_FILE_C__
3712 #include "galiasdef.c"