[gnome-control-center/gnome-3-4] printers: Use PrinterAddOption method to set media size



commit 7c8f7bb2ee6c27f69fc0926585426cfd9bc02873
Author: Marek Kasik <mkasik redhat com>
Date:   Fri Apr 13 13:29:21 2012 +0200

    printers: Use PrinterAddOption method to set media size
    
    Use new cups-pk-helper's method PrinterAddOption for setting
    default media size for new printers. This fixes problem with
    setting default media size (#672694).

 panels/printers/pp-new-printer-dialog.c |  130 +++++--------------------------
 panels/printers/pp-utils.c              |   67 ++++++++++++++++
 panels/printers/pp-utils.h              |    2 +
 3 files changed, 90 insertions(+), 109 deletions(-)
---
diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c
index 0b21502..6302a74 100644
--- a/panels/printers/pp-new-printer-dialog.c
+++ b/panels/printers/pp-new-printer-dialog.c
@@ -1477,7 +1477,7 @@ new_printer_add_button_cb (GtkButton *button,
   gchar              *device_name = NULL;
   gint                device_id = -1;
   gint                device_type = -1;
-  gint                i, j, k;
+  gint                i;
   int                 num_dests;
 
   treeview = (GtkWidget*)
@@ -1738,125 +1738,37 @@ new_printer_add_button_cb (GtkButton *button,
           const char *ppd_file_name = NULL;
           GDBusConnection *bus;
           GError     *error = NULL;
-          GVariant   *output;
-          ppd_file_t  *ppd_file = NULL;
-          gchar       *value = NULL;
-          const gchar *paper_size;
 
           ppd_file_name = cupsGetPPD (pp->devices[device_id].display_name);
 
-          bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
-          if (!bus)
-            {
-              g_warning ("Failed to get system bus: %s", error->message);
-              g_error_free (error);
-            }
-          else
-            {
-              printer_set_accepting_jobs (pp->devices[device_id].display_name, TRUE, NULL);
-              printer_set_enabled (pp->devices[device_id].display_name, TRUE);
-
-              if (g_strcmp0 (pp->devices[device_id].device_class, "direct") == 0)
-                {
-                  gchar *commands = get_dest_attr (pp->devices[device_id].display_name, "printer-commands");
-                  gchar *commands_lowercase = g_ascii_strdown (commands, -1);
-                  ipp_t *response = NULL;
-
-                  if (g_strrstr (commands_lowercase, "autoconfigure"))
-                    {
-                      response = execute_maintenance_command (pp->devices[device_id].display_name,
-                                                              "AutoConfigure",
-                      /* Translators: Name of job which makes printer to autoconfigure itself */
-                                                              _("Automatic configuration"));
-                      if (response)
-                        {
-                          if (response->state == IPP_ERROR)
-                            g_warning ("An error has occured during automatic configuration of new printer.");
-                          ippDelete (response);
-                        }
-                    }
-                  g_free (commands);
-                  g_free (commands_lowercase);
-                }
-
+          printer_set_accepting_jobs (pp->devices[device_id].display_name, TRUE, NULL);
+          printer_set_enabled (pp->devices[device_id].display_name, TRUE);
 
-              /* Set default PaperSize according to the locale */
-              paper_size = gtk_paper_size_get_default ();
-              if (g_str_equal (paper_size, GTK_PAPER_NAME_LETTER))
-                paper_size = "Letter";
-              else
-                paper_size = "A4";
-
-              if (ppd_file_name)
-                {
-                  ppd_file = ppdOpenFile (ppd_file_name);
-                  if (ppd_file)
-                    {
-                      ppdMarkDefaults (ppd_file);
-                      for (i = 0; i < ppd_file->num_groups; i++)
-                        for (j = 0; j < ppd_file->groups[i].num_options; j++)
-                          if (g_strcmp0 ("PageSize", ppd_file->groups[i].options[j].keyword) == 0)
-                            {
-                              for (k = 0; k < ppd_file->groups[i].options[j].num_choices; k++)
-                                {
-                                  if (g_ascii_strncasecmp (paper_size,
-                                                           ppd_file->groups[i].options[j].choices[k].choice,
-                                                           strlen (paper_size)) == 0 &&
-                                      !ppd_file->groups[i].options[j].choices[k].marked)
-                                    {
-                                      value = g_strdup (ppd_file->groups[i].options[j].choices[k].choice);
-                                      break;
-                                    }
-                                }
-                              break;
-                            }
-                      ppdClose (ppd_file);
-                    }
-                }
+          if (g_strcmp0 (pp->devices[device_id].device_class, "direct") == 0)
+            {
+              gchar *commands = get_dest_attr (pp->devices[device_id].display_name, "printer-commands");
+              gchar *commands_lowercase = g_ascii_strdown (commands, -1);
+              ipp_t *response = NULL;
 
-              if (value)
+              if (g_strrstr (commands_lowercase, "autoconfigure"))
                 {
-                  GVariantBuilder array_builder;
-
-                  g_variant_builder_init (&array_builder, G_VARIANT_TYPE ("as"));
-                  g_variant_builder_add (&array_builder, "s", value);
-
-                  output = g_dbus_connection_call_sync (bus,
-                                                        MECHANISM_BUS,
-                                                        "/",
-                                                        MECHANISM_BUS,
-                                                        "PrinterAddOptionDefault",
-                                                        g_variant_new ("(ssas)",
-                                                                       pp->devices[device_id].display_name,
-                                                                       "PageSize",
-                                                                       &array_builder),
-                                                        G_VARIANT_TYPE ("(s)"),
-                                                        G_DBUS_CALL_FLAGS_NONE,
-                                                        -1,
-                                                        NULL,
-                                                        &error);
-                  g_object_unref (bus);
-
-                  if (output)
-                    {
-                      const gchar *ret_error;
-
-                      g_variant_get (output, "(&s)", &ret_error);
-                      if (ret_error[0] != '\0')
-                        g_warning ("%s", ret_error);
-
-                      g_variant_unref (output);
-                    }
-                  else
+                  response = execute_maintenance_command (pp->devices[device_id].display_name,
+                                                          "AutoConfigure",
+                  /* Translators: Name of job which makes printer to autoconfigure itself */
+                                                          _("Automatic configuration"));
+                  if (response)
                     {
-                      g_warning ("%s", error->message);
-                      g_error_free (error);
+                      if (response->state == IPP_ERROR)
+                        g_warning ("An error has occured during automatic configuration of new printer.");
+                      ippDelete (response);
                     }
-
-                  g_free (value);
                 }
+              g_free (commands);
+              g_free (commands_lowercase);
             }
 
+          printer_set_default_media_size (pp->devices[device_id].display_name);
+
           if (pp->devices[device_id].device_uri &&
               dbus_method_available (FIREWALLD_BUS,
                                      FIREWALLD_PATH,
diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c
index d398111..5263af6 100644
--- a/panels/printers/pp-utils.c
+++ b/panels/printers/pp-utils.c
@@ -2493,3 +2493,70 @@ printer_get_hostname (cups_ptype_t  printer_type,
 
   return result;
 }
+
+/* Returns default media size for current locale */
+static const gchar *
+get_paper_size_from_locale ()
+{
+  if (g_str_equal (gtk_paper_size_get_default (), GTK_PAPER_NAME_LETTER))
+    return "na-letter";
+  else
+    return "iso-a4";
+}
+
+/* Set default media size according to the locale */
+void
+printer_set_default_media_size (const gchar *printer_name)
+{
+  GVariantBuilder  array_builder;
+  GDBusConnection *bus;
+  GVariant        *output;
+  GError          *error = NULL;
+
+  bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, &error);
+  if (!bus)
+   {
+     g_warning ("Failed to get system bus: %s", error->message);
+     g_error_free (error);
+     return;
+   }
+
+  g_variant_builder_init (&array_builder, G_VARIANT_TYPE ("as"));
+  g_variant_builder_add (&array_builder, "s", get_paper_size_from_locale ());
+
+  output = g_dbus_connection_call_sync (bus,
+                                        MECHANISM_BUS,
+                                        "/",
+                                        MECHANISM_BUS,
+                                        "PrinterAddOption",
+                                        g_variant_new ("(ssas)",
+                                                       printer_name,
+                                                       "media",
+                                                       &array_builder),
+                                        G_VARIANT_TYPE ("(s)"),
+                                        G_DBUS_CALL_FLAGS_NONE,
+                                        -1,
+                                        NULL,
+                                        &error);
+
+  g_object_unref (bus);
+
+  if (output)
+    {
+      const gchar *ret_error;
+
+      g_variant_get (output, "(&s)", &ret_error);
+      if (ret_error[0] != '\0')
+        g_warning ("%s", ret_error);
+
+      g_variant_unref (output);
+    }
+  else
+    {
+      if (!(error->domain == G_DBUS_ERROR &&
+            (error->code == G_DBUS_ERROR_SERVICE_UNKNOWN ||
+             error->code == G_DBUS_ERROR_UNKNOWN_METHOD)))
+        g_warning ("%s", error->message);
+      g_error_free (error);
+    }
+}
diff --git a/panels/printers/pp-utils.h b/panels/printers/pp-utils.h
index 44a12e2..f5ea27f 100644
--- a/panels/printers/pp-utils.h
+++ b/panels/printers/pp-utils.h
@@ -114,6 +114,8 @@ gchar      *printer_get_hostname (cups_ptype_t  printer_type,
                                   const gchar  *device_uri,
                                   const gchar  *printer_uri);
 
+void        printer_set_default_media_size (const gchar *printer_name);
+
 G_END_DECLS
 
 #endif /* __PP_UTILS_H */



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