2013-11-07 09:14:38 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.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
|
2016-10-25 09:00:07 +00:00
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2013-11-07 09:14:38 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2014-07-10 22:31:24 +00:00
|
|
|
#include "quotainfo.h"
|
|
|
|
#include "account.h"
|
2014-12-17 13:09:57 +00:00
|
|
|
#include "accountstate.h"
|
2014-07-10 22:31:24 +00:00
|
|
|
#include "networkjobs.h"
|
2016-03-17 14:33:37 +00:00
|
|
|
#include "folderman.h"
|
2013-11-07 09:14:38 +00:00
|
|
|
#include "creds/abstractcredentials.h"
|
2016-04-22 08:05:50 +00:00
|
|
|
#include <theme.h>
|
2013-11-07 09:14:38 +00:00
|
|
|
|
|
|
|
#include <QTimer>
|
2013-11-22 23:06:19 +00:00
|
|
|
#include <QDebug>
|
2013-11-07 09:14:38 +00:00
|
|
|
|
2014-11-09 21:34:07 +00:00
|
|
|
namespace OCC {
|
2013-11-07 09:14:38 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
static const int defaultIntervalT = 30*1000;
|
|
|
|
static const int failIntervalT = 5*1000;
|
|
|
|
}
|
|
|
|
|
2015-06-26 13:40:34 +00:00
|
|
|
QuotaInfo::QuotaInfo(AccountState *accountState, QObject *parent)
|
|
|
|
: QObject(parent)
|
2014-12-17 13:09:57 +00:00
|
|
|
, _accountState(accountState)
|
2013-11-07 09:14:38 +00:00
|
|
|
, _lastQuotaTotalBytes(0)
|
|
|
|
, _lastQuotaUsedBytes(0)
|
2015-06-26 13:40:34 +00:00
|
|
|
, _active(false)
|
2013-11-07 09:14:38 +00:00
|
|
|
{
|
2014-12-17 13:09:57 +00:00
|
|
|
connect(accountState, SIGNAL(stateChanged(int)),
|
2015-06-26 13:40:34 +00:00
|
|
|
SLOT(slotAccountStateChanged()));
|
|
|
|
connect(&_jobRestartTimer, SIGNAL(timeout()), SLOT(slotCheckQuota()));
|
|
|
|
_jobRestartTimer.setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuotaInfo::setActive(bool active)
|
|
|
|
{
|
|
|
|
_active = active;
|
|
|
|
slotAccountStateChanged();
|
2013-11-07 09:14:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-26 13:40:34 +00:00
|
|
|
|
|
|
|
void QuotaInfo::slotAccountStateChanged()
|
2013-11-21 14:33:37 +00:00
|
|
|
{
|
2015-02-12 14:16:10 +00:00
|
|
|
if (canGetQuota()) {
|
2015-06-26 16:04:27 +00:00
|
|
|
auto elapsed = _lastQuotaRecieved.msecsTo(QDateTime::currentDateTime());
|
|
|
|
if (_lastQuotaRecieved.isNull() || elapsed >= defaultIntervalT) {
|
2015-06-26 13:40:34 +00:00
|
|
|
slotCheckQuota();
|
|
|
|
} else {
|
2015-06-26 16:04:27 +00:00
|
|
|
_jobRestartTimer.start(defaultIntervalT - elapsed);
|
2015-06-26 13:40:34 +00:00
|
|
|
}
|
2014-12-11 14:43:48 +00:00
|
|
|
} else {
|
2015-06-26 13:40:34 +00:00
|
|
|
_jobRestartTimer.stop();
|
2013-11-21 14:33:37 +00:00
|
|
|
}
|
2013-11-07 09:14:38 +00:00
|
|
|
}
|
|
|
|
|
2013-12-12 09:53:51 +00:00
|
|
|
void QuotaInfo::slotRequestFailed()
|
|
|
|
{
|
2014-02-26 10:26:50 +00:00
|
|
|
_lastQuotaTotalBytes = 0;
|
|
|
|
_lastQuotaUsedBytes = 0;
|
2015-06-26 13:40:34 +00:00
|
|
|
_jobRestartTimer.start(failIntervalT);
|
2013-11-07 09:14:38 +00:00
|
|
|
}
|
|
|
|
|
2015-02-12 14:16:10 +00:00
|
|
|
bool QuotaInfo::canGetQuota() const
|
|
|
|
{
|
2015-06-26 13:40:34 +00:00
|
|
|
if (! _accountState || !_active) {
|
2015-02-12 14:16:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
AccountPtr account = _accountState->account();
|
|
|
|
return _accountState->isConnected()
|
|
|
|
&& account->credentials()
|
|
|
|
&& account->credentials()->ready();
|
|
|
|
}
|
|
|
|
|
2016-03-17 14:33:37 +00:00
|
|
|
QString QuotaInfo::quotaBaseFolder() const
|
|
|
|
{
|
2016-04-22 08:05:50 +00:00
|
|
|
return Theme::instance()->quotaBaseFolder();
|
2016-03-17 14:33:37 +00:00
|
|
|
}
|
|
|
|
|
2014-04-19 07:45:54 +00:00
|
|
|
void QuotaInfo::slotCheckQuota()
|
|
|
|
{
|
2015-02-12 14:16:10 +00:00
|
|
|
if (! canGetQuota()) {
|
2014-12-17 13:09:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-26 14:58:34 +00:00
|
|
|
if (_job) {
|
|
|
|
// The previous job was not finished? Then we cancel it!
|
|
|
|
_job->deleteLater();
|
|
|
|
}
|
|
|
|
|
2014-12-18 11:09:48 +00:00
|
|
|
AccountPtr account = _accountState->account();
|
2016-03-17 14:33:37 +00:00
|
|
|
_job = new PropfindJob(account, quotaBaseFolder(), this);
|
2015-06-26 14:58:34 +00:00
|
|
|
_job->setProperties(QList<QByteArray>() << "quota-available-bytes" << "quota-used-bytes");
|
|
|
|
connect(_job, SIGNAL(result(QVariantMap)), SLOT(slotUpdateLastQuota(QVariantMap)));
|
|
|
|
connect(_job, SIGNAL(networkError(QNetworkReply*)), SLOT(slotRequestFailed()));
|
|
|
|
_job->start();
|
2014-04-19 07:45:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 12:28:15 +00:00
|
|
|
void QuotaInfo::slotUpdateLastQuota(const QVariantMap &result)
|
2013-11-07 09:14:38 +00:00
|
|
|
{
|
2015-10-05 04:21:19 +00:00
|
|
|
// The server can return fractional bytes (#1374)
|
2015-06-25 12:28:15 +00:00
|
|
|
// <d:quota-available-bytes>1374532061.2</d:quota-available-bytes>
|
2015-10-29 13:33:29 +00:00
|
|
|
qint64 avail = result["quota-available-bytes"].toDouble();
|
2015-06-25 12:28:15 +00:00
|
|
|
_lastQuotaUsedBytes = result["quota-used-bytes"].toDouble();
|
2015-10-29 13:33:29 +00:00
|
|
|
// negative value of the available quota have special meaning (#3940)
|
|
|
|
_lastQuotaTotalBytes = avail >= 0 ? _lastQuotaUsedBytes + avail : avail;
|
2015-06-25 12:28:15 +00:00
|
|
|
emit quotaUpdated(_lastQuotaTotalBytes, _lastQuotaUsedBytes);
|
2015-06-26 13:40:34 +00:00
|
|
|
_jobRestartTimer.start(defaultIntervalT);
|
|
|
|
_lastQuotaRecieved = QDateTime::currentDateTime();
|
2013-11-07 09:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|