[gnome-control-center] printers: Return a GPtrArray instead of GList



commit 181a496a4ebeab61ae3bcfa2a5372f43b77138b4
Author: Robert Ancell <robert ancell canonical com>
Date:   Tue Dec 17 15:32:57 2019 +1300

    printers: Return a GPtrArray instead of GList
    
    This makes the memory able to be managed with g_autoptr.
    This fixes a memory leak where the job list was leaked if the operation was cancelled.

 panels/printers/pp-jobs-dialog.c   | 33 ++++++++++++++++-----------------
 panels/printers/pp-printer-entry.c | 19 ++++++++-----------
 panels/printers/pp-printer.c       |  9 +++++----
 panels/printers/pp-printer.h       |  2 +-
 4 files changed, 30 insertions(+), 33 deletions(-)
---
diff --git a/panels/printers/pp-jobs-dialog.c b/panels/printers/pp-jobs-dialog.c
index 4aa0452e5..7707b43c6 100644
--- a/panels/printers/pp-jobs-dialog.c
+++ b/panels/printers/pp-jobs-dialog.c
@@ -317,17 +317,18 @@ update_jobs_list_cb (GObject      *source_object,
                      GAsyncResult *result,
                      gpointer      user_data)
 {
-  PpJobsDialog     *self = user_data;
-  PpPrinter        *printer = PP_PRINTER (source_object);
-  GtkWidget        *clear_all_button;
-  GtkWidget        *infobar;
-  GtkWidget        *label;
-  GtkStack         *stack;
-  g_autoptr(GError) error = NULL;
-  GList            *jobs, *l;
-  PpJob            *job;
-  gchar           **auth_info_required = NULL;
-  gint              num_of_jobs, num_of_auth_jobs = 0;
+  PpJobsDialog        *self = user_data;
+  PpPrinter           *printer = PP_PRINTER (source_object);
+  GtkWidget           *clear_all_button;
+  GtkWidget           *infobar;
+  GtkWidget           *label;
+  GtkStack            *stack;
+  g_autoptr(GError)    error = NULL;
+  g_autoptr(GPtrArray) jobs;
+  PpJob               *job;
+  gchar              **auth_info_required = NULL;
+  gint                 num_of_auth_jobs = 0;
+  guint                i;
 
   g_list_store_remove_all (self->store);
 
@@ -345,8 +346,7 @@ update_jobs_list_cb (GObject      *source_object,
       return;
     }
 
-  num_of_jobs = g_list_length (jobs);
-  if (num_of_jobs > 0)
+  if (jobs->len > 0)
     {
       gtk_widget_set_sensitive (clear_all_button, TRUE);
       gtk_stack_set_visible_child_name (stack, "list-jobs-page");
@@ -357,11 +357,11 @@ update_jobs_list_cb (GObject      *source_object,
       gtk_stack_set_visible_child_name (stack, "no-jobs-page");
     }
 
-  for (l = jobs; l != NULL; l = l->next)
+  for (i = 0; i < jobs->len; i++)
     {
-      job = PP_JOB (l->data);
+      job = PP_JOB (g_ptr_array_index (jobs, i));
 
-      g_list_store_append (self->store, job);
+      g_list_store_append (self->store, g_object_ref (job));
 
       g_object_get (G_OBJECT (job),
                     "auth-info-required", &auth_info_required,
@@ -399,7 +399,6 @@ update_jobs_list_cb (GObject      *source_object,
 
   authenticate_popover_update (self);
 
-  g_list_free (jobs);
   g_clear_object (&self->get_jobs_cancellable);
 
   if (!self->jobs_filled)
diff --git a/panels/printers/pp-printer-entry.c b/panels/printers/pp-printer-entry.c
index 51ed7664e..7fcc33f06 100644
--- a/panels/printers/pp-printer-entry.c
+++ b/panels/printers/pp-printer-entry.c
@@ -568,18 +568,15 @@ get_jobs_cb (GObject      *source_object,
              GAsyncResult *result,
              gpointer      user_data)
 {
-  PpPrinterEntry   *self = user_data;
-  PpPrinter        *printer = PP_PRINTER (source_object);
-  g_autoptr(GError) error = NULL;
-  GList            *jobs;
-  g_autofree gchar *button_label = NULL;
-  gint              num_jobs;
+  PpPrinterEntry      *self = user_data;
+  PpPrinter           *printer = PP_PRINTER (source_object);
+  g_autoptr(GError)    error = NULL;
+  g_autoptr(GPtrArray) jobs = NULL;
+  g_autofree gchar    *button_label = NULL;
 
   jobs = pp_printer_get_jobs_finish (printer, result, &error);
-  num_jobs = g_list_length (jobs);
 
   g_object_unref (source_object);
-  g_list_free_full (jobs, (GDestroyNotify) g_object_unref);
 
   if (error != NULL)
     {
@@ -591,7 +588,7 @@ get_jobs_cb (GObject      *source_object,
       return;
     }
 
-  if (num_jobs == 0)
+  if (jobs->len == 0)
     {
       /* Translators: This is the label of the button that opens the Jobs Dialog. */
       button_label = g_strdup (_("No Active Jobs"));
@@ -599,11 +596,11 @@ get_jobs_cb (GObject      *source_object,
   else
     {
       /* Translators: This is the label of the button that opens the Jobs Dialog. */
-      button_label = g_strdup_printf (ngettext ("%u Job", "%u Jobs", num_jobs), num_jobs);
+      button_label = g_strdup_printf (ngettext ("%u Job", "%u Jobs", jobs->len), jobs->len);
     }
 
   gtk_button_set_label (GTK_BUTTON (self->show_jobs_dialog_button), button_label);
-  gtk_widget_set_sensitive (self->show_jobs_dialog_button, num_jobs > 0);
+  gtk_widget_set_sensitive (self->show_jobs_dialog_button, jobs->len > 0);
 
   if (self->pp_jobs_dialog != NULL)
     {
diff --git a/panels/printers/pp-printer.c b/panels/printers/pp-printer.c
index cd098982d..817da6058 100644
--- a/panels/printers/pp-printer.c
+++ b/panels/printers/pp-printer.c
@@ -301,7 +301,7 @@ get_jobs_thread (GTask        *task,
   ipp_t            *printer_response;
   gchar           **auth_info_required = NULL;
   g_autofree gchar *printer_name = NULL;
-  GList            *list = NULL;
+  g_autoptr(GPtrArray) array = NULL;
   gint              num_jobs;
   gint              i, j;
 
@@ -312,6 +312,7 @@ get_jobs_thread (GTask        *task,
                           get_jobs_data->myjobs ? 1 : 0,
                           get_jobs_data->which_jobs);
 
+  array = g_ptr_array_new_with_free_func (g_object_unref);
   for (i = 0; i < num_jobs; i++)
     {
       auth_info_is_required = FALSE;
@@ -374,7 +375,7 @@ get_jobs_thread (GTask        *task,
                           "auth-info-required", auth_info_is_required ? auth_info_required : NULL,
                           NULL);
 
-      list = g_list_append (list, job);
+      g_ptr_array_add (array, job);
     }
 
   g_strfreev (auth_info_required);
@@ -382,7 +383,7 @@ get_jobs_thread (GTask        *task,
 
   if (g_task_set_return_on_cancel (task, FALSE))
     {
-      g_task_return_pointer (task, list, (GDestroyNotify) g_list_free);
+      g_task_return_pointer (task, g_steal_pointer (&array), (GDestroyNotify) g_ptr_array_unref);
     }
 }
 
@@ -408,7 +409,7 @@ pp_printer_get_jobs_async (PpPrinter           *self,
   g_object_unref (task);
 }
 
-GList *
+GPtrArray *
 pp_printer_get_jobs_finish (PpPrinter          *self,
                             GAsyncResult       *res,
                             GError            **error)
diff --git a/panels/printers/pp-printer.h b/panels/printers/pp-printer.h
index 5c6d6cc67..5fac607a7 100644
--- a/panels/printers/pp-printer.h
+++ b/panels/printers/pp-printer.h
@@ -60,7 +60,7 @@ void         pp_printer_get_jobs_async (PpPrinter           *printer,
                                         GAsyncReadyCallback  callback,
                                         gpointer             user_data);
 
-GList       *pp_printer_get_jobs_finish (PpPrinter          *printer,
+GPtrArray   *pp_printer_get_jobs_finish (PpPrinter          *printer,
                                          GAsyncResult       *res,
                                          GError            **error);
 


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