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 "mirall/quotainfo.h"
|
|
|
|
#include "mirall/account.h"
|
|
|
|
#include "mirall/networkjobs.h"
|
|
|
|
#include "creds/abstractcredentials.h"
|
|
|
|
|
|
|
|
#include <QTimer>
|
2013-11-23 03:06:19 +04:00
|
|
|
#include <QDebug>
|
2013-11-07 13:14:38 +04:00
|
|
|
|
|
|
|
namespace Mirall {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static const int defaultIntervalT = 30*1000;
|
|
|
|
static const int failIntervalT = 5*1000;
|
|
|
|
static const int initialTimeT = 1*1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
QuotaInfo::QuotaInfo(QObject *parent)
|
|
|
|
: QObject(parent)
|
2013-11-23 03:06:19 +04:00
|
|
|
, _account(AccountManager::instance()->account())
|
2013-11-07 13:14:38 +04:00
|
|
|
, _lastQuotaTotalBytes(0)
|
|
|
|
, _lastQuotaUsedBytes(0)
|
|
|
|
, _refreshTimer(new QTimer(this))
|
|
|
|
{
|
|
|
|
connect(AccountManager::instance(), SIGNAL(accountChanged(Account*,Account*)),
|
|
|
|
SLOT(slotAccountChanged(Account*,Account*)));
|
|
|
|
connect(_refreshTimer, SIGNAL(timeout()), SLOT(slotCheckQuota()));
|
|
|
|
_refreshTimer->setSingleShot(true);
|
|
|
|
_refreshTimer->start(initialTimeT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuotaInfo::slotAccountChanged(Account *newAccount, Account *oldAccount)
|
|
|
|
{
|
|
|
|
_account = newAccount;
|
2013-11-25 18:33:35 +04:00
|
|
|
disconnect(oldAccount, SIGNAL(stateChanged(int)), this, SLOT(slotAccountStateChanged(int)));
|
|
|
|
connect(newAccount, SIGNAL(stateChanged(int)), this, SLOT(slotAccountStateChanged(int)));
|
2013-11-21 18:33:37 +04:00
|
|
|
}
|
|
|
|
|
2013-11-25 18:33:35 +04:00
|
|
|
void QuotaInfo::slotAccountStateChanged(int state)
|
2013-11-21 18:33:37 +04:00
|
|
|
{
|
2013-11-25 18:33:35 +04:00
|
|
|
if (state == Account::Connected) {
|
2013-11-23 03:06:19 +04:00
|
|
|
slotCheckQuota();
|
2013-11-21 18:33:37 +04:00
|
|
|
} else {
|
|
|
|
_refreshTimer->stop();
|
|
|
|
}
|
2013-11-07 13:14:38 +04:00
|
|
|
}
|
|
|
|
|
2013-12-12 13:53:51 +04:00
|
|
|
void QuotaInfo::slotRequestFailed()
|
|
|
|
{
|
|
|
|
_refreshTimer->start(failIntervalT);
|
|
|
|
}
|
|
|
|
|
2013-11-07 13:14:38 +04:00
|
|
|
void QuotaInfo::slotCheckQuota()
|
|
|
|
{
|
2014-01-28 14:06:40 +04:00
|
|
|
if (!_account.isNull() && _account->credentials() && _account->credentials()->ready()
|
|
|
|
&& _account->state() == Account::Connected) {
|
2013-11-07 13:14:38 +04:00
|
|
|
CheckQuotaJob *job = new CheckQuotaJob(_account, "/", this);
|
|
|
|
connect(job, SIGNAL(quotaRetrieved(qint64,qint64)), SLOT(slotUpdateLastQuota(qint64,qint64)));
|
2013-12-12 13:53:51 +04:00
|
|
|
connect(job, SIGNAL(networkError(QNetworkReply*)), SLOT(slotRequestFailed()));
|
2013-11-14 22:20:09 +04:00
|
|
|
job->start();
|
2013-11-07 13:14:38 +04:00
|
|
|
} else {
|
|
|
|
_lastQuotaTotalBytes = 0;
|
|
|
|
_lastQuotaUsedBytes = 0;
|
|
|
|
_refreshTimer->start(failIntervalT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QuotaInfo::slotUpdateLastQuota(qint64 total, qint64 used)
|
|
|
|
{
|
|
|
|
_lastQuotaTotalBytes = total;
|
|
|
|
_lastQuotaUsedBytes = used;
|
|
|
|
emit quotaUpdated(total, used);
|
2013-12-11 16:46:54 +04:00
|
|
|
_refreshTimer->start(defaultIntervalT);
|
2013-11-07 13:14:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|