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

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 "accountservicetogglejob.h"
0008 
0009 #include "core.h"
0010 #include <Accounts/Manager>
0011 #include <QDebug>
0012 
0013 namespace KAccounts
0014 {
0015 
0016 class AccountServiceToggleJob::Private
0017 {
0018 public:
0019     Private()
0020     {
0021     }
0022     QString accountId;
0023     QString serviceId;
0024     bool serviceEnabled{false};
0025 };
0026 
0027 AccountServiceToggleJob::AccountServiceToggleJob(QObject *parent)
0028     : KJob(parent)
0029     , d(new Private)
0030 {
0031 }
0032 
0033 AccountServiceToggleJob::~AccountServiceToggleJob()
0034 {
0035     delete d;
0036 }
0037 
0038 QString AccountServiceToggleJob::accountId() const
0039 {
0040     return d->accountId;
0041 }
0042 
0043 void AccountServiceToggleJob::setAccountId(const QString &accountId)
0044 {
0045     d->accountId = accountId;
0046     Q_EMIT accountIdChanged();
0047 }
0048 
0049 QString AccountServiceToggleJob::serviceId() const
0050 {
0051     return d->serviceId;
0052 }
0053 
0054 void AccountServiceToggleJob::setServiceId(const QString &serviceId)
0055 {
0056     d->serviceId = serviceId;
0057     Q_EMIT serviceIdChanged();
0058 }
0059 
0060 bool AccountServiceToggleJob::serviceEnabled() const
0061 {
0062     return d->serviceEnabled;
0063 }
0064 
0065 void AccountServiceToggleJob::setServiceEnabled(bool serviceEnabled)
0066 {
0067     d->serviceEnabled = serviceEnabled;
0068     Q_EMIT serviceEnabledChanged();
0069 }
0070 
0071 void AccountServiceToggleJob::start()
0072 {
0073     Accounts::Manager *accountsManager = KAccounts::accountsManager();
0074     if (accountsManager) {
0075         Accounts::Account *account = accountsManager->account(d->accountId.toInt());
0076         if (account) {
0077             Accounts::Service service = accountsManager->service(d->serviceId);
0078             if (!service.isValid()) {
0079                 // qWarning() << "Looks like we might have been given a name instead of an ID for the service, which will be expected when using the Ubuntu
0080                 // AccountServiceModel, which only gives you the name";
0081                 const auto services = account->services();
0082                 for (const Accounts::Service &aService : services) {
0083                     if (aService.displayName() == d->serviceId) {
0084                         service = aService;
0085                         break;
0086                     }
0087                 }
0088             }
0089             if (service.isValid()) {
0090                 account->selectService(service);
0091                 account->setEnabled(d->serviceEnabled);
0092 
0093                 if (d->serviceEnabled) {
0094                     account->selectService();
0095                     account->setEnabled(true);
0096                 } else {
0097                     bool shouldStayEnabled = false;
0098                     const auto services = account->services();
0099                     for (const Accounts::Service &accountService : services) {
0100                         // Skip the current service, that is not synced to the account yet
0101                         // so it would return the state before the user clicked the checkbox
0102                         if (accountService == service) {
0103                             continue;
0104                         }
0105                         account->selectService(accountService);
0106                         if (account->isEnabled()) {
0107                             // At least one service is enabled, leave the account enabled
0108                             shouldStayEnabled = true;
0109                             break;
0110                         }
0111                     }
0112 
0113                     // Make sure we're operating on the global account
0114                     account->selectService();
0115                     account->setEnabled(shouldStayEnabled);
0116                 }
0117 
0118                 connect(account, &Accounts::Account::synced, this, [this]() {
0119                     emitResult();
0120                 });
0121                 account->sync();
0122             } else {
0123                 qWarning() << "No service found with the ID" << d->serviceId << "on account" << account->displayName();
0124                 emitResult();
0125             }
0126         } else {
0127             qWarning() << "No account found with the ID" << d->accountId;
0128             emitResult();
0129         }
0130     } else {
0131         qWarning() << "No accounts manager, this is not awesome.";
0132         emitResult();
0133     }
0134 }
0135 }