Changeset View
Changeset View
Standalone View
Standalone View
addons/externaltools/externaltools.h
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This file is part of the Kate text editor of the KDE project. | ||||
3 | It describes a "external tools" action for kate and provides a dialog | ||||
4 | page to configure that. | ||||
5 | | ||||
6 | This library is free software; you can redistribute it and/or | ||||
7 | modify it under the terms of the GNU Library General Public | ||||
8 | License version 2 as published by the Free Software Foundation. | ||||
9 | | ||||
10 | This library is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
13 | Library General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU Library General Public License | ||||
16 | along with this library; see the file COPYING.LIB. If not, write to | ||||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
18 | Boston, MA 02110-1301, USA. | ||||
19 | | ||||
20 | --- | ||||
21 | Copyright (C) 2004 Anders Lund <anders@alweb.dk> | ||||
22 | Copyright (C) 2019 Dominik Haumann <dhaumann@kde.org> | ||||
23 | */ | ||||
24 | | ||||
25 | #ifndef KTEXTEDITOR_EXTERNALTOOLS_H | ||||
26 | #define KTEXTEDITOR_EXTERNALTOOLS_H | ||||
27 | | ||||
28 | #include "ui_configwidget.h" | ||||
29 | #include "ui_tooldialog.h" | ||||
30 | | ||||
31 | #include <KTextEditor/Application> | ||||
32 | #include <KTextEditor/Command> | ||||
33 | #include <KTextEditor/ConfigPage> | ||||
34 | #include <KTextEditor/MainWindow> | ||||
35 | #include <KTextEditor/Plugin> | ||||
36 | | ||||
37 | #include <KActionMenu> | ||||
38 | #include <KMacroExpander> | ||||
39 | #include <QDialog> | ||||
40 | | ||||
41 | #include <QHash> | ||||
42 | #include <QPixmap> | ||||
43 | | ||||
44 | class KateExternalToolsPlugin; | ||||
45 | | ||||
46 | /** | ||||
47 | * The external tools action | ||||
48 | * This action creates a menu, in which each item will launch a process | ||||
49 | * with the provided arguments, which may include the following macros: | ||||
50 | * - %URLS: the URLs of all open documents. | ||||
51 | * - %URL: The URL of the active document. | ||||
52 | * - %filedir: The directory of the current document, if that is a local file. | ||||
53 | * - %selection: The selection of the active document. | ||||
54 | * - %text: The text of the active document. | ||||
55 | * - %line: The line number of the cursor in the active view. | ||||
56 | * - %column: The column of the cursor in the active view. | ||||
57 | * | ||||
58 | * Each item has the following properties: | ||||
59 | * - Name: The friendly text for the menu | ||||
60 | * - Exec: The command to execute, including arguments. | ||||
61 | * - TryExec: the name of the executable, if not available, the | ||||
62 | * item will not be displayed. | ||||
63 | * - MimeTypes: An optional list of mimetypes. The item will be disabled or | ||||
64 | * hidden if the current file is not of one of the indicated types. | ||||
65 | * | ||||
66 | */ | ||||
67 | class KateExternalToolsMenuAction : public KActionMenu | ||||
68 | { | ||||
69 | friend class KateExternalToolAction; | ||||
70 | | ||||
71 | Q_OBJECT | ||||
72 | public: | ||||
73 | KateExternalToolsMenuAction(const QString& text, class KActionCollection* collection, QObject* parent, | ||||
74 | class KTextEditor::MainWindow* mw = nullptr); | ||||
75 | virtual ~KateExternalToolsMenuAction(); | ||||
76 | | ||||
77 | /** | ||||
78 | * This will load all the configured services. | ||||
79 | */ | ||||
80 | void reload(); | ||||
81 | | ||||
82 | class KActionCollection* actionCollection() { return m_actionCollection; } | ||||
83 | | ||||
84 | private Q_SLOTS: | ||||
85 | void slotViewChanged(KTextEditor::View* view); | ||||
86 | | ||||
87 | private: | ||||
88 | class KActionCollection* m_actionCollection; | ||||
89 | KTextEditor::MainWindow* mainwindow; // for the actions to access view/doc managers | ||||
90 | }; | ||||
91 | | ||||
92 | /** | ||||
93 | * This Action contains a KateExternalTool | ||||
94 | */ | ||||
95 | class KateExternalToolAction : public QAction, public KWordMacroExpander | ||||
96 | { | ||||
97 | Q_OBJECT | ||||
98 | public: | ||||
99 | KateExternalToolAction(QObject* parent, class KateExternalTool* t); | ||||
100 | ~KateExternalToolAction(); | ||||
101 | | ||||
102 | protected: | ||||
103 | bool expandMacro(const QString& str, QStringList& ret) override; | ||||
104 | | ||||
105 | private Q_SLOTS: | ||||
106 | void slotRun(); | ||||
107 | | ||||
108 | public: | ||||
109 | class KateExternalTool* tool; | ||||
110 | }; | ||||
111 | | ||||
112 | /** | ||||
113 | * This class defines a single external tool. | ||||
114 | */ | ||||
115 | class KateExternalTool | ||||
116 | { | ||||
117 | public: | ||||
118 | explicit KateExternalTool(const QString& name = QString(), const QString& command = QString(), | ||||
119 | const QString& icon = QString(), const QString& executable = QString(), | ||||
120 | const QStringList& mimetypes = QStringList(), const QString& acname = QString(), | ||||
121 | const QString& cmdname = QString(), int save = 0); | ||||
122 | ~KateExternalTool() {} | ||||
123 | | ||||
124 | QString name; ///< The name used in the menu. | ||||
125 | QString executable; ///< The name or path of the executable. | ||||
126 | QString arguments; ///< The command line arguments. | ||||
127 | QString command; ///< The command to execute. | ||||
128 | QString icon; ///< the icon to use in the menu. | ||||
129 | QStringList mimetypes; ///< Optional list of mimetypes for which this action is valid. | ||||
130 | bool hasexec; ///< This is set by the constructor by calling checkExec(), if a | ||||
131 | ///< value is present. | ||||
132 | QString acname; ///< The name for the action. This is generated first time the | ||||
133 | ///< action is is created. | ||||
134 | QString cmdname; ///< The name for the commandline. | ||||
135 | int save; ///< We can save documents prior to activating the tool command: 0 = | ||||
136 | ///< nothing, 1 = current document, 2 = all documents. | ||||
137 | | ||||
138 | /** | ||||
139 | * @return true if mimetypes is empty, or the @p mimetype matches. | ||||
140 | */ | ||||
141 | bool valid(const QString& mimetype) const; | ||||
142 | /** | ||||
143 | * @return true if "executable" exists and has the executable bit set, or is | ||||
144 | * empty. | ||||
145 | * This is run at least once, and the tool is disabled if it fails. | ||||
146 | */ | ||||
147 | bool checkExec(); | ||||
148 | | ||||
149 | private: | ||||
150 | QString m_exec; ///< The fully qualified path of the executable. | ||||
151 | }; | ||||
152 | | ||||
153 | /** | ||||
154 | * The config widget. | ||||
155 | * The config widget allows the user to view a list of services of the type | ||||
156 | * "Kate/ExternalTool" and add, remove or edit them. | ||||
157 | */ | ||||
158 | class KateExternalToolsConfigWidget : public KTextEditor::ConfigPage, public Ui::ExternalToolsConfigWidget | ||||
159 | { | ||||
160 | Q_OBJECT | ||||
161 | public: | ||||
162 | KateExternalToolsConfigWidget(QWidget* parent, KateExternalToolsPlugin* plugin); | ||||
163 | virtual ~KateExternalToolsConfigWidget(); | ||||
164 | | ||||
165 | QString name() const override; | ||||
166 | QString fullName() const override; | ||||
167 | QIcon icon() const override; | ||||
168 | | ||||
169 | public Q_SLOTS: | ||||
170 | void apply() override; | ||||
171 | void reset() override; | ||||
172 | void defaults() override { reset(); } // double sigh | ||||
173 | | ||||
174 | private Q_SLOTS: | ||||
175 | void slotNew(); | ||||
176 | void slotEdit(); | ||||
177 | void slotRemove(); | ||||
178 | void slotInsertSeparator(); | ||||
179 | | ||||
180 | void slotMoveUp(); | ||||
181 | void slotMoveDown(); | ||||
182 | | ||||
183 | void slotSelectionChanged(); | ||||
184 | | ||||
185 | private: | ||||
186 | QPixmap blankIcon(); | ||||
187 | | ||||
188 | QStringList m_removed; | ||||
189 | | ||||
190 | class KConfig* config; | ||||
191 | | ||||
192 | bool m_changed; | ||||
193 | KateExternalToolsPlugin* m_plugin; | ||||
194 | }; | ||||
195 | | ||||
196 | /** | ||||
197 | * A Singleton class for invoking external tools with the view command line | ||||
198 | */ | ||||
199 | class KateExternalToolsCommand : public KTextEditor::Command | ||||
200 | { | ||||
201 | public: | ||||
202 | KateExternalToolsCommand(KateExternalToolsPlugin* plugin); | ||||
203 | virtual ~KateExternalToolsCommand() {} | ||||
204 | void reload(); | ||||
205 | | ||||
206 | public: | ||||
207 | // const QStringList& cmds() override; // FIXME | ||||
208 | bool exec(KTextEditor::View* view, const QString& cmd, QString& msg, | ||||
209 | const KTextEditor::Range& range = KTextEditor::Range::invalid()) override; | ||||
210 | bool help(KTextEditor::View* view, const QString& cmd, QString& msg) override; | ||||
211 | | ||||
212 | private: | ||||
213 | QStringList m_list; | ||||
214 | QHash<QString, QString> m_map; | ||||
215 | QHash<QString, QString> m_name; | ||||
216 | KateExternalToolsPlugin* m_plugin; | ||||
217 | }; | ||||
218 | | ||||
219 | /** | ||||
220 | * A Dialog to edit a single KateExternalTool object | ||||
221 | */ | ||||
222 | class KateExternalToolServiceEditor : public QDialog | ||||
223 | { | ||||
224 | Q_OBJECT | ||||
225 | | ||||
226 | public: | ||||
227 | explicit KateExternalToolServiceEditor(KateExternalTool* tool = nullptr, QWidget* parent = nullptr); | ||||
228 | | ||||
229 | private Q_SLOTS: | ||||
230 | /** | ||||
231 | * Run when the OK button is clicked, to ensure critical values are provided. | ||||
232 | */ | ||||
233 | void slotOKClicked(); | ||||
234 | | ||||
235 | /** | ||||
236 | * show a mimetype chooser dialog | ||||
237 | */ | ||||
238 | void showMTDlg(); | ||||
239 | | ||||
240 | public: | ||||
241 | Ui::ToolDialog * ui; | ||||
242 | private: | ||||
243 | KateExternalTool* tool; | ||||
244 | }; | ||||
245 | | ||||
246 | #endif // KTEXTEDITOR_EXTERNALTOOLS_H | ||||
247 | | ||||
248 | // kate: space-indent on; indent-width 4; replace-tabs on; |