| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtPrintSupport module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | |
| 41 | #include "qcupsjobwidget_p.h" |
| 42 | |
| 43 | #include <QCheckBox> |
| 44 | #include <QDateTime> |
| 45 | #include <QFontDatabase> |
| 46 | #include <QLabel> |
| 47 | #include <QLayout> |
| 48 | #include <QTime> |
| 49 | #include <QTableWidget> |
| 50 | #include <QTableWidgetItem> |
| 51 | #include <QHeaderView> |
| 52 | #include <QPrinter> |
| 53 | #include <QPrintEngine> |
| 54 | |
| 55 | #include <kernel/qprintdevice_p.h> |
| 56 | |
| 57 | QT_BEGIN_NAMESPACE |
| 58 | |
| 59 | /*! |
| 60 | \internal |
| 61 | \class QCupsJobWidget |
| 62 | |
| 63 | A widget to add to QPrintDialog to enable extra CUPS options |
| 64 | such as Job Scheduling, Job Priority or Job Billing |
| 65 | \ingroup printing |
| 66 | \inmodule QtPrintSupport |
| 67 | */ |
| 68 | |
| 69 | QCupsJobWidget::QCupsJobWidget(QPrinter *printer, QPrintDevice *printDevice, QWidget *parent) |
| 70 | : QWidget(parent), |
| 71 | m_printer(printer), |
| 72 | m_printDevice(printDevice) |
| 73 | { |
| 74 | m_ui.setupUi(this); |
| 75 | //set all the default values |
| 76 | initJobHold(); |
| 77 | initJobBilling(); |
| 78 | initJobPriority(); |
| 79 | initBannerPages(); |
| 80 | |
| 81 | updateSavedValues(); |
| 82 | } |
| 83 | |
| 84 | QCupsJobWidget::~QCupsJobWidget() |
| 85 | { |
| 86 | } |
| 87 | |
| 88 | void QCupsJobWidget::setupPrinter() |
| 89 | { |
| 90 | QCUPSSupport::setJobHold(printer: m_printer, jobHold: jobHold(), holdUntilTime: jobHoldTime()); |
| 91 | QCUPSSupport::setJobBilling(printer: m_printer, jobBilling: jobBilling()); |
| 92 | QCUPSSupport::setJobPriority(printer: m_printer, priority: jobPriority()); |
| 93 | QCUPSSupport::setBannerPages(printer: m_printer, startBannerPage: startBannerPage(), endBannerPage: endBannerPage()); |
| 94 | } |
| 95 | |
| 96 | void QCupsJobWidget::updateSavedValues() |
| 97 | { |
| 98 | m_savedJobHoldWithTime = { jobHold(), jobHoldTime() }; |
| 99 | m_savedJobBilling = jobBilling(); |
| 100 | m_savedPriority = jobPriority(); |
| 101 | m_savedJobSheets = { startBannerPage(), endBannerPage() }; |
| 102 | } |
| 103 | |
| 104 | void QCupsJobWidget::revertToSavedValues() |
| 105 | { |
| 106 | setJobHold(jobHold: m_savedJobHoldWithTime.jobHold, holdUntilTime: m_savedJobHoldWithTime.time); |
| 107 | toggleJobHoldTime(); |
| 108 | |
| 109 | setJobBilling(m_savedJobBilling); |
| 110 | |
| 111 | setJobPriority(m_savedPriority); |
| 112 | |
| 113 | setStartBannerPage(m_savedJobSheets.startBannerPage); |
| 114 | setEndBannerPage(m_savedJobSheets.endBannerPage); |
| 115 | } |
| 116 | |
| 117 | void QCupsJobWidget::initJobHold() |
| 118 | { |
| 119 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Print Immediately" ), auserData: QVariant::fromValue(value: QCUPSSupport::NoHold)); |
| 120 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Hold Indefinitely" ), auserData: QVariant::fromValue(value: QCUPSSupport::Indefinite)); |
| 121 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Day (06:00 to 17:59)" ), auserData: QVariant::fromValue(value: QCUPSSupport::DayTime)); |
| 122 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Night (18:00 to 05:59)" ), auserData: QVariant::fromValue(value: QCUPSSupport::Night)); |
| 123 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Second Shift (16:00 to 23:59)" ), auserData: QVariant::fromValue(value: QCUPSSupport::SecondShift)); |
| 124 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Third Shift (00:00 to 07:59)" ), auserData: QVariant::fromValue(value: QCUPSSupport::ThirdShift)); |
| 125 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Weekend (Saturday to Sunday)" ), auserData: QVariant::fromValue(value: QCUPSSupport::Weekend)); |
| 126 | m_ui.jobHoldComboBox->addItem(atext: tr(s: "Specific Time" ), auserData: QVariant::fromValue(value: QCUPSSupport::SpecificTime)); |
| 127 | |
| 128 | connect(sender: m_ui.jobHoldComboBox, signal: QOverload<int>::of(ptr: &QComboBox::currentIndexChanged), receiver: this, slot: &QCupsJobWidget::toggleJobHoldTime); |
| 129 | |
| 130 | QCUPSSupport::JobHoldUntilWithTime jobHoldWithTime; |
| 131 | |
| 132 | if (m_printDevice) { |
| 133 | const QString jobHoldUntilString = m_printDevice->property(PDPK_CupsJobHoldUntil).toString(); |
| 134 | jobHoldWithTime = QCUPSSupport::parseJobHoldUntil(jobHoldUntil: jobHoldUntilString); |
| 135 | } |
| 136 | |
| 137 | setJobHold(jobHold: jobHoldWithTime.jobHold, holdUntilTime: jobHoldWithTime.time); |
| 138 | toggleJobHoldTime(); |
| 139 | } |
| 140 | |
| 141 | void QCupsJobWidget::setJobHold(QCUPSSupport::JobHoldUntil jobHold, const QTime &holdUntilTime) |
| 142 | { |
| 143 | if (jobHold == QCUPSSupport::SpecificTime && holdUntilTime.isNull()) { |
| 144 | jobHold = QCUPSSupport::NoHold; |
| 145 | toggleJobHoldTime(); |
| 146 | } |
| 147 | m_ui.jobHoldComboBox->setCurrentIndex(m_ui.jobHoldComboBox->findData(data: QVariant::fromValue(value: jobHold))); |
| 148 | m_ui.jobHoldTimeEdit->setTime(holdUntilTime); |
| 149 | } |
| 150 | |
| 151 | QCUPSSupport::JobHoldUntil QCupsJobWidget::jobHold() const |
| 152 | { |
| 153 | return qvariant_cast<QCUPSSupport::JobHoldUntil>(v: m_ui.jobHoldComboBox->itemData(index: m_ui.jobHoldComboBox->currentIndex())); |
| 154 | } |
| 155 | |
| 156 | void QCupsJobWidget::toggleJobHoldTime() |
| 157 | { |
| 158 | if (jobHold() == QCUPSSupport::SpecificTime) |
| 159 | m_ui.jobHoldTimeEdit->setEnabled(true); |
| 160 | else |
| 161 | m_ui.jobHoldTimeEdit->setEnabled(false); |
| 162 | } |
| 163 | |
| 164 | QTime QCupsJobWidget::jobHoldTime() const |
| 165 | { |
| 166 | return m_ui.jobHoldTimeEdit->time(); |
| 167 | } |
| 168 | |
| 169 | void QCupsJobWidget::initJobBilling() |
| 170 | { |
| 171 | QString jobBilling; |
| 172 | if (m_printDevice) |
| 173 | jobBilling = m_printDevice->property(PDPK_CupsJobBilling).toString(); |
| 174 | |
| 175 | setJobBilling(jobBilling); |
| 176 | } |
| 177 | |
| 178 | void QCupsJobWidget::setJobBilling(const QString &jobBilling) |
| 179 | { |
| 180 | m_ui.jobBillingLineEdit->setText(jobBilling); |
| 181 | } |
| 182 | |
| 183 | QString QCupsJobWidget::jobBilling() const |
| 184 | { |
| 185 | return m_ui.jobBillingLineEdit->text(); |
| 186 | } |
| 187 | |
| 188 | void QCupsJobWidget::initJobPriority() |
| 189 | { |
| 190 | int priority = -1; |
| 191 | if (m_printDevice) { |
| 192 | bool ok; |
| 193 | priority = m_printDevice->property(PDPK_CupsJobPriority).toInt(ok: &ok); |
| 194 | if (!ok) |
| 195 | priority = -1; |
| 196 | } |
| 197 | |
| 198 | if (priority < 0 || priority > 100) |
| 199 | priority = 50; |
| 200 | |
| 201 | setJobPriority(priority); |
| 202 | } |
| 203 | |
| 204 | void QCupsJobWidget::setJobPriority(int jobPriority) |
| 205 | { |
| 206 | m_ui.jobPrioritySpinBox->setValue(jobPriority); |
| 207 | } |
| 208 | |
| 209 | int QCupsJobWidget::jobPriority() const |
| 210 | { |
| 211 | return m_ui.jobPrioritySpinBox->value(); |
| 212 | } |
| 213 | |
| 214 | void QCupsJobWidget::initBannerPages() |
| 215 | { |
| 216 | m_ui.startBannerPageCombo->addItem(atext: tr(s: "None" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::NoBanner)); |
| 217 | m_ui.startBannerPageCombo->addItem(atext: tr(s: "Standard" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Standard)); |
| 218 | m_ui.startBannerPageCombo->addItem(atext: tr(s: "Unclassified" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Unclassified)); |
| 219 | m_ui.startBannerPageCombo->addItem(atext: tr(s: "Confidential" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Confidential)); |
| 220 | m_ui.startBannerPageCombo->addItem(atext: tr(s: "Classified" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Classified)); |
| 221 | m_ui.startBannerPageCombo->addItem(atext: tr(s: "Secret" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Secret)); |
| 222 | m_ui.startBannerPageCombo->addItem(atext: tr(s: "Top Secret" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::TopSecret)); |
| 223 | |
| 224 | m_ui.endBannerPageCombo->addItem(atext: tr(s: "None" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::NoBanner)); |
| 225 | m_ui.endBannerPageCombo->addItem(atext: tr(s: "Standard" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Standard)); |
| 226 | m_ui.endBannerPageCombo->addItem(atext: tr(s: "Unclassified" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Unclassified)); |
| 227 | m_ui.endBannerPageCombo->addItem(atext: tr(s: "Confidential" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Confidential)); |
| 228 | m_ui.endBannerPageCombo->addItem(atext: tr(s: "Classified" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Classified)); |
| 229 | m_ui.endBannerPageCombo->addItem(atext: tr(s: "Secret" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::Secret)); |
| 230 | m_ui.endBannerPageCombo->addItem(atext: tr(s: "Top Secret" , c: "CUPS Banner page" ), auserData: QVariant::fromValue(value: QCUPSSupport::TopSecret)); |
| 231 | |
| 232 | QCUPSSupport::JobSheets jobSheets; |
| 233 | |
| 234 | if (m_printDevice) { |
| 235 | const QString jobSheetsString = m_printDevice->property(PDPK_CupsJobSheets).toString(); |
| 236 | jobSheets = QCUPSSupport::parseJobSheets(jobSheets: jobSheetsString); |
| 237 | } |
| 238 | |
| 239 | setStartBannerPage(jobSheets.startBannerPage); |
| 240 | setEndBannerPage(jobSheets.endBannerPage); |
| 241 | } |
| 242 | |
| 243 | void QCupsJobWidget::setStartBannerPage(const QCUPSSupport::BannerPage bannerPage) |
| 244 | { |
| 245 | m_ui.startBannerPageCombo->setCurrentIndex(m_ui.startBannerPageCombo->findData(data: QVariant::fromValue(value: bannerPage))); |
| 246 | } |
| 247 | |
| 248 | QCUPSSupport::BannerPage QCupsJobWidget::startBannerPage() const |
| 249 | { |
| 250 | return qvariant_cast<QCUPSSupport::BannerPage>(v: m_ui.startBannerPageCombo->itemData(index: m_ui.startBannerPageCombo->currentIndex())); |
| 251 | } |
| 252 | |
| 253 | void QCupsJobWidget::setEndBannerPage(const QCUPSSupport::BannerPage bannerPage) |
| 254 | { |
| 255 | m_ui.endBannerPageCombo->setCurrentIndex(m_ui.endBannerPageCombo->findData(data: QVariant::fromValue(value: bannerPage))); |
| 256 | } |
| 257 | |
| 258 | QCUPSSupport::BannerPage QCupsJobWidget::endBannerPage() const |
| 259 | { |
| 260 | return qvariant_cast<QCUPSSupport::BannerPage>(v: m_ui.endBannerPageCombo->itemData(index: m_ui.endBannerPageCombo->currentIndex())); |
| 261 | } |
| 262 | |
| 263 | QT_END_NAMESPACE |
| 264 | |