$OpenBSD: patch-libgnomecups_gnome-cups-printer_c,v 1.3 2009/04/10 16:17:49 ajacoutot Exp $

* Parse ~/.cups/lpoptions.
* Check whether the printer URI starts with http://, ipp://, or smb:// to
  test whether its local or not.
* Replace IPP_SET_PRINTER_ATTRIBUTES with CUPS_ADD_MODIFY_PRINTER.
* Fix PPD retrieval for printers picked up with cups browsing.

--- libgnomecups/gnome-cups-printer.c.orig	Tue Jan  2 01:18:51 2007
+++ libgnomecups/gnome-cups-printer.c	Wed Jan 30 14:59:24 2008
@@ -515,6 +515,10 @@ parse_lpoptions (cups_dest_t **dests)
 	num_dests = cups_get_dests (filename, num_dests, dests);
 	g_free (filename);
 
+	filename = g_build_filename (g_get_home_dir (), ".cups", "lpoptions", NULL);
+	num_dests = cups_get_dests (filename, num_dests, dests);
+	g_free (filename);
+
 	return num_dests;
 }
 
@@ -1030,8 +1034,11 @@ gnome_cups_printer_get_is_local (GnomeCupsPrinter *pri
 {
 	g_return_val_if_fail (GNOME_CUPS_IS_PRINTER (printer), FALSE);
 
-	return (printer->details->device_uri != NULL) && 
-		(strcmp (printer->details->device_uri, "") != 0);
+	return printer->details->device_uri != NULL && 
+		strncmp(printer->details->device_uri, "smb:", 4) != 0 &&
+		strncmp(printer->details->device_uri, "http:", 5) != 0 &&
+		strncmp(printer->details->device_uri, "https:", 5) != 0 &&
+		strncmp(printer->details->device_uri, "ipp:", 4)  != 0;
 }
 
 void
@@ -1152,7 +1159,7 @@ gnome_cups_printer_get_ppd (GnomeCupsPrinter *printer)
 		return NULL;
 	}
 
-	host = _gnome_cups_printer_get_host (printer);
+	host = _gnome_cups_printer_get_ppd_host (printer);
 	ppdpath = get_ppd_uri_path (printer);
 
 	gnome_cups_request_file (host, ppdpath, fd, &error);
@@ -1288,9 +1295,14 @@ gnome_cups_printer_get_description (GnomeCupsPrinter *
 	g_return_val_if_fail (printer->details->description, "");
 
 	return printer->details->description;
-	
 }
 
+
+/* Define the CUPS-Add-Modify-Printer,
+ * see http://www.cups.org/documentation.php/spec-ipp.html#CUPS_ADD_MODIFY_PRINTER
+ */
+#define CUPS_ADD_MODIFY_PRINTER 0x4003
+
 void
 gnome_cups_printer_set_description (GnomeCupsPrinter *printer,
 				    const char *description,
@@ -1306,7 +1318,11 @@ gnome_cups_printer_set_description (GnomeCupsPrinter *
 		return;
 	}
 
-	request = gnome_cups_request_new_for_printer (IPP_SET_PRINTER_ATTRIBUTES,
+	/*
+	 * As read in http://lists.samba.org/archive/samba-technical/2003-February/027044.html 
+	 * CUPS does not currently support IPP_SET_PRINTER_ATTRIBUTES, so a is used
+	 */
+	request = gnome_cups_request_new_for_printer (CUPS_ADD_MODIFY_PRINTER,
 						      printer);
 	ippAddString (request, IPP_TAG_PRINTER, IPP_TAG_TEXT,
 		      "printer-info", NULL, description);
@@ -1338,8 +1354,12 @@ gnome_cups_printer_set_location (GnomeCupsPrinter *pri
 		return;
 	}
 
+	/*
+	 * Same as above (IPP_SET_PRINTER_ATTRIBUTES replaced by
+	 * CUPS-Add-Modify-Printer )
+	 */
 	request = gnome_cups_request_new_for_printer (
-		IPP_SET_PRINTER_ATTRIBUTES, printer);
+		CUPS_ADD_MODIFY_PRINTER, printer);
 	ippAddString (request, IPP_TAG_PRINTER, IPP_TAG_TEXT,
 		"printer-location", NULL, location);
 	response = gnome_cups_request_execute (request, NULL, "/admin/", error);
@@ -2018,5 +2038,28 @@ _gnome_cups_printer_get_host (GnomeCupsPrinter *printe
 		}
 	}
 	
+	return host;
+}
+
+gchar *
+_gnome_cups_printer_get_ppd_host (GnomeCupsPrinter *printer)
+{
+	gchar *host = NULL;
+
+	if (printer->details->printer_uri) {
+		gchar *x, *y;
+
+		x = strstr (printer->details->printer_uri, "://");
+
+		if (x) {
+			x += 3;
+			y = strpbrk (x, ":/");
+			if (y)
+				host = g_strndup (x, y - x);
+			else 
+			host = g_strdup (x);
+		}
+	}
+       
 	return host;
 }
