File indexing completed on 2024-04-28 15:40:12
0001 // SPDX-FileCopyrightText: 2009-2020 Wes Hardaker <kpa@capturedonearth.com> 0002 // SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at> 0003 // 0004 // SPDX-License-Identifier: GPL-2.0-or-later 0005 0006 #include "RunDialog.h" 0007 0008 #include "Window.h" 0009 0010 #include <KLocalizedString> 0011 #include <QDialog> 0012 #include <QDialogButtonBox> 0013 #include <QLabel> 0014 #include <QPushButton> 0015 #include <QVBoxLayout> 0016 #include <QWidget> 0017 #include <kio_version.h> 0018 #if KIO_VERSION > QT_VERSION_CHECK(5, 69, 0) 0019 #include <KIO/CommandLauncherJob> 0020 #include <KIO/JobUiDelegate> 0021 #else 0022 #include <KRun> 0023 #endif 0024 #include <kshell.h> 0025 0026 MainWindow::RunDialog::RunDialog(QWidget *parent) 0027 : QDialog(parent) 0028 { 0029 QVBoxLayout *mainLayout = new QVBoxLayout; 0030 setLayout(mainLayout); 0031 0032 // xgettext: no-c-format 0033 QString txt = i18n("<p>Enter your command to run below:</p>" 0034 "<p><i>%all will be replaced with a file list</i></p>"); 0035 QLabel *label = new QLabel(txt); 0036 mainLayout->addWidget(label); 0037 0038 m_cmd = new QLineEdit(); 0039 mainLayout->addWidget(m_cmd); 0040 m_cmd->setMinimumWidth(400); 0041 // xgettext: no-c-format 0042 txt = i18n("<p>Enter the command you want to run on your image file(s). " 0043 "KPhotoAlbum will run your command and replace any '%all' tokens " 0044 "with a list of your files. For example, if you entered:</p>" 0045 "<ul><li>cp %all /tmp</li></ul>" 0046 "<p>Then the files you selected would be copied to the /tmp " 0047 "folder</p>" 0048 "<p>You can also use %each to have a command be run once per " 0049 "file.</p>"); 0050 m_cmd->setWhatsThis(txt); 0051 label->setWhatsThis(txt); 0052 0053 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); 0054 buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return); 0055 mainLayout->addWidget(buttonBox); 0056 0057 connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); 0058 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0059 connect(this, &QDialog::accepted, this, &RunDialog::slotMarkGo); 0060 } 0061 0062 void MainWindow::RunDialog::setImageList(const DB::FileNameList &fileList) 0063 { 0064 m_fileList = fileList; 0065 } 0066 0067 void MainWindow::RunDialog::slotMarkGo() 0068 { 0069 QString cmdString = m_cmd->text(); 0070 // xgettext: no-c-format 0071 QRegExp replaceall = QRegExp(i18nc("As in 'Execute a command and replace any occurrence of %all with the filenames of all selected files'", "%all")); 0072 // xgettext: no-c-format 0073 QRegExp replaceeach = QRegExp(i18nc("As in 'Execute a command for each selected file in turn and replace any occurrence of %each with the filename ", "%each")); 0074 0075 // Replace the %all argument first 0076 QStringList fileList; 0077 for (const DB::FileName &fileName : qAsConst(m_fileList)) 0078 fileList.append(fileName.absolute()); 0079 0080 cmdString.replace(replaceall, KShell::joinArgs(fileList)); 0081 0082 if (cmdString.contains(replaceeach)) { 0083 // cmdString should be run multiple times, once per "each" 0084 QString cmdOnce; 0085 for (const DB::FileName &filename : qAsConst(m_fileList)) { 0086 cmdOnce = cmdString; 0087 cmdOnce.replace(replaceeach, filename.absolute()); 0088 auto *uiParent = MainWindow::Window::theMainWindow(); 0089 #if KIO_VERSION <= QT_VERSION_CHECK(5, 69, 0) 0090 KRun::runCommand(cmdOnce, uiParent); 0091 #else 0092 KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(cmdOnce); 0093 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, uiParent)); 0094 job->start(); 0095 #endif 0096 } 0097 } else { 0098 auto *uiParent = MainWindow::Window::theMainWindow(); 0099 #if KIO_VERSION <= QT_VERSION_CHECK(5, 69, 0) 0100 KRun::runCommand(cmdString, uiParent); 0101 #else 0102 KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(cmdString); 0103 job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, uiParent)); 0104 job->start(); 0105 #endif 0106 } 0107 } 0108 0109 void MainWindow::RunDialog::show() 0110 { 0111 QDialog::show(); 0112 } 0113 0114 // vi:expandtab:tabstop=4 shiftwidth=4: 0115 0116 #include "moc_RunDialog.cpp"