File: ktraderclient.cpp

package info (click to toggle)
kde-cli-tools 4%3A5.27.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,968 kB
  • sloc: cpp: 4,773; sh: 37; makefile: 7
file content (112 lines) | stat: -rw-r--r-- 4,156 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 *  SPDX-FileCopyrightText: 2002, 2003 David Faure <faure@kde.org>
 *
 *  SPDX-License-Identifier: LGPL-2.0-or-later
 */

#include <kmimetypetrader.h>
#include <kservicetypetrader.h>

#include <KAboutData>
#include <KLocalizedString>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <stdio.h>

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);

    KLocalizedString::setApplicationDomain("ktraderclient5");
    KAboutData aboutData(QLatin1String("ktraderclient"), i18n("KTraderClient"), QLatin1String(PROJECT_VERSION));
    aboutData.addAuthor(i18n("David Faure"), QString(), QStringLiteral("faure@kde.org"));

    aboutData.setShortDescription(i18n("A command-line tool for querying the KDE trader system"));
    KAboutData::setApplicationData(aboutData);

    QCommandLineParser parser;
    aboutData.setupCommandLine(&parser);
    parser.addOption(QCommandLineOption(QStringList() << QLatin1String("mimetype"), i18n("A MIME type"), QLatin1String("mimetype")));
    parser.addOption(QCommandLineOption(QStringList() << QLatin1String("servicetype"),
                                        i18n("A servicetype, like KParts/ReadOnlyPart or KMyApp/Plugin"),
                                        QLatin1String("servicetype")));
    parser.addOption(QCommandLineOption(QStringList() << QLatin1String("constraint"),
                                        i18n("A constraint expressed in the trader query language"),
                                        QLatin1String("constraint")));

    parser.addOption(QCommandLineOption(QStringList() << QLatin1String("short"), i18n("Output only paths to desktop files")));

    parser.process(app);
    aboutData.processCommandLine(&parser);

    const QString mimetype = parser.value(QStringLiteral("mimetype"));
    QString servicetype = parser.value(QStringLiteral("servicetype"));
    const QString constraint = parser.value(QStringLiteral("constraint"));
    const bool outputProperties = !parser.isSet(QStringLiteral("short"));

    if (mimetype.isEmpty() && servicetype.isEmpty()) {
        parser.showHelp();
    }

    if (!mimetype.isEmpty()) {
        printf("mimetype is : %s\n", qPrintable(mimetype));
    }
    if (!servicetype.isEmpty()) {
        printf("servicetype is : %s\n", qPrintable(servicetype));
    }
    if (!constraint.isEmpty()) {
        printf("constraint is : %s\n", qPrintable(constraint));
    }

    KService::List offers;
    if (!mimetype.isEmpty()) {
        if (servicetype.isEmpty()) {
            servicetype = QStringLiteral("Application");
        }
        offers = KMimeTypeTrader::self()->query(mimetype, servicetype, constraint);
    } else {
        offers = KServiceTypeTrader::self()->query(servicetype, constraint);
    }

    printf("got %d offers.\n", offers.count());

    int i = 0;
    for (const auto &service : std::as_const(offers)) {
        if (outputProperties) {
            printf("---- Offer %d ----\n", i++);

            const QStringList props = service->propertyNames();
            for (const QString &propName : props) {
                QVariant prop = service->property(propName);

                if (!prop.isValid()) {
                    printf("Invalid property %s\n", propName.toLocal8Bit().data());
                    continue;
                }

                QString outp = propName;
                outp += QStringLiteral(" : '");

                switch (prop.type()) {
                case QVariant::StringList:
                    outp += prop.toStringList().join(QStringLiteral(" - "));
                    break;
                case QVariant::Bool:
                    outp += prop.toBool() ? QStringLiteral("TRUE") : QStringLiteral("FALSE");
                    break;
                default:
                    outp += prop.toString();
                    break;
                }

                if (!outp.isEmpty()) {
                    printf("%s'\n", outp.toLocal8Bit().constData());
                }
            }
        } else {
            printf("%s\n", service->entryPath().toLocal8Bit().constData());
        }
    }
    return 0;
}