[gnome-control-center] printers: Introduce pp_printer_get_jobs_async ()



commit 1d0ae4968570f53ccf426ff895a3b895562d9849
Author: Felipe Borges <felipeborges gnome org>
Date:   Tue Feb 28 12:48:11 2017 +0100

    printers: Introduce pp_printer_get_jobs_async ()
    
    It is a PpPrinter method to query asychronously for printing jobs
    information.
    
    It should replace cups_get_jobs_async ()
    
    https://bugzilla.gnome.org/show_bug.cgi?id=779313

 panels/printers/pp-printer-entry.c |   71 +++++++++++++++++++++++++++----
 panels/printers/pp-printer.c       |   82 +++++++++++++++++++++++++++++++++++-
 panels/printers/pp-printer.h       |   13 ++++++
 3 files changed, 156 insertions(+), 10 deletions(-)
---
diff --git a/panels/printers/pp-printer-entry.c b/panels/printers/pp-printer-entry.c
index 0b138b9..a51a15b 100644
--- a/panels/printers/pp-printer-entry.c
+++ b/panels/printers/pp-printer-entry.c
@@ -27,6 +27,7 @@
 #include "pp-details-dialog.h"
 #include "pp-options-dialog.h"
 #include "pp-jobs-dialog.h"
+#include "pp-printer.h"
 #include "pp-utils.h"
 
 #define SUPPLY_BAR_HEIGHT 8
@@ -65,6 +66,8 @@ struct _PpPrinterEntry
   PpDetailsDialog *pp_details_dialog;
   PpOptionsDialog *pp_options_dialog;
   PpJobsDialog    *pp_jobs_dialog;
+
+  GCancellable *get_jobs_cancellable;
 };
 
 struct _PpPrinterEntryClass
@@ -318,7 +321,6 @@ supply_levels_draw_cb (GtkWidget    *widget,
       gtk_style_context_add_class (gtk_widget_get_style_context (frame),
                                    "background");
     }
-
   return TRUE;
 }
 
@@ -390,17 +392,36 @@ remove_printer (GtkButton      *button,
   printer_delete (self->printer_name);
 }
 
-void
-pp_printer_entry_update_jobs_count (PpPrinterEntry *self)
+static void
+get_jobs_cb (GObject      *source_object,
+             GAsyncResult *result,
+             gpointer      user_data)
 {
-  cups_job_t *jobs = NULL;
-  gchar *button_label;
-  gint   num_jobs, num_of_jobs;
+  PpPrinterEntry *self;
+  PpPrinter      *printer = PP_PRINTER (source_object);
+  GError         *error = NULL;
+  GList          *jobs;
+  gchar          *button_label;
+  gint            num_jobs;
+
+  jobs = pp_printer_get_jobs_finish (printer, result, &error);
+  num_jobs = g_list_length (jobs);
 
-  num_of_jobs = cupsGetJobs (&jobs, self->printer_name, 1, CUPS_WHICHJOBS_ACTIVE);
-  num_jobs = num_of_jobs < 0 ? 0 : (guint) num_of_jobs;
+  g_object_unref (source_object);
+  g_list_free_full (jobs, (GDestroyNotify) g_object_unref);
 
-  if (num_of_jobs <= 0)
+  if (error != NULL)
+    {
+      if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+        {
+          g_warning ("Could not get jobs: %s", error->message);
+        }
+
+      g_error_free (error);
+      return;
+    }
+
+  if (num_jobs == 0)
     {
       /* Translators: This is the label of the button that opens the Jobs Dialog. */
       button_label = g_strdup (_("No Active Jobs"));
@@ -411,6 +432,8 @@ pp_printer_entry_update_jobs_count (PpPrinterEntry *self)
       button_label = g_strdup_printf (ngettext ("%u Job", "%u Jobs", num_jobs), num_jobs);
     }
 
+  self = PP_PRINTER_ENTRY (user_data);
+
   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);
 
@@ -420,6 +443,30 @@ pp_printer_entry_update_jobs_count (PpPrinterEntry *self)
     }
 
   g_free (button_label);
+
+  g_clear_object (&self->get_jobs_cancellable);
+}
+
+void
+pp_printer_entry_update_jobs_count (PpPrinterEntry *self)
+{
+  PpPrinter *printer;
+
+  if (self->get_jobs_cancellable != NULL)
+    {
+      g_cancellable_cancel (self->get_jobs_cancellable);
+      g_clear_object (&self->get_jobs_cancellable);
+    }
+
+  self->get_jobs_cancellable = g_cancellable_new ();
+
+  printer = pp_printer_new (self->printer_name);
+  pp_printer_get_jobs_async (printer,
+                             TRUE,
+                             CUPS_WHICHJOBS_ACTIVE,
+                             self->get_jobs_cancellable,
+                             get_jobs_cb,
+                             self);
 }
 
 static void
@@ -734,6 +781,12 @@ pp_printer_entry_dispose (GObject *object)
   g_clear_pointer (&self->printer_make_and_model, g_free);
   g_clear_pointer (&self->printer_hostname, g_free);
 
+  if (self->get_jobs_cancellable != NULL)
+    {
+      g_cancellable_cancel (self->get_jobs_cancellable);
+      g_clear_object (&self->get_jobs_cancellable);
+    }
+
   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 3854ffe..a7def7c 100644
--- a/panels/printers/pp-printer.c
+++ b/panels/printers/pp-printer.c
@@ -20,7 +20,7 @@
 
 #include "pp-printer.h"
 
-#include "pp-utils.h"
+#include "pp-job.h"
 
 typedef struct _PpPrinter        PpPrinter;
 typedef struct _PpPrinterPrivate PpPrinterPrivate;
@@ -288,3 +288,83 @@ pp_printer_rename_finish (PpPrinter     *printer,
 
   return g_task_propagate_boolean (G_TASK (res), error);
 }
+
+typedef struct
+{
+  gboolean  myjobs;
+  gint      which_jobs;
+} GetJobsData;
+
+static void
+get_jobs_thread (GTask        *task,
+                 gpointer      source_object,
+                 gpointer      task_data,
+                 GCancellable *cancellable)
+{
+  GetJobsData *get_jobs_data = task_data;
+  cups_job_t  *jobs = NULL;
+  PpPrinter   *printer = PP_PRINTER (source_object);
+  gchar       *printer_name;
+  GList       *list = NULL;
+  gint         num_jobs;
+  gint         i;
+
+  g_object_get (printer, "printer-name", &printer_name, NULL);
+
+  num_jobs = cupsGetJobs (&jobs,
+                          printer_name,
+                          get_jobs_data->myjobs ? 1 : 0,
+                          get_jobs_data->which_jobs);
+  g_free (printer_name);
+
+  for (i = 0; i < num_jobs; i++)
+    {
+      PpJob *job;
+
+      job = g_object_new (pp_job_get_type (),
+                          "id",    jobs[i].id,
+                          "title", jobs[i].title,
+                          "state", jobs[i].state,
+                          NULL);
+
+      list = g_list_append (list, job);
+    }
+  cupsFreeJobs (num_jobs, jobs);
+
+  if (g_task_set_return_on_cancel (task, FALSE))
+    {
+      g_task_return_pointer (task, list, (GDestroyNotify) g_list_free);
+    }
+}
+
+void
+pp_printer_get_jobs_async (PpPrinter           *printer,
+                           gboolean             myjobs,
+                           gint                 which_jobs,
+                           GCancellable        *cancellable,
+                           GAsyncReadyCallback  callback,
+                           gpointer             user_data)
+{
+  GetJobsData *get_jobs_data;
+  GTask       *task;
+
+  get_jobs_data = g_new (GetJobsData, 1);
+  get_jobs_data->myjobs = myjobs;
+  get_jobs_data->which_jobs = which_jobs;
+
+  task = g_task_new (G_OBJECT (printer), cancellable, callback, user_data);
+  g_task_set_task_data (task, get_jobs_data, g_free);
+  g_task_set_return_on_cancel (task, TRUE);
+  g_task_run_in_thread (task, get_jobs_thread);
+  g_object_unref (task);
+}
+
+GList *
+pp_printer_get_jobs_finish (PpPrinter          *printer,
+                            GAsyncResult       *res,
+                            GError            **error)
+{
+  g_return_val_if_fail (g_task_is_valid (res, printer), NULL);
+
+  return g_task_propagate_pointer (G_TASK (res), error);
+}
diff --git a/panels/printers/pp-printer.h b/panels/printers/pp-printer.h
index 4a0da70..e3e7729 100644
--- a/panels/printers/pp-printer.h
+++ b/panels/printers/pp-printer.h
@@ -24,6 +24,8 @@
 #include <glib-object.h>
 #include <gio/gio.h>
 
+#include "pp-utils.h"
+
 G_BEGIN_DECLS
 
 #define PP_TYPE_PRINTER (pp_printer_get_type ())
@@ -44,6 +46,17 @@ gboolean     pp_printer_rename_finish (PpPrinter            *printer,
                                        GAsyncResult         *res,
                                        GError              **error);
 
+void         pp_printer_get_jobs_async (PpPrinter           *printer,
+                                        gboolean             myjobs,
+                                        gint                 which_jobs,
+                                        GCancellable        *cancellable,
+                                        GAsyncReadyCallback  callback,
+                                        gpointer             user_data);
+
+GList       *pp_printer_get_jobs_finish (PpPrinter          *printer,
+                                         GAsyncResult       *res,
+                                         GError            **error);
+
 G_END_DECLS
 
 #endif /* __PP_PRINTER_H__ */


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