File indexing completed on 2024-04-21 05:45:51
0001 /* 0002 KT environment variable editor window implementation 0003 -------------------------------------------------------------------- 0004 SPDX-FileCopyrightText: 1999 Gary Meyer <gary@meyer.net> 0005 -------------------------------------------------------------------- 0006 SPDX-License-Identifier: GPL-2.0-or-later 0007 */ 0008 0009 #include "variableEditorDialog.h" 0010 #include <QGridLayout> 0011 #include <QPushButton> 0012 0013 #include <KLocalizedString> 0014 #include <QDialogButtonBox> 0015 #include <QIcon> 0016 0017 #include "ctvariable.h" 0018 0019 #include "crontabWidget.h" 0020 0021 #include "kcronHelper.h" 0022 0023 VariableEditorDialog::VariableEditorDialog(CTVariable *_ctVariable, const QString &_caption, CrontabWidget *_crontabWidget) 0024 : QDialog(_crontabWidget) 0025 { 0026 mCtVariable = _ctVariable; 0027 mCrontabWidget = _crontabWidget; 0028 0029 setModal(true); 0030 setWindowTitle(_caption); 0031 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0032 mOkButton = buttonBox->button(QDialogButtonBox::Ok); 0033 0034 auto layout = new QGridLayout; 0035 setLayout(layout); 0036 0037 layout->setColumnMinimumWidth(1, 270); 0038 layout->setRowStretch(3, 1); 0039 layout->setColumnStretch(1, 1); 0040 0041 setWindowIcon(QIcon::fromTheme(QStringLiteral("kcron"))); 0042 0043 int layoutPosition = 0; 0044 0045 // top title widget 0046 mTitleWidget = new KTitleWidget(this); 0047 mTitleWidget->setText(i18n("Add or modify a variable")); 0048 layout->addWidget(mTitleWidget, layoutPosition, 0, 1, 2); 0049 0050 // variable 0051 auto labVariable = new QLabel(i18nc("The environmental variable name ie HOME, MAILTO etc", "&Variable:"), this); 0052 layout->addWidget(labVariable, ++layoutPosition, 0, Qt::AlignLeft); 0053 0054 mCmbVariable = new QComboBox(this); 0055 mCmbVariable->setEditable(true); 0056 layout->addWidget(mCmbVariable, layoutPosition, 1); 0057 0058 mCmbVariable->addItem(QStringLiteral("HOME")); 0059 mCmbVariable->addItem(QStringLiteral("MAILTO")); 0060 mCmbVariable->addItem(QStringLiteral("PATH")); 0061 mCmbVariable->addItem(QStringLiteral("SHELL")); 0062 mCmbVariable->addItem(QStringLiteral("LD_CONFIG_PATH")); 0063 0064 labVariable->setBuddy(mCmbVariable); 0065 0066 // details 0067 auto labDetails = new QLabel(QLatin1String(""), this); 0068 layout->addWidget(labDetails, ++layoutPosition, 0, Qt::AlignLeft); 0069 0070 auto detailsLayout = new QHBoxLayout; 0071 mDetailsIcon = new QLabel(this); 0072 detailsLayout->addWidget(mDetailsIcon); 0073 0074 mDetails = new QLabel(this); 0075 detailsLayout->addWidget(mDetails); 0076 0077 layout->addLayout(detailsLayout, layoutPosition, 1, Qt::AlignLeft); 0078 0079 // value 0080 auto labValue = new QLabel(i18n("Va&lue:"), this); 0081 layout->addWidget(labValue, ++layoutPosition, 0, Qt::AlignLeft); 0082 0083 mLeValue = new QLineEdit(this); 0084 layout->addWidget(mLeValue, layoutPosition, 1); 0085 mLeValue->setMaxLength(255); 0086 labValue->setBuddy(mLeValue); 0087 0088 // User Combo 0089 auto userLabel = new QLabel(i18n("&Run as:"), this); 0090 layout->addWidget(userLabel, ++layoutPosition, 0); 0091 0092 mUserCombo = new QComboBox(this); 0093 0094 userLabel->setBuddy(mUserCombo); 0095 layout->addWidget(mUserCombo, layoutPosition, 1); 0096 0097 if (mCrontabWidget->variablesWidget()->needUserColumn()) { 0098 KCronHelper::initUserCombo(mUserCombo, mCrontabWidget, mCtVariable->userLogin); 0099 } else { 0100 userLabel->hide(); 0101 mUserCombo->hide(); 0102 } 0103 0104 // comment 0105 auto labComment = new QLabel(i18n("Co&mment:"), this); 0106 layout->addWidget(labComment, ++layoutPosition, 0, Qt::AlignLeft); 0107 0108 mTeComment = KCronHelper::createCommentEdit(this); 0109 layout->addWidget(mTeComment, layoutPosition, 1); 0110 labComment->setBuddy(mTeComment); 0111 0112 // enabled 0113 mChkEnabled = new QCheckBox(i18n("&Enable this variable"), this); 0114 layout->addWidget(mChkEnabled, ++layoutPosition, 0, 1, 2); 0115 0116 layout->addWidget(buttonBox, ++layoutPosition, 0, 1, 2); 0117 0118 // set starting field values 0119 mCmbVariable->setEditText(mCtVariable->variable); 0120 mLeValue->setText(mCtVariable->value); 0121 mTeComment->setText(mCtVariable->comment); 0122 mChkEnabled->setChecked(mCtVariable->enabled); 0123 mCmbVariable->setFocus(); 0124 0125 slotEnabled(); 0126 slotWizard(); 0127 show(); 0128 0129 // connect them up 0130 connect(mCmbVariable, &QComboBox::editTextChanged, this, &VariableEditorDialog::slotWizard); 0131 connect(mLeValue, &QLineEdit::textEdited, this, &VariableEditorDialog::slotWizard); 0132 connect(buttonBox, &QDialogButtonBox::accepted, this, &VariableEditorDialog::slotOk); 0133 connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); 0134 connect(mChkEnabled, &QCheckBox::clicked, this, &VariableEditorDialog::slotEnabled); 0135 } 0136 0137 VariableEditorDialog::~VariableEditorDialog() 0138 { 0139 } 0140 0141 void VariableEditorDialog::setupTitleWidget(const QString &comment, KTitleWidget::MessageType messageType) 0142 { 0143 // krazy:exclude=doublequote_chars 0144 if (comment.isEmpty()) { 0145 mTitleWidget->setComment(i18n("<i>This variable will be used by scheduled tasks.</i>")); 0146 mTitleWidget->setIcon(QIcon::fromTheme(QStringLiteral("text-plain")), KTitleWidget::ImageRight); 0147 } else { 0148 mTitleWidget->setComment(comment, messageType); 0149 if (messageType == KTitleWidget::ErrorMessage) { 0150 mTitleWidget->setIcon(QIcon::fromTheme(QStringLiteral("dialog-error")), KTitleWidget::ImageRight); 0151 } else { 0152 mTitleWidget->setIcon(QIcon::fromTheme(QStringLiteral("dialog-information")), KTitleWidget::ImageRight); 0153 } 0154 } 0155 } 0156 0157 void VariableEditorDialog::slotEnabled() 0158 { 0159 bool enabled = mChkEnabled->isChecked(); 0160 mCmbVariable->setEnabled(enabled); 0161 mLeValue->setEnabled(enabled); 0162 mTeComment->setEnabled(enabled); 0163 mUserCombo->setEnabled(enabled); 0164 } 0165 0166 void VariableEditorDialog::slotOk() 0167 { 0168 mCtVariable->variable = mCmbVariable->currentText(); 0169 mCtVariable->value = mLeValue->text(); 0170 mCtVariable->comment = mTeComment->toPlainText(); 0171 mCtVariable->enabled = mChkEnabled->isChecked(); 0172 0173 // save work in process 0174 if (mCrontabWidget->variablesWidget()->needUserColumn()) { 0175 mCtVariable->userLogin = mUserCombo->currentText(); 0176 } 0177 0178 accept(); 0179 } 0180 0181 void VariableEditorDialog::slotWizard() 0182 { 0183 CTVariable tempVariable(*mCtVariable); 0184 tempVariable.variable = mCmbVariable->currentText(); 0185 0186 mDetailsIcon->setPixmap(tempVariable.variableIcon().pixmap(style()->pixelMetric(QStyle::PM_SmallIconSize, nullptr, this))); 0187 mDetails->setText(tempVariable.information()); 0188 0189 bool error = false; 0190 0191 if (!mChkEnabled->isChecked()) { 0192 setupTitleWidget(i18n("<i>This variable is disabled.</i>")); 0193 mChkEnabled->setFocus(); 0194 mOkButton->setEnabled(true); 0195 error = true; 0196 } 0197 0198 if (mCmbVariable->currentText().isEmpty() && !error) { 0199 setupTitleWidget(i18n("<i>Please enter the variable name...</i>"), KTitleWidget::ErrorMessage); 0200 mCmbVariable->setFocus(); 0201 mOkButton->setEnabled(false); 0202 error = true; 0203 } 0204 0205 if (mLeValue->text().isEmpty() && !error) { 0206 setupTitleWidget(i18n("<i>Please enter the variable value ...</i>"), KTitleWidget::ErrorMessage); 0207 mOkButton->setEnabled(false); 0208 error = true; 0209 } 0210 0211 if (!error) { 0212 setupTitleWidget(); 0213 mOkButton->setEnabled(true); 0214 } 0215 } 0216 0217 #include "moc_variableEditorDialog.cpp"