[Pkg-owncloud-commits] [owncloud-client] 204/498: QuotaInfo: only request the quota when the UI is visible

Sandro Knauß hefee-guest at moszumanska.debian.org
Tue Aug 11 14:48:50 UTC 2015


This is an automated email from the git hooks/post-receive script.

hefee-guest pushed a commit to branch master
in repository owncloud-client.

commit e7d76461516df14dcdb2f128d5cde6ef81f545d8
Author: Olivier Goffart <ogoffart at woboq.com>
Date:   Fri Jun 26 15:40:34 2015 +0200

    QuotaInfo: only request the quota when the UI is visible
---
 src/gui/accountsettings.cpp | 14 ++++++++++----
 src/gui/accountsettings.h   |  6 ++++--
 src/gui/accountstate.cpp    |  9 ---------
 src/gui/accountstate.h      |  4 ----
 src/gui/quotainfo.cpp       | 41 +++++++++++++++++++++++++----------------
 src/gui/quotainfo.h         | 33 +++++++++++++++++++++++++++------
 6 files changed, 66 insertions(+), 41 deletions(-)

diff --git a/src/gui/accountsettings.cpp b/src/gui/accountsettings.cpp
index 640f1f8..8a3fa0a 100644
--- a/src/gui/accountsettings.cpp
+++ b/src/gui/accountsettings.cpp
@@ -62,7 +62,8 @@ AccountSettings::AccountSettings(AccountState *accountState, QWidget *parent) :
     QWidget(parent),
     ui(new Ui::AccountSettings),
     _wasDisabledBefore(false),
-    _accountState(accountState)
+    _accountState(accountState),
+    _quotaInfo(accountState)
 {
     ui->setupUi(this);
 
@@ -123,10 +124,8 @@ AccountSettings::AccountSettings(AccountState *accountState, QWidget *parent) :
     connect(_accountState, SIGNAL(stateChanged(int)), SLOT(slotAccountStateChanged(int)));
     slotAccountStateChanged(_accountState->state());
 
-    QuotaInfo *quotaInfo = _accountState->quotaInfo();
-    connect( quotaInfo, SIGNAL(quotaUpdated(qint64,qint64)),
+    connect( &_quotaInfo, SIGNAL(quotaUpdated(qint64,qint64)),
             this, SLOT(slotUpdateQuota(qint64,qint64)));
-    slotUpdateQuota(quotaInfo->lastQuotaTotalBytes(), quotaInfo->lastQuotaUsedBytes());
 
     connect(ui->deleteButton, SIGNAL(clicked()) , this, SLOT(slotDeleteAccount()));
 
@@ -514,5 +513,12 @@ void AccountSettings::slotDeleteAccount()
     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
diff --git a/src/gui/accountsettings.h b/src/gui/accountsettings.h
index dd6438d..c7ca0f0 100644
--- a/src/gui/accountsettings.h
+++ b/src/gui/accountsettings.h
@@ -21,6 +21,7 @@
 #include <QTimer>
 
 #include "folder.h"
+#include "quotainfo.h"
 #include "progressdispatcher.h"
 
 class QModelIndex;
@@ -76,9 +77,11 @@ protected slots:
     void slotDeleteAccount();
     void refreshSelectiveSyncStatus();
     void slotForceRemoteDiscoveryOnFolders();
+    void slotCustomContextMenuRequested(const QPoint&);
 
 private:
     void showConnectionLabel( const QString& message, const QString& tooltip = QString() );
+    bool event(QEvent*) Q_DECL_OVERRIDE;
 
     Ui::AccountSettings *ui;
 
@@ -88,8 +91,7 @@ private:
     bool _wasDisabledBefore;
     AccountState *_accountState;
     QLabel *_quotaLabel;
-private slots:
-    void slotCustomContextMenuRequested(const QPoint&);
+    QuotaInfo _quotaInfo;
 };
 
 } // namespace OCC
diff --git a/src/gui/accountstate.cpp b/src/gui/accountstate.cpp
index e95ec9b..c8d3ea9 100644
--- a/src/gui/accountstate.cpp
+++ b/src/gui/accountstate.cpp
@@ -12,7 +12,6 @@
  */
 
 #include "accountstate.h"
-#include "quotainfo.h"
 #include "accountmanager.h"
 #include "account.h"
 #include "creds/abstractcredentials.h"
@@ -25,15 +24,12 @@ namespace OCC {
 AccountState::AccountState(AccountPtr account)
     : QObject()
     , _account(account)
-    , _quotaInfo(0)
     , _state(AccountState::Disconnected)
     , _connectionStatus(ConnectionValidator::Undefined)
     , _waitingForNewCredentials(false)
 {
     qRegisterMetaType<AccountState*>("AccountState*");
 
-    _quotaInfo = new QuotaInfo(this); // Need to be initialized when 'this' is fully initialized
-
     connect(account.data(), SIGNAL(invalidCredentials()),
             SLOT(slotInvalidCredentials()));
     connect(account.data(), SIGNAL(credentialsFetched(AbstractCredentials*)),
@@ -134,11 +130,6 @@ bool AccountState::isConnectedOrTemporarilyUnavailable() const
     return isConnected() || _state == ServiceUnavailable;
 }
 
-QuotaInfo *AccountState::quotaInfo()
-{
-    return _quotaInfo;
-}
-
 void AccountState::checkConnectivity()
 {
     if (isSignedOut() || _waitingForNewCredentials) {
diff --git a/src/gui/accountstate.h b/src/gui/accountstate.h
index a2c3c60..d3a72ce 100644
--- a/src/gui/accountstate.h
+++ b/src/gui/accountstate.h
@@ -25,7 +25,6 @@ class QSettings;
 
 namespace OCC {
 
-class QuotaInfo;
 class AccountState;
 class Account;
 class AbstractCredentials;
@@ -82,8 +81,6 @@ public:
     bool isConnected() const;
     bool isConnectedOrTemporarilyUnavailable() const;
 
-    QuotaInfo *quotaInfo();
-
     /// Triggers a ping to the server to update state and
     /// connection status and errors.
     void checkConnectivity();
@@ -106,7 +103,6 @@ protected Q_SLOTS:
 
 private:
     AccountPtr _account;
-    QuotaInfo *_quotaInfo;
     State _state;
     ConnectionStatus _connectionStatus;
     QStringList _connectionErrors;
diff --git a/src/gui/quotainfo.cpp b/src/gui/quotainfo.cpp
index 5474632..8098245 100644
--- a/src/gui/quotainfo.cpp
+++ b/src/gui/quotainfo.cpp
@@ -25,31 +25,39 @@ namespace OCC {
 namespace {
 static const int defaultIntervalT = 30*1000;
 static const int failIntervalT = 5*1000;
-static const int initialTimeT = 1*1000;
 }
 
-QuotaInfo::QuotaInfo(AccountState *accountState)
-    : QObject(accountState)
+QuotaInfo::QuotaInfo(AccountState *accountState, QObject *parent)
+    : QObject(parent)
     , _accountState(accountState)
     , _lastQuotaTotalBytes(0)
     , _lastQuotaUsedBytes(0)
-    , _jobRestartTimer(new QTimer(this))
+    , _active(false)
 {
     connect(accountState, SIGNAL(stateChanged(int)),
-            SLOT(slotAccountStateChanged(int)));
-    connect(_jobRestartTimer, SIGNAL(timeout()), SLOT(slotCheckQuota()));
-    _jobRestartTimer->setSingleShot(true);
-    if (canGetQuota()) {
-        _jobRestartTimer->start(initialTimeT);
-    }
+            SLOT(slotAccountStateChanged()));
+    connect(&_jobRestartTimer, SIGNAL(timeout()), SLOT(slotCheckQuota()));
+    _jobRestartTimer.setSingleShot(true);
+}
+
+void QuotaInfo::setActive(bool active)
+{
+    _active = active;
+    slotAccountStateChanged();
 }
 
-void QuotaInfo::slotAccountStateChanged(int /*state*/)
+
+void QuotaInfo::slotAccountStateChanged()
 {
     if (canGetQuota()) {
-        _jobRestartTimer->start(initialTimeT);
+        if (_lastQuotaRecieved.isNull()
+                || _lastQuotaRecieved.msecsTo(QDateTime::currentDateTime()) > defaultIntervalT) {
+            slotCheckQuota();
+        } else {
+            _jobRestartTimer.start(defaultIntervalT);
+        }
     } else {
-        _jobRestartTimer->stop();
+        _jobRestartTimer.stop();
     }
 }
 
@@ -57,12 +65,12 @@ void QuotaInfo::slotRequestFailed()
 {
     _lastQuotaTotalBytes = 0;
     _lastQuotaUsedBytes = 0;
-    _jobRestartTimer->start(failIntervalT);
+    _jobRestartTimer.start(failIntervalT);
 }
 
 bool QuotaInfo::canGetQuota() const
 {
-    if (! _accountState) {
+    if (! _accountState || !_active) {
         return false;
     }
     AccountPtr account = _accountState->account();
@@ -93,7 +101,8 @@ void QuotaInfo::slotUpdateLastQuota(const QVariantMap &result)
     _lastQuotaUsedBytes = result["quota-used-bytes"].toDouble();
     _lastQuotaTotalBytes = _lastQuotaUsedBytes + avail;
     emit quotaUpdated(_lastQuotaTotalBytes, _lastQuotaUsedBytes);
-    _jobRestartTimer->start(defaultIntervalT);
+    _jobRestartTimer.start(defaultIntervalT);
+    _lastQuotaRecieved = QDateTime::currentDateTime();
 }
 
 }
diff --git a/src/gui/quotainfo.h b/src/gui/quotainfo.h
index 385da01..583d1ed 100644
--- a/src/gui/quotainfo.h
+++ b/src/gui/quotainfo.h
@@ -17,27 +17,46 @@
 #include <QObject>
 #include <QPointer>
 #include <QVariant>
-
-class QTimer;
+#include <QTimer>
+#include <QDateTime>
 
 namespace OCC {
-
 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 {
     Q_OBJECT
 public:
-    QuotaInfo(AccountState *account);
+    explicit QuotaInfo(OCC::AccountState* accountState, QObject* parent = 0);
 
     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 imediatly if the last time
+     * the quota was requested was more than the interval
+     */
+    void setActive(bool active);
+
 public Q_SLOTS:
     void slotCheckQuota();
 
 private Q_SLOTS:
     void slotUpdateLastQuota(const QVariantMap &);
-    void slotAccountStateChanged(int state);
+    void slotAccountStateChanged();
     void slotRequestFailed();
 
 Q_SIGNALS:
@@ -49,7 +68,9 @@ private:
     QPointer<AccountState> _accountState;
     qint64 _lastQuotaTotalBytes;
     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)
 };
 
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud-client.git



More information about the Pkg-owncloud-commits mailing list