Merge branch 'test-ip_mreq_source-android-only' into 'master'
[glib.git] / glib / gbookmarkfile.c
blob3f0275fb967df72410994f052755ac30556e5a6a
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 Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include "config.h"
21 #include "gbookmarkfile.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <locale.h>
29 #include <time.h>
30 #include <stdarg.h>
32 #include "gconvert.h"
33 #include "gdataset.h"
34 #include "gerror.h"
35 #include "gfileutils.h"
36 #include "ghash.h"
37 #include "glibintl.h"
38 #include "glist.h"
39 #include "gmain.h"
40 #include "gmarkup.h"
41 #include "gmem.h"
42 #include "gmessages.h"
43 #include "gshell.h"
44 #include "gslice.h"
45 #include "gstdio.h"
46 #include "gstring.h"
47 #include "gstrfuncs.h"
48 #include "gtimer.h"
49 #include "gutils.h"
52 /**
53 * SECTION:bookmarkfile
54 * @title: Bookmark file parser
55 * @short_description: parses files containing bookmarks
57 * GBookmarkFile lets you parse, edit or create files containing bookmarks
58 * to URI, along with some meta-data about the resource pointed by the URI
59 * like its MIME type, the application that is registering the bookmark and
60 * the icon that should be used to represent the bookmark. The data is stored
61 * using the
62 * [Desktop Bookmark Specification](http://www.gnome.org/~ebassi/bookmark-spec).
64 * The syntax of the bookmark files is described in detail inside the
65 * Desktop Bookmark Specification, here is a quick summary: bookmark
66 * files use a sub-class of the XML Bookmark Exchange Language
67 * specification, consisting of valid UTF-8 encoded XML, under the
68 * <xbel> root element; each bookmark is stored inside a
69 * <bookmark> element, using its URI: no relative paths can
70 * be used inside a bookmark file. The bookmark may have a user defined
71 * title and description, to be used instead of the URI. Under the
72 * <metadata> element, with its owner attribute set to
73 * `http://freedesktop.org`, is stored the meta-data about a resource
74 * pointed by its URI. The meta-data consists of the resource's MIME
75 * type; the applications that have registered a bookmark; the groups
76 * to which a bookmark belongs to; a visibility flag, used to set the
77 * bookmark as "private" to the applications and groups that has it
78 * registered; the URI and MIME type of an icon, to be used when
79 * displaying the bookmark inside a GUI.
81 * Here is an example of a bookmark file:
82 * [bookmarks.xbel](https://git.gnome.org/browse/glib/tree/glib/tests/bookmarks.xbel)
84 * A bookmark file might contain more than one bookmark; each bookmark
85 * is accessed through its URI.
87 * The important caveat of bookmark files is that when you add a new
88 * bookmark you must also add the application that is registering it, using
89 * g_bookmark_file_add_application() or g_bookmark_file_set_app_info().
90 * If a bookmark has no applications then it won't be dumped when creating
91 * the on disk representation, using g_bookmark_file_to_data() or
92 * g_bookmark_file_to_file().
94 * The #GBookmarkFile parser was added in GLib 2.12.
97 /* XBEL 1.0 standard entities */
98 #define XBEL_VERSION "1.0"
99 #define XBEL_DTD_NICK "xbel"
100 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
101 "Exchange Language 1.0//EN//XML"
103 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
105 #define XBEL_ROOT_ELEMENT "xbel"
106 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
107 #define XBEL_BOOKMARK_ELEMENT "bookmark"
108 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
109 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
110 #define XBEL_TITLE_ELEMENT "title"
111 #define XBEL_DESC_ELEMENT "desc"
112 #define XBEL_INFO_ELEMENT "info"
113 #define XBEL_METADATA_ELEMENT "metadata"
115 #define XBEL_VERSION_ATTRIBUTE "version"
116 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
117 #define XBEL_OWNER_ATTRIBUTE "owner"
118 #define XBEL_ADDED_ATTRIBUTE "added"
119 #define XBEL_VISITED_ATTRIBUTE "visited"
120 #define XBEL_MODIFIED_ATTRIBUTE "modified"
121 #define XBEL_ID_ATTRIBUTE "id"
122 #define XBEL_HREF_ATTRIBUTE "href"
123 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
125 #define XBEL_YES_VALUE "yes"
126 #define XBEL_NO_VALUE "no"
128 /* Desktop bookmark spec entities */
129 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
131 #define BOOKMARK_NAMESPACE_NAME "bookmark"
132 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
134 #define BOOKMARK_GROUPS_ELEMENT "groups"
135 #define BOOKMARK_GROUP_ELEMENT "group"
136 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
137 #define BOOKMARK_APPLICATION_ELEMENT "application"
138 #define BOOKMARK_ICON_ELEMENT "icon"
139 #define BOOKMARK_PRIVATE_ELEMENT "private"
141 #define BOOKMARK_NAME_ATTRIBUTE "name"
142 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
143 #define BOOKMARK_COUNT_ATTRIBUTE "count"
144 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp" /* deprecated by "modified" */
145 #define BOOKMARK_MODIFIED_ATTRIBUTE "modified"
146 #define BOOKMARK_HREF_ATTRIBUTE "href"
147 #define BOOKMARK_TYPE_ATTRIBUTE "type"
149 /* Shared MIME Info entities */
150 #define MIME_NAMESPACE_NAME "mime"
151 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
152 #define MIME_TYPE_ELEMENT "mime-type"
153 #define MIME_TYPE_ATTRIBUTE "type"
156 typedef struct _BookmarkAppInfo BookmarkAppInfo;
157 typedef struct _BookmarkMetadata BookmarkMetadata;
158 typedef struct _BookmarkItem BookmarkItem;
159 typedef struct _ParseData ParseData;
161 struct _BookmarkAppInfo
163 gchar *name;
164 gchar *exec;
166 guint count;
168 time_t stamp;
171 struct _BookmarkMetadata
173 gchar *mime_type;
175 GList *groups;
177 GList *applications;
178 GHashTable *apps_by_name;
180 gchar *icon_href;
181 gchar *icon_mime;
183 guint is_private : 1;
186 struct _BookmarkItem
188 gchar *uri;
190 gchar *title;
191 gchar *description;
193 time_t added;
194 time_t modified;
195 time_t visited;
197 BookmarkMetadata *metadata;
200 struct _GBookmarkFile
202 gchar *title;
203 gchar *description;
205 /* we store our items in a list and keep a copy inside
206 * an hash table for faster lookup performances
208 GList *items;
209 GHashTable *items_by_uri;
212 /* parser state machine */
213 typedef enum
215 STATE_STARTED = 0,
217 STATE_ROOT,
218 STATE_BOOKMARK,
219 STATE_TITLE,
220 STATE_DESC,
221 STATE_INFO,
222 STATE_METADATA,
223 STATE_APPLICATIONS,
224 STATE_APPLICATION,
225 STATE_GROUPS,
226 STATE_GROUP,
227 STATE_MIME,
228 STATE_ICON,
230 STATE_FINISHED
231 } ParserState;
233 static void g_bookmark_file_init (GBookmarkFile *bookmark);
234 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
235 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
236 const gchar *buffer,
237 gsize length,
238 GError **error);
239 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
240 gsize *length,
241 GError **error);
242 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
243 const gchar *uri);
244 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
245 BookmarkItem *item,
246 GError **error);
248 static time_t timestamp_from_iso8601 (const gchar *iso_date);
249 static gchar * timestamp_to_iso8601 (time_t timestamp);
251 /********************************
252 * BookmarkAppInfo *
254 * Application metadata storage *
255 ********************************/
256 static BookmarkAppInfo *
257 bookmark_app_info_new (const gchar *name)
259 BookmarkAppInfo *retval;
261 g_warn_if_fail (name != NULL);
263 retval = g_slice_new (BookmarkAppInfo);
265 retval->name = g_strdup (name);
266 retval->exec = NULL;
267 retval->count = 0;
268 retval->stamp = 0;
270 return retval;
273 static void
274 bookmark_app_info_free (BookmarkAppInfo *app_info)
276 if (!app_info)
277 return;
279 g_free (app_info->name);
280 g_free (app_info->exec);
282 g_slice_free (BookmarkAppInfo, app_info);
285 static gchar *
286 bookmark_app_info_dump (BookmarkAppInfo *app_info)
288 gchar *retval;
289 gchar *name, *exec, *modified, *count;
291 g_warn_if_fail (app_info != NULL);
293 if (app_info->count == 0)
294 return NULL;
296 name = g_markup_escape_text (app_info->name, -1);
297 exec = g_markup_escape_text (app_info->exec, -1);
298 modified = timestamp_to_iso8601 (app_info->stamp);
299 count = g_strdup_printf ("%u", app_info->count);
301 retval = g_strconcat (" "
302 "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
303 " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
304 " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\""
305 " " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", modified, "\""
306 " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
307 NULL);
309 g_free (name);
310 g_free (exec);
311 g_free (modified);
312 g_free (count);
314 return retval;
318 /***********************
319 * BookmarkMetadata *
321 * Metadata storage *
322 ***********************/
323 static BookmarkMetadata *
324 bookmark_metadata_new (void)
326 BookmarkMetadata *retval;
328 retval = g_slice_new (BookmarkMetadata);
330 retval->mime_type = NULL;
332 retval->groups = NULL;
334 retval->applications = NULL;
335 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
336 g_str_equal,
337 NULL,
338 NULL);
340 retval->is_private = FALSE;
342 retval->icon_href = NULL;
343 retval->icon_mime = NULL;
345 return retval;
348 static void
349 bookmark_metadata_free (BookmarkMetadata *metadata)
351 if (!metadata)
352 return;
354 g_free (metadata->mime_type);
356 g_list_free_full (metadata->groups, g_free);
357 g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
359 g_hash_table_destroy (metadata->apps_by_name);
361 g_free (metadata->icon_href);
362 g_free (metadata->icon_mime);
364 g_slice_free (BookmarkMetadata, metadata);
367 static gchar *
368 bookmark_metadata_dump (BookmarkMetadata *metadata)
370 GString *retval;
371 gchar *buffer;
373 if (!metadata->applications)
374 return NULL;
376 retval = g_string_sized_new (1024);
378 /* metadata container */
379 g_string_append (retval,
381 "<" XBEL_METADATA_ELEMENT
382 " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
383 "\">\n");
385 /* mime type */
386 if (metadata->mime_type) {
387 buffer = g_strconcat (" "
388 "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
389 MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
390 NULL);
391 g_string_append (retval, buffer);
392 g_free (buffer);
395 if (metadata->groups)
397 GList *l;
399 /* open groups container */
400 g_string_append (retval,
402 "<" BOOKMARK_NAMESPACE_NAME
403 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
405 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
407 gchar *group_name;
409 group_name = g_markup_escape_text ((gchar *) l->data, -1);
410 buffer = g_strconcat (" "
411 "<" BOOKMARK_NAMESPACE_NAME
412 ":" BOOKMARK_GROUP_ELEMENT ">",
413 group_name,
414 "</" BOOKMARK_NAMESPACE_NAME
415 ":" BOOKMARK_GROUP_ELEMENT ">\n", NULL);
416 g_string_append (retval, buffer);
418 g_free (buffer);
419 g_free (group_name);
422 /* close groups container */
423 g_string_append (retval,
425 "</" BOOKMARK_NAMESPACE_NAME
426 ":" BOOKMARK_GROUPS_ELEMENT ">\n");
429 if (metadata->applications)
431 GList *l;
433 /* open applications container */
434 g_string_append (retval,
436 "<" BOOKMARK_NAMESPACE_NAME
437 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
439 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
441 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
442 gchar *app_data;
444 g_warn_if_fail (app_info != NULL);
446 app_data = bookmark_app_info_dump (app_info);
448 if (app_data)
450 retval = g_string_append (retval, app_data);
452 g_free (app_data);
456 /* close applications container */
457 g_string_append (retval,
459 "</" BOOKMARK_NAMESPACE_NAME
460 ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
463 /* icon */
464 if (metadata->icon_href)
466 if (!metadata->icon_mime)
467 metadata->icon_mime = g_strdup ("application/octet-stream");
469 buffer = g_strconcat (" "
470 "<" BOOKMARK_NAMESPACE_NAME
471 ":" BOOKMARK_ICON_ELEMENT
472 " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
473 "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
474 g_string_append (retval, buffer);
476 g_free (buffer);
479 /* private hint */
480 if (metadata->is_private)
481 g_string_append (retval,
483 "<" BOOKMARK_NAMESPACE_NAME
484 ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
486 /* close metadata container */
487 g_string_append (retval,
489 "</" XBEL_METADATA_ELEMENT ">\n");
491 return g_string_free (retval, FALSE);
494 /******************************************************
495 * BookmarkItem *
497 * Storage for a single bookmark item inside the list *
498 ******************************************************/
499 static BookmarkItem *
500 bookmark_item_new (const gchar *uri)
502 BookmarkItem *item;
504 g_warn_if_fail (uri != NULL);
506 item = g_slice_new (BookmarkItem);
507 item->uri = g_strdup (uri);
509 item->title = NULL;
510 item->description = NULL;
512 item->added = (time_t) -1;
513 item->modified = (time_t) -1;
514 item->visited = (time_t) -1;
516 item->metadata = NULL;
518 return item;
521 static void
522 bookmark_item_free (BookmarkItem *item)
524 if (!item)
525 return;
527 g_free (item->uri);
528 g_free (item->title);
529 g_free (item->description);
531 if (item->metadata)
532 bookmark_metadata_free (item->metadata);
534 g_slice_free (BookmarkItem, item);
537 static gchar *
538 bookmark_item_dump (BookmarkItem *item)
540 GString *retval;
541 gchar *added, *visited, *modified;
542 gchar *escaped_uri;
543 gchar *buffer;
545 /* at this point, we must have at least a registered application; if we don't
546 * we don't screw up the bookmark file, and just skip this item
548 if (!item->metadata || !item->metadata->applications)
550 g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri);
551 return NULL;
554 retval = g_string_sized_new (4096);
556 added = timestamp_to_iso8601 (item->added);
557 modified = timestamp_to_iso8601 (item->modified);
558 visited = timestamp_to_iso8601 (item->visited);
560 escaped_uri = g_markup_escape_text (item->uri, -1);
562 buffer = g_strconcat (" <"
563 XBEL_BOOKMARK_ELEMENT
565 XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
566 XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
567 XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
568 XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
569 NULL);
571 g_string_append (retval, buffer);
573 g_free (escaped_uri);
574 g_free (visited);
575 g_free (modified);
576 g_free (added);
577 g_free (buffer);
579 if (item->title)
581 gchar *escaped_title;
583 escaped_title = g_markup_escape_text (item->title, -1);
584 buffer = g_strconcat (" "
585 "<" XBEL_TITLE_ELEMENT ">",
586 escaped_title,
587 "</" XBEL_TITLE_ELEMENT ">\n",
588 NULL);
589 g_string_append (retval, buffer);
591 g_free (escaped_title);
592 g_free (buffer);
595 if (item->description)
597 gchar *escaped_desc;
599 escaped_desc = g_markup_escape_text (item->description, -1);
600 buffer = g_strconcat (" "
601 "<" XBEL_DESC_ELEMENT ">",
602 escaped_desc,
603 "</" XBEL_DESC_ELEMENT ">\n",
604 NULL);
605 g_string_append (retval, buffer);
607 g_free (escaped_desc);
608 g_free (buffer);
611 if (item->metadata)
613 gchar *metadata;
615 metadata = bookmark_metadata_dump (item->metadata);
616 if (metadata)
618 buffer = g_strconcat (" "
619 "<" XBEL_INFO_ELEMENT ">\n",
620 metadata,
622 "</" XBEL_INFO_ELEMENT ">\n",
623 NULL);
624 retval = g_string_append (retval, buffer);
626 g_free (buffer);
627 g_free (metadata);
631 g_string_append (retval, " </" XBEL_BOOKMARK_ELEMENT ">\n");
633 return g_string_free (retval, FALSE);
636 static BookmarkAppInfo *
637 bookmark_item_lookup_app_info (BookmarkItem *item,
638 const gchar *app_name)
640 g_warn_if_fail (item != NULL && app_name != NULL);
642 if (!item->metadata)
643 return NULL;
645 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
648 /*************************
649 * GBookmarkFile *
650 *************************/
652 static void
653 g_bookmark_file_init (GBookmarkFile *bookmark)
655 bookmark->title = NULL;
656 bookmark->description = NULL;
658 bookmark->items = NULL;
659 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
660 g_str_equal,
661 NULL,
662 NULL);
665 static void
666 g_bookmark_file_clear (GBookmarkFile *bookmark)
668 g_free (bookmark->title);
669 g_free (bookmark->description);
671 g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
672 bookmark->items = NULL;
674 if (bookmark->items_by_uri)
676 g_hash_table_destroy (bookmark->items_by_uri);
678 bookmark->items_by_uri = NULL;
682 struct _ParseData
684 ParserState state;
686 GHashTable *namespaces;
688 GBookmarkFile *bookmark_file;
689 BookmarkItem *current_item;
692 static ParseData *
693 parse_data_new (void)
695 ParseData *retval;
697 retval = g_new (ParseData, 1);
699 retval->state = STATE_STARTED;
700 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
701 (GDestroyNotify) g_free,
702 (GDestroyNotify) g_free);
703 retval->bookmark_file = NULL;
704 retval->current_item = NULL;
706 return retval;
709 static void
710 parse_data_free (ParseData *parse_data)
712 g_hash_table_destroy (parse_data->namespaces);
714 g_free (parse_data);
717 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
719 static void
720 parse_bookmark_element (GMarkupParseContext *context,
721 ParseData *parse_data,
722 const gchar **attribute_names,
723 const gchar **attribute_values,
724 GError **error)
726 const gchar *uri, *added, *modified, *visited;
727 const gchar *attr;
728 gint i;
729 BookmarkItem *item;
730 GError *add_error;
732 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
734 i = 0;
735 uri = added = modified = visited = NULL;
736 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
738 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
739 uri = attribute_values[i];
740 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
741 added = attribute_values[i];
742 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
743 modified = attribute_values[i];
744 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
745 visited = attribute_values[i];
746 else
748 /* bookmark is defined by the XBEL spec, so we need
749 * to error out if the element has different or
750 * missing attributes
752 g_set_error (error, G_MARKUP_ERROR,
753 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
754 _("Unexpected attribute “%s” for element “%s”"),
755 attr,
756 XBEL_BOOKMARK_ELEMENT);
757 return;
761 if (!uri)
763 g_set_error (error, G_MARKUP_ERROR,
764 G_MARKUP_ERROR_INVALID_CONTENT,
765 _("Attribute “%s” of element “%s” not found"),
766 XBEL_HREF_ATTRIBUTE,
767 XBEL_BOOKMARK_ELEMENT);
768 return;
771 g_warn_if_fail (parse_data->current_item == NULL);
773 item = bookmark_item_new (uri);
775 if (added)
776 item->added = timestamp_from_iso8601 (added);
778 if (modified)
779 item->modified = timestamp_from_iso8601 (modified);
781 if (visited)
782 item->visited = timestamp_from_iso8601 (visited);
784 add_error = NULL;
785 g_bookmark_file_add_item (parse_data->bookmark_file,
786 item,
787 &add_error);
788 if (add_error)
790 bookmark_item_free (item);
792 g_propagate_error (error, add_error);
794 return;
797 parse_data->current_item = item;
800 static void
801 parse_application_element (GMarkupParseContext *context,
802 ParseData *parse_data,
803 const gchar **attribute_names,
804 const gchar **attribute_values,
805 GError **error)
807 const gchar *name, *exec, *count, *stamp, *modified;
808 const gchar *attr;
809 gint i;
810 BookmarkItem *item;
811 BookmarkAppInfo *ai;
813 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
815 i = 0;
816 name = exec = count = stamp = modified = NULL;
817 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
819 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
820 name = attribute_values[i];
821 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
822 exec = attribute_values[i];
823 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
824 count = attribute_values[i];
825 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
826 stamp = attribute_values[i];
827 else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
828 modified = attribute_values[i];
831 /* the "name" and "exec" attributes are mandatory */
832 if (!name)
834 g_set_error (error, G_MARKUP_ERROR,
835 G_MARKUP_ERROR_INVALID_CONTENT,
836 _("Attribute “%s” of element “%s” not found"),
837 BOOKMARK_NAME_ATTRIBUTE,
838 BOOKMARK_APPLICATION_ELEMENT);
839 return;
842 if (!exec)
844 g_set_error (error, G_MARKUP_ERROR,
845 G_MARKUP_ERROR_INVALID_CONTENT,
846 _("Attribute “%s” of element “%s” not found"),
847 BOOKMARK_EXEC_ATTRIBUTE,
848 BOOKMARK_APPLICATION_ELEMENT);
849 return;
852 g_warn_if_fail (parse_data->current_item != NULL);
853 item = parse_data->current_item;
855 ai = bookmark_item_lookup_app_info (item, name);
856 if (!ai)
858 ai = bookmark_app_info_new (name);
860 if (!item->metadata)
861 item->metadata = bookmark_metadata_new ();
863 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
864 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
867 ai->exec = g_strdup (exec);
869 if (count)
870 ai->count = atoi (count);
871 else
872 ai->count = 1;
874 if (modified)
875 ai->stamp = timestamp_from_iso8601 (modified);
876 else
878 /* the timestamp attribute has been deprecated but we still parse
879 * it for backward compatibility
881 if (stamp)
882 ai->stamp = (time_t) atol (stamp);
883 else
884 ai->stamp = time (NULL);
888 static void
889 parse_mime_type_element (GMarkupParseContext *context,
890 ParseData *parse_data,
891 const gchar **attribute_names,
892 const gchar **attribute_values,
893 GError **error)
895 const gchar *type;
896 const gchar *attr;
897 gint i;
898 BookmarkItem *item;
900 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
902 i = 0;
903 type = NULL;
904 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
906 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
907 type = attribute_values[i];
910 if (!type)
911 type = "application/octet-stream";
913 g_warn_if_fail (parse_data->current_item != NULL);
914 item = parse_data->current_item;
916 if (!item->metadata)
917 item->metadata = bookmark_metadata_new ();
919 g_free (item->metadata->mime_type);
920 item->metadata->mime_type = g_strdup (type);
923 static void
924 parse_icon_element (GMarkupParseContext *context,
925 ParseData *parse_data,
926 const gchar **attribute_names,
927 const gchar **attribute_values,
928 GError **error)
930 const gchar *href;
931 const gchar *type;
932 const gchar *attr;
933 gint i;
934 BookmarkItem *item;
936 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
938 i = 0;
939 href = NULL;
940 type = NULL;
941 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
943 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
944 href = attribute_values[i];
945 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
946 type = attribute_values[i];
949 /* the "href" attribute is mandatory */
950 if (!href)
952 g_set_error (error, G_MARKUP_ERROR,
953 G_MARKUP_ERROR_INVALID_CONTENT,
954 _("Attribute “%s” of element “%s” not found"),
955 BOOKMARK_HREF_ATTRIBUTE,
956 BOOKMARK_ICON_ELEMENT);
957 return;
960 if (!type)
961 type = "application/octet-stream";
963 g_warn_if_fail (parse_data->current_item != NULL);
964 item = parse_data->current_item;
966 if (!item->metadata)
967 item->metadata = bookmark_metadata_new ();
969 g_free (item->metadata->icon_href);
970 g_free (item->metadata->icon_mime);
971 item->metadata->icon_href = g_strdup (href);
972 item->metadata->icon_mime = g_strdup (type);
975 /* scans through the attributes of an element for the "xmlns" pragma, and
976 * adds any resulting namespace declaration to a per-parser hashtable, using
977 * the namespace name as a key for the namespace URI; if no key was found,
978 * the namespace is considered as default, and stored under the "default" key.
980 * FIXME: this works on the assumption that the generator of the XBEL file
981 * is either this code or is smart enough to place the namespace declarations
982 * inside the main root node or inside the metadata node and does not redefine
983 * a namespace inside an inner node; this does *not* conform to the
984 * XML-NS standard, although is a close approximation. In order to make this
985 * conformant to the XML-NS specification we should use a per-element
986 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
988 static void
989 map_namespace_to_name (ParseData *parse_data,
990 const gchar **attribute_names,
991 const gchar **attribute_values)
993 const gchar *attr;
994 gint i;
996 g_warn_if_fail (parse_data != NULL);
998 if (!attribute_names || !attribute_names[0])
999 return;
1001 i = 0;
1002 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1004 if (g_str_has_prefix (attr, "xmlns"))
1006 gchar *namespace_name, *namespace_uri;
1007 gchar *p;
1009 p = g_utf8_strchr (attr, -1, ':');
1010 if (p)
1011 p = g_utf8_next_char (p);
1012 else
1013 p = "default";
1015 namespace_name = g_strdup (p);
1016 namespace_uri = g_strdup (attribute_values[i]);
1018 g_hash_table_replace (parse_data->namespaces,
1019 namespace_name,
1020 namespace_uri);
1025 /* checks whether @element_full is equal to @element.
1027 * if @namespace is set, it tries to resolve the namespace to a known URI,
1028 * and if found is prepended to the element name, from which is separated
1029 * using the character specified in the @sep parameter.
1031 static gboolean
1032 is_element_full (ParseData *parse_data,
1033 const gchar *element_full,
1034 const gchar *namespace,
1035 const gchar *element,
1036 const gchar sep)
1038 gchar *ns_uri, *ns_name;
1039 const gchar *p, *element_name;
1040 gboolean retval;
1042 g_warn_if_fail (parse_data != NULL);
1043 g_warn_if_fail (element_full != NULL);
1045 if (!element)
1046 return FALSE;
1048 /* no namespace requested: dumb element compare */
1049 if (!namespace)
1050 return (0 == strcmp (element_full, element));
1052 /* search for namespace separator; if none found, assume we are under the
1053 * default namespace, and set ns_name to our "default" marker; if no default
1054 * namespace has been set, just do a plain comparison between @full_element
1055 * and @element.
1057 p = g_utf8_strchr (element_full, -1, ':');
1058 if (p)
1060 ns_name = g_strndup (element_full, p - element_full);
1061 element_name = g_utf8_next_char (p);
1063 else
1065 ns_name = g_strdup ("default");
1066 element_name = element_full;
1069 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1070 if (!ns_uri)
1072 /* no default namespace found */
1073 g_free (ns_name);
1075 return (0 == strcmp (element_full, element));
1078 retval = (0 == strcmp (ns_uri, namespace) &&
1079 0 == strcmp (element_name, element));
1081 g_free (ns_name);
1083 return retval;
1086 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1087 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1089 static const gchar *
1090 parser_state_to_element_name (ParserState state)
1092 switch (state)
1094 case STATE_STARTED:
1095 case STATE_FINISHED:
1096 return "(top-level)";
1097 case STATE_ROOT:
1098 return XBEL_ROOT_ELEMENT;
1099 case STATE_BOOKMARK:
1100 return XBEL_BOOKMARK_ELEMENT;
1101 case STATE_TITLE:
1102 return XBEL_TITLE_ELEMENT;
1103 case STATE_DESC:
1104 return XBEL_DESC_ELEMENT;
1105 case STATE_INFO:
1106 return XBEL_INFO_ELEMENT;
1107 case STATE_METADATA:
1108 return XBEL_METADATA_ELEMENT;
1109 case STATE_APPLICATIONS:
1110 return BOOKMARK_APPLICATIONS_ELEMENT;
1111 case STATE_APPLICATION:
1112 return BOOKMARK_APPLICATION_ELEMENT;
1113 case STATE_GROUPS:
1114 return BOOKMARK_GROUPS_ELEMENT;
1115 case STATE_GROUP:
1116 return BOOKMARK_GROUP_ELEMENT;
1117 case STATE_MIME:
1118 return MIME_TYPE_ELEMENT;
1119 case STATE_ICON:
1120 return BOOKMARK_ICON_ELEMENT;
1121 default:
1122 g_assert_not_reached ();
1126 static void
1127 start_element_raw_cb (GMarkupParseContext *context,
1128 const gchar *element_name,
1129 const gchar **attribute_names,
1130 const gchar **attribute_values,
1131 gpointer user_data,
1132 GError **error)
1134 ParseData *parse_data = (ParseData *) user_data;
1136 /* we must check for namespace declarations first
1138 * XXX - we could speed up things by checking for namespace declarations
1139 * only on the root node, where they usually are; this would probably break
1140 * on streams not produced by us or by "smart" generators
1142 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1144 switch (parse_data->state)
1146 case STATE_STARTED:
1147 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1149 const gchar *attr;
1150 gint i;
1152 i = 0;
1153 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1155 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1156 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1157 parse_data->state = STATE_ROOT;
1160 else
1161 g_set_error (error, G_MARKUP_ERROR,
1162 G_MARKUP_ERROR_INVALID_CONTENT,
1163 _("Unexpected tag “%s”, tag “%s” expected"),
1164 element_name, XBEL_ROOT_ELEMENT);
1165 break;
1166 case STATE_ROOT:
1167 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1168 parse_data->state = STATE_TITLE;
1169 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1170 parse_data->state = STATE_DESC;
1171 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1173 GError *inner_error = NULL;
1175 parse_data->state = STATE_BOOKMARK;
1177 parse_bookmark_element (context,
1178 parse_data,
1179 attribute_names,
1180 attribute_values,
1181 &inner_error);
1182 if (inner_error)
1183 g_propagate_error (error, inner_error);
1185 else
1186 g_set_error (error, G_MARKUP_ERROR,
1187 G_MARKUP_ERROR_INVALID_CONTENT,
1188 _("Unexpected tag “%s” inside “%s”"),
1189 element_name,
1190 XBEL_ROOT_ELEMENT);
1191 break;
1192 case STATE_BOOKMARK:
1193 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1194 parse_data->state = STATE_TITLE;
1195 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1196 parse_data->state = STATE_DESC;
1197 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1198 parse_data->state = STATE_INFO;
1199 else
1200 g_set_error (error, G_MARKUP_ERROR,
1201 G_MARKUP_ERROR_INVALID_CONTENT,
1202 _("Unexpected tag “%s” inside “%s”"),
1203 element_name,
1204 XBEL_BOOKMARK_ELEMENT);
1205 break;
1206 case STATE_INFO:
1207 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1209 const gchar *attr;
1210 gint i;
1212 i = 0;
1213 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1215 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1216 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1218 parse_data->state = STATE_METADATA;
1220 if (!parse_data->current_item->metadata)
1221 parse_data->current_item->metadata = bookmark_metadata_new ();
1225 else
1226 g_set_error (error, G_MARKUP_ERROR,
1227 G_MARKUP_ERROR_INVALID_CONTENT,
1228 _("Unexpected tag “%s”, tag “%s” expected"),
1229 element_name,
1230 XBEL_METADATA_ELEMENT);
1231 break;
1232 case STATE_METADATA:
1233 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1234 parse_data->state = STATE_APPLICATIONS;
1235 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1236 parse_data->state = STATE_GROUPS;
1237 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1238 parse_data->current_item->metadata->is_private = TRUE;
1239 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1241 GError *inner_error = NULL;
1243 parse_data->state = STATE_ICON;
1245 parse_icon_element (context,
1246 parse_data,
1247 attribute_names,
1248 attribute_values,
1249 &inner_error);
1250 if (inner_error)
1251 g_propagate_error (error, inner_error);
1253 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1255 GError *inner_error = NULL;
1257 parse_data->state = STATE_MIME;
1259 parse_mime_type_element (context,
1260 parse_data,
1261 attribute_names,
1262 attribute_values,
1263 &inner_error);
1264 if (inner_error)
1265 g_propagate_error (error, inner_error);
1267 else
1268 g_set_error (error, G_MARKUP_ERROR,
1269 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1270 _("Unexpected tag “%s” inside “%s”"),
1271 element_name,
1272 XBEL_METADATA_ELEMENT);
1273 break;
1274 case STATE_APPLICATIONS:
1275 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1277 GError *inner_error = NULL;
1279 parse_data->state = STATE_APPLICATION;
1281 parse_application_element (context,
1282 parse_data,
1283 attribute_names,
1284 attribute_values,
1285 &inner_error);
1286 if (inner_error)
1287 g_propagate_error (error, inner_error);
1289 else
1290 g_set_error (error, G_MARKUP_ERROR,
1291 G_MARKUP_ERROR_INVALID_CONTENT,
1292 _("Unexpected tag “%s”, tag “%s” expected"),
1293 element_name,
1294 BOOKMARK_APPLICATION_ELEMENT);
1295 break;
1296 case STATE_GROUPS:
1297 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1298 parse_data->state = STATE_GROUP;
1299 else
1300 g_set_error (error, G_MARKUP_ERROR,
1301 G_MARKUP_ERROR_INVALID_CONTENT,
1302 _("Unexpected tag “%s”, tag “%s” expected"),
1303 element_name,
1304 BOOKMARK_GROUP_ELEMENT);
1305 break;
1307 case STATE_TITLE:
1308 case STATE_DESC:
1309 case STATE_APPLICATION:
1310 case STATE_GROUP:
1311 case STATE_MIME:
1312 case STATE_ICON:
1313 case STATE_FINISHED:
1314 g_set_error (error, G_MARKUP_ERROR,
1315 G_MARKUP_ERROR_INVALID_CONTENT,
1316 _("Unexpected tag “%s” inside “%s”"),
1317 element_name,
1318 parser_state_to_element_name (parse_data->state));
1319 break;
1321 default:
1322 g_assert_not_reached ();
1323 break;
1327 static void
1328 end_element_raw_cb (GMarkupParseContext *context,
1329 const gchar *element_name,
1330 gpointer user_data,
1331 GError **error)
1333 ParseData *parse_data = (ParseData *) user_data;
1335 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1336 parse_data->state = STATE_FINISHED;
1337 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1339 parse_data->current_item = NULL;
1341 parse_data->state = STATE_ROOT;
1343 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1344 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1345 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1347 if (parse_data->current_item)
1348 parse_data->state = STATE_BOOKMARK;
1349 else
1350 parse_data->state = STATE_ROOT;
1352 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1353 parse_data->state = STATE_INFO;
1354 else if (IS_ELEMENT_NS (parse_data, element_name,
1355 BOOKMARK_NAMESPACE_URI,
1356 BOOKMARK_APPLICATION_ELEMENT))
1357 parse_data->state = STATE_APPLICATIONS;
1358 else if (IS_ELEMENT_NS (parse_data, element_name,
1359 BOOKMARK_NAMESPACE_URI,
1360 BOOKMARK_GROUP_ELEMENT))
1361 parse_data->state = STATE_GROUPS;
1362 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1363 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1364 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1365 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1366 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1367 parse_data->state = STATE_METADATA;
1370 static void
1371 text_raw_cb (GMarkupParseContext *context,
1372 const gchar *text,
1373 gsize length,
1374 gpointer user_data,
1375 GError **error)
1377 ParseData *parse_data = (ParseData *) user_data;
1378 gchar *payload;
1380 payload = g_strndup (text, length);
1382 switch (parse_data->state)
1384 case STATE_TITLE:
1385 if (parse_data->current_item)
1387 g_free (parse_data->current_item->title);
1388 parse_data->current_item->title = g_strdup (payload);
1390 else
1392 g_free (parse_data->bookmark_file->title);
1393 parse_data->bookmark_file->title = g_strdup (payload);
1395 break;
1396 case STATE_DESC:
1397 if (parse_data->current_item)
1399 g_free (parse_data->current_item->description);
1400 parse_data->current_item->description = g_strdup (payload);
1402 else
1404 g_free (parse_data->bookmark_file->description);
1405 parse_data->bookmark_file->description = g_strdup (payload);
1407 break;
1408 case STATE_GROUP:
1410 GList *groups;
1412 g_warn_if_fail (parse_data->current_item != NULL);
1414 if (!parse_data->current_item->metadata)
1415 parse_data->current_item->metadata = bookmark_metadata_new ();
1417 groups = parse_data->current_item->metadata->groups;
1418 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1420 break;
1421 case STATE_ROOT:
1422 case STATE_BOOKMARK:
1423 case STATE_INFO:
1424 case STATE_METADATA:
1425 case STATE_APPLICATIONS:
1426 case STATE_APPLICATION:
1427 case STATE_GROUPS:
1428 case STATE_MIME:
1429 case STATE_ICON:
1430 break;
1431 default:
1432 g_warn_if_reached ();
1433 break;
1436 g_free (payload);
1439 static const GMarkupParser markup_parser =
1441 start_element_raw_cb, /* start_element */
1442 end_element_raw_cb, /* end_element */
1443 text_raw_cb, /* text */
1444 NULL, /* passthrough */
1445 NULL
1448 static gboolean
1449 g_bookmark_file_parse (GBookmarkFile *bookmark,
1450 const gchar *buffer,
1451 gsize length,
1452 GError **error)
1454 GMarkupParseContext *context;
1455 ParseData *parse_data;
1456 GError *parse_error, *end_error;
1457 gboolean retval;
1459 g_warn_if_fail (bookmark != NULL);
1461 if (!buffer)
1462 return FALSE;
1464 parse_error = NULL;
1465 end_error = NULL;
1467 if (length == (gsize) -1)
1468 length = strlen (buffer);
1470 parse_data = parse_data_new ();
1471 parse_data->bookmark_file = bookmark;
1473 context = g_markup_parse_context_new (&markup_parser,
1475 parse_data,
1476 (GDestroyNotify) parse_data_free);
1478 retval = g_markup_parse_context_parse (context,
1479 buffer,
1480 length,
1481 &parse_error);
1482 if (!retval)
1483 g_propagate_error (error, parse_error);
1484 else
1486 retval = g_markup_parse_context_end_parse (context, &end_error);
1487 if (!retval)
1488 g_propagate_error (error, end_error);
1491 g_markup_parse_context_free (context);
1493 return retval;
1496 static gchar *
1497 g_bookmark_file_dump (GBookmarkFile *bookmark,
1498 gsize *length,
1499 GError **error)
1501 GString *retval;
1502 gchar *buffer;
1503 GList *l;
1505 retval = g_string_sized_new (4096);
1507 g_string_append (retval,
1508 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1509 #if 0
1510 /* XXX - do we really need the doctype? */
1511 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1512 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1513 " \"" XBEL_DTD_URI "\">\n"
1514 #endif
1515 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1516 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1517 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1519 if (bookmark->title)
1521 gchar *escaped_title;
1523 escaped_title = g_markup_escape_text (bookmark->title, -1);
1525 buffer = g_strconcat (" "
1526 "<" XBEL_TITLE_ELEMENT ">",
1527 escaped_title,
1528 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1530 g_string_append (retval, buffer);
1532 g_free (buffer);
1533 g_free (escaped_title);
1536 if (bookmark->description)
1538 gchar *escaped_desc;
1540 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1542 buffer = g_strconcat (" "
1543 "<" XBEL_DESC_ELEMENT ">",
1544 escaped_desc,
1545 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1546 g_string_append (retval, buffer);
1548 g_free (buffer);
1549 g_free (escaped_desc);
1552 if (!bookmark->items)
1553 goto out;
1554 else
1555 retval = g_string_append (retval, "\n");
1557 /* the items are stored in reverse order */
1558 for (l = g_list_last (bookmark->items);
1559 l != NULL;
1560 l = l->prev)
1562 BookmarkItem *item = (BookmarkItem *) l->data;
1563 gchar *item_dump;
1565 item_dump = bookmark_item_dump (item);
1566 if (!item_dump)
1567 continue;
1569 retval = g_string_append (retval, item_dump);
1571 g_free (item_dump);
1574 out:
1575 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1577 if (length)
1578 *length = retval->len;
1580 return g_string_free (retval, FALSE);
1583 /**************
1584 * Misc *
1585 **************/
1587 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1588 * should free the returned string.
1590 static gchar *
1591 timestamp_to_iso8601 (time_t timestamp)
1593 GTimeVal stamp;
1595 if (timestamp == (time_t) -1)
1596 g_get_current_time (&stamp);
1597 else
1599 stamp.tv_sec = timestamp;
1600 stamp.tv_usec = 0;
1603 return g_time_val_to_iso8601 (&stamp);
1606 static time_t
1607 timestamp_from_iso8601 (const gchar *iso_date)
1609 GTimeVal stamp;
1611 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1612 return (time_t) -1;
1614 return (time_t) stamp.tv_sec;
1617 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1619 /********************
1620 * Public API *
1621 ********************/
1624 * g_bookmark_file_new: (constructor)
1626 * Creates a new empty #GBookmarkFile object.
1628 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1629 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1630 * file.
1632 * Returns: an empty #GBookmarkFile
1634 * Since: 2.12
1636 GBookmarkFile *
1637 g_bookmark_file_new (void)
1639 GBookmarkFile *bookmark;
1641 bookmark = g_new (GBookmarkFile, 1);
1643 g_bookmark_file_init (bookmark);
1645 return bookmark;
1649 * g_bookmark_file_free:
1650 * @bookmark: a #GBookmarkFile
1652 * Frees a #GBookmarkFile.
1654 * Since: 2.12
1656 void
1657 g_bookmark_file_free (GBookmarkFile *bookmark)
1659 if (!bookmark)
1660 return;
1662 g_bookmark_file_clear (bookmark);
1664 g_free (bookmark);
1668 * g_bookmark_file_load_from_data:
1669 * @bookmark: an empty #GBookmarkFile struct
1670 * @data: (array length=length) (element-type guint8): desktop bookmarks
1671 * loaded in memory
1672 * @length: the length of @data in bytes
1673 * @error: return location for a #GError, or %NULL
1675 * Loads a bookmark file from memory into an empty #GBookmarkFile
1676 * structure. If the object cannot be created then @error is set to a
1677 * #GBookmarkFileError.
1679 * Returns: %TRUE if a desktop bookmark could be loaded.
1681 * Since: 2.12
1683 gboolean
1684 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1685 const gchar *data,
1686 gsize length,
1687 GError **error)
1689 GError *parse_error;
1690 gboolean retval;
1692 g_return_val_if_fail (bookmark != NULL, FALSE);
1694 if (length == (gsize) -1)
1695 length = strlen (data);
1697 if (bookmark->items)
1699 g_bookmark_file_clear (bookmark);
1700 g_bookmark_file_init (bookmark);
1703 parse_error = NULL;
1704 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1706 if (!retval)
1707 g_propagate_error (error, parse_error);
1709 return retval;
1713 * g_bookmark_file_load_from_file:
1714 * @bookmark: an empty #GBookmarkFile struct
1715 * @filename: (type filename): the path of a filename to load, in the
1716 * GLib file name encoding
1717 * @error: return location for a #GError, or %NULL
1719 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1720 * If the file could not be loaded then @error is set to either a #GFileError
1721 * or #GBookmarkFileError.
1723 * Returns: %TRUE if a desktop bookmark file could be loaded
1725 * Since: 2.12
1727 gboolean
1728 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1729 const gchar *filename,
1730 GError **error)
1732 gboolean ret = FALSE;
1733 gchar *buffer = NULL;
1734 gsize len;
1736 g_return_val_if_fail (bookmark != NULL, FALSE);
1737 g_return_val_if_fail (filename != NULL, FALSE);
1739 if (!g_file_get_contents (filename, &buffer, &len, error))
1740 goto out;
1742 if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1743 goto out;
1745 ret = TRUE;
1746 out:
1747 g_free (buffer);
1748 return ret;
1752 /* Iterates through all the directories in *dirs trying to
1753 * find file. When it successfully locates file, returns a
1754 * string its absolute path. It also leaves the unchecked
1755 * directories in *dirs. You should free the returned string
1757 * Adapted from gkeyfile.c
1759 static gchar *
1760 find_file_in_data_dirs (const gchar *file,
1761 gchar ***dirs,
1762 GError **error)
1764 gchar **data_dirs, *data_dir, *path;
1766 path = NULL;
1768 if (dirs == NULL)
1769 return NULL;
1771 data_dirs = *dirs;
1772 path = NULL;
1773 while (data_dirs && (data_dir = *data_dirs) && !path)
1775 gchar *candidate_file, *sub_dir;
1777 candidate_file = (gchar *) file;
1778 sub_dir = g_strdup ("");
1779 while (candidate_file != NULL && !path)
1781 gchar *p;
1783 path = g_build_filename (data_dir, sub_dir,
1784 candidate_file, NULL);
1786 candidate_file = strchr (candidate_file, '-');
1788 if (candidate_file == NULL)
1789 break;
1791 candidate_file++;
1793 g_free (sub_dir);
1794 sub_dir = g_strndup (file, candidate_file - file - 1);
1796 for (p = sub_dir; *p != '\0'; p++)
1798 if (*p == '-')
1799 *p = G_DIR_SEPARATOR;
1802 g_free (sub_dir);
1803 data_dirs++;
1806 *dirs = data_dirs;
1808 if (!path)
1810 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1811 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1812 _("No valid bookmark file found in data dirs"));
1814 return NULL;
1817 return path;
1822 * g_bookmark_file_load_from_data_dirs:
1823 * @bookmark: a #GBookmarkFile
1824 * @file: (type filename): a relative path to a filename to open and parse
1825 * @full_path: (out) (optional) (type filename): return location for a string
1826 * containing the full path of the file, or %NULL
1827 * @error: return location for a #GError, or %NULL
1829 * This function looks for a desktop bookmark file named @file in the
1830 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1831 * loads the file into @bookmark and returns the file's full path in
1832 * @full_path. If the file could not be loaded then @error is
1833 * set to either a #GFileError or #GBookmarkFileError.
1835 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1837 * Since: 2.12
1839 gboolean
1840 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1841 const gchar *file,
1842 gchar **full_path,
1843 GError **error)
1845 GError *file_error = NULL;
1846 gchar **all_data_dirs, **data_dirs;
1847 const gchar *user_data_dir;
1848 const gchar * const * system_data_dirs;
1849 gsize i, j;
1850 gchar *output_path;
1851 gboolean found_file;
1853 g_return_val_if_fail (bookmark != NULL, FALSE);
1854 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1856 user_data_dir = g_get_user_data_dir ();
1857 system_data_dirs = g_get_system_data_dirs ();
1858 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1860 i = 0;
1861 all_data_dirs[i++] = g_strdup (user_data_dir);
1863 j = 0;
1864 while (system_data_dirs[j] != NULL)
1865 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1867 found_file = FALSE;
1868 data_dirs = all_data_dirs;
1869 output_path = NULL;
1870 while (*data_dirs != NULL && !found_file)
1872 g_free (output_path);
1874 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1876 if (file_error)
1878 g_propagate_error (error, file_error);
1879 break;
1882 found_file = g_bookmark_file_load_from_file (bookmark,
1883 output_path,
1884 &file_error);
1885 if (file_error)
1887 g_propagate_error (error, file_error);
1888 break;
1892 if (found_file && full_path)
1893 *full_path = output_path;
1894 else
1895 g_free (output_path);
1897 g_strfreev (all_data_dirs);
1899 return found_file;
1904 * g_bookmark_file_to_data:
1905 * @bookmark: a #GBookmarkFile
1906 * @length: (out) (optional): return location for the length of the returned string, or %NULL
1907 * @error: return location for a #GError, or %NULL
1909 * This function outputs @bookmark as a string.
1911 * Returns: (array length=length) (element-type guint8):
1912 * a newly allocated string holding the contents of the #GBookmarkFile
1914 * Since: 2.12
1916 gchar *
1917 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1918 gsize *length,
1919 GError **error)
1921 GError *write_error = NULL;
1922 gchar *retval;
1924 g_return_val_if_fail (bookmark != NULL, NULL);
1926 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1927 if (write_error)
1929 g_propagate_error (error, write_error);
1931 return NULL;
1934 return retval;
1938 * g_bookmark_file_to_file:
1939 * @bookmark: a #GBookmarkFile
1940 * @filename: (type filename): path of the output file
1941 * @error: return location for a #GError, or %NULL
1943 * This function outputs @bookmark into a file. The write process is
1944 * guaranteed to be atomic by using g_file_set_contents() internally.
1946 * Returns: %TRUE if the file was successfully written.
1948 * Since: 2.12
1950 gboolean
1951 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1952 const gchar *filename,
1953 GError **error)
1955 gchar *data;
1956 GError *data_error, *write_error;
1957 gsize len;
1958 gboolean retval;
1960 g_return_val_if_fail (bookmark != NULL, FALSE);
1961 g_return_val_if_fail (filename != NULL, FALSE);
1963 data_error = NULL;
1964 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1965 if (data_error)
1967 g_propagate_error (error, data_error);
1969 return FALSE;
1972 write_error = NULL;
1973 g_file_set_contents (filename, data, len, &write_error);
1974 if (write_error)
1976 g_propagate_error (error, write_error);
1978 retval = FALSE;
1980 else
1981 retval = TRUE;
1983 g_free (data);
1985 return retval;
1988 static BookmarkItem *
1989 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1990 const gchar *uri)
1992 g_warn_if_fail (bookmark != NULL && uri != NULL);
1994 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1997 /* this function adds a new item to the list */
1998 static void
1999 g_bookmark_file_add_item (GBookmarkFile *bookmark,
2000 BookmarkItem *item,
2001 GError **error)
2003 g_warn_if_fail (bookmark != NULL);
2004 g_warn_if_fail (item != NULL);
2006 /* this should never happen; and if it does, then we are
2007 * screwing up something big time.
2009 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2011 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2012 G_BOOKMARK_FILE_ERROR_INVALID_URI,
2013 _("A bookmark for URI “%s” already exists"),
2014 item->uri);
2015 return;
2018 bookmark->items = g_list_prepend (bookmark->items, item);
2020 g_hash_table_replace (bookmark->items_by_uri,
2021 item->uri,
2022 item);
2024 if (item->added == (time_t) -1)
2025 item->added = time (NULL);
2027 if (item->modified == (time_t) -1)
2028 item->modified = time (NULL);
2032 * g_bookmark_file_remove_item:
2033 * @bookmark: a #GBookmarkFile
2034 * @uri: a valid URI
2035 * @error: return location for a #GError, or %NULL
2037 * Removes the bookmark for @uri from the bookmark file @bookmark.
2039 * Returns: %TRUE if the bookmark was removed successfully.
2041 * Since: 2.12
2043 gboolean
2044 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2045 const gchar *uri,
2046 GError **error)
2048 BookmarkItem *item;
2050 g_return_val_if_fail (bookmark != NULL, FALSE);
2051 g_return_val_if_fail (uri != NULL, FALSE);
2053 item = g_bookmark_file_lookup_item (bookmark, uri);
2055 if (!item)
2057 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2058 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2059 _("No bookmark found for URI “%s”"),
2060 uri);
2061 return FALSE;
2064 bookmark->items = g_list_remove (bookmark->items, item);
2065 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2067 bookmark_item_free (item);
2069 return TRUE;
2073 * g_bookmark_file_has_item:
2074 * @bookmark: a #GBookmarkFile
2075 * @uri: a valid URI
2077 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2079 * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2081 * Since: 2.12
2083 gboolean
2084 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2085 const gchar *uri)
2087 g_return_val_if_fail (bookmark != NULL, FALSE);
2088 g_return_val_if_fail (uri != NULL, FALSE);
2090 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2094 * g_bookmark_file_get_uris:
2095 * @bookmark: a #GBookmarkFile
2096 * @length: (out) (optional): return location for the number of returned URIs, or %NULL
2098 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2099 * The array of returned URIs will be %NULL-terminated, so @length may
2100 * optionally be %NULL.
2102 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2103 * Use g_strfreev() to free it.
2105 * Since: 2.12
2107 gchar **
2108 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2109 gsize *length)
2111 GList *l;
2112 gchar **uris;
2113 gsize i, n_items;
2115 g_return_val_if_fail (bookmark != NULL, NULL);
2117 n_items = g_list_length (bookmark->items);
2118 uris = g_new0 (gchar *, n_items + 1);
2120 /* the items are stored in reverse order, so we walk the list backward */
2121 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2123 BookmarkItem *item = (BookmarkItem *) l->data;
2125 g_warn_if_fail (item != NULL);
2127 uris[i++] = g_strdup (item->uri);
2129 uris[i] = NULL;
2131 if (length)
2132 *length = i;
2134 return uris;
2138 * g_bookmark_file_set_title:
2139 * @bookmark: a #GBookmarkFile
2140 * @uri: (nullable): a valid URI or %NULL
2141 * @title: a UTF-8 encoded string
2143 * Sets @title as the title of the bookmark for @uri inside the
2144 * bookmark file @bookmark.
2146 * If @uri is %NULL, the title of @bookmark is set.
2148 * If a bookmark for @uri cannot be found then it is created.
2150 * Since: 2.12
2152 void
2153 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2154 const gchar *uri,
2155 const gchar *title)
2157 g_return_if_fail (bookmark != NULL);
2159 if (!uri)
2161 g_free (bookmark->title);
2162 bookmark->title = g_strdup (title);
2164 else
2166 BookmarkItem *item;
2168 item = g_bookmark_file_lookup_item (bookmark, uri);
2169 if (!item)
2171 item = bookmark_item_new (uri);
2172 g_bookmark_file_add_item (bookmark, item, NULL);
2175 g_free (item->title);
2176 item->title = g_strdup (title);
2178 item->modified = time (NULL);
2183 * g_bookmark_file_get_title:
2184 * @bookmark: a #GBookmarkFile
2185 * @uri: (nullable): a valid URI or %NULL
2186 * @error: return location for a #GError, or %NULL
2188 * Returns the title of the bookmark for @uri.
2190 * If @uri is %NULL, the title of @bookmark is returned.
2192 * In the event the URI cannot be found, %NULL is returned and
2193 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2195 * Returns: a newly allocated string or %NULL if the specified
2196 * URI cannot be found.
2198 * Since: 2.12
2200 gchar *
2201 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2202 const gchar *uri,
2203 GError **error)
2205 BookmarkItem *item;
2207 g_return_val_if_fail (bookmark != NULL, NULL);
2209 if (!uri)
2210 return g_strdup (bookmark->title);
2212 item = g_bookmark_file_lookup_item (bookmark, uri);
2213 if (!item)
2215 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2216 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2217 _("No bookmark found for URI “%s”"),
2218 uri);
2219 return NULL;
2222 return g_strdup (item->title);
2226 * g_bookmark_file_set_description:
2227 * @bookmark: a #GBookmarkFile
2228 * @uri: (nullable): a valid URI or %NULL
2229 * @description: a string
2231 * Sets @description as the description of the bookmark for @uri.
2233 * If @uri is %NULL, the description of @bookmark is set.
2235 * If a bookmark for @uri cannot be found then it is created.
2237 * Since: 2.12
2239 void
2240 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2241 const gchar *uri,
2242 const gchar *description)
2244 g_return_if_fail (bookmark != NULL);
2246 if (!uri)
2248 g_free (bookmark->description);
2249 bookmark->description = g_strdup (description);
2251 else
2253 BookmarkItem *item;
2255 item = g_bookmark_file_lookup_item (bookmark, uri);
2256 if (!item)
2258 item = bookmark_item_new (uri);
2259 g_bookmark_file_add_item (bookmark, item, NULL);
2262 g_free (item->description);
2263 item->description = g_strdup (description);
2265 item->modified = time (NULL);
2270 * g_bookmark_file_get_description:
2271 * @bookmark: a #GBookmarkFile
2272 * @uri: a valid URI
2273 * @error: return location for a #GError, or %NULL
2275 * Retrieves the description of the bookmark for @uri.
2277 * In the event the URI cannot be found, %NULL is returned and
2278 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2280 * Returns: a newly allocated string or %NULL if the specified
2281 * URI cannot be found.
2283 * Since: 2.12
2285 gchar *
2286 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2287 const gchar *uri,
2288 GError **error)
2290 BookmarkItem *item;
2292 g_return_val_if_fail (bookmark != NULL, NULL);
2294 if (!uri)
2295 return g_strdup (bookmark->description);
2297 item = g_bookmark_file_lookup_item (bookmark, uri);
2298 if (!item)
2300 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2301 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2302 _("No bookmark found for URI “%s”"),
2303 uri);
2304 return NULL;
2307 return g_strdup (item->description);
2311 * g_bookmark_file_set_mime_type:
2312 * @bookmark: a #GBookmarkFile
2313 * @uri: a valid URI
2314 * @mime_type: a MIME type
2316 * Sets @mime_type as the MIME type of the bookmark for @uri.
2318 * If a bookmark for @uri cannot be found then it is created.
2320 * Since: 2.12
2322 void
2323 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2324 const gchar *uri,
2325 const gchar *mime_type)
2327 BookmarkItem *item;
2329 g_return_if_fail (bookmark != NULL);
2330 g_return_if_fail (uri != NULL);
2331 g_return_if_fail (mime_type != NULL);
2333 item = g_bookmark_file_lookup_item (bookmark, uri);
2334 if (!item)
2336 item = bookmark_item_new (uri);
2337 g_bookmark_file_add_item (bookmark, item, NULL);
2340 if (!item->metadata)
2341 item->metadata = bookmark_metadata_new ();
2343 g_free (item->metadata->mime_type);
2345 item->metadata->mime_type = g_strdup (mime_type);
2346 item->modified = time (NULL);
2350 * g_bookmark_file_get_mime_type:
2351 * @bookmark: a #GBookmarkFile
2352 * @uri: a valid URI
2353 * @error: return location for a #GError, or %NULL
2355 * Retrieves the MIME type of the resource pointed by @uri.
2357 * In the event the URI cannot be found, %NULL is returned and
2358 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2359 * event that the MIME type cannot be found, %NULL is returned and
2360 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2362 * Returns: a newly allocated string or %NULL if the specified
2363 * URI cannot be found.
2365 * Since: 2.12
2367 gchar *
2368 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2369 const gchar *uri,
2370 GError **error)
2372 BookmarkItem *item;
2374 g_return_val_if_fail (bookmark != NULL, NULL);
2375 g_return_val_if_fail (uri != NULL, NULL);
2377 item = g_bookmark_file_lookup_item (bookmark, uri);
2378 if (!item)
2380 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2381 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2382 _("No bookmark found for URI “%s”"),
2383 uri);
2384 return NULL;
2387 if (!item->metadata)
2389 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2390 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2391 _("No MIME type defined in the bookmark for URI “%s”"),
2392 uri);
2393 return NULL;
2396 return g_strdup (item->metadata->mime_type);
2400 * g_bookmark_file_set_is_private:
2401 * @bookmark: a #GBookmarkFile
2402 * @uri: a valid URI
2403 * @is_private: %TRUE if the bookmark should be marked as private
2405 * Sets the private flag of the bookmark for @uri.
2407 * If a bookmark for @uri cannot be found then it is created.
2409 * Since: 2.12
2411 void
2412 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2413 const gchar *uri,
2414 gboolean is_private)
2416 BookmarkItem *item;
2418 g_return_if_fail (bookmark != NULL);
2419 g_return_if_fail (uri != NULL);
2421 item = g_bookmark_file_lookup_item (bookmark, uri);
2422 if (!item)
2424 item = bookmark_item_new (uri);
2425 g_bookmark_file_add_item (bookmark, item, NULL);
2428 if (!item->metadata)
2429 item->metadata = bookmark_metadata_new ();
2431 item->metadata->is_private = (is_private == TRUE);
2432 item->modified = time (NULL);
2436 * g_bookmark_file_get_is_private:
2437 * @bookmark: a #GBookmarkFile
2438 * @uri: a valid URI
2439 * @error: return location for a #GError, or %NULL
2441 * Gets whether the private flag of the bookmark for @uri is set.
2443 * In the event the URI cannot be found, %FALSE is returned and
2444 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2445 * event that the private flag cannot be found, %FALSE is returned and
2446 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2448 * Returns: %TRUE if the private flag is set, %FALSE otherwise.
2450 * Since: 2.12
2452 gboolean
2453 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2454 const gchar *uri,
2455 GError **error)
2457 BookmarkItem *item;
2459 g_return_val_if_fail (bookmark != NULL, FALSE);
2460 g_return_val_if_fail (uri != NULL, FALSE);
2462 item = g_bookmark_file_lookup_item (bookmark, uri);
2463 if (!item)
2465 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2466 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2467 _("No bookmark found for URI “%s”"),
2468 uri);
2469 return FALSE;
2472 if (!item->metadata)
2474 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2475 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2476 _("No private flag has been defined in bookmark for URI “%s”"),
2477 uri);
2478 return FALSE;
2481 return item->metadata->is_private;
2485 * g_bookmark_file_set_added:
2486 * @bookmark: a #GBookmarkFile
2487 * @uri: a valid URI
2488 * @added: a timestamp or -1 to use the current time
2490 * Sets the time the bookmark for @uri was added into @bookmark.
2492 * If no bookmark for @uri is found then it is created.
2494 * Since: 2.12
2496 void
2497 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2498 const gchar *uri,
2499 time_t added)
2501 BookmarkItem *item;
2503 g_return_if_fail (bookmark != NULL);
2504 g_return_if_fail (uri != NULL);
2506 item = g_bookmark_file_lookup_item (bookmark, uri);
2507 if (!item)
2509 item = bookmark_item_new (uri);
2510 g_bookmark_file_add_item (bookmark, item, NULL);
2513 if (added == (time_t) -1)
2514 time (&added);
2516 item->added = added;
2517 item->modified = added;
2521 * g_bookmark_file_get_added:
2522 * @bookmark: a #GBookmarkFile
2523 * @uri: a valid URI
2524 * @error: return location for a #GError, or %NULL
2526 * Gets the time the bookmark for @uri was added to @bookmark
2528 * In the event the URI cannot be found, -1 is returned and
2529 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2531 * Returns: a timestamp
2533 * Since: 2.12
2535 time_t
2536 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2537 const gchar *uri,
2538 GError **error)
2540 BookmarkItem *item;
2542 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2543 g_return_val_if_fail (uri != NULL, (time_t) -1);
2545 item = g_bookmark_file_lookup_item (bookmark, uri);
2546 if (!item)
2548 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2549 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2550 _("No bookmark found for URI “%s”"),
2551 uri);
2552 return (time_t) -1;
2555 return item->added;
2559 * g_bookmark_file_set_modified:
2560 * @bookmark: a #GBookmarkFile
2561 * @uri: a valid URI
2562 * @modified: a timestamp or -1 to use the current time
2564 * Sets the last time the bookmark for @uri was last modified.
2566 * If no bookmark for @uri is found then it is created.
2568 * The "modified" time should only be set when the bookmark's meta-data
2569 * was actually changed. Every function of #GBookmarkFile that
2570 * modifies a bookmark also changes the modification time, except for
2571 * g_bookmark_file_set_visited().
2573 * Since: 2.12
2575 void
2576 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2577 const gchar *uri,
2578 time_t modified)
2580 BookmarkItem *item;
2582 g_return_if_fail (bookmark != NULL);
2583 g_return_if_fail (uri != NULL);
2585 item = g_bookmark_file_lookup_item (bookmark, uri);
2586 if (!item)
2588 item = bookmark_item_new (uri);
2589 g_bookmark_file_add_item (bookmark, item, NULL);
2592 if (modified == (time_t) -1)
2593 time (&modified);
2595 item->modified = modified;
2599 * g_bookmark_file_get_modified:
2600 * @bookmark: a #GBookmarkFile
2601 * @uri: a valid URI
2602 * @error: return location for a #GError, or %NULL
2604 * Gets the time when the bookmark for @uri was last modified.
2606 * In the event the URI cannot be found, -1 is returned and
2607 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2609 * Returns: a timestamp
2611 * Since: 2.12
2613 time_t
2614 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2615 const gchar *uri,
2616 GError **error)
2618 BookmarkItem *item;
2620 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2621 g_return_val_if_fail (uri != NULL, (time_t) -1);
2623 item = g_bookmark_file_lookup_item (bookmark, uri);
2624 if (!item)
2626 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2627 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2628 _("No bookmark found for URI “%s”"),
2629 uri);
2630 return (time_t) -1;
2633 return item->modified;
2637 * g_bookmark_file_set_visited:
2638 * @bookmark: a #GBookmarkFile
2639 * @uri: a valid URI
2640 * @visited: a timestamp or -1 to use the current time
2642 * Sets the time the bookmark for @uri was last visited.
2644 * If no bookmark for @uri is found then it is created.
2646 * The "visited" time should only be set if the bookmark was launched,
2647 * either using the command line retrieved by g_bookmark_file_get_app_info()
2648 * or by the default application for the bookmark's MIME type, retrieved
2649 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2650 * does not affect the "modified" time.
2652 * Since: 2.12
2654 void
2655 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2656 const gchar *uri,
2657 time_t visited)
2659 BookmarkItem *item;
2661 g_return_if_fail (bookmark != NULL);
2662 g_return_if_fail (uri != NULL);
2664 item = g_bookmark_file_lookup_item (bookmark, uri);
2665 if (!item)
2667 item = bookmark_item_new (uri);
2668 g_bookmark_file_add_item (bookmark, item, NULL);
2671 if (visited == (time_t) -1)
2672 time (&visited);
2674 item->visited = visited;
2678 * g_bookmark_file_get_visited:
2679 * @bookmark: a #GBookmarkFile
2680 * @uri: a valid URI
2681 * @error: return location for a #GError, or %NULL
2683 * Gets the time the bookmark for @uri was last visited.
2685 * In the event the URI cannot be found, -1 is returned and
2686 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2688 * Returns: a timestamp.
2690 * Since: 2.12
2692 time_t
2693 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2694 const gchar *uri,
2695 GError **error)
2697 BookmarkItem *item;
2699 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2700 g_return_val_if_fail (uri != NULL, (time_t) -1);
2702 item = g_bookmark_file_lookup_item (bookmark, uri);
2703 if (!item)
2705 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2706 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2707 _("No bookmark found for URI “%s”"),
2708 uri);
2709 return (time_t) -1;
2712 return item->visited;
2716 * g_bookmark_file_has_group:
2717 * @bookmark: a #GBookmarkFile
2718 * @uri: a valid URI
2719 * @group: the group name to be searched
2720 * @error: return location for a #GError, or %NULL
2722 * Checks whether @group appears in the list of groups to which
2723 * the bookmark for @uri belongs to.
2725 * In the event the URI cannot be found, %FALSE is returned and
2726 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2728 * Returns: %TRUE if @group was found.
2730 * Since: 2.12
2732 gboolean
2733 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2734 const gchar *uri,
2735 const gchar *group,
2736 GError **error)
2738 BookmarkItem *item;
2739 GList *l;
2741 g_return_val_if_fail (bookmark != NULL, FALSE);
2742 g_return_val_if_fail (uri != NULL, FALSE);
2744 item = g_bookmark_file_lookup_item (bookmark, uri);
2745 if (!item)
2747 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2748 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2749 _("No bookmark found for URI “%s”"),
2750 uri);
2751 return FALSE;
2754 if (!item->metadata)
2755 return FALSE;
2757 for (l = item->metadata->groups; l != NULL; l = l->next)
2759 if (strcmp (l->data, group) == 0)
2760 return TRUE;
2763 return FALSE;
2768 * g_bookmark_file_add_group:
2769 * @bookmark: a #GBookmarkFile
2770 * @uri: a valid URI
2771 * @group: the group name to be added
2773 * Adds @group to the list of groups to which the bookmark for @uri
2774 * belongs to.
2776 * If no bookmark for @uri is found then it is created.
2778 * Since: 2.12
2780 void
2781 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2782 const gchar *uri,
2783 const gchar *group)
2785 BookmarkItem *item;
2787 g_return_if_fail (bookmark != NULL);
2788 g_return_if_fail (uri != NULL);
2789 g_return_if_fail (group != NULL && group[0] != '\0');
2791 item = g_bookmark_file_lookup_item (bookmark, uri);
2792 if (!item)
2794 item = bookmark_item_new (uri);
2795 g_bookmark_file_add_item (bookmark, item, NULL);
2798 if (!item->metadata)
2799 item->metadata = bookmark_metadata_new ();
2801 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2803 item->metadata->groups = g_list_prepend (item->metadata->groups,
2804 g_strdup (group));
2806 item->modified = time (NULL);
2811 * g_bookmark_file_remove_group:
2812 * @bookmark: a #GBookmarkFile
2813 * @uri: a valid URI
2814 * @group: the group name to be removed
2815 * @error: return location for a #GError, or %NULL
2817 * Removes @group from the list of groups to which the bookmark
2818 * for @uri belongs to.
2820 * In the event the URI cannot be found, %FALSE is returned and
2821 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2822 * In the event no group was defined, %FALSE is returned and
2823 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2825 * Returns: %TRUE if @group was successfully removed.
2827 * Since: 2.12
2829 gboolean
2830 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2831 const gchar *uri,
2832 const gchar *group,
2833 GError **error)
2835 BookmarkItem *item;
2836 GList *l;
2838 g_return_val_if_fail (bookmark != NULL, FALSE);
2839 g_return_val_if_fail (uri != NULL, FALSE);
2841 item = g_bookmark_file_lookup_item (bookmark, uri);
2842 if (!item)
2844 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2845 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2846 _("No bookmark found for URI “%s”"),
2847 uri);
2848 return FALSE;
2851 if (!item->metadata)
2853 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2854 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2855 _("No groups set in bookmark for URI “%s”"),
2856 uri);
2857 return FALSE;
2860 for (l = item->metadata->groups; l != NULL; l = l->next)
2862 if (strcmp (l->data, group) == 0)
2864 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2865 g_free (l->data);
2866 g_list_free_1 (l);
2868 item->modified = time (NULL);
2870 return TRUE;
2874 return FALSE;
2878 * g_bookmark_file_set_groups:
2879 * @bookmark: a #GBookmarkFile
2880 * @uri: an item's URI
2881 * @groups: (nullable) (array length=length) (element-type utf8): an array of
2882 * group names, or %NULL to remove all groups
2883 * @length: number of group name values in @groups
2885 * Sets a list of group names for the item with URI @uri. Each previously
2886 * set group name list is removed.
2888 * If @uri cannot be found then an item for it is created.
2890 * Since: 2.12
2892 void
2893 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2894 const gchar *uri,
2895 const gchar **groups,
2896 gsize length)
2898 BookmarkItem *item;
2899 gsize i;
2901 g_return_if_fail (bookmark != NULL);
2902 g_return_if_fail (uri != NULL);
2903 g_return_if_fail (groups != NULL);
2905 item = g_bookmark_file_lookup_item (bookmark, uri);
2906 if (!item)
2908 item = bookmark_item_new (uri);
2909 g_bookmark_file_add_item (bookmark, item, NULL);
2912 if (!item->metadata)
2913 item->metadata = bookmark_metadata_new ();
2915 g_list_free_full (item->metadata->groups, g_free);
2916 item->metadata->groups = NULL;
2918 if (groups)
2920 for (i = 0; i < length && groups[i] != NULL; i++)
2921 item->metadata->groups = g_list_append (item->metadata->groups,
2922 g_strdup (groups[i]));
2925 item->modified = time (NULL);
2929 * g_bookmark_file_get_groups:
2930 * @bookmark: a #GBookmarkFile
2931 * @uri: a valid URI
2932 * @length: (out) (optional): return location for the length of the returned string, or %NULL
2933 * @error: return location for a #GError, or %NULL
2935 * Retrieves the list of group names of the bookmark for @uri.
2937 * In the event the URI cannot be found, %NULL is returned and
2938 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2940 * The returned array is %NULL terminated, so @length may optionally
2941 * be %NULL.
2943 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
2944 * Use g_strfreev() to free it.
2946 * Since: 2.12
2948 gchar **
2949 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2950 const gchar *uri,
2951 gsize *length,
2952 GError **error)
2954 BookmarkItem *item;
2955 GList *l;
2956 gsize len, i;
2957 gchar **retval;
2959 g_return_val_if_fail (bookmark != NULL, NULL);
2960 g_return_val_if_fail (uri != NULL, NULL);
2962 item = g_bookmark_file_lookup_item (bookmark, uri);
2963 if (!item)
2965 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2966 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2967 _("No bookmark found for URI “%s”"),
2968 uri);
2969 return NULL;
2972 if (!item->metadata)
2974 if (length)
2975 *length = 0;
2977 return NULL;
2980 len = g_list_length (item->metadata->groups);
2981 retval = g_new0 (gchar *, len + 1);
2982 for (l = g_list_last (item->metadata->groups), i = 0;
2983 l != NULL;
2984 l = l->prev)
2986 gchar *group_name = (gchar *) l->data;
2988 g_warn_if_fail (group_name != NULL);
2990 retval[i++] = g_strdup (group_name);
2992 retval[i] = NULL;
2994 if (length)
2995 *length = len;
2997 return retval;
3001 * g_bookmark_file_add_application:
3002 * @bookmark: a #GBookmarkFile
3003 * @uri: a valid URI
3004 * @name: (nullable): the name of the application registering the bookmark
3005 * or %NULL
3006 * @exec: (nullable): command line to be used to launch the bookmark or %NULL
3008 * Adds the application with @name and @exec to the list of
3009 * applications that have registered a bookmark for @uri into
3010 * @bookmark.
3012 * Every bookmark inside a #GBookmarkFile must have at least an
3013 * application registered. Each application must provide a name, a
3014 * command line useful for launching the bookmark, the number of times
3015 * the bookmark has been registered by the application and the last
3016 * time the application registered this bookmark.
3018 * If @name is %NULL, the name of the application will be the
3019 * same returned by g_get_application_name(); if @exec is %NULL, the
3020 * command line will be a composition of the program name as
3021 * returned by g_get_prgname() and the "\%u" modifier, which will be
3022 * expanded to the bookmark's URI.
3024 * This function will automatically take care of updating the
3025 * registrations count and timestamping in case an application
3026 * with the same @name had already registered a bookmark for
3027 * @uri inside @bookmark.
3029 * If no bookmark for @uri is found, one is created.
3031 * Since: 2.12
3033 void
3034 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3035 const gchar *uri,
3036 const gchar *name,
3037 const gchar *exec)
3039 BookmarkItem *item;
3040 gchar *app_name, *app_exec;
3042 g_return_if_fail (bookmark != NULL);
3043 g_return_if_fail (uri != NULL);
3045 item = g_bookmark_file_lookup_item (bookmark, uri);
3046 if (!item)
3048 item = bookmark_item_new (uri);
3049 g_bookmark_file_add_item (bookmark, item, NULL);
3052 if (name && name[0] != '\0')
3053 app_name = g_strdup (name);
3054 else
3055 app_name = g_strdup (g_get_application_name ());
3057 if (exec && exec[0] != '\0')
3058 app_exec = g_strdup (exec);
3059 else
3060 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3062 g_bookmark_file_set_app_info (bookmark, uri,
3063 app_name,
3064 app_exec,
3066 (time_t) -1,
3067 NULL);
3069 g_free (app_exec);
3070 g_free (app_name);
3074 * g_bookmark_file_remove_application:
3075 * @bookmark: a #GBookmarkFile
3076 * @uri: a valid URI
3077 * @name: the name of the application
3078 * @error: return location for a #GError or %NULL
3080 * Removes application registered with @name from the list of applications
3081 * that have registered a bookmark for @uri inside @bookmark.
3083 * In the event the URI cannot be found, %FALSE is returned and
3084 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3085 * In the event that no application with name @app_name has registered
3086 * a bookmark for @uri, %FALSE is returned and error is set to
3087 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3089 * Returns: %TRUE if the application was successfully removed.
3091 * Since: 2.12
3093 gboolean
3094 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3095 const gchar *uri,
3096 const gchar *name,
3097 GError **error)
3099 GError *set_error;
3100 gboolean retval;
3102 g_return_val_if_fail (bookmark != NULL, FALSE);
3103 g_return_val_if_fail (uri != NULL, FALSE);
3104 g_return_val_if_fail (name != NULL, FALSE);
3106 set_error = NULL;
3107 retval = g_bookmark_file_set_app_info (bookmark, uri,
3108 name,
3111 (time_t) -1,
3112 &set_error);
3113 if (set_error)
3115 g_propagate_error (error, set_error);
3117 return FALSE;
3120 return retval;
3124 * g_bookmark_file_has_application:
3125 * @bookmark: a #GBookmarkFile
3126 * @uri: a valid URI
3127 * @name: the name of the application
3128 * @error: return location for a #GError or %NULL
3130 * Checks whether the bookmark for @uri inside @bookmark has been
3131 * registered by application @name.
3133 * In the event the URI cannot be found, %FALSE is returned and
3134 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3136 * Returns: %TRUE if the application @name was found
3138 * Since: 2.12
3140 gboolean
3141 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3142 const gchar *uri,
3143 const gchar *name,
3144 GError **error)
3146 BookmarkItem *item;
3148 g_return_val_if_fail (bookmark != NULL, FALSE);
3149 g_return_val_if_fail (uri != NULL, FALSE);
3150 g_return_val_if_fail (name != NULL, FALSE);
3152 item = g_bookmark_file_lookup_item (bookmark, uri);
3153 if (!item)
3155 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3156 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3157 _("No bookmark found for URI “%s”"),
3158 uri);
3159 return FALSE;
3162 return (NULL != bookmark_item_lookup_app_info (item, name));
3166 * g_bookmark_file_set_app_info:
3167 * @bookmark: a #GBookmarkFile
3168 * @uri: a valid URI
3169 * @name: an application's name
3170 * @exec: an application's command line
3171 * @count: the number of registrations done for this application
3172 * @stamp: the time of the last registration for this application
3173 * @error: return location for a #GError or %NULL
3175 * Sets the meta-data of application @name inside the list of
3176 * applications that have registered a bookmark for @uri inside
3177 * @bookmark.
3179 * You should rarely use this function; use g_bookmark_file_add_application()
3180 * and g_bookmark_file_remove_application() instead.
3182 * @name can be any UTF-8 encoded string used to identify an
3183 * application.
3184 * @exec can have one of these two modifiers: "\%f", which will
3185 * be expanded as the local file name retrieved from the bookmark's
3186 * URI; "\%u", which will be expanded as the bookmark's URI.
3187 * The expansion is done automatically when retrieving the stored
3188 * command line using the g_bookmark_file_get_app_info() function.
3189 * @count is the number of times the application has registered the
3190 * bookmark; if is < 0, the current registration count will be increased
3191 * by one, if is 0, the application with @name will be removed from
3192 * the list of registered applications.
3193 * @stamp is the Unix time of the last registration; if it is -1, the
3194 * current time will be used.
3196 * If you try to remove an application by setting its registration count to
3197 * zero, and no bookmark for @uri is found, %FALSE is returned and
3198 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3199 * in the event that no application @name has registered a bookmark
3200 * for @uri, %FALSE is returned and error is set to
3201 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3202 * for @uri is found, one is created.
3204 * Returns: %TRUE if the application's meta-data was successfully
3205 * changed.
3207 * Since: 2.12
3209 gboolean
3210 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3211 const gchar *uri,
3212 const gchar *name,
3213 const gchar *exec,
3214 gint count,
3215 time_t stamp,
3216 GError **error)
3218 BookmarkItem *item;
3219 BookmarkAppInfo *ai;
3221 g_return_val_if_fail (bookmark != NULL, FALSE);
3222 g_return_val_if_fail (uri != NULL, FALSE);
3223 g_return_val_if_fail (name != NULL, FALSE);
3224 g_return_val_if_fail (exec != NULL, FALSE);
3226 item = g_bookmark_file_lookup_item (bookmark, uri);
3227 if (!item)
3229 if (count == 0)
3231 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3232 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3233 _("No bookmark found for URI “%s”"),
3234 uri);
3235 return FALSE;
3237 else
3239 item = bookmark_item_new (uri);
3240 g_bookmark_file_add_item (bookmark, item, NULL);
3244 if (!item->metadata)
3245 item->metadata = bookmark_metadata_new ();
3247 ai = bookmark_item_lookup_app_info (item, name);
3248 if (!ai)
3250 if (count == 0)
3252 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3253 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3254 _("No application with name “%s” registered a bookmark for “%s”"),
3255 name,
3256 uri);
3257 return FALSE;
3259 else
3261 ai = bookmark_app_info_new (name);
3263 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3264 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3268 if (count == 0)
3270 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3271 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3272 bookmark_app_info_free (ai);
3274 item->modified = time (NULL);
3276 return TRUE;
3278 else if (count > 0)
3279 ai->count = count;
3280 else
3281 ai->count += 1;
3283 if (stamp != (time_t) -1)
3284 ai->stamp = stamp;
3285 else
3286 ai->stamp = time (NULL);
3288 if (exec && exec[0] != '\0')
3290 g_free (ai->exec);
3291 ai->exec = g_shell_quote (exec);
3294 item->modified = time (NULL);
3296 return TRUE;
3299 /* expands the application's command line */
3300 static gchar *
3301 expand_exec_line (const gchar *exec_fmt,
3302 const gchar *uri)
3304 GString *exec;
3305 gchar ch;
3307 exec = g_string_sized_new (512);
3308 while ((ch = *exec_fmt++) != '\0')
3310 if (ch != '%')
3312 exec = g_string_append_c (exec, ch);
3313 continue;
3316 ch = *exec_fmt++;
3317 switch (ch)
3319 case '\0':
3320 goto out;
3321 case 'U':
3322 case 'u':
3323 g_string_append (exec, uri);
3324 break;
3325 case 'F':
3326 case 'f':
3328 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3329 if (file)
3331 g_string_append (exec, file);
3332 g_free (file);
3334 else
3336 g_string_free (exec, TRUE);
3337 return NULL;
3340 break;
3341 case '%':
3342 default:
3343 exec = g_string_append_c (exec, ch);
3344 break;
3348 out:
3349 return g_string_free (exec, FALSE);
3353 * g_bookmark_file_get_app_info:
3354 * @bookmark: a #GBookmarkFile
3355 * @uri: a valid URI
3356 * @name: an application's name
3357 * @exec: (out) (optional): return location for the command line of the application, or %NULL
3358 * @count: (out) (optional): return location for the registration count, or %NULL
3359 * @stamp: (out) (optional): return location for the last registration time, or %NULL
3360 * @error: return location for a #GError, or %NULL
3362 * Gets the registration informations of @app_name for the bookmark for
3363 * @uri. See g_bookmark_file_set_app_info() for more informations about
3364 * the returned data.
3366 * The string returned in @app_exec must be freed.
3368 * In the event the URI cannot be found, %FALSE is returned and
3369 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3370 * event that no application with name @app_name has registered a bookmark
3371 * for @uri, %FALSE is returned and error is set to
3372 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3373 * the command line fails, an error of the #G_SHELL_ERROR domain is
3374 * set and %FALSE is returned.
3376 * Returns: %TRUE on success.
3378 * Since: 2.12
3380 gboolean
3381 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3382 const gchar *uri,
3383 const gchar *name,
3384 gchar **exec,
3385 guint *count,
3386 time_t *stamp,
3387 GError **error)
3389 BookmarkItem *item;
3390 BookmarkAppInfo *ai;
3392 g_return_val_if_fail (bookmark != NULL, FALSE);
3393 g_return_val_if_fail (uri != NULL, FALSE);
3394 g_return_val_if_fail (name != NULL, FALSE);
3396 item = g_bookmark_file_lookup_item (bookmark, uri);
3397 if (!item)
3399 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3400 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3401 _("No bookmark found for URI “%s”"),
3402 uri);
3403 return FALSE;
3406 ai = bookmark_item_lookup_app_info (item, name);
3407 if (!ai)
3409 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3410 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3411 _("No application with name “%s” registered a bookmark for “%s”"),
3412 name,
3413 uri);
3414 return FALSE;
3417 if (exec)
3419 GError *unquote_error = NULL;
3420 gchar *command_line;
3422 command_line = g_shell_unquote (ai->exec, &unquote_error);
3423 if (unquote_error)
3425 g_propagate_error (error, unquote_error);
3426 return FALSE;
3429 *exec = expand_exec_line (command_line, uri);
3430 if (!*exec)
3432 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3433 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3434 _("Failed to expand exec line “%s” with URI “%s”"),
3435 ai->exec, uri);
3436 g_free (command_line);
3438 return FALSE;
3440 else
3441 g_free (command_line);
3444 if (count)
3445 *count = ai->count;
3447 if (stamp)
3448 *stamp = ai->stamp;
3450 return TRUE;
3454 * g_bookmark_file_get_applications:
3455 * @bookmark: a #GBookmarkFile
3456 * @uri: a valid URI
3457 * @length: (out) (optional): return location of the length of the returned list, or %NULL
3458 * @error: return location for a #GError, or %NULL
3460 * Retrieves the names of the applications that have registered the
3461 * bookmark for @uri.
3463 * In the event the URI cannot be found, %NULL is returned and
3464 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3466 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3467 * Use g_strfreev() to free it.
3469 * Since: 2.12
3471 gchar **
3472 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3473 const gchar *uri,
3474 gsize *length,
3475 GError **error)
3477 BookmarkItem *item;
3478 GList *l;
3479 gchar **apps;
3480 gsize i, n_apps;
3482 g_return_val_if_fail (bookmark != NULL, NULL);
3483 g_return_val_if_fail (uri != NULL, NULL);
3485 item = g_bookmark_file_lookup_item (bookmark, uri);
3486 if (!item)
3488 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3489 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3490 _("No bookmark found for URI “%s”"),
3491 uri);
3492 return NULL;
3495 if (!item->metadata)
3497 if (length)
3498 *length = 0;
3500 return NULL;
3503 n_apps = g_list_length (item->metadata->applications);
3504 apps = g_new0 (gchar *, n_apps + 1);
3506 for (l = g_list_last (item->metadata->applications), i = 0;
3507 l != NULL;
3508 l = l->prev)
3510 BookmarkAppInfo *ai;
3512 ai = (BookmarkAppInfo *) l->data;
3514 g_warn_if_fail (ai != NULL);
3515 g_warn_if_fail (ai->name != NULL);
3517 apps[i++] = g_strdup (ai->name);
3519 apps[i] = NULL;
3521 if (length)
3522 *length = i;
3524 return apps;
3528 * g_bookmark_file_get_size:
3529 * @bookmark: a #GBookmarkFile
3531 * Gets the number of bookmarks inside @bookmark.
3533 * Returns: the number of bookmarks
3535 * Since: 2.12
3537 gint
3538 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3540 g_return_val_if_fail (bookmark != NULL, 0);
3542 return g_list_length (bookmark->items);
3546 * g_bookmark_file_move_item:
3547 * @bookmark: a #GBookmarkFile
3548 * @old_uri: a valid URI
3549 * @new_uri: (nullable): a valid URI, or %NULL
3550 * @error: return location for a #GError or %NULL
3552 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3553 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3554 * %NULL, then the bookmark is removed.
3556 * In the event the URI cannot be found, %FALSE is returned and
3557 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3559 * Returns: %TRUE if the URI was successfully changed
3561 * Since: 2.12
3563 gboolean
3564 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3565 const gchar *old_uri,
3566 const gchar *new_uri,
3567 GError **error)
3569 BookmarkItem *item;
3571 g_return_val_if_fail (bookmark != NULL, FALSE);
3572 g_return_val_if_fail (old_uri != NULL, FALSE);
3574 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3575 if (!item)
3577 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3578 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3579 _("No bookmark found for URI “%s”"),
3580 old_uri);
3581 return FALSE;
3584 if (new_uri && new_uri[0] != '\0')
3586 if (g_bookmark_file_has_item (bookmark, new_uri))
3588 if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3589 return FALSE;
3592 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3594 g_free (item->uri);
3595 item->uri = g_strdup (new_uri);
3596 item->modified = time (NULL);
3598 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3600 return TRUE;
3602 else
3604 if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
3605 return FALSE;
3607 return TRUE;
3612 * g_bookmark_file_set_icon:
3613 * @bookmark: a #GBookmarkFile
3614 * @uri: a valid URI
3615 * @href: (nullable): the URI of the icon for the bookmark, or %NULL
3616 * @mime_type: the MIME type of the icon for the bookmark
3618 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3619 * the currently set icon. @href can either be a full URL for the icon
3620 * file or the icon name following the Icon Naming specification.
3622 * If no bookmark for @uri is found one is created.
3624 * Since: 2.12
3626 void
3627 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3628 const gchar *uri,
3629 const gchar *href,
3630 const gchar *mime_type)
3632 BookmarkItem *item;
3634 g_return_if_fail (bookmark != NULL);
3635 g_return_if_fail (uri != NULL);
3637 item = g_bookmark_file_lookup_item (bookmark, uri);
3638 if (!item)
3640 item = bookmark_item_new (uri);
3641 g_bookmark_file_add_item (bookmark, item, NULL);
3644 if (!item->metadata)
3645 item->metadata = bookmark_metadata_new ();
3647 g_free (item->metadata->icon_href);
3648 g_free (item->metadata->icon_mime);
3650 item->metadata->icon_href = g_strdup (href);
3652 if (mime_type && mime_type[0] != '\0')
3653 item->metadata->icon_mime = g_strdup (mime_type);
3654 else
3655 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3657 item->modified = time (NULL);
3661 * g_bookmark_file_get_icon:
3662 * @bookmark: a #GBookmarkFile
3663 * @uri: a valid URI
3664 * @href: (out) (optional): return location for the icon's location or %NULL
3665 * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL
3666 * @error: return location for a #GError or %NULL
3668 * Gets the icon of the bookmark for @uri.
3670 * In the event the URI cannot be found, %FALSE is returned and
3671 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3673 * Returns: %TRUE if the icon for the bookmark for the URI was found.
3674 * You should free the returned strings.
3676 * Since: 2.12
3678 gboolean
3679 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3680 const gchar *uri,
3681 gchar **href,
3682 gchar **mime_type,
3683 GError **error)
3685 BookmarkItem *item;
3687 g_return_val_if_fail (bookmark != NULL, FALSE);
3688 g_return_val_if_fail (uri != NULL, FALSE);
3690 item = g_bookmark_file_lookup_item (bookmark, uri);
3691 if (!item)
3693 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3694 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3695 _("No bookmark found for URI “%s”"),
3696 uri);
3697 return FALSE;
3700 if ((!item->metadata) || (!item->metadata->icon_href))
3701 return FALSE;
3703 if (href)
3704 *href = g_strdup (item->metadata->icon_href);
3706 if (mime_type)
3707 *mime_type = g_strdup (item->metadata->icon_mime);
3709 return TRUE;