Imported Upstream version 2.57.2
[platform/upstream/glib.git] / glib / gbookmarkfile.c
1 /* gbookmarkfile.c: parsing and building desktop bookmarks
2  *
3  * Copyright (C) 2005-2006 Emmanuele Bassi
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "config.h"
20
21 #include "gbookmarkfile.h"
22
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>
31
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"
50
51
52 /**
53  * SECTION:bookmarkfile
54  * @title: Bookmark file parser
55  * @short_description: parses files containing bookmarks
56  *
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).
63  *
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.
80  *
81  * Here is an example of a bookmark file:
82  * [bookmarks.xbel](https://git.gnome.org/browse/glib/tree/glib/tests/bookmarks.xbel)
83  *
84  * A bookmark file might contain more than one bookmark; each bookmark
85  * is accessed through its URI.
86  *
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().
93  *
94  * The #GBookmarkFile parser was added in GLib 2.12.
95  */
96
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"
102
103 #define XBEL_DTD_URI            "http://www.python.org/topics/xml/dtds/xbel-1.0.dtd"
104
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"
114
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 */
124
125 #define XBEL_YES_VALUE          "yes"
126 #define XBEL_NO_VALUE           "no"
127
128 /* Desktop bookmark spec entities */
129 #define BOOKMARK_METADATA_OWNER         "http://freedesktop.org"
130
131 #define BOOKMARK_NAMESPACE_NAME         "bookmark"
132 #define BOOKMARK_NAMESPACE_URI          "http://www.freedesktop.org/standards/desktop-bookmarks"
133
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"
140
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"
148
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"
154
155
156 typedef struct _BookmarkAppInfo  BookmarkAppInfo;
157 typedef struct _BookmarkMetadata BookmarkMetadata;
158 typedef struct _BookmarkItem     BookmarkItem;
159 typedef struct _ParseData        ParseData;
160
161 struct _BookmarkAppInfo
162 {
163   gchar *name;
164   gchar *exec;
165   
166   guint count;
167   
168   time_t stamp;
169 };
170
171 struct _BookmarkMetadata
172 {
173   gchar *mime_type;
174   
175   GList *groups;
176   
177   GList *applications;
178   GHashTable *apps_by_name;
179   
180   gchar *icon_href;
181   gchar *icon_mime;
182   
183   guint is_private : 1;
184 };
185
186 struct _BookmarkItem
187 {
188   gchar *uri;
189
190   gchar *title;
191   gchar *description;
192
193   time_t added;
194   time_t modified;
195   time_t visited;
196
197   BookmarkMetadata *metadata;
198 };
199
200 struct _GBookmarkFile
201 {
202   gchar *title;
203   gchar *description;
204
205   /* we store our items in a list and keep a copy inside
206    * an hash table for faster lookup performances
207    */
208   GList *items;
209   GHashTable *items_by_uri;
210 };
211
212 /* parser state machine */
213 typedef enum
214 {
215   STATE_STARTED        = 0,
216   
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,
229   
230   STATE_FINISHED
231 } ParserState;
232
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);
247                                                        
248 static time_t  timestamp_from_iso8601 (const gchar *iso_date);
249 static gchar * timestamp_to_iso8601   (time_t       timestamp);
250
251 /********************************
252  * BookmarkAppInfo              *
253  *                              *
254  * Application metadata storage *
255  ********************************/
256 static BookmarkAppInfo *
257 bookmark_app_info_new (const gchar *name)
258 {
259   BookmarkAppInfo *retval;
260  
261   g_warn_if_fail (name != NULL);
262   
263   retval = g_slice_new (BookmarkAppInfo);
264   
265   retval->name = g_strdup (name);
266   retval->exec = NULL;
267   retval->count = 0;
268   retval->stamp = 0;
269   
270   return retval;
271 }
272
273 static void
274 bookmark_app_info_free (BookmarkAppInfo *app_info)
275 {
276   if (!app_info)
277     return;
278   
279   g_free (app_info->name);
280   g_free (app_info->exec);
281   
282   g_slice_free (BookmarkAppInfo, app_info);
283 }
284
285 static gchar *
286 bookmark_app_info_dump (BookmarkAppInfo *app_info)
287 {
288   gchar *retval;
289   gchar *name, *exec, *modified, *count;
290
291   g_warn_if_fail (app_info != NULL);
292
293   if (app_info->count == 0)
294     return NULL;
295
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);
300
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);
308
309   g_free (name);
310   g_free (exec);
311   g_free (modified);
312   g_free (count);
313
314   return retval;
315 }
316
317
318 /***********************
319  * BookmarkMetadata    *
320  *                     *
321  * Metadata storage    *
322  ***********************/
323 static BookmarkMetadata *
324 bookmark_metadata_new (void)
325 {
326   BookmarkMetadata *retval;
327   
328   retval = g_slice_new (BookmarkMetadata);
329
330   retval->mime_type = NULL;
331   
332   retval->groups = NULL;
333   
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);
339   
340   retval->is_private = FALSE;
341   
342   retval->icon_href = NULL;
343   retval->icon_mime = NULL;
344   
345   return retval;
346 }
347
348 static void
349 bookmark_metadata_free (BookmarkMetadata *metadata)
350 {
351   if (!metadata)
352     return;
353   
354   g_free (metadata->mime_type);
355     
356   g_list_free_full (metadata->groups, g_free);
357   g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
358
359   g_hash_table_destroy (metadata->apps_by_name);
360
361   g_free (metadata->icon_href);
362   g_free (metadata->icon_mime);
363   
364   g_slice_free (BookmarkMetadata, metadata);
365 }
366
367 static gchar *
368 bookmark_metadata_dump (BookmarkMetadata *metadata)
369 {
370   GString *retval;
371   gchar *buffer;
372  
373   if (!metadata->applications)
374     return NULL;
375   
376   retval = g_string_sized_new (1024);
377   
378   /* metadata container */
379   g_string_append (retval,
380                    "      "
381                    "<" XBEL_METADATA_ELEMENT
382                    " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
383                    "\">\n");
384
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);
393   }
394
395   if (metadata->groups)
396     {
397       GList *l;
398       
399       /* open groups container */
400       g_string_append (retval,
401                        "        "
402                        "<" BOOKMARK_NAMESPACE_NAME
403                        ":" BOOKMARK_GROUPS_ELEMENT ">\n");
404       
405       for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
406         {
407           gchar *group_name;
408
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);
417
418           g_free (buffer);
419           g_free (group_name);
420         }
421       
422       /* close groups container */
423       g_string_append (retval,
424                        "        "
425                        "</" BOOKMARK_NAMESPACE_NAME
426                        ":" BOOKMARK_GROUPS_ELEMENT ">\n");
427     }
428   
429   if (metadata->applications)
430     {
431       GList *l;
432       
433       /* open applications container */
434       g_string_append (retval,
435                        "        "
436                        "<" BOOKMARK_NAMESPACE_NAME
437                        ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
438       
439       for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
440         {
441           BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
442           gchar *app_data;
443
444           g_warn_if_fail (app_info != NULL);
445           
446           app_data = bookmark_app_info_dump (app_info);
447
448           if (app_data)
449             {
450               retval = g_string_append (retval, app_data);
451
452               g_free (app_data);
453             }
454         }
455       
456       /* close applications container */
457       g_string_append (retval,
458                        "        "
459                        "</" BOOKMARK_NAMESPACE_NAME
460                        ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
461     }
462   
463   /* icon */
464   if (metadata->icon_href)
465     {
466       if (!metadata->icon_mime)
467         metadata->icon_mime = g_strdup ("application/octet-stream");
468
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);
475
476       g_free (buffer);
477     }
478   
479   /* private hint */
480   if (metadata->is_private)
481     g_string_append (retval,
482                      "        "
483                      "<" BOOKMARK_NAMESPACE_NAME
484                      ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
485   
486   /* close metadata container */
487   g_string_append (retval,
488                    "      "
489                    "</" XBEL_METADATA_ELEMENT ">\n");
490                            
491   return g_string_free (retval, FALSE);
492 }
493
494 /******************************************************
495  * BookmarkItem                                       *
496  *                                                    *
497  * Storage for a single bookmark item inside the list *
498  ******************************************************/
499 static BookmarkItem *
500 bookmark_item_new (const gchar *uri)
501 {
502   BookmarkItem *item;
503  
504   g_warn_if_fail (uri != NULL);
505   
506   item = g_slice_new (BookmarkItem);
507   item->uri = g_strdup (uri);
508   
509   item->title = NULL;
510   item->description = NULL;
511   
512   item->added = (time_t) -1;
513   item->modified = (time_t) -1;
514   item->visited = (time_t) -1;
515   
516   item->metadata = NULL;
517   
518   return item;
519 }
520
521 static void
522 bookmark_item_free (BookmarkItem *item)
523 {
524   if (!item)
525     return;
526
527   g_free (item->uri);
528   g_free (item->title);
529   g_free (item->description);
530   
531   if (item->metadata)
532     bookmark_metadata_free (item->metadata);
533   
534   g_slice_free (BookmarkItem, item);
535 }
536
537 static gchar *
538 bookmark_item_dump (BookmarkItem *item)
539 {
540   GString *retval;
541   gchar *added, *visited, *modified;
542   gchar *escaped_uri;
543   gchar *buffer;
544  
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
547    */
548   if (!item->metadata || !item->metadata->applications)
549     {
550       g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri);
551       return NULL;
552     }
553   
554   retval = g_string_sized_new (4096);
555   
556   added = timestamp_to_iso8601 (item->added);
557   modified = timestamp_to_iso8601 (item->modified);
558   visited = timestamp_to_iso8601 (item->visited);
559
560   escaped_uri = g_markup_escape_text (item->uri, -1);
561
562   buffer = g_strconcat ("  <"
563                         XBEL_BOOKMARK_ELEMENT
564                         " "
565                         XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
566                         XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
567                         XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
568                         XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
569                         NULL);
570
571   g_string_append (retval, buffer);
572
573   g_free (escaped_uri);
574   g_free (visited);
575   g_free (modified);
576   g_free (added);
577   g_free (buffer);
578   
579   if (item->title)
580     {
581       gchar *escaped_title;
582       
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);
590
591       g_free (escaped_title);
592       g_free (buffer);
593     }
594   
595   if (item->description)
596     {
597       gchar *escaped_desc;
598       
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);
606
607       g_free (escaped_desc);
608       g_free (buffer);
609     }
610   
611   if (item->metadata)
612     {
613       gchar *metadata;
614       
615       metadata = bookmark_metadata_dump (item->metadata);
616       if (metadata)
617         {
618           buffer = g_strconcat ("    "
619                                 "<" XBEL_INFO_ELEMENT ">\n",
620                                 metadata,
621                                 "    "
622                                 "</" XBEL_INFO_ELEMENT ">\n",
623                                 NULL);
624           retval = g_string_append (retval, buffer);
625
626           g_free (buffer);
627           g_free (metadata);
628         }
629     }
630
631   g_string_append (retval, "  </" XBEL_BOOKMARK_ELEMENT ">\n");
632   
633   return g_string_free (retval, FALSE);
634 }
635
636 static BookmarkAppInfo *
637 bookmark_item_lookup_app_info (BookmarkItem *item,
638                                const gchar  *app_name)
639 {
640   g_warn_if_fail (item != NULL && app_name != NULL);
641
642   if (!item->metadata)
643     return NULL;
644   
645   return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
646 }
647
648 /*************************
649  *    GBookmarkFile    *
650  *************************/
651  
652 static void
653 g_bookmark_file_init (GBookmarkFile *bookmark)
654 {
655   bookmark->title = NULL;
656   bookmark->description = NULL;
657   
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);
663 }
664
665 static void
666 g_bookmark_file_clear (GBookmarkFile *bookmark)
667 {
668   g_free (bookmark->title);
669   g_free (bookmark->description);
670
671   g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
672   bookmark->items = NULL;
673   
674   if (bookmark->items_by_uri)
675     {
676       g_hash_table_destroy (bookmark->items_by_uri);
677       
678       bookmark->items_by_uri = NULL;
679     }
680 }
681
682 struct _ParseData
683 {
684   ParserState state;
685   
686   GHashTable *namespaces;
687   
688   GBookmarkFile *bookmark_file;
689   BookmarkItem *current_item;
690 };
691
692 static ParseData *
693 parse_data_new (void)
694 {
695   ParseData *retval;
696   
697   retval = g_new (ParseData, 1);
698   
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;
705   
706   return retval;
707 }
708
709 static void
710 parse_data_free (ParseData *parse_data)
711 {
712   g_hash_table_destroy (parse_data->namespaces);
713   
714   g_free (parse_data);
715 }
716
717 #define IS_ATTRIBUTE(s,a)       ((0 == strcmp ((s), (a))))
718
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)
725 {
726   const gchar *uri, *added, *modified, *visited;
727   const gchar *attr;
728   gint i;
729   BookmarkItem *item;
730   GError *add_error;
731  
732   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
733   
734   i = 0;
735   uri = added = modified = visited = NULL;
736   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
737     {
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
747         {
748           /* bookmark is defined by the XBEL spec, so we need
749            * to error out if the element has different or
750            * missing attributes
751            */
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;
758         }
759     }
760   
761   if (!uri)
762     {
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;
769     }
770   
771   g_warn_if_fail (parse_data->current_item == NULL);
772   
773   item = bookmark_item_new (uri);
774   
775   if (added)
776     item->added = timestamp_from_iso8601 (added);
777   
778   if (modified)
779     item->modified = timestamp_from_iso8601 (modified);
780   
781   if (visited)
782     item->visited = timestamp_from_iso8601 (visited);
783
784   add_error = NULL;
785   g_bookmark_file_add_item (parse_data->bookmark_file,
786                             item,
787                             &add_error);
788   if (add_error)
789     {
790       bookmark_item_free (item);
791       
792       g_propagate_error (error, add_error);
793       
794       return;
795     }                         
796   
797   parse_data->current_item = item;
798 }
799
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)
806 {
807   const gchar *name, *exec, *count, *stamp, *modified;
808   const gchar *attr;
809   gint i;
810   BookmarkItem *item;
811   BookmarkAppInfo *ai;
812   
813   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
814
815   i = 0;
816   name = exec = count = stamp = modified = NULL;
817   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
818     {
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];
829     }
830
831   /* the "name" and "exec" attributes are mandatory */
832   if (!name)
833     {
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;
840     }
841   
842   if (!exec)
843     {
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;
850     }
851
852   g_warn_if_fail (parse_data->current_item != NULL);  
853   item = parse_data->current_item;
854     
855   ai = bookmark_item_lookup_app_info (item, name);
856   if (!ai)
857     {
858       ai = bookmark_app_info_new (name);
859       
860       if (!item->metadata)
861         item->metadata = bookmark_metadata_new ();
862       
863       item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
864       g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
865     }
866       
867   ai->exec = g_strdup (exec);
868   
869   if (count)
870     ai->count = atoi (count);
871   else
872     ai->count = 1;
873
874   if (modified)
875     ai->stamp = timestamp_from_iso8601 (modified);
876   else
877     {
878       /* the timestamp attribute has been deprecated but we still parse
879        * it for backward compatibility
880        */
881       if (stamp)
882         ai->stamp = (time_t) atol (stamp);
883       else
884         ai->stamp = time (NULL);
885     }
886 }
887
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)
894 {
895   const gchar *type;
896   const gchar *attr;
897   gint i;
898   BookmarkItem *item;
899   
900   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
901   
902   i = 0;
903   type = NULL;
904   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
905     {
906       if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
907         type = attribute_values[i];
908     }
909
910   if (!type)
911     type = "application/octet-stream";
912
913   g_warn_if_fail (parse_data->current_item != NULL);  
914   item = parse_data->current_item;
915     
916   if (!item->metadata)
917     item->metadata = bookmark_metadata_new ();
918   
919   item->metadata->mime_type = g_strdup (type);
920 }
921
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)
928 {
929   const gchar *href;
930   const gchar *type;
931   const gchar *attr;
932   gint i;
933   BookmarkItem *item;
934   
935   g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
936   
937   i = 0;
938   href = NULL;
939   type = NULL;
940   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
941     {
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];
946     }
947
948   /* the "href" attribute is mandatory */       
949   if (!href)
950     {
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;
957     }
958
959   if (!type)
960     type = "application/octet-stream";
961
962   g_warn_if_fail (parse_data->current_item != NULL);  
963   item = parse_data->current_item;
964     
965   if (!item->metadata)
966     item->metadata = bookmark_metadata_new ();
967   
968   item->metadata->icon_href = g_strdup (href);
969   item->metadata->icon_mime = g_strdup (type);
970 }
971
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.
976  *
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.
984  */
985 static void
986 map_namespace_to_name (ParseData    *parse_data,
987                        const gchar **attribute_names,
988                        const gchar **attribute_values)
989 {
990   const gchar *attr;
991   gint i;
992  
993   g_warn_if_fail (parse_data != NULL);
994   
995   if (!attribute_names || !attribute_names[0])
996     return;
997   
998   i = 0;
999   for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1000     {
1001       if (g_str_has_prefix (attr, "xmlns"))
1002         {
1003           gchar *namespace_name, *namespace_uri;
1004           gchar *p;
1005           
1006           p = g_utf8_strchr (attr, -1, ':');
1007           if (p)
1008             p = g_utf8_next_char (p);
1009           else
1010             p = "default";
1011           
1012           namespace_name = g_strdup (p);
1013           namespace_uri = g_strdup (attribute_values[i]);
1014           
1015           g_hash_table_replace (parse_data->namespaces,
1016                                 namespace_name,
1017                                 namespace_uri);
1018         }
1019      }
1020 }
1021
1022 /* checks whether @element_full is equal to @element.
1023  *
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.
1027  */
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)
1034 {
1035   gchar *ns_uri, *ns_name;
1036   const gchar *p, *element_name;
1037   gboolean retval;
1038  
1039   g_warn_if_fail (parse_data != NULL);
1040   g_warn_if_fail (element_full != NULL);
1041   
1042   if (!element)
1043     return FALSE;
1044     
1045   /* no namespace requested: dumb element compare */
1046   if (!namespace)
1047     return (0 == strcmp (element_full, element));
1048   
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.
1053    */
1054   p = g_utf8_strchr (element_full, -1, ':');
1055   if (p)
1056     {
1057       ns_name = g_strndup (element_full, p - element_full);
1058       element_name = g_utf8_next_char (p);
1059     }
1060   else
1061     {
1062       ns_name = g_strdup ("default");
1063       element_name = element_full;
1064     }
1065   
1066   ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);  
1067   if (!ns_uri)
1068     {
1069       /* no default namespace found */
1070       g_free (ns_name);
1071       
1072       return (0 == strcmp (element_full, element));
1073     }
1074
1075   retval = (0 == strcmp (ns_uri, namespace) &&
1076             0 == strcmp (element_name, element));
1077   
1078   g_free (ns_name);
1079   
1080   return retval;
1081 }
1082
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), '|'))
1085
1086 static const gchar *
1087 parser_state_to_element_name (ParserState state)
1088 {
1089   switch (state)
1090     {
1091     case STATE_STARTED:
1092     case STATE_FINISHED:
1093       return "(top-level)";
1094     case STATE_ROOT:
1095       return XBEL_ROOT_ELEMENT;
1096     case STATE_BOOKMARK:
1097       return XBEL_BOOKMARK_ELEMENT;
1098     case STATE_TITLE:
1099       return XBEL_TITLE_ELEMENT;
1100     case STATE_DESC:
1101       return XBEL_DESC_ELEMENT;
1102     case STATE_INFO:
1103       return XBEL_INFO_ELEMENT;
1104     case STATE_METADATA:
1105       return XBEL_METADATA_ELEMENT;
1106     case STATE_APPLICATIONS:
1107       return BOOKMARK_APPLICATIONS_ELEMENT;
1108     case STATE_APPLICATION:
1109       return BOOKMARK_APPLICATION_ELEMENT;
1110     case STATE_GROUPS:
1111       return BOOKMARK_GROUPS_ELEMENT;
1112     case STATE_GROUP:
1113       return BOOKMARK_GROUP_ELEMENT;
1114     case STATE_MIME:
1115       return MIME_TYPE_ELEMENT;
1116     case STATE_ICON:
1117       return BOOKMARK_ICON_ELEMENT;
1118     default:
1119       g_assert_not_reached ();
1120     }
1121 }
1122
1123 static void
1124 start_element_raw_cb (GMarkupParseContext *context,
1125                       const gchar         *element_name,
1126                       const gchar        **attribute_names,
1127                       const gchar        **attribute_values,
1128                       gpointer             user_data,
1129                       GError             **error)
1130 {
1131   ParseData *parse_data = (ParseData *) user_data;
1132
1133   /* we must check for namespace declarations first
1134    * 
1135    * XXX - we could speed up things by checking for namespace declarations
1136    * only on the root node, where they usually are; this would probably break
1137    * on streams not produced by us or by "smart" generators
1138    */
1139   map_namespace_to_name (parse_data, attribute_names, attribute_values);
1140   
1141   switch (parse_data->state)
1142     {
1143     case STATE_STARTED:
1144       if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1145         {
1146           const gchar *attr;
1147           gint i;
1148           
1149           i = 0;
1150           for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1151             {
1152               if ((IS_ATTRIBUTE (attr, XBEL_VERSION_ATTRIBUTE)) &&
1153                   (0 == strcmp (attribute_values[i], XBEL_VERSION)))
1154                 parse_data->state = STATE_ROOT;
1155             }
1156         }
1157       else
1158         g_set_error (error, G_MARKUP_ERROR,
1159                      G_MARKUP_ERROR_INVALID_CONTENT,
1160                      _("Unexpected tag “%s”, tag “%s” expected"),
1161                      element_name, XBEL_ROOT_ELEMENT);
1162       break;
1163     case STATE_ROOT:
1164       if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1165         parse_data->state = STATE_TITLE;
1166       else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1167         parse_data->state = STATE_DESC;
1168       else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1169         {
1170           GError *inner_error = NULL;
1171           
1172           parse_data->state = STATE_BOOKMARK;
1173           
1174           parse_bookmark_element (context,
1175                                   parse_data,
1176                                   attribute_names,
1177                                   attribute_values,
1178                                   &inner_error);
1179           if (inner_error)
1180             g_propagate_error (error, inner_error);
1181         }
1182       else
1183         g_set_error (error, G_MARKUP_ERROR,
1184                      G_MARKUP_ERROR_INVALID_CONTENT,
1185                      _("Unexpected tag “%s” inside “%s”"),
1186                      element_name,
1187                      XBEL_ROOT_ELEMENT);
1188       break;
1189     case STATE_BOOKMARK:
1190       if (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT))
1191         parse_data->state = STATE_TITLE;
1192       else if (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT))
1193         parse_data->state = STATE_DESC;
1194       else if (IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT))
1195         parse_data->state = STATE_INFO;
1196       else
1197         g_set_error (error, G_MARKUP_ERROR,
1198                      G_MARKUP_ERROR_INVALID_CONTENT,
1199                      _("Unexpected tag “%s” inside “%s”"),
1200                      element_name,
1201                      XBEL_BOOKMARK_ELEMENT);
1202       break;
1203     case STATE_INFO:
1204       if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1205         {
1206           const gchar *attr;
1207           gint i;
1208           
1209           i = 0;
1210           for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
1211             {
1212               if ((IS_ATTRIBUTE (attr, XBEL_OWNER_ATTRIBUTE)) &&
1213                   (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
1214                 {
1215                   parse_data->state = STATE_METADATA;
1216                   
1217                   if (!parse_data->current_item->metadata)
1218                     parse_data->current_item->metadata = bookmark_metadata_new ();
1219                 }
1220             }
1221         }
1222       else
1223         g_set_error (error, G_MARKUP_ERROR,
1224                      G_MARKUP_ERROR_INVALID_CONTENT,
1225                      _("Unexpected tag “%s”, tag “%s” expected"),
1226                      element_name,
1227                      XBEL_METADATA_ELEMENT);
1228       break;
1229     case STATE_METADATA:
1230       if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT))
1231         parse_data->state = STATE_APPLICATIONS;
1232       else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT))
1233         parse_data->state = STATE_GROUPS;
1234       else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT))
1235         parse_data->current_item->metadata->is_private = TRUE;
1236       else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
1237         {
1238           GError *inner_error = NULL;
1239           
1240           parse_data->state = STATE_ICON;
1241           
1242           parse_icon_element (context,
1243                               parse_data,
1244                               attribute_names,
1245                               attribute_values,
1246                               &inner_error);
1247           if (inner_error)
1248             g_propagate_error (error, inner_error);
1249         }
1250       else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
1251         {
1252           GError *inner_error = NULL;
1253           
1254           parse_data->state = STATE_MIME;
1255           
1256           parse_mime_type_element (context,
1257                                    parse_data,
1258                                    attribute_names,
1259                                    attribute_values,
1260                                    &inner_error);
1261           if (inner_error)
1262             g_propagate_error (error, inner_error);
1263         }
1264       else
1265         g_set_error (error, G_MARKUP_ERROR,
1266                      G_MARKUP_ERROR_UNKNOWN_ELEMENT,
1267                      _("Unexpected tag “%s” inside “%s”"),
1268                      element_name,
1269                      XBEL_METADATA_ELEMENT);
1270       break;
1271     case STATE_APPLICATIONS:
1272       if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
1273         {
1274           GError *inner_error = NULL;
1275           
1276           parse_data->state = STATE_APPLICATION;
1277           
1278           parse_application_element (context,
1279                                      parse_data,
1280                                      attribute_names,
1281                                      attribute_values,
1282                                      &inner_error);
1283           if (inner_error)
1284             g_propagate_error (error, inner_error);
1285         }
1286       else
1287         g_set_error (error, G_MARKUP_ERROR,
1288                      G_MARKUP_ERROR_INVALID_CONTENT,
1289                      _("Unexpected tag “%s”, tag “%s” expected"),
1290                      element_name,
1291                      BOOKMARK_APPLICATION_ELEMENT);
1292       break;
1293     case STATE_GROUPS:
1294       if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUP_ELEMENT))
1295         parse_data->state = STATE_GROUP;
1296       else
1297         g_set_error (error, G_MARKUP_ERROR,
1298                      G_MARKUP_ERROR_INVALID_CONTENT,
1299                      _("Unexpected tag “%s”, tag “%s” expected"),
1300                      element_name,
1301                      BOOKMARK_GROUP_ELEMENT);
1302       break;
1303
1304     case STATE_TITLE:
1305     case STATE_DESC:
1306     case STATE_APPLICATION:
1307     case STATE_GROUP:
1308     case STATE_MIME:
1309     case STATE_ICON:
1310     case STATE_FINISHED:
1311       g_set_error (error, G_MARKUP_ERROR,
1312                    G_MARKUP_ERROR_INVALID_CONTENT,
1313                    _("Unexpected tag “%s” inside “%s”"),
1314                    element_name,
1315                    parser_state_to_element_name (parse_data->state));
1316       break;
1317
1318     default:
1319       g_assert_not_reached ();
1320       break;
1321     }
1322 }
1323
1324 static void
1325 end_element_raw_cb (GMarkupParseContext *context,
1326                     const gchar         *element_name,
1327                     gpointer             user_data,
1328                     GError             **error)
1329 {
1330   ParseData *parse_data = (ParseData *) user_data;
1331   
1332   if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
1333     parse_data->state = STATE_FINISHED;
1334   else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
1335     {
1336       parse_data->current_item = NULL;
1337       
1338       parse_data->state = STATE_ROOT;
1339     }
1340   else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
1341            (IS_ELEMENT (parse_data, element_name, XBEL_TITLE_ELEMENT)) ||
1342            (IS_ELEMENT (parse_data, element_name, XBEL_DESC_ELEMENT)))
1343     {
1344       if (parse_data->current_item)
1345         parse_data->state = STATE_BOOKMARK;
1346       else
1347         parse_data->state = STATE_ROOT;
1348     }
1349   else if (IS_ELEMENT (parse_data, element_name, XBEL_METADATA_ELEMENT))
1350     parse_data->state = STATE_INFO;
1351   else if (IS_ELEMENT_NS (parse_data, element_name,
1352                           BOOKMARK_NAMESPACE_URI,
1353                           BOOKMARK_APPLICATION_ELEMENT))
1354     parse_data->state = STATE_APPLICATIONS;
1355   else if (IS_ELEMENT_NS (parse_data, element_name,
1356                           BOOKMARK_NAMESPACE_URI,
1357                           BOOKMARK_GROUP_ELEMENT))
1358     parse_data->state = STATE_GROUPS;
1359   else if ((IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATIONS_ELEMENT)) ||
1360            (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_GROUPS_ELEMENT)) ||
1361            (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_PRIVATE_ELEMENT)) ||
1362            (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT)) ||
1363            (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT)))
1364     parse_data->state = STATE_METADATA;
1365 }
1366
1367 static void
1368 text_raw_cb (GMarkupParseContext *context,
1369              const gchar         *text,
1370              gsize                length,
1371              gpointer             user_data,
1372              GError             **error)
1373 {
1374   ParseData *parse_data = (ParseData *) user_data;
1375   gchar *payload;
1376   
1377   payload = g_strndup (text, length);
1378   
1379   switch (parse_data->state)
1380     {
1381     case STATE_TITLE:
1382       if (parse_data->current_item)
1383         {
1384           g_free (parse_data->current_item->title);
1385           parse_data->current_item->title = g_strdup (payload);
1386         }
1387       else
1388         {
1389           g_free (parse_data->bookmark_file->title);
1390           parse_data->bookmark_file->title = g_strdup (payload);
1391         }
1392       break;
1393     case STATE_DESC:
1394       if (parse_data->current_item)
1395         {
1396           g_free (parse_data->current_item->description);
1397           parse_data->current_item->description = g_strdup (payload);
1398         }
1399       else
1400         {
1401           g_free (parse_data->bookmark_file->description);
1402           parse_data->bookmark_file->description = g_strdup (payload);
1403         }
1404       break;
1405     case STATE_GROUP:
1406       {
1407       GList *groups;
1408       
1409       g_warn_if_fail (parse_data->current_item != NULL);
1410       
1411       if (!parse_data->current_item->metadata)
1412         parse_data->current_item->metadata = bookmark_metadata_new ();
1413       
1414       groups = parse_data->current_item->metadata->groups;
1415       parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
1416       }
1417       break;
1418     case STATE_ROOT:
1419     case STATE_BOOKMARK:
1420     case STATE_INFO:
1421     case STATE_METADATA:
1422     case STATE_APPLICATIONS:
1423     case STATE_APPLICATION:
1424     case STATE_GROUPS:
1425     case STATE_MIME:
1426     case STATE_ICON:
1427       break;
1428     default:
1429       g_warn_if_reached ();
1430       break;
1431     }
1432   
1433   g_free (payload);
1434 }
1435
1436 static const GMarkupParser markup_parser =
1437 {
1438   start_element_raw_cb, /* start_element */
1439   end_element_raw_cb,   /* end_element */
1440   text_raw_cb,          /* text */
1441   NULL,                 /* passthrough */
1442   NULL
1443 };
1444
1445 static gboolean
1446 g_bookmark_file_parse (GBookmarkFile  *bookmark,
1447                          const gchar  *buffer,
1448                          gsize         length,
1449                          GError       **error)
1450 {
1451   GMarkupParseContext *context;
1452   ParseData *parse_data;
1453   GError *parse_error, *end_error;
1454   gboolean retval;
1455   
1456   g_warn_if_fail (bookmark != NULL);
1457
1458   if (!buffer)
1459     return FALSE;
1460
1461   parse_error = NULL;
1462   end_error = NULL;
1463   
1464   if (length == (gsize) -1)
1465     length = strlen (buffer);
1466
1467   parse_data = parse_data_new ();
1468   parse_data->bookmark_file = bookmark;
1469   
1470   context = g_markup_parse_context_new (&markup_parser,
1471                                         0,
1472                                         parse_data,
1473                                         (GDestroyNotify) parse_data_free);
1474   
1475   retval = g_markup_parse_context_parse (context,
1476                                          buffer,
1477                                          length,
1478                                          &parse_error);
1479   if (!retval)
1480     g_propagate_error (error, parse_error);
1481   else
1482    {
1483      retval = g_markup_parse_context_end_parse (context, &end_error);
1484       if (!retval)
1485         g_propagate_error (error, end_error);
1486    }
1487  
1488   g_markup_parse_context_free (context);
1489
1490   return retval;
1491 }
1492
1493 static gchar *
1494 g_bookmark_file_dump (GBookmarkFile  *bookmark,
1495                       gsize          *length,
1496                       GError        **error)
1497 {
1498   GString *retval;
1499   gchar *buffer;
1500   GList *l;
1501   
1502   retval = g_string_sized_new (4096);
1503
1504   g_string_append (retval,
1505                    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
1506 #if 0
1507                    /* XXX - do we really need the doctype? */
1508                    "<!DOCTYPE " XBEL_DTD_NICK "\n"
1509                    "  PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
1510                    "         \"" XBEL_DTD_URI "\">\n"
1511 #endif
1512                    "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
1513                    "      xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
1514                    "      xmlns:" MIME_NAMESPACE_NAME     "=\"" MIME_NAMESPACE_URI "\"\n>");
1515   
1516   if (bookmark->title)
1517     {
1518       gchar *escaped_title;
1519  
1520       escaped_title = g_markup_escape_text (bookmark->title, -1);
1521
1522       buffer = g_strconcat ("  "
1523                             "<" XBEL_TITLE_ELEMENT ">",
1524                             escaped_title,
1525                             "</" XBEL_TITLE_ELEMENT ">\n", NULL);
1526       
1527       g_string_append (retval, buffer);
1528
1529       g_free (buffer);
1530       g_free (escaped_title);
1531     }
1532   
1533   if (bookmark->description)
1534     {
1535       gchar *escaped_desc;
1536  
1537       escaped_desc = g_markup_escape_text (bookmark->description, -1);
1538
1539       buffer = g_strconcat ("  "
1540                             "<" XBEL_DESC_ELEMENT ">",
1541                             escaped_desc,
1542                             "</" XBEL_DESC_ELEMENT ">\n", NULL);
1543       g_string_append (retval, buffer);
1544
1545       g_free (buffer);
1546       g_free (escaped_desc);
1547     }
1548   
1549   if (!bookmark->items)
1550     goto out;
1551   else
1552     retval = g_string_append (retval, "\n");
1553
1554   /* the items are stored in reverse order */
1555   for (l = g_list_last (bookmark->items);
1556        l != NULL;
1557        l = l->prev)
1558     {
1559       BookmarkItem *item = (BookmarkItem *) l->data;
1560       gchar *item_dump;
1561       
1562       item_dump = bookmark_item_dump (item);
1563       if (!item_dump)
1564         continue;
1565       
1566       retval = g_string_append (retval, item_dump);
1567       
1568       g_free (item_dump);
1569     }
1570
1571 out:
1572   g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
1573   
1574   if (length)
1575     *length = retval->len;
1576   
1577   return g_string_free (retval, FALSE);
1578 }
1579
1580 /**************
1581  *    Misc    *
1582  **************/
1583  
1584 /* converts a Unix timestamp in a ISO 8601 compliant string; you
1585  * should free the returned string.
1586  */
1587 static gchar *
1588 timestamp_to_iso8601 (time_t timestamp)
1589 {
1590   GTimeVal stamp;
1591
1592   if (timestamp == (time_t) -1)
1593     g_get_current_time (&stamp);
1594   else
1595     {
1596       stamp.tv_sec = timestamp;
1597       stamp.tv_usec = 0;
1598     }
1599
1600   return g_time_val_to_iso8601 (&stamp);
1601 }
1602
1603 static time_t
1604 timestamp_from_iso8601 (const gchar *iso_date)
1605 {
1606   GTimeVal stamp;
1607
1608   if (!g_time_val_from_iso8601 (iso_date, &stamp))
1609     return (time_t) -1;
1610
1611   return (time_t) stamp.tv_sec;
1612 }
1613
1614 G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
1615
1616 /********************
1617  *    Public API    *
1618  ********************/
1619
1620 /**
1621  * g_bookmark_file_new: (constructor)
1622  *
1623  * Creates a new empty #GBookmarkFile object.
1624  *
1625  * Use g_bookmark_file_load_from_file(), g_bookmark_file_load_from_data()
1626  * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
1627  * file.
1628  *
1629  * Returns: an empty #GBookmarkFile
1630  *
1631  * Since: 2.12
1632  */
1633 GBookmarkFile *
1634 g_bookmark_file_new (void)
1635 {
1636   GBookmarkFile *bookmark;
1637   
1638   bookmark = g_new (GBookmarkFile, 1);
1639   
1640   g_bookmark_file_init (bookmark);
1641   
1642   return bookmark;
1643 }
1644
1645 /**
1646  * g_bookmark_file_free:
1647  * @bookmark: a #GBookmarkFile
1648  *
1649  * Frees a #GBookmarkFile.
1650  *
1651  * Since: 2.12
1652  */
1653 void
1654 g_bookmark_file_free (GBookmarkFile *bookmark)
1655 {
1656   if (!bookmark)
1657     return;
1658   
1659   g_bookmark_file_clear (bookmark);
1660   
1661   g_free (bookmark);  
1662 }
1663
1664 /**
1665  * g_bookmark_file_load_from_data:
1666  * @bookmark: an empty #GBookmarkFile struct
1667  * @data: (array length=length) (element-type guint8): desktop bookmarks
1668  *    loaded in memory
1669  * @length: the length of @data in bytes
1670  * @error: return location for a #GError, or %NULL
1671  *
1672  * Loads a bookmark file from memory into an empty #GBookmarkFile
1673  * structure.  If the object cannot be created then @error is set to a
1674  * #GBookmarkFileError.
1675  *
1676  * Returns: %TRUE if a desktop bookmark could be loaded.
1677  *
1678  * Since: 2.12
1679  */
1680 gboolean
1681 g_bookmark_file_load_from_data (GBookmarkFile  *bookmark,
1682                                 const gchar    *data,
1683                                 gsize           length,
1684                                 GError        **error)
1685 {
1686   GError *parse_error;
1687   gboolean retval;
1688   
1689   g_return_val_if_fail (bookmark != NULL, FALSE);
1690
1691   if (length == (gsize) -1)
1692     length = strlen (data);
1693
1694   if (bookmark->items)
1695     {
1696       g_bookmark_file_clear (bookmark);
1697       g_bookmark_file_init (bookmark);
1698     }
1699
1700   parse_error = NULL;
1701   retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
1702
1703   if (!retval)
1704     g_propagate_error (error, parse_error);
1705
1706   return retval;
1707 }
1708
1709 /**
1710  * g_bookmark_file_load_from_file:
1711  * @bookmark: an empty #GBookmarkFile struct
1712  * @filename: (type filename): the path of a filename to load, in the
1713  *     GLib file name encoding
1714  * @error: return location for a #GError, or %NULL
1715  *
1716  * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
1717  * If the file could not be loaded then @error is set to either a #GFileError
1718  * or #GBookmarkFileError.
1719  *
1720  * Returns: %TRUE if a desktop bookmark file could be loaded
1721  *
1722  * Since: 2.12
1723  */
1724 gboolean
1725 g_bookmark_file_load_from_file (GBookmarkFile  *bookmark,
1726                                 const gchar    *filename,
1727                                 GError        **error)
1728 {
1729   gboolean ret = FALSE;
1730   gchar *buffer = NULL;
1731   gsize len;
1732         
1733   g_return_val_if_fail (bookmark != NULL, FALSE);
1734   g_return_val_if_fail (filename != NULL, FALSE);
1735
1736   if (!g_file_get_contents (filename, &buffer, &len, error))
1737     goto out;
1738   
1739   if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
1740     goto out;
1741
1742   ret = TRUE;
1743  out:
1744   g_free (buffer);
1745   return ret;
1746 }
1747
1748
1749 /* Iterates through all the directories in *dirs trying to
1750  * find file.  When it successfully locates file, returns a
1751  * string its absolute path.  It also leaves the unchecked
1752  * directories in *dirs.  You should free the returned string
1753  *
1754  * Adapted from gkeyfile.c
1755  */
1756 static gchar *
1757 find_file_in_data_dirs (const gchar   *file,
1758                         gchar       ***dirs,
1759                         GError       **error)
1760 {
1761   gchar **data_dirs, *data_dir, *path;
1762
1763   path = NULL;
1764
1765   if (dirs == NULL)
1766     return NULL;
1767
1768   data_dirs = *dirs;
1769   path = NULL;
1770   while (data_dirs && (data_dir = *data_dirs) && !path)
1771     {
1772       gchar *candidate_file, *sub_dir;
1773
1774       candidate_file = (gchar *) file;
1775       sub_dir = g_strdup ("");
1776       while (candidate_file != NULL && !path)
1777         {
1778           gchar *p;
1779
1780           path = g_build_filename (data_dir, sub_dir,
1781                                    candidate_file, NULL);
1782
1783           candidate_file = strchr (candidate_file, '-');
1784
1785           if (candidate_file == NULL)
1786             break;
1787
1788           candidate_file++;
1789
1790           g_free (sub_dir);
1791           sub_dir = g_strndup (file, candidate_file - file - 1);
1792
1793           for (p = sub_dir; *p != '\0'; p++)
1794             {
1795               if (*p == '-')
1796                 *p = G_DIR_SEPARATOR;
1797             }
1798         }
1799       g_free (sub_dir);
1800       data_dirs++;
1801     }
1802
1803   *dirs = data_dirs;
1804
1805   if (!path)
1806     {
1807       g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
1808                            G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
1809                            _("No valid bookmark file found in data dirs"));
1810       
1811       return NULL;
1812     }
1813   
1814   return path;
1815 }
1816
1817
1818 /**
1819  * g_bookmark_file_load_from_data_dirs:
1820  * @bookmark: a #GBookmarkFile
1821  * @file: (type filename): a relative path to a filename to open and parse
1822  * @full_path: (out) (optional) (type filename): return location for a string
1823  *    containing the full path of the file, or %NULL
1824  * @error: return location for a #GError, or %NULL
1825  *
1826  * This function looks for a desktop bookmark file named @file in the
1827  * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), 
1828  * loads the file into @bookmark and returns the file's full path in 
1829  * @full_path.  If the file could not be loaded then @error is
1830  * set to either a #GFileError or #GBookmarkFileError.
1831  *
1832  * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
1833  *
1834  * Since: 2.12
1835  */
1836 gboolean
1837 g_bookmark_file_load_from_data_dirs (GBookmarkFile  *bookmark,
1838                                      const gchar    *file,
1839                                      gchar         **full_path,
1840                                      GError        **error)
1841 {
1842   GError *file_error = NULL;
1843   gchar **all_data_dirs, **data_dirs;
1844   const gchar *user_data_dir;
1845   const gchar * const * system_data_dirs;
1846   gsize i, j;
1847   gchar *output_path;
1848   gboolean found_file;
1849   
1850   g_return_val_if_fail (bookmark != NULL, FALSE);
1851   g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
1852   
1853   user_data_dir = g_get_user_data_dir ();
1854   system_data_dirs = g_get_system_data_dirs ();
1855   all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
1856
1857   i = 0;
1858   all_data_dirs[i++] = g_strdup (user_data_dir);
1859
1860   j = 0;
1861   while (system_data_dirs[j] != NULL)
1862     all_data_dirs[i++] = g_strdup (system_data_dirs[j++]);
1863
1864   found_file = FALSE;
1865   data_dirs = all_data_dirs;
1866   output_path = NULL;
1867   while (*data_dirs != NULL && !found_file)
1868     {
1869       g_free (output_path);
1870
1871       output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
1872       
1873       if (file_error)
1874         {
1875           g_propagate_error (error, file_error);
1876           break;
1877         }
1878
1879       found_file = g_bookmark_file_load_from_file (bookmark,
1880                                                    output_path,
1881                                                    &file_error);
1882       if (file_error)
1883         {
1884           g_propagate_error (error, file_error);
1885           break;
1886         }
1887     }
1888
1889   if (found_file && full_path)
1890     *full_path = output_path;
1891   else 
1892     g_free (output_path);
1893
1894   g_strfreev (all_data_dirs);
1895
1896   return found_file;
1897 }
1898
1899
1900 /**
1901  * g_bookmark_file_to_data:
1902  * @bookmark: a #GBookmarkFile
1903  * @length: (out) (optional): return location for the length of the returned string, or %NULL
1904  * @error: return location for a #GError, or %NULL
1905  *
1906  * This function outputs @bookmark as a string.
1907  *
1908  * Returns: (array length=length) (element-type guint8):
1909  *   a newly allocated string holding the contents of the #GBookmarkFile
1910  *
1911  * Since: 2.12
1912  */
1913 gchar *
1914 g_bookmark_file_to_data (GBookmarkFile  *bookmark,
1915                          gsize          *length,
1916                          GError        **error)
1917 {
1918   GError *write_error = NULL;
1919   gchar *retval;
1920   
1921   g_return_val_if_fail (bookmark != NULL, NULL);
1922   
1923   retval = g_bookmark_file_dump (bookmark, length, &write_error);
1924   if (write_error)
1925     {
1926       g_propagate_error (error, write_error);
1927       
1928       return NULL;
1929     }
1930       
1931   return retval;
1932 }
1933
1934 /**
1935  * g_bookmark_file_to_file:
1936  * @bookmark: a #GBookmarkFile
1937  * @filename: (type filename): path of the output file
1938  * @error: return location for a #GError, or %NULL
1939  *
1940  * This function outputs @bookmark into a file.  The write process is
1941  * guaranteed to be atomic by using g_file_set_contents() internally.
1942  *
1943  * Returns: %TRUE if the file was successfully written.
1944  *
1945  * Since: 2.12
1946  */
1947 gboolean
1948 g_bookmark_file_to_file (GBookmarkFile  *bookmark,
1949                          const gchar    *filename,
1950                          GError        **error)
1951 {
1952   gchar *data;
1953   GError *data_error, *write_error;
1954   gsize len;
1955   gboolean retval;
1956
1957   g_return_val_if_fail (bookmark != NULL, FALSE);
1958   g_return_val_if_fail (filename != NULL, FALSE);
1959   
1960   data_error = NULL;
1961   data = g_bookmark_file_to_data (bookmark, &len, &data_error);
1962   if (data_error)
1963     {
1964       g_propagate_error (error, data_error);
1965       
1966       return FALSE;
1967     }
1968
1969   write_error = NULL;
1970   g_file_set_contents (filename, data, len, &write_error);
1971   if (write_error)
1972     {
1973       g_propagate_error (error, write_error);
1974       
1975       retval = FALSE;
1976     }
1977   else
1978     retval = TRUE;
1979
1980   g_free (data);
1981   
1982   return retval;
1983 }
1984
1985 static BookmarkItem *
1986 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
1987                              const gchar   *uri)
1988 {
1989   g_warn_if_fail (bookmark != NULL && uri != NULL);
1990   
1991   return g_hash_table_lookup (bookmark->items_by_uri, uri);
1992 }
1993
1994 /* this function adds a new item to the list */
1995 static void
1996 g_bookmark_file_add_item (GBookmarkFile  *bookmark,
1997                           BookmarkItem   *item,
1998                           GError        **error)
1999 {
2000   g_warn_if_fail (bookmark != NULL);
2001   g_warn_if_fail (item != NULL);
2002
2003   /* this should never happen; and if it does, then we are
2004    * screwing up something big time.
2005    */
2006   if (G_UNLIKELY (g_bookmark_file_has_item (bookmark, item->uri)))
2007     {
2008       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2009                    G_BOOKMARK_FILE_ERROR_INVALID_URI,
2010                    _("A bookmark for URI “%s” already exists"),
2011                    item->uri);
2012       return;
2013     }
2014   
2015   bookmark->items = g_list_prepend (bookmark->items, item);
2016   
2017   g_hash_table_replace (bookmark->items_by_uri,
2018                         item->uri,
2019                         item);
2020
2021   if (item->added == (time_t) -1)
2022     item->added = time (NULL);
2023   
2024   if (item->modified == (time_t) -1)
2025     item->modified = time (NULL);
2026 }
2027
2028 /**
2029  * g_bookmark_file_remove_item:
2030  * @bookmark: a #GBookmarkFile
2031  * @uri: a valid URI
2032  * @error: return location for a #GError, or %NULL
2033  *
2034  * Removes the bookmark for @uri from the bookmark file @bookmark.
2035  *
2036  * Returns: %TRUE if the bookmark was removed successfully.
2037  * 
2038  * Since: 2.12
2039  */
2040 gboolean
2041 g_bookmark_file_remove_item (GBookmarkFile  *bookmark,
2042                              const gchar    *uri,
2043                              GError        **error)
2044 {
2045   BookmarkItem *item;
2046   
2047   g_return_val_if_fail (bookmark != NULL, FALSE);
2048   g_return_val_if_fail (uri != NULL, FALSE);
2049   
2050   item = g_bookmark_file_lookup_item (bookmark, uri);
2051   
2052   if (!item)
2053     {
2054       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2055                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2056                    _("No bookmark found for URI “%s”"),
2057                    uri);
2058       return FALSE;
2059     }
2060
2061   bookmark->items = g_list_remove (bookmark->items, item);
2062   g_hash_table_remove (bookmark->items_by_uri, item->uri);  
2063   
2064   bookmark_item_free (item);
2065
2066   return TRUE;
2067 }
2068
2069 /**
2070  * g_bookmark_file_has_item:
2071  * @bookmark: a #GBookmarkFile
2072  * @uri: a valid URI
2073  *
2074  * Looks whether the desktop bookmark has an item with its URI set to @uri.
2075  *
2076  * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
2077  *
2078  * Since: 2.12
2079  */
2080 gboolean
2081 g_bookmark_file_has_item (GBookmarkFile *bookmark,
2082                           const gchar   *uri)
2083 {
2084   g_return_val_if_fail (bookmark != NULL, FALSE);
2085   g_return_val_if_fail (uri != NULL, FALSE);
2086   
2087   return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
2088 }
2089
2090 /**
2091  * g_bookmark_file_get_uris:
2092  * @bookmark: a #GBookmarkFile
2093  * @length: (out) (optional): return location for the number of returned URIs, or %NULL
2094  *
2095  * Returns all URIs of the bookmarks in the bookmark file @bookmark.
2096  * The array of returned URIs will be %NULL-terminated, so @length may
2097  * optionally be %NULL.
2098  *
2099  * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
2100  *   Use g_strfreev() to free it.
2101  *
2102  * Since: 2.12
2103  */
2104 gchar **
2105 g_bookmark_file_get_uris (GBookmarkFile *bookmark,
2106                           gsize         *length)
2107 {
2108   GList *l;
2109   gchar **uris;
2110   gsize i, n_items;
2111   
2112   g_return_val_if_fail (bookmark != NULL, NULL);
2113   
2114   n_items = g_list_length (bookmark->items); 
2115   uris = g_new0 (gchar *, n_items + 1);
2116
2117   /* the items are stored in reverse order, so we walk the list backward */
2118   for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
2119     {
2120       BookmarkItem *item = (BookmarkItem *) l->data;
2121       
2122       g_warn_if_fail (item != NULL);
2123       
2124       uris[i++] = g_strdup (item->uri);
2125     }
2126   uris[i] = NULL;
2127   
2128   if (length)
2129     *length = i;
2130   
2131   return uris;
2132 }
2133
2134 /**
2135  * g_bookmark_file_set_title:
2136  * @bookmark: a #GBookmarkFile
2137  * @uri: (nullable): a valid URI or %NULL
2138  * @title: a UTF-8 encoded string
2139  *
2140  * Sets @title as the title of the bookmark for @uri inside the
2141  * bookmark file @bookmark.
2142  *
2143  * If @uri is %NULL, the title of @bookmark is set.
2144  *
2145  * If a bookmark for @uri cannot be found then it is created.
2146  *
2147  * Since: 2.12
2148  */
2149 void
2150 g_bookmark_file_set_title (GBookmarkFile *bookmark,
2151                            const gchar   *uri,
2152                            const gchar   *title)
2153 {
2154   g_return_if_fail (bookmark != NULL);
2155   
2156   if (!uri)
2157     {
2158       g_free (bookmark->title);
2159       bookmark->title = g_strdup (title);
2160     }
2161   else
2162     {
2163       BookmarkItem *item;
2164       
2165       item = g_bookmark_file_lookup_item (bookmark, uri);
2166       if (!item)
2167         {
2168           item = bookmark_item_new (uri);
2169           g_bookmark_file_add_item (bookmark, item, NULL);
2170         }
2171       
2172       g_free (item->title);
2173       item->title = g_strdup (title);
2174       
2175       item->modified = time (NULL);
2176     }
2177 }
2178
2179 /**
2180  * g_bookmark_file_get_title:
2181  * @bookmark: a #GBookmarkFile
2182  * @uri: (nullable): a valid URI or %NULL
2183  * @error: return location for a #GError, or %NULL
2184  *
2185  * Returns the title of the bookmark for @uri.
2186  *
2187  * If @uri is %NULL, the title of @bookmark is returned.
2188  *
2189  * In the event the URI cannot be found, %NULL is returned and
2190  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2191  *
2192  * Returns: a newly allocated string or %NULL if the specified
2193  *   URI cannot be found.
2194  *
2195  * Since: 2.12
2196  */
2197 gchar *
2198 g_bookmark_file_get_title (GBookmarkFile  *bookmark,
2199                            const gchar    *uri,
2200                            GError        **error)
2201 {
2202   BookmarkItem *item;
2203   
2204   g_return_val_if_fail (bookmark != NULL, NULL);
2205   
2206   if (!uri)
2207     return g_strdup (bookmark->title);
2208   
2209   item = g_bookmark_file_lookup_item (bookmark, uri);
2210   if (!item)
2211     {
2212       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2213                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2214                    _("No bookmark found for URI “%s”"),
2215                    uri);
2216       return NULL;
2217     }
2218   
2219   return g_strdup (item->title);
2220 }
2221
2222 /**
2223  * g_bookmark_file_set_description:
2224  * @bookmark: a #GBookmarkFile
2225  * @uri: (nullable): a valid URI or %NULL
2226  * @description: a string
2227  *
2228  * Sets @description as the description of the bookmark for @uri.
2229  *
2230  * If @uri is %NULL, the description of @bookmark is set.
2231  *
2232  * If a bookmark for @uri cannot be found then it is created.
2233  *
2234  * Since: 2.12
2235  */
2236 void
2237 g_bookmark_file_set_description (GBookmarkFile *bookmark,
2238                                  const gchar   *uri,
2239                                  const gchar   *description)
2240 {
2241   g_return_if_fail (bookmark != NULL);
2242
2243   if (!uri)
2244     {
2245       g_free (bookmark->description); 
2246       bookmark->description = g_strdup (description);
2247     }
2248   else
2249     {
2250       BookmarkItem *item;
2251       
2252       item = g_bookmark_file_lookup_item (bookmark, uri);
2253       if (!item)
2254         {
2255           item = bookmark_item_new (uri);
2256           g_bookmark_file_add_item (bookmark, item, NULL);
2257         }
2258       
2259       g_free (item->description);
2260       item->description = g_strdup (description);
2261       
2262       item->modified = time (NULL);
2263     }
2264 }
2265
2266 /**
2267  * g_bookmark_file_get_description:
2268  * @bookmark: a #GBookmarkFile
2269  * @uri: a valid URI
2270  * @error: return location for a #GError, or %NULL
2271  *
2272  * Retrieves the description of the bookmark for @uri.
2273  *
2274  * In the event the URI cannot be found, %NULL is returned and
2275  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2276  *
2277  * Returns: a newly allocated string or %NULL if the specified
2278  *   URI cannot be found.
2279  *
2280  * Since: 2.12
2281  */
2282 gchar *
2283 g_bookmark_file_get_description (GBookmarkFile  *bookmark,
2284                                  const gchar    *uri,
2285                                  GError        **error)
2286 {
2287   BookmarkItem *item;
2288   
2289   g_return_val_if_fail (bookmark != NULL, NULL);
2290
2291   if (!uri)
2292     return g_strdup (bookmark->description);
2293   
2294   item = g_bookmark_file_lookup_item (bookmark, uri);
2295   if (!item)
2296     {
2297       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2298                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2299                    _("No bookmark found for URI “%s”"),
2300                    uri);
2301       return NULL;
2302     }
2303   
2304   return g_strdup (item->description);
2305 }
2306
2307 /**
2308  * g_bookmark_file_set_mime_type:
2309  * @bookmark: a #GBookmarkFile
2310  * @uri: a valid URI
2311  * @mime_type: a MIME type
2312  *
2313  * Sets @mime_type as the MIME type of the bookmark for @uri.
2314  *
2315  * If a bookmark for @uri cannot be found then it is created.
2316  *
2317  * Since: 2.12
2318  */
2319 void
2320 g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
2321                                const gchar   *uri,
2322                                const gchar   *mime_type)
2323 {
2324   BookmarkItem *item;
2325   
2326   g_return_if_fail (bookmark != NULL);
2327   g_return_if_fail (uri != NULL);
2328   g_return_if_fail (mime_type != NULL);
2329   
2330   item = g_bookmark_file_lookup_item (bookmark, uri);
2331   if (!item)
2332     {
2333       item = bookmark_item_new (uri);
2334       g_bookmark_file_add_item (bookmark, item, NULL);
2335     }
2336   
2337   if (!item->metadata)
2338     item->metadata = bookmark_metadata_new ();
2339   
2340   g_free (item->metadata->mime_type);
2341   
2342   item->metadata->mime_type = g_strdup (mime_type);
2343   item->modified = time (NULL);
2344 }
2345
2346 /**
2347  * g_bookmark_file_get_mime_type:
2348  * @bookmark: a #GBookmarkFile
2349  * @uri: a valid URI
2350  * @error: return location for a #GError, or %NULL
2351  *
2352  * Retrieves the MIME type of the resource pointed by @uri.
2353  *
2354  * In the event the URI cannot be found, %NULL is returned and
2355  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
2356  * event that the MIME type cannot be found, %NULL is returned and
2357  * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2358  *
2359  * Returns: a newly allocated string or %NULL if the specified
2360  *   URI cannot be found.
2361  *
2362  * Since: 2.12
2363  */
2364 gchar *
2365 g_bookmark_file_get_mime_type (GBookmarkFile  *bookmark,
2366                                const gchar    *uri,
2367                                GError        **error)
2368 {
2369   BookmarkItem *item;
2370   
2371   g_return_val_if_fail (bookmark != NULL, NULL);
2372   g_return_val_if_fail (uri != NULL, NULL);
2373   
2374   item = g_bookmark_file_lookup_item (bookmark, uri);
2375   if (!item)
2376     {
2377       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2378                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2379                    _("No bookmark found for URI “%s”"),
2380                    uri);
2381       return NULL;
2382     }
2383   
2384   if (!item->metadata)
2385     {
2386       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2387                    G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2388                    _("No MIME type defined in the bookmark for URI “%s”"),
2389                    uri);
2390       return NULL;
2391     }
2392   
2393   return g_strdup (item->metadata->mime_type);
2394 }
2395
2396 /**
2397  * g_bookmark_file_set_is_private:
2398  * @bookmark: a #GBookmarkFile
2399  * @uri: a valid URI
2400  * @is_private: %TRUE if the bookmark should be marked as private
2401  *
2402  * Sets the private flag of the bookmark for @uri.
2403  *
2404  * If a bookmark for @uri cannot be found then it is created.
2405  *
2406  * Since: 2.12
2407  */
2408 void
2409 g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
2410                                 const gchar   *uri,
2411                                 gboolean       is_private)
2412 {
2413   BookmarkItem *item;
2414   
2415   g_return_if_fail (bookmark != NULL);
2416   g_return_if_fail (uri != NULL);
2417   
2418   item = g_bookmark_file_lookup_item (bookmark, uri);
2419   if (!item)
2420     {
2421       item = bookmark_item_new (uri);
2422       g_bookmark_file_add_item (bookmark, item, NULL);
2423     }
2424   
2425   if (!item->metadata)
2426     item->metadata = bookmark_metadata_new ();
2427   
2428   item->metadata->is_private = (is_private == TRUE);
2429   item->modified = time (NULL);
2430 }
2431
2432 /**
2433  * g_bookmark_file_get_is_private:
2434  * @bookmark: a #GBookmarkFile
2435  * @uri: a valid URI
2436  * @error: return location for a #GError, or %NULL
2437  *
2438  * Gets whether the private flag of the bookmark for @uri is set.
2439  *
2440  * In the event the URI cannot be found, %FALSE is returned and
2441  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
2442  * event that the private flag cannot be found, %FALSE is returned and
2443  * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2444  *
2445  * Returns: %TRUE if the private flag is set, %FALSE otherwise.
2446  *
2447  * Since: 2.12
2448  */
2449 gboolean
2450 g_bookmark_file_get_is_private (GBookmarkFile  *bookmark,
2451                                 const gchar    *uri,
2452                                 GError        **error)
2453 {
2454   BookmarkItem *item;
2455   
2456   g_return_val_if_fail (bookmark != NULL, FALSE);
2457   g_return_val_if_fail (uri != NULL, FALSE);
2458   
2459   item = g_bookmark_file_lookup_item (bookmark, uri);
2460   if (!item)
2461     {
2462       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2463                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2464                    _("No bookmark found for URI “%s”"),
2465                    uri);
2466       return FALSE;
2467     }
2468   
2469   if (!item->metadata)
2470     {
2471       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2472                    G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2473                    _("No private flag has been defined in bookmark for URI “%s”"),
2474                     uri);
2475       return FALSE;
2476     }
2477   
2478   return item->metadata->is_private;
2479 }
2480
2481 /**
2482  * g_bookmark_file_set_added:
2483  * @bookmark: a #GBookmarkFile
2484  * @uri: a valid URI
2485  * @added: a timestamp or -1 to use the current time
2486  *
2487  * Sets the time the bookmark for @uri was added into @bookmark.
2488  *
2489  * If no bookmark for @uri is found then it is created.
2490  *
2491  * Since: 2.12
2492  */
2493 void
2494 g_bookmark_file_set_added (GBookmarkFile *bookmark,
2495                            const gchar   *uri,
2496                            time_t         added)
2497 {
2498   BookmarkItem *item;
2499   
2500   g_return_if_fail (bookmark != NULL);
2501   g_return_if_fail (uri != NULL);
2502   
2503   item = g_bookmark_file_lookup_item (bookmark, uri);
2504   if (!item)
2505     {
2506       item = bookmark_item_new (uri);
2507       g_bookmark_file_add_item (bookmark, item, NULL);
2508     }
2509
2510   if (added == (time_t) -1)
2511     time (&added);
2512   
2513   item->added = added;
2514   item->modified = added;
2515 }
2516
2517 /**
2518  * g_bookmark_file_get_added:
2519  * @bookmark: a #GBookmarkFile
2520  * @uri: a valid URI
2521  * @error: return location for a #GError, or %NULL
2522  *
2523  * Gets the time the bookmark for @uri was added to @bookmark
2524  *
2525  * In the event the URI cannot be found, -1 is returned and
2526  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2527  *
2528  * Returns: a timestamp
2529  *
2530  * Since: 2.12
2531  */
2532 time_t
2533 g_bookmark_file_get_added (GBookmarkFile  *bookmark,
2534                            const gchar    *uri,
2535                            GError        **error)
2536 {
2537   BookmarkItem *item;
2538   
2539   g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2540   g_return_val_if_fail (uri != NULL, (time_t) -1);
2541   
2542   item = g_bookmark_file_lookup_item (bookmark, uri);
2543   if (!item)
2544     {
2545       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2546                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2547                    _("No bookmark found for URI “%s”"),
2548                    uri);
2549       return (time_t) -1;
2550     }
2551   
2552   return item->added;
2553 }
2554
2555 /**
2556  * g_bookmark_file_set_modified:
2557  * @bookmark: a #GBookmarkFile
2558  * @uri: a valid URI
2559  * @modified: a timestamp or -1 to use the current time
2560  *
2561  * Sets the last time the bookmark for @uri was last modified.
2562  *
2563  * If no bookmark for @uri is found then it is created.
2564  *
2565  * The "modified" time should only be set when the bookmark's meta-data
2566  * was actually changed.  Every function of #GBookmarkFile that
2567  * modifies a bookmark also changes the modification time, except for
2568  * g_bookmark_file_set_visited().
2569  *
2570  * Since: 2.12
2571  */
2572 void
2573 g_bookmark_file_set_modified (GBookmarkFile *bookmark,
2574                               const gchar   *uri,
2575                               time_t         modified)
2576 {
2577   BookmarkItem *item;
2578   
2579   g_return_if_fail (bookmark != NULL);
2580   g_return_if_fail (uri != NULL);
2581   
2582   item = g_bookmark_file_lookup_item (bookmark, uri);
2583   if (!item)
2584     {
2585       item = bookmark_item_new (uri);
2586       g_bookmark_file_add_item (bookmark, item, NULL);
2587     }
2588   
2589   if (modified == (time_t) -1)
2590     time (&modified);
2591   
2592   item->modified = modified;
2593 }
2594
2595 /**
2596  * g_bookmark_file_get_modified:
2597  * @bookmark: a #GBookmarkFile
2598  * @uri: a valid URI
2599  * @error: return location for a #GError, or %NULL
2600  *
2601  * Gets the time when the bookmark for @uri was last modified.
2602  *
2603  * In the event the URI cannot be found, -1 is returned and
2604  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2605  *
2606  * Returns: a timestamp
2607  *
2608  * Since: 2.12
2609  */
2610 time_t
2611 g_bookmark_file_get_modified (GBookmarkFile  *bookmark,
2612                               const gchar    *uri,
2613                               GError        **error)
2614 {
2615   BookmarkItem *item;
2616   
2617   g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2618   g_return_val_if_fail (uri != NULL, (time_t) -1);
2619   
2620   item = g_bookmark_file_lookup_item (bookmark, uri);
2621   if (!item)
2622     {
2623       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2624                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2625                    _("No bookmark found for URI “%s”"),
2626                    uri);
2627       return (time_t) -1;
2628     }
2629   
2630   return item->modified;
2631 }
2632
2633 /**
2634  * g_bookmark_file_set_visited:
2635  * @bookmark: a #GBookmarkFile
2636  * @uri: a valid URI
2637  * @visited: a timestamp or -1 to use the current time
2638  *
2639  * Sets the time the bookmark for @uri was last visited.
2640  *
2641  * If no bookmark for @uri is found then it is created.
2642  *
2643  * The "visited" time should only be set if the bookmark was launched, 
2644  * either using the command line retrieved by g_bookmark_file_get_app_info()
2645  * or by the default application for the bookmark's MIME type, retrieved
2646  * using g_bookmark_file_get_mime_type().  Changing the "visited" time
2647  * does not affect the "modified" time.
2648  *
2649  * Since: 2.12
2650  */
2651 void
2652 g_bookmark_file_set_visited (GBookmarkFile *bookmark,
2653                              const gchar   *uri,
2654                              time_t         visited)
2655 {
2656   BookmarkItem *item;
2657   
2658   g_return_if_fail (bookmark != NULL);
2659   g_return_if_fail (uri != NULL);
2660   
2661   item = g_bookmark_file_lookup_item (bookmark, uri);
2662   if (!item)
2663     {
2664       item = bookmark_item_new (uri);
2665       g_bookmark_file_add_item (bookmark, item, NULL);
2666     }
2667
2668   if (visited == (time_t) -1)
2669     time (&visited);
2670   
2671   item->visited = visited;
2672 }
2673
2674 /**
2675  * g_bookmark_file_get_visited:
2676  * @bookmark: a #GBookmarkFile
2677  * @uri: a valid URI
2678  * @error: return location for a #GError, or %NULL
2679  *
2680  * Gets the time the bookmark for @uri was last visited.
2681  *
2682  * In the event the URI cannot be found, -1 is returned and
2683  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2684  *
2685  * Returns: a timestamp.
2686  *
2687  * Since: 2.12
2688  */
2689 time_t
2690 g_bookmark_file_get_visited (GBookmarkFile  *bookmark,
2691                              const gchar    *uri,
2692                              GError        **error)
2693 {
2694   BookmarkItem *item;
2695   
2696   g_return_val_if_fail (bookmark != NULL, (time_t) -1);
2697   g_return_val_if_fail (uri != NULL, (time_t) -1);
2698   
2699   item = g_bookmark_file_lookup_item (bookmark, uri);
2700   if (!item)
2701     {
2702       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2703                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2704                    _("No bookmark found for URI “%s”"),
2705                    uri);
2706       return (time_t) -1;
2707     }
2708   
2709   return item->visited;
2710 }
2711
2712 /**
2713  * g_bookmark_file_has_group:
2714  * @bookmark: a #GBookmarkFile
2715  * @uri: a valid URI
2716  * @group: the group name to be searched
2717  * @error: return location for a #GError, or %NULL
2718  *
2719  * Checks whether @group appears in the list of groups to which
2720  * the bookmark for @uri belongs to.
2721  *
2722  * In the event the URI cannot be found, %FALSE is returned and
2723  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2724  *
2725  * Returns: %TRUE if @group was found.
2726  *
2727  * Since: 2.12
2728  */
2729 gboolean
2730 g_bookmark_file_has_group (GBookmarkFile  *bookmark,
2731                            const gchar    *uri,
2732                            const gchar    *group,
2733                            GError        **error)
2734 {
2735   BookmarkItem *item;
2736   GList *l;
2737   
2738   g_return_val_if_fail (bookmark != NULL, FALSE);
2739   g_return_val_if_fail (uri != NULL, FALSE);
2740   
2741   item = g_bookmark_file_lookup_item (bookmark, uri);
2742   if (!item)
2743     {
2744       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2745                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2746                    _("No bookmark found for URI “%s”"),
2747                    uri);
2748       return FALSE;
2749     }
2750   
2751   if (!item->metadata)
2752     return FALSE;
2753    
2754   for (l = item->metadata->groups; l != NULL; l = l->next)
2755     {
2756       if (strcmp (l->data, group) == 0)
2757         return TRUE;
2758     }
2759   
2760   return FALSE;
2761
2762 }
2763
2764 /**
2765  * g_bookmark_file_add_group:
2766  * @bookmark: a #GBookmarkFile
2767  * @uri: a valid URI
2768  * @group: the group name to be added
2769  *
2770  * Adds @group to the list of groups to which the bookmark for @uri
2771  * belongs to.
2772  *
2773  * If no bookmark for @uri is found then it is created.
2774  *
2775  * Since: 2.12
2776  */
2777 void
2778 g_bookmark_file_add_group (GBookmarkFile *bookmark,
2779                            const gchar   *uri,
2780                            const gchar   *group)
2781 {
2782   BookmarkItem *item;
2783   
2784   g_return_if_fail (bookmark != NULL);
2785   g_return_if_fail (uri != NULL);
2786   g_return_if_fail (group != NULL && group[0] != '\0');
2787   
2788   item = g_bookmark_file_lookup_item (bookmark, uri);
2789   if (!item)
2790     {
2791       item = bookmark_item_new (uri);
2792       g_bookmark_file_add_item (bookmark, item, NULL);
2793     }
2794   
2795   if (!item->metadata)
2796     item->metadata = bookmark_metadata_new ();
2797   
2798   if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
2799     {
2800       item->metadata->groups = g_list_prepend (item->metadata->groups,
2801                                                g_strdup (group));
2802       
2803       item->modified = time (NULL);
2804     }
2805 }
2806
2807 /**
2808  * g_bookmark_file_remove_group:
2809  * @bookmark: a #GBookmarkFile
2810  * @uri: a valid URI
2811  * @group: the group name to be removed
2812  * @error: return location for a #GError, or %NULL
2813  *
2814  * Removes @group from the list of groups to which the bookmark
2815  * for @uri belongs to.
2816  *
2817  * In the event the URI cannot be found, %FALSE is returned and
2818  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2819  * In the event no group was defined, %FALSE is returned and
2820  * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
2821  *
2822  * Returns: %TRUE if @group was successfully removed.
2823  *
2824  * Since: 2.12
2825  */
2826 gboolean
2827 g_bookmark_file_remove_group (GBookmarkFile  *bookmark,
2828                               const gchar    *uri,
2829                               const gchar    *group,
2830                               GError        **error)
2831 {
2832   BookmarkItem *item;
2833   GList *l;
2834   
2835   g_return_val_if_fail (bookmark != NULL, FALSE);
2836   g_return_val_if_fail (uri != NULL, FALSE);
2837   
2838   item = g_bookmark_file_lookup_item (bookmark, uri);
2839   if (!item)
2840     {
2841       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2842                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2843                    _("No bookmark found for URI “%s”"),
2844                    uri);
2845       return FALSE;
2846     }
2847   
2848   if (!item->metadata)
2849     {
2850       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2851                    G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
2852                    _("No groups set in bookmark for URI “%s”"),
2853                    uri);
2854       return FALSE;
2855     }
2856   
2857   for (l = item->metadata->groups; l != NULL; l = l->next)
2858     {
2859       if (strcmp (l->data, group) == 0)
2860         {
2861           item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
2862           g_free (l->data);
2863           g_list_free_1 (l);
2864           
2865           item->modified = time (NULL);          
2866           
2867           return TRUE;
2868         }
2869     }
2870   
2871   return FALSE;
2872 }
2873
2874 /**
2875  * g_bookmark_file_set_groups:
2876  * @bookmark: a #GBookmarkFile
2877  * @uri: an item's URI
2878  * @groups: (nullable) (array length=length) (element-type utf8): an array of
2879  *    group names, or %NULL to remove all groups
2880  * @length: number of group name values in @groups
2881  *
2882  * Sets a list of group names for the item with URI @uri.  Each previously
2883  * set group name list is removed.
2884  *
2885  * If @uri cannot be found then an item for it is created.
2886  *
2887  * Since: 2.12
2888  */
2889 void
2890 g_bookmark_file_set_groups (GBookmarkFile  *bookmark,
2891                             const gchar    *uri,
2892                             const gchar   **groups,
2893                             gsize           length)
2894 {
2895   BookmarkItem *item;
2896   gsize i;
2897   
2898   g_return_if_fail (bookmark != NULL);
2899   g_return_if_fail (uri != NULL);
2900   g_return_if_fail (groups != NULL);
2901   
2902   item = g_bookmark_file_lookup_item (bookmark, uri);
2903   if (!item)
2904     {
2905       item = bookmark_item_new (uri);
2906       g_bookmark_file_add_item (bookmark, item, NULL);
2907     }
2908   
2909   if (!item->metadata)
2910     item->metadata = bookmark_metadata_new ();
2911
2912   g_list_free_full (item->metadata->groups, g_free);
2913   item->metadata->groups = NULL;
2914   
2915   if (groups)
2916     {
2917       for (i = 0; i < length && groups[i] != NULL; i++)
2918         item->metadata->groups = g_list_append (item->metadata->groups,
2919                                                 g_strdup (groups[i]));
2920     }
2921
2922   item->modified = time (NULL);
2923 }
2924
2925 /**
2926  * g_bookmark_file_get_groups:
2927  * @bookmark: a #GBookmarkFile
2928  * @uri: a valid URI
2929  * @length: (out) (optional): return location for the length of the returned string, or %NULL
2930  * @error: return location for a #GError, or %NULL
2931  *
2932  * Retrieves the list of group names of the bookmark for @uri.
2933  *
2934  * In the event the URI cannot be found, %NULL is returned and
2935  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
2936  *
2937  * The returned array is %NULL terminated, so @length may optionally
2938  * be %NULL.
2939  *
2940  * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
2941  *   Use g_strfreev() to free it.
2942  *
2943  * Since: 2.12
2944  */
2945 gchar **
2946 g_bookmark_file_get_groups (GBookmarkFile  *bookmark,
2947                             const gchar    *uri,
2948                             gsize          *length,
2949                             GError        **error)
2950 {
2951   BookmarkItem *item;
2952   GList *l;
2953   gsize len, i;
2954   gchar **retval;
2955   
2956   g_return_val_if_fail (bookmark != NULL, NULL);
2957   g_return_val_if_fail (uri != NULL, NULL);
2958   
2959   item = g_bookmark_file_lookup_item (bookmark, uri);
2960   if (!item)
2961     {
2962       g_set_error (error, G_BOOKMARK_FILE_ERROR,
2963                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
2964                    _("No bookmark found for URI “%s”"),
2965                    uri);
2966       return NULL;
2967     }
2968   
2969   if (!item->metadata)
2970     {
2971       if (length)
2972         *length = 0;
2973       
2974       return NULL;
2975     }
2976   
2977   len = g_list_length (item->metadata->groups);
2978   retval = g_new0 (gchar *, len + 1);
2979   for (l = g_list_last (item->metadata->groups), i = 0;
2980        l != NULL;
2981        l = l->prev)
2982     {
2983       gchar *group_name = (gchar *) l->data;
2984       
2985       g_warn_if_fail (group_name != NULL);
2986       
2987       retval[i++] = g_strdup (group_name);
2988     }
2989   retval[i] = NULL;
2990   
2991   if (length)
2992     *length = len;
2993   
2994   return retval;
2995 }
2996
2997 /**
2998  * g_bookmark_file_add_application:
2999  * @bookmark: a #GBookmarkFile
3000  * @uri: a valid URI
3001  * @name: (nullable): the name of the application registering the bookmark
3002  *   or %NULL
3003  * @exec: (nullable): command line to be used to launch the bookmark or %NULL
3004  *
3005  * Adds the application with @name and @exec to the list of
3006  * applications that have registered a bookmark for @uri into
3007  * @bookmark.
3008  *
3009  * Every bookmark inside a #GBookmarkFile must have at least an
3010  * application registered.  Each application must provide a name, a
3011  * command line useful for launching the bookmark, the number of times
3012  * the bookmark has been registered by the application and the last
3013  * time the application registered this bookmark.
3014  *
3015  * If @name is %NULL, the name of the application will be the
3016  * same returned by g_get_application_name(); if @exec is %NULL, the
3017  * command line will be a composition of the program name as
3018  * returned by g_get_prgname() and the "\%u" modifier, which will be
3019  * expanded to the bookmark's URI.
3020  *
3021  * This function will automatically take care of updating the
3022  * registrations count and timestamping in case an application
3023  * with the same @name had already registered a bookmark for
3024  * @uri inside @bookmark.
3025  *
3026  * If no bookmark for @uri is found, one is created.
3027  *
3028  * Since: 2.12
3029  */
3030 void
3031 g_bookmark_file_add_application (GBookmarkFile *bookmark,
3032                                  const gchar   *uri,
3033                                  const gchar   *name,
3034                                  const gchar   *exec)
3035 {
3036   BookmarkItem *item;
3037   gchar *app_name, *app_exec;
3038   
3039   g_return_if_fail (bookmark != NULL);
3040   g_return_if_fail (uri != NULL);
3041   
3042   item = g_bookmark_file_lookup_item (bookmark, uri);
3043   if (!item)
3044     {
3045       item = bookmark_item_new (uri);
3046       g_bookmark_file_add_item (bookmark, item, NULL);
3047     }
3048   
3049   if (name && name[0] != '\0')
3050     app_name = g_strdup (name);
3051   else
3052     app_name = g_strdup (g_get_application_name ());
3053   
3054   if (exec && exec[0] != '\0')
3055     app_exec = g_strdup (exec);
3056   else
3057     app_exec = g_strjoin (" ", g_get_prgname(), "%u", NULL);
3058
3059   g_bookmark_file_set_app_info (bookmark, uri,
3060                                 app_name,
3061                                 app_exec,
3062                                 -1,
3063                                 (time_t) -1,
3064                                 NULL);
3065   
3066   g_free (app_exec);
3067   g_free (app_name);
3068 }
3069
3070 /**
3071  * g_bookmark_file_remove_application:
3072  * @bookmark: a #GBookmarkFile
3073  * @uri: a valid URI
3074  * @name: the name of the application
3075  * @error: return location for a #GError or %NULL
3076  *
3077  * Removes application registered with @name from the list of applications
3078  * that have registered a bookmark for @uri inside @bookmark.
3079  *
3080  * In the event the URI cannot be found, %FALSE is returned and
3081  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3082  * In the event that no application with name @app_name has registered
3083  * a bookmark for @uri,  %FALSE is returned and error is set to
3084  * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
3085  *
3086  * Returns: %TRUE if the application was successfully removed.
3087  *
3088  * Since: 2.12
3089  */
3090 gboolean
3091 g_bookmark_file_remove_application (GBookmarkFile  *bookmark,
3092                                     const gchar    *uri,
3093                                     const gchar    *name,
3094                                     GError        **error)
3095 {
3096   GError *set_error;
3097   gboolean retval;
3098     
3099   g_return_val_if_fail (bookmark != NULL, FALSE);
3100   g_return_val_if_fail (uri != NULL, FALSE);
3101   g_return_val_if_fail (name != NULL, FALSE);
3102   
3103   set_error = NULL;
3104   retval = g_bookmark_file_set_app_info (bookmark, uri,
3105                                          name,
3106                                          "",
3107                                          0,
3108                                          (time_t) -1,
3109                                          &set_error);
3110   if (set_error)
3111     {
3112       g_propagate_error (error, set_error);
3113       
3114       return FALSE;
3115     }
3116   
3117   return retval;
3118 }
3119
3120 /**
3121  * g_bookmark_file_has_application:
3122  * @bookmark: a #GBookmarkFile
3123  * @uri: a valid URI
3124  * @name: the name of the application
3125  * @error: return location for a #GError or %NULL
3126  *
3127  * Checks whether the bookmark for @uri inside @bookmark has been
3128  * registered by application @name.
3129  *
3130  * In the event the URI cannot be found, %FALSE is returned and
3131  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3132  *
3133  * Returns: %TRUE if the application @name was found
3134  *
3135  * Since: 2.12
3136  */
3137 gboolean
3138 g_bookmark_file_has_application (GBookmarkFile  *bookmark,
3139                                  const gchar    *uri,
3140                                  const gchar    *name,
3141                                  GError        **error)
3142 {
3143   BookmarkItem *item;
3144   
3145   g_return_val_if_fail (bookmark != NULL, FALSE);
3146   g_return_val_if_fail (uri != NULL, FALSE);
3147   g_return_val_if_fail (name != NULL, FALSE);
3148   
3149   item = g_bookmark_file_lookup_item (bookmark, uri);
3150   if (!item)
3151     {
3152       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3153                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3154                    _("No bookmark found for URI “%s”"),
3155                    uri);
3156       return FALSE;
3157     }
3158   
3159   return (NULL != bookmark_item_lookup_app_info (item, name));
3160 }
3161
3162 /**
3163  * g_bookmark_file_set_app_info:
3164  * @bookmark: a #GBookmarkFile
3165  * @uri: a valid URI
3166  * @name: an application's name
3167  * @exec: an application's command line
3168  * @count: the number of registrations done for this application
3169  * @stamp: the time of the last registration for this application
3170  * @error: return location for a #GError or %NULL
3171  *
3172  * Sets the meta-data of application @name inside the list of
3173  * applications that have registered a bookmark for @uri inside
3174  * @bookmark.
3175  *
3176  * You should rarely use this function; use g_bookmark_file_add_application()
3177  * and g_bookmark_file_remove_application() instead.
3178  *
3179  * @name can be any UTF-8 encoded string used to identify an
3180  * application.
3181  * @exec can have one of these two modifiers: "\%f", which will
3182  * be expanded as the local file name retrieved from the bookmark's
3183  * URI; "\%u", which will be expanded as the bookmark's URI.
3184  * The expansion is done automatically when retrieving the stored
3185  * command line using the g_bookmark_file_get_app_info() function.
3186  * @count is the number of times the application has registered the
3187  * bookmark; if is < 0, the current registration count will be increased
3188  * by one, if is 0, the application with @name will be removed from
3189  * the list of registered applications.
3190  * @stamp is the Unix time of the last registration; if it is -1, the
3191  * current time will be used.
3192  *
3193  * If you try to remove an application by setting its registration count to
3194  * zero, and no bookmark for @uri is found, %FALSE is returned and
3195  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND; similarly,
3196  * in the event that no application @name has registered a bookmark
3197  * for @uri,  %FALSE is returned and error is set to
3198  * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.  Otherwise, if no bookmark
3199  * for @uri is found, one is created.
3200  *
3201  * Returns: %TRUE if the application's meta-data was successfully
3202  *   changed.
3203  *
3204  * Since: 2.12
3205  */
3206 gboolean
3207 g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
3208                               const gchar    *uri,
3209                               const gchar    *name,
3210                               const gchar    *exec,
3211                               gint            count,
3212                               time_t          stamp,
3213                               GError        **error)
3214 {
3215   BookmarkItem *item;
3216   BookmarkAppInfo *ai;
3217   
3218   g_return_val_if_fail (bookmark != NULL, FALSE);
3219   g_return_val_if_fail (uri != NULL, FALSE);
3220   g_return_val_if_fail (name != NULL, FALSE);
3221   g_return_val_if_fail (exec != NULL, FALSE);
3222   
3223   item = g_bookmark_file_lookup_item (bookmark, uri);
3224   if (!item)
3225     {
3226       if (count == 0)
3227         {
3228           g_set_error (error, G_BOOKMARK_FILE_ERROR,
3229                        G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3230                        _("No bookmark found for URI “%s”"),
3231                        uri);
3232           return FALSE;
3233         }
3234       else
3235         {
3236           item = bookmark_item_new (uri);
3237           g_bookmark_file_add_item (bookmark, item, NULL);
3238         }
3239     }
3240   
3241   if (!item->metadata)
3242     item->metadata = bookmark_metadata_new ();
3243
3244   ai = bookmark_item_lookup_app_info (item, name);
3245   if (!ai)
3246     {
3247       if (count == 0)
3248         {
3249           g_set_error (error, G_BOOKMARK_FILE_ERROR,
3250                        G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3251                        _("No application with name “%s” registered a bookmark for “%s”"),
3252                        name,
3253                        uri);
3254           return FALSE;
3255         }
3256       else
3257         {
3258           ai = bookmark_app_info_new (name);
3259           
3260           item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
3261           g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
3262         }
3263     }
3264
3265   if (count == 0)
3266     {
3267       item->metadata->applications = g_list_remove (item->metadata->applications, ai);
3268       g_hash_table_remove (item->metadata->apps_by_name, ai->name);
3269       bookmark_app_info_free (ai);
3270
3271       item->modified = time (NULL);
3272           
3273       return TRUE;
3274     }
3275   else if (count > 0)
3276     ai->count = count;
3277   else
3278     ai->count += 1;
3279       
3280   if (stamp != (time_t) -1)
3281     ai->stamp = stamp;
3282   else
3283     ai->stamp = time (NULL);
3284   
3285   if (exec && exec[0] != '\0')
3286     {
3287       g_free (ai->exec);
3288       ai->exec = g_shell_quote (exec);
3289     }
3290   
3291   item->modified = time (NULL);
3292   
3293   return TRUE;
3294 }
3295
3296 /* expands the application's command line */
3297 static gchar *
3298 expand_exec_line (const gchar *exec_fmt,
3299                   const gchar *uri)
3300 {
3301   GString *exec;
3302   gchar ch;
3303   
3304   exec = g_string_sized_new (512);
3305   while ((ch = *exec_fmt++) != '\0')
3306    {
3307      if (ch != '%')
3308        {
3309          exec = g_string_append_c (exec, ch);
3310          continue;
3311        }
3312      
3313      ch = *exec_fmt++;
3314      switch (ch)
3315        {
3316        case '\0':
3317          goto out;
3318        case 'U':
3319        case 'u':
3320          g_string_append (exec, uri);
3321          break;
3322        case 'F':
3323        case 'f':
3324          {
3325            gchar *file = g_filename_from_uri (uri, NULL, NULL);
3326            if (file)
3327              {
3328                g_string_append (exec, file);
3329                g_free (file);
3330              }
3331            else
3332              {
3333                g_string_free (exec, TRUE);
3334                return NULL;
3335              }
3336          }
3337          break;
3338        case '%':
3339        default:
3340          exec = g_string_append_c (exec, ch);
3341          break;
3342        }
3343    }
3344    
3345  out:
3346   return g_string_free (exec, FALSE);
3347 }
3348
3349 /**
3350  * g_bookmark_file_get_app_info:
3351  * @bookmark: a #GBookmarkFile
3352  * @uri: a valid URI
3353  * @name: an application's name
3354  * @exec: (out) (optional): return location for the command line of the application, or %NULL
3355  * @count: (out) (optional): return location for the registration count, or %NULL
3356  * @stamp: (out) (optional): return location for the last registration time, or %NULL
3357  * @error: return location for a #GError, or %NULL
3358  *
3359  * Gets the registration informations of @app_name for the bookmark for
3360  * @uri.  See g_bookmark_file_set_app_info() for more informations about
3361  * the returned data.
3362  *
3363  * The string returned in @app_exec must be freed.
3364  *
3365  * In the event the URI cannot be found, %FALSE is returned and
3366  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
3367  * event that no application with name @app_name has registered a bookmark
3368  * for @uri,  %FALSE is returned and error is set to
3369  * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
3370  * the command line fails, an error of the #G_SHELL_ERROR domain is
3371  * set and %FALSE is returned.
3372  *
3373  * Returns: %TRUE on success.
3374  *
3375  * Since: 2.12
3376  */
3377 gboolean
3378 g_bookmark_file_get_app_info (GBookmarkFile  *bookmark,
3379                               const gchar    *uri,
3380                               const gchar    *name,
3381                               gchar         **exec,
3382                               guint          *count,
3383                               time_t         *stamp,
3384                               GError        **error)
3385 {
3386   BookmarkItem *item;
3387   BookmarkAppInfo *ai;
3388   
3389   g_return_val_if_fail (bookmark != NULL, FALSE);
3390   g_return_val_if_fail (uri != NULL, FALSE);
3391   g_return_val_if_fail (name != NULL, FALSE);
3392   
3393   item = g_bookmark_file_lookup_item (bookmark, uri);
3394   if (!item)
3395     {
3396       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3397                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3398                    _("No bookmark found for URI “%s”"),
3399                    uri);
3400       return FALSE;
3401     }
3402   
3403   ai = bookmark_item_lookup_app_info (item, name);
3404   if (!ai)
3405     {
3406       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3407                    G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
3408                    _("No application with name “%s” registered a bookmark for “%s”"),
3409                    name,
3410                    uri);
3411       return FALSE;
3412     }
3413   
3414   if (exec)
3415     {
3416       GError *unquote_error = NULL;
3417       gchar *command_line;
3418       
3419       command_line = g_shell_unquote (ai->exec, &unquote_error);
3420       if (unquote_error)
3421         {
3422           g_propagate_error (error, unquote_error);
3423           return FALSE;
3424         }
3425
3426       *exec = expand_exec_line (command_line, uri);
3427       if (!*exec)
3428         {
3429           g_set_error (error, G_BOOKMARK_FILE_ERROR,
3430                        G_BOOKMARK_FILE_ERROR_INVALID_URI,
3431                        _("Failed to expand exec line “%s” with URI “%s”"),
3432                      ai->exec, uri);
3433           g_free (command_line);
3434
3435           return FALSE;
3436         }
3437       else
3438         g_free (command_line);
3439     } 
3440
3441   if (count)
3442     *count = ai->count;
3443   
3444   if (stamp)
3445     *stamp = ai->stamp;
3446   
3447   return TRUE;
3448 }
3449
3450 /**
3451  * g_bookmark_file_get_applications:
3452  * @bookmark: a #GBookmarkFile
3453  * @uri: a valid URI
3454  * @length: (out) (optional): return location of the length of the returned list, or %NULL
3455  * @error: return location for a #GError, or %NULL
3456  *
3457  * Retrieves the names of the applications that have registered the
3458  * bookmark for @uri.
3459  * 
3460  * In the event the URI cannot be found, %NULL is returned and
3461  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3462  *
3463  * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
3464  *   Use g_strfreev() to free it.
3465  *
3466  * Since: 2.12
3467  */
3468 gchar **
3469 g_bookmark_file_get_applications (GBookmarkFile  *bookmark,
3470                                   const gchar    *uri,
3471                                   gsize          *length,
3472                                   GError        **error)
3473 {
3474   BookmarkItem *item;
3475   GList *l;
3476   gchar **apps;
3477   gsize i, n_apps;
3478   
3479   g_return_val_if_fail (bookmark != NULL, NULL);
3480   g_return_val_if_fail (uri != NULL, NULL);
3481   
3482   item = g_bookmark_file_lookup_item (bookmark, uri);
3483   if (!item)
3484     {
3485       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3486                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3487                    _("No bookmark found for URI “%s”"),
3488                    uri);
3489       return NULL;
3490     }
3491   
3492   if (!item->metadata)
3493     {      
3494       if (length)
3495         *length = 0;
3496       
3497       return NULL;
3498     }
3499   
3500   n_apps = g_list_length (item->metadata->applications);
3501   apps = g_new0 (gchar *, n_apps + 1);
3502   
3503   for (l = g_list_last (item->metadata->applications), i = 0;
3504        l != NULL;
3505        l = l->prev)
3506     {
3507       BookmarkAppInfo *ai;
3508       
3509       ai = (BookmarkAppInfo *) l->data;
3510       
3511       g_warn_if_fail (ai != NULL);
3512       g_warn_if_fail (ai->name != NULL);
3513       
3514       apps[i++] = g_strdup (ai->name);
3515     }
3516   apps[i] = NULL;
3517   
3518   if (length)
3519     *length = i;
3520   
3521   return apps;
3522 }
3523
3524 /**
3525  * g_bookmark_file_get_size:
3526  * @bookmark: a #GBookmarkFile
3527  * 
3528  * Gets the number of bookmarks inside @bookmark.
3529  * 
3530  * Returns: the number of bookmarks
3531  *
3532  * Since: 2.12
3533  */
3534 gint
3535 g_bookmark_file_get_size (GBookmarkFile *bookmark)
3536 {
3537   g_return_val_if_fail (bookmark != NULL, 0);
3538
3539   return g_list_length (bookmark->items);
3540 }
3541
3542 /**
3543  * g_bookmark_file_move_item:
3544  * @bookmark: a #GBookmarkFile
3545  * @old_uri: a valid URI
3546  * @new_uri: (nullable): a valid URI, or %NULL
3547  * @error: return location for a #GError or %NULL
3548  *
3549  * Changes the URI of a bookmark item from @old_uri to @new_uri.  Any
3550  * existing bookmark for @new_uri will be overwritten.  If @new_uri is
3551  * %NULL, then the bookmark is removed.
3552  *
3553  * In the event the URI cannot be found, %FALSE is returned and
3554  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3555  *
3556  * Returns: %TRUE if the URI was successfully changed
3557  *
3558  * Since: 2.12
3559  */
3560 gboolean
3561 g_bookmark_file_move_item (GBookmarkFile  *bookmark,
3562                            const gchar    *old_uri,
3563                            const gchar    *new_uri,
3564                            GError        **error)
3565 {
3566   BookmarkItem *item;
3567   
3568   g_return_val_if_fail (bookmark != NULL, FALSE);
3569   g_return_val_if_fail (old_uri != NULL, FALSE);
3570
3571   item = g_bookmark_file_lookup_item (bookmark, old_uri);
3572   if (!item)
3573     {
3574       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3575                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3576                    _("No bookmark found for URI “%s”"),
3577                    old_uri);
3578       return FALSE;
3579     }
3580
3581   if (new_uri && new_uri[0] != '\0')
3582     {
3583       if (g_bookmark_file_has_item (bookmark, new_uri))
3584         {
3585           if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
3586             return FALSE;
3587         }
3588
3589       g_hash_table_steal (bookmark->items_by_uri, item->uri);
3590       
3591       g_free (item->uri);
3592       item->uri = g_strdup (new_uri);
3593       item->modified = time (NULL);
3594
3595       g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
3596
3597       return TRUE;
3598     }
3599   else
3600     {
3601       if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
3602         return FALSE;
3603
3604       return TRUE;
3605     }
3606 }
3607
3608 /**
3609  * g_bookmark_file_set_icon:
3610  * @bookmark: a #GBookmarkFile
3611  * @uri: a valid URI
3612  * @href: (nullable): the URI of the icon for the bookmark, or %NULL
3613  * @mime_type: the MIME type of the icon for the bookmark
3614  *
3615  * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
3616  * the currently set icon. @href can either be a full URL for the icon
3617  * file or the icon name following the Icon Naming specification.
3618  *
3619  * If no bookmark for @uri is found one is created.
3620  *
3621  * Since: 2.12
3622  */
3623 void
3624 g_bookmark_file_set_icon (GBookmarkFile *bookmark,
3625                           const gchar   *uri,
3626                           const gchar   *href,
3627                           const gchar   *mime_type)
3628 {
3629   BookmarkItem *item;
3630   
3631   g_return_if_fail (bookmark != NULL);
3632   g_return_if_fail (uri != NULL);
3633
3634   item = g_bookmark_file_lookup_item (bookmark, uri);
3635   if (!item)
3636     {
3637       item = bookmark_item_new (uri);
3638       g_bookmark_file_add_item (bookmark, item, NULL);
3639     }
3640   
3641   if (!item->metadata)
3642     item->metadata = bookmark_metadata_new ();
3643   
3644   g_free (item->metadata->icon_href);
3645   g_free (item->metadata->icon_mime);
3646   
3647   item->metadata->icon_href = g_strdup (href);
3648   
3649   if (mime_type && mime_type[0] != '\0')
3650     item->metadata->icon_mime = g_strdup (mime_type);
3651   else
3652     item->metadata->icon_mime = g_strdup ("application/octet-stream");
3653   
3654   item->modified = time (NULL);
3655 }
3656
3657 /**
3658  * g_bookmark_file_get_icon:
3659  * @bookmark: a #GBookmarkFile
3660  * @uri: a valid URI
3661  * @href: (out) (optional): return location for the icon's location or %NULL
3662  * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL
3663  * @error: return location for a #GError or %NULL
3664  *
3665  * Gets the icon of the bookmark for @uri.
3666  *
3667  * In the event the URI cannot be found, %FALSE is returned and
3668  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
3669  *
3670  * Returns: %TRUE if the icon for the bookmark for the URI was found.
3671  *   You should free the returned strings.
3672  *
3673  * Since: 2.12
3674  */
3675 gboolean
3676 g_bookmark_file_get_icon (GBookmarkFile  *bookmark,
3677                           const gchar    *uri,
3678                           gchar         **href,
3679                           gchar         **mime_type,
3680                           GError        **error)
3681 {
3682   BookmarkItem *item;
3683   
3684   g_return_val_if_fail (bookmark != NULL, FALSE);
3685   g_return_val_if_fail (uri != NULL, FALSE);
3686   
3687   item = g_bookmark_file_lookup_item (bookmark, uri);
3688   if (!item)
3689     {
3690       g_set_error (error, G_BOOKMARK_FILE_ERROR,
3691                    G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
3692                    _("No bookmark found for URI “%s”"),
3693                    uri);
3694       return FALSE;
3695     }
3696   
3697   if ((!item->metadata) || (!item->metadata->icon_href))
3698     return FALSE;
3699   
3700   if (href)
3701     *href = g_strdup (item->metadata->icon_href);
3702   
3703   if (mime_type)
3704     *mime_type = g_strdup (item->metadata->icon_mime);
3705   
3706   return TRUE;
3707 }