contenttype: Improve test coverage
[glib.git] / glib / gbookmarkfile.c
blobedf196bde2ab6018840533a3687401c4574ccdee
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,
20 #include "config.h"
22 #include "gbookmarkfile.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <locale.h>
30 #include <time.h>
31 #include <stdarg.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #include "gconvert.h"
39 #include "gdataset.h"
40 #include "gerror.h"
41 #include "gfileutils.h"
42 #include "ghash.h"
43 #include "glibintl.h"
44 #include "glist.h"
45 #include "gslist.h"
46 #include "gmain.h"
47 #include "gmarkup.h"
48 #include "gmem.h"
49 #include "gmessages.h"
50 #include "gshell.h"
51 #include "gslice.h"
52 #include "gstdio.h"
53 #include "gstring.h"
54 #include "gstrfuncs.h"
55 #include "gtimer.h"
56 #include "gutils.h"
59 /**
60 * SECTION:bookmarkfile
61 * @title: Bookmark file parser
62 * @short_description: parses files containing bookmarks
64 * GBookmarkFile lets you parse, edit or create files containing bookmarks
65 * to URI, along with some meta-data about the resource pointed by the URI
66 * like its MIME type, the application that is registering the bookmark and
67 * the icon that should be used to represent the bookmark. The data is stored
68 * using the
69 * <ulink url="http://www.gnome.org/~ebassi/bookmark-spec">Desktop Bookmark
70 * Specification</ulink>.
72 * The syntax of the bookmark files is described in detail inside the Desktop
73 * Bookmark Specification, here is a quick summary: bookmark files use a
74 * sub-class of the <ulink url="">XML Bookmark Exchange Language</ulink>
75 * specification, consisting of valid UTF-8 encoded XML, under the
76 * <literal>xbel</literal> root element; each bookmark is stored inside a
77 * <literal>bookmark</literal> element, using its URI: no relative paths can
78 * be used inside a bookmark file. The bookmark may have a user defined title
79 * and description, to be used instead of the URI. Under the
80 * <literal>metadata</literal> element, with its <literal>owner</literal>
81 * attribute set to <literal>http://freedesktop.org</literal>, is stored the
82 * meta-data about a resource pointed by its URI. The meta-data consists of
83 * the resource's MIME type; the applications that have registered a bookmark;
84 * the groups to which a bookmark belongs to; a visibility flag, used to set
85 * the bookmark as "private" to the applications and groups that has it
86 * registered; the URI and MIME type of an icon, to be used when displaying
87 * the bookmark inside a GUI.
88 * |[<xi:include xmlns:xi="http://www.w3.org/2001/XInclude" parse="text" href="../../../../glib/tests/bookmarks.xbel"><xi:fallback>FIXME: MISSING XINCLUDE CONTENT</xi:fallback></xi:include>]|
90 * A bookmark file might contain more than one bookmark; each bookmark
91 * is accessed through its URI.
93 * The important caveat of bookmark files is that when you add a new
94 * bookmark you must also add the application that is registering it, using
95 * g_bookmark_file_add_application() or g_bookmark_file_set_app_info().
96 * If a bookmark has no applications then it won't be dumped when creating
97 * the on disk representation, using g_bookmark_file_to_data() or
98 * g_bookmark_file_to_file().
100 * The #GBookmarkFile parser was added in GLib 2.12.
103 /* XBEL 1.0 standard entities */
104 #define XBEL_VERSION "1.0"
105 #define XBEL_DTD_NICK "xbel"
106 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
107 "Exchange Language 1.0//EN//XML"
109 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
111 #define XBEL_ROOT_ELEMENT "xbel"
112 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
113 #define XBEL_BOOKMARK_ELEMENT "bookmark"
114 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
115 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
116 #define XBEL_TITLE_ELEMENT "title"
117 #define XBEL_DESC_ELEMENT "desc"
118 #define XBEL_INFO_ELEMENT "info"
119 #define XBEL_METADATA_ELEMENT "metadata"
121 #define XBEL_VERSION_ATTRIBUTE "version"
122 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
123 #define XBEL_OWNER_ATTRIBUTE "owner"
124 #define XBEL_ADDED_ATTRIBUTE "added"
125 #define XBEL_VISITED_ATTRIBUTE "visited"
126 #define XBEL_MODIFIED_ATTRIBUTE "modified"
127 #define XBEL_ID_ATTRIBUTE "id"
128 #define XBEL_HREF_ATTRIBUTE "href"
129 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
131 #define XBEL_YES_VALUE "yes"
132 #define XBEL_NO_VALUE "no"
134 /* Desktop bookmark spec entities */
135 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
137 #define BOOKMARK_NAMESPACE_NAME "bookmark"
138 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
140 #define BOOKMARK_GROUPS_ELEMENT "groups"
141 #define BOOKMARK_GROUP_ELEMENT "group"
142 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
143 #define BOOKMARK_APPLICATION_ELEMENT "application"
144 #define BOOKMARK_ICON_ELEMENT "icon"
145 #define BOOKMARK_PRIVATE_ELEMENT "private"
147 #define BOOKMARK_NAME_ATTRIBUTE "name"
148 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
149 #define BOOKMARK_COUNT_ATTRIBUTE "count"
150 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp" /* deprecated by "modified" */
151 #define BOOKMARK_MODIFIED_ATTRIBUTE "modified"
152 #define BOOKMARK_HREF_ATTRIBUTE "href"
153 #define BOOKMARK_TYPE_ATTRIBUTE "type"
155 /* Shared MIME Info entities */
156 #define MIME_NAMESPACE_NAME "mime"
157 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
158 #define MIME_TYPE_ELEMENT "mime-type"
159 #define MIME_TYPE_ATTRIBUTE "type"
162 typedef struct _BookmarkAppInfo BookmarkAppInfo;
163 typedef struct _BookmarkMetadata BookmarkMetadata;
164 typedef struct _BookmarkItem BookmarkItem;
165 typedef struct _ParseData ParseData;
167 struct _BookmarkAppInfo
169 gchar *name;
170 gchar *exec;
172 guint count;
174 time_t stamp;
177 struct _BookmarkMetadata
179 gchar *mime_type;
181 GList *groups;
183 GList *applications;
184 GHashTable *apps_by_name;
186 gchar *icon_href;
187 gchar *icon_mime;
189 guint is_private : 1;
192 struct _BookmarkItem
194 gchar *uri;
196 gchar *title;
197 gchar *description;
199 time_t added;
200 time_t modified;
201 time_t visited;
203 BookmarkMetadata *metadata;
206 struct _GBookmarkFile
208 gchar *title;
209 gchar *description;
211 /* we store our items in a list and keep a copy inside
212 * an hash table for faster lookup performances
214 GList *items;
215 GHashTable *items_by_uri;
218 /* parser state machine */
219 enum
221 STATE_STARTED = 0,
223 STATE_ROOT,
224 STATE_BOOKMARK,
225 STATE_TITLE,
226 STATE_DESC,
227 STATE_INFO,
228 STATE_METADATA,
229 STATE_APPLICATIONS,
230 STATE_APPLICATION,
231 STATE_GROUPS,
232 STATE_GROUP,
233 STATE_MIME,
234 STATE_ICON,
236 STATE_FINISHED
239 static void g_bookmark_file_init (GBookmarkFile *bookmark);
240 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
241 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
242 const gchar *buffer,
243 gsize length,
244 GError **error);
245 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
246 gsize *length,
247 GError **error);
248 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
249 const gchar *uri);
250 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
251 BookmarkItem *item,
252 GError **error);
254 static time_t timestamp_from_iso8601 (const gchar *iso_date);
255 static gchar * timestamp_to_iso8601 (time_t timestamp);
257 /********************************
258 * BookmarkAppInfo *
260 * Application metadata storage *
261 ********************************/
262 static BookmarkAppInfo *
263 bookmark_app_info_new (const gchar *name)
265 BookmarkAppInfo *retval;
267 g_warn_if_fail (name != NULL);
269 retval = g_slice_new (BookmarkAppInfo);
271 retval->name = g_strdup (name);
272 retval->exec = NULL;
273 retval->count = 0;
274 retval->stamp = 0;
276 return retval;
279 static void
280 bookmark_app_info_free (BookmarkAppInfo *app_info)
282 if (!app_info)
283 return;
285 g_free (app_info->name);
286 g_free (app_info->exec);
288 g_slice_free (BookmarkAppInfo, app_info);
291 static gchar *
292 bookmark_app_info_dump (BookmarkAppInfo *app_info)
294 gchar *retval;
295 gchar *name, *exec, *modified, *count;
297 g_warn_if_fail (app_info != NULL);
299 if (app_info->count == 0)
300 return NULL;
302 name = g_markup_escape_text (app_info->name, -1);
303 exec = g_markup_escape_text (app_info->exec, -1);
304 modified = timestamp_to_iso8601 (app_info->stamp);
305 count = g_strdup_printf ("%u", app_info->count);
307 retval = g_strconcat (" "
308 "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
309 " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
310 " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\""
311 " " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", modified, "\""
312 " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
313 NULL);
315 g_free (name);
316 g_free (exec);
317 g_free (modified);
318 g_free (count);
320 return retval;
324 /***********************
325 * BookmarkMetadata *
327 * Metadata storage *
328 ***********************/
329 static BookmarkMetadata *
330 bookmark_metadata_new (void)
332 BookmarkMetadata *retval;
334 retval = g_slice_new (BookmarkMetadata);
336 retval->mime_type = NULL;
338 retval->groups = NULL;
340 retval->applications = NULL;
341 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
342 g_str_equal,
343 NULL,
344 NULL);
346 retval->is_private = FALSE;
348 retval->icon_href = NULL;
349 retval->icon_mime = NULL;
351 return retval;
354 static void
355 bookmark_metadata_free (BookmarkMetadata *metadata)
357 if (!metadata)
358 return;
360 g_free (metadata->mime_type);
362 g_list_free_full (metadata->groups, g_free);
363 g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
365 g_hash_table_destroy (metadata->apps_by_name);
367 g_free (metadata->icon_href);
368 g_free (metadata->icon_mime);
370 g_slice_free (BookmarkMetadata, metadata);
373 static gchar *
374 bookmark_metadata_dump (BookmarkMetadata *metadata)
376 GString *retval;
377 gchar *buffer;
379 if (!metadata->applications)
380 return NULL;
382 retval = g_string_sized_new (1024);
384 /* metadata container */
385 g_string_append (retval,
387 "<" XBEL_METADATA_ELEMENT
388 " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
389 "\">\n");
391 /* mime type */
392 if (metadata->mime_type) {
393 buffer = g_strconcat (" "
394 "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
395 MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
396 NULL);
397 g_string_append (retval, buffer);
398 g_free (buffer);
401 if (metadata->groups)
403 GList *l;
405 /* open groups container */
406 g_string_append (retval,
408 "<" BOOKMARK_NAMESPACE_NAME
409 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
411 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
413 gchar *group_name;
415 group_name = g_markup_escape_text ((gchar *) l->data, -1);
416 buffer = g_strconcat (" "
417 "<" BOOKMARK_NAMESPACE_NAME
418 ":" BOOKMARK_GROUP_ELEMENT ">",
419 group_name,
420 "</" BOOKMARK_NAMESPACE_NAME
421 ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
422 g_string_append (retval, buffer);
424 g_free (buffer);
425 g_free (group_name);
428 /* close groups container */
429 g_string_append (retval,
431 "</" BOOKMARK_NAMESPACE_NAME
432 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
435 if (metadata->applications)
437 GList *l;
439 /* open applications container */
440 g_string_append (retval,
442 "<" BOOKMARK_NAMESPACE_NAME
443 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
445 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
447 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
448 gchar *app_data;
450 g_warn_if_fail (app_info != NULL);
452 app_data = bookmark_app_info_dump (app_info);
454 if (app_data)
456 retval = g_string_append (retval, app_data);
458 g_free (app_data);
462 /* close applications container */
463 g_string_append (retval,
465 "</" BOOKMARK_NAMESPACE_NAME
466 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
469 /* icon */
470 if (metadata->icon_href)
472 if (!metadata->icon_mime)
473 metadata->icon_mime = g_strdup ("application/octet-stream");
475 buffer = g_strconcat (" "
476 "<" BOOKMARK_NAMESPACE_NAME
477 ":" BOOKMARK_ICON_ELEMENT
478 " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
479 "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
480 g_string_append (retval, buffer);
482 g_free (buffer);
485 /* private hint */
486 if (metadata->is_private)
487 g_string_append (retval,
489 "<" BOOKMARK_NAMESPACE_NAME
490 ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
492 /* close metadata container */
493 g_string_append (retval,
495 "</" XBEL_METADATA_ELEMENT ">\n");
497 return g_string_free (retval, FALSE);
500 /******************************************************
501 * BookmarkItem *
503 * Storage for a single bookmark item inside the list *
504 ******************************************************/
505 static BookmarkItem *
506 bookmark_item_new (const gchar *uri)
508 BookmarkItem *item;
510 g_warn_if_fail (uri != NULL);
512 item = g_slice_new (BookmarkItem);
513 item->uri = g_strdup (uri);
515 item->title = NULL;
516 item->description = NULL;
518 item->added = (time_t) -1;
519 item->modified = (time_t) -1;
520 item->visited = (time_t) -1;
522 item->metadata = NULL;
524 return item;
527 static void
528 bookmark_item_free (BookmarkItem *item)
530 if (!item)
531 return;
533 g_free (item->uri);
534 g_free (item->title);
535 g_free (item->description);
537 if (item->metadata)
538 bookmark_metadata_free (item->metadata);
540 g_slice_free (BookmarkItem, item);
543 static gchar *
544 bookmark_item_dump (BookmarkItem *item)
546 GString *retval;
547 gchar *added, *visited, *modified;
548 gchar *escaped_uri;
549 gchar *buffer;
551 /* at this point, we must have at least a registered application; if we don't
552 * we don't screw up the bookmark file, and just skip this item
554 if (!item->metadata || !item->metadata->applications)
556 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
557 return NULL;
560 retval = g_string_sized_new (4096);
562 added = timestamp_to_iso8601 (item->added);
563 modified = timestamp_to_iso8601 (item->modified);
564 visited = timestamp_to_iso8601 (item->visited);
566 escaped_uri = g_markup_escape_text (item->uri, -1);
568 buffer = g_strconcat (" <"
569 XBEL_BOOKMARK_ELEMENT
571 XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
572 XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
573 XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
574 XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
575 NULL);
577 g_string_append (retval, buffer);
579 g_free (escaped_uri);
580 g_free (visited);
581 g_free (modified);
582 g_free (added);
583 g_free (buffer);
585 if (item->title)
587 gchar *escaped_title;
589 escaped_title = g_markup_escape_text (item->title, -1);
590 buffer = g_strconcat (" "
591 "<" XBEL_TITLE_ELEMENT ">",
592 escaped_title,
593 "</" XBEL_TITLE_ELEMENT ">\n",
594 NULL);
595 g_string_append (retval, buffer);
597 g_free (escaped_title);
598 g_free (buffer);
601 if (item->description)
603 gchar *escaped_desc;
605 escaped_desc = g_markup_escape_text (item->description, -1);
606 buffer = g_strconcat (" "
607 "<" XBEL_DESC_ELEMENT ">",
608 escaped_desc,
609 "</" XBEL_DESC_ELEMENT ">\n",
610 NULL);
611 g_string_append (retval, buffer);
613 g_free (escaped_desc);
614 g_free (buffer);
617 if (item->metadata)
619 gchar *metadata;
621 metadata = bookmark_metadata_dump (item->metadata);
622 if (metadata)
624 buffer = g_strconcat (" "
625 "<" XBEL_INFO_ELEMENT ">\n",
626 metadata,
628 "</" XBEL_INFO_ELEMENT ">\n",
629 NULL);
630 retval = g_string_append (retval, buffer);
632 g_free (buffer);
633 g_free (metadata);
637 g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
639 return g_string_free (retval, FALSE);
642 static BookmarkAppInfo *
643 bookmark_item_lookup_app_info (BookmarkItem *item,
644 const gchar *app_name)
646 g_warn_if_fail (item != NULL && app_name != NULL);
648 if (!item->metadata)
649 return NULL;
651 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
654 /*************************
655 * GBookmarkFile *
656 *************************/
658 static void
659 g_bookmark_file_init (GBookmarkFile *bookmark)
661 bookmark->title = NULL;
662 bookmark->description = NULL;
664 bookmark->items = NULL;
665 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
666 g_str_equal,
667 NULL,
668 NULL);
671 static void
672 g_bookmark_file_clear (GBookmarkFile *bookmark)
674 g_free (bookmark->title);
675 g_free (bookmark->description);
677 g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
678 bookmark->items = NULL;
680 if (bookmark->items_by_uri)
682 g_hash_table_destroy (bookmark->items_by_uri);
684 bookmark->items_by_uri = NULL;
688 struct _ParseData
690 gint state;
692 GHashTable *namespaces;
694 GBookmarkFile *bookmark_file;
695 BookmarkItem *current_item;
698 static ParseData *
699 parse_data_new (void)
701 ParseData *retval;
703 retval = g_new (ParseData, 1);
705 retval->state = STATE_STARTED;
706 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
707 (GDestroyNotify) g_free,
708 (GDestroyNotify) g_free);
709 retval->bookmark_file = NULL;
710 retval->current_item = NULL;
712 return retval;
715 static void
716 parse_data_free (ParseData *parse_data)
718 g_hash_table_destroy (parse_data->namespaces);
720 g_free (parse_data);
723 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
725 static void
726 parse_bookmark_element (GMarkupParseContext *context,
727 ParseData *parse_data,
728 const gchar **attribute_names,
729 const gchar **attribute_values,
730 GError **error)
732 const gchar *uri, *added, *modified, *visited;
733 const gchar *attr;
734 gint i;
735 BookmarkItem *item;
736 GError *add_error;
738 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
740 i = 0;
741 uri = added = modified = visited = NULL;
742 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
744 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
745 uri = attribute_values[i];
746 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
747 added = attribute_values[i];
748 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
749 modified = attribute_values[i];
750 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
751 visited = attribute_values[i];
752 else
754 /* bookmark is defined by the XBEL spec, so we need
755 * to error out if the element has different or
756 * missing attributes
758 g_set_error (error, G_MARKUP_ERROR,
759 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
760 _("Unexpected attribute '%s' for element '%s'"),
761 attr,
762 XBEL_BOOKMARK_ELEMENT);
763 return;
767 if (!uri)
769 g_set_error (error, G_MARKUP_ERROR,
770 G_MARKUP_ERROR_INVALID_CONTENT,
771 _("Attribute '%s' of element '%s' not found"),
772 XBEL_HREF_ATTRIBUTE,
773 XBEL_BOOKMARK_ELEMENT);
774 return;
777 g_warn_if_fail (parse_data->current_item == NULL);
779 item = bookmark_item_new (uri);
781 if (added)
782 item->added = timestamp_from_iso8601 (added);
784 if (modified)
785 item->modified = timestamp_from_iso8601 (modified);
787 if (visited)
788 item->visited = timestamp_from_iso8601 (visited);
790 add_error = NULL;
791 g_bookmark_file_add_item (parse_data->bookmark_file,
792 item,
793 &add_error);
794 if (add_error)
796 bookmark_item_free (item);
798 g_propagate_error (error, add_error);
800 return;
803 parse_data->current_item = item;
806 static void
807 parse_application_element (GMarkupParseContext *context,
808 ParseData *parse_data,
809 const gchar **attribute_names,
810 const gchar **attribute_values,
811 GError **error)
813 const gchar *name, *exec, *count, *stamp, *modified;
814 const gchar *attr;
815 gint i;
816 BookmarkItem *item;
817 BookmarkAppInfo *ai;
819 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
821 i = 0;
822 name = exec = count = stamp = modified = NULL;
823 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
825 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
826 name = attribute_values[i];
827 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
828 exec = attribute_values[i];
829 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
830 count = attribute_values[i];
831 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
832 stamp = attribute_values[i];
833 else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
834 modified = attribute_values[i];
837 /* the "name" and "exec" attributes are mandatory */
838 if (!name)
840 g_set_error (error, G_MARKUP_ERROR,
841 G_MARKUP_ERROR_INVALID_CONTENT,
842 _("Attribute '%s' of element '%s' not found"),
843 BOOKMARK_NAME_ATTRIBUTE,
844 BOOKMARK_APPLICATION_ELEMENT);
845 return;
848 if (!exec)
850 g_set_error (error, G_MARKUP_ERROR,
851 G_MARKUP_ERROR_INVALID_CONTENT,
852 _("Attribute '%s' of element '%s' not found"),
853 BOOKMARK_EXEC_ATTRIBUTE,
854 BOOKMARK_APPLICATION_ELEMENT);
855 return;
858 g_warn_if_fail (parse_data->current_item != NULL);
859 item = parse_data->current_item;
861 ai = bookmark_item_lookup_app_info (item, name);
862 if (!ai)
864 ai = bookmark_app_info_new (name);
866 if (!item->metadata)
867 item->metadata = bookmark_metadata_new ();
869 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
870 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
873 ai->exec = g_strdup (exec);
875 if (count)
876 ai->count = atoi (count);
877 else
878 ai->count = 1;
880 if (modified)
881 ai->stamp = timestamp_from_iso8601 (modified);
882 else
884 /* the timestamp attribute has been deprecated but we still parse
885 * it for backward compatibility
887 if (stamp)
888 ai->stamp = (time_t) atol (stamp);
889 else
890 ai->stamp = time (NULL);
894 static void
895 parse_mime_type_element (GMarkupParseContext *context,
896 ParseData *parse_data,
897 const gchar **attribute_names,
898 const gchar **attribute_values,
899 GError **error)
901 const gchar *type;
902 const gchar *attr;
903 gint i;
904 BookmarkItem *item;
906 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
908 i = 0;
909 type = NULL;
910 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
912 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
913 type = attribute_values[i];
916 if (!type)
917 type = "application/octet-stream";
919 g_warn_if_fail (parse_data->current_item != NULL);
920 item = parse_data->current_item;
922 if (!item->metadata)
923 item->metadata = bookmark_metadata_new ();
925 item->metadata->mime_type = g_strdup (type);
928 static void
929 parse_icon_element (GMarkupParseContext *context,
930 ParseData *parse_data,
931 const gchar **attribute_names,
932 const gchar **attribute_values,
933 GError **error)
935 const gchar *href;
936 const gchar *type;
937 const gchar *attr;
938 gint i;
939 BookmarkItem *item;
941 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
943 i = 0;
944 href = NULL;
945 type = NULL;
946 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
948 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
949 href = attribute_values[i];
950 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
951 type = attribute_values[i];
954 /* the "href" attribute is mandatory */
955 if (!href)
957 g_set_error (error, G_MARKUP_ERROR,
958 G_MARKUP_ERROR_INVALID_CONTENT,
959 _("Attribute '%s' of element '%s' not found"),
960 BOOKMARK_HREF_ATTRIBUTE,
961 BOOKMARK_ICON_ELEMENT);
962 return;
965 if (!type)
966 type = "application/octet-stream";
968 g_warn_if_fail (parse_data->current_item != NULL);
969 item = parse_data->current_item;
971 if (!item->metadata)
972 item->metadata = bookmark_metadata_new ();
974 item->metadata->icon_href = g_strdup (href);
975 item->metadata->icon_mime = g_strdup (type);
978 /* scans through the attributes of an element for the "xmlns" pragma, and
979 * adds any resulting namespace declaration to a per-parser hashtable, using
980 * the namespace name as a key for the namespace URI; if no key was found,
981 * the namespace is considered as default, and stored under the "default" key.
983 * FIXME: this works on the assumption that the generator of the XBEL file
984 * is either this code or is smart enough to place the namespace declarations
985 * inside the main root node or inside the metadata node and does not redefine
986 * a namespace inside an inner node; this does *not* conform to the
987 * XML-NS standard, although is a close approximation. In order to make this
988 * conformant to the XML-NS specification we should use a per-element
989 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
991 static void
992 map_namespace_to_name (ParseData *parse_data,
993 const gchar **attribute_names,
994 const gchar **attribute_values)
996 const gchar *attr;
997 gint i;
999 g_warn_if_fail (parse_data != NULL);
1001 if (!attribute_names || !attribute_names[0])
1002 return;
1004 i = 0;
1005 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1007 if (g_str_has_prefix (attr, "xmlns"))
1009 gchar *namespace_name, *namespace_uri;
1010 gchar *p;
1012 p = g_utf8_strchr (attr, -1, ':');
1013 if (p)
1014 p = g_utf8_next_char (p);
1015 else
1016 p = "default";
1018 namespace_name = g_strdup (p);
1019 namespace_uri = g_strdup (attribute_values[i]);
1021 g_hash_table_replace (parse_data->namespaces,
1022 namespace_name,
1023 namespace_uri);
1028 /* checks whether @element_full is equal to @element.
1030 * if @namespace is set, it tries to resolve the namespace to a known URI,
1031 * and if found is prepended to the element name, from which is separated
1032 * using the character specified in the @sep parameter.
1034 static gboolean
1035 is_element_full (ParseData *parse_data,
1036 const gchar *element_full,
1037 const gchar *namespace,
1038 const gchar *element,
1039 const gchar sep)
1041 gchar *ns_uri, *ns_name;
1042 const gchar *p, *element_name;
1043 gboolean retval;
1045 g_warn_if_fail (parse_data != NULL);
1046 g_warn_if_fail (element_full != NULL);
1048 if (!element)
1049 return FALSE;
1051 /* no namespace requested: dumb element compare */
1052 if (!namespace)
1053 return (0 == strcmp (element_full, element));
1055 /* search for namespace separator; if none found, assume we are under the
1056 * default namespace, and set ns_name to our "default" marker; if no default
1057 * namespace has been set, just do a plain comparison between @full_element
1058 * and @element.
1060 p = g_utf8_strchr (element_full, -1, ':');
1061 if (p)
1063 ns_name = g_strndup (element_full, p - element_full);
1064 element_name = g_utf8_next_char (p);
1066 else
1068 ns_name = g_strdup ("default");
1069 element_name = element_full;
1072 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1073 if (!ns_uri)
1075 /* no default namespace found */
1076 g_free (ns_name);
1078 return (0 == strcmp (element_full, element));
1081 retval = (0 == strcmp (ns_uri, namespace) &&
1082 0 == strcmp (element_name, element));
1084 g_free (ns_name);
1086 return retval;
1089 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1090 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1092 static void
1093 start_element_raw_cb (GMarkupParseContext *context,
1094 const gchar *element_name,
1095 const gchar **attribute_names,
1096 const gchar **attribute_values,
1097 gpointer user_data,
1098 GError **error)
1100 ParseData *parse_data = (ParseData *) user_data;
1102 /* we must check for namespace declarations first
1104 * XXX - we could speed up things by checking for namespace declarations
1105 * only on the root node, where they usually are; this would probably break
1106 * on streams not produced by us or by "smart" generators
1108 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1110 switch (parse_data->state)
1112 case STATE_STARTED:
1113 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1115 const gchar *attr;
1116 gint i;
1118 i = 0;
1119 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1121 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1122 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1123 parse_data->state = STATE_ROOT;
1126 else
1127 g_set_error (error, G_MARKUP_ERROR,
1128 G_MARKUP_ERROR_INVALID_CONTENT,
1129 _("Unexpected tag '%s', tag '%s' expected"),
1130 element_name, XBEL_ROOT_ELEMENT);
1131 break;
1132 case STATE_ROOT:
1133 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1134 parse_data->state = STATE_TITLE;
1135 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1136 parse_data->state = STATE_DESC;
1137 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1139 GError *inner_error = NULL;
1141 parse_data->state = STATE_BOOKMARK;
1143 parse_bookmark_element (context,
1144 parse_data,
1145 attribute_names,
1146 attribute_values,
1147 &inner_error);
1148 if (inner_error)
1149 g_propagate_error (error, inner_error);
1151 else
1152 g_set_error (error, G_MARKUP_ERROR,
1153 G_MARKUP_ERROR_INVALID_CONTENT,
1154 _("Unexpected tag '%s' inside '%s'"),
1155 element_name,
1156 XBEL_ROOT_ELEMENT);
1157 break;
1158 case STATE_BOOKMARK:
1159 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1160 parse_data->state = STATE_TITLE;
1161 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1162 parse_data->state = STATE_DESC;
1163 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1164 parse_data->state = STATE_INFO;
1165 else
1166 g_set_error (error, G_MARKUP_ERROR,
1167 G_MARKUP_ERROR_INVALID_CONTENT,
1168 _("Unexpected tag '%s' inside '%s'"),
1169 element_name,
1170 XBEL_BOOKMARK_ELEMENT);
1171 break;
1172 case STATE_INFO:
1173 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1175 const gchar *attr;
1176 gint i;
1178 i = 0;
1179 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1181 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1182 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1184 parse_data->state = STATE_METADATA;
1186 if (!parse_data->current_item->metadata)
1187 parse_data->current_item->metadata = bookmark_metadata_new ();
1191 else
1192 g_set_error (error, G_MARKUP_ERROR,
1193 G_MARKUP_ERROR_INVALID_CONTENT,
1194 _("Unexpected tag '%s', tag '%s' expected"),
1195 element_name,
1196 XBEL_METADATA_ELEMENT);
1197 break;
1198 case STATE_METADATA:
1199 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1200 parse_data->state = STATE_APPLICATIONS;
1201 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1202 parse_data->state = STATE_GROUPS;
1203 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1204 parse_data->current_item->metadata->is_private = TRUE;
1205 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1207 GError *inner_error = NULL;
1209 parse_data->state = STATE_ICON;
1211 parse_icon_element (context,
1212 parse_data,
1213 attribute_names,
1214 attribute_values,
1215 &inner_error);
1216 if (inner_error)
1217 g_propagate_error (error, inner_error);
1219 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1221 GError *inner_error = NULL;
1223 parse_data->state = STATE_MIME;
1225 parse_mime_type_element (context,
1226 parse_data,
1227 attribute_names,
1228 attribute_values,
1229 &inner_error);
1230 if (inner_error)
1231 g_propagate_error (error, inner_error);
1233 else
1234 g_set_error (error, G_MARKUP_ERROR,
1235 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1236 _("Unexpected tag '%s' inside '%s'"),
1237 element_name,
1238 XBEL_METADATA_ELEMENT);
1239 break;
1240 case STATE_APPLICATIONS:
1241 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1243 GError *inner_error = NULL;
1245 parse_data->state = STATE_APPLICATION;
1247 parse_application_element (context,
1248 parse_data,
1249 attribute_names,
1250 attribute_values,
1251 &inner_error);
1252 if (inner_error)
1253 g_propagate_error (error, inner_error);
1255 else
1256 g_set_error (error, G_MARKUP_ERROR,
1257 G_MARKUP_ERROR_INVALID_CONTENT,
1258 _("Unexpected tag '%s', tag '%s' expected"),
1259 element_name,
1260 BOOKMARK_APPLICATION_ELEMENT);
1261 break;
1262 case STATE_GROUPS:
1263 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1264 parse_data->state = STATE_GROUP;
1265 else
1266 g_set_error (error, G_MARKUP_ERROR,
1267 G_MARKUP_ERROR_INVALID_CONTENT,
1268 _("Unexpected tag '%s', tag '%s' expected"),
1269 element_name,
1270 BOOKMARK_GROUP_ELEMENT);
1271 break;
1272 case STATE_ICON:
1273 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1275 GError *inner_error = NULL;
1277 parse_icon_element (context,
1278 parse_data,
1279 attribute_names,
1280 attribute_values,
1281 &inner_error);
1282 if (inner_error)
1283 g_propagate_error (error, inner_error);
1285 else
1286 g_set_error (error, G_MARKUP_ERROR,
1287 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1288 _("Unexpected tag '%s' inside '%s'"),
1289 element_name,
1290 XBEL_METADATA_ELEMENT);
1291 break;
1292 default:
1293 g_warn_if_reached ();
1294 break;
1298 static void
1299 end_element_raw_cb (GMarkupParseContext *context,
1300 const gchar *element_name,
1301 gpointer user_data,
1302 GError **error)
1304 ParseData *parse_data = (ParseData *) user_data;
1306 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1307 parse_data->state = STATE_FINISHED;
1308 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1310 parse_data->current_item = NULL;
1312 parse_data->state = STATE_ROOT;
1314 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1315 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1316 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1318 if (parse_data->current_item)
1319 parse_data->state = STATE_BOOKMARK;
1320 else
1321 parse_data->state = STATE_ROOT;
1323 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1324 parse_data->state = STATE_INFO;
1325 else if (IS_ELEMENT_NS (parse_data, element_name,
1326 BOOKMARK_NAMESPACE_URI,
1327 BOOKMARK_APPLICATION_ELEMENT))
1328 parse_data->state = STATE_APPLICATIONS;
1329 else if (IS_ELEMENT_NS (parse_data, element_name,
1330 BOOKMARK_NAMESPACE_URI,
1331 BOOKMARK_GROUP_ELEMENT))
1332 parse_data->state = STATE_GROUPS;
1333 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1334 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1335 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1336 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1337 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1338 parse_data->state = STATE_METADATA;
1341 static void
1342 text_raw_cb (GMarkupParseContext *context,
1343 const gchar *text,
1344 gsize length,
1345 gpointer user_data,
1346 GError **error)
1348 ParseData *parse_data = (ParseData *) user_data;
1349 gchar *payload;
1351 payload = g_strndup (text, length);
1353 switch (parse_data->state)
1355 case STATE_TITLE:
1356 if (parse_data->current_item)
1358 g_free (parse_data->current_item->title);
1359 parse_data->current_item->title = g_strdup (payload);
1361 else
1363 g_free (parse_data->bookmark_file->title);
1364 parse_data->bookmark_file->title = g_strdup (payload);
1366 break;
1367 case STATE_DESC:
1368 if (parse_data->current_item)
1370 g_free (parse_data->current_item->description);
1371 parse_data->current_item->description = g_strdup (payload);
1373 else
1375 g_free (parse_data->bookmark_file->description);
1376 parse_data->bookmark_file->description = g_strdup (payload);
1378 break;
1379 case STATE_GROUP:
1381 GList *groups;
1383 g_warn_if_fail (parse_data->current_item != NULL);
1385 if (!parse_data->current_item->metadata)
1386 parse_data->current_item->metadata = bookmark_metadata_new ();
1388 groups = parse_data->current_item->metadata->groups;
1389 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1391 break;
1392 case STATE_ROOT:
1393 case STATE_BOOKMARK:
1394 case STATE_INFO:
1395 case STATE_METADATA:
1396 case STATE_APPLICATIONS:
1397 case STATE_APPLICATION:
1398 case STATE_GROUPS:
1399 case STATE_MIME:
1400 case STATE_ICON:
1401 break;
1402 default:
1403 g_warn_if_reached ();
1404 break;
1407 g_free (payload);
1410 static const GMarkupParser markup_parser =
1412 start_element_raw_cb, /* start_element */
1413 end_element_raw_cb, /* end_element */
1414 text_raw_cb, /* text */
1415 NULL, /* passthrough */
1416 NULL
1419 static gboolean
1420 g_bookmark_file_parse (GBookmarkFile *bookmark,
1421 const gchar *buffer,
1422 gsize length,
1423 GError **error)
1425 GMarkupParseContext *context;
1426 ParseData *parse_data;
1427 GError *parse_error, *end_error;
1428 gboolean retval;
1430 g_warn_if_fail (bookmark != NULL);
1432 if (!buffer)
1433 return FALSE;
1435 parse_error = NULL;
1436 end_error = NULL;
1438 if (length == (gsize) -1)
1439 length = strlen (buffer);
1441 parse_data = parse_data_new ();
1442 parse_data->bookmark_file = bookmark;
1444 context = g_markup_parse_context_new (&markup_parser,
1446 parse_data,
1447 (GDestroyNotify) parse_data_free);
1449 retval = g_markup_parse_context_parse (context,
1450 buffer,
1451 length,
1452 &parse_error);
1453 if (!retval)
1454 g_propagate_error (error, parse_error);
1455 else
1457 retval = g_markup_parse_context_end_parse (context, &end_error);
1458 if (!retval)
1459 g_propagate_error (error, end_error);
1462 g_markup_parse_context_free (context);
1464 return retval;
1467 static gchar *
1468 g_bookmark_file_dump (GBookmarkFile *bookmark,
1469 gsize *length,
1470 GError **error)
1472 GString *retval;
1473 gchar *buffer;
1474 GList *l;
1476 retval = g_string_sized_new (4096);
1478 g_string_append (retval,
1479 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1480 #if 0
1481 /* XXX - do we really need the doctype? */
1482 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1483 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1484 " \"" XBEL_DTD_URI "\">\n"
1485 #endif
1486 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1487 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1488 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1490 if (bookmark->title)
1492 gchar *escaped_title;
1494 escaped_title = g_markup_escape_text (bookmark->title, -1);
1496 buffer = g_strconcat (" "
1497 "<" XBEL_TITLE_ELEMENT ">",
1498 escaped_title,
1499 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1501 g_string_append (retval, buffer);
1503 g_free (buffer);
1504 g_free (escaped_title);
1507 if (bookmark->description)
1509 gchar *escaped_desc;
1511 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1513 buffer = g_strconcat (" "
1514 "<" XBEL_DESC_ELEMENT ">",
1515 escaped_desc,
1516 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1517 g_string_append (retval, buffer);
1519 g_free (buffer);
1520 g_free (escaped_desc);
1523 if (!bookmark->items)
1524 goto out;
1525 else
1526 retval = g_string_append (retval, "\n");
1528 /* the items are stored in reverse order */
1529 for (l = g_list_last (bookmark->items);
1530 l != NULL;
1531 l = l->prev)
1533 BookmarkItem *item = (BookmarkItem *) l->data;
1534 gchar *item_dump;
1536 item_dump = bookmark_item_dump (item);
1537 if (!item_dump)
1538 continue;
1540 retval = g_string_append (retval, item_dump);
1542 g_free (item_dump);
1545 out:
1546 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1548 if (length)
1549 *length = retval->len;
1551 return g_string_free (retval, FALSE);
1554 /**************
1555 * Misc *
1556 **************/
1558 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1559 * should free the returned string.
1561 static gchar *
1562 timestamp_to_iso8601 (time_t timestamp)
1564 GTimeVal stamp;
1566 if (timestamp == (time_t) -1)
1567 g_get_current_time (&stamp);
1568 else
1570 stamp.tv_sec = timestamp;
1571 stamp.tv_usec = 0;
1574 return g_time_val_to_iso8601 (&stamp);
1577 static time_t
1578 timestamp_from_iso8601 (const gchar *iso_date)
1580 GTimeVal stamp;
1582 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1583 return (time_t) -1;
1585 return (time_t) stamp.tv_sec;
1590 GQuark
1591 g_bookmark_file_error_quark (void)
1593 return g_quark_from_static_string ("g-bookmark-file-error-quark");
1598 /********************
1599 * Public API *
1600 ********************/
1603 * g_bookmark_file_new:
1605 * Creates a new empty #GBookmarkFile object.
1607 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1608 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1609 * file.
1611 * Return value: an empty #GBookmarkFile
1613 * Since: 2.12
1615 GBookmarkFile *
1616 g_bookmark_file_new (void)
1618 GBookmarkFile *bookmark;
1620 bookmark = g_new (GBookmarkFile, 1);
1622 g_bookmark_file_init (bookmark);
1624 return bookmark;
1628 * g_bookmark_file_free:
1629 * @bookmark: a #GBookmarkFile
1631 * Frees a #GBookmarkFile.
1633 * Since: 2.12
1635 void
1636 g_bookmark_file_free (GBookmarkFile *bookmark)
1638 if (!bookmark)
1639 return;
1641 g_bookmark_file_clear (bookmark);
1643 g_free (bookmark);
1647 * g_bookmark_file_load_from_data:
1648 * @bookmark: an empty #GBookmarkFile struct
1649 * @data: desktop bookmarks loaded in memory
1650 * @length: the length of @data in bytes
1651 * @error: return location for a #GError, or %NULL
1653 * Loads a bookmark file from memory into an empty #GBookmarkFile
1654 * structure. If the object cannot be created then @error is set to a
1655 * #GBookmarkFileError.
1657 * Return value: %TRUE if a desktop bookmark could be loaded.
1659 * Since: 2.12
1661 gboolean
1662 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1663 const gchar *data,
1664 gsize length,
1665 GError **error)
1667 GError *parse_error;
1668 gboolean retval;
1670 g_return_val_if_fail (bookmark != NULL, FALSE);
1672 if (length == (gsize) -1)
1673 length = strlen (data);
1675 if (bookmark->items)
1677 g_bookmark_file_clear (bookmark);
1678 g_bookmark_file_init (bookmark);
1681 parse_error = NULL;
1682 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1684 if (!retval)
1685 g_propagate_error (error, parse_error);
1687 return retval;
1691 * g_bookmark_file_load_from_file:
1692 * @bookmark: an empty #GBookmarkFile struct
1693 * @filename: the path of a filename to load, in the GLib file name encoding
1694 * @error: return location for a #GError, or %NULL
1696 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1697 * If the file could not be loaded then @error is set to either a #GFileError
1698 * or #GBookmarkFileError.
1700 * Return value: %TRUE if a desktop bookmark file could be loaded
1702 * Since: 2.12
1704 gboolean
1705 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1706 const gchar *filename,
1707 GError **error)
1709 gchar *buffer;
1710 gsize len;
1711 GError *read_error;
1712 gboolean retval;
1714 g_return_val_if_fail (bookmark != NULL, FALSE);
1715 g_return_val_if_fail (filename != NULL, FALSE);
1717 read_error = NULL;
1718 g_file_get_contents (filename, &buffer, &len, &read_error);
1719 if (read_error)
1721 g_propagate_error (error, read_error);
1723 return FALSE;
1726 read_error = NULL;
1727 retval = g_bookmark_file_load_from_data (bookmark,
1728 buffer,
1729 len,
1730 &read_error);
1731 if (read_error)
1733 g_propagate_error (error, read_error);
1735 g_free (buffer);
1737 return FALSE;
1740 g_free (buffer);
1742 return retval;
1746 /* Iterates through all the directories in *dirs trying to
1747 * find file. When it successfully locates file, returns a
1748 * string its absolute path. It also leaves the unchecked
1749 * directories in *dirs. You should free the returned string
1751 * Adapted from gkeyfile.c
1753 static gchar *
1754 find_file_in_data_dirs (const gchar *file,
1755 gchar ***dirs,
1756 GError **error)
1758 gchar **data_dirs, *data_dir, *path;
1760 path = NULL;
1762 if (dirs == NULL)
1763 return NULL;
1765 data_dirs = *dirs;
1766 path = NULL;
1767 while (data_dirs && (data_dir = *data_dirs) && !path)
1769 gchar *candidate_file, *sub_dir;
1771 candidate_file = (gchar *) file;
1772 sub_dir = g_strdup ("");
1773 while (candidate_file != NULL && !path)
1775 gchar *p;
1777 path = g_build_filename (data_dir, sub_dir,
1778 candidate_file, NULL);
1780 candidate_file = strchr (candidate_file, '-');
1782 if (candidate_file == NULL)
1783 break;
1785 candidate_file++;
1787 g_free (sub_dir);
1788 sub_dir = g_strndup (file, candidate_file - file - 1);
1790 for (p = sub_dir; *p != '\0'; p++)
1792 if (*p == '-')
1793 *p = G_DIR_SEPARATOR;
1796 g_free (sub_dir);
1797 data_dirs++;
1800 *dirs = data_dirs;
1802 if (!path)
1804 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1805 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1806 _("No valid bookmark file found in data dirs"));
1808 return NULL;
1811 return path;
1816 * g_bookmark_file_load_from_data_dirs:
1817 * @bookmark: a #GBookmarkFile
1818 * @file: a relative path to a filename to open and parse
1819 * @full_path: (allow-none): return location for a string containing the full path
1820 * of the file, or %NULL
1821 * @error: return location for a #GError, or %NULL
1823 * This function looks for a desktop bookmark file named @file in the
1824 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1825 * loads the file into @bookmark and returns the file's full path in
1826 * @full_path. If the file could not be loaded then an %error is
1827 * set to either a #GFileError or #GBookmarkFileError.
1829 * Return value: %TRUE if a key file could be loaded, %FALSE otherwise
1831 * Since: 2.12
1833 gboolean
1834 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1835 const gchar *file,
1836 gchar **full_path,
1837 GError **error)
1839 GError *file_error = NULL;
1840 gchar **all_data_dirs, **data_dirs;
1841 const gchar *user_data_dir;
1842 const gchar * const * system_data_dirs;
1843 gsize i, j;
1844 gchar *output_path;
1845 gboolean found_file;
1847 g_return_val_if_fail (bookmark != NULL, FALSE);
1848 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1850 user_data_dir = g_get_user_data_dir ();
1851 system_data_dirs = g_get_system_data_dirs ();
1852 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1854 i = 0;
1855 all_data_dirs[i++] = g_strdup (user_data_dir);
1857 j = 0;
1858 while (system_data_dirs[j] != NULL)
1859 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1861 found_file = FALSE;
1862 data_dirs = all_data_dirs;
1863 output_path = NULL;
1864 while (*data_dirs != NULL && !found_file)
1866 g_free (output_path);
1868 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1870 if (file_error)
1872 g_propagate_error (error, file_error);
1873 break;
1876 found_file = g_bookmark_file_load_from_file (bookmark,
1877 output_path,
1878 &file_error);
1879 if (file_error)
1881 g_propagate_error (error, file_error);
1882 break;
1886 if (found_file && full_path)
1887 *full_path = output_path;
1888 else
1889 g_free (output_path);
1891 g_strfreev (all_data_dirs);
1893 return found_file;
1898 * g_bookmark_file_to_data:
1899 * @bookmark: a #GBookmarkFile
1900 * @length: (allow-none): return location for the length of the returned string, or %NULL
1901 * @error: return location for a #GError, or %NULL
1903 * This function outputs @bookmark as a string.
1905 * Return value: a newly allocated string holding
1906 * the contents of the #GBookmarkFile
1908 * Since: 2.12
1910 gchar *
1911 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1912 gsize *length,
1913 GError **error)
1915 GError *write_error = NULL;
1916 gchar *retval;
1918 g_return_val_if_fail (bookmark != NULL, NULL);
1920 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1921 if (write_error)
1923 g_propagate_error (error, write_error);
1925 return NULL;
1928 return retval;
1932 * g_bookmark_file_to_file:
1933 * @bookmark: a #GBookmarkFile
1934 * @filename: path of the output file
1935 * @error: return location for a #GError, or %NULL
1937 * This function outputs @bookmark into a file. The write process is
1938 * guaranteed to be atomic by using g_file_set_contents() internally.
1940 * Return value: %TRUE if the file was successfully written.
1942 * Since: 2.12
1944 gboolean
1945 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1946 const gchar *filename,
1947 GError **error)
1949 gchar *data;
1950 GError *data_error, *write_error;
1951 gsize len;
1952 gboolean retval;
1954 g_return_val_if_fail (bookmark != NULL, FALSE);
1955 g_return_val_if_fail (filename != NULL, FALSE);
1957 data_error = NULL;
1958 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1959 if (data_error)
1961 g_propagate_error (error, data_error);
1963 return FALSE;
1966 write_error = NULL;
1967 g_file_set_contents (filename, data, len, &write_error);
1968 if (write_error)
1970 g_propagate_error (error, write_error);
1972 retval = FALSE;
1974 else
1975 retval = TRUE;
1977 g_free (data);
1979 return retval;
1982 static BookmarkItem *
1983 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1984 const gchar *uri)
1986 g_warn_if_fail (bookmark != NULL && uri != NULL);
1988 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1991 /* this function adds a new item to the list */
1992 static void
1993 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1994 BookmarkItem *item,
1995 GError **error)
1997 g_warn_if_fail (bookmark != NULL);
1998 g_warn_if_fail (item != NULL);
2000 /* this should never happen; and if it does, then we are
2001 * screwing up something big time.
2003 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2005 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2006 G_BOOKMARK_FILE_ERROR_INVALID_URI,
2007 _("A bookmark for URI '%s' already exists"),
2008 item->uri);
2009 return;
2012 bookmark->items = g_list_prepend (bookmark->items, item);
2014 g_hash_table_replace (bookmark->items_by_uri,
2015 item->uri,
2016 item);
2018 if (item->added == (time_t) -1)
2019 item->added = time (NULL);
2021 if (item->modified == (time_t) -1)
2022 item->modified = time (NULL);
2026 * g_bookmark_file_remove_item:
2027 * @bookmark: a #GBookmarkFile
2028 * @uri: a valid URI
2029 * @error: return location for a #GError, or %NULL
2031 * Removes the bookmark for @uri from the bookmark file @bookmark.
2033 * Return value: %TRUE if the bookmark was removed successfully.
2035 * Since: 2.12
2037 gboolean
2038 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2039 const gchar *uri,
2040 GError **error)
2042 BookmarkItem *item;
2044 g_return_val_if_fail (bookmark != NULL, FALSE);
2045 g_return_val_if_fail (uri != NULL, FALSE);
2047 item = g_bookmark_file_lookup_item (bookmark, uri);
2049 if (!item)
2051 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2052 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2053 _("No bookmark found for URI '%s'"),
2054 uri);
2055 return FALSE;
2058 bookmark->items = g_list_remove (bookmark->items, item);
2059 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2061 bookmark_item_free (item);
2063 return TRUE;
2067 * g_bookmark_file_has_item:
2068 * @bookmark: a #GBookmarkFile
2069 * @uri: a valid URI
2071 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2073 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2075 * Since: 2.12
2077 gboolean
2078 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2079 const gchar *uri)
2081 g_return_val_if_fail (bookmark != NULL, FALSE);
2082 g_return_val_if_fail (uri != NULL, FALSE);
2084 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2088 * g_bookmark_file_get_uris:
2089 * @bookmark: a #GBookmarkFile
2090 * @length: (allow-none): return location for the number of returned URIs, or %NULL
2092 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2093 * The array of returned URIs will be %NULL-terminated, so @length may
2094 * optionally be %NULL.
2096 * Return value: a newly allocated %NULL-terminated array of strings.
2097 * Use g_strfreev() to free it.
2099 * Since: 2.12
2101 gchar **
2102 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2103 gsize *length)
2105 GList *l;
2106 gchar **uris;
2107 gsize i, n_items;
2109 g_return_val_if_fail (bookmark != NULL, NULL);
2111 n_items = g_list_length (bookmark->items);
2112 uris = g_new0 (gchar *, n_items + 1);
2114 /* the items are stored in reverse order, so we walk the list backward */
2115 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2117 BookmarkItem *item = (BookmarkItem *) l->data;
2119 g_warn_if_fail (item != NULL);
2121 uris[i++] = g_strdup (item->uri);
2123 uris[i] = NULL;
2125 if (length)
2126 *length = i;
2128 return uris;
2132 * g_bookmark_file_set_title:
2133 * @bookmark: a #GBookmarkFile
2134 * @uri: (allow-none): a valid URI or %NULL
2135 * @title: a UTF-8 encoded string
2137 * Sets @title as the title of the bookmark for @uri inside the
2138 * bookmark file @bookmark.
2140 * If @uri is %NULL, the title of @bookmark is set.
2142 * If a bookmark for @uri cannot be found then it is created.
2144 * Since: 2.12
2146 void
2147 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2148 const gchar *uri,
2149 const gchar *title)
2151 g_return_if_fail (bookmark != NULL);
2153 if (!uri)
2155 g_free (bookmark->title);
2156 bookmark->title = g_strdup (title);
2158 else
2160 BookmarkItem *item;
2162 item = g_bookmark_file_lookup_item (bookmark, uri);
2163 if (!item)
2165 item = bookmark_item_new (uri);
2166 g_bookmark_file_add_item (bookmark, item, NULL);
2169 g_free (item->title);
2170 item->title = g_strdup (title);
2172 item->modified = time (NULL);
2177 * g_bookmark_file_get_title:
2178 * @bookmark: a #GBookmarkFile
2179 * @uri: (allow-none): a valid URI or %NULL
2180 * @error: return location for a #GError, or %NULL
2182 * Returns the title of the bookmark for @uri.
2184 * If @uri is %NULL, the title of @bookmark is returned.
2186 * In the event the URI cannot be found, %NULL is returned and
2187 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2189 * Return value: a newly allocated string or %NULL if the specified
2190 * URI cannot be found.
2192 * Since: 2.12
2194 gchar *
2195 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2196 const gchar *uri,
2197 GError **error)
2199 BookmarkItem *item;
2201 g_return_val_if_fail (bookmark != NULL, NULL);
2203 if (!uri)
2204 return g_strdup (bookmark->title);
2206 item = g_bookmark_file_lookup_item (bookmark, uri);
2207 if (!item)
2209 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2210 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2211 _("No bookmark found for URI '%s'"),
2212 uri);
2213 return NULL;
2216 return g_strdup (item->title);
2220 * g_bookmark_file_set_description:
2221 * @bookmark: a #GBookmarkFile
2222 * @uri: (allow-none): a valid URI or %NULL
2223 * @description: a string
2225 * Sets @description as the description of the bookmark for @uri.
2227 * If @uri is %NULL, the description of @bookmark is set.
2229 * If a bookmark for @uri cannot be found then it is created.
2231 * Since: 2.12
2233 void
2234 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2235 const gchar *uri,
2236 const gchar *description)
2238 g_return_if_fail (bookmark != NULL);
2240 if (!uri)
2242 g_free (bookmark->description);
2243 bookmark->description = g_strdup (description);
2245 else
2247 BookmarkItem *item;
2249 item = g_bookmark_file_lookup_item (bookmark, uri);
2250 if (!item)
2252 item = bookmark_item_new (uri);
2253 g_bookmark_file_add_item (bookmark, item, NULL);
2256 g_free (item->description);
2257 item->description = g_strdup (description);
2259 item->modified = time (NULL);
2264 * g_bookmark_file_get_description:
2265 * @bookmark: a #GBookmarkFile
2266 * @uri: a valid URI
2267 * @error: return location for a #GError, or %NULL
2269 * Retrieves the description of the bookmark for @uri.
2271 * In the event the URI cannot be found, %NULL is returned and
2272 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2274 * Return value: a newly allocated string or %NULL if the specified
2275 * URI cannot be found.
2277 * Since: 2.12
2279 gchar *
2280 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2281 const gchar *uri,
2282 GError **error)
2284 BookmarkItem *item;
2286 g_return_val_if_fail (bookmark != NULL, NULL);
2288 if (!uri)
2289 return g_strdup (bookmark->description);
2291 item = g_bookmark_file_lookup_item (bookmark, uri);
2292 if (!item)
2294 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2295 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2296 _("No bookmark found for URI '%s'"),
2297 uri);
2298 return NULL;
2301 return g_strdup (item->description);
2305 * g_bookmark_file_set_mime_type:
2306 * @bookmark: a #GBookmarkFile
2307 * @uri: a valid URI
2308 * @mime_type: a MIME type
2310 * Sets @mime_type as the MIME type of the bookmark for @uri.
2312 * If a bookmark for @uri cannot be found then it is created.
2314 * Since: 2.12
2316 void
2317 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2318 const gchar *uri,
2319 const gchar *mime_type)
2321 BookmarkItem *item;
2323 g_return_if_fail (bookmark != NULL);
2324 g_return_if_fail (uri != NULL);
2325 g_return_if_fail (mime_type != NULL);
2327 item = g_bookmark_file_lookup_item (bookmark, uri);
2328 if (!item)
2330 item = bookmark_item_new (uri);
2331 g_bookmark_file_add_item (bookmark, item, NULL);
2334 if (!item->metadata)
2335 item->metadata = bookmark_metadata_new ();
2337 g_free (item->metadata->mime_type);
2339 item->metadata->mime_type = g_strdup (mime_type);
2340 item->modified = time (NULL);
2344 * g_bookmark_file_get_mime_type:
2345 * @bookmark: a #GBookmarkFile
2346 * @uri: a valid URI
2347 * @error: return location for a #GError, or %NULL
2349 * Retrieves the MIME type of the resource pointed by @uri.
2351 * In the event the URI cannot be found, %NULL is returned and
2352 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2353 * event that the MIME type cannot be found, %NULL is returned and
2354 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2356 * Return value: a newly allocated string or %NULL if the specified
2357 * URI cannot be found.
2359 * Since: 2.12
2361 gchar *
2362 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2363 const gchar *uri,
2364 GError **error)
2366 BookmarkItem *item;
2368 g_return_val_if_fail (bookmark != NULL, NULL);
2369 g_return_val_if_fail (uri != NULL, NULL);
2371 item = g_bookmark_file_lookup_item (bookmark, uri);
2372 if (!item)
2374 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2375 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2376 _("No bookmark found for URI '%s'"),
2377 uri);
2378 return NULL;
2381 if (!item->metadata)
2383 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2384 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2385 _("No MIME type defined in the bookmark for URI '%s'"),
2386 uri);
2387 return NULL;
2390 return g_strdup (item->metadata->mime_type);
2394 * g_bookmark_file_set_is_private:
2395 * @bookmark: a #GBookmarkFile
2396 * @uri: a valid URI
2397 * @is_private: %TRUE if the bookmark should be marked as private
2399 * Sets the private flag of the bookmark for @uri.
2401 * If a bookmark for @uri cannot be found then it is created.
2403 * Since: 2.12
2405 void
2406 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2407 const gchar *uri,
2408 gboolean is_private)
2410 BookmarkItem *item;
2412 g_return_if_fail (bookmark != NULL);
2413 g_return_if_fail (uri != NULL);
2415 item = g_bookmark_file_lookup_item (bookmark, uri);
2416 if (!item)
2418 item = bookmark_item_new (uri);
2419 g_bookmark_file_add_item (bookmark, item, NULL);
2422 if (!item->metadata)
2423 item->metadata = bookmark_metadata_new ();
2425 item->metadata->is_private = (is_private == TRUE);
2426 item->modified = time (NULL);
2430 * g_bookmark_file_get_is_private:
2431 * @bookmark: a #GBookmarkFile
2432 * @uri: a valid URI
2433 * @error: return location for a #GError, or %NULL
2435 * Gets whether the private flag of the bookmark for @uri is set.
2437 * In the event the URI cannot be found, %FALSE is returned and
2438 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2439 * event that the private flag cannot be found, %FALSE is returned and
2440 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2442 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2444 * Since: 2.12
2446 gboolean
2447 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2448 const gchar *uri,
2449 GError **error)
2451 BookmarkItem *item;
2453 g_return_val_if_fail (bookmark != NULL, FALSE);
2454 g_return_val_if_fail (uri != NULL, FALSE);
2456 item = g_bookmark_file_lookup_item (bookmark, uri);
2457 if (!item)
2459 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2460 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2461 _("No bookmark found for URI '%s'"),
2462 uri);
2463 return FALSE;
2466 if (!item->metadata)
2468 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2469 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2470 _("No private flag has been defined in bookmark for URI '%s'"),
2471 uri);
2472 return FALSE;
2475 return item->metadata->is_private;
2479 * g_bookmark_file_set_added:
2480 * @bookmark: a #GBookmarkFile
2481 * @uri: a valid URI
2482 * @added: a timestamp or -1 to use the current time
2484 * Sets the time the bookmark for @uri was added into @bookmark.
2486 * If no bookmark for @uri is found then it is created.
2488 * Since: 2.12
2490 void
2491 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2492 const gchar *uri,
2493 time_t added)
2495 BookmarkItem *item;
2497 g_return_if_fail (bookmark != NULL);
2498 g_return_if_fail (uri != NULL);
2500 item = g_bookmark_file_lookup_item (bookmark, uri);
2501 if (!item)
2503 item = bookmark_item_new (uri);
2504 g_bookmark_file_add_item (bookmark, item, NULL);
2507 if (added == (time_t) -1)
2508 time (&added);
2510 item->added = added;
2511 item->modified = added;
2515 * g_bookmark_file_get_added:
2516 * @bookmark: a #GBookmarkFile
2517 * @uri: a valid URI
2518 * @error: return location for a #GError, or %NULL
2520 * Gets the time the bookmark for @uri was added to @bookmark
2522 * In the event the URI cannot be found, -1 is returned and
2523 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2525 * Return value: a timestamp
2527 * Since: 2.12
2529 time_t
2530 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2531 const gchar *uri,
2532 GError **error)
2534 BookmarkItem *item;
2536 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2537 g_return_val_if_fail (uri != NULL, (time_t) -1);
2539 item = g_bookmark_file_lookup_item (bookmark, uri);
2540 if (!item)
2542 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2543 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2544 _("No bookmark found for URI '%s'"),
2545 uri);
2546 return (time_t) -1;
2549 return item->added;
2553 * g_bookmark_file_set_modified:
2554 * @bookmark: a #GBookmarkFile
2555 * @uri: a valid URI
2556 * @modified: a timestamp or -1 to use the current time
2558 * Sets the last time the bookmark for @uri was last modified.
2560 * If no bookmark for @uri is found then it is created.
2562 * The "modified" time should only be set when the bookmark's meta-data
2563 * was actually changed. Every function of #GBookmarkFile that
2564 * modifies a bookmark also changes the modification time, except for
2565 * g_bookmark_file_set_visited().
2567 * Since: 2.12
2569 void
2570 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2571 const gchar *uri,
2572 time_t modified)
2574 BookmarkItem *item;
2576 g_return_if_fail (bookmark != NULL);
2577 g_return_if_fail (uri != NULL);
2579 item = g_bookmark_file_lookup_item (bookmark, uri);
2580 if (!item)
2582 item = bookmark_item_new (uri);
2583 g_bookmark_file_add_item (bookmark, item, NULL);
2586 if (modified == (time_t) -1)
2587 time (&modified);
2589 item->modified = modified;
2593 * g_bookmark_file_get_modified:
2594 * @bookmark: a #GBookmarkFile
2595 * @uri: a valid URI
2596 * @error: return location for a #GError, or %NULL
2598 * Gets the time when the bookmark for @uri was last modified.
2600 * In the event the URI cannot be found, -1 is returned and
2601 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2603 * Return value: a timestamp
2605 * Since: 2.12
2607 time_t
2608 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2609 const gchar *uri,
2610 GError **error)
2612 BookmarkItem *item;
2614 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2615 g_return_val_if_fail (uri != NULL, (time_t) -1);
2617 item = g_bookmark_file_lookup_item (bookmark, uri);
2618 if (!item)
2620 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2621 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2622 _("No bookmark found for URI '%s'"),
2623 uri);
2624 return (time_t) -1;
2627 return item->modified;
2631 * g_bookmark_file_set_visited:
2632 * @bookmark: a #GBookmarkFile
2633 * @uri: a valid URI
2634 * @visited: a timestamp or -1 to use the current time
2636 * Sets the time the bookmark for @uri was last visited.
2638 * If no bookmark for @uri is found then it is created.
2640 * The "visited" time should only be set if the bookmark was launched,
2641 * either using the command line retrieved by g_bookmark_file_get_app_info()
2642 * or by the default application for the bookmark's MIME type, retrieved
2643 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2644 * does not affect the "modified" time.
2646 * Since: 2.12
2648 void
2649 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2650 const gchar *uri,
2651 time_t visited)
2653 BookmarkItem *item;
2655 g_return_if_fail (bookmark != NULL);
2656 g_return_if_fail (uri != NULL);
2658 item = g_bookmark_file_lookup_item (bookmark, uri);
2659 if (!item)
2661 item = bookmark_item_new (uri);
2662 g_bookmark_file_add_item (bookmark, item, NULL);
2665 if (visited == (time_t) -1)
2666 time (&visited);
2668 item->visited = visited;
2672 * g_bookmark_file_get_visited:
2673 * @bookmark: a #GBookmarkFile
2674 * @uri: a valid URI
2675 * @error: return location for a #GError, or %NULL
2677 * Gets the time the bookmark for @uri was last visited.
2679 * In the event the URI cannot be found, -1 is returned and
2680 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2682 * Return value: a timestamp.
2684 * Since: 2.12
2686 time_t
2687 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2688 const gchar *uri,
2689 GError **error)
2691 BookmarkItem *item;
2693 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2694 g_return_val_if_fail (uri != NULL, (time_t) -1);
2696 item = g_bookmark_file_lookup_item (bookmark, uri);
2697 if (!item)
2699 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2700 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2701 _("No bookmark found for URI '%s'"),
2702 uri);
2703 return (time_t) -1;
2706 return item->visited;
2710 * g_bookmark_file_has_group:
2711 * @bookmark: a #GBookmarkFile
2712 * @uri: a valid URI
2713 * @group: the group name to be searched
2714 * @error: return location for a #GError, or %NULL
2716 * Checks whether @group appears in the list of groups to which
2717 * the bookmark for @uri belongs to.
2719 * In the event the URI cannot be found, %FALSE is returned and
2720 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2722 * Return value: %TRUE if @group was found.
2724 * Since: 2.12
2726 gboolean
2727 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2728 const gchar *uri,
2729 const gchar *group,
2730 GError **error)
2732 BookmarkItem *item;
2733 GList *l;
2735 g_return_val_if_fail (bookmark != NULL, FALSE);
2736 g_return_val_if_fail (uri != NULL, FALSE);
2738 item = g_bookmark_file_lookup_item (bookmark, uri);
2739 if (!item)
2741 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2742 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2743 _("No bookmark found for URI '%s'"),
2744 uri);
2745 return FALSE;
2748 if (!item->metadata)
2749 return FALSE;
2751 for (l = item->metadata->groups; l != NULL; l = l->next)
2753 if (strcmp (l->data, group) == 0)
2754 return TRUE;
2757 return FALSE;
2762 * g_bookmark_file_add_group:
2763 * @bookmark: a #GBookmarkFile
2764 * @uri: a valid URI
2765 * @group: the group name to be added
2767 * Adds @group to the list of groups to which the bookmark for @uri
2768 * belongs to.
2770 * If no bookmark for @uri is found then it is created.
2772 * Since: 2.12
2774 void
2775 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2776 const gchar *uri,
2777 const gchar *group)
2779 BookmarkItem *item;
2781 g_return_if_fail (bookmark != NULL);
2782 g_return_if_fail (uri != NULL);
2783 g_return_if_fail (group != NULL && group[0] != '\0');
2785 item = g_bookmark_file_lookup_item (bookmark, uri);
2786 if (!item)
2788 item = bookmark_item_new (uri);
2789 g_bookmark_file_add_item (bookmark, item, NULL);
2792 if (!item->metadata)
2793 item->metadata = bookmark_metadata_new ();
2795 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2797 item->metadata->groups = g_list_prepend (item->metadata->groups,
2798 g_strdup (group));
2800 item->modified = time (NULL);
2805 * g_bookmark_file_remove_group:
2806 * @bookmark: a #GBookmarkFile
2807 * @uri: a valid URI
2808 * @group: the group name to be removed
2809 * @error: return location for a #GError, or %NULL
2811 * Removes @group from the list of groups to which the bookmark
2812 * for @uri belongs to.
2814 * In the event the URI cannot be found, %FALSE is returned and
2815 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2816 * In the event no group was defined, %FALSE is returned and
2817 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2819 * Return value: %TRUE if @group was successfully removed.
2821 * Since: 2.12
2823 gboolean
2824 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2825 const gchar *uri,
2826 const gchar *group,
2827 GError **error)
2829 BookmarkItem *item;
2830 GList *l;
2832 g_return_val_if_fail (bookmark != NULL, FALSE);
2833 g_return_val_if_fail (uri != NULL, FALSE);
2835 item = g_bookmark_file_lookup_item (bookmark, uri);
2836 if (!item)
2838 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2839 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2840 _("No bookmark found for URI '%s'"),
2841 uri);
2842 return FALSE;
2845 if (!item->metadata)
2847 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2848 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2849 _("No groups set in bookmark for URI '%s'"),
2850 uri);
2851 return FALSE;
2854 for (l = item->metadata->groups; l != NULL; l = l->next)
2856 if (strcmp (l->data, group) == 0)
2858 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2859 g_free (l->data);
2860 g_list_free_1 (l);
2862 item->modified = time (NULL);
2864 return TRUE;
2868 return FALSE;
2872 * g_bookmark_file_set_groups:
2873 * @bookmark: a #GBookmarkFile
2874 * @uri: an item's URI
2875 * @groups: (allow-none): an array of group names, or %NULL to remove all groups
2876 * @length: number of group name values in @groups
2878 * Sets a list of group names for the item with URI @uri. Each previously
2879 * set group name list is removed.
2881 * If @uri cannot be found then an item for it is created.
2883 * Since: 2.12
2885 void
2886 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2887 const gchar *uri,
2888 const gchar **groups,
2889 gsize length)
2891 BookmarkItem *item;
2892 gsize i;
2894 g_return_if_fail (bookmark != NULL);
2895 g_return_if_fail (uri != NULL);
2896 g_return_if_fail (groups != NULL);
2898 item = g_bookmark_file_lookup_item (bookmark, uri);
2899 if (!item)
2901 item = bookmark_item_new (uri);
2902 g_bookmark_file_add_item (bookmark, item, NULL);
2905 if (!item->metadata)
2906 item->metadata = bookmark_metadata_new ();
2908 g_list_free_full (item->metadata->groups, g_free);
2909 item->metadata->groups = NULL;
2911 if (groups)
2913 for (i = 0; groups[i] != NULL && i < length; i++)
2914 item->metadata->groups = g_list_append (item->metadata->groups,
2915 g_strdup (groups[i]));
2918 item->modified = time (NULL);
2922 * g_bookmark_file_get_groups:
2923 * @bookmark: a #GBookmarkFile
2924 * @uri: a valid URI
2925 * @length: (allow-none): return location for the length of the returned string, or %NULL
2926 * @error: return location for a #GError, or %NULL
2928 * Retrieves the list of group names of the bookmark for @uri.
2930 * In the event the URI cannot be found, %NULL is returned and
2931 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2933 * The returned array is %NULL terminated, so @length may optionally
2934 * be %NULL.
2936 * Return value: a newly allocated %NULL-terminated array of group names.
2937 * Use g_strfreev() to free it.
2939 * Since: 2.12
2941 gchar **
2942 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2943 const gchar *uri,
2944 gsize *length,
2945 GError **error)
2947 BookmarkItem *item;
2948 GList *l;
2949 gsize len, i;
2950 gchar **retval;
2952 g_return_val_if_fail (bookmark != NULL, NULL);
2953 g_return_val_if_fail (uri != NULL, NULL);
2955 item = g_bookmark_file_lookup_item (bookmark, uri);
2956 if (!item)
2958 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2959 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2960 _("No bookmark found for URI '%s'"),
2961 uri);
2962 return NULL;
2965 if (!item->metadata)
2967 if (length)
2968 *length = 0;
2970 return NULL;
2973 len = g_list_length (item->metadata->groups);
2974 retval = g_new0 (gchar *, len + 1);
2975 for (l = g_list_last (item->metadata->groups), i = 0;
2976 l != NULL;
2977 l = l->prev)
2979 gchar *group_name = (gchar *) l->data;
2981 g_warn_if_fail (group_name != NULL);
2983 retval[i++] = g_strdup (group_name);
2985 retval[i] = NULL;
2987 if (length)
2988 *length = len;
2990 return retval;
2994 * g_bookmark_file_add_application:
2995 * @bookmark: a #GBookmarkFile
2996 * @uri: a valid URI
2997 * @name: (allow-none): the name of the application registering the bookmark
2998 * or %NULL
2999 * @exec: (allow-none): command line to be used to launch the bookmark or %NULL
3001 * Adds the application with @name and @exec to the list of
3002 * applications that have registered a bookmark for @uri into
3003 * @bookmark.
3005 * Every bookmark inside a #GBookmarkFile must have at least an
3006 * application registered. Each application must provide a name, a
3007 * command line useful for launching the bookmark, the number of times
3008 * the bookmark has been registered by the application and the last
3009 * time the application registered this bookmark.
3011 * If @name is %NULL, the name of the application will be the
3012 * same returned by g_get_application_name(); if @exec is %NULL, the
3013 * command line will be a composition of the program name as
3014 * returned by g_get_prgname() and the "\%u" modifier, which will be
3015 * expanded to the bookmark's URI.
3017 * This function will automatically take care of updating the
3018 * registrations count and timestamping in case an application
3019 * with the same @name had already registered a bookmark for
3020 * @uri inside @bookmark.
3022 * If no bookmark for @uri is found, one is created.
3024 * Since: 2.12
3026 void
3027 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3028 const gchar *uri,
3029 const gchar *name,
3030 const gchar *exec)
3032 BookmarkItem *item;
3033 gchar *app_name, *app_exec;
3035 g_return_if_fail (bookmark != NULL);
3036 g_return_if_fail (uri != NULL);
3038 item = g_bookmark_file_lookup_item (bookmark, uri);
3039 if (!item)
3041 item = bookmark_item_new (uri);
3042 g_bookmark_file_add_item (bookmark, item, NULL);
3045 if (name && name[0] != '\0')
3046 app_name = g_strdup (name);
3047 else
3048 app_name = g_strdup (g_get_application_name ());
3050 if (exec && exec[0] != '\0')
3051 app_exec = g_strdup (exec);
3052 else
3053 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3055 g_bookmark_file_set_app_info (bookmark, uri,
3056 app_name,
3057 app_exec,
3059 (time_t) -1,
3060 NULL);
3062 g_free (app_exec);
3063 g_free (app_name);
3067 * g_bookmark_file_remove_application:
3068 * @bookmark: a #GBookmarkFile
3069 * @uri: a valid URI
3070 * @name: the name of the application
3071 * @error: return location for a #GError or %NULL
3073 * Removes application registered with @name from the list of applications
3074 * that have registered a bookmark for @uri inside @bookmark.
3076 * In the event the URI cannot be found, %FALSE is returned and
3077 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3078 * In the event that no application with name @app_name has registered
3079 * a bookmark for @uri, %FALSE is returned and error is set to
3080 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3082 * Return value: %TRUE if the application was successfully removed.
3084 * Since: 2.12
3086 gboolean
3087 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3088 const gchar *uri,
3089 const gchar *name,
3090 GError **error)
3092 GError *set_error;
3093 gboolean retval;
3095 g_return_val_if_fail (bookmark != NULL, FALSE);
3096 g_return_val_if_fail (uri != NULL, FALSE);
3097 g_return_val_if_fail (name != NULL, FALSE);
3099 set_error = NULL;
3100 retval = g_bookmark_file_set_app_info (bookmark, uri,
3101 name,
3104 (time_t) -1,
3105 &set_error);
3106 if (set_error)
3108 g_propagate_error (error, set_error);
3110 return FALSE;
3113 return retval;
3117 * g_bookmark_file_has_application:
3118 * @bookmark: a #GBookmarkFile
3119 * @uri: a valid URI
3120 * @name: the name of the application
3121 * @error: return location for a #GError or %NULL
3123 * Checks whether the bookmark for @uri inside @bookmark has been
3124 * registered by application @name.
3126 * In the event the URI cannot be found, %FALSE is returned and
3127 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3129 * Return value: %TRUE if the application @name was found
3131 * Since: 2.12
3133 gboolean
3134 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3135 const gchar *uri,
3136 const gchar *name,
3137 GError **error)
3139 BookmarkItem *item;
3141 g_return_val_if_fail (bookmark != NULL, FALSE);
3142 g_return_val_if_fail (uri != NULL, FALSE);
3143 g_return_val_if_fail (name != NULL, FALSE);
3145 item = g_bookmark_file_lookup_item (bookmark, uri);
3146 if (!item)
3148 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3149 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3150 _("No bookmark found for URI '%s'"),
3151 uri);
3152 return FALSE;
3155 return (NULL != bookmark_item_lookup_app_info (item, name));
3159 * g_bookmark_file_set_app_info:
3160 * @bookmark: a #GBookmarkFile
3161 * @uri: a valid URI
3162 * @name: an application's name
3163 * @exec: an application's command line
3164 * @count: the number of registrations done for this application
3165 * @stamp: the time of the last registration for this application
3166 * @error: return location for a #GError or %NULL
3168 * Sets the meta-data of application @name inside the list of
3169 * applications that have registered a bookmark for @uri inside
3170 * @bookmark.
3172 * You should rarely use this function; use g_bookmark_file_add_application()
3173 * and g_bookmark_file_remove_application() instead.
3175 * @name can be any UTF-8 encoded string used to identify an
3176 * application.
3177 * @exec can have one of these two modifiers: "\%f", which will
3178 * be expanded as the local file name retrieved from the bookmark's
3179 * URI; "\%u", which will be expanded as the bookmark's URI.
3180 * The expansion is done automatically when retrieving the stored
3181 * command line using the g_bookmark_file_get_app_info() function.
3182 * @count is the number of times the application has registered the
3183 * bookmark; if is < 0, the current registration count will be increased
3184 * by one, if is 0, the application with @name will be removed from
3185 * the list of registered applications.
3186 * @stamp is the Unix time of the last registration; if it is -1, the
3187 * current time will be used.
3189 * If you try to remove an application by setting its registration count to
3190 * zero, and no bookmark for @uri is found, %FALSE is returned and
3191 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3192 * in the event that no application @name has registered a bookmark
3193 * for @uri, %FALSE is returned and error is set to
3194 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3195 * for @uri is found, one is created.
3197 * Return value: %TRUE if the application's meta-data was successfully
3198 * changed.
3200 * Since: 2.12
3202 gboolean
3203 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3204 const gchar *uri,
3205 const gchar *name,
3206 const gchar *exec,
3207 gint count,
3208 time_t stamp,
3209 GError **error)
3211 BookmarkItem *item;
3212 BookmarkAppInfo *ai;
3214 g_return_val_if_fail (bookmark != NULL, FALSE);
3215 g_return_val_if_fail (uri != NULL, FALSE);
3216 g_return_val_if_fail (name != NULL, FALSE);
3217 g_return_val_if_fail (exec != NULL, FALSE);
3219 item = g_bookmark_file_lookup_item (bookmark, uri);
3220 if (!item)
3222 if (count == 0)
3224 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3225 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3226 _("No bookmark found for URI '%s'"),
3227 uri);
3228 return FALSE;
3230 else
3232 item = bookmark_item_new (uri);
3233 g_bookmark_file_add_item (bookmark, item, NULL);
3237 ai = bookmark_item_lookup_app_info (item, name);
3238 if (!ai)
3240 if (count == 0)
3242 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3243 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3244 _("No application with name '%s' registered a bookmark for '%s'"),
3245 name,
3246 uri);
3247 return FALSE;
3249 else
3251 ai = bookmark_app_info_new (name);
3253 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3254 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3258 if (count == 0)
3260 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3261 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3262 bookmark_app_info_free (ai);
3264 item->modified = time (NULL);
3266 return TRUE;
3268 else if (count > 0)
3269 ai->count = count;
3270 else
3271 ai->count += 1;
3273 if (stamp != (time_t) -1)
3274 ai->stamp = stamp;
3275 else
3276 ai->stamp = time (NULL);
3278 if (exec && exec[0] != '\0')
3280 g_free (ai->exec);
3281 ai->exec = g_shell_quote (exec);
3284 item->modified = time (NULL);
3286 return TRUE;
3289 /* expands the application's command line */
3290 static gchar *
3291 expand_exec_line (const gchar *exec_fmt,
3292 const gchar *uri)
3294 GString *exec;
3295 gchar ch;
3297 exec = g_string_sized_new (512);
3298 while ((ch = *exec_fmt++) != '\0')
3300 if (ch != '%')
3302 exec = g_string_append_c (exec, ch);
3303 continue;
3306 ch = *exec_fmt++;
3307 switch (ch)
3309 case '\0':
3310 goto out;
3311 case 'U':
3312 case 'u':
3313 g_string_append (exec, uri);
3314 break;
3315 case 'F':
3316 case 'f':
3318 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3319 if (file)
3321 g_string_append (exec, file);
3322 g_free (file);
3324 else
3326 g_string_free (exec, TRUE);
3327 return NULL;
3330 break;
3331 case '%':
3332 default:
3333 exec = g_string_append_c (exec, ch);
3334 break;
3338 out:
3339 return g_string_free (exec, FALSE);
3343 * g_bookmark_file_get_app_info:
3344 * @bookmark: a #GBookmarkFile
3345 * @uri: a valid URI
3346 * @name: an application's name
3347 * @exec: (allow-none): location for the command line of the application, or %NULL
3348 * @count: (allow-none): return location for the registration count, or %NULL
3349 * @stamp: (allow-none): return location for the last registration time, or %NULL
3350 * @error: return location for a #GError, or %NULL
3352 * Gets the registration informations of @app_name for the bookmark for
3353 * @uri. See g_bookmark_file_set_app_info() for more informations about
3354 * the returned data.
3356 * The string returned in @app_exec must be freed.
3358 * In the event the URI cannot be found, %FALSE is returned and
3359 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3360 * event that no application with name @app_name has registered a bookmark
3361 * for @uri, %FALSE is returned and error is set to
3362 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3363 * the command line fails, an error of the #G_SHELL_ERROR domain is
3364 * set and %FALSE is returned.
3366 * Return value: %TRUE on success.
3368 * Since: 2.12
3370 gboolean
3371 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3372 const gchar *uri,
3373 const gchar *name,
3374 gchar **exec,
3375 guint *count,
3376 time_t *stamp,
3377 GError **error)
3379 BookmarkItem *item;
3380 BookmarkAppInfo *ai;
3382 g_return_val_if_fail (bookmark != NULL, FALSE);
3383 g_return_val_if_fail (uri != NULL, FALSE);
3384 g_return_val_if_fail (name != NULL, FALSE);
3386 item = g_bookmark_file_lookup_item (bookmark, uri);
3387 if (!item)
3389 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3390 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3391 _("No bookmark found for URI '%s'"),
3392 uri);
3393 return FALSE;
3396 ai = bookmark_item_lookup_app_info (item, name);
3397 if (!ai)
3399 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3400 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3401 _("No application with name '%s' registered a bookmark for '%s'"),
3402 name,
3403 uri);
3404 return FALSE;
3407 if (exec)
3409 GError *unquote_error = NULL;
3410 gchar *command_line;
3412 command_line = g_shell_unquote (ai->exec, &unquote_error);
3413 if (unquote_error)
3415 g_propagate_error (error, unquote_error);
3416 return FALSE;
3419 *exec = expand_exec_line (command_line, uri);
3420 if (!*exec)
3422 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3423 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3424 _("Failed to expand exec line '%s' with URI '%s'"),
3425 ai->exec, uri);
3426 g_free (command_line);
3428 return FALSE;
3430 else
3431 g_free (command_line);
3434 if (count)
3435 *count = ai->count;
3437 if (stamp)
3438 *stamp = ai->stamp;
3440 return TRUE;
3444 * g_bookmark_file_get_applications:
3445 * @bookmark: a #GBookmarkFile
3446 * @uri: a valid URI
3447 * @length: (allow-none): return location of the length of the returned list, or %NULL
3448 * @error: return location for a #GError, or %NULL
3450 * Retrieves the names of the applications that have registered the
3451 * bookmark for @uri.
3453 * In the event the URI cannot be found, %NULL is returned and
3454 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3456 * Return value: a newly allocated %NULL-terminated array of strings.
3457 * Use g_strfreev() to free it.
3459 * Since: 2.12
3461 gchar **
3462 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3463 const gchar *uri,
3464 gsize *length,
3465 GError **error)
3467 BookmarkItem *item;
3468 GList *l;
3469 gchar **apps;
3470 gsize i, n_apps;
3472 g_return_val_if_fail (bookmark != NULL, NULL);
3473 g_return_val_if_fail (uri != NULL, NULL);
3475 item = g_bookmark_file_lookup_item (bookmark, uri);
3476 if (!item)
3478 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3479 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3480 _("No bookmark found for URI '%s'"),
3481 uri);
3482 return NULL;
3485 if (!item->metadata)
3487 if (length)
3488 *length = 0;
3490 return NULL;
3493 n_apps = g_list_length (item->metadata->applications);
3494 apps = g_new0 (gchar *, n_apps + 1);
3496 for (l = g_list_last (item->metadata->applications), i = 0;
3497 l != NULL;
3498 l = l->prev)
3500 BookmarkAppInfo *ai;
3502 ai = (BookmarkAppInfo *) l->data;
3504 g_warn_if_fail (ai != NULL);
3505 g_warn_if_fail (ai->name != NULL);
3507 apps[i++] = g_strdup (ai->name);
3509 apps[i] = NULL;
3511 if (length)
3512 *length = i;
3514 return apps;
3518 * g_bookmark_file_get_size:
3519 * @bookmark: a #GBookmarkFile
3521 * Gets the number of bookmarks inside @bookmark.
3523 * Return value: the number of bookmarks
3525 * Since: 2.12
3527 gint
3528 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3530 g_return_val_if_fail (bookmark != NULL, 0);
3532 return g_list_length (bookmark->items);
3536 * g_bookmark_file_move_item:
3537 * @bookmark: a #GBookmarkFile
3538 * @old_uri: a valid URI
3539 * @new_uri: (allow-none): a valid URI, or %NULL
3540 * @error: return location for a #GError or %NULL
3542 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3543 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3544 * %NULL, then the bookmark is removed.
3546 * In the event the URI cannot be found, %FALSE is returned and
3547 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3549 * Return value: %TRUE if the URI was successfully changed
3551 * Since: 2.12
3553 gboolean
3554 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3555 const gchar *old_uri,
3556 const gchar *new_uri,
3557 GError **error)
3559 BookmarkItem *item;
3560 GError *remove_error;
3562 g_return_val_if_fail (bookmark != NULL, FALSE);
3563 g_return_val_if_fail (old_uri != NULL, FALSE);
3565 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3566 if (!item)
3568 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3569 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3570 _("No bookmark found for URI '%s'"),
3571 old_uri);
3572 return FALSE;
3575 if (new_uri && new_uri[0] != '\0')
3577 if (g_bookmark_file_has_item (bookmark, new_uri))
3579 remove_error = NULL;
3580 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3581 if (remove_error)
3583 g_propagate_error (error, remove_error);
3585 return FALSE;
3589 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3591 g_free (item->uri);
3592 item->uri = g_strdup (new_uri);
3593 item->modified = time (NULL);
3595 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3597 return TRUE;
3599 else
3601 remove_error = NULL;
3602 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3603 if (remove_error)
3605 g_propagate_error (error, remove_error);
3607 return FALSE;
3610 return TRUE;
3615 * g_bookmark_file_set_icon:
3616 * @bookmark: a #GBookmarkFile
3617 * @uri: a valid URI
3618 * @href: (allow-none): the URI of the icon for the bookmark, or %NULL
3619 * @mime_type: the MIME type of the icon for the bookmark
3621 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3622 * the currently set icon. @href can either be a full URL for the icon
3623 * file or the icon name following the Icon Naming specification.
3625 * If no bookmark for @uri is found one is created.
3627 * Since: 2.12
3629 void
3630 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3631 const gchar *uri,
3632 const gchar *href,
3633 const gchar *mime_type)
3635 BookmarkItem *item;
3637 g_return_if_fail (bookmark != NULL);
3638 g_return_if_fail (uri != NULL);
3640 item = g_bookmark_file_lookup_item (bookmark, uri);
3641 if (!item)
3643 item = bookmark_item_new (uri);
3644 g_bookmark_file_add_item (bookmark, item, NULL);
3647 if (!item->metadata)
3648 item->metadata = bookmark_metadata_new ();
3650 g_free (item->metadata->icon_href);
3651 g_free (item->metadata->icon_mime);
3653 item->metadata->icon_href = g_strdup (href);
3655 if (mime_type && mime_type[0] != '\0')
3656 item->metadata->icon_mime = g_strdup (mime_type);
3657 else
3658 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3660 item->modified = time (NULL);
3664 * g_bookmark_file_get_icon:
3665 * @bookmark: a #GBookmarkFile
3666 * @uri: a valid URI
3667 * @href: (allow-none): return location for the icon's location or %NULL
3668 * @mime_type: (allow-none): return location for the icon's MIME type or %NULL
3669 * @error: return location for a #GError or %NULL
3671 * Gets the icon of the bookmark for @uri.
3673 * In the event the URI cannot be found, %FALSE is returned and
3674 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3676 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3677 * You should free the returned strings.
3679 * Since: 2.12
3681 gboolean
3682 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3683 const gchar *uri,
3684 gchar **href,
3685 gchar **mime_type,
3686 GError **error)
3688 BookmarkItem *item;
3690 g_return_val_if_fail (bookmark != NULL, FALSE);
3691 g_return_val_if_fail (uri != NULL, FALSE);
3693 item = g_bookmark_file_lookup_item (bookmark, uri);
3694 if (!item)
3696 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3697 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3698 _("No bookmark found for URI '%s'"),
3699 uri);
3700 return FALSE;
3703 if ((!item->metadata) || (!item->metadata->icon_href))
3704 return FALSE;
3706 if (href)
3707 *href = g_strdup (item->metadata->icon_href);
3709 if (mime_type)
3710 *mime_type = g_strdup (item->metadata->icon_mime);
3712 return TRUE;