1 /* gbookmarkfile.c: parsing and building desktop bookmarks
3 * Copyright (C) 2005-2006 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 #include "gbookmarkfile.h"
32 #include <sys/types.h>
41 #include "gfileutils.h"
49 #include "gmessages.h"
53 #include "gstrfuncs.h"
59 /* XBEL 1.0 standard entities */
60 #define XBEL_VERSION "1.0"
61 #define XBEL_DTD_NICK "xbel"
62 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
63 "Exchange Language 1.0//EN//XML"
65 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
67 #define XBEL_ROOT_ELEMENT "xbel"
68 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
69 #define XBEL_BOOKMARK_ELEMENT "bookmark"
70 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
71 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
72 #define XBEL_TITLE_ELEMENT "title"
73 #define XBEL_DESC_ELEMENT "desc"
74 #define XBEL_INFO_ELEMENT "info"
75 #define XBEL_METADATA_ELEMENT "metadata"
77 #define XBEL_VERSION_ATTRIBUTE "version"
78 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
79 #define XBEL_OWNER_ATTRIBUTE "owner"
80 #define XBEL_ADDED_ATTRIBUTE "added"
81 #define XBEL_VISITED_ATTRIBUTE "visited"
82 #define XBEL_MODIFIED_ATTRIBUTE "modified"
83 #define XBEL_ID_ATTRIBUTE "id"
84 #define XBEL_HREF_ATTRIBUTE "href"
85 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
87 #define XBEL_YES_VALUE "yes"
88 #define XBEL_NO_VALUE "no"
90 /* Desktop bookmark spec entities */
91 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
93 #define BOOKMARK_NAMESPACE_NAME "bookmark"
94 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
96 #define BOOKMARK_GROUPS_ELEMENT "groups"
97 #define BOOKMARK_GROUP_ELEMENT "group"
98 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
99 #define BOOKMARK_APPLICATION_ELEMENT "application"
100 #define BOOKMARK_ICON_ELEMENT "icon"
101 #define BOOKMARK_PRIVATE_ELEMENT "private"
103 #define BOOKMARK_NAME_ATTRIBUTE "name"
104 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
105 #define BOOKMARK_COUNT_ATTRIBUTE "count"
106 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp"
107 #define BOOKMARK_HREF_ATTRIBUTE "href"
108 #define BOOKMARK_TYPE_ATTRIBUTE "type"
110 /* Shared MIME Info entities */
111 #define MIME_NAMESPACE_NAME "mime"
112 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
113 #define MIME_TYPE_ELEMENT "mime-type"
114 #define MIME_TYPE_ATTRIBUTE "type"
117 typedef struct _BookmarkAppInfo BookmarkAppInfo;
118 typedef struct _BookmarkMetadata BookmarkMetadata;
119 typedef struct _BookmarkItem BookmarkItem;
120 typedef struct _ParseData ParseData;
122 struct _BookmarkAppInfo
132 struct _BookmarkMetadata
139 GHashTable *apps_by_name;
144 guint is_private : 1;
158 BookmarkMetadata *metadata;
161 struct _GBookmarkFile
166 /* we store our items in a list and keep a copy inside
167 * an hash table for faster lookup performances
170 GHashTable *items_by_uri;
173 /* parser state machine */
194 static void g_bookmark_file_init (GBookmarkFile *bookmark);
195 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
196 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
200 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
203 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
205 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
209 static time_t timestamp_from_iso8601 (const gchar *iso_date);
210 static gchar * timestamp_to_iso8601 (time_t timestamp);
212 /********************************
215 * Application metadata storage *
216 ********************************/
217 static BookmarkAppInfo *
218 bookmark_app_info_new (const gchar *name)
220 BookmarkAppInfo *retval;
222 g_assert (name != NULL);
224 retval = g_slice_new (BookmarkAppInfo);
226 retval->name = g_strdup (name);
229 retval->stamp = time (NULL);
235 bookmark_app_info_free (BookmarkAppInfo *app_info)
240 g_free (app_info->name);
241 g_free (app_info->exec);
243 g_slice_free (BookmarkAppInfo, app_info);
247 bookmark_app_info_dump (BookmarkAppInfo *app_info)
252 g_assert (app_info != NULL);
254 if (app_info->count == 0)
257 name = g_markup_escape_text (app_info->name, -1);
258 exec = g_markup_escape_text (app_info->exec, -1);
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, name,
264 BOOKMARK_EXEC_ATTRIBUTE, exec,
265 BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
266 BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
275 /***********************
279 ***********************/
280 static BookmarkMetadata *
281 bookmark_metadata_new (void)
283 BookmarkMetadata *retval;
285 retval = g_slice_new (BookmarkMetadata);
287 retval->mime_type = NULL;
289 retval->groups = NULL;
291 retval->applications = NULL;
292 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
297 retval->is_private = FALSE;
299 retval->icon_href = NULL;
300 retval->icon_mime = NULL;
306 bookmark_metadata_free (BookmarkMetadata *metadata)
311 g_free (metadata->mime_type);
313 if (metadata->groups)
315 g_list_foreach (metadata->groups,
318 g_list_free (metadata->groups);
321 if (metadata->applications)
323 g_list_foreach (metadata->applications,
324 (GFunc) bookmark_app_info_free,
326 g_list_free (metadata->applications);
328 g_hash_table_destroy (metadata->apps_by_name);
331 g_free (metadata->icon_href);
332 g_free (metadata->icon_mime);
334 g_slice_free (BookmarkMetadata, metadata);
338 bookmark_metadata_dump (BookmarkMetadata *metadata)
342 if (!metadata->applications)
345 retval = g_string_new (NULL);
347 /* metadata container */
348 g_string_append_printf (retval,
350 XBEL_METADATA_ELEMENT,
351 XBEL_OWNER_ATTRIBUTE, BOOKMARK_METADATA_OWNER);
354 if (metadata->mime_type)
355 g_string_append_printf (retval,
356 " <%s:%s %s=\"%s\"/>\n",
359 MIME_TYPE_ATTRIBUTE, metadata->mime_type);
361 if (metadata->groups)
365 /* open groups container */
366 g_string_append_printf (retval,
368 BOOKMARK_NAMESPACE_NAME,
369 BOOKMARK_GROUPS_ELEMENT);
371 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
375 group_name = g_markup_escape_text ((gchar *) l->data, -1);
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);
386 /* close groups container */
387 g_string_append_printf (retval,
389 BOOKMARK_NAMESPACE_NAME,
390 BOOKMARK_GROUPS_ELEMENT);
393 if (metadata->applications)
397 /* open applications container */
398 g_string_append_printf (retval,
400 BOOKMARK_NAMESPACE_NAME,
401 BOOKMARK_APPLICATIONS_ELEMENT);
403 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
405 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
408 g_assert (app_info != NULL);
410 app_data = bookmark_app_info_dump (app_info);
414 retval = g_string_append (retval, app_data);
420 /* close applications container */
421 g_string_append_printf (retval,
423 BOOKMARK_NAMESPACE_NAME,
424 BOOKMARK_APPLICATIONS_ELEMENT);
428 if (metadata->icon_href)
430 if (!metadata->icon_mime)
431 metadata->icon_mime = g_strdup ("application/octet-stream");
433 g_string_append_printf (retval,
434 " <%s:%s %s=\"%s\" %s=\"%s\"/>\n",
435 BOOKMARK_NAMESPACE_NAME,
436 BOOKMARK_ICON_ELEMENT,
437 BOOKMARK_HREF_ATTRIBUTE, metadata->icon_href,
438 BOOKMARK_TYPE_ATTRIBUTE, metadata->icon_mime);
442 if (metadata->is_private)
443 g_string_append_printf (retval,
445 BOOKMARK_NAMESPACE_NAME,
446 BOOKMARK_PRIVATE_ELEMENT);
448 /* close metadata container */
449 g_string_append_printf (retval, " </%s>\n", XBEL_METADATA_ELEMENT);
451 return g_string_free (retval, FALSE);
454 /******************************************************
457 * Storage for a single bookmark item inside the list *
458 ******************************************************/
459 static BookmarkItem *
460 bookmark_item_new (const gchar *uri)
464 g_assert (uri != NULL);
466 item = g_slice_new (BookmarkItem);
467 item->uri = g_strdup (uri);
470 item->description = NULL;
472 item->added = (time_t) -1;
473 item->modified = (time_t) -1;
474 item->visited = (time_t) -1;
476 item->metadata = NULL;
482 bookmark_item_free (BookmarkItem *item)
488 g_free (item->title);
489 g_free (item->description);
492 bookmark_metadata_free (item->metadata);
494 g_slice_free (BookmarkItem, item);
498 bookmark_item_dump (BookmarkItem *item)
501 gchar *added, *visited, *modified;
504 /* at this point, we must have at least a registered application; if we don't
505 * we don't screw up the bookmark file, and just skip this item
507 if (!item->metadata || !item->metadata->applications)
509 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
513 retval = g_string_new (NULL);
515 added = timestamp_to_iso8601 (item->added);
516 modified = timestamp_to_iso8601 (item->modified);
517 visited = timestamp_to_iso8601 (item->visited);
519 escaped_uri = g_markup_escape_text (item->uri, -1);
521 g_string_append_printf (retval,
522 " <%s %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\">\n",
523 XBEL_BOOKMARK_ELEMENT,
524 XBEL_HREF_ATTRIBUTE, escaped_uri,
525 XBEL_ADDED_ATTRIBUTE, added,
526 XBEL_MODIFIED_ATTRIBUTE, modified,
527 XBEL_VISITED_ATTRIBUTE, visited);
528 g_free (escaped_uri);
535 gchar *escaped_title;
537 escaped_title = g_markup_escape_text (item->title, -1);
538 g_string_append_printf (retval,
543 g_free (escaped_title);
546 if (item->description)
550 escaped_desc = g_markup_escape_text (item->description, -1);
551 g_string_append_printf (retval,
556 g_free (escaped_desc);
563 /* open info container */
564 g_string_append_printf (retval, " <%s>\n", XBEL_INFO_ELEMENT);
566 metadata = bookmark_metadata_dump (item->metadata);
569 retval = g_string_append (retval, metadata);
574 /* close info container */
575 g_string_append_printf (retval, " </%s>\n", XBEL_INFO_ELEMENT);
578 g_string_append_printf (retval, " </%s>\n", XBEL_BOOKMARK_ELEMENT);
580 return g_string_free (retval, FALSE);
583 static BookmarkAppInfo *
584 bookmark_item_lookup_app_info (BookmarkItem *item,
585 const gchar *app_name)
587 g_assert (item != NULL && app_name != NULL);
592 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
595 /*************************
597 *************************/
600 g_bookmark_file_init (GBookmarkFile *bookmark)
602 bookmark->title = NULL;
603 bookmark->description = NULL;
605 bookmark->items = NULL;
606 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
613 g_bookmark_file_clear (GBookmarkFile *bookmark)
615 g_free (bookmark->title);
616 g_free (bookmark->description);
620 g_list_foreach (bookmark->items,
621 (GFunc) bookmark_item_free,
623 g_list_free (bookmark->items);
625 bookmark->items = NULL;
628 if (bookmark->items_by_uri)
630 g_hash_table_destroy (bookmark->items_by_uri);
632 bookmark->items_by_uri = NULL;
640 GHashTable *namespaces;
642 GBookmarkFile *bookmark_file;
643 BookmarkItem *current_item;
647 parse_data_new (void)
651 retval = g_new (ParseData, 1);
653 retval->state = STATE_STARTED;
654 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
655 (GDestroyNotify) g_free,
656 (GDestroyNotify) g_free);
657 retval->bookmark_file = NULL;
658 retval->current_item = NULL;
664 parse_data_free (ParseData *parse_data)
666 g_hash_table_destroy (parse_data->namespaces);
671 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
674 parse_bookmark_element (GMarkupParseContext *context,
675 ParseData *parse_data,
676 const gchar **attribute_names,
677 const gchar **attribute_values,
680 const gchar *uri, *added, *modified, *visited;
686 g_assert ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
689 uri = added = modified = visited = NULL;
690 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
692 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
693 uri = attribute_values[i];
694 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
695 added = attribute_values[i];
696 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
697 modified = attribute_values[i];
698 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
699 visited = attribute_values[i];
702 g_set_error (error, G_MARKUP_ERROR,
703 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
704 _("Unexpected attribute '%s' for element '%s'"),
706 XBEL_BOOKMARK_ELEMENT);
713 g_set_error (error, G_MARKUP_ERROR,
714 G_MARKUP_ERROR_INVALID_CONTENT,
715 _("Attribute '%s' of element '%s' not found"),
717 XBEL_BOOKMARK_ELEMENT);
721 g_assert (parse_data->current_item == NULL);
723 item = bookmark_item_new (uri);
726 item->added = timestamp_from_iso8601 (added);
729 item->modified = timestamp_from_iso8601 (modified);
732 item->visited = timestamp_from_iso8601 (visited);
735 g_bookmark_file_add_item (parse_data->bookmark_file,
740 bookmark_item_free (item);
742 g_propagate_error (error, add_error);
747 parse_data->current_item = item;
751 parse_application_element (GMarkupParseContext *context,
752 ParseData *parse_data,
753 const gchar **attribute_names,
754 const gchar **attribute_values,
757 const gchar *name, *exec, *count, *stamp;
763 g_assert ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
766 name = exec = count = stamp = NULL;
767 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
769 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
770 name = attribute_values[i];
771 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
772 exec = attribute_values[i];
773 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
774 count = attribute_values[i];
775 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
776 stamp = attribute_values[i];
779 g_set_error (error, G_MARKUP_ERROR,
780 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
781 _("Unexpected attribute '%s' for element '%s'"),
783 BOOKMARK_APPLICATION_ELEMENT);
790 g_set_error (error, G_MARKUP_ERROR,
791 G_MARKUP_ERROR_INVALID_CONTENT,
792 _("Attribute '%s' of element '%s' not found"),
793 BOOKMARK_NAME_ATTRIBUTE,
794 BOOKMARK_APPLICATION_ELEMENT);
800 g_set_error (error, G_MARKUP_ERROR,
801 G_MARKUP_ERROR_INVALID_CONTENT,
802 _("Attribute '%s' of element '%s' not found"),
803 BOOKMARK_EXEC_ATTRIBUTE,
804 BOOKMARK_APPLICATION_ELEMENT);
808 g_assert (parse_data->current_item != NULL);
809 item = parse_data->current_item;
811 ai = bookmark_item_lookup_app_info (item, name);
814 ai = bookmark_app_info_new (name);
817 item->metadata = bookmark_metadata_new ();
819 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
820 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
823 ai->exec = g_strdup (exec);
826 ai->count = atoi (count);
831 ai->stamp = (time_t) atol (stamp);
833 ai->stamp = time (NULL);
837 parse_mime_type_element (GMarkupParseContext *context,
838 ParseData *parse_data,
839 const gchar **attribute_names,
840 const gchar **attribute_values,
848 g_assert ((parse_data != NULL) && (parse_data->state == STATE_MIME));
852 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
854 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
855 type = attribute_values[i];
858 g_set_error (error, G_MARKUP_ERROR,
859 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
860 _("Unexpected attribute '%s' for element '%s'"),
868 type = "application/octet-stream";
870 g_assert (parse_data->current_item != NULL);
871 item = parse_data->current_item;
874 item->metadata = bookmark_metadata_new ();
876 item->metadata->mime_type = g_strdup (type);
880 parse_icon_element (GMarkupParseContext *context,
881 ParseData *parse_data,
882 const gchar **attribute_names,
883 const gchar **attribute_values,
892 g_assert ((parse_data != NULL) && (parse_data->state == STATE_ICON));
897 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
899 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
900 href = attribute_values[i];
901 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
902 type = attribute_values[i];
905 g_set_error (error, G_MARKUP_ERROR,
906 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
907 _("Unexpected attribute '%s' for element '%s'"),
909 BOOKMARK_ICON_ELEMENT);
916 g_set_error (error, G_MARKUP_ERROR,
917 G_MARKUP_ERROR_INVALID_CONTENT,
918 _("Attribute '%s' of element '%s' not found"),
919 BOOKMARK_HREF_ATTRIBUTE,
920 BOOKMARK_ICON_ELEMENT);
925 type = "application/octet-stream";
927 g_assert (parse_data->current_item != NULL);
928 item = parse_data->current_item;
931 item->metadata = bookmark_metadata_new ();
933 item->metadata->icon_href = g_strdup (href);
934 item->metadata->icon_mime = g_strdup (type);
937 /* scans through the attributes of an element for the "xmlns" pragma, and
938 * adds any resulting namespace declaration to a per-parser hashtable, using
939 * the namespace name as a key for the namespace URI; if no key was found,
940 * the namespace is considered as default, and stored under the "default" key.
942 * FIXME: this works on the assumption that the generator of the XBEL file
943 * is either this code or is smart enough to place the namespace declarations
944 * inside the main root node or inside the metadata node and does not redefine
945 * a namespace inside an inner node; this does *not* conform to the
946 * XML-NS standard, although is a close approximation. In order to make this
947 * conformant to the XML-NS specification we should use a per-element
948 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
951 map_namespace_to_name (ParseData *parse_data,
952 const gchar **attribute_names,
953 const gchar **attribute_values)
958 g_assert (parse_data != NULL);
960 if (!attribute_names || !attribute_names[0])
964 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
966 if (g_str_has_prefix (attr, "xmlns"))
968 gchar *namespace_name, *namespace_uri;
971 p = g_utf8_strchr (attr, -1, ':');
973 p = g_utf8_next_char (p);
977 namespace_name = g_strdup (p);
978 namespace_uri = g_strdup (attribute_values[i]);
980 g_hash_table_replace (parse_data->namespaces,
987 /* checks whether @element_full is equal to @element.
989 * if @namespace is set, it tries to resolve the namespace to a known URI,
990 * and if found is prepended to the element name, from which is separated
991 * using the character specified in the @sep parameter.
994 is_element_full (ParseData *parse_data,
995 const gchar *element_full,
996 const gchar *namespace,
997 const gchar *element,
1000 gchar *ns_uri, *ns_name, *s, *resolved;
1001 const gchar *p, *element_name;
1004 g_assert (parse_data != NULL);
1005 g_assert (element_full != NULL);
1010 /* no namespace requested: dumb element compare */
1012 return (0 == strcmp (element_full, element));
1014 /* search for namespace separator; if none found, assume we are under the
1015 * default namespace, and set ns_name to our "default" marker; if no default
1016 * namespace has been set, just do a plain comparison between @full_element
1019 p = strchr (element_full, ':');
1022 ns_name = g_strndup (element_full, p - element_full);
1023 element_name = g_utf8_next_char (p);
1027 ns_name = g_strdup ("default");
1028 element_name = element_full;
1031 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1034 /* no default namespace found */
1037 return (0 == strcmp (element_full, element));
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));
1051 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1052 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1055 start_element_raw_cb (GMarkupParseContext *context,
1056 const gchar *element_name,
1057 const gchar **attribute_names,
1058 const gchar **attribute_values,
1062 ParseData *parse_data = (ParseData *) user_data;
1064 /* we must check for namespace declarations first
1066 * XXX - we could speed up things by checking for namespace declarations
1067 * only on the root node, where they usually are; this would probably break
1068 * on streams not produced by us or by "smart" generators
1070 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1072 switch (parse_data->state)
1075 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1081 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1083 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1084 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1085 parse_data->state = STATE_ROOT;
1089 g_set_error (error, G_MARKUP_ERROR,
1090 G_MARKUP_ERROR_INVALID_CONTENT,
1091 _("Unexpected tag '%s', tag '%s' expected"),
1092 element_name, XBEL_ROOT_ELEMENT);
1095 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1096 parse_data->state = STATE_TITLE;
1097 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1098 parse_data->state = STATE_DESC;
1099 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1101 GError *inner_error = NULL;
1103 parse_data->state = STATE_BOOKMARK;
1105 parse_bookmark_element (context,
1111 g_propagate_error (error, inner_error);
1114 g_set_error (error, G_MARKUP_ERROR,
1115 G_MARKUP_ERROR_INVALID_CONTENT,
1116 _("Unexpected tag '%s' inside '%s'"),
1120 case STATE_BOOKMARK:
1121 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1122 parse_data->state = STATE_TITLE;
1123 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1124 parse_data->state = STATE_DESC;
1125 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1126 parse_data->state = STATE_INFO;
1128 g_set_error (error, G_MARKUP_ERROR,
1129 G_MARKUP_ERROR_INVALID_CONTENT,
1130 _("Unexpected tag '%s' inside '%s'"),
1132 XBEL_BOOKMARK_ELEMENT);
1135 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1141 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1143 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1144 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1146 parse_data->state = STATE_METADATA;
1148 if (!parse_data->current_item->metadata)
1149 parse_data->current_item->metadata = bookmark_metadata_new ();
1154 g_set_error (error, G_MARKUP_ERROR,
1155 G_MARKUP_ERROR_INVALID_CONTENT,
1156 _("Unexpected tag '%s', tag '%s' expected"),
1158 XBEL_METADATA_ELEMENT);
1160 case STATE_METADATA:
1161 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1162 parse_data->state = STATE_APPLICATIONS;
1163 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1164 parse_data->state = STATE_GROUPS;
1165 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1166 parse_data->current_item->metadata->is_private = TRUE;
1167 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1169 GError *inner_error = NULL;
1171 parse_data->state = STATE_ICON;
1173 parse_icon_element (context,
1179 g_propagate_error (error, inner_error);
1181 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1183 GError *inner_error = NULL;
1185 parse_data->state = STATE_MIME;
1187 parse_mime_type_element (context,
1193 g_propagate_error (error, inner_error);
1196 g_set_error (error, G_MARKUP_ERROR,
1197 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1198 _("Unexpected tag '%s' inside '%s'"),
1200 XBEL_METADATA_ELEMENT);
1202 case STATE_APPLICATIONS:
1203 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1205 GError *inner_error = NULL;
1207 parse_data->state = STATE_APPLICATION;
1209 parse_application_element (context,
1215 g_propagate_error (error, inner_error);
1218 g_set_error (error, G_MARKUP_ERROR,
1219 G_MARKUP_ERROR_INVALID_CONTENT,
1220 _("Unexpected tag '%s', tag '%s' expected"),
1222 BOOKMARK_APPLICATION_ELEMENT);
1225 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1226 parse_data->state = STATE_GROUP;
1228 g_set_error (error, G_MARKUP_ERROR,
1229 G_MARKUP_ERROR_INVALID_CONTENT,
1230 _("Unexpected tag '%s', tag '%s' expected"),
1232 BOOKMARK_GROUP_ELEMENT);
1235 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1237 GError *inner_error = NULL;
1239 parse_icon_element (context,
1245 g_propagate_error (error, inner_error);
1248 g_set_error (error, G_MARKUP_ERROR,
1249 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1250 _("Unexpected tag '%s' inside '%s'"),
1252 XBEL_METADATA_ELEMENT);
1255 g_assert_not_reached ();
1261 end_element_raw_cb (GMarkupParseContext *context,
1262 const gchar *element_name,
1266 ParseData *parse_data = (ParseData *) user_data;
1268 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1269 parse_data->state = STATE_FINISHED;
1270 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1272 parse_data->current_item = NULL;
1274 parse_data->state = STATE_ROOT;
1276 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1277 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1278 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1280 if (parse_data->current_item)
1281 parse_data->state = STATE_BOOKMARK;
1283 parse_data->state = STATE_ROOT;
1285 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1286 parse_data->state = STATE_INFO;
1287 else if (IS_ELEMENT_NS (parse_data, element_name,
1288 BOOKMARK_NAMESPACE_URI,
1289 BOOKMARK_APPLICATION_ELEMENT))
1290 parse_data->state = STATE_APPLICATIONS;
1291 else if (IS_ELEMENT_NS (parse_data, element_name,
1292 BOOKMARK_NAMESPACE_URI,
1293 BOOKMARK_GROUP_ELEMENT))
1294 parse_data->state = STATE_GROUPS;
1295 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1296 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1297 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1298 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1299 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1300 parse_data->state = STATE_METADATA;
1304 text_raw_cb (GMarkupParseContext *context,
1310 ParseData *parse_data = (ParseData *) user_data;
1313 payload = g_strndup (text, length);
1315 switch (parse_data->state)
1318 if (parse_data->current_item)
1320 g_free (parse_data->current_item->title);
1321 parse_data->current_item->title = g_strdup (payload);
1325 g_free (parse_data->bookmark_file->title);
1326 parse_data->bookmark_file->title = g_strdup (payload);
1330 if (parse_data->current_item)
1332 g_free (parse_data->current_item->description);
1333 parse_data->current_item->description = g_strdup (payload);
1337 g_free (parse_data->bookmark_file->description);
1338 parse_data->bookmark_file->description = g_strdup (payload);
1345 g_assert (parse_data->current_item != NULL);
1347 if (!parse_data->current_item->metadata)
1348 parse_data->current_item->metadata = bookmark_metadata_new ();
1350 groups = parse_data->current_item->metadata->groups;
1351 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1355 case STATE_BOOKMARK:
1357 case STATE_METADATA:
1358 case STATE_APPLICATIONS:
1359 case STATE_APPLICATION:
1365 g_assert_not_reached ();
1372 static const GMarkupParser markup_parser =
1374 start_element_raw_cb, /* start_element */
1375 end_element_raw_cb, /* end_element */
1376 text_raw_cb, /* text */
1377 NULL, /* passthrough */
1382 g_bookmark_file_parse (GBookmarkFile *bookmark,
1383 const gchar *buffer,
1387 GMarkupParseContext *context;
1388 ParseData *parse_data;
1389 GError *parse_error, *end_error;
1392 g_assert (bookmark != NULL);
1398 length = strlen (buffer);
1400 parse_data = parse_data_new ();
1401 parse_data->bookmark_file = bookmark;
1403 context = g_markup_parse_context_new (&markup_parser,
1406 (GDestroyNotify) parse_data_free);
1409 retval = g_markup_parse_context_parse (context,
1415 g_propagate_error (error, parse_error);
1421 retval = g_markup_parse_context_end_parse (context, &end_error);
1424 g_propagate_error (error, end_error);
1429 g_markup_parse_context_free (context);
1435 g_bookmark_file_dump (GBookmarkFile *bookmark,
1442 retval = g_string_new (NULL);
1444 g_string_append_printf (retval,
1445 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1447 /* XXX - do we really need the doctype? */
1453 " xmlns:%s=\"%s\"\n"
1454 " xmlns:%s=\"%s\"\n>",
1456 /* XXX - do we really need the doctype? */
1458 XBEL_DTD_SYSTEM, XBEL_DTD_URI,
1461 XBEL_VERSION_ATTRIBUTE, XBEL_VERSION,
1462 BOOKMARK_NAMESPACE_NAME, BOOKMARK_NAMESPACE_URI,
1463 MIME_NAMESPACE_NAME, MIME_NAMESPACE_URI);
1465 if (bookmark->title)
1467 gchar *escaped_title;
1469 escaped_title = g_markup_escape_text (bookmark->title, -1);
1471 g_string_append_printf (retval, " <%s>%s</%s>\n",
1474 XBEL_TITLE_ELEMENT);
1476 g_free (escaped_title);
1479 if (bookmark->description)
1481 gchar *escaped_desc;
1483 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1485 g_string_append_printf (retval, " <%s>%s</%s>\n",
1490 g_free (escaped_desc);
1493 if (!bookmark->items)
1496 retval = g_string_append (retval, "\n");
1498 for (l = g_list_last (bookmark->items);
1502 BookmarkItem *item = (BookmarkItem *) l->data;
1505 item_dump = bookmark_item_dump (item);
1509 retval = g_string_append (retval, item_dump);
1515 g_string_append_printf (retval, "</%s>", XBEL_ROOT_ELEMENT);
1518 *length = retval->len;
1520 return g_string_free (retval, FALSE);
1527 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1528 * should free the returned string.
1531 timestamp_to_iso8601 (time_t timestamp)
1535 if (timestamp == (time_t) -1)
1536 g_get_current_time (&stamp);
1539 stamp.tv_sec = timestamp;
1543 return g_time_val_to_iso8601 (&stamp);
1547 timestamp_from_iso8601 (const gchar *iso_date)
1551 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1554 return (time_t) stamp.tv_sec;
1560 g_bookmark_file_error_quark (void)
1562 return g_quark_from_static_string ("egg-bookmark-file-error-quark");
1567 /********************
1569 ********************/
1572 * g_bookmark_file_new:
1574 * Creates a new empty #GBookmarkFile object.
1576 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1577 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1580 * Return value: an empty #GBookmarkFile
1585 g_bookmark_file_new (void)
1587 GBookmarkFile *bookmark;
1589 bookmark = g_new (GBookmarkFile, 1);
1591 g_bookmark_file_init (bookmark);
1597 * g_bookmark_file_free:
1598 * @bookmark: a #GBookmarkFile
1600 * Frees a #GBookmarkFile.
1605 g_bookmark_file_free (GBookmarkFile *bookmark)
1610 g_bookmark_file_clear (bookmark);
1616 * g_bookmark_file_load_from_data:
1617 * @bookmark: an empty #GBookmarkFile struct
1618 * @data: desktop bookmarks loaded in memory
1619 * @length: the length of @data in bytes
1620 * @error: return location for a #GError, or %NULL
1622 * Loads a bookmark file from memory into an empty #GBookmarkFile
1623 * structure. If the object cannot be created then @error is set to a
1624 * #GBookmarkFileError.
1626 * Return value: %TRUE if a desktop bookmark could be loaded.
1631 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1636 GError *parse_error;
1639 g_return_val_if_fail (bookmark != NULL, FALSE);
1640 g_return_val_if_fail (data != NULL, FALSE);
1641 g_return_val_if_fail (length != 0, FALSE);
1643 if (length == (gsize) -1)
1644 length = strlen (data);
1646 if (bookmark->items)
1648 g_bookmark_file_clear (bookmark);
1649 g_bookmark_file_init (bookmark);
1653 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1656 g_propagate_error (error, parse_error);
1665 * g_bookmark_file_load_from_file:
1666 * @bookmark: an empty #GBookmarkFile struct
1667 * @filename: the path of a filename to load, in the GLib file name encoding
1668 * @error: return location for a #GError, or %NULL
1670 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1671 * If the file could not be loaded then @error is set to either a #GFileError
1672 * or #GBookmarkFileError.
1674 * Return value: %TRUE if a desktop bookmark file could be loaded
1679 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1680 const gchar *filename,
1688 g_return_val_if_fail (bookmark != NULL, FALSE);
1689 g_return_val_if_fail (filename != NULL, FALSE);
1692 g_file_get_contents (filename, &buffer, &len, &read_error);
1695 g_propagate_error (error, read_error);
1701 retval = g_bookmark_file_load_from_data (bookmark,
1707 g_propagate_error (error, read_error);
1720 /* Iterates through all the directories in *dirs trying to
1721 * find file. When it successfully locates file, returns a
1722 * string its absolute path. It also leaves the unchecked
1723 * directories in *dirs. You should free the returned string
1725 * Adapted from gkeyfile.c
1728 find_file_in_data_dirs (const gchar *file,
1732 gchar **data_dirs, *data_dir, *path;
1741 while (data_dirs && (data_dir = *data_dirs) && !path)
1743 gchar *candidate_file, *sub_dir;
1745 candidate_file = (gchar *) file;
1746 sub_dir = g_strdup ("");
1747 while (candidate_file != NULL && !path)
1751 path = g_build_filename (data_dir, sub_dir,
1752 candidate_file, NULL);
1754 candidate_file = strchr (candidate_file, '-');
1756 if (candidate_file == NULL)
1762 sub_dir = g_strndup (file, candidate_file - file - 1);
1764 for (p = sub_dir; *p != '\0'; p++)
1767 *p = G_DIR_SEPARATOR;
1778 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1779 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1780 _("No valid bookmark file found in data dirs"));
1790 * g_bookmark_file_load_from_data_dirs:
1791 * @bookmark: a #GBookmarkFile
1792 * @file: a relative path to a filename to open and parse
1793 * @full_path: return location for a string containing the full path
1794 * of the file, or %NULL
1795 * @error: return location for a #GError, or %NULL
1797 * This function looks for a desktop bookmark file named @file in the
1798 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1799 * loads the file into @bookmark and returns the file's full path in
1800 * @full_path. If the file could not be loaded then an %error is
1801 * set to either a #GFileError or #GBookmarkFileError.
1803 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1808 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1813 GError *file_error = NULL;
1814 gchar **all_data_dirs, **data_dirs;
1815 const gchar *user_data_dir;
1816 const gchar * const * system_data_dirs;
1819 gboolean found_file;
1821 g_return_val_if_fail (bookmark != NULL, FALSE);
1822 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1824 user_data_dir = g_get_user_data_dir ();
1825 system_data_dirs = g_get_system_data_dirs ();
1826 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1829 all_data_dirs[i++] = g_strdup (user_data_dir);
1832 while (system_data_dirs[j] != NULL)
1833 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1836 data_dirs = all_data_dirs;
1838 while (*data_dirs != NULL && !found_file)
1840 g_free (output_path);
1842 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1846 g_propagate_error (error, file_error);
1850 found_file = g_bookmark_file_load_from_file (bookmark,
1855 g_propagate_error (error, file_error);
1860 if (found_file && full_path)
1861 *full_path = output_path;
1863 g_free (output_path);
1865 g_strfreev (all_data_dirs);
1872 * g_bookmark_file_to_data:
1873 * @bookmark: a #GBookmarkFile
1874 * @length: return location for the length of the returned string, or %NULL
1875 * @error: return location for a #GError, or %NULL
1877 * This function outputs @bookmark as a string.
1879 * Return value: a newly allocated string holding
1880 * the contents of the #GBookmarkFile
1885 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1889 GError *write_error = NULL;
1892 g_return_val_if_fail (bookmark != NULL, NULL);
1894 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1897 g_propagate_error (error, write_error);
1906 * g_bookmark_file_to_file:
1907 * @bookmark: a #GBookmarkFile
1908 * @filename: path of the output file
1909 * @error: return location for a #GError, or %NULL
1911 * This function outputs @bookmark into a file. The write process is
1912 * guaranteed to be atomic by using g_file_set_contents() internally.
1914 * Return value: %TRUE if the file was successfully written.
1919 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1920 const gchar *filename,
1924 GError *data_error, *write_error;
1928 g_return_val_if_fail (bookmark != NULL, FALSE);
1929 g_return_val_if_fail (filename != NULL, FALSE);
1932 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1935 g_propagate_error (error, data_error);
1941 g_file_set_contents (filename, data, len, &write_error);
1944 g_propagate_error (error, write_error);
1956 static BookmarkItem *
1957 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1960 g_assert (bookmark != NULL && uri != NULL);
1962 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1965 /* this function adds a new item to the list */
1967 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1971 g_assert (bookmark != NULL);
1972 g_assert (item != NULL);
1974 /* this should never happen; and if it does, then we are
1975 * screwing up something big time.
1977 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1979 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1980 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1981 _("A bookmark for URI '%s' already exists"),
1986 bookmark->items = g_list_prepend (bookmark->items, item);
1988 g_hash_table_replace (bookmark->items_by_uri,
1992 if (item->added == (time_t) -1)
1993 item->added = time (NULL);
1995 if (item->modified == (time_t) -1)
1996 item->modified = time (NULL);
2000 * g_bookmark_file_remove_item:
2001 * @bookmark: a #GBookmarkFile
2003 * @error: return location for a #GError, or %NULL
2005 * Removes the bookmark for @uri from the bookmark file @bookmark.
2007 * Return value: %TRUE if the bookmark was removed successfully.
2012 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2018 g_return_val_if_fail (bookmark != NULL, FALSE);
2019 g_return_val_if_fail (uri != NULL, FALSE);
2021 item = g_bookmark_file_lookup_item (bookmark, uri);
2025 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2026 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2027 _("No bookmark found for URI '%s'"),
2032 bookmark->items = g_list_remove (bookmark->items, item);
2033 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2035 bookmark_item_free (item);
2041 * g_bookmark_file_has_item:
2042 * @bookmark: a #GBookmarkFile
2045 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2047 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2052 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2055 g_return_val_if_fail (bookmark != NULL, FALSE);
2056 g_return_val_if_fail (uri != NULL, FALSE);
2058 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2062 * g_bookmark_file_get_uris:
2063 * @bookmark: a #GBookmarkFile
2064 * @length: return location for the number of returned URIs, or %NULL
2066 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2067 * The array of returned URIs will be %NULL-terminated, so @length may
2068 * optionally be %NULL.
2070 * Return value: a newly allocated %NULL-terminated array of strings.
2071 * Use g_strfreev() to free it.
2076 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2083 g_return_val_if_fail (bookmark != NULL, NULL);
2085 n_items = g_list_length (bookmark->items);
2086 uris = g_new0 (gchar *, n_items + 1);
2088 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2090 BookmarkItem *item = (BookmarkItem *) l->data;
2092 g_assert (item != NULL);
2094 uris[i++] = g_strdup (item->uri);
2105 * g_bookmark_file_set_title:
2106 * @bookmark: a #GBookmarkFile
2107 * @uri: a valid URI or %NULL
2108 * @title: a UTF-8 encoded string
2110 * Sets @title as the title of the bookmark for @uri inside the
2111 * bookmark file @bookmark.
2113 * If @uri is %NULL, the title of @bookmark is set.
2115 * If a bookmark for @uri cannot be found then it is created.
2120 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2124 g_return_if_fail (bookmark != NULL);
2128 g_free (bookmark->title);
2129 bookmark->title = g_strdup (title);
2135 item = g_bookmark_file_lookup_item (bookmark, uri);
2138 item = bookmark_item_new (uri);
2139 g_bookmark_file_add_item (bookmark, item, NULL);
2142 g_free (item->title);
2143 item->title = g_strdup (title);
2145 item->modified = time (NULL);
2150 * g_bookmark_file_get_title:
2151 * @bookmark: a #GBookmarkFile
2152 * @uri: a valid URI or %NULL
2153 * @error: return location for a #GError, or %NULL
2155 * Returns the title of the bookmark for @uri.
2157 * If @uri is %NULL, the title of @bookmark is returned.
2159 * In the event the URI cannot be found, %NULL is returned and
2160 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2162 * Return value: a newly allocated string or %NULL if the specified
2163 * URI cannot be found.
2168 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2174 g_return_val_if_fail (bookmark != NULL, NULL);
2177 return g_strdup (bookmark->title);
2179 item = g_bookmark_file_lookup_item (bookmark, uri);
2182 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2183 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2184 _("No bookmark found for URI '%s'"),
2189 return g_strdup (item->title);
2193 * g_bookmark_file_set_description:
2194 * @bookmark: a #GBookmarkFile
2195 * @uri: a valid URI or %NULL
2196 * @description: a string
2198 * Sets @description as the description of the bookmark for @uri.
2200 * If @uri is %NULL, the description of @bookmark is set.
2202 * If a bookmark for @uri cannot be found then it is created.
2207 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2209 const gchar *description)
2211 g_return_if_fail (bookmark != NULL);
2215 g_free (bookmark->description);
2216 bookmark->description = g_strdup (description);
2222 item = g_bookmark_file_lookup_item (bookmark, uri);
2225 item = bookmark_item_new (uri);
2226 g_bookmark_file_add_item (bookmark, item, NULL);
2229 g_free (item->description);
2230 item->description = g_strdup (description);
2232 item->modified = time (NULL);
2237 * g_bookmark_file_get_description:
2238 * @bookmark: a #GBookmarkFile
2240 * @error: return location for a #GError, or %NULL
2242 * Retrieves the description of the bookmark for @uri.
2244 * In the event the URI cannot be found, %NULL is returned and
2245 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2247 * Return value: a newly allocated string or %NULL if the specified
2248 * URI cannot be found.
2253 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2259 g_return_val_if_fail (bookmark != NULL, NULL);
2262 return g_strdup (bookmark->description);
2264 item = g_bookmark_file_lookup_item (bookmark, uri);
2267 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2268 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2269 _("No bookmark found for URI '%s'"),
2274 return g_strdup (item->description);
2278 * g_bookmark_file_set_mime_type:
2279 * @bookmark: a #GBookmarkFile
2281 * @mime_type: a MIME type
2283 * Sets @mime_type as the MIME type of the bookmark for @uri.
2285 * If a bookmark for @uri cannot be found then it is created.
2290 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2292 const gchar *mime_type)
2296 g_return_if_fail (bookmark != NULL);
2297 g_return_if_fail (uri != NULL);
2298 g_return_if_fail (mime_type != NULL);
2300 item = g_bookmark_file_lookup_item (bookmark, uri);
2303 item = bookmark_item_new (uri);
2304 g_bookmark_file_add_item (bookmark, item, NULL);
2307 if (!item->metadata)
2308 item->metadata = bookmark_metadata_new ();
2310 g_free (item->metadata->mime_type);
2312 item->metadata->mime_type = g_strdup (mime_type);
2313 item->modified = time (NULL);
2317 * g_bookmark_file_get_mime_type:
2318 * @bookmark: a #GBookmarkFile
2320 * @error: return location for a #GError, or %NULL
2322 * Retrieves the MIME type of the resource pointed by @uri.
2324 * In the event the URI cannot be found, %NULL is returned and
2325 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2326 * event that the MIME type cannot be found, %NULL is returned and
2327 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2329 * Return value: a newly allocated string or %NULL if the specified
2330 * URI cannot be found.
2335 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2341 g_return_val_if_fail (bookmark != NULL, NULL);
2342 g_return_val_if_fail (uri != NULL, NULL);
2344 item = g_bookmark_file_lookup_item (bookmark, uri);
2347 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2348 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2349 _("No bookmark found for URI '%s'"),
2354 if (!item->metadata)
2356 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2357 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2358 _("No MIME type defined in the bookmark for URI '%s'"),
2363 return g_strdup (item->metadata->mime_type);
2367 * g_bookmark_file_set_is_private:
2368 * @bookmark: a #GBookmarkFile
2370 * @is_private: %TRUE if the bookmark should be marked as private
2372 * Sets the private flag of the bookmark for @uri.
2374 * If a bookmark for @uri cannot be found then it is created.
2379 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2381 gboolean is_private)
2385 g_return_if_fail (bookmark != NULL);
2386 g_return_if_fail (uri != NULL);
2388 item = g_bookmark_file_lookup_item (bookmark, uri);
2391 item = bookmark_item_new (uri);
2392 g_bookmark_file_add_item (bookmark, item, NULL);
2395 if (!item->metadata)
2396 item->metadata = bookmark_metadata_new ();
2398 item->metadata->is_private = (is_private == TRUE);
2399 item->modified = time (NULL);
2403 * g_bookmark_file_get_is_private:
2404 * @bookmark: a #GBookmarkFile
2406 * @error: return location for a #GError, or %NULL
2408 * Gets whether the private flag of the bookmark for @uri is set.
2410 * In the event the URI cannot be found, %FALSE is returned and
2411 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2412 * event that the private flag cannot be found, %FALSE is returned and
2413 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2415 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2420 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2426 g_return_val_if_fail (bookmark != NULL, FALSE);
2427 g_return_val_if_fail (uri != NULL, FALSE);
2429 item = g_bookmark_file_lookup_item (bookmark, uri);
2432 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2433 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2434 _("No bookmark found for URI '%s'"),
2439 if (!item->metadata)
2441 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2442 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2443 _("No private flag has been defined in bookmark for URI '%s'"),
2448 return item->metadata->is_private;
2452 * g_bookmark_file_set_added:
2453 * @bookmark: a #GBookmarkFile
2455 * @added: a timestamp or -1 to use the current time
2457 * Sets the time the bookmark for @uri was added into @bookmark.
2459 * If no bookmark for @uri is found then it is created.
2464 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2470 g_return_if_fail (bookmark != NULL);
2471 g_return_if_fail (uri != NULL);
2473 item = g_bookmark_file_lookup_item (bookmark, uri);
2476 item = bookmark_item_new (uri);
2477 g_bookmark_file_add_item (bookmark, item, NULL);
2480 if (added == (time_t) -1)
2483 item->added = added;
2484 item->modified = added;
2488 * g_bookmark_file_get_added:
2489 * @bookmark: a #GBookmarkFile
2491 * @error: return location for a #GError, or %NULL
2493 * Gets the time the bookmark for @uri was added to @bookmark
2495 * In the event the URI cannot be found, -1 is returned and
2496 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2498 * Return value: a timestamp
2503 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2509 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2510 g_return_val_if_fail (uri != NULL, (time_t) -1);
2512 item = g_bookmark_file_lookup_item (bookmark, uri);
2515 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2516 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2517 _("No bookmark found for URI '%s'"),
2526 * g_bookmark_file_set_modified:
2527 * @bookmark: a #GBookmarkFile
2529 * @modified: a timestamp or -1 to use the current time
2531 * Sets the last time the bookmark for @uri was last modified.
2533 * If no bookmark for @uri is found then it is created.
2535 * The "modified" time should only be set when the bookmark's meta-data
2536 * was actually changed. Every function of #GBookmarkFile that
2537 * modifies a bookmark also changes the modification time, except for
2538 * g_bookmark_file_set_visited().
2543 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2549 g_return_if_fail (bookmark != NULL);
2550 g_return_if_fail (uri != NULL);
2552 item = g_bookmark_file_lookup_item (bookmark, uri);
2555 item = bookmark_item_new (uri);
2556 g_bookmark_file_add_item (bookmark, item, NULL);
2559 if (modified == (time_t) -1)
2562 item->modified = modified;
2566 * g_bookmark_file_get_modified:
2567 * @bookmark: a #GBookmarkFile
2569 * @error: return location for a #GError, or %NULL
2571 * Gets the time when the bookmark for @uri was last modified.
2573 * In the event the URI cannot be found, -1 is returned and
2574 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2576 * Return value: a timestamp
2581 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2587 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2588 g_return_val_if_fail (uri != NULL, (time_t) -1);
2590 item = g_bookmark_file_lookup_item (bookmark, uri);
2593 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2594 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2595 _("No bookmark found for URI '%s'"),
2600 return item->modified;
2604 * g_bookmark_file_set_visited:
2605 * @bookmark: a #GBookmarkFile
2607 * @visited: a timestamp or -1 to use the current time
2609 * Sets the time the bookmark for @uri was last visited.
2611 * If no bookmark for @uri is found then it is created.
2613 * The "visited" time should only be set if the bookmark was launched,
2614 * either using the command line retrieved by g_bookmark_file_get_app_info()
2615 * or by the default application for the bookmark's MIME type, retrieved
2616 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2617 * does not affect the "modified" time.
2622 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2628 g_return_if_fail (bookmark != NULL);
2629 g_return_if_fail (uri != NULL);
2631 item = g_bookmark_file_lookup_item (bookmark, uri);
2634 item = bookmark_item_new (uri);
2635 g_bookmark_file_add_item (bookmark, item, NULL);
2638 if (visited == (time_t) -1)
2641 item->visited = visited;
2645 * g_bookmark_file_get_visited:
2646 * @bookmark: a #GBookmarkFile
2648 * @error: return location for a #GError, or %NULL
2650 * Gets the time the bookmark for @uri was last visited.
2652 * In the event the URI cannot be found, -1 is returned and
2653 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2655 * Return value: a timestamp.
2660 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2666 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2667 g_return_val_if_fail (uri != NULL, (time_t) -1);
2669 item = g_bookmark_file_lookup_item (bookmark, uri);
2672 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2673 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2674 _("No bookmark found for URI '%s'"),
2679 return item->visited;
2683 * g_bookmark_file_has_group:
2684 * @bookmark: a #GBookmarkFile
2686 * @group: the group name to be searched
2687 * @error: return location for a #GError, or %NULL
2689 * Checks whether @group appears in the list of groups to which
2690 * the bookmark for @uri belongs to.
2692 * In the event the URI cannot be found, %FALSE is returned and
2693 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2695 * Return value: %TRUE if @group was found.
2700 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2708 g_return_val_if_fail (bookmark != NULL, FALSE);
2709 g_return_val_if_fail (uri != NULL, FALSE);
2711 item = g_bookmark_file_lookup_item (bookmark, uri);
2714 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2715 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2716 _("No bookmark found for URI '%s'"),
2721 if (!item->metadata)
2724 for (l = item->metadata->groups; l != NULL; l = l->next)
2726 if (strcmp (l->data, group) == 0)
2735 * g_bookmark_file_add_group:
2736 * @bookmark: a #GBookmarkFile
2738 * @group: the group name to be added
2740 * Adds @group to the list of groups to which the bookmark for @uri
2743 * If no bookmark for @uri is found then it is created.
2748 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2754 g_return_if_fail (bookmark != NULL);
2755 g_return_if_fail (uri != NULL);
2756 g_return_if_fail (group != NULL && group[0] != '\0');
2758 item = g_bookmark_file_lookup_item (bookmark, uri);
2761 item = bookmark_item_new (uri);
2762 g_bookmark_file_add_item (bookmark, item, NULL);
2765 if (!item->metadata)
2766 item->metadata = bookmark_metadata_new ();
2768 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2770 item->metadata->groups = g_list_prepend (item->metadata->groups,
2773 item->modified = time (NULL);
2778 * g_bookmark_file_remove_group:
2779 * @bookmark: a #GBookmarkFile
2781 * @group: the group name to be removed
2782 * @error: return location for a #GError, or %NULL
2784 * Removes @group from the list of groups to which the bookmark
2785 * for @uri belongs to.
2787 * In the event the URI cannot be found, %FALSE is returned and
2788 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2789 * In the event no group was defined, %FALSE is returned and
2790 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2792 * Return value: %TRUE if @group was successfully removed.
2797 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2805 g_return_val_if_fail (bookmark != NULL, FALSE);
2806 g_return_val_if_fail (uri != NULL, FALSE);
2808 item = g_bookmark_file_lookup_item (bookmark, uri);
2811 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2812 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2813 _("No bookmark found for URI '%s'"),
2818 if (!item->metadata)
2820 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2821 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2822 _("No groups set in bookmark for URI '%s'"),
2827 for (l = item->metadata->groups; l != NULL; l = l->next)
2829 if (strcmp (l->data, group) == 0)
2831 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2835 item->modified = time (NULL);
2845 * g_bookmark_file_set_groups:
2846 * @bookmark: a #GBookmarkFile
2847 * @uri: an item's URI
2848 * @groups: an array of group names, or %NULL to remove all groups
2849 * @length: number of group name values in @groups
2851 * Sets a list of group names for the item with URI @uri. Each previously
2852 * set group name list is removed.
2854 * If @uri cannot be found then an item for it is created.
2859 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2861 const gchar **groups,
2867 g_return_if_fail (bookmark != NULL);
2868 g_return_if_fail (uri != NULL);
2869 g_return_if_fail (groups != NULL);
2871 item = g_bookmark_file_lookup_item (bookmark, uri);
2874 item = bookmark_item_new (uri);
2875 g_bookmark_file_add_item (bookmark, item, NULL);
2878 if (!item->metadata)
2879 item->metadata = bookmark_metadata_new ();
2881 if (item->metadata->groups != NULL)
2883 g_list_foreach (item->metadata->groups,
2886 g_list_free (item->metadata->groups);
2887 item->metadata->groups = NULL;
2892 for (i = 0; groups[i] != NULL && i < length; i++)
2893 item->metadata->groups = g_list_append (item->metadata->groups,
2894 g_strdup (groups[i]));
2897 item->modified = time (NULL);
2901 * g_bookmark_file_get_groups:
2902 * @bookmark: a #GBookmarkFile
2904 * @length: return location for the length of the returned string, or %NULL
2905 * @error: return location for a #GError, or %NULL
2907 * Retrieves the list of group names of the bookmark for @uri.
2909 * In the event the URI cannot be found, %NULL is returned and
2910 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2912 * The returned array is %NULL terminated, so @length may optionally
2915 * Return value: a newly allocated %NULL-terminated array of group names.
2916 * Use g_strfreev() to free it.
2921 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2931 g_return_val_if_fail (bookmark != NULL, NULL);
2932 g_return_val_if_fail (uri != NULL, NULL);
2934 item = g_bookmark_file_lookup_item (bookmark, uri);
2937 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2938 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2939 _("No bookmark found for URI '%s'"),
2944 if (!item->metadata)
2952 len = g_list_length (item->metadata->groups);
2953 retval = g_new0 (gchar *, len + 1);
2954 for (l = g_list_last (item->metadata->groups), i = 0;
2958 gchar *group_name = (gchar *) l->data;
2960 g_assert (group_name != NULL);
2962 retval[i++] = g_strdup (group_name);
2973 * g_bookmark_file_add_application:
2974 * @bookmark: a #GBookmarkFile
2976 * @name: the name of the application registering the bookmark
2978 * @exec: command line to be used to launch the bookmark or %NULL
2980 * Adds the application with @name and @exec to the list of
2981 * applications that have registered a bookmark for @uri into
2984 * Every bookmark inside a #GBookmarkFile must have at least an
2985 * application registered. Each application must provide a name, a
2986 * command line useful for launching the bookmark, the number of times
2987 * the bookmark has been registered by the application and the last
2988 * time the application registered this bookmark.
2990 * If @name is %NULL, the name of the application will be the
2991 * same returned by g_get_application(); if @exec is %NULL, the
2992 * command line will be a composition of the program name as
2993 * returned by g_get_prgname() and the "%u" modifier, which will be
2994 * expanded to the bookmark's URI.
2996 * This function will automatically take care of updating the
2997 * registrations count and timestamping in case an application
2998 * with the same @name had already registered a bookmark for
2999 * @uri inside @bookmark.
3001 * If no bookmark for @uri is found, one is created.
3006 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3012 gchar *app_name, *app_exec;
3014 g_return_if_fail (bookmark != NULL);
3015 g_return_if_fail (uri != NULL);
3017 item = g_bookmark_file_lookup_item (bookmark, uri);
3020 item = bookmark_item_new (uri);
3021 g_bookmark_file_add_item (bookmark, item, NULL);
3024 if (name && name[0] != '\0')
3025 app_name = g_strdup (name);
3027 app_name = g_strdup (g_get_application_name ());
3029 if (exec && exec[0] != '\0')
3030 app_exec = g_strdup (exec);
3032 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3034 g_bookmark_file_set_app_info (bookmark, uri,
3046 * g_bookmark_file_remove_application:
3047 * @bookmark: a #GBookmarkFile
3049 * @name: the name of the application
3050 * @error: return location for a #GError or %NULL
3052 * Removes application registered with @name from the list of applications
3053 * that have registered a bookmark for @uri inside @bookmark.
3055 * In the event the URI cannot be found, %FALSE is returned and
3056 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3057 * In the event that no application with name @app_name has registered
3058 * a bookmark for @uri, %FALSE is returned and error is set to
3059 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3061 * Return value: %TRUE if the application was successfully removed.
3066 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3074 g_return_val_if_fail (bookmark != NULL, FALSE);
3075 g_return_val_if_fail (uri != NULL, FALSE);
3076 g_return_val_if_fail (name != NULL, FALSE);
3079 retval = g_bookmark_file_set_app_info (bookmark, uri,
3087 g_propagate_error (error, set_error);
3096 * g_bookmark_file_has_application:
3097 * @bookmark: a #GBookmarkFile
3099 * @name: the name of the application
3100 * @error: return location for a #GError or %NULL
3102 * Checks whether the bookmark for @uri inside @bookmark has been
3103 * registered by application @name.
3105 * In the event the URI cannot be found, %FALSE is returned and
3106 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3108 * Return value: %TRUE if the application @name was found
3113 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3120 g_return_val_if_fail (bookmark != NULL, FALSE);
3121 g_return_val_if_fail (uri != NULL, FALSE);
3122 g_return_val_if_fail (name != NULL, FALSE);
3124 item = g_bookmark_file_lookup_item (bookmark, uri);
3127 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3128 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3129 _("No bookmark found for URI '%s'"),
3134 return (NULL != bookmark_item_lookup_app_info (item, name));
3138 * g_bookmark_file_set_app_info:
3139 * @bookmark: a #GBookmarkFile
3141 * @name: an application's name
3142 * @exec: an application's command line
3143 * @count: the number of registrations done for this application
3144 * @stamp: the time of the last registration for this application
3145 * @error: return location for a #GError or %NULL
3147 * Sets the meta-data of application @name inside the list of
3148 * applications that have registered a bookmark for @uri inside
3151 * You should rarely use this function; use g_bookmark_file_add_application()
3152 * and g_bookmark_file_remove_application() instead.
3154 * @name can be any UTF-8 encoded string used to identify an
3156 * @exec can have one of these two modifiers: "%f", which will
3157 * be expanded as the local file name retrieved from the bookmark's
3158 * URI; "%u", which will be expanded as the bookmark's URI.
3159 * The expansion is done automatically when retrieving the stored
3160 * command line using the g_bookmark_file_get_app_info() function.
3161 * @count is the number of times the application has registered the
3162 * bookmark; if is < 0, the current registration count will be increased
3163 * by one, if is 0, the application with @name will be removed from
3164 * the list of registered applications.
3165 * @stamp is the Unix time of the last registration; if it is -1, the
3166 * current time will be used.
3168 * If you try to remove an application by setting its registration count to
3169 * zero, and no bookmark for @uri is found, %FALSE is returned and
3170 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3171 * in the event that no application @name has registered a bookmark
3172 * for @uri, %FALSE is returned and error is set to
3173 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3174 * for @uri is found, one is created.
3176 * Return value: %TRUE if the application's meta-data was successfully
3182 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3191 BookmarkAppInfo *ai;
3193 g_return_val_if_fail (bookmark != NULL, FALSE);
3194 g_return_val_if_fail (uri != NULL, FALSE);
3195 g_return_val_if_fail (name != NULL, FALSE);
3196 g_return_val_if_fail (exec != NULL, FALSE);
3198 item = g_bookmark_file_lookup_item (bookmark, uri);
3203 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3204 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3205 _("No bookmark found for URI '%s'"),
3211 item = bookmark_item_new (uri);
3212 g_bookmark_file_add_item (bookmark, item, NULL);
3216 ai = bookmark_item_lookup_app_info (item, name);
3221 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3222 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3223 _("No application with name '%s' registered a bookmark for '%s'"),
3230 ai = bookmark_app_info_new (name);
3232 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3233 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3239 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3240 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3241 bookmark_app_info_free (ai);
3243 item->modified = time (NULL);
3252 if (stamp != (time_t) -1)
3255 ai->stamp = time (NULL);
3257 if (exec && exec[0] != '\0')
3260 ai->exec = g_strdup (exec);
3263 item->modified = time (NULL);
3268 /* expands the application's command line */
3270 expand_exec_line (const gchar *exec_fmt,
3276 exec = g_string_new (NULL);
3277 while ((ch = *exec_fmt++) != '\0')
3281 exec = g_string_append_c (exec, ch);
3292 g_string_append (exec, uri);
3297 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3300 g_string_append (exec, file);
3305 g_string_free (exec, TRUE);
3312 exec = g_string_append_c (exec, ch);
3318 return g_string_free (exec, FALSE);
3322 * g_bookmark_file_get_app_info:
3323 * @bookmark: a #GBookmarkFile
3325 * @name: an application's name
3326 * @exec: location for the command line of the application, or %NULL
3327 * @count: return location for the registration count, or %NULL
3328 * @stamp: return location for the last registration time, or %NULL
3329 * @error: return location for a #GError, or %NULL
3331 * Gets the registration informations of @app_name for the bookmark for
3332 * @uri. See g_bookmark_file_set_app_info() for more informations about
3333 * the returned data.
3335 * The string returned in @app_exec must be freed.
3337 * In the event the URI cannot be found, %FALSE is returned and
3338 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3339 * event that no application with name @app_name has registered a bookmark
3340 * for @uri, %FALSE is returned and error is set to
3341 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3343 * Return value: %TRUE on success.
3348 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3357 BookmarkAppInfo *ai;
3359 g_return_val_if_fail (bookmark != NULL, FALSE);
3360 g_return_val_if_fail (uri != NULL, FALSE);
3361 g_return_val_if_fail (name != NULL, FALSE);
3363 item = g_bookmark_file_lookup_item (bookmark, uri);
3366 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3367 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3368 _("No bookmark found for URI '%s'"),
3373 ai = bookmark_item_lookup_app_info (item, name);
3376 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3377 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3378 _("No application with name '%s' registered a bookmark for '%s'"),
3386 *exec = expand_exec_line (ai->exec, uri);
3389 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3390 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3391 _("Failed to expand exec line '%s' with URI '%s'"),
3407 * g_bookmark_file_get_applications:
3408 * @bookmark: a #GBookmarkFile
3410 * @length: return location of the length of the returned list, or %NULL
3411 * @error: return location for a #GError, or %NULL
3413 * Retrieves the names of the applications that have registered the
3414 * bookmark for @uri.
3416 * In the event the URI cannot be found, %NULL is returned and
3417 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3419 * Return value: a newly allocated %NULL-terminated array of strings.
3420 * Use g_strfreev() to free it.
3425 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3435 g_return_val_if_fail (bookmark != NULL, NULL);
3436 g_return_val_if_fail (uri != NULL, NULL);
3438 item = g_bookmark_file_lookup_item (bookmark, uri);
3441 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3442 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3443 _("No bookmark found for URI '%s'"),
3448 if (!item->metadata)
3456 n_apps = g_list_length (item->metadata->applications);
3457 apps = g_new0 (gchar *, n_apps + 1);
3459 for (l = g_list_last (item->metadata->applications), i = 0;
3463 BookmarkAppInfo *ai;
3465 ai = (BookmarkAppInfo *) l->data;
3467 g_assert (ai != NULL);
3468 g_assert (ai->name != NULL);
3470 apps[i++] = g_strdup (ai->name);
3481 * g_bookmark_file_get_size:
3482 * @bookmark: a #GBookmarkFile
3484 * Gets the number of bookmarks inside @bookmark.
3486 * Return value: the number of bookmarks
3491 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3493 g_return_val_if_fail (bookmark != NULL, 0);
3495 return g_list_length (bookmark->items);
3499 * g_bookmark_file_move_item:
3500 * @bookmark: a #GBookmarkFile
3501 * @old_uri: a valid URI
3502 * @new_uri: a valid URI, or %NULL
3503 * @error: return location for a #GError or %NULL
3505 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3506 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3507 * %NULL, then the bookmark is removed.
3509 * In the event the URI cannot be found, %FALSE is returned and
3510 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3512 * Return value: %TRUE if the URI was successfully changed
3517 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3518 const gchar *old_uri,
3519 const gchar *new_uri,
3523 GError *remove_error;
3525 g_return_val_if_fail (bookmark != NULL, FALSE);
3526 g_return_val_if_fail (old_uri != NULL, FALSE);
3528 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3531 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3532 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3533 _("No bookmark found for URI '%s'"),
3538 if (new_uri && new_uri[0] != '\0')
3540 if (g_bookmark_file_has_item (bookmark, new_uri))
3542 remove_error = NULL;
3543 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3546 g_propagate_error (error, remove_error);
3552 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3555 item->uri = g_strdup (new_uri);
3556 item->modified = time (NULL);
3558 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3564 remove_error = NULL;
3565 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3568 g_propagate_error (error, remove_error);
3578 * g_bookmark_file_set_icon:
3579 * @bookmark: a #GBookmarkFile
3581 * @href: the URI of the icon for the bookmark, or %NULL
3582 * @mime_type: the MIME type of the icon for the bookmark
3584 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3585 * the currently set icon.
3587 * If no bookmark for @uri is found it is created.
3592 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3595 const gchar *mime_type)
3599 g_return_if_fail (bookmark != NULL);
3600 g_return_if_fail (uri != NULL);
3602 item = g_bookmark_file_lookup_item (bookmark, uri);
3605 item = bookmark_item_new (uri);
3606 g_bookmark_file_add_item (bookmark, item, NULL);
3609 if (!item->metadata)
3610 item->metadata = bookmark_metadata_new ();
3612 g_free (item->metadata->icon_href);
3613 g_free (item->metadata->icon_mime);
3615 item->metadata->icon_href = g_strdup (href);
3617 if (mime_type && mime_type[0] != '\0')
3618 item->metadata->icon_mime = g_strdup (mime_type);
3620 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3622 item->modified = time (NULL);
3626 * g_bookmark_file_get_icon:
3627 * @bookmark: a #GBookmarkFile
3629 * @href: return location for the icon's location or %NULL
3630 * @mime_type: return location for the icon's MIME type or %NULL
3631 * @error: return location for a #GError or %NULL
3633 * Gets the icon of the bookmark for @uri.
3635 * In the event the URI cannot be found, %FALSE is returned and
3636 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3638 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3639 * You should free the returned strings.
3644 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3652 g_return_val_if_fail (bookmark != NULL, FALSE);
3653 g_return_val_if_fail (uri != NULL, FALSE);
3655 item = g_bookmark_file_lookup_item (bookmark, uri);
3658 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3659 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3660 _("No bookmark found for URI '%s'"),
3665 if ((!item->metadata) || (!item->metadata->icon_href))
3669 *href = g_strdup (item->metadata->icon_href);
3672 *mime_type = g_strdup (item->metadata->icon_mime);
3677 #define __G_BOOKMARK_FILE_C__
3678 #include "galiasdef.c"