File indexing completed on 2025-01-19 04:22:43

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #pragma once
0008 #include "libkommit_export.h"
0009 #include <QByteArray>
0010 #include <QObject>
0011 #include <QStringList>
0012 
0013 class QWidget;
0014 
0015 namespace Git
0016 {
0017 
0018 enum class OptionalBool {
0019     False,
0020     Unset,
0021     True,
0022 };
0023 
0024 enum class FastForwardType { Unset, Yes, No, OnlyFastForward };
0025 
0026 LIBKOMMIT_EXPORT OptionalBool checkStateToOptionalBool(Qt::CheckState checkState);
0027 
0028 class Manager;
0029 class LIBKOMMIT_EXPORT AbstractCommand : public QObject
0030 {
0031     Q_OBJECT
0032     Q_PROPERTY(int progress READ progress WRITE setProgress NOTIFY progressChanged)
0033 
0034 public:
0035     enum Status { None, InvalidArgs, Ready, Running, Finished, Error };
0036 
0037     explicit AbstractCommand(QObject *parent = nullptr);
0038     explicit AbstractCommand(QStringList args);
0039     explicit AbstractCommand(Manager *git);
0040 
0041     virtual ~AbstractCommand();
0042 
0043     virtual QStringList generateArgs() const = 0;
0044 
0045     virtual bool parseOutput(const QByteArray &output, const QByteArray &errorOutput);
0046     virtual void parseOutputSection(const QByteArray &output, const QByteArray &errorOutput);
0047 
0048     bool isValid() const;
0049 
0050     virtual bool supportWidget() const
0051     {
0052         return false;
0053     }
0054     virtual bool supportProgress() const
0055     {
0056         return false;
0057     }
0058     virtual QWidget *createWidget();
0059     int progress() const;
0060 
0061     Q_REQUIRED_RESULT Status status() const;
0062     void setStatus(Status newStatus);
0063 
0064     Q_REQUIRED_RESULT const QString &errorMessage() const;
0065     void setErrorMessage(const QString &newErrorMessage);
0066 
0067 Q_SIGNALS:
0068     void progressChanged(int progress);
0069 
0070 protected:
0071     QStringList mArgs;
0072     Manager *mGit{nullptr};
0073     void setProgress(int newProgress);
0074     void appendBool(OptionalBool b, QStringList &cmd, const QString &name) const;
0075     void appendBool(bool b, QStringList &cmd, const QString &name) const;
0076 
0077 private:
0078     bool mIsValid{false};
0079     int m_progress{0};
0080     Status mStatus{None};
0081     QString mErrorMessage;
0082 };
0083 
0084 } // namespace Git