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

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "notificationhistorywidget.h"
0008 #include "misc/lineeditcatchreturnkey.h"
0009 #include "misc/serverscombobox.h"
0010 #include "model/notificationhistorymodel.h"
0011 #include "model/notificationhistorymodelfilterproxymodel.h"
0012 #include "notificationhistorylistview.h"
0013 #include "notificationhistorymanager.h"
0014 #include "ruqolawidgets_debug.h"
0015 #include <KLocalizedString>
0016 #include <QLineEdit>
0017 #include <QListView>
0018 #include <QVBoxLayout>
0019 
0020 #include "config-ruqola.h"
0021 
0022 #if HAVE_TEXT_TO_SPEECH
0023 #include <TextEditTextToSpeech/TextToSpeechContainerWidget>
0024 #endif
0025 
0026 NotificationHistoryWidget::NotificationHistoryWidget(QWidget *parent)
0027     : QWidget{parent}
0028     , mListNotificationsListView(new NotificationHistoryListView(this))
0029     , mSearchLineEdit(new QLineEdit(this))
0030     , mNotificationFilterProxyModel(new NotificationHistoryModelFilterProxyModel(this))
0031 #if HAVE_TEXT_TO_SPEECH
0032     , mTextToSpeechWidget(new TextEditTextToSpeech::TextToSpeechContainerWidget(this))
0033 #endif
0034     , mServersComboBox(new ServersComboBox(this))
0035 {
0036     auto mainLayout = new QVBoxLayout(this);
0037     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0038     mainLayout->setContentsMargins({});
0039 
0040     auto searchLayout = new QHBoxLayout;
0041     searchLayout->setObjectName(QStringLiteral("searchLayout"));
0042     searchLayout->setContentsMargins({});
0043 
0044     mSearchLineEdit->setObjectName(QStringLiteral("mSearchLineEdit"));
0045     mSearchLineEdit->setPlaceholderText(i18n("Search..."));
0046     searchLayout->addWidget(mSearchLineEdit);
0047     mSearchLineEdit->setClearButtonEnabled(true);
0048     new LineEditCatchReturnKey(mSearchLineEdit, this);
0049     mServersComboBox->setObjectName(QStringLiteral("mServersComboBox"));
0050     searchLayout->addWidget(mServersComboBox);
0051 
0052     mainLayout->addLayout(searchLayout);
0053 
0054 #if HAVE_TEXT_TO_SPEECH
0055     mTextToSpeechWidget->setObjectName(QStringLiteral("mTextToSpeechWidget"));
0056     mainLayout->addWidget(mTextToSpeechWidget);
0057     connect(mListNotificationsListView,
0058             &NotificationHistoryListView::textToSpeech,
0059             mTextToSpeechWidget,
0060             &TextEditTextToSpeech::TextToSpeechContainerWidget::say);
0061 #endif
0062 
0063     mListNotificationsListView->setObjectName(QStringLiteral("mListNotifications"));
0064     mainLayout->addWidget(mListNotificationsListView);
0065 
0066     auto model = NotificationHistoryManager::self()->notificationHistoryModel();
0067 
0068     mNotificationFilterProxyModel->setObjectName(QStringLiteral("mNotificationFilterProxyModel"));
0069     mNotificationFilterProxyModel->setSourceModel(model);
0070     mListNotificationsListView->setModel(mNotificationFilterProxyModel);
0071 
0072     connect(mListNotificationsListView, &QListView::doubleClicked, this, &NotificationHistoryWidget::slotShowMessage);
0073     connect(mListNotificationsListView, &NotificationHistoryListView::showMessage, this, &NotificationHistoryWidget::slotShowMessage);
0074 
0075     connect(model, &QAbstractItemModel::rowsAboutToBeInserted, mListNotificationsListView, &MessageListViewBase::checkIfAtBottom);
0076     connect(model, &QAbstractItemModel::rowsAboutToBeRemoved, mListNotificationsListView, &MessageListViewBase::checkIfAtBottom);
0077     connect(model, &QAbstractItemModel::modelAboutToBeReset, mListNotificationsListView, &MessageListViewBase::checkIfAtBottom);
0078 
0079     connect(mSearchLineEdit, &QLineEdit::textChanged, this, &NotificationHistoryWidget::slotTextChanged);
0080 
0081     connect(mServersComboBox, &ServersComboBox::accountSelected, this, &NotificationHistoryWidget::slotFilterAccount);
0082 }
0083 
0084 NotificationHistoryWidget::~NotificationHistoryWidget() = default;
0085 
0086 void NotificationHistoryWidget::slotFilterAccount(const QString &accountName)
0087 {
0088     mNotificationFilterProxyModel->setAccountNameFilter(accountName);
0089 }
0090 
0091 void NotificationHistoryWidget::slotTextChanged(const QString &str)
0092 {
0093     mNotificationFilterProxyModel->setFilterString(str);
0094     mListNotificationsListView->setSearchText(str);
0095 }
0096 
0097 void NotificationHistoryWidget::slotShowMessage(const QModelIndex &index)
0098 {
0099     if (index.isValid()) {
0100         const QString roomId = index.data(NotificationHistoryModel::RoomId).toString();
0101         const QString messageId = index.data(NotificationHistoryModel::MessageId).toString();
0102         const QString accountName = index.data(NotificationHistoryModel::AccountName).toString();
0103         if (!accountName.isEmpty() && !roomId.isEmpty() && !messageId.isEmpty()) {
0104             Q_EMIT showNotifyMessage(accountName, messageId, roomId);
0105         } else {
0106             qCWarning(RUQOLAWIDGETS_LOG) << " Problem with index. AccountName " << accountName << " roomId : " << roomId << "messageId " << messageId;
0107         }
0108     }
0109 }
0110 
0111 void NotificationHistoryWidget::addServerList(const QStringList &serverNames)
0112 {
0113     mServersComboBox->addServerList(serverNames);
0114 }
0115 
0116 #include "moc_notificationhistorywidget.cpp"