File indexing completed on 2024-05-19 15:57:54

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "changeaccountdisplaynamejob.h"
0008 
0009 #include "core.h"
0010 #include <Accounts/Manager>
0011 #include <KLocalizedString>
0012 #include <QDebug>
0013 
0014 namespace KAccounts
0015 {
0016 
0017 class ChangeAccountDisplayNameJob::Private
0018 {
0019 public:
0020     Private()
0021     {
0022     }
0023     QString accountId;
0024     QString displayName;
0025 };
0026 
0027 ChangeAccountDisplayNameJob::ChangeAccountDisplayNameJob(QObject *parent)
0028     : KJob(parent)
0029     , d(new Private)
0030 {
0031 }
0032 
0033 ChangeAccountDisplayNameJob::~ChangeAccountDisplayNameJob()
0034 {
0035     delete d;
0036 }
0037 
0038 QString ChangeAccountDisplayNameJob::accountId() const
0039 {
0040     return d->accountId;
0041 }
0042 
0043 void ChangeAccountDisplayNameJob::setAccountId(const QString &accountId)
0044 {
0045     d->accountId = accountId;
0046     Q_EMIT accountIdChanged();
0047 }
0048 
0049 QString ChangeAccountDisplayNameJob::displayName() const
0050 {
0051     return d->displayName;
0052 }
0053 
0054 void ChangeAccountDisplayNameJob::setDisplayName(const QString &displayName)
0055 {
0056     d->displayName = displayName;
0057     Q_EMIT displayNameChanged();
0058 }
0059 
0060 void ChangeAccountDisplayNameJob::start()
0061 {
0062     if (!d->displayName.isEmpty()) {
0063         Accounts::Manager *accountsManager = KAccounts::accountsManager();
0064         if (accountsManager) {
0065             Accounts::Account *account = accountsManager->account(d->accountId.toInt());
0066             if (account) {
0067                 account->setDisplayName(d->displayName);
0068                 connect(account, &Accounts::Account::synced, this, [this]() {
0069                     emitResult();
0070                 });
0071                 account->sync();
0072             } else {
0073                 qWarning() << "No account found with the ID" << d->accountId;
0074                 setErrorText(i18n("No account found with the ID %1").arg(d->accountId));
0075                 emitResult();
0076             }
0077         } else {
0078             qWarning() << "No accounts manager, this is not awesome.";
0079             setErrorText(i18n("No accounts manager, this is not awesome."));
0080             emitResult();
0081         }
0082     } else {
0083         qWarning() << "Setting an account display name to empty is a terrible idea, and we refuse to do that";
0084         setErrorText(i18n("The display name cannot be empty"));
0085         emitResult();
0086     }
0087 }
0088 
0089 };