File indexing completed on 2024-02-25 04:02:13

0001 /*
0002     SPDX-FileCopyrightText: 2014 Alex Merry <alex.merry@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #include <stdio.h>
0008 
0009 #include <QCommandLineOption>
0010 #include <QCommandLineParser>
0011 #include <QCoreApplication>
0012 #include <QTextStream>
0013 
0014 #include <KService>
0015 
0016 int main(int argc, char **argv)
0017 {
0018     QCoreApplication app(argc, argv);
0019     QCoreApplication::setApplicationName(QStringLiteral("findservice"));
0020     QCoreApplication::setApplicationVersion(QStringLiteral("1.0.0.0"));
0021 
0022     QCommandLineParser parser;
0023     parser.setApplicationDescription(QStringLiteral("Finds a service using KService"));
0024     parser.addHelpOption();
0025     parser.addVersionOption();
0026     parser.addPositionalArgument(QStringLiteral("id"), QStringLiteral("service identifier"));
0027     QCommandLineOption desktopName(QStringList() << QStringLiteral("n") << QStringLiteral("desktop-name"),
0028                                    QStringLiteral("Find the service by its desktop name (default)"));
0029     parser.addOption(desktopName);
0030     QCommandLineOption desktopPath(QStringList() << QStringLiteral("p") << QStringLiteral("desktop-path"),
0031                                    QStringLiteral("Find the service by its desktop path"));
0032     parser.addOption(desktopPath);
0033     QCommandLineOption menuId(QStringList() << QStringLiteral("m") << QStringLiteral("menu-id"), QStringLiteral("Find the service by its menu id"));
0034     parser.addOption(menuId);
0035     QCommandLineOption storageId(QStringList() << QStringLiteral("s") << QStringLiteral("storage-id"), QStringLiteral("Find the service by its storage id"));
0036     parser.addOption(storageId);
0037 
0038     parser.process(app);
0039 
0040     if (parser.positionalArguments().count() != 1) {
0041         QTextStream(stderr) << "Exactly one identifier required\n";
0042         parser.showHelp(1);
0043     }
0044 
0045     const QString id = parser.positionalArguments().at(0);
0046 
0047     KService::Ptr service;
0048     if (parser.isSet(menuId)) {
0049         service = KService::serviceByMenuId(id);
0050     } else if (parser.isSet(storageId)) {
0051         service = KService::serviceByStorageId(id);
0052     } else if (parser.isSet(desktopPath)) {
0053         service = KService::serviceByDesktopPath(id);
0054     } else {
0055         service = KService::serviceByDesktopName(id);
0056     }
0057 
0058     if (service) {
0059         QTextStream(stdout) << "Found \"" << service->entryPath() << "\"\n";
0060         QTextStream(stdout) << "Desktop name: \"" << service->desktopEntryName() << "\"\n";
0061         QTextStream(stdout) << "Menu ID: \"" << service->menuId() << "\"\n";
0062         QTextStream(stdout) << "Storage ID: \"" << service->storageId() << "\"\n";
0063         QTextStream(stdout) << "MIME types: \"" << service->mimeTypes().join(QLatin1Char(' ')) << "\"\n";
0064         QTextStream(stdout) << "Supported protocols: \"" << service->supportedProtocols().join(QLatin1Char(' ')) << "\"\n";
0065 
0066     } else {
0067         QTextStream(stdout) << "Not found\n";
0068         return 2;
0069     }
0070 
0071     return 0;
0072 }