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

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 "showattachmentwidget.h"
0008 #include "attachment/listattachmentdelegate.h"
0009 #include "misc/lineeditcatchreturnkey.h"
0010 #include "model/filesforroomfilterproxymodel.h"
0011 #include "showattachmentcombobox.h"
0012 #include <KLocalizedString>
0013 #include <QLabel>
0014 #include <QLineEdit>
0015 #include <QListView>
0016 #include <QVBoxLayout>
0017 
0018 ShowAttachmentWidget::ShowAttachmentWidget(RocketChatAccount *account, QWidget *parent)
0019     : QWidget(parent)
0020     , mSearchAttachmentFileLineEdit(new QLineEdit(this))
0021     , mAttachmentCombobox(new ShowAttachmentComboBox(this))
0022     , mInfo(new QLabel(this))
0023     , mListAttachment(new QListView(this))
0024 {
0025     auto mainLayout = new QVBoxLayout(this);
0026     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0027     mainLayout->setContentsMargins({});
0028 
0029     auto searchAttachmentLayout = new QHBoxLayout;
0030     searchAttachmentLayout->setObjectName(QStringLiteral("searchAttachmentLayout"));
0031     searchAttachmentLayout->setContentsMargins({});
0032     mainLayout->addLayout(searchAttachmentLayout);
0033 
0034     mSearchAttachmentFileLineEdit->setObjectName(QStringLiteral("mSearchAttachmentFileLineEdit"));
0035     mSearchAttachmentFileLineEdit->setClearButtonEnabled(true);
0036     new LineEditCatchReturnKey(mSearchAttachmentFileLineEdit, this);
0037     mSearchAttachmentFileLineEdit->setPlaceholderText(i18n("Search attachments..."));
0038     connect(mSearchAttachmentFileLineEdit, &QLineEdit::textChanged, this, &ShowAttachmentWidget::slotSearchMessageTextChanged);
0039     searchAttachmentLayout->addWidget(mSearchAttachmentFileLineEdit);
0040 
0041     mAttachmentCombobox->setObjectName(QStringLiteral("mAttachmentCombobox"));
0042     searchAttachmentLayout->addWidget(mAttachmentCombobox);
0043     connect(mAttachmentCombobox, &ShowAttachmentComboBox::currentIndexChanged, this, &ShowAttachmentWidget::slotChangeAttachmentType);
0044 
0045     mInfo->setObjectName(QStringLiteral("mInfo"));
0046     mInfo->setTextFormat(Qt::RichText);
0047     mInfo->setContextMenuPolicy(Qt::NoContextMenu);
0048     mainLayout->addWidget(mInfo);
0049     QFont labFont = mInfo->font();
0050     labFont.setBold(true);
0051     mInfo->setFont(labFont);
0052     connect(mInfo, &QLabel::linkActivated, this, &ShowAttachmentWidget::loadMoreFileAttachment);
0053 
0054     mListAttachment->setObjectName(QStringLiteral("mListAttachment"));
0055     mainLayout->addWidget(mListAttachment);
0056     auto delegate = new ListAttachmentDelegate(account, this);
0057     connect(delegate, &ListAttachmentDelegate::deleteAttachment, this, &ShowAttachmentWidget::deleteAttachment);
0058     mListAttachment->setItemDelegate(delegate);
0059 }
0060 
0061 ShowAttachmentWidget::~ShowAttachmentWidget()
0062 {
0063     // Reset it
0064     if (mModel) {
0065         mModel->setFilterString(QString());
0066         mModel->clear();
0067     }
0068 }
0069 
0070 void ShowAttachmentWidget::slotChangeAttachmentType(int index)
0071 {
0072     mModel->setTypeGroup(mAttachmentCombobox->itemData(index).toString());
0073 }
0074 
0075 void ShowAttachmentWidget::setModel(FilesForRoomFilterProxyModel *model)
0076 {
0077     mModel = model;
0078     mModel->resetTypeGroup();
0079     mListAttachment->setModel(model);
0080     connect(mModel, &FilesForRoomFilterProxyModel::hasFullListChanged, this, &ShowAttachmentWidget::updateLabel);
0081     connect(mModel, &FilesForRoomFilterProxyModel::totalChanged, this, &ShowAttachmentWidget::updateLabel);
0082     connect(mModel, &FilesForRoomFilterProxyModel::loadingInProgressChanged, this, &ShowAttachmentWidget::updateLabel);
0083     updateLabel();
0084 }
0085 
0086 void ShowAttachmentWidget::slotSearchMessageTextChanged(const QString &str)
0087 {
0088     mModel->setFilterString(str);
0089 }
0090 
0091 void ShowAttachmentWidget::updateLabel()
0092 {
0093     mInfo->setText(mModel->attachmentCount() == 0 ? i18n("No Attachments found") : displayShowMessageInRoom());
0094 }
0095 
0096 QString ShowAttachmentWidget::displayShowMessageInRoom() const
0097 {
0098     QString displayMessageStr = i18np("%1 Attachment in room (Total: %2)", "%1 Attachments in room (Total: %2)", mModel->attachmentCount(), mModel->total());
0099     if (!mModel->hasFullList()) {
0100         displayMessageStr += QStringLiteral(" <a href=\"loadmoreelement\">%1</a>").arg(i18n("(Click here for Loading more...)"));
0101     }
0102     return displayMessageStr;
0103 }
0104 
0105 #include "moc_showattachmentwidget.cpp"