[gnome-control-center] printers: Add pp_job_new and associated methods.



commit 2def2718248cdc9d49f3d1eed289b898d6964944
Author: Robert Ancell <robert ancell canonical com>
Date:   Thu Oct 15 09:18:34 2020 +1300

    printers: Add pp_job_new and associated methods.
    
    The previous code was leaking a string (job title), used an integer pointer
    instead of an integer for an id and requited a lot of memory management for a
    string array. There was a lot of boilerplate code required for property
    management which was not being used much. This is why type safe methods are a
    much better idea than GObject properties. :)

 panels/printers/cc-printers-panel.c |   2 +-
 panels/printers/pp-job.c            | 143 ++++++++++--------------------------
 panels/printers/pp-job.h            |  12 ++-
 panels/printers/pp-jobs-dialog.c    |  50 +++----------
 panels/printers/pp-printer.c        |   7 +-
 5 files changed, 62 insertions(+), 152 deletions(-)
---
diff --git a/panels/printers/cc-printers-panel.c b/panels/printers/cc-printers-panel.c
index 49ca0d1ba..4ac1dbf4f 100644
--- a/panels/printers/cc-printers-panel.c
+++ b/panels/printers/cc-printers-panel.c
@@ -461,7 +461,7 @@ on_cups_notification (GDBusConnection *connection,
     {
       g_autoptr(PpJob) job = NULL;
 
-      job = g_object_new (PP_TYPE_JOB, "id", job_id, NULL);
+      job = pp_job_new (job_id, NULL, 0, NULL);
       pp_job_get_attributes_async (job,
                                    requested_attrs,
                                    cc_panel_get_cancellable (CC_PANEL (self)),
diff --git a/panels/printers/pp-job.c b/panels/printers/pp-job.c
index bd567e03c..9351ac773 100644
--- a/panels/printers/pp-job.c
+++ b/panels/printers/pp-job.c
@@ -50,23 +50,11 @@ struct _PpJob
   gint    id;
   gchar  *title;
   gint    state;
-  gchar **auth_info_required;
+  GStrv   auth_info_required;
 };
 
 G_DEFINE_TYPE (PpJob, pp_job, G_TYPE_OBJECT)
 
-enum
-{
-  PROP_0,
-  PROP_ID,
-  PROP_TITLE,
-  PROP_STATE,
-  PROP_AUTH_INFO_REQUIRED,
-  LAST_PROPERTY
-};
-
-static GParamSpec *properties[LAST_PROPERTY];
-
 static void
 pp_job_cancel_purge_async_dbus_cb (GObject      *source_object,
                                    GAsyncResult *res,
@@ -79,15 +67,46 @@ pp_job_cancel_purge_async_dbus_cb (GObject      *source_object,
                                           NULL);
 }
 
+PpJob *
+pp_job_new (gint id, const gchar *title, gint state, GStrv auth_info_required)
+{
+   PpJob *job = g_object_new (pp_job_get_type (), NULL);
+
+   job->id = id;
+   job->title = g_strdup (title);
+   job->state = state;
+   job->auth_info_required = g_strdupv (auth_info_required);
+
+   return job;
+}
+
+const gchar *
+pp_job_get_title (PpJob *self)
+{
+   g_return_val_if_fail (PP_IS_JOB(self), NULL);
+   return self->title;
+}
+
+gint
+pp_job_get_state (PpJob *self)
+{
+   g_return_val_if_fail (PP_IS_JOB(self), -1);
+   return self->state;
+}
+
+GStrv
+pp_job_get_auth_info_required (PpJob *self)
+{
+   g_return_val_if_fail (PP_IS_JOB(self), NULL);
+   return self->auth_info_required;
+}
+
 void
 pp_job_cancel_purge_async (PpJob        *self,
                            gboolean      job_purge)
 {
   g_autoptr(GDBusConnection) bus = NULL;
   g_autoptr(GError) error = NULL;
-  gint            *job_id;
-
-  g_object_get (self, "id", &job_id, NULL);
 
   bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
   if (!bus)
@@ -102,7 +121,7 @@ pp_job_cancel_purge_async (PpJob        *self,
                           MECHANISM_BUS,
                           "JobCancelPurge",
                           g_variant_new ("(ib)",
-                                         job_id,
+                                         self->id,
                                          job_purge),
                           G_VARIANT_TYPE ("(s)"),
                           G_DBUS_CALL_FLAGS_NONE,
@@ -130,9 +149,6 @@ pp_job_set_hold_until_async (PpJob        *self,
 {
   g_autoptr(GDBusConnection) bus = NULL;
   g_autoptr(GError) error = NULL;
-  gint             *job_id;
-
-  g_object_get (self, "id", &job_id, NULL);
 
   bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
   if (!bus)
@@ -147,7 +163,7 @@ pp_job_set_hold_until_async (PpJob        *self,
                           MECHANISM_BUS,
                           "JobSetHoldUntil",
                           g_variant_new ("(is)",
-                                         job_id,
+                                         self->id,
                                          job_hold_until),
                           G_VARIANT_TYPE ("(s)"),
                           G_DBUS_CALL_FLAGS_NONE,
@@ -162,63 +178,6 @@ pp_job_init (PpJob *obj)
 {
 }
 
-static void
-pp_job_get_property (GObject    *object,
-                     guint       property_id,
-                     GValue     *value,
-                     GParamSpec *pspec)
-{
-  PpJob *self = PP_JOB (object);
-
-  switch (property_id)
-    {
-      case PROP_ID:
-        g_value_set_int (value, self->id);
-        break;
-      case PROP_TITLE:
-        g_value_set_string (value, self->title);
-        break;
-      case PROP_STATE:
-        g_value_set_int (value, self->state);
-        break;
-      case PROP_AUTH_INFO_REQUIRED:
-        g_value_set_pointer (value, g_strdupv (self->auth_info_required));
-        break;
-      default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-        break;
-    }
-}
-
-static void
-pp_job_set_property (GObject      *object,
-                     guint         property_id,
-                     const GValue *value,
-                     GParamSpec   *pspec)
-{
-  PpJob *self = PP_JOB (object);
-
-  switch (property_id)
-    {
-      case PROP_ID:
-        self->id = g_value_get_int (value);
-        break;
-      case PROP_TITLE:
-        g_free (self->title);
-        self->title = g_value_dup_string (value);
-        break;
-      case PROP_STATE:
-        self->state = g_value_get_int (value);
-        break;
-      case PROP_AUTH_INFO_REQUIRED:
-        self->auth_info_required = g_strdupv (g_value_get_pointer (value));
-        break;
-      default:
-        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-        break;
-    }
-}
-
 static void
 pp_job_finalize (GObject *object)
 {
@@ -235,35 +194,7 @@ pp_job_class_init (PpJobClass *class)
 {
   GObjectClass *object_class = G_OBJECT_CLASS (class);
 
-  object_class->get_property = pp_job_get_property;
-  object_class->set_property = pp_job_set_property;
   object_class->finalize = pp_job_finalize;
-
-  properties[PROP_ID] = g_param_spec_int ("id",
-                                          "Id",
-                                          "Job id",
-                                          0,
-                                          G_MAXINT,
-                                          0,
-                                          G_PARAM_READWRITE);
-  properties[PROP_TITLE] = g_param_spec_string ("title",
-                                                "Title",
-                                                "Title of this print job",
-                                                NULL,
-                                                G_PARAM_READWRITE);
-  properties[PROP_STATE] = g_param_spec_int ("state",
-                                             "State",
-                                             "State of this print job (Paused, Completed, Cancelled,...)",
-                                             0,
-                                             G_MAXINT,
-                                             0,
-                                             G_PARAM_READWRITE);
-  properties[PROP_AUTH_INFO_REQUIRED] = g_param_spec_pointer ("auth-info-required",
-                                                              "Authentication info required",
-                                                              "Which authentication info is required for 
this print job",
-                                                              G_PARAM_READWRITE);
-
-  g_object_class_install_properties (object_class, LAST_PROPERTY, properties);
 }
 
 static void
diff --git a/panels/printers/pp-job.h b/panels/printers/pp-job.h
index fb4105fce..e99abdf05 100644
--- a/panels/printers/pp-job.h
+++ b/panels/printers/pp-job.h
@@ -27,9 +27,19 @@
 
 G_BEGIN_DECLS
 
-#define PP_TYPE_JOB (pp_job_get_type ())
 G_DECLARE_FINAL_TYPE (PpJob, pp_job, PP, JOB, GObject)
 
+PpJob         *pp_job_new                        (gint                  id,
+                                                  const gchar          *title,
+                                                  gint                  state,
+                                                  GStrv                 auth_info_required);
+
+const gchar   *pp_job_get_title                  (PpJob                *job);
+
+gint           pp_job_get_state                  (PpJob                *job);
+
+GStrv          pp_job_get_auth_info_required     (PpJob                *job);
+
 void           pp_job_set_hold_until_async       (PpJob                *job,
                                                   const gchar          *job_hold_until);
 
diff --git a/panels/printers/pp-jobs-dialog.c b/panels/printers/pp-jobs-dialog.c
index 08e2dc8ce..d629f9803 100644
--- a/panels/printers/pp-jobs-dialog.c
+++ b/panels/printers/pp-jobs-dialog.c
@@ -185,14 +185,10 @@ static void
 job_pause_cb (GtkButton *button,
               PpJob     *job)
 {
-  gint job_state;
-
-  g_object_get (job, "state", &job_state, NULL);
-
-  pp_job_set_hold_until_async (job, job_state == IPP_JOB_HELD ? "no-hold" : "indefinite");
+  pp_job_set_hold_until_async (job, pp_job_get_state (job) == IPP_JOB_HELD ? "no-hold" : "indefinite");
 
   gtk_button_set_image (button,
-                        gtk_image_new_from_icon_name (job_state == IPP_JOB_HELD ?
+                        gtk_image_new_from_icon_name (pp_job_get_state (job) == IPP_JOB_HELD ?
                                                       "media-playback-pause-symbolic" : 
"media-playback-start-symbolic",
                                                       GTK_ICON_SIZE_SMALL_TOOLBAR));
 }
@@ -204,25 +200,16 @@ create_listbox_row (gpointer item,
   GtkWidget  *widget;
   GtkWidget  *box;
   PpJob      *job = (PpJob *)item;
-  gchar     **auth_info_required;
-  gchar      *title;
-  gchar      *state_string = NULL;
-  gint        job_state;
-
-  g_object_get (job,
-                "title", &title,
-                "state", &job_state,
-                "auth-info-required", &auth_info_required,
-                NULL);
-
-  switch (job_state)
+  g_autofree gchar *state_string = NULL;
+
+  switch (pp_job_get_state (job))
     {
       case IPP_JOB_PENDING:
         /* Translators: Job's state (job is waiting to be printed) */
         state_string = g_strdup (C_("print job", "Pending"));
         break;
       case IPP_JOB_HELD:
-        if (auth_info_required == NULL)
+        if (pp_job_get_auth_info_required (job) == NULL)
           {
             /* Translators: Job's state (job is held for printing) */
             state_string = g_strdup (C_("print job", "Paused"));
@@ -259,7 +246,7 @@ create_listbox_row (gpointer item,
   g_object_set (box, "margin", 6, NULL);
   gtk_container_set_border_width (GTK_CONTAINER (box), 2);
 
-  widget = gtk_label_new (title);
+  widget = gtk_label_new (pp_job_get_title (job));
   gtk_label_set_max_width_chars (GTK_LABEL (widget), 40);
   gtk_label_set_ellipsize (GTK_LABEL (widget), PANGO_ELLIPSIZE_END);
   gtk_widget_set_halign (widget, GTK_ALIGN_START);
@@ -272,10 +259,10 @@ create_listbox_row (gpointer item,
   gtk_widget_set_margin_start (widget, 64);
   gtk_box_pack_start (GTK_BOX (box), widget, FALSE, FALSE, 10);
 
-  widget = gtk_button_new_from_icon_name (job_state == IPP_JOB_HELD ? "media-playback-start-symbolic" : 
"media-playback-pause-symbolic",
+  widget = gtk_button_new_from_icon_name (pp_job_get_state (job) == IPP_JOB_HELD ? 
"media-playback-start-symbolic" : "media-playback-pause-symbolic",
                                           GTK_ICON_SIZE_SMALL_TOOLBAR);
   g_signal_connect (widget, "clicked", G_CALLBACK (job_pause_cb), item);
-  gtk_widget_set_sensitive (widget, auth_info_required == NULL);
+  gtk_widget_set_sensitive (widget, pp_job_get_auth_info_required (job) == NULL);
   gtk_box_pack_start (GTK_BOX (box), widget, FALSE, FALSE, 4);
 
   widget = gtk_button_new_from_icon_name ("edit-delete-symbolic",
@@ -305,7 +292,6 @@ update_jobs_list_cb (GObject      *source_object,
   g_autoptr(GError)    error = NULL;
   g_autoptr(GPtrArray) jobs;
   PpJob               *job;
-  gchar              **auth_info_required = NULL;
   gint                 num_of_auth_jobs = 0;
   guint                i;
 
@@ -339,19 +325,12 @@ update_jobs_list_cb (GObject      *source_object,
 
       g_list_store_append (self->store, g_object_ref (job));
 
-      g_object_get (G_OBJECT (job),
-                    "auth-info-required", &auth_info_required,
-                    NULL);
-      if (auth_info_required != NULL)
+      if (pp_job_get_auth_info_required (job) != NULL)
         {
           num_of_auth_jobs++;
 
           if (self->actual_auth_info_required == NULL)
-            self->actual_auth_info_required = auth_info_required;
-          else
-            g_strfreev (auth_info_required);
-
-          auth_info_required = NULL;
+            self->actual_auth_info_required = g_strdupv (pp_job_get_auth_info_required (job));
         }
     }
 
@@ -452,7 +431,6 @@ static void
 authenticate_button_clicked (PpJobsDialog *self)
 {
   PpJob        *job;
-  gchar       **auth_info_required = NULL;
   gchar       **auth_info;
   guint         num_items;
   gint          i;
@@ -473,13 +451,9 @@ authenticate_button_clicked (PpJobsDialog *self)
     {
       job = PP_JOB (g_list_model_get_item (G_LIST_MODEL (self->store), i));
 
-      g_object_get (job, "auth-info-required", &auth_info_required, NULL);
-      if (auth_info_required != NULL)
+      if (pp_job_get_auth_info_required (job) != NULL)
         {
           pp_job_authenticate_async (job, auth_info, NULL, pp_job_authenticate_cb, self);
-
-          g_strfreev (auth_info_required);
-          auth_info_required = NULL;
         }
     }
 
diff --git a/panels/printers/pp-printer.c b/panels/printers/pp-printer.c
index 081d29319..70c580fde 100644
--- a/panels/printers/pp-printer.c
+++ b/panels/printers/pp-printer.c
@@ -357,12 +357,7 @@ get_jobs_thread (GTask        *task,
             }
         }
 
-      job = g_object_new (pp_job_get_type (),
-                          "id",    jobs[i].id,
-                          "title", jobs[i].title,
-                          "state", jobs[i].state,
-                          "auth-info-required", auth_info_is_required ? auth_info_required : NULL,
-                          NULL);
+      job = pp_job_new (jobs[i].id, jobs[i].title, jobs[i].state, auth_info_is_required ? auth_info_required 
: NULL);
 
       g_ptr_array_add (array, job);
     }


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