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

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 "myaccountpreferenceconfigurewidget.h"
0008 #include "connection.h"
0009 #include "misc/lineeditcatchreturnkey.h"
0010 #include "rocketchataccount.h"
0011 #include "ruqolawidgets_debug.h"
0012 #include "users/userrequestdatadownloadjob.h"
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 #include <KSeparator>
0016 #include <QCheckBox>
0017 #include <QComboBox>
0018 #include <QLabel>
0019 #include <QLineEdit>
0020 #include <QPushButton>
0021 #include <QSpinBox>
0022 #include <QVBoxLayout>
0023 
0024 MyAccountPreferenceConfigureWidget::MyAccountPreferenceConfigureWidget(RocketChatAccount *account, QWidget *parent)
0025     : QWidget(parent)
0026     , mHighlightWords(new QLineEdit(this))
0027     , mDesktopNotification(new QComboBox(this))
0028     , mEmailNotification(new QComboBox(this))
0029     , mPushNotification(new QComboBox(this))
0030     , mUseEmojis(new QCheckBox(i18n("Use Emojis"), this))
0031     , mConvertAsciiEmoji(new QCheckBox(i18n("Convert Ascii to Emoji"), this))
0032     , mHideRoles(new QCheckBox(i18n("Hide roles"), this))
0033     , mDisplayAvatars(new QCheckBox(i18n("Display avatars"), this))
0034     , mReceiveLoginDetectionEmails(new QCheckBox(i18n("Receive login detection emails"), this))
0035     , mIdleTimeLimit(new QSpinBox(this))
0036     , mAutomaticAway(new QCheckBox(i18n("Enable Auto Away"), this))
0037     , mRocketChatAccount(account)
0038 {
0039     mUseEmojis->setObjectName(QStringLiteral("mUseEmojis"));
0040     mConvertAsciiEmoji->setObjectName(QStringLiteral("mConvertAsciiEmoji"));
0041     mHideRoles->setObjectName(QStringLiteral("mHideRoles"));
0042     mDisplayAvatars->setObjectName(QStringLiteral("mDisplayAvatars"));
0043     auto mainLayout = new QVBoxLayout(this);
0044     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0045 
0046     auto highlightWordsLabel = new QLabel(i18n("Highlight words:"), this);
0047     highlightWordsLabel->setObjectName(QStringLiteral("highlightWordsLabel"));
0048     highlightWordsLabel->setTextFormat(Qt::PlainText);
0049     mainLayout->addWidget(highlightWordsLabel);
0050 
0051     mHighlightWords->setObjectName(QStringLiteral("mHighlightWords"));
0052     new LineEditCatchReturnKey(mHighlightWords, this);
0053     mHighlightWords->setPlaceholderText(i18n("Use \',\' for separating words"));
0054     mHighlightWords->setToolTip(i18n("Separate each word with \',\'."));
0055     connect(mHighlightWords, &QLineEdit::textEdited, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0056     mainLayout->addWidget(mHighlightWords);
0057 
0058     mDesktopNotification->setObjectName(QStringLiteral("mDesktopNotification"));
0059     mEmailNotification->setObjectName(QStringLiteral("mEmailNotification"));
0060     mPushNotification->setObjectName(QStringLiteral("mPushNotification"));
0061 
0062     auto desktopNotificationLabel = new QLabel(i18n("Desktop notification:"), this);
0063     desktopNotificationLabel->setObjectName(QStringLiteral("desktopNotificationLabel"));
0064     desktopNotificationLabel->setTextFormat(Qt::PlainText);
0065     mainLayout->addWidget(desktopNotificationLabel);
0066 
0067     mainLayout->addWidget(mDesktopNotification);
0068 
0069     auto emailNotificationLabel = new QLabel(i18n("Offline Email notification:"), this);
0070     emailNotificationLabel->setObjectName(QStringLiteral("emailNotificationLabel"));
0071     emailNotificationLabel->setTextFormat(Qt::PlainText);
0072     mainLayout->addWidget(emailNotificationLabel);
0073 
0074     mainLayout->addWidget(mEmailNotification);
0075 
0076     auto pushNotificationLabel = new QLabel(i18n("Push notification:"), this);
0077     pushNotificationLabel->setObjectName(QStringLiteral("pushNotificationLabel"));
0078     pushNotificationLabel->setTextFormat(Qt::PlainText);
0079     mainLayout->addWidget(pushNotificationLabel);
0080 
0081     mainLayout->addWidget(mPushNotification);
0082 
0083     mReceiveLoginDetectionEmails->setObjectName(QStringLiteral("mReceiveLoginDetectionEmails"));
0084     mReceiveLoginDetectionEmails->setToolTip(i18n("Receive an email each time a new login is detected on your account."));
0085     connect(mReceiveLoginDetectionEmails, &QCheckBox::clicked, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0086     mainLayout->addWidget(mReceiveLoginDetectionEmails);
0087 
0088     mainLayout->addWidget(mUseEmojis);
0089     connect(mUseEmojis, &QCheckBox::clicked, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0090     mainLayout->addWidget(mConvertAsciiEmoji);
0091     connect(mConvertAsciiEmoji, &QCheckBox::clicked, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0092     mainLayout->addWidget(mHideRoles);
0093     connect(mHideRoles, &QCheckBox::clicked, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0094     mainLayout->addWidget(mDisplayAvatars);
0095     connect(mDisplayAvatars, &QCheckBox::clicked, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0096 
0097     mainLayout->addWidget(new KSeparator(this));
0098 
0099     mAutomaticAway->setObjectName(QStringLiteral("mAutomaticAways"));
0100     connect(mAutomaticAway, &QCheckBox::clicked, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0101     mainLayout->addWidget(mAutomaticAway);
0102 
0103     auto idleTimeLimitLabel = new QLabel(i18n("Idle Time Limit:"), this);
0104     idleTimeLimitLabel->setObjectName(QStringLiteral("idleTimeLimitLabel"));
0105     idleTimeLimitLabel->setTextFormat(Qt::PlainText);
0106     mainLayout->addWidget(idleTimeLimitLabel);
0107 
0108     mIdleTimeLimit->setObjectName(QStringLiteral("mIdleTimeLimit"));
0109     mIdleTimeLimit->setMaximum(9999);
0110     mIdleTimeLimit->setToolTip(i18n("Period of time until status changes to away. Value needs to be in seconds."));
0111     connect(mIdleTimeLimit, &QSpinBox::valueChanged, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0112     mainLayout->addWidget(mIdleTimeLimit);
0113 
0114     QWidget *downloadWidget = new QWidget;
0115     downloadWidget->setObjectName(QStringLiteral("downloadWidget"));
0116     auto downloadWidgetLayout = new QVBoxLayout(downloadWidget);
0117     downloadWidgetLayout->setContentsMargins({});
0118 
0119     downloadWidgetLayout->addWidget(new KSeparator(this));
0120 
0121     auto downloadLayout = new QHBoxLayout;
0122     downloadLayout->setObjectName(QStringLiteral("downloadLayout"));
0123     downloadLayout->setContentsMargins({});
0124 
0125     downloadWidgetLayout->addLayout(downloadLayout);
0126 
0127     auto downloadDataButton = new QPushButton(i18n("Download my Data (HTML)"), this);
0128     downloadDataButton->setObjectName(QStringLiteral("downloadDataButton"));
0129     downloadLayout->addWidget(downloadDataButton);
0130     connect(downloadDataButton, &QPushButton::clicked, this, [this]() {
0131         downloadData(false);
0132     });
0133 
0134     auto exportDataButton = new QPushButton(i18n("Export my Data (JSON)"), this);
0135     exportDataButton->setObjectName(QStringLiteral("exportDataButton"));
0136     downloadLayout->addWidget(exportDataButton);
0137     connect(exportDataButton, &QPushButton::clicked, this, [this]() {
0138         downloadData(true);
0139     });
0140 
0141     mainLayout->addWidget(downloadWidget);
0142 
0143     mainLayout->addStretch();
0144     if (mRocketChatAccount && !mRocketChatAccount->ruqolaServerConfig()->userDataDownloadEnabled()) {
0145         downloadWidget->setVisible(false);
0146     }
0147     initComboboxValues();
0148     if (mRocketChatAccount) {
0149         if (!mRocketChatAccount->ruqolaServerConfig()->hasAtLeastVersion(5, 4, 0)) {
0150             mReceiveLoginDetectionEmails->setVisible(false);
0151         } else if (!mRocketChatAccount->ruqolaServerConfig()->deviceManagementEnableLoginEmails()
0152                    || !mRocketChatAccount->ruqolaServerConfig()->deviceManagementAllowLoginEmailpreference()) {
0153             mReceiveLoginDetectionEmails->setVisible(false);
0154         }
0155     }
0156 }
0157 
0158 MyAccountPreferenceConfigureWidget::~MyAccountPreferenceConfigureWidget() = default;
0159 
0160 void MyAccountPreferenceConfigureWidget::downloadData(bool fullData)
0161 {
0162     auto job = new RocketChatRestApi::UserRequestDataDownloadJob(this);
0163     job->setFullExport(fullData);
0164     mRocketChatAccount->restApi()->initializeRestApiJob(job);
0165     connect(job,
0166             &RocketChatRestApi::UserRequestDataDownloadJob::userRequestDataDownloadDone,
0167             this,
0168             &MyAccountPreferenceConfigureWidget::slotUserRequestDataDownloadDone);
0169     if (!job->start()) {
0170         qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start UserRequestDataDownloadJob job";
0171     }
0172 }
0173 
0174 void MyAccountPreferenceConfigureWidget::slotUserRequestDataDownloadDone()
0175 {
0176     KMessageBox::information(this,
0177                              i18n("Your data file will be generated. A link to download it will be sent to your email address when ready."),
0178                              i18n("Download File Requested"));
0179 }
0180 
0181 void MyAccountPreferenceConfigureWidget::initComboboxValues()
0182 {
0183     // Default ?
0184     mDesktopNotification->addItem(i18n("All Messages"), QStringLiteral("all"));
0185     mDesktopNotification->addItem(i18n("Mentions"), QStringLiteral("mentions"));
0186     mDesktopNotification->addItem(i18n("Nothing"), QStringLiteral("nothing"));
0187 
0188     // Default ?
0189     mPushNotification->addItem(i18n("All Messages"), QStringLiteral("all"));
0190     mPushNotification->addItem(i18n("Mentions"), QStringLiteral("mentions"));
0191     mPushNotification->addItem(i18n("Nothing"), QStringLiteral("nothing"));
0192 
0193     // Default ?
0194     mEmailNotification->addItem(i18n("Each Mentions"), QStringLiteral("mentions"));
0195     mEmailNotification->addItem(i18n("Disabled"), QStringLiteral("nothing"));
0196 
0197     connect(mDesktopNotification, &QComboBox::activated, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0198     connect(mPushNotification, &QComboBox::activated, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0199     connect(mEmailNotification, &QComboBox::activated, this, &MyAccountPreferenceConfigureWidget::setWasChanged);
0200 }
0201 
0202 void MyAccountPreferenceConfigureWidget::save()
0203 {
0204     if (mChanged) {
0205         const QStringList highlightWords = mHighlightWords->text().split(QLatin1Char(','));
0206         QStringList listWords;
0207         for (QString word : highlightWords) {
0208             word = word.trimmed();
0209             if (!word.isEmpty()) {
0210                 listWords.append(word);
0211             }
0212         }
0213 
0214         RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo info;
0215         info.highlights = listWords;
0216         info.pushNotifications = mPushNotification->currentData().toString();
0217         info.desktopNotifications = mDesktopNotification->currentData().toString();
0218         info.emailNotificationMode = mEmailNotification->currentData().toString();
0219         info.userId = mRocketChatAccount->userId();
0220         info.useEmoji = RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToState(mUseEmojis->isChecked());
0221         info.hideRoles = RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToState(mHideRoles->isChecked());
0222         info.displayAvatars = RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToState(mDisplayAvatars->isChecked());
0223         info.convertAsciiToEmoji = RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToState(mConvertAsciiEmoji->isChecked());
0224         info.idleTimeLimit = mIdleTimeLimit->value();
0225         info.enableAutoAway = RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToState(mAutomaticAway->isChecked());
0226         if (mRocketChatAccount) {
0227             if (mRocketChatAccount->ruqolaServerConfig()->hasAtLeastVersion(5, 4, 0)) {
0228                 if (mRocketChatAccount->ruqolaServerConfig()->deviceManagementEnableLoginEmails()
0229                     && mRocketChatAccount->ruqolaServerConfig()->deviceManagementAllowLoginEmailpreference()) {
0230                     info.receiveLoginDetectionEmail =
0231                         RocketChatRestApi::UsersSetPreferencesJob::UsersSetPreferencesInfo::convertToState(mReceiveLoginDetectionEmails->isChecked());
0232                 }
0233             }
0234         }
0235         mRocketChatAccount->setUserPreferences(info);
0236     }
0237 }
0238 
0239 void MyAccountPreferenceConfigureWidget::load()
0240 {
0241     const OwnUserPreferences ownUserPreferences = mRocketChatAccount->ownUserPreferences();
0242     mHighlightWords->setText(ownUserPreferences.highlightWords().join(QLatin1Char(',')));
0243     mPushNotification->setCurrentIndex(mPushNotification->findData(ownUserPreferences.pushNotifications()));
0244     mEmailNotification->setCurrentIndex(mEmailNotification->findData(ownUserPreferences.emailNotificationMode()));
0245     mDesktopNotification->setCurrentIndex(mDesktopNotification->findData(ownUserPreferences.desktopNotifications()));
0246     mUseEmojis->setChecked(ownUserPreferences.useEmojis());
0247     mHideRoles->setChecked(ownUserPreferences.hideRoles());
0248     mDisplayAvatars->setChecked(ownUserPreferences.displayAvatars());
0249     mConvertAsciiEmoji->setChecked(ownUserPreferences.convertAsciiEmoji());
0250     mReceiveLoginDetectionEmails->setChecked(ownUserPreferences.receiveLoginDetectionEmail());
0251     mIdleTimeLimit->setValue(ownUserPreferences.idleTimeLimit());
0252     mAutomaticAway->setChecked(ownUserPreferences.enableAutoAway());
0253     mChanged = false;
0254 }
0255 
0256 void MyAccountPreferenceConfigureWidget::setWasChanged()
0257 {
0258     mChanged = true;
0259 }
0260 
0261 #include "moc_myaccountpreferenceconfigurewidget.cpp"