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 "ConfigurePrinterInterface.h"
0008 #include "configureprinteradaptor.h"
0009 
0010 #include "ConfigureDialog.h"
0011 #include "Debug.h"
0012 
0013 #include <KCupsPrinter.h>
0014 #include <KCupsRequest.h>
0015 
0016 #include <KX11Extras>
0017 #include <KWindowSystem>
0018 #include <QDBusConnection>
0019 #include <QDialog>
0020 #include <QPointer>
0021 #include <QTimer>
0022 
0023 ConfigurePrinterInterface::ConfigurePrinterInterface(QObject *parent)
0024     : QObject(parent)
0025 {
0026     qCDebug(PM_CONFIGURE_PRINTER) << "Creating Helper";
0027     (void)new ConfigurePrinterAdaptor(this);
0028     if (!QDBusConnection::sessionBus().registerService(QLatin1String("org.kde.ConfigurePrinter"))) {
0029         qCDebug(PM_CONFIGURE_PRINTER) << "another helper is already running";
0030         return;
0031     }
0032 
0033     if (!QDBusConnection::sessionBus().registerObject(QLatin1String("/"), this)) {
0034         qCDebug(PM_CONFIGURE_PRINTER) << "unable to register service interface to dbus";
0035         return;
0036     }
0037 
0038     // setup the timer that updates the UIs
0039     m_updateUi = new QTimer(this);
0040     m_updateUi->setInterval(1000);
0041     m_updateUi->start();
0042 }
0043 
0044 ConfigurePrinterInterface::~ConfigurePrinterInterface()
0045 {
0046 }
0047 
0048 void ConfigurePrinterInterface::ConfigurePrinter(const QString &destName)
0049 {
0050     if (!m_uis.contains(destName)) {
0051         // Reserve this since the CUPS call might take a long time
0052         m_uis[destName] = nullptr;
0053 
0054         // Get destinations with these attributes
0055         QPointer<KCupsRequest> request = new KCupsRequest;
0056         request->getPrinters({KCUPS_PRINTER_NAME, KCUPS_PRINTER_TYPE});
0057         request->waitTillFinished();
0058         if (!request) {
0059             return;
0060         }
0061 
0062         bool found = false;
0063         KCupsPrinter printer;
0064         const KCupsPrinters printers = request->printers();
0065         for (const KCupsPrinter &p : printers) {
0066             if (p.name() == destName) {
0067                 printer = p;
0068                 found = true;
0069                 break;
0070             }
0071         }
0072         request->deleteLater();
0073 
0074         if (found) {
0075             auto ui = new ConfigureDialog(printer.name(), printer.isClass());
0076             connect(m_updateUi, &QTimer::timeout, ui, static_cast<void (ConfigureDialog::*)()>(&ConfigureDialog::update));
0077             connect(ui, &ConfigureDialog::finished, this, &ConfigurePrinterInterface::RemovePrinter);
0078             ui->show();
0079             m_uis[printer.name()] = ui;
0080         } else {
0081             // Remove the reservation
0082             m_uis.remove(destName);
0083 
0084             // if no destination was found and we aren't showing
0085             // a queue quit the app
0086             if (m_uis.isEmpty()) {
0087                 Q_EMIT quit();
0088             }
0089             return;
0090         }
0091     }
0092 
0093     // Check if it's not reserved
0094     if (m_uis.value(destName)) {
0095         if (KWindowSystem::isPlatformX11()) {
0096             KX11Extras::forceActiveWindow(m_uis.value(destName)->winId());
0097         }
0098     }
0099 }
0100 
0101 void ConfigurePrinterInterface::RemovePrinter()
0102 {
0103     auto ui = qobject_cast<QWidget *>(sender());
0104     if (ui) {
0105         m_uis.remove(m_uis.key(ui));
0106     }
0107 
0108     // if no destination was found and we aren't showing
0109     // a queue quit the app
0110     if (m_uis.isEmpty()) {
0111         Q_EMIT quit();
0112     }
0113 }
0114 
0115 #include "moc_ConfigurePrinterInterface.cpp"