File indexing completed on 2024-04-28 05:45:31

0001 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0002 // SPDX-FileCopyrightText: 2021 Harald Sitter <sitter@kde.org>
0003 
0004 #include "DiagnosticResolver.h"
0005 
0006 #include <QDebug>
0007 #include <QFileInfo>
0008 #include <QProcess>
0009 #include <QTemporaryFile>
0010 
0011 #include <PackageKit/Daemon>
0012 
0013 #include "Debug.h"
0014 #include "DebugPackage.h"
0015 
0016 // NB: none of the strings are i18n'd because we need to understand them in
0017 //   bug reports. They are here for our benefit, not the user's.
0018 
0019 DiagnosticResolver::DiagnosticResolver(std::shared_ptr<File> file, QObject *parent)
0020     : QObject(parent)
0021     , m_file(std::move(file))
0022 {
0023 }
0024 
0025 void DiagnosticResolver::resolve()
0026 {
0027     auto proc = new QProcess(this);
0028     proc->setProcessChannelMode(QProcess::MergedChannels);
0029     proc->setInputChannelMode(QProcess::ManagedInputChannel);
0030 
0031     connect(proc, &QProcess::started, this, [this, proc]() {
0032         QFile script(QStringLiteral(":/Diagnostic.sh"));
0033         if (!script.open(QFile::ReadOnly)) {
0034             m_data += QStringLiteral("Failed to open diagnostic script asset.");
0035             proc->closeWriteChannel();
0036             proc->kill();
0037             return;
0038         }
0039         proc->write(script.readAll());
0040         proc->closeWriteChannel();
0041     });
0042 
0043     connect(proc, &QProcess::readyReadStandardOutput, this, [this, proc]() {
0044         m_data += QString::fromUtf8(proc->readAll());
0045     });
0046 
0047     connect(proc, &QProcess::finished, this, [this, proc]() {
0048         proc->deleteLater();
0049 
0050         // Read remaining data in case there is any.
0051         m_data += QString::fromUtf8(proc->readAll());
0052         Q_EMIT finished();
0053     });
0054 
0055     proc->start(QStringLiteral("/bin/sh"), {QStringLiteral("-s"), m_file->path(), QFileInfo(m_file->path()).canonicalFilePath()});
0056 }
0057 
0058 QString DiagnosticResolver::data() const
0059 {
0060     return m_data;
0061 }