File indexing completed on 2024-11-24 04:43:08

0001 /*
0002    SPDX-FileCopyrightText: 2017-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "externalcomposerconfigurewidget.h"
0008 #include <KConfigGroup>
0009 #include <KLineEdit>
0010 #include <KLineEditEventHandler>
0011 #include <KLocalizedString>
0012 #include <KSharedConfig>
0013 #include <KUrlRequester>
0014 #include <QCheckBox>
0015 #include <QLabel>
0016 #include <QVBoxLayout>
0017 namespace
0018 {
0019 static const char myExternalComposerGroupName[] = "External Composer";
0020 }
0021 ExternalComposerConfigureWidget::ExternalComposerConfigureWidget(QWidget *parent)
0022     : MessageComposer::PluginEditorConfigureBaseWidget(parent)
0023     , mExternalEditorCheck(new QCheckBox(i18n("Use external editor instead of composer"), this))
0024     , mEditorRequester(new KUrlRequester(this))
0025 {
0026     auto mainLayout = new QVBoxLayout(this);
0027     mainLayout->setContentsMargins({});
0028     mainLayout->setObjectName(QLatin1StringView("mainlayout"));
0029 
0030     mExternalEditorCheck->setObjectName(QLatin1StringView("enabled"));
0031     mExternalEditorCheck->setChecked(false);
0032     mainLayout->addWidget(mExternalEditorCheck);
0033 
0034     auto hbox = new QHBoxLayout;
0035     auto label = new QLabel(QStringLiteral("Specify editor:"), this);
0036     label->setObjectName(QLatin1StringView("urlrequesterlabel"));
0037     hbox->addWidget(label);
0038 
0039     mEditorRequester->setObjectName(QLatin1StringView("mEditorRequester"));
0040     hbox->addWidget(mEditorRequester);
0041 
0042     mEditorRequester->setMimeTypeFilters(
0043         {QStringLiteral("application/x-executable"), QStringLiteral("application/x-shellscript"), QStringLiteral("application/x-desktop")});
0044 
0045     mEditorRequester->setMode(KFile::File | KFile::ExistingOnly | KFile::LocalOnly);
0046     mEditorRequester->setEnabled(false);
0047     KLineEditEventHandler::catchReturnKey(mEditorRequester->lineEdit());
0048 
0049     mainLayout->addLayout(hbox);
0050 
0051     label = new QLabel(i18n("<b>%f</b> will be replaced with the "
0052                             "filename to edit.<br />"
0053                             "<b>%w</b> will be replaced with the window id.<br />"
0054                             "<b>%l</b> will be replaced with the line number."),
0055                        this);
0056     label->setObjectName(QLatin1StringView("explanationlabel"));
0057     label->setEnabled(false); // see above
0058     mainLayout->addWidget(label);
0059 
0060     mainLayout->addStretch(1);
0061 
0062     connect(mExternalEditorCheck, &QAbstractButton::toggled, label, &QWidget::setEnabled);
0063     connect(mExternalEditorCheck, &QAbstractButton::toggled, mEditorRequester, &QWidget::setEnabled);
0064 }
0065 
0066 ExternalComposerConfigureWidget::~ExternalComposerConfigureWidget() = default;
0067 
0068 void ExternalComposerConfigureWidget::loadSettings()
0069 {
0070     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0071     KConfigGroup group = config->group(QLatin1StringView(myExternalComposerGroupName));
0072     mExternalEditorCheck->setChecked(group.readEntry("Enabled", false));
0073     mEditorRequester->setText(group.readEntry("ComposerPath", QStringLiteral("kwrite %f")));
0074 }
0075 
0076 void ExternalComposerConfigureWidget::saveSettings()
0077 {
0078     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0079     KConfigGroup group = config->group(QLatin1StringView(myExternalComposerGroupName));
0080     group.writeEntry("Enabled", mExternalEditorCheck->isChecked());
0081     group.writeEntry("ComposerPath", mEditorRequester->text());
0082 }
0083 
0084 void ExternalComposerConfigureWidget::resetSettings()
0085 {
0086     mExternalEditorCheck->setChecked(false);
0087     mEditorRequester->setText(QStringLiteral("kwrite %f"));
0088 }
0089 
0090 #include "moc_externalcomposerconfigurewidget.cpp"