Gravatar

gravatardownloadpixmapwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "gravatardownloadpixmapwidget.h"
8using namespace Qt::Literals::StringLiterals;
9
10#include "gravatar/gravatarresolvurljob.h"
11#include "gravatar_debug.h"
12#include <KLocalizedString>
13#include <QCheckBox>
14#include <QLabel>
15#include <QLineEdit>
16#include <QPushButton>
17#include <QVBoxLayout>
18
19using namespace Gravatar;
20GravatarDownloadPixmapWidget::GravatarDownloadPixmapWidget(QWidget *parent)
21 : QWidget(parent)
22{
23 auto mainLayout = new QVBoxLayout(this);
24 auto hbox = new QHBoxLayout;
25 mainLayout->addLayout(hbox);
26 auto lab = new QLabel(i18nc("@label:textbox", "Email:"), this);
27 lab->setObjectName("labemail"_L1);
28 hbox->addWidget(lab);
29 mLineEdit = new QLineEdit;
30 mLineEdit->setObjectName("email"_L1);
31 connect(mLineEdit, &QLineEdit::textChanged, this, &GravatarDownloadPixmapWidget::slotTextChanged);
32 hbox->addWidget(mLineEdit);
33
34 mUseLibravatar = new QCheckBox(i18nc("@option:check", "Use Libravatar"), this);
35 mUseLibravatar->setObjectName("uselibravatar"_L1);
36 mainLayout->addWidget(mUseLibravatar);
37
38 mFallbackGravatar = new QCheckBox(i18nc("@option:check", "Fallback to Gravatar"), this);
39 mFallbackGravatar->setObjectName("fallbackgravatar"_L1);
40 mainLayout->addWidget(mFallbackGravatar);
41
42 mGetPixmapButton = new QPushButton(i18nc("@action:button", "&Search"), this);
43 mGetPixmapButton->setObjectName("searchbutton"_L1);
44 connect(mGetPixmapButton, &QAbstractButton::clicked, this, &GravatarDownloadPixmapWidget::slotSearchButton);
45 hbox->addWidget(mGetPixmapButton);
46 mGetPixmapButton->setEnabled(false);
47
48 mResultLabel = new QLabel(this);
49 QFont font = mResultLabel->font();
50 font.setBold(true);
51 mResultLabel->setFont(font);
52 mResultLabel->setObjectName("resultlabel"_L1);
53 mainLayout->addWidget(mResultLabel);
54}
55
56GravatarDownloadPixmapWidget::~GravatarDownloadPixmapWidget() = default;
57
58QPixmap GravatarDownloadPixmapWidget::gravatarPixmap() const
59{
60 return mGravatarPixmap;
61}
62
63void GravatarDownloadPixmapWidget::slotResolvUrlFinish(Gravatar::GravatarResolvUrlJob *job)
64{
65 if (job) {
66 qCDebug(GRAVATAR_LOG) << job->hasGravatar();
67 if (job->hasGravatar()) {
68 mGravatarPixmap = job->pixmap();
69 mResultLabel->setPixmap(mGravatarPixmap);
70 } else {
71 mResultLabel->setText(i18n("No Gravatar found."));
72 mGravatarPixmap = QPixmap();
73 }
74 }
75}
76
77void GravatarDownloadPixmapWidget::slotSearchButton()
78{
79 mResultLabel->setText(QString());
80 auto job = new Gravatar::GravatarResolvUrlJob(this);
81 job->setEmail(mLineEdit->text());
82 // For testing
83 // job->setUseDefaultPixmap(true);
84 job->setUseLibravatar(mUseLibravatar->isChecked());
85 job->setFallbackGravatar(mFallbackGravatar->isChecked());
86 if (job->canStart()) {
87 connect(job, &GravatarResolvUrlJob::finished, this, &GravatarDownloadPixmapWidget::slotResolvUrlFinish);
88 job->start();
89 } else {
90 job->deleteLater();
91 }
92}
93
94void GravatarDownloadPixmapWidget::slotTextChanged(const QString &text)
95{
96 mGetPixmapButton->setEnabled(!text.trimmed().isEmpty());
97}
98
99#include "moc_gravatardownloadpixmapwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
bool isChecked() const const
void clicked(bool checked)
void addLayout(QLayout *layout, int stretch)
void setBold(bool enable)
void setPixmap(const QPixmap &)
void setText(const QString &)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void setObjectName(QAnyStringView name)
bool isEmpty() const const
QString trimmed() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
void setEnabled(bool)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:39:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.