File indexing completed on 2025-03-09 05:06:36

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 "PrintQueue.h"
0008 #include "PrintQueueUi.h"
0009 
0010 #include <KCupsRequest.h>
0011 
0012 #include <QDebug>
0013 #include <QPointer>
0014 #include <QTimer>
0015 
0016 #include <KWindowSystem>
0017 #include <KX11Extras>
0018 
0019 PrintQueue::PrintQueue(int &argc, char **argv)
0020     : QApplication(argc, argv)
0021 {
0022 }
0023 
0024 PrintQueue::~PrintQueue()
0025 {
0026 }
0027 
0028 void PrintQueue::showQueues(const QStringList &queues, const QString &cwd)
0029 {
0030     Q_UNUSED(cwd)
0031 
0032     if (!queues.isEmpty()) {
0033         for (const QString &queue : queues) {
0034             showQueue(queue);
0035         }
0036     } else {
0037         qDebug() << "called with no args";
0038         // If DBus called the ui list won't be empty
0039         QTimer::singleShot(500, this, &PrintQueue::removeQueue);
0040     }
0041 }
0042 
0043 void PrintQueue::showQueue(const QString &destName)
0044 {
0045     qDebug() << Q_FUNC_INFO << destName;
0046     if (!m_uis.contains(destName)) {
0047         // Reserve this since the CUPS call might take a long time
0048         m_uis[destName] = nullptr;
0049 
0050         // Get destinations with these attributes
0051         QPointer<KCupsRequest> request = new KCupsRequest;
0052         request->getPrinters({KCUPS_PRINTER_NAME, KCUPS_PRINTER_TYPE});
0053         request->waitTillFinished();
0054         if (!request) {
0055             return;
0056         }
0057 
0058         bool found = false;
0059         KCupsPrinter printer;
0060         const KCupsPrinters printers = request->printers();
0061         for (const KCupsPrinter &printerItem : printers) {
0062             if (printerItem.name() == destName) {
0063                 printer = printerItem;
0064                 found = true;
0065                 break;
0066             }
0067         }
0068         request->deleteLater();
0069 
0070         if (found) {
0071             auto ui = new PrintQueueUi(printer);
0072             connect(ui, &PrintQueueUi::finished, this, &PrintQueue::removeQueue);
0073             ui->show();
0074             m_uis[printer.name()] = ui;
0075         } else {
0076             // Remove the reservation
0077             m_uis.remove(destName);
0078 
0079             // if no destination was found and we aren't showing
0080             // a queue quit the app
0081             if (m_uis.isEmpty()) {
0082                 Q_EMIT quit();
0083             }
0084             return;
0085         }
0086     }
0087 
0088     // Check if it's not reserved
0089     if (m_uis.value(destName)) {
0090         if (KWindowSystem::isPlatformX11()) {
0091             KX11Extras::forceActiveWindow(m_uis.value(destName)->winId());
0092         }
0093     }
0094 }
0095 
0096 void PrintQueue::removeQueue()
0097 {
0098     auto ui = qobject_cast<QWidget *>(sender());
0099     if (ui) {
0100         m_uis.remove(m_uis.key(ui));
0101     }
0102 
0103     // if no destination was found and we aren't showing
0104     // a queue quit the app
0105     if (m_uis.isEmpty()) {
0106         quit();
0107     }
0108 }
0109 
0110 #include "moc_PrintQueue.cpp"