File indexing completed on 2024-05-12 05:46:52

0001 /*
0002  * Copyright 2014 Alex Merry <alex.merry@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Lesser General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2.1 of the License, or (at your option) version 3, or any
0008  * later version accepted by the membership of KDE e.V. (or its
0009  * successor approved by the membership of KDE e.V.), which shall
0010  * act as a proxy defined in Section 6 of version 3 of the license.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library.  If not, see
0019  * <http://www.gnu.org/licenses/>.
0020  */
0021 
0022 
0023 #include <stdio.h>
0024 
0025 #include <QCommandLineOption>
0026 #include <QCommandLineParser>
0027 #include <QCoreApplication>
0028 #include <QTextStream>
0029 
0030 #include <KService>
0031 
0032 int main(int argc, char **argv)
0033 {
0034     QCoreApplication app(argc, argv);
0035     QCoreApplication::setApplicationName(QStringLiteral("findservice"));
0036     QCoreApplication::setApplicationVersion(QStringLiteral("1.0.0.0"));
0037 
0038     QCommandLineParser parser;
0039     parser.setApplicationDescription(QStringLiteral("Finds a service using KService"));
0040     parser.addHelpOption();
0041     parser.addVersionOption();
0042     parser.addPositionalArgument(QStringLiteral("id"), QStringLiteral("service identifier"));
0043     QCommandLineOption desktopName(
0044         QStringList() << QStringLiteral("n") << QStringLiteral("desktop-name"),
0045         QStringLiteral("Find the service by its desktop name (default)"));
0046     parser.addOption(desktopName);
0047     QCommandLineOption desktopPath(
0048         QStringList() << QStringLiteral("p") << QStringLiteral("desktop-path"),
0049         QStringLiteral("Find the service by its desktop path"));
0050     parser.addOption(desktopPath);
0051     QCommandLineOption menuId(
0052         QStringList() << QStringLiteral("m") << QStringLiteral("menu-id"),
0053         QStringLiteral("Find the service by its menu id"));
0054     parser.addOption(menuId);
0055     QCommandLineOption storageId(
0056         QStringList() << QStringLiteral("s") << QStringLiteral("storage-id"),
0057         QStringLiteral("Find the service by its storage id"));
0058     parser.addOption(storageId);
0059 
0060     parser.process(app);
0061 
0062     if (parser.positionalArguments().count() != 1) {
0063         QTextStream(stderr) << "Exactly one identifier required\n";
0064         parser.showHelp(1);
0065     }
0066 
0067     const QString id = parser.positionalArguments().at(0);
0068 
0069     KService::Ptr service;
0070     if (parser.isSet(menuId)) {
0071         service = KService::serviceByMenuId(id);
0072     } else if (parser.isSet(storageId)) {
0073         service = KService::serviceByStorageId(id);
0074     } else if (parser.isSet(desktopPath)) {
0075         service = KService::serviceByDesktopPath(id);
0076     } else {
0077         service = KService::serviceByDesktopName(id);
0078     }
0079 
0080     if (service) {
0081         QTextStream(stdout) << "Found \"" << service->entryPath() << "\"\n";
0082         QTextStream(stdout) << "Desktop name: \"" << service->desktopEntryName() << "\"\n";
0083         QTextStream(stdout) << "Menu ID: \"" << service->menuId() << "\"\n";
0084         QTextStream(stdout) << "Storage ID: \"" << service->storageId() << "\"\n";
0085     } else {
0086         QTextStream(stdout) << "Not found\n";
0087         return 2;
0088     }
0089 
0090     return 0;
0091 }