[gnome-control-center] printers: Replace PpDevicesList with standard GPtrArray



commit bc648d9c923d2b5191ca86f228078a462dfad499
Author: Robert Ancell <robert ancell canonical com>
Date:   Mon Jun 29 11:42:24 2020 +1200

    printers: Replace PpDevicesList with standard GPtrArray

 panels/printers/pp-host.c               |  60 ++++++------
 panels/printers/pp-host.h               |   8 +-
 panels/printers/pp-new-printer-dialog.c | 158 ++++++++++++--------------------
 panels/printers/pp-samba.c              |  37 ++++----
 panels/printers/pp-samba.h              |   2 +-
 panels/printers/pp-utils.c              |  56 +++++------
 panels/printers/pp-utils.h              |  11 +--
 7 files changed, 135 insertions(+), 197 deletions(-)
---
diff --git a/panels/printers/pp-host.c b/panels/printers/pp-host.c
index 37c7009185..a31a606e31 100644
--- a/panels/printers/pp-host.c
+++ b/panels/printers/pp-host.c
@@ -247,15 +247,13 @@ _pp_host_get_snmp_devices_thread (GTask        *task,
 {
   PpHost           *self = source_object;
   PpHostPrivate    *priv = pp_host_get_instance_private (self);
-  PpDevicesList    *devices;
-  PpPrintDevice    *device;
-  gboolean          is_network_device;
+  g_autoptr(GPtrArray) devices = NULL;
   g_autoptr(GError) error = NULL;
   g_auto(GStrv)     argv = NULL;
   g_autofree gchar *stdout_string = NULL;
   gint              exit_status;
 
-  devices = g_new0 (PpDevicesList, 1);
+  devices = g_ptr_array_new_with_free_func (g_object_unref);
 
   argv = g_new0 (gchar *, 3);
   argv[0] = g_strdup ("/usr/lib/cups/backend/snmp");
@@ -284,6 +282,8 @@ _pp_host_get_snmp_devices_thread (GTask        *task,
       if (length >= 4)
         {
           g_autofree gchar *device_name = NULL;
+          gboolean is_network_device;
+          PpPrintDevice *device;
 
           device_name = g_strdup (printer_informations[3]);
           g_strcanon (device_name, ALLOWED_CHARACTERS, '-');
@@ -304,11 +304,11 @@ _pp_host_get_snmp_devices_thread (GTask        *task,
           if (length >= 6 && printer_informations[5][0] != '\0')
             g_object_set (device, "device-location", printer_informations[5], NULL);
 
-          devices->devices = g_list_append (devices->devices, device);
+          g_ptr_array_add (devices, device);
         }
     }
 
-  g_task_return_pointer (task, devices, (GDestroyNotify) pp_devices_list_free);
+  g_task_return_pointer (task, g_ptr_array_ref (devices), (GDestroyNotify) g_ptr_array_unref);
 }
 
 void
@@ -323,7 +323,7 @@ pp_host_get_snmp_devices_async (PpHost              *self,
   g_task_run_in_thread (task, _pp_host_get_snmp_devices_thread);
 }
 
-PpDevicesList *
+GPtrArray *
 pp_host_get_snmp_devices_finish (PpHost        *self,
                                  GAsyncResult  *res,
                                  GError       **error)
@@ -342,15 +342,13 @@ _pp_host_get_remote_cups_devices_thread (GTask        *task,
   cups_dest_t   *dests = NULL;
   PpHost        *self = (PpHost *) source_object;
   PpHostPrivate *priv = pp_host_get_instance_private (self);
-  PpDevicesList *devices;
-  PpPrintDevice *device;
-  const char    *device_location;
+  g_autoptr(GPtrArray) devices = NULL;
   http_t        *http;
   gint           num_of_devices = 0;
   gint           port;
   gint           i;
 
-  devices = g_new0 (PpDevicesList, 1);
+  devices = g_ptr_array_new_with_free_func (g_object_unref);
 
   if (priv->port == PP_HOST_UNSET_PORT)
     port = PP_HOST_DEFAULT_IPP_PORT;
@@ -372,6 +370,8 @@ _pp_host_get_remote_cups_devices_thread (GTask        *task,
           for (i = 0; i < num_of_devices; i++)
             {
               g_autofree gchar *device_uri = NULL;
+              const char *device_location;
+              PpPrintDevice *device;
 
               device_uri = g_strdup_printf ("ipp://%s:%d/printers/%s",
                                             priv->hostname,
@@ -391,15 +391,14 @@ _pp_host_get_remote_cups_devices_thread (GTask        *task,
                                      "host-port", port,
                                      "acquisition-method", ACQUISITION_METHOD_REMOTE_CUPS_SERVER,
                                      NULL);
-
-              devices->devices = g_list_append (devices->devices, device);
+              g_ptr_array_add (devices, device);
             }
         }
 
       httpClose (http);
     }
 
-  g_task_return_pointer (task, devices, (GDestroyNotify) pp_devices_list_free);
+  g_task_return_pointer (task, g_ptr_array_ref (devices), (GDestroyNotify) g_ptr_array_unref);
 }
 
 void
@@ -414,7 +413,7 @@ pp_host_get_remote_cups_devices_async (PpHost              *self,
   g_task_run_in_thread (task, _pp_host_get_remote_cups_devices_thread);
 }
 
-PpDevicesList *
+GPtrArray *
 pp_host_get_remote_cups_devices_finish (PpHost        *self,
                                         GAsyncResult  *res,
                                         GError       **error)
@@ -447,16 +446,15 @@ jetdirect_connection_test_cb (GObject      *source_object,
 {
   g_autoptr(GSocketConnection) connection = NULL;
   PpHostPrivate               *priv;
-  PpPrintDevice               *device;
   JetDirectData               *data;
-  PpDevicesList               *devices;
+  g_autoptr(GPtrArray)         devices = NULL;
   g_autoptr(GError)            error = NULL;
   g_autoptr(GTask)             task = G_TASK (user_data);
 
   data = g_task_get_task_data (task);
   priv = pp_host_get_instance_private (data->host);
 
-  devices = g_new0 (PpDevicesList, 1);
+  devices = g_ptr_array_new_with_free_func (g_object_unref);
 
   connection = g_socket_client_connect_to_host_finish (G_SOCKET_CLIENT (source_object),
                                                        res,
@@ -465,6 +463,7 @@ jetdirect_connection_test_cb (GObject      *source_object,
   if (connection != NULL)
     {
       g_autofree gchar *device_uri = NULL;
+      PpPrintDevice *device;
 
       g_io_stream_close (G_IO_STREAM (connection), NULL, NULL);
 
@@ -481,11 +480,10 @@ jetdirect_connection_test_cb (GObject      *source_object,
                              "host-port", data->port,
                              "acquisition-method", ACQUISITION_METHOD_JETDIRECT,
                              NULL);
-
-      devices->devices = g_list_append (devices->devices, device);
+      g_ptr_array_add (devices, device);
     }
 
-  g_task_return_pointer (task, devices, (GDestroyNotify) pp_devices_list_free);
+  g_task_return_pointer (task, g_ptr_array_ref (devices), (GDestroyNotify) g_ptr_array_unref);
 }
 
 /* Test whether given host has an AppSocket/HP JetDirect printer connected.
@@ -529,11 +527,12 @@ pp_host_get_jetdirect_devices_async (PpHost              *self,
     }
   else
     {
-      g_task_return_pointer (task, g_new0 (PpDevicesList, 1), (GDestroyNotify) pp_devices_list_free);
+      GPtrArray *devices = g_ptr_array_new_with_free_func (g_object_unref);
+      g_task_return_pointer (task, devices, (GDestroyNotify) g_ptr_array_unref);
     }
 }
 
-PpDevicesList *
+GPtrArray *
 pp_host_get_jetdirect_devices_finish (PpHost        *self,
                                       GAsyncResult  *res,
                                       GError       **error)
@@ -622,10 +621,9 @@ _pp_host_get_lpd_devices_thread (GTask        *task,
                                  GCancellable *cancellable)
 {
   g_autoptr(GSocketConnection) connection = NULL;
-  PpPrintDevice               *device;
   PpHost                      *self = source_object;
   PpHostPrivate               *priv = pp_host_get_instance_private (self);
-  PpDevicesList               *devices;
+  g_autoptr(GPtrArray)         devices = NULL;
   g_autoptr(GSocketClient)     client = NULL;
   g_autoptr(GError)            error = NULL;
   GList                       *candidates = NULL;
@@ -641,12 +639,12 @@ _pp_host_get_lpd_devices_thread (GTask        *task,
   else
     port = priv->port;
 
-  devices = g_new0 (PpDevicesList, 1);
+  devices = g_ptr_array_new_with_free_func (g_object_unref);
 
   address = g_strdup_printf ("%s:%d", priv->hostname, port);
   if (address == NULL || address[0] == '/')
     {
-      g_task_return_pointer (task, devices, (GDestroyNotify) pp_devices_list_free);
+      g_task_return_pointer (task, g_ptr_array_ref (devices), (GDestroyNotify) g_ptr_array_unref);
       return;
     }
 
@@ -701,6 +699,7 @@ _pp_host_get_lpd_devices_thread (GTask        *task,
       if (found_queue != NULL)
         {
           g_autofree gchar *device_uri = NULL;
+          PpPrintDevice *device;
 
           device_uri = g_strdup_printf ("lpd://%s:%d/%s",
                                         priv->hostname,
@@ -716,14 +715,13 @@ _pp_host_get_lpd_devices_thread (GTask        *task,
                                  "host-port", port,
                                  "acquisition-method", ACQUISITION_METHOD_LPD,
                                  NULL);
-
-          devices->devices = g_list_append (devices->devices, device);
+          g_ptr_array_add (devices, device);
         }
 
       g_list_free_full (candidates, g_free);
     }
 
-  g_task_return_pointer (task, devices, (GDestroyNotify) pp_devices_list_free);
+  g_task_return_pointer (task, g_ptr_array_ref (devices), (GDestroyNotify) g_ptr_array_unref);
 }
 
 void
@@ -738,7 +736,7 @@ pp_host_get_lpd_devices_async (PpHost              *self,
   g_task_run_in_thread (task, _pp_host_get_lpd_devices_thread);
 }
 
-PpDevicesList *
+GPtrArray *
 pp_host_get_lpd_devices_finish (PpHost        *self,
                                 GAsyncResult  *res,
                                 GError       **error)
diff --git a/panels/printers/pp-host.h b/panels/printers/pp-host.h
index ea7354e97e..3e292172d7 100644
--- a/panels/printers/pp-host.h
+++ b/panels/printers/pp-host.h
@@ -46,7 +46,7 @@ void           pp_host_get_snmp_devices_async         (PpHost               *hos
                                                        GAsyncReadyCallback   callback,
                                                        gpointer              user_data);
 
-PpDevicesList *pp_host_get_snmp_devices_finish        (PpHost               *host,
+GPtrArray     *pp_host_get_snmp_devices_finish        (PpHost               *host,
                                                        GAsyncResult         *result,
                                                        GError              **error);
 
@@ -55,7 +55,7 @@ void           pp_host_get_remote_cups_devices_async  (PpHost               *hos
                                                        GAsyncReadyCallback   callback,
                                                        gpointer              user_data);
 
-PpDevicesList *pp_host_get_remote_cups_devices_finish (PpHost               *host,
+GPtrArray     *pp_host_get_remote_cups_devices_finish (PpHost               *host,
                                                        GAsyncResult         *result,
                                                        GError              **error);
 
@@ -64,7 +64,7 @@ void           pp_host_get_jetdirect_devices_async    (PpHost               *hos
                                                        GAsyncReadyCallback   callback,
                                                        gpointer              user_data);
 
-PpDevicesList *pp_host_get_jetdirect_devices_finish   (PpHost               *host,
+GPtrArray     *pp_host_get_jetdirect_devices_finish   (PpHost               *host,
                                                        GAsyncResult         *result,
                                                        GError              **error);
 
@@ -73,7 +73,7 @@ void           pp_host_get_lpd_devices_async          (PpHost               *hos
                                                        GAsyncReadyCallback   callback,
                                                        gpointer              user_data);
 
-PpDevicesList *pp_host_get_lpd_devices_finish         (PpHost               *host,
+GPtrArray     *pp_host_get_lpd_devices_finish         (PpHost               *host,
                                                        GAsyncResult         *result,
                                                        GError              **error);
 
diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c
index be900813f7..9da38b7d1c 100644
--- a/panels/printers/pp-new-printer-dialog.c
+++ b/panels/printers/pp-new-printer-dialog.c
@@ -71,7 +71,7 @@ static void     new_printer_dialog_response_cb (PpNewPrinterDialog *self,
                                                 gint                response_id);
 static void     update_dialog_state (PpNewPrinterDialog *self);
 static void     add_devices_to_list (PpNewPrinterDialog  *self,
-                                     GList               *devices);
+                                     GPtrArray           *devices);
 static void     remove_device_from_list (PpNewPrinterDialog *self,
                                          const gchar        *device_name);
 
@@ -93,7 +93,7 @@ struct _PpNewPrinterDialog
 
   GtkBuilder *builder;
 
-  GList *local_cups_devices;
+  GPtrArray *local_cups_devices;
 
   GtkListStore       *store;
   GtkTreeModelFilter *filter;
@@ -246,25 +246,24 @@ get_authenticated_samba_devices_cb (GObject      *source_object,
 {
   AuthSMBData               *data = user_data;
   PpNewPrinterDialog        *self = PP_NEW_PRINTER_DIALOG (data->dialog);
-  PpDevicesList             *result;
-  PpPrintDevice             *device;
+  g_autoptr(GPtrArray)       devices = NULL;
   gboolean                   cancelled = FALSE;
   PpSamba                   *samba = (PpSamba *) source_object;
   g_autoptr(GError)          error = NULL;
-  GList                     *iter;
 
   g_object_ref (samba);
 
-  result = pp_samba_get_devices_finish (samba, res, &error);
+  devices = pp_samba_get_devices_finish (samba, res, &error);
   g_object_unref (source_object);
 
-  if (result != NULL)
+  if (devices != NULL)
     {
       self->samba_authenticated_searching = FALSE;
 
-      for (iter = result->devices; iter; iter = iter->next)
+      for (guint i = 0; i < devices->len; i++)
         {
-          device = (PpPrintDevice *) iter->data;
+          PpPrintDevice *device = g_ptr_array_index (devices, i);
+
           if (pp_print_device_is_authenticated_server (device))
             {
               cancelled = TRUE;
@@ -274,23 +273,19 @@ get_authenticated_samba_devices_cb (GObject      *source_object,
 
       if (!cancelled)
         {
-
-          if (result->devices != NULL)
+          if (devices != NULL)
             {
-              add_devices_to_list (self, result->devices);
+              add_devices_to_list (self, devices);
 
-              device = (PpPrintDevice *) result->devices->data;
-              if (device != NULL)
+              if (devices->len > 0)
                 {
-                  gtk_entry_set_text (GTK_ENTRY (WID ("search-entry")), pp_print_device_get_device_location 
(device));
+                  gtk_entry_set_text (GTK_ENTRY (WID ("search-entry")), pp_print_device_get_device_location 
(g_ptr_array_index (devices, 0)));
                   search_entry_activated_cb (self);
                 }
             }
         }
 
       update_dialog_state (self);
-
-      pp_devices_list_free (result);
     }
   else
     {
@@ -490,6 +485,8 @@ pp_new_printer_dialog_init (PpNewPrinterDialog *self)
       g_warning ("Could not load ui: %s", error->message);
     }
 
+  self->local_cups_devices = g_ptr_array_new_with_free_func (g_object_unref);
+
   /* GCancellable for cancelling of async operations */
   self->cancellable = g_cancellable_new ();
 
@@ -531,12 +528,6 @@ pp_new_printer_dialog_init (PpNewPrinterDialog *self)
   populate_devices_list (self);
 }
 
-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)
 {
@@ -551,7 +542,7 @@ pp_new_printer_dialog_finalize (GObject *object)
   g_clear_pointer (&self->dialog, gtk_widget_destroy);
   g_clear_pointer (&self->list, ppd_list_free);
   g_clear_object (&self->builder);
-  g_clear_pointer (&self->local_cups_devices, free_devices_list);
+  g_clear_pointer (&self->local_cups_devices, g_ptr_array_unref);
   g_clear_object (&self->local_printer_icon);
   g_clear_object (&self->remote_printer_icon);
   g_clear_object (&self->authenticated_server_icon);
@@ -704,7 +695,7 @@ add_device_to_list (PpNewPrinterDialog *self,
                         NULL);
 
           if (pp_print_device_get_acquisition_method (device) == ACQUISITION_METHOD_DEFAULT_CUPS_SERVER)
-            self->local_cups_devices = g_list_append (self->local_cups_devices, g_object_ref (device));
+            g_ptr_array_add (self->local_cups_devices, g_object_ref (device));
           else
             set_device (self, device, NULL);
         }
@@ -726,26 +717,19 @@ add_device_to_list (PpNewPrinterDialog *self,
 
 static void
 add_devices_to_list (PpNewPrinterDialog  *self,
-                     GList               *devices)
+                     GPtrArray           *devices)
 {
-  GList *iter;
-
-  for (iter = devices; iter; iter = iter->next)
-    {
-      add_device_to_list (self, (PpPrintDevice *) iter->data);
-    }
+  for (guint i = 0; i < devices->len; i++)
+    add_device_to_list (self, g_ptr_array_index (devices, i));
 }
 
 static PpPrintDevice *
 device_in_list (gchar *device_uri,
-                GList *device_list)
+                GPtrArray *device_list)
 {
-  PpPrintDevice *device;
-  GList         *iter;
-
-  for (iter = device_list; iter; iter = iter->next)
+  for (guint i = 0; i < device_list->len; i++)
     {
-      device = (PpPrintDevice *) iter->data;
+      PpPrintDevice *device = g_ptr_array_index (device_list, i);
       /* GroupPhysicalDevices returns uris without port numbers */
       if (pp_print_device_get_device_uri (device) != NULL &&
           g_str_has_prefix (pp_print_device_get_device_uri (device), device_uri))
@@ -826,7 +810,6 @@ group_physical_devices_cb (gchar    ***device_uris,
 {
   PpNewPrinterDialog        *self = user_data;
   PpPrintDevice             *device, *better_device;
-  GList                     *iter;
   gint                       i, j;
 
   if (device_uris != NULL)
@@ -876,9 +859,9 @@ group_physical_devices_cb (gchar    ***device_uris,
     }
   else
     {
-      for (iter = self->local_cups_devices; iter != NULL; iter = iter->next)
-        set_device (self, (PpPrintDevice *) iter->data, NULL);
-      g_clear_pointer (&self->local_cups_devices, free_devices_list);
+      for (i = 0; i < self->local_cups_devices->len; i++)
+        set_device (self, g_ptr_array_index (self->local_cups_devices, i), NULL);
+      g_ptr_array_set_size (self->local_cups_devices, 0);
     }
 
   update_dialog_state (self);
@@ -939,7 +922,7 @@ DBus method \"GroupPhysicalDevices\" to group duplicates in device list.");
 }
 
 static void
-get_cups_devices_cb (GList    *devices,
+get_cups_devices_cb (GPtrArray *devices,
                      gboolean  finished,
                      gboolean  cancelled,
                      gpointer  user_data)
@@ -949,13 +932,11 @@ get_cups_devices_cb (GList    *devices,
   GVariantBuilder             device_list;
   GVariantBuilder             device_hash;
   PpPrintDevice             **all_devices;
-  PpPrintDevice              *pp_device;
   PpPrintDevice              *device;
   const gchar                *device_class;
   GtkTreeIter                 iter;
   gboolean                    cont;
   g_autoptr(GError)           error = NULL;
-  GList                      *liter;
   gint                        length, i;
 
 
@@ -966,11 +947,11 @@ get_cups_devices_cb (GList    *devices,
           self->cups_searching = FALSE;
         }
 
-      if (devices)
+      if (devices != NULL)
         {
           add_devices_to_list (self, devices);
 
-          length = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (self->store), NULL) + g_list_length 
(self->local_cups_devices);
+          length = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (self->store), NULL) + 
self->local_cups_devices->len;
           if (length > 0)
             {
               all_devices = g_new0 (PpPrintDevice *, length);
@@ -996,19 +977,16 @@ get_cups_devices_cb (GList    *devices,
                   cont = gtk_tree_model_iter_next (GTK_TREE_MODEL (self->store), &iter);
                 }
 
-              for (liter = self->local_cups_devices; liter != NULL; liter = liter->next)
+              for (guint j = 0; j < self->local_cups_devices->len; j++)
                 {
-                  pp_device = (PpPrintDevice *) liter->data;
-                  if (pp_device != NULL)
-                    {
-                      all_devices[i] = g_object_new (PP_TYPE_PRINT_DEVICE,
-                                                     "device-id", pp_print_device_get_device_id (pp_device),
-                                                     "device-make-and-model", 
pp_print_device_get_device_make_and_model (pp_device),
-                                                     "is-network-device", pp_print_device_is_network_device 
(pp_device),
-                                                     "device-uri", pp_print_device_get_device_uri 
(pp_device),
-                                                     NULL);
-                      i++;
-                    }
+                  PpPrintDevice *pp_device = g_ptr_array_index (self->local_cups_devices, j);
+                  all_devices[i] = g_object_new (PP_TYPE_PRINT_DEVICE,
+                                                 "device-id", pp_print_device_get_device_id (pp_device),
+                                                 "device-make-and-model", 
pp_print_device_get_device_make_and_model (pp_device),
+                                                 "is-network-device", pp_print_device_is_network_device 
(pp_device),
+                                                 "device-uri", pp_print_device_get_device_uri (pp_device),
+                                                 NULL);
+                   i++;
                 }
 
               bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
@@ -1084,8 +1062,6 @@ get_cups_devices_cb (GList    *devices,
           update_dialog_state (self);
         }
     }
-
-  free_devices_list (devices);
 }
 
 static void
@@ -1096,21 +1072,19 @@ get_snmp_devices_cb (GObject      *source_object,
   PpNewPrinterDialog        *self = user_data;
   PpHost                    *host = (PpHost *) source_object;
   g_autoptr(GError)          error = NULL;
-  PpDevicesList             *result;
+  g_autoptr(GPtrArray)       devices = NULL;
 
-  result = pp_host_get_snmp_devices_finish (host, res, &error);
+  devices = pp_host_get_snmp_devices_finish (host, res, &error);
   g_object_unref (source_object);
 
-  if (result)
+  if (devices != NULL)
     {
       if ((gpointer) source_object == (gpointer) self->snmp_host)
         self->snmp_host = NULL;
 
-      add_devices_to_list (self, result->devices);
+      add_devices_to_list (self, devices);
 
       update_dialog_state (self);
-
-      pp_devices_list_free (result);
     }
   else
     {
@@ -1134,21 +1108,19 @@ get_remote_cups_devices_cb (GObject      *source_object,
   PpNewPrinterDialog        *self = user_data;
   PpHost                    *host = (PpHost *) source_object;
   g_autoptr(GError)          error = NULL;
-  PpDevicesList             *result;
+  g_autoptr(GPtrArray)       devices = NULL;
 
-  result = pp_host_get_remote_cups_devices_finish (host, res, &error);
+  devices = pp_host_get_remote_cups_devices_finish (host, res, &error);
   g_object_unref (source_object);
 
-  if (result)
+  if (devices != NULL)
     {
       if ((gpointer) source_object == (gpointer) self->remote_cups_host)
         self->remote_cups_host = NULL;
 
-      add_devices_to_list (self, result->devices);
+      add_devices_to_list (self, devices);
 
       update_dialog_state (self);
-
-      pp_devices_list_free (result);
     }
   else
     {
@@ -1170,23 +1142,21 @@ get_samba_host_devices_cb (GObject      *source_object,
                            gpointer      user_data)
 {
   PpNewPrinterDialog        *self = user_data;
-  PpDevicesList             *result;
+  g_autoptr(GPtrArray)       devices = NULL;
   PpSamba                   *samba = (PpSamba *) source_object;
   g_autoptr(GError)          error = NULL;
 
-  result = pp_samba_get_devices_finish (samba, res, &error);
+  devices = pp_samba_get_devices_finish (samba, res, &error);
   g_object_unref (source_object);
 
-  if (result)
+  if (devices != NULL)
     {
       if ((gpointer) source_object == (gpointer) self->samba_host)
         self->samba_host = NULL;
 
-      add_devices_to_list (self, result->devices);
+      add_devices_to_list (self, devices);
 
       update_dialog_state (self);
-
-      pp_devices_list_free (result);
     }
   else
     {
@@ -1208,22 +1178,20 @@ get_samba_devices_cb (GObject      *source_object,
                       gpointer      user_data)
 {
   PpNewPrinterDialog        *self = user_data;
-  PpDevicesList             *result;
+  g_autoptr(GPtrArray)       devices = NULL;
   PpSamba                   *samba = (PpSamba *) source_object;
   g_autoptr(GError)          error = NULL;
 
-  result = pp_samba_get_devices_finish (samba, res, &error);
+  devices = pp_samba_get_devices_finish (samba, res, &error);
   g_object_unref (source_object);
 
-  if (result)
+  if (devices != NULL)
     {
       self->samba_searching = FALSE;
 
-      add_devices_to_list (self, result->devices);
+      add_devices_to_list (self, devices);
 
       update_dialog_state (self);
-
-      pp_devices_list_free (result);
     }
   else
     {
@@ -1246,21 +1214,19 @@ get_jetdirect_devices_cb (GObject      *source_object,
   PpNewPrinterDialog        *self = user_data;
   PpHost                    *host = (PpHost *) source_object;
   g_autoptr(GError)          error = NULL;
-  PpDevicesList             *result;
+  g_autoptr(GPtrArray)       devices = NULL;
 
-  result = pp_host_get_jetdirect_devices_finish (host, res, &error);
+  devices = pp_host_get_jetdirect_devices_finish (host, res, &error);
   g_object_unref (source_object);
 
-  if (result != NULL)
+  if (devices != NULL)
     {
       if ((gpointer) source_object == (gpointer) self->socket_host)
         self->socket_host = NULL;
 
-      add_devices_to_list (self, result->devices);
+      add_devices_to_list (self, devices);
 
       update_dialog_state (self);
-
-      pp_devices_list_free (result);
     }
   else
     {
@@ -1284,21 +1250,19 @@ get_lpd_devices_cb (GObject      *source_object,
   PpNewPrinterDialog        *self = user_data;
   PpHost                    *host = (PpHost *) source_object;
   g_autoptr(GError)          error = NULL;
-  PpDevicesList             *result;
+  g_autoptr(GPtrArray)       devices = NULL;
 
-  result = pp_host_get_lpd_devices_finish (host, res, &error);
+  devices = pp_host_get_lpd_devices_finish (host, res, &error);
   g_object_unref (source_object);
 
-  if (result != NULL)
+  if (devices != NULL)
     {
       if ((gpointer) source_object == (gpointer) self->lpd_host)
         self->lpd_host = NULL;
 
-      add_devices_to_list (self, result->devices);
+      add_devices_to_list (self, devices);
 
       update_dialog_state (self);
-
-      pp_devices_list_free (result);
     }
   else
     {
diff --git a/panels/printers/pp-samba.c b/panels/printers/pp-samba.c
index 1dfc2cc127..5eaf5b762b 100644
--- a/panels/printers/pp-samba.c
+++ b/panels/printers/pp-samba.c
@@ -75,7 +75,7 @@ pp_samba_new (const gchar *hostname)
 typedef struct
 {
   PpSamba       *samba;
-  PpDevicesList *devices;
+  GPtrArray     *devices;
   GMainContext  *context;
   gboolean       auth_if_needed;
   gboolean       hostname_set;
@@ -87,7 +87,7 @@ smb_data_free (SMBData *data)
 {
   if (data)
     {
-      pp_devices_list_free (data->devices);
+      g_ptr_array_unref (data->devices);
 
       g_free (data);
     }
@@ -221,7 +221,6 @@ list_dir (SMBCCTX      *smb_context,
   smbc_closedir_fn    smbclient_closedir;
   smbc_readdir_fn     smbclient_readdir;
   smbc_opendir_fn     smbclient_opendir;
-  PpPrintDevice      *device;
   const gchar        *host_name;
   SMBCFILE           *dir;
 
@@ -248,12 +247,11 @@ list_dir (SMBCCTX      *smb_context,
 
               if (data->cancelled)
                 {
-                  device = g_object_new (PP_TYPE_PRINT_DEVICE,
-                                         "host-name", host_name,
-                                         "is-authenticated-server", TRUE,
-                                         NULL);
-
-                  data->devices->devices = g_list_append (data->devices->devices, device);
+                  PpPrintDevice *device = g_object_new (PP_TYPE_PRINT_DEVICE,
+                                                        "host-name", host_name,
+                                                        "is-authenticated-server", TRUE,
+                                                        NULL);
+                  g_ptr_array_add (data->devices, device);
 
                   if (dir)
                     smbclient_closedir (smb_context, dir);
@@ -262,12 +260,11 @@ list_dir (SMBCCTX      *smb_context,
             }
           else
             {
-              device = g_object_new (PP_TYPE_PRINT_DEVICE,
-                                     "host-name", host_name,
-                                     "is-authenticated-server", TRUE,
-                                     NULL);
-
-              data->devices->devices = g_list_append (data->devices->devices, device);
+              PpPrintDevice *device = g_object_new (PP_TYPE_PRINT_DEVICE,
+                                                    "host-name", host_name,
+                                                    "is-authenticated-server", TRUE,
+                                                    NULL);
+              g_ptr_array_add (data->devices, device);
             }
         }
 
@@ -293,6 +290,7 @@ list_dir (SMBCCTX      *smb_context,
               g_autofree gchar *uri = NULL;
               g_autofree gchar *device_name = NULL;
               g_autofree gchar *device_uri = NULL;
+              PpPrintDevice *device;
 
               uri = g_strdup_printf ("%s/%s", dirname, dirent->name);
               device_uri = g_uri_escape_string (uri,
@@ -313,7 +311,7 @@ list_dir (SMBCCTX      *smb_context,
                                      "host-name", dirname,
                                      NULL);
 
-              data->devices->devices = g_list_append (data->devices->devices, device);
+              g_ptr_array_add (data->devices, device);
             }
 
           if (subdirname)
@@ -341,8 +339,7 @@ _pp_samba_get_devices_thread (GTask        *task,
   SMBData        *data = (SMBData *) task_data;
   SMBCCTX        *smb_context;
 
-  data->devices = g_new0 (PpDevicesList, 1);
-  data->devices->devices = NULL;
+  data->devices = g_ptr_array_new_with_free_func (g_object_unref);
   data->samba = PP_SAMBA (source_object);
 
   g_mutex_lock (&mutex);
@@ -379,7 +376,7 @@ _pp_samba_get_devices_thread (GTask        *task,
 
   g_mutex_unlock (&mutex);
 
-  g_task_return_pointer (task, g_steal_pointer (&data->devices), (GDestroyNotify) pp_devices_list_free);
+  g_task_return_pointer (task, g_ptr_array_ref (data->devices), (GDestroyNotify) g_ptr_array_unref);
 }
 
 void
@@ -406,7 +403,7 @@ pp_samba_get_devices_async (PpSamba             *samba,
   g_task_run_in_thread (task, _pp_samba_get_devices_thread);
 }
 
-PpDevicesList *
+GPtrArray *
 pp_samba_get_devices_finish (PpSamba       *samba,
                              GAsyncResult  *res,
                              GError       **error)
diff --git a/panels/printers/pp-samba.h b/panels/printers/pp-samba.h
index e2c5ddf20e..0801317720 100644
--- a/panels/printers/pp-samba.h
+++ b/panels/printers/pp-samba.h
@@ -36,7 +36,7 @@ void           pp_samba_get_devices_async  (PpSamba             *samba,
                                             GAsyncReadyCallback  callback,
                                             gpointer             user_data);
 
-PpDevicesList *pp_samba_get_devices_finish (PpSamba             *samba,
+GPtrArray     *pp_samba_get_devices_finish (PpSamba             *samba,
                                             GAsyncResult        *result,
                                             GError             **error);
 
diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c
index 043091695e..ebdaa41eac 100644
--- a/panels/printers/pp-utils.c
+++ b/panels/printers/pp-utils.c
@@ -2974,16 +2974,6 @@ printer_get_ppd_async (const gchar *printer_name,
     }
 }
 
-void
-pp_devices_list_free (PpDevicesList *result)
-{
-  if (result)
-    {
-      g_list_free_full (result->devices, (GDestroyNotify) g_object_unref);
-      g_free (result);
-    }
-}
-
 typedef struct
 {
   gchar        *printer_name;
@@ -3207,11 +3197,10 @@ get_cups_devices_async_dbus_cb (GObject      *source_object,
                                 gpointer      user_data)
 
 {
-  PpPrintDevice     **devices = NULL;
+  g_autoptr(GPtrArray) devices = NULL;
   g_autoptr(GVariant) output = NULL;
   GCDData            *data = (GCDData *) user_data;
   g_autoptr(GError)   error = NULL;
-  GList              *result = NULL;
   gint                num_of_devices = 0;
 
   output = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source_object),
@@ -3249,46 +3238,43 @@ get_cups_devices_async_dbus_cb (GObject      *source_object,
           g_autoptr(GVariantIter) iter2 = NULL;
 
           num_of_devices = max_index + 1;
-          devices = g_new0 (PpPrintDevice *, num_of_devices);
+          devices = g_ptr_array_new_with_free_func (g_object_unref);
+          for (i = 0; i < num_of_devices; i++)
+             g_ptr_array_add (devices, pp_print_device_new ());
 
           g_variant_get (devices_variant, "a{ss}", &iter2);
           while (g_variant_iter_next (iter2, "{&s&s}", &key, &value))
             {
+              PpPrintDevice *device;
+
               index = get_suffix_index (key);
               if (index >= 0)
                 {
-                  if (!devices[index])
-                    devices[index] = pp_print_device_new ();
-
+                  device = g_ptr_array_index (devices, index);
                   if (g_str_has_prefix (key, "device-class"))
                     {
                       is_network_device = g_strcmp0 (value, "network") == 0;
-                      g_object_set (devices[index], "is-network-device", is_network_device, NULL);
+                      g_object_set (device, "is-network-device", is_network_device, NULL);
                     }
                   else if (g_str_has_prefix (key, "device-id"))
-                    g_object_set (devices[index], "device-id", value, NULL);
+                    g_object_set (device, "device-id", value, NULL);
                   else if (g_str_has_prefix (key, "device-info"))
-                    g_object_set (devices[index], "device-info", value, NULL);
+                    g_object_set (device, "device-info", value, NULL);
                   else if (g_str_has_prefix (key, "device-make-and-model"))
                     {
-                      g_object_set (devices[index],
+                      g_object_set (device,
                                     "device-make-and-model", value,
                                     "device-name", value,
                                     NULL);
                     }
                   else if (g_str_has_prefix (key, "device-uri"))
-                    g_object_set (devices[index], "device-uri", value, NULL);
+                    g_object_set (device, "device-uri", value, NULL);
                   else if (g_str_has_prefix (key, "device-location"))
-                    g_object_set (devices[index], "device-location", value, NULL);
+                    g_object_set (device, "device-location", value, NULL);
 
-                  g_object_set (devices[index], "acquisition-method", 
ACQUISITION_METHOD_DEFAULT_CUPS_SERVER, NULL);
+                  g_object_set (device, "acquisition-method", ACQUISITION_METHOD_DEFAULT_CUPS_SERVER, NULL);
                 }
             }
-
-          for (i = 0; i < num_of_devices; i++)
-            result = g_list_append (result, devices[i]);
-
-          g_free (devices);
         }
     }
   else
@@ -3296,7 +3282,7 @@ get_cups_devices_async_dbus_cb (GObject      *source_object,
       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
         g_warning ("%s", error->message);
 
-      data->callback (result,
+      data->callback (devices,
                       TRUE,
                       g_cancellable_is_cancelled (data->cancellable),
                       data->user_data);
@@ -3321,7 +3307,7 @@ get_cups_devices_async_dbus_cb (GObject      *source_object,
 
           backend_name = data->backend_list->data;
 
-          data->callback (result,
+          data->callback (devices,
                           FALSE,
                           FALSE,
                           data->user_data);
@@ -3365,7 +3351,7 @@ get_cups_devices_async_dbus_cb (GObject      *source_object,
         }
       else
         {
-          data->callback (result,
+          data->callback (devices,
                           TRUE,
                           TRUE,
                           data->user_data);
@@ -3376,7 +3362,7 @@ get_cups_devices_async_dbus_cb (GObject      *source_object,
     }
   else
     {
-      data->callback (result,
+      data->callback (devices,
                       TRUE,
                       g_cancellable_is_cancelled (data->cancellable),
                       data->user_data);
@@ -3510,7 +3496,7 @@ guess_device_hostname (PpPrintDevice *device)
 
 gchar *
 canonicalize_device_name (GList         *device_names,
-                          GList         *local_cups_devices,
+                          GPtrArray     *local_cups_devices,
                           cups_dest_t   *dests,
                           gint           num_of_dests,
                           PpPrintDevice *device)
@@ -3632,9 +3618,9 @@ canonicalize_device_name (GList         *device_names,
             already_present = TRUE;
         }
 
-      for (iter = local_cups_devices; iter; iter = iter->next)
+      for (guint i = 0; i < local_cups_devices->len; i++)
         {
-          item = (PpPrintDevice *) iter->data;
+          item = g_ptr_array_index (local_cups_devices, i);
           if (g_strcmp0 (pp_print_device_get_device_original_name (item), new_name) == 0)
             already_present = TRUE;
         }
diff --git a/panels/printers/pp-utils.h b/panels/printers/pp-utils.h
index f1631426f0..211fff52d6 100644
--- a/panels/printers/pp-utils.h
+++ b/panels/printers/pp-utils.h
@@ -80,11 +80,6 @@ typedef struct
   gsize                 num_of_manufacturers;
 } PPDList;
 
-typedef struct
-{
-  GList *devices;
-} PpDevicesList;
-
 gchar      *get_tag_value (const gchar *tag_string,
                            const gchar *tag_name);
 
@@ -241,11 +236,9 @@ void        printer_add_option_async (const gchar   *printer_name,
                                       PAOCallback    callback,
                                       gpointer       user_data);
 
-void         pp_devices_list_free (PpDevicesList *result);
-
 const gchar *get_page_size_from_locale (void);
 
-typedef void (*GCDCallback) (GList          *devices,
+typedef void (*GCDCallback) (GPtrArray      *devices,
                              gboolean        finished,
                              gboolean        cancelled,
                              gpointer        user_data);
@@ -257,7 +250,7 @@ void        get_cups_devices_async (GCancellable *cancellable,
 gchar      *guess_device_hostname (PpPrintDevice *device);
 
 gchar      *canonicalize_device_name (GList         *device_names,
-                                      GList         *local_cups_devices,
+                                      GPtrArray     *local_cups_devices,
                                       cups_dest_t   *dests,
                                       gint           num_of_dests,
                                       PpPrintDevice *device);



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