File indexing completed on 2024-12-01 04:46:41
0001 /* 0002 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "gravatarupdatewidget.h" 0008 0009 #include <KLocalizedString> 0010 #include <QCheckBox> 0011 #include <QGridLayout> 0012 #include <QLabel> 0013 #include <QPushButton> 0014 0015 #include <Gravatar/GravatarResolvUrlJob> 0016 0017 #include <KIO/TransferJob> 0018 0019 using namespace KABGravatar; 0020 GravatarUpdateWidget::GravatarUpdateWidget(QWidget *parent) 0021 : QWidget(parent) 0022 , mEmailLab(new QLabel(this)) 0023 , mSearchGravatar(new QPushButton(i18n("Search"), this)) 0024 , mResultGravatar(new QLabel(this)) 0025 { 0026 auto mainLayout = new QGridLayout(this); 0027 mainLayout->setContentsMargins({}); 0028 0029 auto hboxEmail = new QHBoxLayout; 0030 0031 auto lab = new QLabel(i18n("Email:"), this); 0032 lab->setObjectName(QLatin1StringView("emaillabel")); 0033 hboxEmail->addWidget(lab); 0034 0035 mEmailLab->setObjectName(QLatin1StringView("email")); 0036 hboxEmail->addWidget(mEmailLab); 0037 mainLayout->addLayout(hboxEmail, 0, 0); 0038 0039 mSearchGravatar->setEnabled(false); 0040 mSearchGravatar->setObjectName(QLatin1StringView("search")); 0041 mainLayout->addWidget(mSearchGravatar, 4, 0); 0042 connect(mSearchGravatar, &QAbstractButton::clicked, this, &GravatarUpdateWidget::slotSearchGravatar); 0043 QFont font = mResultGravatar->font(); 0044 font.setBold(true); 0045 mResultGravatar->setFont(font); 0046 0047 mResultGravatar->setObjectName(QLatin1StringView("result")); 0048 mainLayout->addWidget(mResultGravatar, 0, 2, 4, 1, Qt::AlignCenter); 0049 } 0050 0051 GravatarUpdateWidget::~GravatarUpdateWidget() = default; 0052 0053 void GravatarUpdateWidget::setEmail(const QString &email) 0054 { 0055 mEmail = email; 0056 mEmailLab->setText(mEmail); 0057 mResultGravatar->setText(QString()); 0058 mSearchGravatar->setEnabled(!mEmail.trimmed().isEmpty()); 0059 } 0060 0061 QPixmap GravatarUpdateWidget::pixmap() const 0062 { 0063 return mPixmap; 0064 } 0065 0066 void GravatarUpdateWidget::setOriginalUrl(const QUrl &url) 0067 { 0068 QImage image; 0069 QByteArray imageData; 0070 KIO::TransferJob *job = KIO::get(url, KIO::NoReload); 0071 QObject::connect(job, &KIO::TransferJob::data, [&imageData](KIO::Job *, const QByteArray &data) { 0072 imageData.append(data); 0073 }); 0074 if (job->exec()) { 0075 if (image.loadFromData(imageData)) { 0076 mResultGravatar->setPixmap(QPixmap::fromImage(image)); 0077 } 0078 } 0079 } 0080 0081 void GravatarUpdateWidget::setOriginalPixmap(const QPixmap &pix) 0082 { 0083 if (!pix.isNull()) { 0084 mResultGravatar->setPixmap(pix); 0085 } 0086 } 0087 0088 QUrl GravatarUpdateWidget::resolvedUrl() const 0089 { 0090 return mCurrentUrl; 0091 } 0092 0093 void GravatarUpdateWidget::slotSearchGravatar() 0094 { 0095 mCurrentUrl.clear(); 0096 if (!mEmail.isEmpty()) { 0097 auto job = new Gravatar::GravatarResolvUrlJob(this); 0098 job->setEmail(mEmail); 0099 if (job->canStart()) { 0100 job->setUseDefaultPixmap(false); 0101 connect(job, &Gravatar::GravatarResolvUrlJob::finished, this, &GravatarUpdateWidget::slotSearchGravatarFinished); 0102 connect(job, &Gravatar::GravatarResolvUrlJob::resolvUrl, this, &GravatarUpdateWidget::slotResolvUrl); 0103 mSearchGravatar->setEnabled(false); 0104 Q_EMIT activateDialogButton(false); 0105 mPixmap = QPixmap(); 0106 mCurrentUrl.clear(); 0107 job->start(); 0108 } else { 0109 mResultGravatar->setText(i18n("Search is impossible.")); 0110 job->deleteLater(); 0111 } 0112 } 0113 } 0114 0115 void GravatarUpdateWidget::slotResolvUrl(const QUrl &url) 0116 { 0117 mCurrentUrl = url; 0118 } 0119 0120 void GravatarUpdateWidget::slotSearchGravatarFinished(Gravatar::GravatarResolvUrlJob *job) 0121 { 0122 bool foundGravatar = false; 0123 if (job && job->hasGravatar()) { 0124 mPixmap = job->pixmap(); 0125 mResultGravatar->setPixmap(mPixmap); 0126 foundGravatar = true; 0127 } else { 0128 mResultGravatar->setText(i18n("No Gravatar Found.")); 0129 } 0130 Q_EMIT activateDialogButton(foundGravatar); 0131 mSearchGravatar->setEnabled(true); 0132 } 0133 0134 #include "moc_gravatarupdatewidget.cpp"