1 /*
2    SPDX-FileCopyrightText: 2016-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "viewerpluginexternaleditwidget.h"
8 #include <KLineEdit>
9 #include <KLocalizedString>
10 #include <KUrlRequester>
11 #include <Libkdepim/LineEditCatchReturnKey>
12 #include <QFormLayout>
13 #include <QLabel>
14 #include <QWhatsThis>
15 
ViewerPluginExternalEditWidget(QWidget * parent)16 ViewerPluginExternalEditWidget::ViewerPluginExternalEditWidget(QWidget *parent)
17     : QWidget(parent)
18     , mName(new QLineEdit(this))
19     , mDescription(new QLineEdit(this))
20     , mCommandLine(new QLineEdit(this))
21     , mExecutable(new KUrlRequester(this))
22 {
23     auto mainLayout = new QFormLayout(this);
24     mainLayout->setObjectName(QStringLiteral("mainlayout"));
25     mainLayout->setContentsMargins({});
26 
27     mName->setObjectName(QStringLiteral("name"));
28     mainLayout->addRow(i18n("Name:"), mName);
29     new KPIM::LineEditCatchReturnKey(mName, this);
30 
31     mDescription->setObjectName(QStringLiteral("description"));
32     mainLayout->addRow(i18n("Description:"), mDescription);
33     new KPIM::LineEditCatchReturnKey(mDescription, this);
34 
35     mCommandLine->setClearButtonEnabled(true);
36     mCommandLine->setObjectName(QStringLiteral("commandline"));
37     mCommandLine->setPlaceholderText(i18n("Add command arguments..."));
38     mainLayout->addRow(i18n("Command Line:"), mCommandLine);
39     new KPIM::LineEditCatchReturnKey(mCommandLine, this);
40 
41     auto formatHelp = new QLabel(i18n("<qt><a href=\"whatsthis1\">Argument format information...</a></qt>"), this);
42     formatHelp->setObjectName(QStringLiteral("formatHelp"));
43     formatHelp->setContextMenuPolicy(Qt::NoContextMenu);
44     connect(formatHelp, &QLabel::linkActivated, this, &ViewerPluginExternalEditWidget::slotLinkClicked);
45 
46     mCommandWhatsThis = i18n(
47         "<qt><p><strong>These expressions may be used for the arguments:"
48         "</strong></p>"
49         "<ul>"
50         "<li>%s - the subject of message</li>"
51         "<li>%from - the from email address</li>"
52         "<li>%cc - the list of cc email address</li>"
53         "<li>%bcc - the list of bcc email address</li>"
54         "<li>%body - the body of message</li>"
55         "<li>%inreplyto - the reply email address</li>"
56         "<li>%akonadiUrl - the akonadi url (as akonadi:?item=187020)</li>"
57         "</ul>"
58         "</strong></p></qt>");
59     formatHelp->setWhatsThis(mCommandWhatsThis);
60 
61     mainLayout->addWidget(formatHelp);
62 
63     mExecutable->setObjectName(QStringLiteral("mEditorRequester"));
64 
65     mExecutable->setMimeTypeFilters(
66         {QStringLiteral("application/x-executable"), QStringLiteral("application/x-shellscript"), QStringLiteral("application/x-desktop")});
67 
68     mExecutable->setMode(KFile::File | KFile::ExistingOnly | KFile::LocalOnly);
69     mExecutable->lineEdit()->setClearButtonEnabled(true);
70     mainLayout->addRow(i18n("Executable:"), mExecutable);
71     connect(mName, &QLineEdit::textChanged, this, &ViewerPluginExternalEditWidget::slotInfoChanged);
72     connect(mExecutable->lineEdit(), &QLineEdit::textChanged, this, &ViewerPluginExternalEditWidget::slotInfoChanged);
73 }
74 
~ViewerPluginExternalEditWidget()75 ViewerPluginExternalEditWidget::~ViewerPluginExternalEditWidget()
76 {
77 }
78 
slotLinkClicked(const QString & link)79 void ViewerPluginExternalEditWidget::slotLinkClicked(const QString &link)
80 {
81     if (link == QLatin1String("whatsthis1")) {
82         QWhatsThis::showText(QCursor::pos(), mCommandWhatsThis);
83     }
84 }
85 
slotInfoChanged()86 void ViewerPluginExternalEditWidget::slotInfoChanged()
87 {
88     Q_EMIT scriptIsValid(!mExecutable->text().trimmed().isEmpty() && !mName->text().trimmed().isEmpty() && !mExistingsNames.contains(mName->text()));
89 }
90 
setExistingsNames(const QStringList & existingsNames)91 void ViewerPluginExternalEditWidget::setExistingsNames(const QStringList &existingsNames)
92 {
93     mExistingsNames = existingsNames;
94 }
95 
setScriptInfo(const ViewerPluginExternalScriptInfo & info)96 void ViewerPluginExternalEditWidget::setScriptInfo(const ViewerPluginExternalScriptInfo &info)
97 {
98     mScriptInfo = info;
99     mName->setText(mScriptInfo.name());
100     mDescription->setText(mScriptInfo.description());
101     mCommandLine->setText(mScriptInfo.commandLine());
102     mExecutable->setText(mScriptInfo.executable());
103 }
104 
save()105 void ViewerPluginExternalEditWidget::save()
106 {
107     mScriptInfo.setName(mName->text());
108     mScriptInfo.setDescription(mDescription->text());
109     mScriptInfo.setCommandLine(mCommandLine->text());
110     mScriptInfo.setExecutable(mExecutable->text());
111 }
112 
scriptInfo() const113 ViewerPluginExternalScriptInfo ViewerPluginExternalEditWidget::scriptInfo() const
114 {
115     return mScriptInfo;
116 }
117