File indexing completed on 2025-01-05 05:01:15

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
0003 
0004 // Collects various data into a json blob for use in the sentry payloads.
0005 
0006 #include <chrono>
0007 #include <iostream>
0008 
0009 #include <QCoreApplication>
0010 #include <QDBusConnection>
0011 #include <QDBusInterface>
0012 #include <QDBusMessage>
0013 #include <QDBusReply>
0014 #include <QJsonDocument>
0015 #include <QJsonObject>
0016 
0017 #include <KOSRelease>
0018 
0019 using namespace std::chrono_literals;
0020 using namespace Qt::StringLiterals;
0021 
0022 int main(int argc, char **argv)
0023 {
0024     QCoreApplication app(argc, argv);
0025 
0026     QVariantHash blob;
0027 
0028     struct BusInterface {
0029         QString service;
0030         QString path;
0031         QString interface;
0032         QDBusConnection bus = QDBusConnection::sessionBus();
0033     };
0034 
0035     auto fillProperties = [&blob]<typename... Args>(const BusInterface &iface, Args &&...properties) {
0036         QDBusInterface dbusIface(iface.service, iface.path, iface.service, iface.bus);
0037         dbusIface.setTimeout(std::chrono::milliseconds(4s).count()); // arbitrarily low timeout
0038         for (const auto &property : {properties...}) {
0039             const QVariant value = dbusIface.property(qUtf8Printable(property));
0040             if (value.isValid()) {
0041                 Q_ASSERT(!blob.contains(property));
0042                 blob[property] = value;
0043             }
0044         }
0045     };
0046 
0047     fillProperties({.service = u"org.freedesktop.hostname1"_qs,
0048                     .path = u"/org/freedesktop/hostname1"_qs,
0049                     .interface = u"org.freedesktop.hostname1"_qs,
0050                     .bus = QDBusConnection::systemBus()},
0051                    u"Hostname"_qs,
0052                    u"Chassis"_qs);
0053 
0054     fillProperties({.service = u"org.freedesktop.systemd1"_qs,
0055                     .path = u"/org/freedesktop/systemd1"_qs,
0056                     .interface = u"org.freedesktop.systemd1.Manager"_qs,
0057                     .bus = QDBusConnection::sessionBus()},
0058                    u"Virtualization"_qs);
0059     // Convert to bool
0060     blob[u"Virtualization"_qs] = !blob[u"Virtualization"_qs].toString().isEmpty();
0061 
0062     fillProperties({.service = u"org.freedesktop.timedate1"_qs,
0063                     .path = u"/org/freedesktop/timedate1"_qs,
0064                     .interface = u"org.freedesktop.timedate1"_qs,
0065                     .bus = QDBusConnection::systemBus()},
0066                    u"Timezone"_qs);
0067 
0068     KOSRelease os;
0069     blob[u"OS_NAME"_qs] = os.name();
0070     blob[u"OS_VERSION_ID"_qs] = os.versionId();
0071     blob[u"OS_BUILD_ID"_qs] = os.buildId();
0072     blob[u"OS_VARIANT_ID"_qs] = os.variantId();
0073 
0074     QJsonDocument doc(QJsonObject::fromVariantHash(blob));
0075     std::cout << doc.toJson().toStdString() << std::endl;
0076 
0077     return 0;
0078 }