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>
42 #include "gfileutils.h"
50 #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_assert (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)
242 g_free (app_info->name);
245 g_free (app_info->exec);
247 g_slice_free (BookmarkAppInfo, app_info);
251 bookmark_app_info_dump (BookmarkAppInfo *app_info)
255 g_assert (app_info != NULL);
257 if (app_info->count == 0)
260 retval = g_strdup_printf (" <%s:%s %s=\"%s\" %s=\"%s\" %s=\"%ld\" %s=\"%u\"/>\n",
261 BOOKMARK_NAMESPACE_NAME,
262 BOOKMARK_APPLICATION_ELEMENT,
263 BOOKMARK_NAME_ATTRIBUTE, app_info->name,
264 BOOKMARK_EXEC_ATTRIBUTE, app_info->exec,
265 BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
266 BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
272 /***********************
276 ***********************/
277 static BookmarkMetadata *
278 bookmark_metadata_new (void)
280 BookmarkMetadata *retval;
282 retval = g_slice_new (BookmarkMetadata);
284 retval->mime_type = NULL;
286 retval->groups = NULL;
288 retval->applications = NULL;
289 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
294 retval->is_private = FALSE;
296 retval->icon_href = NULL;
297 retval->icon_mime = NULL;
303 bookmark_metadata_free (BookmarkMetadata *metadata)
308 if (metadata->mime_type)
309 g_free (metadata->mime_type);
311 if (metadata->groups)
313 g_list_foreach (metadata->groups,
316 g_list_free (metadata->groups);
319 if (metadata->applications)
321 g_list_foreach (metadata->applications,
322 (GFunc) bookmark_app_info_free,
324 g_list_free (metadata->applications);
326 g_hash_table_destroy (metadata->apps_by_name);
329 if (metadata->icon_href)
330 g_free (metadata->icon_href);
332 if (metadata->icon_mime)
333 g_free (metadata->icon_mime);
335 g_slice_free (BookmarkMetadata, metadata);
339 bookmark_metadata_dump (BookmarkMetadata *metadata)
343 if (!metadata->applications)
346 retval = g_string_new (NULL);
348 /* metadata container */
349 g_string_append_printf (retval,
351 XBEL_METADATA_ELEMENT,
352 XBEL_OWNER_ATTRIBUTE, BOOKMARK_METADATA_OWNER);
355 if (metadata->mime_type)
356 g_string_append_printf (retval,
357 " <%s:%s %s=\"%s\"/>\n",
360 MIME_TYPE_ATTRIBUTE, metadata->mime_type);
362 if (metadata->groups)
366 /* open groups container */
367 g_string_append_printf (retval,
369 BOOKMARK_NAMESPACE_NAME,
370 BOOKMARK_GROUPS_ELEMENT);
372 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
374 gchar *group_name = (gchar *) l->data;
376 g_string_append_printf (retval,
377 " <%s:%s>%s</%s:%s>\n",
378 BOOKMARK_NAMESPACE_NAME,
379 BOOKMARK_GROUP_ELEMENT,
381 BOOKMARK_NAMESPACE_NAME,
382 BOOKMARK_GROUP_ELEMENT);
385 /* close groups container */
386 g_string_append_printf (retval,
388 BOOKMARK_NAMESPACE_NAME,
389 BOOKMARK_GROUPS_ELEMENT);
392 if (metadata->applications)
396 /* open applications container */
397 g_string_append_printf (retval,
399 BOOKMARK_NAMESPACE_NAME,
400 BOOKMARK_APPLICATIONS_ELEMENT);
402 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
404 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
407 g_assert (app_info != NULL);
409 app_data = bookmark_app_info_dump (app_info);
413 retval = g_string_append (retval, app_data);
419 /* close applications container */
420 g_string_append_printf (retval,
422 BOOKMARK_NAMESPACE_NAME,
423 BOOKMARK_APPLICATIONS_ELEMENT);
427 if (metadata->icon_href)
429 if (!metadata->icon_mime)
430 metadata->icon_mime = g_strdup ("application/octet-stream");
432 g_string_append_printf (retval,
433 " <%s:%s %s=\"%s\" %s=\"%s\"/>\n",
434 BOOKMARK_NAMESPACE_NAME,
435 BOOKMARK_ICON_ELEMENT,
436 BOOKMARK_HREF_ATTRIBUTE, metadata->icon_href,
437 BOOKMARK_TYPE_ATTRIBUTE, metadata->icon_mime);
441 if (metadata->is_private)
442 g_string_append_printf (retval,
444 BOOKMARK_NAMESPACE_NAME,
445 BOOKMARK_PRIVATE_ELEMENT);
447 /* close metadata container */
448 g_string_append_printf (retval, " </%s>\n", XBEL_METADATA_ELEMENT);
450 return g_string_free (retval, FALSE);
453 /******************************************************
456 * Storage for a single bookmark item inside the list *
457 ******************************************************/
458 static BookmarkItem *
459 bookmark_item_new (const gchar *uri)
463 g_assert (uri != NULL);
465 item = g_slice_new (BookmarkItem);
466 item->uri = g_strdup (uri);
469 item->description = NULL;
471 item->added = (time_t) -1;
472 item->modified = (time_t) -1;
473 item->visited = (time_t) -1;
475 item->metadata = NULL;
481 bookmark_item_free (BookmarkItem *item)
490 g_free (item->title);
492 if (item->description)
493 g_free (item->description);
496 bookmark_metadata_free (item->metadata);
498 g_slice_free (BookmarkItem, item);
502 bookmark_item_dump (BookmarkItem *item)
505 gchar *added, *visited, *modified;
508 /* at this point, we must have at least a registered application; if we don't
509 * we don't screw up the bookmark file, and just skip this item
511 if (!item->metadata || !item->metadata->applications)
513 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
517 retval = g_string_new (NULL);
519 added = timestamp_to_iso8601 (item->added);
520 modified = timestamp_to_iso8601 (item->modified);
521 visited = timestamp_to_iso8601 (item->visited);
523 escaped_uri = g_markup_escape_text (item->uri, strlen (item->uri));
525 g_string_append_printf (retval,
526 " <%s %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\">\n",
527 XBEL_BOOKMARK_ELEMENT,
528 XBEL_HREF_ATTRIBUTE, escaped_uri,
529 XBEL_ADDED_ATTRIBUTE, added,
530 XBEL_MODIFIED_ATTRIBUTE, modified,
531 XBEL_VISITED_ATTRIBUTE, visited);
532 g_free (escaped_uri);
539 gchar *escaped_title;
541 escaped_title = g_markup_escape_text (item->title, strlen (item->title));
542 g_string_append_printf (retval,
547 g_free (escaped_title);
550 if (item->description)
554 escaped_desc = g_markup_escape_text (item->description, strlen (item->description));
555 g_string_append_printf (retval,
560 g_free (escaped_desc);
567 /* open info container */
568 g_string_append_printf (retval, " <%s>\n", XBEL_INFO_ELEMENT);
570 metadata = bookmark_metadata_dump (item->metadata);
573 retval = g_string_append (retval, metadata);
578 /* close info container */
579 g_string_append_printf (retval, " </%s>\n", XBEL_INFO_ELEMENT);
582 g_string_append_printf (retval, " </%s>\n", XBEL_BOOKMARK_ELEMENT);
584 return g_string_free (retval, FALSE);
587 static BookmarkAppInfo *
588 bookmark_item_lookup_app_info (BookmarkItem *item,
589 const gchar *app_name)
591 g_assert (item != NULL && app_name != NULL);
596 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
599 /*************************
601 *************************/
604 g_bookmark_file_init (GBookmarkFile *bookmark)
606 bookmark->title = NULL;
607 bookmark->description = NULL;
609 bookmark->items = NULL;
610 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
617 g_bookmark_file_clear (GBookmarkFile *bookmark)
619 g_free (bookmark->title);
620 g_free (bookmark->description);
624 g_list_foreach (bookmark->items,
625 (GFunc) bookmark_item_free,
627 g_list_free (bookmark->items);
629 bookmark->items = NULL;
632 if (bookmark->items_by_uri)
634 g_hash_table_destroy (bookmark->items_by_uri);
636 bookmark->items_by_uri = NULL;
644 GHashTable *namespaces;
646 GBookmarkFile *bookmark_file;
647 BookmarkItem *current_item;
651 parse_data_new (void)
655 retval = g_new (ParseData, 1);
657 retval->state = STATE_STARTED;
658 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
659 (GDestroyNotify) g_free,
660 (GDestroyNotify) g_free);
661 retval->bookmark_file = NULL;
662 retval->current_item = NULL;
668 parse_data_free (ParseData *parse_data)
670 g_hash_table_destroy (parse_data->namespaces);
675 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
678 parse_bookmark_element (GMarkupParseContext *context,
679 ParseData *parse_data,
680 const gchar **attribute_names,
681 const gchar **attribute_values,
684 const gchar *uri, *added, *modified, *visited;
690 g_assert ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
693 uri = added = modified = visited = NULL;
694 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
696 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
697 uri = attribute_values[i];
698 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
699 added = attribute_values[i];
700 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
701 modified = attribute_values[i];
702 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
703 visited = attribute_values[i];
706 g_set_error (error, G_MARKUP_ERROR,
707 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
708 _("Unexpected attribute '%s' for element '%s'"),
710 XBEL_BOOKMARK_ELEMENT);
717 g_set_error (error, G_MARKUP_ERROR,
718 G_MARKUP_ERROR_INVALID_CONTENT,
719 _("Attribute '%s' of element '%s' not found"),
721 XBEL_BOOKMARK_ELEMENT);
725 g_assert (parse_data->current_item == NULL);
727 item = bookmark_item_new (uri);
730 item->added = timestamp_from_iso8601 (added);
733 item->modified = timestamp_from_iso8601 (modified);
736 item->visited = timestamp_from_iso8601 (visited);
739 g_bookmark_file_add_item (parse_data->bookmark_file,
744 bookmark_item_free (item);
746 g_propagate_error (error, add_error);
751 parse_data->current_item = item;
755 parse_application_element (GMarkupParseContext *context,
756 ParseData *parse_data,
757 const gchar **attribute_names,
758 const gchar **attribute_values,
761 const gchar *name, *exec, *count, *stamp;
767 g_assert ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
770 name = exec = count = stamp = NULL;
771 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
773 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
774 name = attribute_values[i];
775 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
776 exec = attribute_values[i];
777 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
778 count = attribute_values[i];
779 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
780 stamp = attribute_values[i];
783 g_set_error (error, G_MARKUP_ERROR,
784 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
785 _("Unexpected attribute '%s' for element '%s'"),
787 BOOKMARK_APPLICATION_ELEMENT);
794 g_set_error (error, G_MARKUP_ERROR,
795 G_MARKUP_ERROR_INVALID_CONTENT,
796 _("Attribute '%s' of element '%s' not found"),
797 BOOKMARK_NAME_ATTRIBUTE,
798 BOOKMARK_APPLICATION_ELEMENT);
804 g_set_error (error, G_MARKUP_ERROR,
805 G_MARKUP_ERROR_INVALID_CONTENT,
806 _("Attribute '%s' of element '%s' not found"),
807 BOOKMARK_EXEC_ATTRIBUTE,
808 BOOKMARK_APPLICATION_ELEMENT);
812 g_assert (parse_data->current_item != NULL);
813 item = parse_data->current_item;
815 ai = bookmark_item_lookup_app_info (item, name);
818 ai = bookmark_app_info_new (name);
821 item->metadata = bookmark_metadata_new ();
823 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
824 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
827 ai->exec = g_strdup (exec);
830 ai->count = atoi (count);
835 ai->stamp = (time_t) atol (stamp);
837 ai->stamp = time (NULL);
841 parse_mime_type_element (GMarkupParseContext *context,
842 ParseData *parse_data,
843 const gchar **attribute_names,
844 const gchar **attribute_values,
852 g_assert ((parse_data != NULL) && (parse_data->state == STATE_MIME));
856 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
858 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
859 type = attribute_values[i];
862 g_set_error (error, G_MARKUP_ERROR,
863 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
864 _("Unexpected attribute '%s' for element '%s'"),
872 type = "application/octet-stream";
874 g_assert (parse_data->current_item != NULL);
875 item = parse_data->current_item;
878 item->metadata = bookmark_metadata_new ();
880 item->metadata->mime_type = g_strdup (type);
884 parse_icon_element (GMarkupParseContext *context,
885 ParseData *parse_data,
886 const gchar **attribute_names,
887 const gchar **attribute_values,
896 g_assert ((parse_data != NULL) && (parse_data->state == STATE_ICON));
901 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
903 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
904 href = attribute_values[i];
905 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
906 type = attribute_values[i];
909 g_set_error (error, G_MARKUP_ERROR,
910 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
911 _("Unexpected attribute '%s' for element '%s'"),
913 BOOKMARK_ICON_ELEMENT);
920 g_set_error (error, G_MARKUP_ERROR,
921 G_MARKUP_ERROR_INVALID_CONTENT,
922 _("Attribute '%s' of element '%s' not found"),
923 BOOKMARK_HREF_ATTRIBUTE,
924 BOOKMARK_ICON_ELEMENT);
929 type = "application/octet-stream";
931 g_assert (parse_data->current_item != NULL);
932 item = parse_data->current_item;
935 item->metadata = bookmark_metadata_new ();
937 item->metadata->icon_href = g_strdup (href);
938 item->metadata->icon_mime = g_strdup (type);
941 /* scans through the attributes of an element for the "xmlns" pragma, and
942 * adds any resulting namespace declaration to a per-parser hashtable, using
943 * the namespace name as a key for the namespace URI; if no key was found,
944 * the namespace is considered as default, and stored under the "default" key.
946 * FIXME: this works on the assumption that the generator of the XBEL file
947 * is either this code or is smart enough to place the namespace declarations
948 * inside the main root node or inside the metadata node and does not redefine
949 * a namespace inside an inner node; this does *not* conform to the
950 * XML-NS standard, although is a close approximation. In order to make this
951 * conformant to the XML-NS specification we should use a per-element
952 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
955 map_namespace_to_name (ParseData *parse_data,
956 const gchar **attribute_names,
957 const gchar **attribute_values)
962 g_assert (parse_data != NULL);
964 if (!attribute_names || !attribute_names[0])
968 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
970 if (g_str_has_prefix (attr, "xmlns"))
972 gchar *namespace_name, *namespace_uri;
975 p = g_utf8_strchr (attr, -1, ':');
977 p = g_utf8_next_char (p);
981 namespace_name = g_strdup (p);
982 namespace_uri = g_strdup (attribute_values[i]);
984 g_hash_table_replace (parse_data->namespaces,
991 /* checks whether @element_full is equal to @element.
993 * if @namespace is set, it tries to resolve the namespace to a known URI,
994 * and if found is prepended to the element name, from which is separated
995 * using the character specified in the @sep parameter.
998 is_element_full (ParseData *parse_data,
999 const gchar *element_full,
1000 const gchar *namespace,
1001 const gchar *element,
1004 gchar *ns_uri, *ns_name;
1005 gchar *element_name, *resolved;
1009 g_assert (parse_data != NULL);
1010 g_assert (element_full != NULL);
1015 /* no namespace requested: dumb element compare */
1017 return (0 == strcmp (element_full, element));
1019 /* search for namespace separator; if none found, assume we are under the
1020 * default namespace, and set ns_name to our "default" marker; if no default
1021 * namespace has been set, just do a plain comparison between @full_element
1024 p = g_utf8_strchr (element_full, -1, ':');
1026 ns_name = g_strndup (element_full, p - element_full);
1028 ns_name = g_strdup ("default");
1030 ns_uri = g_strdup (g_hash_table_lookup (parse_data->namespaces, ns_name));
1033 /* no default namespace found */
1036 return (0 == strcmp (element_full, element));
1039 p = g_utf8_next_char (p);
1040 element_name = g_strdup (p);
1042 resolved = g_strdup_printf ("%s%c%s", ns_uri, sep, element_name);
1043 s = g_strdup_printf ("%s%c%s", namespace, sep, element);
1044 retval = (0 == strcmp (resolved, s));
1048 g_free (element_name);
1055 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1056 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1059 start_element_raw_cb (GMarkupParseContext *context,
1060 const gchar *element_name,
1061 const gchar **attribute_names,
1062 const gchar **attribute_values,
1066 ParseData *parse_data = (ParseData *) user_data;
1068 /* we must check for namespace declarations first
1070 * XXX - we could speed up things by checking for namespace declarations
1071 * only on the root node, where they usually are; this would probably break
1072 * on streams not produced by us or by "smart" generators
1074 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1076 switch (parse_data->state)
1079 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1085 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1087 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1088 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1089 parse_data->state = STATE_ROOT;
1093 g_set_error (error, G_MARKUP_ERROR,
1094 G_MARKUP_ERROR_INVALID_CONTENT,
1095 _("Unexpected tag '%s', tag '%s' expected"),
1096 element_name, XBEL_ROOT_ELEMENT);
1099 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1100 parse_data->state = STATE_TITLE;
1101 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1102 parse_data->state = STATE_DESC;
1103 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1105 GError *inner_error = NULL;
1107 parse_data->state = STATE_BOOKMARK;
1109 parse_bookmark_element (context,
1115 g_propagate_error (error, inner_error);
1118 g_set_error (error, G_MARKUP_ERROR,
1119 G_MARKUP_ERROR_INVALID_CONTENT,
1120 _("Unexpected tag '%s' inside '%s'"),
1124 case STATE_BOOKMARK:
1125 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1126 parse_data->state = STATE_TITLE;
1127 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1128 parse_data->state = STATE_DESC;
1129 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1130 parse_data->state = STATE_INFO;
1132 g_set_error (error, G_MARKUP_ERROR,
1133 G_MARKUP_ERROR_INVALID_CONTENT,
1134 _("Unexpected tag '%s' inside '%s'"),
1136 XBEL_BOOKMARK_ELEMENT);
1139 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1145 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1147 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1148 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1150 parse_data->state = STATE_METADATA;
1152 if (!parse_data->current_item->metadata)
1153 parse_data->current_item->metadata = bookmark_metadata_new ();
1158 g_set_error (error, G_MARKUP_ERROR,
1159 G_MARKUP_ERROR_INVALID_CONTENT,
1160 _("Unexpected tag '%s', tag '%s' expected"),
1162 XBEL_METADATA_ELEMENT);
1164 case STATE_METADATA:
1165 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1166 parse_data->state = STATE_APPLICATIONS;
1167 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1168 parse_data->state = STATE_GROUPS;
1169 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1170 parse_data->current_item->metadata->is_private = TRUE;
1171 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1173 GError *inner_error = NULL;
1175 parse_data->state = STATE_ICON;
1177 parse_icon_element (context,
1183 g_propagate_error (error, inner_error);
1185 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1187 GError *inner_error = NULL;
1189 parse_data->state = STATE_MIME;
1191 parse_mime_type_element (context,
1197 g_propagate_error (error, inner_error);
1200 g_set_error (error, G_MARKUP_ERROR,
1201 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1202 _("Unexpected tag '%s' inside '%s'"),
1204 XBEL_METADATA_ELEMENT);
1206 case STATE_APPLICATIONS:
1207 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1209 GError *inner_error = NULL;
1211 parse_data->state = STATE_APPLICATION;
1213 parse_application_element (context,
1219 g_propagate_error (error, inner_error);
1222 g_set_error (error, G_MARKUP_ERROR,
1223 G_MARKUP_ERROR_INVALID_CONTENT,
1224 _("Unexpected tag '%s', tag '%s' expected"),
1226 BOOKMARK_APPLICATION_ELEMENT);
1229 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1230 parse_data->state = STATE_GROUP;
1232 g_set_error (error, G_MARKUP_ERROR,
1233 G_MARKUP_ERROR_INVALID_CONTENT,
1234 _("Unexpected tag '%s', tag '%s' expected"),
1236 BOOKMARK_GROUP_ELEMENT);
1239 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1241 GError *inner_error = NULL;
1243 parse_icon_element (context,
1249 g_propagate_error (error, inner_error);
1252 g_set_error (error, G_MARKUP_ERROR,
1253 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1254 _("Unexpected tag '%s' inside '%s'"),
1256 XBEL_METADATA_ELEMENT);
1259 g_assert_not_reached ();
1265 end_element_raw_cb (GMarkupParseContext *context,
1266 const gchar *element_name,
1270 ParseData *parse_data = (ParseData *) user_data;
1272 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1273 parse_data->state = STATE_FINISHED;
1274 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1276 parse_data->current_item = NULL;
1278 parse_data->state = STATE_ROOT;
1280 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1281 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1282 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1284 if (parse_data->current_item)
1285 parse_data->state = STATE_BOOKMARK;
1287 parse_data->state = STATE_ROOT;
1289 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1290 parse_data->state = STATE_INFO;
1291 else if (IS_ELEMENT_NS (parse_data, element_name,
1292 BOOKMARK_NAMESPACE_URI,
1293 BOOKMARK_APPLICATION_ELEMENT))
1294 parse_data->state = STATE_APPLICATIONS;
1295 else if (IS_ELEMENT_NS (parse_data, element_name,
1296 BOOKMARK_NAMESPACE_URI,
1297 BOOKMARK_GROUP_ELEMENT))
1298 parse_data->state = STATE_GROUPS;
1299 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1300 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1301 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1302 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1303 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1304 parse_data->state = STATE_METADATA;
1308 text_raw_cb (GMarkupParseContext *context,
1314 ParseData *parse_data = (ParseData *) user_data;
1317 payload = g_strndup (text, length);
1319 switch (parse_data->state)
1322 if (parse_data->current_item)
1324 g_free (parse_data->current_item->title);
1325 parse_data->current_item->title = g_strdup (payload);
1329 g_free (parse_data->bookmark_file->title);
1330 parse_data->bookmark_file->title = g_strdup (payload);
1334 if (parse_data->current_item)
1336 g_free (parse_data->current_item->description);
1337 parse_data->current_item->description = g_strdup (payload);
1341 g_free (parse_data->bookmark_file->description);
1342 parse_data->bookmark_file->description = g_strdup (payload);
1349 g_assert (parse_data->current_item != NULL);
1351 if (!parse_data->current_item->metadata)
1352 parse_data->current_item->metadata = bookmark_metadata_new ();
1354 groups = parse_data->current_item->metadata->groups;
1355 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1359 case STATE_BOOKMARK:
1361 case STATE_METADATA:
1362 case STATE_APPLICATIONS:
1363 case STATE_APPLICATION:
1369 g_assert_not_reached ();
1376 static GMarkupParser markup_parser =
1378 start_element_raw_cb, /* start_element */
1379 end_element_raw_cb, /* end_element */
1380 text_raw_cb, /* text */
1381 NULL, /* passthrough */
1386 g_bookmark_file_parse (GBookmarkFile *bookmark,
1387 const gchar *buffer,
1391 GMarkupParseContext *context;
1392 ParseData *parse_data;
1393 GError *parse_error, *end_error;
1396 g_assert (bookmark != NULL);
1402 length = strlen (buffer);
1404 parse_data = parse_data_new ();
1405 parse_data->bookmark_file = bookmark;
1407 context = g_markup_parse_context_new (&markup_parser,
1410 (GDestroyNotify) parse_data_free);
1413 retval = g_markup_parse_context_parse (context,
1419 g_propagate_error (error, parse_error);
1425 retval = g_markup_parse_context_end_parse (context, &end_error);
1428 g_propagate_error (error, end_error);
1433 g_markup_parse_context_free (context);
1439 g_bookmark_file_dump (GBookmarkFile *bookmark,
1446 retval = g_string_new (NULL);
1448 g_string_append_printf (retval,
1449 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1451 /* XXX - do we really need the doctype? */
1457 " xmlns:%s=\"%s\"\n"
1458 " xmlns:%s=\"%s\"\n>",
1460 /* XXX - do we really need the doctype? */
1462 XBEL_DTD_SYSTEM, XBEL_DTD_URI,
1465 XBEL_VERSION_ATTRIBUTE, XBEL_VERSION,
1466 BOOKMARK_NAMESPACE_NAME, BOOKMARK_NAMESPACE_URI,
1467 MIME_NAMESPACE_NAME, MIME_NAMESPACE_URI);
1469 if (bookmark->title)
1471 gchar *escaped_title;
1473 escaped_title = g_markup_escape_text (bookmark->title, -1);
1475 g_string_append_printf (retval, " <%s>%s</%s>\n",
1478 XBEL_TITLE_ELEMENT);
1480 g_free (escaped_title);
1483 if (bookmark->description)
1485 gchar *escaped_desc;
1487 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1489 g_string_append_printf (retval, " <%s>%s</%s>\n",
1494 g_free (escaped_desc);
1497 if (!bookmark->items)
1500 retval = g_string_append (retval, "\n");
1502 for (l = g_list_last (bookmark->items);
1506 BookmarkItem *item = (BookmarkItem *) l->data;
1509 item_dump = bookmark_item_dump (item);
1513 retval = g_string_append (retval, item_dump);
1519 g_string_append_printf (retval, "</%s>", XBEL_ROOT_ELEMENT);
1522 *length = retval->len;
1524 return g_string_free (retval, FALSE);
1531 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1532 * should free the returned string.
1535 timestamp_to_iso8601 (time_t timestamp)
1539 if (timestamp == (time_t) -1)
1540 g_get_current_time (&stamp);
1543 stamp.tv_sec = timestamp;
1547 return g_time_val_to_iso8601 (&stamp);
1551 timestamp_from_iso8601 (const gchar *iso_date)
1555 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1558 return (time_t) stamp.tv_sec;
1564 g_bookmark_file_error_quark (void)
1566 static GQuark error_quark = 0;
1568 if (G_UNLIKELY (error_quark == 0))
1569 error_quark = g_quark_from_static_string ("egg-bookmark-file-error-quark");
1576 /********************
1578 ********************/
1581 * g_bookmark_file_new:
1583 * Creates a new empty #GBookmarkFile object.
1585 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1586 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1589 * Return value: an empty #GBookmarkFile
1594 g_bookmark_file_new (void)
1596 GBookmarkFile *bookmark;
1598 bookmark = g_new (GBookmarkFile, 1);
1600 g_bookmark_file_init (bookmark);
1606 * g_bookmark_file_free:
1607 * @bookmark: a #GBookmarkFile
1609 * Frees a #GBookmarkFile.
1614 g_bookmark_file_free (GBookmarkFile *bookmark)
1619 g_bookmark_file_clear (bookmark);
1625 * g_bookmark_file_load_from_data:
1626 * @bookmark: an empty #GBookmarkFile struct
1627 * @data: desktop bookmarks loaded in memory
1628 * @length: the length of @data in bytes
1629 * @error: return location for a #GError, or %NULL
1631 * Loads a bookmark file from memory into an empty #GBookmarkFile
1632 * structure. If the object cannot be created then @error is set to a
1633 * #GBookmarkFileError.
1635 * Return value: %TRUE if a desktop bookmark could be loaded.
1640 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1645 GError *parse_error;
1648 g_return_val_if_fail (bookmark != NULL, FALSE);
1649 g_return_val_if_fail (data != NULL, FALSE);
1650 g_return_val_if_fail (length != 0, FALSE);
1652 if (length == (gsize) -1)
1653 length = strlen (data);
1655 if (bookmark->items)
1657 g_bookmark_file_clear (bookmark);
1658 g_bookmark_file_init (bookmark);
1662 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1665 g_propagate_error (error, parse_error);
1674 * g_bookmark_file_load_from_file:
1675 * @bookmark: an empty #GBookmarkFile struct
1676 * @filename: the path of a filename to load, in the GLib file name encoding
1677 * @error: return location for a #GError, or %NULL
1679 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1680 * If the file could not be loaded then @error is set to either a #GFileError
1681 * or #GBookmarkFileError.
1683 * Return value: %TRUE if a desktop bookmark file could be loaded
1688 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1689 const gchar *filename,
1697 g_return_val_if_fail (bookmark != NULL, FALSE);
1698 g_return_val_if_fail (filename != NULL, FALSE);
1701 g_file_get_contents (filename, &buffer, &len, &read_error);
1704 g_propagate_error (error, read_error);
1710 retval = g_bookmark_file_load_from_data (bookmark,
1716 g_propagate_error (error, read_error);
1729 /* Iterates through all the directories in *dirs trying to
1730 * find file. When it successfully locates file, returns a
1731 * string its absolute path. It also leaves the unchecked
1732 * directories in *dirs. You should free the returned string
1734 * Adapted from gkeyfile.c
1737 find_file_in_data_dirs (const gchar *file,
1741 gchar **data_dirs, *data_dir, *path;
1750 while (data_dirs && (data_dir = *data_dirs) && !path)
1752 gchar *candidate_file, *sub_dir;
1754 candidate_file = (gchar *) file;
1755 sub_dir = g_strdup ("");
1756 while (candidate_file != NULL && !path)
1760 path = g_build_filename (data_dir, sub_dir,
1761 candidate_file, NULL);
1763 candidate_file = strchr (candidate_file, '-');
1765 if (candidate_file == NULL)
1771 sub_dir = g_strndup (file, candidate_file - file - 1);
1773 for (p = sub_dir; *p != '\0'; p++)
1776 *p = G_DIR_SEPARATOR;
1787 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1788 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1789 _("No valid bookmark file was be found in data dirs"));
1799 * g_bookmark_file_load_from_data_dirs:
1800 * @bookmark: a #GBookmarkFile
1801 * @file: a relative path to a filename to open and parse
1802 * @full_path: return location for a string containing the full path
1803 * of the file, or %NULL
1804 * @error: return location for a #GError, or %NULL
1806 * This function looks for a desktop bookmark file named @file in the
1807 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1808 * loads the file into @bookmark and returns the file's full path in
1809 * @full_path. If the file could not be loaded then an %error is
1810 * set to either a #GFileError or #GBookmarkFileError.
1812 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1817 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1822 GError *file_error = NULL;
1823 gchar **all_data_dirs, **data_dirs;
1824 const gchar *user_data_dir;
1825 const gchar * const * system_data_dirs;
1828 gboolean found_file;
1830 g_return_val_if_fail (bookmark != NULL, FALSE);
1831 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1833 user_data_dir = g_get_user_data_dir ();
1834 system_data_dirs = g_get_system_data_dirs ();
1835 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1838 all_data_dirs[i++] = g_strdup (user_data_dir);
1841 while (system_data_dirs[j] != NULL)
1842 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1845 data_dirs = all_data_dirs;
1847 while (*data_dirs != NULL && !found_file)
1849 g_free (output_path);
1851 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1855 g_propagate_error (error, file_error);
1859 found_file = g_bookmark_file_load_from_file (bookmark,
1864 g_propagate_error (error, file_error);
1869 if (found_file && full_path)
1870 *full_path = output_path;
1872 g_free (output_path);
1874 g_strfreev (all_data_dirs);
1881 * g_bookmark_file_to_data:
1882 * @bookmark: a #GBookmarkFile
1883 * @length: return location for the length of the returned string, or %NULL
1884 * @error: return location for a #GError, or %NULL
1886 * This function outputs @bookmark as a string.
1888 * Return value: a newly allocated string holding
1889 * the contents of the #GBookmarkFile
1894 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1898 GError *write_error = NULL;
1901 g_return_val_if_fail (bookmark != NULL, NULL);
1903 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1906 g_propagate_error (error, write_error);
1915 * g_bookmark_file_to_file:
1916 * @bookmark: a #GBookmarkFile
1917 * @filename: path of the output file
1918 * @error: return location for a #GError, or %NULL
1920 * This function outputs @bookmark into a file. The write process is
1921 * guaranteed to be atomic by using g_file_set_contents() internally.
1923 * Return value: %TRUE if the file was successfully written.
1928 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1929 const gchar *filename,
1933 GError *data_error, *write_error;
1937 g_return_val_if_fail (bookmark != NULL, FALSE);
1938 g_return_val_if_fail (filename != NULL, FALSE);
1941 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1944 g_propagate_error (error, data_error);
1950 g_file_set_contents (filename, data, len, &write_error);
1953 g_propagate_error (error, write_error);
1965 static BookmarkItem *
1966 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1969 g_assert (bookmark != NULL && uri != NULL);
1971 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1974 /* this function adds a new item to the list */
1976 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1980 g_assert (bookmark != NULL);
1981 g_assert (item != NULL);
1983 /* this should never happen; and if it does, then we are
1984 * screwing up something big time.
1986 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1988 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1989 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1990 _("A bookmark for URI '%s' already exists"),
1995 bookmark->items = g_list_prepend (bookmark->items, item);
1997 g_hash_table_replace (bookmark->items_by_uri,
2001 if (item->added == (time_t) -1)
2002 item->added = time (NULL);
2004 if (item->modified == (time_t) -1)
2005 item->modified = time (NULL);
2009 * g_bookmark_file_remove_item:
2010 * @bookmark: a #GBookmarkFile
2012 * @error: return location for a #GError, or %NULL
2014 * Removes the bookmark for @uri from the bookmark file @bookmark.
2019 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2025 g_return_if_fail (bookmark != NULL);
2026 g_return_if_fail (uri != NULL);
2028 item = g_bookmark_file_lookup_item (bookmark, uri);
2032 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2033 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2034 _("No bookmark found for URI '%s'"),
2039 bookmark->items = g_list_remove (bookmark->items, item);
2040 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2042 bookmark_item_free (item);
2046 * g_bookmark_file_has_item:
2047 * @bookmark: a #GBookmarkFile
2050 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2052 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2057 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2060 g_return_val_if_fail (bookmark != NULL, FALSE);
2061 g_return_val_if_fail (uri != NULL, FALSE);
2063 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2067 * g_bookmark_file_get_uris:
2068 * @bookmark: a #GBookmarkFile
2069 * @length: return location for the number of returned URIs, or %NULL
2071 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2072 * The array of returned URIs will be %NULL-terminated, so @length may
2073 * optionally be %NULL.
2075 * Return value: a newly allocated %NULL-terminated array of strings.
2076 * Use g_strfreev() to free it.
2081 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2088 g_return_val_if_fail (bookmark != NULL, NULL);
2090 n_items = g_list_length (bookmark->items);
2091 uris = g_new0 (gchar *, n_items + 1);
2093 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2095 BookmarkItem *item = (BookmarkItem *) l->data;
2097 g_assert (item != NULL);
2099 uris[i++] = g_strdup (item->uri);
2110 * g_bookmark_file_set_title:
2111 * @bookmark: a #GBookmarkFile
2112 * @uri: a valid URI or %NULL
2113 * @title: a UTF-8 encoded string
2115 * Sets @title as the title of the bookmark for @uri inside the
2116 * bookmark file @bookmark.
2118 * If @uri is %NULL, the title of @bookmark is set.
2120 * If a bookmark for @uri cannot be found then it is created.
2125 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2129 g_return_if_fail (bookmark != NULL);
2133 g_free (bookmark->title);
2135 if (title && title[0] != '\0')
2136 bookmark->title = g_strdup (title);
2142 item = g_bookmark_file_lookup_item (bookmark, uri);
2145 item = bookmark_item_new (uri);
2146 g_bookmark_file_add_item (bookmark, item, NULL);
2149 g_free (item->title);
2151 if (title && title[0] != '\0')
2152 item->title = g_strdup (title);
2154 item->modified = time (NULL);
2159 * g_bookmark_file_get_title:
2160 * @bookmark: a #GBookmarkFile
2161 * @uri: a valid URI or %NULL
2162 * @error: return location for a #GError, or %NULL
2164 * Returns the title of the bookmark for @uri.
2166 * If @uri is %NULL, the title of @bookmark is returned.
2168 * In the event the URI cannot be found, %NULL is returned and
2169 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2171 * Return value: a newly allocated string or %NULL if the specified
2172 * URI cannot be found.
2177 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2183 g_return_val_if_fail (bookmark != NULL, NULL);
2186 return g_strdup (bookmark->title);
2188 item = g_bookmark_file_lookup_item (bookmark, uri);
2191 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2192 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2193 _("No bookmark found for URI '%s'"),
2198 return g_strdup (item->title);
2202 * g_bookmark_file_set_description:
2203 * @bookmark: a #GBookmarkFile
2204 * @uri: a valid URI or %NULL
2205 * @description: a string
2207 * Sets @description as the description of the bookmark for @uri.
2209 * If @uri is %NULL, the description of @bookmark is set.
2211 * If a bookmark for @uri cannot be found then it is created.
2216 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2218 const gchar *description)
2220 g_return_if_fail (bookmark != NULL);
2224 g_free (bookmark->description);
2226 if (description && description[0] != '\0')
2227 bookmark->description = g_strdup (description);
2233 item = g_bookmark_file_lookup_item (bookmark, uri);
2236 item = bookmark_item_new (uri);
2237 g_bookmark_file_add_item (bookmark, item, NULL);
2240 g_free (item->description);
2242 if (description && description[0] != '\0')
2243 item->description = g_strdup (description);
2245 item->modified = time (NULL);
2250 * g_bookmark_file_get_description:
2251 * @bookmark: a #GBookmarkFile
2253 * @error: return location for a #GError, or %NULL
2255 * Retrieves the description of the bookmark for @uri.
2257 * In the event the URI cannot be found, %NULL is returned and
2258 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2260 * Return value: a newly allocated string or %NULL if the specified
2261 * URI cannot be found.
2266 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2272 g_return_val_if_fail (bookmark != NULL, NULL);
2275 return g_strdup (bookmark->description);
2277 item = g_bookmark_file_lookup_item (bookmark, uri);
2280 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2281 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2282 _("No bookmark found for URI '%s'"),
2287 return g_strdup (item->description);
2291 * g_bookmark_file_set_mime_type:
2292 * @bookmark: a #GBookmarkFile
2294 * @mime_type: a MIME type
2296 * Sets @mime_type as the MIME type of the bookmark for @uri.
2298 * If a bookmark for @uri cannot be found then it is created.
2303 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2305 const gchar *mime_type)
2309 g_return_if_fail (bookmark != NULL);
2310 g_return_if_fail (uri != NULL);
2311 g_return_if_fail (mime_type != NULL);
2313 item = g_bookmark_file_lookup_item (bookmark, uri);
2316 item = bookmark_item_new (uri);
2317 g_bookmark_file_add_item (bookmark, item, NULL);
2320 if (!item->metadata)
2321 item->metadata = bookmark_metadata_new ();
2323 if (item->metadata->mime_type != NULL)
2324 g_free (item->metadata->mime_type);
2326 item->metadata->mime_type = g_strdup (mime_type);
2327 item->modified = time (NULL);
2331 * g_bookmark_file_get_mime_type:
2332 * @bookmark: a #GBookmarkFile
2334 * @error: return location for a #GError, or %NULL
2336 * Retrieves the MIME type of the resource pointed by @uri.
2338 * In the event the URI cannot be found, %NULL is returned and
2339 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2340 * event that the MIME type cannot be found, %NULL is returned and
2341 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2343 * Return value: a newly allocated string or %NULL if the specified
2344 * URI cannot be found.
2349 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2355 g_return_val_if_fail (bookmark != NULL, NULL);
2356 g_return_val_if_fail (uri != NULL, NULL);
2358 item = g_bookmark_file_lookup_item (bookmark, uri);
2361 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2362 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2363 _("No bookmark found for URI '%s'"),
2368 if (!item->metadata)
2370 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2371 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2372 _("No MIME type defined in the bookmark for URI '%s'"),
2377 return g_strdup (item->metadata->mime_type);
2381 * g_bookmark_file_set_is_private:
2382 * @bookmark: a #GBookmarkFile
2384 * @is_private: %TRUE if the bookmark should be marked as private
2386 * Sets the private flag of the bookmark for @uri.
2388 * If a bookmark for @uri cannot be found then it is created.
2393 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2395 gboolean is_private)
2399 g_return_if_fail (bookmark != NULL);
2400 g_return_if_fail (uri != NULL);
2402 item = g_bookmark_file_lookup_item (bookmark, uri);
2405 item = bookmark_item_new (uri);
2406 g_bookmark_file_add_item (bookmark, item, NULL);
2409 if (!item->metadata)
2410 item->metadata = bookmark_metadata_new ();
2412 item->metadata->is_private = (is_private == TRUE);
2413 item->modified = time (NULL);
2417 * g_bookmark_file_get_is_private:
2418 * @bookmark: a #GBookmarkFile
2420 * @error: return location for a #GError, or %NULL
2422 * Gets whether the private flag of the bookmark for @uri is set.
2424 * In the event the URI cannot be found, %FALSE is returned and
2425 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2426 * event that the private flag cannot be found, %FALSE is returned and
2427 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2429 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2434 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2440 g_return_val_if_fail (bookmark != NULL, FALSE);
2441 g_return_val_if_fail (uri != NULL, FALSE);
2443 item = g_bookmark_file_lookup_item (bookmark, uri);
2446 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2447 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2448 _("No bookmark found for URI '%s'"),
2453 if (!item->metadata)
2455 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2456 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2457 _("No private flag has been defined in bookmark for URI '%s'"),
2462 return item->metadata->is_private;
2466 * g_bookmark_file_set_added:
2467 * @bookmark: a #GBookmarkFile
2469 * @added: a timestamp or -1 to use the current time
2471 * Sets the time the bookmark for @uri was added into @bookmark.
2473 * If no bookmark for @uri is found then it is created.
2478 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2484 g_return_if_fail (bookmark != NULL);
2485 g_return_if_fail (uri != NULL);
2487 item = g_bookmark_file_lookup_item (bookmark, uri);
2490 item = bookmark_item_new (uri);
2491 g_bookmark_file_add_item (bookmark, item, NULL);
2494 if (added == (time_t) -1)
2497 item->added = added;
2498 item->modified = added;
2502 * g_bookmark_file_get_added:
2503 * @bookmark: a #GBookmarkFile
2505 * @error: return location for a #GError, or %NULL
2507 * Gets the time the bookmark for @uri was added to @bookmark
2509 * In the event the URI cannot be found, -1 is returned and
2510 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2512 * Return value: a timestamp
2517 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2523 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2524 g_return_val_if_fail (uri != NULL, (time_t) -1);
2526 item = g_bookmark_file_lookup_item (bookmark, uri);
2529 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2530 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2531 _("No bookmark found for URI '%s'"),
2540 * g_bookmark_file_set_modified:
2541 * @bookmark: a #GBookmarkFile
2543 * @modified: a timestamp or -1 to use the current time
2545 * Sets the last time the bookmark for @uri was last modified.
2547 * If no bookmark for @uri is found then it is created.
2549 * The "modified" time should only be set when the bookmark's meta-data
2550 * was actually changed. Every function of #GBookmarkFile that
2551 * modifies a bookmark also changes the modification time, except for
2552 * g_bookmark_file_set_visited().
2557 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2563 g_return_if_fail (bookmark != NULL);
2564 g_return_if_fail (uri != NULL);
2566 item = g_bookmark_file_lookup_item (bookmark, uri);
2569 item = bookmark_item_new (uri);
2570 g_bookmark_file_add_item (bookmark, item, NULL);
2573 if (modified == (time_t) -1)
2576 item->modified = modified;
2580 * g_bookmark_file_get_modified:
2581 * @bookmark: a #GBookmarkFile
2583 * @error: return location for a #GError, or %NULL
2585 * Gets the time when the bookmark for @uri was last modified.
2587 * In the event the URI cannot be found, -1 is returned and
2588 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2590 * Return value: a timestamp
2595 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2601 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2602 g_return_val_if_fail (uri != NULL, (time_t) -1);
2604 item = g_bookmark_file_lookup_item (bookmark, uri);
2607 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2608 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2609 _("No bookmark found for URI '%s'"),
2614 return item->modified;
2618 * g_bookmark_file_set_visited:
2619 * @bookmark: a #GBookmarkFile
2621 * @visited: a timestamp or -1 to use the current time
2623 * Sets the time the bookmark for @uri was last visited.
2625 * If no bookmark for @uri is found then it is created.
2627 * The "visited" time should only be set if the bookmark was launched,
2628 * either using the command line retrieved by g_bookmark_file_get_app_info()
2629 * or by the default application for the bookmark's MIME type, retrieved
2630 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2631 * does not affect the "modified" time.
2636 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2642 g_return_if_fail (bookmark != NULL);
2643 g_return_if_fail (uri != NULL);
2645 item = g_bookmark_file_lookup_item (bookmark, uri);
2648 item = bookmark_item_new (uri);
2649 g_bookmark_file_add_item (bookmark, item, NULL);
2652 if (visited == (time_t) -1)
2655 item->visited = visited;
2659 * g_bookmark_file_get_visited:
2660 * @bookmark: a #GBookmarkFile
2662 * @error: return location for a #GError, or %NULL
2664 * Gets the time the bookmark for @uri was last visited.
2666 * In the event the URI cannot be found, -1 is returned and
2667 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2669 * Return value: a timestamp.
2674 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2680 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2681 g_return_val_if_fail (uri != NULL, (time_t) -1);
2683 item = g_bookmark_file_lookup_item (bookmark, uri);
2686 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2687 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2688 _("No bookmark found for URI '%s'"),
2693 return item->visited;
2697 * g_bookmark_file_has_group:
2698 * @bookmark: a #GBookmarkFile
2700 * @group: the group name to be searched
2701 * @error: return location for a #GError, or %NULL
2703 * Checks whether @group appears in the list of groups to which
2704 * the bookmark for @uri belongs to.
2706 * In the event the URI cannot be found, %FALSE is returned and
2707 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2709 * Return value: %TRUE if @group was found.
2714 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2722 g_return_val_if_fail (bookmark != NULL, FALSE);
2723 g_return_val_if_fail (uri != NULL, FALSE);
2725 item = g_bookmark_file_lookup_item (bookmark, uri);
2728 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2729 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2730 _("No bookmark found for URI '%s'"),
2735 if (!item->metadata)
2738 for (l = item->metadata->groups; l != NULL; l = l->next)
2740 if (strcmp (l->data, group) == 0)
2749 * g_bookmark_file_add_group:
2750 * @bookmark: a #GBookmarkFile
2752 * @group: the group name to be added
2754 * Adds @group to the list of groups to which the bookmark for @uri
2757 * If no bookmark for @uri is found then it is created.
2762 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2768 g_return_if_fail (bookmark != NULL);
2769 g_return_if_fail (uri != NULL);
2770 g_return_if_fail (group != NULL && group[0] != '\0');
2772 item = g_bookmark_file_lookup_item (bookmark, uri);
2775 item = bookmark_item_new (uri);
2776 g_bookmark_file_add_item (bookmark, item, NULL);
2779 if (!item->metadata)
2780 item->metadata = bookmark_metadata_new ();
2782 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2784 item->metadata->groups = g_list_prepend (item->metadata->groups,
2787 item->modified = time (NULL);
2792 * g_bookmark_file_remove_group:
2793 * @bookmark: a #GBookmarkFile
2795 * @group: the group name to be removed
2796 * @error: return location for a #GError, or %NULL
2798 * Removes @group from the list of groups to which the bookmark
2799 * for @uri belongs to.
2801 * In the event the URI cannot be found, %FALSE is returned and
2802 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2803 * In the event no group was defined, %FALSE is returned and
2804 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2806 * Return value: %TRUE if @group was successfully removed.
2811 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2819 g_return_val_if_fail (bookmark != NULL, FALSE);
2820 g_return_val_if_fail (uri != NULL, FALSE);
2822 item = g_bookmark_file_lookup_item (bookmark, uri);
2825 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2826 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2827 _("No bookmark found for URI '%s'"),
2832 if (!item->metadata)
2834 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2835 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2836 _("No groups set in bookmark for URI '%s'"),
2841 for (l = item->metadata->groups; l != NULL; l = l->next)
2843 if (strcmp (l->data, group) == 0)
2845 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2848 item->modified = time (NULL);
2858 * g_bookmark_file_set_groups:
2859 * @bookmark: a #GBookmarkFile
2860 * @uri: an item's URI
2861 * @groups: an array of group names, or %NULL to remove all groups
2862 * @length: number of group name values in @groups
2864 * Sets a list of group names for the item with URI @uri. Each previously
2865 * set group name list is removed.
2867 * If @uri cannot be found then an item for it is created.
2872 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2874 const gchar **groups,
2880 g_return_if_fail (bookmark != NULL);
2881 g_return_if_fail (uri != NULL);
2882 g_return_if_fail (groups != NULL);
2884 item = g_bookmark_file_lookup_item (bookmark, uri);
2887 item = bookmark_item_new (uri);
2888 g_bookmark_file_add_item (bookmark, item, NULL);
2891 if (!item->metadata)
2892 item->metadata = bookmark_metadata_new ();
2894 if (item->metadata->groups != NULL)
2896 g_list_foreach (item->metadata->groups,
2899 g_list_free (item->metadata->groups);
2900 item->metadata->groups = NULL;
2905 for (i = 0; groups[i] != NULL && i < length; i++)
2906 item->metadata->groups = g_list_append (item->metadata->groups,
2907 g_strdup (groups[i]));
2910 item->modified = time (NULL);
2914 * g_bookmark_file_get_groups:
2915 * @bookmark: a #GBookmarkFile
2917 * @length: return location for the length of the returned string, or %NULL
2918 * @error: return location for a #GError, or %NULL
2920 * Retrieves the list of group names of the bookmark for @uri.
2922 * In the event the URI cannot be found, %NULL is returned and
2923 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2925 * The returned array is %NULL terminated, so @length may optionally
2928 * Return value: a newly allocated %NULL-terminated array of group names.
2929 * Use g_strfreev() to free it.
2932 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2942 g_return_val_if_fail (bookmark != NULL, NULL);
2943 g_return_val_if_fail (uri != NULL, NULL);
2945 item = g_bookmark_file_lookup_item (bookmark, uri);
2948 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2949 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2950 _("No bookmark found for URI '%s'"),
2955 if (!item->metadata)
2963 len = g_list_length (item->metadata->groups);
2964 retval = g_new0 (gchar *, len + 1);
2965 for (l = g_list_last (item->metadata->groups), i = 0;
2969 gchar *group_name = (gchar *) l->data;
2971 g_assert (group_name != NULL);
2973 retval[i++] = g_strdup (group_name);
2984 * g_bookmark_file_add_application:
2985 * @bookmark: a #GBookmarkFile
2987 * @name: the name of the application registering the bookmark
2989 * @exec: command line to be used to launch the bookmark or %NULL
2991 * Adds the application with @name and @exec to the list of
2992 * applications that have registered a bookmark for @uri into
2995 * Every bookmark inside a #GBookmarkFile must have at least an
2996 * application registered. Each application must provide a name, a
2997 * command line useful for launching the bookmark, the number of times
2998 * the bookmark has been registered by the application and the last
2999 * time the application registered this bookmark.
3001 * If @name is %NULL, the name of the application will be the
3002 * same returned by g_get_application(); if @exec is %NULL, the
3003 * command line will be a composition of the program name as
3004 * returned by g_get_prgname() and the "%u" modifier, which will be
3005 * expanded to the bookmark's URI.
3007 * This function will automatically take care of updating the
3008 * registrations count and timestamping in case an application
3009 * with the same @name had already registered a bookmark for
3010 * @uri inside @bookmark.
3012 * If no bookmark for @uri is found, one is created.
3017 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3023 gchar *app_name, *app_exec;
3025 g_return_if_fail (bookmark != NULL);
3026 g_return_if_fail (uri != NULL);
3028 item = g_bookmark_file_lookup_item (bookmark, uri);
3031 item = bookmark_item_new (uri);
3032 g_bookmark_file_add_item (bookmark, item, NULL);
3035 if (name && name[0] != '\0')
3036 app_name = g_strdup (name);
3038 app_name = g_strdup (g_get_application_name ());
3040 if (exec && exec[0] != '\0')
3041 app_exec = g_strdup (exec);
3043 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3045 g_bookmark_file_set_app_info (bookmark, uri,
3057 * g_bookmark_file_remove_application:
3058 * @bookmark: a #GBookmarkFile
3060 * @name: the name of the application
3061 * @error: return location for a #GError or %NULL
3063 * Removes application registered with @name from the list of applications
3064 * that have registered a bookmark for @uri inside @bookmark.
3066 * In the event the URI cannot be found, %FALSE is returned and
3067 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3068 * In the event that no application with name @app_name has registered
3069 * a bookmark for @uri, %FALSE is returned and error is set to
3070 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3072 * Return value: %TRUE if the application was successfully removed.
3077 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3085 g_return_val_if_fail (bookmark != NULL, FALSE);
3086 g_return_val_if_fail (uri != NULL, FALSE);
3087 g_return_val_if_fail (name != NULL, FALSE);
3090 retval = g_bookmark_file_set_app_info (bookmark, uri,
3098 g_propagate_error (error, set_error);
3107 * g_bookmark_file_has_application:
3108 * @bookmark: a #GBookmarkFile
3110 * @name: the name of the application
3111 * @error: return location for a #GError or %NULL
3113 * Checks whether the bookmark for @uri inside @bookmark has been
3114 * registered by application @name.
3116 * In the event the URI cannot be found, %FALSE is returned and
3117 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3119 * Return value: %TRUE if the application @name was found
3124 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3131 g_return_val_if_fail (bookmark != NULL, FALSE);
3132 g_return_val_if_fail (uri != NULL, FALSE);
3133 g_return_val_if_fail (name != NULL, FALSE);
3135 item = g_bookmark_file_lookup_item (bookmark, uri);
3138 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3139 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3140 _("No bookmark found for URI '%s'"),
3145 return (NULL != bookmark_item_lookup_app_info (item, name));
3149 * g_bookmark_file_set_app_info:
3150 * @bookmark: a #GBookmarkFile
3152 * @name: an application's name
3153 * @exec: an application's command line
3154 * @count: the number of registrations done for this application
3155 * @stamp: the time of the last registration for this application
3156 * @error: return location for a #GError or %NULL
3158 * Sets the meta-data of application @name inside the list of
3159 * applications that have registered a bookmark for @uri inside
3162 * You should rarely use this function; use g_bookmark_file_add_application()
3163 * and g_bookmark_file_remove_application() instead.
3165 * @name can be any UTF-8 encoded string used to identify an
3167 * @exec can have one of these two modifiers: "%f", which will
3168 * be expanded as the local file name retrieved from the bookmark's
3169 * URI; "%u", which will be expanded as the bookmark's URI.
3170 * The expansion is done automatically when retrieving the stored
3171 * command line using the g_bookmark_file_get_app_info() function.
3172 * @count is the number of times the application has registered the
3173 * bookmark; if is < 0, the current registration count will be increased
3174 * by one, if is 0, the application with @name will be removed from
3175 * the list of registered applications.
3176 * @stamp is the Unix time of the last registration; if it is -1, the
3177 * current time will be used.
3179 * If you try to remove an application by setting its registration count to
3180 * zero, and no bookmark for @uri is found, %FALSE is returned and
3181 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3182 * in the event that no application @name has registered a bookmark
3183 * for @uri, %FALSE is returned and error is set to
3184 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3185 * for @uri is found, one is created.
3187 * Return value: %TRUE if the application's meta-data was successfully
3193 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3202 BookmarkAppInfo *ai;
3204 g_return_val_if_fail (bookmark != NULL, FALSE);
3205 g_return_val_if_fail (uri != NULL, FALSE);
3206 g_return_val_if_fail (name != NULL, FALSE);
3207 g_return_val_if_fail (exec != NULL, FALSE);
3209 item = g_bookmark_file_lookup_item (bookmark, uri);
3214 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3215 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3216 _("No bookmark found for URI '%s'"),
3222 item = bookmark_item_new (uri);
3223 g_bookmark_file_add_item (bookmark, item, NULL);
3227 ai = bookmark_item_lookup_app_info (item, name);
3232 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3233 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3234 _("No application with name '%s' registered a bookmark for '%s'"),
3241 ai = bookmark_app_info_new (name);
3243 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3244 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3250 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3251 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3253 item->modified = time (NULL);
3262 if (stamp != (time_t) -1)
3265 ai->stamp = time (NULL);
3267 if (exec && exec[0] != '\0')
3270 ai->exec = g_strdup (exec);
3273 item->modified = time (NULL);
3278 /* expands the application's command line */
3280 expand_exec_line (const gchar *exec_fmt,
3286 exec = g_string_new (NULL);
3287 while ((ch = *exec_fmt++) != '\0')
3291 exec = g_string_append_c (exec, ch);
3299 g_string_append_printf (exec, "%s", uri);
3303 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3304 g_string_append_printf (exec, "%s", file);
3310 exec = g_string_append_c (exec, ch);
3315 return g_string_free (exec, FALSE);
3319 * g_bookmark_file_get_app_info:
3320 * @bookmark: a #GBookmarkFile
3322 * @name: an application's name
3323 * @exec: location for the command line of the application, or %NULL
3324 * @count: return location for the registration count, or %NULL
3325 * @stamp: return location for the last registration time, or %NULL
3326 * @error: return location for a #GError, or %NULL
3328 * Gets the registration informations of @app_name for the bookmark for
3329 * @uri. See g_bookmark_file_set_app_info() for more informations about
3330 * the returned data.
3332 * The string returned in @app_exec must be freed.
3334 * In the event the URI cannot be found, %FALSE is returned and
3335 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3336 * event that no application with name @app_name has registered a bookmark
3337 * for @uri, %FALSE is returned and error is set to
3338 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3340 * Return value: %TRUE on success.
3345 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3354 BookmarkAppInfo *ai;
3356 g_return_val_if_fail (bookmark != NULL, FALSE);
3357 g_return_val_if_fail (uri != NULL, FALSE);
3358 g_return_val_if_fail (app_name != NULL, FALSE);
3360 item = g_bookmark_file_lookup_item (bookmark, uri);
3363 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3364 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3365 _("No bookmark found for URI '%s'"),
3370 ai = bookmark_item_lookup_app_info (item, name);
3373 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3374 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3375 _("No application with name '%s' registered a bookmark for '%s'"),
3382 *exec = expand_exec_line (ai->exec, uri);
3394 * g_bookmark_file_get_applications:
3395 * @bookmark: a #GBookmarkFile
3397 * @length: return location of the length of the returned list, or %NULL
3398 * @error: return location for a #GError, or %NULL
3400 * Retrieves the names of the applications that have registered the
3401 * bookmark for @uri.
3403 * In the event the URI cannot be found, %NULL is returned and
3404 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3406 * Return value: a newly allocated %NULL-terminated array of strings.
3407 * Use g_strfreev() to free it.
3412 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3422 g_return_val_if_fail (bookmark != NULL, NULL);
3423 g_return_val_if_fail (uri != NULL, NULL);
3425 item = g_bookmark_file_lookup_item (bookmark, uri);
3428 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3429 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3430 _("No bookmark found for URI '%s'"),
3435 if (!item->metadata)
3443 n_apps = g_list_length (item->metadata->applications);
3444 apps = g_new0 (gchar *, n_apps + 1);
3446 for (l = g_list_last (item->metadata->applications), i = 0;
3450 BookmarkAppInfo *ai;
3452 ai = (BookmarkAppInfo *) l->data;
3454 g_assert (ai != NULL);
3455 g_assert (ai->name != NULL);
3457 apps[i++] = g_strdup (ai->name);
3468 * g_bookmark_file_get_size:
3469 * @bookmark: a #GBookmarkFile
3471 * Gets the number of bookmarks inside @bookmark.
3473 * Return value: the number of bookmarks
3478 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3480 g_return_val_if_fail (bookmark != NULL, 0);
3482 return g_list_length (bookmark->items);
3486 * g_bookmark_file_move_item:
3487 * @bookmark: a #GBookmarkFile
3488 * @old_uri: a valid URI
3489 * @new_uri: a valid URI, or %NULL
3490 * @error: return location for a #GError or %NULL
3492 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3493 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3494 * %NULL, then the bookmark is removed.
3496 * In the event the URI cannot be found, %FALSE is returned and
3497 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3499 * Return value: %TRUE if the URI was successfully changed
3504 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3505 const gchar *old_uri,
3506 const gchar *new_uri,
3510 GError *remove_error;
3512 g_return_val_if_fail (bookmark != NULL, FALSE);
3513 g_return_val_if_fail (old_uri != NULL, FALSE);
3515 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3518 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3519 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3520 _("No bookmark found for URI '%s'"),
3525 if (new_uri && new_uri[0] != '\0')
3527 if (g_bookmark_file_has_item (bookmark, new_uri))
3529 remove_error = NULL;
3530 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3533 g_propagate_error (error, remove_error);
3540 item->uri = g_strdup (new_uri);
3541 item->modified = time (NULL);
3547 remove_error = NULL;
3548 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3551 g_propagate_error (error, remove_error);
3561 * g_bookmark_file_set_icon:
3562 * @bookmark: a #GBookmarkFile
3564 * @href: the URI of the icon for the bookmark, or %NULL
3565 * @mime_type: the MIME type of the icon for the bookmark
3567 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3568 * the currently set icon.
3570 * If no bookmark for @uri is found it is created.
3575 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3578 const gchar *mime_type)
3582 g_return_if_fail (bookmark != NULL);
3583 g_return_if_fail (uri != NULL);
3585 item = g_bookmark_file_lookup_item (bookmark, uri);
3588 item = bookmark_item_new (uri);
3589 g_bookmark_file_add_item (bookmark, item, NULL);
3592 if (!item->metadata)
3593 item->metadata = bookmark_metadata_new ();
3595 g_free (item->metadata->icon_href);
3596 g_free (item->metadata->icon_mime);
3598 if (href && href[0] != '\0')
3599 item->metadata->icon_href = g_strdup (href);
3601 if (mime_type && mime_type[0] != '\0')
3602 item->metadata->icon_mime = g_strdup (mime_type);
3604 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3606 item->modified = time (NULL);
3610 * g_bookmark_file_get_icon:
3611 * @bookmark: a #GBookmarkFile
3613 * @href: return location for the icon's location or %NULL
3614 * @mime_type: return location for the icon's MIME type or %NULL
3615 * @error: return location for a #GError or %NULL
3617 * Gets the icon of the bookmark for @uri.
3619 * In the event the URI cannot be found, %FALSE is returned and
3620 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3622 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3623 * You should free the returned strings.
3628 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3636 g_return_val_if_fail (bookmark != NULL, FALSE);
3637 g_return_val_if_fail (uri != NULL, FALSE);
3639 item = g_bookmark_file_lookup_item (bookmark, uri);
3642 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3643 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3644 _("No bookmark found for URI '%s'"),
3649 if ((!item->metadata) || (!item->metadata->icon_href))
3653 *href = g_strdup (item->metadata->icon_href);
3656 *mime_type = g_strdup (item->metadata->icon_mime);
3661 #define __G_BOOKMARK_FILE_C__
3662 #include "galiasdef.c"