Skip to content

Commit

Permalink
Add space id to folder and use it
Browse files Browse the repository at this point in the history
Ideally we would no longer need to store the webdav url for spaces,
but while the client is starting up or we are offline when starting the client,
we don't have the relevant information available.

Fixes: #10936
  • Loading branch information
TheOneRing committed Aug 11, 2023
1 parent 0da7e98 commit 23bbc69
Show file tree
Hide file tree
Showing 18 changed files with 180 additions and 80 deletions.
75 changes: 68 additions & 7 deletions src/gui/folder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "accountstate.h"
#include "application.h"
#include "common/checksums.h"
#include "common/depreaction.h"
#include "common/filesystembase.h"
#include "common/syncjournalfilerecord.h"
#include "common/version.h"
Expand All @@ -30,6 +31,7 @@
#include "filesystem.h"
#include "folderman.h"
#include "folderwatcher.h"
#include "libsync/graphapi/spacesmanager.h"
#include "localdiscoverytracker.h"
#include "networkjobs.h"
#include "scheduling/syncscheduler.h"
Expand Down Expand Up @@ -69,8 +71,14 @@ auto versionC()

auto davUrlC()
{
return QLatin1String("davUrl");
return QStringLiteral("davUrl");
}

auto spaceIdC()
{
return QStringLiteral("spaceId");
}

auto displayNameC()
{
return QLatin1String("displayString");
Expand Down Expand Up @@ -368,6 +376,12 @@ QString Folder::remotePath() const

QUrl Folder::webDavUrl() const
{
const QString spaceId = _definition.spaceId();
if (!spaceId.isEmpty()) {
if (auto *space = _accountState->account()->spacesManager()->space(spaceId)) {
return QUrl(space->drive().getRoot().getWebDavUrl());
}
}
return _definition.webDavUrl();
}

Expand Down Expand Up @@ -774,15 +788,29 @@ void Folder::saveToSettings() const
removeFromSettings();

auto settings = _accountState->settings();

settings->beginGroup(QStringLiteral("Folders"));

auto definitionToSave = _definition;

// migration
if (accountState()->supportsSpaces() && _definition.spaceId().isEmpty()) {
OC_DISABLE_DEPRECATED_WARNING
if (auto *space = accountState()->account()->spacesManager()->spaceByUrl(webDavUrl())) {
OC_ENABLE_DEPRECATED_WARNING
definitionToSave.setSpaceId(space->drive().getRoot().getId());
}
}
// with spaces we rely on the space id
// we save the dav url nevertheless to have it available during startup
definitionToSave.setWebDavUrl(webDavUrl());

// Note: Each of these groups might have a "version" tag, but that's
// currently unused.
settings->beginGroup(QString::fromUtf8(_definition.id()));
FolderDefinition::save(*settings, _definition);
settings->beginGroup(QString::fromUtf8(definitionToSave.id()));
FolderDefinition::save(*settings, definitionToSave);

settings->sync();
qCInfo(lcFolder) << "Saved folder" << _definition.localPath() << "to settings, status" << settings->status();
qCInfo(lcFolder) << "Saved folder" << definitionToSave.localPath() << "to settings, status" << settings->status();
}

void Folder::removeFromSettings() const
Expand Down Expand Up @@ -1244,8 +1272,9 @@ void Folder::slotAboutToRemoveAllFiles(SyncFileItem::Direction direction)
ownCloudGui::raiseDialog(msgBox);
}

FolderDefinition::FolderDefinition(const QByteArray &id, const QUrl &davUrl, const QString &displayName)
FolderDefinition::FolderDefinition(const QByteArray &id, const QUrl &davUrl, const QString &spaceId, const QString &displayName)
: _webDavUrl(davUrl)
, _spaceId(spaceId)
, _id(id)
, _displayName(displayName)
{
Expand All @@ -1266,6 +1295,9 @@ void FolderDefinition::save(QSettings &settings, const FolderDefinition &folder)
settings.setValue(QStringLiteral("localPath"), folder.localPath());
settings.setValue(QStringLiteral("journalPath"), folder.journalPath);
settings.setValue(QStringLiteral("targetPath"), folder.targetPath());
if (!folder.spaceId().isEmpty()) {
settings.setValue(spaceIdC(), folder.spaceId());
}
settings.setValue(davUrlC(), folder.webDavUrl());
settings.setValue(displayNameC(), folder.displayName());
settings.setValue(QStringLiteral("paused"), folder.paused);
Expand All @@ -1281,7 +1313,7 @@ void FolderDefinition::save(QSettings &settings, const FolderDefinition &folder)

FolderDefinition FolderDefinition::load(QSettings &settings, const QByteArray &id)
{
FolderDefinition folder(id, settings.value(davUrlC()).toUrl(), settings.value(displayNameC()).toString());
FolderDefinition folder{id, settings.value(davUrlC()).toUrl(), settings.value(spaceIdC()).toString(), settings.value(displayNameC()).toString()};
folder.setLocalPath(settings.value(QStringLiteral("localPath")).toString());
folder.journalPath = settings.value(QStringLiteral("journalPath")).toString();
folder.setTargetPath(settings.value(QStringLiteral("targetPath")).toString());
Expand Down Expand Up @@ -1363,8 +1395,37 @@ bool Folder::groupInSidebar() const
return false;
}

QString Folder::spaceId() const
{
return _definition.spaceId();
}

bool FolderDefinition::isDeployed() const
{
return _deployed;
}

QUrl FolderDefinition::webDavUrl() const
{
Q_ASSERT(_webDavUrl.isValid());
return _webDavUrl;
}

QString FolderDefinition::targetPath() const
{
return _targetPath;
}

QString FolderDefinition::localPath() const
{
return _localPath;
}

QString FolderDefinition::spaceId() const
{
// we might call the function to check for the id
// anyhow one of the conditions needs to be true
Q_ASSERT(_webDavUrl.isValid() || !_spaceId.isEmpty());
return _spaceId;
}
} // namespace OCC
42 changes: 25 additions & 17 deletions src/gui/folder.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class LocalDiscoveryTracker;
class FolderDefinition
{
public:
static auto createNewFolderDefinition(const QUrl &davUrl, const QString &displayName = {})
static auto createNewFolderDefinition(const QUrl &davUrl, const QString &spaceId, const QString &displayName = {})
{
return FolderDefinition(QUuid::createUuid().toByteArray(QUuid::WithoutBraces), davUrl, displayName);
return FolderDefinition(QUuid::createUuid().toByteArray(QUuid::WithoutBraces), davUrl, spaceId, displayName);
}

/// path to the journal, usually relative to localPath
Expand Down Expand Up @@ -84,19 +84,20 @@ class FolderDefinition
/// journalPath relative to localPath.
QString absoluteJournalPath() const;

QString localPath() const
{
return _localPath;
}
QString targetPath() const
{
return _targetPath;
}
const QUrl &webDavUrl() const
{
Q_ASSERT(_webDavUrl.isValid());
return _webDavUrl;
}
QString localPath() const;

QString targetPath() const;

QUrl webDavUrl() const;

// could change in the case of spaces
void setWebDavUrl(const QUrl &url) { _webDavUrl = url; }

// when using spaces we don't store the dav url but the space id
// this id is then used to look up the dav url
QString spaceId() const;

void setSpaceId(const QString &spaceId) { _spaceId = spaceId; }

const QByteArray &id() const;

Expand All @@ -108,7 +109,6 @@ class FolderDefinition
*/
bool isDeployed() const;


/**
* Higher values mean more imortant
* Used for sorting
Expand All @@ -118,9 +118,12 @@ class FolderDefinition
void setPriority(uint32_t newPriority);

private:
FolderDefinition(const QByteArray &id, const QUrl &davUrl, const QString &displayName);
FolderDefinition(const QByteArray &id, const QUrl &davUrl, const QString &spaceId, const QString &displayName);

// oc10 and as cache for ocis
QUrl _webDavUrl;

QString _spaceId;
/// For legacy reasons this can be a string, new folder objects will use a uuid
QByteArray _id;
QString _displayName;
Expand Down Expand Up @@ -195,6 +198,11 @@ class Folder : public QObject
*/
QUrl webDavUrl() const;

/**
* The space Id (empty for oc10)
*/
QString spaceId() const;

/**
* remote folder path, always with a trailing /
*/
Expand Down
18 changes: 9 additions & 9 deletions src/gui/folderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void FolderMan::unloadAndDeleteAllFolders()
// clear the list of existing folders.
const auto folders = std::move(_folders);
for (auto *folder : folders) {
folder->saveToSettings();
_socketApi->slotUnregisterPath(folder);
folder->deleteLater();
}
Expand Down Expand Up @@ -875,16 +876,12 @@ bool FolderMan::checkVfsAvailability(const QString &path, Vfs::Mode mode) const
return unsupportedConfiguration(path) && Vfs::checkAvailability(path, mode);
}

Folder *FolderMan::addFolderFromWizard(const AccountStatePtr &accountStatePtr, const QString &localFolder, const QString &remotePath, const QUrl &webDavUrl, const QString &displayName, bool useVfs)
Folder *FolderMan::addFolderFromWizard(const AccountStatePtr &accountStatePtr, FolderDefinition &&folderDefinition, bool useVfs)
{
if (!FolderMan::prepareFolder(localFolder)) {
if (!FolderMan::prepareFolder(folderDefinition.localPath())) {
return {};
}

qCInfo(lcFolderMan) << "Adding folder definition for" << localFolder << remotePath;
auto folderDefinition = FolderDefinition::createNewFolderDefinition(webDavUrl, displayName);
folderDefinition.setLocalPath(localFolder);
folderDefinition.setTargetPath(remotePath);
folderDefinition.ignoreHiddenFiles = ignoreHiddenFiles();

if (useVfs) {
Expand All @@ -896,13 +893,13 @@ Folder *FolderMan::addFolderFromWizard(const AccountStatePtr &accountStatePtr, c
if (newFolder) {
// With spaces we only handle the main folder
if (!newFolder->groupInSidebar()) {
Utility::setupFavLink(localFolder);
Utility::setupFavLink(folderDefinition.localPath());
}
if (!ConfigFile().newBigFolderSizeLimit().first) {
// The user already accepted the selective sync dialog. everything is in the white list
newFolder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, {QStringLiteral("/")});
}
qCDebug(lcFolderMan) << "Local sync folder" << localFolder << "successfully created!";
qCDebug(lcFolderMan) << "Local sync folder" << folderDefinition.localPath() << "successfully created!";
newFolder->saveToSettings();
} else {
qCWarning(lcFolderMan) << "Failed to create local sync folder!";
Expand All @@ -912,7 +909,10 @@ Folder *FolderMan::addFolderFromWizard(const AccountStatePtr &accountStatePtr, c

Folder *FolderMan::addFolderFromFolderWizardResult(const AccountStatePtr &accountStatePtr, const FolderWizard::Result &config)
{
auto f = addFolderFromWizard(accountStatePtr, config.localPath, config.remotePath, config.davUrl, config.displayName, config.useVirtualFiles);
FolderDefinition definition = FolderDefinition::createNewFolderDefinition(config.davUrl, config.spaceId, config.displayName);
definition.setLocalPath(config.localPath);
definition.setTargetPath(config.remotePath);
auto f = addFolderFromWizard(accountStatePtr, std::move(definition), config.useVirtualFiles);
if (f) {
f->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, config.selectiveSyncBlackList);
f->setPriority(config.priority);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/folderman.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class FolderMan : public QObject
* Adds a folder for an account. Used to be part of the wizard code base. Constructs the folder definition from the parameters.
* In case Wizard::SyncMode::SelectiveSync is used, nullptr is returned.
*/
Folder *addFolderFromWizard(const AccountStatePtr &accountStatePtr, const QString &localFolder, const QString &remotePath, const QUrl &webDavUrl, const QString &displayName, bool useVfs);
Folder *addFolderFromWizard(const AccountStatePtr &accountStatePtr, FolderDefinition &&definition, bool useVfs);
Folder *addFolderFromFolderWizardResult(const AccountStatePtr &accountStatePtr, const FolderWizard::Result &config);

/** Removes a folder */
Expand Down
59 changes: 32 additions & 27 deletions src/gui/folderstatusmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,37 +47,38 @@ namespace {
const char propertyParentIndexC[] = "oc_parentIndex";
const char propertyPermissionMap[] = "oc_permissionMap";

int64_t getQuota(const AccountStatePtr &accountState, const QUrl &davUrl, FolderStatusModel::Columns type)
int64_t getQuota(const AccountStatePtr &accountState, const QString &spaceId, FolderStatusModel::Columns type)
{
if (accountState->supportsSpaces()) {
if (auto spacesManager = accountState->account()->spacesManager()) {
const auto *space = spacesManager->spaceByUrl(davUrl);
if (space) {
const auto quota = space->drive().getQuota();
if (quota.isValid()) {
switch (type) {
case FolderStatusModel::Columns::QuotaTotal:
return quota.getTotal();
case FolderStatusModel::Columns::QuotaUsed:
return quota.getUsed();
default:
Q_UNREACHABLE();
}
if (auto spacesManager = accountState->account()->spacesManager()) {
const auto *space = spacesManager->space(spaceId);
if (space) {
const auto quota = space->drive().getQuota();
if (quota.isValid()) {
switch (type) {
case FolderStatusModel::Columns::QuotaTotal:
return quota.getTotal();
case FolderStatusModel::Columns::QuotaUsed:
return quota.getUsed();
default:
Q_UNREACHABLE();
}
}
}
} else {
switch (type) {
case FolderStatusModel::Columns::QuotaTotal:
return accountState->quotaInfo()->lastQuotaTotalBytes();
case FolderStatusModel::Columns::QuotaUsed:
return accountState->quotaInfo()->lastQuotaUsedBytes();
default:
Q_UNREACHABLE();
}
}
return {};
}

int64_t getQuotaOc10(const AccountStatePtr &accountState, const QUrl &davUrl, FolderStatusModel::Columns type)
{
switch (type) {
case FolderStatusModel::Columns::QuotaTotal:
return accountState->quotaInfo()->lastQuotaTotalBytes();
case FolderStatusModel::Columns::QuotaUsed:
return accountState->quotaInfo()->lastQuotaUsedBytes();
default:
Q_UNREACHABLE();
}
}
}

FolderStatusModel::FolderStatusModel(QObject *parent)
Expand Down Expand Up @@ -246,7 +247,7 @@ QVariant FolderStatusModel::data(const QModelIndex &index, int role) const

const auto getSpace = [&]() -> GraphApi::Space * {
if (_accountState->supportsSpaces()) {
return _accountState->account()->spacesManager()->spaceByUrl(f->webDavUrl());
return _accountState->account()->spacesManager()->space(f->spaceId());
}
return nullptr;
};
Expand Down Expand Up @@ -323,9 +324,13 @@ QVariant FolderStatusModel::data(const QModelIndex &index, int role) const
case Columns::Priority:
return f->priority();
case Columns::QuotaTotal:
return QVariant::fromValue(getQuota(_accountState, f->webDavUrl(), Columns::QuotaTotal));
[[fallthrough]];
case Columns::QuotaUsed:
return QVariant::fromValue(getQuota(_accountState, f->webDavUrl(), Columns::QuotaUsed));
if (_accountState->supportsSpaces()) {
return QVariant::fromValue(getQuota(_accountState, f->spaceId(), column));
} else {
return QVariant::fromValue(getQuotaOc10(_accountState, f->webDavUrl(), column));
}
case Columns::IsUsingSpaces: // handled before
[[fallthrough]];
case Columns::ItemType: // handled before
Expand Down
Loading

0 comments on commit 23bbc69

Please sign in to comment.