From 83538a360e246ac2242977015e4a374a9f0e21b8 Mon Sep 17 00:00:00 2001
From: Albert Astals Cid <albert.astals.cid@kdab.com>
Date: Wed, 6 Dec 2017 11:28:55 +0100
Subject: [PATCH] CUPS: Use printer job-sheets as default instead of none,none

This also reads the job-sheets from lpoptions if set there for the particular printer

Change-Id: I35aff103261ef58492779071d866e8a15ac78607
Reviewed-by: Laurent Montel <laurent.montel@kdab.com>
Reviewed-by: Frederik Gladhorn <frederik.gladhorn@qt.io>
---
 src/plugins/printsupport/cups/qppdprintdevice.cpp |  2 ++
 src/printsupport/kernel/qcups.cpp                 | 26 +++++++++++++++++++++++
 src/printsupport/kernel/qcups_p.h                 |  8 +++++++
 src/printsupport/widgets/qcupsjobwidget.cpp       | 11 ++++++++--
 4 files changed, 45 insertions(+), 2 deletions(-)

diff --git a/src/plugins/printsupport/cups/qppdprintdevice.cpp b/src/plugins/printsupport/cups/qppdprintdevice.cpp
index 56976a6..e11d301 100644
--- a/src/plugins/printsupport/cups/qppdprintdevice.cpp
+++ b/src/plugins/printsupport/cups/qppdprintdevice.cpp
@@ -429,6 +429,8 @@ QVariant QPpdPrintDevice::property(QPrintDevice::PrintDevicePropertyKey key) con
         return QVariant::fromValue<ppd_file_t *>(m_ppd);
     else if (key == PDPK_CupsJobPriority)
         return printerOption(QStringLiteral("job-priority"));
+    else if (key == PDPK_CupsJobSheets)
+        return printerOption(QStringLiteral("job-sheets"));
 
     return QVariant();
 }
diff --git a/src/printsupport/kernel/qcups.cpp b/src/printsupport/kernel/qcups.cpp
index b9e162a..aa1cdff 100644
--- a/src/printsupport/kernel/qcups.cpp
+++ b/src/printsupport/kernel/qcups.cpp
@@ -150,6 +150,32 @@ static inline QString bannerPageToString(const QCUPSSupport::BannerPage bannerPa
     return QString();
 };
 
+static inline QCUPSSupport::BannerPage stringToBannerPage(const QString &bannerPage)
+{
+    if (bannerPage == QLatin1String("none")) return QCUPSSupport::NoBanner;
+    else if (bannerPage == QLatin1String("standard")) return QCUPSSupport::Standard;
+    else if (bannerPage == QLatin1String("unclassified")) return QCUPSSupport::Unclassified;
+    else if (bannerPage == QLatin1String("confidential")) return QCUPSSupport::Confidential;
+    else if (bannerPage == QLatin1String("classified")) return QCUPSSupport::Classified;
+    else if (bannerPage == QLatin1String("secret")) return QCUPSSupport::Secret;
+    else if (bannerPage == QLatin1String("topsecret")) return QCUPSSupport::TopSecret;
+
+    return QCUPSSupport::NoBanner;
+}
+
+QCUPSSupport::JobSheets QCUPSSupport::parseJobSheets(const QString &jobSheets)
+{
+    JobSheets result;
+
+    const QStringList parts = jobSheets.split(QLatin1Char(','));
+    if (parts.count() == 2) {
+        result.startBannerPage = stringToBannerPage(parts[0]);
+        result.endBannerPage = stringToBannerPage(parts[1]);
+    }
+
+    return result;
+}
+
 void QCUPSSupport::setBannerPages(QPrinter *printer, const BannerPage startBannerPage, const BannerPage endBannerPage)
 {
     QStringList cupsOptions = cupsOptionsList(printer);
diff --git a/src/printsupport/kernel/qcups_p.h b/src/printsupport/kernel/qcups_p.h
index 3abccf2..906a548 100644
--- a/src/printsupport/kernel/qcups_p.h
+++ b/src/printsupport/kernel/qcups_p.h
@@ -70,6 +70,7 @@ QT_BEGIN_NAMESPACE
 #define PDPK_PpdFile         QPrintDevice::PrintDevicePropertyKey(QPrintDevice::PDPK_CustomBase)
 #define PDPK_PpdOption       QPrintDevice::PrintDevicePropertyKey(QPrintDevice::PDPK_CustomBase + 1)
 #define PDPK_CupsJobPriority QPrintDevice::PrintDevicePropertyKey(QPrintDevice::PDPK_CustomBase + 2)
+#define PDPK_CupsJobSheets   QPrintDevice::PrintDevicePropertyKey(QPrintDevice::PDPK_CustomBase + 3)
 
 class Q_PRINTSUPPORT_EXPORT QCUPSSupport
 {
@@ -139,6 +140,13 @@ public:
     static void setPagesPerSheetLayout(QPrinter *printer, const PagesPerSheet pagesPerSheet,
                                        const PagesPerSheetLayout pagesPerSheetLayout);
     static void setPageRange(QPrinter *printer, int pageFrom, int pageTo);
+
+    struct JobSheets
+    {
+        BannerPage startBannerPage = QCUPSSupport::NoBanner;
+        BannerPage endBannerPage = QCUPSSupport::NoBanner;
+    };
+    static JobSheets parseJobSheets(const QString &jobSheets);
 };
 Q_DECLARE_TYPEINFO(QCUPSSupport::JobHoldUntil,        Q_PRIMITIVE_TYPE);
 Q_DECLARE_TYPEINFO(QCUPSSupport::BannerPage,          Q_PRIMITIVE_TYPE);
diff --git a/src/printsupport/widgets/qcupsjobwidget.cpp b/src/printsupport/widgets/qcupsjobwidget.cpp
index f21e229..4535f75 100644
--- a/src/printsupport/widgets/qcupsjobwidget.cpp
+++ b/src/printsupport/widgets/qcupsjobwidget.cpp
@@ -196,8 +196,15 @@ void QCupsJobWidget::initBannerPages()
     m_ui.endBannerPageCombo->addItem(tr("Secret", "CUPS Banner page"),       QVariant::fromValue(QCUPSSupport::Secret));
     m_ui.endBannerPageCombo->addItem(tr("Top Secret", "CUPS Banner page"),   QVariant::fromValue(QCUPSSupport::TopSecret));
 
-    setStartBannerPage(QCUPSSupport::NoBanner);
-    setEndBannerPage(QCUPSSupport::NoBanner);
+    QCUPSSupport::JobSheets jobSheets;
+
+    if (m_printDevice) {
+        const QString jobSheetsString = m_printDevice->property(PDPK_CupsJobSheets).toString();
+        jobSheets = QCUPSSupport::parseJobSheets(jobSheetsString);
+    }
+
+    setStartBannerPage(jobSheets.startBannerPage);
+    setEndBannerPage(jobSheets.endBannerPage);
 }
 
 void QCupsJobWidget::setStartBannerPage(const QCUPSSupport::BannerPage bannerPage)
-- 
2.7.4

