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 
QCupsJobWidget(QPrinter * printer,QPrintDevice * printDevice,QWidget * parent)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 
~QCupsJobWidget()84 QCupsJobWidget::~QCupsJobWidget()
85 {
86 }
87 
setupPrinter()88 void QCupsJobWidget::setupPrinter()
89 {
90     QCUPSSupport::setJobHold(m_printer, jobHold(), jobHoldTime());
91     QCUPSSupport::setJobBilling(m_printer, jobBilling());
92     QCUPSSupport::setJobPriority(m_printer, jobPriority());
93     QCUPSSupport::setBannerPages(m_printer, startBannerPage(), endBannerPage());
94 }
95 
updateSavedValues()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 
revertToSavedValues()104 void QCupsJobWidget::revertToSavedValues()
105 {
106     setJobHold(m_savedJobHoldWithTime.jobHold, 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 
initJobHold()117 void QCupsJobWidget::initJobHold()
118 {
119     m_ui.jobHoldComboBox->addItem(tr("Print Immediately"),             QVariant::fromValue(QCUPSSupport::NoHold));
120     m_ui.jobHoldComboBox->addItem(tr("Hold Indefinitely"),             QVariant::fromValue(QCUPSSupport::Indefinite));
121     m_ui.jobHoldComboBox->addItem(tr("Day (06:00 to 17:59)"),          QVariant::fromValue(QCUPSSupport::DayTime));
122     m_ui.jobHoldComboBox->addItem(tr("Night (18:00 to 05:59)"),        QVariant::fromValue(QCUPSSupport::Night));
123     m_ui.jobHoldComboBox->addItem(tr("Second Shift (16:00 to 23:59)"), QVariant::fromValue(QCUPSSupport::SecondShift));
124     m_ui.jobHoldComboBox->addItem(tr("Third Shift (00:00 to 07:59)"),  QVariant::fromValue(QCUPSSupport::ThirdShift));
125     m_ui.jobHoldComboBox->addItem(tr("Weekend (Saturday to Sunday)"),  QVariant::fromValue(QCUPSSupport::Weekend));
126     m_ui.jobHoldComboBox->addItem(tr("Specific Time"),                 QVariant::fromValue(QCUPSSupport::SpecificTime));
127 
128     connect(m_ui.jobHoldComboBox, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &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(jobHoldUntilString);
135     }
136 
137     setJobHold(jobHoldWithTime.jobHold, jobHoldWithTime.time);
138     toggleJobHoldTime();
139 }
140 
setJobHold(QCUPSSupport::JobHoldUntil jobHold,const QTime & holdUntilTime)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(QVariant::fromValue(jobHold)));
148     m_ui.jobHoldTimeEdit->setTime(holdUntilTime);
149 }
150 
jobHold() const151 QCUPSSupport::JobHoldUntil QCupsJobWidget::jobHold() const
152 {
153     return qvariant_cast<QCUPSSupport::JobHoldUntil>(m_ui.jobHoldComboBox->itemData(m_ui.jobHoldComboBox->currentIndex()));
154 }
155 
toggleJobHoldTime()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 
jobHoldTime() const164 QTime QCupsJobWidget::jobHoldTime() const
165 {
166     return m_ui.jobHoldTimeEdit->time();
167 }
168 
initJobBilling()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 
setJobBilling(const QString & jobBilling)178 void QCupsJobWidget::setJobBilling(const QString &jobBilling)
179 {
180     m_ui.jobBillingLineEdit->setText(jobBilling);
181 }
182 
jobBilling() const183 QString QCupsJobWidget::jobBilling() const
184 {
185     return m_ui.jobBillingLineEdit->text();
186 }
187 
initJobPriority()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);
194         if (!ok)
195             priority = -1;
196     }
197 
198     if (priority < 0 || priority > 100)
199         priority = 50;
200 
201     setJobPriority(priority);
202 }
203 
setJobPriority(int jobPriority)204 void QCupsJobWidget::setJobPriority(int jobPriority)
205 {
206     m_ui.jobPrioritySpinBox->setValue(jobPriority);
207 }
208 
jobPriority() const209 int QCupsJobWidget::jobPriority() const
210 {
211     return m_ui.jobPrioritySpinBox->value();
212 }
213 
initBannerPages()214 void QCupsJobWidget::initBannerPages()
215 {
216     m_ui.startBannerPageCombo->addItem(tr("None", "CUPS Banner page"),         QVariant::fromValue(QCUPSSupport::NoBanner));
217     m_ui.startBannerPageCombo->addItem(tr("Standard", "CUPS Banner page"),     QVariant::fromValue(QCUPSSupport::Standard));
218     m_ui.startBannerPageCombo->addItem(tr("Unclassified", "CUPS Banner page"), QVariant::fromValue(QCUPSSupport::Unclassified));
219     m_ui.startBannerPageCombo->addItem(tr("Confidential", "CUPS Banner page"), QVariant::fromValue(QCUPSSupport::Confidential));
220     m_ui.startBannerPageCombo->addItem(tr("Classified", "CUPS Banner page"),   QVariant::fromValue(QCUPSSupport::Classified));
221     m_ui.startBannerPageCombo->addItem(tr("Secret", "CUPS Banner page"),       QVariant::fromValue(QCUPSSupport::Secret));
222     m_ui.startBannerPageCombo->addItem(tr("Top Secret", "CUPS Banner page"),   QVariant::fromValue(QCUPSSupport::TopSecret));
223 
224     m_ui.endBannerPageCombo->addItem(tr("None", "CUPS Banner page"),         QVariant::fromValue(QCUPSSupport::NoBanner));
225     m_ui.endBannerPageCombo->addItem(tr("Standard", "CUPS Banner page"),     QVariant::fromValue(QCUPSSupport::Standard));
226     m_ui.endBannerPageCombo->addItem(tr("Unclassified", "CUPS Banner page"), QVariant::fromValue(QCUPSSupport::Unclassified));
227     m_ui.endBannerPageCombo->addItem(tr("Confidential", "CUPS Banner page"), QVariant::fromValue(QCUPSSupport::Confidential));
228     m_ui.endBannerPageCombo->addItem(tr("Classified", "CUPS Banner page"),   QVariant::fromValue(QCUPSSupport::Classified));
229     m_ui.endBannerPageCombo->addItem(tr("Secret", "CUPS Banner page"),       QVariant::fromValue(QCUPSSupport::Secret));
230     m_ui.endBannerPageCombo->addItem(tr("Top Secret", "CUPS Banner page"),   QVariant::fromValue(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(jobSheetsString);
237     }
238 
239     setStartBannerPage(jobSheets.startBannerPage);
240     setEndBannerPage(jobSheets.endBannerPage);
241 }
242 
setStartBannerPage(const QCUPSSupport::BannerPage bannerPage)243 void QCupsJobWidget::setStartBannerPage(const QCUPSSupport::BannerPage bannerPage)
244 {
245     m_ui.startBannerPageCombo->setCurrentIndex(m_ui.startBannerPageCombo->findData(QVariant::fromValue(bannerPage)));
246 }
247 
startBannerPage() const248 QCUPSSupport::BannerPage QCupsJobWidget::startBannerPage() const
249 {
250     return qvariant_cast<QCUPSSupport::BannerPage>(m_ui.startBannerPageCombo->itemData(m_ui.startBannerPageCombo->currentIndex()));
251 }
252 
setEndBannerPage(const QCUPSSupport::BannerPage bannerPage)253 void QCupsJobWidget::setEndBannerPage(const QCUPSSupport::BannerPage bannerPage)
254 {
255     m_ui.endBannerPageCombo->setCurrentIndex(m_ui.endBannerPageCombo->findData(QVariant::fromValue(bannerPage)));
256 }
257 
endBannerPage() const258 QCUPSSupport::BannerPage QCupsJobWidget::endBannerPage() const
259 {
260     return qvariant_cast<QCUPSSupport::BannerPage>(m_ui.endBannerPageCombo->itemData(m_ui.endBannerPageCombo->currentIndex()));
261 }
262 
263 QT_END_NAMESPACE
264