File indexing completed on 2024-05-19 05:04:16

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "prunemessageswidget.h"
0008 #include "misc/adduserswidget.h"
0009 
0010 #include <KLocalizedString>
0011 #include <QCheckBox>
0012 #include <QDateTimeEdit>
0013 #include <QLabel>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 
0017 PruneMessagesWidget::PruneMessagesWidget(RocketChatAccount *account, QWidget *parent)
0018     : QWidget(parent)
0019     , mInclusive(new QCheckBox(i18n("Inclusive"), this))
0020     , mDoNotPrunePinnedMessage(new QCheckBox(i18n("Do not Prune Pinned Messages"), this))
0021     , mDoNotPruneDiscussionMessage(new QCheckBox(i18n("Do not Prune Discussion Messages"), this))
0022     , mDoNotPruneThreads(new QCheckBox(i18n("Do not Prune Threads"), this))
0023     , mOnlyRemoveAttachedFiles(new QCheckBox(i18n("Only Remove Attached Files. Keep messages"), this))
0024     , mLastestDateTimeEdit(new QDateTimeEdit(this))
0025     , mOldestDateTimeEdit(new QDateTimeEdit(this))
0026     , mUsers(new AddUsersWidget(account, this))
0027     , mInfoLabel(new QLabel(this))
0028 {
0029     auto mainLayout = new QVBoxLayout(this);
0030     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0031     mainLayout->setContentsMargins({});
0032 
0033     auto lastestLayout = new QHBoxLayout;
0034     lastestLayout->setObjectName(QStringLiteral("lastestLayout"));
0035     lastestLayout->setContentsMargins({});
0036     mainLayout->addLayout(lastestLayout);
0037 
0038     auto lastestLabel = new QLabel(i18n("Newer than:"), this);
0039     lastestLabel->setObjectName(QStringLiteral("lastestLabel"));
0040     lastestLayout->addWidget(lastestLabel);
0041 
0042     mLastestDateTimeEdit->setObjectName(QStringLiteral("mLastestDateTimeEdit"));
0043     connect(mLastestDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &PruneMessagesWidget::slotCheckDateTime);
0044     lastestLayout->addWidget(mLastestDateTimeEdit);
0045 
0046     auto oldestLayout = new QHBoxLayout;
0047     oldestLayout->setObjectName(QStringLiteral("oldestLayout"));
0048     oldestLayout->setContentsMargins({});
0049     mainLayout->addLayout(oldestLayout);
0050 
0051     auto oldestLabel = new QLabel(i18n("Older than:"), this);
0052     oldestLabel->setObjectName(QStringLiteral("oldestLabel"));
0053     oldestLayout->addWidget(oldestLabel);
0054 
0055     mOldestDateTimeEdit->setObjectName(QStringLiteral("mOldestDateTimeEdit"));
0056     connect(mOldestDateTimeEdit, &QDateTimeEdit::dateTimeChanged, this, &PruneMessagesWidget::slotCheckDateTime);
0057     oldestLayout->addWidget(mOldestDateTimeEdit);
0058 
0059     auto usersLabel = new QLabel(i18n("Only Prune content from these users (Keep empty to prune everyone's content)"), this);
0060     usersLabel->setObjectName(QStringLiteral("usersLabel"));
0061     usersLabel->setWordWrap(true);
0062     usersLabel->setTextFormat(Qt::PlainText);
0063     mainLayout->addWidget(usersLabel);
0064 
0065     mUsers->setObjectName(QStringLiteral("mUsers"));
0066     mUsers->setPlaceholderText(i18n("Select users..."));
0067     mainLayout->addWidget(mUsers);
0068 
0069     mInclusive->setObjectName(QStringLiteral("mInclusive"));
0070     mainLayout->addWidget(mInclusive);
0071 
0072     mDoNotPrunePinnedMessage->setObjectName(QStringLiteral("mDoNotPrunePinnedMessage"));
0073     mainLayout->addWidget(mDoNotPrunePinnedMessage);
0074 
0075     mDoNotPruneDiscussionMessage->setObjectName(QStringLiteral("mDoNotPruneDiscussionMessage"));
0076     mainLayout->addWidget(mDoNotPruneDiscussionMessage);
0077 
0078     mDoNotPruneThreads->setObjectName(QStringLiteral("mDoNotPruneThreads"));
0079     mainLayout->addWidget(mDoNotPruneThreads);
0080 
0081     mOnlyRemoveAttachedFiles->setObjectName(QStringLiteral("mOnlyRemoveAttachedFiles"));
0082     mainLayout->addWidget(mOnlyRemoveAttachedFiles);
0083 
0084     mInfoLabel->setObjectName(QStringLiteral("mInfoLabel"));
0085     mInfoLabel->setWordWrap(true);
0086     mainLayout->addWidget(mInfoLabel);
0087 
0088     mainLayout->addStretch(1);
0089 }
0090 
0091 PruneMessagesWidget::~PruneMessagesWidget() = default;
0092 
0093 void PruneMessagesWidget::updateLabelInfo()
0094 {
0095     const QString message = i18n("This will delete all messages in %3 between %1 and %2.",
0096                                  mLastestDateTimeEdit->dateTime().toString(),
0097                                  mOldestDateTimeEdit->dateTime().toString(),
0098                                  mRoomName);
0099     mInfoLabel->setText(message);
0100 }
0101 
0102 void PruneMessagesWidget::slotCheckDateTime()
0103 {
0104     const bool valid =
0105         (mLastestDateTimeEdit->dateTime() != mOldestDateTimeEdit->dateTime()) && (mLastestDateTimeEdit->dateTime() < mOldestDateTimeEdit->dateTime());
0106     updateLabelInfo();
0107     Q_EMIT updateOkButton(valid);
0108 }
0109 
0110 RocketChatRestApi::RoomsCleanHistoryJob::CleanHistoryInfo PruneMessagesWidget::cleanHistoryInfo() const
0111 {
0112     RocketChatRestApi::RoomsCleanHistoryJob::CleanHistoryInfo info;
0113     info.latest = mLastestDateTimeEdit->dateTime().toUTC();
0114     info.oldest = mOldestDateTimeEdit->dateTime().toUTC();
0115     info.inclusive = mInclusive->isChecked();
0116     info.users = mUsers->userNames();
0117     info.ignoreThreads = mDoNotPruneThreads->isChecked();
0118     info.filesOnly = mOnlyRemoveAttachedFiles->isChecked();
0119     info.excludePinned = mDoNotPrunePinnedMessage->isChecked();
0120     info.ignoreDiscussion = mDoNotPruneDiscussionMessage->isChecked();
0121     // qDebug() << " info " << info;
0122     return info;
0123 }
0124 
0125 void PruneMessagesWidget::setRoomName(const QString &roomName)
0126 {
0127     mRoomName = roomName;
0128 }
0129 
0130 #include "moc_prunemessageswidget.cpp"