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