1 /* gbookmarkfile.c: parsing and building desktop bookmarks
3 * Copyright (C) 2005-2006 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 #include "gbookmarkfile.h"
32 #include <sys/types.h>
41 #include "gfileutils.h"
49 #include "gmessages.h"
54 #include "gstrfuncs.h"
60 /* XBEL 1.0 standard entities */
61 #define XBEL_VERSION "1.0"
62 #define XBEL_DTD_NICK "xbel"
63 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
64 "Exchange Language 1.0//EN//XML"
66 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
68 #define XBEL_ROOT_ELEMENT "xbel"
69 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
70 #define XBEL_BOOKMARK_ELEMENT "bookmark"
71 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
72 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
73 #define XBEL_TITLE_ELEMENT "title"
74 #define XBEL_DESC_ELEMENT "desc"
75 #define XBEL_INFO_ELEMENT "info"
76 #define XBEL_METADATA_ELEMENT "metadata"
78 #define XBEL_VERSION_ATTRIBUTE "version"
79 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
80 #define XBEL_OWNER_ATTRIBUTE "owner"
81 #define XBEL_ADDED_ATTRIBUTE "added"
82 #define XBEL_VISITED_ATTRIBUTE "visited"
83 #define XBEL_MODIFIED_ATTRIBUTE "modified"
84 #define XBEL_ID_ATTRIBUTE "id"
85 #define XBEL_HREF_ATTRIBUTE "href"
86 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
88 #define XBEL_YES_VALUE "yes"
89 #define XBEL_NO_VALUE "no"
91 /* Desktop bookmark spec entities */
92 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
94 #define BOOKMARK_NAMESPACE_NAME "bookmark"
95 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
97 #define BOOKMARK_GROUPS_ELEMENT "groups"
98 #define BOOKMARK_GROUP_ELEMENT "group"
99 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
100 #define BOOKMARK_APPLICATION_ELEMENT "application"
101 #define BOOKMARK_ICON_ELEMENT "icon"
102 #define BOOKMARK_PRIVATE_ELEMENT "private"
104 #define BOOKMARK_NAME_ATTRIBUTE "name"
105 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
106 #define BOOKMARK_COUNT_ATTRIBUTE "count"
107 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp"
108 #define BOOKMARK_HREF_ATTRIBUTE "href"
109 #define BOOKMARK_TYPE_ATTRIBUTE "type"
111 /* Shared MIME Info entities */
112 #define MIME_NAMESPACE_NAME "mime"
113 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
114 #define MIME_TYPE_ELEMENT "mime-type"
115 #define MIME_TYPE_ATTRIBUTE "type"
118 typedef struct _BookmarkAppInfo BookmarkAppInfo;
119 typedef struct _BookmarkMetadata BookmarkMetadata;
120 typedef struct _BookmarkItem BookmarkItem;
121 typedef struct _ParseData ParseData;
123 struct _BookmarkAppInfo
133 struct _BookmarkMetadata
140 GHashTable *apps_by_name;
145 guint is_private : 1;
159 BookmarkMetadata *metadata;
162 struct _GBookmarkFile
167 /* we store our items in a list and keep a copy inside
168 * an hash table for faster lookup performances
171 GHashTable *items_by_uri;
174 /* parser state machine */
195 static void g_bookmark_file_init (GBookmarkFile *bookmark);
196 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
197 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
201 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
204 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
206 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
210 static time_t timestamp_from_iso8601 (const gchar *iso_date);
211 static gchar * timestamp_to_iso8601 (time_t timestamp);
213 /********************************
216 * Application metadata storage *
217 ********************************/
218 static BookmarkAppInfo *
219 bookmark_app_info_new (const gchar *name)
221 BookmarkAppInfo *retval;
223 g_assert (name != NULL);
225 retval = g_slice_new (BookmarkAppInfo);
227 retval->name = g_strdup (name);
230 retval->stamp = time (NULL);
236 bookmark_app_info_free (BookmarkAppInfo *app_info)
241 g_free (app_info->name);
242 g_free (app_info->exec);
244 g_slice_free (BookmarkAppInfo, app_info);
248 bookmark_app_info_dump (BookmarkAppInfo *app_info)
253 g_assert (app_info != NULL);
255 if (app_info->count == 0)
258 name = g_markup_escape_text (app_info->name, -1);
259 exec = g_markup_escape_text (app_info->exec, -1);
261 retval = g_strdup_printf (" <%s:%s %s=\"%s\" %s=\"%s\" %s=\"%ld\" %s=\"%u\"/>\n",
262 BOOKMARK_NAMESPACE_NAME,
263 BOOKMARK_APPLICATION_ELEMENT,
264 BOOKMARK_NAME_ATTRIBUTE, name,
265 BOOKMARK_EXEC_ATTRIBUTE, exec,
266 BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
267 BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
276 /***********************
280 ***********************/
281 static BookmarkMetadata *
282 bookmark_metadata_new (void)
284 BookmarkMetadata *retval;
286 retval = g_slice_new (BookmarkMetadata);
288 retval->mime_type = NULL;
290 retval->groups = NULL;
292 retval->applications = NULL;
293 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
298 retval->is_private = FALSE;
300 retval->icon_href = NULL;
301 retval->icon_mime = NULL;
307 bookmark_metadata_free (BookmarkMetadata *metadata)
312 g_free (metadata->mime_type);
314 if (metadata->groups)
316 g_list_foreach (metadata->groups,
319 g_list_free (metadata->groups);
322 if (metadata->applications)
324 g_list_foreach (metadata->applications,
325 (GFunc) bookmark_app_info_free,
327 g_list_free (metadata->applications);
329 g_hash_table_destroy (metadata->apps_by_name);
332 g_free (metadata->icon_href);
333 g_free (metadata->icon_mime);
335 g_slice_free (BookmarkMetadata, metadata);
339 bookmark_metadata_dump (BookmarkMetadata *metadata)
343 if (!metadata->applications)
346 retval = g_string_new (NULL);
348 /* metadata container */
349 g_string_append_printf (retval,
351 XBEL_METADATA_ELEMENT,
352 XBEL_OWNER_ATTRIBUTE, BOOKMARK_METADATA_OWNER);
355 if (metadata->mime_type)
356 g_string_append_printf (retval,
357 " <%s:%s %s=\"%s\"/>\n",
360 MIME_TYPE_ATTRIBUTE, metadata->mime_type);
362 if (metadata->groups)
366 /* open groups container */
367 g_string_append_printf (retval,
369 BOOKMARK_NAMESPACE_NAME,
370 BOOKMARK_GROUPS_ELEMENT);
372 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
376 group_name = g_markup_escape_text ((gchar *) l->data, -1);
377 g_string_append_printf (retval,
378 " <%s:%s>%s</%s:%s>\n",
379 BOOKMARK_NAMESPACE_NAME,
380 BOOKMARK_GROUP_ELEMENT,
382 BOOKMARK_NAMESPACE_NAME,
383 BOOKMARK_GROUP_ELEMENT);
387 /* close groups container */
388 g_string_append_printf (retval,
390 BOOKMARK_NAMESPACE_NAME,
391 BOOKMARK_GROUPS_ELEMENT);
394 if (metadata->applications)
398 /* open applications container */
399 g_string_append_printf (retval,
401 BOOKMARK_NAMESPACE_NAME,
402 BOOKMARK_APPLICATIONS_ELEMENT);
404 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
406 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
409 g_assert (app_info != NULL);
411 app_data = bookmark_app_info_dump (app_info);
415 retval = g_string_append (retval, app_data);
421 /* close applications container */
422 g_string_append_printf (retval,
424 BOOKMARK_NAMESPACE_NAME,
425 BOOKMARK_APPLICATIONS_ELEMENT);
429 if (metadata->icon_href)
431 if (!metadata->icon_mime)
432 metadata->icon_mime = g_strdup ("application/octet-stream");
434 g_string_append_printf (retval,
435 " <%s:%s %s=\"%s\" %s=\"%s\"/>\n",
436 BOOKMARK_NAMESPACE_NAME,
437 BOOKMARK_ICON_ELEMENT,
438 BOOKMARK_HREF_ATTRIBUTE, metadata->icon_href,
439 BOOKMARK_TYPE_ATTRIBUTE, metadata->icon_mime);
443 if (metadata->is_private)
444 g_string_append_printf (retval,
446 BOOKMARK_NAMESPACE_NAME,
447 BOOKMARK_PRIVATE_ELEMENT);
449 /* close metadata container */
450 g_string_append_printf (retval, " </%s>\n", XBEL_METADATA_ELEMENT);
452 return g_string_free (retval, FALSE);
455 /******************************************************
458 * Storage for a single bookmark item inside the list *
459 ******************************************************/
460 static BookmarkItem *
461 bookmark_item_new (const gchar *uri)
465 g_assert (uri != NULL);
467 item = g_slice_new (BookmarkItem);
468 item->uri = g_strdup (uri);
471 item->description = NULL;
473 item->added = (time_t) -1;
474 item->modified = (time_t) -1;
475 item->visited = (time_t) -1;
477 item->metadata = NULL;
483 bookmark_item_free (BookmarkItem *item)
489 g_free (item->title);
490 g_free (item->description);
493 bookmark_metadata_free (item->metadata);
495 g_slice_free (BookmarkItem, item);
499 bookmark_item_dump (BookmarkItem *item)
502 gchar *added, *visited, *modified;
505 /* at this point, we must have at least a registered application; if we don't
506 * we don't screw up the bookmark file, and just skip this item
508 if (!item->metadata || !item->metadata->applications)
510 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
514 retval = g_string_new (NULL);
516 added = timestamp_to_iso8601 (item->added);
517 modified = timestamp_to_iso8601 (item->modified);
518 visited = timestamp_to_iso8601 (item->visited);
520 escaped_uri = g_markup_escape_text (item->uri, -1);
522 g_string_append_printf (retval,
523 " <%s %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\">\n",
524 XBEL_BOOKMARK_ELEMENT,
525 XBEL_HREF_ATTRIBUTE, escaped_uri,
526 XBEL_ADDED_ATTRIBUTE, added,
527 XBEL_MODIFIED_ATTRIBUTE, modified,
528 XBEL_VISITED_ATTRIBUTE, visited);
529 g_free (escaped_uri);
536 gchar *escaped_title;
538 escaped_title = g_markup_escape_text (item->title, -1);
539 g_string_append_printf (retval,
544 g_free (escaped_title);
547 if (item->description)
551 escaped_desc = g_markup_escape_text (item->description, -1);
552 g_string_append_printf (retval,
557 g_free (escaped_desc);
564 /* open info container */
565 g_string_append_printf (retval, " <%s>\n", XBEL_INFO_ELEMENT);
567 metadata = bookmark_metadata_dump (item->metadata);
570 retval = g_string_append (retval, metadata);
575 /* close info container */
576 g_string_append_printf (retval, " </%s>\n", XBEL_INFO_ELEMENT);
579 g_string_append_printf (retval, " </%s>\n", XBEL_BOOKMARK_ELEMENT);
581 return g_string_free (retval, FALSE);
584 static BookmarkAppInfo *
585 bookmark_item_lookup_app_info (BookmarkItem *item,
586 const gchar *app_name)
588 g_assert (item != NULL && app_name != NULL);
593 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
596 /*************************
598 *************************/
601 g_bookmark_file_init (GBookmarkFile *bookmark)
603 bookmark->title = NULL;
604 bookmark->description = NULL;
606 bookmark->items = NULL;
607 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
614 g_bookmark_file_clear (GBookmarkFile *bookmark)
616 g_free (bookmark->title);
617 g_free (bookmark->description);
621 g_list_foreach (bookmark->items,
622 (GFunc) bookmark_item_free,
624 g_list_free (bookmark->items);
626 bookmark->items = NULL;
629 if (bookmark->items_by_uri)
631 g_hash_table_destroy (bookmark->items_by_uri);
633 bookmark->items_by_uri = NULL;
641 GHashTable *namespaces;
643 GBookmarkFile *bookmark_file;
644 BookmarkItem *current_item;
648 parse_data_new (void)
652 retval = g_new (ParseData, 1);
654 retval->state = STATE_STARTED;
655 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
656 (GDestroyNotify) g_free,
657 (GDestroyNotify) g_free);
658 retval->bookmark_file = NULL;
659 retval->current_item = NULL;
665 parse_data_free (ParseData *parse_data)
667 g_hash_table_destroy (parse_data->namespaces);
672 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
675 parse_bookmark_element (GMarkupParseContext *context,
676 ParseData *parse_data,
677 const gchar **attribute_names,
678 const gchar **attribute_values,
681 const gchar *uri, *added, *modified, *visited;
687 g_assert ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
690 uri = added = modified = visited = NULL;
691 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
693 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
694 uri = attribute_values[i];
695 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
696 added = attribute_values[i];
697 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
698 modified = attribute_values[i];
699 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
700 visited = attribute_values[i];
703 g_set_error (error, G_MARKUP_ERROR,
704 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
705 _("Unexpected attribute '%s' for element '%s'"),
707 XBEL_BOOKMARK_ELEMENT);
714 g_set_error (error, G_MARKUP_ERROR,
715 G_MARKUP_ERROR_INVALID_CONTENT,
716 _("Attribute '%s' of element '%s' not found"),
718 XBEL_BOOKMARK_ELEMENT);
722 g_assert (parse_data->current_item == NULL);
724 item = bookmark_item_new (uri);
727 item->added = timestamp_from_iso8601 (added);
730 item->modified = timestamp_from_iso8601 (modified);
733 item->visited = timestamp_from_iso8601 (visited);
736 g_bookmark_file_add_item (parse_data->bookmark_file,
741 bookmark_item_free (item);
743 g_propagate_error (error, add_error);
748 parse_data->current_item = item;
752 parse_application_element (GMarkupParseContext *context,
753 ParseData *parse_data,
754 const gchar **attribute_names,
755 const gchar **attribute_values,
758 const gchar *name, *exec, *count, *stamp;
764 g_assert ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
767 name = exec = count = stamp = NULL;
768 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
770 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
771 name = attribute_values[i];
772 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
773 exec = attribute_values[i];
774 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
775 count = attribute_values[i];
776 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
777 stamp = attribute_values[i];
780 g_set_error (error, G_MARKUP_ERROR,
781 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
782 _("Unexpected attribute '%s' for element '%s'"),
784 BOOKMARK_APPLICATION_ELEMENT);
791 g_set_error (error, G_MARKUP_ERROR,
792 G_MARKUP_ERROR_INVALID_CONTENT,
793 _("Attribute '%s' of element '%s' not found"),
794 BOOKMARK_NAME_ATTRIBUTE,
795 BOOKMARK_APPLICATION_ELEMENT);
801 g_set_error (error, G_MARKUP_ERROR,
802 G_MARKUP_ERROR_INVALID_CONTENT,
803 _("Attribute '%s' of element '%s' not found"),
804 BOOKMARK_EXEC_ATTRIBUTE,
805 BOOKMARK_APPLICATION_ELEMENT);
809 g_assert (parse_data->current_item != NULL);
810 item = parse_data->current_item;
812 ai = bookmark_item_lookup_app_info (item, name);
815 ai = bookmark_app_info_new (name);
818 item->metadata = bookmark_metadata_new ();
820 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
821 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
824 ai->exec = g_strdup (exec);
827 ai->count = atoi (count);
832 ai->stamp = (time_t) atol (stamp);
834 ai->stamp = time (NULL);
838 parse_mime_type_element (GMarkupParseContext *context,
839 ParseData *parse_data,
840 const gchar **attribute_names,
841 const gchar **attribute_values,
849 g_assert ((parse_data != NULL) && (parse_data->state == STATE_MIME));
853 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
855 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
856 type = attribute_values[i];
859 g_set_error (error, G_MARKUP_ERROR,
860 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
861 _("Unexpected attribute '%s' for element '%s'"),
869 type = "application/octet-stream";
871 g_assert (parse_data->current_item != NULL);
872 item = parse_data->current_item;
875 item->metadata = bookmark_metadata_new ();
877 item->metadata->mime_type = g_strdup (type);
881 parse_icon_element (GMarkupParseContext *context,
882 ParseData *parse_data,
883 const gchar **attribute_names,
884 const gchar **attribute_values,
893 g_assert ((parse_data != NULL) && (parse_data->state == STATE_ICON));
898 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
900 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
901 href = attribute_values[i];
902 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
903 type = attribute_values[i];
906 g_set_error (error, G_MARKUP_ERROR,
907 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
908 _("Unexpected attribute '%s' for element '%s'"),
910 BOOKMARK_ICON_ELEMENT);
917 g_set_error (error, G_MARKUP_ERROR,
918 G_MARKUP_ERROR_INVALID_CONTENT,
919 _("Attribute '%s' of element '%s' not found"),
920 BOOKMARK_HREF_ATTRIBUTE,
921 BOOKMARK_ICON_ELEMENT);
926 type = "application/octet-stream";
928 g_assert (parse_data->current_item != NULL);
929 item = parse_data->current_item;
932 item->metadata = bookmark_metadata_new ();
934 item->metadata->icon_href = g_strdup (href);
935 item->metadata->icon_mime = g_strdup (type);
938 /* scans through the attributes of an element for the "xmlns" pragma, and
939 * adds any resulting namespace declaration to a per-parser hashtable, using
940 * the namespace name as a key for the namespace URI; if no key was found,
941 * the namespace is considered as default, and stored under the "default" key.
943 * FIXME: this works on the assumption that the generator of the XBEL file
944 * is either this code or is smart enough to place the namespace declarations
945 * inside the main root node or inside the metadata node and does not redefine
946 * a namespace inside an inner node; this does *not* conform to the
947 * XML-NS standard, although is a close approximation. In order to make this
948 * conformant to the XML-NS specification we should use a per-element
949 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
952 map_namespace_to_name (ParseData *parse_data,
953 const gchar **attribute_names,
954 const gchar **attribute_values)
959 g_assert (parse_data != NULL);
961 if (!attribute_names || !attribute_names[0])
965 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
967 if (g_str_has_prefix (attr, "xmlns"))
969 gchar *namespace_name, *namespace_uri;
972 p = g_utf8_strchr (attr, -1, ':');
974 p = g_utf8_next_char (p);
978 namespace_name = g_strdup (p);
979 namespace_uri = g_strdup (attribute_values[i]);
981 g_hash_table_replace (parse_data->namespaces,
988 /* checks whether @element_full is equal to @element.
990 * if @namespace is set, it tries to resolve the namespace to a known URI,
991 * and if found is prepended to the element name, from which is separated
992 * using the character specified in the @sep parameter.
995 is_element_full (ParseData *parse_data,
996 const gchar *element_full,
997 const gchar *namespace,
998 const gchar *element,
1001 gchar *ns_uri, *ns_name, *s, *resolved;
1002 const gchar *p, *element_name;
1005 g_assert (parse_data != NULL);
1006 g_assert (element_full != NULL);
1011 /* no namespace requested: dumb element compare */
1013 return (0 == strcmp (element_full, element));
1015 /* search for namespace separator; if none found, assume we are under the
1016 * default namespace, and set ns_name to our "default" marker; if no default
1017 * namespace has been set, just do a plain comparison between @full_element
1020 p = strchr (element_full, ':');
1023 ns_name = g_strndup (element_full, p - element_full);
1024 element_name = g_utf8_next_char (p);
1028 ns_name = g_strdup ("default");
1029 element_name = element_full;
1032 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1035 /* no default namespace found */
1038 return (0 == strcmp (element_full, element));
1041 resolved = g_strdup_printf ("%s%c%s", ns_uri, sep, element_name);
1042 s = g_strdup_printf ("%s%c%s", namespace, sep, element);
1043 retval = (0 == strcmp (resolved, s));
1052 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1053 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1056 start_element_raw_cb (GMarkupParseContext *context,
1057 const gchar *element_name,
1058 const gchar **attribute_names,
1059 const gchar **attribute_values,
1063 ParseData *parse_data = (ParseData *) user_data;
1065 /* we must check for namespace declarations first
1067 * XXX - we could speed up things by checking for namespace declarations
1068 * only on the root node, where they usually are; this would probably break
1069 * on streams not produced by us or by "smart" generators
1071 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1073 switch (parse_data->state)
1076 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1082 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1084 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1085 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1086 parse_data->state = STATE_ROOT;
1090 g_set_error (error, G_MARKUP_ERROR,
1091 G_MARKUP_ERROR_INVALID_CONTENT,
1092 _("Unexpected tag '%s', tag '%s' expected"),
1093 element_name, XBEL_ROOT_ELEMENT);
1096 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1097 parse_data->state = STATE_TITLE;
1098 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1099 parse_data->state = STATE_DESC;
1100 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1102 GError *inner_error = NULL;
1104 parse_data->state = STATE_BOOKMARK;
1106 parse_bookmark_element (context,
1112 g_propagate_error (error, inner_error);
1115 g_set_error (error, G_MARKUP_ERROR,
1116 G_MARKUP_ERROR_INVALID_CONTENT,
1117 _("Unexpected tag '%s' inside '%s'"),
1121 case STATE_BOOKMARK:
1122 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1123 parse_data->state = STATE_TITLE;
1124 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1125 parse_data->state = STATE_DESC;
1126 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1127 parse_data->state = STATE_INFO;
1129 g_set_error (error, G_MARKUP_ERROR,
1130 G_MARKUP_ERROR_INVALID_CONTENT,
1131 _("Unexpected tag '%s' inside '%s'"),
1133 XBEL_BOOKMARK_ELEMENT);
1136 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1142 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1144 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1145 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1147 parse_data->state = STATE_METADATA;
1149 if (!parse_data->current_item->metadata)
1150 parse_data->current_item->metadata = bookmark_metadata_new ();
1155 g_set_error (error, G_MARKUP_ERROR,
1156 G_MARKUP_ERROR_INVALID_CONTENT,
1157 _("Unexpected tag '%s', tag '%s' expected"),
1159 XBEL_METADATA_ELEMENT);
1161 case STATE_METADATA:
1162 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1163 parse_data->state = STATE_APPLICATIONS;
1164 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1165 parse_data->state = STATE_GROUPS;
1166 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1167 parse_data->current_item->metadata->is_private = TRUE;
1168 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1170 GError *inner_error = NULL;
1172 parse_data->state = STATE_ICON;
1174 parse_icon_element (context,
1180 g_propagate_error (error, inner_error);
1182 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1184 GError *inner_error = NULL;
1186 parse_data->state = STATE_MIME;
1188 parse_mime_type_element (context,
1194 g_propagate_error (error, inner_error);
1197 g_set_error (error, G_MARKUP_ERROR,
1198 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1199 _("Unexpected tag '%s' inside '%s'"),
1201 XBEL_METADATA_ELEMENT);
1203 case STATE_APPLICATIONS:
1204 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1206 GError *inner_error = NULL;
1208 parse_data->state = STATE_APPLICATION;
1210 parse_application_element (context,
1216 g_propagate_error (error, inner_error);
1219 g_set_error (error, G_MARKUP_ERROR,
1220 G_MARKUP_ERROR_INVALID_CONTENT,
1221 _("Unexpected tag '%s', tag '%s' expected"),
1223 BOOKMARK_APPLICATION_ELEMENT);
1226 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1227 parse_data->state = STATE_GROUP;
1229 g_set_error (error, G_MARKUP_ERROR,
1230 G_MARKUP_ERROR_INVALID_CONTENT,
1231 _("Unexpected tag '%s', tag '%s' expected"),
1233 BOOKMARK_GROUP_ELEMENT);
1236 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1238 GError *inner_error = NULL;
1240 parse_icon_element (context,
1246 g_propagate_error (error, inner_error);
1249 g_set_error (error, G_MARKUP_ERROR,
1250 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1251 _("Unexpected tag '%s' inside '%s'"),
1253 XBEL_METADATA_ELEMENT);
1256 g_assert_not_reached ();
1262 end_element_raw_cb (GMarkupParseContext *context,
1263 const gchar *element_name,
1267 ParseData *parse_data = (ParseData *) user_data;
1269 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1270 parse_data->state = STATE_FINISHED;
1271 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1273 parse_data->current_item = NULL;
1275 parse_data->state = STATE_ROOT;
1277 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1278 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1279 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1281 if (parse_data->current_item)
1282 parse_data->state = STATE_BOOKMARK;
1284 parse_data->state = STATE_ROOT;
1286 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1287 parse_data->state = STATE_INFO;
1288 else if (IS_ELEMENT_NS (parse_data, element_name,
1289 BOOKMARK_NAMESPACE_URI,
1290 BOOKMARK_APPLICATION_ELEMENT))
1291 parse_data->state = STATE_APPLICATIONS;
1292 else if (IS_ELEMENT_NS (parse_data, element_name,
1293 BOOKMARK_NAMESPACE_URI,
1294 BOOKMARK_GROUP_ELEMENT))
1295 parse_data->state = STATE_GROUPS;
1296 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1297 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1298 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1299 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1300 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1301 parse_data->state = STATE_METADATA;
1305 text_raw_cb (GMarkupParseContext *context,
1311 ParseData *parse_data = (ParseData *) user_data;
1314 payload = g_strndup (text, length);
1316 switch (parse_data->state)
1319 if (parse_data->current_item)
1321 g_free (parse_data->current_item->title);
1322 parse_data->current_item->title = g_strdup (payload);
1326 g_free (parse_data->bookmark_file->title);
1327 parse_data->bookmark_file->title = g_strdup (payload);
1331 if (parse_data->current_item)
1333 g_free (parse_data->current_item->description);
1334 parse_data->current_item->description = g_strdup (payload);
1338 g_free (parse_data->bookmark_file->description);
1339 parse_data->bookmark_file->description = g_strdup (payload);
1346 g_assert (parse_data->current_item != NULL);
1348 if (!parse_data->current_item->metadata)
1349 parse_data->current_item->metadata = bookmark_metadata_new ();
1351 groups = parse_data->current_item->metadata->groups;
1352 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1356 case STATE_BOOKMARK:
1358 case STATE_METADATA:
1359 case STATE_APPLICATIONS:
1360 case STATE_APPLICATION:
1366 g_assert_not_reached ();
1373 static const GMarkupParser markup_parser =
1375 start_element_raw_cb, /* start_element */
1376 end_element_raw_cb, /* end_element */
1377 text_raw_cb, /* text */
1378 NULL, /* passthrough */
1383 g_bookmark_file_parse (GBookmarkFile *bookmark,
1384 const gchar *buffer,
1388 GMarkupParseContext *context;
1389 ParseData *parse_data;
1390 GError *parse_error, *end_error;
1393 g_assert (bookmark != NULL);
1399 length = strlen (buffer);
1401 parse_data = parse_data_new ();
1402 parse_data->bookmark_file = bookmark;
1404 context = g_markup_parse_context_new (&markup_parser,
1407 (GDestroyNotify) parse_data_free);
1410 retval = g_markup_parse_context_parse (context,
1416 g_propagate_error (error, parse_error);
1422 retval = g_markup_parse_context_end_parse (context, &end_error);
1425 g_propagate_error (error, end_error);
1430 g_markup_parse_context_free (context);
1436 g_bookmark_file_dump (GBookmarkFile *bookmark,
1443 retval = g_string_new (NULL);
1445 g_string_append_printf (retval,
1446 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1448 /* XXX - do we really need the doctype? */
1454 " xmlns:%s=\"%s\"\n"
1455 " xmlns:%s=\"%s\"\n>",
1457 /* XXX - do we really need the doctype? */
1459 XBEL_DTD_SYSTEM, XBEL_DTD_URI,
1462 XBEL_VERSION_ATTRIBUTE, XBEL_VERSION,
1463 BOOKMARK_NAMESPACE_NAME, BOOKMARK_NAMESPACE_URI,
1464 MIME_NAMESPACE_NAME, MIME_NAMESPACE_URI);
1466 if (bookmark->title)
1468 gchar *escaped_title;
1470 escaped_title = g_markup_escape_text (bookmark->title, -1);
1472 g_string_append_printf (retval, " <%s>%s</%s>\n",
1475 XBEL_TITLE_ELEMENT);
1477 g_free (escaped_title);
1480 if (bookmark->description)
1482 gchar *escaped_desc;
1484 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1486 g_string_append_printf (retval, " <%s>%s</%s>\n",
1491 g_free (escaped_desc);
1494 if (!bookmark->items)
1497 retval = g_string_append (retval, "\n");
1499 for (l = g_list_last (bookmark->items);
1503 BookmarkItem *item = (BookmarkItem *) l->data;
1506 item_dump = bookmark_item_dump (item);
1510 retval = g_string_append (retval, item_dump);
1516 g_string_append_printf (retval, "</%s>", XBEL_ROOT_ELEMENT);
1519 *length = retval->len;
1521 return g_string_free (retval, FALSE);
1528 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1529 * should free the returned string.
1532 timestamp_to_iso8601 (time_t timestamp)
1536 if (timestamp == (time_t) -1)
1537 g_get_current_time (&stamp);
1540 stamp.tv_sec = timestamp;
1544 return g_time_val_to_iso8601 (&stamp);
1548 timestamp_from_iso8601 (const gchar *iso_date)
1552 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1555 return (time_t) stamp.tv_sec;
1561 g_bookmark_file_error_quark (void)
1563 return g_quark_from_static_string ("egg-bookmark-file-error-quark");
1568 /********************
1570 ********************/
1573 * g_bookmark_file_new:
1575 * Creates a new empty #GBookmarkFile object.
1577 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1578 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1581 * Return value: an empty #GBookmarkFile
1586 g_bookmark_file_new (void)
1588 GBookmarkFile *bookmark;
1590 bookmark = g_new (GBookmarkFile, 1);
1592 g_bookmark_file_init (bookmark);
1598 * g_bookmark_file_free:
1599 * @bookmark: a #GBookmarkFile
1601 * Frees a #GBookmarkFile.
1606 g_bookmark_file_free (GBookmarkFile *bookmark)
1611 g_bookmark_file_clear (bookmark);
1617 * g_bookmark_file_load_from_data:
1618 * @bookmark: an empty #GBookmarkFile struct
1619 * @data: desktop bookmarks loaded in memory
1620 * @length: the length of @data in bytes
1621 * @error: return location for a #GError, or %NULL
1623 * Loads a bookmark file from memory into an empty #GBookmarkFile
1624 * structure. If the object cannot be created then @error is set to a
1625 * #GBookmarkFileError.
1627 * Return value: %TRUE if a desktop bookmark could be loaded.
1632 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1637 GError *parse_error;
1640 g_return_val_if_fail (bookmark != NULL, FALSE);
1641 g_return_val_if_fail (data != NULL, FALSE);
1642 g_return_val_if_fail (length != 0, FALSE);
1644 if (length == (gsize) -1)
1645 length = strlen (data);
1647 if (bookmark->items)
1649 g_bookmark_file_clear (bookmark);
1650 g_bookmark_file_init (bookmark);
1654 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1657 g_propagate_error (error, parse_error);
1666 * g_bookmark_file_load_from_file:
1667 * @bookmark: an empty #GBookmarkFile struct
1668 * @filename: the path of a filename to load, in the GLib file name encoding
1669 * @error: return location for a #GError, or %NULL
1671 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1672 * If the file could not be loaded then @error is set to either a #GFileError
1673 * or #GBookmarkFileError.
1675 * Return value: %TRUE if a desktop bookmark file could be loaded
1680 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1681 const gchar *filename,
1689 g_return_val_if_fail (bookmark != NULL, FALSE);
1690 g_return_val_if_fail (filename != NULL, FALSE);
1693 g_file_get_contents (filename, &buffer, &len, &read_error);
1696 g_propagate_error (error, read_error);
1702 retval = g_bookmark_file_load_from_data (bookmark,
1708 g_propagate_error (error, read_error);
1721 /* Iterates through all the directories in *dirs trying to
1722 * find file. When it successfully locates file, returns a
1723 * string its absolute path. It also leaves the unchecked
1724 * directories in *dirs. You should free the returned string
1726 * Adapted from gkeyfile.c
1729 find_file_in_data_dirs (const gchar *file,
1733 gchar **data_dirs, *data_dir, *path;
1742 while (data_dirs && (data_dir = *data_dirs) && !path)
1744 gchar *candidate_file, *sub_dir;
1746 candidate_file = (gchar *) file;
1747 sub_dir = g_strdup ("");
1748 while (candidate_file != NULL && !path)
1752 path = g_build_filename (data_dir, sub_dir,
1753 candidate_file, NULL);
1755 candidate_file = strchr (candidate_file, '-');
1757 if (candidate_file == NULL)
1763 sub_dir = g_strndup (file, candidate_file - file - 1);
1765 for (p = sub_dir; *p != '\0'; p++)
1768 *p = G_DIR_SEPARATOR;
1779 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1780 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1781 _("No valid bookmark file found in data dirs"));
1791 * g_bookmark_file_load_from_data_dirs:
1792 * @bookmark: a #GBookmarkFile
1793 * @file: a relative path to a filename to open and parse
1794 * @full_path: return location for a string containing the full path
1795 * of the file, or %NULL
1796 * @error: return location for a #GError, or %NULL
1798 * This function looks for a desktop bookmark file named @file in the
1799 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1800 * loads the file into @bookmark and returns the file's full path in
1801 * @full_path. If the file could not be loaded then an %error is
1802 * set to either a #GFileError or #GBookmarkFileError.
1804 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1809 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1814 GError *file_error = NULL;
1815 gchar **all_data_dirs, **data_dirs;
1816 const gchar *user_data_dir;
1817 const gchar * const * system_data_dirs;
1820 gboolean found_file;
1822 g_return_val_if_fail (bookmark != NULL, FALSE);
1823 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1825 user_data_dir = g_get_user_data_dir ();
1826 system_data_dirs = g_get_system_data_dirs ();
1827 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1830 all_data_dirs[i++] = g_strdup (user_data_dir);
1833 while (system_data_dirs[j] != NULL)
1834 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1837 data_dirs = all_data_dirs;
1839 while (*data_dirs != NULL && !found_file)
1841 g_free (output_path);
1843 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1847 g_propagate_error (error, file_error);
1851 found_file = g_bookmark_file_load_from_file (bookmark,
1856 g_propagate_error (error, file_error);
1861 if (found_file && full_path)
1862 *full_path = output_path;
1864 g_free (output_path);
1866 g_strfreev (all_data_dirs);
1873 * g_bookmark_file_to_data:
1874 * @bookmark: a #GBookmarkFile
1875 * @length: return location for the length of the returned string, or %NULL
1876 * @error: return location for a #GError, or %NULL
1878 * This function outputs @bookmark as a string.
1880 * Return value: a newly allocated string holding
1881 * the contents of the #GBookmarkFile
1886 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1890 GError *write_error = NULL;
1893 g_return_val_if_fail (bookmark != NULL, NULL);
1895 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1898 g_propagate_error (error, write_error);
1907 * g_bookmark_file_to_file:
1908 * @bookmark: a #GBookmarkFile
1909 * @filename: path of the output file
1910 * @error: return location for a #GError, or %NULL
1912 * This function outputs @bookmark into a file. The write process is
1913 * guaranteed to be atomic by using g_file_set_contents() internally.
1915 * Return value: %TRUE if the file was successfully written.
1920 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1921 const gchar *filename,
1925 GError *data_error, *write_error;
1929 g_return_val_if_fail (bookmark != NULL, FALSE);
1930 g_return_val_if_fail (filename != NULL, FALSE);
1933 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1936 g_propagate_error (error, data_error);
1942 g_file_set_contents (filename, data, len, &write_error);
1945 g_propagate_error (error, write_error);
1957 static BookmarkItem *
1958 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1961 g_assert (bookmark != NULL && uri != NULL);
1963 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1966 /* this function adds a new item to the list */
1968 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1972 g_assert (bookmark != NULL);
1973 g_assert (item != NULL);
1975 /* this should never happen; and if it does, then we are
1976 * screwing up something big time.
1978 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1980 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1981 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1982 _("A bookmark for URI '%s' already exists"),
1987 bookmark->items = g_list_prepend (bookmark->items, item);
1989 g_hash_table_replace (bookmark->items_by_uri,
1993 if (item->added == (time_t) -1)
1994 item->added = time (NULL);
1996 if (item->modified == (time_t) -1)
1997 item->modified = time (NULL);
2001 * g_bookmark_file_remove_item:
2002 * @bookmark: a #GBookmarkFile
2004 * @error: return location for a #GError, or %NULL
2006 * Removes the bookmark for @uri from the bookmark file @bookmark.
2008 * Return value: %TRUE if the bookmark was removed successfully.
2013 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2019 g_return_val_if_fail (bookmark != NULL, FALSE);
2020 g_return_val_if_fail (uri != NULL, FALSE);
2022 item = g_bookmark_file_lookup_item (bookmark, uri);
2026 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2027 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2028 _("No bookmark found for URI '%s'"),
2033 bookmark->items = g_list_remove (bookmark->items, item);
2034 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2036 bookmark_item_free (item);
2042 * g_bookmark_file_has_item:
2043 * @bookmark: a #GBookmarkFile
2046 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2048 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2053 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2056 g_return_val_if_fail (bookmark != NULL, FALSE);
2057 g_return_val_if_fail (uri != NULL, FALSE);
2059 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2063 * g_bookmark_file_get_uris:
2064 * @bookmark: a #GBookmarkFile
2065 * @length: return location for the number of returned URIs, or %NULL
2067 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2068 * The array of returned URIs will be %NULL-terminated, so @length may
2069 * optionally be %NULL.
2071 * Return value: a newly allocated %NULL-terminated array of strings.
2072 * Use g_strfreev() to free it.
2077 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2084 g_return_val_if_fail (bookmark != NULL, NULL);
2086 n_items = g_list_length (bookmark->items);
2087 uris = g_new0 (gchar *, n_items + 1);
2089 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2091 BookmarkItem *item = (BookmarkItem *) l->data;
2093 g_assert (item != NULL);
2095 uris[i++] = g_strdup (item->uri);
2106 * g_bookmark_file_set_title:
2107 * @bookmark: a #GBookmarkFile
2108 * @uri: a valid URI or %NULL
2109 * @title: a UTF-8 encoded string
2111 * Sets @title as the title of the bookmark for @uri inside the
2112 * bookmark file @bookmark.
2114 * If @uri is %NULL, the title of @bookmark is set.
2116 * If a bookmark for @uri cannot be found then it is created.
2121 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2125 g_return_if_fail (bookmark != NULL);
2129 g_free (bookmark->title);
2130 bookmark->title = g_strdup (title);
2136 item = g_bookmark_file_lookup_item (bookmark, uri);
2139 item = bookmark_item_new (uri);
2140 g_bookmark_file_add_item (bookmark, item, NULL);
2143 g_free (item->title);
2144 item->title = g_strdup (title);
2146 item->modified = time (NULL);
2151 * g_bookmark_file_get_title:
2152 * @bookmark: a #GBookmarkFile
2153 * @uri: a valid URI or %NULL
2154 * @error: return location for a #GError, or %NULL
2156 * Returns the title of the bookmark for @uri.
2158 * If @uri is %NULL, the title of @bookmark is returned.
2160 * In the event the URI cannot be found, %NULL is returned and
2161 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2163 * Return value: a newly allocated string or %NULL if the specified
2164 * URI cannot be found.
2169 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2175 g_return_val_if_fail (bookmark != NULL, NULL);
2178 return g_strdup (bookmark->title);
2180 item = g_bookmark_file_lookup_item (bookmark, uri);
2183 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2184 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2185 _("No bookmark found for URI '%s'"),
2190 return g_strdup (item->title);
2194 * g_bookmark_file_set_description:
2195 * @bookmark: a #GBookmarkFile
2196 * @uri: a valid URI or %NULL
2197 * @description: a string
2199 * Sets @description as the description of the bookmark for @uri.
2201 * If @uri is %NULL, the description of @bookmark is set.
2203 * If a bookmark for @uri cannot be found then it is created.
2208 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2210 const gchar *description)
2212 g_return_if_fail (bookmark != NULL);
2216 g_free (bookmark->description);
2217 bookmark->description = g_strdup (description);
2223 item = g_bookmark_file_lookup_item (bookmark, uri);
2226 item = bookmark_item_new (uri);
2227 g_bookmark_file_add_item (bookmark, item, NULL);
2230 g_free (item->description);
2231 item->description = g_strdup (description);
2233 item->modified = time (NULL);
2238 * g_bookmark_file_get_description:
2239 * @bookmark: a #GBookmarkFile
2241 * @error: return location for a #GError, or %NULL
2243 * Retrieves the description of the bookmark for @uri.
2245 * In the event the URI cannot be found, %NULL is returned and
2246 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2248 * Return value: a newly allocated string or %NULL if the specified
2249 * URI cannot be found.
2254 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2260 g_return_val_if_fail (bookmark != NULL, NULL);
2263 return g_strdup (bookmark->description);
2265 item = g_bookmark_file_lookup_item (bookmark, uri);
2268 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2269 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2270 _("No bookmark found for URI '%s'"),
2275 return g_strdup (item->description);
2279 * g_bookmark_file_set_mime_type:
2280 * @bookmark: a #GBookmarkFile
2282 * @mime_type: a MIME type
2284 * Sets @mime_type as the MIME type of the bookmark for @uri.
2286 * If a bookmark for @uri cannot be found then it is created.
2291 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2293 const gchar *mime_type)
2297 g_return_if_fail (bookmark != NULL);
2298 g_return_if_fail (uri != NULL);
2299 g_return_if_fail (mime_type != NULL);
2301 item = g_bookmark_file_lookup_item (bookmark, uri);
2304 item = bookmark_item_new (uri);
2305 g_bookmark_file_add_item (bookmark, item, NULL);
2308 if (!item->metadata)
2309 item->metadata = bookmark_metadata_new ();
2311 g_free (item->metadata->mime_type);
2313 item->metadata->mime_type = g_strdup (mime_type);
2314 item->modified = time (NULL);
2318 * g_bookmark_file_get_mime_type:
2319 * @bookmark: a #GBookmarkFile
2321 * @error: return location for a #GError, or %NULL
2323 * Retrieves the MIME type of the resource pointed by @uri.
2325 * In the event the URI cannot be found, %NULL is returned and
2326 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2327 * event that the MIME type cannot be found, %NULL is returned and
2328 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2330 * Return value: a newly allocated string or %NULL if the specified
2331 * URI cannot be found.
2336 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2342 g_return_val_if_fail (bookmark != NULL, NULL);
2343 g_return_val_if_fail (uri != NULL, NULL);
2345 item = g_bookmark_file_lookup_item (bookmark, uri);
2348 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2349 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2350 _("No bookmark found for URI '%s'"),
2355 if (!item->metadata)
2357 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2358 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2359 _("No MIME type defined in the bookmark for URI '%s'"),
2364 return g_strdup (item->metadata->mime_type);
2368 * g_bookmark_file_set_is_private:
2369 * @bookmark: a #GBookmarkFile
2371 * @is_private: %TRUE if the bookmark should be marked as private
2373 * Sets the private flag of the bookmark for @uri.
2375 * If a bookmark for @uri cannot be found then it is created.
2380 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2382 gboolean is_private)
2386 g_return_if_fail (bookmark != NULL);
2387 g_return_if_fail (uri != NULL);
2389 item = g_bookmark_file_lookup_item (bookmark, uri);
2392 item = bookmark_item_new (uri);
2393 g_bookmark_file_add_item (bookmark, item, NULL);
2396 if (!item->metadata)
2397 item->metadata = bookmark_metadata_new ();
2399 item->metadata->is_private = (is_private == TRUE);
2400 item->modified = time (NULL);
2404 * g_bookmark_file_get_is_private:
2405 * @bookmark: a #GBookmarkFile
2407 * @error: return location for a #GError, or %NULL
2409 * Gets whether the private flag of the bookmark for @uri is set.
2411 * In the event the URI cannot be found, %FALSE is returned and
2412 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2413 * event that the private flag cannot be found, %FALSE is returned and
2414 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2416 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2421 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2427 g_return_val_if_fail (bookmark != NULL, FALSE);
2428 g_return_val_if_fail (uri != NULL, FALSE);
2430 item = g_bookmark_file_lookup_item (bookmark, uri);
2433 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2434 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2435 _("No bookmark found for URI '%s'"),
2440 if (!item->metadata)
2442 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2443 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2444 _("No private flag has been defined in bookmark for URI '%s'"),
2449 return item->metadata->is_private;
2453 * g_bookmark_file_set_added:
2454 * @bookmark: a #GBookmarkFile
2456 * @added: a timestamp or -1 to use the current time
2458 * Sets the time the bookmark for @uri was added into @bookmark.
2460 * If no bookmark for @uri is found then it is created.
2465 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2471 g_return_if_fail (bookmark != NULL);
2472 g_return_if_fail (uri != NULL);
2474 item = g_bookmark_file_lookup_item (bookmark, uri);
2477 item = bookmark_item_new (uri);
2478 g_bookmark_file_add_item (bookmark, item, NULL);
2481 if (added == (time_t) -1)
2484 item->added = added;
2485 item->modified = added;
2489 * g_bookmark_file_get_added:
2490 * @bookmark: a #GBookmarkFile
2492 * @error: return location for a #GError, or %NULL
2494 * Gets the time the bookmark for @uri was added to @bookmark
2496 * In the event the URI cannot be found, -1 is returned and
2497 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2499 * Return value: a timestamp
2504 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2510 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2511 g_return_val_if_fail (uri != NULL, (time_t) -1);
2513 item = g_bookmark_file_lookup_item (bookmark, uri);
2516 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2517 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2518 _("No bookmark found for URI '%s'"),
2527 * g_bookmark_file_set_modified:
2528 * @bookmark: a #GBookmarkFile
2530 * @modified: a timestamp or -1 to use the current time
2532 * Sets the last time the bookmark for @uri was last modified.
2534 * If no bookmark for @uri is found then it is created.
2536 * The "modified" time should only be set when the bookmark's meta-data
2537 * was actually changed. Every function of #GBookmarkFile that
2538 * modifies a bookmark also changes the modification time, except for
2539 * g_bookmark_file_set_visited().
2544 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2550 g_return_if_fail (bookmark != NULL);
2551 g_return_if_fail (uri != NULL);
2553 item = g_bookmark_file_lookup_item (bookmark, uri);
2556 item = bookmark_item_new (uri);
2557 g_bookmark_file_add_item (bookmark, item, NULL);
2560 if (modified == (time_t) -1)
2563 item->modified = modified;
2567 * g_bookmark_file_get_modified:
2568 * @bookmark: a #GBookmarkFile
2570 * @error: return location for a #GError, or %NULL
2572 * Gets the time when the bookmark for @uri was last modified.
2574 * In the event the URI cannot be found, -1 is returned and
2575 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2577 * Return value: a timestamp
2582 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2588 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2589 g_return_val_if_fail (uri != NULL, (time_t) -1);
2591 item = g_bookmark_file_lookup_item (bookmark, uri);
2594 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2595 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2596 _("No bookmark found for URI '%s'"),
2601 return item->modified;
2605 * g_bookmark_file_set_visited:
2606 * @bookmark: a #GBookmarkFile
2608 * @visited: a timestamp or -1 to use the current time
2610 * Sets the time the bookmark for @uri was last visited.
2612 * If no bookmark for @uri is found then it is created.
2614 * The "visited" time should only be set if the bookmark was launched,
2615 * either using the command line retrieved by g_bookmark_file_get_app_info()
2616 * or by the default application for the bookmark's MIME type, retrieved
2617 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2618 * does not affect the "modified" time.
2623 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2629 g_return_if_fail (bookmark != NULL);
2630 g_return_if_fail (uri != NULL);
2632 item = g_bookmark_file_lookup_item (bookmark, uri);
2635 item = bookmark_item_new (uri);
2636 g_bookmark_file_add_item (bookmark, item, NULL);
2639 if (visited == (time_t) -1)
2642 item->visited = visited;
2646 * g_bookmark_file_get_visited:
2647 * @bookmark: a #GBookmarkFile
2649 * @error: return location for a #GError, or %NULL
2651 * Gets the time the bookmark for @uri was last visited.
2653 * In the event the URI cannot be found, -1 is returned and
2654 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2656 * Return value: a timestamp.
2661 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2667 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2668 g_return_val_if_fail (uri != NULL, (time_t) -1);
2670 item = g_bookmark_file_lookup_item (bookmark, uri);
2673 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2674 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2675 _("No bookmark found for URI '%s'"),
2680 return item->visited;
2684 * g_bookmark_file_has_group:
2685 * @bookmark: a #GBookmarkFile
2687 * @group: the group name to be searched
2688 * @error: return location for a #GError, or %NULL
2690 * Checks whether @group appears in the list of groups to which
2691 * the bookmark for @uri belongs to.
2693 * In the event the URI cannot be found, %FALSE is returned and
2694 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2696 * Return value: %TRUE if @group was found.
2701 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2709 g_return_val_if_fail (bookmark != NULL, FALSE);
2710 g_return_val_if_fail (uri != NULL, FALSE);
2712 item = g_bookmark_file_lookup_item (bookmark, uri);
2715 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2716 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2717 _("No bookmark found for URI '%s'"),
2722 if (!item->metadata)
2725 for (l = item->metadata->groups; l != NULL; l = l->next)
2727 if (strcmp (l->data, group) == 0)
2736 * g_bookmark_file_add_group:
2737 * @bookmark: a #GBookmarkFile
2739 * @group: the group name to be added
2741 * Adds @group to the list of groups to which the bookmark for @uri
2744 * If no bookmark for @uri is found then it is created.
2749 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2755 g_return_if_fail (bookmark != NULL);
2756 g_return_if_fail (uri != NULL);
2757 g_return_if_fail (group != NULL && group[0] != '\0');
2759 item = g_bookmark_file_lookup_item (bookmark, uri);
2762 item = bookmark_item_new (uri);
2763 g_bookmark_file_add_item (bookmark, item, NULL);
2766 if (!item->metadata)
2767 item->metadata = bookmark_metadata_new ();
2769 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2771 item->metadata->groups = g_list_prepend (item->metadata->groups,
2774 item->modified = time (NULL);
2779 * g_bookmark_file_remove_group:
2780 * @bookmark: a #GBookmarkFile
2782 * @group: the group name to be removed
2783 * @error: return location for a #GError, or %NULL
2785 * Removes @group from the list of groups to which the bookmark
2786 * for @uri belongs to.
2788 * In the event the URI cannot be found, %FALSE is returned and
2789 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2790 * In the event no group was defined, %FALSE is returned and
2791 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2793 * Return value: %TRUE if @group was successfully removed.
2798 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2806 g_return_val_if_fail (bookmark != NULL, FALSE);
2807 g_return_val_if_fail (uri != NULL, FALSE);
2809 item = g_bookmark_file_lookup_item (bookmark, uri);
2812 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2813 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2814 _("No bookmark found for URI '%s'"),
2819 if (!item->metadata)
2821 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2822 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2823 _("No groups set in bookmark for URI '%s'"),
2828 for (l = item->metadata->groups; l != NULL; l = l->next)
2830 if (strcmp (l->data, group) == 0)
2832 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2836 item->modified = time (NULL);
2846 * g_bookmark_file_set_groups:
2847 * @bookmark: a #GBookmarkFile
2848 * @uri: an item's URI
2849 * @groups: an array of group names, or %NULL to remove all groups
2850 * @length: number of group name values in @groups
2852 * Sets a list of group names for the item with URI @uri. Each previously
2853 * set group name list is removed.
2855 * If @uri cannot be found then an item for it is created.
2860 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2862 const gchar **groups,
2868 g_return_if_fail (bookmark != NULL);
2869 g_return_if_fail (uri != NULL);
2870 g_return_if_fail (groups != NULL);
2872 item = g_bookmark_file_lookup_item (bookmark, uri);
2875 item = bookmark_item_new (uri);
2876 g_bookmark_file_add_item (bookmark, item, NULL);
2879 if (!item->metadata)
2880 item->metadata = bookmark_metadata_new ();
2882 if (item->metadata->groups != NULL)
2884 g_list_foreach (item->metadata->groups,
2887 g_list_free (item->metadata->groups);
2888 item->metadata->groups = NULL;
2893 for (i = 0; groups[i] != NULL && i < length; i++)
2894 item->metadata->groups = g_list_append (item->metadata->groups,
2895 g_strdup (groups[i]));
2898 item->modified = time (NULL);
2902 * g_bookmark_file_get_groups:
2903 * @bookmark: a #GBookmarkFile
2905 * @length: return location for the length of the returned string, or %NULL
2906 * @error: return location for a #GError, or %NULL
2908 * Retrieves the list of group names of the bookmark for @uri.
2910 * In the event the URI cannot be found, %NULL is returned and
2911 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2913 * The returned array is %NULL terminated, so @length may optionally
2916 * Return value: a newly allocated %NULL-terminated array of group names.
2917 * Use g_strfreev() to free it.
2922 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2932 g_return_val_if_fail (bookmark != NULL, NULL);
2933 g_return_val_if_fail (uri != NULL, NULL);
2935 item = g_bookmark_file_lookup_item (bookmark, uri);
2938 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2939 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2940 _("No bookmark found for URI '%s'"),
2945 if (!item->metadata)
2953 len = g_list_length (item->metadata->groups);
2954 retval = g_new0 (gchar *, len + 1);
2955 for (l = g_list_last (item->metadata->groups), i = 0;
2959 gchar *group_name = (gchar *) l->data;
2961 g_assert (group_name != NULL);
2963 retval[i++] = g_strdup (group_name);
2974 * g_bookmark_file_add_application:
2975 * @bookmark: a #GBookmarkFile
2977 * @name: the name of the application registering the bookmark
2979 * @exec: command line to be used to launch the bookmark or %NULL
2981 * Adds the application with @name and @exec to the list of
2982 * applications that have registered a bookmark for @uri into
2985 * Every bookmark inside a #GBookmarkFile must have at least an
2986 * application registered. Each application must provide a name, a
2987 * command line useful for launching the bookmark, the number of times
2988 * the bookmark has been registered by the application and the last
2989 * time the application registered this bookmark.
2991 * If @name is %NULL, the name of the application will be the
2992 * same returned by g_get_application(); if @exec is %NULL, the
2993 * command line will be a composition of the program name as
2994 * returned by g_get_prgname() and the "%u" modifier, which will be
2995 * expanded to the bookmark's URI.
2997 * This function will automatically take care of updating the
2998 * registrations count and timestamping in case an application
2999 * with the same @name had already registered a bookmark for
3000 * @uri inside @bookmark.
3002 * If no bookmark for @uri is found, one is created.
3007 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3013 gchar *app_name, *app_exec;
3015 g_return_if_fail (bookmark != NULL);
3016 g_return_if_fail (uri != NULL);
3018 item = g_bookmark_file_lookup_item (bookmark, uri);
3021 item = bookmark_item_new (uri);
3022 g_bookmark_file_add_item (bookmark, item, NULL);
3025 if (name && name[0] != '\0')
3026 app_name = g_strdup (name);
3028 app_name = g_strdup (g_get_application_name ());
3030 if (exec && exec[0] != '\0')
3031 app_exec = g_strdup (exec);
3033 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3035 g_bookmark_file_set_app_info (bookmark, uri,
3047 * g_bookmark_file_remove_application:
3048 * @bookmark: a #GBookmarkFile
3050 * @name: the name of the application
3051 * @error: return location for a #GError or %NULL
3053 * Removes application registered with @name from the list of applications
3054 * that have registered a bookmark for @uri inside @bookmark.
3056 * In the event the URI cannot be found, %FALSE is returned and
3057 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3058 * In the event that no application with name @app_name has registered
3059 * a bookmark for @uri, %FALSE is returned and error is set to
3060 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3062 * Return value: %TRUE if the application was successfully removed.
3067 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3075 g_return_val_if_fail (bookmark != NULL, FALSE);
3076 g_return_val_if_fail (uri != NULL, FALSE);
3077 g_return_val_if_fail (name != NULL, FALSE);
3080 retval = g_bookmark_file_set_app_info (bookmark, uri,
3088 g_propagate_error (error, set_error);
3097 * g_bookmark_file_has_application:
3098 * @bookmark: a #GBookmarkFile
3100 * @name: the name of the application
3101 * @error: return location for a #GError or %NULL
3103 * Checks whether the bookmark for @uri inside @bookmark has been
3104 * registered by application @name.
3106 * In the event the URI cannot be found, %FALSE is returned and
3107 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3109 * Return value: %TRUE if the application @name was found
3114 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3121 g_return_val_if_fail (bookmark != NULL, FALSE);
3122 g_return_val_if_fail (uri != NULL, FALSE);
3123 g_return_val_if_fail (name != NULL, FALSE);
3125 item = g_bookmark_file_lookup_item (bookmark, uri);
3128 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3129 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3130 _("No bookmark found for URI '%s'"),
3135 return (NULL != bookmark_item_lookup_app_info (item, name));
3139 * g_bookmark_file_set_app_info:
3140 * @bookmark: a #GBookmarkFile
3142 * @name: an application's name
3143 * @exec: an application's command line
3144 * @count: the number of registrations done for this application
3145 * @stamp: the time of the last registration for this application
3146 * @error: return location for a #GError or %NULL
3148 * Sets the meta-data of application @name inside the list of
3149 * applications that have registered a bookmark for @uri inside
3152 * You should rarely use this function; use g_bookmark_file_add_application()
3153 * and g_bookmark_file_remove_application() instead.
3155 * @name can be any UTF-8 encoded string used to identify an
3157 * @exec can have one of these two modifiers: "%f", which will
3158 * be expanded as the local file name retrieved from the bookmark's
3159 * URI; "%u", which will be expanded as the bookmark's URI.
3160 * The expansion is done automatically when retrieving the stored
3161 * command line using the g_bookmark_file_get_app_info() function.
3162 * @count is the number of times the application has registered the
3163 * bookmark; if is < 0, the current registration count will be increased
3164 * by one, if is 0, the application with @name will be removed from
3165 * the list of registered applications.
3166 * @stamp is the Unix time of the last registration; if it is -1, the
3167 * current time will be used.
3169 * If you try to remove an application by setting its registration count to
3170 * zero, and no bookmark for @uri is found, %FALSE is returned and
3171 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3172 * in the event that no application @name has registered a bookmark
3173 * for @uri, %FALSE is returned and error is set to
3174 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3175 * for @uri is found, one is created.
3177 * Return value: %TRUE if the application's meta-data was successfully
3183 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3192 BookmarkAppInfo *ai;
3194 g_return_val_if_fail (bookmark != NULL, FALSE);
3195 g_return_val_if_fail (uri != NULL, FALSE);
3196 g_return_val_if_fail (name != NULL, FALSE);
3197 g_return_val_if_fail (exec != NULL, FALSE);
3199 item = g_bookmark_file_lookup_item (bookmark, uri);
3204 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3205 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3206 _("No bookmark found for URI '%s'"),
3212 item = bookmark_item_new (uri);
3213 g_bookmark_file_add_item (bookmark, item, NULL);
3217 ai = bookmark_item_lookup_app_info (item, name);
3222 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3223 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3224 _("No application with name '%s' registered a bookmark for '%s'"),
3231 ai = bookmark_app_info_new (name);
3233 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3234 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3240 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3241 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3242 bookmark_app_info_free (ai);
3244 item->modified = time (NULL);
3253 if (stamp != (time_t) -1)
3256 ai->stamp = time (NULL);
3258 if (exec && exec[0] != '\0')
3261 ai->exec = g_shell_quote (exec);
3264 item->modified = time (NULL);
3269 /* expands the application's command line */
3271 expand_exec_line (const gchar *exec_fmt,
3277 exec = g_string_new (NULL);
3278 while ((ch = *exec_fmt++) != '\0')
3282 exec = g_string_append_c (exec, ch);
3293 g_string_append (exec, uri);
3298 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3301 g_string_append (exec, file);
3306 g_string_free (exec, TRUE);
3313 exec = g_string_append_c (exec, ch);
3319 return g_string_free (exec, FALSE);
3323 * g_bookmark_file_get_app_info:
3324 * @bookmark: a #GBookmarkFile
3326 * @name: an application's name
3327 * @exec: location for the command line of the application, or %NULL
3328 * @count: return location for the registration count, or %NULL
3329 * @stamp: return location for the last registration time, or %NULL
3330 * @error: return location for a #GError, or %NULL
3332 * Gets the registration informations of @app_name for the bookmark for
3333 * @uri. See g_bookmark_file_set_app_info() for more informations about
3334 * the returned data.
3336 * The string returned in @app_exec must be freed.
3338 * In the event the URI cannot be found, %FALSE is returned and
3339 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3340 * event that no application with name @app_name has registered a bookmark
3341 * for @uri, %FALSE is returned and error is set to
3342 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3343 * the command line fails, an error of the #G_SHELL_ERROR domain is
3344 * set and %FALSE is returned.
3346 * Return value: %TRUE on success.
3351 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3360 BookmarkAppInfo *ai;
3362 g_return_val_if_fail (bookmark != NULL, FALSE);
3363 g_return_val_if_fail (uri != NULL, FALSE);
3364 g_return_val_if_fail (name != NULL, FALSE);
3366 item = g_bookmark_file_lookup_item (bookmark, uri);
3369 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3370 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3371 _("No bookmark found for URI '%s'"),
3376 ai = bookmark_item_lookup_app_info (item, name);
3379 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3380 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3381 _("No application with name '%s' registered a bookmark for '%s'"),
3389 GError *unquote_error = NULL;
3390 gchar *command_line;
3392 command_line = g_shell_unquote (ai->exec, &unquote_error);
3395 g_propagate_error (error, unquote_error);
3399 *exec = expand_exec_line (command_line, uri);
3402 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3403 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3404 _("Failed to expand exec line '%s' with URI '%s'"),
3406 g_free (command_line);
3411 g_free (command_line);
3424 * g_bookmark_file_get_applications:
3425 * @bookmark: a #GBookmarkFile
3427 * @length: return location of the length of the returned list, or %NULL
3428 * @error: return location for a #GError, or %NULL
3430 * Retrieves the names of the applications that have registered the
3431 * bookmark for @uri.
3433 * In the event the URI cannot be found, %NULL is returned and
3434 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3436 * Return value: a newly allocated %NULL-terminated array of strings.
3437 * Use g_strfreev() to free it.
3442 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3452 g_return_val_if_fail (bookmark != NULL, NULL);
3453 g_return_val_if_fail (uri != NULL, NULL);
3455 item = g_bookmark_file_lookup_item (bookmark, uri);
3458 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3459 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3460 _("No bookmark found for URI '%s'"),
3465 if (!item->metadata)
3473 n_apps = g_list_length (item->metadata->applications);
3474 apps = g_new0 (gchar *, n_apps + 1);
3476 for (l = g_list_last (item->metadata->applications), i = 0;
3480 BookmarkAppInfo *ai;
3482 ai = (BookmarkAppInfo *) l->data;
3484 g_assert (ai != NULL);
3485 g_assert (ai->name != NULL);
3487 apps[i++] = g_strdup (ai->name);
3498 * g_bookmark_file_get_size:
3499 * @bookmark: a #GBookmarkFile
3501 * Gets the number of bookmarks inside @bookmark.
3503 * Return value: the number of bookmarks
3508 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3510 g_return_val_if_fail (bookmark != NULL, 0);
3512 return g_list_length (bookmark->items);
3516 * g_bookmark_file_move_item:
3517 * @bookmark: a #GBookmarkFile
3518 * @old_uri: a valid URI
3519 * @new_uri: a valid URI, or %NULL
3520 * @error: return location for a #GError or %NULL
3522 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3523 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3524 * %NULL, then the bookmark is removed.
3526 * In the event the URI cannot be found, %FALSE is returned and
3527 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3529 * Return value: %TRUE if the URI was successfully changed
3534 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3535 const gchar *old_uri,
3536 const gchar *new_uri,
3540 GError *remove_error;
3542 g_return_val_if_fail (bookmark != NULL, FALSE);
3543 g_return_val_if_fail (old_uri != NULL, FALSE);
3545 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3548 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3549 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3550 _("No bookmark found for URI '%s'"),
3555 if (new_uri && new_uri[0] != '\0')
3557 if (g_bookmark_file_has_item (bookmark, new_uri))
3559 remove_error = NULL;
3560 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3563 g_propagate_error (error, remove_error);
3569 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3572 item->uri = g_strdup (new_uri);
3573 item->modified = time (NULL);
3575 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3581 remove_error = NULL;
3582 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3585 g_propagate_error (error, remove_error);
3595 * g_bookmark_file_set_icon:
3596 * @bookmark: a #GBookmarkFile
3598 * @href: the URI of the icon for the bookmark, or %NULL
3599 * @mime_type: the MIME type of the icon for the bookmark
3601 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3602 * the currently set icon.
3604 * If no bookmark for @uri is found it is created.
3609 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3612 const gchar *mime_type)
3616 g_return_if_fail (bookmark != NULL);
3617 g_return_if_fail (uri != NULL);
3619 item = g_bookmark_file_lookup_item (bookmark, uri);
3622 item = bookmark_item_new (uri);
3623 g_bookmark_file_add_item (bookmark, item, NULL);
3626 if (!item->metadata)
3627 item->metadata = bookmark_metadata_new ();
3629 g_free (item->metadata->icon_href);
3630 g_free (item->metadata->icon_mime);
3632 item->metadata->icon_href = g_strdup (href);
3634 if (mime_type && mime_type[0] != '\0')
3635 item->metadata->icon_mime = g_strdup (mime_type);
3637 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3639 item->modified = time (NULL);
3643 * g_bookmark_file_get_icon:
3644 * @bookmark: a #GBookmarkFile
3646 * @href: return location for the icon's location or %NULL
3647 * @mime_type: return location for the icon's MIME type or %NULL
3648 * @error: return location for a #GError or %NULL
3650 * Gets the icon of the bookmark for @uri.
3652 * In the event the URI cannot be found, %FALSE is returned and
3653 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3655 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3656 * You should free the returned strings.
3661 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3669 g_return_val_if_fail (bookmark != NULL, FALSE);
3670 g_return_val_if_fail (uri != NULL, FALSE);
3672 item = g_bookmark_file_lookup_item (bookmark, uri);
3675 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3676 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3677 _("No bookmark found for URI '%s'"),
3682 if ((!item->metadata) || (!item->metadata->icon_href))
3686 *href = g_strdup (item->metadata->icon_href);
3689 *mime_type = g_strdup (item->metadata->icon_mime);
3694 #define __G_BOOKMARK_FILE_C__
3695 #include "galiasdef.c"