File indexing completed on 2022-09-27 16:37:19

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