File indexing completed on 2024-04-28 16:44:28

0001 /*
0002  *  SPDX-FileCopyrightText: 2002, 2003 David Faure <faure@kde.org>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include <kmimetypetrader.h>
0008 #include <kservicetypetrader.h>
0009 
0010 #include <KAboutData>
0011 #include <KLocalizedString>
0012 #include <QCommandLineOption>
0013 #include <QCommandLineParser>
0014 #include <QCoreApplication>
0015 #include <stdio.h>
0016 
0017 int main(int argc, char **argv)
0018 {
0019     QCoreApplication app(argc, argv);
0020 
0021     KLocalizedString::setApplicationDomain("ktraderclient5");
0022     KAboutData aboutData(QLatin1String("ktraderclient"), i18n("KTraderClient"), QLatin1String(PROJECT_VERSION));
0023     aboutData.addAuthor(i18n("David Faure"), QString(), QStringLiteral("faure@kde.org"));
0024 
0025     aboutData.setShortDescription(i18n("A command-line tool for querying the KDE trader system"));
0026     KAboutData::setApplicationData(aboutData);
0027 
0028     QCommandLineParser parser;
0029     aboutData.setupCommandLine(&parser);
0030     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("mimetype"), i18n("A MIME type"), QLatin1String("mimetype")));
0031     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("servicetype"),
0032                                         i18n("A servicetype, like KParts/ReadOnlyPart or KMyApp/Plugin"),
0033                                         QLatin1String("servicetype")));
0034     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("constraint"),
0035                                         i18n("A constraint expressed in the trader query language"),
0036                                         QLatin1String("constraint")));
0037 
0038     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("short"), i18n("Output only paths to desktop files")));
0039 
0040     parser.process(app);
0041     aboutData.processCommandLine(&parser);
0042 
0043     const QString mimetype = parser.value(QStringLiteral("mimetype"));
0044     QString servicetype = parser.value(QStringLiteral("servicetype"));
0045     const QString constraint = parser.value(QStringLiteral("constraint"));
0046     const bool outputProperties = !parser.isSet(QStringLiteral("short"));
0047 
0048     if (mimetype.isEmpty() && servicetype.isEmpty()) {
0049         parser.showHelp();
0050     }
0051 
0052     if (!mimetype.isEmpty()) {
0053         printf("mimetype is : %s\n", qPrintable(mimetype));
0054     }
0055     if (!servicetype.isEmpty()) {
0056         printf("servicetype is : %s\n", qPrintable(servicetype));
0057     }
0058     if (!constraint.isEmpty()) {
0059         printf("constraint is : %s\n", qPrintable(constraint));
0060     }
0061 
0062     KService::List offers;
0063     if (!mimetype.isEmpty()) {
0064         if (servicetype.isEmpty()) {
0065             servicetype = QStringLiteral("Application");
0066         }
0067         offers = KMimeTypeTrader::self()->query(mimetype, servicetype, constraint);
0068     } else {
0069         offers = KServiceTypeTrader::self()->query(servicetype, constraint);
0070     }
0071 
0072     printf("got %d offers.\n", offers.count());
0073 
0074     int i = 0;
0075     for (const auto &service : std::as_const(offers)) {
0076         if (outputProperties) {
0077             printf("---- Offer %d ----\n", i++);
0078 
0079             const QStringList props = service->propertyNames();
0080             for (const QString &propName : props) {
0081                 QVariant prop = service->property(propName);
0082 
0083                 if (!prop.isValid()) {
0084                     printf("Invalid property %s\n", propName.toLocal8Bit().data());
0085                     continue;
0086                 }
0087 
0088                 QString outp = propName;
0089                 outp += QStringLiteral(" : '");
0090 
0091                 switch (prop.type()) {
0092                 case QVariant::StringList:
0093                     outp += prop.toStringList().join(QStringLiteral(" - "));
0094                     break;
0095                 case QVariant::Bool:
0096                     outp += prop.toBool() ? QStringLiteral("TRUE") : QStringLiteral("FALSE");
0097                     break;
0098                 default:
0099                     outp += prop.toString();
0100                     break;
0101                 }
0102 
0103                 if (!outp.isEmpty()) {
0104                     printf("%s'\n", outp.toLocal8Bit().constData());
0105                 }
0106             }
0107         } else {
0108             printf("%s\n", service->entryPath().toLocal8Bit().constData());
0109         }
0110     }
0111     return 0;
0112 }