[gnome-control-center] printers: Set default printer properly



commit 2b3da7feb859a8d7ea9c4d9fe8fcf2dd1225f1d0
Author: Marek Kasik <mkasik redhat com>
Date:   Tue Apr 12 16:43:36 2011 +0200

    printers: Set default printer properly
    
    Clean "~/.cups/lpoptions" file before setting default printer
    on local CUPS server. Store default printer to "~/.cups/lpoptions"
    if we are connected to a remote CUPS server (#646100).

 panels/printers/cc-printers-panel.c |   62 +++++++++++++++++++++++------------
 panels/printers/pp-utils.c          |   23 +++++++++++++
 panels/printers/pp-utils.h          |    2 +
 3 files changed, 66 insertions(+), 21 deletions(-)
---
diff --git a/panels/printers/cc-printers-panel.c b/panels/printers/cc-printers-panel.c
index 730aee5..9356963 100644
--- a/panels/printers/cc-printers-panel.c
+++ b/panels/printers/cc-printers-panel.c
@@ -1996,6 +1996,7 @@ printer_set_default_cb (GtkToggleButton *button,
   GError                 *error = NULL;
   char                   *ret_error = NULL;
   char                   *name = NULL;
+  const char             *cups_server;
 
   priv = PRINTERS_PANEL_PRIVATE (self);
 
@@ -2006,35 +2007,54 @@ printer_set_default_cb (GtkToggleButton *button,
 
   if (name)
     {
-      proxy = get_dbus_proxy (MECHANISM_BUS,
-                              "/",
-                              MECHANISM_BUS,
-                              TRUE);
+      cups_server = cupsServer ();
+      if (g_ascii_strncasecmp (cups_server, "localhost", 9) == 0 ||
+          g_ascii_strncasecmp (cups_server, "127.0.0.1", 9) == 0 ||
+          g_ascii_strncasecmp (cups_server, "::1", 3) == 0 ||
+          cups_server[0] == '/')
+        {
+          /* Clean .cups/lpoptions before setting
+           * default printer on local CUPS server.
+           */
+          set_local_default_printer (NULL);
 
-      if (!proxy)
-        return;
+          proxy = get_dbus_proxy (MECHANISM_BUS,
+                                  "/",
+                                  MECHANISM_BUS,
+                                  TRUE);
 
-      dbus_g_proxy_call (proxy, "PrinterSetDefault", &error,
-                         G_TYPE_STRING, name,
-                         G_TYPE_INVALID,
-                         G_TYPE_STRING, &ret_error,
-                         G_TYPE_INVALID);
+          if (!proxy)
+            return;
 
-      g_object_unref (proxy);
+          dbus_g_proxy_call (proxy, "PrinterSetDefault", &error,
+                             G_TYPE_STRING, name,
+                             G_TYPE_INVALID,
+                             G_TYPE_STRING, &ret_error,
+                             G_TYPE_INVALID);
 
-      if (error || (ret_error && ret_error[0] != '\0'))
-        {
-          if (error)
-            g_warning ("%s", error->message);
+          g_object_unref (proxy);
 
-          if (ret_error && ret_error[0] != '\0')
-            g_warning ("%s", ret_error);
+          if (error || (ret_error && ret_error[0] != '\0'))
+            {
+              if (error)
+                g_warning ("%s", error->message);
 
+              if (ret_error && ret_error[0] != '\0')
+                g_warning ("%s", ret_error);
+            }
+          else
+            actualize_printers_list (self);
+
+          g_clear_error (&error);
         }
       else
-        actualize_printers_list (self);
-
-      g_clear_error (&error);
+        /* Store default printer to .cups/lpoptions
+         * if we are connected to a remote CUPS server.
+         */
+        {
+          set_local_default_printer (name);
+          actualize_printers_list (self);
+        }
 
       g_signal_handlers_block_by_func (G_OBJECT (button), printer_set_default_cb, self);
       gtk_toggle_button_set_active (button, priv->dests[priv->current_dest].is_default);
diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c
index f9bd48f..3b221bd 100644
--- a/panels/printers/pp-utils.c
+++ b/panels/printers/pp-utils.c
@@ -480,3 +480,26 @@ renew_cups_subscription (gint id,
 
   return result;
 }
+
+/*  Set default destination in ~/.cups/lpoptions.
+ *  Unset default destination if "dest" is NULL.
+ */
+void
+set_local_default_printer (gchar *printer_name)
+{
+  cups_dest_t *dests = NULL;
+  int          num_dests = 0;
+  int          i;
+
+  num_dests = cupsGetDests (&dests);
+
+  for (i = 0; i < num_dests; i ++)
+    {
+      if (printer_name && g_strcmp0 (dests[i].name, printer_name) == 0)
+        dests[i].is_default = 1;
+      else
+        dests[i].is_default = 0;
+    }
+
+  cupsSetDests (num_dests, dests);
+}
diff --git a/panels/printers/pp-utils.h b/panels/printers/pp-utils.h
index 9e3749d..6097367 100644
--- a/panels/printers/pp-utils.h
+++ b/panels/printers/pp-utils.h
@@ -61,6 +61,8 @@ gint        renew_cups_subscription (gint id,
                                      gint num_events,
                                      gint lease_duration);
 
+void        set_local_default_printer (gchar *printer_name);
+
 G_END_DECLS
 
 #endif /* __PP_UTILS_H */



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