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

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 "AddPrinter.h"
0008 
0009 #include "AddPrinterAssistant.h"
0010 
0011 #include <KCupsRequest.h>
0012 #include <KWindowSystem>
0013 
0014 #include <QPointer>
0015 
0016 #include <QLoggingCategory>
0017 
0018 Q_DECLARE_LOGGING_CATEGORY(PM_ADD_PRINTER)
0019 
0020 AddPrinter::AddPrinter(int &argc, char **argv) :
0021     QApplication(argc, argv)
0022 {
0023     setQuitOnLastWindowClosed(true);
0024 }
0025 
0026 AddPrinter::~AddPrinter()
0027 {
0028 }
0029 
0030 void AddPrinter::addPrinter(qulonglong wid)
0031 {
0032     auto wizard = new AddPrinterAssistant();
0033     wizard->initAddPrinter();
0034     show(wizard, wid);
0035 }
0036 
0037 void AddPrinter::addClass(qulonglong wid)
0038 {
0039     auto wizard = new AddPrinterAssistant();
0040     wizard->initAddClass();
0041     show(wizard, wid);
0042 }
0043 
0044 void AddPrinter::changePPD(qulonglong wid, const QString &name)
0045 {
0046     // Fist we need to get the printer attributes
0047     QPointer<KCupsRequest> request = new KCupsRequest;
0048     const QStringList attr({
0049                                KCUPS_PRINTER_TYPE, // needed to know if it's a remote printer
0050                                KCUPS_PRINTER_MAKE_AND_MODEL,
0051                                KCUPS_DEVICE_URI
0052                            });
0053     request->getPrinterAttributes(name, false, attr);
0054     request->waitTillFinished();
0055     if (request) {
0056         if (!request->hasError() && request->printers().size() == 1) {
0057             const KCupsPrinter printer = request->printers().first();
0058             if (printer.type() & CUPS_PRINTER_REMOTE) {
0059                 qCWarning(PM_ADD_PRINTER) << "Ignoring request, can not change PPD of remote printer" << name;
0060             } else {
0061                 auto wizard = new AddPrinterAssistant();
0062                 wizard->initChangePPD(name, printer.deviceUri(), printer.makeAndModel());
0063                 show(wizard, wid);
0064             }
0065         } else {
0066             qWarning() << "Ignoring request, printer not found" << name << request->errorMsg();
0067         }
0068         request->deleteLater();
0069     }
0070 }
0071 
0072 void AddPrinter::newPrinterFromDevice(qulonglong wid, const QString &name, const QString &device_id)
0073 {
0074     // Example of data
0075     // "direct"
0076     // "MFG:Samsung;CMD:GDI;MDL:SCX-4200 Series;CLS:PRINTER;MODE:PCL;STATUS:IDLE;"
0077     // "Samsung SCX-4200 Series"
0078     // "Samsung SCX-4200 Series"
0079     // "usb://Samsung/SCX-4200%20Series"
0080     // ""
0081 
0082     //        printer = "Samsung SCX-3400 Series";
0083     //        deviceId = "MFG:Samsung;CMD:SPL,FWV,PIC,BDN,EXT;MDL:SCX-3400 Series;CLS:PRINTER;MODE:SCN,SPL3,R000105;STATUS:BUSY;";
0084 
0085     //        printer = "Samsung SCX-4200 Series";
0086     //        deviceId = "MFG:Samsung;CMD:GDI;MDL:SCX-4200 Series;CLS:PRINTER;MODE:PCL;STATUS:IDLE;";
0087 
0088     //        printer = "HP PSC 1400 series";
0089     //        deviceId = "MFG:HP;MDL:PSC 1400 series;DES:;CMD:LDL,MLC,PML,DYN;";
0090 
0091     auto wizard = new AddPrinterAssistant();
0092     wizard->initAddPrinter(name, device_id);
0093     show(wizard, wid);
0094 }
0095 
0096 void AddPrinter::show(QWidget *widget, qulonglong wid) const
0097 {
0098     widget->show();
0099     KWindowSystem::forceActiveWindow(widget->winId());
0100     widget->setAttribute(Qt::WA_NativeWindow, true);
0101     KWindowSystem::setMainWindow(widget->windowHandle(), wid);
0102 }
0103 
0104 #include "moc_AddPrinter.cpp"