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 <QMetaType> 0010 #include <QString> 0011 #include <QStringList> 0012 #include <QUrl> 0013 0014 #include <optional> 0015 0016 class KConfigGroup; 0017 0018 /** 0019 * This class defines a single external tool. 0020 */ 0021 class KateExternalTool 0022 { 0023 public: 0024 /** 0025 * Defines whether any document should be saved before running the tool. 0026 */ 0027 enum class SaveMode { 0028 //! Do not save any document. 0029 None, 0030 //! Save current document. 0031 CurrentDocument, 0032 //! Save all documents 0033 AllDocuments 0034 }; 0035 0036 /** 0037 * Defines where to redirect stdout from the tool. 0038 */ 0039 enum class OutputMode { 0040 Ignore, 0041 InsertAtCursor, 0042 ReplaceSelectedText, 0043 ReplaceCurrentDocument, 0044 AppendToCurrentDocument, 0045 InsertInNewDocument, 0046 CopyToClipboard, 0047 DisplayInPane 0048 }; 0049 0050 enum class Trigger { 0051 //! No trigger 0052 None, 0053 //! Run the tool before saving 0054 BeforeSave, 0055 //! Run the tool after saving 0056 AfterSave, 0057 }; 0058 0059 public: 0060 /// The category used in the menu to categorize the tool. 0061 QString category; 0062 /// The name used in the menu. 0063 QString name; 0064 /// the icon to use in the menu. 0065 QString icon; 0066 /// The name or path of the executable. 0067 QString executable; 0068 /// The command line arguments. 0069 QString arguments; 0070 /// The stdin input. 0071 QString input; 0072 /// The working directory, if specified. 0073 QString workingDir; 0074 /// Optional list of mimetypes for which this action is valid. 0075 QStringList mimetypes; 0076 /// The name for the action for persistent keyboard shortcuts. 0077 /// This is generated first time the action is is created. 0078 QString actionName; 0079 /// The name for the commandline. 0080 QString cmdname; 0081 /// Possibly save documents prior to activating the tool command. 0082 SaveMode saveMode = SaveMode::None; 0083 /// Reload current document after execution 0084 bool reload = false; 0085 /// Defines where to redirect the tool's output 0086 OutputMode outputMode = OutputMode::Ignore; 0087 /// Trigger to run tool 0088 Trigger trigger = Trigger::None; 0089 0090 public: 0091 /// This is set when loading the Tool from disk. 0092 /// If the tool has an expandable variable, this will nullopt 0093 std::optional<bool> hasexec = false; 0094 0095 /** 0096 * @return true if the executable has a valid executable. 0097 * Will also return true, if the execuable contains an 0098 * expandable variable 0099 */ 0100 bool canExecute() const 0101 { 0102 return !hasexec || (hasexec.has_value() && hasexec.value() == true); 0103 } 0104 0105 /** 0106 * @return true if the @p mimetype matches. 0107 */ 0108 bool matchesMimetype(const QString &mimetype) const; 0109 0110 /** 0111 * @return true if "executable" exists and has the executable bit set, or is 0112 * empty. 0113 * This is run at least once, and the tool is disabled if it fails. 0114 */ 0115 bool checkExec() const; 0116 0117 /** 0118 * Load tool data from the config group @p cg. 0119 */ 0120 void load(const KConfigGroup &cg); 0121 0122 /** 0123 * Save tool data to the config group @p cg. 0124 */ 0125 void save(KConfigGroup &cg) const; 0126 0127 /** 0128 * Returns the translated name if possible. 0129 */ 0130 QString translatedName() const; 0131 0132 /** 0133 * Returns the translated category if possible. 0134 */ 0135 QString translatedCategory() const; 0136 0137 /** 0138 * Returns the config file name for this tool, created based on the tool "name". 0139 * this will be the name of the config file in e.g. ~/.config/kate/externaltools/ 0140 * will ensure we end up with some valid file name 0141 */ 0142 static QString configFileName(QString name) 0143 { 0144 // just percent encode the name, see bug 453272 0145 // we add a file ending to not clash with old files, old files were all lowercase 0146 return QString::fromUtf8(QUrl::toPercentEncoding(name)) + QStringLiteral(".ini"); 0147 } 0148 0149 /** 0150 * OLD names: we need this to cleanup! 0151 * Returns the config file name for this tool, created based on the tool "name", e.g. 0152 * "Clang Format Full File" -> clang_format_full_file 0153 * this will be the name of the config file in e.g. ~/.config/kate/externaltools/ 0154 */ 0155 static QString configFileNameOldStyleOnlyForRemove(QString name) 0156 { 0157 name.replace(QLatin1Char(' '), QLatin1Char('_')); 0158 // '(' and ')' are problematic as file names in the .qrc file 0159 name.replace(QLatin1Char('('), QLatin1Char('_')); 0160 name.replace(QLatin1Char(')'), QLatin1Char('_')); 0161 return name.toLower(); 0162 } 0163 }; 0164 0165 /** 0166 * Compares for equality. All fields have to match. 0167 */ 0168 bool operator==(const KateExternalTool &lhs, const KateExternalTool &rhs); 0169 0170 // for use in QVariant (QAction::setData() and QAction::data()) 0171 Q_DECLARE_METATYPE(KateExternalTool *) 0172 0173 // kate: space-indent on; indent-width 4; replace-tabs on;