Move docs inline, and improve wording. (#372598, Behdad Esfahbod)
[glib.git] / glib / gbookmarkfile.c
blob914da012dd3d23070e6bf2ff9aec1764f35b710d
1 /* gbookmarkfile.c: parsing and building desktop bookmarks
3 * Copyright (C) 2005-2006 Emmanuele Bassi
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 #include "config.h"
22 #include "gbookmarkfile.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <locale.h>
30 #include <time.h>
31 #include <stdarg.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #include "gconvert.h"
39 #include "gdataset.h"
40 #include "gerror.h"
41 #include "gfileutils.h"
42 #include "ghash.h"
43 #include "glibintl.h"
44 #include "glist.h"
45 #include "gslist.h"
46 #include "gmain.h"
47 #include "gmarkup.h"
48 #include "gmem.h"
49 #include "gmessages.h"
50 #include "gslice.h"
51 #include "gstdio.h"
52 #include "gstring.h"
53 #include "gstrfuncs.h"
54 #include "gtimer.h"
55 #include "gutils.h"
57 #include "galias.h"
59 /* XBEL 1.0 standard entities */
60 #define XBEL_VERSION "1.0"
61 #define XBEL_DTD_NICK "xbel"
62 #define XBEL_DTD_SYSTEM "+//IDN python.org//DTD XML Bookmark " \
63 "Exchange Language 1.0//EN//XML"
65 #define XBEL_DTD_URI "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
67 #define XBEL_ROOT_ELEMENT "xbel"
68 #define XBEL_FOLDER_ELEMENT "folder" /* unused */
69 #define XBEL_BOOKMARK_ELEMENT "bookmark"
70 #define XBEL_ALIAS_ELEMENT "alias" /* unused */
71 #define XBEL_SEPARATOR_ELEMENT "separator" /* unused */
72 #define XBEL_TITLE_ELEMENT "title"
73 #define XBEL_DESC_ELEMENT "desc"
74 #define XBEL_INFO_ELEMENT "info"
75 #define XBEL_METADATA_ELEMENT "metadata"
77 #define XBEL_VERSION_ATTRIBUTE "version"
78 #define XBEL_FOLDED_ATTRIBUTE "folded" /* unused */
79 #define XBEL_OWNER_ATTRIBUTE "owner"
80 #define XBEL_ADDED_ATTRIBUTE "added"
81 #define XBEL_VISITED_ATTRIBUTE "visited"
82 #define XBEL_MODIFIED_ATTRIBUTE "modified"
83 #define XBEL_ID_ATTRIBUTE "id"
84 #define XBEL_HREF_ATTRIBUTE "href"
85 #define XBEL_REF_ATTRIBUTE "ref" /* unused */
87 #define XBEL_YES_VALUE "yes"
88 #define XBEL_NO_VALUE "no"
90 /* Desktop bookmark spec entities */
91 #define BOOKMARK_METADATA_OWNER "http://freedesktop.org"
93 #define BOOKMARK_NAMESPACE_NAME "bookmark"
94 #define BOOKMARK_NAMESPACE_URI "http://www.freedesktop.org/standards/desktop-bookmarks"
96 #define BOOKMARK_GROUPS_ELEMENT "groups"
97 #define BOOKMARK_GROUP_ELEMENT "group"
98 #define BOOKMARK_APPLICATIONS_ELEMENT "applications"
99 #define BOOKMARK_APPLICATION_ELEMENT "application"
100 #define BOOKMARK_ICON_ELEMENT "icon"
101 #define BOOKMARK_PRIVATE_ELEMENT "private"
103 #define BOOKMARK_NAME_ATTRIBUTE "name"
104 #define BOOKMARK_EXEC_ATTRIBUTE "exec"
105 #define BOOKMARK_COUNT_ATTRIBUTE "count"
106 #define BOOKMARK_TIMESTAMP_ATTRIBUTE "timestamp"
107 #define BOOKMARK_HREF_ATTRIBUTE "href"
108 #define BOOKMARK_TYPE_ATTRIBUTE "type"
110 /* Shared MIME Info entities */
111 #define MIME_NAMESPACE_NAME "mime"
112 #define MIME_NAMESPACE_URI "http://www.freedesktop.org/standards/shared-mime-info"
113 #define MIME_TYPE_ELEMENT "mime-type"
114 #define MIME_TYPE_ATTRIBUTE "type"
117 typedef struct _BookmarkAppInfo BookmarkAppInfo;
118 typedef struct _BookmarkMetadata BookmarkMetadata;
119 typedef struct _BookmarkItem BookmarkItem;
120 typedef struct _ParseData ParseData;
122 struct _BookmarkAppInfo
124 gchar *name;
125 gchar *exec;
127 guint count;
129 time_t stamp;
132 struct _BookmarkMetadata
134 gchar *mime_type;
136 GList *groups;
138 GList *applications;
139 GHashTable *apps_by_name;
141 gchar *icon_href;
142 gchar *icon_mime;
144 guint is_private : 1;
147 struct _BookmarkItem
149 gchar *uri;
151 gchar *title;
152 gchar *description;
154 time_t added;
155 time_t modified;
156 time_t visited;
158 BookmarkMetadata *metadata;
161 struct _GBookmarkFile
163 gchar *title;
164 gchar *description;
166 /* we store our items in a list and keep a copy inside
167 * an hash table for faster lookup performances
169 GList *items;
170 GHashTable *items_by_uri;
173 /* parser state machine */
174 enum
176 STATE_STARTED = 0,
178 STATE_ROOT,
179 STATE_BOOKMARK,
180 STATE_TITLE,
181 STATE_DESC,
182 STATE_INFO,
183 STATE_METADATA,
184 STATE_APPLICATIONS,
185 STATE_APPLICATION,
186 STATE_GROUPS,
187 STATE_GROUP,
188 STATE_MIME,
189 STATE_ICON,
191 STATE_FINISHED
194 static void g_bookmark_file_init (GBookmarkFile *bookmark);
195 static void g_bookmark_file_clear (GBookmarkFile *bookmark);
196 static gboolean g_bookmark_file_parse (GBookmarkFile *bookmark,
197 const gchar *buffer,
198 gsize length,
199 GError **error);
200 static gchar * g_bookmark_file_dump (GBookmarkFile *bookmark,
201 gsize *length,
202 GError **error);
203 static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
204 const gchar *uri);
205 static void g_bookmark_file_add_item (GBookmarkFile *bookmark,
206 BookmarkItem *item,
207 GError **error);
209 static time_t timestamp_from_iso8601 (const gchar *iso_date);
210 static gchar * timestamp_to_iso8601 (time_t timestamp);
212 /********************************
213 * BookmarkAppInfo *
215 * Application metadata storage *
216 ********************************/
217 static BookmarkAppInfo *
218 bookmark_app_info_new (const gchar *name)
220 BookmarkAppInfo *retval;
222 g_assert (name != NULL);
224 retval = g_slice_new (BookmarkAppInfo);
226 retval->name = g_strdup (name);
227 retval->exec = NULL;
228 retval->count = 0;
229 retval->stamp = time (NULL);
231 return retval;
234 static void
235 bookmark_app_info_free (BookmarkAppInfo *app_info)
237 if (!app_info)
238 return;
240 if (app_info->name)
241 g_free (app_info->name);
243 if (app_info->exec)
244 g_free (app_info->exec);
246 g_slice_free (BookmarkAppInfo, app_info);
249 static gchar *
250 bookmark_app_info_dump (BookmarkAppInfo *app_info)
252 gchar *retval;
253 gchar *name, *exec;
255 g_assert (app_info != NULL);
257 if (app_info->count == 0)
258 return NULL;
260 name = g_markup_escape_text (app_info->name, -1);
261 exec = g_markup_escape_text (app_info->exec, -1);
263 retval = g_strdup_printf (" <%s:%s %s=\"%s\" %s=\"%s\" %s=\"%ld\" %s=\"%u\"/>\n",
264 BOOKMARK_NAMESPACE_NAME,
265 BOOKMARK_APPLICATION_ELEMENT,
266 BOOKMARK_NAME_ATTRIBUTE, name,
267 BOOKMARK_EXEC_ATTRIBUTE, exec,
268 BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
269 BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
271 g_free (name);
272 g_free (exec);
274 return retval;
278 /***********************
279 * BookmarkMetadata *
281 * Metadata storage *
282 ***********************/
283 static BookmarkMetadata *
284 bookmark_metadata_new (void)
286 BookmarkMetadata *retval;
288 retval = g_slice_new (BookmarkMetadata);
290 retval->mime_type = NULL;
292 retval->groups = NULL;
294 retval->applications = NULL;
295 retval->apps_by_name = g_hash_table_new_full (g_str_hash,
296 g_str_equal,
297 NULL,
298 NULL);
300 retval->is_private = FALSE;
302 retval->icon_href = NULL;
303 retval->icon_mime = NULL;
305 return retval;
308 static void
309 bookmark_metadata_free (BookmarkMetadata *metadata)
311 if (!metadata)
312 return;
314 if (metadata->mime_type)
315 g_free (metadata->mime_type);
317 if (metadata->groups)
319 g_list_foreach (metadata->groups,
320 (GFunc) g_free,
321 NULL);
322 g_list_free (metadata->groups);
325 if (metadata->applications)
327 g_list_foreach (metadata->applications,
328 (GFunc) bookmark_app_info_free,
329 NULL);
330 g_list_free (metadata->applications);
332 g_hash_table_destroy (metadata->apps_by_name);
335 if (metadata->icon_href)
336 g_free (metadata->icon_href);
338 if (metadata->icon_mime)
339 g_free (metadata->icon_mime);
341 g_slice_free (BookmarkMetadata, metadata);
344 static gchar *
345 bookmark_metadata_dump (BookmarkMetadata *metadata)
347 GString *retval;
349 if (!metadata->applications)
350 return NULL;
352 retval = g_string_new (NULL);
354 /* metadata container */
355 g_string_append_printf (retval,
356 " <%s %s=\"%s\">\n",
357 XBEL_METADATA_ELEMENT,
358 XBEL_OWNER_ATTRIBUTE, BOOKMARK_METADATA_OWNER);
360 /* mime type */
361 if (metadata->mime_type)
362 g_string_append_printf (retval,
363 " <%s:%s %s=\"%s\"/>\n",
364 MIME_NAMESPACE_NAME,
365 MIME_TYPE_ELEMENT,
366 MIME_TYPE_ATTRIBUTE, metadata->mime_type);
368 if (metadata->groups)
370 GList *l;
372 /* open groups container */
373 g_string_append_printf (retval,
374 " <%s:%s>\n",
375 BOOKMARK_NAMESPACE_NAME,
376 BOOKMARK_GROUPS_ELEMENT);
378 for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
380 gchar *group_name;
382 group_name = g_markup_escape_text ((gchar *) l->data, -1);
383 g_string_append_printf (retval,
384 " <%s:%s>%s</%s:%s>\n",
385 BOOKMARK_NAMESPACE_NAME,
386 BOOKMARK_GROUP_ELEMENT,
387 group_name,
388 BOOKMARK_NAMESPACE_NAME,
389 BOOKMARK_GROUP_ELEMENT);
390 g_free (group_name);
393 /* close groups container */
394 g_string_append_printf (retval,
395 " </%s:%s>\n",
396 BOOKMARK_NAMESPACE_NAME,
397 BOOKMARK_GROUPS_ELEMENT);
400 if (metadata->applications)
402 GList *l;
404 /* open applications container */
405 g_string_append_printf (retval,
406 " <%s:%s>\n",
407 BOOKMARK_NAMESPACE_NAME,
408 BOOKMARK_APPLICATIONS_ELEMENT);
410 for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
412 BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
413 gchar *app_data;
415 g_assert (app_info != NULL);
417 app_data = bookmark_app_info_dump (app_info);
419 if (app_data)
421 retval = g_string_append (retval, app_data);
423 g_free (app_data);
427 /* close applications container */
428 g_string_append_printf (retval,
429 " </%s:%s>\n",
430 BOOKMARK_NAMESPACE_NAME,
431 BOOKMARK_APPLICATIONS_ELEMENT);
434 /* icon */
435 if (metadata->icon_href)
437 if (!metadata->icon_mime)
438 metadata->icon_mime = g_strdup ("application/octet-stream");
440 g_string_append_printf (retval,
441 " <%s:%s %s=\"%s\" %s=\"%s\"/>\n",
442 BOOKMARK_NAMESPACE_NAME,
443 BOOKMARK_ICON_ELEMENT,
444 BOOKMARK_HREF_ATTRIBUTE, metadata->icon_href,
445 BOOKMARK_TYPE_ATTRIBUTE, metadata->icon_mime);
448 /* private hint */
449 if (metadata->is_private)
450 g_string_append_printf (retval,
451 " <%s:%s/>\n",
452 BOOKMARK_NAMESPACE_NAME,
453 BOOKMARK_PRIVATE_ELEMENT);
455 /* close metadata container */
456 g_string_append_printf (retval, " </%s>\n", XBEL_METADATA_ELEMENT);
458 return g_string_free (retval, FALSE);
461 /******************************************************
462 * BookmarkItem *
464 * Storage for a single bookmark item inside the list *
465 ******************************************************/
466 static BookmarkItem *
467 bookmark_item_new (const gchar *uri)
469 BookmarkItem *item;
471 g_assert (uri != NULL);
473 item = g_slice_new (BookmarkItem);
474 item->uri = g_strdup (uri);
476 item->title = NULL;
477 item->description = NULL;
479 item->added = (time_t) -1;
480 item->modified = (time_t) -1;
481 item->visited = (time_t) -1;
483 item->metadata = NULL;
485 return item;
488 static void
489 bookmark_item_free (BookmarkItem *item)
491 if (!item)
492 return;
494 if (item->uri)
495 g_free (item->uri);
497 if (item->title)
498 g_free (item->title);
500 if (item->description)
501 g_free (item->description);
503 if (item->metadata)
504 bookmark_metadata_free (item->metadata);
506 g_slice_free (BookmarkItem, item);
509 static gchar *
510 bookmark_item_dump (BookmarkItem *item)
512 GString *retval;
513 gchar *added, *visited, *modified;
514 gchar *escaped_uri;
516 /* at this point, we must have at least a registered application; if we don't
517 * we don't screw up the bookmark file, and just skip this item
519 if (!item->metadata || !item->metadata->applications)
521 g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
522 return NULL;
525 retval = g_string_new (NULL);
527 added = timestamp_to_iso8601 (item->added);
528 modified = timestamp_to_iso8601 (item->modified);
529 visited = timestamp_to_iso8601 (item->visited);
531 escaped_uri = g_markup_escape_text (item->uri, -1);
533 g_string_append_printf (retval,
534 " <%s %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\">\n",
535 XBEL_BOOKMARK_ELEMENT,
536 XBEL_HREF_ATTRIBUTE, escaped_uri,
537 XBEL_ADDED_ATTRIBUTE, added,
538 XBEL_MODIFIED_ATTRIBUTE, modified,
539 XBEL_VISITED_ATTRIBUTE, visited);
540 g_free (escaped_uri);
541 g_free (visited);
542 g_free (modified);
543 g_free (added);
545 if (item->title)
547 gchar *escaped_title;
549 escaped_title = g_markup_escape_text (item->title, -1);
550 g_string_append_printf (retval,
551 " <%s>%s</%s>\n",
552 XBEL_TITLE_ELEMENT,
553 escaped_title,
554 XBEL_TITLE_ELEMENT);
555 g_free (escaped_title);
558 if (item->description)
560 gchar *escaped_desc;
562 escaped_desc = g_markup_escape_text (item->description, -1);
563 g_string_append_printf (retval,
564 " <%s>%s</%s>\n",
565 XBEL_DESC_ELEMENT,
566 escaped_desc,
567 XBEL_DESC_ELEMENT);
568 g_free (escaped_desc);
571 if (item->metadata)
573 gchar *metadata;
575 /* open info container */
576 g_string_append_printf (retval, " <%s>\n", XBEL_INFO_ELEMENT);
578 metadata = bookmark_metadata_dump (item->metadata);
579 if (metadata)
581 retval = g_string_append (retval, metadata);
583 g_free (metadata);
586 /* close info container */
587 g_string_append_printf (retval, " </%s>\n", XBEL_INFO_ELEMENT);
590 g_string_append_printf (retval, " </%s>\n", XBEL_BOOKMARK_ELEMENT);
592 return g_string_free (retval, FALSE);
595 static BookmarkAppInfo *
596 bookmark_item_lookup_app_info (BookmarkItem *item,
597 const gchar *app_name)
599 g_assert (item != NULL && app_name != NULL);
601 if (!item->metadata)
602 return NULL;
604 return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
607 /*************************
608 * GBookmarkFile *
609 *************************/
611 static void
612 g_bookmark_file_init (GBookmarkFile *bookmark)
614 bookmark->title = NULL;
615 bookmark->description = NULL;
617 bookmark->items = NULL;
618 bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
619 g_str_equal,
620 NULL,
621 NULL);
624 static void
625 g_bookmark_file_clear (GBookmarkFile *bookmark)
627 g_free (bookmark->title);
628 g_free (bookmark->description);
630 if (bookmark->items)
632 g_list_foreach (bookmark->items,
633 (GFunc) bookmark_item_free,
634 NULL);
635 g_list_free (bookmark->items);
637 bookmark->items = NULL;
640 if (bookmark->items_by_uri)
642 g_hash_table_destroy (bookmark->items_by_uri);
644 bookmark->items_by_uri = NULL;
648 struct _ParseData
650 gint state;
652 GHashTable *namespaces;
654 GBookmarkFile *bookmark_file;
655 BookmarkItem *current_item;
658 static ParseData *
659 parse_data_new (void)
661 ParseData *retval;
663 retval = g_new (ParseData, 1);
665 retval->state = STATE_STARTED;
666 retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
667 (GDestroyNotify) g_free,
668 (GDestroyNotify) g_free);
669 retval->bookmark_file = NULL;
670 retval->current_item = NULL;
672 return retval;
675 static void
676 parse_data_free (ParseData *parse_data)
678 g_hash_table_destroy (parse_data->namespaces);
680 g_free (parse_data);
683 #define IS_ATTRIBUTE(s,a) ((0 == strcmp ((s), (a))))
685 static void
686 parse_bookmark_element (GMarkupParseContext *context,
687 ParseData *parse_data,
688 const gchar **attribute_names,
689 const gchar **attribute_values,
690 GError **error)
692 const gchar *uri, *added, *modified, *visited;
693 const gchar *attr;
694 gint i;
695 BookmarkItem *item;
696 GError *add_error;
698 g_assert ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
700 i = 0;
701 uri = added = modified = visited = NULL;
702 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
704 if (IS_ATTRIBUTE (attr, XBEL_HREF_ATTRIBUTE))
705 uri = attribute_values[i];
706 else if (IS_ATTRIBUTE (attr, XBEL_ADDED_ATTRIBUTE))
707 added = attribute_values[i];
708 else if (IS_ATTRIBUTE (attr, XBEL_MODIFIED_ATTRIBUTE))
709 modified = attribute_values[i];
710 else if (IS_ATTRIBUTE (attr, XBEL_VISITED_ATTRIBUTE))
711 visited = attribute_values[i];
712 else
714 g_set_error (error, G_MARKUP_ERROR,
715 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
716 _("Unexpected attribute '%s' for element '%s'"),
717 attr,
718 XBEL_BOOKMARK_ELEMENT);
719 return;
723 if (!uri)
725 g_set_error (error, G_MARKUP_ERROR,
726 G_MARKUP_ERROR_INVALID_CONTENT,
727 _("Attribute '%s' of element '%s' not found"),
728 XBEL_HREF_ATTRIBUTE,
729 XBEL_BOOKMARK_ELEMENT);
730 return;
733 g_assert (parse_data->current_item == NULL);
735 item = bookmark_item_new (uri);
737 if (added)
738 item->added = timestamp_from_iso8601 (added);
740 if (modified)
741 item->modified = timestamp_from_iso8601 (modified);
743 if (visited)
744 item->visited = timestamp_from_iso8601 (visited);
746 add_error = NULL;
747 g_bookmark_file_add_item (parse_data->bookmark_file,
748 item,
749 &add_error);
750 if (add_error)
752 bookmark_item_free (item);
754 g_propagate_error (error, add_error);
756 return;
759 parse_data->current_item = item;
762 static void
763 parse_application_element (GMarkupParseContext *context,
764 ParseData *parse_data,
765 const gchar **attribute_names,
766 const gchar **attribute_values,
767 GError **error)
769 const gchar *name, *exec, *count, *stamp;
770 const gchar *attr;
771 gint i;
772 BookmarkItem *item;
773 BookmarkAppInfo *ai;
775 g_assert ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
777 i = 0;
778 name = exec = count = stamp = NULL;
779 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
781 if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
782 name = attribute_values[i];
783 else if (IS_ATTRIBUTE (attr, BOOKMARK_EXEC_ATTRIBUTE))
784 exec = attribute_values[i];
785 else if (IS_ATTRIBUTE (attr, BOOKMARK_COUNT_ATTRIBUTE))
786 count = attribute_values[i];
787 else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
788 stamp = attribute_values[i];
789 else
791 g_set_error (error, G_MARKUP_ERROR,
792 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
793 _("Unexpected attribute '%s' for element '%s'"),
794 attr,
795 BOOKMARK_APPLICATION_ELEMENT);
796 return;
800 if (!name)
802 g_set_error (error, G_MARKUP_ERROR,
803 G_MARKUP_ERROR_INVALID_CONTENT,
804 _("Attribute '%s' of element '%s' not found"),
805 BOOKMARK_NAME_ATTRIBUTE,
806 BOOKMARK_APPLICATION_ELEMENT);
807 return;
810 if (!exec)
812 g_set_error (error, G_MARKUP_ERROR,
813 G_MARKUP_ERROR_INVALID_CONTENT,
814 _("Attribute '%s' of element '%s' not found"),
815 BOOKMARK_EXEC_ATTRIBUTE,
816 BOOKMARK_APPLICATION_ELEMENT);
817 return;
820 g_assert (parse_data->current_item != NULL);
821 item = parse_data->current_item;
823 ai = bookmark_item_lookup_app_info (item, name);
824 if (!ai)
826 ai = bookmark_app_info_new (name);
828 if (!item->metadata)
829 item->metadata = bookmark_metadata_new ();
831 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
832 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
835 ai->exec = g_strdup (exec);
837 if (count)
838 ai->count = atoi (count);
839 else
840 ai->count = 1;
842 if (stamp)
843 ai->stamp = (time_t) atol (stamp);
844 else
845 ai->stamp = time (NULL);
848 static void
849 parse_mime_type_element (GMarkupParseContext *context,
850 ParseData *parse_data,
851 const gchar **attribute_names,
852 const gchar **attribute_values,
853 GError **error)
855 const gchar *type;
856 const gchar *attr;
857 gint i;
858 BookmarkItem *item;
860 g_assert ((parse_data != NULL) && (parse_data->state == STATE_MIME));
862 i = 0;
863 type = NULL;
864 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
866 if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
867 type = attribute_values[i];
868 else
870 g_set_error (error, G_MARKUP_ERROR,
871 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
872 _("Unexpected attribute '%s' for element '%s'"),
873 attr,
874 MIME_TYPE_ELEMENT);
875 return;
879 if (!type)
880 type = "application/octet-stream";
882 g_assert (parse_data->current_item != NULL);
883 item = parse_data->current_item;
885 if (!item->metadata)
886 item->metadata = bookmark_metadata_new ();
888 item->metadata->mime_type = g_strdup (type);
891 static void
892 parse_icon_element (GMarkupParseContext *context,
893 ParseData *parse_data,
894 const gchar **attribute_names,
895 const gchar **attribute_values,
896 GError **error)
898 const gchar *href;
899 const gchar *type;
900 const gchar *attr;
901 gint i;
902 BookmarkItem *item;
904 g_assert ((parse_data != NULL) && (parse_data->state == STATE_ICON));
906 i = 0;
907 href = NULL;
908 type = NULL;
909 for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
911 if (IS_ATTRIBUTE (attr, BOOKMARK_HREF_ATTRIBUTE))
912 href = attribute_values[i];
913 else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
914 type = attribute_values[i];
915 else
917 g_set_error (error, G_MARKUP_ERROR,
918 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
919 _("Unexpected attribute '%s' for element '%s'"),
920 attr,
921 BOOKMARK_ICON_ELEMENT);
922 return;
926 if (!href)
928 g_set_error (error, G_MARKUP_ERROR,
929 G_MARKUP_ERROR_INVALID_CONTENT,
930 _("Attribute '%s' of element '%s' not found"),
931 BOOKMARK_HREF_ATTRIBUTE,
932 BOOKMARK_ICON_ELEMENT);
933 return;
936 if (!type)
937 type = "application/octet-stream";
939 g_assert (parse_data->current_item != NULL);
940 item = parse_data->current_item;
942 if (!item->metadata)
943 item->metadata = bookmark_metadata_new ();
945 item->metadata->icon_href = g_strdup (href);
946 item->metadata->icon_mime = g_strdup (type);
949 /* scans through the attributes of an element for the "xmlns" pragma, and
950 * adds any resulting namespace declaration to a per-parser hashtable, using
951 * the namespace name as a key for the namespace URI; if no key was found,
952 * the namespace is considered as default, and stored under the "default" key.
954 * FIXME: this works on the assumption that the generator of the XBEL file
955 * is either this code or is smart enough to place the namespace declarations
956 * inside the main root node or inside the metadata node and does not redefine
957 * a namespace inside an inner node; this does *not* conform to the
958 * XML-NS standard, although is a close approximation. In order to make this
959 * conformant to the XML-NS specification we should use a per-element
960 * namespace table inside GMarkup and ask it to resolve the namespaces for us.
962 static void
963 map_namespace_to_name (ParseData *parse_data,
964 const gchar **attribute_names,
965 const gchar **attribute_values)
967 const gchar *attr;
968 gint i;
970 g_assert (parse_data != NULL);
972 if (!attribute_names || !attribute_names[0])
973 return;
975 i = 0;
976 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
978 if (g_str_has_prefix (attr, "xmlns"))
980 gchar *namespace_name, *namespace_uri;
981 gchar *p;
983 p = g_utf8_strchr (attr, -1, ':');
984 if (p)
985 p = g_utf8_next_char (p);
986 else
987 p = "default";
989 namespace_name = g_strdup (p);
990 namespace_uri = g_strdup (attribute_values[i]);
992 g_hash_table_replace (parse_data->namespaces,
993 namespace_name,
994 namespace_uri);
999 /* checks whether @element_full is equal to @element.
1001 * if @namespace is set, it tries to resolve the namespace to a known URI,
1002 * and if found is prepended to the element name, from which is separated
1003 * using the character specified in the @sep parameter.
1005 static gboolean
1006 is_element_full (ParseData *parse_data,
1007 const gchar *element_full,
1008 const gchar *namespace,
1009 const gchar *element,
1010 const gchar sep)
1012 gchar *ns_uri, *ns_name, *s, *resolved;
1013 const gchar *p, *element_name;
1014 gboolean retval;
1016 g_assert (parse_data != NULL);
1017 g_assert (element_full != NULL);
1019 if (!element)
1020 return FALSE;
1022 /* no namespace requested: dumb element compare */
1023 if (!namespace)
1024 return (0 == strcmp (element_full, element));
1026 /* search for namespace separator; if none found, assume we are under the
1027 * default namespace, and set ns_name to our "default" marker; if no default
1028 * namespace has been set, just do a plain comparison between @full_element
1029 * and @element.
1031 p = strchr (element_full, ':');
1032 if (p)
1034 ns_name = g_strndup (element_full, p - element_full);
1035 element_name = g_utf8_next_char (p);
1037 else
1039 ns_name = g_strdup ("default");
1040 element_name = element_full;
1043 ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
1044 if (!ns_uri)
1046 /* no default namespace found */
1047 g_free (ns_name);
1049 return (0 == strcmp (element_full, element));
1052 resolved = g_strdup_printf ("%s%c%s", ns_uri, sep, element_name);
1053 s = g_strdup_printf ("%s%c%s", namespace, sep, element);
1054 retval = (0 == strcmp (resolved, s));
1056 g_free (ns_name);
1057 g_free (resolved);
1058 g_free (s);
1060 return retval;
1063 #define IS_ELEMENT(p,s,e) (is_element_full ((p), (s), NULL, (e), '\0'))
1064 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
1066 static void
1067 start_element_raw_cb (GMarkupParseContext *context,
1068 const gchar *element_name,
1069 const gchar **attribute_names,
1070 const gchar **attribute_values,
1071 gpointer user_data,
1072 GError **error)
1074 ParseData *parse_data = (ParseData *) user_data;
1076 /* we must check for namespace declarations first
1078 * XXX - we could speed up things by checking for namespace declarations
1079 * only on the root node, where they usually are; this would probably break
1080 * on streams not produced by us or by "smart" generators
1082 map_namespace_to_name (parse_data, attribute_names, attribute_values);
1084 switch (parse_data->state)
1086 case STATE_STARTED:
1087 if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1089 const gchar *attr;
1090 gint i;
1092 i = 0;
1093 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1095 if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1096 (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1097 parse_data->state = STATE_ROOT;
1100 else
1101 g_set_error (error, G_MARKUP_ERROR,
1102 G_MARKUP_ERROR_INVALID_CONTENT,
1103 _("Unexpected tag '%s', tag '%s' expected"),
1104 element_name, XBEL_ROOT_ELEMENT);
1105 break;
1106 case STATE_ROOT:
1107 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1108 parse_data->state = STATE_TITLE;
1109 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1110 parse_data->state = STATE_DESC;
1111 else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1113 GError *inner_error = NULL;
1115 parse_data->state = STATE_BOOKMARK;
1117 parse_bookmark_element (context,
1118 parse_data,
1119 attribute_names,
1120 attribute_values,
1121 &inner_error);
1122 if (inner_error)
1123 g_propagate_error (error, inner_error);
1125 else
1126 g_set_error (error, G_MARKUP_ERROR,
1127 G_MARKUP_ERROR_INVALID_CONTENT,
1128 _("Unexpected tag '%s' inside '%s'"),
1129 element_name,
1130 XBEL_ROOT_ELEMENT);
1131 break;
1132 case STATE_BOOKMARK:
1133 if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1134 parse_data->state = STATE_TITLE;
1135 else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1136 parse_data->state = STATE_DESC;
1137 else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1138 parse_data->state = STATE_INFO;
1139 else
1140 g_set_error (error, G_MARKUP_ERROR,
1141 G_MARKUP_ERROR_INVALID_CONTENT,
1142 _("Unexpected tag '%s' inside '%s'"),
1143 element_name,
1144 XBEL_BOOKMARK_ELEMENT);
1145 break;
1146 case STATE_INFO:
1147 if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1149 const gchar *attr;
1150 gint i;
1152 i = 0;
1153 for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1155 if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1156 (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1158 parse_data->state = STATE_METADATA;
1160 if (!parse_data->current_item->metadata)
1161 parse_data->current_item->metadata = bookmark_metadata_new ();
1165 else
1166 g_set_error (error, G_MARKUP_ERROR,
1167 G_MARKUP_ERROR_INVALID_CONTENT,
1168 _("Unexpected tag '%s', tag '%s' expected"),
1169 element_name,
1170 XBEL_METADATA_ELEMENT);
1171 break;
1172 case STATE_METADATA:
1173 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1174 parse_data->state = STATE_APPLICATIONS;
1175 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1176 parse_data->state = STATE_GROUPS;
1177 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1178 parse_data->current_item->metadata->is_private = TRUE;
1179 else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1181 GError *inner_error = NULL;
1183 parse_data->state = STATE_ICON;
1185 parse_icon_element (context,
1186 parse_data,
1187 attribute_names,
1188 attribute_values,
1189 &inner_error);
1190 if (inner_error)
1191 g_propagate_error (error, inner_error);
1193 else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1195 GError *inner_error = NULL;
1197 parse_data->state = STATE_MIME;
1199 parse_mime_type_element (context,
1200 parse_data,
1201 attribute_names,
1202 attribute_values,
1203 &inner_error);
1204 if (inner_error)
1205 g_propagate_error (error, inner_error);
1207 else
1208 g_set_error (error, G_MARKUP_ERROR,
1209 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1210 _("Unexpected tag '%s' inside '%s'"),
1211 element_name,
1212 XBEL_METADATA_ELEMENT);
1213 break;
1214 case STATE_APPLICATIONS:
1215 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1217 GError *inner_error = NULL;
1219 parse_data->state = STATE_APPLICATION;
1221 parse_application_element (context,
1222 parse_data,
1223 attribute_names,
1224 attribute_values,
1225 &inner_error);
1226 if (inner_error)
1227 g_propagate_error (error, inner_error);
1229 else
1230 g_set_error (error, G_MARKUP_ERROR,
1231 G_MARKUP_ERROR_INVALID_CONTENT,
1232 _("Unexpected tag '%s', tag '%s' expected"),
1233 element_name,
1234 BOOKMARK_APPLICATION_ELEMENT);
1235 break;
1236 case STATE_GROUPS:
1237 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1238 parse_data->state = STATE_GROUP;
1239 else
1240 g_set_error (error, G_MARKUP_ERROR,
1241 G_MARKUP_ERROR_INVALID_CONTENT,
1242 _("Unexpected tag '%s', tag '%s' expected"),
1243 element_name,
1244 BOOKMARK_GROUP_ELEMENT);
1245 break;
1246 case STATE_ICON:
1247 if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1249 GError *inner_error = NULL;
1251 parse_icon_element (context,
1252 parse_data,
1253 attribute_names,
1254 attribute_values,
1255 &inner_error);
1256 if (inner_error)
1257 g_propagate_error (error, inner_error);
1259 else
1260 g_set_error (error, G_MARKUP_ERROR,
1261 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1262 _("Unexpected tag '%s' inside '%s'"),
1263 element_name,
1264 XBEL_METADATA_ELEMENT);
1265 break;
1266 default:
1267 g_assert_not_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_assert (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_assert_not_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_assert (bookmark != NULL);
1406 if (!buffer)
1407 return FALSE;
1409 if (length == -1)
1410 length = strlen (buffer);
1412 parse_data = parse_data_new ();
1413 parse_data->bookmark_file = bookmark;
1415 context = g_markup_parse_context_new (&markup_parser,
1417 parse_data,
1418 (GDestroyNotify) parse_data_free);
1420 parse_error = NULL;
1421 retval = g_markup_parse_context_parse (context,
1422 buffer,
1423 length,
1424 &parse_error);
1425 if (!retval)
1427 g_propagate_error (error, parse_error);
1429 return FALSE;
1432 end_error = NULL;
1433 retval = g_markup_parse_context_end_parse (context, &end_error);
1434 if (!retval)
1436 g_propagate_error (error, end_error);
1438 return FALSE;
1441 g_markup_parse_context_free (context);
1443 return TRUE;
1446 static gchar *
1447 g_bookmark_file_dump (GBookmarkFile *bookmark,
1448 gsize *length,
1449 GError **error)
1451 GString *retval;
1452 GList *l;
1454 retval = g_string_new (NULL);
1456 g_string_append_printf (retval,
1457 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1458 #if 0
1459 /* XXX - do we really need the doctype? */
1460 "<!DOCTYPE %s\n"
1461 " PUBLIC \"%s\"\n"
1462 " \"%s\">\n"
1463 #endif
1464 "<%s %s=\"%s\"\n"
1465 " xmlns:%s=\"%s\"\n"
1466 " xmlns:%s=\"%s\"\n>",
1467 #if 0
1468 /* XXX - do we really need the doctype? */
1469 XBEL_DTD_NICK,
1470 XBEL_DTD_SYSTEM, XBEL_DTD_URI,
1471 #endif
1472 XBEL_ROOT_ELEMENT,
1473 XBEL_VERSION_ATTRIBUTE, XBEL_VERSION,
1474 BOOKMARK_NAMESPACE_NAME, BOOKMARK_NAMESPACE_URI,
1475 MIME_NAMESPACE_NAME, MIME_NAMESPACE_URI);
1477 if (bookmark->title)
1479 gchar *escaped_title;
1481 escaped_title = g_markup_escape_text (bookmark->title, -1);
1483 g_string_append_printf (retval, " <%s>%s</%s>\n",
1484 XBEL_TITLE_ELEMENT,
1485 escaped_title,
1486 XBEL_TITLE_ELEMENT);
1488 g_free (escaped_title);
1491 if (bookmark->description)
1493 gchar *escaped_desc;
1495 escaped_desc = g_markup_escape_text (bookmark->description, -1);
1497 g_string_append_printf (retval, " <%s>%s</%s>\n",
1498 XBEL_DESC_ELEMENT,
1499 escaped_desc,
1500 XBEL_DESC_ELEMENT);
1502 g_free (escaped_desc);
1505 if (!bookmark->items)
1506 goto out;
1507 else
1508 retval = g_string_append (retval, "\n");
1510 for (l = g_list_last (bookmark->items);
1511 l != NULL;
1512 l = l->prev)
1514 BookmarkItem *item = (BookmarkItem *) l->data;
1515 gchar *item_dump;
1517 item_dump = bookmark_item_dump (item);
1518 if (!item_dump)
1519 continue;
1521 retval = g_string_append (retval, item_dump);
1523 g_free (item_dump);
1526 out:
1527 g_string_append_printf (retval, "</%s>", XBEL_ROOT_ELEMENT);
1529 if (length)
1530 *length = retval->len;
1532 return g_string_free (retval, FALSE);
1535 /**************
1536 * Misc *
1537 **************/
1539 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1540 * should free the returned string.
1542 static gchar *
1543 timestamp_to_iso8601 (time_t timestamp)
1545 GTimeVal stamp;
1547 if (timestamp == (time_t) -1)
1548 g_get_current_time (&stamp);
1549 else
1551 stamp.tv_sec = timestamp;
1552 stamp.tv_usec = 0;
1555 return g_time_val_to_iso8601 (&stamp);
1558 static time_t
1559 timestamp_from_iso8601 (const gchar *iso_date)
1561 GTimeVal stamp;
1563 if (!g_time_val_from_iso8601 (iso_date, &stamp))
1564 return (time_t) -1;
1566 return (time_t) stamp.tv_sec;
1571 GQuark
1572 g_bookmark_file_error_quark (void)
1574 return g_quark_from_static_string ("egg-bookmark-file-error-quark");
1579 /********************
1580 * Public API *
1581 ********************/
1584 * g_bookmark_file_new:
1586 * Creates a new empty #GBookmarkFile object.
1588 * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1589 * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1590 * file.
1592 * Return value: an empty #GBookmarkFile
1594 * Since: 2.12
1596 GBookmarkFile *
1597 g_bookmark_file_new (void)
1599 GBookmarkFile *bookmark;
1601 bookmark = g_new (GBookmarkFile, 1);
1603 g_bookmark_file_init (bookmark);
1605 return bookmark;
1609 * g_bookmark_file_free:
1610 * @bookmark: a #GBookmarkFile
1612 * Frees a #GBookmarkFile.
1614 * Since: 2.12
1616 void
1617 g_bookmark_file_free (GBookmarkFile *bookmark)
1619 if (!bookmark)
1620 return;
1622 g_bookmark_file_clear (bookmark);
1624 g_free (bookmark);
1628 * g_bookmark_file_load_from_data:
1629 * @bookmark: an empty #GBookmarkFile struct
1630 * @data: desktop bookmarks loaded in memory
1631 * @length: the length of @data in bytes
1632 * @error: return location for a #GError, or %NULL
1634 * Loads a bookmark file from memory into an empty #GBookmarkFile
1635 * structure. If the object cannot be created then @error is set to a
1636 * #GBookmarkFileError.
1638 * Return value: %TRUE if a desktop bookmark could be loaded.
1640 * Since: 2.12
1642 gboolean
1643 g_bookmark_file_load_from_data (GBookmarkFile *bookmark,
1644 const gchar *data,
1645 gsize length,
1646 GError **error)
1648 GError *parse_error;
1649 gboolean retval;
1651 g_return_val_if_fail (bookmark != NULL, FALSE);
1652 g_return_val_if_fail (data != NULL, FALSE);
1653 g_return_val_if_fail (length != 0, FALSE);
1655 if (length == (gsize) -1)
1656 length = strlen (data);
1658 if (bookmark->items)
1660 g_bookmark_file_clear (bookmark);
1661 g_bookmark_file_init (bookmark);
1664 parse_error = NULL;
1665 retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1666 if (!retval)
1668 g_propagate_error (error, parse_error);
1670 return FALSE;
1673 return TRUE;
1677 * g_bookmark_file_load_from_file:
1678 * @bookmark: an empty #GBookmarkFile struct
1679 * @filename: the path of a filename to load, in the GLib file name encoding
1680 * @error: return location for a #GError, or %NULL
1682 * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1683 * If the file could not be loaded then @error is set to either a #GFileError
1684 * or #GBookmarkFileError.
1686 * Return value: %TRUE if a desktop bookmark file could be loaded
1688 * Since: 2.12
1690 gboolean
1691 g_bookmark_file_load_from_file (GBookmarkFile *bookmark,
1692 const gchar *filename,
1693 GError **error)
1695 gchar *buffer;
1696 gsize len;
1697 GError *read_error;
1698 gboolean retval;
1700 g_return_val_if_fail (bookmark != NULL, FALSE);
1701 g_return_val_if_fail (filename != NULL, FALSE);
1703 read_error = NULL;
1704 g_file_get_contents (filename, &buffer, &len, &read_error);
1705 if (read_error)
1707 g_propagate_error (error, read_error);
1709 return FALSE;
1712 read_error = NULL;
1713 retval = g_bookmark_file_load_from_data (bookmark,
1714 buffer,
1715 len,
1716 &read_error);
1717 if (read_error)
1719 g_propagate_error (error, read_error);
1721 g_free (buffer);
1723 return FALSE;
1726 g_free (buffer);
1728 return retval;
1732 /* Iterates through all the directories in *dirs trying to
1733 * find file. When it successfully locates file, returns a
1734 * string its absolute path. It also leaves the unchecked
1735 * directories in *dirs. You should free the returned string
1737 * Adapted from gkeyfile.c
1739 static gchar *
1740 find_file_in_data_dirs (const gchar *file,
1741 gchar ***dirs,
1742 GError **error)
1744 gchar **data_dirs, *data_dir, *path;
1746 path = NULL;
1748 if (dirs == NULL)
1749 return NULL;
1751 data_dirs = *dirs;
1752 path = NULL;
1753 while (data_dirs && (data_dir = *data_dirs) && !path)
1755 gchar *candidate_file, *sub_dir;
1757 candidate_file = (gchar *) file;
1758 sub_dir = g_strdup ("");
1759 while (candidate_file != NULL && !path)
1761 gchar *p;
1763 path = g_build_filename (data_dir, sub_dir,
1764 candidate_file, NULL);
1766 candidate_file = strchr (candidate_file, '-');
1768 if (candidate_file == NULL)
1769 break;
1771 candidate_file++;
1773 g_free (sub_dir);
1774 sub_dir = g_strndup (file, candidate_file - file - 1);
1776 for (p = sub_dir; *p != '\0'; p++)
1778 if (*p == '-')
1779 *p = G_DIR_SEPARATOR;
1782 g_free (sub_dir);
1783 data_dirs++;
1786 *dirs = data_dirs;
1788 if (!path)
1790 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1791 G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1792 _("No valid bookmark file found in data dirs"));
1794 return NULL;
1797 return path;
1802 * g_bookmark_file_load_from_data_dirs:
1803 * @bookmark: a #GBookmarkFile
1804 * @file: a relative path to a filename to open and parse
1805 * @full_path: return location for a string containing the full path
1806 * of the file, or %NULL
1807 * @error: return location for a #GError, or %NULL
1809 * This function looks for a desktop bookmark file named @file in the
1810 * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
1811 * loads the file into @bookmark and returns the file's full path in
1812 * @full_path. If the file could not be loaded then an %error is
1813 * set to either a #GFileError or #GBookmarkFileError.
1815 * Return value: %TRUE if a key file could be loaded, %FALSE othewise
1817 * Since: 2.12
1819 gboolean
1820 g_bookmark_file_load_from_data_dirs (GBookmarkFile *bookmark,
1821 const gchar *file,
1822 gchar **full_path,
1823 GError **error)
1825 GError *file_error = NULL;
1826 gchar **all_data_dirs, **data_dirs;
1827 const gchar *user_data_dir;
1828 const gchar * const * system_data_dirs;
1829 gsize i, j;
1830 gchar *output_path;
1831 gboolean found_file;
1833 g_return_val_if_fail (bookmark != NULL, FALSE);
1834 g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1836 user_data_dir = g_get_user_data_dir ();
1837 system_data_dirs = g_get_system_data_dirs ();
1838 all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1840 i = 0;
1841 all_data_dirs[i++] = g_strdup (user_data_dir);
1843 j = 0;
1844 while (system_data_dirs[j] != NULL)
1845 all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1847 found_file = FALSE;
1848 data_dirs = all_data_dirs;
1849 output_path = NULL;
1850 while (*data_dirs != NULL && !found_file)
1852 g_free (output_path);
1854 output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1856 if (file_error)
1858 g_propagate_error (error, file_error);
1859 break;
1862 found_file = g_bookmark_file_load_from_file (bookmark,
1863 output_path,
1864 &file_error);
1865 if (file_error)
1867 g_propagate_error (error, file_error);
1868 break;
1872 if (found_file && full_path)
1873 *full_path = output_path;
1874 else
1875 g_free (output_path);
1877 g_strfreev (all_data_dirs);
1879 return found_file;
1884 * g_bookmark_file_to_data:
1885 * @bookmark: a #GBookmarkFile
1886 * @length: return location for the length of the returned string, or %NULL
1887 * @error: return location for a #GError, or %NULL
1889 * This function outputs @bookmark as a string.
1891 * Return value: a newly allocated string holding
1892 * the contents of the #GBookmarkFile
1894 * Since: 2.12
1896 gchar *
1897 g_bookmark_file_to_data (GBookmarkFile *bookmark,
1898 gsize *length,
1899 GError **error)
1901 GError *write_error = NULL;
1902 gchar *retval;
1904 g_return_val_if_fail (bookmark != NULL, NULL);
1906 retval = g_bookmark_file_dump (bookmark, length, &write_error);
1907 if (write_error)
1909 g_propagate_error (error, write_error);
1911 return NULL;
1914 return retval;
1918 * g_bookmark_file_to_file:
1919 * @bookmark: a #GBookmarkFile
1920 * @filename: path of the output file
1921 * @error: return location for a #GError, or %NULL
1923 * This function outputs @bookmark into a file. The write process is
1924 * guaranteed to be atomic by using g_file_set_contents() internally.
1926 * Return value: %TRUE if the file was successfully written.
1928 * Since: 2.12
1930 gboolean
1931 g_bookmark_file_to_file (GBookmarkFile *bookmark,
1932 const gchar *filename,
1933 GError **error)
1935 gchar *data;
1936 GError *data_error, *write_error;
1937 gsize len;
1938 gboolean retval;
1940 g_return_val_if_fail (bookmark != NULL, FALSE);
1941 g_return_val_if_fail (filename != NULL, FALSE);
1943 data_error = NULL;
1944 data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1945 if (data_error)
1947 g_propagate_error (error, data_error);
1949 return FALSE;
1952 write_error = NULL;
1953 g_file_set_contents (filename, data, len, &write_error);
1954 if (write_error)
1956 g_propagate_error (error, write_error);
1958 retval = FALSE;
1960 else
1961 retval = TRUE;
1963 g_free (data);
1965 return retval;
1968 static BookmarkItem *
1969 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1970 const gchar *uri)
1972 g_assert (bookmark != NULL && uri != NULL);
1974 return g_hash_table_lookup (bookmark->items_by_uri, uri);
1977 /* this function adds a new item to the list */
1978 static void
1979 g_bookmark_file_add_item (GBookmarkFile *bookmark,
1980 BookmarkItem *item,
1981 GError **error)
1983 g_assert (bookmark != NULL);
1984 g_assert (item != NULL);
1986 /* this should never happen; and if it does, then we are
1987 * screwing up something big time.
1989 if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
1991 g_set_error (error, G_BOOKMARK_FILE_ERROR,
1992 G_BOOKMARK_FILE_ERROR_INVALID_URI,
1993 _("A bookmark for URI '%s' already exists"),
1994 item->uri);
1995 return;
1998 bookmark->items = g_list_prepend (bookmark->items, item);
2000 g_hash_table_replace (bookmark->items_by_uri,
2001 item->uri,
2002 item);
2004 if (item->added == (time_t) -1)
2005 item->added = time (NULL);
2007 if (item->modified == (time_t) -1)
2008 item->modified = time (NULL);
2012 * g_bookmark_file_remove_item:
2013 * @bookmark: a #GBookmarkFile
2014 * @uri: a valid URI
2015 * @error: return location for a #GError, or %NULL
2017 * Removes the bookmark for @uri from the bookmark file @bookmark.
2019 * Return value: %TRUE if the bookmark was removed successfully.
2021 * Since: 2.12
2023 gboolean
2024 g_bookmark_file_remove_item (GBookmarkFile *bookmark,
2025 const gchar *uri,
2026 GError **error)
2028 BookmarkItem *item;
2030 g_return_val_if_fail (bookmark != NULL, FALSE);
2031 g_return_val_if_fail (uri != NULL, FALSE);
2033 item = g_bookmark_file_lookup_item (bookmark, uri);
2035 if (!item)
2037 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2038 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2039 _("No bookmark found for URI '%s'"),
2040 uri);
2041 return FALSE;
2044 bookmark->items = g_list_remove (bookmark->items, item);
2045 g_hash_table_remove (bookmark->items_by_uri, item->uri);
2047 bookmark_item_free (item);
2049 return TRUE;
2053 * g_bookmark_file_has_item:
2054 * @bookmark: a #GBookmarkFile
2055 * @uri: a valid URI
2057 * Looks whether the desktop bookmark has an item with its URI set to @uri.
2059 * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2061 * Since: 2.12
2063 gboolean
2064 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2065 const gchar *uri)
2067 g_return_val_if_fail (bookmark != NULL, FALSE);
2068 g_return_val_if_fail (uri != NULL, FALSE);
2070 return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2074 * g_bookmark_file_get_uris:
2075 * @bookmark: a #GBookmarkFile
2076 * @length: return location for the number of returned URIs, or %NULL
2078 * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2079 * The array of returned URIs will be %NULL-terminated, so @length may
2080 * optionally be %NULL.
2082 * Return value: a newly allocated %NULL-terminated array of strings.
2083 * Use g_strfreev() to free it.
2085 * Since: 2.12
2087 gchar **
2088 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2089 gsize *length)
2091 GList *l;
2092 gchar **uris;
2093 gsize i, n_items;
2095 g_return_val_if_fail (bookmark != NULL, NULL);
2097 n_items = g_list_length (bookmark->items);
2098 uris = g_new0 (gchar *, n_items + 1);
2100 for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2102 BookmarkItem *item = (BookmarkItem *) l->data;
2104 g_assert (item != NULL);
2106 uris[i++] = g_strdup (item->uri);
2108 uris[i] = NULL;
2110 if (length)
2111 *length = i;
2113 return uris;
2117 * g_bookmark_file_set_title:
2118 * @bookmark: a #GBookmarkFile
2119 * @uri: a valid URI or %NULL
2120 * @title: a UTF-8 encoded string
2122 * Sets @title as the title of the bookmark for @uri inside the
2123 * bookmark file @bookmark.
2125 * If @uri is %NULL, the title of @bookmark is set.
2127 * If a bookmark for @uri cannot be found then it is created.
2129 * Since: 2.12
2131 void
2132 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2133 const gchar *uri,
2134 const gchar *title)
2136 g_return_if_fail (bookmark != NULL);
2138 if (!uri)
2140 g_free (bookmark->title);
2141 bookmark->title = g_strdup (title);
2143 else
2145 BookmarkItem *item;
2147 item = g_bookmark_file_lookup_item (bookmark, uri);
2148 if (!item)
2150 item = bookmark_item_new (uri);
2151 g_bookmark_file_add_item (bookmark, item, NULL);
2154 g_free (item->title);
2155 item->title = g_strdup (title);
2157 item->modified = time (NULL);
2162 * g_bookmark_file_get_title:
2163 * @bookmark: a #GBookmarkFile
2164 * @uri: a valid URI or %NULL
2165 * @error: return location for a #GError, or %NULL
2167 * Returns the title of the bookmark for @uri.
2169 * If @uri is %NULL, the title of @bookmark is returned.
2171 * In the event the URI cannot be found, %NULL is returned and
2172 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2174 * Return value: a newly allocated string or %NULL if the specified
2175 * URI cannot be found.
2177 * Since: 2.12
2179 gchar *
2180 g_bookmark_file_get_title (GBookmarkFile *bookmark,
2181 const gchar *uri,
2182 GError **error)
2184 BookmarkItem *item;
2186 g_return_val_if_fail (bookmark != NULL, NULL);
2188 if (!uri)
2189 return g_strdup (bookmark->title);
2191 item = g_bookmark_file_lookup_item (bookmark, uri);
2192 if (!item)
2194 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2195 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2196 _("No bookmark found for URI '%s'"),
2197 uri);
2198 return NULL;
2201 return g_strdup (item->title);
2205 * g_bookmark_file_set_description:
2206 * @bookmark: a #GBookmarkFile
2207 * @uri: a valid URI or %NULL
2208 * @description: a string
2210 * Sets @description as the description of the bookmark for @uri.
2212 * If @uri is %NULL, the description of @bookmark is set.
2214 * If a bookmark for @uri cannot be found then it is created.
2216 * Since: 2.12
2218 void
2219 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2220 const gchar *uri,
2221 const gchar *description)
2223 g_return_if_fail (bookmark != NULL);
2225 if (!uri)
2227 g_free (bookmark->description);
2228 bookmark->description = g_strdup (description);
2230 else
2232 BookmarkItem *item;
2234 item = g_bookmark_file_lookup_item (bookmark, uri);
2235 if (!item)
2237 item = bookmark_item_new (uri);
2238 g_bookmark_file_add_item (bookmark, item, NULL);
2241 g_free (item->description);
2242 item->description = g_strdup (description);
2244 item->modified = time (NULL);
2249 * g_bookmark_file_get_description:
2250 * @bookmark: a #GBookmarkFile
2251 * @uri: a valid URI
2252 * @error: return location for a #GError, or %NULL
2254 * Retrieves the description of the bookmark for @uri.
2256 * In the event the URI cannot be found, %NULL is returned and
2257 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2259 * Return value: a newly allocated string or %NULL if the specified
2260 * URI cannot be found.
2262 * Since: 2.12
2264 gchar *
2265 g_bookmark_file_get_description (GBookmarkFile *bookmark,
2266 const gchar *uri,
2267 GError **error)
2269 BookmarkItem *item;
2271 g_return_val_if_fail (bookmark != NULL, NULL);
2273 if (!uri)
2274 return g_strdup (bookmark->description);
2276 item = g_bookmark_file_lookup_item (bookmark, uri);
2277 if (!item)
2279 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2280 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2281 _("No bookmark found for URI '%s'"),
2282 uri);
2283 return NULL;
2286 return g_strdup (item->description);
2290 * g_bookmark_file_set_mime_type:
2291 * @bookmark: a #GBookmarkFile
2292 * @uri: a valid URI
2293 * @mime_type: a MIME type
2295 * Sets @mime_type as the MIME type of the bookmark for @uri.
2297 * If a bookmark for @uri cannot be found then it is created.
2299 * Since: 2.12
2301 void
2302 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2303 const gchar *uri,
2304 const gchar *mime_type)
2306 BookmarkItem *item;
2308 g_return_if_fail (bookmark != NULL);
2309 g_return_if_fail (uri != NULL);
2310 g_return_if_fail (mime_type != NULL);
2312 item = g_bookmark_file_lookup_item (bookmark, uri);
2313 if (!item)
2315 item = bookmark_item_new (uri);
2316 g_bookmark_file_add_item (bookmark, item, NULL);
2319 if (!item->metadata)
2320 item->metadata = bookmark_metadata_new ();
2322 if (item->metadata->mime_type != NULL)
2323 g_free (item->metadata->mime_type);
2325 item->metadata->mime_type = g_strdup (mime_type);
2326 item->modified = time (NULL);
2330 * g_bookmark_file_get_mime_type:
2331 * @bookmark: a #GBookmarkFile
2332 * @uri: a valid URI
2333 * @error: return location for a #GError, or %NULL
2335 * Retrieves the MIME type of the resource pointed by @uri.
2337 * In the event the URI cannot be found, %NULL is returned and
2338 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2339 * event that the MIME type cannot be found, %NULL is returned and
2340 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2342 * Return value: a newly allocated string or %NULL if the specified
2343 * URI cannot be found.
2345 * Since: 2.12
2347 gchar *
2348 g_bookmark_file_get_mime_type (GBookmarkFile *bookmark,
2349 const gchar *uri,
2350 GError **error)
2352 BookmarkItem *item;
2354 g_return_val_if_fail (bookmark != NULL, NULL);
2355 g_return_val_if_fail (uri != NULL, NULL);
2357 item = g_bookmark_file_lookup_item (bookmark, uri);
2358 if (!item)
2360 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2361 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2362 _("No bookmark found for URI '%s'"),
2363 uri);
2364 return NULL;
2367 if (!item->metadata)
2369 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2370 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2371 _("No MIME type defined in the bookmark for URI '%s'"),
2372 uri);
2373 return NULL;
2376 return g_strdup (item->metadata->mime_type);
2380 * g_bookmark_file_set_is_private:
2381 * @bookmark: a #GBookmarkFile
2382 * @uri: a valid URI
2383 * @is_private: %TRUE if the bookmark should be marked as private
2385 * Sets the private flag of the bookmark for @uri.
2387 * If a bookmark for @uri cannot be found then it is created.
2389 * Since: 2.12
2391 void
2392 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2393 const gchar *uri,
2394 gboolean is_private)
2396 BookmarkItem *item;
2398 g_return_if_fail (bookmark != NULL);
2399 g_return_if_fail (uri != NULL);
2401 item = g_bookmark_file_lookup_item (bookmark, uri);
2402 if (!item)
2404 item = bookmark_item_new (uri);
2405 g_bookmark_file_add_item (bookmark, item, NULL);
2408 if (!item->metadata)
2409 item->metadata = bookmark_metadata_new ();
2411 item->metadata->is_private = (is_private == TRUE);
2412 item->modified = time (NULL);
2416 * g_bookmark_file_get_is_private:
2417 * @bookmark: a #GBookmarkFile
2418 * @uri: a valid URI
2419 * @error: return location for a #GError, or %NULL
2421 * Gets whether the private flag of the bookmark for @uri is set.
2423 * In the event the URI cannot be found, %FALSE is returned and
2424 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
2425 * event that the private flag cannot be found, %FALSE is returned and
2426 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2428 * Return value: %TRUE if the private flag is set, %FALSE otherwise.
2430 * Since: 2.12
2432 gboolean
2433 g_bookmark_file_get_is_private (GBookmarkFile *bookmark,
2434 const gchar *uri,
2435 GError **error)
2437 BookmarkItem *item;
2439 g_return_val_if_fail (bookmark != NULL, FALSE);
2440 g_return_val_if_fail (uri != NULL, FALSE);
2442 item = g_bookmark_file_lookup_item (bookmark, uri);
2443 if (!item)
2445 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2446 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2447 _("No bookmark found for URI '%s'"),
2448 uri);
2449 return FALSE;
2452 if (!item->metadata)
2454 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2455 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2456 _("No private flag has been defined in bookmark for URI '%s'"),
2457 uri);
2458 return FALSE;
2461 return item->metadata->is_private;
2465 * g_bookmark_file_set_added:
2466 * @bookmark: a #GBookmarkFile
2467 * @uri: a valid URI
2468 * @added: a timestamp or -1 to use the current time
2470 * Sets the time the bookmark for @uri was added into @bookmark.
2472 * If no bookmark for @uri is found then it is created.
2474 * Since: 2.12
2476 void
2477 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2478 const gchar *uri,
2479 time_t added)
2481 BookmarkItem *item;
2483 g_return_if_fail (bookmark != NULL);
2484 g_return_if_fail (uri != NULL);
2486 item = g_bookmark_file_lookup_item (bookmark, uri);
2487 if (!item)
2489 item = bookmark_item_new (uri);
2490 g_bookmark_file_add_item (bookmark, item, NULL);
2493 if (added == (time_t) -1)
2494 time (&added);
2496 item->added = added;
2497 item->modified = added;
2501 * g_bookmark_file_get_added:
2502 * @bookmark: a #GBookmarkFile
2503 * @uri: a valid URI
2504 * @error: return location for a #GError, or %NULL
2506 * Gets the time the bookmark for @uri was added to @bookmark
2508 * In the event the URI cannot be found, -1 is returned and
2509 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2511 * Return value: a timestamp
2513 * Since: 2.12
2515 time_t
2516 g_bookmark_file_get_added (GBookmarkFile *bookmark,
2517 const gchar *uri,
2518 GError **error)
2520 BookmarkItem *item;
2522 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2523 g_return_val_if_fail (uri != NULL, (time_t) -1);
2525 item = g_bookmark_file_lookup_item (bookmark, uri);
2526 if (!item)
2528 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2529 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2530 _("No bookmark found for URI '%s'"),
2531 uri);
2532 return (time_t) -1;
2535 return item->added;
2539 * g_bookmark_file_set_modified:
2540 * @bookmark: a #GBookmarkFile
2541 * @uri: a valid URI
2542 * @modified: a timestamp or -1 to use the current time
2544 * Sets the last time the bookmark for @uri was last modified.
2546 * If no bookmark for @uri is found then it is created.
2548 * The "modified" time should only be set when the bookmark's meta-data
2549 * was actually changed. Every function of #GBookmarkFile that
2550 * modifies a bookmark also changes the modification time, except for
2551 * g_bookmark_file_set_visited().
2553 * Since: 2.12
2555 void
2556 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2557 const gchar *uri,
2558 time_t modified)
2560 BookmarkItem *item;
2562 g_return_if_fail (bookmark != NULL);
2563 g_return_if_fail (uri != NULL);
2565 item = g_bookmark_file_lookup_item (bookmark, uri);
2566 if (!item)
2568 item = bookmark_item_new (uri);
2569 g_bookmark_file_add_item (bookmark, item, NULL);
2572 if (modified == (time_t) -1)
2573 time (&modified);
2575 item->modified = modified;
2579 * g_bookmark_file_get_modified:
2580 * @bookmark: a #GBookmarkFile
2581 * @uri: a valid URI
2582 * @error: return location for a #GError, or %NULL
2584 * Gets the time when the bookmark for @uri was last modified.
2586 * In the event the URI cannot be found, -1 is returned and
2587 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2589 * Return value: a timestamp
2591 * Since: 2.12
2593 time_t
2594 g_bookmark_file_get_modified (GBookmarkFile *bookmark,
2595 const gchar *uri,
2596 GError **error)
2598 BookmarkItem *item;
2600 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2601 g_return_val_if_fail (uri != NULL, (time_t) -1);
2603 item = g_bookmark_file_lookup_item (bookmark, uri);
2604 if (!item)
2606 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2607 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2608 _("No bookmark found for URI '%s'"),
2609 uri);
2610 return (time_t) -1;
2613 return item->modified;
2617 * g_bookmark_file_set_visited:
2618 * @bookmark: a #GBookmarkFile
2619 * @uri: a valid URI
2620 * @visited: a timestamp or -1 to use the current time
2622 * Sets the time the bookmark for @uri was last visited.
2624 * If no bookmark for @uri is found then it is created.
2626 * The "visited" time should only be set if the bookmark was launched,
2627 * either using the command line retrieved by g_bookmark_file_get_app_info()
2628 * or by the default application for the bookmark's MIME type, retrieved
2629 * using g_bookmark_file_get_mime_type(). Changing the "visited" time
2630 * does not affect the "modified" time.
2632 * Since: 2.12
2634 void
2635 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2636 const gchar *uri,
2637 time_t visited)
2639 BookmarkItem *item;
2641 g_return_if_fail (bookmark != NULL);
2642 g_return_if_fail (uri != NULL);
2644 item = g_bookmark_file_lookup_item (bookmark, uri);
2645 if (!item)
2647 item = bookmark_item_new (uri);
2648 g_bookmark_file_add_item (bookmark, item, NULL);
2651 if (visited == (time_t) -1)
2652 time (&visited);
2654 item->visited = visited;
2658 * g_bookmark_file_get_visited:
2659 * @bookmark: a #GBookmarkFile
2660 * @uri: a valid URI
2661 * @error: return location for a #GError, or %NULL
2663 * Gets the time the bookmark for @uri was last visited.
2665 * In the event the URI cannot be found, -1 is returned and
2666 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2668 * Return value: a timestamp.
2670 * Since: 2.12
2672 time_t
2673 g_bookmark_file_get_visited (GBookmarkFile *bookmark,
2674 const gchar *uri,
2675 GError **error)
2677 BookmarkItem *item;
2679 g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2680 g_return_val_if_fail (uri != NULL, (time_t) -1);
2682 item = g_bookmark_file_lookup_item (bookmark, uri);
2683 if (!item)
2685 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2686 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2687 _("No bookmark found for URI '%s'"),
2688 uri);
2689 return (time_t) -1;
2692 return item->visited;
2696 * g_bookmark_file_has_group:
2697 * @bookmark: a #GBookmarkFile
2698 * @uri: a valid URI
2699 * @group: the group name to be searched
2700 * @error: return location for a #GError, or %NULL
2702 * Checks whether @group appears in the list of groups to which
2703 * the bookmark for @uri belongs to.
2705 * In the event the URI cannot be found, %FALSE is returned and
2706 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2708 * Return value: %TRUE if @group was found.
2710 * Since: 2.12
2712 gboolean
2713 g_bookmark_file_has_group (GBookmarkFile *bookmark,
2714 const gchar *uri,
2715 const gchar *group,
2716 GError **error)
2718 BookmarkItem *item;
2719 GList *l;
2721 g_return_val_if_fail (bookmark != NULL, FALSE);
2722 g_return_val_if_fail (uri != NULL, FALSE);
2724 item = g_bookmark_file_lookup_item (bookmark, uri);
2725 if (!item)
2727 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2728 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2729 _("No bookmark found for URI '%s'"),
2730 uri);
2731 return FALSE;
2734 if (!item->metadata)
2735 return FALSE;
2737 for (l = item->metadata->groups; l != NULL; l = l->next)
2739 if (strcmp (l->data, group) == 0)
2740 return TRUE;
2743 return FALSE;
2748 * g_bookmark_file_add_group:
2749 * @bookmark: a #GBookmarkFile
2750 * @uri: a valid URI
2751 * @group: the group name to be added
2753 * Adds @group to the list of groups to which the bookmark for @uri
2754 * belongs to.
2756 * If no bookmark for @uri is found then it is created.
2758 * Since: 2.12
2760 void
2761 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2762 const gchar *uri,
2763 const gchar *group)
2765 BookmarkItem *item;
2767 g_return_if_fail (bookmark != NULL);
2768 g_return_if_fail (uri != NULL);
2769 g_return_if_fail (group != NULL && group[0] != '\0');
2771 item = g_bookmark_file_lookup_item (bookmark, uri);
2772 if (!item)
2774 item = bookmark_item_new (uri);
2775 g_bookmark_file_add_item (bookmark, item, NULL);
2778 if (!item->metadata)
2779 item->metadata = bookmark_metadata_new ();
2781 if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2783 item->metadata->groups = g_list_prepend (item->metadata->groups,
2784 g_strdup (group));
2786 item->modified = time (NULL);
2791 * g_bookmark_file_remove_group:
2792 * @bookmark: a #GBookmarkFile
2793 * @uri: a valid URI
2794 * @group: the group name to be removed
2795 * @error: return location for a #GError, or %NULL
2797 * Removes @group from the list of groups to which the bookmark
2798 * for @uri belongs to.
2800 * In the event the URI cannot be found, %FALSE is returned and
2801 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2802 * In the event no group was defined, %FALSE is returned and
2803 * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2805 * Return value: %TRUE if @group was successfully removed.
2807 * Since: 2.12
2809 gboolean
2810 g_bookmark_file_remove_group (GBookmarkFile *bookmark,
2811 const gchar *uri,
2812 const gchar *group,
2813 GError **error)
2815 BookmarkItem *item;
2816 GList *l;
2818 g_return_val_if_fail (bookmark != NULL, FALSE);
2819 g_return_val_if_fail (uri != NULL, FALSE);
2821 item = g_bookmark_file_lookup_item (bookmark, uri);
2822 if (!item)
2824 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2825 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2826 _("No bookmark found for URI '%s'"),
2827 uri);
2828 return FALSE;
2831 if (!item->metadata)
2833 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2834 G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2835 _("No groups set in bookmark for URI '%s'"),
2836 uri);
2837 return FALSE;
2840 for (l = item->metadata->groups; l != NULL; l = l->next)
2842 if (strcmp (l->data, group) == 0)
2844 item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2845 g_free (l->data);
2846 g_list_free_1 (l);
2848 item->modified = time (NULL);
2850 return TRUE;
2854 return FALSE;
2858 * g_bookmark_file_set_groups:
2859 * @bookmark: a #GBookmarkFile
2860 * @uri: an item's URI
2861 * @groups: an array of group names, or %NULL to remove all groups
2862 * @length: number of group name values in @groups
2864 * Sets a list of group names for the item with URI @uri. Each previously
2865 * set group name list is removed.
2867 * If @uri cannot be found then an item for it is created.
2869 * Since: 2.12
2871 void
2872 g_bookmark_file_set_groups (GBookmarkFile *bookmark,
2873 const gchar *uri,
2874 const gchar **groups,
2875 gsize length)
2877 BookmarkItem *item;
2878 gsize i;
2880 g_return_if_fail (bookmark != NULL);
2881 g_return_if_fail (uri != NULL);
2882 g_return_if_fail (groups != NULL);
2884 item = g_bookmark_file_lookup_item (bookmark, uri);
2885 if (!item)
2887 item = bookmark_item_new (uri);
2888 g_bookmark_file_add_item (bookmark, item, NULL);
2891 if (!item->metadata)
2892 item->metadata = bookmark_metadata_new ();
2894 if (item->metadata->groups != NULL)
2896 g_list_foreach (item->metadata->groups,
2897 (GFunc) g_free,
2898 NULL);
2899 g_list_free (item->metadata->groups);
2900 item->metadata->groups = NULL;
2903 if (groups)
2905 for (i = 0; groups[i] != NULL && i < length; i++)
2906 item->metadata->groups = g_list_append (item->metadata->groups,
2907 g_strdup (groups[i]));
2910 item->modified = time (NULL);
2914 * g_bookmark_file_get_groups:
2915 * @bookmark: a #GBookmarkFile
2916 * @uri: a valid URI
2917 * @length: return location for the length of the returned string, or %NULL
2918 * @error: return location for a #GError, or %NULL
2920 * Retrieves the list of group names of the bookmark for @uri.
2922 * In the event the URI cannot be found, %NULL is returned and
2923 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2925 * The returned array is %NULL terminated, so @length may optionally
2926 * be %NULL.
2928 * Return value: a newly allocated %NULL-terminated array of group names.
2929 * Use g_strfreev() to free it.
2931 * Since: 2.12
2933 gchar **
2934 g_bookmark_file_get_groups (GBookmarkFile *bookmark,
2935 const gchar *uri,
2936 gsize *length,
2937 GError **error)
2939 BookmarkItem *item;
2940 GList *l;
2941 gsize len, i;
2942 gchar **retval;
2944 g_return_val_if_fail (bookmark != NULL, NULL);
2945 g_return_val_if_fail (uri != NULL, NULL);
2947 item = g_bookmark_file_lookup_item (bookmark, uri);
2948 if (!item)
2950 g_set_error (error, G_BOOKMARK_FILE_ERROR,
2951 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2952 _("No bookmark found for URI '%s'"),
2953 uri);
2954 return NULL;
2957 if (!item->metadata)
2959 if (length)
2960 *length = 0;
2962 return NULL;
2965 len = g_list_length (item->metadata->groups);
2966 retval = g_new0 (gchar *, len + 1);
2967 for (l = g_list_last (item->metadata->groups), i = 0;
2968 l != NULL;
2969 l = l->prev)
2971 gchar *group_name = (gchar *) l->data;
2973 g_assert (group_name != NULL);
2975 retval[i++] = g_strdup (group_name);
2977 retval[i] = NULL;
2979 if (length)
2980 *length = len;
2982 return retval;
2986 * g_bookmark_file_add_application:
2987 * @bookmark: a #GBookmarkFile
2988 * @uri: a valid URI
2989 * @name: the name of the application registering the bookmark
2990 * or %NULL
2991 * @exec: command line to be used to launch the bookmark or %NULL
2993 * Adds the application with @name and @exec to the list of
2994 * applications that have registered a bookmark for @uri into
2995 * @bookmark.
2997 * Every bookmark inside a #GBookmarkFile must have at least an
2998 * application registered. Each application must provide a name, a
2999 * command line useful for launching the bookmark, the number of times
3000 * the bookmark has been registered by the application and the last
3001 * time the application registered this bookmark.
3003 * If @name is %NULL, the name of the application will be the
3004 * same returned by g_get_application(); if @exec is %NULL, the
3005 * command line will be a composition of the program name as
3006 * returned by g_get_prgname() and the "%u" modifier, which will be
3007 * expanded to the bookmark's URI.
3009 * This function will automatically take care of updating the
3010 * registrations count and timestamping in case an application
3011 * with the same @name had already registered a bookmark for
3012 * @uri inside @bookmark.
3014 * If no bookmark for @uri is found, one is created.
3016 * Since: 2.12
3018 void
3019 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3020 const gchar *uri,
3021 const gchar *name,
3022 const gchar *exec)
3024 BookmarkItem *item;
3025 gchar *app_name, *app_exec;
3027 g_return_if_fail (bookmark != NULL);
3028 g_return_if_fail (uri != NULL);
3030 item = g_bookmark_file_lookup_item (bookmark, uri);
3031 if (!item)
3033 item = bookmark_item_new (uri);
3034 g_bookmark_file_add_item (bookmark, item, NULL);
3037 if (name && name[0] != '\0')
3038 app_name = g_strdup (name);
3039 else
3040 app_name = g_strdup (g_get_application_name ());
3042 if (exec && exec[0] != '\0')
3043 app_exec = g_strdup (exec);
3044 else
3045 app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3047 g_bookmark_file_set_app_info (bookmark, uri,
3048 app_name,
3049 app_exec,
3051 (time_t) -1,
3052 NULL);
3054 g_free (app_exec);
3055 g_free (app_name);
3059 * g_bookmark_file_remove_application:
3060 * @bookmark: a #GBookmarkFile
3061 * @uri: a valid URI
3062 * @name: the name of the application
3063 * @error: return location for a #GError or %NULL
3065 * Removes application registered with @name from the list of applications
3066 * that have registered a bookmark for @uri inside @bookmark.
3068 * In the event the URI cannot be found, %FALSE is returned and
3069 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3070 * In the event that no application with name @app_name has registered
3071 * a bookmark for @uri, %FALSE is returned and error is set to
3072 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3074 * Return value: %TRUE if the application was successfully removed.
3076 * Since: 2.12
3078 gboolean
3079 g_bookmark_file_remove_application (GBookmarkFile *bookmark,
3080 const gchar *uri,
3081 const gchar *name,
3082 GError **error)
3084 GError *set_error;
3085 gboolean retval;
3087 g_return_val_if_fail (bookmark != NULL, FALSE);
3088 g_return_val_if_fail (uri != NULL, FALSE);
3089 g_return_val_if_fail (name != NULL, FALSE);
3091 set_error = NULL;
3092 retval = g_bookmark_file_set_app_info (bookmark, uri,
3093 name,
3096 (time_t) -1,
3097 &set_error);
3098 if (set_error)
3100 g_propagate_error (error, set_error);
3102 return FALSE;
3105 return retval;
3109 * g_bookmark_file_has_application:
3110 * @bookmark: a #GBookmarkFile
3111 * @uri: a valid URI
3112 * @name: the name of the application
3113 * @error: return location for a #GError or %NULL
3115 * Checks whether the bookmark for @uri inside @bookmark has been
3116 * registered by application @name.
3118 * In the event the URI cannot be found, %FALSE is returned and
3119 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3121 * Return value: %TRUE if the application @name was found
3123 * Since: 2.12
3125 gboolean
3126 g_bookmark_file_has_application (GBookmarkFile *bookmark,
3127 const gchar *uri,
3128 const gchar *name,
3129 GError **error)
3131 BookmarkItem *item;
3133 g_return_val_if_fail (bookmark != NULL, FALSE);
3134 g_return_val_if_fail (uri != NULL, FALSE);
3135 g_return_val_if_fail (name != NULL, FALSE);
3137 item = g_bookmark_file_lookup_item (bookmark, uri);
3138 if (!item)
3140 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3141 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3142 _("No bookmark found for URI '%s'"),
3143 uri);
3144 return FALSE;
3147 return (NULL != bookmark_item_lookup_app_info (item, name));
3151 * g_bookmark_file_set_app_info:
3152 * @bookmark: a #GBookmarkFile
3153 * @uri: a valid URI
3154 * @name: an application's name
3155 * @exec: an application's command line
3156 * @count: the number of registrations done for this application
3157 * @stamp: the time of the last registration for this application
3158 * @error: return location for a #GError or %NULL
3160 * Sets the meta-data of application @name inside the list of
3161 * applications that have registered a bookmark for @uri inside
3162 * @bookmark.
3164 * You should rarely use this function; use g_bookmark_file_add_application()
3165 * and g_bookmark_file_remove_application() instead.
3167 * @name can be any UTF-8 encoded string used to identify an
3168 * application.
3169 * @exec can have one of these two modifiers: "%f", which will
3170 * be expanded as the local file name retrieved from the bookmark's
3171 * URI; "%u", which will be expanded as the bookmark's URI.
3172 * The expansion is done automatically when retrieving the stored
3173 * command line using the g_bookmark_file_get_app_info() function.
3174 * @count is the number of times the application has registered the
3175 * bookmark; if is < 0, the current registration count will be increased
3176 * by one, if is 0, the application with @name will be removed from
3177 * the list of registered applications.
3178 * @stamp is the Unix time of the last registration; if it is -1, the
3179 * current time will be used.
3181 * If you try to remove an application by setting its registration count to
3182 * zero, and no bookmark for @uri is found, %FALSE is returned and
3183 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3184 * in the event that no application @name has registered a bookmark
3185 * for @uri, %FALSE is returned and error is set to
3186 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. Otherwise, if no bookmark
3187 * for @uri is found, one is created.
3189 * Return value: %TRUE if the application's meta-data was successfully
3190 * changed.
3192 * Since: 2.12
3194 gboolean
3195 g_bookmark_file_set_app_info (GBookmarkFile *bookmark,
3196 const gchar *uri,
3197 const gchar *name,
3198 const gchar *exec,
3199 gint count,
3200 time_t stamp,
3201 GError **error)
3203 BookmarkItem *item;
3204 BookmarkAppInfo *ai;
3206 g_return_val_if_fail (bookmark != NULL, FALSE);
3207 g_return_val_if_fail (uri != NULL, FALSE);
3208 g_return_val_if_fail (name != NULL, FALSE);
3209 g_return_val_if_fail (exec != NULL, FALSE);
3211 item = g_bookmark_file_lookup_item (bookmark, uri);
3212 if (!item)
3214 if (count == 0)
3216 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3217 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3218 _("No bookmark found for URI '%s'"),
3219 uri);
3220 return FALSE;
3222 else
3224 item = bookmark_item_new (uri);
3225 g_bookmark_file_add_item (bookmark, item, NULL);
3229 ai = bookmark_item_lookup_app_info (item, name);
3230 if (!ai)
3232 if (count == 0)
3234 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3235 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3236 _("No application with name '%s' registered a bookmark for '%s'"),
3237 name,
3238 uri);
3239 return FALSE;
3241 else
3243 ai = bookmark_app_info_new (name);
3245 item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3246 g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3250 if (count == 0)
3252 item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3253 g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3254 bookmark_app_info_free (ai);
3256 item->modified = time (NULL);
3258 return TRUE;
3260 else if (count > 0)
3261 ai->count = count;
3262 else
3263 ai->count += 1;
3265 if (stamp != (time_t) -1)
3266 ai->stamp = stamp;
3267 else
3268 ai->stamp = time (NULL);
3270 if (exec && exec[0] != '\0')
3272 g_free (ai->exec);
3273 ai->exec = g_strdup (exec);
3276 item->modified = time (NULL);
3278 return TRUE;
3281 /* expands the application's command line */
3282 static gchar *
3283 expand_exec_line (const gchar *exec_fmt,
3284 const gchar *uri)
3286 GString *exec;
3287 gchar ch;
3289 exec = g_string_new (NULL);
3290 while ((ch = *exec_fmt++) != '\0')
3292 if (ch != '%')
3294 exec = g_string_append_c (exec, ch);
3295 continue;
3298 ch = *exec_fmt++;
3299 switch (ch)
3301 case '\0':
3302 goto out;
3303 case 'u':
3304 g_string_append (exec, uri);
3305 break;
3306 case 'f':
3308 gchar *file = g_filename_from_uri (uri, NULL, NULL);
3309 g_string_append (exec, file);
3310 g_free (file);
3312 break;
3313 case '%':
3314 default:
3315 exec = g_string_append_c (exec, ch);
3316 break;
3320 out:
3321 return g_string_free (exec, FALSE);
3325 * g_bookmark_file_get_app_info:
3326 * @bookmark: a #GBookmarkFile
3327 * @uri: a valid URI
3328 * @name: an application's name
3329 * @exec: location for the command line of the application, or %NULL
3330 * @count: return location for the registration count, or %NULL
3331 * @stamp: return location for the last registration time, or %NULL
3332 * @error: return location for a #GError, or %NULL
3334 * Gets the registration informations of @app_name for the bookmark for
3335 * @uri. See g_bookmark_file_set_app_info() for more informations about
3336 * the returned data.
3338 * The string returned in @app_exec must be freed.
3340 * In the event the URI cannot be found, %FALSE is returned and
3341 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND. In the
3342 * event that no application with name @app_name has registered a bookmark
3343 * for @uri, %FALSE is returned and error is set to
3344 * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3346 * Return value: %TRUE on success.
3348 * Since: 2.12
3350 gboolean
3351 g_bookmark_file_get_app_info (GBookmarkFile *bookmark,
3352 const gchar *uri,
3353 const gchar *name,
3354 gchar **exec,
3355 guint *count,
3356 time_t *stamp,
3357 GError **error)
3359 BookmarkItem *item;
3360 BookmarkAppInfo *ai;
3362 g_return_val_if_fail (bookmark != NULL, FALSE);
3363 g_return_val_if_fail (uri != NULL, FALSE);
3364 g_return_val_if_fail (name != NULL, FALSE);
3366 item = g_bookmark_file_lookup_item (bookmark, uri);
3367 if (!item)
3369 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3370 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3371 _("No bookmark found for URI '%s'"),
3372 uri);
3373 return FALSE;
3376 ai = bookmark_item_lookup_app_info (item, name);
3377 if (!ai)
3379 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3380 G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3381 _("No application with name '%s' registered a bookmark for '%s'"),
3382 name,
3383 uri);
3384 return FALSE;
3387 if (exec)
3388 *exec = expand_exec_line (ai->exec, uri);
3390 if (count)
3391 *count = ai->count;
3393 if (stamp)
3394 *stamp = ai->stamp;
3396 return TRUE;
3400 * g_bookmark_file_get_applications:
3401 * @bookmark: a #GBookmarkFile
3402 * @uri: a valid URI
3403 * @length: return location of the length of the returned list, or %NULL
3404 * @error: return location for a #GError, or %NULL
3406 * Retrieves the names of the applications that have registered the
3407 * bookmark for @uri.
3409 * In the event the URI cannot be found, %NULL is returned and
3410 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3412 * Return value: a newly allocated %NULL-terminated array of strings.
3413 * Use g_strfreev() to free it.
3415 * Since: 2.12
3417 gchar **
3418 g_bookmark_file_get_applications (GBookmarkFile *bookmark,
3419 const gchar *uri,
3420 gsize *length,
3421 GError **error)
3423 BookmarkItem *item;
3424 GList *l;
3425 gchar **apps;
3426 gsize i, n_apps;
3428 g_return_val_if_fail (bookmark != NULL, NULL);
3429 g_return_val_if_fail (uri != NULL, NULL);
3431 item = g_bookmark_file_lookup_item (bookmark, uri);
3432 if (!item)
3434 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3435 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3436 _("No bookmark found for URI '%s'"),
3437 uri);
3438 return NULL;
3441 if (!item->metadata)
3443 if (length)
3444 *length = 0;
3446 return NULL;
3449 n_apps = g_list_length (item->metadata->applications);
3450 apps = g_new0 (gchar *, n_apps + 1);
3452 for (l = g_list_last (item->metadata->applications), i = 0;
3453 l != NULL;
3454 l = l->prev)
3456 BookmarkAppInfo *ai;
3458 ai = (BookmarkAppInfo *) l->data;
3460 g_assert (ai != NULL);
3461 g_assert (ai->name != NULL);
3463 apps[i++] = g_strdup (ai->name);
3465 apps[i] = NULL;
3467 if (length)
3468 *length = i;
3470 return apps;
3474 * g_bookmark_file_get_size:
3475 * @bookmark: a #GBookmarkFile
3477 * Gets the number of bookmarks inside @bookmark.
3479 * Return value: the number of bookmarks
3481 * Since: 2.12
3483 gint
3484 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3486 g_return_val_if_fail (bookmark != NULL, 0);
3488 return g_list_length (bookmark->items);
3492 * g_bookmark_file_move_item:
3493 * @bookmark: a #GBookmarkFile
3494 * @old_uri: a valid URI
3495 * @new_uri: a valid URI, or %NULL
3496 * @error: return location for a #GError or %NULL
3498 * Changes the URI of a bookmark item from @old_uri to @new_uri. Any
3499 * existing bookmark for @new_uri will be overwritten. If @new_uri is
3500 * %NULL, then the bookmark is removed.
3502 * In the event the URI cannot be found, %FALSE is returned and
3503 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3505 * Return value: %TRUE if the URI was successfully changed
3507 * Since: 2.12
3509 gboolean
3510 g_bookmark_file_move_item (GBookmarkFile *bookmark,
3511 const gchar *old_uri,
3512 const gchar *new_uri,
3513 GError **error)
3515 BookmarkItem *item;
3516 GError *remove_error;
3518 g_return_val_if_fail (bookmark != NULL, FALSE);
3519 g_return_val_if_fail (old_uri != NULL, FALSE);
3521 item = g_bookmark_file_lookup_item (bookmark, old_uri);
3522 if (!item)
3524 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3525 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3526 _("No bookmark found for URI '%s'"),
3527 old_uri);
3528 return FALSE;
3531 if (new_uri && new_uri[0] != '\0')
3533 if (g_bookmark_file_has_item (bookmark, new_uri))
3535 remove_error = NULL;
3536 g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
3537 if (remove_error)
3539 g_propagate_error (error, remove_error);
3541 return FALSE;
3545 g_hash_table_steal (bookmark->items_by_uri, item->uri);
3547 g_free (item->uri);
3548 item->uri = g_strdup (new_uri);
3549 item->modified = time (NULL);
3551 g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3553 return TRUE;
3555 else
3557 remove_error = NULL;
3558 g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
3559 if (remove_error)
3561 g_propagate_error (error, remove_error);
3563 return FALSE;
3566 return TRUE;
3571 * g_bookmark_file_set_icon:
3572 * @bookmark: a #GBookmarkFile
3573 * @uri: a valid URI
3574 * @href: the URI of the icon for the bookmark, or %NULL
3575 * @mime_type: the MIME type of the icon for the bookmark
3577 * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3578 * the currently set icon.
3580 * If no bookmark for @uri is found it is created.
3582 * Since: 2.12
3584 void
3585 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3586 const gchar *uri,
3587 const gchar *href,
3588 const gchar *mime_type)
3590 BookmarkItem *item;
3592 g_return_if_fail (bookmark != NULL);
3593 g_return_if_fail (uri != NULL);
3595 item = g_bookmark_file_lookup_item (bookmark, uri);
3596 if (!item)
3598 item = bookmark_item_new (uri);
3599 g_bookmark_file_add_item (bookmark, item, NULL);
3602 if (!item->metadata)
3603 item->metadata = bookmark_metadata_new ();
3605 g_free (item->metadata->icon_href);
3606 g_free (item->metadata->icon_mime);
3608 item->metadata->icon_href = g_strdup (href);
3610 if (mime_type && mime_type[0] != '\0')
3611 item->metadata->icon_mime = g_strdup (mime_type);
3612 else
3613 item->metadata->icon_mime = g_strdup ("application/octet-stream");
3615 item->modified = time (NULL);
3619 * g_bookmark_file_get_icon:
3620 * @bookmark: a #GBookmarkFile
3621 * @uri: a valid URI
3622 * @href: return location for the icon's location or %NULL
3623 * @mime_type: return location for the icon's MIME type or %NULL
3624 * @error: return location for a #GError or %NULL
3626 * Gets the icon of the bookmark for @uri.
3628 * In the event the URI cannot be found, %FALSE is returned and
3629 * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3631 * Return value: %TRUE if the icon for the bookmark for the URI was found.
3632 * You should free the returned strings.
3634 * Since: 2.12
3636 gboolean
3637 g_bookmark_file_get_icon (GBookmarkFile *bookmark,
3638 const gchar *uri,
3639 gchar **href,
3640 gchar **mime_type,
3641 GError **error)
3643 BookmarkItem *item;
3645 g_return_val_if_fail (bookmark != NULL, FALSE);
3646 g_return_val_if_fail (uri != NULL, FALSE);
3648 item = g_bookmark_file_lookup_item (bookmark, uri);
3649 if (!item)
3651 g_set_error (error, G_BOOKMARK_FILE_ERROR,
3652 G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3653 _("No bookmark found for URI '%s'"),
3654 uri);
3655 return FALSE;
3658 if ((!item->metadata) || (!item->metadata->icon_href))
3659 return FALSE;
3661 if (href)
3662 *href = g_strdup (item->metadata->icon_href);
3664 if (mime_type)
3665 *mime_type = g_strdup (item->metadata->icon_mime);
3667 return TRUE;
3670 #define __G_BOOKMARK_FILE_C__
3671 #include "galiasdef.c"