File indexing completed on 2024-04-28 04:38:40
0001 /* 0002 SPDX-FileCopyrightText: 2010 Milian Wolff <mail@milianw.de> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "editexternalscript.h" 0008 0009 #include "externalscriptitem.h" 0010 0011 #include <QAction> 0012 #include <QDialogButtonBox> 0013 #include <QPushButton> 0014 0015 #include <KLocalizedString> 0016 #include <KShell> 0017 0018 EditExternalScript::EditExternalScript(ExternalScriptItem* item, QWidget* parent) 0019 : QDialog(parent) 0020 , m_item(item) 0021 { 0022 setupUi(this); 0023 0024 connect(buttonBox, &QDialogButtonBox::accepted, this, &EditExternalScript::accept); 0025 connect(buttonBox, &QDialogButtonBox::rejected, this, &EditExternalScript::reject); 0026 shortcutWidget->layout()->setContentsMargins(0, 0, 0, 0); 0027 0028 //BEGIN setup tooltips 0029 QString tooltip = i18nc("@info:tooltip", 0030 "<p>Defines the command that should be executed when this script is run. Basic shell features of your platform should be available.</p>\n" 0031 "<p>There are a few placeholders you can use in the command:</p>\n" 0032 "<dl>\n" 0033 " <dt><code>%u</code></dt>\n" 0034 " <dd>Gets replaced by the URL of the active document.</dd>\n" 0035 " <dt><code>%f</code></dt>\n" 0036 " <dd>Gets replaced by the local filepath to the active document.</dd>\n" 0037 " <dt><code>%n</code></dt>\n" 0038 " <dd>Gets replaced by the name of the active document, including its extension.</dd>\n" 0039 " <dt><code>%b</code></dt>\n" 0040 " <dd>Gets replaced by the name of the active document without its extension.</dd>\n" 0041 " <dt><code>%d</code></dt>\n" 0042 " <dd>Gets replaced by the path to the directory of the active document.</dd>\n" 0043 " <dt><code>%p</code></dt>\n" 0044 " <dd>Gets replaced by the URL to the project of the active document.</dd>\n" 0045 " <dt><code>%s</code></dt>\n" 0046 " <dd>Gets replaced with the shell escaped contents of the selection in the active document.</dd>\n" 0047 " <dt><code>%i</code></dt>\n" 0048 " <dd>Gets replaced with the PID of the currently running KDevelop process.</dd>\n" 0049 "</dl>\n" 0050 "<p><b>NOTE:</b> It is your responsibility to prevent running hazardous commands that could lead to data loss.</p>\n" 0051 ); 0052 commandEdit->setToolTip(tooltip); 0053 commandLabel->setToolTip(tooltip); 0054 0055 tooltip = i18nc("@info:tooltip", 0056 "<p>Defines what the external script should get as input (via <code>STDIN</code>).</p>" 0057 ); 0058 stdinCombo->setToolTip(tooltip); 0059 stdinLabel->setToolTip(tooltip); 0060 0061 tooltip = i18nc("@info:tooltip", 0062 "<p>Defines what should be done with the output (i.e. <code>STDOUT</code>) of the script.</p>" 0063 ); 0064 stdoutCombo->setToolTip(tooltip); 0065 stdoutLabel->setToolTip(tooltip); 0066 0067 tooltip = i18nc("@info:tooltip", 0068 "<p>Defines what should be done with the errors (i.e. <code>STDERR</code>) of the script.</p>" 0069 "<p>Note: if the action is the same as that chosen for the output, the channels will be merged " 0070 "and handled together.</p>" 0071 ); 0072 stderrCombo->setToolTip(tooltip); 0073 stderrLabel->setToolTip(tooltip); 0074 0075 tooltip = i18nc("@info:tooltip", 0076 "<p>Defines the name of the script. Just for displaying purposes.</p>" 0077 ); 0078 nameEdit->setToolTip(tooltip); 0079 nameLabel->setToolTip(tooltip); 0080 0081 tooltip = i18nc("@info:tooltip", 0082 "<p>Defines the shortcut(s) you can use to execute this external script.</p>" 0083 ); 0084 shortcutLabel->setToolTip(tooltip); 0085 shortcutWidget->setToolTip(tooltip); 0086 0087 tooltip = i18nc("@info:tooltip", 0088 "<p>Defines whether documents should be saved before the script gets executed.</p>" 0089 ); 0090 saveLabel->setToolTip(tooltip); 0091 saveCombo->setToolTip(tooltip); 0092 0093 tooltip = i18nc("@info:tooltip", 0094 "<p>Defines whether the output of the script should be shown in a tool view.</p>" 0095 ); 0096 showOutputBox->setToolTip(tooltip); 0097 tooltip = i18nc("@info:tooltip", 0098 "<p>Defines what type of filtering should be applied to the output. E.g. to indicate errors by red text.</p>" 0099 ); 0100 outputFilterLabel->setToolTip(tooltip); 0101 outputFilterCombo->setToolTip(tooltip); 0102 //END setup tooltips 0103 0104 //BEGIN item to UI copying 0105 if (item->text().isEmpty()) { 0106 setWindowTitle(i18nc("@title:window", "Create New External Script")); 0107 } else { 0108 setWindowTitle(i18nc("@title:window", "Edit External Script '%1'", item->text())); 0109 } 0110 nameEdit->setText(item->text()); 0111 commandEdit->setText(item->command()); 0112 stdinCombo->setCurrentIndex(item->inputMode()); 0113 stdoutCombo->setCurrentIndex(item->outputMode()); 0114 stderrCombo->setCurrentIndex(item->errorMode()); 0115 saveCombo->setCurrentIndex(item->saveMode()); 0116 shortcutWidget->setShortcut(item->action()->shortcuts()); 0117 showOutputBox->setChecked(item->showOutput()); 0118 outputFilterCombo->setCurrentIndex(item->filterMode()); 0119 //END item to UI copying 0120 0121 validate(); 0122 0123 nameEdit->setFocus(); 0124 0125 connect(buttonBox->button(QDialogButtonBox::Ok), &QPushButton::clicked, this, &EditExternalScript::save); 0126 connect(buttonBox->button(QDialogButtonBox::Apply), &QPushButton::clicked, this, &EditExternalScript::save); 0127 0128 connect(nameEdit, &QLineEdit::textEdited, this, &EditExternalScript::validate); 0129 connect(commandEdit, &QLineEdit::textEdited, this, &EditExternalScript::validate); 0130 } 0131 0132 EditExternalScript::~EditExternalScript() 0133 { 0134 } 0135 0136 void EditExternalScript::save() 0137 { 0138 m_item->setText(nameEdit->text()); 0139 m_item->setCommand(commandEdit->text()); 0140 0141 auto inputMode = static_cast<ExternalScriptItem::InputMode>(stdinCombo->currentIndex()); 0142 m_item->setInputMode(inputMode); 0143 0144 auto outputMode = static_cast<ExternalScriptItem::OutputMode>(stdoutCombo->currentIndex()); 0145 m_item->setOutputMode(outputMode); 0146 0147 auto errorMode = static_cast<ExternalScriptItem::ErrorMode>(stderrCombo->currentIndex()); 0148 m_item->setErrorMode(errorMode); 0149 0150 auto saveMode = static_cast<ExternalScriptItem::SaveMode>(saveCombo->currentIndex()); 0151 m_item->setSaveMode(saveMode); 0152 0153 m_item->setShowOutput(showOutputBox->isChecked()); 0154 0155 m_item->setFilterMode(outputFilterCombo->currentIndex()); 0156 m_item->action()->setShortcuts(shortcutWidget->shortcut()); 0157 } 0158 0159 void EditExternalScript::validate() 0160 { 0161 bool valid = !nameEdit->text().isEmpty() && !commandEdit->text().isEmpty(); 0162 if (valid) { 0163 KShell::Errors errors = KShell::NoError; 0164 KShell::splitArgs(commandEdit->text(), KShell::TildeExpand, &errors); 0165 valid = errors == KShell::NoError; 0166 } 0167 0168 buttonBox->button(QDialogButtonBox::Ok)->setEnabled(valid); 0169 buttonBox->button(QDialogButtonBox::Apply)->setEnabled(valid); 0170 } 0171 0172 #include "moc_editexternalscript.cpp"