This repository has been archived by the owner on Nov 8, 2021. It is now read-only.
forked from KDE/kio-gdrive
-
Notifications
You must be signed in to change notification settings - Fork 4
/
kaccountsmanager.cpp
161 lines (134 loc) · 5.05 KB
/
kaccountsmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2017 Elvis Angelaccio <elvis.angelaccio@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "kaccountsmanager.h"
#include "onedrivedebug.h"
#include <Accounts/Manager>
#include <Accounts/Provider>
#include <KAccounts/Core>
#include <KAccounts/GetCredentialsJob>
#include <KGAPI/Account>
#include <QProcess>
#include <QStandardPaths>
using namespace KGAPI2;
KAccountsManager::KAccountsManager()
{
loadAccounts();
}
KAccountsManager::~KAccountsManager()
{}
AccountPtr KAccountsManager::account(const QString &accountName)
{
const auto accounts = m_accounts.values();
for (const auto &account : accounts) {
if (account->accountName() == accountName) {
return account;
}
}
return AccountPtr(new Account());
}
AccountPtr KAccountsManager::createAccount()
{
if (QStandardPaths::findExecutable(QStringLiteral("kcmshell5")).isEmpty()) {
return AccountPtr(new Account());
}
const auto oldAccounts = accounts();
QProcess process;
process.start(QStringLiteral("kcmshell5"), {QStringLiteral("kcm_kaccounts")});
qCDebug(ONEDRIVE) << "Waiting for kcmshell process...";
if (process.waitForFinished(-1)) {
loadAccounts();
}
const auto newAccounts = accounts();
for (const auto &accountName : newAccounts) {
if (oldAccounts.contains(accountName)) {
continue;
}
// The KCM allows to add more than one account, but we can return only one from here.
// So we just return the first new account in the set.
qCDebug(ONEDRIVE) << "New account successfully created:" << accountName;
return account(accountName);
}
// No accounts at all or no new account(s).
qCDebug(ONEDRIVE) << "No new account created.";
return AccountPtr(new Account());
}
AccountPtr KAccountsManager::refreshAccount(const AccountPtr &account)
{
Q_UNUSED(account)
qCWarning(ONEDRIVE) << Q_FUNC_INFO << "not implemented.";
return {};
}
void KAccountsManager::removeAccount(const QString &accountName)
{
if (!accounts().contains(accountName)) {
return;
}
for (auto it = m_accounts.constBegin(); it != m_accounts.constEnd(); ++it) {
if (it.value()->accountName() != accountName) {
continue;
}
auto manager = KAccounts::accountsManager();
auto account = Accounts::Account::fromId(manager, it.key());
Q_ASSERT(account->displayName() == accountName);
qCDebug(ONEDRIVE) << "Going to remove account:" << account->displayName();
account->selectService(manager->service(QStringLiteral("microsoft-onedrive")));
account->setEnabled(false);
account->sync();
return;
}
}
QSet<QString> KAccountsManager::accounts()
{
auto accountNames = QSet<QString>();
const auto accounts = m_accounts.values();
for (const auto &account : accounts) {
accountNames << account->accountName();
}
return accountNames;
}
void KAccountsManager::loadAccounts()
{
m_accounts.clear();
auto manager = KAccounts::accountsManager();
const auto enabledIDs = manager->accountListEnabled();
for (const auto id : enabledIDs) {
auto account = manager->account(id);
if (account->providerName() != QLatin1String("microsoft")) {
continue;
}
qCDebug(ONEDRIVE) << "Found microsoft-provided account:" << account->displayName();
const auto services = account->enabledServices();
for (const auto &service : services) {
if (service.name() != QLatin1String("microsoft-onedrive")) {
continue;
}
qCDebug(ONEDRIVE) << account->displayName() << "supports onedrive!";
auto job = new GetCredentialsJob(id, nullptr);
job->exec();
auto gapiAccount = AccountPtr(new Account(account->displayName(),
job->credentialsData().value(QStringLiteral("AccessToken")).toString(),
job->credentialsData().value(QStringLiteral("RefreshToken")).toString()));
const auto scopes = job->credentialsData().value(QStringLiteral("Scope")).toStringList();
for (const auto &scope : scopes) {
gapiAccount->addScope(QUrl::fromUserInput(scope));
}
m_accounts.insert(id, gapiAccount);
}
}
}