Welcome to mirror list, hosted at ThFree Co, Russian Federation.

userinfo.h « gui « src - github.com/nextcloud/desktop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de8c7ec6bbf57190f3f00bfdc0780f668be3a9f9 (plain)
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
/*
 * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
 * Copyright (C) by Michael Schuster <michael@nextcloud.com>
 *
 * 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.
 */

#ifndef USERINFO_H
#define USERINFO_H

#include <QObject>
#include <QPointer>
#include <QVariant>
#include <QTimer>
#include <QDateTime>

namespace OCC {
class AccountState;
class JsonApiJob;

/**
 * @brief handles getting the user info and quota to display in the UI
 *
 * It is typically owned by the AccountSetting page.
 *
 * The user info and quota is requested if these 3 conditions are met:
 *  - This object is active via setActive() (typically if the settings page is visible.)
 *  - The account is connected.
 *  - Every 30 seconds (defaultIntervalT) or 5 seconds in case of failure (failIntervalT)
 *
 * We only request the info when the UI is visible otherwise this might slow down the server with
 * too many requests. But we still need to do it every 30 seconds otherwise user complains that the
 * quota is not updated fast enough when changed on the server.
 *
 * If the fetch job is not finished within 30 seconds, it is cancelled and another one is started
 *
 * Constructor notes:
 *  - allowDisconnectedAccountState: set to true if you want to ignore AccountState's isConnected() state,
 *    this is used by ConnectionValidator (prior having a valid AccountState).
 *  - fetchAvatarImage: set to false if you don't want to fetch the avatar image
 *
 * @ingroup gui
 *
 * Here follows the state machine

 \code{.unparsed}
 *---> slotFetchInfo
         JsonApiJob (ocs/v1.php/cloud/user)
         |
         +-> slotUpdateLastInfo
               AvatarJob (if _fetchAvatarImage is true)
               |
               +-> slotAvatarImage -->
   +-----------------------------------+
   |
   +-> Client Side Encryption Checks --+ --reportResult()
     \endcode
  */
class UserInfo : public QObject
{
    Q_OBJECT
public:
    explicit UserInfo(OCC::AccountState *accountState, bool allowDisconnectedAccountState, bool fetchAvatarImage, QObject *parent = nullptr);

    qint64 lastQuotaTotalBytes() const { return _lastQuotaTotalBytes; }
    qint64 lastQuotaUsedBytes() const { return _lastQuotaUsedBytes; }

    /**
     * When the quotainfo is active, it requests the quota at regular interval.
     * When setting it to active it will request the quota immediately if the last time
     * the quota was requested was more than the interval
     */
    void setActive(bool active);

public Q_SLOTS:
    void slotFetchInfo();

private Q_SLOTS:
    void slotUpdateLastInfo(const QJsonDocument &json);
    void slotAccountStateChanged();
    void slotRequestFailed();
    void slotAvatarImage(const QImage &img);

Q_SIGNALS:
    void quotaUpdated(qint64 total, qint64 used);
    void fetchedLastInfo(UserInfo *userInfo);

private:
    bool canGetInfo() const;

    QPointer<AccountState> _accountState;
    bool _allowDisconnectedAccountState;
    bool _fetchAvatarImage;

    qint64 _lastQuotaTotalBytes;
    qint64 _lastQuotaUsedBytes;
    QTimer _jobRestartTimer;
    QDateTime _lastInfoReceived; // the time at which the user info and quota was received last
    bool _active; // if we should check at regular interval (when the UI is visible)
    QPointer<JsonApiJob> _job; // the currently running job
};


} // namespace OCC

#endif //USERINFO_H