[devhelp] Book: store index_file GFile, not path



commit 6b570216ce789a65ff2105d1511359b41560f5eb
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Dec 8 14:14:12 2017 +0100

    Book: store index_file GFile, not path
    
    dh_book_get_path() is not used by gnome-builder nor Anjuta, so it's fine
    to break the API.

 docs/reference/api-breaks.xml       |    6 +++++
 docs/reference/devhelp-sections.txt |    2 +-
 src/dh-book-manager.c               |    6 +++-
 src/dh-book.c                       |   36 ++++++++++++++++++++--------------
 src/dh-book.h                       |    4 +-
 5 files changed, 34 insertions(+), 20 deletions(-)
---
diff --git a/docs/reference/api-breaks.xml b/docs/reference/api-breaks.xml
index a479fcd..b268368 100644
--- a/docs/reference/api-breaks.xml
+++ b/docs/reference/api-breaks.xml
@@ -150,6 +150,12 @@
           The <code>dh_link_get_file_name()</code> function has been removed.
         </para>
       </listitem>
+      <listitem>
+        <para>
+          The <code>dh_book_get_path()</code> function has been replaced by
+          <link linkend="dh-book-get-index-file">dh_book_get_index_file()</link>.
+        </para>
+      </listitem>
     </itemizedlist>
   </refsect1>
 </part>
diff --git a/docs/reference/devhelp-sections.txt b/docs/reference/devhelp-sections.txt
index 1a7190e..d17b828 100644
--- a/docs/reference/devhelp-sections.txt
+++ b/docs/reference/devhelp-sections.txt
@@ -34,7 +34,7 @@ dh_book_get_tree
 dh_book_get_name
 dh_book_get_title
 dh_book_get_language
-dh_book_get_path
+dh_book_get_index_file
 dh_book_get_enabled
 dh_book_set_enabled
 dh_book_cmp_by_path
diff --git a/src/dh-book-manager.c b/src/dh-book-manager.c
index 70eeef4..1d4c939 100644
--- a/src/dh-book-manager.c
+++ b/src/dh-book-manager.c
@@ -456,11 +456,13 @@ book_manager_book_updated_cb (DhBook   *book,
                               gpointer  user_data)
 {
         DhBookManager *book_manager = user_data;
-        gchar         *book_path;
+        GFile *index_file;
+        gchar *book_path;
 
         /* When we update a book, we need to delete it and then
          * create it again. */
-        book_path = g_strdup (dh_book_get_path (book));
+        index_file = dh_book_get_index_file (book);
+        book_path = g_file_get_path (index_file);
         book_manager_book_deleted_cb (book, book_manager);
         book_manager_add_from_filepath (book_manager, book_path);
         g_free (book_path);
diff --git a/src/dh-book.c b/src/dh-book.c
index cf9bb72..6d80cc5 100644
--- a/src/dh-book.c
+++ b/src/dh-book.c
@@ -57,7 +57,7 @@ typedef enum {
 } DhBookMonitorEvent;
 
 typedef struct {
-        gchar *index_file_path;
+        GFile *index_file;
         gchar *name;
         gchar *title;
         gchar *language;
@@ -110,13 +110,13 @@ dh_book_finalize (GObject *object)
 
         priv = dh_book_get_instance_private (DH_BOOK (object));
 
+        g_clear_object (&priv->index_file);
         _dh_util_free_book_tree (priv->tree);
         g_list_free_full (priv->keywords, (GDestroyNotify)dh_link_unref);
         g_list_free_full (priv->completions, g_free);
         g_free (priv->language);
         g_free (priv->title);
         g_free (priv->name);
-        g_free (priv->index_file_path);
 
         G_OBJECT_CLASS (dh_book_parent_class)->finalize (object);
 }
@@ -279,7 +279,6 @@ dh_book_new (const gchar *index_file_path)
 {
         DhBookPrivate *priv;
         DhBook *book;
-        GFile *index_file;
         gchar *language = NULL;
         GError *error = NULL;
 
@@ -288,10 +287,10 @@ dh_book_new (const gchar *index_file_path)
         book = g_object_new (DH_TYPE_BOOK, NULL);
         priv = dh_book_get_instance_private (book);
 
-        index_file = g_file_new_for_path (index_file_path);
+        priv->index_file = g_file_new_for_path (index_file_path);
 
         /* Parse file storing contents in the book struct. */
-        if (!dh_parser_read_file (index_file,
+        if (!dh_parser_read_file (priv->index_file,
                                   &priv->title,
                                   &priv->name,
                                   &language,
@@ -312,8 +311,6 @@ dh_book_new (const gchar *index_file_path)
                 return NULL;
         }
 
-        priv->index_file_path = g_strdup (index_file_path);
-
         /* Rewrite language, if any, including the prefix we want to use when
          * seeing it. It is pretty ugly to do it here, but it's the only way of
          * making sure we standarize how the language group is shown.
@@ -325,7 +322,7 @@ dh_book_new (const gchar *index_file_path)
         g_free (language);
 
         /* Setup monitor for changes */
-        priv->monitor = g_file_monitor_file (index_file,
+        priv->monitor = g_file_monitor_file (priv->index_file,
                                              G_FILE_MONITOR_NONE,
                                              NULL,
                                              NULL);
@@ -339,7 +336,6 @@ dh_book_new (const gchar *index_file_path)
                            priv->title);
         }
 
-        g_object_unref (index_file);
         return book;
 }
 
@@ -487,13 +483,13 @@ dh_book_get_language (DhBook *book)
 }
 
 /**
- * dh_book_get_path:
+ * dh_book_get_index_file:
  * @book: a #DhBook.
  *
- * Returns: the path to the index file.
+ * Returns: (transfer none): the index file.
  */
-const gchar *
-dh_book_get_path (DhBook *book)
+GFile *
+dh_book_get_index_file (DhBook *book)
 {
         DhBookPrivate *priv;
 
@@ -501,7 +497,7 @@ dh_book_get_path (DhBook *book)
 
         priv = dh_book_get_instance_private (book);
 
-        return priv->index_file_path;
+        return priv->index_file;
 }
 
 /**
@@ -562,6 +558,9 @@ dh_book_cmp_by_path (DhBook *a,
 {
         DhBookPrivate *priv_a;
         DhBookPrivate *priv_b;
+        gchar *path_a;
+        gchar *path_b;
+        gint diff;
 
         if (a == NULL || b == NULL)
                 return -1;
@@ -569,7 +568,14 @@ dh_book_cmp_by_path (DhBook *a,
         priv_a = dh_book_get_instance_private (a);
         priv_b = dh_book_get_instance_private (b);
 
-        return g_strcmp0 (priv_a->index_file_path, priv_b->index_file_path);
+        path_a = g_file_get_path (priv_a->index_file);
+        path_b = g_file_get_path (priv_b->index_file);
+
+        diff = g_strcmp0 (path_a, path_b);
+
+        g_free (path_a);
+        g_free (path_b);
+        return diff;
 }
 
 /**
diff --git a/src/dh-book.h b/src/dh-book.h
index 223c1f2..b97f51e 100644
--- a/src/dh-book.h
+++ b/src/dh-book.h
@@ -22,7 +22,7 @@
 #ifndef DH_BOOK_H
 #define DH_BOOK_H
 
-#include <glib-object.h>
+#include <gio/gio.h>
 
 G_BEGIN_DECLS
 
@@ -60,7 +60,7 @@ const gchar *dh_book_get_title       (DhBook *book);
 
 const gchar *dh_book_get_language    (DhBook *book);
 
-const gchar *dh_book_get_path        (DhBook *book);
+GFile *      dh_book_get_index_file  (DhBook *book);
 
 gboolean     dh_book_get_enabled     (DhBook *book);
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]