]> Pileus Git - ~andy/gtk/blobdiff - modules/printbackends/cups/gtkprintbackendcups.c
Replace a lot of idle and timeout calls by the new gdk_threads api.
[~andy/gtk] / modules / printbackends / cups / gtkprintbackendcups.c
index 722dcb03427a885342e19c8ebba9161226dd4878..5aab327f47f02c4ce39b76e7e938d5db39c22d58 100644 (file)
@@ -49,6 +49,7 @@
 #include "gtkcupsutils.h"
 #include "gtkdebug.h"
 
+
 typedef struct _GtkPrintBackendCupsClass GtkPrintBackendCupsClass;
 
 #define GTK_PRINT_BACKEND_CUPS_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_BACKEND_CUPS, GtkPrintBackendCupsClass))
@@ -448,7 +449,8 @@ gtk_print_backend_cups_print_stream (GtkPrintBackend         *print_backend,
 static void
 gtk_print_backend_cups_init (GtkPrintBackendCups *backend_cups)
 {
-  backend_cups->list_printers_poll = 0;  
+  backend_cups->list_printers_poll = FALSE;  
+  backend_cups->got_default_printer = FALSE;  
   backend_cups->list_printers_pending = FALSE;
 
   cups_request_default_printer (backend_cups);
@@ -964,7 +966,8 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
   if (gtk_cups_result_is_error (result))
     {
       g_warning ("Error getting printer list: %s", gtk_cups_result_get_error_string (result));
-      return;
+
+      goto done;
     }
   
   /* Gather the names of the printers in the current queue
@@ -1074,11 +1077,11 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
                        resource);
 #endif
 
-          if (member_uris && !strncmp (resource, "/printers/", 10))
+          if (!strncmp (resource, "/printers/", 10))
            {
              cups_printer->ppd_name = g_strdup (resource + 10);
               GTK_NOTE (PRINTING,
-                        g_print ("CUPS Backend: Setting ppd name '%s' for printer class '%s'\n", cups_printer->ppd_name, printer_name));
+                        g_print ("CUPS Backend: Setting ppd name '%s' for printer/class '%s'\n", cups_printer->ppd_name, printer_name));
             }
 
          gethostname (uri, sizeof(uri));
@@ -1133,6 +1136,7 @@ cups_request_printer_list_cb (GtkPrintBackendCups *cups_backend,
       list_has_changed = TRUE;
     }
   
+done:
   if (list_has_changed)
     g_signal_emit_by_name (backend, "printer-list-changed");
   
@@ -1154,6 +1158,9 @@ cups_request_printer_list (GtkPrintBackendCups *cups_backend)
       !cups_backend->got_default_printer)
     return TRUE;
 
+  g_object_ref (cups_backend);
+  GDK_THREADS_LEAVE ();
+
   cups_backend->list_printers_pending = TRUE;
 
   request = gtk_cups_request_new (NULL,
@@ -1172,6 +1179,8 @@ cups_request_printer_list (GtkPrintBackendCups *cups_backend)
                         (GtkPrintCupsResponseCallbackFunc) cups_request_printer_list_cb,
                        request,
                        NULL);
+  GDK_THREADS_ENTER ();
+  g_object_unref (cups_backend);
 
   return TRUE;
 }
@@ -1185,7 +1194,7 @@ cups_get_printer_list (GtkPrintBackend *backend)
   if (cups_backend->list_printers_poll == 0)
     {
       cups_request_printer_list (cups_backend);
-      cups_backend->list_printers_poll = g_timeout_add (3000 * 100000,
+      cups_backend->list_printers_poll = gdk_threads_add_timeout (3000,
                                                         (GSourceFunc) cups_request_printer_list,
                                                         backend);
     }
@@ -1661,6 +1670,16 @@ value_is_off (const char *value)
           strcasecmp (value, "False") == 0);
 }
 
+static char *
+ppd_group_name (ppd_group_t *group)
+{
+#if CUPS_VERSION_MAJOR > 1 || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR > 1) || (CUPS_VERSION_MAJOR == 1 && CUPS_VERSION_MINOR == 1 && CUPS_VERSION_PATCH >= 18) 
+  return group->name;
+#else
+  return group->text;
+#endif
+}
+
 static int
 available_choices (ppd_file_t     *ppd,
                   ppd_option_t   *option,
@@ -1686,7 +1705,10 @@ available_choices (ppd_file_t     *ppd,
   installed_options = NULL;
   for (i = 0; i < ppd->num_groups; i++)
     {
-      if (strcmp (ppd->groups[i].name, "InstallableOptions") == 0)
+      char *name; 
+
+      name = ppd_group_name (&ppd->groups[i]);
+      if (strcmp (name, "InstallableOptions") == 0)
        {
          installed_options = &ppd->groups[i];
          break;
@@ -1824,18 +1846,19 @@ available_choices (ppd_file_t     *ppd,
   return option->num_choices - num_conflicts + add_auto;
 }
 
-#if HAVE_CUPS_API_1_2
 static GtkPrinterOption *
-create_pickone_option_custom (ppd_file_t   *ppd_file,
-                             ppd_option_t *ppd_option,
-                             ppd_coption_t *coption,
-                             const gchar  *gtk_name)
+create_pickone_option (ppd_file_t   *ppd_file,
+                      ppd_option_t *ppd_option,
+                      const gchar  *gtk_name)
 {
   GtkPrinterOption *option;
   ppd_choice_t **available;
   char *label;
   int n_choices;
   int i;
+#ifdef HAVE_CUPS_API_1_2
+  ppd_coption_t *coption;
+#endif
 
   g_assert (ppd_option->ui == PPD_UI_PICKONE);
   
@@ -1844,15 +1867,20 @@ create_pickone_option_custom (ppd_file_t   *ppd_file,
   n_choices = available_choices (ppd_file, ppd_option, &available, g_str_has_prefix (gtk_name, "gtk-"));
   if (n_choices > 0)
     {
-      ppd_cparam_t *cparam;
       
-      label = get_option_text (ppd_file, ppd_option);
-
       /* right now only support one parameter per custom option 
        * if more than one print warning and only offer the default choices
        */
+
+      label = get_option_text (ppd_file, ppd_option);
+
+#ifdef HAVE_CUPS_API_1_2
+      coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
+
       if (coption)
         {
+         ppd_cparam_t *cparam;
+
           cparam = ppdFirstCustomParam (coption);
 
           if (ppdNextCustomParam (coption) == NULL)
@@ -1893,6 +1921,7 @@ create_pickone_option_custom (ppd_file_t   *ppd_file,
          else
            g_warning ("Not Supported: PPD Custom Option has more than one parameter");
        }
+#endif /* HAVE_CUPS_API_1_2 */
 
       if (!option)
         option = gtk_printer_option_new (gtk_name, label,
@@ -1924,19 +1953,6 @@ create_pickone_option_custom (ppd_file_t   *ppd_file,
 
   return option;
 }
-#endif /* HAVE_CUPS_API_1_2 */
-
-static GtkPrinterOption *
-create_pickone_option (ppd_file_t   *ppd_file,
-                      ppd_option_t *ppd_option,
-                      const gchar  *gtk_name)
-{
-  GtkPrinterOption *option;  
-
-  option = create_pickone_option_custom (ppd_file, ppd_option, NULL, gtk_name);
-
-  return option;
-}
 
 static GtkPrinterOption *
 create_boolean_option (ppd_file_t   *ppd_file,
@@ -2019,11 +2035,6 @@ handle_option (GtkPrinterOptionSet *set,
   GtkPrinterOption *option;
   char *name;
 
-#ifdef HAVE_CUPS_API_1_2
-  ppd_coption_t *coption = NULL;
-  coption = ppdFindCustomOption (ppd_file, ppd_option->keyword);
-#endif
-
   if (STRING_IN_TABLE (ppd_option->keyword, cups_option_blacklist))
     return;
 
@@ -2032,12 +2043,7 @@ handle_option (GtkPrinterOptionSet *set,
   option = NULL;
   if (ppd_option->ui == PPD_UI_PICKONE)
     {
-#ifdef HAVE_CUPS_API_1_2
-      if (coption)
-        option = create_pickone_option_custom (ppd_file, ppd_option, coption, name);
-      else
-#endif
-        option = create_pickone_option (ppd_file, ppd_option, name);
+      option = create_pickone_option (ppd_file, ppd_option, name);
     }
   else if (ppd_option->ui == PPD_UI_BOOLEAN)
     {
@@ -2049,21 +2055,24 @@ handle_option (GtkPrinterOptionSet *set,
   
   if (option)
     {
-      if (STRING_IN_TABLE (toplevel_group->name,
+      char *name;
+
+      name = ppd_group_name (toplevel_group);
+      if (STRING_IN_TABLE (name,
                           color_group_whitelist) ||
          STRING_IN_TABLE (ppd_option->keyword,
                           color_option_whitelist))
        {
          option->group = g_strdup ("ColorPage");
        }
-      else if (STRING_IN_TABLE (toplevel_group->name,
+      else if (STRING_IN_TABLE (name,
                                image_quality_group_whitelist) ||
               STRING_IN_TABLE (ppd_option->keyword,
                                image_quality_option_whitelist))
        {
          option->group = g_strdup ("ImageQualityPage");
        }
-      else if (STRING_IN_TABLE (toplevel_group->name,
+      else if (STRING_IN_TABLE (name,
                                finishing_group_whitelist) ||
               STRING_IN_TABLE (ppd_option->keyword,
                                finishing_option_whitelist))
@@ -2091,9 +2100,11 @@ handle_group (GtkPrinterOptionSet *set,
              GtkPrintSettings    *settings)
 {
   gint i;
-
+  gchar *name;
+  
   /* Ignore installable options */
-  if (strcmp (toplevel_group->name, "InstallableOptions") == 0)
+  name = ppd_group_name (toplevel_group);
+  if (strcmp (name, "InstallableOptions") == 0)
     return;
   
   for (i = 0; i < group->num_options; i++)
@@ -2190,14 +2201,27 @@ cups_printer_get_options (GtkPrinter           *printer,
     {
       GtkPaperSize *paper_size;
       ppd_option_t *option;
+      const gchar  *ppd_name;
 
       ppdMarkDefaults (ppd_file);
 
       paper_size = gtk_page_setup_get_paper_size (page_setup);
 
       option = ppdFindOption (ppd_file, "PageSize");
-      strncpy (option->defchoice, gtk_paper_size_get_ppd_name (paper_size),
-              PPD_MAX_NAME);
+      ppd_name = gtk_paper_size_get_ppd_name (paper_size);
+      
+      if (ppd_name)
+       strncpy (option->defchoice, ppd_name, PPD_MAX_NAME);
+      else
+        {
+          gchar *custom_name;
+
+         custom_name = g_strdup_printf (_("Custom.%2fx%.2f"),
+                                        gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS),
+                                        gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS));
+          strncpy (option->defchoice, custom_name, PPD_MAX_NAME);
+          g_free (custom_name);
+        }
 
       for (i = 0; i < ppd_file->num_groups; i++)
         handle_group (set, ppd_file, &ppd_file->groups[i], &ppd_file->groups[i], settings);