File indexing completed on 2024-04-28 04:38:41

0001 /*
0002     SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #ifndef KDEVPLATFORM_PLUGIN_EXTERNALSCRIPTPLUGIN_H
0008 #define KDEVPLATFORM_PLUGIN_EXTERNALSCRIPTPLUGIN_H
0009 
0010 #include <interfaces/iplugin.h>
0011 #include <QVariantList>
0012 #include <KConfigGroup>
0013 #include <QUrl>
0014 
0015 class ExternalScriptItem;
0016 
0017 class QStandardItem;
0018 class QStandardItemModel;
0019 class QModelIndex;
0020 
0021 class ExternalScriptPlugin
0022     : public KDevelop::IPlugin
0023 {
0024     Q_OBJECT
0025     Q_CLASSINFO("D-Bus Interface", "org.kdevelop.ExternalScriptPlugin")
0026 
0027 public:
0028     explicit ExternalScriptPlugin(QObject* parent, const QVariantList& args = QVariantList());
0029 
0030     ~ExternalScriptPlugin() override;
0031     void unload() override;
0032     KDevelop::ContextMenuExtension contextMenuExtension(KDevelop::Context* context, QWidget* parent) override;
0033 
0034     static ExternalScriptPlugin* self();
0035 
0036     /**
0037      * @return The model storing all external scripts managed by this plugin.
0038      * @NOTE: always append() items, never insert in the middle!
0039      */
0040     QStandardItemModel* model() const;
0041 
0042     /**
0043      * Executes @p script.
0044      */
0045     void execute(ExternalScriptItem* item, const QUrl& url) const;
0046 
0047     /**
0048      * Executes @p script.
0049      */
0050     void execute(ExternalScriptItem* item) const;
0051 
0052     /**
0053      * Returns config group to store all settings for this plugin in.
0054      */
0055     KConfigGroup getConfig() const;
0056 
0057     /**
0058      * Saves the @p script to the config and updates the key
0059      */
0060     void saveItem(const ExternalScriptItem* item);
0061 
0062 public Q_SLOTS:
0063     void executeScriptFromActionData() const;
0064 
0065     /**
0066      * Executes the command (Used by the shell-integration)
0067      * */
0068     Q_SCRIPTABLE bool executeCommand(const QString& command, const QString& workingDirectory) const;
0069 
0070     /**
0071      * Executes the command synchronously and returns the output text (Used by the shell-integration)
0072      * */
0073     Q_SCRIPTABLE QString executeCommandSync(const QString& command, const QString& workingDirectory) const;
0074 
0075 private Q_SLOTS:
0076     void rowsAboutToBeRemoved(const QModelIndex& parent, int start, int end);
0077     void rowsInserted(const QModelIndex& parent, int start, int end);
0078     void executeScriptFromContextMenu() const;
0079 
0080 private:
0081     /// @param row row in the model for the item to save
0082     void saveItemForRow(int row);
0083 
0084     /**
0085      * Sets up unique keys for items in the range [start, end]
0086      * @param start start of the range
0087      * @param end end of the range
0088      */
0089     void setupKeys(int start, int end);
0090 
0091     QStandardItemModel* m_model;
0092     QList<QUrl> m_urls;
0093     static ExternalScriptPlugin* m_self;
0094 
0095     class ExternalScriptViewFactory* m_factory;
0096 };
0097 
0098 #endif // KDEVPLATFORM_PLUGIN_EXTERNALSCRIPTPLUGIN_H