1 /* This file is part of the KDE project
2  *
3  *  SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
4  *
5  *  SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 #ifndef KTEXTEDITOR_EXTERNALTOOLS_PLUGIN_H
8 #define KTEXTEDITOR_EXTERNALTOOLS_PLUGIN_H
9 
10 #include <KTextEditor/Plugin>
11 #include <QVector>
12 
13 #include <KSharedConfig>
14 
15 namespace KTextEditor
16 {
17 class View;
18 }
19 
20 class KateExternalToolsMenuAction;
21 class KateExternalToolsPluginView;
22 class KateExternalToolsCommand;
23 class KateExternalTool;
24 class KateToolRunner;
25 
26 class KateExternalToolsPlugin : public KTextEditor::Plugin
27 {
28     Q_OBJECT
29 
30 public:
31     explicit KateExternalToolsPlugin(QObject *parent = nullptr, const QList<QVariant> & = QList<QVariant>());
32     ~KateExternalToolsPlugin() override;
33 
34     /**
35      * Returns the global config object for the plugin (on Linux
36      * this is ~/.config/kateexternaltoolspluginrc).
37      */
config()38     KSharedConfigPtr config()
39     {
40         return m_config;
41     }
42 
43     /**
44      * Reimplemented to return the number of config pages, in this case 1.
45      */
46     int configPages() const override;
47 
48     /**
49      * Reimplemented to return the KateExternalToolConfigWidget for number==0.
50      */
51     KTextEditor::ConfigPage *configPage(int number = 0, QWidget *parent = nullptr) override;
52 
53     /**
54      * Reimplemented to instantiate a PluginView for each MainWindow.
55      */
56     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
57 
58     /**
59      * Deletes all tools.
60      */
61     void clearTools();
62 
63     /**
64      * Reloads the external tools configuration from disk.
65      */
66     void reload();
67 
68     /**
69      * Returns a list of KTextEDitor::Command strings. This is needed by
70      * the KateExternalToolsCommand constructor to pass the list of commands to
71      * the KTextEditor::Editor.
72      */
73     QStringList commands() const;
74 
75     /**
76      * Returns the KateExternalTool for a specific command line command 'cmd.
77      */
78     const KateExternalTool *toolForCommand(const QString &cmd) const;
79 
80     /**
81      * Returns a list of all existing external tools.
82      */
83     const QVector<KateExternalTool *> &tools() const;
84 
85     /**
86      * Returns the list of external tools that are shipped by default with
87      * the external tools plugin.
88      */
89     QVector<KateExternalTool> defaultTools() const;
90 
91     /**
92      * Executes the tool based on the view as current document.
93      */
94     void runTool(const KateExternalTool &tool, KTextEditor::View *view);
95 
96 Q_SIGNALS:
97     /**
98      * This signal is emitted whenever the external tools change.
99      * This is typically the case when external tools were modified,
100      * added, or removed via the config page.
101      */
102     void externalToolsChanged();
103 
104 public:
105     /**
106      * Called by the KateExternalToolsPluginView to register itself.
107      */
108     void registerPluginView(KateExternalToolsPluginView *view);
109 
110     /**
111      * Called by the KateExternalToolsPluginView to unregister itself.
112      */
113     void unregisterPluginView(KateExternalToolsPluginView *view);
114 
115     /**
116      * Returns the KateExternalToolsPluginView for the given mainWindow.
117      */
118     KateExternalToolsPluginView *viewForMainWindow(KTextEditor::MainWindow *mainWindow) const;
119 
120     void addNewTool(KateExternalTool *tool);
121 
122     /**
123      * Removes the tools in @p toRemove, this includes removing the relevant
124      * config file from disk.
125      */
126     void removeTools(const std::vector<KateExternalTool *> &toRemove);
127 
128     /**
129      * Saves @p tool config. If @p oldName is not empty, then the tool's name
130      * was changed, and after saving the config (to a new file based on the
131      * new name), the old config file is removed.
132      */
133     void save(KateExternalTool *tool, const QString &oldName) const;
134 
135 private:
136     void migrateConfig();
137 
138     KSharedConfigPtr m_config;
139     QVector<KateExternalTool> m_defaultTools;
140     QVector<KateExternalToolsPluginView *> m_views;
141     QVector<KateExternalTool *> m_tools;
142     QStringList m_commands;
143     KateExternalToolsCommand *m_command = nullptr;
144 
145 private Q_SLOTS:
146     /**
147      * Called whenever an external tool is done.
148      */
149     void handleToolFinished(KateToolRunner *runner, int exitCode, bool crashed);
150 };
151 
152 #endif
153 
154 // kate: space-indent on; indent-width 4; replace-tabs on;
155