// SPDX-FileCopyrightText: 2009-2020 Wes Hardaker <kpa@capturedonearth.com>
// SPDX-FileCopyrightText: 2022 Johannes Zarl-Zierl <johannes@zarl-zierl.at>
// SPDX-FileCopyrightText: 2024 Tobias Leupold <tl@stonemx.de>
//
// SPDX-License-Identifier: GPL-2.0-or-later

#include "RunDialog.h"

#include "Window.h"

#include <KIO/CommandLauncherJob>
#include <KIO/JobUiDelegate>
#include <KLocalizedString>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>
#include <kio_version.h>
#include <kshell.h>

#include <utility>

MainWindow::RunDialog::RunDialog(QWidget *parent)
    : QDialog(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout;
    setLayout(mainLayout);

    // xgettext: no-c-format
    QString txt = i18n("<p>Enter your command to run below:</p>"
                       "<p><i>%all will be replaced with a file list</i></p>");
    QLabel *label = new QLabel(txt);
    mainLayout->addWidget(label);

    m_cmd = new QLineEdit();
    mainLayout->addWidget(m_cmd);
    m_cmd->setMinimumWidth(400);
    // xgettext: no-c-format
    txt = i18n("<p>Enter the command you want to run on your image file(s). "
               "KPhotoAlbum will run your command and replace any '%all' tokens "
               "with a list of your files. For example, if you entered:</p>"
               "<ul><li>cp %all /tmp</li></ul>"
               "<p>Then the files you selected would be copied to the /tmp "
               "folder</p>"
               "<p>You can also use %each to have a command be run once per "
               "file.</p>");
    m_cmd->setWhatsThis(txt);
    label->setWhatsThis(txt);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return);
    mainLayout->addWidget(buttonBox);

    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
    connect(this, &QDialog::accepted, this, &RunDialog::slotMarkGo);
}

void MainWindow::RunDialog::setImageList(const DB::FileNameList &fileList)
{
    m_fileList = fileList;
}

void MainWindow::RunDialog::slotMarkGo()
{
    QString cmdString = m_cmd->text();

    // FIXME: KF6 port: Neither replaceall nor replaceeach are actually regular expressions, neither
    // do we need regular expressions for the simple substitution below. So I suppose we can simply
    // use QStrings here?!

    // xgettext: no-c-format
    const QString replaceall = i18nc("As in 'Execute a command and replace any occurrence of %all with the filenames of all selected files'", "%all");
    // xgettext: no-c-format
    const QString replaceeach = i18nc("As in 'Execute a command for each selected file in turn and replace any occurrence of %each with the filename ", "%each");

    // Replace the %all argument first
    QStringList fileList;
    for (const DB::FileName &fileName : std::as_const(m_fileList))
        fileList.append(fileName.absolute());

    cmdString.replace(replaceall, KShell::joinArgs(fileList));

    if (cmdString.contains(replaceeach)) {
        // cmdString should be run multiple times, once per "each"
        QString cmdOnce;
        for (const DB::FileName &filename : std::as_const(m_fileList)) {
            cmdOnce = cmdString;
            cmdOnce.replace(replaceeach, filename.absolute());
            auto *uiParent = MainWindow::Window::theMainWindow();
            KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(cmdOnce);
            job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, uiParent));
            job->start();
        }
    } else {
        auto *uiParent = MainWindow::Window::theMainWindow();
        KIO::CommandLauncherJob *job = new KIO::CommandLauncherJob(cmdString);
        job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, uiParent));
        job->start();
    }
}

void MainWindow::RunDialog::show()
{
    QDialog::show();
}

// vi:expandtab:tabstop=4 shiftwidth=4:

#include "moc_RunDialog.cpp"
