File: kshellcmdplugin.cpp

package info (click to toggle)
konqueror 4%3A25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 61,780 kB
  • sloc: cpp: 59,015; python: 943; xml: 683; javascript: 186; sh: 165; makefile: 10
file content (95 lines) | stat: -rw-r--r-- 3,210 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/* This file is part of the KDE project
    SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "kshellcmdplugin.h"
#include "kshellcmddialog.h"
#include <kparts/part.h>
#include <kactioncollection.h>
#include <KLocalizedString>
#include <kmessagebox.h>
#include <kshell.h>
#include <kpluginfactory.h>
#include <kauthorized.h>
#include <kparts/fileinfoextension.h>
#include <KParts/ReadOnlyPart>
#include <kio/statjob.h>

#include <QAction>
#include <QInputDialog>

KShellCmdPlugin::KShellCmdPlugin(QObject *parent, const QVariantList &)
    : KonqParts::Plugin(parent)
{
    if (!KAuthorized::authorize(QStringLiteral("shell_access"))) {
        return;
    }

    QAction *action = actionCollection()->addAction(QStringLiteral("executeshellcommand"));
    action->setIcon(QIcon::fromTheme(QStringLiteral("system-run")));
    action->setText(i18n("&Execute Shell Command..."));
    connect(action, &QAction::triggered, this, &KShellCmdPlugin::slotExecuteShellCommand);
    actionCollection()->setDefaultShortcut(action, QKeySequence(Qt::CTRL | Qt::Key_E));
}

void KShellCmdPlugin::slotExecuteShellCommand()
{
    KParts::ReadOnlyPart *part = qobject_cast<KParts::ReadOnlyPart *>(parent());
    if (!part)  {
        KMessageBox::error(nullptr, i18n("KShellCmdPlugin::slotExecuteShellCommand: Program error, please report a bug."));
        return;
    }

    QUrl url;
    KIO::StatJob *statJob = KIO::mostLocalUrl(part->url());
    if (statJob->exec()) {
        url = statJob->mostLocalUrl();
    }
    if (!url.isLocalFile()) {
        KMessageBox::error(part->widget(), i18n("Executing shell commands works only on local directories."));
        return;
    }

    QString path;
    KParts::FileInfoExtension *ext = KParts::FileInfoExtension::childObject(part);

    if (ext && ext->hasSelection() && (ext->supportedQueryModes() & KParts::FileInfoExtension::SelectedItems)) {
        KFileItemList list = ext->queryFor(KParts::FileInfoExtension::SelectedItems);
        QStringList fileNames;
        for (const KFileItem &item: list) {
            fileNames << item.name();
        }
        path = KShell::joinArgs(fileNames);
    }

    if (path.isEmpty()) {
        path = KShell::quoteArg(url.toLocalFile());
    }

    bool ok;
    QString cmd = QInputDialog::getText(part->widget(),
                                        i18nc("@title:window", "Execute Shell Command"),
                                        i18n("Execute shell command in current directory:"),
                                        QLineEdit::Normal,
                                        path,
                                        &ok);
    if (ok) {
        QString exeCmd;
        exeCmd = QStringLiteral("cd ");
        exeCmd += KShell::quoteArg(part->url().path());
        exeCmd += QLatin1String("; ");
        exeCmd += cmd;

        KShellCommandDialog *dlg = new KShellCommandDialog(i18n("Output from command: \"%1\"", cmd), exeCmd, part->widget(), true);
        dlg->resize(500, 300);
        dlg->executeCommand();
        delete dlg;
    }
}

K_PLUGIN_CLASS_WITH_JSON(KShellCmdPlugin, "kshellcmdplugin.json")

#include "kshellcmdplugin.moc"