1/*
2 * Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 */
14
15#ifndef QUOTAINFO_H
16#define QUOTAINFO_H
17
18#include <QObject>
19#include <QPointer>
20#include <QVariant>
21#include <QTimer>
22#include <QDateTime>
23
24namespace OCC {
25class AccountState;
26class PropfindJob;
27
28/**
29 * @brief handles getting the quota to display in the UI
30 *
31 * It is typically owned by the AccountSetting page.
32 *
33 * The quota is requested if these 3 conditions are met:
34 * - This object is active via setActive() (typically if the settings page is visible.)
35 * - The account is connected.
36 * - Every 30 seconds (defaultIntervalT) or 5 seconds in case of failure (failIntervalT)
37 *
38 * We only request the quota when the UI is visible otherwise this might slow down the server with
39 * too many requests. But we still need to do it every 30 seconds otherwise user complains that the
40 * quota is not updated fast enough when changed on the server.
41 *
42 * If the quota job is not finished within 30 seconds, it is cancelled and another one is started
43 *
44 * @ingroup gui
45 */
46class QuotaInfo : public QObject
47{
48 Q_OBJECT
49public:
50 explicit QuotaInfo(OCC::AccountState *accountState, QObject *parent = 0);
51
52 qint64 lastQuotaTotalBytes() const { return _lastQuotaTotalBytes; }
53 qint64 lastQuotaUsedBytes() const { return _lastQuotaUsedBytes; }
54
55 /**
56 * When the quotainfo is active, it requests the quota at regular interval.
57 * When setting it to active it will request the quota immediately if the last time
58 * the quota was requested was more than the interval
59 */
60 void setActive(bool active);
61
62public Q_SLOTS:
63 void slotCheckQuota();
64
65private Q_SLOTS:
66 void slotUpdateLastQuota(const QVariantMap &);
67 void slotAccountStateChanged();
68 void slotRequestFailed();
69
70Q_SIGNALS:
71 void quotaUpdated(qint64 total, qint64 used);
72
73private:
74 bool canGetQuota() const;
75
76 /// Returns the folder that quota shall be retrieved for
77 QString quotaBaseFolder() const;
78
79 QPointer<AccountState> _accountState;
80 qint64 _lastQuotaTotalBytes;
81 qint64 _lastQuotaUsedBytes;
82 QTimer _jobRestartTimer;
83 QDateTime _lastQuotaRecieved; // the time at which the quota was received last
84 bool _active; // if we should check at regular interval (when the UI is visible)
85 QPointer<PropfindJob> _job; // the currently running job
86};
87
88
89} // namespace OCC
90
91#endif //QUOTAINFO_H
92