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

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 "configurenotificationwidget.h"
0008 #include "model/notificationdesktopsoundpreferencemodel.h"
0009 #include "model/notificationpreferencemodel.h"
0010 #include "notifications/notificationpreferences.h"
0011 #include "rocketchataccount.h"
0012 #include "room.h"
0013 #include <KLocalizedString>
0014 #include <QCheckBox>
0015 #include <QComboBox>
0016 #include <QFormLayout>
0017 #include <QGroupBox>
0018 #include <QVBoxLayout>
0019 
0020 ConfigureNotificationWidget::ConfigureNotificationWidget(RocketChatAccount *account, QWidget *parent)
0021     : QWidget(parent)
0022     , mDisableNotification(new QCheckBox(i18n("Disable Notification"), this))
0023     , mHideUnreadRoomStatus(new QCheckBox(i18n("Hide Unread Room Status"), this))
0024     , mMuteGroupMentions(new QCheckBox(i18n("Mute %1 and %2 mentions", QStringLiteral("@all"), QStringLiteral("@here")), this))
0025     , mShowBadgeMentions(new QCheckBox(i18n("Show badge for mentions"), this))
0026     , mDesktopAlertCombobox(new QComboBox(this))
0027     , mDesktopSoundCombobox(new QComboBox(this))
0028     , mMobileAlertCombobox(new QComboBox(this))
0029     , mEmailAlertCombobox(new QComboBox(this))
0030 {
0031     auto topLayout = new QVBoxLayout(this);
0032     topLayout->setObjectName(QStringLiteral("topLayout"));
0033     topLayout->setContentsMargins({});
0034 
0035     mDisableNotification->setObjectName(QStringLiteral("mDisableNotification"));
0036     mDisableNotification->setToolTip(i18n("Receive alerts"));
0037     topLayout->addWidget(mDisableNotification);
0038     connect(mDisableNotification, &QCheckBox::clicked, this, [this, account](bool checked) {
0039         account->changeNotificationsSettings(mRoom->roomId(), RocketChatAccount::DisableNotifications, checked);
0040     });
0041 
0042     mHideUnreadRoomStatus->setObjectName(QStringLiteral("mHideUnreadRoomStatus"));
0043     topLayout->addWidget(mHideUnreadRoomStatus);
0044     connect(mHideUnreadRoomStatus, &QCheckBox::clicked, this, [this, account](bool checked) {
0045         account->changeNotificationsSettings(mRoom->roomId(), RocketChatAccount::HideUnreadStatus, checked);
0046     });
0047 
0048     mMuteGroupMentions->setObjectName(QStringLiteral("mMuteGroupMentions"));
0049     topLayout->addWidget(mMuteGroupMentions);
0050     connect(mMuteGroupMentions, &QCheckBox::clicked, this, [this, account](bool checked) {
0051         account->changeNotificationsSettings(mRoom->roomId(), RocketChatAccount::MuteGroupMentions, checked);
0052     });
0053 
0054     mShowBadgeMentions->setObjectName(QStringLiteral("mShowBadgeMentions"));
0055     mShowBadgeMentions->setToolTip(i18n("Display badge for direct mentions only"));
0056     topLayout->addWidget(mShowBadgeMentions);
0057     connect(mShowBadgeMentions, &QCheckBox::clicked, this, [this, account](bool checked) {
0058         account->changeNotificationsSettings(mRoom->roomId(), RocketChatAccount::HideMentionStatus, !checked);
0059     });
0060 
0061     auto desktopGroupBox = new QGroupBox(i18n("Desktop"), this);
0062     desktopGroupBox->setObjectName(QStringLiteral("desktopGroupBox"));
0063     topLayout->addWidget(desktopGroupBox);
0064 
0065     auto desktopGroupBoxLayout = new QFormLayout(desktopGroupBox);
0066     desktopGroupBoxLayout->setObjectName(QStringLiteral("desktopGroupBoxLayout"));
0067 
0068     mDesktopAlertCombobox->setObjectName(QStringLiteral("mDesktopAlertCombobox"));
0069     desktopGroupBoxLayout->addRow(i18n("Alert:"), mDesktopAlertCombobox);
0070     mDesktopAlertCombobox->setModel(NotificationPreferences::self()->desktopNotificationModel());
0071     connect(mDesktopAlertCombobox, &QComboBox::activated, this, [this, account](int index) {
0072         account->changeNotificationsSettings(mRoom->roomId(),
0073                                              RocketChatAccount::DesktopNotifications,
0074                                              NotificationPreferences::self()->desktopNotificationModel()->currentPreference(index));
0075     });
0076 
0077     mDesktopSoundCombobox->setObjectName(QStringLiteral("mDesktopSoundCombobox"));
0078     desktopGroupBoxLayout->addRow(i18n("Sound:"), mDesktopSoundCombobox);
0079     mDesktopSoundCombobox->setModel(NotificationPreferences::self()->desktopSoundNotificationModel());
0080     connect(mDesktopSoundCombobox, &QComboBox::activated, this, [this, account](int index) {
0081         account->changeNotificationsSettings(mRoom->roomId(),
0082                                              RocketChatAccount::DesktopSoundNotifications,
0083                                              NotificationPreferences::self()->desktopSoundNotificationModel()->currentPreference(index));
0084     });
0085 
0086     auto mobileGroupBox = new QGroupBox(i18n("Mobile"), this);
0087     mobileGroupBox->setObjectName(QStringLiteral("mobileGroupBox"));
0088     topLayout->addWidget(mobileGroupBox);
0089 
0090     auto mobileGroupBoxLayout = new QFormLayout(mobileGroupBox);
0091     mobileGroupBoxLayout->setObjectName(QStringLiteral("mobileGroupBoxLayout"));
0092 
0093     mMobileAlertCombobox->setObjectName(QStringLiteral("mMobileAlertCombobox"));
0094     mobileGroupBoxLayout->addRow(i18n("Alert:"), mMobileAlertCombobox);
0095     mMobileAlertCombobox->setModel(NotificationPreferences::self()->mobileNotificationModel());
0096     connect(mMobileAlertCombobox, &QComboBox::activated, this, [this, account](int index) {
0097         account->changeNotificationsSettings(mRoom->roomId(),
0098                                              RocketChatAccount::MobilePushNotifications,
0099                                              NotificationPreferences::self()->mobileNotificationModel()->currentPreference(index));
0100     });
0101 
0102     auto emailGroupBox = new QGroupBox(i18n("Email"), this);
0103     emailGroupBox->setObjectName(QStringLiteral("emailGroupBox"));
0104     topLayout->addWidget(emailGroupBox);
0105 
0106     auto emailGroupBoxLayout = new QFormLayout(emailGroupBox);
0107     emailGroupBoxLayout->setObjectName(QStringLiteral("emailGroupBoxLayout"));
0108 
0109     mEmailAlertCombobox->setObjectName(QStringLiteral("mEmailAlertCombobox"));
0110     emailGroupBoxLayout->addRow(i18n("Alert:"), mEmailAlertCombobox);
0111     mEmailAlertCombobox->setModel(NotificationPreferences::self()->emailNotificationModel());
0112     connect(mEmailAlertCombobox, &QComboBox::activated, this, [this, account](int index) {
0113         account->changeNotificationsSettings(mRoom->roomId(),
0114                                              RocketChatAccount::EmailNotifications,
0115                                              NotificationPreferences::self()->emailNotificationModel()->currentPreference(index));
0116     });
0117 }
0118 
0119 ConfigureNotificationWidget::~ConfigureNotificationWidget() = default;
0120 
0121 Room *ConfigureNotificationWidget::room() const
0122 {
0123     return mRoom;
0124 }
0125 
0126 void ConfigureNotificationWidget::setRoom(Room *room)
0127 {
0128     mRoom = room;
0129     NotificationOptions notificationOptions = mRoom->notificationOptions();
0130     mDisableNotification->setChecked(notificationOptions.disableNotifications());
0131     mHideUnreadRoomStatus->setChecked(notificationOptions.hideUnreadStatus());
0132     mMuteGroupMentions->setChecked(notificationOptions.muteGroupMentions());
0133     mShowBadgeMentions->setChecked(!notificationOptions.hideMentionStatus());
0134     mDesktopAlertCombobox->setCurrentIndex(NotificationPreferences::self()->desktopNotificationModel()->setCurrentNotificationPreference(
0135         notificationOptions.desktopNotifications().currentValue()));
0136     mDesktopSoundCombobox->setCurrentIndex(
0137         NotificationPreferences::self()->desktopSoundNotificationModel()->setCurrentNotificationPreference(notificationOptions.audioNotificationValue()));
0138     mMobileAlertCombobox->setCurrentIndex(NotificationPreferences::self()->mobileNotificationModel()->setCurrentNotificationPreference(
0139         notificationOptions.mobilePushNotification().currentValue()));
0140     mEmailAlertCombobox->setCurrentIndex(
0141         NotificationPreferences::self()->emailNotificationModel()->setCurrentNotificationPreference(notificationOptions.emailNotifications().currentValue()));
0142 }
0143 
0144 #include "moc_configurenotificationwidget.cpp"