File indexing completed on 2024-12-01 04:36:53

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 "myaccountprofileconfigureavatarwidget.h"
0008 #include "rocketchataccount.h"
0009 #include <KLocalizedString>
0010 #include <QContextMenuEvent>
0011 #include <QFileDialog>
0012 #include <QHBoxLayout>
0013 #include <QImageReader>
0014 #include <QInputDialog>
0015 #include <QMenu>
0016 
0017 MyAccountProfileConfigureAvatarWidget::MyAccountProfileConfigureAvatarWidget(RocketChatAccount *account, QWidget *parent)
0018     : QWidget(parent)
0019     , mAvatarImage(new AvatarImage(account, this))
0020 {
0021     auto mainLayout = new QHBoxLayout(this);
0022     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0023     mAvatarImage->setObjectName(QStringLiteral("mAvatarImage"));
0024     mainLayout->addWidget(mAvatarImage, 0, Qt::AlignLeft);
0025 }
0026 
0027 MyAccountProfileConfigureAvatarWidget::~MyAccountProfileConfigureAvatarWidget() = default;
0028 
0029 void MyAccountProfileConfigureAvatarWidget::setCurrentIconPath(const QString &currentPath)
0030 {
0031     mAvatarImage->setCurrentIconPath(currentPath);
0032 }
0033 
0034 AvatarImage::AvatarImage(RocketChatAccount *account, QWidget *parent)
0035     : QPushButton(parent)
0036     , mRocketChatAccount(account)
0037 {
0038     setIconSize(QSize(100, 100));
0039     setFixedSize(QSize(120, 120));
0040 
0041     connect(this, &AvatarImage::clicked, this, &AvatarImage::changeImage);
0042     if (mRocketChatAccount) {
0043         connect(mRocketChatAccount, &RocketChatAccount::fileDownloaded, this, &AvatarImage::slotFileDownloaded);
0044     }
0045 }
0046 
0047 AvatarImage::~AvatarImage() = default;
0048 
0049 void AvatarImage::slotFileDownloaded(const QString &filePath, const QUrl &cacheImageUrl)
0050 {
0051     Q_UNUSED(filePath)
0052     Utils::AvatarInfo info;
0053     info.avatarType = Utils::AvatarType::User;
0054     info.identifier = mRocketChatAccount->ownUser().userName();
0055     const QUrl iconUrlStr = QUrl(mRocketChatAccount->avatarUrl(info));
0056     if (!iconUrlStr.isEmpty()) {
0057         if (iconUrlStr == cacheImageUrl) {
0058             setCurrentIconPath(cacheImageUrl.toLocalFile());
0059         }
0060     }
0061 }
0062 
0063 void AvatarImage::changeImage()
0064 {
0065     QString filter;
0066     const QList<QByteArray> supportedImage = QImageReader::supportedImageFormats();
0067     for (const QByteArray &ba : supportedImage) {
0068         if (!filter.isEmpty()) {
0069             filter += QLatin1Char(' ');
0070         }
0071         filter += QLatin1String("*.") + QString::fromLatin1(ba);
0072     }
0073     filter = QStringLiteral("%1 (%2)").arg(i18n("Image"), filter);
0074     const QUrl url = QFileDialog::getOpenFileUrl(this, i18n("Select Image"), {}, filter);
0075     if (!url.isEmpty()) {
0076         mRocketChatAccount->setImageUrl(url);
0077         setCurrentIconPath(url.toLocalFile());
0078     }
0079 }
0080 
0081 void AvatarImage::changeUrl()
0082 {
0083     bool ok = false;
0084     const QString url = QInputDialog::getText(this, i18n("Change Url"), i18n("Define Avatar Url:"), QLineEdit::Normal, {}, &ok);
0085     if (ok && !url.isEmpty()) {
0086         mRocketChatAccount->setAvatarUrl(url);
0087     }
0088 }
0089 
0090 void AvatarImage::resetAvatar()
0091 {
0092     mRocketChatAccount->resetAvatar();
0093 }
0094 
0095 void AvatarImage::contextMenuEvent(QContextMenuEvent *event)
0096 {
0097     QMenu menu;
0098     menu.addAction(i18n("Change Picture..."), this, &AvatarImage::changeImage);
0099     menu.addAction(i18n("Change URL for avatar..."), this, &AvatarImage::changeUrl);
0100     menu.addSeparator();
0101     menu.addAction(i18n("Reset Avatar"), this, &AvatarImage::resetAvatar);
0102     menu.exec(event->globalPos());
0103 }
0104 
0105 void AvatarImage::setCurrentIconPath(const QString &currentPath)
0106 {
0107     mCurrentIconPath = currentPath;
0108     setIcon(QIcon(mCurrentIconPath));
0109 }
0110 
0111 #include "moc_myaccountprofileconfigureavatarwidget.cpp"