File indexing completed on 2025-03-09 05:06:32
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 "PrinterBehavior.h" 0008 #include "Debug.h" 0009 #include "KCupsRequest.h" 0010 #include "ui_PrinterBehavior.h" 0011 #include <KCupsRequest.h> 0012 #include <KLocalizedString> 0013 #include <QPointer> 0014 0015 PrinterBehavior::PrinterBehavior(const QString &destName, bool isClass, QWidget *parent) 0016 : PrinterPage(parent) 0017 , ui(new Ui::PrinterBehavior) 0018 , m_destName(destName) 0019 , m_isClass(isClass) 0020 { 0021 ui->setupUi(this); 0022 0023 connect(ui->errorPolicyCB, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &PrinterBehavior::currentIndexChangedCB); 0024 connect(ui->operationPolicyCB, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &PrinterBehavior::currentIndexChangedCB); 0025 0026 connect(ui->startingBannerCB, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &PrinterBehavior::currentIndexChangedCB); 0027 connect(ui->endingBannerCB, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &PrinterBehavior::currentIndexChangedCB); 0028 0029 connect(ui->usersELB, &KEditListWidget::changed, this, &PrinterBehavior::userListChanged); 0030 connect(ui->allowRB, &QRadioButton::toggled, this, &PrinterBehavior::userListChanged); 0031 } 0032 0033 PrinterBehavior::~PrinterBehavior() 0034 { 0035 delete ui; 0036 } 0037 0038 void PrinterBehavior::setValues(const KCupsPrinter &printer) 0039 { 0040 int defaultChoice; 0041 ui->errorPolicyCB->clear(); 0042 const QStringList errorPolicySupported = printer.errorPolicySupported(); 0043 for (const QString &value : errorPolicySupported) { 0044 ui->errorPolicyCB->addItem(errorPolicyString(value), value); 0045 } 0046 const QStringList errorPolicy = printer.errorPolicy(); 0047 if (!errorPolicy.isEmpty()) { 0048 defaultChoice = ui->errorPolicyCB->findData(errorPolicy.first()); 0049 ui->errorPolicyCB->setCurrentIndex(defaultChoice); 0050 ui->errorPolicyCB->setProperty("defaultChoice", defaultChoice); 0051 } 0052 0053 ui->operationPolicyCB->clear(); 0054 const QStringList opPolicySupported = printer.opPolicySupported(); 0055 for (const QString &value : opPolicySupported) { 0056 ui->operationPolicyCB->addItem(operationPolicyString(value), value); 0057 } 0058 const QStringList operationPolicy = printer.opPolicy(); 0059 if (!errorPolicy.isEmpty()) { 0060 defaultChoice = ui->operationPolicyCB->findData(operationPolicy.first()); 0061 ui->operationPolicyCB->setCurrentIndex(defaultChoice); 0062 ui->operationPolicyCB->setProperty("defaultChoice", defaultChoice); 0063 } 0064 0065 ui->startingBannerCB->clear(); 0066 ui->endingBannerCB->clear(); 0067 const QStringList jobSheetsSupported = printer.jobSheetsSupported(); 0068 for (const QString &value : jobSheetsSupported) { 0069 ui->startingBannerCB->addItem(jobSheetsString(value), value); 0070 ui->endingBannerCB->addItem(jobSheetsString(value), value); 0071 } 0072 const QStringList bannerPolicy = printer.jobSheetsDefault(); 0073 if (bannerPolicy.size() == 2) { 0074 defaultChoice = ui->startingBannerCB->findData(bannerPolicy.at(0)); 0075 ui->startingBannerCB->setCurrentIndex(defaultChoice); 0076 ui->startingBannerCB->setProperty("defaultChoice", defaultChoice); 0077 defaultChoice = ui->endingBannerCB->findData(bannerPolicy.at(1)); 0078 ui->endingBannerCB->setCurrentIndex(defaultChoice); 0079 ui->endingBannerCB->setProperty("defaultChoice", defaultChoice); 0080 } 0081 0082 if (!printer.requestingUserNameAllowed().isEmpty()) { 0083 QStringList list = printer.requestingUserNameAllowed(); 0084 list.sort(); // sort the list here to be able to compare it later 0085 ui->usersELB->setEnabled(true); 0086 if (list != ui->usersELB->items()) { 0087 ui->usersELB->clear(); 0088 ui->usersELB->insertStringList(list); 0089 } 0090 ui->usersELB->setProperty("defaultList", list); 0091 ui->allowRB->setProperty("defaultChoice", true); 0092 // Set checked AFTER the default choice was set 0093 // otherwise the signal will be emitted 0094 // which sets that we have a change 0095 ui->allowRB->setChecked(true); 0096 0097 } else if (!printer.requestingUserNameDenied().isEmpty()) { 0098 QStringList list = printer.requestingUserNameDenied(); 0099 list.sort(); // sort the list here to be able to compare it later 0100 ui->usersELB->setEnabled(true); 0101 if (list != ui->usersELB->items()) { 0102 ui->usersELB->clear(); 0103 ui->usersELB->insertStringList(list); 0104 } 0105 ui->usersELB->setProperty("defaultList", list); 0106 ui->allowRB->setProperty("defaultChoice", false); 0107 // Set checked AFTER the default choice was set 0108 // otherwise the signal will be emitted 0109 // which sets that we have a change 0110 ui->preventRB->setChecked(true); 0111 } 0112 0113 // Clear previous changes 0114 m_changes = 0; 0115 Q_EMIT changed(false); 0116 m_changedValues.clear(); 0117 ui->errorPolicyCB->setProperty("different", false); 0118 ui->operationPolicyCB->setProperty("different", false); 0119 ui->startingBannerCB->setProperty("different", false); 0120 ui->endingBannerCB->setProperty("different", false); 0121 ui->usersELB->setProperty("different", false); 0122 } 0123 0124 void PrinterBehavior::userListChanged() 0125 { 0126 if (ui->usersELB->isEnabled() == false && (ui->allowRB->isChecked() || ui->preventRB->isChecked())) { 0127 // this only happen when the list was empty 0128 ui->usersELB->setEnabled(true); 0129 } 0130 0131 QStringList currentList = ui->usersELB->items(); 0132 // sort the list so we can be sure it's different 0133 currentList.sort(); 0134 const QStringList defaultList = ui->usersELB->property("defaultList").toStringList(); 0135 0136 bool isDifferent = currentList != defaultList; 0137 if (isDifferent == false && currentList.isEmpty() == false) { 0138 // if the lists are equal and not empty the user might have 0139 // changed the Radio Button... 0140 if (ui->allowRB->isChecked() != ui->allowRB->property("defaultChoice").toBool()) { 0141 isDifferent = true; 0142 } 0143 } 0144 0145 if (isDifferent != ui->usersELB->property("different").toBool()) { 0146 // it's different from the last time so add or remove changes 0147 isDifferent ? m_changes++ : m_changes--; 0148 0149 ui->usersELB->setProperty("different", isDifferent); 0150 Q_EMIT changed(m_changes); 0151 } 0152 } 0153 0154 void PrinterBehavior::currentIndexChangedCB(int index) 0155 { 0156 auto comboBox = qobject_cast<QComboBox *>(sender()); 0157 bool isDifferent = comboBox->property("defaultChoice").toInt() != index; 0158 qCDebug(PM_CONFIGURE_PRINTER) << Q_FUNC_INFO << "isDifferent" << isDifferent << this; 0159 0160 if (isDifferent != comboBox->property("different").toBool()) { 0161 // it's different from the last time so add or remove changes 0162 isDifferent ? m_changes++ : m_changes--; 0163 0164 comboBox->setProperty("different", isDifferent); 0165 qCDebug(PM_CONFIGURE_PRINTER) << Q_FUNC_INFO << m_changes << this; 0166 0167 Q_EMIT changed(m_changes); 0168 } 0169 0170 const QString attribute = comboBox->property("AttributeName").toString(); 0171 QVariant value; 0172 // job-sheets-default has always two values 0173 if (attribute == QLatin1String("job-sheets-default")) { 0174 value = QStringList({ui->startingBannerCB->itemData(ui->startingBannerCB->currentIndex()).toString(), 0175 ui->endingBannerCB->itemData(ui->endingBannerCB->currentIndex()).toString()}); 0176 } else { 0177 value = comboBox->itemData(index).toString(); 0178 } 0179 0180 // store the new values 0181 if (isDifferent) { 0182 m_changedValues[attribute] = value; 0183 } else { 0184 m_changedValues.remove(attribute); 0185 } 0186 } 0187 0188 QString PrinterBehavior::errorPolicyString(const QString &policy) const 0189 { 0190 // TODO search for others policies of printer-error-policy-supported 0191 if (policy == QLatin1String("abort-job")) { 0192 return i18n("Abort job"); 0193 } else if (policy == QLatin1String("retry-current-job")) { 0194 return i18n("Retry current job"); 0195 } else if (policy == QLatin1String("retry-job")) { 0196 return i18n("Retry job"); 0197 } else if (policy == QLatin1String("stop-printer")) { 0198 return i18n("Stop printer"); 0199 } 0200 return policy; 0201 } 0202 0203 QString PrinterBehavior::operationPolicyString(const QString &policy) const 0204 { 0205 // TODO search for others policies of printer-error-policy-supported 0206 if (policy == QLatin1String("authenticated")) { 0207 return i18n("Authenticated"); 0208 } else if (policy == QLatin1String("default")) { 0209 return i18n("Default"); 0210 } 0211 return policy; 0212 } 0213 0214 QString PrinterBehavior::jobSheetsString(const QString &policy) const 0215 { 0216 // TODO search for others policies of printer-error-policy-supported 0217 if (policy == QLatin1String("none")) { 0218 return i18n("None"); 0219 } else if (policy == QLatin1String("classified")) { 0220 return i18n("Classified"); 0221 } else if (policy == QLatin1String("confidential")) { 0222 return i18n("Confidential"); 0223 } else if (policy == QLatin1String("secret")) { 0224 return i18n("Secret"); 0225 } else if (policy == QLatin1String("standard")) { 0226 return i18n("Standard"); 0227 } else if (policy == QLatin1String("topsecret")) { 0228 return i18n("Topsecret"); 0229 } else if (policy == QLatin1String("unclassified")) { 0230 return i18n("Unclassified"); 0231 } 0232 return policy; 0233 } 0234 0235 void PrinterBehavior::save() 0236 { 0237 if (m_changes) { 0238 QVariantMap changedValues = m_changedValues; 0239 // since a QStringList might be big we get it here instead 0240 // of adding it at edit time. 0241 if (ui->usersELB->property("different").toBool()) { 0242 QStringList list = ui->usersELB->items(); 0243 if (list.isEmpty()) { 0244 list << QLatin1String("all"); 0245 changedValues[KCUPS_REQUESTING_USER_NAME_ALLOWED] = list; 0246 } else { 0247 if (ui->allowRB->isChecked()) { 0248 changedValues[KCUPS_REQUESTING_USER_NAME_ALLOWED] = list; 0249 } else { 0250 changedValues[KCUPS_REQUESTING_USER_NAME_DENIED] = list; 0251 } 0252 } 0253 } 0254 QPointer<KCupsRequest> request = new KCupsRequest; 0255 if (m_isClass) { 0256 request->addOrModifyClass(m_destName, changedValues); 0257 } else { 0258 request->addOrModifyPrinter(m_destName, changedValues); 0259 } 0260 request->waitTillFinished(); 0261 if (request) { 0262 if (!request->hasError()) { 0263 request->getPrinterAttributes(m_destName, m_isClass, neededValues()); 0264 request->waitTillFinished(); 0265 if (request && !request->hasError() && !request->printers().isEmpty()) { 0266 KCupsPrinter printer = request->printers().first(); 0267 setValues(printer); 0268 } 0269 } 0270 request->deleteLater(); 0271 } 0272 } 0273 } 0274 0275 void PrinterBehavior::setRemote(bool remote) 0276 { 0277 ui->errorPolicyCB->setEnabled(!remote); 0278 ui->operationPolicyCB->setEnabled(!remote); 0279 ui->startingBannerCB->setEnabled(!remote); 0280 ui->endingBannerCB->setEnabled(!remote); 0281 ui->allowRB->setEnabled(!remote); 0282 ui->preventRB->setEnabled(!remote); 0283 ui->usersELB->setEnabled(!remote); 0284 } 0285 0286 bool PrinterBehavior::hasChanges() 0287 { 0288 return m_changes; 0289 } 0290 0291 QStringList PrinterBehavior::neededValues() const 0292 { 0293 return QStringList({KCUPS_JOB_SHEETS_DEFAULT, 0294 KCUPS_JOB_SHEETS_SUPPORTED, 0295 0296 KCUPS_PRINTER_ERROR_POLICY, 0297 KCUPS_PRINTER_ERROR_POLICY_SUPPORTED, 0298 0299 KCUPS_PRINTER_OP_POLICY, 0300 KCUPS_PRINTER_OP_POLICY_SUPPORTED, 0301 0302 KCUPS_REQUESTING_USER_NAME_ALLOWED, 0303 KCUPS_REQUESTING_USER_NAME_DENIED}); 0304 } 0305 0306 #include "moc_PrinterBehavior.cpp"