File indexing completed on 2025-01-19 04:46:53

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "openurlwithconfigurecreatewidget.h"
0008 #include <KLineEdit>
0009 #include <KLineEditEventHandler>
0010 #include <KLocalizedString>
0011 #include <KUrlRequester>
0012 #include <QFormLayout>
0013 #include <QLabel>
0014 #include <QLineEdit>
0015 #include <QWhatsThis>
0016 OpenUrlWithConfigureCreateWidget::OpenUrlWithConfigureCreateWidget(QWidget *parent)
0017     : QWidget{parent}
0018     , mServerName(new QLineEdit(this))
0019     , mCommandLine(new QLineEdit(this))
0020     , mExecutable(new KUrlRequester(this))
0021 {
0022     auto mainLayout = new QFormLayout(this);
0023     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0024     mainLayout->setContentsMargins(QMargins());
0025 
0026     mServerName->setObjectName(QLatin1StringView("mServerName"));
0027     mainLayout->addRow(i18n("Server Name:"), mServerName);
0028     mServerName->setClearButtonEnabled(true);
0029     KLineEditEventHandler::catchReturnKey(mServerName);
0030 
0031     auto formatHelp = new QLabel(i18n("<qt><a href=\"whatsthis1\">Argument format information...</a></qt>"), this);
0032     formatHelp->setObjectName(QLatin1StringView("formatHelp"));
0033     formatHelp->setContextMenuPolicy(Qt::NoContextMenu);
0034     connect(formatHelp, &QLabel::linkActivated, this, &OpenUrlWithConfigureCreateWidget::slotLinkClicked);
0035 
0036     mCommandWhatsThis = i18n(
0037         "<qt><p><strong>These expressions may be used for the arguments:"
0038         "</strong></p>"
0039         "<ul>"
0040         "<li>%u - url used by command</li>"
0041         "</ul>"
0042         "</strong></p></qt>");
0043     formatHelp->setWhatsThis(mCommandWhatsThis);
0044     mainLayout->addWidget(formatHelp);
0045 
0046     mCommandLine->setObjectName(QLatin1StringView("mCommandLine"));
0047     mCommandLine->setClearButtonEnabled(true);
0048     mainLayout->addRow(i18n("Command line:"), mCommandLine);
0049     KLineEditEventHandler::catchReturnKey(mCommandLine);
0050 
0051     mExecutable->setObjectName(QLatin1StringView("mEditorRequester"));
0052 
0053     mExecutable->setMimeTypeFilters(
0054         {QStringLiteral("application/x-executable"), QStringLiteral("application/x-shellscript"), QStringLiteral("application/x-desktop")});
0055 
0056     mExecutable->setMode(KFile::File | KFile::ExistingOnly | KFile::LocalOnly);
0057     mExecutable->lineEdit()->setClearButtonEnabled(true);
0058     mainLayout->addRow(i18n("Executable:"), mExecutable);
0059     connect(mServerName, &QLineEdit::textChanged, this, &OpenUrlWithConfigureCreateWidget::slotInfoChanged);
0060     connect(mExecutable->lineEdit(), &QLineEdit::textChanged, this, &OpenUrlWithConfigureCreateWidget::slotInfoChanged);
0061 }
0062 
0063 OpenUrlWithConfigureCreateWidget::~OpenUrlWithConfigureCreateWidget() = default;
0064 
0065 void OpenUrlWithConfigureCreateWidget::slotLinkClicked(const QString &link)
0066 {
0067     if (link == QLatin1StringView("whatsthis1")) {
0068         QWhatsThis::showText(QCursor::pos(), mCommandWhatsThis);
0069     }
0070 }
0071 
0072 OpenUrlWithConfigureCreateWidget::OpenUrlWithInfo OpenUrlWithConfigureCreateWidget::info() const
0073 {
0074     OpenUrlWithConfigureCreateWidget::OpenUrlWithInfo info;
0075     info.url = mServerName->text().trimmed();
0076     info.command = mExecutable->text().trimmed();
0077     info.commandLines = mCommandLine->text().trimmed();
0078     return info;
0079 }
0080 
0081 void OpenUrlWithConfigureCreateWidget::setInfo(const OpenUrlWithInfo &i)
0082 {
0083     mServerName->setText(i.url);
0084     mExecutable->setText(i.command);
0085     mCommandLine->setText(i.commandLines);
0086 }
0087 
0088 bool OpenUrlWithConfigureCreateWidget::OpenUrlWithInfo::isValid() const
0089 {
0090     return !url.trimmed().isEmpty() && !command.trimmed().isEmpty();
0091 }
0092 
0093 void OpenUrlWithConfigureCreateWidget::slotInfoChanged()
0094 {
0095     Q_EMIT updateOkButton(!mServerName->text().trimmed().isEmpty() && !mExecutable->text().trimmed().isEmpty());
0096 }
0097 
0098 #include "moc_openurlwithconfigurecreatewidget.cpp"