googlesettings.cpp 4.45 KB
Newer Older
1
/*
2
3
    SPDX-FileCopyrightText: 2011-2013 Dan Vratil <dan@progdan.cz>
    SPDX-FileCopyrightText: 2020 Igor Poboiko <igor.poboiko@gmail.com>
4

5
    SPDX-License-Identifier: GPL-3.0-or-later
6
7
8
9
10
11
12
*/

#include "googlesettings.h"
#include "googleresource_debug.h"

#include <KGAPI/Account>
#include <KWallet>
Laurent Montel's avatar
Laurent Montel committed
13
14
#include <qt5keychain/keychain.h>
using namespace QKeychain;
15
16
17
18
19
20
21
22
23

using namespace KWallet;
using namespace KGAPI2;

static const QString googleWalletFolder = QStringLiteral("Akonadi Google");

GoogleSettings::GoogleSettings()
    : m_winId(0)
{
Laurent Montel's avatar
Laurent Montel committed
24
    m_wallet = Wallet::openWallet(Wallet::NetworkWallet(), m_winId, Wallet::Asynchronous);
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    if (m_wallet) {
        connect(m_wallet.data(), &Wallet::walletOpened, this, &GoogleSettings::slotWalletOpened);
    } else {
        qCWarning(GOOGLE_LOG) << "Failed to open wallet!";
    }
}

void GoogleSettings::slotWalletOpened(bool success)
{
    if (!success) {
        qCWarning(GOOGLE_LOG) << "Failed to open wallet!";
        Q_EMIT accountReady(false);
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
40
    if (!m_wallet->hasFolder(googleWalletFolder) && !m_wallet->createFolder(googleWalletFolder)) {
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
        qCWarning(GOOGLE_LOG) << "Failed to create wallet folder" << googleWalletFolder;
        Q_EMIT accountReady(false);
        return;
    }

    if (!m_wallet->setFolder(googleWalletFolder)) {
        qWarning() << "Failed to open wallet folder" << googleWalletFolder;
        Q_EMIT accountReady(false);
        return;
    }
    qCDebug(GOOGLE_LOG) << "Wallet opened, reading" << account();
    if (!account().isEmpty()) {
        m_account = fetchAccountFromWallet(account());
    }
    m_isReady = true;
    Q_EMIT accountReady(true);
}

KGAPI2::AccountPtr GoogleSettings::fetchAccountFromWallet(const QString &accountName)
{
    if (!m_wallet->entryList().contains(accountName)) {
        qCDebug(GOOGLE_LOG) << "Account" << accountName << "not found in KWallet";
        return AccountPtr();
    }

    QMap<QString, QString> map;
    m_wallet->readMap(accountName, map);

    const QStringList scopes = map[QStringLiteral("scopes")].split(QLatin1Char(','), Qt::SkipEmptyParts);
    QList<QUrl> scopeUrls;
    scopeUrls.reserve(scopes.count());
    for (const QString &scope : scopes) {
        scopeUrls << QUrl(scope);
    }
Laurent Montel's avatar
Laurent Montel committed
75
    AccountPtr account(new Account(accountName, map[QStringLiteral("accessToken")], map[QStringLiteral("refreshToken")], scopeUrls));
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
    return account;
}

bool GoogleSettings::storeAccount(AccountPtr account)
{
    // Removing the old one (if present)
    if (m_account && (account->accountName() != m_account->accountName())) {
        cleanup();
    }
    // Populating the new one
    m_account = account;

    QStringList scopes;
    const QList<QUrl> urlScopes = m_account->scopes();
    scopes.reserve(urlScopes.count());
    for (const QUrl &url : urlScopes) {
        scopes << url.toString();
    }

    QMap<QString, QString> map;
    map[QStringLiteral("accessToken")] = m_account->accessToken();
    map[QStringLiteral("refreshToken")] = m_account->refreshToken();
    map[QStringLiteral("scopes")] = scopes.join(QLatin1Char(','));
    // Removing previous junk (if present)
    cleanup();
    if (m_wallet->writeMap(m_account->accountName(), map) != 0) {
        qCWarning(GOOGLE_LOG) << "Failed to write new account entry to wallet";
        return false;
    }
    SettingsBase::setAccount(m_account->accountName());
    m_isReady = true;
    return true;
}

void GoogleSettings::cleanup()
{
Laurent Montel's avatar
Laurent Montel committed
112
113
114
    auto deleteJob = new DeletePasswordJob(googleWalletFolder);
    deleteJob->setKey(m_account->accountName());
    deleteJob->start();
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
162
163
}

void GoogleSettings::addCalendar(const QString &calendar)
{
    if (calendars().isEmpty() || calendars().contains(calendar)) {
        return;
    }
    setCalendars(calendars() << calendar);
    save();
}

void GoogleSettings::addTaskList(const QString &taskList)
{
    if (calendars().isEmpty() || taskLists().contains(taskList)) {
        return;
    }
    setTaskLists(taskLists() << taskList);
    save();
}

QString GoogleSettings::clientId() const
{
    return QStringLiteral("554041944266.apps.googleusercontent.com");
}

QString GoogleSettings::clientSecret() const
{
    return QStringLiteral("mdT1DjzohxN3npUUzkENT0gO");
}

bool GoogleSettings::isReady() const
{
    return m_isReady;
}

AccountPtr GoogleSettings::accountPtr()
{
    return m_account;
}

void GoogleSettings::setWindowId(WId id)
{
    m_winId = id;
}

void GoogleSettings::setResourceId(const QString &resourceIdentificator)
{
    m_resourceId = resourceIdentificator;
}