File indexing completed on 2024-04-21 04:58:02
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include "kshellcmdplugin.h" 0008 #include "kshellcmddialog.h" 0009 #include <kparts/part.h> 0010 #include <kactioncollection.h> 0011 #include <KLocalizedString> 0012 #include <kmessagebox.h> 0013 #include <kshell.h> 0014 #include <kpluginfactory.h> 0015 #include <kauthorized.h> 0016 #include <kparts/fileinfoextension.h> 0017 #include <KParts/ReadOnlyPart> 0018 #include <kio/statjob.h> 0019 0020 #include <QAction> 0021 #include <QInputDialog> 0022 0023 KShellCmdPlugin::KShellCmdPlugin(QObject *parent, const QVariantList &) 0024 : KonqParts::Plugin(parent) 0025 { 0026 if (!KAuthorized::authorize(QStringLiteral("shell_access"))) { 0027 return; 0028 } 0029 0030 QAction *action = actionCollection()->addAction(QStringLiteral("executeshellcommand")); 0031 action->setIcon(QIcon::fromTheme(QStringLiteral("system-run"))); 0032 action->setText(i18n("&Execute Shell Command...")); 0033 connect(action, &QAction::triggered, this, &KShellCmdPlugin::slotExecuteShellCommand); 0034 actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_E)); 0035 } 0036 0037 void KShellCmdPlugin::slotExecuteShellCommand() 0038 { 0039 KParts::ReadOnlyPart *part = qobject_cast<KParts::ReadOnlyPart *>(parent()); 0040 if (!part) { 0041 KMessageBox::error(nullptr, i18n("KShellCmdPlugin::slotExecuteShellCommand: Program error, please report a bug.")); 0042 return; 0043 } 0044 0045 QUrl url; 0046 KIO::StatJob *statJob = KIO::mostLocalUrl(part->url()); 0047 if (statJob->exec()) { 0048 url = statJob->mostLocalUrl(); 0049 } 0050 if (!url.isLocalFile()) { 0051 KMessageBox::error(part->widget(), i18n("Executing shell commands works only on local directories.")); 0052 return; 0053 } 0054 0055 QString path; 0056 KParts::FileInfoExtension *ext = KParts::FileInfoExtension::childObject(part); 0057 0058 if (ext && ext->hasSelection() && (ext->supportedQueryModes() & KParts::FileInfoExtension::SelectedItems)) { 0059 KFileItemList list = ext->queryFor(KParts::FileInfoExtension::SelectedItems); 0060 QStringList fileNames; 0061 for (const KFileItem &item: list) { 0062 fileNames << item.name(); 0063 } 0064 path = KShell::joinArgs(fileNames); 0065 } 0066 0067 if (path.isEmpty()) { 0068 path = KShell::quoteArg(url.toLocalFile()); 0069 } 0070 0071 bool ok; 0072 QString cmd = QInputDialog::getText(part->widget(), 0073 i18nc("@title:window", "Execute Shell Command"), 0074 i18n("Execute shell command in current directory:"), 0075 QLineEdit::Normal, 0076 path, 0077 &ok); 0078 if (ok) { 0079 QString exeCmd; 0080 exeCmd = QStringLiteral("cd "); 0081 exeCmd += KShell::quoteArg(part->url().path()); 0082 exeCmd += QLatin1String("; "); 0083 exeCmd += cmd; 0084 0085 KShellCommandDialog *dlg = new KShellCommandDialog(i18n("Output from command: \"%1\"", cmd), exeCmd, part->widget(), true); 0086 dlg->resize(500, 300); 0087 dlg->executeCommand(); 0088 delete dlg; 0089 } 0090 } 0091 0092 K_PLUGIN_CLASS_WITH_JSON(KShellCmdPlugin, "kshellcmdplugin.json") 0093 0094 #include "kshellcmdplugin.moc" 0095