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.
|
|
|
|
*/
|
|
|
|
|
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"
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-17 16:09:57 +03:00
|
|
|
QuotaInfo::QuotaInfo(AccountState *accountState)
|
|
|
|
: QObject(accountState)
|
|
|
|
, _accountState(accountState)
|
2013-11-07 13:14:38 +04:00
|
|
|
, _lastQuotaTotalBytes(0)
|
|
|
|
, _lastQuotaUsedBytes(0)
|
2014-02-20 20:00:45 +04:00
|
|
|
, _jobRestartTimer(new QTimer(this))
|
2013-11-07 13:14:38 +04:00
|
|
|
{
|
2014-12-17 16:09:57 +03:00
|
|
|
connect(accountState, SIGNAL(stateChanged(int)),
|
2014-12-11 17:43:48 +03:00
|
|
|
SLOT(slotAccountStateChanged(int)));
|
2014-02-20 20:00:45 +04:00
|
|
|
connect(_jobRestartTimer, SIGNAL(timeout()), SLOT(slotCheckQuota()));
|
|
|
|
_jobRestartTimer->setSingleShot(true);
|
|
|
|
_jobRestartTimer->start(initialTimeT);
|
2013-11-07 13:14:38 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 18:33:35 +04:00
|
|
|
void QuotaInfo::slotAccountStateChanged(int state)
|
2013-11-21 18:33:37 +04:00
|
|
|
{
|
2014-12-17 16:09:57 +03:00
|
|
|
if (state == AccountState::Connected) {
|
2013-11-23 03:06:19 +04:00
|
|
|
slotCheckQuota();
|
2014-12-11 17:43:48 +03:00
|
|
|
} else {
|
|
|
|
_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;
|
|
|
|
_jobRestartTimer->start(failIntervalT);
|
2013-11-07 13:14:38 +04:00
|
|
|
}
|
|
|
|
|
2014-04-19 11:45:54 +04:00
|
|
|
void QuotaInfo::slotCheckQuota()
|
|
|
|
{
|
2014-12-17 16:09:57 +03:00
|
|
|
if (!_accountState) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-18 14:09:48 +03:00
|
|
|
AccountPtr account = _accountState->account();
|
2014-12-17 16:09:57 +03:00
|
|
|
if (_accountState->isConnected()
|
|
|
|
&& account->credentials()
|
|
|
|
&& account->credentials()->ready()) {
|
|
|
|
CheckQuotaJob *job = new CheckQuotaJob(account, "/", this);
|
2014-04-19 11:45:54 +04:00
|
|
|
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);
|
2014-02-20 20:00:45 +04:00
|
|
|
_jobRestartTimer->start(defaultIntervalT);
|
2013-11-07 13:14:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|