1 /*
2     SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
3 
4     SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #ifndef KDEVPLATFORM_PLUGIN_EXTERNALSCRIPTITEM_H
8 #define KDEVPLATFORM_PLUGIN_EXTERNALSCRIPTITEM_H
9 
10 #include <QStandardItem>
11 
12 class QAction;
13 
14 /**
15  * NOTE: use @c text() and @c setText() to define the label/name of the external script.
16  */
17 class ExternalScriptItem
18     : public QStandardItem
19 {
20 public:
21     ExternalScriptItem();
22 
23     /**
24      * The key is supposed to be unique inside the model
25      * @return The key of this script item
26      */
27     QString key() const;
28     /**
29      * Sets the label
30      */
31     void setKey(const QString& key);
32 
33     /**
34      * @return The command to execute.
35      */
36     QString command() const;
37     /**
38      * Sets the command to execute.
39      */
40     void setCommand(const QString& command);
41 
42     /**
43      * @return The working directory where to execute the command.
44      *         If this is empty (default), it should be derived from the active document.
45      */
46     QString workingDirectory() const;
47 
48     /**
49      * Specify the working directory where the command should be executed
50      */
51     void setWorkingDirectory(const QString& workingDirectory);
52 
53     /**
54      * Whether placeholders like %b etc. in the command should be substituted. Default is true.
55      * */
56     bool performParameterReplacement() const;
57 
58     /**
59      * Set whether placeholders like %b etc. in the command should be substituted. Default is true.
60      * */
61     void setPerformParameterReplacement(bool perform);
62 
63     enum SaveMode {
64         /// Nothing needs to be saved.
65         SaveNone,
66         /// Currently active document gets saved.
67         SaveCurrentDocument,
68         /// All opened documents get saved.
69         SaveAllDocuments
70     };
71     /**
72      * @return @c SaveMode that decides what document should be saved before executing this script.
73      */
74     SaveMode saveMode() const;
75     /**
76      * Sets the @c SaveMode that decides what document should be saved before executing this script.
77      */
78     void setSaveMode(SaveMode mode);
79 
80     /**
81      * @return what type of filter should be applied to the execution of the external script
82      **/
83     int filterMode() const;
84 
85     /**
86      * Sets the filtering mode
87      **/
88     void setFilterMode(int mode);
89 
90     /// Defines what should be done with the @c STDOUT of a script run.
91     enum OutputMode {
92         /// Ignore output and do nothing.
93         OutputNone,
94         /// Output gets inserted at the cursor position of the current document.
95         OutputInsertAtCursor,
96         /// Current selection gets replaced in the active document.
97         /// If no selection exists, the output will get inserted at the
98         /// current cursor position in the active document view.
99         OutputReplaceSelectionOrInsertAtCursor,
100         /// Current selection gets replaced in the active document.
101         /// If no selection exists, the whole document gets replaced.
102         OutputReplaceSelectionOrDocument,
103         /// The whole contents of the active document gets replaced.
104         OutputReplaceDocument,
105         /// Create a new file from the output.
106         OutputCreateNewFile
107     };
108     /**
109      * @return @c OutputMode that decides what parts of the active document should be replaced by the
110      *         @c STDOUT of the @c command() execution.
111      */
112     OutputMode outputMode() const;
113     /**
114      * Sets the @c OutputMode that decides what parts of the active document should be replaced by the
115      * @c STDOUT of the @c command() execution.
116      */
117     void setOutputMode(OutputMode mode);
118 
119     /// Defines what should be done with the @c STDERR of a script run.
120     enum ErrorMode {
121         /// Ignore errors and do nothing.
122         ErrorNone,
123         /// Merge with @c STDOUT and use @c OutputMode.
124         ErrorMergeOutput,
125         /// Errors get inserted at the cursor position of the current document.
126         ErrorInsertAtCursor,
127         /// Current selection gets replaced in the active document.
128         /// If no selection exists, the output will get inserted at the
129         /// current cursor position in the active document view.
130         ErrorReplaceSelectionOrInsertAtCursor,
131         /// Current selection gets replaced in the active document.
132         /// If no selection exists, the whole document gets replaced.
133         ErrorReplaceSelectionOrDocument,
134         /// The whole contents of the active document gets replaced.
135         ErrorReplaceDocument,
136         /// Create a new file from the errors.
137         ErrorCreateNewFile
138     };
139 
140     /**
141      * @return @c ErrorMode that decides what parts of the active document should be replaced by the
142      *         @c STDERR of the @c command() execution.
143      */
144     ErrorMode errorMode() const;
145     /**
146      * Sets the @c ErrorMode that decides what parts of the active document should be replaced by the
147      * @c STDERR of the @c command() execution.
148      */
149     void setErrorMode(ErrorMode mode);
150 
151     enum InputMode {
152         /// Nothing gets streamed to the @c STDIN of the external script.
153         InputNone,
154         /// Current selection gets streamed into the @c STDIN of the external script.
155         /// If no selection exists, nothing gets streamed.
156         InputSelectionOrNone,
157         /// Current selection gets streamed into the @c STDIN of the external script.
158         /// If no selection exists, the whole document gets streamed.
159         InputSelectionOrDocument,
160         /// The whole contents of the active document get streamed into the @c STDIN of the external script.
161         InputDocument,
162     };
163     /**
164      * @return @c InputMode that decides what parts of the active document should be streamded into
165      *         the @c STDIN of the external script.
166      */
167     InputMode inputMode() const;
168     /**
169      * Sets the @c InputMode that decides what parts of the active document should be streamded into
170      * the @c STDIN of the external script.
171      */
172     void setInputMode(InputMode mode);
173 
174     /**
175      * Action to trigger insertion of this snippet.
176      */
177     QAction* action();
178 
179     /**
180      * @return True when this command should have its output shown, false otherwise.
181      */
182     bool showOutput() const;
183     /**
184      * Set @p show to true when the output of this command shout be shown, false otherwise.
185      */
186     void setShowOutput(bool show);
187 
188     ///TODO: custom icon
189     ///TODO: mimetype / language filter
190     ///TODO: kate commandline integration
191     ///TODO: filter for local/remote files
192 
193     /**
194      * Saves this item after changes.
195      */
196     void save() const;
197 
198 private:
199     QString m_key;
200     QString m_command;
201     QString m_workingDirectory;
202     SaveMode m_saveMode = SaveNone;
203     OutputMode m_outputMode = OutputNone;
204     ErrorMode m_errorMode = ErrorNone;
205     InputMode m_inputMode = InputNone;
206     QAction* m_action = nullptr;
207     bool m_showOutput = true;
208     int m_filterMode = 0;
209     bool m_performReplacements = true;
210 };
211 
212 Q_DECLARE_METATYPE(ExternalScriptItem*)
213 
214 #endif // KDEVPLATFORM_PLUGIN_EXTERNALSCRIPTITEM_H
215