File indexing completed on 2024-05-12 16:27:12

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "channelinfoprunewidget.h"
0008 #include "retentioninfo.h"
0009 #include "room.h"
0010 #include <KLocalizedString>
0011 #include <QCheckBox>
0012 #include <QGroupBox>
0013 #include <QLabel>
0014 #include <QSpinBox>
0015 #include <QVBoxLayout>
0016 
0017 ChannelInfoPruneWidget::ChannelInfoPruneWidget(QWidget *parent)
0018     : QWidget(parent)
0019     , mExcludePinnedMessages(new QCheckBox(i18n("Exclude pinned messages"), this))
0020     , mPruneFileOnlyKeepMessages(new QCheckBox(i18n("Prune files only, keep messages"), this))
0021     , mAutomaticPruneOldMessages(new QCheckBox(i18n("Automatically prune old messages"), this))
0022     , mOverrideGlobalRetentionPolicy(new QCheckBox(i18n("Override global retention policy"), this))
0023     , mMaximumAgeInDay(new QSpinBox(this))
0024 {
0025     auto mainLayout = new QVBoxLayout(this);
0026     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0027     mainLayout->setContentsMargins({});
0028     auto groupBox = new QGroupBox(i18n("Prune"), this);
0029     groupBox->setObjectName(QStringLiteral("groupBox"));
0030     mainLayout->addWidget(groupBox);
0031 
0032     auto groupBoxLayout = new QVBoxLayout(groupBox);
0033 
0034     mAutomaticPruneOldMessages->setObjectName(QStringLiteral("mAutomaticPruneOldMessages"));
0035     groupBoxLayout->addWidget(mAutomaticPruneOldMessages);
0036     mOverrideGlobalRetentionPolicy->setObjectName(QStringLiteral("mOverrideGlobalRetentionPolicy"));
0037     groupBoxLayout->addWidget(mOverrideGlobalRetentionPolicy);
0038     connect(mOverrideGlobalRetentionPolicy, &QCheckBox::clicked, this, &ChannelInfoPruneWidget::setOverrideGlobalSettings);
0039 
0040     mExcludePinnedMessages->setObjectName(QStringLiteral("mExcludePinnedMessages"));
0041     groupBoxLayout->addWidget(mExcludePinnedMessages);
0042     mPruneFileOnlyKeepMessages->setObjectName(QStringLiteral("mPruneFileOnlyKeepMessages"));
0043     groupBoxLayout->addWidget(mPruneFileOnlyKeepMessages);
0044 
0045     auto maxAgeLayout = new QHBoxLayout;
0046     maxAgeLayout->setObjectName(QStringLiteral("maxAgeLayout"));
0047     maxAgeLayout->setContentsMargins({});
0048     groupBoxLayout->addLayout(maxAgeLayout);
0049 
0050     auto label = new QLabel(i18n("Maximum message age in days (default: 30):"), this);
0051     label->setObjectName(QStringLiteral("label"));
0052     mMaximumAgeInDay->setObjectName(QStringLiteral("mMaximumAgeInDay"));
0053     maxAgeLayout->addWidget(label);
0054     maxAgeLayout->addWidget(mMaximumAgeInDay);
0055     mMaximumAgeInDay->setValue(30);
0056     mMaximumAgeInDay->setMinimum(1);
0057     mMaximumAgeInDay->setMaximum(99999);
0058 }
0059 
0060 ChannelInfoPruneWidget::~ChannelInfoPruneWidget() = default;
0061 
0062 void ChannelInfoPruneWidget::setOverrideGlobalSettings(bool override)
0063 {
0064     mExcludePinnedMessages->setEnabled(override);
0065     mPruneFileOnlyKeepMessages->setEnabled(override);
0066     mMaximumAgeInDay->setEnabled(override);
0067 }
0068 
0069 void ChannelInfoPruneWidget::setRetentionInfo(RetentionInfo retentionInfo)
0070 {
0071     mExcludePinnedMessages->setChecked(retentionInfo.excludePinned());
0072     mPruneFileOnlyKeepMessages->setChecked(retentionInfo.filesOnly());
0073     mAutomaticPruneOldMessages->setChecked(retentionInfo.enabled());
0074     mOverrideGlobalRetentionPolicy->setChecked(retentionInfo.overrideGlobal());
0075     mMaximumAgeInDay->setValue(retentionInfo.maxAge());
0076     setOverrideGlobalSettings(retentionInfo.overrideGlobal());
0077 }
0078 
0079 void ChannelInfoPruneWidget::saveRoomSettingsInfo(RocketChatRestApi::SaveRoomSettingsJob::SaveRoomSettingsInfo &info, Room *mRoom)
0080 {
0081     const auto retentionInfo = mRoom->retentionInfo();
0082     if (retentionInfo.maxAge() != mMaximumAgeInDay->value()) {
0083         info.mSettingsWillBeChanged |= RocketChatRestApi::SaveRoomSettingsJob::SaveRoomSettingsInfo::RetentionMaxAge;
0084         info.retentionMaxAge = mMaximumAgeInDay->value();
0085     }
0086     if (retentionInfo.excludePinned() != mExcludePinnedMessages->isChecked()) {
0087         info.mSettingsWillBeChanged |= RocketChatRestApi::SaveRoomSettingsJob::SaveRoomSettingsInfo::RetentionExcludePinned;
0088         info.retentionExcludePinned = mExcludePinnedMessages->isChecked();
0089     }
0090     if (retentionInfo.filesOnly() != mPruneFileOnlyKeepMessages->isChecked()) {
0091         info.mSettingsWillBeChanged |= RocketChatRestApi::SaveRoomSettingsJob::SaveRoomSettingsInfo::RetentionFilesOnly;
0092         info.retentionFilesOnly = mPruneFileOnlyKeepMessages->isChecked();
0093     }
0094     if (retentionInfo.filesOnly() != mAutomaticPruneOldMessages->isChecked()) {
0095         info.mSettingsWillBeChanged |= RocketChatRestApi::SaveRoomSettingsJob::SaveRoomSettingsInfo::RetentionEnabled;
0096         info.retentionEnabled = mAutomaticPruneOldMessages->isChecked();
0097     }
0098 
0099     if (retentionInfo.overrideGlobal() != mOverrideGlobalRetentionPolicy->isChecked()) {
0100         info.mSettingsWillBeChanged |= RocketChatRestApi::SaveRoomSettingsJob::SaveRoomSettingsInfo::RetentionOverrideGlobal;
0101         info.retentionOverrideGlobal = mOverrideGlobalRetentionPolicy->isChecked();
0102     }
0103 }
0104 
0105 #include "moc_channelinfoprunewidget.cpp"