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

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 "myaccountprofileconfigurewidget.h"
0008 #include "dialogs/asktwoauthenticationpassworddialog.h"
0009 #include "misc/lineeditcatchreturnkey.h"
0010 #include "misc/passwordconfirmwidget.h"
0011 #include "myaccountprofileconfigureavatarwidget.h"
0012 #include "rocketchataccount.h"
0013 #include <KAuthorized>
0014 #include <KLocalizedString>
0015 #include <KMessageBox>
0016 #include <KPasswordDialog>
0017 #include <QFormLayout>
0018 #include <QLabel>
0019 #include <QLineEdit>
0020 #include <QPointer>
0021 #include <QPushButton>
0022 
0023 MyAccountProfileConfigureWidget::MyAccountProfileConfigureWidget(RocketChatAccount *account, QWidget *parent)
0024     : QWidget(parent)
0025     , mEmail(new QLineEdit(this))
0026     , mEmailInfo(new QLabel(i18n("Your administrator has disabled the changing of email."), this))
0027     , mName(new QLineEdit(this))
0028     , mUserName(new QLineEdit(this))
0029     , mNickName(new QLineEdit(this))
0030     , mStatusText(new QLineEdit(this))
0031     , mDeleteMyAccount(new QPushButton(i18n("Delete my Account"), this))
0032     , mLogoutFromOtherLocation(new QPushButton(i18n("Logout From Other Logged In Locations"), this))
0033     , mPasswordConfirmWidget(new PasswordConfirmWidget(this))
0034     , mConfigureAvatarWidget(new MyAccountProfileConfigureAvatarWidget(account, this))
0035     , mRocketChatAccount(account)
0036 {
0037     auto topLayout = new QVBoxLayout(this);
0038     topLayout->setObjectName(QStringLiteral("topLayout"));
0039     topLayout->setContentsMargins({});
0040 
0041     mConfigureAvatarWidget->setObjectName(QStringLiteral("mConfigureAvatarWidget"));
0042     topLayout->addWidget(mConfigureAvatarWidget);
0043 
0044     auto mainLayout = new QFormLayout;
0045     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0046     topLayout->addLayout(mainLayout);
0047 
0048     mName->setObjectName(QStringLiteral("mName"));
0049     new LineEditCatchReturnKey(mName, this);
0050     mainLayout->addRow(i18n("Name:"), mName);
0051     mName->setClearButtonEnabled(true);
0052 
0053     mUserName->setObjectName(QStringLiteral("mUserName"));
0054     new LineEditCatchReturnKey(mUserName, this);
0055     mainLayout->addRow(i18n("Username:"), mUserName);
0056 
0057     mEmail->setObjectName(QStringLiteral("mEmail"));
0058     new LineEditCatchReturnKey(mEmail, this);
0059     mainLayout->addRow(i18n("Email:"), mEmail);
0060 
0061     mEmailInfo->setObjectName(QStringLiteral("mEmailInfo"));
0062     mainLayout->addWidget(mEmailInfo);
0063 
0064     mNickName->setObjectName(QStringLiteral("mNickName"));
0065     new LineEditCatchReturnKey(mNickName, this);
0066     mainLayout->addRow(i18n("Nickname:"), mNickName);
0067     mNickName->setClearButtonEnabled(true);
0068 
0069     new LineEditCatchReturnKey(mStatusText, this);
0070     mStatusText->setObjectName(QStringLiteral("mStatusText"));
0071     mainLayout->addRow(i18n("Status text:"), mStatusText);
0072     mStatusText->setClearButtonEnabled(true);
0073 
0074     mPasswordConfirmWidget->setObjectName(QStringLiteral("mPasswordConfirmWidget"));
0075     mainLayout->addRow(mPasswordConfirmWidget);
0076 
0077     mDeleteMyAccount->setObjectName(QStringLiteral("mDeleteMyAccount"));
0078     mainLayout->addWidget(mDeleteMyAccount);
0079     connect(mDeleteMyAccount, &QPushButton::clicked, this, &MyAccountProfileConfigureWidget::slotDeleteMyAccount);
0080 
0081     mLogoutFromOtherLocation->setObjectName(QStringLiteral("mLogoutFromOtherLocation"));
0082     mainLayout->addWidget(mLogoutFromOtherLocation);
0083     connect(mLogoutFromOtherLocation, &QPushButton::clicked, this, &MyAccountProfileConfigureWidget::slotLogoutFromOtherLocation);
0084     topLayout->addStretch();
0085 }
0086 
0087 MyAccountProfileConfigureWidget::~MyAccountProfileConfigureWidget() = default;
0088 
0089 void MyAccountProfileConfigureWidget::slotLogoutFromOtherLocation()
0090 {
0091     mRocketChatAccount->logoutFromOtherLocation();
0092 }
0093 
0094 void MyAccountProfileConfigureWidget::slotDeleteMyAccount()
0095 {
0096     if (KMessageBox::ButtonCode::PrimaryAction
0097         == KMessageBox::questionTwoActions(this,
0098                                            i18n("Do you really delete your account ?"),
0099                                            i18nc("@title", "Delete my Account"),
0100                                            KStandardGuiItem::del(),
0101                                            KStandardGuiItem::cancel())) {
0102         QPointer<KPasswordDialog> dlg = new KPasswordDialog(this);
0103         dlg->setRevealPasswordAvailable(KAuthorized::authorize(QStringLiteral("lineedit_reveal_password")));
0104         dlg->setPrompt(i18n("Current Password"));
0105         if (dlg->exec()) {
0106             mRocketChatAccount->deleteOwnAccount(dlg->password());
0107         }
0108         delete dlg;
0109     }
0110 }
0111 
0112 void MyAccountProfileConfigureWidget::initialize()
0113 {
0114     mUserName->setReadOnly(!mRocketChatAccount->allowUsernameChange());
0115     mEmail->setReadOnly(!mRocketChatAccount->allowEmailChange());
0116     mEmailInfo->setVisible(!mRocketChatAccount->allowEmailChange());
0117 
0118     mPasswordConfirmWidget->setVisible(mRocketChatAccount->allowPasswordChange());
0119     mDeleteMyAccount->setVisible(mRocketChatAccount->allowDeleteOwnAccount());
0120     mConfigureAvatarWidget->setVisible(mRocketChatAccount->allowAvatarChanged());
0121 }
0122 
0123 void MyAccountProfileConfigureWidget::load()
0124 {
0125     mOwnUser = mRocketChatAccount->ownUser();
0126     mEmail->setText(mOwnUser.email());
0127     mName->setText(mOwnUser.name());
0128     mUserName->setText(mOwnUser.userName());
0129     mNickName->setText(mOwnUser.nickName());
0130     mStatusText->setText(mOwnUser.statusText());
0131     Utils::AvatarInfo info;
0132     info.avatarType = Utils::AvatarType::User;
0133     info.identifier = mOwnUser.userName();
0134     const QUrl iconUrlStr = QUrl(mRocketChatAccount->avatarUrl(info));
0135     if (!iconUrlStr.isEmpty()) {
0136         const QString iconPath{QUrl(iconUrlStr).toLocalFile()};
0137         mConfigureAvatarWidget->setCurrentIconPath(iconPath);
0138     }
0139 }
0140 
0141 void MyAccountProfileConfigureWidget::save()
0142 {
0143     RocketChatRestApi::UsersUpdateOwnBasicInfoJob::UpdateOwnBasicInfo updateInfo;
0144     if (!mEmail->isReadOnly() && (mOwnUser.email() != mEmail->text())) {
0145         updateInfo.type |= RocketChatRestApi::UsersUpdateOwnBasicInfoJob::UpdateOwnBasicInfo::BasicInfoType::Email;
0146         updateInfo.email = mEmail->text();
0147     }
0148     if (!mNickName->isReadOnly() && (mOwnUser.nickName() != mNickName->text())) {
0149         updateInfo.type |= RocketChatRestApi::UsersUpdateOwnBasicInfoJob::UpdateOwnBasicInfo::BasicInfoType::NickName;
0150         updateInfo.nickName = mNickName->text();
0151     }
0152     if (!mUserName->isReadOnly() && (mOwnUser.userName() != mUserName->text())) {
0153         updateInfo.type |= RocketChatRestApi::UsersUpdateOwnBasicInfoJob::UpdateOwnBasicInfo::BasicInfoType::UserName;
0154         updateInfo.userName = mUserName->text();
0155     }
0156     if (!mStatusText->isReadOnly() && (mOwnUser.statusText() != mStatusText->text())) {
0157         updateInfo.type |= RocketChatRestApi::UsersUpdateOwnBasicInfoJob::UpdateOwnBasicInfo::BasicInfoType::StatusText;
0158         updateInfo.statusText = mStatusText->text();
0159     }
0160     if (!mName->isReadOnly() && (mOwnUser.name() != mName->text())) {
0161         updateInfo.type |= RocketChatRestApi::UsersUpdateOwnBasicInfoJob::UpdateOwnBasicInfo::BasicInfoType::Name;
0162         updateInfo.name = mName->text();
0163     }
0164     if (mPasswordConfirmWidget->isVisible() && mPasswordConfirmWidget->isNewPasswordConfirmed()) {
0165         updateInfo.type |= RocketChatRestApi::UsersUpdateOwnBasicInfoJob::UpdateOwnBasicInfo::BasicInfoType::Password;
0166         updateInfo.newPassword = mPasswordConfirmWidget->password(); // Not encrypt it ???!
0167         QPointer<KPasswordDialog> dlg = new KPasswordDialog(this);
0168         dlg->setPrompt(i18n("Current Password"));
0169         if (dlg->exec()) {
0170             updateInfo.currentPassword = Utils::convertSha256Password(dlg->password());
0171         } else {
0172             delete dlg;
0173             return;
0174         }
0175         delete dlg;
0176     }
0177     if (mRocketChatAccount->ownUser().servicePassword().email2faEnabled()) { // TODO verify it
0178         QPointer<AskTwoAuthenticationPasswordDialog> dlg = new AskTwoAuthenticationPasswordDialog(this);
0179         dlg->setRocketChatAccount(mRocketChatAccount);
0180         QString code;
0181         if (dlg->exec()) {
0182             code = dlg->code();
0183             qWarning() << " Code not used yet ! Implement it";
0184             // TODO use code !
0185         }
0186         delete dlg;
0187     }
0188 
0189     // TODO add more.
0190     if (updateInfo.isValid()) {
0191         mRocketChatAccount->updateOwnBasicInfo(updateInfo);
0192     }
0193 }
0194 
0195 #include "moc_myaccountprofileconfigurewidget.cpp"