[nautilus] bookmark-list: Use g_auto* macros



commit 377070def80c1ab5236c96e458ab49921a1cf2b7
Author: Yi-Soo An <yisooan gmail com>
Date:   Fri Jul 13 21:54:23 2018 +0900

    bookmark-list: Use g_auto* macros
    
    According to the documentation, the variables with these macros
    are cleaned up in a way appropriate to its type when the variables
    go out of scope.

 src/nautilus-bookmark-list.c | 100 +++++++++++++++----------------------------
 1 file changed, 34 insertions(+), 66 deletions(-)
---
diff --git a/src/nautilus-bookmark-list.c b/src/nautilus-bookmark-list.c
index 3df456ac4..1623c3936 100644
--- a/src/nautilus-bookmark-list.c
+++ b/src/nautilus-bookmark-list.c
@@ -63,21 +63,17 @@ G_DEFINE_TYPE (NautilusBookmarkList, nautilus_bookmark_list, G_TYPE_OBJECT)
 static NautilusBookmark *
 new_bookmark_from_uri (const char *uri, const char *label)
 {
-    NautilusBookmark *new_bookmark;
-    GFile *location;
+    NautilusBookmark *new_bookmark = NULL;
+    g_autoptr (GFile) location = NULL;
 
-    location = NULL;
     if (uri)
     {
         location = g_file_new_for_uri (uri);
-    }
-
-    new_bookmark = NULL;
 
-    if (location)
-    {
-        new_bookmark = nautilus_bookmark_new (location, label);
-        g_object_unref (location);
+        if (location)
+        {
+            new_bookmark = nautilus_bookmark_new (location, label);
+        }
     }
 
     return new_bookmark;
@@ -86,34 +82,26 @@ new_bookmark_from_uri (const char *uri, const char *label)
 static GFile *
 nautilus_bookmark_list_get_legacy_file (void)
 {
-    char *filename;
-    GFile *file;
+    g_autofree char *filename = NULL;
 
     filename = g_build_filename (g_get_home_dir (),
                                  ".gtk-bookmarks",
                                  NULL);
-    file = g_file_new_for_path (filename);
-
-    g_free (filename);
 
-    return file;
+    return g_file_new_for_path (filename);
 }
 
 static GFile *
 nautilus_bookmark_list_get_file (void)
 {
-    char *filename;
-    GFile *file;
+    g_autofree char *filename = NULL;
 
     filename = g_build_filename (g_get_user_config_dir (),
                                  "gtk-3.0",
                                  "bookmarks",
                                  NULL);
-    file = g_file_new_for_path (filename);
-
-    g_free (filename);
 
-    return file;
+    return g_file_new_for_path (filename);
 }
 
 /* Initialization.  */
@@ -217,7 +205,7 @@ bookmark_monitor_changed_cb (GFileMonitor      *monitor,
 static void
 nautilus_bookmark_list_init (NautilusBookmarkList *bookmarks)
 {
-    GFile *file;
+    g_autoptr (GFile) file = NULL;
 
     bookmarks->pending_ops = g_queue_new ();
 
@@ -229,8 +217,6 @@ nautilus_bookmark_list_init (NautilusBookmarkList *bookmarks)
 
     g_signal_connect (bookmarks->monitor, "changed",
                       G_CALLBACK (bookmark_monitor_changed_cb), bookmarks);
-
-    g_object_unref (file);
 }
 
 static void
@@ -264,9 +250,8 @@ nautilus_bookmark_list_item_with_location (NautilusBookmarkList *bookmarks,
                                            guint                *index)
 {
     GList *node;
-    GFile *bookmark_location;
+    g_autoptr (GFile) bookmark_location = NULL;
     NautilusBookmark *bookmark;
-    gboolean found = FALSE;
     guint idx;
 
     g_return_val_if_fail (NAUTILUS_IS_BOOKMARK_LIST (bookmarks), NULL);
@@ -280,18 +265,12 @@ nautilus_bookmark_list_item_with_location (NautilusBookmarkList *bookmarks,
         bookmark_location = nautilus_bookmark_get_location (bookmark);
 
         if (g_file_equal (location, bookmark_location))
-        {
-            found = TRUE;
-        }
-
-        g_object_unref (bookmark_location);
-
-        if (found)
         {
             if (index)
             {
                 *index = idx;
             }
+
             return bookmark;
         }
 
@@ -345,18 +324,16 @@ load_callback (GObject      *source_object,
                gpointer      user_data)
 {
     NautilusBookmarkList *self = NAUTILUS_BOOKMARK_LIST (source_object);
-    GError *error = NULL;
-    gchar *contents;
+    g_autoptr (GError) error = NULL;
+    g_autofree gchar *contents = NULL;
     char **lines;
     int i;
 
     contents = g_task_propagate_pointer (G_TASK (res), &error);
-
     if (error != NULL)
     {
         g_warning ("Unable to get contents of the bookmarks file: %s",
                    error->message);
-        g_error_free (error);
         op_processed_cb (self);
         return;
     }
@@ -369,9 +346,9 @@ load_callback (GObject      *source_object,
         {
             /* gtk 2.7/2.8 might have labels appended to bookmarks which are separated by a space */
             /* we must seperate the bookmark uri and the potential label */
-            char *space, *label;
+            char *space;
+            g_autofree char *label = NULL;
 
-            label = NULL;
             space = strchr (lines[i], ' ');
             if (space)
             {
@@ -380,7 +357,6 @@ load_callback (GObject      *source_object,
             }
 
             insert_bookmark_internal (self, new_bookmark_from_uri (lines[i], label), -1);
-            g_free (label);
         }
     }
 
@@ -388,7 +364,6 @@ load_callback (GObject      *source_object,
     op_processed_cb (self);
 
     g_strfreev (lines);
-    g_free (contents);
 }
 
 static void
@@ -424,7 +399,7 @@ load_io_thread (GTask        *task,
 static void
 load_file_async (NautilusBookmarkList *self)
 {
-    GTask *task;
+    g_autoptr (GTask) task = NULL;
 
     /* Wipe out old list. */
     clear (self);
@@ -433,7 +408,6 @@ load_file_async (NautilusBookmarkList *self)
                        NULL,
                        load_callback, NULL);
     g_task_run_in_thread (task, load_io_thread);
-    g_object_unref (task);
 }
 
 static void
@@ -442,9 +416,9 @@ save_callback (GObject      *source_object,
                gpointer      user_data)
 {
     NautilusBookmarkList *self = NAUTILUS_BOOKMARK_LIST (source_object);
-    GError *error = NULL;
+    g_autoptr (GError) error = NULL;
     gboolean success;
-    GFile *file;
+    g_autoptr (GFile) file = NULL;
 
     success = g_task_propagate_boolean (G_TASK (res), &error);
 
@@ -452,7 +426,6 @@ save_callback (GObject      *source_object,
     {
         g_warning ("Unable to replace contents of the bookmarks file: %s",
                    error->message);
-        g_error_free (error);
     }
 
     /* g_file_replace_contents() returned FALSE, but did not set an error. */
@@ -464,7 +437,6 @@ save_callback (GObject      *source_object,
     /* re-enable bookmark file monitoring */
     file = nautilus_bookmark_list_get_file ();
     self->monitor = g_file_monitor_file (file, 0, NULL, NULL);
-    g_object_unref (file);
 
     g_file_monitor_set_rate_limit (self->monitor, 1000);
     g_signal_connect (self->monitor, "changed",
@@ -479,17 +451,18 @@ save_io_thread (GTask        *task,
                 gpointer      task_data,
                 GCancellable *cancellable)
 {
-    gchar *contents, *path;
-    GFile *parent, *file;
+    gchar *contents;
+    g_autofree gchar *path = NULL;
+    g_autoptr (GFile) parent = NULL;
+    g_autoptr (GFile) file = NULL;
     gboolean success;
     GError *error = NULL;
 
     file = nautilus_bookmark_list_get_file ();
     parent = g_file_get_parent (file);
     path = g_file_get_path (parent);
+
     g_mkdir_with_parents (path, 0700);
-    g_free (path);
-    g_object_unref (parent);
 
     contents = (gchar *) g_task_get_task_data (task);
 
@@ -506,14 +479,12 @@ save_io_thread (GTask        *task,
     {
         g_task_return_boolean (task, success);
     }
-
-    g_object_unref (file);
 }
 
 static void
 save_file_async (NautilusBookmarkList *self)
 {
-    GTask *task;
+    g_autoptr (GTask) task = NULL;
     GString *bookmark_string;
     gchar *contents;
     GList *l;
@@ -537,19 +508,21 @@ save_file_async (NautilusBookmarkList *self)
         if (nautilus_bookmark_get_has_custom_name (bookmark))
         {
             const char *label;
-            char *uri;
+            g_autofree char *uri = NULL;
+
             label = nautilus_bookmark_get_name (bookmark);
             uri = nautilus_bookmark_get_uri (bookmark);
+
             g_string_append_printf (bookmark_string,
                                     "%s %s\n", uri, label);
-            g_free (uri);
         }
         else
         {
-            char *uri;
+            g_autofree char *uri = NULL;
+
             uri = nautilus_bookmark_get_uri (bookmark);
+
             g_string_append_printf (bookmark_string, "%s\n", uri);
-            g_free (uri);
         }
     }
 
@@ -560,7 +533,6 @@ save_file_async (NautilusBookmarkList *self)
     g_task_set_task_data (task, contents, g_free);
 
     g_task_run_in_thread (task, save_io_thread);
-    g_object_unref (task);
 }
 
 static void
@@ -620,8 +592,7 @@ gboolean
 nautilus_bookmark_list_can_bookmark_location (NautilusBookmarkList *list,
                                               GFile                *location)
 {
-    NautilusBookmark *bookmark;
-    gboolean is_builtin;
+    g_autoptr (NautilusBookmark) bookmark = NULL;
 
     if (nautilus_bookmark_list_item_with_location (list, location, NULL))
     {
@@ -645,10 +616,7 @@ nautilus_bookmark_list_can_bookmark_location (NautilusBookmarkList *list,
     }
 
     bookmark = nautilus_bookmark_new (location, NULL);
-    is_builtin = nautilus_bookmark_get_is_builtin (bookmark);
-    g_object_unref (bookmark);
-
-    return !is_builtin;
+    return !nautilus_bookmark_get_is_builtin (bookmark);
 }
 
 /**


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