LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File 0001-Add-remote-print-queue-support.patch of Package libqt5-qtbase-5.9.0 (Project home:jayvdb:Qt5)

From d3faa880d19d9bf5b411dc81c0d5d881a323997d Mon Sep 17 00:00:00 2001
From: Antonio Larrosa <larrosa@kde.org>
Date: Tue, 6 Jun 2017 16:34:32 +0200
Subject: [PATCH] Add remote print queue support

Cups servers which announce themselves on avahi will be shown in
the printer dialog. This adds a delay the first time the print dialog
is opened in order to search for print queues . If you don't have any
network printer queue and you find the delay too annoying, it can be
disabled by setting the QT_DISABLE_PRINTER_DISCOVERY environment variable
to 1

Change-Id: Ib70715d331e8f380a3c9039011bb8521986652aa
---
 src/plugins/printsupport/cups/qcupsprintengine.cpp | 35 +++++++-
 .../printsupport/cups/qcupsprintersupport.cpp      | 95 ++++++++++++++++++++--
 .../printsupport/cups/qcupsprintersupport_p.h      |  8 ++
 src/plugins/printsupport/cups/qppdprintdevice.cpp  | 33 +++++++-
 4 files changed, 160 insertions(+), 11 deletions(-)

diff --git a/src/plugins/printsupport/cups/qcupsprintengine.cpp b/src/plugins/printsupport/cups/qcupsprintengine.cpp
index a16eb3abb5..6dc123fb51 100644
--- a/src/plugins/printsupport/cups/qcupsprintengine.cpp
+++ b/src/plugins/printsupport/cups/qcupsprintengine.cpp
@@ -48,6 +48,7 @@
 #include <qbuffer.h>
 #include "private/qcups_p.h" // Only needed for PPK_CupsOptions
 #include <QtGui/qpagelayout.h>
+#include "qcupsprintersupport_p.h"
 
 #include <cups/cups.h>
 
@@ -241,8 +242,40 @@ void QCupsPrintEnginePrivate::closePrintDevice()
 
         // Print the file.
         cups_option_t* optPtr = cupsOptStruct.size() ? &cupsOptStruct.first() : 0;
-        cupsPrintFile(printerName.toLocal8Bit().constData(), tempFile.toLocal8Bit().constData(),
+
+        bool fallbackToLocal = false;
+        cups_dest_t *cupsDest = NULL;
+
+        if (qEnvironmentVariableIsSet("QT_DISABLE_PRINTER_DISCOVERY")) {
+            fallbackToLocal = true;
+        } else {
+            cupsDest = cupsGetDest(printerName.toLocal8Bit(), NULL, QCupsPrinterSupport::cupsPrintersCount(), QCupsPrinterSupport::cupsPrinters());
+        }
+
+        if (cupsDest) {
+            char resource[HTTP_MAX_URI];
+            http_t *http = cupsConnectDest (cupsDest, 0, -1, 0,
+                                         resource, sizeof (resource),
+                                         0, 0);
+            if (http) {
+                char *name = strrchr (resource, '/');
+                qDebug() << "resource:" << resource << "," << name;
+                if (name)
+                    cupsPrintFile2 (http, ++name, tempFile.toLocal8Bit().constData(),
                       title.toLocal8Bit().constData(), cupsOptStruct.size(), optPtr);
+                httpClose(http);
+            } else {
+                fallbackToLocal=true;
+            }
+        }
+        else {
+            fallbackToLocal=true;
+        }
+
+        if (fallbackToLocal) {
+            cupsPrintFile(printerName.toLocal8Bit().constData(), tempFile.toLocal8Bit().constData(),
+                          title.toLocal8Bit().constData(), cupsOptStruct.size(), optPtr);
+        }
 
         QFile::remove(tempFile);
     }
diff --git a/src/plugins/printsupport/cups/qcupsprintersupport.cpp b/src/plugins/printsupport/cups/qcupsprintersupport.cpp
index 1887625406..a145beaf18 100644
--- a/src/plugins/printsupport/cups/qcupsprintersupport.cpp
+++ b/src/plugins/printsupport/cups/qcupsprintersupport.cpp
@@ -52,6 +52,35 @@
 # include <cups/language.h>
 #endif
 
+typedef struct
+{
+    cups_dest_t *printers;
+    int num_printers;
+} EnumDestsContext;
+
+static int enum_dest_cb2 (void *user_data, unsigned flags, cups_dest_t *dest)
+{
+   EnumDestsContext *context = (EnumDestsContext *) user_data;
+   if ((flags & (CUPS_DEST_FLAGS_UNCONNECTED |
+                 CUPS_DEST_FLAGS_REMOVED |
+                 CUPS_DEST_FLAGS_ERROR |
+                 CUPS_DEST_FLAGS_RESOLVING |
+                 CUPS_DEST_FLAGS_CONNECTING |
+                 CUPS_DEST_FLAGS_CANCELED)) == 0) {
+
+       context->num_printers = cupsCopyDest (dest, context->num_printers,
+                                              &context->printers);
+
+       // Also copy whether this is the local default /
+       cups_dest_t *the_dest;
+       the_dest = cupsGetDest(dest->name, dest->instance,
+                               context->num_printers, context->printers);
+       the_dest->is_default = dest->is_default;
+       qDebug() << dest->name << "_" << dest->instance << "_" << context->num_printers;
+   }
+   return 1;
+}
+
 QT_BEGIN_NAMESPACE
 
 QCupsPrinterSupport::QCupsPrinterSupport()
@@ -81,17 +110,35 @@ QPrintDevice QCupsPrinterSupport::createPrintDevice(const QString &id)
 
 QStringList QCupsPrinterSupport::availablePrintDeviceIds() const
 {
-    QStringList list;
+/*  // Reset cache disabled for now
+    if (qt_cups_printers) {
+        cupsFreeDests( qt_cups_num_printers, qt_cups_printers );
+        qt_cups_printers = NULL;
+        qt_cups_num_printers = 0;
+    }
+*/
     cups_dest_t *dests;
-    int count = cupsGetDests(&dests);
-    list.reserve(count);
-    for (int i = 0; i < count; ++i) {
+    bool disablePrinterDiscovery = qEnvironmentVariableIsSet("QT_DISABLE_PRINTER_DISCOVERY");
+    if (disablePrinterDiscovery) {
+        qt_cups_num_printers = cupsGetDests(&dests);
+    } else {
+        if (qt_cups_num_printers == 0)
+            QCupsPrinterSupport::fillCupsPrinters();
+
+        dests = qt_cups_printers;
+    }
+    QStringList list;
+    list.reserve(qt_cups_num_printers);
+    for (int i = 0; i < qt_cups_num_printers; ++i) {
         QString printerId = QString::fromLocal8Bit(dests[i].name);
         if (dests[i].instance)
             printerId += QLatin1Char('/') + QString::fromLocal8Bit(dests[i].instance);
         list.append(printerId);
     }
-    cupsFreeDests(count, dests);
+    if (disablePrinterDiscovery) {
+        cupsFreeDests(qt_cups_num_printers, dests);
+    }
+
     return list;
 }
 
@@ -99,8 +146,18 @@ QString QCupsPrinterSupport::defaultPrintDeviceId() const
 {
     QString printerId;
     cups_dest_t *dests;
-    int count = cupsGetDests(&dests);
-    for (int i = 0; i < count; ++i) {
+
+    bool disablePrinterDiscovery = qEnvironmentVariableIsSet("QT_DISABLE_PRINTER_DISCOVERY");
+    if (disablePrinterDiscovery) {
+        qt_cups_num_printers = cupsGetDests(&dests);
+    } else {
+        if (qt_cups_num_printers == 0)
+            QCupsPrinterSupport::fillCupsPrinters();
+
+        dests = qt_cups_printers;
+    }
+
+    for (int i = 0; i < qt_cups_num_printers; ++i) {
         if (dests[i].is_default) {
             printerId = QString::fromLocal8Bit(dests[i].name);
             if (dests[i].instance) {
@@ -109,8 +166,30 @@ QString QCupsPrinterSupport::defaultPrintDeviceId() const
             }
         }
     }
-    cupsFreeDests(count, dests);
+    if (disablePrinterDiscovery) {
+        cupsFreeDests(qt_cups_num_printers, dests);
+    }
     return printerId;
 }
 
+void QCupsPrinterSupport::fillCupsPrinters()
+{
+    EnumDestsContext context;
+    context.printers = 0;
+    context.num_printers = 0;
+
+    qDebug() << "begin enumerating printers";
+
+    cupsEnumDests(CUPS_DEST_FLAGS_NONE, 4000, NULL, 0, 0,
+                   enum_dest_cb2, &context);
+
+    qDebug() << "end enumerating printers";
+    qt_cups_printers = context.printers;
+    qt_cups_num_printers = context.num_printers;
+}
+
+cups_dest_t *QCupsPrinterSupport::qt_cups_printers = NULL;
+int QCupsPrinterSupport::qt_cups_num_printers = 0;
+
+
 QT_END_NAMESPACE
diff --git a/src/plugins/printsupport/cups/qcupsprintersupport_p.h b/src/plugins/printsupport/cups/qcupsprintersupport_p.h
index 13f64b5e69..81eaa09882 100644
--- a/src/plugins/printsupport/cups/qcupsprintersupport_p.h
+++ b/src/plugins/printsupport/cups/qcupsprintersupport_p.h
@@ -56,6 +56,8 @@
 
 #include <QtCore/qstringlist.h>
 
+#include <cups/cups.h>
+
 QT_BEGIN_NAMESPACE
 
 class QCupsPrinterSupport : public QPlatformPrinterSupport
@@ -71,8 +73,14 @@ public:
     QStringList availablePrintDeviceIds() const Q_DECL_OVERRIDE;
     QString defaultPrintDeviceId() const Q_DECL_OVERRIDE;
 
+    static void fillCupsPrinters();
+    static cups_dest_t *cupsPrinters() { return qt_cups_printers; };
+    static int cupsPrintersCount() { return qt_cups_num_printers; };
 private:
     QString cupsOption(int i, const QString &key) const;
+
+    static cups_dest_t *qt_cups_printers;
+    static int qt_cups_num_printers;
 };
 
 QT_END_NAMESPACE
diff --git a/src/plugins/printsupport/cups/qppdprintdevice.cpp b/src/plugins/printsupport/cups/qppdprintdevice.cpp
index 9efa83d409..5b7222902d 100644
--- a/src/plugins/printsupport/cups/qppdprintdevice.cpp
+++ b/src/plugins/printsupport/cups/qppdprintdevice.cpp
@@ -41,6 +41,7 @@
 
 #include <QtCore/QMimeDatabase>
 #include <qdebug.h>
+#include "qcupsprintersupport_p.h"
 
 #ifndef QT_LINUXBASE // LSB merges everything into cups.h
 #include <cups/language.h>
@@ -451,10 +452,38 @@ void QPpdPrintDevice::loadPrinter()
         m_cupsDest = 0;
     }
 
+    bool disablePrinterDiscovery = qEnvironmentVariableIsSet("QT_DISABLE_PRINTER_DISCOVERY");
+
     // Get the print instance and PPD file
-    m_cupsDest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, m_cupsName, m_cupsInstance);
+
+    if (disablePrinterDiscovery) {
+        m_cupsDest = cupsGetNamedDest(CUPS_HTTP_DEFAULT, m_cupsName, m_cupsInstance);
+    } else {
+        cups_dest_t *cupsDest = cupsGetDest( m_cupsName,
+                                         (m_cupsInstance.isEmpty() ? NULL : m_cupsInstance.data()),
+                                         QCupsPrinterSupport::cupsPrintersCount(),
+                                         QCupsPrinterSupport::cupsPrinters() );
+        cupsCopyDest(cupsDest, 0, &m_cupsDest);
+    }
     if (m_cupsDest) {
-        const char *ppdFile = cupsGetPPD(m_cupsName);
+        char resource[HTTP_MAX_URI];
+        http_t *http = NULL;
+        const char *ppdFile = NULL;
+
+        if (!disablePrinterDiscovery) {
+            http = cupsConnectDest (m_cupsDest, 0, -1, 0,
+                             resource, sizeof (resource),
+                             0, 0);
+        }
+        if (http) {
+            char *name = strrchr (resource, '/');
+            if (name)
+                ppdFile = cupsGetPPD2 (http, ++name);
+            httpClose(http);
+        } else {
+            ppdFile = cupsGetPPD(m_cupsName);
+        }
+
         if (ppdFile) {
             m_ppd = ppdOpenFile(ppdFile);
             unlink(ppdFile);
-- 
2.13.0