Imported Upstream version 2.61.2
[platform/upstream/glib.git] / glib / gbookmarkfile.c
index cf052fa..88808e7 100644 (file)
@@ -3,18 +3,17 @@
  * Copyright (C) 2005-2006 Emmanuele Bassi
  *
  * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Library General Public
+ * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * Library General Public License for more details.
+ * Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "config.h"
 #include <locale.h>
 #include <time.h>
 #include <stdarg.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#ifdef HAVE_UNISTD_H
-#include <unistd.h>
-#endif
 
 #include "gconvert.h"
 #include "gdataset.h"
 #include "ghash.h"
 #include "glibintl.h"
 #include "glist.h"
-#include "gslist.h"
 #include "gmain.h"
 #include "gmarkup.h"
 #include "gmem.h"
 #include "gmessages.h"
+#include "gshell.h"
 #include "gslice.h"
 #include "gstdio.h"
 #include "gstring.h"
 #include "gtimer.h"
 #include "gutils.h"
 
-#include "galias.h"
+
+/**
+ * SECTION:bookmarkfile
+ * @title: Bookmark file parser
+ * @short_description: parses files containing bookmarks
+ *
+ * GBookmarkFile lets you parse, edit or create files containing bookmarks
+ * to URI, along with some meta-data about the resource pointed by the URI
+ * like its MIME type, the application that is registering the bookmark and
+ * the icon that should be used to represent the bookmark. The data is stored
+ * using the
+ * [Desktop Bookmark Specification](http://www.gnome.org/~ebassi/bookmark-spec).
+ *
+ * The syntax of the bookmark files is described in detail inside the
+ * Desktop Bookmark Specification, here is a quick summary: bookmark
+ * files use a sub-class of the XML Bookmark Exchange Language
+ * specification, consisting of valid UTF-8 encoded XML, under the
+ * <xbel> root element; each bookmark is stored inside a
+ * <bookmark> element, using its URI: no relative paths can
+ * be used inside a bookmark file. The bookmark may have a user defined
+ * title and description, to be used instead of the URI. Under the
+ * <metadata> element, with its owner attribute set to
+ * `http://freedesktop.org`, is stored the meta-data about a resource
+ * pointed by its URI. The meta-data consists of the resource's MIME
+ * type; the applications that have registered a bookmark; the groups
+ * to which a bookmark belongs to; a visibility flag, used to set the
+ * bookmark as "private" to the applications and groups that has it
+ * registered; the URI and MIME type of an icon, to be used when
+ * displaying the bookmark inside a GUI.
+ *
+ * Here is an example of a bookmark file:
+ * [bookmarks.xbel](https://git.gnome.org/browse/glib/tree/glib/tests/bookmarks.xbel)
+ *
+ * A bookmark file might contain more than one bookmark; each bookmark
+ * is accessed through its URI.
+ *
+ * The important caveat of bookmark files is that when you add a new
+ * bookmark you must also add the application that is registering it, using
+ * g_bookmark_file_add_application() or g_bookmark_file_set_app_info().
+ * If a bookmark has no applications then it won't be dumped when creating
+ * the on disk representation, using g_bookmark_file_to_data() or
+ * g_bookmark_file_to_file().
+ *
+ * The #GBookmarkFile parser was added in GLib 2.12.
+ */
 
 /* XBEL 1.0 standard entities */
 #define XBEL_VERSION           "1.0"
 #define BOOKMARK_NAME_ATTRIBUTE                "name"
 #define BOOKMARK_EXEC_ATTRIBUTE                "exec"
 #define BOOKMARK_COUNT_ATTRIBUTE       "count"
-#define BOOKMARK_TIMESTAMP_ATTRIBUTE   "timestamp"
+#define BOOKMARK_TIMESTAMP_ATTRIBUTE   "timestamp"     /* deprecated by "modified" */
+#define BOOKMARK_MODIFIED_ATTRIBUTE     "modified"
 #define BOOKMARK_HREF_ATTRIBUTE        "href"
 #define BOOKMARK_TYPE_ATTRIBUTE        "type"
 
@@ -123,24 +162,24 @@ struct _BookmarkAppInfo
 {
   gchar *name;
   gchar *exec;
-  
+
   guint count;
-  
+
   time_t stamp;
 };
 
 struct _BookmarkMetadata
 {
   gchar *mime_type;
-  
+
   GList *groups;
-  
+
   GList *applications;
   GHashTable *apps_by_name;
-  
+
   gchar *icon_href;
   gchar *icon_mime;
-  
+
   guint is_private : 1;
 };
 
@@ -171,10 +210,10 @@ struct _GBookmarkFile
 };
 
 /* parser state machine */
-enum
+typedef enum
 {
   STATE_STARTED        = 0,
-  
+
   STATE_ROOT,
   STATE_BOOKMARK,
   STATE_TITLE,
@@ -187,9 +226,9 @@ enum
   STATE_GROUP,
   STATE_MIME,
   STATE_ICON,
-  
+
   STATE_FINISHED
-};
+} ParserState;
 
 static void          g_bookmark_file_init        (GBookmarkFile  *bookmark);
 static void          g_bookmark_file_clear       (GBookmarkFile  *bookmark);
@@ -205,9 +244,11 @@ static BookmarkItem *g_bookmark_file_lookup_item (GBookmarkFile  *bookmark,
 static void          g_bookmark_file_add_item    (GBookmarkFile  *bookmark,
                                                  BookmarkItem   *item,
                                                  GError        **error);
-                                                      
-static time_t  timestamp_from_iso8601 (const gchar *iso_date);
-static gchar * timestamp_to_iso8601   (time_t       timestamp);
+
+static gboolean  timestamp_from_iso8601 (const gchar  *iso_date,
+                                         time_t       *out_timestamp,
+                                         GError      **error);
+static gchar    *timestamp_to_iso8601   (time_t        timestamp);
 
 /********************************
  * BookmarkAppInfo              *
@@ -218,16 +259,16 @@ static BookmarkAppInfo *
 bookmark_app_info_new (const gchar *name)
 {
   BookmarkAppInfo *retval;
-  g_assert (name != NULL);
-  
+
+  g_warn_if_fail (name != NULL);
+
   retval = g_slice_new (BookmarkAppInfo);
-  
+
   retval->name = g_strdup (name);
   retval->exec = NULL;
   retval->count = 0;
-  retval->stamp = time (NULL);
-  
+  retval->stamp = 0;
+
   return retval;
 }
 
@@ -236,13 +277,10 @@ bookmark_app_info_free (BookmarkAppInfo *app_info)
 {
   if (!app_info)
     return;
-  
-  if (app_info->name)
-    g_free (app_info->name);
-  
-  if (app_info->exec)
-    g_free (app_info->exec);
-  
+
+  g_free (app_info->name);
+  g_free (app_info->exec);
+
   g_slice_free (BookmarkAppInfo, app_info);
 }
 
@@ -250,19 +288,30 @@ static gchar *
 bookmark_app_info_dump (BookmarkAppInfo *app_info)
 {
   gchar *retval;
+  gchar *name, *exec, *modified, *count;
 
-  g_assert (app_info != NULL);
+  g_warn_if_fail (app_info != NULL);
 
   if (app_info->count == 0)
     return NULL;
-  retval = g_strdup_printf ("          <%s:%s %s=\"%s\" %s=\"%s\" %s=\"%ld\" %s=\"%u\"/>\n",
-                            BOOKMARK_NAMESPACE_NAME,
-                            BOOKMARK_APPLICATION_ELEMENT,
-                            BOOKMARK_NAME_ATTRIBUTE, app_info->name,
-                            BOOKMARK_EXEC_ATTRIBUTE, app_info->exec,
-                            BOOKMARK_TIMESTAMP_ATTRIBUTE, (time_t) app_info->stamp,
-                            BOOKMARK_COUNT_ATTRIBUTE, app_info->count);
+
+  name = g_markup_escape_text (app_info->name, -1);
+  exec = g_markup_escape_text (app_info->exec, -1);
+  modified = timestamp_to_iso8601 (app_info->stamp);
+  count = g_strdup_printf ("%u", app_info->count);
+
+  retval = g_strconcat ("          "
+                        "<" BOOKMARK_NAMESPACE_NAME ":" BOOKMARK_APPLICATION_ELEMENT
+                        " " BOOKMARK_NAME_ATTRIBUTE "=\"", name, "\""
+                        " " BOOKMARK_EXEC_ATTRIBUTE "=\"", exec, "\""
+                        " " BOOKMARK_MODIFIED_ATTRIBUTE "=\"", modified, "\""
+                        " " BOOKMARK_COUNT_ATTRIBUTE "=\"", count, "\"/>\n",
+                        NULL);
+
+  g_free (name);
+  g_free (exec);
+  g_free (modified);
+  g_free (count);
 
   return retval;
 }
@@ -277,24 +326,24 @@ static BookmarkMetadata *
 bookmark_metadata_new (void)
 {
   BookmarkMetadata *retval;
-  
+
   retval = g_slice_new (BookmarkMetadata);
 
   retval->mime_type = NULL;
-  
+
   retval->groups = NULL;
-  
+
   retval->applications = NULL;
   retval->apps_by_name = g_hash_table_new_full (g_str_hash,
                                                 g_str_equal,
                                                 NULL,
                                                 NULL);
-  
+
   retval->is_private = FALSE;
-  
+
   retval->icon_href = NULL;
   retval->icon_mime = NULL;
-  
+
   return retval;
 }
 
@@ -303,34 +352,17 @@ bookmark_metadata_free (BookmarkMetadata *metadata)
 {
   if (!metadata)
     return;
-  
-  if (metadata->mime_type)
-    g_free (metadata->mime_type);
-    
-  if (metadata->groups)
-    {
-      g_list_foreach (metadata->groups,
-                     (GFunc) g_free,
-                     NULL);
-      g_list_free (metadata->groups);
-    }
-  
-  if (metadata->applications)
-    {
-      g_list_foreach (metadata->applications,
-                     (GFunc) bookmark_app_info_free,
-                     NULL);
-      g_list_free (metadata->applications);
-      
-      g_hash_table_destroy (metadata->apps_by_name);
-    }
-  
-  if (metadata->icon_href)
-    g_free (metadata->icon_href);
-  
-  if (metadata->icon_mime)
-    g_free (metadata->icon_mime);
-  
+
+  g_free (metadata->mime_type);
+
+  g_list_free_full (metadata->groups, g_free);
+  g_list_free_full (metadata->applications, (GDestroyNotify) bookmark_app_info_free);
+
+  g_hash_table_destroy (metadata->apps_by_name);
+
+  g_free (metadata->icon_href);
+  g_free (metadata->icon_mime);
+
   g_slice_free (BookmarkMetadata, metadata);
 }
 
@@ -338,73 +370,81 @@ static gchar *
 bookmark_metadata_dump (BookmarkMetadata *metadata)
 {
   GString *retval;
+  gchar *buffer;
+
   if (!metadata->applications)
     return NULL;
-  
-  retval = g_string_new (NULL);
-  
+
+  retval = g_string_sized_new (1024);
+
   /* metadata container */
-  g_string_append_printf (retval,
-                         "      <%s %s=\"%s\">\n",
-                         XBEL_METADATA_ELEMENT,
-                         XBEL_OWNER_ATTRIBUTE, BOOKMARK_METADATA_OWNER);
-  
+  g_string_append (retval,
+                  "      "
+                  "<" XBEL_METADATA_ELEMENT
+                  " " XBEL_OWNER_ATTRIBUTE "=\"" BOOKMARK_METADATA_OWNER
+                  "\">\n");
+
   /* mime type */
-  if (metadata->mime_type)
-    g_string_append_printf (retval,
-                           "        <%s:%s %s=\"%s\"/>\n",
-                           MIME_NAMESPACE_NAME,
-                           MIME_TYPE_ELEMENT,
-                           MIME_TYPE_ATTRIBUTE, metadata->mime_type);
-  
+  if (metadata->mime_type) {
+    buffer = g_strconcat ("        "
+                         "<" MIME_NAMESPACE_NAME ":" MIME_TYPE_ELEMENT " "
+                         MIME_TYPE_ATTRIBUTE "=\"", metadata->mime_type, "\"/>\n",
+                         NULL);
+    g_string_append (retval, buffer);
+    g_free (buffer);
+  }
+
   if (metadata->groups)
     {
       GList *l;
-      
+
       /* open groups container */
-      g_string_append_printf (retval,
-                             "        <%s:%s>\n",
-                             BOOKMARK_NAMESPACE_NAME,
-                             BOOKMARK_GROUPS_ELEMENT);
-      
+      g_string_append (retval,
+                      "        "
+                      "<" BOOKMARK_NAMESPACE_NAME
+                      ":" BOOKMARK_GROUPS_ELEMENT ">\n");
+
       for (l = g_list_last (metadata->groups); l != NULL; l = l->prev)
         {
-          gchar *group_name = (gchar *) l->data;
-          
-          g_string_append_printf (retval,
-                                 "          <%s:%s>%s</%s:%s>\n",
-                                 BOOKMARK_NAMESPACE_NAME,
-                                 BOOKMARK_GROUP_ELEMENT,
-                                 group_name,
-                                 BOOKMARK_NAMESPACE_NAME,
-                                 BOOKMARK_GROUP_ELEMENT);
+          gchar *group_name;
+
+         group_name = g_markup_escape_text ((gchar *) l->data, -1);
+         buffer = g_strconcat ("          "
+                               "<" BOOKMARK_NAMESPACE_NAME
+                               ":" BOOKMARK_GROUP_ELEMENT ">",
+                               group_name,
+                               "</" BOOKMARK_NAMESPACE_NAME
+                               ":"  BOOKMARK_GROUP_ELEMENT ">\n", NULL);
+         g_string_append (retval, buffer);
+
+         g_free (buffer);
+         g_free (group_name);
         }
-      
+
       /* close groups container */
-      g_string_append_printf (retval,
-                             "        </%s:%s>\n",
-                             BOOKMARK_NAMESPACE_NAME,
-                             BOOKMARK_GROUPS_ELEMENT);
+      g_string_append (retval,
+                      "        "
+                      "</" BOOKMARK_NAMESPACE_NAME
+                      ":" BOOKMARK_GROUPS_ELEMENT ">\n");
     }
-  
+
   if (metadata->applications)
     {
       GList *l;
-      
+
       /* open applications container */
-      g_string_append_printf (retval,
-                             "        <%s:%s>\n",
-                             BOOKMARK_NAMESPACE_NAME,
-                             BOOKMARK_APPLICATIONS_ELEMENT);
-      
+      g_string_append (retval,
+                      "        "
+                      "<" BOOKMARK_NAMESPACE_NAME
+                      ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
+
       for (l = g_list_last (metadata->applications); l != NULL; l = l->prev)
         {
           BookmarkAppInfo *app_info = (BookmarkAppInfo *) l->data;
           gchar *app_data;
 
-         g_assert (app_info != NULL);
-          
+         g_warn_if_fail (app_info != NULL);
+
           app_data = bookmark_app_info_dump (app_info);
 
          if (app_data)
@@ -414,38 +454,42 @@ bookmark_metadata_dump (BookmarkMetadata *metadata)
              g_free (app_data);
            }
         }
-      
+
       /* close applications container */
-      g_string_append_printf (retval,
-                             "        </%s:%s>\n",
-                             BOOKMARK_NAMESPACE_NAME,
-                             BOOKMARK_APPLICATIONS_ELEMENT);
+      g_string_append (retval,
+                      "        "
+                      "</" BOOKMARK_NAMESPACE_NAME
+                      ":" BOOKMARK_APPLICATIONS_ELEMENT ">\n");
     }
-  
+
   /* icon */
   if (metadata->icon_href)
     {
       if (!metadata->icon_mime)
         metadata->icon_mime = g_strdup ("application/octet-stream");
-      
-      g_string_append_printf (retval,
-                             "       <%s:%s %s=\"%s\" %s=\"%s\"/>\n",
-                             BOOKMARK_NAMESPACE_NAME,
-                             BOOKMARK_ICON_ELEMENT,
-                             BOOKMARK_HREF_ATTRIBUTE, metadata->icon_href,
-                             BOOKMARK_TYPE_ATTRIBUTE, metadata->icon_mime);
-    }
-  
+
+      buffer = g_strconcat ("       "
+                           "<" BOOKMARK_NAMESPACE_NAME
+                           ":" BOOKMARK_ICON_ELEMENT
+                           " " BOOKMARK_HREF_ATTRIBUTE "=\"", metadata->icon_href,
+                           "\" " BOOKMARK_TYPE_ATTRIBUTE "=\"", metadata->icon_mime, "\"/>\n", NULL);
+      g_string_append (retval, buffer);
+
+      g_free (buffer);
+    }
+
   /* private hint */
   if (metadata->is_private)
-    g_string_append_printf (retval,
-                           "        <%s:%s/>\n",
-                           BOOKMARK_NAMESPACE_NAME,
-                           BOOKMARK_PRIVATE_ELEMENT);
-  
+    g_string_append (retval,
+                    "        "
+                    "<" BOOKMARK_NAMESPACE_NAME
+                    ":" BOOKMARK_PRIVATE_ELEMENT "/>\n");
+
   /* close metadata container */
-  g_string_append_printf (retval, "      </%s>\n", XBEL_METADATA_ELEMENT);
-                          
+  g_string_append (retval,
+                  "      "
+                  "</" XBEL_METADATA_ELEMENT ">\n");
+
   return g_string_free (retval, FALSE);
 }
 
@@ -458,21 +502,21 @@ static BookmarkItem *
 bookmark_item_new (const gchar *uri)
 {
   BookmarkItem *item;
-  g_assert (uri != NULL);
-  
+
+  g_warn_if_fail (uri != NULL);
+
   item = g_slice_new (BookmarkItem);
   item->uri = g_strdup (uri);
-  
+
   item->title = NULL;
   item->description = NULL;
-  
+
   item->added = (time_t) -1;
   item->modified = (time_t) -1;
   item->visited = (time_t) -1;
-  
+
   item->metadata = NULL;
-  
+
   return item;
 }
 
@@ -482,18 +526,13 @@ bookmark_item_free (BookmarkItem *item)
   if (!item)
     return;
 
-  if (item->uri)
-    g_free (item->uri);
-  
-  if (item->title)
-    g_free (item->title);
-  
-  if (item->description)
-    g_free (item->description);
-  
+  g_free (item->uri);
+  g_free (item->title);
+  g_free (item->description);
+
   if (item->metadata)
     bookmark_metadata_free (item->metadata);
-  
+
   g_slice_free (BookmarkItem, item);
 }
 
@@ -503,83 +542,96 @@ bookmark_item_dump (BookmarkItem *item)
   GString *retval;
   gchar *added, *visited, *modified;
   gchar *escaped_uri;
+  gchar *buffer;
+
   /* at this point, we must have at least a registered application; if we don't
    * we don't screw up the bookmark file, and just skip this item
    */
   if (!item->metadata || !item->metadata->applications)
     {
-      g_warning ("Item for URI '%s' has no registered applications: skipping.\n", item->uri);
+      g_warning ("Item for URI '%s' has no registered applications: skipping.", item->uri);
       return NULL;
     }
-  
-  retval = g_string_new (NULL);
-  
+
+  retval = g_string_sized_new (4096);
+
   added = timestamp_to_iso8601 (item->added);
   modified = timestamp_to_iso8601 (item->modified);
   visited = timestamp_to_iso8601 (item->visited);
 
-  escaped_uri = g_markup_escape_text (item->uri, strlen (item->uri));
-  
-  g_string_append_printf (retval,
-                          "  <%s %s=\"%s\" %s=\"%s\" %s=\"%s\" %s=\"%s\">\n",
-                          XBEL_BOOKMARK_ELEMENT,
-                          XBEL_HREF_ATTRIBUTE, escaped_uri,
-                          XBEL_ADDED_ATTRIBUTE, added,
-                          XBEL_MODIFIED_ATTRIBUTE, modified,
-                          XBEL_VISITED_ATTRIBUTE, visited);
+  escaped_uri = g_markup_escape_text (item->uri, -1);
+
+  buffer = g_strconcat ("  <"
+                        XBEL_BOOKMARK_ELEMENT
+                        " "
+                        XBEL_HREF_ATTRIBUTE "=\"", escaped_uri, "\" "
+                        XBEL_ADDED_ATTRIBUTE "=\"", added, "\" "
+                        XBEL_MODIFIED_ATTRIBUTE "=\"", modified, "\" "
+                        XBEL_VISITED_ATTRIBUTE "=\"", visited, "\">\n",
+                        NULL);
+
+  g_string_append (retval, buffer);
+
   g_free (escaped_uri);
   g_free (visited);
   g_free (modified);
   g_free (added);
-  
+  g_free (buffer);
+
   if (item->title)
     {
       gchar *escaped_title;
-      
-      escaped_title = g_markup_escape_text (item->title, strlen (item->title));
-      g_string_append_printf (retval,
-                             "    <%s>%s</%s>\n",
-                             XBEL_TITLE_ELEMENT,
-                             escaped_title,
-                             XBEL_TITLE_ELEMENT);
+
+      escaped_title = g_markup_escape_text (item->title, -1);
+      buffer = g_strconcat ("    "
+                            "<" XBEL_TITLE_ELEMENT ">",
+                            escaped_title,
+                            "</" XBEL_TITLE_ELEMENT ">\n",
+                            NULL);
+      g_string_append (retval, buffer);
+
       g_free (escaped_title);
+      g_free (buffer);
     }
-  
+
   if (item->description)
     {
       gchar *escaped_desc;
-      
-      escaped_desc = g_markup_escape_text (item->description, strlen (item->description));
-      g_string_append_printf (retval,
-                             "    <%s>%s</%s>\n",
-                             XBEL_DESC_ELEMENT,
-                             item->description,
-                             XBEL_DESC_ELEMENT);
+
+      escaped_desc = g_markup_escape_text (item->description, -1);
+      buffer = g_strconcat ("    "
+                            "<" XBEL_DESC_ELEMENT ">",
+                            escaped_desc,
+                            "</" XBEL_DESC_ELEMENT ">\n",
+                            NULL);
+      g_string_append (retval, buffer);
+
       g_free (escaped_desc);
+      g_free (buffer);
     }
-  
+
   if (item->metadata)
     {
       gchar *metadata;
-      
-      /* open info container */
-      g_string_append_printf (retval, "    <%s>\n", XBEL_INFO_ELEMENT);
-      
+
       metadata = bookmark_metadata_dump (item->metadata);
       if (metadata)
         {
-          retval = g_string_append (retval, metadata);
+          buffer = g_strconcat ("    "
+                                "<" XBEL_INFO_ELEMENT ">\n",
+                                metadata,
+                                "    "
+                               "</" XBEL_INFO_ELEMENT ">\n",
+                                NULL);
+          retval = g_string_append (retval, buffer);
 
+          g_free (buffer);
          g_free (metadata);
        }
-      
-      /* close info container */
-      g_string_append_printf (retval, "    </%s>\n", XBEL_INFO_ELEMENT);
     }
-  
-  g_string_append_printf (retval, "  </%s>\n", XBEL_BOOKMARK_ELEMENT);
-  
+
+  g_string_append (retval, "  </" XBEL_BOOKMARK_ELEMENT ">\n");
+
   return g_string_free (retval, FALSE);
 }
 
@@ -587,24 +639,24 @@ static BookmarkAppInfo *
 bookmark_item_lookup_app_info (BookmarkItem *item,
                               const gchar  *app_name)
 {
-  g_assert (item != NULL && app_name != NULL);
+  g_warn_if_fail (item != NULL && app_name != NULL);
 
   if (!item->metadata)
     return NULL;
-  
+
   return g_hash_table_lookup (item->metadata->apps_by_name, app_name);
 }
 
 /*************************
  *    GBookmarkFile    *
  *************************/
+
 static void
 g_bookmark_file_init (GBookmarkFile *bookmark)
 {
   bookmark->title = NULL;
   bookmark->description = NULL;
-  
+
   bookmark->items = NULL;
   bookmark->items_by_uri = g_hash_table_new_full (g_str_hash,
                                                   g_str_equal,
@@ -618,30 +670,23 @@ g_bookmark_file_clear (GBookmarkFile *bookmark)
   g_free (bookmark->title);
   g_free (bookmark->description);
 
-  if (bookmark->items)
-    {
-      g_list_foreach (bookmark->items,
-                     (GFunc) bookmark_item_free,
-                     NULL);
-      g_list_free (bookmark->items);
-      
-      bookmark->items = NULL;
-    }
-  
+  g_list_free_full (bookmark->items, (GDestroyNotify) bookmark_item_free);
+  bookmark->items = NULL;
+
   if (bookmark->items_by_uri)
     {
       g_hash_table_destroy (bookmark->items_by_uri);
-      
+
       bookmark->items_by_uri = NULL;
     }
 }
 
 struct _ParseData
 {
-  gint state;
-  
+  ParserState state;
+
   GHashTable *namespaces;
-  
+
   GBookmarkFile *bookmark_file;
   BookmarkItem *current_item;
 };
@@ -650,16 +695,16 @@ static ParseData *
 parse_data_new (void)
 {
   ParseData *retval;
-  
+
   retval = g_new (ParseData, 1);
-  
+
   retval->state = STATE_STARTED;
   retval->namespaces = g_hash_table_new_full (g_str_hash, g_str_equal,
                                              (GDestroyNotify) g_free,
                                              (GDestroyNotify) g_free);
   retval->bookmark_file = NULL;
   retval->current_item = NULL;
-  
+
   return retval;
 }
 
@@ -667,7 +712,7 @@ static void
 parse_data_free (ParseData *parse_data)
 {
   g_hash_table_destroy (parse_data->namespaces);
-  
+
   g_free (parse_data);
 }
 
@@ -685,9 +730,9 @@ parse_bookmark_element (GMarkupParseContext  *context,
   gint i;
   BookmarkItem *item;
   GError *add_error;
-  g_assert ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
-  
+
+  g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_BOOKMARK));
+
   i = 0;
   uri = added = modified = visited = NULL;
   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
@@ -702,51 +747,55 @@ parse_bookmark_element (GMarkupParseContext  *context,
         visited = attribute_values[i];
       else
         {
+          /* bookmark is defined by the XBEL spec, so we need
+           * to error out if the element has different or
+           * missing attributes
+           */
           g_set_error (error, G_MARKUP_ERROR,
                       G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
-                      _("Unexpected attribute '%s' for element '%s'"),
+                      _("Unexpected attribute “%s” for element “%s”"),
                       attr,
                       XBEL_BOOKMARK_ELEMENT);
           return;
         }
     }
-  
+
   if (!uri)
     {
       g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
-                  _("Attribute '%s' of element '%s' not found"),
+                  _("Attribute “%s” of element “%s” not found"),
                   XBEL_HREF_ATTRIBUTE,
                   XBEL_BOOKMARK_ELEMENT);
       return;
     }
-  
-  g_assert (parse_data->current_item == NULL);
-  
+
+  g_warn_if_fail (parse_data->current_item == NULL);
+
   item = bookmark_item_new (uri);
-  
-  if (added)
-    item->added = timestamp_from_iso8601 (added);
-  
-  if (modified)
-    item->modified = timestamp_from_iso8601 (modified);
-  
-  if (visited)
-    item->visited = timestamp_from_iso8601 (visited);
+
+  if (added != NULL && !timestamp_from_iso8601 (added, &item->added, error))
+    return;
+
+  if (modified != NULL && !timestamp_from_iso8601 (modified, &item->modified, error))
+    return;
+
+  if (visited != NULL && !timestamp_from_iso8601 (visited, &item->visited, error))
+    return;
 
   add_error = NULL;
   g_bookmark_file_add_item (parse_data->bookmark_file,
-                             item,
-                             &add_error);
+                           item,
+                           &add_error);
   if (add_error)
     {
       bookmark_item_free (item);
-      
+
       g_propagate_error (error, add_error);
-      
+
       return;
-    }                                
-  
+    }
+
   parse_data->current_item = item;
 }
 
@@ -757,16 +806,16 @@ parse_application_element (GMarkupParseContext  *context,
                           const gchar         **attribute_values,
                           GError              **error)
 {
-  const gchar *name, *exec, *count, *stamp;
+  const gchar *name, *exec, *count, *stamp, *modified;
   const gchar *attr;
   gint i;
   BookmarkItem *item;
   BookmarkAppInfo *ai;
-  
-  g_assert ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
+
+  g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_APPLICATION));
 
   i = 0;
-  name = exec = count = stamp = NULL;
+  name = exec = count = stamp = modified = NULL;
   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
     {
       if (IS_ATTRIBUTE (attr, BOOKMARK_NAME_ATTRIBUTE))
@@ -777,63 +826,69 @@ parse_application_element (GMarkupParseContext  *context,
         count = attribute_values[i];
       else if (IS_ATTRIBUTE (attr, BOOKMARK_TIMESTAMP_ATTRIBUTE))
         stamp = attribute_values[i];
-      else
-        {
-          g_set_error (error, G_MARKUP_ERROR,
-                      G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
-                      _("Unexpected attribute '%s' for element '%s'"),
-                      attr,
-                      BOOKMARK_APPLICATION_ELEMENT);
-          return;
-        }        
+      else if (IS_ATTRIBUTE (attr, BOOKMARK_MODIFIED_ATTRIBUTE))
+        modified = attribute_values[i];
     }
 
+  /* the "name" and "exec" attributes are mandatory */
   if (!name)
     {
       g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
-                  _("Attribute '%s' of element '%s' not found"),
+                  _("Attribute “%s” of element “%s” not found"),
                   BOOKMARK_NAME_ATTRIBUTE,
                   BOOKMARK_APPLICATION_ELEMENT);
       return;
     }
-  
+
   if (!exec)
     {
       g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
-                  _("Attribute '%s' of element '%s' not found"),
+                  _("Attribute “%s” of element “%s” not found"),
                   BOOKMARK_EXEC_ATTRIBUTE,
                   BOOKMARK_APPLICATION_ELEMENT);
       return;
     }
 
-  g_assert (parse_data->current_item != NULL);  
+  g_warn_if_fail (parse_data->current_item != NULL);
   item = parse_data->current_item;
-    
+
   ai = bookmark_item_lookup_app_info (item, name);
   if (!ai)
     {
       ai = bookmark_app_info_new (name);
-      
+
       if (!item->metadata)
        item->metadata = bookmark_metadata_new ();
-      
+
       item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
       g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
     }
-      
+
+  g_free (ai->exec);
   ai->exec = g_strdup (exec);
-  
+
   if (count)
     ai->count = atoi (count);
   else
     ai->count = 1;
-  
-  if (stamp)
-    ai->stamp = (time_t) atol (stamp);
+
+  if (modified != NULL)
+    {
+      if (!timestamp_from_iso8601 (modified, &ai->stamp, error))
+        return;
+    }
   else
-    ai->stamp = time (NULL);
+    {
+      /* the timestamp attribute has been deprecated but we still parse
+       * it for backward compatibility
+       */
+      if (stamp)
+        ai->stamp = (time_t) atol (stamp);
+      else
+        ai->stamp = time (NULL);
+    }
 }
 
 static void
@@ -847,35 +902,27 @@ parse_mime_type_element (GMarkupParseContext  *context,
   const gchar *attr;
   gint i;
   BookmarkItem *item;
-  
-  g_assert ((parse_data != NULL) && (parse_data->state == STATE_MIME));
-  
+
+  g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_MIME));
+
   i = 0;
   type = NULL;
   for (attr = attribute_names[i]; attr != NULL; attr = attribute_names[++i])
     {
       if (IS_ATTRIBUTE (attr, MIME_TYPE_ATTRIBUTE))
         type = attribute_values[i];
-      else
-        {
-          g_set_error (error, G_MARKUP_ERROR,
-                      G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
-                      _("Unexpected attribute '%s' for element '%s'"),
-                      attr,
-                      MIME_TYPE_ELEMENT);
-          return;
-        }        
     }
 
   if (!type)
     type = "application/octet-stream";
 
-  g_assert (parse_data->current_item != NULL);  
+  g_warn_if_fail (parse_data->current_item != NULL);
   item = parse_data->current_item;
-    
+
   if (!item->metadata)
     item->metadata = bookmark_metadata_new ();
-  
+
+  g_free (item->metadata->mime_type);
   item->metadata->mime_type = g_strdup (type);
 }
 
@@ -891,9 +938,9 @@ parse_icon_element (GMarkupParseContext  *context,
   const gchar *attr;
   gint i;
   BookmarkItem *item;
-  
-  g_assert ((parse_data != NULL) && (parse_data->state == STATE_ICON));
-  
+
+  g_warn_if_fail ((parse_data != NULL) && (parse_data->state == STATE_ICON));
+
   i = 0;
   href = NULL;
   type = NULL;
@@ -903,22 +950,14 @@ parse_icon_element (GMarkupParseContext  *context,
         href = attribute_values[i];
       else if (IS_ATTRIBUTE (attr, BOOKMARK_TYPE_ATTRIBUTE))
         type = attribute_values[i];
-      else
-        {
-          g_set_error (error, G_MARKUP_ERROR,
-                      G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
-                      _("Unexpected attribute '%s' for element '%s'"),
-                      attr,
-                      BOOKMARK_ICON_ELEMENT);
-          return;
-        }        
     }
 
+  /* the "href" attribute is mandatory */
   if (!href)
     {
       g_set_error (error, G_MARKUP_ERROR,
                   G_MARKUP_ERROR_INVALID_CONTENT,
-                  _("Attribute '%s' of element '%s' not found"),
+                  _("Attribute “%s” of element “%s” not found"),
                   BOOKMARK_HREF_ATTRIBUTE,
                   BOOKMARK_ICON_ELEMENT);
       return;
@@ -927,12 +966,14 @@ parse_icon_element (GMarkupParseContext  *context,
   if (!type)
     type = "application/octet-stream";
 
-  g_assert (parse_data->current_item != NULL);  
+  g_warn_if_fail (parse_data->current_item != NULL);
   item = parse_data->current_item;
-    
+
   if (!item->metadata)
     item->metadata = bookmark_metadata_new ();
-  
+
+  g_free (item->metadata->icon_href);
+  g_free (item->metadata->icon_mime);
   item->metadata->icon_href = g_strdup (href);
   item->metadata->icon_mime = g_strdup (type);
 }
@@ -944,7 +985,7 @@ parse_icon_element (GMarkupParseContext  *context,
  *
  * FIXME: this works on the assumption that the generator of the XBEL file
  * is either this code or is smart enough to place the namespace declarations
- * inside the main root node or inside the metadata node and does not redefine 
+ * inside the main root node or inside the metadata node and does not redefine
  * a namespace inside an inner node; this does *not* conform to the
  * XML-NS standard, although is a close approximation.  In order to make this
  * conformant to the XML-NS specification we should use a per-element
@@ -957,12 +998,12 @@ map_namespace_to_name (ParseData    *parse_data,
 {
   const gchar *attr;
   gint i;
-  g_assert (parse_data != NULL);
-  
+
+  g_warn_if_fail (parse_data != NULL);
+
   if (!attribute_names || !attribute_names[0])
     return;
-  
+
   i = 0;
   for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
     {
@@ -970,16 +1011,16 @@ map_namespace_to_name (ParseData    *parse_data,
         {
           gchar *namespace_name, *namespace_uri;
           gchar *p;
-          
+
           p = g_utf8_strchr (attr, -1, ':');
           if (p)
             p = g_utf8_next_char (p);
           else
             p = "default";
-          
+
           namespace_name = g_strdup (p);
           namespace_uri = g_strdup (attribute_values[i]);
-          
+
           g_hash_table_replace (parse_data->namespaces,
                                 namespace_name,
                                 namespace_uri);
@@ -1001,26 +1042,25 @@ is_element_full (ParseData   *parse_data,
                  const gchar  sep)
 {
   gchar *ns_uri, *ns_name;
-  gchar *element_name, *resolved;
-  gchar *p, *s;
+  const gchar *p, *element_name;
   gboolean retval;
-  g_assert (parse_data != NULL);
-  g_assert (element_full != NULL);
-  
+
+  g_warn_if_fail (parse_data != NULL);
+  g_warn_if_fail (element_full != NULL);
+
   if (!element)
     return FALSE;
-    
+
   /* no namespace requested: dumb element compare */
   if (!namespace)
     return (0 == strcmp (element_full, element));
-  
+
   /* search for namespace separator; if none found, assume we are under the
    * default namespace, and set ns_name to our "default" marker; if no default
    * namespace has been set, just do a plain comparison between @full_element
    * and @element.
    */
-  p = strchr (element_full, ':');
+  p = g_utf8_strchr (element_full, -1, ':');
   if (p)
     {
       ns_name = g_strndup (element_full, p - element_full);
@@ -1031,30 +1071,64 @@ is_element_full (ParseData   *parse_data,
       ns_name = g_strdup ("default");
       element_name = element_full;
     }
-  
-  ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);  
+
+  ns_uri = g_hash_table_lookup (parse_data->namespaces, ns_name);
   if (!ns_uri)
     {
       /* no default namespace found */
       g_free (ns_name);
-      
+
       return (0 == strcmp (element_full, element));
     }
-  
-  resolved = g_strdup_printf ("%s%c%s", ns_uri, sep, element_name);
-  s = g_strdup_printf ("%s%c%s", namespace, sep, element);
-  retval = (0 == strcmp (resolved, s));
-  
+
+  retval = (0 == strcmp (ns_uri, namespace) &&
+            0 == strcmp (element_name, element));
+
   g_free (ns_name);
-  g_free (resolved);
-  g_free (s);
-  
+
   return retval;
 }
 
 #define IS_ELEMENT(p,s,e)      (is_element_full ((p), (s), NULL, (e), '\0'))
 #define IS_ELEMENT_NS(p,s,n,e) (is_element_full ((p), (s), (n), (e), '|'))
 
+static const gchar *
+parser_state_to_element_name (ParserState state)
+{
+  switch (state)
+    {
+    case STATE_STARTED:
+    case STATE_FINISHED:
+      return "(top-level)";
+    case STATE_ROOT:
+      return XBEL_ROOT_ELEMENT;
+    case STATE_BOOKMARK:
+      return XBEL_BOOKMARK_ELEMENT;
+    case STATE_TITLE:
+      return XBEL_TITLE_ELEMENT;
+    case STATE_DESC:
+      return XBEL_DESC_ELEMENT;
+    case STATE_INFO:
+      return XBEL_INFO_ELEMENT;
+    case STATE_METADATA:
+      return XBEL_METADATA_ELEMENT;
+    case STATE_APPLICATIONS:
+      return BOOKMARK_APPLICATIONS_ELEMENT;
+    case STATE_APPLICATION:
+      return BOOKMARK_APPLICATION_ELEMENT;
+    case STATE_GROUPS:
+      return BOOKMARK_GROUPS_ELEMENT;
+    case STATE_GROUP:
+      return BOOKMARK_GROUP_ELEMENT;
+    case STATE_MIME:
+      return MIME_TYPE_ELEMENT;
+    case STATE_ICON:
+      return BOOKMARK_ICON_ELEMENT;
+    default:
+      g_assert_not_reached ();
+    }
+}
+
 static void
 start_element_raw_cb (GMarkupParseContext *context,
                       const gchar         *element_name,
@@ -1066,13 +1140,13 @@ start_element_raw_cb (GMarkupParseContext *context,
   ParseData *parse_data = (ParseData *) user_data;
 
   /* we must check for namespace declarations first
-   * 
+   *
    * XXX - we could speed up things by checking for namespace declarations
    * only on the root node, where they usually are; this would probably break
    * on streams not produced by us or by "smart" generators
    */
   map_namespace_to_name (parse_data, attribute_names, attribute_values);
-  
+
   switch (parse_data->state)
     {
     case STATE_STARTED:
@@ -1080,7 +1154,7 @@ start_element_raw_cb (GMarkupParseContext *context,
         {
           const gchar *attr;
           gint i;
-          
+
           i = 0;
           for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
             {
@@ -1092,7 +1166,7 @@ start_element_raw_cb (GMarkupParseContext *context,
       else
         g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_INVALID_CONTENT,
-                    _("Unexpected tag '%s', tag '%s' expected"),
+                    _("Unexpected tag “%s”, tag “%s” expected"),
                     element_name, XBEL_ROOT_ELEMENT);
       break;
     case STATE_ROOT:
@@ -1103,9 +1177,9 @@ start_element_raw_cb (GMarkupParseContext *context,
       else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
         {
           GError *inner_error = NULL;
-          
+
           parse_data->state = STATE_BOOKMARK;
-          
+
           parse_bookmark_element (context,
                                  parse_data,
                                  attribute_names,
@@ -1117,7 +1191,7 @@ start_element_raw_cb (GMarkupParseContext *context,
       else
         g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_INVALID_CONTENT,
-                    _("Unexpected tag '%s' inside '%s'"),
+                    _("Unexpected tag “%s” inside “%s”"),
                     element_name,
                     XBEL_ROOT_ELEMENT);
       break;
@@ -1131,7 +1205,7 @@ start_element_raw_cb (GMarkupParseContext *context,
       else
         g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_INVALID_CONTENT,
-                    _("Unexpected tag '%s' inside '%s'"),
+                    _("Unexpected tag “%s” inside “%s”"),
                     element_name,
                     XBEL_BOOKMARK_ELEMENT);
       break;
@@ -1140,7 +1214,7 @@ start_element_raw_cb (GMarkupParseContext *context,
         {
           const gchar *attr;
           gint i;
-          
+
           i = 0;
           for (attr = attribute_names[i]; attr; attr = attribute_names[++i])
             {
@@ -1148,7 +1222,7 @@ start_element_raw_cb (GMarkupParseContext *context,
                   (0 == strcmp (attribute_values[i], BOOKMARK_METADATA_OWNER)))
                 {
                   parse_data->state = STATE_METADATA;
-                  
+
                   if (!parse_data->current_item->metadata)
                     parse_data->current_item->metadata = bookmark_metadata_new ();
                 }
@@ -1157,7 +1231,7 @@ start_element_raw_cb (GMarkupParseContext *context,
       else
         g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_INVALID_CONTENT,
-                    _("Unexpected tag '%s', tag '%s' expected"),
+                    _("Unexpected tag “%s”, tag “%s” expected"),
                     element_name,
                     XBEL_METADATA_ELEMENT);
       break;
@@ -1171,9 +1245,9 @@ start_element_raw_cb (GMarkupParseContext *context,
       else if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
         {
           GError *inner_error = NULL;
-          
+
          parse_data->state = STATE_ICON;
-          
+
           parse_icon_element (context,
                              parse_data,
                              attribute_names,
@@ -1185,9 +1259,9 @@ start_element_raw_cb (GMarkupParseContext *context,
       else if (IS_ELEMENT_NS (parse_data, element_name, MIME_NAMESPACE_URI, MIME_TYPE_ELEMENT))
         {
           GError *inner_error = NULL;
-          
+
           parse_data->state = STATE_MIME;
-          
+
           parse_mime_type_element (context,
                                   parse_data,
                                   attribute_names,
@@ -1199,7 +1273,7 @@ start_element_raw_cb (GMarkupParseContext *context,
       else
         g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_UNKNOWN_ELEMENT,
-                    _("Unexpected tag '%s' inside '%s'"),
+                    _("Unexpected tag “%s” inside “%s”"),
                     element_name,
                     XBEL_METADATA_ELEMENT);
       break;
@@ -1207,9 +1281,9 @@ start_element_raw_cb (GMarkupParseContext *context,
       if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_APPLICATION_ELEMENT))
         {
           GError *inner_error = NULL;
-          
+
           parse_data->state = STATE_APPLICATION;
-          
+
           parse_application_element (context,
                                     parse_data,
                                     attribute_names,
@@ -1221,7 +1295,7 @@ start_element_raw_cb (GMarkupParseContext *context,
       else
         g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_INVALID_CONTENT,
-                    _("Unexpected tag '%s', tag '%s' expected"),
+                    _("Unexpected tag “%s”, tag “%s” expected"),
                     element_name,
                     BOOKMARK_APPLICATION_ELEMENT);
       break;
@@ -1231,30 +1305,25 @@ start_element_raw_cb (GMarkupParseContext *context,
       else
         g_set_error (error, G_MARKUP_ERROR,
                     G_MARKUP_ERROR_INVALID_CONTENT,
-                    _("Unexpected tag '%s', tag '%s' expected"),
+                    _("Unexpected tag “%s”, tag “%s” expected"),
                     element_name,
                     BOOKMARK_GROUP_ELEMENT);
       break;
+
+    case STATE_TITLE:
+    case STATE_DESC:
+    case STATE_APPLICATION:
+    case STATE_GROUP:
+    case STATE_MIME:
     case STATE_ICON:
-      if (IS_ELEMENT_NS (parse_data, element_name, BOOKMARK_NAMESPACE_URI, BOOKMARK_ICON_ELEMENT))
-        {
-          GError *inner_error = NULL;
-          
-          parse_icon_element (context,
-                             parse_data,
-                             attribute_names,
-                             attribute_values,
-                             &inner_error);
-          if (inner_error)
-            g_propagate_error (error, inner_error);
-        }
-      else
-        g_set_error (error, G_MARKUP_ERROR,
-                    G_MARKUP_ERROR_UNKNOWN_ELEMENT,
-                    _("Unexpected tag '%s' inside '%s'"),
-                    element_name,
-                    XBEL_METADATA_ELEMENT);
+    case STATE_FINISHED:
+      g_set_error (error, G_MARKUP_ERROR,
+                   G_MARKUP_ERROR_INVALID_CONTENT,
+                   _("Unexpected tag “%s” inside “%s”"),
+                   element_name,
+                   parser_state_to_element_name (parse_data->state));
       break;
+
     default:
       g_assert_not_reached ();
       break;
@@ -1268,13 +1337,13 @@ end_element_raw_cb (GMarkupParseContext *context,
                     GError             **error)
 {
   ParseData *parse_data = (ParseData *) user_data;
-  
+
   if (IS_ELEMENT (parse_data, element_name, XBEL_ROOT_ELEMENT))
     parse_data->state = STATE_FINISHED;
   else if (IS_ELEMENT (parse_data, element_name, XBEL_BOOKMARK_ELEMENT))
     {
       parse_data->current_item = NULL;
-      
+
       parse_data->state = STATE_ROOT;
     }
   else if ((IS_ELEMENT (parse_data, element_name, XBEL_INFO_ELEMENT)) ||
@@ -1313,9 +1382,9 @@ text_raw_cb (GMarkupParseContext *context,
 {
   ParseData *parse_data = (ParseData *) user_data;
   gchar *payload;
-  
+
   payload = g_strndup (text, length);
-  
+
   switch (parse_data->state)
     {
     case STATE_TITLE:
@@ -1345,12 +1414,12 @@ text_raw_cb (GMarkupParseContext *context,
     case STATE_GROUP:
       {
       GList *groups;
-      
-      g_assert (parse_data->current_item != NULL);
-      
+
+      g_warn_if_fail (parse_data->current_item != NULL);
+
       if (!parse_data->current_item->metadata)
         parse_data->current_item->metadata = bookmark_metadata_new ();
-      
+
       groups = parse_data->current_item->metadata->groups;
       parse_data->current_item->metadata->groups = g_list_prepend (groups, g_strdup (payload));
       }
@@ -1366,10 +1435,10 @@ text_raw_cb (GMarkupParseContext *context,
     case STATE_ICON:
       break;
     default:
-      g_assert_not_reached ();
+      g_warn_if_reached ();
       break;
     }
-  
+
   g_free (payload);
 }
 
@@ -1384,196 +1453,185 @@ static const GMarkupParser markup_parser =
 
 static gboolean
 g_bookmark_file_parse (GBookmarkFile  *bookmark,
-                        const gchar      *buffer,
-                        gsize             length,
-                        GError          **error)
+                        const gchar  *buffer,
+                        gsize         length,
+                        GError       **error)
 {
   GMarkupParseContext *context;
   ParseData *parse_data;
   GError *parse_error, *end_error;
   gboolean retval;
-  
-  g_assert (bookmark != NULL);
+
+  g_warn_if_fail (bookmark != NULL);
 
   if (!buffer)
     return FALSE;
-  
-  if (length == -1)
+
+  parse_error = NULL;
+  end_error = NULL;
+
+  if (length == (gsize) -1)
     length = strlen (buffer);
 
   parse_data = parse_data_new ();
   parse_data->bookmark_file = bookmark;
-  
+
   context = g_markup_parse_context_new (&markup_parser,
                                        0,
                                        parse_data,
                                        (GDestroyNotify) parse_data_free);
-  
-  parse_error = NULL;
+
   retval = g_markup_parse_context_parse (context,
                                         buffer,
                                         length,
                                         &parse_error);
   if (!retval)
-    {
-      g_propagate_error (error, parse_error);
-      
-      return FALSE;
-    }
-  
-  end_error = NULL;
-  retval = g_markup_parse_context_end_parse (context, &end_error);
-  if (!retval)
-    {
-      g_propagate_error (error, end_error);
-      
-      return FALSE;
-    }
-  
+    g_propagate_error (error, parse_error);
+  else
+   {
+     retval = g_markup_parse_context_end_parse (context, &end_error);
+      if (!retval)
+        g_propagate_error (error, end_error);
+   }
+
   g_markup_parse_context_free (context);
-  
-  return TRUE;
+
+  return retval;
 }
 
 static gchar *
 g_bookmark_file_dump (GBookmarkFile  *bookmark,
-                       gsize            *length,
-                       GError          **error)
+                     gsize          *length,
+                     GError        **error)
 {
   GString *retval;
+  gchar *buffer;
   GList *l;
-  
-  retval = g_string_new (NULL);
-  
-  g_string_append_printf (retval,
-                         "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
-#if 0
-                         /* XXX - do we really need the doctype? */
-                         "<!DOCTYPE %s\n"
-                         "  PUBLIC \"%s\"\n"
-                         "         \"%s\">\n"
-#endif
-                         "<%s %s=\"%s\"\n"
-                         "      xmlns:%s=\"%s\"\n"
-                         "      xmlns:%s=\"%s\"\n>",
+
+  retval = g_string_sized_new (4096);
+
+  g_string_append (retval,
+                  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
 #if 0
-                         /* XXX - do we really need the doctype? */
-                         XBEL_DTD_NICK,
-                         XBEL_DTD_SYSTEM, XBEL_DTD_URI,
+                  /* XXX - do we really need the doctype? */
+                  "<!DOCTYPE " XBEL_DTD_NICK "\n"
+                  "  PUBLIC \"" XBEL_DTD_SYSTEM "\"\n"
+                  "         \"" XBEL_DTD_URI "\">\n"
 #endif
-                         XBEL_ROOT_ELEMENT,
-                         XBEL_VERSION_ATTRIBUTE, XBEL_VERSION,
-                         BOOKMARK_NAMESPACE_NAME, BOOKMARK_NAMESPACE_URI,
-                         MIME_NAMESPACE_NAME, MIME_NAMESPACE_URI);
-  
+                  "<" XBEL_ROOT_ELEMENT " " XBEL_VERSION_ATTRIBUTE "=\"" XBEL_VERSION "\"\n"
+                  "      xmlns:" BOOKMARK_NAMESPACE_NAME "=\"" BOOKMARK_NAMESPACE_URI "\"\n"
+                  "      xmlns:" MIME_NAMESPACE_NAME     "=\"" MIME_NAMESPACE_URI "\"\n>");
+
   if (bookmark->title)
     {
       gchar *escaped_title;
-      
+
       escaped_title = g_markup_escape_text (bookmark->title, -1);
-      
-      g_string_append_printf (retval, "  <%s>%s</%s>\n",
-                              XBEL_TITLE_ELEMENT,
-                              escaped_title,
-                              XBEL_TITLE_ELEMENT);
-      
+
+      buffer = g_strconcat ("  "
+                           "<" XBEL_TITLE_ELEMENT ">",
+                           escaped_title,
+                           "</" XBEL_TITLE_ELEMENT ">\n", NULL);
+
+      g_string_append (retval, buffer);
+
+      g_free (buffer);
       g_free (escaped_title);
     }
-  
+
   if (bookmark->description)
     {
       gchar *escaped_desc;
-      
+
       escaped_desc = g_markup_escape_text (bookmark->description, -1);
-      
-      g_string_append_printf (retval, "  <%s>%s</%s>\n",
-                              XBEL_DESC_ELEMENT,
-                              escaped_desc,
-                              XBEL_DESC_ELEMENT);
-      
+
+      buffer = g_strconcat ("  "
+                           "<" XBEL_DESC_ELEMENT ">",
+                           escaped_desc,
+                           "</" XBEL_DESC_ELEMENT ">\n", NULL);
+      g_string_append (retval, buffer);
+
+      g_free (buffer);
       g_free (escaped_desc);
     }
-  
+
   if (!bookmark->items)
     goto out;
   else
     retval = g_string_append (retval, "\n");
-  
+
+  /* the items are stored in reverse order */
   for (l = g_list_last (bookmark->items);
        l != NULL;
        l = l->prev)
     {
       BookmarkItem *item = (BookmarkItem *) l->data;
       gchar *item_dump;
-      
+
       item_dump = bookmark_item_dump (item);
       if (!item_dump)
         continue;
-      
+
       retval = g_string_append (retval, item_dump);
-      
-      g_free (item_dump);      
+
+      g_free (item_dump);
     }
 
 out:
-  g_string_append_printf (retval, "</%s>", XBEL_ROOT_ELEMENT);
-  
+  g_string_append (retval, "</" XBEL_ROOT_ELEMENT ">");
+
   if (length)
     *length = retval->len;
-  
+
   return g_string_free (retval, FALSE);
 }
 
 /**************
  *    Misc    *
  **************/
+
 /* converts a Unix timestamp in a ISO 8601 compliant string; you
  * should free the returned string.
  */
 static gchar *
 timestamp_to_iso8601 (time_t timestamp)
 {
-  GTimeVal stamp;
+  GDateTime *dt = g_date_time_new_from_unix_utc (timestamp);
+  gchar *iso8601_string = g_date_time_format_iso8601 (dt);
+  g_date_time_unref (dt);
 
-  if (timestamp == (time_t) -1)
-    g_get_current_time (&stamp);
-  else
-    {
-      stamp.tv_sec = timestamp;
-      stamp.tv_usec = 0;
-    }
-
-  return g_time_val_to_iso8601 (&stamp);
+  return g_steal_pointer (&iso8601_string);
 }
 
-static time_t
-timestamp_from_iso8601 (const gchar *iso_date)
+static gboolean
+timestamp_from_iso8601 (const gchar  *iso_date,
+                        time_t       *out_timestamp,
+                        GError      **error)
 {
-  GTimeVal stamp;
-
-  if (!g_time_val_from_iso8601 (iso_date, &stamp))
-    return (time_t) -1;
-
-  return (time_t) stamp.tv_sec;
-}
-
+  gint64 time_val;
+  GDateTime *dt = g_date_time_new_from_iso8601 (iso_date, NULL);
+  if (dt == NULL)
+    {
+      g_set_error (error, G_BOOKMARK_FILE_ERROR, G_BOOKMARK_FILE_ERROR_READ,
+                   _("Invalid date/time ‘%s’ in bookmark file"), iso_date);
+      return FALSE;
+    }
 
+  time_val = g_date_time_to_unix (dt);
+  g_date_time_unref (dt);
 
-GQuark
-g_bookmark_file_error_quark (void)
-{
-  return g_quark_from_static_string ("egg-bookmark-file-error-quark");
+  *out_timestamp = time_val;
+  return TRUE;
 }
 
-
+G_DEFINE_QUARK (g-bookmark-file-error-quark, g_bookmark_file_error)
 
 /********************
  *    Public API    *
  ********************/
 
 /**
- * g_bookmark_file_new:
+ * g_bookmark_file_new: (constructor)
  *
  * Creates a new empty #GBookmarkFile object.
  *
@@ -1581,7 +1639,7 @@ g_bookmark_file_error_quark (void)
  * or g_bookmark_file_load_from_data_dirs() to read an existing bookmark
  * file.
  *
- * Return value: an empty #GBookmarkFile
+ * Returns: an empty #GBookmarkFile
  *
  * Since: 2.12
  */
@@ -1589,11 +1647,11 @@ GBookmarkFile *
 g_bookmark_file_new (void)
 {
   GBookmarkFile *bookmark;
-  
+
   bookmark = g_new (GBookmarkFile, 1);
-  
+
   g_bookmark_file_init (bookmark);
-  
+
   return bookmark;
 }
 
@@ -1610,16 +1668,17 @@ g_bookmark_file_free (GBookmarkFile *bookmark)
 {
   if (!bookmark)
     return;
-  
+
   g_bookmark_file_clear (bookmark);
-  
-  g_free (bookmark);  
+
+  g_free (bookmark);
 }
 
 /**
  * g_bookmark_file_load_from_data:
  * @bookmark: an empty #GBookmarkFile struct
- * @data: desktop bookmarks loaded in memory
+ * @data: (array length=length) (element-type guint8): desktop bookmarks
+ *    loaded in memory
  * @length: the length of @data in bytes
  * @error: return location for a #GError, or %NULL
  *
@@ -1627,7 +1686,7 @@ g_bookmark_file_free (GBookmarkFile *bookmark)
  * structure.  If the object cannot be created then @error is set to a
  * #GBookmarkFileError.
  *
- * Return value: %TRUE if a desktop bookmark could be loaded.
+ * Returns: %TRUE if a desktop bookmark could be loaded.
  *
  * Since: 2.12
  */
@@ -1639,11 +1698,9 @@ g_bookmark_file_load_from_data (GBookmarkFile  *bookmark,
 {
   GError *parse_error;
   gboolean retval;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
-  g_return_val_if_fail (data != NULL, FALSE);
-  g_return_val_if_fail (length != 0, FALSE);
-  
+
   if (length == (gsize) -1)
     length = strlen (data);
 
@@ -1652,30 +1709,28 @@ g_bookmark_file_load_from_data (GBookmarkFile  *bookmark,
       g_bookmark_file_clear (bookmark);
       g_bookmark_file_init (bookmark);
     }
-  
+
   parse_error = NULL;
   retval = g_bookmark_file_parse (bookmark, data, length, &parse_error);
+
   if (!retval)
-    {
-      g_propagate_error (error, parse_error);
-      
-      return FALSE;
-    }
-  
-  return TRUE;
+    g_propagate_error (error, parse_error);
+
+  return retval;
 }
 
 /**
  * g_bookmark_file_load_from_file:
  * @bookmark: an empty #GBookmarkFile struct
- * @filename: the path of a filename to load, in the GLib file name encoding
+ * @filename: (type filename): the path of a filename to load, in the
+ *     GLib file name encoding
  * @error: return location for a #GError, or %NULL
  *
  * Loads a desktop bookmark file into an empty #GBookmarkFile structure.
  * If the file could not be loaded then @error is set to either a #GFileError
  * or #GBookmarkFileError.
  *
- * Return value: %TRUE if a desktop bookmark file could be loaded
+ * Returns: %TRUE if a desktop bookmark file could be loaded
  *
  * Since: 2.12
  */
@@ -1684,40 +1739,23 @@ g_bookmark_file_load_from_file (GBookmarkFile  *bookmark,
                                const gchar    *filename,
                                GError        **error)
 {
-  gchar *buffer;
+  gboolean ret = FALSE;
+  gchar *buffer = NULL;
   gsize len;
-  GError *read_error;
-  gboolean retval;
-       
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (filename != NULL, FALSE);
 
-  read_error = NULL;
-  g_file_get_contents (filename, &buffer, &len, &read_error);
-  if (read_error)
-    {
-      g_propagate_error (error, read_error);
-
-      return FALSE;
-    }
-  
-  read_error = NULL;
-  retval = g_bookmark_file_load_from_data (bookmark,
-                                          buffer,
-                                          len,
-                                          &read_error);
-  if (read_error)
-    {
-      g_propagate_error (error, read_error);
-
-      g_free (buffer);
+  if (!g_file_get_contents (filename, &buffer, &len, error))
+    goto out;
 
-      return FALSE;
-    }
+  if (!g_bookmark_file_load_from_data (bookmark, buffer, len, error))
+    goto out;
 
+  ret = TRUE;
+ out:
   g_free (buffer);
-
-  return retval;
+  return ret;
 }
 
 
@@ -1779,13 +1817,13 @@ find_file_in_data_dirs (const gchar   *file,
 
   if (!path)
     {
-      g_set_error (error, G_BOOKMARK_FILE_ERROR,
-                   G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
-                   _("No valid bookmark file was be found in data dirs"));
-      
+      g_set_error_literal (error, G_BOOKMARK_FILE_ERROR,
+                           G_BOOKMARK_FILE_ERROR_FILE_NOT_FOUND,
+                           _("No valid bookmark file found in data dirs"));
+
       return NULL;
     }
-  
+
   return path;
 }
 
@@ -1793,18 +1831,18 @@ find_file_in_data_dirs (const gchar   *file,
 /**
  * g_bookmark_file_load_from_data_dirs:
  * @bookmark: a #GBookmarkFile
- * @file: a relative path to a filename to open and parse
- * @full_path: return location for a string containing the full path
- *   of the file, or %NULL
+ * @file: (type filename): a relative path to a filename to open and parse
+ * @full_path: (out) (optional) (type filename): return location for a string
+ *    containing the full path of the file, or %NULL
  * @error: return location for a #GError, or %NULL
  *
  * This function looks for a desktop bookmark file named @file in the
- * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(), 
- * loads the file into @bookmark and returns the file's full path in 
- * @full_path.  If the file could not be loaded then an %error is
+ * paths returned from g_get_user_data_dir() and g_get_system_data_dirs(),
+ * loads the file into @bookmark and returns the file's full path in
+ * @full_path.  If the file could not be loaded then @error is
  * set to either a #GFileError or #GBookmarkFileError.
  *
- * Return value: %TRUE if a key file could be loaded, %FALSE othewise
+ * Returns: %TRUE if a key file could be loaded, %FALSE otherwise
  *
  * Since: 2.12
  */
@@ -1821,10 +1859,10 @@ g_bookmark_file_load_from_data_dirs (GBookmarkFile  *bookmark,
   gsize i, j;
   gchar *output_path;
   gboolean found_file;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (!g_path_is_absolute (file), FALSE);
-  
+
   user_data_dir = g_get_user_data_dir ();
   system_data_dirs = g_get_system_data_dirs ();
   all_data_dirs = g_new0 (gchar *, g_strv_length ((gchar **)system_data_dirs) + 2);
@@ -1844,7 +1882,7 @@ g_bookmark_file_load_from_data_dirs (GBookmarkFile  *bookmark,
       g_free (output_path);
 
       output_path = find_file_in_data_dirs (file, &data_dirs, &file_error);
-      
+
       if (file_error)
         {
           g_propagate_error (error, file_error);
@@ -1863,7 +1901,7 @@ g_bookmark_file_load_from_data_dirs (GBookmarkFile  *bookmark,
 
   if (found_file && full_path)
     *full_path = output_path;
-  else 
+  else
     g_free (output_path);
 
   g_strfreev (all_data_dirs);
@@ -1875,13 +1913,13 @@ g_bookmark_file_load_from_data_dirs (GBookmarkFile  *bookmark,
 /**
  * g_bookmark_file_to_data:
  * @bookmark: a #GBookmarkFile
- * @length: return location for the length of the returned string, or %NULL
+ * @length: (out) (optional): return location for the length of the returned string, or %NULL
  * @error: return location for a #GError, or %NULL
  *
  * This function outputs @bookmark as a string.
  *
- * Return value: a newly allocated string holding
- *   the contents of the #GBookmarkFile
+ * Returns: (array length=length) (element-type guint8):
+ *   a newly allocated string holding the contents of the #GBookmarkFile
  *
  * Since: 2.12
  */
@@ -1892,30 +1930,30 @@ g_bookmark_file_to_data (GBookmarkFile  *bookmark,
 {
   GError *write_error = NULL;
   gchar *retval;
-  
+
   g_return_val_if_fail (bookmark != NULL, NULL);
-  
+
   retval = g_bookmark_file_dump (bookmark, length, &write_error);
   if (write_error)
     {
       g_propagate_error (error, write_error);
-      
+
       return NULL;
     }
-      
+
   return retval;
 }
 
 /**
  * g_bookmark_file_to_file:
  * @bookmark: a #GBookmarkFile
- * @filename: path of the output file
+ * @filename: (type filename): path of the output file
  * @error: return location for a #GError, or %NULL
  *
  * This function outputs @bookmark into a file.  The write process is
  * guaranteed to be atomic by using g_file_set_contents() internally.
  *
- * Return value: %TRUE if the file was successfully written.
+ * Returns: %TRUE if the file was successfully written.
  *
  * Since: 2.12
  */
@@ -1931,13 +1969,13 @@ g_bookmark_file_to_file (GBookmarkFile  *bookmark,
 
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (filename != NULL, FALSE);
-  
+
   data_error = NULL;
   data = g_bookmark_file_to_data (bookmark, &len, &data_error);
   if (data_error)
     {
       g_propagate_error (error, data_error);
-      
+
       return FALSE;
     }
 
@@ -1946,14 +1984,14 @@ g_bookmark_file_to_file (GBookmarkFile  *bookmark,
   if (write_error)
     {
       g_propagate_error (error, write_error);
-      
+
       retval = FALSE;
     }
   else
     retval = TRUE;
 
   g_free (data);
-  
+
   return retval;
 }
 
@@ -1961,8 +1999,8 @@ static BookmarkItem *
 g_bookmark_file_lookup_item (GBookmarkFile *bookmark,
                             const gchar   *uri)
 {
-  g_assert (bookmark != NULL && uri != NULL);
-  
+  g_warn_if_fail (bookmark != NULL && uri != NULL);
+
   return g_hash_table_lookup (bookmark->items_by_uri, uri);
 }
 
@@ -1972,8 +2010,8 @@ g_bookmark_file_add_item (GBookmarkFile  *bookmark,
                          BookmarkItem   *item,
                          GError        **error)
 {
-  g_assert (bookmark != NULL);
-  g_assert (item != NULL);
+  g_warn_if_fail (bookmark != NULL);
+  g_warn_if_fail (item != NULL);
 
   /* this should never happen; and if it does, then we are
    * screwing up something big time.
@@ -1982,20 +2020,20 @@ g_bookmark_file_add_item (GBookmarkFile  *bookmark,
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_INVALID_URI,
-                  _("A bookmark for URI '%s' already exists"),
+                  _("A bookmark for URI “%s” already exists"),
                   item->uri);
       return;
     }
-  
+
   bookmark->items = g_list_prepend (bookmark->items, item);
-  
+
   g_hash_table_replace (bookmark->items_by_uri,
                        item->uri,
                        item);
 
   if (item->added == (time_t) -1)
     item->added = time (NULL);
-  
+
   if (item->modified == (time_t) -1)
     item->modified = time (NULL);
 }
@@ -2008,33 +2046,37 @@ g_bookmark_file_add_item (GBookmarkFile  *bookmark,
  *
  * Removes the bookmark for @uri from the bookmark file @bookmark.
  *
+ * Returns: %TRUE if the bookmark was removed successfully.
+ *
  * Since: 2.12
  */
-void
+gboolean
 g_bookmark_file_remove_item (GBookmarkFile  *bookmark,
                             const gchar    *uri,
                             GError        **error)
 {
   BookmarkItem *item;
-  
-  g_return_if_fail (bookmark != NULL);
-  g_return_if_fail (uri != NULL);
-  
+
+  g_return_val_if_fail (bookmark != NULL, FALSE);
+  g_return_val_if_fail (uri != NULL, FALSE);
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
-  
+
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
-      return;   
+      return FALSE;
     }
 
   bookmark->items = g_list_remove (bookmark->items, item);
-  g_hash_table_remove (bookmark->items_by_uri, item->uri);  
-  
+  g_hash_table_remove (bookmark->items_by_uri, item->uri);
+
   bookmark_item_free (item);
+
+  return TRUE;
 }
 
 /**
@@ -2044,7 +2086,7 @@ g_bookmark_file_remove_item (GBookmarkFile  *bookmark,
  *
  * Looks whether the desktop bookmark has an item with its URI set to @uri.
  *
- * Return value: %TRUE if @uri is inside @bookmark, %FALSE otherwise
+ * Returns: %TRUE if @uri is inside @bookmark, %FALSE otherwise
  *
  * Since: 2.12
  */
@@ -2054,20 +2096,20 @@ g_bookmark_file_has_item (GBookmarkFile *bookmark,
 {
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
-  
+
   return (NULL != g_hash_table_lookup (bookmark->items_by_uri, uri));
 }
 
 /**
  * g_bookmark_file_get_uris:
  * @bookmark: a #GBookmarkFile
- * @length: return location for the number of returned URIs, or %NULL
+ * @length: (out) (optional): return location for the number of returned URIs, or %NULL
  *
  * Returns all URIs of the bookmarks in the bookmark file @bookmark.
  * The array of returned URIs will be %NULL-terminated, so @length may
  * optionally be %NULL.
  *
- * Return value: a newly allocated %NULL-terminated array of strings.
+ * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
  *   Use g_strfreev() to free it.
  *
  * Since: 2.12
@@ -2079,32 +2121,33 @@ g_bookmark_file_get_uris (GBookmarkFile *bookmark,
   GList *l;
   gchar **uris;
   gsize i, n_items;
-  
+
   g_return_val_if_fail (bookmark != NULL, NULL);
-  
-  n_items = g_list_length (bookmark->items); 
+
+  n_items = g_list_length (bookmark->items);
   uris = g_new0 (gchar *, n_items + 1);
-  
+
+  /* the items are stored in reverse order, so we walk the list backward */
   for (l = g_list_last (bookmark->items), i = 0; l != NULL; l = l->prev)
     {
       BookmarkItem *item = (BookmarkItem *) l->data;
-      
-      g_assert (item != NULL);
-      
+
+      g_warn_if_fail (item != NULL);
+
       uris[i++] = g_strdup (item->uri);
     }
   uris[i] = NULL;
-  
+
   if (length)
     *length = i;
-  
+
   return uris;
 }
 
 /**
  * g_bookmark_file_set_title:
  * @bookmark: a #GBookmarkFile
- * @uri: a valid URI or %NULL
+ * @uri: (nullable): a valid URI or %NULL
  * @title: a UTF-8 encoded string
  *
  * Sets @title as the title of the bookmark for @uri inside the
@@ -2122,30 +2165,26 @@ g_bookmark_file_set_title (GBookmarkFile *bookmark,
                           const gchar   *title)
 {
   g_return_if_fail (bookmark != NULL);
-  
+
   if (!uri)
     {
       g_free (bookmark->title);
-      
-      if (title && title[0] != '\0')
-        bookmark->title = g_strdup (title);
+      bookmark->title = g_strdup (title);
     }
   else
     {
       BookmarkItem *item;
-      
+
       item = g_bookmark_file_lookup_item (bookmark, uri);
       if (!item)
         {
           item = bookmark_item_new (uri);
           g_bookmark_file_add_item (bookmark, item, NULL);
         }
-      
+
       g_free (item->title);
-      
-      if (title && title[0] != '\0')
-        item->title = g_strdup (title);
-      
+      item->title = g_strdup (title);
+
       item->modified = time (NULL);
     }
 }
@@ -2153,7 +2192,7 @@ g_bookmark_file_set_title (GBookmarkFile *bookmark,
 /**
  * g_bookmark_file_get_title:
  * @bookmark: a #GBookmarkFile
- * @uri: a valid URI or %NULL
+ * @uri: (nullable): a valid URI or %NULL
  * @error: return location for a #GError, or %NULL
  *
  * Returns the title of the bookmark for @uri.
@@ -2163,7 +2202,7 @@ g_bookmark_file_set_title (GBookmarkFile *bookmark,
  * In the event the URI cannot be found, %NULL is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: a newly allocated string or %NULL if the specified
+ * Returns: a newly allocated string or %NULL if the specified
  *   URI cannot be found.
  *
  * Since: 2.12
@@ -2174,29 +2213,29 @@ g_bookmark_file_get_title (GBookmarkFile  *bookmark,
                           GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, NULL);
-  
+
   if (!uri)
     return g_strdup (bookmark->title);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return NULL;
     }
-  
+
   return g_strdup (item->title);
 }
 
 /**
  * g_bookmark_file_set_description:
  * @bookmark: a #GBookmarkFile
- * @uri: a valid URI or %NULL
+ * @uri: (nullable): a valid URI or %NULL
  * @description: a string
  *
  * Sets @description as the description of the bookmark for @uri.
@@ -2217,26 +2256,22 @@ g_bookmark_file_set_description (GBookmarkFile *bookmark,
   if (!uri)
     {
       g_free (bookmark->description);
-      
-      if (description && description[0] != '\0')
-        bookmark->description = g_strdup (description);
+      bookmark->description = g_strdup (description);
     }
   else
     {
       BookmarkItem *item;
-      
+
       item = g_bookmark_file_lookup_item (bookmark, uri);
       if (!item)
         {
           item = bookmark_item_new (uri);
           g_bookmark_file_add_item (bookmark, item, NULL);
         }
-      
+
       g_free (item->description);
-      
-      if (description && description[0] != '\0')
-        item->description = g_strdup (description);
-      
+      item->description = g_strdup (description);
+
       item->modified = time (NULL);
     }
 }
@@ -2252,7 +2287,7 @@ g_bookmark_file_set_description (GBookmarkFile *bookmark,
  * In the event the URI cannot be found, %NULL is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: a newly allocated string or %NULL if the specified
+ * Returns: a newly allocated string or %NULL if the specified
  *   URI cannot be found.
  *
  * Since: 2.12
@@ -2263,22 +2298,22 @@ g_bookmark_file_get_description (GBookmarkFile  *bookmark,
                                 GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, NULL);
 
   if (!uri)
     return g_strdup (bookmark->description);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return NULL;
     }
-  
+
   return g_strdup (item->description);
 }
 
@@ -2300,24 +2335,23 @@ g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
                               const gchar   *mime_type)
 {
   BookmarkItem *item;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
   g_return_if_fail (mime_type != NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       item = bookmark_item_new (uri);
       g_bookmark_file_add_item (bookmark, item, NULL);
     }
-  
+
   if (!item->metadata)
     item->metadata = bookmark_metadata_new ();
-  
-  if (item->metadata->mime_type != NULL)
-    g_free (item->metadata->mime_type);
-  
+
+  g_free (item->metadata->mime_type);
+
   item->metadata->mime_type = g_strdup (mime_type);
   item->modified = time (NULL);
 }
@@ -2335,7 +2369,7 @@ g_bookmark_file_set_mime_type (GBookmarkFile *bookmark,
  * event that the MIME type cannot be found, %NULL is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
  *
- * Return value: a newly allocated string or %NULL if the specified
+ * Returns: a newly allocated string or %NULL if the specified
  *   URI cannot be found.
  *
  * Since: 2.12
@@ -2346,29 +2380,29 @@ g_bookmark_file_get_mime_type (GBookmarkFile  *bookmark,
                               GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, NULL);
   g_return_val_if_fail (uri != NULL, NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return NULL;
     }
-  
+
   if (!item->metadata)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
-                  _("No MIME type defined in the bookmark for URI '%s'"),
+                  _("No MIME type defined in the bookmark for URI “%s”"),
                   uri);
       return NULL;
     }
-  
+
   return g_strdup (item->metadata->mime_type);
 }
 
@@ -2390,20 +2424,20 @@ g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
                                gboolean       is_private)
 {
   BookmarkItem *item;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       item = bookmark_item_new (uri);
       g_bookmark_file_add_item (bookmark, item, NULL);
     }
-  
+
   if (!item->metadata)
     item->metadata = bookmark_metadata_new ();
-  
+
   item->metadata->is_private = (is_private == TRUE);
   item->modified = time (NULL);
 }
@@ -2421,7 +2455,7 @@ g_bookmark_file_set_is_private (GBookmarkFile *bookmark,
  * event that the private flag cannot be found, %FALSE is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
  *
- * Return value: %TRUE if the private flag is set, %FALSE otherwise.
+ * Returns: %TRUE if the private flag is set, %FALSE otherwise.
  *
  * Since: 2.12
  */
@@ -2431,29 +2465,29 @@ g_bookmark_file_get_is_private (GBookmarkFile  *bookmark,
                                GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return FALSE;
     }
-  
+
   if (!item->metadata)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
-                  _("No private flag has been defined in bookmark for URI '%s'"),
+                  _("No private flag has been defined in bookmark for URI “%s”"),
                    uri);
       return FALSE;
     }
-  
+
   return item->metadata->is_private;
 }
 
@@ -2475,10 +2509,10 @@ g_bookmark_file_set_added (GBookmarkFile *bookmark,
                           time_t         added)
 {
   BookmarkItem *item;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
@@ -2488,7 +2522,7 @@ g_bookmark_file_set_added (GBookmarkFile *bookmark,
 
   if (added == (time_t) -1)
     time (&added);
-  
+
   item->added = added;
   item->modified = added;
 }
@@ -2504,7 +2538,7 @@ g_bookmark_file_set_added (GBookmarkFile *bookmark,
  * In the event the URI cannot be found, -1 is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: a timestamp
+ * Returns: a timestamp
  *
  * Since: 2.12
  */
@@ -2514,20 +2548,20 @@ g_bookmark_file_get_added (GBookmarkFile  *bookmark,
                           GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, (time_t) -1);
   g_return_val_if_fail (uri != NULL, (time_t) -1);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return (time_t) -1;
     }
-  
+
   return item->added;
 }
 
@@ -2554,20 +2588,20 @@ g_bookmark_file_set_modified (GBookmarkFile *bookmark,
                              time_t         modified)
 {
   BookmarkItem *item;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       item = bookmark_item_new (uri);
       g_bookmark_file_add_item (bookmark, item, NULL);
     }
-  
+
   if (modified == (time_t) -1)
     time (&modified);
-  
+
   item->modified = modified;
 }
 
@@ -2582,7 +2616,7 @@ g_bookmark_file_set_modified (GBookmarkFile *bookmark,
  * In the event the URI cannot be found, -1 is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: a timestamp
+ * Returns: a timestamp
  *
  * Since: 2.12
  */
@@ -2592,20 +2626,20 @@ g_bookmark_file_get_modified (GBookmarkFile  *bookmark,
                              GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, (time_t) -1);
   g_return_val_if_fail (uri != NULL, (time_t) -1);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return (time_t) -1;
     }
-  
+
   return item->modified;
 }
 
@@ -2619,7 +2653,7 @@ g_bookmark_file_get_modified (GBookmarkFile  *bookmark,
  *
  * If no bookmark for @uri is found then it is created.
  *
- * The "visited" time should only be set if the bookmark was launched, 
+ * The "visited" time should only be set if the bookmark was launched,
  * either using the command line retrieved by g_bookmark_file_get_app_info()
  * or by the default application for the bookmark's MIME type, retrieved
  * using g_bookmark_file_get_mime_type().  Changing the "visited" time
@@ -2633,10 +2667,10 @@ g_bookmark_file_set_visited (GBookmarkFile *bookmark,
                             time_t         visited)
 {
   BookmarkItem *item;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
@@ -2646,7 +2680,7 @@ g_bookmark_file_set_visited (GBookmarkFile *bookmark,
 
   if (visited == (time_t) -1)
     time (&visited);
-  
+
   item->visited = visited;
 }
 
@@ -2661,7 +2695,7 @@ g_bookmark_file_set_visited (GBookmarkFile *bookmark,
  * In the event the URI cannot be found, -1 is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: a timestamp.
+ * Returns: a timestamp.
  *
  * Since: 2.12
  */
@@ -2671,20 +2705,20 @@ g_bookmark_file_get_visited (GBookmarkFile  *bookmark,
                             GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, (time_t) -1);
   g_return_val_if_fail (uri != NULL, (time_t) -1);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return (time_t) -1;
     }
-  
+
   return item->visited;
 }
 
@@ -2701,7 +2735,7 @@ g_bookmark_file_get_visited (GBookmarkFile  *bookmark,
  * In the event the URI cannot be found, %FALSE is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: %TRUE if @group was found.
+ * Returns: %TRUE if @group was found.
  *
  * Since: 2.12
  */
@@ -2713,29 +2747,29 @@ g_bookmark_file_has_group (GBookmarkFile  *bookmark,
 {
   BookmarkItem *item;
   GList *l;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return FALSE;
     }
-  
+
   if (!item->metadata)
     return FALSE;
-   
+
   for (l = item->metadata->groups; l != NULL; l = l->next)
     {
       if (strcmp (l->data, group) == 0)
         return TRUE;
     }
-  
+
   return FALSE;
 
 }
@@ -2759,26 +2793,26 @@ g_bookmark_file_add_group (GBookmarkFile *bookmark,
                           const gchar   *group)
 {
   BookmarkItem *item;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
   g_return_if_fail (group != NULL && group[0] != '\0');
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       item = bookmark_item_new (uri);
       g_bookmark_file_add_item (bookmark, item, NULL);
     }
-  
+
   if (!item->metadata)
     item->metadata = bookmark_metadata_new ();
-  
+
   if (!g_bookmark_file_has_group (bookmark, uri, group, NULL))
     {
       item->metadata->groups = g_list_prepend (item->metadata->groups,
                                                g_strdup (group));
-      
+
       item->modified = time (NULL);
     }
 }
@@ -2798,7 +2832,7 @@ g_bookmark_file_add_group (GBookmarkFile *bookmark,
  * In the event no group was defined, %FALSE is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_INVALID_VALUE.
  *
- * Return value: %TRUE if @group was successfully removed.
+ * Returns: %TRUE if @group was successfully removed.
  *
  * Since: 2.12
  */
@@ -2810,42 +2844,43 @@ g_bookmark_file_remove_group (GBookmarkFile  *bookmark,
 {
   BookmarkItem *item;
   GList *l;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return FALSE;
     }
-  
+
   if (!item->metadata)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                    G_BOOKMARK_FILE_ERROR_INVALID_VALUE,
-                   _("No groups set in bookmark for URI '%s'"),
+                   _("No groups set in bookmark for URI “%s”"),
                    uri);
       return FALSE;
     }
-  
+
   for (l = item->metadata->groups; l != NULL; l = l->next)
     {
       if (strcmp (l->data, group) == 0)
         {
           item->metadata->groups = g_list_remove_link (item->metadata->groups, l);
-          g_list_free_1 (l);
-          
-          item->modified = time (NULL);          
-          
+          g_free (l->data);
+         g_list_free_1 (l);
+
+          item->modified = time (NULL);
+
           return TRUE;
         }
     }
-  
+
   return FALSE;
 }
 
@@ -2853,7 +2888,8 @@ g_bookmark_file_remove_group (GBookmarkFile  *bookmark,
  * g_bookmark_file_set_groups:
  * @bookmark: a #GBookmarkFile
  * @uri: an item's URI
- * @groups: an array of group names, or %NULL to remove all groups
+ * @groups: (nullable) (array length=length) (element-type utf8): an array of
+ *    group names, or %NULL to remove all groups
  * @length: number of group name values in @groups
  *
  * Sets a list of group names for the item with URI @uri.  Each previously
@@ -2871,33 +2907,27 @@ g_bookmark_file_set_groups (GBookmarkFile  *bookmark,
 {
   BookmarkItem *item;
   gsize i;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
   g_return_if_fail (groups != NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       item = bookmark_item_new (uri);
       g_bookmark_file_add_item (bookmark, item, NULL);
     }
-  
+
   if (!item->metadata)
     item->metadata = bookmark_metadata_new ();
 
-  if (item->metadata->groups != NULL)
-    {
-      g_list_foreach (item->metadata->groups,
-                     (GFunc) g_free,
-                     NULL);
-      g_list_free (item->metadata->groups);
-      item->metadata->groups = NULL;
-    }
-  
+  g_list_free_full (item->metadata->groups, g_free);
+  item->metadata->groups = NULL;
+
   if (groups)
     {
-      for (i = 0; groups[i] != NULL && i < length; i++)
+      for (i = 0; i < length && groups[i] != NULL; i++)
         item->metadata->groups = g_list_append (item->metadata->groups,
                                                g_strdup (groups[i]));
     }
@@ -2909,7 +2939,7 @@ g_bookmark_file_set_groups (GBookmarkFile  *bookmark,
  * g_bookmark_file_get_groups:
  * @bookmark: a #GBookmarkFile
  * @uri: a valid URI
- * @length: return location for the length of the returned string, or %NULL
+ * @length: (out) (optional): return location for the length of the returned string, or %NULL
  * @error: return location for a #GError, or %NULL
  *
  * Retrieves the list of group names of the bookmark for @uri.
@@ -2920,8 +2950,10 @@ g_bookmark_file_set_groups (GBookmarkFile  *bookmark,
  * The returned array is %NULL terminated, so @length may optionally
  * be %NULL.
  *
- * Return value: a newly allocated %NULL-terminated array of group names.
+ * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of group names.
  *   Use g_strfreev() to free it.
+ *
+ * Since: 2.12
  */
 gchar **
 g_bookmark_file_get_groups (GBookmarkFile  *bookmark,
@@ -2933,28 +2965,28 @@ g_bookmark_file_get_groups (GBookmarkFile  *bookmark,
   GList *l;
   gsize len, i;
   gchar **retval;
-  
+
   g_return_val_if_fail (bookmark != NULL, NULL);
   g_return_val_if_fail (uri != NULL, NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return NULL;
     }
-  
+
   if (!item->metadata)
     {
       if (length)
        *length = 0;
-      
+
       return NULL;
     }
-  
+
   len = g_list_length (item->metadata->groups);
   retval = g_new0 (gchar *, len + 1);
   for (l = g_list_last (item->metadata->groups), i = 0;
@@ -2962,16 +2994,16 @@ g_bookmark_file_get_groups (GBookmarkFile  *bookmark,
        l = l->prev)
     {
       gchar *group_name = (gchar *) l->data;
-      
-      g_assert (group_name != NULL);
-      
+
+      g_warn_if_fail (group_name != NULL);
+
       retval[i++] = g_strdup (group_name);
     }
   retval[i] = NULL;
-  
+
   if (length)
     *length = len;
-  
+
   return retval;
 }
 
@@ -2979,9 +3011,9 @@ g_bookmark_file_get_groups (GBookmarkFile  *bookmark,
  * g_bookmark_file_add_application:
  * @bookmark: a #GBookmarkFile
  * @uri: a valid URI
- * @name: the name of the application registering the bookmark
+ * @name: (nullable): the name of the application registering the bookmark
  *   or %NULL
- * @exec: command line to be used to launch the bookmark or %NULL
+ * @exec: (nullable): command line to be used to launch the bookmark or %NULL
  *
  * Adds the application with @name and @exec to the list of
  * applications that have registered a bookmark for @uri into
@@ -2994,9 +3026,9 @@ g_bookmark_file_get_groups (GBookmarkFile  *bookmark,
  * time the application registered this bookmark.
  *
  * If @name is %NULL, the name of the application will be the
- * same returned by g_get_application(); if @exec is %NULL, the
+ * same returned by g_get_application_name(); if @exec is %NULL, the
  * command line will be a composition of the program name as
- * returned by g_get_prgname() and the "%u" modifier, which will be
+ * returned by g_get_prgname() and the "\%u" modifier, which will be
  * expanded to the bookmark's URI.
  *
  * This function will automatically take care of updating the
@@ -3016,22 +3048,22 @@ g_bookmark_file_add_application (GBookmarkFile *bookmark,
 {
   BookmarkItem *item;
   gchar *app_name, *app_exec;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       item = bookmark_item_new (uri);
       g_bookmark_file_add_item (bookmark, item, NULL);
     }
-  
+
   if (name && name[0] != '\0')
     app_name = g_strdup (name);
   else
     app_name = g_strdup (g_get_application_name ());
-  
+
   if (exec && exec[0] != '\0')
     app_exec = g_strdup (exec);
   else
@@ -3043,7 +3075,7 @@ g_bookmark_file_add_application (GBookmarkFile *bookmark,
                                 -1,
                                 (time_t) -1,
                                 NULL);
-  
+
   g_free (app_exec);
   g_free (app_name);
 }
@@ -3064,7 +3096,7 @@ g_bookmark_file_add_application (GBookmarkFile *bookmark,
  * a bookmark for @uri,  %FALSE is returned and error is set to
  * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
  *
- * Return value: %TRUE if the application was successfully removed.
+ * Returns: %TRUE if the application was successfully removed.
  *
  * Since: 2.12
  */
@@ -3076,25 +3108,25 @@ g_bookmark_file_remove_application (GBookmarkFile  *bookmark,
 {
   GError *set_error;
   gboolean retval;
-    
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
   g_return_val_if_fail (name != NULL, FALSE);
-  
+
   set_error = NULL;
   retval = g_bookmark_file_set_app_info (bookmark, uri,
                                         name,
-                                        NULL,
+                                        "",
                                         0,
                                         (time_t) -1,
                                         &set_error);
   if (set_error)
     {
       g_propagate_error (error, set_error);
-      
+
       return FALSE;
     }
-  
+
   return retval;
 }
 
@@ -3111,7 +3143,7 @@ g_bookmark_file_remove_application (GBookmarkFile  *bookmark,
  * In the event the URI cannot be found, %FALSE is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: %TRUE if the application @name was found
+ * Returns: %TRUE if the application @name was found
  *
  * Since: 2.12
  */
@@ -3122,21 +3154,21 @@ g_bookmark_file_has_application (GBookmarkFile  *bookmark,
                                 GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
   g_return_val_if_fail (name != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return FALSE;
     }
-  
+
   return (NULL != bookmark_item_lookup_app_info (item, name));
 }
 
@@ -3159,9 +3191,9 @@ g_bookmark_file_has_application (GBookmarkFile  *bookmark,
  *
  * @name can be any UTF-8 encoded string used to identify an
  * application.
- * @exec can have one of these two modifiers: "%f", which will
+ * @exec can have one of these two modifiers: "\%f", which will
  * be expanded as the local file name retrieved from the bookmark's
- * URI; "%u", which will be expanded as the bookmark's URI.
+ * URI; "\%u", which will be expanded as the bookmark's URI.
  * The expansion is done automatically when retrieving the stored
  * command line using the g_bookmark_file_get_app_info() function.
  * @count is the number of times the application has registered the
@@ -3179,7 +3211,7 @@ g_bookmark_file_has_application (GBookmarkFile  *bookmark,
  * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.  Otherwise, if no bookmark
  * for @uri is found, one is created.
  *
- * Return value: %TRUE if the application's meta-data was successfully
+ * Returns: %TRUE if the application's meta-data was successfully
  *   changed.
  *
  * Since: 2.12
@@ -3195,12 +3227,12 @@ g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
 {
   BookmarkItem *item;
   BookmarkAppInfo *ai;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
   g_return_val_if_fail (name != NULL, FALSE);
   g_return_val_if_fail (exec != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
@@ -3208,7 +3240,7 @@ g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
         {
           g_set_error (error, G_BOOKMARK_FILE_ERROR,
                       G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                      _("No bookmark found for URI '%s'"),
+                      _("No bookmark found for URI “%s”"),
                       uri);
          return FALSE;
        }
@@ -3218,7 +3250,10 @@ g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
          g_bookmark_file_add_item (bookmark, item, NULL);
        }
     }
-  
+
+  if (!item->metadata)
+    item->metadata = bookmark_metadata_new ();
+
   ai = bookmark_item_lookup_app_info (item, name);
   if (!ai)
     {
@@ -3226,7 +3261,7 @@ g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
         {
           g_set_error (error, G_BOOKMARK_FILE_ERROR,
                       G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
-                      _("No application with name '%s' registered a bookmark for '%s'"),
+                      _("No application with name “%s” registered a bookmark for “%s”"),
                       name,
                       uri);
           return FALSE;
@@ -3234,7 +3269,7 @@ g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
       else
         {
           ai = bookmark_app_info_new (name);
-          
+
           item->metadata->applications = g_list_prepend (item->metadata->applications, ai);
           g_hash_table_replace (item->metadata->apps_by_name, ai->name, ai);
         }
@@ -3244,29 +3279,30 @@ g_bookmark_file_set_app_info (GBookmarkFile  *bookmark,
     {
       item->metadata->applications = g_list_remove (item->metadata->applications, ai);
       g_hash_table_remove (item->metadata->apps_by_name, ai->name);
-          
+      bookmark_app_info_free (ai);
+
       item->modified = time (NULL);
-          
+
       return TRUE;
     }
   else if (count > 0)
     ai->count = count;
   else
     ai->count += 1;
-      
+
   if (stamp != (time_t) -1)
     ai->stamp = stamp;
   else
     ai->stamp = time (NULL);
-  
+
   if (exec && exec[0] != '\0')
     {
       g_free (ai->exec);
-      ai->exec = g_strdup (exec);
+      ai->exec = g_shell_quote (exec);
     }
-  
+
   item->modified = time (NULL);
-  
+
   return TRUE;
 }
 
@@ -3277,8 +3313,8 @@ expand_exec_line (const gchar *exec_fmt,
 {
   GString *exec;
   gchar ch;
-  
-  exec = g_string_new (NULL);
+
+  exec = g_string_sized_new (512);
   while ((ch = *exec_fmt++) != '\0')
    {
      if (ch != '%')
@@ -3286,18 +3322,30 @@ expand_exec_line (const gchar *exec_fmt,
          exec = g_string_append_c (exec, ch);
          continue;
        }
-     
+
      ch = *exec_fmt++;
      switch (ch)
        {
+       case '\0':
+        goto out;
+       case 'U':
        case 'u':
-         g_string_append_printf (exec, "%s", uri);
+         g_string_append (exec, uri);
          break;
+       case 'F':
        case 'f':
          {
-         gchar *file = g_filename_from_uri (uri, NULL, NULL);
-         g_string_append_printf (exec, "%s", file);
-         g_free (file);
+          gchar *file = g_filename_from_uri (uri, NULL, NULL);
+           if (file)
+             {
+              g_string_append (exec, file);
+              g_free (file);
+             }
+           else
+             {
+               g_string_free (exec, TRUE);
+               return NULL;
+             }
          }
          break;
        case '%':
@@ -3306,7 +3354,8 @@ expand_exec_line (const gchar *exec_fmt,
          break;
        }
    }
-   
+
+ out:
   return g_string_free (exec, FALSE);
 }
 
@@ -3315,13 +3364,13 @@ expand_exec_line (const gchar *exec_fmt,
  * @bookmark: a #GBookmarkFile
  * @uri: a valid URI
  * @name: an application's name
- * @exec: location for the command line of the application, or %NULL
- * @count: return location for the registration count, or %NULL
- * @stamp: return location for the last registration time, or %NULL
+ * @exec: (out) (optional): return location for the command line of the application, or %NULL
+ * @count: (out) (optional): return location for the registration count, or %NULL
+ * @stamp: (out) (optional): return location for the last registration time, or %NULL
  * @error: return location for a #GError, or %NULL
  *
- * Gets the registration informations of @app_name for the bookmark for
- * @uri.  See g_bookmark_file_set_app_info() for more informations about
+ * Gets the registration information of @app_name for the bookmark for
+ * @uri.  See g_bookmark_file_set_app_info() for more information about
  * the returned data.
  *
  * The string returned in @app_exec must be freed.
@@ -3330,9 +3379,11 @@ expand_exec_line (const gchar *exec_fmt,
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.  In the
  * event that no application with name @app_name has registered a bookmark
  * for @uri,  %FALSE is returned and error is set to
- * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED.
+ * #G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED. In the event that unquoting
+ * the command line fails, an error of the #G_SHELL_ERROR domain is
+ * set and %FALSE is returned.
  *
- * Return value: %TRUE on success.
+ * Returns: %TRUE on success.
  *
  * Since: 2.12
  */
@@ -3347,41 +3398,65 @@ g_bookmark_file_get_app_info (GBookmarkFile  *bookmark,
 {
   BookmarkItem *item;
   BookmarkAppInfo *ai;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
   g_return_val_if_fail (name != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return FALSE;
     }
-  
+
   ai = bookmark_item_lookup_app_info (item, name);
   if (!ai)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_APP_NOT_REGISTERED,
-                  _("No application with name '%s' registered a bookmark for '%s'"),
+                  _("No application with name “%s” registered a bookmark for “%s”"),
                   name,
                   uri);
       return FALSE;
     }
-  
+
   if (exec)
-    *exec = expand_exec_line (ai->exec, uri);
-  
+    {
+      GError *unquote_error = NULL;
+      gchar *command_line;
+
+      command_line = g_shell_unquote (ai->exec, &unquote_error);
+      if (unquote_error)
+        {
+          g_propagate_error (error, unquote_error);
+          return FALSE;
+        }
+
+      *exec = expand_exec_line (command_line, uri);
+      if (!*exec)
+        {
+          g_set_error (error, G_BOOKMARK_FILE_ERROR,
+                      G_BOOKMARK_FILE_ERROR_INVALID_URI,
+                      _("Failed to expand exec line “%s” with URI “%s”"),
+                    ai->exec, uri);
+          g_free (command_line);
+
+          return FALSE;
+        }
+      else
+        g_free (command_line);
+    }
+
   if (count)
     *count = ai->count;
-  
+
   if (stamp)
     *stamp = ai->stamp;
-  
+
   return TRUE;
 }
 
@@ -3389,16 +3464,16 @@ g_bookmark_file_get_app_info (GBookmarkFile  *bookmark,
  * g_bookmark_file_get_applications:
  * @bookmark: a #GBookmarkFile
  * @uri: a valid URI
- * @length: return location of the length of the returned list, or %NULL
+ * @length: (out) (optional): return location of the length of the returned list, or %NULL
  * @error: return location for a #GError, or %NULL
  *
  * Retrieves the names of the applications that have registered the
  * bookmark for @uri.
- * 
+ *
  * In the event the URI cannot be found, %NULL is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: a newly allocated %NULL-terminated array of strings.
+ * Returns: (array length=length) (transfer full): a newly allocated %NULL-terminated array of strings.
  *   Use g_strfreev() to free it.
  *
  * Since: 2.12
@@ -3413,59 +3488,59 @@ g_bookmark_file_get_applications (GBookmarkFile  *bookmark,
   GList *l;
   gchar **apps;
   gsize i, n_apps;
-  
+
   g_return_val_if_fail (bookmark != NULL, NULL);
   g_return_val_if_fail (uri != NULL, NULL);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return NULL;
     }
-  
+
   if (!item->metadata)
-    {     
+    {
       if (length)
        *length = 0;
-      
+
       return NULL;
     }
-  
+
   n_apps = g_list_length (item->metadata->applications);
   apps = g_new0 (gchar *, n_apps + 1);
-  
+
   for (l = g_list_last (item->metadata->applications), i = 0;
        l != NULL;
        l = l->prev)
     {
       BookmarkAppInfo *ai;
-      
+
       ai = (BookmarkAppInfo *) l->data;
-      
-      g_assert (ai != NULL);
-      g_assert (ai->name != NULL);
-      
+
+      g_warn_if_fail (ai != NULL);
+      g_warn_if_fail (ai->name != NULL);
+
       apps[i++] = g_strdup (ai->name);
     }
   apps[i] = NULL;
-  
+
   if (length)
     *length = i;
-  
+
   return apps;
 }
 
 /**
  * g_bookmark_file_get_size:
  * @bookmark: a #GBookmarkFile
- * 
+ *
  * Gets the number of bookmarks inside @bookmark.
- * 
- * Return value: the number of bookmarks
+ *
+ * Returns: the number of bookmarks
  *
  * Since: 2.12
  */
@@ -3481,7 +3556,7 @@ g_bookmark_file_get_size (GBookmarkFile *bookmark)
  * g_bookmark_file_move_item:
  * @bookmark: a #GBookmarkFile
  * @old_uri: a valid URI
- * @new_uri: a valid URI, or %NULL
+ * @new_uri: (nullable): a valid URI, or %NULL
  * @error: return location for a #GError or %NULL
  *
  * Changes the URI of a bookmark item from @old_uri to @new_uri.  Any
@@ -3491,7 +3566,7 @@ g_bookmark_file_get_size (GBookmarkFile *bookmark)
  * In the event the URI cannot be found, %FALSE is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: %TRUE if the URI was successfully changed
+ * Returns: %TRUE if the URI was successfully changed
  *
  * Since: 2.12
  */
@@ -3502,67 +3577,62 @@ g_bookmark_file_move_item (GBookmarkFile  *bookmark,
                           GError        **error)
 {
   BookmarkItem *item;
-  GError *remove_error;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (old_uri != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, old_uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   old_uri);
       return FALSE;
     }
 
   if (new_uri && new_uri[0] != '\0')
     {
+      if (g_strcmp0 (old_uri, new_uri) == 0)
+        return TRUE;
+
       if (g_bookmark_file_has_item (bookmark, new_uri))
         {
-          remove_error = NULL;
-          g_bookmark_file_remove_item (bookmark, new_uri, &remove_error);
-          if (remove_error)
-            {
-              g_propagate_error (error, remove_error);
-              
-              return FALSE;
-            }
+          if (!g_bookmark_file_remove_item (bookmark, new_uri, error))
+            return FALSE;
         }
-      
+
+      g_hash_table_steal (bookmark->items_by_uri, item->uri);
+
       g_free (item->uri);
       item->uri = g_strdup (new_uri);
       item->modified = time (NULL);
-      
+
+      g_hash_table_replace (bookmark->items_by_uri, item->uri, item);
+
       return TRUE;
     }
   else
     {
-      remove_error = NULL;
-      g_bookmark_file_remove_item (bookmark, old_uri, &remove_error);
-      if (remove_error)
-        {
-          g_propagate_error (error, remove_error);
-          
-          return FALSE;
-        }
+      if (!g_bookmark_file_remove_item (bookmark, old_uri, error))
+        return FALSE;
+
+      return TRUE;
     }
-  
-  return FALSE;
 }
 
 /**
  * g_bookmark_file_set_icon:
  * @bookmark: a #GBookmarkFile
  * @uri: a valid URI
- * @href: the URI of the icon for the bookmark, or %NULL
+ * @href: (nullable): the URI of the icon for the bookmark, or %NULL
  * @mime_type: the MIME type of the icon for the bookmark
  *
- * Sets the icon for the bookmark for @uri.  If @href is %NULL, unsets
- * the currently set icon.
+ * Sets the icon for the bookmark for @uri. If @href is %NULL, unsets
+ * the currently set icon. @href can either be a full URL for the icon
+ * file or the icon name following the Icon Naming specification.
  *
- * If no bookmark for @uri is found it is created.
+ * If no bookmark for @uri is found one is created.
  *
  * Since: 2.12
  */
@@ -3573,7 +3643,7 @@ g_bookmark_file_set_icon (GBookmarkFile *bookmark,
                          const gchar   *mime_type)
 {
   BookmarkItem *item;
-  
+
   g_return_if_fail (bookmark != NULL);
   g_return_if_fail (uri != NULL);
 
@@ -3583,21 +3653,20 @@ g_bookmark_file_set_icon (GBookmarkFile *bookmark,
       item = bookmark_item_new (uri);
       g_bookmark_file_add_item (bookmark, item, NULL);
     }
-  
+
   if (!item->metadata)
     item->metadata = bookmark_metadata_new ();
-  
+
   g_free (item->metadata->icon_href);
   g_free (item->metadata->icon_mime);
-  
-  if (href && href[0] != '\0')
-    item->metadata->icon_href = g_strdup (href);
-  
+
+  item->metadata->icon_href = g_strdup (href);
+
   if (mime_type && mime_type[0] != '\0')
     item->metadata->icon_mime = g_strdup (mime_type);
   else
     item->metadata->icon_mime = g_strdup ("application/octet-stream");
-  
+
   item->modified = time (NULL);
 }
 
@@ -3605,8 +3674,8 @@ g_bookmark_file_set_icon (GBookmarkFile *bookmark,
  * g_bookmark_file_get_icon:
  * @bookmark: a #GBookmarkFile
  * @uri: a valid URI
- * @href: return location for the icon's location or %NULL
- * @mime_type: return location for the icon's MIME type or %NULL
+ * @href: (out) (optional): return location for the icon's location or %NULL
+ * @mime_type: (out) (optional): return location for the icon's MIME type or %NULL
  * @error: return location for a #GError or %NULL
  *
  * Gets the icon of the bookmark for @uri.
@@ -3614,7 +3683,7 @@ g_bookmark_file_set_icon (GBookmarkFile *bookmark,
  * In the event the URI cannot be found, %FALSE is returned and
  * @error is set to #G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND.
  *
- * Return value: %TRUE if the icon for the bookmark for the URI was found.
+ * Returns: %TRUE if the icon for the bookmark for the URI was found.
  *   You should free the returned strings.
  *
  * Since: 2.12
@@ -3627,31 +3696,28 @@ g_bookmark_file_get_icon (GBookmarkFile  *bookmark,
                          GError        **error)
 {
   BookmarkItem *item;
-  
+
   g_return_val_if_fail (bookmark != NULL, FALSE);
   g_return_val_if_fail (uri != NULL, FALSE);
-  
+
   item = g_bookmark_file_lookup_item (bookmark, uri);
   if (!item)
     {
       g_set_error (error, G_BOOKMARK_FILE_ERROR,
                   G_BOOKMARK_FILE_ERROR_URI_NOT_FOUND,
-                  _("No bookmark found for URI '%s'"),
+                  _("No bookmark found for URI “%s”"),
                   uri);
       return FALSE;
     }
-  
+
   if ((!item->metadata) || (!item->metadata->icon_href))
     return FALSE;
-  
+
   if (href)
     *href = g_strdup (item->metadata->icon_href);
-  
+
   if (mime_type)
     *mime_type = g_strdup (item->metadata->icon_mime);
-  
+
   return TRUE;
 }
-
-#define __G_BOOKMARK_FILE_C__
-#include "galiasdef.c"