File indexing completed on 2024-05-05 05:51:22

0001 /* This file is part of the KDE project
0002  *
0003  *  SPDX-FileCopyrightText: 2019 Dominik Haumann <dhaumann@kde.org>
0004  *
0005  *  SPDX-License-Identifier: LGPL-2.0-or-later
0006  */
0007 #pragma once
0008 
0009 #include <KTextEditor/Plugin>
0010 #include <QList>
0011 
0012 #include <KSharedConfig>
0013 
0014 namespace KTextEditor
0015 {
0016 class View;
0017 }
0018 
0019 class KateExternalToolsMenuAction;
0020 class KateExternalToolsPluginView;
0021 class KateExternalToolsCommand;
0022 class KateExternalTool;
0023 class KateToolRunner;
0024 
0025 class KateExternalToolsPlugin : public KTextEditor::Plugin
0026 {
0027     Q_OBJECT
0028 
0029 public:
0030     explicit KateExternalToolsPlugin(QObject *parent = nullptr, const QVariantList & = QVariantList());
0031     ~KateExternalToolsPlugin() override;
0032 
0033     /**
0034      * Returns the global config object for the plugin (on Linux
0035      * this is ~/.config/kateexternaltoolspluginrc).
0036      */
0037     KSharedConfigPtr config()
0038     {
0039         return m_config;
0040     }
0041 
0042     /**
0043      * Reimplemented to return the number of config pages, in this case 1.
0044      */
0045     int configPages() const override;
0046 
0047     /**
0048      * Reimplemented to return the KateExternalToolConfigWidget for number==0.
0049      */
0050     KTextEditor::ConfigPage *configPage(int number = 0, QWidget *parent = nullptr) override;
0051 
0052     /**
0053      * Reimplemented to instantiate a PluginView for each MainWindow.
0054      */
0055     QObject *createView(KTextEditor::MainWindow *mainWindow) override;
0056 
0057     /**
0058      * Deletes all tools.
0059      */
0060     void clearTools();
0061 
0062     /**
0063      * Reloads the external tools configuration from disk.
0064      */
0065     void reload();
0066 
0067     /**
0068      * Returns a list of KTextEDitor::Command strings. This is needed by
0069      * the KateExternalToolsCommand constructor to pass the list of commands to
0070      * the KTextEditor::Editor.
0071      */
0072     QStringList commands() const;
0073 
0074     /**
0075      * Returns the KateExternalTool for a specific command line command 'cmd.
0076      */
0077     const KateExternalTool *toolForCommand(const QString &cmd) const;
0078 
0079     /**
0080      * Returns a list of all existing external tools.
0081      */
0082     const QList<KateExternalTool *> &tools() const;
0083 
0084     /**
0085      * Returns the list of external tools that are shipped by default with
0086      * the external tools plugin.
0087      */
0088     QList<KateExternalTool> defaultTools() const;
0089 
0090     /**
0091      * Executes the tool based on the view as current document.
0092      */
0093     void runTool(const KateExternalTool &tool, KTextEditor::View *view, bool executingSaveTrigger = false);
0094 
0095     /**
0096      * Executes the tool based on the view as current document.
0097      * same as @ref runTool but waits for the tool to finish before returning
0098      */
0099     void blockingRunTool(const KateExternalTool &tool, KTextEditor::View *view, bool executingSaveTrigger = false);
0100 
0101 Q_SIGNALS:
0102     /**
0103      * This signal is emitted whenever the external tools change.
0104      * This is typically the case when external tools were modified,
0105      * added, or removed via the config page.
0106      */
0107     void externalToolsChanged();
0108 
0109 public:
0110     /**
0111      * Called by the KateExternalToolsPluginView to register itself.
0112      */
0113     void registerPluginView(KateExternalToolsPluginView *view);
0114 
0115     /**
0116      * Called by the KateExternalToolsPluginView to unregister itself.
0117      */
0118     void unregisterPluginView(KateExternalToolsPluginView *view);
0119 
0120     /**
0121      * Returns the KateExternalToolsPluginView for the given mainWindow.
0122      */
0123     KateExternalToolsPluginView *viewForMainWindow(KTextEditor::MainWindow *mainWindow) const;
0124 
0125     void addNewTool(KateExternalTool *tool);
0126 
0127     /**
0128      * Removes the tools in @p toRemove, this includes removing the relevant
0129      * config file from disk.
0130      */
0131     void removeTools(const std::vector<KateExternalTool *> &toRemove);
0132 
0133     /**
0134      * Saves @p tool config. If @p oldName is not empty, then the tool's name
0135      * was changed, and after saving the config (to a new file based on the
0136      * new name), the old config file is removed.
0137      */
0138     static void save(KateExternalTool *tool, const QString &oldName);
0139 
0140 private:
0141     void migrateConfig();
0142     KateToolRunner *runnerForTool(const KateExternalTool &tool, KTextEditor::View *view, bool executingSaveTrigger);
0143 
0144     KSharedConfigPtr m_config;
0145     QList<KateExternalTool> m_defaultTools;
0146     QList<KateExternalToolsPluginView *> m_views;
0147     QList<KateExternalTool *> m_tools;
0148     QStringList m_commands;
0149     KateExternalToolsCommand *m_command = nullptr;
0150 
0151 private Q_SLOTS:
0152     /**
0153      * Called whenever an external tool is done.
0154      */
0155     void handleToolFinished(KateToolRunner *runner, int exitCode, bool crashed);
0156 };
0157 
0158 // kate: space-indent on; indent-width 4; replace-tabs on;