Bug 1184604 - Lazily override printer output format on Linux if not already set. r=karlt a=sylvestre
authorMike Conley <mconley@mozilla.com>
Fri, 31 Jul 2015 13:53:49 -0400
changeset 288839 d00f8aacf0e64a5fd05d21ceae46f32735c34eb2
parent 288838 46c9817524db5f174db20a5291d791074c528c88
child 288840 c11fa31e5fc1cf899d683b29dd27a92d3b4ee67b
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskarlt, sylvestre
bugs1184604
milestone42.0a2
Bug 1184604 - Lazily override printer output format on Linux if not already set. r=karlt a=sylvestre GTK versions prior to 2.24 incorrectly advertise themselves as able to print PDFs, even when they can't. We were wholesale setting the output format to PostScript for these older GTK versions, without giving the user the opportunity to override it. We now lazily determine whether or not the output should be in PostScript, which should give the user the opportunity to override.
widget/gtk/nsDeviceContextSpecG.cpp
widget/gtk/nsPrintSettingsGTK.cpp
widget/gtk/nsPrintSettingsGTK.h
--- a/widget/gtk/nsDeviceContextSpecG.cpp
+++ b/widget/gtk/nsDeviceContextSpecG.cpp
@@ -152,28 +152,16 @@ NS_IMETHODIMP nsDeviceContextSpecGTK::Ge
   gfxSize surfaceSize(width, height);
 
   // Determine the real format with some GTK magic
   if (format == nsIPrintSettings::kOutputFormatNative) {
     if (mIsPPreview) {
       // There is nothing to detect on Print Preview, use PS.
       format = nsIPrintSettings::kOutputFormatPS;
     } else {
-      const gchar* fmtGTK = gtk_print_settings_get(mGtkPrintSettings, GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
-      if (fmtGTK) {
-        if (nsDependentCString(fmtGTK).EqualsIgnoreCase("pdf")) {
-          format = nsIPrintSettings::kOutputFormatPDF;
-        } else {
-          format = nsIPrintSettings::kOutputFormatPS;
-        }
-      }
-    }
-
-    // If we haven't found the format at this point, we're sunk. :(
-    if (format == nsIPrintSettings::kOutputFormatNative) {
       return NS_ERROR_FAILURE;
     }
   }
 
   if (format == nsIPrintSettings::kOutputFormatPDF) {
     surface = new gfxPDFSurface(stream, surfaceSize);
   } else {
     int32_t orientation;
--- a/widget/gtk/nsPrintSettingsGTK.cpp
+++ b/widget/gtk/nsPrintSettingsGTK.cpp
@@ -195,27 +195,54 @@ nsPrintSettingsGTK::SetGtkPrintSettings(
  */
 void
 nsPrintSettingsGTK::SetGtkPrinter(GtkPrinter *aPrinter)
 {
   if (mGTKPrinter)
     g_object_unref(mGTKPrinter);
 
   mGTKPrinter = (GtkPrinter*) g_object_ref(aPrinter);
+}
 
-  // Prior to gtk 2.24, gtk_printer_accepts_pdf() and
-  // gtk_printer_accepts_ps() always returned true regardless of the
-  // printer's capability.
-  bool shouldTrustGTK =
-    (gtk_major_version > 2 ||
-     (gtk_major_version == 2 && gtk_minor_version >= 24));
-  bool acceptsPDF = shouldTrustGTK && gtk_printer_accepts_pdf(mGTKPrinter);
+NS_IMETHODIMP nsPrintSettingsGTK::GetOutputFormat(int16_t *aOutputFormat)
+{
+  NS_ENSURE_ARG_POINTER(aOutputFormat);
+
+  int16_t format;
+  nsresult rv = nsPrintSettings::GetOutputFormat(&format);
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
 
-  SetOutputFormat(acceptsPDF ? nsIPrintSettings::kOutputFormatPDF
-                             : nsIPrintSettings::kOutputFormatPS);
+  if (format == nsIPrintSettings::kOutputFormatNative) {
+    const gchar* fmtGTK =
+      gtk_print_settings_get(mPrintSettings,
+                             GTK_PRINT_SETTINGS_OUTPUT_FILE_FORMAT);
+    if (fmtGTK) {
+      if (nsDependentCString(fmtGTK).EqualsIgnoreCase("pdf")) {
+        format = nsIPrintSettings::kOutputFormatPDF;
+      } else {
+        format = nsIPrintSettings::kOutputFormatPS;
+      }
+    } else if (GTK_IS_PRINTER(mGTKPrinter)) {
+      // Prior to gtk 2.24, gtk_printer_accepts_pdf() and
+      // gtk_printer_accepts_ps() always returned true regardless of the
+      // printer's capability.
+      bool shouldTrustGTK =
+        (gtk_major_version > 2 ||
+         (gtk_major_version == 2 && gtk_minor_version >= 24));
+      bool acceptsPDF = shouldTrustGTK && gtk_printer_accepts_pdf(mGTKPrinter);
+
+      format = acceptsPDF ? nsIPrintSettings::kOutputFormatPDF
+                          : nsIPrintSettings::kOutputFormatPS;
+    }
+  }
+
+  *aOutputFormat = format;
+  return NS_OK;
 }
 
 /**
  * Reimplementation of nsPrintSettings functions so that we get the values
  * from the GTK objects rather than our own variables.
  */
 
 NS_IMETHODIMP nsPrintSettingsGTK::GetPrintRange(int16_t *aPrintRange)
--- a/widget/gtk/nsPrintSettingsGTK.h
+++ b/widget/gtk/nsPrintSettingsGTK.h
@@ -108,16 +108,18 @@ public:
   NS_IMETHOD GetPageRanges(nsTArray<int32_t> &aPages) override;
 
   NS_IMETHOD GetResolution(int32_t *aResolution) override;
   NS_IMETHOD SetResolution(int32_t aResolution) override;
 
   NS_IMETHOD GetDuplex(int32_t *aDuplex) override;
   NS_IMETHOD SetDuplex(int32_t aDuplex) override;
 
+  NS_IMETHOD GetOutputFormat(int16_t *aOutputFormat) override;
+
 protected:
   virtual ~nsPrintSettingsGTK();
 
   nsPrintSettingsGTK(const nsPrintSettingsGTK& src);
   nsPrintSettingsGTK& operator=(const nsPrintSettingsGTK& rhs);
 
   virtual nsresult _Clone(nsIPrintSettings **_retval) override;
   virtual nsresult _Assign(nsIPrintSettings *aPS) override;