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>
40 #include "gfileutils.h"
48 #include "gmessages.h"
52 #include "gstrfuncs.h"
58 /* XBEL 1.0 standard entities */
59 #define XBEL_VERSION "1.0"
60 #define XBEL_DTD_NICK "xbel"
61 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
62 "Exchange Language 1.0//EN//XML"
64 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
66 #define XBEL_ROOT_ELEMENT "xbel"
67 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
68 #define XBEL_BOOKMARK_ELEMENT "bookmark"
69 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
70 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
71 #define XBEL_TITLE_ELEMENT "title"
72 #define XBEL_DESC_ELEMENT "desc"
73 #define XBEL_INFO_ELEMENT "info"
74 #define XBEL_METADATA_ELEMENT "metadata"
76 #define XBEL_VERSION_ATTRIBUTE "version"
77 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
78 #define XBEL_OWNER_ATTRIBUTE "owner"
79 #define XBEL_ADDED_ATTRIBUTE "added"
80 #define XBEL_VISITED_ATTRIBUTE "visited"
81 #define XBEL_MODIFIED_ATTRIBUTE "modified"
82 #define XBEL_ID_ATTRIBUTE "id"
83 #define XBEL_HREF_ATTRIBUTE "href"
84 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
86 #define XBEL_YES_VALUE "yes"
87 #define XBEL_NO_VALUE "no"
89 /* Desktop bookmark spec entities */
90 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
92 #define BOOKMARK_NAMESPACE_NAME "bookmark"
93 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
95 #define BOOKMARK_GROUPS_ELEMENT "groups"
96 #define BOOKMARK_GROUP_ELEMENT "group"
97 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
98 #define BOOKMARK_APPLICATION_ELEMENT "application"
99 #define BOOKMARK_ICON_ELEMENT "icon"
100 #define BOOKMARK_PRIVATE_ELEMENT "private"
102 #define BOOKMARK_NAME_ATTRIBUTE "name"
103 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
104 #define BOOKMARK_COUNT_ATTRIBUTE "count"
105 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp"
106 #define BOOKMARK_HREF_ATTRIBUTE "href"
107 #define BOOKMARK_TYPE_ATTRIBUTE "type"
109 /* Shared MIME Info entities */
110 #define MIME_NAMESPACE_NAME "mime"
111 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
112 #define MIME_TYPE_ELEMENT "mime-type"
113 #define MIME_TYPE_ATTRIBUTE "type"
116 typedef struct _BookmarkAppInfo BookmarkAppInfo;
117 typedef struct _BookmarkMetadata BookmarkMetadata;
118 typedef struct _BookmarkItem BookmarkItem;
119 typedef struct _ParseData ParseData;
121 struct _BookmarkAppInfo
131 struct _BookmarkMetadata
138 GHashTable *apps_by_name;
143 guint is_private : 1;
157 BookmarkMetadata *metadata;
160 struct _GBookmarkFile
165 /* we store our items in a list and keep a copy inside
166 * an hash table for faster lookup performances
169 GHashTable *items_by_uri;
172 /* parser state machine */
193 static void g_bookmark_file_init (GBookmarkFile *bookmark);
194 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
195 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
199 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
202 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
204 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
208 static time_t timestamp_from_iso8601 (const gchar *iso_date);
209 static gchar * timestamp_to_iso8601 (time_t timestamp);
211 /********************************
214 * Application metadata storage *
215 ********************************/
216 static BookmarkAppInfo *
217 bookmark_app_info_new (const gchar *name)
219 BookmarkAppInfo *retval;
221 g_assert (name != NULL);
223 retval = g_slice_new (BookmarkAppInfo);
225 retval->name = g_strdup (name);
228 retval->stamp = time (NULL);
234 bookmark_app_info_free (BookmarkAppInfo *app_info)
240 g_free (app_info->name);
243 g_free (app_info->exec);
245 g_slice_free (BookmarkAppInfo, app_info);
249 bookmark_app_info_dump (BookmarkAppInfo *app_info)
253 g_assert (app_info != NULL);
255 if (app_info->count == 0)
258 retval = g_strdup_printf (" <%s:%s %s=\"%s\" %s=\"%s\" %s=\"%ld\" %s=\"%u\"/>\n",
259 BOOKMARK_NAMESPACE_NAME,
260 BOOKMARK_APPLICATION_ELEMENT,
261 BOOKMARK_NAME_ATTRIBUTE, app_info->name,
262 BOOKMARK_EXEC_ATTRIBUTE, app_info->exec,
263 BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
264 BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
270 /***********************
274 ***********************/
275 static BookmarkMetadata *
276 bookmark_metadata_new (void)
278 BookmarkMetadata *retval;
280 retval = g_slice_new (BookmarkMetadata);
282 retval->mime_type = NULL;
284 retval->groups = NULL;
286 retval->applications = NULL;
287 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
292 retval->is_private = FALSE;
294 retval->icon_href = NULL;
295 retval->icon_mime = NULL;
301 bookmark_metadata_free (BookmarkMetadata *metadata)
306 if (metadata->mime_type)
307 g_free (metadata->mime_type);
309 if (metadata->groups)
311 g_list_foreach (metadata->groups,
314 g_list_free (metadata->groups);
317 if (metadata->applications)
319 g_list_foreach (metadata->applications,
320 (GFunc) bookmark_app_info_free,
322 g_list_free (metadata->applications);
324 g_hash_table_destroy (metadata->apps_by_name);
327 if (metadata->icon_href)
328 g_free (metadata->icon_href);
330 if (metadata->icon_mime)
331 g_free (metadata->icon_mime);
333 g_slice_free (BookmarkMetadata, metadata);
337 bookmark_metadata_dump (BookmarkMetadata *metadata)
341 if (!metadata->applications)
344 retval = g_string_new (NULL);
346 /* metadata container */
347 g_string_append_printf (retval,
349 XBEL_METADATA_ELEMENT,
350 XBEL_OWNER_ATTRIBUTE, BOOKMARK_METADATA_OWNER);
353 if (metadata->mime_type)
354 g_string_append_printf (retval,
355 " <%s:%s %s=\"%s\"/>\n",
358 MIME_TYPE_ATTRIBUTE, metadata->mime_type);
360 if (metadata->groups)
364 /* open groups container */
365 g_string_append_printf (retval,
367 BOOKMARK_NAMESPACE_NAME,
368 BOOKMARK_GROUPS_ELEMENT);
370 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
372 gchar *group_name = (gchar *) l->data;
374 g_string_append_printf (retval,
375 " <%s:%s>%s</%s:%s>\n",
376 BOOKMARK_NAMESPACE_NAME,
377 BOOKMARK_GROUP_ELEMENT,
379 BOOKMARK_NAMESPACE_NAME,
380 BOOKMARK_GROUP_ELEMENT);
383 /* close groups container */
384 g_string_append_printf (retval,
386 BOOKMARK_NAMESPACE_NAME,
387 BOOKMARK_GROUPS_ELEMENT);
390 if (metadata->applications)
394 /* open applications container */
395 g_string_append_printf (retval,
397 BOOKMARK_NAMESPACE_NAME,
398 BOOKMARK_APPLICATIONS_ELEMENT);
400 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
402 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
405 g_assert (app_info != NULL);
407 app_data = bookmark_app_info_dump (app_info);
411 retval = g_string_append (retval, app_data);
417 /* close applications container */
418 g_string_append_printf (retval,
420 BOOKMARK_NAMESPACE_NAME,
421 BOOKMARK_APPLICATIONS_ELEMENT);
425 if (metadata->icon_href)
427 if (!metadata->icon_mime)
428 metadata->icon_mime = g_strdup ("application/octet-stream");
430 g_string_append_printf (retval,
431 " <%s:%s %s=\"%s\" %s=\"%s\"/>\n",
432 BOOKMARK_NAMESPACE_NAME,
433 BOOKMARK_ICON_ELEMENT,
434 BOOKMARK_HREF_ATTRIBUTE, metadata->icon_href,
435 BOOKMARK_TYPE_ATTRIBUTE, metadata->icon_mime);
439 if (metadata->is_private)
440 g_string_append_printf (retval,
442 BOOKMARK_NAMESPACE_NAME,
443 BOOKMARK_PRIVATE_ELEMENT);
445 /* close metadata container */
446 g_string_append_printf (retval, " </%s>\n", XBEL_METADATA_ELEMENT);
448 return g_string_free (retval, FALSE);
451 /******************************************************
454 * Storage for a single bookmark item inside the list *
455 ******************************************************/
456 static BookmarkItem *
457 bookmark_item_new (const gchar *uri)
461 g_assert (uri != NULL);
463 item = g_slice_new (BookmarkItem);
464 item->uri = g_strdup (uri);
467 item->description = NULL;
469 item->added = (time_t) -1;
470 item->modified = (time_t) -1;
471 item->visited = (time_t) -1;
473 item->metadata = NULL;
479 bookmark_item_free (BookmarkItem *item)
488 g_free (item->title);
490 if (item->description)
491 g_free (item->description);
494 bookmark_metadata_free (item->metadata);
496 g_slice_free (BookmarkItem, item);
500 bookmark_item_dump (BookmarkItem *item)
503 gchar *added, *visited, *modified;
506 /* at this point, we must have at least a registered application; if we don't
507 * we don't screw up the bookmark file, and just skip this item
509 if (!item->metadata || !item->metadata->applications)
511 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
515 retval = g_string_new (NULL);
517 added = timestamp_to_iso8601 (item->added);
518 modified = timestamp_to_iso8601 (item->modified);
519 visited = timestamp_to_iso8601 (item->visited);
521 escaped_uri = g_markup_escape_text (item->uri, strlen (item->uri));
523 g_string_append_printf (retval,
524 " <%s %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\">\n",
525 XBEL_BOOKMARK_ELEMENT,
526 XBEL_HREF_ATTRIBUTE, escaped_uri,
527 XBEL_ADDED_ATTRIBUTE, added,
528 XBEL_MODIFIED_ATTRIBUTE, modified,
529 XBEL_VISITED_ATTRIBUTE, visited);
530 g_free (escaped_uri);
537 gchar *escaped_title;
539 escaped_title = g_markup_escape_text (item->title, strlen (item->title));
540 g_string_append_printf (retval,
545 g_free (escaped_title);
548 if (item->description)
552 escaped_desc = g_markup_escape_text (item->description, strlen (item->description));
553 g_string_append_printf (retval,
558 g_free (escaped_desc);
565 /* open info container */
566 g_string_append_printf (retval, " <%s>\n", XBEL_INFO_ELEMENT);
568 metadata = bookmark_metadata_dump (item->metadata);
571 retval = g_string_append (retval, metadata);
576 /* close info container */
577 g_string_append_printf (retval, " </%s>\n", XBEL_INFO_ELEMENT);
580 g_string_append_printf (retval, " </%s>\n", XBEL_BOOKMARK_ELEMENT);
582 return g_string_free (retval, FALSE);
585 static BookmarkAppInfo *
586 bookmark_item_lookup_app_info (BookmarkItem *item,
587 const gchar *app_name)
589 g_assert (item != NULL && app_name != NULL);
594 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
597 /*************************
599 *************************/
602 g_bookmark_file_init (GBookmarkFile *bookmark)
604 bookmark->title = NULL;
605 bookmark->description = NULL;
607 bookmark->items = NULL;
608 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
615 g_bookmark_file_clear (GBookmarkFile *bookmark)
617 g_free (bookmark->title);
618 g_free (bookmark->description);
622 g_list_foreach (bookmark->items,
623 (GFunc) bookmark_item_free,
625 g_list_free (bookmark->items);
627 bookmark->items = NULL;
630 if (bookmark->items_by_uri)
632 g_hash_table_destroy (bookmark->items_by_uri);
634 bookmark->items_by_uri = NULL;
642 GHashTable *namespaces;
644 GBookmarkFile *bookmark_file;
645 BookmarkItem *current_item;
649 parse_data_new (void)
653 retval = g_new (ParseData, 1);
655 retval->state = STATE_STARTED;
656 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
657 (GDestroyNotify) g_free,
658 (GDestroyNotify) g_free);
659 retval->bookmark_file = NULL;
660 retval->current_item = NULL;
666 parse_data_free (ParseData *parse_data)
668 g_hash_table_destroy (parse_data->namespaces);
673 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
676 parse_bookmark_element (GMarkupParseContext *context,
677 ParseData *parse_data,
678 const gchar **attribute_names,
679 const gchar **attribute_values,
682 const gchar *uri, *added, *modified, *visited;
688 g_assert ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
691 uri = added = modified = visited = NULL;
692 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
694 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
695 uri = attribute_values[i];
696 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
697 added = attribute_values[i];
698 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
699 modified = attribute_values[i];
700 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
701 visited = attribute_values[i];
704 g_set_error (error, G_MARKUP_ERROR,
705 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
706 _("Unexpected attribute '%s' for element '%s'"),
708 XBEL_BOOKMARK_ELEMENT);
715 g_set_error (error, G_MARKUP_ERROR,
716 G_MARKUP_ERROR_INVALID_CONTENT,
717 _("Attribute '%s' of element '%s' not found"),
719 XBEL_BOOKMARK_ELEMENT);
723 g_assert (parse_data->current_item == NULL);
725 item = bookmark_item_new (uri);
728 item->added = timestamp_from_iso8601 (added);
731 item->modified = timestamp_from_iso8601 (modified);
734 item->visited = timestamp_from_iso8601 (visited);
737 g_bookmark_file_add_item (parse_data->bookmark_file,
742 bookmark_item_free (item);
744 g_propagate_error (error, add_error);
749 parse_data->current_item = item;
753 parse_application_element (GMarkupParseContext *context,
754 ParseData *parse_data,
755 const gchar **attribute_names,
756 const gchar **attribute_values,
759 const gchar *name, *exec, *count, *stamp;
765 g_assert ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
768 name = exec = count = stamp = NULL;
769 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
771 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
772 name = attribute_values[i];
773 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
774 exec = attribute_values[i];
775 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
776 count = attribute_values[i];
777 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
778 stamp = attribute_values[i];
781 g_set_error (error, G_MARKUP_ERROR,
782 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
783 _("Unexpected attribute '%s' for element '%s'"),
785 BOOKMARK_APPLICATION_ELEMENT);
792 g_set_error (error, G_MARKUP_ERROR,
793 G_MARKUP_ERROR_INVALID_CONTENT,
794 _("Attribute '%s' of element '%s' not found"),
795 BOOKMARK_NAME_ATTRIBUTE,
796 BOOKMARK_APPLICATION_ELEMENT);
802 g_set_error (error, G_MARKUP_ERROR,
803 G_MARKUP_ERROR_INVALID_CONTENT,
804 _("Attribute '%s' of element '%s' not found"),
805 BOOKMARK_EXEC_ATTRIBUTE,
806 BOOKMARK_APPLICATION_ELEMENT);
810 g_assert (parse_data->current_item != NULL);
811 item = parse_data->current_item;
813 ai = bookmark_item_lookup_app_info (item, name);
816 ai = bookmark_app_info_new (name);
819 item->metadata = bookmark_metadata_new ();
821 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
822 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
825 ai->exec = g_strdup (exec);
828 ai->count = atoi (count);
833 ai->stamp = (time_t) atol (stamp);
835 ai->stamp = time (NULL);
839 parse_mime_type_element (GMarkupParseContext *context,
840 ParseData *parse_data,
841 const gchar **attribute_names,
842 const gchar **attribute_values,
850 g_assert ((parse_data != NULL) && (parse_data->state == STATE_MIME));
854 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
856 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
857 type = attribute_values[i];
860 g_set_error (error, G_MARKUP_ERROR,
861 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
862 _("Unexpected attribute '%s' for element '%s'"),
870 type = "application/octet-stream";
872 g_assert (parse_data->current_item != NULL);
873 item = parse_data->current_item;
876 item->metadata = bookmark_metadata_new ();
878 item->metadata->mime_type = g_strdup (type);
882 parse_icon_element (GMarkupParseContext *context,
883 ParseData *parse_data,
884 const gchar **attribute_names,
885 const gchar **attribute_values,
894 g_assert ((parse_data != NULL) && (parse_data->state == STATE_ICON));
899 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
901 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
902 href = attribute_values[i];
903 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
904 type = attribute_values[i];
907 g_set_error (error, G_MARKUP_ERROR,
908 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
909 _("Unexpected attribute '%s' for element '%s'"),
911 BOOKMARK_ICON_ELEMENT);
918 g_set_error (error, G_MARKUP_ERROR,
919 G_MARKUP_ERROR_INVALID_CONTENT,
920 _("Attribute '%s' of element '%s' not found"),
921 BOOKMARK_HREF_ATTRIBUTE,
922 BOOKMARK_ICON_ELEMENT);
927 type = "application/octet-stream";
929 g_assert (parse_data->current_item != NULL);
930 item = parse_data->current_item;
933 item->metadata = bookmark_metadata_new ();
935 item->metadata->icon_href = g_strdup (href);
936 item->metadata->icon_mime = g_strdup (type);
939 /* scans through the attributes of an element for the "xmlns" pragma, and
940 * adds any resulting namespace declaration to a per-parser hashtable, using
941 * the namespace name as a key for the namespace URI; if no key was found,
942 * the namespace is considered as default, and stored under the "default" key.
944 * FIXME: this works on the assumption that the generator of the XBEL file
945 * is either this code or is smart enough to place the namespace declarations
946 * inside the main root node or inside the metadata node and does not redefine
947 * a namespace inside an inner node; this does *not* conform to the
948 * XML-NS standard, although is a close approximation. In order to make this
949 * conformant to the XML-NS specification we should use a per-element
950 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
953 map_namespace_to_name (ParseData *parse_data,
954 const gchar **attribute_names,
955 const gchar **attribute_values)
960 g_assert (parse_data != NULL);
962 if (!attribute_names || !attribute_names[0])
966 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
968 if (g_str_has_prefix (attr, "xmlns"))
970 gchar *namespace_name, *namespace_uri;
973 p = g_utf8_strchr (attr, -1, ':');
975 p = g_utf8_next_char (p);
979 namespace_name = g_strdup (p);
980 namespace_uri = g_strdup (attribute_values[i]);
982 g_hash_table_replace (parse_data->namespaces,
989 /* checks whether @element_full is equal to @element.
991 * if @namespace is set, it tries to resolve the namespace to a known URI,
992 * and if found is prepended to the element name, from which is separated
993 * using the character specified in the @sep parameter.
996 is_element_full (ParseData *parse_data,
997 const gchar *element_full,
998 const gchar *namespace,
999 const gchar *element,
1002 gchar *ns_uri, *ns_name;
1003 gchar *element_name, *resolved;
1007 g_assert (parse_data != NULL);
1008 g_assert (element_full != NULL);
1013 /* no namespace requested: dumb element compare */
1015 return (0 == strcmp (element_full, element));
1017 /* search for namespace separator; if none found, assume we are under the
1018 * default namespace, and set ns_name to our "default" marker; if no default
1019 * namespace has been set, just do a plain comparison between @full_element
1022 p = g_utf8_strchr (element_full, -1, ':');
1024 ns_name = g_strndup (element_full, p - element_full);
1026 ns_name = g_strdup ("default");
1028 ns_uri = g_strdup (g_hash_table_lookup (parse_data->namespaces, ns_name));
1031 /* no default namespace found */
1034 return (0 == strcmp (element_full, element));
1037 p = g_utf8_next_char (p);
1038 element_name = g_strdup (p);
1040 resolved = g_strdup_printf ("%s%c%s", ns_uri, sep, element_name);
1041 s = g_strdup_printf ("%s%c%s", namespace, sep, element);
1042 retval = (0 == strcmp (resolved, s));
1046 g_free (element_name);
1053 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1054 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1057 start_element_raw_cb (GMarkupParseContext *context,
1058 const gchar *element_name,
1059 const gchar **attribute_names,
1060 const gchar **attribute_values,
1064 ParseData *parse_data = (ParseData *) user_data;
1066 /* we must check for namespace declarations first
1068 * XXX - we could speed up things by checking for namespace declarations
1069 * only on the root node, where they usually are; this would probably break
1070 * on streams not produced by us or by "smart" generators
1072 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1074 switch (parse_data->state)
1077 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1083 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1085 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1086 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1087 parse_data->state = STATE_ROOT;
1091 g_set_error (error, G_MARKUP_ERROR,
1092 G_MARKUP_ERROR_INVALID_CONTENT,
1093 _("Unexpected tag '%s', tag '%s' expected"),
1094 element_name, XBEL_ROOT_ELEMENT);
1097 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1098 parse_data->state = STATE_TITLE;
1099 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1100 parse_data->state = STATE_DESC;
1101 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1103 GError *inner_error = NULL;
1105 parse_data->state = STATE_BOOKMARK;
1107 parse_bookmark_element (context,
1113 g_propagate_error (error, inner_error);
1116 g_set_error (error, G_MARKUP_ERROR,
1117 G_MARKUP_ERROR_INVALID_CONTENT,
1118 _("Unexpected tag '%s' inside '%s'"),
1122 case STATE_BOOKMARK:
1123 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1124 parse_data->state = STATE_TITLE;
1125 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1126 parse_data->state = STATE_DESC;
1127 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1128 parse_data->state = STATE_INFO;
1130 g_set_error (error, G_MARKUP_ERROR,
1131 G_MARKUP_ERROR_INVALID_CONTENT,
1132 _("Unexpected tag '%s' inside '%s'"),
1134 XBEL_BOOKMARK_ELEMENT);
1137 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1143 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1145 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1146 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1148 parse_data->state = STATE_METADATA;
1150 if (!parse_data->current_item->metadata)
1151 parse_data->current_item->metadata = bookmark_metadata_new ();
1156 g_set_error (error, G_MARKUP_ERROR,
1157 G_MARKUP_ERROR_INVALID_CONTENT,
1158 _("Unexpected tag '%s', tag '%s' expected"),
1160 XBEL_METADATA_ELEMENT);
1162 case STATE_METADATA:
1163 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1164 parse_data->state = STATE_APPLICATIONS;
1165 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1166 parse_data->state = STATE_GROUPS;
1167 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1168 parse_data->current_item->metadata->is_private = TRUE;
1169 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1171 GError *inner_error = NULL;
1173 parse_data->state = STATE_ICON;
1175 parse_icon_element (context,
1181 g_propagate_error (error, inner_error);
1183 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1185 GError *inner_error = NULL;
1187 parse_data->state = STATE_MIME;
1189 parse_mime_type_element (context,
1195 g_propagate_error (error, inner_error);
1198 g_set_error (error, G_MARKUP_ERROR,
1199 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1200 _("Unexpected tag '%s' inside '%s'"),
1202 XBEL_METADATA_ELEMENT);
1204 case STATE_APPLICATIONS:
1205 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1207 GError *inner_error = NULL;
1209 parse_data->state = STATE_APPLICATION;
1211 parse_application_element (context,
1217 g_propagate_error (error, inner_error);
1220 g_set_error (error, G_MARKUP_ERROR,
1221 G_MARKUP_ERROR_INVALID_CONTENT,
1222 _("Unexpected tag '%s', tag '%s' expected"),
1224 BOOKMARK_APPLICATION_ELEMENT);
1227 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1228 parse_data->state = STATE_GROUP;
1230 g_set_error (error, G_MARKUP_ERROR,
1231 G_MARKUP_ERROR_INVALID_CONTENT,
1232 _("Unexpected tag '%s', tag '%s' expected"),
1234 BOOKMARK_GROUP_ELEMENT);
1237 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1239 GError *inner_error = NULL;
1241 parse_icon_element (context,
1247 g_propagate_error (error, inner_error);
1250 g_set_error (error, G_MARKUP_ERROR,
1251 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1252 _("Unexpected tag '%s' inside '%s'"),
1254 XBEL_METADATA_ELEMENT);
1257 g_assert_not_reached ();
1263 end_element_raw_cb (GMarkupParseContext *context,
1264 const gchar *element_name,
1268 ParseData *parse_data = (ParseData *) user_data;
1270 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1271 parse_data->state = STATE_FINISHED;
1272 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1274 parse_data->current_item = NULL;
1276 parse_data->state = STATE_ROOT;
1278 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1279 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1280 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1282 if (parse_data->current_item)
1283 parse_data->state = STATE_BOOKMARK;
1285 parse_data->state = STATE_ROOT;
1287 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1288 parse_data->state = STATE_INFO;
1289 else if (IS_ELEMENT_NS (parse_data, element_name,
1290 BOOKMARK_NAMESPACE_URI,
1291 BOOKMARK_APPLICATION_ELEMENT))
1292 parse_data->state = STATE_APPLICATIONS;
1293 else if (IS_ELEMENT_NS (parse_data, element_name,
1294 BOOKMARK_NAMESPACE_URI,
1295 BOOKMARK_GROUP_ELEMENT))
1296 parse_data->state = STATE_GROUPS;
1297 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1298 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1299 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1300 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1301 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1302 parse_data->state = STATE_METADATA;
1306 text_raw_cb (GMarkupParseContext *context,
1312 ParseData *parse_data = (ParseData *) user_data;
1315 payload = g_strndup (text, length);
1317 switch (parse_data->state)
1320 if (parse_data->current_item)
1322 g_free (parse_data->current_item->title);
1323 parse_data->current_item->title = g_strdup (payload);
1327 g_free (parse_data->bookmark_file->title);
1328 parse_data->bookmark_file->title = g_strdup (payload);
1332 if (parse_data->current_item)
1334 g_free (parse_data->current_item->description);
1335 parse_data->current_item->description = g_strdup (payload);
1339 g_free (parse_data->bookmark_file->description);
1340 parse_data->bookmark_file->description = g_strdup (payload);
1347 g_assert (parse_data->current_item != NULL);
1349 if (!parse_data->current_item->metadata)
1350 parse_data->current_item->metadata = bookmark_metadata_new ();
1352 groups = parse_data->current_item->metadata->groups;
1353 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1357 case STATE_BOOKMARK:
1359 case STATE_METADATA:
1360 case STATE_APPLICATIONS:
1361 case STATE_APPLICATION:
1367 g_assert_not_reached ();
1374 static GMarkupParser markup_parser =
1376 start_element_raw_cb, /* start_element */
1377 end_element_raw_cb, /* end_element */
1378 text_raw_cb, /* text */
1379 NULL, /* passthrough */
1384 g_bookmark_file_parse (GBookmarkFile *bookmark,
1385 const gchar *buffer,
1389 GMarkupParseContext *context;
1390 ParseData *parse_data;
1391 GError *parse_error, *end_error;
1394 g_assert (bookmark != NULL);
1400 length = strlen (buffer);
1402 parse_data = parse_data_new ();
1403 parse_data->bookmark_file = bookmark;
1405 context = g_markup_parse_context_new (&markup_parser,
1408 (GDestroyNotify) parse_data_free);
1411 retval = g_markup_parse_context_parse (context,
1417 g_propagate_error (error, parse_error);
1423 retval = g_markup_parse_context_end_parse (context, &end_error);
1426 g_propagate_error (error, end_error);
1431 g_markup_parse_context_free (context);
1437 g_bookmark_file_dump (GBookmarkFile *bookmark,
1444 retval = g_string_new (NULL);
1446 g_string_append_printf (retval,
1447 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1449 /* XXX - do we really need the doctype? */
1455 " xmlns:%s=\"%s\"\n"
1456 " xmlns:%s=\"%s\"\n>",
1458 /* XXX - do we really need the doctype? */
1460 XBEL_DTD_SYSTEM, XBEL_DTD_URI,
1463 XBEL_VERSION_ATTRIBUTE, XBEL_VERSION,
1464 BOOKMARK_NAMESPACE_NAME, BOOKMARK_NAMESPACE_URI,
1465 MIME_NAMESPACE_NAME, MIME_NAMESPACE_URI);
1467 if (bookmark->title)
1469 gchar *escaped_title;
1471 escaped_title = g_markup_escape_text (bookmark->title, -1);
1473 g_string_append_printf (retval, " <%s>%s</%s>\n",
1476 XBEL_TITLE_ELEMENT);
1478 g_free (escaped_title);
1481 if (bookmark->description)
1483 gchar *escaped_desc;
1485 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1487 g_string_append_printf (retval, " <%s>%s</%s>\n",
1492 g_free (escaped_desc);
1495 if (!bookmark->items)
1498 retval = g_string_append (retval, "\n");
1500 for (l = g_list_last (bookmark->items);
1504 BookmarkItem *item = (BookmarkItem *) l->data;
1507 item_dump = bookmark_item_dump (item);
1511 retval = g_string_append (retval, item_dump);
1517 g_string_append_printf (retval, "</%s>", XBEL_ROOT_ELEMENT);
1520 *length = retval->len;
1522 return g_string_free (retval, FALSE);
1529 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1530 * should free the returned string.
1533 timestamp_to_iso8601 (time_t timestamp)
1537 if (timestamp == (time_t) -1)
1538 g_get_current_time (&stamp);
1541 stamp.tv_sec = timestamp;
1545 return g_time_val_to_iso8601 (&stamp);
1549 timestamp_from_iso8601 (const gchar *iso_date)
1553 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1556 return (time_t) stamp.tv_sec;
1562 g_bookmark_file_error_quark (void)
1564 static GQuark error_quark = 0;
1566 if (G_UNLIKELY (error_quark == 0))
1567 error_quark = g_quark_from_static_string ("egg-bookmark-file-error-quark");
1574 /********************
1576 ********************/
1579 * g_bookmark_file_new:
1581 * Creates a new empty #GBookmarkFile object.
1583 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1584 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1587 * Return value: an empty #GBookmarkFile
1592 g_bookmark_file_new (void)
1594 GBookmarkFile *bookmark;
1596 bookmark = g_new (GBookmarkFile, 1);
1598 g_bookmark_file_init (bookmark);
1604 * g_bookmark_file_free:
1605 * @bookmark: a #GBookmarkFile
1607 * Frees a #GBookmarkFile.
1612 g_bookmark_file_free (GBookmarkFile *bookmark)
1617 g_bookmark_file_clear (bookmark);
1623 * g_bookmark_file_load_from_data:
1624 * @bookmark: an empty #GBookmarkFile struct
1625 * @data: desktop bookmarks loaded in memory
1626 * @length: the length of @data in bytes
1627 * @error: return location for a #GError, or %NULL
1629 * Loads a bookmark file from memory into an empty #GBookmarkFile
1630 * structure. If the object cannot be created then @error is set to a
1631 * #GBookmarkFileError.
1633 * Return value: %TRUE if a desktop bookmark could be loaded.
1638 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1643 GError *parse_error;
1646 g_return_val_if_fail (bookmark != NULL, FALSE);
1647 g_return_val_if_fail (data != NULL, FALSE);
1648 g_return_val_if_fail (length != 0, FALSE);
1650 if (length == (gsize) -1)
1651 length = strlen (data);
1653 if (bookmark->items)
1655 g_bookmark_file_clear (bookmark);
1656 g_bookmark_file_init (bookmark);
1660 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1663 g_propagate_error (error, parse_error);
1672 * g_bookmark_file_load_from_file:
1673 * @bookmark: an empty #GBookmarkFile struct
1674 * @filename: the path of a filename to load, in the GLib file name encoding
1675 * @error: return location for a #GError, or %NULL
1677 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1678 * If the file could not be loaded then @error is set to either a #GFileError
1679 * or #GBookmarkFileError.
1681 * Return value: %TRUE if a desktop bookmark file could be loaded
1686 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1687 const gchar *filename,
1695 g_return_val_if_fail (bookmark != NULL, FALSE);
1696 g_return_val_if_fail (filename != NULL, FALSE);
1699 g_file_get_contents (filename, &buffer, &len, &read_error);
1702 g_propagate_error (error, read_error);
1708 retval = g_bookmark_file_load_from_data (bookmark,
1714 g_propagate_error (error, read_error);
1727 /* Iterates through all the directories in *dirs trying to
1728 * find file. When it successfully locates file, returns a
1729 * string its absolute path. It also leaves the unchecked
1730 * directories in *dirs. You should free the returned string
1732 * Adapted from gkeyfile.c
1735 find_file_in_data_dirs (const gchar *file,
1739 gchar **data_dirs, *data_dir, *path;
1748 while (data_dirs && (data_dir = *data_dirs) && !path)
1750 gchar *candidate_file, *sub_dir;
1752 candidate_file = (gchar *) file;
1753 sub_dir = g_strdup ("");
1754 while (candidate_file != NULL && !path)
1758 path = g_build_filename (data_dir, sub_dir,
1759 candidate_file, NULL);
1761 candidate_file = strchr (candidate_file, '-');
1763 if (candidate_file == NULL)
1769 sub_dir = g_strndup (file, candidate_file - file - 1);
1771 for (p = sub_dir; *p != '\0'; p++)
1774 *p = G_DIR_SEPARATOR;
1785 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1786 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1787 _("No valid bookmark file was be found in data dirs"));
1797 * g_bookmark_file_load_from_data_dirs:
1798 * @bookmark: a #GBookmarkFile
1799 * @file: a relative path to a filename to open and parse
1800 * @full_path: return location for a string containing the full path
1801 * of the file, or %NULL
1802 * @error: return location for a #GError, or %NULL
1804 * This function looks for a desktop bookmark file named @file in the
1805 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1806 * loads the file into @bookmark and returns the file's full path in
1807 * @full_path. If the file could not be loaded then an %error is
1808 * set to either a #GFileError or #GBookmarkFileError.
1810 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1815 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1820 GError *file_error = NULL;
1821 gchar **all_data_dirs, **data_dirs;
1822 const gchar *user_data_dir;
1823 const gchar * const * system_data_dirs;
1826 gboolean found_file;
1828 g_return_val_if_fail (bookmark != NULL, FALSE);
1829 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1831 user_data_dir = g_get_user_data_dir ();
1832 system_data_dirs = g_get_system_data_dirs ();
1833 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1836 all_data_dirs[i++] = g_strdup (user_data_dir);
1839 while (system_data_dirs[j] != NULL)
1840 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1843 data_dirs = all_data_dirs;
1845 while (*data_dirs != NULL && !found_file)
1847 g_free (output_path);
1849 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1853 g_propagate_error (error, file_error);
1857 found_file = g_bookmark_file_load_from_file (bookmark,
1862 g_propagate_error (error, file_error);
1867 if (found_file && full_path)
1868 *full_path = output_path;
1870 g_free (output_path);
1872 g_strfreev (all_data_dirs);
1879 * g_bookmark_file_to_data:
1880 * @bookmark: a #GBookmarkFile
1881 * @length: return location for the length of the returned string, or %NULL
1882 * @error: return location for a #GError, or %NULL
1884 * This function outputs @bookmark as a string.
1886 * Return value: a newly allocated string holding
1887 * the contents of the #GBookmarkFile
1892 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1896 GError *write_error = NULL;
1899 g_return_val_if_fail (bookmark != NULL, NULL);
1901 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1904 g_propagate_error (error, write_error);
1913 * g_bookmark_file_to_file:
1914 * @bookmark: a #GBookmarkFile
1915 * @filename: path of the output file
1916 * @error: return location for a #GError, or %NULL
1918 * This function outputs @bookmark into a file. The write process is
1919 * guaranteed to be atomic by using g_file_set_contents() internally.
1921 * Return value: %TRUE if the file was successfully written.
1926 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1927 const gchar *filename,
1931 GError *data_error, *write_error;
1935 g_return_val_if_fail (bookmark != NULL, FALSE);
1936 g_return_val_if_fail (filename != NULL, FALSE);
1939 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1942 g_propagate_error (error, data_error);
1948 g_file_set_contents (filename, data, len, &write_error);
1951 g_propagate_error (error, write_error);
1963 static BookmarkItem *
1964 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1967 g_assert (bookmark != NULL && uri != NULL);
1969 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1972 /* this function adds a new item to the list */
1974 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1978 g_assert (bookmark != NULL);
1979 g_assert (item != NULL);
1981 /* this should never happen; and if it does, then we are
1982 * screwing up something big time.
1984 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1986 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1987 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1988 _("A bookmark for URI '%s' already exists"),
1993 bookmark->items = g_list_prepend (bookmark->items, item);
1995 g_hash_table_replace (bookmark->items_by_uri,
1999 if (item->added == (time_t) -1)
2000 item->added = time (NULL);
2002 if (item->modified == (time_t) -1)
2003 item->modified = time (NULL);
2007 * g_bookmark_file_remove_item:
2008 * @bookmark: a #GBookmarkFile
2010 * @error: return location for a #GError, or %NULL
2012 * Removes the bookmark for @uri from the bookmark file @bookmark.
2017 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2023 g_return_if_fail (bookmark != NULL);
2024 g_return_if_fail (uri != NULL);
2026 item = g_bookmark_file_lookup_item (bookmark, uri);
2030 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2031 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2032 _("No bookmark found for URI '%s'"),
2037 bookmark->items = g_list_remove (bookmark->items, item);
2038 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2040 bookmark_item_free (item);
2044 * g_bookmark_file_has_item:
2045 * @bookmark: a #GBookmarkFile
2048 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2050 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2055 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2058 g_return_val_if_fail (bookmark != NULL, FALSE);
2059 g_return_val_if_fail (uri != NULL, FALSE);
2061 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2065 * g_bookmark_file_get_uris:
2066 * @bookmark: a #GBookmarkFile
2067 * @length: return location for the number of returned URIs, or %NULL
2069 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2070 * The array of returned URIs will be %NULL-terminated, so @length may
2071 * optionally be %NULL.
2073 * Return value: a newly allocated %NULL-terminated array of strings.
2074 * Use g_strfreev() to free it.
2079 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2086 g_return_val_if_fail (bookmark != NULL, NULL);
2088 n_items = g_list_length (bookmark->items);
2089 uris = g_new0 (gchar *, n_items + 1);
2091 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2093 BookmarkItem *item = (BookmarkItem *) l->data;
2095 g_assert (item != NULL);
2097 uris[i++] = g_strdup (item->uri);
2108 * g_bookmark_file_set_title:
2109 * @bookmark: a #GBookmarkFile
2110 * @uri: a valid URI or %NULL
2111 * @title: a UTF-8 encoded string
2113 * Sets @title as the title of the bookmark for @uri inside the
2114 * bookmark file @bookmark.
2116 * If @uri is %NULL, the title of @bookmark is set.
2118 * If a bookmark for @uri cannot be found then it is created.
2123 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2127 g_return_if_fail (bookmark != NULL);
2131 g_free (bookmark->title);
2133 if (title && title[0] != '\0')
2134 bookmark->title = g_strdup (title);
2140 item = g_bookmark_file_lookup_item (bookmark, uri);
2143 item = bookmark_item_new (uri);
2144 g_bookmark_file_add_item (bookmark, item, NULL);
2147 g_free (item->title);
2149 if (title && title[0] != '\0')
2150 item->title = g_strdup (title);
2152 item->modified = time (NULL);
2157 * g_bookmark_file_get_title:
2158 * @bookmark: a #GBookmarkFile
2159 * @uri: a valid URI or %NULL
2160 * @error: return location for a #GError, or %NULL
2162 * Returns the title of the bookmark for @uri.
2164 * If @uri is %NULL, the title of @bookmark is returned.
2166 * In the event the URI cannot be found, %NULL is returned and
2167 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2169 * Return value: a newly allocated string or %NULL if the specified
2170 * URI cannot be found.
2175 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2181 g_return_val_if_fail (bookmark != NULL, NULL);
2184 return g_strdup (bookmark->title);
2186 item = g_bookmark_file_lookup_item (bookmark, uri);
2189 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2190 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2191 _("No bookmark found for URI '%s'"),
2196 return g_strdup (item->title);
2200 * g_bookmark_file_set_description:
2201 * @bookmark: a #GBookmarkFile
2202 * @uri: a valid URI or %NULL
2203 * @description: a string
2205 * Sets @description as the description of the bookmark for @uri.
2207 * If @uri is %NULL, the description of @bookmark is set.
2209 * If a bookmark for @uri cannot be found then it is created.
2214 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2216 const gchar *description)
2218 g_return_if_fail (bookmark != NULL);
2222 g_free (bookmark->description);
2224 if (description && description[0] != '\0')
2225 bookmark->description = g_strdup (description);
2231 item = g_bookmark_file_lookup_item (bookmark, uri);
2234 item = bookmark_item_new (uri);
2235 g_bookmark_file_add_item (bookmark, item, NULL);
2238 g_free (item->description);
2240 if (description && description[0] != '\0')
2241 item->description = g_strdup (description);
2243 item->modified = time (NULL);
2248 * g_bookmark_file_get_description:
2249 * @bookmark: a #GBookmarkFile
2251 * @error: return location for a #GError, or %NULL
2253 * Retrieves the description of the bookmark for @uri.
2255 * In the event the URI cannot be found, %NULL is returned and
2256 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2258 * Return value: a newly allocated string or %NULL if the specified
2259 * URI cannot be found.
2264 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2270 g_return_val_if_fail (bookmark != NULL, NULL);
2273 return g_strdup (bookmark->description);
2275 item = g_bookmark_file_lookup_item (bookmark, uri);
2278 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2279 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2280 _("No bookmark found for URI '%s'"),
2285 return g_strdup (item->description);
2289 * g_bookmark_file_set_mime_type:
2290 * @bookmark: a #GBookmarkFile
2292 * @mime_type: a MIME type
2294 * Sets @mime_type as the MIME type of the bookmark for @uri.
2296 * If a bookmark for @uri cannot be found then it is created.
2301 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2303 const gchar *mime_type)
2307 g_return_if_fail (bookmark != NULL);
2308 g_return_if_fail (uri != NULL);
2309 g_return_if_fail (mime_type != NULL);
2311 item = g_bookmark_file_lookup_item (bookmark, uri);
2314 item = bookmark_item_new (uri);
2315 g_bookmark_file_add_item (bookmark, item, NULL);
2318 if (!item->metadata)
2319 item->metadata = bookmark_metadata_new ();
2321 if (item->metadata->mime_type != NULL)
2322 g_free (item->metadata->mime_type);
2324 item->metadata->mime_type = g_strdup (mime_type);
2325 item->modified = time (NULL);
2329 * g_bookmark_file_get_mime_type:
2330 * @bookmark: a #GBookmarkFile
2332 * @error: return location for a #GError, or %NULL
2334 * Retrieves the MIME type of the resource pointed by @uri.
2336 * In the event the URI cannot be found, %NULL is returned and
2337 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2338 * event that the MIME type cannot be found, %NULL is returned and
2339 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2341 * Return value: a newly allocated string or %NULL if the specified
2342 * URI cannot be found.
2347 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2353 g_return_val_if_fail (bookmark != NULL, NULL);
2354 g_return_val_if_fail (uri != NULL, NULL);
2356 item = g_bookmark_file_lookup_item (bookmark, uri);
2359 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2360 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2361 _("No bookmark found for URI '%s'"),
2366 if (!item->metadata)
2368 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2369 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2370 _("No MIME type defined in the bookmark for URI '%s'"),
2375 return g_strdup (item->metadata->mime_type);
2379 * g_bookmark_file_set_is_private:
2380 * @bookmark: a #GBookmarkFile
2382 * @is_private: %TRUE if the bookmark should be marked as private
2384 * Sets the private flag of the bookmark for @uri.
2386 * If a bookmark for @uri cannot be found then it is created.
2391 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2393 gboolean is_private)
2397 g_return_if_fail (bookmark != NULL);
2398 g_return_if_fail (uri != NULL);
2400 item = g_bookmark_file_lookup_item (bookmark, uri);
2403 item = bookmark_item_new (uri);
2404 g_bookmark_file_add_item (bookmark, item, NULL);
2407 if (!item->metadata)
2408 item->metadata = bookmark_metadata_new ();
2410 item->metadata->is_private = (is_private == TRUE);
2411 item->modified = time (NULL);
2415 * g_bookmark_file_get_is_private:
2416 * @bookmark: a #GBookmarkFile
2418 * @error: return location for a #GError, or %NULL
2420 * Gets whether the private flag of the bookmark for @uri is set.
2422 * In the event the URI cannot be found, %FALSE is returned and
2423 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2424 * event that the private flag cannot be found, %FALSE is returned and
2425 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2427 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2432 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2438 g_return_val_if_fail (bookmark != NULL, FALSE);
2439 g_return_val_if_fail (uri != NULL, FALSE);
2441 item = g_bookmark_file_lookup_item (bookmark, uri);
2444 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2445 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2446 _("No bookmark found for URI '%s'"),
2451 if (!item->metadata)
2453 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2454 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2455 _("No private flag has been defined in bookmark for URI '%s'"),
2460 return item->metadata->is_private;
2464 * g_bookmark_file_set_added:
2465 * @bookmark: a #GBookmarkFile
2467 * @added: a timestamp or -1 to use the current time
2469 * Sets the time the bookmark for @uri was added into @bookmark.
2471 * If no bookmark for @uri is found then it is created.
2476 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2482 g_return_if_fail (bookmark != NULL);
2483 g_return_if_fail (uri != NULL);
2485 item = g_bookmark_file_lookup_item (bookmark, uri);
2488 item = bookmark_item_new (uri);
2489 g_bookmark_file_add_item (bookmark, item, NULL);
2492 if (added == (time_t) -1)
2495 item->added = added;
2496 item->modified = added;
2500 * g_bookmark_file_get_added:
2501 * @bookmark: a #GBookmarkFile
2503 * @error: return location for a #GError, or %NULL
2505 * Gets the time the bookmark for @uri was added to @bookmark
2507 * In the event the URI cannot be found, -1 is returned and
2508 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2510 * Return value: a timestamp
2515 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2521 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2522 g_return_val_if_fail (uri != NULL, (time_t) -1);
2524 item = g_bookmark_file_lookup_item (bookmark, uri);
2527 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2528 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2529 _("No bookmark found for URI '%s'"),
2538 * g_bookmark_file_set_modified:
2539 * @bookmark: a #GBookmarkFile
2541 * @modified: a timestamp or -1 to use the current time
2543 * Sets the last time the bookmark for @uri was last modified.
2545 * If no bookmark for @uri is found then it is created.
2547 * The "modified" time should only be set when the bookmark's meta-data
2548 * was actually changed. Every function of #GBookmarkFile that
2549 * modifies a bookmark also changes the modification time, except for
2550 * g_bookmark_file_set_visited().
2555 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2561 g_return_if_fail (bookmark != NULL);
2562 g_return_if_fail (uri != NULL);
2564 item = g_bookmark_file_lookup_item (bookmark, uri);
2567 item = bookmark_item_new (uri);
2568 g_bookmark_file_add_item (bookmark, item, NULL);
2571 if (modified == (time_t) -1)
2574 item->modified = modified;
2578 * g_bookmark_file_get_modified:
2579 * @bookmark: a #GBookmarkFile
2581 * @error: return location for a #GError, or %NULL
2583 * Gets the time when the bookmark for @uri was last modified.
2585 * In the event the URI cannot be found, -1 is returned and
2586 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2588 * Return value: a timestamp
2593 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2599 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2600 g_return_val_if_fail (uri != NULL, (time_t) -1);
2602 item = g_bookmark_file_lookup_item (bookmark, uri);
2605 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2606 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2607 _("No bookmark found for URI '%s'"),
2612 return item->modified;
2616 * g_bookmark_file_set_visited:
2617 * @bookmark: a #GBookmarkFile
2619 * @visited: a timestamp or -1 to use the current time
2621 * Sets the time the bookmark for @uri was last visited.
2623 * If no bookmark for @uri is found then it is created.
2625 * The "visited" time should only be set if the bookmark was launched,
2626 * either using the command line retrieved by g_bookmark_file_get_app_info()
2627 * or by the default application for the bookmark's MIME type, retrieved
2628 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2629 * does not affect the "modified" time.
2634 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2640 g_return_if_fail (bookmark != NULL);
2641 g_return_if_fail (uri != NULL);
2643 item = g_bookmark_file_lookup_item (bookmark, uri);
2646 item = bookmark_item_new (uri);
2647 g_bookmark_file_add_item (bookmark, item, NULL);
2650 if (visited == (time_t) -1)
2653 item->visited = visited;
2657 * g_bookmark_file_get_visited:
2658 * @bookmark: a #GBookmarkFile
2660 * @error: return location for a #GError, or %NULL
2662 * Gets the time the bookmark for @uri was last visited.
2664 * In the event the URI cannot be found, -1 is returned and
2665 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2667 * Return value: a timestamp.
2672 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2678 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2679 g_return_val_if_fail (uri != NULL, (time_t) -1);
2681 item = g_bookmark_file_lookup_item (bookmark, uri);
2684 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2685 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2686 _("No bookmark found for URI '%s'"),
2691 return item->visited;
2695 * g_bookmark_file_has_group:
2696 * @bookmark: a #GBookmarkFile
2698 * @group: the group name to be searched
2699 * @error: return location for a #GError, or %NULL
2701 * Checks whether @group appears in the list of groups to which
2702 * the bookmark for @uri belongs to.
2704 * In the event the URI cannot be found, %FALSE is returned and
2705 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2707 * Return value: %TRUE if @group was found.
2712 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2720 g_return_val_if_fail (bookmark != NULL, FALSE);
2721 g_return_val_if_fail (uri != NULL, FALSE);
2723 item = g_bookmark_file_lookup_item (bookmark, uri);
2726 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2727 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2728 _("No bookmark found for URI '%s'"),
2733 if (!item->metadata)
2736 for (l = item->metadata->groups; l != NULL; l = l->next)
2738 if (strcmp (l->data, group) == 0)
2747 * g_bookmark_file_add_group:
2748 * @bookmark: a #GBookmarkFile
2750 * @group: the group name to be added
2752 * Adds @group to the list of groups to which the bookmark for @uri
2755 * If no bookmark for @uri is found then it is created.
2760 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2766 g_return_if_fail (bookmark != NULL);
2767 g_return_if_fail (uri != NULL);
2768 g_return_if_fail (group != NULL && group[0] != '\0');
2770 item = g_bookmark_file_lookup_item (bookmark, uri);
2773 item = bookmark_item_new (uri);
2774 g_bookmark_file_add_item (bookmark, item, NULL);
2777 if (!item->metadata)
2778 item->metadata = bookmark_metadata_new ();
2780 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2782 item->metadata->groups = g_list_prepend (item->metadata->groups,
2785 item->modified = time (NULL);
2790 * g_bookmark_file_remove_group:
2791 * @bookmark: a #GBookmarkFile
2793 * @group: the group name to be removed
2794 * @error: return location for a #GError, or %NULL
2796 * Removes @group from the list of groups to which the bookmark
2797 * for @uri belongs to.
2799 * In the event the URI cannot be found, %FALSE is returned and
2800 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2801 * In the event no group was defined, %FALSE is returned and
2802 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2804 * Return value: %TRUE if @group was successfully removed.
2809 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2817 g_return_val_if_fail (bookmark != NULL, FALSE);
2818 g_return_val_if_fail (uri != NULL, FALSE);
2820 item = g_bookmark_file_lookup_item (bookmark, uri);
2823 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2824 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2825 _("No bookmark found for URI '%s'"),
2830 if (!item->metadata)
2832 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2833 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2834 _("No groups set in bookmark for URI '%s'"),
2839 for (l = item->metadata->groups; l != NULL; l = l->next)
2841 if (strcmp (l->data, group) == 0)
2843 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2846 item->modified = time (NULL);
2856 * g_bookmark_file_set_groups:
2857 * @bookmark: a #GBookmarkFile
2858 * @uri: an item's URI
2859 * @groups: an array of group names, or %NULL to remove all groups
2860 * @length: number of group name values in @groups
2862 * Sets a list of group names for the item with URI @uri. Each previously
2863 * set group name list is removed.
2865 * If @uri cannot be found then an item for it is created.
2870 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2872 const gchar **groups,
2878 g_return_if_fail (bookmark != NULL);
2879 g_return_if_fail (uri != NULL);
2880 g_return_if_fail (groups != NULL);
2882 item = g_bookmark_file_lookup_item (bookmark, uri);
2885 item = bookmark_item_new (uri);
2886 g_bookmark_file_add_item (bookmark, item, NULL);
2889 if (!item->metadata)
2890 item->metadata = bookmark_metadata_new ();
2892 if (item->metadata->groups != NULL)
2894 g_list_foreach (item->metadata->groups,
2897 g_list_free (item->metadata->groups);
2898 item->metadata->groups = NULL;
2903 for (i = 0; groups[i] != NULL && i < length; i++)
2904 item->metadata->groups = g_list_append (item->metadata->groups,
2905 g_strdup (groups[i]));
2908 item->modified = time (NULL);
2912 * g_bookmark_file_get_groups:
2913 * @bookmark: a #GBookmarkFile
2915 * @length: return location for the length of the returned string, or %NULL
2916 * @error: return location for a #GError, or %NULL
2918 * Retrieves the list of group names of the bookmark for @uri.
2920 * In the event the URI cannot be found, %NULL is returned and
2921 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2923 * The returned array is %NULL terminated, so @length may optionally
2926 * Return value: a newly allocated %NULL-terminated array of group names.
2927 * Use g_strfreev() to free it.
2930 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2940 g_return_val_if_fail (bookmark != NULL, NULL);
2941 g_return_val_if_fail (uri != NULL, NULL);
2943 item = g_bookmark_file_lookup_item (bookmark, uri);
2946 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2947 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2948 _("No bookmark found for URI '%s'"),
2953 if (!item->metadata)
2961 len = g_list_length (item->metadata->groups);
2962 retval = g_new0 (gchar *, len + 1);
2963 for (l = g_list_last (item->metadata->groups), i = 0;
2967 gchar *group_name = (gchar *) l->data;
2969 g_assert (group_name != NULL);
2971 retval[i++] = g_strdup (group_name);
2982 * g_bookmark_file_add_application:
2983 * @bookmark: a #GBookmarkFile
2985 * @name: the name of the application registering the bookmark
2987 * @exec: command line to be used to launch the bookmark or %NULL
2989 * Adds the application with @name and @exec to the list of
2990 * applications that have registered a bookmark for @uri into
2993 * Every bookmark inside a #GBookmarkFile must have at least an
2994 * application registered. Each application must provide a name, a
2995 * command line useful for launching the bookmark, the number of times
2996 * the bookmark has been registered by the application and the last
2997 * time the application registered this bookmark.
2999 * If @name is %NULL, the name of the application will be the
3000 * same returned by g_get_application(); if @exec is %NULL, the
3001 * command line will be a composition of the program name as
3002 * returned by g_get_prgname() and the "%u" modifier, which will be
3003 * expanded to the bookmark's URI.
3005 * This function will automatically take care of updating the
3006 * registrations count and timestamping in case an application
3007 * with the same @name had already registered a bookmark for
3008 * @uri inside @bookmark.
3010 * If no bookmark for @uri is found, one is created.
3015 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3021 gchar *app_name, *app_exec;
3023 g_return_if_fail (bookmark != NULL);
3024 g_return_if_fail (uri != NULL);
3026 item = g_bookmark_file_lookup_item (bookmark, uri);
3029 item = bookmark_item_new (uri);
3030 g_bookmark_file_add_item (bookmark, item, NULL);
3033 if (name && name[0] != '\0')
3034 app_name = g_strdup (name);
3036 app_name = g_strdup (g_get_application_name ());
3038 if (exec && exec[0] != '\0')
3039 app_exec = g_strdup (exec);
3041 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3043 g_bookmark_file_set_app_info (bookmark, uri,
3055 * g_bookmark_file_remove_application:
3056 * @bookmark: a #GBookmarkFile
3058 * @name: the name of the application
3059 * @error: return location for a #GError or %NULL
3061 * Removes application registered with @name from the list of applications
3062 * that have registered a bookmark for @uri inside @bookmark.
3064 * In the event the URI cannot be found, %FALSE is returned and
3065 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3066 * In the event that no application with name @app_name has registered
3067 * a bookmark for @uri, %FALSE is returned and error is set to
3068 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3070 * Return value: %TRUE if the application was successfully removed.
3075 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3083 g_return_val_if_fail (bookmark != NULL, FALSE);
3084 g_return_val_if_fail (uri != NULL, FALSE);
3085 g_return_val_if_fail (name != NULL, FALSE);
3088 retval = g_bookmark_file_set_app_info (bookmark, uri,
3096 g_propagate_error (error, set_error);
3105 * g_bookmark_file_has_application:
3106 * @bookmark: a #GBookmarkFile
3108 * @name: the name of the application
3109 * @error: return location for a #GError or %NULL
3111 * Checks whether the bookmark for @uri inside @bookmark has been
3112 * registered by application @name.
3114 * In the event the URI cannot be found, %FALSE is returned and
3115 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3117 * Return value: %TRUE if the application @name was found
3122 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3129 g_return_val_if_fail (bookmark != NULL, FALSE);
3130 g_return_val_if_fail (uri != NULL, FALSE);
3131 g_return_val_if_fail (name != NULL, FALSE);
3133 item = g_bookmark_file_lookup_item (bookmark, uri);
3136 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3137 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3138 _("No bookmark found for URI '%s'"),
3143 return (NULL != bookmark_item_lookup_app_info (item, name));
3147 * g_bookmark_file_set_app_info:
3148 * @bookmark: a #GBookmarkFile
3150 * @name: an application's name
3151 * @exec: an application's command line
3152 * @count: the number of registrations done for this application
3153 * @stamp: the time of the last registration for this application
3154 * @error: return location for a #GError or %NULL
3156 * Sets the meta-data of application @name inside the list of
3157 * applications that have registered a bookmark for @uri inside
3160 * You should rarely use this function; use g_bookmark_file_add_application()
3161 * and g_bookmark_file_remove_application() instead.
3163 * @name can be any UTF-8 encoded string used to identify an
3165 * @exec can have one of these two modifiers: "%f", which will
3166 * be expanded as the local file name retrieved from the bookmark's
3167 * URI; "%u", which will be expanded as the bookmark's URI.
3168 * The expansion is done automatically when retrieving the stored
3169 * command line using the g_bookmark_file_get_app_info() function.
3170 * @count is the number of times the application has registered the
3171 * bookmark; if is < 0, the current registration count will be increased
3172 * by one, if is 0, the application with @name will be removed from
3173 * the list of registered applications.
3174 * @stamp is the Unix time of the last registration; if it is -1, the
3175 * current time will be used.
3177 * If you try to remove an application by setting its registration count to
3178 * zero, and no bookmark for @uri is found, %FALSE is returned and
3179 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3180 * in the event that no application @name has registered a bookmark
3181 * for @uri, %FALSE is returned and error is set to
3182 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3183 * for @uri is found, one is created.
3185 * Return value: %TRUE if the application's meta-data was successfully
3191 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3200 BookmarkAppInfo *ai;
3202 g_return_val_if_fail (bookmark != NULL, FALSE);
3203 g_return_val_if_fail (uri != NULL, FALSE);
3204 g_return_val_if_fail (name != NULL, FALSE);
3205 g_return_val_if_fail (exec != NULL, FALSE);
3207 item = g_bookmark_file_lookup_item (bookmark, uri);
3212 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3213 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3214 _("No bookmark found for URI '%s'"),
3220 item = bookmark_item_new (uri);
3221 g_bookmark_file_add_item (bookmark, item, NULL);
3225 ai = bookmark_item_lookup_app_info (item, name);
3230 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3231 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3232 _("No application with name '%s' registered a bookmark for '%s'"),
3239 ai = bookmark_app_info_new (name);
3241 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3242 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3248 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3249 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3251 item->modified = time (NULL);
3260 if (stamp != (time_t) -1)
3263 ai->stamp = time (NULL);
3265 if (exec && exec[0] != '\0')
3268 ai->exec = g_strdup (exec);
3271 item->modified = time (NULL);
3276 /* expands the application's command line */
3278 expand_exec_line (const gchar *exec_fmt,
3284 exec = g_string_new (NULL);
3285 while ((ch = *exec_fmt++) != '\0')
3289 exec = g_string_append_c (exec, ch);
3297 g_string_append_printf (exec, "%s", uri);
3301 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3302 g_string_append_printf (exec, "%s", file);
3308 exec = g_string_append_c (exec, ch);
3313 return g_string_free (exec, FALSE);
3317 * g_bookmark_file_get_app_info:
3318 * @bookmark: a #GBookmarkFile
3320 * @name: an application's name
3321 * @exec: location for the command line of the application, or %NULL
3322 * @count: return location for the registration count, or %NULL
3323 * @stamp: return location for the last registration time, or %NULL
3324 * @error: return location for a #GError, or %NULL
3326 * Gets the registration informations of @app_name for the bookmark for
3327 * @uri. See g_bookmark_file_set_app_info() for more informations about
3328 * the returned data.
3330 * The string returned in @app_exec must be freed.
3332 * In the event the URI cannot be found, %FALSE is returned and
3333 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3334 * event that no application with name @app_name has registered a bookmark
3335 * for @uri, %FALSE is returned and error is set to
3336 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3338 * Return value: %TRUE on success.
3343 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3352 BookmarkAppInfo *ai;
3354 g_return_val_if_fail (bookmark != NULL, FALSE);
3355 g_return_val_if_fail (uri != NULL, FALSE);
3356 g_return_val_if_fail (name != NULL, FALSE);
3358 item = g_bookmark_file_lookup_item (bookmark, uri);
3361 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3362 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3363 _("No bookmark found for URI '%s'"),
3368 ai = bookmark_item_lookup_app_info (item, name);
3371 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3372 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3373 _("No application with name '%s' registered a bookmark for '%s'"),
3380 *exec = expand_exec_line (ai->exec, uri);
3392 * g_bookmark_file_get_applications:
3393 * @bookmark: a #GBookmarkFile
3395 * @length: return location of the length of the returned list, or %NULL
3396 * @error: return location for a #GError, or %NULL
3398 * Retrieves the names of the applications that have registered the
3399 * bookmark for @uri.
3401 * In the event the URI cannot be found, %NULL is returned and
3402 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3404 * Return value: a newly allocated %NULL-terminated array of strings.
3405 * Use g_strfreev() to free it.
3410 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3420 g_return_val_if_fail (bookmark != NULL, NULL);
3421 g_return_val_if_fail (uri != NULL, NULL);
3423 item = g_bookmark_file_lookup_item (bookmark, uri);
3426 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3427 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3428 _("No bookmark found for URI '%s'"),
3433 if (!item->metadata)
3441 n_apps = g_list_length (item->metadata->applications);
3442 apps = g_new0 (gchar *, n_apps + 1);
3444 for (l = g_list_last (item->metadata->applications), i = 0;
3448 BookmarkAppInfo *ai;
3450 ai = (BookmarkAppInfo *) l->data;
3452 g_assert (ai != NULL);
3453 g_assert (ai->name != NULL);
3455 apps[i++] = g_strdup (ai->name);
3466 * g_bookmark_file_get_size:
3467 * @bookmark: a #GBookmarkFile
3469 * Gets the number of bookmarks inside @bookmark.
3471 * Return value: the number of bookmarks
3476 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3478 g_return_val_if_fail (bookmark != NULL, 0);
3480 return g_list_length (bookmark->items);
3484 * g_bookmark_file_move_item:
3485 * @bookmark: a #GBookmarkFile
3486 * @old_uri: a valid URI
3487 * @new_uri: a valid URI, or %NULL
3488 * @error: return location for a #GError or %NULL
3490 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3491 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3492 * %NULL, then the bookmark is removed.
3494 * In the event the URI cannot be found, %FALSE is returned and
3495 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3497 * Return value: %TRUE if the URI was successfully changed
3502 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3503 const gchar *old_uri,
3504 const gchar *new_uri,
3508 GError *remove_error;
3510 g_return_val_if_fail (bookmark != NULL, FALSE);
3511 g_return_val_if_fail (old_uri != NULL, FALSE);
3513 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3516 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3517 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3518 _("No bookmark found for URI '%s'"),
3523 if (new_uri && new_uri[0] != '\0')
3525 if (g_bookmark_file_has_item (bookmark, new_uri))
3527 remove_error = NULL;
3528 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3531 g_propagate_error (error, remove_error);
3538 item->uri = g_strdup (new_uri);
3539 item->modified = time (NULL);
3545 remove_error = NULL;
3546 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3549 g_propagate_error (error, remove_error);
3559 * g_bookmark_file_set_icon:
3560 * @bookmark: a #GBookmarkFile
3562 * @href: the URI of the icon for the bookmark, or %NULL
3563 * @mime_type: the MIME type of the icon for the bookmark
3565 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3566 * the currently set icon.
3568 * If no bookmark for @uri is found it is created.
3573 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3576 const gchar *mime_type)
3580 g_return_if_fail (bookmark != NULL);
3581 g_return_if_fail (uri != NULL);
3583 item = g_bookmark_file_lookup_item (bookmark, uri);
3586 item = bookmark_item_new (uri);
3587 g_bookmark_file_add_item (bookmark, item, NULL);
3590 if (!item->metadata)
3591 item->metadata = bookmark_metadata_new ();
3593 g_free (item->metadata->icon_href);
3594 g_free (item->metadata->icon_mime);
3596 if (href && href[0] != '\0')
3597 item->metadata->icon_href = g_strdup (href);
3599 if (mime_type && mime_type[0] != '\0')
3600 item->metadata->icon_mime = g_strdup (mime_type);
3602 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3604 item->modified = time (NULL);
3608 * g_bookmark_file_get_icon:
3609 * @bookmark: a #GBookmarkFile
3611 * @href: return location for the icon's location or %NULL
3612 * @mime_type: return location for the icon's MIME type or %NULL
3613 * @error: return location for a #GError or %NULL
3615 * Gets the icon of the bookmark for @uri.
3617 * In the event the URI cannot be found, %FALSE is returned and
3618 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3620 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3621 * You should free the returned strings.
3626 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3634 g_return_val_if_fail (bookmark != NULL, FALSE);
3635 g_return_val_if_fail (uri != NULL, FALSE);
3637 item = g_bookmark_file_lookup_item (bookmark, uri);
3640 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3641 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3642 _("No bookmark found for URI '%s'"),
3647 if ((!item->metadata) || (!item->metadata->icon_href))
3651 *href = g_strdup (item->metadata->icon_href);
3654 *mime_type = g_strdup (item->metadata->icon_mime);
3659 #define __G_BOOKMARK_FILE_C__
3660 #include "galiasdef.c"