File indexing completed on 2024-05-12 05:48:39

0001 /*
0002     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
0003     SPDX-FileCopyrightText: 2015 Vyacheslav Matyushin
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "journaldConfigurationWidget.h"
0009 #include "globals.h"
0010 #include "journaldAddressDialog.h"
0011 #include "journaldConfiguration.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QCheckBox>
0016 
0017 JournaldConfigurationWidget::JournaldConfigurationWidget()
0018     : LogModeConfigurationWidget(i18n("Journald Log"), QLatin1String(JOURNALD_MODE_ICON), i18n("Journald Log"))
0019 {
0020     setupUi(this);
0021 
0022     remoteJournalsListWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
0023     connect(remoteJournalsListWidget, &QTableWidget::itemSelectionChanged, this, &JournaldConfigurationWidget::updateButtons);
0024 
0025     connect(lastBootOnly, &QCheckBox::stateChanged, this, &JournaldConfigurationWidget::configurationChanged);
0026     connect(entriesTypeComboBox, &QComboBox::currentIndexChanged, this, &JournaldConfigurationWidget::configurationChanged);
0027 
0028     connect(addAddressButton, &QPushButton::clicked, this, &JournaldConfigurationWidget::addRemoteJournal);
0029     connect(modifyAddressButton, &QPushButton::clicked, this, &JournaldConfigurationWidget::modifyRemoteJournal);
0030     connect(removeAddressButton, &QPushButton::clicked, this, &JournaldConfigurationWidget::removeRemoteJournal);
0031     connect(remoteJournalsListWidget, &QTableWidget::cellDoubleClicked, this, &JournaldConfigurationWidget::tableItemClicked);
0032 }
0033 
0034 void JournaldConfigurationWidget::saveConfig()
0035 {
0036     auto *configuration = Globals::instance().findLogMode(QLatin1String(JOURNALD_LOG_MODE_ID))->logModeConfiguration<JournaldConfiguration *>();
0037 
0038     configuration->setDisplayCurrentBootOnly(lastBootOnly->isChecked());
0039     configuration->setEntriesType((JournaldConfiguration::EntriesType)entriesTypeComboBox->currentIndex());
0040 
0041     QVector<JournalAddress> remoteJournals;
0042     for (int row = 0; row < remoteJournalsListWidget->rowCount(); row++) {
0043         QTableWidgetItem *addressItem = remoteJournalsListWidget->item(row, 0);
0044         QTableWidgetItem *portItem = remoteJournalsListWidget->item(row, 1);
0045         QTableWidgetItem *httpsItem = remoteJournalsListWidget->item(row, 2);
0046         JournalAddress addressInfo;
0047         addressInfo.address = addressItem->text();
0048         addressInfo.port = portItem->text().toUInt();
0049         Qt::CheckState const httpsCheckState = httpsItem->checkState();
0050         addressInfo.https = (httpsCheckState == Qt::Checked);
0051         remoteJournals.append(addressInfo);
0052     }
0053     configuration->setRemoteJournals(remoteJournals);
0054 
0055     Q_EMIT configSaved();
0056 }
0057 
0058 void JournaldConfigurationWidget::readConfig()
0059 {
0060     auto *configuration = Globals::instance().findLogMode(QLatin1String(JOURNALD_LOG_MODE_ID))->logModeConfiguration<JournaldConfiguration *>();
0061 
0062     lastBootOnly->setChecked(configuration->displayCurrentBootOnly());
0063     entriesTypeComboBox->setCurrentIndex(configuration->entriesType());
0064 
0065     remoteJournalsListWidget->clearContents();
0066     while (remoteJournalsListWidget->rowCount() > 0) {
0067         remoteJournalsListWidget->removeRow(0);
0068     }
0069 
0070     const QVector<JournalAddress> remoteJournals = configuration->remoteJournals();
0071     for (const JournalAddress &addressInfo : remoteJournals) {
0072         if (haveJournalAddress(addressInfo.address, QString::number(addressInfo.port), addressInfo.https)) {
0073             continue;
0074         }
0075         remoteJournalsListWidget->insertRow(remoteJournalsListWidget->rowCount());
0076         remoteJournalsListWidget->setItem(remoteJournalsListWidget->rowCount() - 1, 0, new QTableWidgetItem(addressInfo.address));
0077         remoteJournalsListWidget->setItem(remoteJournalsListWidget->rowCount() - 1, 1, new QTableWidgetItem(QString::number(addressInfo.port)));
0078         auto item = new QTableWidgetItem(i18n("Enabled"));
0079         item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
0080         item->setCheckState(addressInfo.https ? Qt::Checked : Qt::Unchecked);
0081         remoteJournalsListWidget->setItem(remoteJournalsListWidget->rowCount() - 1, 2, item);
0082     }
0083 }
0084 
0085 void JournaldConfigurationWidget::defaultConfig()
0086 {
0087     readConfig();
0088 }
0089 
0090 void JournaldConfigurationWidget::updateButtons()
0091 {
0092     const auto selectedItems = remoteJournalsListWidget->selectedItems();
0093     const bool haveItems = !selectedItems.empty();
0094     modifyAddressButton->setEnabled(haveItems);
0095     removeAddressButton->setEnabled(haveItems);
0096 }
0097 
0098 void JournaldConfigurationWidget::addRemoteJournal()
0099 {
0100     JournaldAddressDialog dialog(this, i18n("Add remote journal"));
0101     if (dialog.exec() == QDialog::Accepted) {
0102         const QString address = dialog.address();
0103         const QString port = dialog.port();
0104         const bool httpsEnabled = dialog.httpsEnabled();
0105 
0106         if (!haveJournalAddress(address, port, httpsEnabled)) {
0107             remoteJournalsListWidget->insertRow(remoteJournalsListWidget->rowCount());
0108             remoteJournalsListWidget->setItem(remoteJournalsListWidget->rowCount() - 1, 0, new QTableWidgetItem(address));
0109             remoteJournalsListWidget->setItem(remoteJournalsListWidget->rowCount() - 1, 1, new QTableWidgetItem(port));
0110             auto item = new QTableWidgetItem(i18n("Enabled"));
0111             item->setFlags(item->flags() | Qt::ItemIsUserCheckable);
0112             item->setCheckState(dialog.httpsEnabled() ? Qt::Checked : Qt::Unchecked);
0113             remoteJournalsListWidget->setItem(remoteJournalsListWidget->rowCount() - 1, 2, item);
0114         }
0115     }
0116 }
0117 
0118 void JournaldConfigurationWidget::modifyRemoteJournal()
0119 {
0120     tableItemClicked(remoteJournalsListWidget->currentRow());
0121 }
0122 
0123 void JournaldConfigurationWidget::removeRemoteJournal()
0124 {
0125     remoteJournalsListWidget->removeRow(remoteJournalsListWidget->currentRow());
0126 }
0127 
0128 void JournaldConfigurationWidget::tableItemClicked(int row)
0129 {
0130     QTableWidgetItem *addressItem = remoteJournalsListWidget->item(row, 0);
0131     QTableWidgetItem *portItem = remoteJournalsListWidget->item(row, 1);
0132     QTableWidgetItem *httpsItem = remoteJournalsListWidget->item(row, 2);
0133     bool const httpsEnabled = (Qt::Checked == httpsItem->checkState());
0134     JournaldAddressDialog dialog(this, i18n("Modify remote journal"), addressItem->text(), portItem->text(), httpsEnabled);
0135     if (dialog.exec() == QDialog::Accepted) {
0136         QString const address = dialog.address();
0137         QString const port = dialog.port();
0138         bool const newHttpsEnabled = dialog.httpsEnabled();
0139         if (!haveJournalAddress(address, port, newHttpsEnabled)) {
0140             addressItem->setText(address);
0141             portItem->setText(port);
0142             httpsItem->setCheckState(newHttpsEnabled ? Qt::Checked : Qt::Unchecked);
0143         }
0144     }
0145 }
0146 
0147 bool JournaldConfigurationWidget::haveJournalAddress(const QString &address, const QString &port, bool httpsEnabled) const
0148 {
0149     for (int row = 0; row < remoteJournalsListWidget->rowCount(); row++) {
0150         QTableWidgetItem *addressItem = remoteJournalsListWidget->item(row, 0);
0151         QTableWidgetItem *portItem = remoteJournalsListWidget->item(row, 1);
0152         QTableWidgetItem *httpsItem = remoteJournalsListWidget->item(row, 2);
0153         bool const https = (Qt::Checked == httpsItem->checkState());
0154         if ((addressItem->text() == address) && (portItem->text() == port) && (https == httpsEnabled)) {
0155             return true;
0156         }
0157     }
0158     return false;
0159 }
0160 
0161 #include "moc_journaldConfigurationWidget.cpp"