[gnome-control-center] printers: Set hostname for devices with no hostname



commit fc0a68430f3790af808e0bb1265cad2427e0e651
Author: Marek Kasik <mkasik redhat com>
Date:   Thu Feb 20 15:17:02 2014 +0100

    printers: Set hostname for devices with no hostname
    
    Guess hostname of found device if it was not provided before.
    
    CUPS browses its printers as "PrinterName @ ComputerName" or
    "PrinterInfo @ ComputerName" through DNS-SD. Get the last part
    of that string and set it as hostname.
    
    HPLIP printers have URIs of form
    hp:/net/PrinterModel?ip=IPAddress&port=Port or
    hp:/net/PrinterModel?ip=IPAddress.
    
    URIs of other protocols are parsed by httpSeparateURI().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=693183

 panels/printers/pp-new-printer-dialog.c |    3 +
 panels/printers/pp-utils.c              |   69 +++++++++++++++++++++++++++++++
 panels/printers/pp-utils.h              |    2 +
 3 files changed, 74 insertions(+), 0 deletions(-)
---
diff --git a/panels/printers/pp-new-printer-dialog.c b/panels/printers/pp-new-printer-dialog.c
index 6e4a0cb..d61511b 100644
--- a/panels/printers/pp-new-printer-dialog.c
+++ b/panels/printers/pp-new-printer-dialog.c
@@ -637,6 +637,9 @@ add_device_to_list (PpNewPrinterDialog *dialog,
 
   if (device)
     {
+      if (device->host_name == NULL)
+        device->host_name = guess_device_hostname (device);
+
       if (device->device_id ||
           device->device_ppd ||
           (device->host_name &&
diff --git a/panels/printers/pp-utils.c b/panels/printers/pp-utils.c
index 243866a..dd361fb 100644
--- a/panels/printers/pp-utils.c
+++ b/panels/printers/pp-utils.c
@@ -4042,3 +4042,72 @@ job_set_hold_until_async (gint          job_id,
                           job_set_hold_until_async_dbus_cb,
                           data);
 }
+
+gchar *
+guess_device_hostname (PpPrintDevice *device)
+{
+  http_uri_status_t  status;
+  char               scheme[HTTP_MAX_URI];
+  char               username[HTTP_MAX_URI];
+  char               hostname[HTTP_MAX_URI];
+  char               resource[HTTP_MAX_URI];
+  int                port;
+  gchar             *result = NULL;
+  gchar             *hostname_begin;
+  gchar             *hostname_end = NULL;
+
+  if (device != NULL && device->device_uri != NULL)
+    {
+      if (g_str_has_prefix (device->device_uri, "socket") ||
+          g_str_has_prefix (device->device_uri, "lpd") ||
+          g_str_has_prefix (device->device_uri, "ipp") ||
+          g_str_has_prefix (device->device_uri, "smb"))
+        {
+          status = httpSeparateURI (HTTP_URI_CODING_ALL,
+                                    device->device_uri,
+                                    scheme, HTTP_MAX_URI,
+                                    username, HTTP_MAX_URI,
+                                    hostname, HTTP_MAX_URI,
+                                    &port,
+                                    resource, HTTP_MAX_URI);
+
+          if (status >= HTTP_URI_STATUS_OK &&
+              hostname[0] != '\0')
+            result = g_strdup (hostname);
+        }
+      else if ((g_str_has_prefix (device->device_uri, "dnssd") ||
+                g_str_has_prefix (device->device_uri, "mdns")) &&
+               device->device_info != NULL)
+        {
+          /*
+           * CUPS browses its printers as
+           * "PrinterName @ ComputerName" or "PrinterInfo @ ComputerName"
+           * through DNS-SD.
+           */
+          hostname_begin = g_strrstr (device->device_info, " @ ");
+          if (hostname_begin != NULL)
+            result = g_strdup (hostname_begin + 3);
+        }
+      else if (g_str_has_prefix (device->device_uri, "hp:/net/") ||
+               g_str_has_prefix (device->device_uri, "hpfax:/net/"))
+        {
+          /*
+           * HPLIP printers have URI of form hp:/net/%s?ip=%s&port=%d
+           * or hp:/net/%s?ip=%s.
+           */
+          hostname_begin = g_strrstr (device->device_uri, "ip=");
+          if (hostname_begin != NULL)
+            {
+              hostname_begin += 3;
+              hostname_end = strstr (hostname_begin, "&");
+            }
+
+          if (hostname_end != NULL)
+            result = g_strndup (hostname_begin, hostname_end - hostname_begin);
+          else
+            result = g_strdup (hostname_begin);
+        }
+    }
+
+  return result;
+}
diff --git a/panels/printers/pp-utils.h b/panels/printers/pp-utils.h
index 19cae49..ed4b113 100644
--- a/panels/printers/pp-utils.h
+++ b/panels/printers/pp-utils.h
@@ -301,6 +301,8 @@ void        get_cups_devices_async (GCancellable *cancellable,
                                     GCDCallback   callback,
                                     gpointer      user_data);
 
+gchar      *guess_device_hostname (PpPrintDevice *device);
+
 G_END_DECLS
 
 #endif /* __PP_UTILS_H */


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