File indexing completed on 2024-04-28 17:06:31

0001 /*
0002     SPDX-FileCopyrightText: 2004 Shie Erlich <erlich@users.sourceforge.net>
0003     SPDX-FileCopyrightText: 2004 Rafi Yanai <yanai@users.sourceforge.net>
0004     SPDX-FileCopyrightText: 2006 Jonas Bähr <jonas.baehr@web.de>
0005     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 
0010 #ifndef KRACTION_H
0011 #define KRACTION_H
0012 
0013 // QtCore
0014 #include <QByteArray>
0015 #include <QUrl>
0016 // QtGui
0017 #include <QFont>
0018 // QtWidgets
0019 #include <QAction>
0020 #include <QDialog>
0021 
0022 #include <KCoreAddons/KProcess>
0023 #include <KTextWidgets/KTextEdit>
0024 
0025 #include "kractionbase.h"
0026 
0027 class QDomDocument;
0028 class QDomElement;
0029 class KActionCollection;
0030 
0031 /**
0032  * This subclass of QAction extends it with an individual executor and
0033  * a struct UserActionProperties.
0034  * It is used to integrate useractions into KDE's QAction-System
0035  */
0036 class KrAction : public QAction, public KrActionBase
0037 {
0038     Q_OBJECT
0039 public:
0040     explicit KrAction(KActionCollection *parent, const QString &name = QString());
0041     ~KrAction() override;
0042 
0043     /**
0044      * This chekcs if the KrAction is for a specific file / location available
0045      * @param currentURL Check for this file
0046      * @return true if the KrAction if available
0047      */
0048     bool isAvailable(const QUrl &currentURL);
0049 
0050     const QString &iconName() const
0051     {
0052         return _iconName;
0053     } // TODO: added for kde4 porting (functionality is missing)
0054     void setIconName(const QString &name)
0055     {
0056         _iconName = name;
0057     }
0058 
0059     bool xmlRead(const QDomElement &element);
0060     QDomElement xmlDump(QDomDocument &doc) const;
0061 
0062     QString category() const
0063     {
0064         return _category;
0065     };
0066     void setCategory(const QString &category)
0067     {
0068         _category = category;
0069     };
0070 
0071     QString command() const override
0072     {
0073         return _command;
0074     };
0075     void setCommand(const QString &command)
0076     {
0077         _command = command;
0078     };
0079 
0080     QString user() const override
0081     {
0082         return _user;
0083     };
0084     void setUser(const QString &user)
0085     {
0086         _user = user;
0087     };
0088 
0089     QString startpath() const override
0090     {
0091         return _startpath;
0092     };
0093     void setStartpath(const QString &startpath)
0094     {
0095         _startpath = startpath;
0096     };
0097 
0098     ExecType execType() const override
0099     {
0100         return _execType;
0101     };
0102     void setExecType(ExecType execType)
0103     {
0104         _execType = execType;
0105     };
0106 
0107     bool acceptURLs() const override
0108     {
0109         return _acceptURLs;
0110     };
0111     void setAcceptURLs(const bool &acceptURLs)
0112     {
0113         _acceptURLs = acceptURLs;
0114     };
0115 
0116     bool confirmExecution() const override
0117     {
0118         return _confirmExecution;
0119     };
0120     void setConfirmExecution(const bool &confirmExecution)
0121     {
0122         _confirmExecution = confirmExecution;
0123     };
0124 
0125     QStringList showonlyProtocol() const
0126     {
0127         return _showonlyProtocol;
0128     };
0129     void setShowonlyProtocol(const QStringList &showonlyProtocol)
0130     {
0131         _showonlyProtocol = showonlyProtocol;
0132     };
0133 
0134     QStringList showonlyPath() const
0135     {
0136         return _showonlyPath;
0137     };
0138     void setShowonlyPath(const QStringList &showonlyPath)
0139     {
0140         _showonlyPath = showonlyPath;
0141     };
0142 
0143     QStringList showonlyMime() const
0144     {
0145         return _showonlyMime;
0146     };
0147     void setShowonlyMime(const QStringList &showonlyMime)
0148     {
0149         _showonlyMime = showonlyMime;
0150     };
0151 
0152     QStringList showonlyFile() const
0153     {
0154         return _showonlyFile;
0155     };
0156     void setShowonlyFile(const QStringList &showonlyFile)
0157     {
0158         _showonlyFile = showonlyFile;
0159     };
0160 
0161     bool doSubstitution() const override
0162     {
0163         return true;
0164     }
0165 
0166     QString text() const override
0167     {
0168         return QAction::text();
0169     }
0170 
0171 public slots:
0172     void exec()
0173     {
0174         KrActionBase::exec();
0175     }
0176 
0177 private:
0178     void readCommand(const QDomElement &element);
0179     QDomElement dumpCommand(QDomDocument &doc) const;
0180 
0181     void readAvailability(const QDomElement &element);
0182     QDomElement dumpAvailability(QDomDocument &doc) const;
0183 
0184     QString _iconName;
0185     QString _category;
0186     QString _command;
0187     QString _user;
0188     QString _startpath;
0189     ExecType _execType;
0190     bool _acceptURLs;
0191     bool _confirmExecution;
0192     QStringList _showonlyProtocol;
0193     QStringList _showonlyPath;
0194     QStringList _showonlyMime;
0195     QStringList _showonlyFile;
0196     KActionCollection *_actionCollection;
0197 };
0198 
0199 /**
0200  * This displays the output of a process
0201  */
0202 class KrActionProcDlg : public QDialog
0203 {
0204     Q_OBJECT
0205 public:
0206     explicit KrActionProcDlg(const QString &caption, bool enableStderr = false, QWidget *parent = nullptr);
0207 
0208 public slots:
0209     void addStderr(const QString &str);
0210     void addStdout(const QString &str);
0211     void slotProcessFinished();
0212 
0213 protected slots:
0214     void toggleFixedFont(bool state);
0215     void slotSaveAs();
0216 
0217 signals:
0218     void killClicked();
0219 
0220 private:
0221     KTextEdit *_stdout;
0222     KTextEdit *_stderr;
0223     KTextEdit *_currentTextEdit;
0224     QFont normalFont;
0225     QFont fixedFont;
0226     QPushButton *closeButton;
0227     QPushButton *killButton;
0228 private slots:
0229     void currentTextEditChanged();
0230 };
0231 
0232 /**
0233  * This executes a command of a UserAction
0234  */
0235 // TODO jonas: call a list of commands separately (I began it but it doesn't work)
0236 class KrActionProc : public QObject
0237 {
0238     Q_OBJECT
0239 public:
0240     explicit KrActionProc(KrActionBase *action);
0241     ~KrActionProc() override;
0242     void start(const QString &cmdLine);
0243     void start(QStringList cmdLineList);
0244 
0245 protected slots:
0246     void kill()
0247     {
0248         _proc->kill();
0249     }
0250     void processExited(int exitCode, QProcess::ExitStatus exitStatus);
0251     void addStderr();
0252     void addStdout();
0253 
0254 private:
0255     KrActionBase *_action;
0256     KProcess *_proc;
0257     QString _stdout;
0258     QString _stderr;
0259     KrActionProcDlg *_output;
0260 };
0261 
0262 #endif // KRACTION_H