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 "gravatarupdatedialog.h"
0008 #include "gravatarupdatewidget.h"
0009 #include <KConfigGroup>
0010 #include <KLocalizedString>
0011 #include <KSharedConfig>
0012 #include <KWindowConfig>
0013 #include <QDialogButtonBox>
0014 #include <QPushButton>
0015 #include <QVBoxLayout>
0016 #include <QWindow>
0017 
0018 using namespace KABGravatar;
0019 namespace
0020 {
0021 static const char myConfigGroupName[] = "GravatarUpdateDialog";
0022 }
0023 GravatarUpdateDialog::GravatarUpdateDialog(QWidget *parent)
0024     : QDialog(parent)
0025     , mGravatarUpdateWidget(new GravatarUpdateWidget(this))
0026     , mSaveImageButton(new QPushButton(i18n("Save Image"), this))
0027     , mSaveUrlButton(new QPushButton(i18n("Save Image URL"), this))
0028 {
0029     auto mainLayout = new QVBoxLayout(this);
0030     setWindowTitle(i18nc("@title:window", "Check and update Gravatar"));
0031     connect(mGravatarUpdateWidget, &GravatarUpdateWidget::activateDialogButton, this, &GravatarUpdateDialog::slotActivateButton);
0032     mGravatarUpdateWidget->setObjectName(QLatin1StringView("gravatarupdatewidget"));
0033     mainLayout->addWidget(mGravatarUpdateWidget);
0034     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Cancel, this);
0035     buttonBox->setObjectName(QLatin1StringView("buttonbox"));
0036     mSaveImageButton->setEnabled(false);
0037     buttonBox->addButton(mSaveImageButton, QDialogButtonBox::ActionRole);
0038     connect(mSaveImageButton, &QPushButton::clicked, this, &GravatarUpdateDialog::slotSaveImage);
0039 
0040     buttonBox->addButton(mSaveUrlButton, QDialogButtonBox::ActionRole);
0041     mSaveUrlButton->setEnabled(false);
0042     connect(mSaveUrlButton, &QPushButton::clicked, this, &GravatarUpdateDialog::slotSaveUrl);
0043 
0044     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0045     mainLayout->addWidget(buttonBox);
0046     readConfig();
0047 }
0048 
0049 GravatarUpdateDialog::~GravatarUpdateDialog()
0050 {
0051     writeConfig();
0052 }
0053 
0054 void GravatarUpdateDialog::slotActivateButton(bool state)
0055 {
0056     mSaveUrlButton->setEnabled(state);
0057     mSaveImageButton->setEnabled(state);
0058 }
0059 
0060 bool GravatarUpdateDialog::saveUrl() const
0061 {
0062     return mSaveUrl;
0063 }
0064 
0065 void GravatarUpdateDialog::slotSaveUrl()
0066 {
0067     mSaveUrl = true;
0068     accept();
0069 }
0070 
0071 void GravatarUpdateDialog::slotSaveImage()
0072 {
0073     mSaveUrl = false;
0074     accept();
0075 }
0076 
0077 void GravatarUpdateDialog::setEmail(const QString &email)
0078 {
0079     mGravatarUpdateWidget->setEmail(email);
0080 }
0081 
0082 QPixmap GravatarUpdateDialog::pixmap() const
0083 {
0084     return mGravatarUpdateWidget->pixmap();
0085 }
0086 
0087 void GravatarUpdateDialog::setOriginalUrl(const QUrl &url)
0088 {
0089     mGravatarUpdateWidget->setOriginalUrl(url);
0090 }
0091 
0092 QUrl GravatarUpdateDialog::resolvedUrl() const
0093 {
0094     return mGravatarUpdateWidget->resolvedUrl();
0095 }
0096 
0097 void GravatarUpdateDialog::setOriginalPixmap(const QPixmap &pix)
0098 {
0099     mGravatarUpdateWidget->setOriginalPixmap(pix);
0100 }
0101 
0102 void GravatarUpdateDialog::readConfig()
0103 {
0104     create(); // ensure a window is created
0105     windowHandle()->resize(QSize(300, 200));
0106     KConfigGroup group(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
0107     KWindowConfig::restoreWindowSize(windowHandle(), group);
0108     resize(windowHandle()->size()); // workaround for QTBUG-40584
0109 }
0110 
0111 void GravatarUpdateDialog::writeConfig()
0112 {
0113     KConfigGroup grp(KSharedConfig::openStateConfig(), QLatin1StringView(myConfigGroupName));
0114     KWindowConfig::saveWindowSize(windowHandle(), grp);
0115     grp.sync();
0116 }
0117 
0118 #include "moc_gravatarupdatedialog.cpp"