File indexing completed on 2025-01-05 05:09:30

0001 /*
0002     SPDX-FileCopyrightText: 2010-2018 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "KCupsJob.h"
0008 
0009 KCupsJob::KCupsJob()
0010     : m_jobId(0)
0011 {
0012 }
0013 
0014 KCupsJob::KCupsJob(int jobId, const QString &printer)
0015     : m_jobId(jobId)
0016     , m_printer(printer)
0017 {
0018     m_arguments[KCUPS_JOB_ID] = QString::number(jobId);
0019 }
0020 
0021 KCupsJob::KCupsJob(const QVariantMap &arguments)
0022     : m_arguments(arguments)
0023 {
0024     m_jobId = arguments[KCUPS_JOB_ID].toInt();
0025     m_printer = arguments[KCUPS_JOB_PRINTER_URI].toString().section(QLatin1Char('/'), -1);
0026 }
0027 
0028 int KCupsJob::id() const
0029 {
0030     return m_jobId;
0031 }
0032 
0033 QString KCupsJob::idStr() const
0034 {
0035     return m_arguments[KCUPS_JOB_ID].toString();
0036 }
0037 
0038 QString KCupsJob::name() const
0039 {
0040     return m_arguments[KCUPS_JOB_NAME].toString();
0041 }
0042 
0043 QString KCupsJob::originatingUserName() const
0044 {
0045     return m_arguments[KCUPS_JOB_ORIGINATING_USER_NAME].toString();
0046 }
0047 
0048 QString KCupsJob::originatingHostName() const
0049 {
0050     return m_arguments[KCUPS_JOB_ORIGINATING_HOST_NAME].toString();
0051 }
0052 
0053 QString KCupsJob::printer() const
0054 {
0055     return m_printer;
0056 }
0057 
0058 QDateTime KCupsJob::createdAt() const
0059 {
0060     QDateTime ret;
0061     const auto it = m_arguments.constFind(KCUPS_TIME_AT_CREATION);
0062     if (it != m_arguments.constEnd()) {
0063         ret.fromSecsSinceEpoch(it.value().toInt());
0064     }
0065     return ret;
0066 }
0067 
0068 QDateTime KCupsJob::completedAt() const
0069 {
0070     QDateTime ret;
0071     const auto it = m_arguments.constFind(KCUPS_TIME_AT_COMPLETED);
0072     if (it != m_arguments.constEnd()) {
0073         ret.fromSecsSinceEpoch(it.value().toInt());
0074     }
0075     return ret;
0076 }
0077 
0078 QDateTime KCupsJob::processedAt() const
0079 {
0080     QDateTime ret;
0081     const auto it = m_arguments.constFind(KCUPS_TIME_AT_PROCESSING);
0082     if (it != m_arguments.constEnd()) {
0083         ret.fromSecsSinceEpoch(it.value().toInt());
0084     }
0085     return ret;
0086 }
0087 
0088 int KCupsJob::pages() const
0089 {
0090     return m_arguments[KCUPS_JOB_MEDIA_SHEETS].toInt();
0091 }
0092 
0093 int KCupsJob::processedPages() const
0094 {
0095     return m_arguments[KCUPS_JOB_MEDIA_SHEETS_COMPLETED].toInt();
0096 }
0097 
0098 int KCupsJob::size() const
0099 {
0100     int jobKOctets = m_arguments[KCUPS_JOB_K_OCTETS].toInt();
0101     jobKOctets *= 1024; // transform it to bytes
0102     return jobKOctets;
0103 }
0104 
0105 bool KCupsJob::preserved() const
0106 {
0107     return m_arguments[KCUPS_JOB_PRESERVED].toInt();
0108 }
0109 
0110 QString KCupsJob::iconName(ipp_jstate_t state)
0111 {
0112     QString ret;
0113     switch (state) {
0114     case IPP_JOB_PENDING:
0115         ret = QLatin1String("chronometer");
0116         break;
0117     case IPP_JOB_HELD:
0118         ret = QLatin1String("media-playback-pause");
0119         break;
0120     case IPP_JOB_PROCESSING:
0121         ret = QLatin1String("draw-arrow-forward");
0122         break;
0123     case IPP_JOB_STOPPED:
0124         ret = QLatin1String("draw-rectangle");
0125         break;
0126     case IPP_JOB_CANCELED:
0127         ret = QLatin1String("archive-remove");
0128         break;
0129     case IPP_JOB_ABORTED:
0130         ret = QLatin1String("task-attempt");
0131         break;
0132     case IPP_JOB_COMPLETED:
0133         ret = QLatin1String("task-complete");
0134         break;
0135     default:
0136         ret = QLatin1String("unknown");
0137     }
0138     return ret;
0139 }
0140 
0141 ipp_jstate_t KCupsJob::state() const
0142 {
0143     return static_cast<ipp_jstate_t>(m_arguments[KCUPS_JOB_STATE].toUInt());
0144 }
0145 
0146 QString KCupsJob::stateMsg() const
0147 {
0148     return m_arguments[KCUPS_JOB_PRINTER_STATE_MESSAGE].toString();
0149 }
0150 
0151 QString KCupsJob::stateReason() const
0152 {
0153     return m_arguments[KCUPS_JOB_STATE_REASONS].toString();
0154 }
0155 
0156 QString KCupsJob::holdUntil() const
0157 {
0158     return m_arguments[KCUPS_JOB_HOLD_UNTIL].toString();
0159 }
0160 
0161 bool KCupsJob::authenticationRequired() const
0162 {
0163     return stateReason() == QStringLiteral("cups-held-for-authentication") || holdUntil() == QStringLiteral("auth-info-required");
0164 }
0165 
0166 bool KCupsJob::reprintEnabled() const
0167 {
0168     if (state() >= IPP_JOB_STOPPED && preserved()) {
0169         return true;
0170     }
0171     return false;
0172 }
0173 
0174 bool KCupsJob::cancelEnabled(ipp_jstate_t state)
0175 {
0176     switch (state) {
0177     case IPP_JOB_CANCELED:
0178     case IPP_JOB_COMPLETED:
0179     case IPP_JOB_ABORTED:
0180         return false;
0181     default:
0182         return true;
0183     }
0184 }
0185 
0186 bool KCupsJob::holdEnabled(ipp_jstate_t state)
0187 {
0188     switch (state) {
0189     case IPP_JOB_CANCELED:
0190     case IPP_JOB_COMPLETED:
0191     case IPP_JOB_ABORTED:
0192     case IPP_JOB_HELD:
0193     case IPP_JOB_STOPPED:
0194         return false;
0195     default:
0196         return true;
0197     }
0198 }
0199 
0200 bool KCupsJob::releaseEnabled(ipp_jstate_t state)
0201 {
0202     switch (state) {
0203     case IPP_JOB_HELD:
0204     case IPP_JOB_STOPPED:
0205         return true;
0206     default:
0207         return false;
0208     }
0209 }