nextcloud-desktop/src/gui/quotainfo.cpp

87 lines
2.4 KiB
C++
Raw Normal View History

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
* the Free Software Foundation; version 2 of the License.
*
* 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.
*/
#include "quotainfo.h"
#include "account.h"
#include "accountstate.h"
#include "networkjobs.h"
2013-11-07 13:14:38 +04:00
#include "creds/abstractcredentials.h"
#include <QTimer>
2013-11-23 03:06:19 +04:00
#include <QDebug>
2013-11-07 13:14:38 +04:00
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;
static const int initialTimeT = 1*1000;
}
QuotaInfo::QuotaInfo(AccountState *accountState)
: QObject(accountState)
, _accountState(accountState)
2013-11-07 13:14:38 +04:00
, _lastQuotaTotalBytes(0)
, _lastQuotaUsedBytes(0)
, _jobRestartTimer(new QTimer(this))
2013-11-07 13:14:38 +04:00
{
connect(accountState, SIGNAL(stateChanged(int)),
SLOT(slotAccountStateChanged(int)));
connect(_jobRestartTimer, SIGNAL(timeout()), SLOT(slotCheckQuota()));
_jobRestartTimer->setSingleShot(true);
_jobRestartTimer->start(initialTimeT);
2013-11-07 13:14:38 +04:00
}
void QuotaInfo::slotAccountStateChanged(int state)
{
if (state == AccountState::Connected) {
2013-11-23 03:06:19 +04:00
slotCheckQuota();
} else {
_jobRestartTimer->stop();
}
2013-11-07 13:14:38 +04:00
}
void QuotaInfo::slotRequestFailed()
{
2014-02-26 14:26:50 +04:00
_lastQuotaTotalBytes = 0;
_lastQuotaUsedBytes = 0;
_jobRestartTimer->start(failIntervalT);
2013-11-07 13:14:38 +04:00
}
void QuotaInfo::slotCheckQuota()
{
if (!_accountState) {
return;
}
AccountPtr account = _accountState->account();
if (_accountState->isConnected()
&& account->credentials()
&& account->credentials()->ready()) {
CheckQuotaJob *job = new CheckQuotaJob(account, "/", this);
connect(job, SIGNAL(quotaRetrieved(qint64,qint64)), SLOT(slotUpdateLastQuota(qint64,qint64)));
connect(job, SIGNAL(networkError(QNetworkReply*)), SLOT(slotRequestFailed()));
job->start();
}
}
2013-11-07 13:14:38 +04:00
void QuotaInfo::slotUpdateLastQuota(qint64 total, qint64 used)
{
_lastQuotaTotalBytes = total;
_lastQuotaUsedBytes = used;
emit quotaUpdated(total, used);
_jobRestartTimer->start(defaultIntervalT);
2013-11-07 13:14:38 +04:00
}
}