2020-02-14 04:10:01 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
|
2020-10-15 03:15:42 +03:00
|
|
|
* Copyright (C) by Michael Schuster <michael@schuster.ms>
|
2020-02-14 04:10:01 +03:00
|
|
|
*
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 "userinfo.h"
|
|
|
|
#include "account.h"
|
|
|
|
#include "accountstate.h"
|
|
|
|
#include "networkjobs.h"
|
|
|
|
#include "folderman.h"
|
|
|
|
#include "creds/abstractcredentials.h"
|
|
|
|
#include <theme.h>
|
|
|
|
|
|
|
|
#include <QTimer>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static const int defaultIntervalT = 30 * 1000;
|
|
|
|
static const int failIntervalT = 5 * 1000;
|
|
|
|
}
|
|
|
|
|
|
|
|
UserInfo::UserInfo(AccountState *accountState, bool allowDisconnectedAccountState, bool fetchAvatarImage, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, _accountState(accountState)
|
|
|
|
, _allowDisconnectedAccountState(allowDisconnectedAccountState)
|
|
|
|
, _fetchAvatarImage(fetchAvatarImage)
|
|
|
|
{
|
|
|
|
connect(accountState, &AccountState::stateChanged,
|
|
|
|
this, &UserInfo::slotAccountStateChanged);
|
|
|
|
connect(&_jobRestartTimer, &QTimer::timeout, this, &UserInfo::slotFetchInfo);
|
|
|
|
_jobRestartTimer.setSingleShot(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserInfo::setActive(bool active)
|
|
|
|
{
|
|
|
|
_active = active;
|
|
|
|
slotAccountStateChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void UserInfo::slotAccountStateChanged()
|
|
|
|
{
|
|
|
|
if (canGetInfo()) {
|
2020-08-18 22:46:30 +03:00
|
|
|
// Obviously assumes there will never be more than thousand of hours between last info
|
|
|
|
// received and now, hence why we static_cast
|
|
|
|
auto elapsed = static_cast<int>(_lastInfoReceived.msecsTo(QDateTime::currentDateTime()));
|
2020-02-14 04:10:01 +03:00
|
|
|
if (_lastInfoReceived.isNull() || elapsed >= defaultIntervalT) {
|
|
|
|
slotFetchInfo();
|
|
|
|
} else {
|
|
|
|
_jobRestartTimer.start(defaultIntervalT - elapsed);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_jobRestartTimer.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserInfo::slotRequestFailed()
|
|
|
|
{
|
|
|
|
_lastQuotaTotalBytes = 0;
|
|
|
|
_lastQuotaUsedBytes = 0;
|
|
|
|
_jobRestartTimer.start(failIntervalT);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UserInfo::canGetInfo() const
|
|
|
|
{
|
|
|
|
if (!_accountState || !_active) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
AccountPtr account = _accountState->account();
|
|
|
|
return (_accountState->isConnected() || _allowDisconnectedAccountState)
|
|
|
|
&& account->credentials()
|
|
|
|
&& account->credentials()->ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserInfo::slotFetchInfo()
|
|
|
|
{
|
|
|
|
if (!canGetInfo()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_job) {
|
|
|
|
// The previous job was not finished? Then we cancel it!
|
|
|
|
_job->deleteLater();
|
|
|
|
}
|
|
|
|
|
|
|
|
AccountPtr account = _accountState->account();
|
|
|
|
_job = new JsonApiJob(account, QLatin1String("ocs/v1.php/cloud/user"), this);
|
|
|
|
_job->setTimeout(20 * 1000);
|
|
|
|
connect(_job.data(), &JsonApiJob::jsonReceived, this, &UserInfo::slotUpdateLastInfo);
|
|
|
|
connect(_job.data(), &AbstractNetworkJob::networkError, this, &UserInfo::slotRequestFailed);
|
|
|
|
_job->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UserInfo::slotUpdateLastInfo(const QJsonDocument &json)
|
|
|
|
{
|
|
|
|
auto objData = json.object().value("ocs").toObject().value("data").toObject();
|
|
|
|
|
|
|
|
AccountPtr account = _accountState->account();
|
|
|
|
|
2023-08-30 19:34:35 +03:00
|
|
|
if (const auto newUserId = objData.value("id").toString(); !newUserId.isEmpty()) {
|
2023-08-15 19:20:18 +03:00
|
|
|
if (QString::compare(account->davUser(), newUserId, Qt::CaseInsensitive) != 0) {
|
|
|
|
// TODO: the error message should be in the UI
|
2023-08-30 19:34:35 +03:00
|
|
|
qInfo() << "Authenticated with the wrong user! Please login with the account:" << account->prettyName();
|
2023-08-15 19:20:18 +03:00
|
|
|
if (const auto cred = account->credentials()) {
|
|
|
|
account->credentials()->askFromUser();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
account->setDavUser(newUserId);
|
2020-02-14 04:10:01 +03:00
|
|
|
}
|
2023-08-15 19:20:18 +03:00
|
|
|
|
2020-02-14 04:10:01 +03:00
|
|
|
QString displayName = objData.value("display-name").toString();
|
|
|
|
if (!displayName.isEmpty()) {
|
|
|
|
account->setDavDisplayName(displayName);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto objQuota = objData.value("quota").toObject();
|
|
|
|
qint64 used = objQuota.value("used").toDouble();
|
2020-07-01 22:30:17 +03:00
|
|
|
qint64 total = objQuota.value("quota").toDouble();
|
2020-02-14 04:10:01 +03:00
|
|
|
|
|
|
|
if(_lastInfoReceived.isNull() || _lastQuotaUsedBytes != used || _lastQuotaTotalBytes != total) {
|
|
|
|
_lastQuotaUsedBytes = used;
|
|
|
|
_lastQuotaTotalBytes = total;
|
|
|
|
emit quotaUpdated(_lastQuotaTotalBytes, _lastQuotaUsedBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
_jobRestartTimer.start(defaultIntervalT);
|
|
|
|
_lastInfoReceived = QDateTime::currentDateTime();
|
|
|
|
|
|
|
|
if(_fetchAvatarImage) {
|
2020-05-18 21:54:23 +03:00
|
|
|
auto *job = new AvatarJob(account, account->davUser(), 128, this);
|
2020-02-14 04:10:01 +03:00
|
|
|
job->setTimeout(20 * 1000);
|
|
|
|
QObject::connect(job, &AvatarJob::avatarPixmap, this, &UserInfo::slotAvatarImage);
|
|
|
|
job->start();
|
2023-08-15 19:20:18 +03:00
|
|
|
return;
|
2020-02-14 04:10:01 +03:00
|
|
|
}
|
2023-08-15 19:20:18 +03:00
|
|
|
|
|
|
|
emit fetchedLastInfo(this);
|
2020-02-14 04:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void UserInfo::slotAvatarImage(const QImage &img)
|
|
|
|
{
|
|
|
|
_accountState->account()->setAvatar(img);
|
|
|
|
|
|
|
|
emit fetchedLastInfo(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace OCC
|