QuotaInfo: only request the quota when the UI is visible

This commit is contained in:
Olivier Goffart 2015-06-26 15:40:34 +02:00
parent 7757886ebc
commit e7d7646151
6 changed files with 66 additions and 41 deletions

View file

@ -62,7 +62,8 @@ AccountSettings::AccountSettings(AccountState *accountState, QWidget *parent) :
QWidget(parent), QWidget(parent),
ui(new Ui::AccountSettings), ui(new Ui::AccountSettings),
_wasDisabledBefore(false), _wasDisabledBefore(false),
_accountState(accountState) _accountState(accountState),
_quotaInfo(accountState)
{ {
ui->setupUi(this); ui->setupUi(this);
@ -123,10 +124,8 @@ AccountSettings::AccountSettings(AccountState *accountState, QWidget *parent) :
connect(_accountState, SIGNAL(stateChanged(int)), SLOT(slotAccountStateChanged(int))); connect(_accountState, SIGNAL(stateChanged(int)), SLOT(slotAccountStateChanged(int)));
slotAccountStateChanged(_accountState->state()); slotAccountStateChanged(_accountState->state());
QuotaInfo *quotaInfo = _accountState->quotaInfo(); connect( &_quotaInfo, SIGNAL(quotaUpdated(qint64,qint64)),
connect( quotaInfo, SIGNAL(quotaUpdated(qint64,qint64)),
this, SLOT(slotUpdateQuota(qint64,qint64))); this, SLOT(slotUpdateQuota(qint64,qint64)));
slotUpdateQuota(quotaInfo->lastQuotaTotalBytes(), quotaInfo->lastQuotaUsedBytes());
connect(ui->deleteButton, SIGNAL(clicked()) , this, SLOT(slotDeleteAccount())); connect(ui->deleteButton, SIGNAL(clicked()) , this, SLOT(slotDeleteAccount()));
@ -514,5 +513,12 @@ void AccountSettings::slotDeleteAccount()
manager->save(); manager->save();
} }
bool AccountSettings::event(QEvent* e)
{
if (e->type() == QEvent::Hide || e->type() == QEvent::Show) {
_quotaInfo.setActive(isVisible());
}
return QWidget::event(e);
}
} // namespace OCC } // namespace OCC

View file

@ -21,6 +21,7 @@
#include <QTimer> #include <QTimer>
#include "folder.h" #include "folder.h"
#include "quotainfo.h"
#include "progressdispatcher.h" #include "progressdispatcher.h"
class QModelIndex; class QModelIndex;
@ -76,9 +77,11 @@ protected slots:
void slotDeleteAccount(); void slotDeleteAccount();
void refreshSelectiveSyncStatus(); void refreshSelectiveSyncStatus();
void slotForceRemoteDiscoveryOnFolders(); void slotForceRemoteDiscoveryOnFolders();
void slotCustomContextMenuRequested(const QPoint&);
private: private:
void showConnectionLabel( const QString& message, const QString& tooltip = QString() ); void showConnectionLabel( const QString& message, const QString& tooltip = QString() );
bool event(QEvent*) Q_DECL_OVERRIDE;
Ui::AccountSettings *ui; Ui::AccountSettings *ui;
@ -88,8 +91,7 @@ private:
bool _wasDisabledBefore; bool _wasDisabledBefore;
AccountState *_accountState; AccountState *_accountState;
QLabel *_quotaLabel; QLabel *_quotaLabel;
private slots: QuotaInfo _quotaInfo;
void slotCustomContextMenuRequested(const QPoint&);
}; };
} // namespace OCC } // namespace OCC

View file

@ -12,7 +12,6 @@
*/ */
#include "accountstate.h" #include "accountstate.h"
#include "quotainfo.h"
#include "accountmanager.h" #include "accountmanager.h"
#include "account.h" #include "account.h"
#include "creds/abstractcredentials.h" #include "creds/abstractcredentials.h"
@ -25,15 +24,12 @@ namespace OCC {
AccountState::AccountState(AccountPtr account) AccountState::AccountState(AccountPtr account)
: QObject() : QObject()
, _account(account) , _account(account)
, _quotaInfo(0)
, _state(AccountState::Disconnected) , _state(AccountState::Disconnected)
, _connectionStatus(ConnectionValidator::Undefined) , _connectionStatus(ConnectionValidator::Undefined)
, _waitingForNewCredentials(false) , _waitingForNewCredentials(false)
{ {
qRegisterMetaType<AccountState*>("AccountState*"); qRegisterMetaType<AccountState*>("AccountState*");
_quotaInfo = new QuotaInfo(this); // Need to be initialized when 'this' is fully initialized
connect(account.data(), SIGNAL(invalidCredentials()), connect(account.data(), SIGNAL(invalidCredentials()),
SLOT(slotInvalidCredentials())); SLOT(slotInvalidCredentials()));
connect(account.data(), SIGNAL(credentialsFetched(AbstractCredentials*)), connect(account.data(), SIGNAL(credentialsFetched(AbstractCredentials*)),
@ -134,11 +130,6 @@ bool AccountState::isConnectedOrTemporarilyUnavailable() const
return isConnected() || _state == ServiceUnavailable; return isConnected() || _state == ServiceUnavailable;
} }
QuotaInfo *AccountState::quotaInfo()
{
return _quotaInfo;
}
void AccountState::checkConnectivity() void AccountState::checkConnectivity()
{ {
if (isSignedOut() || _waitingForNewCredentials) { if (isSignedOut() || _waitingForNewCredentials) {

View file

@ -25,7 +25,6 @@ class QSettings;
namespace OCC { namespace OCC {
class QuotaInfo;
class AccountState; class AccountState;
class Account; class Account;
class AbstractCredentials; class AbstractCredentials;
@ -82,8 +81,6 @@ public:
bool isConnected() const; bool isConnected() const;
bool isConnectedOrTemporarilyUnavailable() const; bool isConnectedOrTemporarilyUnavailable() const;
QuotaInfo *quotaInfo();
/// Triggers a ping to the server to update state and /// Triggers a ping to the server to update state and
/// connection status and errors. /// connection status and errors.
void checkConnectivity(); void checkConnectivity();
@ -106,7 +103,6 @@ protected Q_SLOTS:
private: private:
AccountPtr _account; AccountPtr _account;
QuotaInfo *_quotaInfo;
State _state; State _state;
ConnectionStatus _connectionStatus; ConnectionStatus _connectionStatus;
QStringList _connectionErrors; QStringList _connectionErrors;

View file

@ -25,31 +25,39 @@ namespace OCC {
namespace { namespace {
static const int defaultIntervalT = 30*1000; static const int defaultIntervalT = 30*1000;
static const int failIntervalT = 5*1000; static const int failIntervalT = 5*1000;
static const int initialTimeT = 1*1000;
} }
QuotaInfo::QuotaInfo(AccountState *accountState) QuotaInfo::QuotaInfo(AccountState *accountState, QObject *parent)
: QObject(accountState) : QObject(parent)
, _accountState(accountState) , _accountState(accountState)
, _lastQuotaTotalBytes(0) , _lastQuotaTotalBytes(0)
, _lastQuotaUsedBytes(0) , _lastQuotaUsedBytes(0)
, _jobRestartTimer(new QTimer(this)) , _active(false)
{ {
connect(accountState, SIGNAL(stateChanged(int)), connect(accountState, SIGNAL(stateChanged(int)),
SLOT(slotAccountStateChanged(int))); SLOT(slotAccountStateChanged()));
connect(_jobRestartTimer, SIGNAL(timeout()), SLOT(slotCheckQuota())); connect(&_jobRestartTimer, SIGNAL(timeout()), SLOT(slotCheckQuota()));
_jobRestartTimer->setSingleShot(true); _jobRestartTimer.setSingleShot(true);
if (canGetQuota()) {
_jobRestartTimer->start(initialTimeT);
}
} }
void QuotaInfo::slotAccountStateChanged(int /*state*/) void QuotaInfo::setActive(bool active)
{
_active = active;
slotAccountStateChanged();
}
void QuotaInfo::slotAccountStateChanged()
{ {
if (canGetQuota()) { if (canGetQuota()) {
_jobRestartTimer->start(initialTimeT); if (_lastQuotaRecieved.isNull()
|| _lastQuotaRecieved.msecsTo(QDateTime::currentDateTime()) > defaultIntervalT) {
slotCheckQuota();
} else {
_jobRestartTimer.start(defaultIntervalT);
}
} else { } else {
_jobRestartTimer->stop(); _jobRestartTimer.stop();
} }
} }
@ -57,12 +65,12 @@ void QuotaInfo::slotRequestFailed()
{ {
_lastQuotaTotalBytes = 0; _lastQuotaTotalBytes = 0;
_lastQuotaUsedBytes = 0; _lastQuotaUsedBytes = 0;
_jobRestartTimer->start(failIntervalT); _jobRestartTimer.start(failIntervalT);
} }
bool QuotaInfo::canGetQuota() const bool QuotaInfo::canGetQuota() const
{ {
if (! _accountState) { if (! _accountState || !_active) {
return false; return false;
} }
AccountPtr account = _accountState->account(); AccountPtr account = _accountState->account();
@ -93,7 +101,8 @@ void QuotaInfo::slotUpdateLastQuota(const QVariantMap &result)
_lastQuotaUsedBytes = result["quota-used-bytes"].toDouble(); _lastQuotaUsedBytes = result["quota-used-bytes"].toDouble();
_lastQuotaTotalBytes = _lastQuotaUsedBytes + avail; _lastQuotaTotalBytes = _lastQuotaUsedBytes + avail;
emit quotaUpdated(_lastQuotaTotalBytes, _lastQuotaUsedBytes); emit quotaUpdated(_lastQuotaTotalBytes, _lastQuotaUsedBytes);
_jobRestartTimer->start(defaultIntervalT); _jobRestartTimer.start(defaultIntervalT);
_lastQuotaRecieved = QDateTime::currentDateTime();
} }
} }

View file

@ -17,27 +17,46 @@
#include <QObject> #include <QObject>
#include <QPointer> #include <QPointer>
#include <QVariant> #include <QVariant>
#include <QTimer>
class QTimer; #include <QDateTime>
namespace OCC { namespace OCC {
class AccountState; class AccountState;
/**
* This class handle the getting the quota to display in the UI
* It is typically owned by the AccountSetting page.
*
* The quota is requested if those 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 quota 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.
*/
class QuotaInfo : public QObject { class QuotaInfo : public QObject {
Q_OBJECT Q_OBJECT
public: public:
QuotaInfo(AccountState *account); explicit QuotaInfo(OCC::AccountState* accountState, QObject* parent = 0);
qint64 lastQuotaTotalBytes() const { return _lastQuotaTotalBytes; } qint64 lastQuotaTotalBytes() const { return _lastQuotaTotalBytes; }
qint64 lastQuotaUsedBytes() const { return _lastQuotaUsedBytes; } 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 imediatly if the last time
* the quota was requested was more than the interval
*/
void setActive(bool active);
public Q_SLOTS: public Q_SLOTS:
void slotCheckQuota(); void slotCheckQuota();
private Q_SLOTS: private Q_SLOTS:
void slotUpdateLastQuota(const QVariantMap &); void slotUpdateLastQuota(const QVariantMap &);
void slotAccountStateChanged(int state); void slotAccountStateChanged();
void slotRequestFailed(); void slotRequestFailed();
Q_SIGNALS: Q_SIGNALS:
@ -49,7 +68,9 @@ private:
QPointer<AccountState> _accountState; QPointer<AccountState> _accountState;
qint64 _lastQuotaTotalBytes; qint64 _lastQuotaTotalBytes;
qint64 _lastQuotaUsedBytes; qint64 _lastQuotaUsedBytes;
QTimer *_jobRestartTimer; QTimer _jobRestartTimer;
QDateTime _lastQuotaRecieved; // the time at which de quota was recieved last
bool _active; // if we should check at regular interval (when the UI is visible)
}; };