accountservicetogglejob.cpp 3.99 KB
Newer Older
1
2
3
4
5
/*
 *  SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <admin@leinir.dk>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */
6

7
#include "accountservicetogglejob.h"
8

Nicolas Fella's avatar
Nicolas Fella committed
9
#include "core.h"
10
#include <Accounts/Manager>
11
#include <QDebug>
12

13
14
class AccountServiceToggleJob::Private
{
15
public:
16
17
18
    Private()
    {
    }
19
20
21
22
23
    QString accountId;
    QString serviceId;
    bool serviceEnabled{false};
};

24
AccountServiceToggleJob::AccountServiceToggleJob(QObject *parent)
25
26
    : KJob(parent)
    , d(new Private)
27
28
{
}
29

30
AccountServiceToggleJob::~AccountServiceToggleJob()
31
32
33
34
{
    delete d;
}

35
QString AccountServiceToggleJob::accountId() const
36
37
38
39
{
    return d->accountId;
}

40
void AccountServiceToggleJob::setAccountId(const QString &accountId)
41
42
43
44
45
{
    d->accountId = accountId;
    Q_EMIT accountIdChanged();
}

46
QString AccountServiceToggleJob::serviceId() const
47
48
49
50
{
    return d->serviceId;
}

51
void AccountServiceToggleJob::setServiceId(const QString &serviceId)
52
53
54
55
56
{
    d->serviceId = serviceId;
    Q_EMIT serviceIdChanged();
}

57
bool AccountServiceToggleJob::serviceEnabled() const
58
59
60
61
{
    return d->serviceEnabled;
}

62
void AccountServiceToggleJob::setServiceEnabled(bool serviceEnabled)
63
64
65
66
67
{
    d->serviceEnabled = serviceEnabled;
    Q_EMIT serviceEnabledChanged();
}

68
void AccountServiceToggleJob::start()
69
{
70
    Accounts::Manager *accountsManager = KAccounts::accountsManager();
71
72
73
74
75
    if (accountsManager) {
        Accounts::Account *account = accountsManager->account(d->accountId.toInt());
        if (account) {
            Accounts::Service service = accountsManager->service(d->serviceId);
            if (!service.isValid()) {
Nicolas Fella's avatar
Nicolas Fella committed
76
77
                // 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
                // AccountServiceModel, which only gives you the name";
Nicolas Fella's avatar
Nicolas Fella committed
78
                const auto services = account->services();
79
                for (const Accounts::Service &aService : services) {
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                    if (aService.displayName() == d->serviceId) {
                        service = aService;
                        break;
                    }
                }
            }
            if (service.isValid()) {
                account->selectService(service);
                account->setEnabled(d->serviceEnabled);

                if (d->serviceEnabled) {
                    account->selectService();
                    account->setEnabled(true);
                } else {
                    bool shouldStayEnabled = false;
Nicolas Fella's avatar
Nicolas Fella committed
95
96
                    const auto services = account->services();
                    for (const Accounts::Service &accountService : services) {
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
                        // Skip the current service, that is not synced to the account yet
                        // so it would return the state before the user clicked the checkbox
                        if (accountService == service) {
                            continue;
                        }
                        account->selectService(accountService);
                        if (account->isEnabled()) {
                            // At least one service is enabled, leave the account enabled
                            shouldStayEnabled = true;
                            break;
                        }
                    }

                    // Make sure we're operating on the global account
                    account->selectService();
                    account->setEnabled(shouldStayEnabled);
                }

115
116
117
                connect(account, &Accounts::Account::synced, this, [this]() {
                    emitResult();
                });
118
119
120
121
122
123
124
125
126
127
128
129
130
131
                account->sync();
            } else {
                qWarning() << "No service found with the ID" << d->serviceId << "on account" << account->displayName();
                emitResult();
            }
        } else {
            qWarning() << "No account found with the ID" << d->accountId;
            emitResult();
        }
    } else {
        qWarning() << "No accounts manager, this is not awesome.";
        emitResult();
    }
}