File indexing completed on 2024-05-12 05:22:14

0001 /*
0002     SPDX-FileCopyrightText: 2012-2018 Daniel Vrátil <dvratil@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #pragma once
0008 
0009 #include <QMetaType>
0010 #include <QSharedPointer>
0011 #include <QString>
0012 #include <QUrl>
0013 
0014 #include "kgapicore_export.h"
0015 #include "types.h"
0016 
0017 namespace KGAPI2
0018 {
0019 
0020 /**
0021  * @headerfile account.h
0022  * @brief A Google account
0023  *
0024  * This class represents a single Google account. The account is uniquely
0025  * identified by Account::accountName (which is actually the user's GMail address).
0026  *
0027  * The class stores an access token, refresh token (to retrieve a new access token
0028  * when the old one expires) and list of scopes (APIs that current access token
0029  * can be used to access).
0030  *
0031  * Unlike in previous versions, account management is not handled by LibKGAPI
0032  * anymore and it's up to programmer to store the account in a persistent storage.
0033  *
0034  * To obtain a new account, use AuthJob.
0035  *
0036  * @author Daniel Vrátil <dvratil@redhat.com>
0037  * @since 0.1
0038  */
0039 class KGAPICORE_EXPORT Account
0040 {
0041 public:
0042     /**
0043      * @brief Constructs an invalid account.
0044      */
0045     Account();
0046 
0047     /**
0048      * @brief Constructs a new valid account
0049      *
0050      * @param account Google account name (usually user.name@gmail.com)
0051      * @param accessToken Access token to \p scopes for \p account
0052      * @param refreshToken Refresh token
0053      * @param scopes List of scopes
0054      */
0055     explicit Account(const QString &account,
0056                      const QString &accessToken = QString(),
0057                      const QString &refreshToken = QString(),
0058                      const QList<QUrl> &scopes = QList<QUrl>());
0059 
0060     /**
0061      * @brief Copy constructor
0062      */
0063     Account(const Account &other);
0064 
0065     /**
0066      * @brief Destructor
0067      */
0068     virtual ~Account();
0069 
0070     bool operator==(const Account &other) const;
0071 
0072     /**
0073      * @returns Returns unique account identifier
0074      */
0075     [[nodiscard]] QString accountName() const;
0076 
0077     /**
0078      * Sets account name.
0079      *
0080      * @param accountName
0081      */
0082     void setAccountName(const QString &accountName);
0083 
0084     /**
0085      * @return Returns access token.
0086      */
0087     [[nodiscard]] QString accessToken() const;
0088 
0089     /**
0090      * Sets a new access token.
0091      *
0092      * @param accessToken
0093      */
0094     void setAccessToken(const QString &accessToken);
0095 
0096     /**
0097      * @return Returns refresh token.
0098      */
0099     [[nodiscard]] QString refreshToken() const;
0100 
0101     /**
0102      * Sets a new refresh token for the access token.
0103      *
0104      * @param refreshToken
0105      */
0106     void setRefreshToken(const QString &refreshToken);
0107 
0108     /**
0109      * @return Returns list of scopes the account is authenticated against.
0110      */
0111     [[nodiscard]] QList<QUrl> scopes() const;
0112 
0113     /**
0114      * \brief Sets new scopes.
0115      *
0116      * @note Note that changing scopes requires makes current tokens invalid.
0117      * This means that when this Account is used next time, AuthJob will be
0118      * automatically started and user will be prompted with a dialog to grant
0119      * access to all scopes.
0120      *
0121      * @param scopes
0122      */
0123     void setScopes(const QList<QUrl> &scopes);
0124 
0125     /**
0126      * Adds a single scope to account scopes.
0127      *
0128      * @param scope
0129      * @see Account::setScopes(const QList<QUrl>)
0130      */
0131     void addScope(const QUrl &scope);
0132 
0133     /**
0134      * Removes scope from the list.
0135      *
0136      * @param scope
0137      * @see Account::setScopes(const QList<QUrl>)
0138      */
0139     void removeScope(const QUrl &scope);
0140 
0141     /**
0142      * @since 2.0.82
0143      * Returns expire date time token
0144      */
0145     [[nodiscard]] QDateTime expireDateTime() const;
0146 
0147     /**
0148      * @since 2.0.82
0149      * set expire date time
0150      */
0151     void setExpireDateTime(const QDateTime &expire);
0152 
0153     /**
0154      * Returns scope URL for AccountInfo service.
0155      */
0156     static QUrl accountInfoScopeUrl();
0157 
0158     /**
0159      * Returns scope URL to retrieve AccountInfo with email.
0160      */
0161     static QUrl accountInfoEmailScopeUrl();
0162 
0163     /**
0164      * Returns scope URL for Google Calendar service.
0165      */
0166     static QUrl calendarScopeUrl();
0167 
0168     /**
0169      * Returns scope URL for Google Tasks service.
0170      */
0171     static QUrl tasksScopeUrl();
0172 
0173     /**
0174      * Returns scope URL for Google People service.
0175      */
0176     static QUrl peopleScopeUrl();
0177 
0178     /**
0179      * Returns scope URL for Google Latitude service.
0180      */
0181     static QUrl latitudeScopeUrl();
0182 
0183     /**
0184      * Returns scope URL for Google Blogger service.
0185      */
0186     static QUrl bloggerScopeUrl();
0187 
0188     /**
0189      * Returns scope URL for Gmail service.
0190      */
0191     static QUrl mailScopeUrl();
0192 
0193     /**
0194      * Returns scope URL for Drive service.
0195      */
0196     static QUrl driveScopeUrl();
0197 
0198 private:
0199     class Private;
0200     Private *const d;
0201 
0202     /**
0203      * @internal
0204      * Whether scopes were changed or not.
0205      *
0206      * AuthJob reads this attribute when Account is passed to it to
0207      * determine whether completely new process of authentication is needed,
0208      * or whether just refreshing tokens is enough.
0209      *
0210      * When m_scopesChanged is \p true and AuthJob successffulyperforms full
0211      * re-authentication it sets this attribute to \p false and next time it
0212      * will just refresh existing tokens until the scopes are changed again.
0213      */
0214     bool m_scopesChanged; // krazy:exclude=dpointer
0215 
0216     friend class AuthJob;
0217 };
0218 
0219 } // namespace KGAPI2
0220 
0221 Q_DECLARE_METATYPE(KGAPI2::AccountPtr)