Commit 42a4e4db authored by Jonathan Marten's avatar Jonathan Marten Committed by Stefano Crocco

Port and update the Shell Command plugin

kDebug(), qDebug() -> qCDebug() with logging category
No need to link with KDELibs4Support
Use Qt5 signal connection syntax
KDialog -> QDialog
KInputDialog -> QInputDialog
KIO::NetAccess::mostLocalUrl() -> KIO::mostLocalUrl()
parent 1b6ba1e8
......@@ -4,11 +4,9 @@ find_package(KF5 REQUIRED Su)
add_definitions(-DTRANSLATION_DOMAIN=\"kshellcmdplugin\")
set(konq_shellcmdplugin_PART_SRCS kshellcmdexecutor.cpp kshellcmddialog.cpp kshellcmdplugin.cpp )
add_library(konq_shellcmdplugin MODULE ${konq_shellcmdplugin_PART_SRCS})
target_link_libraries(konq_shellcmdplugin KF5::Parts KF5::Su KF5::KDELibs4Support)
target_link_libraries(konq_shellcmdplugin KF5::Parts KF5::Su)
install(TARGETS konq_shellcmdplugin DESTINATION ${KDE_INSTALL_PLUGINDIR} )
......
......@@ -24,6 +24,7 @@
#include <QLayout>
#include <QLabel>
#include <QPushButton>
#include <QDialogButtonBox>
// KDE
#include <KLocalizedString>
......@@ -33,36 +34,26 @@
#include "kshellcmdexecutor.h"
KShellCommandDialog::KShellCommandDialog(const QString &title, const QString &command, QWidget *parent, bool modal)
: KDialog(parent)
: QDialog(parent)
{
setModal(modal);
setButtons(KDialog::None);
QWidget *w = new QWidget(this);
QVBoxLayout *box = new QVBoxLayout;
w->setLayout(box);
setMainWidget(w);
QLabel *label = new QLabel(title, this);
m_shell = new KShellCommandExecutor(command, this);
cancelButton = new QPushButton(this);
KGuiItem::assign(cancelButton, KStandardGuiItem::cancel());
closeButton = new QPushButton(this);
KGuiItem::assign(closeButton, KStandardGuiItem::close());
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close|QDialogButtonBox::Cancel, this);
cancelButton = buttonBox->button(QDialogButtonBox::Cancel);
closeButton = buttonBox->button(QDialogButtonBox::Close);
closeButton->setDefault(true);
label->resize(label->sizeHint());
m_shell->resize(m_shell->sizeHint());
closeButton->setFixedSize(closeButton->sizeHint());
cancelButton->setFixedSize(cancelButton->sizeHint());
box->addWidget(label, 0);
box->addWidget(m_shell, 1);
QHBoxLayout *hlayout = new QHBoxLayout();
box->addLayout(hlayout);
hlayout->addWidget(cancelButton);
hlayout->addWidget(closeButton);
QVBoxLayout *box = new QVBoxLayout(this);
box->addWidget(label);
box->addWidget(m_shell);
box->setStretchFactor(m_shell, 1);
box->addWidget(buttonBox);
m_shell->setFocus();
......@@ -95,7 +86,7 @@ int KShellCommandDialog::executeCommand()
if (m_shell == nullptr) {
return 0;
}
//kDebug()<<"---------- KShellCommandDialog::executeCommand()";
m_shell->exec();
return exec();
}
......
......@@ -20,12 +20,12 @@
#ifndef KSHELLCMDDIALOG_H
#define KSHELLCMDDIALOG_H
#include <kdialog.h>
#include <qdialog.h>
class KShellCommandExecutor;
class QPushButton;
class KShellCommandDialog: public KDialog
class KShellCommandDialog: public QDialog
{
Q_OBJECT
public:
......
......@@ -25,13 +25,12 @@
#include <stdlib.h>
#include <QSocketNotifier>
#include <QInputDialog>
#include <QFontDatabase>
#include <kinputdialog.h>
#include <kdesu/process.h>
#include <KLocalizedString>
#include <QFontDatabase>
using namespace KDESu;
KShellCommandExecutor::KShellCommandExecutor(const QString &command, QWidget *parent)
: QTextEdit(parent)
......@@ -55,7 +54,7 @@ KShellCommandExecutor::~KShellCommandExecutor()
int KShellCommandExecutor::exec()
{
//kDebug()<<"---------- KShellCommandExecutor::exec()";
//qCDebug(KONQUEROR_LOG)<<"---------- KShellCommandExecutor::exec()";
setText(QLatin1String(""));
if (m_shellProcess != nullptr) {
::kill(m_shellProcess->pid(), SIGTERM);
......@@ -64,13 +63,13 @@ int KShellCommandExecutor::exec()
delete m_readNotifier;
delete m_writeNotifier;
m_shellProcess = new PtyProcess();
m_shellProcess = new KDESu::PtyProcess();
m_shellProcess->setTerminal(true);
QList<QByteArray> args;
args += "-c";
args += m_command.toLocal8Bit();
//kDebug()<<"------- executing: "<<m_command.toLocal8Bit();
//qCDebug(KONQUEROR_LOG)<<"------- executing: "<<m_command.toLocal8Bit();
QByteArray shell(getenv("SHELL"));
if (shell.isEmpty()) {
......@@ -79,7 +78,7 @@ int KShellCommandExecutor::exec()
int ret = m_shellProcess->exec(shell, args);
if (ret < 0) {
//kDebug()<<"could not execute";
//qCDebug(KONQUEROR_LOG)<<"could not execute";
delete m_shellProcess;
m_shellProcess = nullptr;
return 0;
......@@ -88,15 +87,15 @@ int KShellCommandExecutor::exec()
m_readNotifier = new QSocketNotifier(m_shellProcess->fd(), QSocketNotifier::Read, this);
m_writeNotifier = new QSocketNotifier(m_shellProcess->fd(), QSocketNotifier::Write, this);
m_writeNotifier->setEnabled(false);
connect(m_readNotifier, SIGNAL(activated(int)), this, SLOT(readDataFromShell()));
connect(m_writeNotifier, SIGNAL(activated(int)), this, SLOT(writeDataToShell()));
connect(m_readNotifier, &QSocketNotifier::activated, this, &KShellCommandExecutor::readDataFromShell);
connect(m_writeNotifier, &QSocketNotifier::activated, this, &KShellCommandExecutor::writeDataToShell);
return 1;
}
void KShellCommandExecutor::readDataFromShell()
{
//kDebug()<<"--------- reading ------------";
//qCDebug(KONQUEROR_LOG)<<"--------- reading ------------";
char buffer[16 * 1024];
int bytesRead =::read(m_shellProcess->fd(), buffer, 16 * 1024 - 1);
//0-terminate the buffer
......@@ -104,7 +103,7 @@ void KShellCommandExecutor::readDataFromShell()
if (bytesRead <= 0) {
slotFinished();
} else if (bytesRead > 0) {
//kDebug()<<"***********************\n"<<buffer<<"###################\n";
//qCDebug(KONQUEROR_LOG)<<"***********************\n"<<buffer<<"###################\n";
buffer[bytesRead] = '\0';
this->append(QString::fromLocal8Bit(buffer));
setAcceptRichText(false);
......@@ -113,10 +112,14 @@ void KShellCommandExecutor::readDataFromShell()
void KShellCommandExecutor::writeDataToShell()
{
//kDebug()<<"--------- writing ------------";
//qCDebug(KONQUEROR_LOG)<<"--------- writing ------------";
bool ok;
QString str = KInputDialog::getText(QString(),
i18n("Input Required:"), QString(), &ok, this);
QString str = QInputDialog::getText(this,
QString(),
i18n("Input Required:"),
QLineEdit::Normal,
QString(),
&ok);
if (ok) {
QByteArray input = str.toLocal8Bit();
::write(m_shellProcess->fd(), input, input.length());
......@@ -139,7 +142,7 @@ void KShellCommandExecutor::slotFinished()
delete m_writeNotifier;
m_writeNotifier = nullptr;
//kDebug()<<"slotFinished: pid: "<<m_shellProcess->pid();
//qCDebug(KONQUEROR_LOG)<<"slotFinished: pid: "<<m_shellProcess->pid();
::kill(m_shellProcess->pid() + 1, SIGTERM);
::kill(m_shellProcess->pid(), SIGTERM);
};
......
......@@ -20,16 +20,17 @@
#include "kshellcmddialog.h"
#include <kparts/part.h>
#include <kactioncollection.h>
#include <kinputdialog.h>
#include <KLocalizedString>
#include <kmessagebox.h>
#include <kshell.h>
#include <kpluginfactory.h>
#include <kauthorized.h>
#include <kio/netaccess.h>
#include <kparts/fileinfoextension.h>
#include <KParts/ReadOnlyPart>
#include <kio/statjob.h>
#include <QAction>
#include <QInputDialog>
KShellCmdPlugin::KShellCmdPlugin(QObject *parent, const QVariantList &)
: KParts::Plugin(parent)
......@@ -53,7 +54,11 @@ void KShellCmdPlugin::slotExecuteShellCommand()
return;
}
QUrl url = KIO::NetAccess::mostLocalUrl(part->url(), nullptr);
QUrl url;
KIO::StatJob *statJob = KIO::mostLocalUrl(part->url());
if (statJob->exec()) {
url = statJob->mostLocalUrl();
}
if (!url.isLocalFile()) {
KMessageBox::sorry(part->widget(), i18n("Executing shell commands works only on local directories."));
return;
......@@ -76,9 +81,12 @@ void KShellCmdPlugin::slotExecuteShellCommand()
}
bool ok;
QString cmd = KInputDialog::getText(i18nc("@title:window", "Execute Shell Command"),
QString cmd = QInputDialog::getText(part->widget(),
i18nc("@title:window", "Execute Shell Command"),
i18n("Execute shell command in current directory:"),
path, &ok, part->widget());
QLineEdit::Normal,
path,
&ok);
if (ok) {
QString exeCmd;
exeCmd = QStringLiteral("cd ");
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment