Changeset View
Changeset View
Standalone View
Standalone View
resources/google-groupware/googlesettings.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2011-2013 Dan Vratil <dan@progdan.cz> | ||||
3 | 2020 Igor Poboiko <igor.poboiko@gmail.com> | ||||
4 | | ||||
5 | This program is free software: you can redistribute it and/or modify | ||||
6 | it under the terms of the GNU General Public License as published by | ||||
7 | the Free Software Foundation, either version 3 of the License, or | ||||
8 | (at your option) any later version. | ||||
9 | | ||||
10 | This program is distributed in the hope that it will be useful, | ||||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | GNU General Public License for more details. | ||||
14 | | ||||
15 | You should have received a copy of the GNU General Public License | ||||
16 | along with this program. If not, see <https://www.gnu.org/licenses/>. | ||||
17 | */ | ||||
18 | | ||||
19 | #include "googlesettings.h" | ||||
20 | #include "settingsbase.h" | ||||
21 | #include "googleresource_debug.h" | ||||
22 | | ||||
23 | #include <QGlobalStatic> | ||||
24 | #include <KGAPI/Account> | ||||
25 | #include <KWallet> | ||||
26 | | ||||
27 | using namespace KWallet; | ||||
28 | using namespace KGAPI2; | ||||
29 | | ||||
30 | static const QString googleWalletFolder = QStringLiteral("Akonadi Google"); | ||||
31 | | ||||
32 | GoogleSettings::GoogleSettings() | ||||
33 | : m_winId(0) | ||||
34 | { | ||||
35 | m_wallet = Wallet::openWallet(Wallet::NetworkWallet(), | ||||
36 | m_winId, Wallet::Asynchronous); | ||||
37 | if (m_wallet) { | ||||
38 | connect(m_wallet.data(), &Wallet::walletOpened, this, &GoogleSettings::slotWalletOpened); | ||||
39 | } else { | ||||
40 | qCWarning(GOOGLE_LOG) << "Failed to open wallet!"; | ||||
41 | } | ||||
42 | } | ||||
43 | | ||||
44 | void GoogleSettings::slotWalletOpened(bool success) | ||||
45 | { | ||||
46 | if (!success) { | ||||
47 | qCWarning(GOOGLE_LOG) << "Failed to open wallet!"; | ||||
48 | Q_EMIT accountReady(false); | ||||
49 | return; | ||||
50 | } | ||||
51 | | ||||
52 | if (!m_wallet->hasFolder(googleWalletFolder) | ||||
53 | && !m_wallet->createFolder(googleWalletFolder)) { | ||||
54 | qCWarning(GOOGLE_LOG) << "Failed to create wallet folder" << googleWalletFolder; | ||||
55 | Q_EMIT accountReady(false); | ||||
56 | return; | ||||
57 | } | ||||
58 | | ||||
59 | if (!m_wallet->setFolder(googleWalletFolder)) { | ||||
60 | qWarning() << "Failed to open wallet folder" << googleWalletFolder; | ||||
61 | Q_EMIT accountReady(false); | ||||
62 | return; | ||||
63 | } | ||||
64 | qCDebug(GOOGLE_LOG) << "Wallet opened, reading" << account(); | ||||
65 | if (!account().isEmpty()) { | ||||
66 | m_account = fetchAccountFromWallet(account()); | ||||
67 | } | ||||
68 | m_isReady = true; | ||||
69 | Q_EMIT accountReady(true); | ||||
70 | } | ||||
71 | | ||||
72 | KGAPI2::AccountPtr GoogleSettings::fetchAccountFromWallet(const QString &accountName) | ||||
73 | { | ||||
74 | if (!m_wallet->entryList().contains(accountName)) { | ||||
75 | qCDebug(GOOGLE_LOG) << "Account" << accountName << "not found in KWallet"; | ||||
76 | return AccountPtr(); | ||||
77 | } | ||||
78 | | ||||
79 | QMap<QString, QString> map; | ||||
80 | m_wallet->readMap(accountName, map); | ||||
81 | | ||||
82 | #if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) | ||||
83 | const QStringList scopes = map[QStringLiteral("scopes")].split(QLatin1Char(','), QString::SkipEmptyParts); | ||||
84 | #else | ||||
85 | const QStringList scopes = map[QStringLiteral("scopes")].split(QLatin1Char(','), Qt::SkipEmptyParts); | ||||
86 | #endif | ||||
87 | QList<QUrl> scopeUrls; | ||||
88 | scopeUrls.reserve(scopes.count()); | ||||
89 | for (const QString &scope : scopes) { | ||||
90 | scopeUrls << QUrl(scope); | ||||
91 | } | ||||
92 | AccountPtr account(new Account(accountName, | ||||
93 | map[QStringLiteral("accessToken")], | ||||
94 | map[QStringLiteral("refreshToken")], | ||||
95 | scopeUrls)); | ||||
96 | return account; | ||||
97 | } | ||||
98 | | ||||
99 | bool GoogleSettings::storeAccount(AccountPtr account) | ||||
100 | { | ||||
101 | // Removing the old one (if present) | ||||
102 | if (m_account && (account->accountName() != m_account->accountName())) { | ||||
103 | cleanup(); | ||||
104 | } | ||||
105 | // Populating the new one | ||||
106 | m_account = account; | ||||
107 | | ||||
108 | QStringList scopes; | ||||
109 | const QList<QUrl> urlScopes = m_account->scopes(); | ||||
110 | scopes.reserve(urlScopes.count()); | ||||
111 | for (const QUrl &url : urlScopes) { | ||||
112 | scopes << url.toString(); | ||||
113 | } | ||||
114 | | ||||
115 | QMap<QString, QString> map; | ||||
116 | map[QStringLiteral("accessToken")] = m_account->accessToken(); | ||||
117 | map[QStringLiteral("refreshToken")] = m_account->refreshToken(); | ||||
118 | map[QStringLiteral("scopes")] = scopes.join(QLatin1Char(',')); | ||||
119 | // Removing previous junk (if present) | ||||
120 | cleanup(); | ||||
121 | if (m_wallet->writeMap(m_account->accountName(), map) != 0) { | ||||
122 | qCWarning(GOOGLE_LOG) << "Failed to write new account entry to wallet"; | ||||
123 | return false; | ||||
124 | } | ||||
125 | SettingsBase::setAccount(m_account->accountName()); | ||||
126 | m_isReady = true; | ||||
127 | return true; | ||||
128 | } | ||||
129 | | ||||
130 | void GoogleSettings::cleanup() | ||||
131 | { | ||||
132 | if (m_account && m_wallet) { | ||||
133 | m_wallet->removeEntry(m_account->accountName()); | ||||
134 | } | ||||
135 | } | ||||
136 | | ||||
137 | void GoogleSettings::addCalendar(const QString &calendar) | ||||
138 | { | ||||
139 | if (calendars().isEmpty() || calendars().contains(calendar)) { | ||||
140 | return; | ||||
141 | } | ||||
142 | setCalendars(calendars() << calendar); | ||||
143 | save(); | ||||
144 | } | ||||
145 | | ||||
146 | void GoogleSettings::addTaskList(const QString &taskList) | ||||
147 | { | ||||
148 | if (calendars().isEmpty() || taskLists().contains(taskList)) { | ||||
149 | return; | ||||
150 | } | ||||
151 | setTaskLists(taskLists() << taskList); | ||||
152 | save(); | ||||
153 | } | ||||
154 | | ||||
155 | QString GoogleSettings::clientId() const | ||||
156 | { | ||||
157 | return QStringLiteral("554041944266.apps.googleusercontent.com"); | ||||
158 | } | ||||
159 | | ||||
160 | QString GoogleSettings::clientSecret() const | ||||
161 | { | ||||
162 | return QStringLiteral("mdT1DjzohxN3npUUzkENT0gO"); | ||||
163 | } | ||||
164 | | ||||
165 | bool GoogleSettings::isReady() const | ||||
166 | { | ||||
167 | return m_isReady; | ||||
168 | } | ||||
169 | | ||||
170 | AccountPtr GoogleSettings::accountPtr() | ||||
171 | { | ||||
172 | return m_account; | ||||
173 | } | ||||
174 | | ||||
175 | void GoogleSettings::setWindowId(WId id) | ||||
176 | { | ||||
177 | m_winId = id; | ||||
178 | } | ||||
179 | | ||||
180 | void GoogleSettings::setResourceId(const QString &resourceIdentificator) | ||||
181 | { | ||||
182 | m_resourceId = resourceIdentificator; | ||||
183 | } |