File indexing completed on 2024-11-10 04:54:52

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2020-2022 Harald Sitter <sitter@kde.org>
0003 
0004 #include <QApplication>
0005 #include <QCommandLineParser>
0006 #include <QDebug>
0007 #include <QIcon>
0008 #include <QQmlApplicationEngine>
0009 #include <QQmlContext>
0010 
0011 #include <KAboutData>
0012 #include <KConfigGroup>
0013 #include <KLocalizedString>
0014 
0015 #include <config-drkonqi.h>
0016 #include <coredumpwatcher.h>
0017 
0018 #include "DetailsLoader.h"
0019 #include "Patient.h"
0020 #include "PatientModel.h"
0021 
0022 int main(int argc, char *argv[])
0023 {
0024     QApplication app(argc, argv);
0025     app.setApplicationName(QStringLiteral("drkonqi-coredump-gui"));
0026     app.setOrganizationDomain(QStringLiteral("kde.org"));
0027     app.setWindowIcon(QIcon::fromTheme(QStringLiteral("tools-report-bug"), app.windowIcon()));
0028 
0029     KAboutData aboutData(QStringLiteral("drkonqi"),
0030                          i18nc("@title CLI title", "Crashed Processes Viewer"),
0031                          QString::fromLatin1(PROJECT_VERSION),
0032                          i18nc("@info program description", "Offers detailed view of past crashes"),
0033                          KAboutLicense::GPL,
0034                          i18n("(C) 2020-2022, The DrKonqi Authors"));
0035     aboutData.setDesktopFileName(QStringLiteral("org.kde.drkonqi.coredump.gui"));
0036     KAboutData::setApplicationData(aboutData);
0037 
0038     QCommandLineParser parser;
0039     aboutData.setupCommandLine(&parser);
0040     parser.process(app);
0041     aboutData.processCommandLine(&parser);
0042 
0043     PatientModel model(Patient::staticMetaObject);
0044     qmlRegisterSingletonInstance("org.kde.drkonqi.coredump.gui", 1, 0, "PatientModel", &model);
0045     qmlRegisterType<DetailsLoader>("org.kde.drkonqi.coredump.gui", 1, 0, "DetailsLoader");
0046 
0047     KLocalizedContext i18nContext;
0048     i18nContext.setTranslationDomain(QStringLiteral(TRANSLATION_DOMAIN));
0049 
0050     QQmlApplicationEngine engine;
0051     engine.rootContext()->setContextObject(&i18nContext);
0052 
0053     const QUrl url(QStringLiteral("qrc:/main.qml"));
0054     QObject::connect(
0055         &engine,
0056         &QQmlApplicationEngine::objectCreated,
0057         &app,
0058         [url](QObject *obj, const QUrl &objUrl) {
0059             if (!obj && url == objUrl) {
0060                 qWarning() << "ABORT ABORT";
0061                 QCoreApplication::exit(-1);
0062             }
0063         },
0064         Qt::QueuedConnection);
0065     engine.load(url);
0066 
0067     auto expectedJournal = owning_ptr_call<sd_journal>(sd_journal_open, SD_JOURNAL_LOCAL_ONLY);
0068     Q_ASSERT(expectedJournal.ret == 0);
0069     Q_ASSERT(expectedJournal.value);
0070     CoredumpWatcher watcher(std::move(expectedJournal.value), {}, {}, nullptr);
0071     QObject::connect(&watcher, &CoredumpWatcher::newDump, &model, [&](const Coredump &dump) {
0072         model.addObject(std::make_unique<Patient>(dump));
0073     });
0074     QObject::connect(&watcher, &CoredumpWatcher::atLogEnd, &model, [&]() {
0075         model.setReady(true);
0076     });
0077     watcher.metaObject()->invokeMethod(&watcher, &CoredumpWatcher::start, Qt::QueuedConnection);
0078 
0079     return app.exec();
0080 }