]> Pileus Git - ~andy/gtk/blobdiff - modules/printbackends/cups/gtkprintbackendcups.c
Change FSF Address
[~andy/gtk] / modules / printbackends / cups / gtkprintbackendcups.c
index 2e1e1a9da82895fb1bfb8f25097dd0a8689ca227..bc1f30508c1fdd7f06ca2e7daba9293ae7ef80fa 100644 (file)
@@ -14,9 +14,7 @@
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
 #ifdef __linux__
@@ -54,6 +52,9 @@
 
 #include "gtkcupsutils.h"
 
+#ifdef HAVE_COLORD
+#include <colord.h>
+#endif
 
 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
 
@@ -92,6 +93,7 @@ typedef struct
 
   http_t *http;
   GtkCupsRequest *request;
+  GtkCupsPollState poll_state;
   GPollFD *data_poll;
   GtkPrintBackendCups *backend;
   GtkPrintCupsResponseCallbackFunc callback;
@@ -125,6 +127,9 @@ struct _GtkPrintBackendCups
   GHashTable *auth;
   gchar      *username;
   gboolean    authentication_lock;
+#ifdef HAVE_COLORD
+  CdClient   *colord_client;
+#endif
 };
 
 static GObjectClass *backend_parent_class;
@@ -482,22 +487,68 @@ cups_print_cb (GtkPrintBackendCups *print_backend,
   GDK_THREADS_LEAVE ();  
 }
 
+typedef struct {
+  GtkCupsRequest *request;
+  GtkPrinterCups *printer;
+} CupsOptionsData;
+
 static void
 add_cups_options (const gchar *key,
                  const gchar *value,
                  gpointer     user_data)
 {
-  GtkCupsRequest *request = user_data;
+  CupsOptionsData *data = (CupsOptionsData *) user_data;
+  GtkCupsRequest *request = data->request;
+  GtkPrinterCups *printer = data->printer;
+  gboolean custom_value = FALSE;
+  gchar *new_value = NULL;
+  gint i;
+
+  if (!key || !value)
+    return;
 
   if (!g_str_has_prefix (key, "cups-"))
     return;
 
   if (strcmp (value, "gtk-ignore-value") == 0)
     return;
-  
+
   key = key + strlen ("cups-");
 
-  gtk_cups_request_encode_option (request, key, value);
+  if (printer && printer->ppd_file)
+    {
+      ppd_coption_t *coption;
+      gboolean       found = FALSE;
+      gboolean       custom_values_enabled = FALSE;
+
+      coption = ppdFindCustomOption (printer->ppd_file, key);
+      if (coption && coption->option)
+        {
+          for (i = 0; i < coption->option->num_choices; i++)
+            {
+              /* Are custom values enabled ? */
+              if (g_str_equal (coption->option->choices[i].choice, "Custom"))
+                custom_values_enabled = TRUE;
+
+              /* Is the value among available choices ? */
+              if (g_str_equal (coption->option->choices[i].choice, value))
+                found = TRUE;
+            }
+
+          if (custom_values_enabled && !found)
+            custom_value = TRUE;
+        }
+    }
+
+  /* Add "Custom." prefix to custom values. */
+  if (custom_value)
+    {
+      new_value = g_strdup_printf ("Custom.%s", value);
+      gtk_cups_request_encode_option (request, key, new_value);
+      g_free (new_value);
+    }
+  else
+    gtk_cups_request_encode_option (request, key, value);
 }
 
 static void
@@ -510,6 +561,7 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
 {
   GtkPrinterCups *cups_printer;
   CupsPrintStreamData *ps;
+  CupsOptionsData *options_data;
   GtkCupsRequest *request;
   GtkPrintSettings *settings;
   const gchar *title;
@@ -557,8 +609,12 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
                                      IPP_TAG_NAME, "job-name", 
                                      NULL, title);
 
-  gtk_print_settings_foreach (settings, add_cups_options, request);
-  
+  options_data = g_new0 (CupsOptionsData, 1);
+  options_data->request = request;
+  options_data->printer = cups_printer;
+  gtk_print_settings_foreach (settings, add_cups_options, options_data);
+  g_free (options_data);
+
   ps = g_new0 (CupsPrintStreamData, 1);
   ps->callback = callback;
   ps->user_data = user_data;
@@ -607,6 +663,10 @@ gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
 
   backend_cups->username = NULL;
 
+#ifdef HAVE_COLORD
+  backend_cups->colord_client = cd_client_new ();
+#endif
+
   cups_get_local_default_printer (backend_cups);
 }
 
@@ -633,6 +693,10 @@ gtk_print_backend_cups_finalize (GObject *object)
 
   g_free (backend_cups->username);
 
+#ifdef HAVE_COLORD
+  g_object_unref (backend_cups->colord_client);
+#endif
+
   backend_parent_class->finalize (object);
 }
 
@@ -790,7 +854,7 @@ request_password (gpointer data)
   gint                       i;
 
   if (dispatch->backend->authentication_lock)
-    return FALSE;
+    return G_SOURCE_REMOVE;
 
   httpGetHostname (dispatch->request->http, hostname, sizeof (hostname));
   if (is_address_local (hostname))
@@ -903,7 +967,7 @@ request_password (gpointer data)
   g_free (auth_info_visible);
   g_free (key);
 
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
 static void
@@ -916,11 +980,20 @@ cups_dispatch_add_poll (GSource *source)
 
   poll_state = gtk_cups_request_get_poll_state (dispatch->request);
 
+  /* Remove the old source if the poll state changed. */
+  if (poll_state != dispatch->poll_state && dispatch->data_poll != NULL)
+    {
+      g_source_remove_poll (source, dispatch->data_poll);
+      g_free (dispatch->data_poll);
+      dispatch->data_poll = NULL;
+    }
+
   if (dispatch->request->http != NULL)
     {
       if (dispatch->data_poll == NULL)
         {
          dispatch->data_poll = g_new0 (GPollFD, 1);
+         dispatch->poll_state = poll_state;
 
          if (poll_state == GTK_CUPS_HTTP_READ)
            dispatch->data_poll->events = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI;
@@ -978,10 +1051,10 @@ check_auth_info (gpointer user_data)
           dispatch->request->auth_info = NULL;
         }
 
-      return FALSE;
+      return G_SOURCE_REMOVE;
     }
 
-  return TRUE;
+  return G_SOURCE_CONTINUE;
 }
 
 static gboolean
@@ -1092,13 +1165,11 @@ cups_dispatch_watch_check (GSource *source)
 
   poll_state = gtk_cups_request_get_poll_state (dispatch->request);
 
-  cups_dispatch_add_poll (source);
-    
   if (poll_state != GTK_CUPS_HTTP_IDLE && !dispatch->request->need_password)
     if (!(dispatch->data_poll->revents & dispatch->data_poll->events)) 
        return FALSE;
   
-  result = gtk_cups_request_read_write (dispatch->request);
+  result = gtk_cups_request_read_write (dispatch->request, FALSE);
   if (result && dispatch->data_poll != NULL)
     {
       g_source_remove_poll (source, dispatch->data_poll);
@@ -1129,8 +1200,8 @@ cups_dispatch_watch_prepare (GSource *source,
             g_print ("CUPS Backend: %s <source %p>\n", G_STRFUNC, source));
 
   *timeout_ = -1;
-  
-  result = gtk_cups_request_read_write (dispatch->request);
+
+  result = gtk_cups_request_read_write (dispatch->request, TRUE);
 
   cups_dispatch_add_poll (source);
 
@@ -1230,7 +1301,12 @@ cups_dispatch_watch_finalize (GSource *source)
       dispatch->backend = NULL;
     }
 
-  g_free (dispatch->data_poll);
+  if (dispatch->data_poll)
+    {
+      g_source_remove_poll (source, dispatch->data_poll);
+      g_free (dispatch->data_poll);
+      dispatch->data_poll = NULL;
+    }
 }
 
 static GSourceFuncs _cups_dispatch_watch_funcs = {
@@ -1259,6 +1335,7 @@ cups_request_execute (GtkPrintBackendCups              *print_backend,
 
   dispatch->request = request;
   dispatch->backend = g_object_ref (print_backend);
+  dispatch->poll_state = GTK_CUPS_HTTP_IDLE;
   dispatch->data_poll = NULL;
   dispatch->callback = NULL;
   dispatch->callback_data = NULL;
@@ -1546,7 +1623,7 @@ cups_job_info_poll_timeout (gpointer user_data)
   else
     cups_request_job_info (data);
   
-  return FALSE;
+  return G_SOURCE_REMOVE;
 }
 
 static void
@@ -1882,7 +1959,13 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
          char *cups_server;            /* CUPS server */
          
           list_has_changed = TRUE;
-         cups_printer = gtk_printer_cups_new (printer_name, backend);
+#ifdef HAVE_COLORD
+          cups_printer = gtk_printer_cups_new (printer_name,
+                                               backend,
+                                               cups_backend->colord_client);
+#else
+          cups_printer = gtk_printer_cups_new (printer_name, backend, NULL);
+#endif
 
          cups_printer->device_uri = g_strdup_printf ("/printers/%s", printer_name);
 
@@ -2074,8 +2157,7 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
      as inactive if it is in the list, emitting a printer_removed signal */
   if (removed_printer_checklist != NULL)
     {
-      g_list_foreach (removed_printer_checklist, (GFunc) mark_printer_inactive, backend);
-      g_list_free (removed_printer_checklist);
+      g_list_free_full (removed_printer_checklist, (GDestroyNotify) mark_printer_inactive);
       list_has_changed = TRUE;
     }
   
@@ -3311,7 +3393,18 @@ create_pickone_option (ppd_file_t   *ppd_file,
              option->choices_display[i] = get_choice_text (ppd_file, available[i]);
            }
        }
-      gtk_printer_option_set (option, ppd_option->defchoice);
+
+      if (option->type != GTK_PRINTER_OPTION_TYPE_PICKONE)
+        {
+          if (g_str_has_prefix (ppd_option->defchoice, "Custom."))
+            gtk_printer_option_set (option, ppd_option->defchoice + 7);
+          else
+            gtk_printer_option_set (option, ppd_option->defchoice);
+        }
+      else
+        {
+          gtk_printer_option_set (option, ppd_option->defchoice);
+        }
     }
 #ifdef PRINT_IGNORED_OPTIONS
   else
@@ -3511,6 +3604,23 @@ handle_group (GtkPrinterOptionSet *set,
 
 }
 
+#ifdef HAVE_COLORD
+
+typedef struct {
+        GtkPrintSettings     *settings;
+        GtkPrinter           *printer;
+} GtkPrintBackendCupsColordHelper;
+
+static void
+colord_printer_option_set_changed_cb (GtkPrinterOptionSet *set,
+                                      GtkPrintBackendCupsColordHelper *helper)
+{
+  gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (helper->printer),
+                                    helper->settings,
+                                    set);
+}
+#endif
+
 static GtkPrinterOptionSet *
 cups_printer_get_options (GtkPrinter           *printer,
                          GtkPrintSettings     *settings,
@@ -3542,7 +3652,9 @@ cups_printer_get_options (GtkPrinter           *printer,
   GtkPrintBackendCups *backend;
   GtkTextDirection text_direction;
   GtkPrinterCups *cups_printer = NULL;
-
+#ifdef HAVE_COLORD
+  GtkPrintBackendCupsColordHelper *helper;
+#endif
 
   set = gtk_printer_option_set_new ();
 
@@ -3808,6 +3920,38 @@ cups_printer_get_options (GtkPrinter           *printer,
 
   cupsFreeOptions (num_opts, opts);
 
+#ifdef HAVE_COLORD
+  /* TRANSLATORS: this this the ICC color profile to use for this job */
+  option = gtk_printer_option_new ("colord-profile",
+                                   _("Printer Profile"),
+                                   GTK_PRINTER_OPTION_TYPE_INFO);
+
+  /* assign it to the color page */
+  option->group = g_strdup ("ColorPage");
+
+  /* TRANSLATORS: this is when color profile information is unavailable */
+  gtk_printer_option_set (option, _("Unavailable"));
+  gtk_printer_option_set_add (set, option);
+
+  /* watch to see if the user changed the options */
+  helper = g_new (GtkPrintBackendCupsColordHelper, 1);
+  helper->printer = printer;
+  helper->settings = settings;
+  g_signal_connect_data (set, "changed",
+                         G_CALLBACK (colord_printer_option_set_changed_cb),
+                         helper,
+                         (GClosureNotify) g_free,
+                         0);
+
+  /* initial coldplug */
+  gtk_printer_cups_update_settings (GTK_PRINTER_CUPS (printer),
+                                    settings, set);
+  g_object_bind_property (printer, "profile-title",
+                          option, "value",
+                          G_BINDING_DEFAULT);
+
+#endif
+
   return set;
 }
 
@@ -4550,7 +4694,12 @@ cups_printer_get_default_page_size (GtkPrinter *printer)
     return NULL;
 
   option = ppdFindOption (ppd_file, "PageSize");
+  if (option == NULL)
+    return NULL;
+
   size = ppdPageSize (ppd_file, option->defchoice); 
+  if (size == NULL)
+    return NULL;
 
   return create_page_setup (ppd_file, size);
 }