File indexing completed on 2024-04-21 05:45:51

0001 /*
0002     KT list view item tasks 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 "variablesWidget.h"
0010 
0011 #include <QAction>
0012 #include <QList>
0013 
0014 #include <KLocalizedString>
0015 #include <QIcon>
0016 
0017 #include "ctcron.h"
0018 #include "ctvariable.h"
0019 
0020 #include "crontabWidget.h"
0021 #include "variableEditorDialog.h"
0022 #include "variableWidget.h"
0023 
0024 #include "kcm_cron_debug.h"
0025 /**
0026  * Construct tasks folder from branch.
0027  */
0028 VariablesWidget::VariablesWidget(CrontabWidget *crontabWidget)
0029     : GenericListWidget(crontabWidget, i18n("<b>Environment Variables</b>"), QIcon::fromTheme(QStringLiteral("text-plain")))
0030 {
0031     refreshHeaders();
0032 
0033     treeWidget()->sortItems(0, Qt::AscendingOrder);
0034 
0035     setupActions();
0036     prepareContextualMenu();
0037 
0038     connect(treeWidget(), &QTreeWidget::itemSelectionChanged, this, &VariablesWidget::changeCurrentSelection);
0039 
0040     qCDebug(KCM_CRON_LOG) << "Variables list created";
0041 }
0042 
0043 VariablesWidget::~VariablesWidget()
0044 {
0045 }
0046 
0047 void VariablesWidget::modifySelection()
0048 {
0049     modifySelection(firstSelectedVariableWidget(), -1);
0050 }
0051 
0052 void VariablesWidget::modifySelection(QTreeWidgetItem *item, int position)
0053 {
0054     auto variableWidget = static_cast<VariableWidget *>(item);
0055 
0056     if (variableWidget) {
0057         if (position == statusColumnIndex()) {
0058             variableWidget->toggleEnable();
0059             Q_EMIT variableModified(true);
0060         } else {
0061             CTVariable *variable = variableWidget->getCTVariable();
0062             VariableEditorDialog variableEditorDialog(variable, i18n("Modify Variable"), crontabWidget());
0063             int result = variableEditorDialog.exec();
0064 
0065             if (result == QDialog::Accepted) {
0066                 crontabWidget()->currentCron()->modifyVariable(variable);
0067                 variableWidget->refresh();
0068 
0069                 Q_EMIT variableModified(true);
0070             }
0071         }
0072     }
0073 }
0074 
0075 QList<VariableWidget *> VariablesWidget::selectedVariablesWidget() const
0076 {
0077     QList<VariableWidget *> variablesWidget;
0078 
0079     const QList<QTreeWidgetItem *> variablesItems = treeWidget()->selectedItems();
0080     variablesWidget.reserve(variablesItems.count());
0081     for (QTreeWidgetItem *item : variablesItems) {
0082         auto variableWidget = static_cast<VariableWidget *>(item);
0083         variablesWidget.append(variableWidget);
0084     }
0085 
0086     return variablesWidget;
0087 }
0088 
0089 VariableWidget *VariablesWidget::firstSelectedVariableWidget() const
0090 {
0091     QTreeWidgetItem *item = firstSelected();
0092     if (!item) {
0093         return nullptr;
0094     }
0095 
0096     return static_cast<VariableWidget *>(item);
0097 }
0098 
0099 void VariablesWidget::deleteSelection()
0100 {
0101     const QList<QTreeWidgetItem *> variablesItems = treeWidget()->selectedItems();
0102     bool deleteSomething = !(variablesItems.isEmpty());
0103 
0104     for (QTreeWidgetItem *item : variablesItems) {
0105         auto variableWidget = static_cast<VariableWidget *>(item);
0106 
0107         crontabWidget()->currentCron()->removeVariable(variableWidget->getCTVariable());
0108         delete variableWidget->getCTVariable();
0109         treeWidget()->takeTopLevelItem(treeWidget()->indexOfTopLevelItem(variableWidget));
0110         delete variableWidget;
0111     }
0112 
0113     if (deleteSomething) {
0114         Q_EMIT variableModified(true);
0115         changeCurrentSelection();
0116     }
0117 }
0118 
0119 bool VariablesWidget::needUserColumn()
0120 {
0121     CTCron *currentCron = crontabWidget()->currentCron();
0122     if (currentCron->isMultiUserCron() && !currentCron->isSystemCron()) {
0123         return true;
0124     }
0125 
0126     return false;
0127 }
0128 
0129 int VariablesWidget::statusColumnIndex()
0130 {
0131     if (needUserColumn()) {
0132         return 3;
0133     }
0134 
0135     return 2;
0136 }
0137 
0138 void VariablesWidget::createVariable()
0139 {
0140     auto variable = new CTVariable(QLatin1String(""), QLatin1String(""), crontabWidget()->currentCron()->userLogin());
0141 
0142     VariableEditorDialog variableEditorDialog(variable, i18n("New Variable"), crontabWidget());
0143     int result = variableEditorDialog.exec();
0144 
0145     if (result == QDialog::Accepted) {
0146         addVariable(variable);
0147         Q_EMIT variableModified(true);
0148         changeCurrentSelection();
0149     } else {
0150         delete variable;
0151     }
0152 }
0153 
0154 void VariablesWidget::addVariable(CTVariable *variable)
0155 {
0156     qCDebug(KCM_CRON_LOG) << "Add a new variable";
0157     crontabWidget()->currentCron()->addVariable(variable);
0158     new VariableWidget(this, variable);
0159 
0160     changeCurrentSelection();
0161 }
0162 
0163 void VariablesWidget::refreshVariables(CTCron *cron)
0164 {
0165     // Remove previous items
0166     removeAll();
0167 
0168     refreshHeaders();
0169 
0170     const auto variables = cron->variables();
0171     for (CTVariable *ctVariable : variables) {
0172         new VariableWidget(this, ctVariable);
0173     }
0174 
0175     resizeColumnContents();
0176 }
0177 
0178 void VariablesWidget::refreshHeaders()
0179 {
0180     QStringList headerLabels;
0181 
0182     if (needUserColumn()) {
0183         headerLabels << i18n("User");
0184     }
0185 
0186     headerLabels << i18n("Variable");
0187     headerLabels << i18n("Value");
0188     headerLabels << i18n("Status");
0189     headerLabels << i18n("Comment");
0190 
0191     treeWidget()->setHeaderLabels(headerLabels);
0192 
0193     if (needUserColumn()) {
0194         treeWidget()->setColumnCount(5);
0195     } else {
0196         treeWidget()->setColumnCount(4);
0197     }
0198 }
0199 
0200 void VariablesWidget::setupActions()
0201 {
0202     mNewVariableAction = new QAction(this);
0203     mNewVariableAction->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
0204     mNewVariableAction->setText(i18nc("Adds a new variable", "New &Variable..."));
0205     mNewVariableAction->setToolTip(i18n("Create a new variable."));
0206     addRightAction(mNewVariableAction, this, SLOT(createVariable()));
0207 
0208     mModifyAction = new QAction(this);
0209     mModifyAction->setText(i18n("M&odify..."));
0210     mModifyAction->setIcon(QIcon::fromTheme(QStringLiteral("document-open")));
0211     mModifyAction->setToolTip(i18n("Modify the selected variable."));
0212     addRightAction(mModifyAction, this, SLOT(modifySelection()));
0213 
0214     mDeleteAction = new QAction(this);
0215     mDeleteAction->setText(i18n("&Delete"));
0216     mDeleteAction->setIcon(QIcon::fromTheme(QStringLiteral("edit-delete")));
0217     mDeleteAction->setToolTip(i18n("Delete the selected variable."));
0218     addRightAction(mDeleteAction, this, SLOT(deleteSelection()));
0219 
0220     addRightStretch();
0221 }
0222 
0223 void VariablesWidget::prepareContextualMenu()
0224 {
0225     treeWidget()->addAction(mNewVariableAction);
0226 
0227     treeWidget()->addAction(createSeparator());
0228 
0229     treeWidget()->addAction(mModifyAction);
0230     treeWidget()->addAction(mDeleteAction);
0231 
0232     treeWidget()->addAction(createSeparator());
0233 
0234     const auto cutCopyPasteActions = crontabWidget()->cutCopyPasteActions();
0235     for (QAction *action : cutCopyPasteActions) {
0236         treeWidget()->addAction(action);
0237     }
0238 }
0239 
0240 void VariablesWidget::toggleModificationActions(bool state)
0241 {
0242     setActionEnabled(mModifyAction, state);
0243     setActionEnabled(mDeleteAction, state);
0244 }
0245 
0246 void VariablesWidget::toggleNewEntryAction(bool state)
0247 {
0248     setActionEnabled(mNewVariableAction, state);
0249 }
0250 
0251 void VariablesWidget::changeCurrentSelection()
0252 {
0253     qCDebug(KCM_CRON_LOG) << "Change selection...";
0254 
0255     bool enabled;
0256     if (treeWidget()->selectedItems().isEmpty()) {
0257         enabled = false;
0258     } else {
0259         enabled = true;
0260     }
0261 
0262     toggleModificationActions(enabled);
0263 }
0264 
0265 #include "moc_variablesWidget.cpp"