[gimp] Use g_set_object() in a lot of places, and splrinkle some g_clear_object()



commit 1b7d63cce90ce6d018df9359eeb972db05b15671
Author: Michael Natterer <mitch gimp org>
Date:   Fri Jun 1 12:59:52 2018 +0200

    Use g_set_object() in a lot of places, and splrinkle some g_clear_object()

 app/core/gimp.c                       |  42 ++-------
 app/core/gimpcontext.c                | 169 +++++++++++-----------------------
 app/core/gimpdata.c                   |   4 +-
 app/core/gimpdrawable.c               |  11 +--
 app/core/gimpimage-metadata.c         |   7 +-
 app/core/gimpimage.c                  |  31 +------
 app/core/gimpitem.c                   |   6 +-
 app/core/gimplayer.c                  |  10 +-
 app/core/gimppaintinfo.c              |  11 +--
 app/core/gimptoolinfo.c               |  11 +--
 app/gegl/gimpapplicator.c             |   6 +-
 app/paint/gimpbrushcore.c             |  12 +--
 app/tools/gimpcolortool.c             |  17 +---
 app/tools/gimpgradienttool.c          |  22 +----
 app/tools/tool_manager.c              |   4 +-
 app/widgets/gimpcontainerview.c       |  18 ++--
 app/widgets/gimppickablebutton.c      |  22 ++---
 app/widgets/gimpviewrenderer.c        |  59 ++++--------
 libgimpwidgets/gimpcolorarea.c        |   5 +-
 libgimpwidgets/gimpcolorbutton.c      |  10 +-
 libgimpwidgets/gimpcolorprofileview.c |   9 +-
 libgimpwidgets/gimpcolorscale.c       |   5 +-
 libgimpwidgets/gimpcolorselect.c      |   5 +-
 libgimpwidgets/gimppreview.c          |  11 +--
 libgimpwidgets/gimppreviewarea.c      |   5 +-
 modules/color-selector-cmyk.c         |  20 ++--
 modules/color-selector-water.c        |   5 +-
 modules/gimpcolorwheel.c              |   5 +-
 28 files changed, 129 insertions(+), 413 deletions(-)
---
diff --git a/app/core/gimp.c b/app/core/gimp.c
index 5530f5c135..e13b0884de 100644
--- a/app/core/gimp.c
+++ b/app/core/gimp.c
@@ -932,17 +932,10 @@ gimp_set_clipboard_image (Gimp      *gimp,
   g_return_if_fail (GIMP_IS_GIMP (gimp));
   g_return_if_fail (image == NULL || GIMP_IS_IMAGE (image));
 
-  /* ref first, it could be the same as gimp->clipboard_image, but
-   * don't bail if equal because always we want the signal emission
-   */
-  if (image)
-    g_object_ref (image);
-
   g_clear_object (&gimp->clipboard_buffer);
-  g_clear_object (&gimp->clipboard_image);
-
-  gimp->clipboard_image = image;
+  g_set_object (&gimp->clipboard_image, image);
 
+  /* we want the signal emission */
   g_signal_emit (gimp, gimp_signals[CLIPBOARD_CHANGED], 0);
 }
 
@@ -961,15 +954,10 @@ gimp_set_clipboard_buffer (Gimp       *gimp,
   g_return_if_fail (GIMP_IS_GIMP (gimp));
   g_return_if_fail (buffer == NULL || GIMP_IS_BUFFER (buffer));
 
-  /* see above */
-  if (buffer)
-    g_object_ref (buffer);
-
   g_clear_object (&gimp->clipboard_image);
-  g_clear_object (&gimp->clipboard_buffer);
-
-  gimp->clipboard_buffer = buffer;
+  g_set_object (&gimp->clipboard_buffer, buffer);
 
+  /* we want the signal emission */
   g_signal_emit (gimp, gimp_signals[CLIPBOARD_CHANGED], 0);
 }
 
@@ -1022,16 +1010,7 @@ gimp_set_default_context (Gimp        *gimp,
   g_return_if_fail (GIMP_IS_GIMP (gimp));
   g_return_if_fail (context == NULL || GIMP_IS_CONTEXT (context));
 
-  if (context != gimp->default_context)
-    {
-      if (gimp->default_context)
-        g_object_unref (gimp->default_context);
-
-      gimp->default_context = context;
-
-      if (gimp->default_context)
-        g_object_ref (gimp->default_context);
-    }
+  g_set_object (&gimp->default_context, context);
 }
 
 GimpContext *
@@ -1049,16 +1028,7 @@ gimp_set_user_context (Gimp        *gimp,
   g_return_if_fail (GIMP_IS_GIMP (gimp));
   g_return_if_fail (context == NULL || GIMP_IS_CONTEXT (context));
 
-  if (context != gimp->user_context)
-    {
-      if (gimp->user_context)
-        g_object_unref (gimp->user_context);
-
-      gimp->user_context = context;
-
-      if (gimp->user_context)
-        g_object_ref (gimp->user_context);
-    }
+  g_set_object (&gimp->user_context, context);
 }
 
 GimpContext *
diff --git a/app/core/gimpcontext.c b/app/core/gimpcontext.c
index b4f79dab69..da55e09b46 100644
--- a/app/core/gimpcontext.c
+++ b/app/core/gimpcontext.c
@@ -2123,19 +2123,14 @@ gimp_context_real_set_tool (GimpContext  *context,
 
   /*  disconnect from the old tool's signals  */
   if (context->tool_info)
-    {
-      g_signal_handlers_disconnect_by_func (context->tool_info,
-                                            gimp_context_tool_dirty,
-                                            context);
-      g_object_unref (context->tool_info);
-    }
+    g_signal_handlers_disconnect_by_func (context->tool_info,
+                                          gimp_context_tool_dirty,
+                                          context);
 
-  context->tool_info = tool_info;
+  g_set_object (&context->tool_info, tool_info);
 
   if (tool_info)
     {
-      g_object_ref (tool_info);
-
       g_signal_connect_object (tool_info, "name-changed",
                                G_CALLBACK (gimp_context_tool_dirty),
                                context,
@@ -2250,19 +2245,14 @@ gimp_context_real_set_paint_info (GimpContext   *context,
 
   /*  disconnect from the old paint info's signals  */
   if (context->paint_info)
-    {
-      g_signal_handlers_disconnect_by_func (context->paint_info,
-                                            gimp_context_paint_info_dirty,
-                                            context);
-      g_object_unref (context->paint_info);
-    }
+    g_signal_handlers_disconnect_by_func (context->paint_info,
+                                          gimp_context_paint_info_dirty,
+                                          context);
 
-  context->paint_info = paint_info;
+  g_set_object (&context->paint_info, paint_info);
 
   if (paint_info)
     {
-      g_object_ref (paint_info);
-
       g_signal_connect_object (paint_info, "name-changed",
                                G_CALLBACK (gimp_context_paint_info_dirty),
                                context,
@@ -2608,19 +2598,14 @@ gimp_context_real_set_brush (GimpContext *context,
 
   /*  disconnect from the old brush's signals  */
   if (context->brush)
-    {
-      g_signal_handlers_disconnect_by_func (context->brush,
-                                            gimp_context_brush_dirty,
-                                            context);
-      g_object_unref (context->brush);
-    }
+    g_signal_handlers_disconnect_by_func (context->brush,
+                                          gimp_context_brush_dirty,
+                                          context);
 
-  context->brush = brush;
+  g_set_object (&context->brush, brush);
 
   if (brush)
     {
-      g_object_ref (brush);
-
       g_signal_connect_object (brush, "name-changed",
                                G_CALLBACK (gimp_context_brush_dirty),
                                context,
@@ -2729,19 +2714,14 @@ gimp_context_real_set_dynamics (GimpContext  *context,
 
   /*  disconnect from the old dynamics' signals  */
   if (context->dynamics)
-    {
-      g_signal_handlers_disconnect_by_func (context->dynamics,
-                                            gimp_context_dynamics_dirty,
-                                            context);
-      g_object_unref (context->dynamics);
-    }
+    g_signal_handlers_disconnect_by_func (context->dynamics,
+                                          gimp_context_dynamics_dirty,
+                                          context);
 
-  context->dynamics = dynamics;
+  g_set_object (&context->dynamics, dynamics);
 
   if (dynamics)
     {
-      g_object_ref (dynamics);
-
       g_signal_connect_object (dynamics, "name-changed",
                                G_CALLBACK (gimp_context_dynamics_dirty),
                                context,
@@ -2853,19 +2833,14 @@ gimp_context_real_set_mybrush (GimpContext *context,
 
   /*  disconnect from the old mybrush's signals  */
   if (context->mybrush)
-    {
-      g_signal_handlers_disconnect_by_func (context->mybrush,
-                                            gimp_context_mybrush_dirty,
-                                            context);
-      g_object_unref (context->mybrush);
-    }
+    g_signal_handlers_disconnect_by_func (context->mybrush,
+                                          gimp_context_mybrush_dirty,
+                                          context);
 
-  context->mybrush = brush;
+  g_set_object (&context->mybrush, brush);
 
   if (brush)
     {
-      g_object_ref (brush);
-
       g_signal_connect_object (brush, "name-changed",
                                G_CALLBACK (gimp_context_mybrush_dirty),
                                context,
@@ -2976,19 +2951,14 @@ gimp_context_real_set_pattern (GimpContext *context,
 
   /*  disconnect from the old pattern's signals  */
   if (context->pattern)
-    {
-      g_signal_handlers_disconnect_by_func (context->pattern,
-                                            gimp_context_pattern_dirty,
-                                            context);
-      g_object_unref (context->pattern);
-    }
+    g_signal_handlers_disconnect_by_func (context->pattern,
+                                          gimp_context_pattern_dirty,
+                                          context);
 
-  context->pattern = pattern;
+  g_set_object (&context->pattern, pattern);
 
   if (pattern)
     {
-      g_object_ref (pattern);
-
       g_signal_connect_object (pattern, "name-changed",
                                G_CALLBACK (gimp_context_pattern_dirty),
                                context,
@@ -3099,19 +3069,14 @@ gimp_context_real_set_gradient (GimpContext  *context,
 
   /*  disconnect from the old gradient's signals  */
   if (context->gradient)
-    {
-      g_signal_handlers_disconnect_by_func (context->gradient,
-                                            gimp_context_gradient_dirty,
-                                            context);
-      g_object_unref (context->gradient);
-    }
+    g_signal_handlers_disconnect_by_func (context->gradient,
+                                          gimp_context_gradient_dirty,
+                                          context);
 
-  context->gradient = gradient;
+  g_set_object (&context->gradient, gradient);
 
   if (gradient)
     {
-      g_object_ref (gradient);
-
       g_signal_connect_object (gradient, "name-changed",
                                G_CALLBACK (gimp_context_gradient_dirty),
                                context,
@@ -3222,19 +3187,14 @@ gimp_context_real_set_palette (GimpContext *context,
 
   /*  disconnect from the old palette's signals  */
   if (context->palette)
-    {
-      g_signal_handlers_disconnect_by_func (context->palette,
-                                            gimp_context_palette_dirty,
-                                            context);
-      g_object_unref (context->palette);
-    }
+    g_signal_handlers_disconnect_by_func (context->palette,
+                                          gimp_context_palette_dirty,
+                                          context);
 
-  context->palette = palette;
+  g_set_object (&context->palette, palette);
 
   if (palette)
     {
-      g_object_ref (palette);
-
       g_signal_connect_object (palette, "name-changed",
                                G_CALLBACK (gimp_context_palette_dirty),
                                context,
@@ -3338,19 +3298,14 @@ gimp_context_real_set_tool_preset (GimpContext    *context,
 
   /*  disconnect from the old tool preset's signals  */
   if (context->tool_preset)
-    {
-      g_signal_handlers_disconnect_by_func (context->tool_preset,
-                                            gimp_context_tool_preset_dirty,
-                                            context);
-      g_object_unref (context->tool_preset);
-    }
+    g_signal_handlers_disconnect_by_func (context->tool_preset,
+                                          gimp_context_tool_preset_dirty,
+                                          context);
 
-  context->tool_preset = tool_preset;
+  g_set_object (&context->tool_preset, tool_preset);
 
   if (tool_preset)
     {
-      g_object_ref (tool_preset);
-
       g_signal_connect_object (tool_preset, "name-changed",
                                G_CALLBACK (gimp_context_tool_preset_dirty),
                                context,
@@ -3494,19 +3449,14 @@ gimp_context_real_set_font (GimpContext *context,
 
   /*  disconnect from the old font's signals  */
   if (context->font)
-    {
-      g_signal_handlers_disconnect_by_func (context->font,
-                                            gimp_context_font_dirty,
-                                            context);
-      g_object_unref (context->font);
-    }
+    g_signal_handlers_disconnect_by_func (context->font,
+                                          gimp_context_font_dirty,
+                                          context);
 
-  context->font = font;
+  g_set_object (&context->font, font);
 
   if (font)
     {
-      g_object_ref (font);
-
       g_signal_connect_object (font, "name-changed",
                                G_CALLBACK (gimp_context_font_dirty),
                                context,
@@ -3621,19 +3571,14 @@ gimp_context_real_set_buffer (GimpContext *context,
 
   /*  disconnect from the old buffer's signals  */
   if (context->buffer)
-    {
-      g_signal_handlers_disconnect_by_func (context->buffer,
-                                            gimp_context_buffer_dirty,
-                                            context);
-      g_object_unref (context->buffer);
-    }
+    g_signal_handlers_disconnect_by_func (context->buffer,
+                                          gimp_context_buffer_dirty,
+                                          context);
 
-  context->buffer = buffer;
+  g_set_object (&context->buffer, buffer);
 
   if (buffer)
     {
-      g_object_ref (buffer);
-
       g_signal_connect_object (buffer, "name-changed",
                                G_CALLBACK (gimp_context_buffer_dirty),
                                context,
@@ -3747,19 +3692,14 @@ gimp_context_real_set_imagefile (GimpContext   *context,
 
   /*  disconnect from the old imagefile's signals  */
   if (context->imagefile)
-    {
-      g_signal_handlers_disconnect_by_func (context->imagefile,
-                                            gimp_context_imagefile_dirty,
-                                            context);
-      g_object_unref (context->imagefile);
-    }
+    g_signal_handlers_disconnect_by_func (context->imagefile,
+                                          gimp_context_imagefile_dirty,
+                                          context);
 
-  context->imagefile = imagefile;
+  g_set_object (&context->imagefile, imagefile);
 
   if (imagefile)
     {
-      g_object_ref (imagefile);
-
       g_signal_connect_object (imagefile, "name-changed",
                                G_CALLBACK (gimp_context_imagefile_dirty),
                                context,
@@ -3873,19 +3813,14 @@ gimp_context_real_set_template (GimpContext  *context,
 
   /*  disconnect from the old template's signals  */
   if (context->template)
-    {
-      g_signal_handlers_disconnect_by_func (context->template,
-                                            gimp_context_template_dirty,
-                                            context);
-      g_object_unref (context->template);
-    }
+    g_signal_handlers_disconnect_by_func (context->template,
+                                          gimp_context_template_dirty,
+                                          context);
 
-  context->template = template;
+  g_set_object (&context->template, template);
 
   if (template)
     {
-      g_object_ref (template);
-
       g_signal_connect_object (template, "name-changed",
                                G_CALLBACK (gimp_context_template_dirty),
                                context,
diff --git a/app/core/gimpdata.c b/app/core/gimpdata.c
index f909ed899e..4a58f368e1 100644
--- a/app/core/gimpdata.c
+++ b/app/core/gimpdata.c
@@ -813,10 +813,8 @@ gimp_data_set_file (GimpData *data,
   if (private->internal)
     return;
 
-  if (private->file)
-    g_object_unref (private->file);
+  g_set_object (&private->file, file);
 
-  private->file      = g_object_ref (file);
   private->writable  = FALSE;
   private->deletable = FALSE;
 
diff --git a/app/core/gimpdrawable.c b/app/core/gimpdrawable.c
index 3d8f6caa5e..9d77e9b393 100644
--- a/app/core/gimpdrawable.c
+++ b/app/core/gimpdrawable.c
@@ -813,17 +813,10 @@ gimp_drawable_real_set_buffer (GimpDrawable *drawable,
     gimp_image_undo_push_drawable_mod (gimp_item_get_image (item), undo_desc,
                                        drawable, FALSE);
 
-  /*  ref new before unrefing old, they might be the same  */
-  g_object_ref (buffer);
-
   if (drawable->private->buffer)
-    {
-      old_has_alpha = gimp_drawable_has_alpha (drawable);
-
-      g_object_unref (drawable->private->buffer);
-    }
+    old_has_alpha = gimp_drawable_has_alpha (drawable);
 
-  drawable->private->buffer = buffer;
+  g_set_object (&drawable->private->buffer, buffer);
 
   if (drawable->private->buffer_source_node)
     gegl_node_set (drawable->private->buffer_source_node,
diff --git a/app/core/gimpimage-metadata.c b/app/core/gimpimage-metadata.c
index 459dac19e0..b1cbee2c2a 100644
--- a/app/core/gimpimage-metadata.c
+++ b/app/core/gimpimage-metadata.c
@@ -61,17 +61,12 @@ gimp_image_set_metadata (GimpImage    *image,
       if (push_undo)
         gimp_image_undo_push_image_metadata (image, NULL);
 
-      if (private->metadata)
-        g_object_unref (private->metadata);
-
-      private->metadata = metadata;
+      g_set_object (&private->metadata, metadata);
 
       if (private->metadata)
         {
           gdouble xres, yres;
 
-          g_object_ref (private->metadata);
-
           gimp_metadata_set_pixel_size (metadata,
                                         gimp_image_get_width  (image),
                                         gimp_image_get_height (image));
diff --git a/app/core/gimpimage.c b/app/core/gimpimage.c
index 2af10d8b64..22d40a52c2 100644
--- a/app/core/gimpimage.c
+++ b/app/core/gimpimage.c
@@ -2118,16 +2118,8 @@ gimp_image_set_imported_file (GimpImage *image,
 
   private = GIMP_IMAGE_GET_PRIVATE (image);
 
-  if (private->imported_file != file)
+  if (g_set_object (&private->imported_file, file))
     {
-      if (private->imported_file)
-        g_object_unref (private->imported_file);
-
-      private->imported_file = file;
-
-      if (private->imported_file)
-        g_object_ref (private->imported_file);
-
       gimp_object_name_changed (GIMP_OBJECT (image));
     }
 }
@@ -2151,16 +2143,8 @@ gimp_image_set_exported_file (GimpImage *image,
 
   private = GIMP_IMAGE_GET_PRIVATE (image);
 
-  if (private->exported_file != file)
+  if (g_set_object (&private->exported_file, file))
     {
-      if (private->exported_file)
-        g_object_unref (private->exported_file);
-
-      private->exported_file = file;
-
-      if (private->exported_file)
-        g_object_ref (private->exported_file);
-
       gimp_object_name_changed (GIMP_OBJECT (image));
     }
 }
@@ -2184,16 +2168,7 @@ gimp_image_set_save_a_copy_file (GimpImage *image,
 
   private = GIMP_IMAGE_GET_PRIVATE (image);
 
-  if (private->save_a_copy_file != file)
-    {
-      if (private->save_a_copy_file)
-        g_object_unref (private->save_a_copy_file);
-
-      private->save_a_copy_file = file;
-
-      if (private->save_a_copy_file)
-        g_object_ref (private->save_a_copy_file);
-    }
+  g_set_object (&private->save_a_copy_file, file);
 }
 
 static gchar *
diff --git a/app/core/gimpitem.c b/app/core/gimpitem.c
index 84685e74f7..b80cac04d3 100644
--- a/app/core/gimpitem.c
+++ b/app/core/gimpitem.c
@@ -2054,11 +2054,7 @@ gimp_item_set_parasites (GimpItem         *item,
 
   private = GET_PRIVATE (item);
 
-  if (parasites != private->parasites)
-    {
-      g_object_unref (private->parasites);
-      private->parasites = g_object_ref (parasites);
-    }
+  g_set_object (&private->parasites, parasites);
 }
 
 /**
diff --git a/app/core/gimplayer.c b/app/core/gimplayer.c
index d5a461e43d..0b25e79f1f 100644
--- a/app/core/gimplayer.c
+++ b/app/core/gimplayer.c
@@ -2452,7 +2452,7 @@ gimp_layer_set_floating_sel_drawable (GimpLayer    *layer,
   g_return_if_fail (GIMP_IS_LAYER (layer));
   g_return_if_fail (drawable == NULL || GIMP_IS_DRAWABLE (drawable));
 
-  if (layer->fs.drawable != drawable)
+  if (g_set_object (&layer->fs.drawable, drawable))
     {
       if (layer->fs.segs)
         {
@@ -2461,14 +2461,6 @@ gimp_layer_set_floating_sel_drawable (GimpLayer    *layer,
           layer->fs.num_segs = 0;
         }
 
-      if (layer->fs.drawable)
-        g_object_unref (layer->fs.drawable);
-
-      layer->fs.drawable = drawable;
-
-      if (layer->fs.drawable)
-        g_object_ref (layer->fs.drawable);
-
       g_object_notify (G_OBJECT (layer), "floating-selection");
     }
 }
diff --git a/app/core/gimppaintinfo.c b/app/core/gimppaintinfo.c
index 313cc5672d..4a621b7755 100644
--- a/app/core/gimppaintinfo.c
+++ b/app/core/gimppaintinfo.c
@@ -130,16 +130,7 @@ gimp_paint_info_set_standard (Gimp          *gimp,
   g_return_if_fail (GIMP_IS_GIMP (gimp));
   g_return_if_fail (! paint_info || GIMP_IS_PAINT_INFO (paint_info));
 
-  if (paint_info != gimp->standard_paint_info)
-    {
-      if (gimp->standard_paint_info)
-        g_object_unref (gimp->standard_paint_info);
-
-      gimp->standard_paint_info = paint_info;
-
-      if (gimp->standard_paint_info)
-        g_object_ref (gimp->standard_paint_info);
-    }
+  g_set_object (&gimp->standard_paint_info, paint_info);
 }
 
 GimpPaintInfo *
diff --git a/app/core/gimptoolinfo.c b/app/core/gimptoolinfo.c
index 2da88ffa10..7cc2970254 100644
--- a/app/core/gimptoolinfo.c
+++ b/app/core/gimptoolinfo.c
@@ -272,16 +272,7 @@ gimp_tool_info_set_standard (Gimp         *gimp,
   g_return_if_fail (GIMP_IS_GIMP (gimp));
   g_return_if_fail (! tool_info || GIMP_IS_TOOL_INFO (tool_info));
 
-  if (tool_info != gimp->standard_tool_info)
-    {
-      if (gimp->standard_tool_info)
-        g_object_unref (gimp->standard_tool_info);
-
-      gimp->standard_tool_info = tool_info;
-
-      if (gimp->standard_tool_info)
-        g_object_ref (gimp->standard_tool_info);
-    }
+  g_set_object (&gimp->standard_tool_info, tool_info);
 }
 
 GimpToolInfo *
diff --git a/app/gegl/gimpapplicator.c b/app/gegl/gimpapplicator.c
index bc0e218607..78b58330ed 100644
--- a/app/gegl/gimpapplicator.c
+++ b/app/gegl/gimpapplicator.c
@@ -70,11 +70,7 @@ gimp_applicator_finalize (GObject *object)
 {
   GimpApplicator *applicator = GIMP_APPLICATOR (object);
 
-  if (applicator->node)
-    {
-      g_object_unref (applicator->node);
-      applicator->node = NULL;
-    }
+  g_clear_object (&applicator->node);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
diff --git a/app/paint/gimpbrushcore.c b/app/paint/gimpbrushcore.c
index 3baa5b0875..f16f0f8d45 100644
--- a/app/paint/gimpbrushcore.c
+++ b/app/paint/gimpbrushcore.c
@@ -878,14 +878,12 @@ gimp_brush_core_real_set_brush (GimpBrushCore *core,
                                             gimp_brush_core_invalidate_cache,
                                             core);
       gimp_brush_end_use (core->main_brush);
-      g_clear_object (&core->main_brush);
     }
 
-  core->main_brush = brush;
+  g_set_object (&core->main_brush, brush);
 
   if (core->main_brush)
     {
-      g_object_ref (core->main_brush);
       gimp_brush_begin_use (core->main_brush);
       g_signal_connect (core->main_brush, "invalidate-preview",
                         G_CALLBACK (gimp_brush_core_invalidate_cache),
@@ -897,13 +895,7 @@ static void
 gimp_brush_core_real_set_dynamics (GimpBrushCore *core,
                                    GimpDynamics  *dynamics)
 {
-  if (dynamics == core->dynamics)
-    return;
-
-  g_clear_object (&core->dynamics);
-
-  if (dynamics)
-    core->dynamics = g_object_ref (dynamics);
+  g_set_object (&core->dynamics, dynamics);
 }
 
 void
diff --git a/app/tools/gimpcolortool.c b/app/tools/gimpcolortool.c
index 231bd2a55d..993320ae11 100644
--- a/app/tools/gimpcolortool.c
+++ b/app/tools/gimpcolortool.c
@@ -184,11 +184,7 @@ gimp_color_tool_finalize (GObject *object)
 {
   GimpColorTool *color_tool = GIMP_COLOR_TOOL (object);
 
-  if (color_tool->options)
-    {
-      g_object_unref (color_tool->options);
-      color_tool->options = NULL;
-    }
+  g_clear_object (&color_tool->options);
 
   G_OBJECT_CLASS (parent_class)->finalize (object);
 }
@@ -642,10 +638,7 @@ gimp_color_tool_enable (GimpColorTool    *color_tool,
       return;
     }
 
-  if (color_tool->options)
-    g_object_unref (color_tool->options);
-
-  color_tool->options = g_object_ref (options);
+  g_set_object (&color_tool->options, options);
 
   /*  color picking doesn't snap, see bug #768058  */
   color_tool->saved_snap_to = gimp_tool_control_get_snap_to (tool->control);
@@ -669,11 +662,7 @@ gimp_color_tool_disable (GimpColorTool *color_tool)
       return;
     }
 
-  if (color_tool->options)
-    {
-      g_object_unref (color_tool->options);
-      color_tool->options = NULL;
-    }
+  g_clear_object (&color_tool->options);
 
   gimp_tool_control_set_snap_to (tool->control, color_tool->saved_snap_to);
   color_tool->saved_snap_to = FALSE;
diff --git a/app/tools/gimpgradienttool.c b/app/tools/gimpgradienttool.c
index bfb87d8636..09d59f1762 100644
--- a/app/tools/gimpgradienttool.c
+++ b/app/tools/gimpgradienttool.c
@@ -984,20 +984,15 @@ gimp_gradient_tool_set_gradient (GimpGradientTool *gradient_tool,
                                  GimpGradient     *gradient)
 {
   if (gradient_tool->gradient)
-    {
-      g_signal_handlers_disconnect_by_func (gradient_tool->gradient,
-                                            G_CALLBACK (gimp_gradient_tool_gradient_dirty),
-                                            gradient_tool);
+    g_signal_handlers_disconnect_by_func (gradient_tool->gradient,
+                                          G_CALLBACK (gimp_gradient_tool_gradient_dirty),
+                                          gradient_tool);
 
-      g_object_unref (gradient_tool->gradient);
-    }
 
-  gradient_tool->gradient = gradient;
+  g_set_object (&gradient_tool->gradient, gradient);
 
   if (gradient_tool->gradient)
     {
-      g_object_ref (gradient);
-
       g_signal_connect_swapped (gradient_tool->gradient, "dirty",
                                 G_CALLBACK (gimp_gradient_tool_gradient_dirty),
                                 gradient_tool);
@@ -1074,15 +1069,8 @@ gimp_gradient_tool_set_tentative_gradient (GimpGradientTool *gradient_tool,
   g_return_if_fail (GIMP_IS_GRADIENT_TOOL (gradient_tool));
   g_return_if_fail (gradient == NULL || GIMP_IS_GRADIENT (gradient));
 
-  if (gradient != gradient_tool->tentative_gradient)
+  if (g_set_object (&gradient_tool->tentative_gradient, gradient))
     {
-      g_clear_object (&gradient_tool->tentative_gradient);
-
-      gradient_tool->tentative_gradient = gradient;
-
-      if (gradient)
-        g_object_ref (gradient);
-
       if (gradient_tool->render_node)
         {
           gegl_node_set (gradient_tool->render_node,
diff --git a/app/tools/tool_manager.c b/app/tools/tool_manager.c
index 6822c1ea3d..2fc5c0dd1c 100644
--- a/app/tools/tool_manager.c
+++ b/app/tools/tool_manager.c
@@ -638,11 +638,9 @@ tool_manager_select_tool (Gimp     *gimp,
           tool_manager_control_active (gimp, GIMP_TOOL_ACTION_HALT, display);
           tool_manager_focus_display_active (gimp, NULL);
         }
-
-      g_object_unref (tool_manager->active_tool);
     }
 
-  tool_manager->active_tool = g_object_ref (tool);
+  g_set_object (&tool_manager->active_tool, tool);
 }
 
 static void
diff --git a/app/widgets/gimpcontainerview.c b/app/widgets/gimpcontainerview.c
index f9b9046695..68fd3b3fd4 100644
--- a/app/widgets/gimpcontainerview.c
+++ b/app/widgets/gimpcontainerview.c
@@ -465,22 +465,18 @@ gimp_container_view_real_set_context (GimpContainerView *view,
 {
   GimpContainerViewPrivate *private = GIMP_CONTAINER_VIEW_GET_PRIVATE (view);
 
-  if (private->context)
+  if (private->context &&
+      private->container)
     {
-      if (private->container)
-        gimp_container_view_disconnect_context (view);
-
-      g_object_unref (private->context);
+      gimp_container_view_disconnect_context (view);
     }
 
-  private->context = context;
+  g_set_object (&private->context, context);
 
-  if (private->context)
+  if (private->context &&
+      private->container)
     {
-      g_object_ref (private->context);
-
-      if (private->container)
-        gimp_container_view_connect_context (view);
+      gimp_container_view_connect_context (view);
     }
 }
 
diff --git a/app/widgets/gimppickablebutton.c b/app/widgets/gimppickablebutton.c
index a422024976..0dd6c1dde0 100644
--- a/app/widgets/gimppickablebutton.c
+++ b/app/widgets/gimppickablebutton.c
@@ -327,24 +327,16 @@ gimp_pickable_button_set_pickable (GimpPickableButton *button,
   if (pickable != button->private->pickable)
     {
       if (button->private->pickable)
-        {
-          g_signal_handlers_disconnect_by_func (button->private->pickable,
-                                                gimp_pickable_button_notify_buffer,
-                                                button);
+        g_signal_handlers_disconnect_by_func (button->private->pickable,
+                                              gimp_pickable_button_notify_buffer,
+                                              button);
 
-          g_object_unref (button->private->pickable);
-        }
-
-      button->private->pickable = pickable;
+      g_set_object (&button->private->pickable, pickable);
 
       if (button->private->pickable)
-        {
-          g_object_ref (button->private->pickable);
-
-          g_signal_connect (button->private->pickable, "notify::buffer",
-                            G_CALLBACK (gimp_pickable_button_notify_buffer),
-                            button);
-        }
+        g_signal_connect (button->private->pickable, "notify::buffer",
+                          G_CALLBACK (gimp_pickable_button_notify_buffer),
+                          button);
 
       gimp_view_set_viewable (GIMP_VIEW (button->private->view),
                               GIMP_VIEWABLE (pickable));
diff --git a/app/widgets/gimpviewrenderer.c b/app/widgets/gimpviewrenderer.c
index 3e8019321c..7957f84669 100644
--- a/app/widgets/gimpviewrenderer.c
+++ b/app/widgets/gimpviewrenderer.c
@@ -538,24 +538,16 @@ gimp_view_renderer_set_color_config (GimpViewRenderer *renderer,
   if (color_config != renderer->priv->color_config)
     {
       if (renderer->priv->color_config)
-        {
-          g_signal_handlers_disconnect_by_func (renderer->priv->color_config,
-                                                gimp_view_renderer_config_notify,
-                                                renderer);
-
-          g_object_unref (renderer->priv->color_config);
-        }
+        g_signal_handlers_disconnect_by_func (renderer->priv->color_config,
+                                              gimp_view_renderer_config_notify,
+                                              renderer);
 
-      renderer->priv->color_config = color_config;
+      g_set_object (&renderer->priv->color_config, color_config);
 
       if (renderer->priv->color_config)
-        {
-          g_object_ref (renderer->priv->color_config);
-
-          g_signal_connect (renderer->priv->color_config, "notify",
-                            G_CALLBACK (gimp_view_renderer_config_notify),
-                            renderer);
-        }
+        g_signal_connect (renderer->priv->color_config, "notify",
+                          G_CALLBACK (gimp_view_renderer_config_notify),
+                          renderer);
 
       gimp_view_renderer_config_notify (G_OBJECT (renderer->priv->color_config),
                                         NULL, renderer);
@@ -701,28 +693,20 @@ static void
 gimp_view_renderer_real_set_context (GimpViewRenderer *renderer,
                                      GimpContext      *context)
 {
-  if (renderer->context)
+  if (renderer->context &&
+      renderer->priv->color_config ==
+      renderer->context->gimp->config->color_management)
     {
-      if (renderer->priv->color_config ==
-          renderer->context->gimp->config->color_management)
-        {
-          gimp_view_renderer_set_color_config (renderer, NULL);
-        }
-
-      g_object_unref (renderer->context);
+      gimp_view_renderer_set_color_config (renderer, NULL);
     }
 
-  renderer->context = context;
+  g_set_object (&renderer->context, context);
 
-  if (renderer->context)
+  if (renderer->context &&
+      renderer->priv->color_config == NULL)
     {
-      g_object_ref (renderer->context);
-
-      if (renderer->priv->color_config == NULL)
-        {
-          gimp_view_renderer_set_color_config (renderer,
-                                               renderer->context->gimp->config->color_management);
-        }
+      gimp_view_renderer_set_color_config (renderer,
+                                           renderer->context->gimp->config->color_management);
     }
 }
 
@@ -967,19 +951,12 @@ gimp_view_renderer_render_pixbuf (GimpViewRenderer *renderer,
           dest += dest_stride;
         }
 
-      if (renderer->priv->pixbuf)
-        g_object_unref (renderer->priv->pixbuf);
-
+      g_clear_object (&renderer->priv->pixbuf);
       renderer->priv->pixbuf = new;
     }
   else
     {
-      g_object_ref (pixbuf);
-
-      if (renderer->priv->pixbuf)
-        g_object_unref (renderer->priv->pixbuf);
-
-      renderer->priv->pixbuf = pixbuf;
+      g_set_object (&renderer->priv->pixbuf, pixbuf);
     }
 }
 
diff --git a/libgimpwidgets/gimpcolorarea.c b/libgimpwidgets/gimpcolorarea.c
index 31940eb2de..7f7b426fd8 100644
--- a/libgimpwidgets/gimpcolorarea.c
+++ b/libgimpwidgets/gimpcolorarea.c
@@ -681,17 +681,14 @@ gimp_color_area_set_color_config (GimpColorArea   *area,
           g_signal_handlers_disconnect_by_func (priv->config,
                                                 gimp_color_area_destroy_transform,
                                                 area);
-          g_object_unref (priv->config);
 
           gimp_color_area_destroy_transform (area);
         }
 
-      priv->config = config;
+      g_set_object (&priv->config, config);
 
       if (priv->config)
         {
-          g_object_ref (priv->config);
-
           g_signal_connect_swapped (priv->config, "notify",
                                     G_CALLBACK (gimp_color_area_destroy_transform),
                                     area);
diff --git a/libgimpwidgets/gimpcolorbutton.c b/libgimpwidgets/gimpcolorbutton.c
index 201bdc9198..bdf336c61f 100644
--- a/libgimpwidgets/gimpcolorbutton.c
+++ b/libgimpwidgets/gimpcolorbutton.c
@@ -895,16 +895,8 @@ gimp_color_button_set_color_config (GimpColorButton *button,
 
   priv = GET_PRIVATE (button);
 
-  if (config != priv->config)
+  if (g_set_object (&priv->config, config))
     {
-      if (priv->config)
-        g_object_unref (priv->config);
-
-      priv->config = config;
-
-      if (priv->config)
-        g_object_ref (priv->config);
-
       if (priv->color_area)
         gimp_color_area_set_color_config (GIMP_COLOR_AREA (priv->color_area),
                                           priv->config);
diff --git a/libgimpwidgets/gimpcolorprofileview.c b/libgimpwidgets/gimpcolorprofileview.c
index 9742e09507..f6c7955b8a 100644
--- a/libgimpwidgets/gimpcolorprofileview.c
+++ b/libgimpwidgets/gimpcolorprofileview.c
@@ -131,18 +131,11 @@ gimp_color_profile_view_set_profile (GimpColorProfileView *view,
 
   gtk_text_buffer_set_text (buffer, "", 0);
 
-  if (view->priv->profile)
-    g_object_unref (view->priv->profile);
-
-  view->priv->profile = profile;
-
-  if (view->priv->profile)
+  if (g_set_object (&view->priv->profile, profile) && profile)
     {
       GtkTextIter  iter;
       const gchar *text;
 
-      g_object_ref (view->priv->profile);
-
       gtk_text_buffer_get_start_iter (buffer, &iter);
 
       text = gimp_color_profile_get_label (profile);
diff --git a/libgimpwidgets/gimpcolorscale.c b/libgimpwidgets/gimpcolorscale.c
index 0e6b36d66b..5f23f06781 100644
--- a/libgimpwidgets/gimpcolorscale.c
+++ b/libgimpwidgets/gimpcolorscale.c
@@ -718,17 +718,14 @@ gimp_color_scale_set_color_config (GimpColorScale  *scale,
           g_signal_handlers_disconnect_by_func (priv->config,
                                                 gimp_color_scale_notify_config,
                                                 scale);
-          g_object_unref (priv->config);
 
           gimp_color_scale_destroy_transform (scale);
         }
 
-      priv->config = config;
+      g_set_object (&priv->config, config);
 
       if (priv->config)
         {
-          g_object_ref (priv->config);
-
           g_signal_connect (priv->config, "notify",
                             G_CALLBACK (gimp_color_scale_notify_config),
                             scale);
diff --git a/libgimpwidgets/gimpcolorselect.c b/libgimpwidgets/gimpcolorselect.c
index 59b7db7a9f..f6340b87b6 100644
--- a/libgimpwidgets/gimpcolorselect.c
+++ b/libgimpwidgets/gimpcolorselect.c
@@ -634,17 +634,14 @@ gimp_color_select_set_config (GimpColorSelector *selector,
           g_signal_handlers_disconnect_by_func (select->config,
                                                 gimp_color_select_notify_config,
                                                 select);
-          g_object_unref (select->config);
 
           gimp_color_select_destroy_transform (select);
         }
 
-      select->config = config;
+      g_set_object (&select->config, config);
 
       if (select->config)
         {
-          g_object_ref (select->config);
-
           g_signal_connect (select->config, "notify",
                             G_CALLBACK (gimp_color_select_notify_config),
                             select);
diff --git a/libgimpwidgets/gimppreview.c b/libgimpwidgets/gimppreview.c
index 5f9fd101f4..29b6b6d18f 100644
--- a/libgimpwidgets/gimppreview.c
+++ b/libgimpwidgets/gimppreview.c
@@ -941,16 +941,7 @@ gimp_preview_set_default_cursor (GimpPreview *preview,
 
   priv = GET_PRIVATE (preview);
 
-  if (cursor != priv->default_cursor)
-    {
-      if (priv->default_cursor)
-        g_object_unref (priv->default_cursor);
-
-      if (cursor)
-        g_object_ref (cursor);
-
-      priv->default_cursor = cursor;
-    }
+  g_set_object (&priv->default_cursor, cursor);
 }
 
 GdkCursor *
diff --git a/libgimpwidgets/gimppreviewarea.c b/libgimpwidgets/gimppreviewarea.c
index cff2148d3c..1846cbb2b4 100644
--- a/libgimpwidgets/gimppreviewarea.c
+++ b/libgimpwidgets/gimppreviewarea.c
@@ -1820,17 +1820,14 @@ gimp_preview_area_set_color_config (GimpPreviewArea *area,
           g_signal_handlers_disconnect_by_func (priv->config,
                                                 gimp_preview_area_destroy_transform,
                                                 area);
-          g_object_unref (priv->config);
 
           gimp_preview_area_destroy_transform (area);
         }
 
-      priv->config = config;
+      g_set_object (&priv->config, config);
 
       if (priv->config)
         {
-          g_object_ref (priv->config);
-
           g_signal_connect_swapped (priv->config, "notify",
                                     G_CALLBACK (gimp_preview_area_destroy_transform),
                                     area);
diff --git a/modules/color-selector-cmyk.c b/modules/color-selector-cmyk.c
index 8b2806e260..0135341a04 100644
--- a/modules/color-selector-cmyk.c
+++ b/modules/color-selector-cmyk.c
@@ -270,22 +270,16 @@ colorsel_cmyk_set_config (GimpColorSelector *selector,
   if (config != module->config)
     {
       if (module->config)
-        {
-          g_signal_handlers_disconnect_by_func (module->config,
-                                                colorsel_cmyk_config_changed,
-                                                module);
-          g_object_unref (module->config);
-        }
+        g_signal_handlers_disconnect_by_func (module->config,
+                                              colorsel_cmyk_config_changed,
+                                              module);
 
-      module->config = config;
+      g_set_object (&module->config, config);
 
       if (module->config)
-        {
-          g_object_ref (module->config);
-          g_signal_connect_swapped (module->config, "notify",
-                                    G_CALLBACK (colorsel_cmyk_config_changed),
-                                    module);
-        }
+        g_signal_connect_swapped (module->config, "notify",
+                                  G_CALLBACK (colorsel_cmyk_config_changed),
+                                  module);
 
       colorsel_cmyk_config_changed (module);
     }
diff --git a/modules/color-selector-water.c b/modules/color-selector-water.c
index ab65236c48..785baa179f 100644
--- a/modules/color-selector-water.c
+++ b/modules/color-selector-water.c
@@ -235,17 +235,14 @@ colorsel_water_set_config (GimpColorSelector *selector,
           g_signal_handlers_disconnect_by_func (water->config,
                                                 colorsel_water_destroy_transform,
                                                 water);
-          g_object_unref (water->config);
 
           colorsel_water_destroy_transform (water);
         }
 
-      water->config = config;
+      g_set_object (&water->config, config);
 
       if (water->config)
         {
-          g_object_ref (water->config);
-
           g_signal_connect_swapped (water->config, "notify",
                                     G_CALLBACK (colorsel_water_destroy_transform),
                                     water);
diff --git a/modules/gimpcolorwheel.c b/modules/gimpcolorwheel.c
index 663404b3d7..4f1173dbc3 100644
--- a/modules/gimpcolorwheel.c
+++ b/modules/gimpcolorwheel.c
@@ -1463,17 +1463,14 @@ gimp_color_wheel_set_color_config (GimpColorWheel  *wheel,
           g_signal_handlers_disconnect_by_func (priv->config,
                                                 gimp_color_wheel_destroy_transform,
                                                 wheel);
-          g_object_unref (priv->config);
 
           gimp_color_wheel_destroy_transform (wheel);
         }
 
-      priv->config = config;
+      g_set_object (&priv->config, config);
 
       if (priv->config)
         {
-          g_object_ref (priv->config);
-
           g_signal_connect_swapped (priv->config, "notify",
                                     G_CALLBACK (gimp_color_wheel_destroy_transform),
                                     wheel);



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