[gnome-control-center] printers: Simplify finalize/dispose methods



commit 85604ab4f066e47f9e46f5c62daeb02f16bae7bb
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Dec 6 09:28:35 2018 +1300

    printers: Simplify finalize/dispose methods
    
    Use g_clear_* functions to simplify code and ensure values are set to NULL after freeing.

 panels/printers/cc-printers-panel.c     | 91 ++++++++-------------------------
 panels/printers/pp-ipp-option-widget.c  | 41 +++------------
 panels/printers/pp-job.c                |  4 +-
 panels/printers/pp-new-printer-dialog.c | 51 +++++++-----------
 panels/printers/pp-ppd-option-widget.c  | 47 ++++-------------
 panels/printers/pp-printer-entry.c      |  8 ++-
 panels/printers/pp-printer.c            |  2 +-
 panels/printers/pp-samba.c              | 10 ++--
 8 files changed, 70 insertions(+), 184 deletions(-)
---
diff --git a/panels/printers/cc-printers-panel.c b/panels/printers/cc-printers-panel.c
index faa833910..5946cb5aa 100644
--- a/panels/printers/cc-printers-panel.c
+++ b/panels/printers/cc-printers-panel.c
@@ -279,89 +279,42 @@ cc_printers_panel_dispose (GObject *object)
 {
   CcPrintersPanel *self = CC_PRINTERS_PANEL (object);
 
-  if (self->pp_new_printer_dialog)
-    g_clear_object (&self->pp_new_printer_dialog);
-
-  free_dests (CC_PRINTERS_PANEL (object));
-
-  g_clear_pointer (&self->new_printer_name, g_free);
-  g_clear_pointer (&self->new_printer_location, g_free);
-  g_clear_pointer (&self->new_printer_make_and_model, g_free);
-
-  g_clear_pointer (&self->renamed_printer_name, g_free);
-  g_clear_pointer (&self->old_printer_name, g_free);
-
-  if (self->builder)
-    {
-      g_object_unref (self->builder);
-      self->builder = NULL;
-    }
-
-  if (self->lockdown_settings)
-    {
-      g_object_unref (self->lockdown_settings);
-      self->lockdown_settings = NULL;
-    }
-
-  if (self->permission)
-    {
-      g_object_unref (self->permission);
-      self->permission = NULL;
-    }
-
   g_cancellable_cancel (self->subscription_renew_cancellable);
-  g_clear_object (&self->subscription_renew_cancellable);
-
   g_cancellable_cancel (self->actualize_printers_list_cancellable);
-  g_clear_object (&self->actualize_printers_list_cancellable);
-
-  detach_from_cups_notifier (CC_PRINTERS_PANEL (object));
-
   g_cancellable_cancel (self->cups_status_check_cancellable);
-  g_clear_object (&self->cups_status_check_cancellable);
-
-  if (self->cups_status_check_id > 0)
-    {
-      g_source_remove (self->cups_status_check_id);
-      self->cups_status_check_id = 0;
-    }
+  g_cancellable_cancel (self->get_all_ppds_cancellable);
 
-  if (self->remove_printer_timeout_id > 0)
-    {
-      g_source_remove (self->remove_printer_timeout_id);
-      self->remove_printer_timeout_id = 0;
-    }
-
-  if (self->all_ppds_list)
-    {
-      ppd_list_free (self->all_ppds_list);
-      self->all_ppds_list = NULL;
-    }
-
-  if (self->get_all_ppds_cancellable)
-    {
-      g_cancellable_cancel (self->get_all_ppds_cancellable);
-      g_object_unref (self->get_all_ppds_cancellable);
-      self->get_all_ppds_cancellable = NULL;
-    }
+  detach_from_cups_notifier (CC_PRINTERS_PANEL (object));
 
   if (self->deleted_printer_name != NULL)
     {
-      PpPrinter *printer;
-
-      printer = pp_printer_new (self->deleted_printer_name);
-      g_clear_pointer (&self->deleted_printer_name, g_free);
-
+      PpPrinter *printer = pp_printer_new (self->deleted_printer_name);
       pp_printer_delete_async (printer,
                                NULL,
                                printer_removed_cb,
                                NULL);
     }
 
-  if (self->action != NULL)
-    g_variant_unref (self->action);
-
+  g_clear_object (&self->pp_new_printer_dialog);
+  g_clear_pointer (&self->new_printer_name, g_free);
+  g_clear_pointer (&self->new_printer_location, g_free);
+  g_clear_pointer (&self->new_printer_make_and_model, g_free);
+  g_clear_pointer (&self->renamed_printer_name, g_free);
+  g_clear_pointer (&self->old_printer_name, g_free);
+  g_clear_object (&self->builder);
+  g_clear_object (&self->lockdown_settings);
+  g_clear_object (&self->permission);
+  g_clear_object (&self->subscription_renew_cancellable);
+  g_clear_object (&self->actualize_printers_list_cancellable);
+  g_clear_object (&self->cups_status_check_cancellable);
+  g_clear_handle_id (&self->cups_status_check_id, g_source_remove);
+  g_clear_handle_id (&self->remove_printer_timeout_id, g_source_remove);
+  g_clear_object (&self->get_all_ppds_cancellable);
+  g_clear_pointer (&self->deleted_printer_name, g_free);
+  g_clear_pointer (&self->action, g_variant_unref);
   g_clear_pointer (&self->printer_entries, g_hash_table_destroy);
+  g_clear_pointer (&self->all_ppds_list, ppd_list_free);
+  free_dests (self);
 
   G_OBJECT_CLASS (cc_printers_panel_parent_class)->dispose (object);
 }
diff --git a/panels/printers/pp-ipp-option-widget.c b/panels/printers/pp-ipp-option-widget.c
index f9c7f0bc6..5ae738463 100644
--- a/panels/printers/pp-ipp-option-widget.c
+++ b/panels/printers/pp-ipp-option-widget.c
@@ -128,41 +128,14 @@ pp_ipp_option_widget_finalize (GObject *object)
 {
   PpIPPOptionWidget *widget = PP_IPP_OPTION_WIDGET (object);
 
-  if (widget->option_name)
-    {
-      g_free (widget->option_name);
-      widget->option_name = NULL;
-    }
-
-  if (widget->printer_name)
-    {
-      g_free (widget->printer_name);
-      widget->printer_name = NULL;
-    }
-
-  if (widget->option_supported)
-    {
-      ipp_attribute_free (widget->option_supported);
-      widget->option_supported = NULL;
-    }
-
-  if (widget->option_default)
-    {
-      ipp_attribute_free (widget->option_default);
-      widget->option_default = NULL;
-    }
+  g_cancellable_cancel (widget->cancellable);
 
-  if (widget->ipp_attribute)
-    {
-      g_hash_table_unref (widget->ipp_attribute);
-      widget->ipp_attribute = NULL;
-    }
- 
-  if (widget->cancellable)
-    {
-      g_cancellable_cancel (widget->cancellable);
-      g_object_unref (widget->cancellable);
-    }
+  g_clear_pointer (&widget->option_name, g_free);
+  g_clear_pointer (&widget->printer_name, g_free);
+  g_clear_pointer (&widget->option_supported, ipp_attribute_free);
+  g_clear_pointer (&widget->option_default, ipp_attribute_free);
+  g_clear_pointer (&widget->ipp_attribute, g_hash_table_unref);
+  g_clear_object (&widget->cancellable);
 
   G_OBJECT_CLASS (pp_ipp_option_widget_parent_class)->finalize (object);
 }
diff --git a/panels/printers/pp-job.c b/panels/printers/pp-job.c
index b80320d54..90f7e3b4b 100644
--- a/panels/printers/pp-job.c
+++ b/panels/printers/pp-job.c
@@ -236,8 +236,8 @@ pp_job_finalize (GObject *object)
 {
   PpJob *self = PP_JOB (object);
 
-  g_free (self->title);
-  g_strfreev (self->auth_info_required);
+  g_clear_pointer (&self->title, g_free);
+  g_clear_pointer (&self->auth_info_required, g_strfreev);
 
   G_OBJECT_CLASS (pp_job_parent_class)->finalize (object);
 }
diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c
index d76f62f7d..e7335b913 100644
--- a/panels/printers/pp-new-printer-dialog.c
+++ b/panels/printers/pp-new-printer-dialog.c
@@ -561,41 +561,33 @@ pp_new_printer_dialog_init (PpNewPrinterDialog *dialog)
   populate_devices_list (dialog);
 }
 
+static void
+free_devices_list (GList *devices)
+{
+  g_list_free_full (devices, (GDestroyNotify) g_object_unref);
+}
+
 static void
 pp_new_printer_dialog_finalize (GObject *object)
 {
   PpNewPrinterDialog *dialog = PP_NEW_PRINTER_DIALOG (object);
 
+  g_cancellable_cancel (dialog->remote_host_cancellable);
+  g_cancellable_cancel (dialog->cancellable);
+
   dialog->text_renderer = NULL;
   dialog->icon_renderer = NULL;
 
-  if (dialog->host_search_timeout_id != 0)
-    {
-      g_source_remove (dialog->host_search_timeout_id);
-      dialog->host_search_timeout_id = 0;
-    }
-
-  if (dialog->remote_host_cancellable)
-    {
-      g_cancellable_cancel (dialog->remote_host_cancellable);
-      g_clear_object (&dialog->remote_host_cancellable);
-    }
-
-  if (dialog->cancellable)
-    {
-      g_cancellable_cancel (dialog->cancellable);
-      g_clear_object (&dialog->cancellable);
-    }
-
+  g_clear_handle_id (&dialog->host_search_timeout_id, g_source_remove);
+  g_clear_object (&dialog->remote_host_cancellable);
+  g_clear_object (&dialog->cancellable);
   g_clear_pointer (&dialog->dialog, gtk_widget_destroy);
-
   g_clear_pointer (&dialog->list, ppd_list_free);
-
-  if (dialog->builder)
-    g_clear_object (&dialog->builder);
-
-  g_list_free_full (dialog->local_cups_devices, (GDestroyNotify) g_object_unref);
-  dialog->local_cups_devices = NULL;
+  g_clear_object (&dialog->builder);
+  g_clear_pointer (&dialog->local_cups_devices, free_devices_list);
+  g_clear_object (&dialog->local_printer_icon);
+  g_clear_object (&dialog->remote_printer_icon);
+  g_clear_object (&dialog->authenticated_server_icon);
 
   if (dialog->num_of_dests > 0)
     {
@@ -604,10 +596,6 @@ pp_new_printer_dialog_finalize (GObject *object)
       dialog->dests = NULL;
     }
 
-  g_clear_object (&dialog->local_printer_icon);
-  g_clear_object (&dialog->remote_printer_icon);
-  g_clear_object (&dialog->authenticated_server_icon);
-
   G_OBJECT_CLASS (pp_new_printer_dialog_parent_class)->finalize (object);
 }
 
@@ -928,8 +916,7 @@ group_physical_devices_cb (gchar    ***device_uris,
     {
       for (iter = dialog->local_cups_devices; iter != NULL; iter = iter->next)
         set_device (dialog, (PpPrintDevice *) iter->data, NULL);
-      g_list_free_full (dialog->local_cups_devices, g_object_unref);
-      dialog->local_cups_devices = NULL;
+      g_clear_pointer (&dialog->local_cups_devices, free_devices_list);
     }
 
   update_dialog_state (dialog);
@@ -1158,7 +1145,7 @@ get_cups_devices_cb (GList    *devices,
         }
     }
 
-  g_list_free_full (devices, (GDestroyNotify) g_object_unref);
+  free_devices_list (devices);
 }
 
 static void
diff --git a/panels/printers/pp-ppd-option-widget.c b/panels/printers/pp-ppd-option-widget.c
index a592a9b1e..6beddc089 100644
--- a/panels/printers/pp-ppd-option-widget.c
+++ b/panels/printers/pp-ppd-option-widget.c
@@ -167,45 +167,20 @@ pp_ppd_option_widget_finalize (GObject *object)
 {
   PpPPDOptionWidget        *widget = PP_PPD_OPTION_WIDGET (object);
 
+  g_cancellable_cancel (widget->cancellable);
+  if (widget->ppd_filename)
+    g_unlink (widget->ppd_filename);
+
+  g_clear_pointer (&widget->option, cups_option_free);
+  g_clear_pointer (&widget->printer_name, g_free);
+  g_clear_pointer (&widget->option_name, g_free);
   if (widget->destination)
     {
-      if (widget->option)
-        {
-          cups_option_free (widget->option);
-          widget->option = NULL;
-        }
-
-      if (widget->printer_name)
-        {
-          g_free (widget->printer_name);
-          widget->printer_name = NULL;
-        }
-
-      if (widget->option_name)
-        {
-          g_free (widget->option_name);
-          widget->option_name = NULL;
-        }
-
-      if (widget->destination)
-        {
-          cupsFreeDests (1, widget->destination);
-          widget->destination = NULL;
-        }
-
-      if (widget->ppd_filename)
-        {
-          g_unlink (widget->ppd_filename);
-          g_free (widget->ppd_filename);
-          widget->ppd_filename = NULL;
-        }
-
-      if (widget->cancellable)
-        {
-          g_cancellable_cancel (widget->cancellable);
-          g_object_unref (widget->cancellable);
-        }
+      cupsFreeDests (1, widget->destination);
+      widget->destination = NULL;
     }
+  g_clear_pointer (&widget->ppd_filename, g_free);
+  g_clear_object (&widget->cancellable);
 
   G_OBJECT_CLASS (pp_ppd_option_widget_parent_class)->finalize (object);
 }
diff --git a/panels/printers/pp-printer-entry.c b/panels/printers/pp-printer-entry.c
index b534af884..f1649ac30 100644
--- a/panels/printers/pp-printer-entry.c
+++ b/panels/printers/pp-printer-entry.c
@@ -1001,6 +1001,9 @@ pp_printer_entry_dispose (GObject *object)
 {
   PpPrinterEntry *self = PP_PRINTER_ENTRY (object);
 
+  g_cancellable_cancel (self->get_jobs_cancellable);
+  g_cancellable_cancel (self->check_clean_heads_cancellable);
+
   if (self->pp_details_dialog != NULL)
     {
       g_signal_handlers_disconnect_by_data (self->pp_details_dialog, self);
@@ -1018,13 +1021,8 @@ pp_printer_entry_dispose (GObject *object)
   g_clear_pointer (&self->printer_make_and_model, g_free);
   g_clear_pointer (&self->printer_hostname, g_free);
   g_clear_pointer (&self->inklevel, ink_level_data_free);
-
-  g_cancellable_cancel (self->get_jobs_cancellable);
   g_clear_object (&self->get_jobs_cancellable);
-
-  g_cancellable_cancel (self->check_clean_heads_cancellable);
   g_clear_object (&self->check_clean_heads_cancellable);
-
   g_clear_object (&self->clean_command);
 
   G_OBJECT_CLASS (pp_printer_entry_parent_class)->dispose (object);
diff --git a/panels/printers/pp-printer.c b/panels/printers/pp-printer.c
index 7063e037a..adf0d5312 100644
--- a/panels/printers/pp-printer.c
+++ b/panels/printers/pp-printer.c
@@ -45,7 +45,7 @@ pp_printer_dispose (GObject *object)
 {
   PpPrinter *self = PP_PRINTER (object);
 
-  g_free (self->printer_name);
+  g_clear_pointer (&self->printer_name, g_free);
 
   G_OBJECT_CLASS (pp_printer_parent_class)->dispose (object);
 }
diff --git a/panels/printers/pp-samba.c b/panels/printers/pp-samba.c
index f79fceb36..8f37a8ad2 100644
--- a/panels/printers/pp-samba.c
+++ b/panels/printers/pp-samba.c
@@ -48,11 +48,11 @@ pp_samba_finalize (GObject *object)
 {
   PpSamba *self = PP_SAMBA (object);
 
-  g_free (self->server);
-  g_free (self->share);
-  g_free (self->workgroup);
-  g_free (self->username);
-  g_free (self->password);
+  g_clear_pointer (&self->server, g_free);
+  g_clear_pointer (&self->share, g_free);
+  g_clear_pointer (&self->workgroup, g_free);
+  g_clear_pointer (&self->username, g_free);
+  g_clear_pointer (&self->password, g_free);
 
   G_OBJECT_CLASS (pp_samba_parent_class)->finalize (object);
 }


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