Update German translation
[glib.git] / glib / gbookmarkfile.c
blob6a05623ffd66e088ea1ceef0e5002223e70ce8c6
1 /* gbookmarkfile.c: parsing and building desktop bookmarks
3 * Copyright (C) 2005-2006 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public 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 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
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.\n", 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 gint 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 item->metadata->mime_type = g_strdup (type);
922 static void
923 parse_icon_element (GMarkupParseContext *context,
924 ParseData *parse_data,
925 const gchar **attribute_names,
926 const gchar **attribute_values,
927 GError **error)
929 const gchar *href;
930 const gchar *type;
931 const gchar *attr;
932 gint i;
933 BookmarkItem *item;
935 g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
937 i = 0;
938 href = NULL;
939 type = NULL;
940 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
942 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
943 href = attribute_values[i];
944 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
945 type = attribute_values[i];
948 /* the "href" attribute is mandatory */
949 if (!href)
951 g_set_error (error, G_MARKUP_ERROR,
952 G_MARKUP_ERROR_INVALID_CONTENT,
953 _("Attribute “%s” of element “%s” not found"),
954 BOOKMARK_HREF_ATTRIBUTE,
955 BOOKMARK_ICON_ELEMENT);
956 return;
959 if (!type)
960 type = "application/octet-stream";
962 g_warn_if_fail (parse_data->current_item != NULL);
963 item = parse_data->current_item;
965 if (!item->metadata)
966 item->metadata = bookmark_metadata_new ();
968 item->metadata->icon_href = g_strdup (href);
969 item->metadata->icon_mime = g_strdup (type);
972 /* scans through the attributes of an element for the "xmlns" pragma, and
973 * adds any resulting namespace declaration to a per-parser hashtable, using
974 * the namespace name as a key for the namespace URI; if no key was found,
975 * the namespace is considered as default, and stored under the "default" key.
977 * FIXME: this works on the assumption that the generator of the XBEL file
978 * is either this code or is smart enough to place the namespace declarations
979 * inside the main root node or inside the metadata node and does not redefine
980 * a namespace inside an inner node; this does *not* conform to the
981 * XML-NS standard, although is a close approximation. In order to make this
982 * conformant to the XML-NS specification we should use a per-element
983 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
985 static void
986 map_namespace_to_name (ParseData *parse_data,
987 const gchar **attribute_names,
988 const gchar **attribute_values)
990 const gchar *attr;
991 gint i;
993 g_warn_if_fail (parse_data != NULL);
995 if (!attribute_names || !attribute_names[0])
996 return;
998 i = 0;
999 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1001 if (g_str_has_prefix (attr, "xmlns"))
1003 gchar *namespace_name, *namespace_uri;
1004 gchar *p;
1006 p = g_utf8_strchr (attr, -1, ':');
1007 if (p)
1008 p = g_utf8_next_char (p);
1009 else
1010 p = "default";
1012 namespace_name = g_strdup (p);
1013 namespace_uri = g_strdup (attribute_values[i]);
1015 g_hash_table_replace (parse_data->namespaces,
1016 namespace_name,
1017 namespace_uri);
1022 /* checks whether @element_full is equal to @element.
1024 * if @namespace is set, it tries to resolve the namespace to a known URI,
1025 * and if found is prepended to the element name, from which is separated
1026 * using the character specified in the @sep parameter.
1028 static gboolean
1029 is_element_full (ParseData *parse_data,
1030 const gchar *element_full,
1031 const gchar *namespace,
1032 const gchar *element,
1033 const gchar sep)
1035 gchar *ns_uri, *ns_name;
1036 const gchar *p, *element_name;
1037 gboolean retval;
1039 g_warn_if_fail (parse_data != NULL);
1040 g_warn_if_fail (element_full != NULL);
1042 if (!element)
1043 return FALSE;
1045 /* no namespace requested: dumb element compare */
1046 if (!namespace)
1047 return (0 == strcmp (element_full, element));
1049 /* search for namespace separator; if none found, assume we are under the
1050 * default namespace, and set ns_name to our "default" marker; if no default
1051 * namespace has been set, just do a plain comparison between @full_element
1052 * and @element.
1054 p = g_utf8_strchr (element_full, -1, ':');
1055 if (p)
1057 ns_name = g_strndup (element_full, p - element_full);
1058 element_name = g_utf8_next_char (p);
1060 else
1062 ns_name = g_strdup ("default");
1063 element_name = element_full;
1066 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1067 if (!ns_uri)
1069 /* no default namespace found */
1070 g_free (ns_name);
1072 return (0 == strcmp (element_full, element));
1075 retval = (0 == strcmp (ns_uri, namespace) &&
1076 0 == strcmp (element_name, element));
1078 g_free (ns_name);
1080 return retval;
1083 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1084 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1086 static void
1087 start_element_raw_cb (GMarkupParseContext *context,
1088 const gchar *element_name,
1089 const gchar **attribute_names,
1090 const gchar **attribute_values,
1091 gpointer user_data,
1092 GError **error)
1094 ParseData *parse_data = (ParseData *) user_data;
1096 /* we must check for namespace declarations first
1098 * XXX - we could speed up things by checking for namespace declarations
1099 * only on the root node, where they usually are; this would probably break
1100 * on streams not produced by us or by "smart" generators
1102 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1104 switch (parse_data->state)
1106 case STATE_STARTED:
1107 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1109 const gchar *attr;
1110 gint i;
1112 i = 0;
1113 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1115 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1116 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1117 parse_data->state = STATE_ROOT;
1120 else
1121 g_set_error (error, G_MARKUP_ERROR,
1122 G_MARKUP_ERROR_INVALID_CONTENT,
1123 _("Unexpected tag “%s”, tag “%s” expected"),
1124 element_name, XBEL_ROOT_ELEMENT);
1125 break;
1126 case STATE_ROOT:
1127 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1128 parse_data->state = STATE_TITLE;
1129 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1130 parse_data->state = STATE_DESC;
1131 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1133 GError *inner_error = NULL;
1135 parse_data->state = STATE_BOOKMARK;
1137 parse_bookmark_element (context,
1138 parse_data,
1139 attribute_names,
1140 attribute_values,
1141 &inner_error);
1142 if (inner_error)
1143 g_propagate_error (error, inner_error);
1145 else
1146 g_set_error (error, G_MARKUP_ERROR,
1147 G_MARKUP_ERROR_INVALID_CONTENT,
1148 _("Unexpected tag “%s” inside “%s”"),
1149 element_name,
1150 XBEL_ROOT_ELEMENT);
1151 break;
1152 case STATE_BOOKMARK:
1153 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1154 parse_data->state = STATE_TITLE;
1155 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1156 parse_data->state = STATE_DESC;
1157 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1158 parse_data->state = STATE_INFO;
1159 else
1160 g_set_error (error, G_MARKUP_ERROR,
1161 G_MARKUP_ERROR_INVALID_CONTENT,
1162 _("Unexpected tag “%s” inside “%s”"),
1163 element_name,
1164 XBEL_BOOKMARK_ELEMENT);
1165 break;
1166 case STATE_INFO:
1167 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1169 const gchar *attr;
1170 gint i;
1172 i = 0;
1173 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1175 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1176 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1178 parse_data->state = STATE_METADATA;
1180 if (!parse_data->current_item->metadata)
1181 parse_data->current_item->metadata = bookmark_metadata_new ();
1185 else
1186 g_set_error (error, G_MARKUP_ERROR,
1187 G_MARKUP_ERROR_INVALID_CONTENT,
1188 _("Unexpected tag “%s”, tag “%s” expected"),
1189 element_name,
1190 XBEL_METADATA_ELEMENT);
1191 break;
1192 case STATE_METADATA:
1193 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1194 parse_data->state = STATE_APPLICATIONS;
1195 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1196 parse_data->state = STATE_GROUPS;
1197 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1198 parse_data->current_item->metadata->is_private = TRUE;
1199 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1201 GError *inner_error = NULL;
1203 parse_data->state = STATE_ICON;
1205 parse_icon_element (context,
1206 parse_data,
1207 attribute_names,
1208 attribute_values,
1209 &inner_error);
1210 if (inner_error)
1211 g_propagate_error (error, inner_error);
1213 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1215 GError *inner_error = NULL;
1217 parse_data->state = STATE_MIME;
1219 parse_mime_type_element (context,
1220 parse_data,
1221 attribute_names,
1222 attribute_values,
1223 &inner_error);
1224 if (inner_error)
1225 g_propagate_error (error, inner_error);
1227 else
1228 g_set_error (error, G_MARKUP_ERROR,
1229 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1230 _("Unexpected tag “%s” inside “%s”"),
1231 element_name,
1232 XBEL_METADATA_ELEMENT);
1233 break;
1234 case STATE_APPLICATIONS:
1235 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1237 GError *inner_error = NULL;
1239 parse_data->state = STATE_APPLICATION;
1241 parse_application_element (context,
1242 parse_data,
1243 attribute_names,
1244 attribute_values,
1245 &inner_error);
1246 if (inner_error)
1247 g_propagate_error (error, inner_error);
1249 else
1250 g_set_error (error, G_MARKUP_ERROR,
1251 G_MARKUP_ERROR_INVALID_CONTENT,
1252 _("Unexpected tag “%s”, tag “%s” expected"),
1253 element_name,
1254 BOOKMARK_APPLICATION_ELEMENT);
1255 break;
1256 case STATE_GROUPS:
1257 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1258 parse_data->state = STATE_GROUP;
1259 else
1260 g_set_error (error, G_MARKUP_ERROR,
1261 G_MARKUP_ERROR_INVALID_CONTENT,
1262 _("Unexpected tag “%s”, tag “%s” expected"),
1263 element_name,
1264 BOOKMARK_GROUP_ELEMENT);
1265 break;
1266 default:
1267 g_warn_if_reached ();
1268 break;
1272 static void
1273 end_element_raw_cb (GMarkupParseContext *context,
1274 const gchar *element_name,
1275 gpointer user_data,
1276 GError **error)
1278 ParseData *parse_data = (ParseData *) user_data;
1280 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1281 parse_data->state = STATE_FINISHED;
1282 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1284 parse_data->current_item = NULL;
1286 parse_data->state = STATE_ROOT;
1288 else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1289 (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1290 (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1292 if (parse_data->current_item)
1293 parse_data->state = STATE_BOOKMARK;
1294 else
1295 parse_data->state = STATE_ROOT;
1297 else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1298 parse_data->state = STATE_INFO;
1299 else if (IS_ELEMENT_NS (parse_data, element_name,
1300 BOOKMARK_NAMESPACE_URI,
1301 BOOKMARK_APPLICATION_ELEMENT))
1302 parse_data->state = STATE_APPLICATIONS;
1303 else if (IS_ELEMENT_NS (parse_data, element_name,
1304 BOOKMARK_NAMESPACE_URI,
1305 BOOKMARK_GROUP_ELEMENT))
1306 parse_data->state = STATE_GROUPS;
1307 else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1308 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1309 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1310 (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1311 (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1312 parse_data->state = STATE_METADATA;
1315 static void
1316 text_raw_cb (GMarkupParseContext *context,
1317 const gchar *text,
1318 gsize length,
1319 gpointer user_data,
1320 GError **error)
1322 ParseData *parse_data = (ParseData *) user_data;
1323 gchar *payload;
1325 payload = g_strndup (text, length);
1327 switch (parse_data->state)
1329 case STATE_TITLE:
1330 if (parse_data->current_item)
1332 g_free (parse_data->current_item->title);
1333 parse_data->current_item->title = g_strdup (payload);
1335 else
1337 g_free (parse_data->bookmark_file->title);
1338 parse_data->bookmark_file->title = g_strdup (payload);
1340 break;
1341 case STATE_DESC:
1342 if (parse_data->current_item)
1344 g_free (parse_data->current_item->description);
1345 parse_data->current_item->description = g_strdup (payload);
1347 else
1349 g_free (parse_data->bookmark_file->description);
1350 parse_data->bookmark_file->description = g_strdup (payload);
1352 break;
1353 case STATE_GROUP:
1355 GList *groups;
1357 g_warn_if_fail (parse_data->current_item != NULL);
1359 if (!parse_data->current_item->metadata)
1360 parse_data->current_item->metadata = bookmark_metadata_new ();
1362 groups = parse_data->current_item->metadata->groups;
1363 parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1365 break;
1366 case STATE_ROOT:
1367 case STATE_BOOKMARK:
1368 case STATE_INFO:
1369 case STATE_METADATA:
1370 case STATE_APPLICATIONS:
1371 case STATE_APPLICATION:
1372 case STATE_GROUPS:
1373 case STATE_MIME:
1374 case STATE_ICON:
1375 break;
1376 default:
1377 g_warn_if_reached ();
1378 break;
1381 g_free (payload);
1384 static const GMarkupParser markup_parser =
1386 start_element_raw_cb, /* start_element */
1387 end_element_raw_cb, /* end_element */
1388 text_raw_cb, /* text */
1389 NULL, /* passthrough */
1390 NULL
1393 static gboolean
1394 g_bookmark_file_parse (GBookmarkFile *bookmark,
1395 const gchar *buffer,
1396 gsize length,
1397 GError **error)
1399 GMarkupParseContext *context;
1400 ParseData *parse_data;
1401 GError *parse_error, *end_error;
1402 gboolean retval;
1404 g_warn_if_fail (bookmark != NULL);
1406 if (!buffer)
1407 return FALSE;
1409 parse_error = NULL;
1410 end_error = NULL;
1412 if (length == (gsize) -1)
1413 length = strlen (buffer);
1415 parse_data = parse_data_new ();
1416 parse_data->bookmark_file = bookmark;
1418 context = g_markup_parse_context_new (&markup_parser,
1420 parse_data,
1421 (GDestroyNotify) parse_data_free);
1423 retval = g_markup_parse_context_parse (context,
1424 buffer,
1425 length,
1426 &parse_error);
1427 if (!retval)
1428 g_propagate_error (error, parse_error);
1429 else
1431 retval = g_markup_parse_context_end_parse (context, &end_error);
1432 if (!retval)
1433 g_propagate_error (error, end_error);
1436 g_markup_parse_context_free (context);
1438 return retval;
1441 static gchar *
1442 g_bookmark_file_dump (GBookmarkFile *bookmark,
1443 gsize *length,
1444 GError **error)
1446 GString *retval;
1447 gchar *buffer;
1448 GList *l;
1450 retval = g_string_sized_new (4096);
1452 g_string_append (retval,
1453 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1454 #if 0
1455 /* XXX - do we really need the doctype? */
1456 "<!DOCTYPE " XBEL_DTD_NICK "\n"
1457 " PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1458 " \"" XBEL_DTD_URI "\">\n"
1459 #endif
1460 "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1461 " xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1462 " xmlns:" MIME_NAMESPACE_NAME "=\"" MIME_NAMESPACE_URI "\"\n>");
1464 if (bookmark->title)
1466 gchar *escaped_title;
1468 escaped_title = g_markup_escape_text (bookmark->title, -1);
1470 buffer = g_strconcat (" "
1471 "<" XBEL_TITLE_ELEMENT ">",
1472 escaped_title,
1473 "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1475 g_string_append (retval, buffer);
1477 g_free (buffer);
1478 g_free (escaped_title);
1481 if (bookmark->description)
1483 gchar *escaped_desc;
1485 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1487 buffer = g_strconcat (" "
1488 "<" XBEL_DESC_ELEMENT ">",
1489 escaped_desc,
1490 "</" XBEL_DESC_ELEMENT ">\n", NULL);
1491 g_string_append (retval, buffer);
1493 g_free (buffer);
1494 g_free (escaped_desc);
1497 if (!bookmark->items)
1498 goto out;
1499 else
1500 retval = g_string_append (retval, "\n");
1502 /* the items are stored in reverse order */
1503 for (l = g_list_last (bookmark->items);
1504 l != NULL;
1505 l = l->prev)
1507 BookmarkItem *item = (BookmarkItem *) l->data;
1508 gchar *item_dump;
1510 item_dump = bookmark_item_dump (item);
1511 if (!item_dump)
1512 continue;
1514 retval = g_string_append (retval, item_dump);
1516 g_free (item_dump);
1519 out:
1520 g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1522 if (length)
1523 *length = retval->len;
1525 return g_string_free (retval, FALSE);
1528 /**************
1529 * Misc *
1530 **************/
1532 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1533 * should free the returned string.
1535 static gchar *
1536 timestamp_to_iso8601 (time_t timestamp)
1538 GTimeVal stamp;
1540 if (timestamp == (time_t) -1)
1541 g_get_current_time (&stamp);
1542 else
1544 stamp.tv_sec = timestamp;
1545 stamp.tv_usec = 0;
1548 return g_time_val_to_iso8601 (&stamp);
1551 static time_t
1552 timestamp_from_iso8601 (const gchar *iso_date)
1554 GTimeVal stamp;
1556 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1557 return (time_t) -1;
1559 return (time_t) stamp.tv_sec;
1562 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1564 /********************
1565 * Public API *
1566 ********************/
1569 * g_bookmark_file_new:
1571 * Creates a new empty #GBookmarkFile object.
1573 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1574 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1575 * file.
1577 * Returns: an empty #GBookmarkFile
1579 * Since: 2.12
1581 GBookmarkFile *
1582 g_bookmark_file_new (void)
1584 GBookmarkFile *bookmark;
1586 bookmark = g_new (GBookmarkFile, 1);
1588 g_bookmark_file_init (bookmark);
1590 return bookmark;
1594 * g_bookmark_file_free:
1595 * @bookmark: a #GBookmarkFile
1597 * Frees a #GBookmarkFile.
1599 * Since: 2.12
1601 void
1602 g_bookmark_file_free (GBookmarkFile *bookmark)
1604 if (!bookmark)
1605 return;
1607 g_bookmark_file_clear (bookmark);
1609 g_free (bookmark);
1613 * g_bookmark_file_load_from_data:
1614 * @bookmark: an empty #GBookmarkFile struct
1615 * @data: desktop bookmarks loaded in memory
1616 * @length: the length of @data in bytes
1617 * @error: return location for a #GError, or %NULL
1619 * Loads a bookmark file from memory into an empty #GBookmarkFile
1620 * structure. If the object cannot be created then @error is set to a
1621 * #GBookmarkFileError.
1623 * Returns: %TRUE if a desktop bookmark could be loaded.
1625 * Since: 2.12
1627 gboolean
1628 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1629 const gchar *data,
1630 gsize length,
1631 GError **error)
1633 GError *parse_error;
1634 gboolean retval;
1636 g_return_val_if_fail (bookmark != NULL, FALSE);
1638 if (length == (gsize) -1)
1639 length = strlen (data);
1641 if (bookmark->items)
1643 g_bookmark_file_clear (bookmark);
1644 g_bookmark_file_init (bookmark);
1647 parse_error = NULL;
1648 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1650 if (!retval)
1651 g_propagate_error (error, parse_error);
1653 return retval;
1657 * g_bookmark_file_load_from_file:
1658 * @bookmark: an empty #GBookmarkFile struct
1659 * @filename: (type filename): the path of a filename to load, in the
1660 * GLib file name encoding
1661 * @error: return location for a #GError, or %NULL
1663 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1664 * If the file could not be loaded then @error is set to either a #GFileError
1665 * or #GBookmarkFileError.
1667 * Returns: %TRUE if a desktop bookmark file could be loaded
1669 * Since: 2.12
1671 gboolean
1672 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1673 const gchar *filename,
1674 GError **error)
1676 gboolean ret = FALSE;
1677 gchar *buffer = NULL;
1678 gsize len;
1680 g_return_val_if_fail (bookmark != NULL, FALSE);
1681 g_return_val_if_fail (filename != NULL, FALSE);
1683 if (!g_file_get_contents (filename, &buffer, &len, error))
1684 goto out;
1686 if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1687 goto out;
1689 ret = TRUE;
1690 out:
1691 g_free (buffer);
1692 return ret;
1696 /* Iterates through all the directories in *dirs trying to
1697 * find file. When it successfully locates file, returns a
1698 * string its absolute path. It also leaves the unchecked
1699 * directories in *dirs. You should free the returned string
1701 * Adapted from gkeyfile.c
1703 static gchar *
1704 find_file_in_data_dirs (const gchar *file,
1705 gchar ***dirs,
1706 GError **error)
1708 gchar **data_dirs, *data_dir, *path;
1710 path = NULL;
1712 if (dirs == NULL)
1713 return NULL;
1715 data_dirs = *dirs;
1716 path = NULL;
1717 while (data_dirs && (data_dir = *data_dirs) && !path)
1719 gchar *candidate_file, *sub_dir;
1721 candidate_file = (gchar *) file;
1722 sub_dir = g_strdup ("");
1723 while (candidate_file != NULL && !path)
1725 gchar *p;
1727 path = g_build_filename (data_dir, sub_dir,
1728 candidate_file, NULL);
1730 candidate_file = strchr (candidate_file, '-');
1732 if (candidate_file == NULL)
1733 break;
1735 candidate_file++;
1737 g_free (sub_dir);
1738 sub_dir = g_strndup (file, candidate_file - file - 1);
1740 for (p = sub_dir; *p != '\0'; p++)
1742 if (*p == '-')
1743 *p = G_DIR_SEPARATOR;
1746 g_free (sub_dir);
1747 data_dirs++;
1750 *dirs = data_dirs;
1752 if (!path)
1754 g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1755 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1756 _("No valid bookmark file found in data dirs"));
1758 return NULL;
1761 return path;
1766 * g_bookmark_file_load_from_data_dirs:
1767 * @bookmark: a #GBookmarkFile
1768 * @file: (type filename): a relative path to a filename to open and parse
1769 * @full_path: (type filename) (nullable): return location for a string
1770 * containing the full path of the file, or %NULL
1771 * @error: return location for a #GError, or %NULL
1773 * This function looks for a desktop bookmark file named @file in the
1774 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1775 * loads the file into @bookmark and returns the file's full path in
1776 * @full_path. If the file could not be loaded then an %error is
1777 * set to either a #GFileError or #GBookmarkFileError.
1779 * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1781 * Since: 2.12
1783 gboolean
1784 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1785 const gchar *file,
1786 gchar **full_path,
1787 GError **error)
1789 GError *file_error = NULL;
1790 gchar **all_data_dirs, **data_dirs;
1791 const gchar *user_data_dir;
1792 const gchar * const * system_data_dirs;
1793 gsize i, j;
1794 gchar *output_path;
1795 gboolean found_file;
1797 g_return_val_if_fail (bookmark != NULL, FALSE);
1798 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1800 user_data_dir = g_get_user_data_dir ();
1801 system_data_dirs = g_get_system_data_dirs ();
1802 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1804 i = 0;
1805 all_data_dirs[i++] = g_strdup (user_data_dir);
1807 j = 0;
1808 while (system_data_dirs[j] != NULL)
1809 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1811 found_file = FALSE;
1812 data_dirs = all_data_dirs;
1813 output_path = NULL;
1814 while (*data_dirs != NULL && !found_file)
1816 g_free (output_path);
1818 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1820 if (file_error)
1822 g_propagate_error (error, file_error);
1823 break;
1826 found_file = g_bookmark_file_load_from_file (bookmark,
1827 output_path,
1828 &file_error);
1829 if (file_error)
1831 g_propagate_error (error, file_error);
1832 break;
1836 if (found_file && full_path)
1837 *full_path = output_path;
1838 else
1839 g_free (output_path);
1841 g_strfreev (all_data_dirs);
1843 return found_file;
1848 * g_bookmark_file_to_data:
1849 * @bookmark: a #GBookmarkFile
1850 * @length: (out) (optional): return location for the length of the returned string, or %NULL
1851 * @error: return location for a #GError, or %NULL
1853 * This function outputs @bookmark as a string.
1855 * Returns: a newly allocated string holding
1856 * the contents of the #GBookmarkFile
1858 * Since: 2.12
1860 gchar *
1861 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1862 gsize *length,
1863 GError **error)
1865 GError *write_error = NULL;
1866 gchar *retval;
1868 g_return_val_if_fail (bookmark != NULL, NULL);
1870 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1871 if (write_error)
1873 g_propagate_error (error, write_error);
1875 return NULL;
1878 return retval;
1882 * g_bookmark_file_to_file:
1883 * @bookmark: a #GBookmarkFile
1884 * @filename: (type filename): path of the output file
1885 * @error: return location for a #GError, or %NULL
1887 * This function outputs @bookmark into a file. The write process is
1888 * guaranteed to be atomic by using g_file_set_contents() internally.
1890 * Returns: %TRUE if the file was successfully written.
1892 * Since: 2.12
1894 gboolean
1895 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1896 const gchar *filename,
1897 GError **error)
1899 gchar *data;
1900 GError *data_error, *write_error;
1901 gsize len;
1902 gboolean retval;
1904 g_return_val_if_fail (bookmark != NULL, FALSE);
1905 g_return_val_if_fail (filename != NULL, FALSE);
1907 data_error = NULL;
1908 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1909 if (data_error)
1911 g_propagate_error (error, data_error);
1913 return FALSE;
1916 write_error = NULL;
1917 g_file_set_contents (filename, data, len, &write_error);
1918 if (write_error)
1920 g_propagate_error (error, write_error);
1922 retval = FALSE;
1924 else
1925 retval = TRUE;
1927 g_free (data);
1929 return retval;
1932 static BookmarkItem *
1933 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1934 const gchar *uri)
1936 g_warn_if_fail (bookmark != NULL && uri != NULL);
1938 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1941 /* this function adds a new item to the list */
1942 static void
1943 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1944 BookmarkItem *item,
1945 GError **error)
1947 g_warn_if_fail (bookmark != NULL);
1948 g_warn_if_fail (item != NULL);
1950 /* this should never happen; and if it does, then we are
1951 * screwing up something big time.
1953 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1955 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1956 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1957 _("A bookmark for URI “%s” already exists"),
1958 item->uri);
1959 return;
1962 bookmark->items = g_list_prepend (bookmark->items, item);
1964 g_hash_table_replace (bookmark->items_by_uri,
1965 item->uri,
1966 item);
1968 if (item->added == (time_t) -1)
1969 item->added = time (NULL);
1971 if (item->modified == (time_t) -1)
1972 item->modified = time (NULL);
1976 * g_bookmark_file_remove_item:
1977 * @bookmark: a #GBookmarkFile
1978 * @uri: a valid URI
1979 * @error: return location for a #GError, or %NULL
1981 * Removes the bookmark for @uri from the bookmark file @bookmark.
1983 * Returns: %TRUE if the bookmark was removed successfully.
1985 * Since: 2.12
1987 gboolean
1988 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
1989 const gchar *uri,
1990 GError **error)
1992 BookmarkItem *item;
1994 g_return_val_if_fail (bookmark != NULL, FALSE);
1995 g_return_val_if_fail (uri != NULL, FALSE);
1997 item = g_bookmark_file_lookup_item (bookmark, uri);
1999 if (!item)
2001 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2002 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2003 _("No bookmark found for URI “%s”"),
2004 uri);
2005 return FALSE;
2008 bookmark->items = g_list_remove (bookmark->items, item);
2009 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2011 bookmark_item_free (item);
2013 return TRUE;
2017 * g_bookmark_file_has_item:
2018 * @bookmark: a #GBookmarkFile
2019 * @uri: a valid URI
2021 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2023 * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2025 * Since: 2.12
2027 gboolean
2028 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2029 const gchar *uri)
2031 g_return_val_if_fail (bookmark != NULL, FALSE);
2032 g_return_val_if_fail (uri != NULL, FALSE);
2034 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2038 * g_bookmark_file_get_uris:
2039 * @bookmark: a #GBookmarkFile
2040 * @length: (out) (optional): return location for the number of returned URIs, or %NULL
2042 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2043 * The array of returned URIs will be %NULL-terminated, so @length may
2044 * optionally be %NULL.
2046 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2047 * Use g_strfreev() to free it.
2049 * Since: 2.12
2051 gchar **
2052 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2053 gsize *length)
2055 GList *l;
2056 gchar **uris;
2057 gsize i, n_items;
2059 g_return_val_if_fail (bookmark != NULL, NULL);
2061 n_items = g_list_length (bookmark->items);
2062 uris = g_new0 (gchar *, n_items + 1);
2064 /* the items are stored in reverse order, so we walk the list backward */
2065 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2067 BookmarkItem *item = (BookmarkItem *) l->data;
2069 g_warn_if_fail (item != NULL);
2071 uris[i++] = g_strdup (item->uri);
2073 uris[i] = NULL;
2075 if (length)
2076 *length = i;
2078 return uris;
2082 * g_bookmark_file_set_title:
2083 * @bookmark: a #GBookmarkFile
2084 * @uri: (nullable): a valid URI or %NULL
2085 * @title: a UTF-8 encoded string
2087 * Sets @title as the title of the bookmark for @uri inside the
2088 * bookmark file @bookmark.
2090 * If @uri is %NULL, the title of @bookmark is set.
2092 * If a bookmark for @uri cannot be found then it is created.
2094 * Since: 2.12
2096 void
2097 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2098 const gchar *uri,
2099 const gchar *title)
2101 g_return_if_fail (bookmark != NULL);
2103 if (!uri)
2105 g_free (bookmark->title);
2106 bookmark->title = g_strdup (title);
2108 else
2110 BookmarkItem *item;
2112 item = g_bookmark_file_lookup_item (bookmark, uri);
2113 if (!item)
2115 item = bookmark_item_new (uri);
2116 g_bookmark_file_add_item (bookmark, item, NULL);
2119 g_free (item->title);
2120 item->title = g_strdup (title);
2122 item->modified = time (NULL);
2127 * g_bookmark_file_get_title:
2128 * @bookmark: a #GBookmarkFile
2129 * @uri: (nullable): a valid URI or %NULL
2130 * @error: return location for a #GError, or %NULL
2132 * Returns the title of the bookmark for @uri.
2134 * If @uri is %NULL, the title of @bookmark is returned.
2136 * In the event the URI cannot be found, %NULL is returned and
2137 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2139 * Returns: a newly allocated string or %NULL if the specified
2140 * URI cannot be found.
2142 * Since: 2.12
2144 gchar *
2145 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2146 const gchar *uri,
2147 GError **error)
2149 BookmarkItem *item;
2151 g_return_val_if_fail (bookmark != NULL, NULL);
2153 if (!uri)
2154 return g_strdup (bookmark->title);
2156 item = g_bookmark_file_lookup_item (bookmark, uri);
2157 if (!item)
2159 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2160 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2161 _("No bookmark found for URI “%s”"),
2162 uri);
2163 return NULL;
2166 return g_strdup (item->title);
2170 * g_bookmark_file_set_description:
2171 * @bookmark: a #GBookmarkFile
2172 * @uri: (nullable): a valid URI or %NULL
2173 * @description: a string
2175 * Sets @description as the description of the bookmark for @uri.
2177 * If @uri is %NULL, the description of @bookmark is set.
2179 * If a bookmark for @uri cannot be found then it is created.
2181 * Since: 2.12
2183 void
2184 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2185 const gchar *uri,
2186 const gchar *description)
2188 g_return_if_fail (bookmark != NULL);
2190 if (!uri)
2192 g_free (bookmark->description);
2193 bookmark->description = g_strdup (description);
2195 else
2197 BookmarkItem *item;
2199 item = g_bookmark_file_lookup_item (bookmark, uri);
2200 if (!item)
2202 item = bookmark_item_new (uri);
2203 g_bookmark_file_add_item (bookmark, item, NULL);
2206 g_free (item->description);
2207 item->description = g_strdup (description);
2209 item->modified = time (NULL);
2214 * g_bookmark_file_get_description:
2215 * @bookmark: a #GBookmarkFile
2216 * @uri: a valid URI
2217 * @error: return location for a #GError, or %NULL
2219 * Retrieves the description of the bookmark for @uri.
2221 * In the event the URI cannot be found, %NULL is returned and
2222 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2224 * Returns: a newly allocated string or %NULL if the specified
2225 * URI cannot be found.
2227 * Since: 2.12
2229 gchar *
2230 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2231 const gchar *uri,
2232 GError **error)
2234 BookmarkItem *item;
2236 g_return_val_if_fail (bookmark != NULL, NULL);
2238 if (!uri)
2239 return g_strdup (bookmark->description);
2241 item = g_bookmark_file_lookup_item (bookmark, uri);
2242 if (!item)
2244 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2245 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2246 _("No bookmark found for URI “%s”"),
2247 uri);
2248 return NULL;
2251 return g_strdup (item->description);
2255 * g_bookmark_file_set_mime_type:
2256 * @bookmark: a #GBookmarkFile
2257 * @uri: a valid URI
2258 * @mime_type: a MIME type
2260 * Sets @mime_type as the MIME type of the bookmark for @uri.
2262 * If a bookmark for @uri cannot be found then it is created.
2264 * Since: 2.12
2266 void
2267 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2268 const gchar *uri,
2269 const gchar *mime_type)
2271 BookmarkItem *item;
2273 g_return_if_fail (bookmark != NULL);
2274 g_return_if_fail (uri != NULL);
2275 g_return_if_fail (mime_type != NULL);
2277 item = g_bookmark_file_lookup_item (bookmark, uri);
2278 if (!item)
2280 item = bookmark_item_new (uri);
2281 g_bookmark_file_add_item (bookmark, item, NULL);
2284 if (!item->metadata)
2285 item->metadata = bookmark_metadata_new ();
2287 g_free (item->metadata->mime_type);
2289 item->metadata->mime_type = g_strdup (mime_type);
2290 item->modified = time (NULL);
2294 * g_bookmark_file_get_mime_type:
2295 * @bookmark: a #GBookmarkFile
2296 * @uri: a valid URI
2297 * @error: return location for a #GError, or %NULL
2299 * Retrieves the MIME type of the resource pointed by @uri.
2301 * In the event the URI cannot be found, %NULL is returned and
2302 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2303 * event that the MIME type cannot be found, %NULL is returned and
2304 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2306 * Returns: a newly allocated string or %NULL if the specified
2307 * URI cannot be found.
2309 * Since: 2.12
2311 gchar *
2312 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2313 const gchar *uri,
2314 GError **error)
2316 BookmarkItem *item;
2318 g_return_val_if_fail (bookmark != NULL, NULL);
2319 g_return_val_if_fail (uri != NULL, NULL);
2321 item = g_bookmark_file_lookup_item (bookmark, uri);
2322 if (!item)
2324 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2325 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2326 _("No bookmark found for URI “%s”"),
2327 uri);
2328 return NULL;
2331 if (!item->metadata)
2333 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2334 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2335 _("No MIME type defined in the bookmark for URI “%s”"),
2336 uri);
2337 return NULL;
2340 return g_strdup (item->metadata->mime_type);
2344 * g_bookmark_file_set_is_private:
2345 * @bookmark: a #GBookmarkFile
2346 * @uri: a valid URI
2347 * @is_private: %TRUE if the bookmark should be marked as private
2349 * Sets the private flag of the bookmark for @uri.
2351 * If a bookmark for @uri cannot be found then it is created.
2353 * Since: 2.12
2355 void
2356 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2357 const gchar *uri,
2358 gboolean is_private)
2360 BookmarkItem *item;
2362 g_return_if_fail (bookmark != NULL);
2363 g_return_if_fail (uri != NULL);
2365 item = g_bookmark_file_lookup_item (bookmark, uri);
2366 if (!item)
2368 item = bookmark_item_new (uri);
2369 g_bookmark_file_add_item (bookmark, item, NULL);
2372 if (!item->metadata)
2373 item->metadata = bookmark_metadata_new ();
2375 item->metadata->is_private = (is_private == TRUE);
2376 item->modified = time (NULL);
2380 * g_bookmark_file_get_is_private:
2381 * @bookmark: a #GBookmarkFile
2382 * @uri: a valid URI
2383 * @error: return location for a #GError, or %NULL
2385 * Gets whether the private flag of the bookmark for @uri is set.
2387 * In the event the URI cannot be found, %FALSE is returned and
2388 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2389 * event that the private flag cannot be found, %FALSE is returned and
2390 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2392 * Returns: %TRUE if the private flag is set, %FALSE otherwise.
2394 * Since: 2.12
2396 gboolean
2397 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2398 const gchar *uri,
2399 GError **error)
2401 BookmarkItem *item;
2403 g_return_val_if_fail (bookmark != NULL, FALSE);
2404 g_return_val_if_fail (uri != NULL, FALSE);
2406 item = g_bookmark_file_lookup_item (bookmark, uri);
2407 if (!item)
2409 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2410 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2411 _("No bookmark found for URI “%s”"),
2412 uri);
2413 return FALSE;
2416 if (!item->metadata)
2418 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2419 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2420 _("No private flag has been defined in bookmark for URI “%s”"),
2421 uri);
2422 return FALSE;
2425 return item->metadata->is_private;
2429 * g_bookmark_file_set_added:
2430 * @bookmark: a #GBookmarkFile
2431 * @uri: a valid URI
2432 * @added: a timestamp or -1 to use the current time
2434 * Sets the time the bookmark for @uri was added into @bookmark.
2436 * If no bookmark for @uri is found then it is created.
2438 * Since: 2.12
2440 void
2441 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2442 const gchar *uri,
2443 time_t added)
2445 BookmarkItem *item;
2447 g_return_if_fail (bookmark != NULL);
2448 g_return_if_fail (uri != NULL);
2450 item = g_bookmark_file_lookup_item (bookmark, uri);
2451 if (!item)
2453 item = bookmark_item_new (uri);
2454 g_bookmark_file_add_item (bookmark, item, NULL);
2457 if (added == (time_t) -1)
2458 time (&added);
2460 item->added = added;
2461 item->modified = added;
2465 * g_bookmark_file_get_added:
2466 * @bookmark: a #GBookmarkFile
2467 * @uri: a valid URI
2468 * @error: return location for a #GError, or %NULL
2470 * Gets the time the bookmark for @uri was added to @bookmark
2472 * In the event the URI cannot be found, -1 is returned and
2473 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2475 * Returns: a timestamp
2477 * Since: 2.12
2479 time_t
2480 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2481 const gchar *uri,
2482 GError **error)
2484 BookmarkItem *item;
2486 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2487 g_return_val_if_fail (uri != NULL, (time_t) -1);
2489 item = g_bookmark_file_lookup_item (bookmark, uri);
2490 if (!item)
2492 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2493 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2494 _("No bookmark found for URI “%s”"),
2495 uri);
2496 return (time_t) -1;
2499 return item->added;
2503 * g_bookmark_file_set_modified:
2504 * @bookmark: a #GBookmarkFile
2505 * @uri: a valid URI
2506 * @modified: a timestamp or -1 to use the current time
2508 * Sets the last time the bookmark for @uri was last modified.
2510 * If no bookmark for @uri is found then it is created.
2512 * The "modified" time should only be set when the bookmark's meta-data
2513 * was actually changed. Every function of #GBookmarkFile that
2514 * modifies a bookmark also changes the modification time, except for
2515 * g_bookmark_file_set_visited().
2517 * Since: 2.12
2519 void
2520 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2521 const gchar *uri,
2522 time_t modified)
2524 BookmarkItem *item;
2526 g_return_if_fail (bookmark != NULL);
2527 g_return_if_fail (uri != NULL);
2529 item = g_bookmark_file_lookup_item (bookmark, uri);
2530 if (!item)
2532 item = bookmark_item_new (uri);
2533 g_bookmark_file_add_item (bookmark, item, NULL);
2536 if (modified == (time_t) -1)
2537 time (&modified);
2539 item->modified = modified;
2543 * g_bookmark_file_get_modified:
2544 * @bookmark: a #GBookmarkFile
2545 * @uri: a valid URI
2546 * @error: return location for a #GError, or %NULL
2548 * Gets the time when the bookmark for @uri was last modified.
2550 * In the event the URI cannot be found, -1 is returned and
2551 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2553 * Returns: a timestamp
2555 * Since: 2.12
2557 time_t
2558 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2559 const gchar *uri,
2560 GError **error)
2562 BookmarkItem *item;
2564 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2565 g_return_val_if_fail (uri != NULL, (time_t) -1);
2567 item = g_bookmark_file_lookup_item (bookmark, uri);
2568 if (!item)
2570 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2571 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2572 _("No bookmark found for URI “%s”"),
2573 uri);
2574 return (time_t) -1;
2577 return item->modified;
2581 * g_bookmark_file_set_visited:
2582 * @bookmark: a #GBookmarkFile
2583 * @uri: a valid URI
2584 * @visited: a timestamp or -1 to use the current time
2586 * Sets the time the bookmark for @uri was last visited.
2588 * If no bookmark for @uri is found then it is created.
2590 * The "visited" time should only be set if the bookmark was launched,
2591 * either using the command line retrieved by g_bookmark_file_get_app_info()
2592 * or by the default application for the bookmark's MIME type, retrieved
2593 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2594 * does not affect the "modified" time.
2596 * Since: 2.12
2598 void
2599 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2600 const gchar *uri,
2601 time_t visited)
2603 BookmarkItem *item;
2605 g_return_if_fail (bookmark != NULL);
2606 g_return_if_fail (uri != NULL);
2608 item = g_bookmark_file_lookup_item (bookmark, uri);
2609 if (!item)
2611 item = bookmark_item_new (uri);
2612 g_bookmark_file_add_item (bookmark, item, NULL);
2615 if (visited == (time_t) -1)
2616 time (&visited);
2618 item->visited = visited;
2622 * g_bookmark_file_get_visited:
2623 * @bookmark: a #GBookmarkFile
2624 * @uri: a valid URI
2625 * @error: return location for a #GError, or %NULL
2627 * Gets the time the bookmark for @uri was last visited.
2629 * In the event the URI cannot be found, -1 is returned and
2630 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2632 * Returns: a timestamp.
2634 * Since: 2.12
2636 time_t
2637 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2638 const gchar *uri,
2639 GError **error)
2641 BookmarkItem *item;
2643 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2644 g_return_val_if_fail (uri != NULL, (time_t) -1);
2646 item = g_bookmark_file_lookup_item (bookmark, uri);
2647 if (!item)
2649 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2650 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2651 _("No bookmark found for URI “%s”"),
2652 uri);
2653 return (time_t) -1;
2656 return item->visited;
2660 * g_bookmark_file_has_group:
2661 * @bookmark: a #GBookmarkFile
2662 * @uri: a valid URI
2663 * @group: the group name to be searched
2664 * @error: return location for a #GError, or %NULL
2666 * Checks whether @group appears in the list of groups to which
2667 * the bookmark for @uri belongs to.
2669 * In the event the URI cannot be found, %FALSE is returned and
2670 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2672 * Returns: %TRUE if @group was found.
2674 * Since: 2.12
2676 gboolean
2677 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2678 const gchar *uri,
2679 const gchar *group,
2680 GError **error)
2682 BookmarkItem *item;
2683 GList *l;
2685 g_return_val_if_fail (bookmark != NULL, FALSE);
2686 g_return_val_if_fail (uri != NULL, FALSE);
2688 item = g_bookmark_file_lookup_item (bookmark, uri);
2689 if (!item)
2691 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2692 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2693 _("No bookmark found for URI “%s”"),
2694 uri);
2695 return FALSE;
2698 if (!item->metadata)
2699 return FALSE;
2701 for (l = item->metadata->groups; l != NULL; l = l->next)
2703 if (strcmp (l->data, group) == 0)
2704 return TRUE;
2707 return FALSE;
2712 * g_bookmark_file_add_group:
2713 * @bookmark: a #GBookmarkFile
2714 * @uri: a valid URI
2715 * @group: the group name to be added
2717 * Adds @group to the list of groups to which the bookmark for @uri
2718 * belongs to.
2720 * If no bookmark for @uri is found then it is created.
2722 * Since: 2.12
2724 void
2725 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2726 const gchar *uri,
2727 const gchar *group)
2729 BookmarkItem *item;
2731 g_return_if_fail (bookmark != NULL);
2732 g_return_if_fail (uri != NULL);
2733 g_return_if_fail (group != NULL && group[0] != '\0');
2735 item = g_bookmark_file_lookup_item (bookmark, uri);
2736 if (!item)
2738 item = bookmark_item_new (uri);
2739 g_bookmark_file_add_item (bookmark, item, NULL);
2742 if (!item->metadata)
2743 item->metadata = bookmark_metadata_new ();
2745 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2747 item->metadata->groups = g_list_prepend (item->metadata->groups,
2748 g_strdup (group));
2750 item->modified = time (NULL);
2755 * g_bookmark_file_remove_group:
2756 * @bookmark: a #GBookmarkFile
2757 * @uri: a valid URI
2758 * @group: the group name to be removed
2759 * @error: return location for a #GError, or %NULL
2761 * Removes @group from the list of groups to which the bookmark
2762 * for @uri belongs to.
2764 * In the event the URI cannot be found, %FALSE is returned and
2765 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2766 * In the event no group was defined, %FALSE is returned and
2767 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2769 * Returns: %TRUE if @group was successfully removed.
2771 * Since: 2.12
2773 gboolean
2774 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2775 const gchar *uri,
2776 const gchar *group,
2777 GError **error)
2779 BookmarkItem *item;
2780 GList *l;
2782 g_return_val_if_fail (bookmark != NULL, FALSE);
2783 g_return_val_if_fail (uri != NULL, FALSE);
2785 item = g_bookmark_file_lookup_item (bookmark, uri);
2786 if (!item)
2788 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2789 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2790 _("No bookmark found for URI “%s”"),
2791 uri);
2792 return FALSE;
2795 if (!item->metadata)
2797 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2798 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2799 _("No groups set in bookmark for URI “%s”"),
2800 uri);
2801 return FALSE;
2804 for (l = item->metadata->groups; l != NULL; l = l->next)
2806 if (strcmp (l->data, group) == 0)
2808 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2809 g_free (l->data);
2810 g_list_free_1 (l);
2812 item->modified = time (NULL);
2814 return TRUE;
2818 return FALSE;
2822 * g_bookmark_file_set_groups:
2823 * @bookmark: a #GBookmarkFile
2824 * @uri: an item's URI
2825 * @groups: (nullable): an array of group names, or %NULL to remove all groups
2826 * @length: number of group name values in @groups
2828 * Sets a list of group names for the item with URI @uri. Each previously
2829 * set group name list is removed.
2831 * If @uri cannot be found then an item for it is created.
2833 * Since: 2.12
2835 void
2836 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2837 const gchar *uri,
2838 const gchar **groups,
2839 gsize length)
2841 BookmarkItem *item;
2842 gsize i;
2844 g_return_if_fail (bookmark != NULL);
2845 g_return_if_fail (uri != NULL);
2846 g_return_if_fail (groups != NULL);
2848 item = g_bookmark_file_lookup_item (bookmark, uri);
2849 if (!item)
2851 item = bookmark_item_new (uri);
2852 g_bookmark_file_add_item (bookmark, item, NULL);
2855 if (!item->metadata)
2856 item->metadata = bookmark_metadata_new ();
2858 g_list_free_full (item->metadata->groups, g_free);
2859 item->metadata->groups = NULL;
2861 if (groups)
2863 for (i = 0; groups[i] != NULL && i < length; i++)
2864 item->metadata->groups = g_list_append (item->metadata->groups,
2865 g_strdup (groups[i]));
2868 item->modified = time (NULL);
2872 * g_bookmark_file_get_groups:
2873 * @bookmark: a #GBookmarkFile
2874 * @uri: a valid URI
2875 * @length: (out) (optional): return location for the length of the returned string, or %NULL
2876 * @error: return location for a #GError, or %NULL
2878 * Retrieves the list of group names of the bookmark for @uri.
2880 * In the event the URI cannot be found, %NULL is returned and
2881 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2883 * The returned array is %NULL terminated, so @length may optionally
2884 * be %NULL.
2886 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
2887 * Use g_strfreev() to free it.
2889 * Since: 2.12
2891 gchar **
2892 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2893 const gchar *uri,
2894 gsize *length,
2895 GError **error)
2897 BookmarkItem *item;
2898 GList *l;
2899 gsize len, i;
2900 gchar **retval;
2902 g_return_val_if_fail (bookmark != NULL, NULL);
2903 g_return_val_if_fail (uri != NULL, NULL);
2905 item = g_bookmark_file_lookup_item (bookmark, uri);
2906 if (!item)
2908 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2909 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2910 _("No bookmark found for URI “%s”"),
2911 uri);
2912 return NULL;
2915 if (!item->metadata)
2917 if (length)
2918 *length = 0;
2920 return NULL;
2923 len = g_list_length (item->metadata->groups);
2924 retval = g_new0 (gchar *, len + 1);
2925 for (l = g_list_last (item->metadata->groups), i = 0;
2926 l != NULL;
2927 l = l->prev)
2929 gchar *group_name = (gchar *) l->data;
2931 g_warn_if_fail (group_name != NULL);
2933 retval[i++] = g_strdup (group_name);
2935 retval[i] = NULL;
2937 if (length)
2938 *length = len;
2940 return retval;
2944 * g_bookmark_file_add_application:
2945 * @bookmark: a #GBookmarkFile
2946 * @uri: a valid URI
2947 * @name: (nullable): the name of the application registering the bookmark
2948 * or %NULL
2949 * @exec: (nullable): command line to be used to launch the bookmark or %NULL
2951 * Adds the application with @name and @exec to the list of
2952 * applications that have registered a bookmark for @uri into
2953 * @bookmark.
2955 * Every bookmark inside a #GBookmarkFile must have at least an
2956 * application registered. Each application must provide a name, a
2957 * command line useful for launching the bookmark, the number of times
2958 * the bookmark has been registered by the application and the last
2959 * time the application registered this bookmark.
2961 * If @name is %NULL, the name of the application will be the
2962 * same returned by g_get_application_name(); if @exec is %NULL, the
2963 * command line will be a composition of the program name as
2964 * returned by g_get_prgname() and the "\%u" modifier, which will be
2965 * expanded to the bookmark's URI.
2967 * This function will automatically take care of updating the
2968 * registrations count and timestamping in case an application
2969 * with the same @name had already registered a bookmark for
2970 * @uri inside @bookmark.
2972 * If no bookmark for @uri is found, one is created.
2974 * Since: 2.12
2976 void
2977 g_bookmark_file_add_application (GBookmarkFile *bookmark,
2978 const gchar *uri,
2979 const gchar *name,
2980 const gchar *exec)
2982 BookmarkItem *item;
2983 gchar *app_name, *app_exec;
2985 g_return_if_fail (bookmark != NULL);
2986 g_return_if_fail (uri != NULL);
2988 item = g_bookmark_file_lookup_item (bookmark, uri);
2989 if (!item)
2991 item = bookmark_item_new (uri);
2992 g_bookmark_file_add_item (bookmark, item, NULL);
2995 if (name && name[0] != '\0')
2996 app_name = g_strdup (name);
2997 else
2998 app_name = g_strdup (g_get_application_name ());
3000 if (exec && exec[0] != '\0')
3001 app_exec = g_strdup (exec);
3002 else
3003 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3005 g_bookmark_file_set_app_info (bookmark, uri,
3006 app_name,
3007 app_exec,
3009 (time_t) -1,
3010 NULL);
3012 g_free (app_exec);
3013 g_free (app_name);
3017 * g_bookmark_file_remove_application:
3018 * @bookmark: a #GBookmarkFile
3019 * @uri: a valid URI
3020 * @name: the name of the application
3021 * @error: return location for a #GError or %NULL
3023 * Removes application registered with @name from the list of applications
3024 * that have registered a bookmark for @uri inside @bookmark.
3026 * In the event the URI cannot be found, %FALSE is returned and
3027 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3028 * In the event that no application with name @app_name has registered
3029 * a bookmark for @uri, %FALSE is returned and error is set to
3030 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3032 * Returns: %TRUE if the application was successfully removed.
3034 * Since: 2.12
3036 gboolean
3037 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3038 const gchar *uri,
3039 const gchar *name,
3040 GError **error)
3042 GError *set_error;
3043 gboolean retval;
3045 g_return_val_if_fail (bookmark != NULL, FALSE);
3046 g_return_val_if_fail (uri != NULL, FALSE);
3047 g_return_val_if_fail (name != NULL, FALSE);
3049 set_error = NULL;
3050 retval = g_bookmark_file_set_app_info (bookmark, uri,
3051 name,
3054 (time_t) -1,
3055 &set_error);
3056 if (set_error)
3058 g_propagate_error (error, set_error);
3060 return FALSE;
3063 return retval;
3067 * g_bookmark_file_has_application:
3068 * @bookmark: a #GBookmarkFile
3069 * @uri: a valid URI
3070 * @name: the name of the application
3071 * @error: return location for a #GError or %NULL
3073 * Checks whether the bookmark for @uri inside @bookmark has been
3074 * registered by application @name.
3076 * In the event the URI cannot be found, %FALSE is returned and
3077 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3079 * Returns: %TRUE if the application @name was found
3081 * Since: 2.12
3083 gboolean
3084 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3085 const gchar *uri,
3086 const gchar *name,
3087 GError **error)
3089 BookmarkItem *item;
3091 g_return_val_if_fail (bookmark != NULL, FALSE);
3092 g_return_val_if_fail (uri != NULL, FALSE);
3093 g_return_val_if_fail (name != NULL, FALSE);
3095 item = g_bookmark_file_lookup_item (bookmark, uri);
3096 if (!item)
3098 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3099 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3100 _("No bookmark found for URI “%s”"),
3101 uri);
3102 return FALSE;
3105 return (NULL != bookmark_item_lookup_app_info (item, name));
3109 * g_bookmark_file_set_app_info:
3110 * @bookmark: a #GBookmarkFile
3111 * @uri: a valid URI
3112 * @name: an application's name
3113 * @exec: an application's command line
3114 * @count: the number of registrations done for this application
3115 * @stamp: the time of the last registration for this application
3116 * @error: return location for a #GError or %NULL
3118 * Sets the meta-data of application @name inside the list of
3119 * applications that have registered a bookmark for @uri inside
3120 * @bookmark.
3122 * You should rarely use this function; use g_bookmark_file_add_application()
3123 * and g_bookmark_file_remove_application() instead.
3125 * @name can be any UTF-8 encoded string used to identify an
3126 * application.
3127 * @exec can have one of these two modifiers: "\%f", which will
3128 * be expanded as the local file name retrieved from the bookmark's
3129 * URI; "\%u", which will be expanded as the bookmark's URI.
3130 * The expansion is done automatically when retrieving the stored
3131 * command line using the g_bookmark_file_get_app_info() function.
3132 * @count is the number of times the application has registered the
3133 * bookmark; if is < 0, the current registration count will be increased
3134 * by one, if is 0, the application with @name will be removed from
3135 * the list of registered applications.
3136 * @stamp is the Unix time of the last registration; if it is -1, the
3137 * current time will be used.
3139 * If you try to remove an application by setting its registration count to
3140 * zero, and no bookmark for @uri is found, %FALSE is returned and
3141 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3142 * in the event that no application @name has registered a bookmark
3143 * for @uri, %FALSE is returned and error is set to
3144 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3145 * for @uri is found, one is created.
3147 * Returns: %TRUE if the application's meta-data was successfully
3148 * changed.
3150 * Since: 2.12
3152 gboolean
3153 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3154 const gchar *uri,
3155 const gchar *name,
3156 const gchar *exec,
3157 gint count,
3158 time_t stamp,
3159 GError **error)
3161 BookmarkItem *item;
3162 BookmarkAppInfo *ai;
3164 g_return_val_if_fail (bookmark != NULL, FALSE);
3165 g_return_val_if_fail (uri != NULL, FALSE);
3166 g_return_val_if_fail (name != NULL, FALSE);
3167 g_return_val_if_fail (exec != NULL, FALSE);
3169 item = g_bookmark_file_lookup_item (bookmark, uri);
3170 if (!item)
3172 if (count == 0)
3174 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3175 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3176 _("No bookmark found for URI “%s”"),
3177 uri);
3178 return FALSE;
3180 else
3182 item = bookmark_item_new (uri);
3183 g_bookmark_file_add_item (bookmark, item, NULL);
3187 if (!item->metadata)
3188 item->metadata = bookmark_metadata_new ();
3190 ai = bookmark_item_lookup_app_info (item, name);
3191 if (!ai)
3193 if (count == 0)
3195 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3196 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3197 _("No application with name “%s” registered a bookmark for “%s”"),
3198 name,
3199 uri);
3200 return FALSE;
3202 else
3204 ai = bookmark_app_info_new (name);
3206 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3207 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3211 if (count == 0)
3213 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3214 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3215 bookmark_app_info_free (ai);
3217 item->modified = time (NULL);
3219 return TRUE;
3221 else if (count > 0)
3222 ai->count = count;
3223 else
3224 ai->count += 1;
3226 if (stamp != (time_t) -1)
3227 ai->stamp = stamp;
3228 else
3229 ai->stamp = time (NULL);
3231 if (exec && exec[0] != '\0')
3233 g_free (ai->exec);
3234 ai->exec = g_shell_quote (exec);
3237 item->modified = time (NULL);
3239 return TRUE;
3242 /* expands the application's command line */
3243 static gchar *
3244 expand_exec_line (const gchar *exec_fmt,
3245 const gchar *uri)
3247 GString *exec;
3248 gchar ch;
3250 exec = g_string_sized_new (512);
3251 while ((ch = *exec_fmt++) != '\0')
3253 if (ch != '%')
3255 exec = g_string_append_c (exec, ch);
3256 continue;
3259 ch = *exec_fmt++;
3260 switch (ch)
3262 case '\0':
3263 goto out;
3264 case 'U':
3265 case 'u':
3266 g_string_append (exec, uri);
3267 break;
3268 case 'F':
3269 case 'f':
3271 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3272 if (file)
3274 g_string_append (exec, file);
3275 g_free (file);
3277 else
3279 g_string_free (exec, TRUE);
3280 return NULL;
3283 break;
3284 case '%':
3285 default:
3286 exec = g_string_append_c (exec, ch);
3287 break;
3291 out:
3292 return g_string_free (exec, FALSE);
3296 * g_bookmark_file_get_app_info:
3297 * @bookmark: a #GBookmarkFile
3298 * @uri: a valid URI
3299 * @name: an application's name
3300 * @exec: (out) (optional): return location for the command line of the application, or %NULL
3301 * @count: (out) (optional): return location for the registration count, or %NULL
3302 * @stamp: (out) (optional): return location for the last registration time, or %NULL
3303 * @error: return location for a #GError, or %NULL
3305 * Gets the registration informations of @app_name for the bookmark for
3306 * @uri. See g_bookmark_file_set_app_info() for more informations about
3307 * the returned data.
3309 * The string returned in @app_exec must be freed.
3311 * In the event the URI cannot be found, %FALSE is returned and
3312 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3313 * event that no application with name @app_name has registered a bookmark
3314 * for @uri, %FALSE is returned and error is set to
3315 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3316 * the command line fails, an error of the #G_SHELL_ERROR domain is
3317 * set and %FALSE is returned.
3319 * Returns: %TRUE on success.
3321 * Since: 2.12
3323 gboolean
3324 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3325 const gchar *uri,
3326 const gchar *name,
3327 gchar **exec,
3328 guint *count,
3329 time_t *stamp,
3330 GError **error)
3332 BookmarkItem *item;
3333 BookmarkAppInfo *ai;
3335 g_return_val_if_fail (bookmark != NULL, FALSE);
3336 g_return_val_if_fail (uri != NULL, FALSE);
3337 g_return_val_if_fail (name != NULL, FALSE);
3339 item = g_bookmark_file_lookup_item (bookmark, uri);
3340 if (!item)
3342 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3343 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3344 _("No bookmark found for URI “%s”"),
3345 uri);
3346 return FALSE;
3349 ai = bookmark_item_lookup_app_info (item, name);
3350 if (!ai)
3352 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3353 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3354 _("No application with name “%s” registered a bookmark for “%s”"),
3355 name,
3356 uri);
3357 return FALSE;
3360 if (exec)
3362 GError *unquote_error = NULL;
3363 gchar *command_line;
3365 command_line = g_shell_unquote (ai->exec, &unquote_error);
3366 if (unquote_error)
3368 g_propagate_error (error, unquote_error);
3369 return FALSE;
3372 *exec = expand_exec_line (command_line, uri);
3373 if (!*exec)
3375 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3376 G_BOOKMARK_FILE_ERROR_INVALID_URI,
3377 _("Failed to expand exec line “%s” with URI “%s”"),
3378 ai->exec, uri);
3379 g_free (command_line);
3381 return FALSE;
3383 else
3384 g_free (command_line);
3387 if (count)
3388 *count = ai->count;
3390 if (stamp)
3391 *stamp = ai->stamp;
3393 return TRUE;
3397 * g_bookmark_file_get_applications:
3398 * @bookmark: a #GBookmarkFile
3399 * @uri: a valid URI
3400 * @length: (out) (optional): return location of the length of the returned list, or %NULL
3401 * @error: return location for a #GError, or %NULL
3403 * Retrieves the names of the applications that have registered the
3404 * bookmark for @uri.
3406 * In the event the URI cannot be found, %NULL is returned and
3407 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3409 * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3410 * Use g_strfreev() to free it.
3412 * Since: 2.12
3414 gchar **
3415 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3416 const gchar *uri,
3417 gsize *length,
3418 GError **error)
3420 BookmarkItem *item;
3421 GList *l;
3422 gchar **apps;
3423 gsize i, n_apps;
3425 g_return_val_if_fail (bookmark != NULL, NULL);
3426 g_return_val_if_fail (uri != NULL, NULL);
3428 item = g_bookmark_file_lookup_item (bookmark, uri);
3429 if (!item)
3431 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3432 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3433 _("No bookmark found for URI “%s”"),
3434 uri);
3435 return NULL;
3438 if (!item->metadata)
3440 if (length)
3441 *length = 0;
3443 return NULL;
3446 n_apps = g_list_length (item->metadata->applications);
3447 apps = g_new0 (gchar *, n_apps + 1);
3449 for (l = g_list_last (item->metadata->applications), i = 0;
3450 l != NULL;
3451 l = l->prev)
3453 BookmarkAppInfo *ai;
3455 ai = (BookmarkAppInfo *) l->data;
3457 g_warn_if_fail (ai != NULL);
3458 g_warn_if_fail (ai->name != NULL);
3460 apps[i++] = g_strdup (ai->name);
3462 apps[i] = NULL;
3464 if (length)
3465 *length = i;
3467 return apps;
3471 * g_bookmark_file_get_size:
3472 * @bookmark: a #GBookmarkFile
3474 * Gets the number of bookmarks inside @bookmark.
3476 * Returns: the number of bookmarks
3478 * Since: 2.12
3480 gint
3481 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3483 g_return_val_if_fail (bookmark != NULL, 0);
3485 return g_list_length (bookmark->items);
3489 * g_bookmark_file_move_item:
3490 * @bookmark: a #GBookmarkFile
3491 * @old_uri: a valid URI
3492 * @new_uri: (nullable): a valid URI, or %NULL
3493 * @error: return location for a #GError or %NULL
3495 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3496 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3497 * %NULL, then the bookmark is removed.
3499 * In the event the URI cannot be found, %FALSE is returned and
3500 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3502 * Returns: %TRUE if the URI was successfully changed
3504 * Since: 2.12
3506 gboolean
3507 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3508 const gchar *old_uri,
3509 const gchar *new_uri,
3510 GError **error)
3512 BookmarkItem *item;
3514 g_return_val_if_fail (bookmark != NULL, FALSE);
3515 g_return_val_if_fail (old_uri != NULL, FALSE);
3517 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3518 if (!item)
3520 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3521 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3522 _("No bookmark found for URI “%s”"),
3523 old_uri);
3524 return FALSE;
3527 if (new_uri && new_uri[0] != '\0')
3529 if (g_bookmark_file_has_item (bookmark, new_uri))
3531 if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3532 return FALSE;
3535 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3537 g_free (item->uri);
3538 item->uri = g_strdup (new_uri);
3539 item->modified = time (NULL);
3541 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3543 return TRUE;
3545 else
3547 if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
3548 return FALSE;
3550 return TRUE;
3555 * g_bookmark_file_set_icon:
3556 * @bookmark: a #GBookmarkFile
3557 * @uri: a valid URI
3558 * @href: (nullable): the URI of the icon for the bookmark, or %NULL
3559 * @mime_type: the MIME type of the icon for the bookmark
3561 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3562 * the currently set icon. @href can either be a full URL for the icon
3563 * file or the icon name following the Icon Naming specification.
3565 * If no bookmark for @uri is found one is created.
3567 * Since: 2.12
3569 void
3570 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3571 const gchar *uri,
3572 const gchar *href,
3573 const gchar *mime_type)
3575 BookmarkItem *item;
3577 g_return_if_fail (bookmark != NULL);
3578 g_return_if_fail (uri != NULL);
3580 item = g_bookmark_file_lookup_item (bookmark, uri);
3581 if (!item)
3583 item = bookmark_item_new (uri);
3584 g_bookmark_file_add_item (bookmark, item, NULL);
3587 if (!item->metadata)
3588 item->metadata = bookmark_metadata_new ();
3590 g_free (item->metadata->icon_href);
3591 g_free (item->metadata->icon_mime);
3593 item->metadata->icon_href = g_strdup (href);
3595 if (mime_type && mime_type[0] != '\0')
3596 item->metadata->icon_mime = g_strdup (mime_type);
3597 else
3598 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3600 item->modified = time (NULL);
3604 * g_bookmark_file_get_icon:
3605 * @bookmark: a #GBookmarkFile
3606 * @uri: a valid URI
3607 * @href: (out) (optional): return location for the icon's location or %NULL
3608 * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL
3609 * @error: return location for a #GError or %NULL
3611 * Gets the icon of the bookmark for @uri.
3613 * In the event the URI cannot be found, %FALSE is returned and
3614 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3616 * Returns: %TRUE if the icon for the bookmark for the URI was found.
3617 * You should free the returned strings.
3619 * Since: 2.12
3621 gboolean
3622 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3623 const gchar *uri,
3624 gchar **href,
3625 gchar **mime_type,
3626 GError **error)
3628 BookmarkItem *item;
3630 g_return_val_if_fail (bookmark != NULL, FALSE);
3631 g_return_val_if_fail (uri != NULL, FALSE);
3633 item = g_bookmark_file_lookup_item (bookmark, uri);
3634 if (!item)
3636 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3637 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3638 _("No bookmark found for URI “%s”"),
3639 uri);
3640 return FALSE;
3643 if ((!item->metadata) || (!item->metadata->icon_href))
3644 return FALSE;
3646 if (href)
3647 *href = g_strdup (item->metadata->icon_href);
3649 if (mime_type)
3650 *mime_type = g_strdup (item->metadata->icon_mime);
3652 return TRUE;