File indexing completed on 2025-01-19 05:06:22

0001 /*
0002     SPDX-FileCopyrightText: 2010 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "AddPrinter.h"
0008 
0009 #include <config.h>
0010 
0011 #include <QCommandLineOption>
0012 #include <QCommandLineParser>
0013 
0014 #include <QLoggingCategory>
0015 
0016 #include <KAboutData>
0017 #include <KLocalizedString>
0018 
0019 Q_LOGGING_CATEGORY(PM_ADD_PRINTER, "pm.add.printer")
0020 
0021 int main(int argc, char **argv)
0022 {
0023     AddPrinter app(argc, argv);
0024     app.setOrganizationDomain(QLatin1String("org.kde"));
0025 
0026     KAboutData about(QLatin1String("kde-add-printer"),
0027                      i18n("Add Printer"),
0028                      QLatin1String(PM_VERSION),
0029                      i18n("Tool for adding new printers"),
0030                      KAboutLicense::GPL,
0031                      i18n("(C) 2010-2018 Daniel Nicoletti"));
0032 
0033     about.addAuthor(QLatin1String("Daniel Nicoletti"), QString(), QLatin1String("dantti12@gmail.com"));
0034     about.addAuthor(QStringLiteral("Lukáš Tinkl"), i18n("Port to Qt 5 / Plasma 5"), QStringLiteral("ltinkl@redhat.com"));
0035     about.addAuthor(QStringLiteral("Mike Noe"), i18n("Port to Qt 6 / Plasma 6"), QStringLiteral("noeerover@gmail.com"));
0036     KAboutData::setApplicationData(about);
0037 
0038     QCommandLineParser parser;
0039     about.setupCommandLine(&parser);
0040 
0041     QCommandLineOption parentWindowOpt({QLatin1String("w"), QLatin1String("parent-window")}, i18n("Parent Window ID"), QLatin1String("wid"));
0042     parser.addOption(parentWindowOpt);
0043 
0044     QCommandLineOption addPrinterOpt(QLatin1String("add-printer"), i18n("Add a new printer"));
0045     parser.addOption(addPrinterOpt);
0046 
0047     QCommandLineOption addClassOpt(QLatin1String("add-class"), i18n("Add a new printer class"));
0048     parser.addOption(addClassOpt);
0049 
0050     QCommandLineOption changePpdOpt(QLatin1String("change-ppd"), i18n("Changes the PPD of a given printer"), QLatin1String("printer-name"));
0051     parser.addOption(changePpdOpt);
0052 
0053     QCommandLineOption newPrinterDevOpt(QLatin1String("new-printer-from-device"),
0054                                         i18n("Changes the PPD of a given printer/deviceid"),
0055                                         QLatin1String("printername/deviceid"));
0056     parser.addOption(newPrinterDevOpt);
0057 
0058     parser.process(app);
0059     about.processCommandLine(&parser);
0060 
0061     qulonglong wid = 0;
0062     if (parser.isSet(parentWindowOpt)) {
0063         wid = parser.value(parentWindowOpt).toULongLong();
0064     }
0065 
0066     if (parser.isSet(addPrinterOpt)) {
0067         app.addPrinter(wid);
0068     } else if (parser.isSet(addClassOpt)) {
0069         app.addClass(wid);
0070     } else if (parser.isSet(changePpdOpt)) {
0071         app.changePPD(wid, parser.value(changePpdOpt));
0072     } else if (parser.isSet(newPrinterDevOpt)) {
0073         const QString value = parser.value(newPrinterDevOpt);
0074         const QStringList values = value.split(QLatin1String("/"));
0075         if (values.size() == 2) {
0076             app.newPrinterFromDevice(wid, values.first(), values.last());
0077         } else {
0078             qWarning() << "The expected input should be printer/deviceid";
0079             exit(EXIT_FAILURE);
0080         }
0081     } else {
0082         parser.showHelp(EXIT_FAILURE);
0083     }
0084 
0085     return app.exec();
0086 }