1 /* gbookmarkfile.c: parsing and building desktop bookmarks
3 * Copyright (C) 2005-2006 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 #include "gbookmarkfile.h"
32 #include <sys/types.h>
41 #include "gfileutils.h"
49 #include "gmessages.h"
54 #include "gstrfuncs.h"
60 /* XBEL 1.0 standard entities */
61 #define XBEL_VERSION "1.0"
62 #define XBEL_DTD_NICK "xbel"
63 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
64 "Exchange Language 1.0//EN//XML"
66 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
68 #define XBEL_ROOT_ELEMENT "xbel"
69 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
70 #define XBEL_BOOKMARK_ELEMENT "bookmark"
71 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
72 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
73 #define XBEL_TITLE_ELEMENT "title"
74 #define XBEL_DESC_ELEMENT "desc"
75 #define XBEL_INFO_ELEMENT "info"
76 #define XBEL_METADATA_ELEMENT "metadata"
78 #define XBEL_VERSION_ATTRIBUTE "version"
79 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
80 #define XBEL_OWNER_ATTRIBUTE "owner"
81 #define XBEL_ADDED_ATTRIBUTE "added"
82 #define XBEL_VISITED_ATTRIBUTE "visited"
83 #define XBEL_MODIFIED_ATTRIBUTE "modified"
84 #define XBEL_ID_ATTRIBUTE "id"
85 #define XBEL_HREF_ATTRIBUTE "href"
86 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
88 #define XBEL_YES_VALUE "yes"
89 #define XBEL_NO_VALUE "no"
91 /* Desktop bookmark spec entities */
92 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
94 #define BOOKMARK_NAMESPACE_NAME "bookmark"
95 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
97 #define BOOKMARK_GROUPS_ELEMENT "groups"
98 #define BOOKMARK_GROUP_ELEMENT "group"
99 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
100 #define BOOKMARK_APPLICATION_ELEMENT "application"
101 #define BOOKMARK_ICON_ELEMENT "icon"
102 #define BOOKMARK_PRIVATE_ELEMENT "private"
104 #define BOOKMARK_NAME_ATTRIBUTE "name"
105 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
106 #define BOOKMARK_COUNT_ATTRIBUTE "count"
107 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp"
108 #define BOOKMARK_HREF_ATTRIBUTE "href"
109 #define BOOKMARK_TYPE_ATTRIBUTE "type"
111 /* Shared MIME Info entities */
112 #define MIME_NAMESPACE_NAME "mime"
113 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
114 #define MIME_TYPE_ELEMENT "mime-type"
115 #define MIME_TYPE_ATTRIBUTE "type"
118 typedef struct _BookmarkAppInfo BookmarkAppInfo;
119 typedef struct _BookmarkMetadata BookmarkMetadata;
120 typedef struct _BookmarkItem BookmarkItem;
121 typedef struct _ParseData ParseData;
123 struct _BookmarkAppInfo
133 struct _BookmarkMetadata
140 GHashTable *apps_by_name;
145 guint is_private : 1;
159 BookmarkMetadata *metadata;
162 struct _GBookmarkFile
167 /* we store our items in a list and keep a copy inside
168 * an hash table for faster lookup performances
171 GHashTable *items_by_uri;
174 /* parser state machine */
195 static void g_bookmark_file_init (GBookmarkFile *bookmark);
196 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
197 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
201 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
204 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
206 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
210 static time_t timestamp_from_iso8601 (const gchar *iso_date);
211 static gchar * timestamp_to_iso8601 (time_t timestamp);
213 /********************************
216 * Application metadata storage *
217 ********************************/
218 static BookmarkAppInfo *
219 bookmark_app_info_new (const gchar *name)
221 BookmarkAppInfo *retval;
223 g_warn_if_fail (name != NULL);
225 retval = g_slice_new (BookmarkAppInfo);
227 retval->name = g_strdup (name);
230 retval->stamp = time (NULL);
236 bookmark_app_info_free (BookmarkAppInfo *app_info)
241 g_free (app_info->name);
242 g_free (app_info->exec);
244 g_slice_free (BookmarkAppInfo, app_info);
248 bookmark_app_info_dump (BookmarkAppInfo *app_info)
253 g_warn_if_fail (app_info != NULL);
255 if (app_info->count == 0)
258 name = g_markup_escape_text (app_info->name, -1);
259 exec = g_markup_escape_text (app_info->exec, -1);
261 retval = g_strdup_printf (" <%s:%s %s=\"%s\" %s=\"%s\" %s=\"%ld\" %s=\"%u\"/>\n",
262 BOOKMARK_NAMESPACE_NAME,
263 BOOKMARK_APPLICATION_ELEMENT,
264 BOOKMARK_NAME_ATTRIBUTE, name,
265 BOOKMARK_EXEC_ATTRIBUTE, exec,
266 BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
267 BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
276 /***********************
280 ***********************/
281 static BookmarkMetadata *
282 bookmark_metadata_new (void)
284 BookmarkMetadata *retval;
286 retval = g_slice_new (BookmarkMetadata);
288 retval->mime_type = NULL;
290 retval->groups = NULL;
292 retval->applications = NULL;
293 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
298 retval->is_private = FALSE;
300 retval->icon_href = NULL;
301 retval->icon_mime = NULL;
307 bookmark_metadata_free (BookmarkMetadata *metadata)
312 g_free (metadata->mime_type);
314 if (metadata->groups)
316 g_list_foreach (metadata->groups,
319 g_list_free (metadata->groups);
322 if (metadata->applications)
324 g_list_foreach (metadata->applications,
325 (GFunc) bookmark_app_info_free,
327 g_list_free (metadata->applications);
329 g_hash_table_destroy (metadata->apps_by_name);
332 g_free (metadata->icon_href);
333 g_free (metadata->icon_mime);
335 g_slice_free (BookmarkMetadata, metadata);
339 bookmark_metadata_dump (BookmarkMetadata *metadata)
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_warn_if_fail (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_warn_if_fail (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_warn_if_fail (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_warn_if_fail ((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_warn_if_fail (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_warn_if_fail ((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_warn_if_fail (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_warn_if_fail ((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_warn_if_fail (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_warn_if_fail ((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_warn_if_fail (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_warn_if_fail (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;
1002 const gchar *p, *element_name;
1005 g_warn_if_fail (parse_data != NULL);
1006 g_warn_if_fail (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 = g_utf8_strchr (element_full, -1, ':');
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 retval = (0 == strcmp (ns_uri, namespace) &&
1042 0 == strcmp (element_name, element));
1049 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1050 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1053 start_element_raw_cb (GMarkupParseContext *context,
1054 const gchar *element_name,
1055 const gchar **attribute_names,
1056 const gchar **attribute_values,
1060 ParseData *parse_data = (ParseData *) user_data;
1062 /* we must check for namespace declarations first
1064 * XXX - we could speed up things by checking for namespace declarations
1065 * only on the root node, where they usually are; this would probably break
1066 * on streams not produced by us or by "smart" generators
1068 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1070 switch (parse_data->state)
1073 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1079 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1081 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1082 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1083 parse_data->state = STATE_ROOT;
1087 g_set_error (error, G_MARKUP_ERROR,
1088 G_MARKUP_ERROR_INVALID_CONTENT,
1089 _("Unexpected tag '%s', tag '%s' expected"),
1090 element_name, XBEL_ROOT_ELEMENT);
1093 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1094 parse_data->state = STATE_TITLE;
1095 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1096 parse_data->state = STATE_DESC;
1097 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1099 GError *inner_error = NULL;
1101 parse_data->state = STATE_BOOKMARK;
1103 parse_bookmark_element (context,
1109 g_propagate_error (error, inner_error);
1112 g_set_error (error, G_MARKUP_ERROR,
1113 G_MARKUP_ERROR_INVALID_CONTENT,
1114 _("Unexpected tag '%s' inside '%s'"),
1118 case STATE_BOOKMARK:
1119 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1120 parse_data->state = STATE_TITLE;
1121 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1122 parse_data->state = STATE_DESC;
1123 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1124 parse_data->state = STATE_INFO;
1126 g_set_error (error, G_MARKUP_ERROR,
1127 G_MARKUP_ERROR_INVALID_CONTENT,
1128 _("Unexpected tag '%s' inside '%s'"),
1130 XBEL_BOOKMARK_ELEMENT);
1133 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1139 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1141 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1142 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1144 parse_data->state = STATE_METADATA;
1146 if (!parse_data->current_item->metadata)
1147 parse_data->current_item->metadata = bookmark_metadata_new ();
1152 g_set_error (error, G_MARKUP_ERROR,
1153 G_MARKUP_ERROR_INVALID_CONTENT,
1154 _("Unexpected tag '%s', tag '%s' expected"),
1156 XBEL_METADATA_ELEMENT);
1158 case STATE_METADATA:
1159 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1160 parse_data->state = STATE_APPLICATIONS;
1161 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1162 parse_data->state = STATE_GROUPS;
1163 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1164 parse_data->current_item->metadata->is_private = TRUE;
1165 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1167 GError *inner_error = NULL;
1169 parse_data->state = STATE_ICON;
1171 parse_icon_element (context,
1177 g_propagate_error (error, inner_error);
1179 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1181 GError *inner_error = NULL;
1183 parse_data->state = STATE_MIME;
1185 parse_mime_type_element (context,
1191 g_propagate_error (error, inner_error);
1194 g_set_error (error, G_MARKUP_ERROR,
1195 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1196 _("Unexpected tag '%s' inside '%s'"),
1198 XBEL_METADATA_ELEMENT);
1200 case STATE_APPLICATIONS:
1201 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1203 GError *inner_error = NULL;
1205 parse_data->state = STATE_APPLICATION;
1207 parse_application_element (context,
1213 g_propagate_error (error, inner_error);
1216 g_set_error (error, G_MARKUP_ERROR,
1217 G_MARKUP_ERROR_INVALID_CONTENT,
1218 _("Unexpected tag '%s', tag '%s' expected"),
1220 BOOKMARK_APPLICATION_ELEMENT);
1223 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1224 parse_data->state = STATE_GROUP;
1226 g_set_error (error, G_MARKUP_ERROR,
1227 G_MARKUP_ERROR_INVALID_CONTENT,
1228 _("Unexpected tag '%s', tag '%s' expected"),
1230 BOOKMARK_GROUP_ELEMENT);
1233 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1235 GError *inner_error = NULL;
1237 parse_icon_element (context,
1243 g_propagate_error (error, inner_error);
1246 g_set_error (error, G_MARKUP_ERROR,
1247 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1248 _("Unexpected tag '%s' inside '%s'"),
1250 XBEL_METADATA_ELEMENT);
1253 g_warn_if_reached ();
1259 end_element_raw_cb (GMarkupParseContext *context,
1260 const gchar *element_name,
1264 ParseData *parse_data = (ParseData *) user_data;
1266 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1267 parse_data->state = STATE_FINISHED;
1268 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1270 parse_data->current_item = NULL;
1272 parse_data->state = STATE_ROOT;
1274 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1275 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1276 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1278 if (parse_data->current_item)
1279 parse_data->state = STATE_BOOKMARK;
1281 parse_data->state = STATE_ROOT;
1283 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1284 parse_data->state = STATE_INFO;
1285 else if (IS_ELEMENT_NS (parse_data, element_name,
1286 BOOKMARK_NAMESPACE_URI,
1287 BOOKMARK_APPLICATION_ELEMENT))
1288 parse_data->state = STATE_APPLICATIONS;
1289 else if (IS_ELEMENT_NS (parse_data, element_name,
1290 BOOKMARK_NAMESPACE_URI,
1291 BOOKMARK_GROUP_ELEMENT))
1292 parse_data->state = STATE_GROUPS;
1293 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1294 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1295 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1296 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1297 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1298 parse_data->state = STATE_METADATA;
1302 text_raw_cb (GMarkupParseContext *context,
1308 ParseData *parse_data = (ParseData *) user_data;
1311 payload = g_strndup (text, length);
1313 switch (parse_data->state)
1316 if (parse_data->current_item)
1318 g_free (parse_data->current_item->title);
1319 parse_data->current_item->title = g_strdup (payload);
1323 g_free (parse_data->bookmark_file->title);
1324 parse_data->bookmark_file->title = g_strdup (payload);
1328 if (parse_data->current_item)
1330 g_free (parse_data->current_item->description);
1331 parse_data->current_item->description = g_strdup (payload);
1335 g_free (parse_data->bookmark_file->description);
1336 parse_data->bookmark_file->description = g_strdup (payload);
1343 g_warn_if_fail (parse_data->current_item != NULL);
1345 if (!parse_data->current_item->metadata)
1346 parse_data->current_item->metadata = bookmark_metadata_new ();
1348 groups = parse_data->current_item->metadata->groups;
1349 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1353 case STATE_BOOKMARK:
1355 case STATE_METADATA:
1356 case STATE_APPLICATIONS:
1357 case STATE_APPLICATION:
1363 g_warn_if_reached ();
1370 static const GMarkupParser markup_parser =
1372 start_element_raw_cb, /* start_element */
1373 end_element_raw_cb, /* end_element */
1374 text_raw_cb, /* text */
1375 NULL, /* passthrough */
1380 g_bookmark_file_parse (GBookmarkFile *bookmark,
1381 const gchar *buffer,
1385 GMarkupParseContext *context;
1386 ParseData *parse_data;
1387 GError *parse_error, *end_error;
1390 g_warn_if_fail (bookmark != NULL);
1396 length = strlen (buffer);
1398 parse_data = parse_data_new ();
1399 parse_data->bookmark_file = bookmark;
1401 context = g_markup_parse_context_new (&markup_parser,
1404 (GDestroyNotify) parse_data_free);
1407 retval = g_markup_parse_context_parse (context,
1413 g_propagate_error (error, parse_error);
1419 retval = g_markup_parse_context_end_parse (context, &end_error);
1422 g_propagate_error (error, end_error);
1427 g_markup_parse_context_free (context);
1433 g_bookmark_file_dump (GBookmarkFile *bookmark,
1440 retval = g_string_new (NULL);
1442 g_string_append_printf (retval,
1443 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1445 /* XXX - do we really need the doctype? */
1451 " xmlns:%s=\"%s\"\n"
1452 " xmlns:%s=\"%s\"\n>",
1454 /* XXX - do we really need the doctype? */
1456 XBEL_DTD_SYSTEM, XBEL_DTD_URI,
1459 XBEL_VERSION_ATTRIBUTE, XBEL_VERSION,
1460 BOOKMARK_NAMESPACE_NAME, BOOKMARK_NAMESPACE_URI,
1461 MIME_NAMESPACE_NAME, MIME_NAMESPACE_URI);
1463 if (bookmark->title)
1465 gchar *escaped_title;
1467 escaped_title = g_markup_escape_text (bookmark->title, -1);
1469 g_string_append_printf (retval, " <%s>%s</%s>\n",
1472 XBEL_TITLE_ELEMENT);
1474 g_free (escaped_title);
1477 if (bookmark->description)
1479 gchar *escaped_desc;
1481 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1483 g_string_append_printf (retval, " <%s>%s</%s>\n",
1488 g_free (escaped_desc);
1491 if (!bookmark->items)
1494 retval = g_string_append (retval, "\n");
1496 for (l = g_list_last (bookmark->items);
1500 BookmarkItem *item = (BookmarkItem *) l->data;
1503 item_dump = bookmark_item_dump (item);
1507 retval = g_string_append (retval, item_dump);
1513 g_string_append_printf (retval, "</%s>", XBEL_ROOT_ELEMENT);
1516 *length = retval->len;
1518 return g_string_free (retval, FALSE);
1525 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1526 * should free the returned string.
1529 timestamp_to_iso8601 (time_t timestamp)
1533 if (timestamp == (time_t) -1)
1534 g_get_current_time (&stamp);
1537 stamp.tv_sec = timestamp;
1541 return g_time_val_to_iso8601 (&stamp);
1545 timestamp_from_iso8601 (const gchar *iso_date)
1549 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1552 return (time_t) stamp.tv_sec;
1558 g_bookmark_file_error_quark (void)
1560 return g_quark_from_static_string ("egg-bookmark-file-error-quark");
1565 /********************
1567 ********************/
1570 * g_bookmark_file_new:
1572 * Creates a new empty #GBookmarkFile object.
1574 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1575 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1578 * Return value: an empty #GBookmarkFile
1583 g_bookmark_file_new (void)
1585 GBookmarkFile *bookmark;
1587 bookmark = g_new (GBookmarkFile, 1);
1589 g_bookmark_file_init (bookmark);
1595 * g_bookmark_file_free:
1596 * @bookmark: a #GBookmarkFile
1598 * Frees a #GBookmarkFile.
1603 g_bookmark_file_free (GBookmarkFile *bookmark)
1608 g_bookmark_file_clear (bookmark);
1614 * g_bookmark_file_load_from_data:
1615 * @bookmark: an empty #GBookmarkFile struct
1616 * @data: desktop bookmarks loaded in memory
1617 * @length: the length of @data in bytes
1618 * @error: return location for a #GError, or %NULL
1620 * Loads a bookmark file from memory into an empty #GBookmarkFile
1621 * structure. If the object cannot be created then @error is set to a
1622 * #GBookmarkFileError.
1624 * Return value: %TRUE if a desktop bookmark could be loaded.
1629 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1634 GError *parse_error;
1637 g_return_val_if_fail (bookmark != NULL, FALSE);
1638 g_return_val_if_fail (data != NULL, FALSE);
1639 g_return_val_if_fail (length != 0, FALSE);
1641 if (length == (gsize) -1)
1642 length = strlen (data);
1644 if (bookmark->items)
1646 g_bookmark_file_clear (bookmark);
1647 g_bookmark_file_init (bookmark);
1651 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1654 g_propagate_error (error, parse_error);
1663 * g_bookmark_file_load_from_file:
1664 * @bookmark: an empty #GBookmarkFile struct
1665 * @filename: the path of a filename to load, in the GLib file name encoding
1666 * @error: return location for a #GError, or %NULL
1668 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1669 * If the file could not be loaded then @error is set to either a #GFileError
1670 * or #GBookmarkFileError.
1672 * Return value: %TRUE if a desktop bookmark file could be loaded
1677 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1678 const gchar *filename,
1686 g_return_val_if_fail (bookmark != NULL, FALSE);
1687 g_return_val_if_fail (filename != NULL, FALSE);
1690 g_file_get_contents (filename, &buffer, &len, &read_error);
1693 g_propagate_error (error, read_error);
1699 retval = g_bookmark_file_load_from_data (bookmark,
1705 g_propagate_error (error, read_error);
1718 /* Iterates through all the directories in *dirs trying to
1719 * find file. When it successfully locates file, returns a
1720 * string its absolute path. It also leaves the unchecked
1721 * directories in *dirs. You should free the returned string
1723 * Adapted from gkeyfile.c
1726 find_file_in_data_dirs (const gchar *file,
1730 gchar **data_dirs, *data_dir, *path;
1739 while (data_dirs && (data_dir = *data_dirs) && !path)
1741 gchar *candidate_file, *sub_dir;
1743 candidate_file = (gchar *) file;
1744 sub_dir = g_strdup ("");
1745 while (candidate_file != NULL && !path)
1749 path = g_build_filename (data_dir, sub_dir,
1750 candidate_file, NULL);
1752 candidate_file = strchr (candidate_file, '-');
1754 if (candidate_file == NULL)
1760 sub_dir = g_strndup (file, candidate_file - file - 1);
1762 for (p = sub_dir; *p != '\0'; p++)
1765 *p = G_DIR_SEPARATOR;
1776 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1777 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1778 _("No valid bookmark file found in data dirs"));
1788 * g_bookmark_file_load_from_data_dirs:
1789 * @bookmark: a #GBookmarkFile
1790 * @file: a relative path to a filename to open and parse
1791 * @full_path: return location for a string containing the full path
1792 * of the file, or %NULL
1793 * @error: return location for a #GError, or %NULL
1795 * This function looks for a desktop bookmark file named @file in the
1796 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1797 * loads the file into @bookmark and returns the file's full path in
1798 * @full_path. If the file could not be loaded then an %error is
1799 * set to either a #GFileError or #GBookmarkFileError.
1801 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1806 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1811 GError *file_error = NULL;
1812 gchar **all_data_dirs, **data_dirs;
1813 const gchar *user_data_dir;
1814 const gchar * const * system_data_dirs;
1817 gboolean found_file;
1819 g_return_val_if_fail (bookmark != NULL, FALSE);
1820 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1822 user_data_dir = g_get_user_data_dir ();
1823 system_data_dirs = g_get_system_data_dirs ();
1824 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1827 all_data_dirs[i++] = g_strdup (user_data_dir);
1830 while (system_data_dirs[j] != NULL)
1831 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1834 data_dirs = all_data_dirs;
1836 while (*data_dirs != NULL && !found_file)
1838 g_free (output_path);
1840 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1844 g_propagate_error (error, file_error);
1848 found_file = g_bookmark_file_load_from_file (bookmark,
1853 g_propagate_error (error, file_error);
1858 if (found_file && full_path)
1859 *full_path = output_path;
1861 g_free (output_path);
1863 g_strfreev (all_data_dirs);
1870 * g_bookmark_file_to_data:
1871 * @bookmark: a #GBookmarkFile
1872 * @length: return location for the length of the returned string, or %NULL
1873 * @error: return location for a #GError, or %NULL
1875 * This function outputs @bookmark as a string.
1877 * Return value: a newly allocated string holding
1878 * the contents of the #GBookmarkFile
1883 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1887 GError *write_error = NULL;
1890 g_return_val_if_fail (bookmark != NULL, NULL);
1892 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1895 g_propagate_error (error, write_error);
1904 * g_bookmark_file_to_file:
1905 * @bookmark: a #GBookmarkFile
1906 * @filename: path of the output file
1907 * @error: return location for a #GError, or %NULL
1909 * This function outputs @bookmark into a file. The write process is
1910 * guaranteed to be atomic by using g_file_set_contents() internally.
1912 * Return value: %TRUE if the file was successfully written.
1917 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1918 const gchar *filename,
1922 GError *data_error, *write_error;
1926 g_return_val_if_fail (bookmark != NULL, FALSE);
1927 g_return_val_if_fail (filename != NULL, FALSE);
1930 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1933 g_propagate_error (error, data_error);
1939 g_file_set_contents (filename, data, len, &write_error);
1942 g_propagate_error (error, write_error);
1954 static BookmarkItem *
1955 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1958 g_warn_if_fail (bookmark != NULL && uri != NULL);
1960 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1963 /* this function adds a new item to the list */
1965 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1969 g_warn_if_fail (bookmark != NULL);
1970 g_warn_if_fail (item != NULL);
1972 /* this should never happen; and if it does, then we are
1973 * screwing up something big time.
1975 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1977 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1978 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1979 _("A bookmark for URI '%s' already exists"),
1984 bookmark->items = g_list_prepend (bookmark->items, item);
1986 g_hash_table_replace (bookmark->items_by_uri,
1990 if (item->added == (time_t) -1)
1991 item->added = time (NULL);
1993 if (item->modified == (time_t) -1)
1994 item->modified = time (NULL);
1998 * g_bookmark_file_remove_item:
1999 * @bookmark: a #GBookmarkFile
2001 * @error: return location for a #GError, or %NULL
2003 * Removes the bookmark for @uri from the bookmark file @bookmark.
2005 * Return value: %TRUE if the bookmark was removed successfully.
2010 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2016 g_return_val_if_fail (bookmark != NULL, FALSE);
2017 g_return_val_if_fail (uri != NULL, FALSE);
2019 item = g_bookmark_file_lookup_item (bookmark, uri);
2023 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2024 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2025 _("No bookmark found for URI '%s'"),
2030 bookmark->items = g_list_remove (bookmark->items, item);
2031 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2033 bookmark_item_free (item);
2039 * g_bookmark_file_has_item:
2040 * @bookmark: a #GBookmarkFile
2043 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2045 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2050 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2053 g_return_val_if_fail (bookmark != NULL, FALSE);
2054 g_return_val_if_fail (uri != NULL, FALSE);
2056 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2060 * g_bookmark_file_get_uris:
2061 * @bookmark: a #GBookmarkFile
2062 * @length: return location for the number of returned URIs, or %NULL
2064 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2065 * The array of returned URIs will be %NULL-terminated, so @length may
2066 * optionally be %NULL.
2068 * Return value: a newly allocated %NULL-terminated array of strings.
2069 * Use g_strfreev() to free it.
2074 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2081 g_return_val_if_fail (bookmark != NULL, NULL);
2083 n_items = g_list_length (bookmark->items);
2084 uris = g_new0 (gchar *, n_items + 1);
2086 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2088 BookmarkItem *item = (BookmarkItem *) l->data;
2090 g_warn_if_fail (item != NULL);
2092 uris[i++] = g_strdup (item->uri);
2103 * g_bookmark_file_set_title:
2104 * @bookmark: a #GBookmarkFile
2105 * @uri: a valid URI or %NULL
2106 * @title: a UTF-8 encoded string
2108 * Sets @title as the title of the bookmark for @uri inside the
2109 * bookmark file @bookmark.
2111 * If @uri is %NULL, the title of @bookmark is set.
2113 * If a bookmark for @uri cannot be found then it is created.
2118 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2122 g_return_if_fail (bookmark != NULL);
2126 g_free (bookmark->title);
2127 bookmark->title = g_strdup (title);
2133 item = g_bookmark_file_lookup_item (bookmark, uri);
2136 item = bookmark_item_new (uri);
2137 g_bookmark_file_add_item (bookmark, item, NULL);
2140 g_free (item->title);
2141 item->title = g_strdup (title);
2143 item->modified = time (NULL);
2148 * g_bookmark_file_get_title:
2149 * @bookmark: a #GBookmarkFile
2150 * @uri: a valid URI or %NULL
2151 * @error: return location for a #GError, or %NULL
2153 * Returns the title of the bookmark for @uri.
2155 * If @uri is %NULL, the title of @bookmark is returned.
2157 * In the event the URI cannot be found, %NULL is returned and
2158 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2160 * Return value: a newly allocated string or %NULL if the specified
2161 * URI cannot be found.
2166 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2172 g_return_val_if_fail (bookmark != NULL, NULL);
2175 return g_strdup (bookmark->title);
2177 item = g_bookmark_file_lookup_item (bookmark, uri);
2180 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2181 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2182 _("No bookmark found for URI '%s'"),
2187 return g_strdup (item->title);
2191 * g_bookmark_file_set_description:
2192 * @bookmark: a #GBookmarkFile
2193 * @uri: a valid URI or %NULL
2194 * @description: a string
2196 * Sets @description as the description of the bookmark for @uri.
2198 * If @uri is %NULL, the description of @bookmark is set.
2200 * If a bookmark for @uri cannot be found then it is created.
2205 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2207 const gchar *description)
2209 g_return_if_fail (bookmark != NULL);
2213 g_free (bookmark->description);
2214 bookmark->description = g_strdup (description);
2220 item = g_bookmark_file_lookup_item (bookmark, uri);
2223 item = bookmark_item_new (uri);
2224 g_bookmark_file_add_item (bookmark, item, NULL);
2227 g_free (item->description);
2228 item->description = g_strdup (description);
2230 item->modified = time (NULL);
2235 * g_bookmark_file_get_description:
2236 * @bookmark: a #GBookmarkFile
2238 * @error: return location for a #GError, or %NULL
2240 * Retrieves the description of the bookmark for @uri.
2242 * In the event the URI cannot be found, %NULL is returned and
2243 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2245 * Return value: a newly allocated string or %NULL if the specified
2246 * URI cannot be found.
2251 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2257 g_return_val_if_fail (bookmark != NULL, NULL);
2260 return g_strdup (bookmark->description);
2262 item = g_bookmark_file_lookup_item (bookmark, uri);
2265 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2266 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2267 _("No bookmark found for URI '%s'"),
2272 return g_strdup (item->description);
2276 * g_bookmark_file_set_mime_type:
2277 * @bookmark: a #GBookmarkFile
2279 * @mime_type: a MIME type
2281 * Sets @mime_type as the MIME type of the bookmark for @uri.
2283 * If a bookmark for @uri cannot be found then it is created.
2288 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2290 const gchar *mime_type)
2294 g_return_if_fail (bookmark != NULL);
2295 g_return_if_fail (uri != NULL);
2296 g_return_if_fail (mime_type != NULL);
2298 item = g_bookmark_file_lookup_item (bookmark, uri);
2301 item = bookmark_item_new (uri);
2302 g_bookmark_file_add_item (bookmark, item, NULL);
2305 if (!item->metadata)
2306 item->metadata = bookmark_metadata_new ();
2308 g_free (item->metadata->mime_type);
2310 item->metadata->mime_type = g_strdup (mime_type);
2311 item->modified = time (NULL);
2315 * g_bookmark_file_get_mime_type:
2316 * @bookmark: a #GBookmarkFile
2318 * @error: return location for a #GError, or %NULL
2320 * Retrieves the MIME type of the resource pointed by @uri.
2322 * In the event the URI cannot be found, %NULL is returned and
2323 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2324 * event that the MIME type cannot be found, %NULL is returned and
2325 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2327 * Return value: a newly allocated string or %NULL if the specified
2328 * URI cannot be found.
2333 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2339 g_return_val_if_fail (bookmark != NULL, NULL);
2340 g_return_val_if_fail (uri != NULL, NULL);
2342 item = g_bookmark_file_lookup_item (bookmark, uri);
2345 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2346 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2347 _("No bookmark found for URI '%s'"),
2352 if (!item->metadata)
2354 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2355 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2356 _("No MIME type defined in the bookmark for URI '%s'"),
2361 return g_strdup (item->metadata->mime_type);
2365 * g_bookmark_file_set_is_private:
2366 * @bookmark: a #GBookmarkFile
2368 * @is_private: %TRUE if the bookmark should be marked as private
2370 * Sets the private flag of the bookmark for @uri.
2372 * If a bookmark for @uri cannot be found then it is created.
2377 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2379 gboolean is_private)
2383 g_return_if_fail (bookmark != NULL);
2384 g_return_if_fail (uri != NULL);
2386 item = g_bookmark_file_lookup_item (bookmark, uri);
2389 item = bookmark_item_new (uri);
2390 g_bookmark_file_add_item (bookmark, item, NULL);
2393 if (!item->metadata)
2394 item->metadata = bookmark_metadata_new ();
2396 item->metadata->is_private = (is_private == TRUE);
2397 item->modified = time (NULL);
2401 * g_bookmark_file_get_is_private:
2402 * @bookmark: a #GBookmarkFile
2404 * @error: return location for a #GError, or %NULL
2406 * Gets whether the private flag of the bookmark for @uri is set.
2408 * In the event the URI cannot be found, %FALSE is returned and
2409 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2410 * event that the private flag cannot be found, %FALSE is returned and
2411 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2413 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2418 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2424 g_return_val_if_fail (bookmark != NULL, FALSE);
2425 g_return_val_if_fail (uri != NULL, FALSE);
2427 item = g_bookmark_file_lookup_item (bookmark, uri);
2430 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2431 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2432 _("No bookmark found for URI '%s'"),
2437 if (!item->metadata)
2439 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2440 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2441 _("No private flag has been defined in bookmark for URI '%s'"),
2446 return item->metadata->is_private;
2450 * g_bookmark_file_set_added:
2451 * @bookmark: a #GBookmarkFile
2453 * @added: a timestamp or -1 to use the current time
2455 * Sets the time the bookmark for @uri was added into @bookmark.
2457 * If no bookmark for @uri is found then it is created.
2462 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2468 g_return_if_fail (bookmark != NULL);
2469 g_return_if_fail (uri != NULL);
2471 item = g_bookmark_file_lookup_item (bookmark, uri);
2474 item = bookmark_item_new (uri);
2475 g_bookmark_file_add_item (bookmark, item, NULL);
2478 if (added == (time_t) -1)
2481 item->added = added;
2482 item->modified = added;
2486 * g_bookmark_file_get_added:
2487 * @bookmark: a #GBookmarkFile
2489 * @error: return location for a #GError, or %NULL
2491 * Gets the time the bookmark for @uri was added to @bookmark
2493 * In the event the URI cannot be found, -1 is returned and
2494 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2496 * Return value: a timestamp
2501 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2507 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2508 g_return_val_if_fail (uri != NULL, (time_t) -1);
2510 item = g_bookmark_file_lookup_item (bookmark, uri);
2513 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2514 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2515 _("No bookmark found for URI '%s'"),
2524 * g_bookmark_file_set_modified:
2525 * @bookmark: a #GBookmarkFile
2527 * @modified: a timestamp or -1 to use the current time
2529 * Sets the last time the bookmark for @uri was last modified.
2531 * If no bookmark for @uri is found then it is created.
2533 * The "modified" time should only be set when the bookmark's meta-data
2534 * was actually changed. Every function of #GBookmarkFile that
2535 * modifies a bookmark also changes the modification time, except for
2536 * g_bookmark_file_set_visited().
2541 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2547 g_return_if_fail (bookmark != NULL);
2548 g_return_if_fail (uri != NULL);
2550 item = g_bookmark_file_lookup_item (bookmark, uri);
2553 item = bookmark_item_new (uri);
2554 g_bookmark_file_add_item (bookmark, item, NULL);
2557 if (modified == (time_t) -1)
2560 item->modified = modified;
2564 * g_bookmark_file_get_modified:
2565 * @bookmark: a #GBookmarkFile
2567 * @error: return location for a #GError, or %NULL
2569 * Gets the time when the bookmark for @uri was last modified.
2571 * In the event the URI cannot be found, -1 is returned and
2572 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2574 * Return value: a timestamp
2579 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2585 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2586 g_return_val_if_fail (uri != NULL, (time_t) -1);
2588 item = g_bookmark_file_lookup_item (bookmark, uri);
2591 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2592 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2593 _("No bookmark found for URI '%s'"),
2598 return item->modified;
2602 * g_bookmark_file_set_visited:
2603 * @bookmark: a #GBookmarkFile
2605 * @visited: a timestamp or -1 to use the current time
2607 * Sets the time the bookmark for @uri was last visited.
2609 * If no bookmark for @uri is found then it is created.
2611 * The "visited" time should only be set if the bookmark was launched,
2612 * either using the command line retrieved by g_bookmark_file_get_app_info()
2613 * or by the default application for the bookmark's MIME type, retrieved
2614 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2615 * does not affect the "modified" time.
2620 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2626 g_return_if_fail (bookmark != NULL);
2627 g_return_if_fail (uri != NULL);
2629 item = g_bookmark_file_lookup_item (bookmark, uri);
2632 item = bookmark_item_new (uri);
2633 g_bookmark_file_add_item (bookmark, item, NULL);
2636 if (visited == (time_t) -1)
2639 item->visited = visited;
2643 * g_bookmark_file_get_visited:
2644 * @bookmark: a #GBookmarkFile
2646 * @error: return location for a #GError, or %NULL
2648 * Gets the time the bookmark for @uri was last visited.
2650 * In the event the URI cannot be found, -1 is returned and
2651 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2653 * Return value: a timestamp.
2658 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2664 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2665 g_return_val_if_fail (uri != NULL, (time_t) -1);
2667 item = g_bookmark_file_lookup_item (bookmark, uri);
2670 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2671 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2672 _("No bookmark found for URI '%s'"),
2677 return item->visited;
2681 * g_bookmark_file_has_group:
2682 * @bookmark: a #GBookmarkFile
2684 * @group: the group name to be searched
2685 * @error: return location for a #GError, or %NULL
2687 * Checks whether @group appears in the list of groups to which
2688 * the bookmark for @uri belongs to.
2690 * In the event the URI cannot be found, %FALSE is returned and
2691 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2693 * Return value: %TRUE if @group was found.
2698 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2706 g_return_val_if_fail (bookmark != NULL, FALSE);
2707 g_return_val_if_fail (uri != NULL, FALSE);
2709 item = g_bookmark_file_lookup_item (bookmark, uri);
2712 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2713 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2714 _("No bookmark found for URI '%s'"),
2719 if (!item->metadata)
2722 for (l = item->metadata->groups; l != NULL; l = l->next)
2724 if (strcmp (l->data, group) == 0)
2733 * g_bookmark_file_add_group:
2734 * @bookmark: a #GBookmarkFile
2736 * @group: the group name to be added
2738 * Adds @group to the list of groups to which the bookmark for @uri
2741 * If no bookmark for @uri is found then it is created.
2746 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2752 g_return_if_fail (bookmark != NULL);
2753 g_return_if_fail (uri != NULL);
2754 g_return_if_fail (group != NULL && group[0] != '\0');
2756 item = g_bookmark_file_lookup_item (bookmark, uri);
2759 item = bookmark_item_new (uri);
2760 g_bookmark_file_add_item (bookmark, item, NULL);
2763 if (!item->metadata)
2764 item->metadata = bookmark_metadata_new ();
2766 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2768 item->metadata->groups = g_list_prepend (item->metadata->groups,
2771 item->modified = time (NULL);
2776 * g_bookmark_file_remove_group:
2777 * @bookmark: a #GBookmarkFile
2779 * @group: the group name to be removed
2780 * @error: return location for a #GError, or %NULL
2782 * Removes @group from the list of groups to which the bookmark
2783 * for @uri belongs to.
2785 * In the event the URI cannot be found, %FALSE is returned and
2786 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2787 * In the event no group was defined, %FALSE is returned and
2788 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2790 * Return value: %TRUE if @group was successfully removed.
2795 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2803 g_return_val_if_fail (bookmark != NULL, FALSE);
2804 g_return_val_if_fail (uri != NULL, FALSE);
2806 item = g_bookmark_file_lookup_item (bookmark, uri);
2809 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2810 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2811 _("No bookmark found for URI '%s'"),
2816 if (!item->metadata)
2818 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2819 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2820 _("No groups set in bookmark for URI '%s'"),
2825 for (l = item->metadata->groups; l != NULL; l = l->next)
2827 if (strcmp (l->data, group) == 0)
2829 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2833 item->modified = time (NULL);
2843 * g_bookmark_file_set_groups:
2844 * @bookmark: a #GBookmarkFile
2845 * @uri: an item's URI
2846 * @groups: an array of group names, or %NULL to remove all groups
2847 * @length: number of group name values in @groups
2849 * Sets a list of group names for the item with URI @uri. Each previously
2850 * set group name list is removed.
2852 * If @uri cannot be found then an item for it is created.
2857 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2859 const gchar **groups,
2865 g_return_if_fail (bookmark != NULL);
2866 g_return_if_fail (uri != NULL);
2867 g_return_if_fail (groups != NULL);
2869 item = g_bookmark_file_lookup_item (bookmark, uri);
2872 item = bookmark_item_new (uri);
2873 g_bookmark_file_add_item (bookmark, item, NULL);
2876 if (!item->metadata)
2877 item->metadata = bookmark_metadata_new ();
2879 if (item->metadata->groups != NULL)
2881 g_list_foreach (item->metadata->groups,
2884 g_list_free (item->metadata->groups);
2885 item->metadata->groups = NULL;
2890 for (i = 0; groups[i] != NULL && i < length; i++)
2891 item->metadata->groups = g_list_append (item->metadata->groups,
2892 g_strdup (groups[i]));
2895 item->modified = time (NULL);
2899 * g_bookmark_file_get_groups:
2900 * @bookmark: a #GBookmarkFile
2902 * @length: return location for the length of the returned string, or %NULL
2903 * @error: return location for a #GError, or %NULL
2905 * Retrieves the list of group names of the bookmark for @uri.
2907 * In the event the URI cannot be found, %NULL is returned and
2908 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2910 * The returned array is %NULL terminated, so @length may optionally
2913 * Return value: a newly allocated %NULL-terminated array of group names.
2914 * Use g_strfreev() to free it.
2919 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2929 g_return_val_if_fail (bookmark != NULL, NULL);
2930 g_return_val_if_fail (uri != NULL, NULL);
2932 item = g_bookmark_file_lookup_item (bookmark, uri);
2935 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2936 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2937 _("No bookmark found for URI '%s'"),
2942 if (!item->metadata)
2950 len = g_list_length (item->metadata->groups);
2951 retval = g_new0 (gchar *, len + 1);
2952 for (l = g_list_last (item->metadata->groups), i = 0;
2956 gchar *group_name = (gchar *) l->data;
2958 g_warn_if_fail (group_name != NULL);
2960 retval[i++] = g_strdup (group_name);
2971 * g_bookmark_file_add_application:
2972 * @bookmark: a #GBookmarkFile
2974 * @name: the name of the application registering the bookmark
2976 * @exec: command line to be used to launch the bookmark or %NULL
2978 * Adds the application with @name and @exec to the list of
2979 * applications that have registered a bookmark for @uri into
2982 * Every bookmark inside a #GBookmarkFile must have at least an
2983 * application registered. Each application must provide a name, a
2984 * command line useful for launching the bookmark, the number of times
2985 * the bookmark has been registered by the application and the last
2986 * time the application registered this bookmark.
2988 * If @name is %NULL, the name of the application will be the
2989 * same returned by g_get_application(); if @exec is %NULL, the
2990 * command line will be a composition of the program name as
2991 * returned by g_get_prgname() and the "%u" modifier, which will be
2992 * expanded to the bookmark's URI.
2994 * This function will automatically take care of updating the
2995 * registrations count and timestamping in case an application
2996 * with the same @name had already registered a bookmark for
2997 * @uri inside @bookmark.
2999 * If no bookmark for @uri is found, one is created.
3004 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3010 gchar *app_name, *app_exec;
3012 g_return_if_fail (bookmark != NULL);
3013 g_return_if_fail (uri != NULL);
3015 item = g_bookmark_file_lookup_item (bookmark, uri);
3018 item = bookmark_item_new (uri);
3019 g_bookmark_file_add_item (bookmark, item, NULL);
3022 if (name && name[0] != '\0')
3023 app_name = g_strdup (name);
3025 app_name = g_strdup (g_get_application_name ());
3027 if (exec && exec[0] != '\0')
3028 app_exec = g_strdup (exec);
3030 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3032 g_bookmark_file_set_app_info (bookmark, uri,
3044 * g_bookmark_file_remove_application:
3045 * @bookmark: a #GBookmarkFile
3047 * @name: the name of the application
3048 * @error: return location for a #GError or %NULL
3050 * Removes application registered with @name from the list of applications
3051 * that have registered a bookmark for @uri inside @bookmark.
3053 * In the event the URI cannot be found, %FALSE is returned and
3054 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3055 * In the event that no application with name @app_name has registered
3056 * a bookmark for @uri, %FALSE is returned and error is set to
3057 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3059 * Return value: %TRUE if the application was successfully removed.
3064 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3072 g_return_val_if_fail (bookmark != NULL, FALSE);
3073 g_return_val_if_fail (uri != NULL, FALSE);
3074 g_return_val_if_fail (name != NULL, FALSE);
3077 retval = g_bookmark_file_set_app_info (bookmark, uri,
3085 g_propagate_error (error, set_error);
3094 * g_bookmark_file_has_application:
3095 * @bookmark: a #GBookmarkFile
3097 * @name: the name of the application
3098 * @error: return location for a #GError or %NULL
3100 * Checks whether the bookmark for @uri inside @bookmark has been
3101 * registered by application @name.
3103 * In the event the URI cannot be found, %FALSE is returned and
3104 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3106 * Return value: %TRUE if the application @name was found
3111 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3118 g_return_val_if_fail (bookmark != NULL, FALSE);
3119 g_return_val_if_fail (uri != NULL, FALSE);
3120 g_return_val_if_fail (name != NULL, FALSE);
3122 item = g_bookmark_file_lookup_item (bookmark, uri);
3125 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3126 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3127 _("No bookmark found for URI '%s'"),
3132 return (NULL != bookmark_item_lookup_app_info (item, name));
3136 * g_bookmark_file_set_app_info:
3137 * @bookmark: a #GBookmarkFile
3139 * @name: an application's name
3140 * @exec: an application's command line
3141 * @count: the number of registrations done for this application
3142 * @stamp: the time of the last registration for this application
3143 * @error: return location for a #GError or %NULL
3145 * Sets the meta-data of application @name inside the list of
3146 * applications that have registered a bookmark for @uri inside
3149 * You should rarely use this function; use g_bookmark_file_add_application()
3150 * and g_bookmark_file_remove_application() instead.
3152 * @name can be any UTF-8 encoded string used to identify an
3154 * @exec can have one of these two modifiers: "%f", which will
3155 * be expanded as the local file name retrieved from the bookmark's
3156 * URI; "%u", which will be expanded as the bookmark's URI.
3157 * The expansion is done automatically when retrieving the stored
3158 * command line using the g_bookmark_file_get_app_info() function.
3159 * @count is the number of times the application has registered the
3160 * bookmark; if is < 0, the current registration count will be increased
3161 * by one, if is 0, the application with @name will be removed from
3162 * the list of registered applications.
3163 * @stamp is the Unix time of the last registration; if it is -1, the
3164 * current time will be used.
3166 * If you try to remove an application by setting its registration count to
3167 * zero, and no bookmark for @uri is found, %FALSE is returned and
3168 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3169 * in the event that no application @name has registered a bookmark
3170 * for @uri, %FALSE is returned and error is set to
3171 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3172 * for @uri is found, one is created.
3174 * Return value: %TRUE if the application's meta-data was successfully
3180 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3189 BookmarkAppInfo *ai;
3191 g_return_val_if_fail (bookmark != NULL, FALSE);
3192 g_return_val_if_fail (uri != NULL, FALSE);
3193 g_return_val_if_fail (name != NULL, FALSE);
3194 g_return_val_if_fail (exec != NULL, FALSE);
3196 item = g_bookmark_file_lookup_item (bookmark, uri);
3201 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3202 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3203 _("No bookmark found for URI '%s'"),
3209 item = bookmark_item_new (uri);
3210 g_bookmark_file_add_item (bookmark, item, NULL);
3214 ai = bookmark_item_lookup_app_info (item, name);
3219 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3220 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3221 _("No application with name '%s' registered a bookmark for '%s'"),
3228 ai = bookmark_app_info_new (name);
3230 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3231 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3237 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3238 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3239 bookmark_app_info_free (ai);
3241 item->modified = time (NULL);
3250 if (stamp != (time_t) -1)
3253 ai->stamp = time (NULL);
3255 if (exec && exec[0] != '\0')
3258 ai->exec = g_shell_quote (exec);
3261 item->modified = time (NULL);
3266 /* expands the application's command line */
3268 expand_exec_line (const gchar *exec_fmt,
3274 exec = g_string_new (NULL);
3275 while ((ch = *exec_fmt++) != '\0')
3279 exec = g_string_append_c (exec, ch);
3290 g_string_append (exec, uri);
3295 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3298 g_string_append (exec, file);
3303 g_string_free (exec, TRUE);
3310 exec = g_string_append_c (exec, ch);
3316 return g_string_free (exec, FALSE);
3320 * g_bookmark_file_get_app_info:
3321 * @bookmark: a #GBookmarkFile
3323 * @name: an application's name
3324 * @exec: location for the command line of the application, or %NULL
3325 * @count: return location for the registration count, or %NULL
3326 * @stamp: return location for the last registration time, or %NULL
3327 * @error: return location for a #GError, or %NULL
3329 * Gets the registration informations of @app_name for the bookmark for
3330 * @uri. See g_bookmark_file_set_app_info() for more informations about
3331 * the returned data.
3333 * The string returned in @app_exec must be freed.
3335 * In the event the URI cannot be found, %FALSE is returned and
3336 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3337 * event that no application with name @app_name has registered a bookmark
3338 * for @uri, %FALSE is returned and error is set to
3339 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3340 * the command line fails, an error of the #G_SHELL_ERROR domain is
3341 * set and %FALSE is returned.
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 GError *unquote_error = NULL;
3387 gchar *command_line;
3389 command_line = g_shell_unquote (ai->exec, &unquote_error);
3392 g_propagate_error (error, unquote_error);
3396 *exec = expand_exec_line (command_line, uri);
3399 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3400 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3401 _("Failed to expand exec line '%s' with URI '%s'"),
3403 g_free (command_line);
3408 g_free (command_line);
3421 * g_bookmark_file_get_applications:
3422 * @bookmark: a #GBookmarkFile
3424 * @length: return location of the length of the returned list, or %NULL
3425 * @error: return location for a #GError, or %NULL
3427 * Retrieves the names of the applications that have registered the
3428 * bookmark for @uri.
3430 * In the event the URI cannot be found, %NULL is returned and
3431 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3433 * Return value: a newly allocated %NULL-terminated array of strings.
3434 * Use g_strfreev() to free it.
3439 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3449 g_return_val_if_fail (bookmark != NULL, NULL);
3450 g_return_val_if_fail (uri != NULL, NULL);
3452 item = g_bookmark_file_lookup_item (bookmark, uri);
3455 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3456 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3457 _("No bookmark found for URI '%s'"),
3462 if (!item->metadata)
3470 n_apps = g_list_length (item->metadata->applications);
3471 apps = g_new0 (gchar *, n_apps + 1);
3473 for (l = g_list_last (item->metadata->applications), i = 0;
3477 BookmarkAppInfo *ai;
3479 ai = (BookmarkAppInfo *) l->data;
3481 g_warn_if_fail (ai != NULL);
3482 g_warn_if_fail (ai->name != NULL);
3484 apps[i++] = g_strdup (ai->name);
3495 * g_bookmark_file_get_size:
3496 * @bookmark: a #GBookmarkFile
3498 * Gets the number of bookmarks inside @bookmark.
3500 * Return value: the number of bookmarks
3505 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3507 g_return_val_if_fail (bookmark != NULL, 0);
3509 return g_list_length (bookmark->items);
3513 * g_bookmark_file_move_item:
3514 * @bookmark: a #GBookmarkFile
3515 * @old_uri: a valid URI
3516 * @new_uri: a valid URI, or %NULL
3517 * @error: return location for a #GError or %NULL
3519 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3520 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3521 * %NULL, then the bookmark is removed.
3523 * In the event the URI cannot be found, %FALSE is returned and
3524 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3526 * Return value: %TRUE if the URI was successfully changed
3531 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3532 const gchar *old_uri,
3533 const gchar *new_uri,
3537 GError *remove_error;
3539 g_return_val_if_fail (bookmark != NULL, FALSE);
3540 g_return_val_if_fail (old_uri != NULL, FALSE);
3542 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3545 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3546 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3547 _("No bookmark found for URI '%s'"),
3552 if (new_uri && new_uri[0] != '\0')
3554 if (g_bookmark_file_has_item (bookmark, new_uri))
3556 remove_error = NULL;
3557 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3560 g_propagate_error (error, remove_error);
3566 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3569 item->uri = g_strdup (new_uri);
3570 item->modified = time (NULL);
3572 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3578 remove_error = NULL;
3579 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3582 g_propagate_error (error, remove_error);
3592 * g_bookmark_file_set_icon:
3593 * @bookmark: a #GBookmarkFile
3595 * @href: the URI of the icon for the bookmark, or %NULL
3596 * @mime_type: the MIME type of the icon for the bookmark
3598 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3599 * the currently set icon.
3601 * If no bookmark for @uri is found it is created.
3606 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3609 const gchar *mime_type)
3613 g_return_if_fail (bookmark != NULL);
3614 g_return_if_fail (uri != NULL);
3616 item = g_bookmark_file_lookup_item (bookmark, uri);
3619 item = bookmark_item_new (uri);
3620 g_bookmark_file_add_item (bookmark, item, NULL);
3623 if (!item->metadata)
3624 item->metadata = bookmark_metadata_new ();
3626 g_free (item->metadata->icon_href);
3627 g_free (item->metadata->icon_mime);
3629 item->metadata->icon_href = g_strdup (href);
3631 if (mime_type && mime_type[0] != '\0')
3632 item->metadata->icon_mime = g_strdup (mime_type);
3634 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3636 item->modified = time (NULL);
3640 * g_bookmark_file_get_icon:
3641 * @bookmark: a #GBookmarkFile
3643 * @href: return location for the icon's location or %NULL
3644 * @mime_type: return location for the icon's MIME type or %NULL
3645 * @error: return location for a #GError or %NULL
3647 * Gets the icon of the bookmark for @uri.
3649 * In the event the URI cannot be found, %FALSE is returned and
3650 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3652 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3653 * You should free the returned strings.
3658 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3666 g_return_val_if_fail (bookmark != NULL, FALSE);
3667 g_return_val_if_fail (uri != NULL, FALSE);
3669 item = g_bookmark_file_lookup_item (bookmark, uri);
3672 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3673 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3674 _("No bookmark found for URI '%s'"),
3679 if ((!item->metadata) || (!item->metadata->icon_href))
3683 *href = g_strdup (item->metadata->icon_href);
3686 *mime_type = g_strdup (item->metadata->icon_mime);
3691 #define __G_BOOKMARK_FILE_C__
3692 #include "galiasdef.c"