File indexing completed on 2024-05-05 05:30:13

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003     SPDX-FileCopyrightText: 2021-2022 Harald Sitter <sitter@kde.org>
0004 */
0005 
0006 #pragma once
0007 
0008 #include <QMap>
0009 #include <QObject>
0010 #include <QUrl>
0011 
0012 // Somewhat general-purpose command executor. This class runs the executable with arguments, collecting all its output
0013 // and potentially filtering it to limit the lines to only ones matching.
0014 class CommandOutputContext : public QObject
0015 {
0016     Q_OBJECT
0017     Q_PROPERTY(QString executable READ executableName CONSTANT)
0018     Q_PROPERTY(QStringList arguments READ arguments CONSTANT)
0019     // Output. With filter applied.
0020     Q_PROPERTY(QString text MEMBER m_text NOTIFY textChanged)
0021     // Filter string. Case in-sensitive. If lines do not contain the filter string they'll be removed from text.
0022     Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
0023     // Ready when the underlying process has terminated
0024     Q_PROPERTY(bool ready MEMBER m_ready NOTIFY readyChanged)
0025     // Potential error description. Empty when there is no error to report.
0026     Q_PROPERTY(QString error MEMBER m_error NOTIFY errorChanged)
0027     // Extra explanatory text for error conditions. Empty when no explanatory text has been specified.
0028     Q_PROPERTY(QString explanation MEMBER m_explanation NOTIFY explanationChanged)
0029     // URL where the user can report a bug when there is an error. Empty when there is no error, or no applicable place to report a bug
0030     Q_PROPERTY(QUrl bugReportUrl MEMBER m_bugReportUrl CONSTANT)
0031 public:
0032     CommandOutputContext(const QStringList &findExecutables, const QString &executable, const QStringList &arguments, QObject *parent = nullptr);
0033     CommandOutputContext(const QString &executable, const QStringList &arguments, QObject *parent = nullptr);
0034 
0035     QString executableName() const;
0036     QStringList arguments() const;
0037 
0038     QString filter() const;
0039     void setFilter(const QString &filter);
0040     Q_SIGNAL void filterChanged();
0041 
0042     void setTrimAllowed(bool allow);
0043 
0044 Q_SIGNALS:
0045     void textChanged();
0046     void readyChanged();
0047     void errorChanged();
0048     void explanationChanged();
0049 
0050 private:
0051     void reset();
0052     void load();
0053     void setError(const QString &message, const QString &explanation);
0054     void setReady();
0055 
0056     const QString m_executableName;
0057     QString m_executablePath;
0058     QMap<QString, QString> m_foundExecutablePaths;
0059     const QStringList m_arguments;
0060     const QUrl m_bugReportUrl;
0061 
0062     QStringList m_originalLines;
0063 
0064     bool m_trimAllowed = true;
0065     bool m_ready = false;
0066     QString m_error;
0067     QString m_explanation;
0068 
0069     QString m_text; // possibly filtered
0070     QString m_filter;
0071 };