Changeset View
Changeset View
Standalone View
Standalone View
src/lib/changeaccountdisplaynamejob.cpp
- This file was added.
1 | /************************************************************************************* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2020 by Dan Leinir Turthra Jensen <admin@leinir.dk> * | ||||
3 | * * | ||||
4 | * This program is free software; you can redistribute it and/or * | ||||
5 | * modify it under the terms of the GNU General Public License * | ||||
6 | * as published by the Free Software Foundation; either version 2 * | ||||
7 | * of the License, or (at your option) any later version. * | ||||
8 | * * | ||||
9 | * This program is distributed in the hope that it will be useful, * | ||||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
12 | * GNU General Public License for more details. * | ||||
13 | * * | ||||
14 | * You should have received a copy of the GNU General Public License * | ||||
15 | * along with this program; if not, write to the Free Software * | ||||
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * | ||||
17 | *************************************************************************************/ | ||||
18 | | ||||
19 | #include "changeaccountdisplaynamejob.h" | ||||
20 | | ||||
21 | #include "core.h" | ||||
22 | #include <Accounts/Manager> | ||||
23 | #include <KLocalizedString> | ||||
24 | #include <QDebug> | ||||
25 | | ||||
26 | class ChangeAccountDisplayNameJob::Private { | ||||
27 | public: | ||||
28 | Private() {} | ||||
29 | QString accountId; | ||||
30 | QString displayName; | ||||
31 | }; | ||||
32 | | ||||
33 | ChangeAccountDisplayNameJob::ChangeAccountDisplayNameJob(QObject* parent) | ||||
34 | : KJob(parent) | ||||
35 | , d(new Private) | ||||
36 | { } | ||||
37 | | ||||
38 | ChangeAccountDisplayNameJob::~ChangeAccountDisplayNameJob() | ||||
39 | { | ||||
40 | delete d; | ||||
41 | } | ||||
42 | | ||||
43 | QString ChangeAccountDisplayNameJob::accountId() const | ||||
44 | { | ||||
45 | return d->accountId; | ||||
46 | } | ||||
47 | | ||||
48 | void ChangeAccountDisplayNameJob::setAccountId(const QString& accountId) | ||||
49 | { | ||||
50 | d->accountId = accountId; | ||||
51 | Q_EMIT accountIdChanged(); | ||||
52 | } | ||||
53 | | ||||
54 | QString ChangeAccountDisplayNameJob::displayName() const | ||||
55 | { | ||||
56 | return d->displayName; | ||||
57 | } | ||||
58 | | ||||
59 | void ChangeAccountDisplayNameJob::setDisplayName(const QString& displayName) | ||||
60 | { | ||||
61 | d->displayName = displayName; | ||||
62 | Q_EMIT displayNameChanged(); | ||||
63 | } | ||||
64 | | ||||
65 | void ChangeAccountDisplayNameJob::start() | ||||
66 | { | ||||
67 | if (!d->displayName.isEmpty()) { | ||||
68 | Accounts::Manager* accountsManager = KAccounts::accountsManager(); | ||||
69 | if (accountsManager) { | ||||
70 | Accounts::Account *account = accountsManager->account(d->accountId.toInt()); | ||||
71 | if (account) { | ||||
72 | account->setDisplayName(d->displayName); | ||||
73 | connect(account, &Accounts::Account::synced, this, [this](){ emitResult(); }); | ||||
74 | account->sync(); | ||||
75 | } else { | ||||
76 | qWarning() << "No account found with the ID" << d->accountId; | ||||
77 | setErrorText(i18n("No account found with the ID %1").arg(d->accountId)); | ||||
78 | emitResult(); | ||||
79 | } | ||||
80 | } else { | ||||
81 | qWarning() << "No accounts manager, this is not awesome."; | ||||
82 | setErrorText(i18n("No accounts manager, this is not awesome.")); | ||||
83 | emitResult(); | ||||
84 | } | ||||
85 | } else { | ||||
86 | qWarning() << "Setting an account display name to empty is a terrible idea, and we refuse to do that"; | ||||
87 | setErrorText(i18n("The display name cannot be empty")); | ||||
88 | emitResult(); | ||||
89 | } | ||||
90 | } |