nextcloud-desktop/src/gui/folderstatusmodel.cpp

1277 lines
44 KiB
C++
Raw Normal View History

/*
* Copyright (C) by Klaas Freitag <freitag@kde.org>
*
* 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 "folderstatusmodel.h"
#include "folderman.h"
#include "accountstate.h"
2017-09-01 19:11:43 +03:00
#include "common/asserts.h"
#include <theme.h>
#include <account.h>
#include "folderstatusdelegate.h"
2015-06-02 21:01:13 +03:00
#include <QFileIconProvider>
2015-08-06 14:54:43 +03:00
#include <QVarLengthArray>
#include <set>
Q_DECLARE_METATYPE(QPersistentModelIndex)
2014-11-10 00:34:07 +03:00
namespace OCC {
Q_LOGGING_CATEGORY(lcFolderStatus, "nextcloud.gui.folder.model", QtInfoMsg)
static const char propertyParentIndexC[] = "oc_parentIndex";
static const char propertyPermissionMap[] = "oc_permissionMap";
2017-05-17 11:55:42 +03:00
static QString removeTrailingSlash(const QString &s)
{
if (s.endsWith('/')) {
return s.left(s.size() - 1);
}
return s;
}
FolderStatusModel::FolderStatusModel(QObject *parent)
2017-05-17 11:55:42 +03:00
: QAbstractItemModel(parent)
{
}
FolderStatusModel::~FolderStatusModel() = default;
2017-05-17 11:55:42 +03:00
static bool sortByFolderHeader(const FolderStatusModel::SubFolderInfo &lhs, const FolderStatusModel::SubFolderInfo &rhs)
2016-11-08 15:30:18 +03:00
{
return QString::compare(lhs._folder->shortGuiRemotePathOrAppName(),
2017-05-17 11:55:42 +03:00
rhs._folder->shortGuiRemotePathOrAppName(),
Qt::CaseInsensitive)
< 0;
2016-11-08 15:30:18 +03:00
}
2017-05-17 11:55:42 +03:00
void FolderStatusModel::setAccountState(const AccountState *accountState)
{
beginResetModel();
_dirty = false;
_folders.clear();
_accountState = accountState;
connect(FolderMan::instance(), &FolderMan::folderSyncStateChange,
this, &FolderStatusModel::slotFolderSyncStateChange, Qt::UniqueConnection);
connect(FolderMan::instance(), &FolderMan::scheduleQueueChanged,
this, &FolderStatusModel::slotFolderScheduleQueueChanged, Qt::UniqueConnection);
auto folders = FolderMan::instance()->map();
foreach (auto f, folders) {
2017-05-17 11:55:42 +03:00
if (!accountState)
break;
if (f->accountState() != accountState)
continue;
SubFolderInfo info;
info._name = f->alias();
info._path = "/";
info._folder = f;
info._checked = Qt::PartiallyChecked;
_folders << info;
connect(f, &Folder::progressInfo, this, &FolderStatusModel::slotSetProgress, Qt::UniqueConnection);
connect(f, &Folder::newBigFolderDiscovered, this, &FolderStatusModel::slotNewBigFolder, Qt::UniqueConnection);
}
2016-11-08 15:30:18 +03:00
// Sort by header text
2017-12-08 13:07:46 +03:00
std::sort(_folders.begin(), _folders.end(), sortByFolderHeader);
2016-11-08 15:30:18 +03:00
// Set the root _pathIdx after the sorting
for (int i = 0; i < _folders.size(); ++i) {
_folders[i]._pathIdx << i;
}
endResetModel();
emit dirtyChanged();
}
2017-05-17 11:55:42 +03:00
Qt::ItemFlags FolderStatusModel::flags(const QModelIndex &index) const
{
2017-05-17 11:55:42 +03:00
if (!_accountState) {
return {};
2017-05-17 11:55:42 +03:00
}
switch (classify(index)) {
2017-05-17 11:55:42 +03:00
case AddButton: {
Qt::ItemFlags ret;
ret = Qt::ItemNeverHasChildren;
if (!_accountState->isConnected()) {
return ret;
}
2017-05-17 11:55:42 +03:00
return Qt::ItemIsEnabled | ret;
}
case FetchLabel:
2017-09-13 07:26:56 +03:00
return Qt::ItemIsEnabled | Qt::ItemNeverHasChildren;
2017-05-17 11:55:42 +03:00
case RootFolder:
return Qt::ItemIsEnabled;
case SubFolder:
return Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsSelectable;
}
return {};
}
QVariant FolderStatusModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (role == Qt::EditRole)
return QVariant();
2017-05-17 11:55:42 +03:00
switch (classify(index)) {
case AddButton: {
if (role == FolderStatusDelegate::AddButton) {
return QVariant(true);
} else if (role == Qt::ToolTipRole) {
if (!_accountState->isConnected()) {
return tr("You need to be connected to add a folder");
2017-05-17 11:55:42 +03:00
}
return tr("Click this button to add a folder to synchronize.");
}
return QVariant();
}
2017-05-17 11:55:42 +03:00
case SubFolder: {
const auto &x = static_cast<SubFolderInfo *>(index.internalPointer())->_subs[index.row()];
switch (role) {
case Qt::DisplayRole:
//: Example text: "File.txt (23KB)"
return x._size < 0 ? x._name : tr("%1 (%2)").arg(x._name, Utility::octetsToString(x._size));
case Qt::ToolTipRole:
return QString(QLatin1String("<qt>") + Utility::escape(x._size < 0 ? x._name : tr("%1 (%2)").arg(x._name, Utility::octetsToString(x._size))) + QLatin1String("</qt>"));
case Qt::CheckStateRole:
return x._checked;
case Qt::DecorationRole: {
Q_ASSERT(x._folder->remotePath().startsWith('/'));
const auto rootPath = x._folder->remotePath().mid(1);
const auto absoluteRemotePath = rootPath.isEmpty() ? x._path : rootPath + '/' + x._path;
if (_accountState->account()->e2e()->isFolderEncrypted(absoluteRemotePath)) {
return QIcon(QLatin1String(":/client/theme/lock-https.svg"));
} else if (x._size > 0 && _accountState->account()->e2e()->isAnyParentFolderEncrypted(absoluteRemotePath)) {
return QIcon(QLatin1String(":/client/theme/lock-broken.svg"));
}
return QFileIconProvider().icon(x._isExternal ? QFileIconProvider::Network : QFileIconProvider::Folder);
}
case Qt::ForegroundRole:
if (x._isUndecided) {
return QColor(Qt::red);
}
break;
case FileIdRole:
return x._fileId;
case FolderStatusDelegate::FolderPathRole: {
auto f = x._folder;
if (!f)
return QVariant();
return QVariant(f->path() + x._path);
2017-05-17 11:55:42 +03:00
}
}
}
return QVariant();
2017-05-17 11:55:42 +03:00
case FetchLabel: {
const auto x = static_cast<SubFolderInfo *>(index.internalPointer());
2017-05-17 11:55:42 +03:00
switch (role) {
case Qt::DisplayRole:
if (x->_hasError) {
return QVariant(tr("Error while loading the list of folders from the server.")
+ QString("\n") + x->_lastErrorString);
} else {
return tr("Fetching folder list from server …");
2017-05-17 11:55:42 +03:00
}
break;
default:
return QVariant();
}
}
case RootFolder:
break;
}
2017-05-17 11:55:42 +03:00
const SubFolderInfo &folderInfo = _folders.at(index.row());
2015-07-03 15:28:04 +03:00
auto f = folderInfo._folder;
if (!f)
return QVariant();
2017-05-17 11:55:42 +03:00
const SubFolderInfo::Progress &progress = folderInfo._progress;
const bool accountConnected = _accountState->isConnected();
switch (role) {
2017-05-17 11:55:42 +03:00
case FolderStatusDelegate::FolderPathRole:
return f->shortGuiLocalPath();
case FolderStatusDelegate::FolderSecondPathRole:
return f->remotePath();
case FolderStatusDelegate::FolderConflictMsg:
return (f->syncResult().hasUnresolvedConflicts())
? QStringList(tr("There are unresolved conflicts. Click for details."))
: QStringList();
2017-05-17 11:55:42 +03:00
case FolderStatusDelegate::FolderErrorMsg:
return f->syncResult().errorStrings();
case FolderStatusDelegate::FolderInfoMsg:
return f->supportsVirtualFiles()
? QStringList(tr("Virtual file support is enabled."))
: QStringList();
2017-05-17 11:55:42 +03:00
case FolderStatusDelegate::SyncRunning:
return f->syncResult().status() == SyncResult::SyncRunning;
case FolderStatusDelegate::SyncDate:
return f->syncResult().syncTime();
2017-05-17 11:55:42 +03:00
case FolderStatusDelegate::HeaderRole:
return f->shortGuiRemotePathOrAppName();
case FolderStatusDelegate::FolderAliasRole:
return f->alias();
case FolderStatusDelegate::FolderSyncPaused:
return f->syncPaused();
case FolderStatusDelegate::FolderAccountConnected:
return accountConnected;
2015-11-09 18:38:48 +03:00
case Qt::ToolTipRole: {
QString toolTip;
if (!progress.isNull()) {
return progress._progressString;
}
2017-05-17 11:55:42 +03:00
if (accountConnected)
2015-11-09 18:38:48 +03:00
toolTip = Theme::instance()->statusHeaderText(f->syncResult().status());
else
2015-11-09 18:38:48 +03:00
toolTip = tr("Signed out");
toolTip += "\n";
toolTip += folderInfo._folder->path();
return toolTip;
}
case FolderStatusDelegate::FolderStatusIconRole:
2017-05-17 11:55:42 +03:00
if (accountConnected) {
auto theme = Theme::instance();
auto status = f->syncResult().status();
2017-05-17 11:55:42 +03:00
if (f->syncPaused()) {
return theme->folderDisabledIcon();
} else {
if (status == SyncResult::SyncPrepare || status == SyncResult::Undefined) {
return theme->syncStateIcon(SyncResult::SyncRunning);
} else {
// The "Problem" *result* just means some files weren't
// synced, so we show "Success" in these cases. But we
// do use the "Problem" *icon* for unresolved conflicts.
if (status == SyncResult::Success || status == SyncResult::Problem) {
if (f->syncResult().hasUnresolvedConflicts()) {
return theme->syncStateIcon(SyncResult::Problem);
} else {
return theme->syncStateIcon(SyncResult::Success);
}
} else {
2017-05-17 11:55:42 +03:00
return theme->syncStateIcon(status);
}
}
}
} else {
return Theme::instance()->folderOfflineIcon();
}
case FolderStatusDelegate::SyncProgressItemString:
2015-07-03 15:28:04 +03:00
return progress._progressString;
case FolderStatusDelegate::WarningCount:
2015-07-03 15:28:04 +03:00
return progress._warningCount;
case FolderStatusDelegate::SyncProgressOverallPercent:
2015-07-03 15:28:04 +03:00
return progress._overallPercent;
case FolderStatusDelegate::SyncProgressOverallString:
2015-07-03 15:28:04 +03:00
return progress._overallSyncString;
}
return QVariant();
}
2017-05-17 11:55:42 +03:00
bool FolderStatusModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
2017-05-17 11:55:42 +03:00
if (role == Qt::CheckStateRole) {
auto info = infoForIndex(index);
auto checked = static_cast<Qt::CheckState>(value.toInt());
if (info && info->_checked != checked) {
info->_checked = checked;
if (checked == Qt::Checked) {
// If we are checked, check that we may need to check the parent as well if
// all the siblings are also checked
QModelIndex parent = index.parent();
auto parentInfo = infoForIndex(parent);
if (parentInfo && parentInfo->_checked != Qt::Checked) {
bool hasUnchecked = false;
2017-05-17 11:55:42 +03:00
foreach (const auto &sub, parentInfo->_subs) {
if (sub._checked != Qt::Checked) {
hasUnchecked = true;
break;
}
}
if (!hasUnchecked) {
setData(parent, Qt::Checked, Qt::CheckStateRole);
} else if (parentInfo->_checked == Qt::Unchecked) {
setData(parent, Qt::PartiallyChecked, Qt::CheckStateRole);
}
}
// also check all the children
for (int i = 0; i < info->_subs.count(); ++i) {
if (info->_subs[i]._checked != Qt::Checked) {
setData(this->index(i, 0, index), Qt::Checked, Qt::CheckStateRole);
}
}
}
if (checked == Qt::Unchecked) {
QModelIndex parent = index.parent();
auto parentInfo = infoForIndex(parent);
if (parentInfo && parentInfo->_checked == Qt::Checked) {
setData(parent, Qt::PartiallyChecked, Qt::CheckStateRole);
}
// Uncheck all the children
for (int i = 0; i < info->_subs.count(); ++i) {
if (info->_subs[i]._checked != Qt::Unchecked) {
setData(this->index(i, 0, index), Qt::Unchecked, Qt::CheckStateRole);
}
}
}
if (checked == Qt::PartiallyChecked) {
QModelIndex parent = index.parent();
auto parentInfo = infoForIndex(parent);
if (parentInfo && parentInfo->_checked != Qt::PartiallyChecked) {
setData(parent, Qt::PartiallyChecked, Qt::CheckStateRole);
}
}
}
_dirty = true;
emit dirtyChanged();
2015-06-02 20:57:41 +03:00
emit dataChanged(index, index, QVector<int>() << role);
return true;
}
return QAbstractItemModel::setData(index, value, role);
}
2017-05-17 11:55:42 +03:00
int FolderStatusModel::columnCount(const QModelIndex &) const
{
return 1;
}
2017-05-17 11:55:42 +03:00
int FolderStatusModel::rowCount(const QModelIndex &parent) const
{
if (!parent.isValid()) {
2017-05-17 11:55:42 +03:00
if (Theme::instance()->singleSyncFolder() && _folders.count() != 0) {
// "Add folder" button not visible in the singleSyncFolder configuration.
return _folders.count();
}
return _folders.count() + 1; // +1 for the "add folder" button
}
auto info = infoForIndex(parent);
if (!info)
return 0;
if (info->_folder && !info->_folder->supportsSelectiveSync())
return 0;
if (info->hasLabel())
return 1;
return info->_subs.count();
}
2017-05-17 11:55:42 +03:00
FolderStatusModel::ItemType FolderStatusModel::classify(const QModelIndex &index) const
{
2017-05-17 11:55:42 +03:00
if (auto sub = static_cast<SubFolderInfo *>(index.internalPointer())) {
if (sub->hasLabel()) {
return FetchLabel;
} else {
return SubFolder;
}
}
if (index.row() < _folders.count()) {
return RootFolder;
}
return AddButton;
}
2017-05-17 11:55:42 +03:00
FolderStatusModel::SubFolderInfo *FolderStatusModel::infoForIndex(const QModelIndex &index) const
{
if (!index.isValid())
return nullptr;
2017-05-17 11:55:42 +03:00
if (auto parentInfo = static_cast<SubFolderInfo *>(index.internalPointer())) {
if (parentInfo->hasLabel()) {
return nullptr;
}
if (index.row() >= parentInfo->_subs.size()) {
return nullptr;
}
return &parentInfo->_subs[index.row()];
} else {
if (index.row() >= _folders.count()) {
// AddButton
return nullptr;
}
return const_cast<SubFolderInfo *>(&_folders[index.row()]);
}
}
2017-05-17 11:55:42 +03:00
QModelIndex FolderStatusModel::indexForPath(Folder *f, const QString &path) const
{
2017-05-17 11:55:42 +03:00
if (!f) {
return {};
}
int slashPos = path.lastIndexOf('/');
if (slashPos == -1) {
// first level folder
for (int i = 0; i < _folders.size(); ++i) {
2017-05-17 11:55:42 +03:00
auto &info = _folders.at(i);
2016-11-08 15:57:03 +03:00
if (info._folder == f) {
2017-05-17 11:55:42 +03:00
if (path.isEmpty()) { // the folder object
return index(i, 0);
}
2016-11-08 15:57:03 +03:00
for (int j = 0; j < info._subs.size(); ++j) {
const QString subName = info._subs.at(j)._name;
if (subName == path) {
return index(j, 0, index(i));
}
}
return {};
}
}
return {};
}
auto parent = indexForPath(f, path.left(slashPos));
if (!parent.isValid())
return parent;
if (slashPos == path.size() - 1) {
// The slash is the last part, we found our index
return parent;
}
auto parentInfo = infoForIndex(parent);
if (!parentInfo) {
return {};
}
for (int i = 0; i < parentInfo->_subs.size(); ++i) {
2017-05-17 11:55:42 +03:00
if (parentInfo->_subs.at(i)._name == path.mid(slashPos + 1)) {
return index(i, 0, parent);
}
}
return {};
}
2017-05-17 11:55:42 +03:00
QModelIndex FolderStatusModel::index(int row, int column, const QModelIndex &parent) const
{
if (!parent.isValid()) {
2017-05-17 11:55:42 +03:00
return createIndex(row, column /*, nullptr*/);
}
switch (classify(parent)) {
case AddButton:
case FetchLabel:
return {};
2017-05-17 11:55:42 +03:00
case RootFolder:
if (_folders.count() <= parent.row())
return {}; // should not happen
2017-05-17 11:55:42 +03:00
return createIndex(row, column, const_cast<SubFolderInfo *>(&_folders[parent.row()]));
case SubFolder: {
auto pinfo = static_cast<SubFolderInfo *>(parent.internalPointer());
if (pinfo->_subs.count() <= parent.row())
return {}; // should not happen
2017-05-17 11:55:42 +03:00
auto &info = pinfo->_subs[parent.row()];
if (!info.hasLabel()
&& info._subs.count() <= row)
return {}; // should not happen
2017-05-17 11:55:42 +03:00
return createIndex(row, column, &info);
}
}
return {};
}
2017-05-17 11:55:42 +03:00
QModelIndex FolderStatusModel::parent(const QModelIndex &child) const
{
if (!child.isValid()) {
return {};
}
2017-05-17 11:55:42 +03:00
switch (classify(child)) {
case RootFolder:
case AddButton:
return {};
2017-05-17 11:55:42 +03:00
case SubFolder:
case FetchLabel:
break;
}
2017-05-17 11:55:42 +03:00
auto pathIdx = static_cast<SubFolderInfo *>(child.internalPointer())->_pathIdx;
int i = 1;
ASSERT(pathIdx.at(0) < _folders.count());
if (pathIdx.count() == 1) {
2017-05-17 11:55:42 +03:00
return createIndex(pathIdx.at(0), 0 /*, nullptr*/);
}
const SubFolderInfo *info = &_folders[pathIdx.at(0)];
while (i < pathIdx.count() - 1) {
ASSERT(pathIdx.at(i) < info->_subs.count());
info = &info->_subs[pathIdx.at(i)];
++i;
}
return createIndex(pathIdx.at(i), 0, const_cast<SubFolderInfo *>(info));
}
2017-05-17 11:55:42 +03:00
bool FolderStatusModel::hasChildren(const QModelIndex &parent) const
{
if (!parent.isValid())
return true;
auto info = infoForIndex(parent);
if (!info)
return false;
if (info->_folder && !info->_folder->supportsSelectiveSync())
return false;
if (!info->_fetched)
return true;
if (info->_subs.isEmpty())
return false;
return true;
}
2017-05-17 11:55:42 +03:00
bool FolderStatusModel::canFetchMore(const QModelIndex &parent) const
{
if (!_accountState) {
2017-05-17 11:55:42 +03:00
return false;
}
if (_accountState->state() != AccountState::Connected) {
return false;
}
auto info = infoForIndex(parent);
if (!info || info->_fetched || info->_fetchingJob)
return false;
if (info->_hasError) {
// Keep showing the error to the user, it will be hidden when the account reconnects
return false;
}
if (info->_folder && !info->_folder->supportsSelectiveSync()) {
// Selective sync is hidden in that case
return false;
}
return true;
}
2017-05-17 11:55:42 +03:00
void FolderStatusModel::fetchMore(const QModelIndex &parent)
{
auto info = infoForIndex(parent);
if (!info || info->_fetched || info->_fetchingJob)
return;
info->resetSubs(this, parent);
QString path = info->_folder->remotePathTrailingSlash();
if (info->_path != QLatin1String("/")) {
path += info->_path;
}
auto *job = new LsColJob(_accountState->account(), path, this);
info->_fetchingJob = job;
2017-05-17 11:55:42 +03:00
job->setProperties(QList<QByteArray>() << "resourcetype"
<< "http://owncloud.org/ns:size"
<< "http://owncloud.org/ns:permissions"
<< "http://owncloud.org/ns:fileid");
job->setTimeout(60 * 1000);
connect(job, &LsColJob::directoryListingSubfolders,
this, &FolderStatusModel::slotUpdateDirectories);
connect(job, &LsColJob::finishedWithError,
this, &FolderStatusModel::slotLscolFinishedWithError);
connect(job, &LsColJob::directoryListingIterated,
this, &FolderStatusModel::slotGatherPermissions);
job->start();
QPersistentModelIndex persistentIndex(parent);
2017-05-17 11:55:42 +03:00
job->setProperty(propertyParentIndexC, QVariant::fromValue(persistentIndex));
// Show 'fetching data...' hint after a while.
_fetchingItems[persistentIndex].start();
QTimer::singleShot(1000, this, &FolderStatusModel::slotShowFetchProgress);
}
void FolderStatusModel::resetAndFetch(const QModelIndex &parent)
{
auto info = infoForIndex(parent);
info->resetSubs(this, parent);
fetchMore(parent);
}
2017-05-17 11:55:42 +03:00
void FolderStatusModel::slotGatherPermissions(const QString &href, const QMap<QString, QString> &map)
{
auto it = map.find("permissions");
if (it == map.end())
return;
auto job = sender();
auto permissionMap = job->property(propertyPermissionMap).toMap();
job->setProperty(propertyPermissionMap, QVariant()); // avoid a detach of the map while it is modified
ASSERT(!href.endsWith(QLatin1Char('/')), "LsColXMLParser::parse should remove the trailing slash before calling us.");
permissionMap[href] = *it;
job->setProperty(propertyPermissionMap, permissionMap);
}
void FolderStatusModel::slotUpdateDirectories(const QStringList &list)
{
auto job = qobject_cast<LsColJob *>(sender());
ASSERT(job);
QModelIndex idx = qvariant_cast<QPersistentModelIndex>(job->property(propertyParentIndexC));
auto parentInfo = infoForIndex(idx);
if (!parentInfo) {
return;
}
if (!parentInfo->_folder->supportsSelectiveSync()) {
return;
}
ASSERT(parentInfo->_fetchingJob == job);
ASSERT(parentInfo->_subs.isEmpty());
if (parentInfo->hasLabel()) {
2017-05-17 11:55:42 +03:00
beginRemoveRows(idx, 0, 0);
parentInfo->_hasError = false;
parentInfo->_fetchingLabel = false;
endRemoveRows();
}
parentInfo->_lastErrorString.clear();
parentInfo->_fetchingJob = nullptr;
parentInfo->_fetched = true;
QUrl url = parentInfo->_folder->remoteUrl();
QString pathToRemove = url.path();
if (!pathToRemove.endsWith('/'))
pathToRemove += '/';
QStringList selectiveSyncBlackList;
bool ok1 = true;
bool ok2 = true;
if (parentInfo->_checked == Qt::PartiallyChecked) {
selectiveSyncBlackList = parentInfo->_folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok1);
}
auto selectiveSyncUndecidedList = parentInfo->_folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, &ok2);
2017-05-17 11:55:42 +03:00
if (!(ok1 && ok2)) {
qCWarning(lcFolderStatus) << "Could not retrieve selective sync info from journal";
return;
}
std::set<QString> selectiveSyncUndecidedSet; // not QSet because it's not sorted
foreach (const QString &str, selectiveSyncUndecidedList) {
if (str.startsWith(parentInfo->_path) || parentInfo->_path == QLatin1String("/")) {
selectiveSyncUndecidedSet.insert(str);
}
}
const auto permissionMap = job->property(propertyPermissionMap).toMap();
QStringList sortedSubfolders = list;
if (!sortedSubfolders.isEmpty())
sortedSubfolders.removeFirst(); // skip the parent item (first in the list)
Utility::sortFilenames(sortedSubfolders);
QVarLengthArray<int, 10> undecidedIndexes;
QVector<SubFolderInfo> newSubs;
newSubs.reserve(sortedSubfolders.size());
2017-05-17 11:55:42 +03:00
foreach (const QString &path, sortedSubfolders) {
auto relativePath = path.mid(pathToRemove.size());
if (parentInfo->_folder->isFileExcludedRelative(relativePath)) {
continue;
}
SubFolderInfo newInfo;
newInfo._folder = parentInfo->_folder;
newInfo._pathIdx = parentInfo->_pathIdx;
newInfo._pathIdx << newSubs.size();
newInfo._isExternal = permissionMap.value(removeTrailingSlash(path)).toString().contains("M");
newInfo._path = relativePath;
SyncJournalFileRecord rec;
parentInfo->_folder->journalDb()->getFileRecordByE2eMangledName(removeTrailingSlash(relativePath), &rec);
if (rec.isValid()) {
newInfo._name = removeTrailingSlash(rec._path).split('/').last();
} else {
newInfo._name = removeTrailingSlash(relativePath).split('/').last();
}
const auto& folderInfo = job->_folderInfos.value(path);
newInfo._size = folderInfo.size;
newInfo._fileId = folderInfo.fileId;
if (relativePath.isEmpty())
continue;
if (parentInfo->_checked == Qt::Unchecked) {
newInfo._checked = Qt::Unchecked;
} else if (parentInfo->_checked == Qt::Checked) {
newInfo._checked = Qt::Checked;
} else {
2017-05-17 11:55:42 +03:00
foreach (const QString &str, selectiveSyncBlackList) {
if (str == relativePath || str == QLatin1String("/")) {
newInfo._checked = Qt::Unchecked;
break;
} else if (str.startsWith(relativePath)) {
newInfo._checked = Qt::PartiallyChecked;
}
}
}
auto it = selectiveSyncUndecidedSet.lower_bound(relativePath);
if (it != selectiveSyncUndecidedSet.end()) {
if (*it == relativePath) {
newInfo._isUndecided = true;
selectiveSyncUndecidedSet.erase(it);
} else if ((*it).startsWith(relativePath)) {
undecidedIndexes.append(newInfo._pathIdx.last());
// Remove all the items from the selectiveSyncUndecidedSet that starts with this path
QString relativePathNext = relativePath;
2017-05-17 11:55:42 +03:00
relativePathNext[relativePathNext.length() - 1].unicode()++;
auto it2 = selectiveSyncUndecidedSet.lower_bound(relativePathNext);
selectiveSyncUndecidedSet.erase(it, it2);
}
}
newSubs.append(newInfo);
}
if (!newSubs.isEmpty()) {
beginInsertRows(idx, 0, newSubs.size() - 1);
parentInfo->_subs = std::move(newSubs);
endInsertRows();
}
for (int undecidedIndex : qAsConst(undecidedIndexes)) {
suggestExpand(index(undecidedIndex, 0, idx));
}
/* Try to remove the the undecided lists the items that are not on the server. */
auto it = std::remove_if(selectiveSyncUndecidedList.begin(), selectiveSyncUndecidedList.end(),
2017-05-17 11:55:42 +03:00
[&](const QString &s) { return selectiveSyncUndecidedSet.count(s); });
if (it != selectiveSyncUndecidedList.end()) {
selectiveSyncUndecidedList.erase(it, selectiveSyncUndecidedList.end());
parentInfo->_folder->journalDb()->setSelectiveSyncList(
2017-05-17 11:55:42 +03:00
SyncJournalDb::SelectiveSyncUndecidedList, selectiveSyncUndecidedList);
emit dirtyChanged();
}
}
2017-05-17 11:55:42 +03:00
void FolderStatusModel::slotLscolFinishedWithError(QNetworkReply *r)
{
auto job = qobject_cast<LsColJob *>(sender());
ASSERT(job);
QModelIndex idx = qvariant_cast<QPersistentModelIndex>(job->property(propertyParentIndexC));
if (!idx.isValid()) {
return;
}
auto parentInfo = infoForIndex(idx);
if (parentInfo) {
qCDebug(lcFolderStatus) << r->errorString();
parentInfo->_lastErrorString = r->errorString();
auto error = r->error();
parentInfo->resetSubs(this, idx);
if (error == QNetworkReply::ContentNotFoundError) {
parentInfo->_fetched = true;
} else {
ASSERT(!parentInfo->hasLabel());
beginInsertRows(idx, 0, 0);
parentInfo->_hasError = true;
endInsertRows();
}
}
}
QStringList FolderStatusModel::createBlackList(FolderStatusModel::SubFolderInfo *root,
2017-05-17 11:55:42 +03:00
const QStringList &oldBlackList) const
{
2017-05-17 11:55:42 +03:00
if (!root)
return QStringList();
switch (root->_checked) {
case Qt::Unchecked:
return QStringList(root->_path);
case Qt::Checked:
return QStringList();
case Qt::PartiallyChecked:
break;
}
QStringList result;
if (root->_fetched) {
for (int i = 0; i < root->_subs.count(); ++i) {
result += createBlackList(&root->_subs[i], oldBlackList);
}
} else {
// We did not load from the server so we re-use the one from the old black list
QString path = root->_path;
2017-05-17 11:55:42 +03:00
foreach (const QString &it, oldBlackList) {
if (it.startsWith(path))
result += it;
}
}
return result;
}
void FolderStatusModel::slotUpdateFolderState(Folder *folder)
{
2017-05-17 11:55:42 +03:00
if (!folder)
return;
for (int i = 0; i < _folders.count(); ++i) {
if (_folders.at(i)._folder == folder) {
emit dataChanged(index(i), index(i));
}
}
}
void FolderStatusModel::slotApplySelectiveSync()
{
for (int i = 0; i < _folders.count(); ++i) {
if (!_folders[i]._fetched) {
_folders[i]._folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, QStringList());
continue;
}
auto folder = _folders.at(i)._folder;
bool ok = false;
auto oldBlackList = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok);
2017-05-17 11:55:42 +03:00
if (!ok) {
qCWarning(lcFolderStatus) << "Could not read selective sync list from db.";
continue;
}
QStringList blackList = createBlackList(&_folders[i], oldBlackList);
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, blackList);
auto blackListSet = blackList.toSet();
auto oldBlackListSet = oldBlackList.toSet();
// The folders that were undecided or blacklisted and that are now checked should go on the white list.
// The user confirmed them already just now.
2017-05-17 11:55:42 +03:00
QStringList toAddToWhiteList = ((oldBlackListSet + folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, &ok).toSet()) - blackListSet).toList();
if (!toAddToWhiteList.isEmpty()) {
auto whiteList = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, &ok);
if (ok) {
whiteList += toAddToWhiteList;
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, whiteList);
}
}
// clear the undecided list
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, QStringList());
// do the sync if there were changes
auto changes = (oldBlackListSet - blackListSet) + (blackListSet - oldBlackListSet);
if (!changes.isEmpty()) {
if (folder->isBusy()) {
folder->slotTerminateSync();
}
//The part that changed should not be read from the DB on next sync because there might be new folders
// (the ones that are no longer in the blacklist)
2017-05-17 11:55:42 +03:00
foreach (const auto &it, changes) {
folder->journalDb()->schedulePathForRemoteDiscovery(it);
folder->schedulePathForLocalDiscovery(it);
}
FolderMan::instance()->scheduleFolder(folder);
}
}
resetFolders();
}
void FolderStatusModel::slotSetProgress(const ProgressInfo &progress)
{
2017-05-17 11:55:42 +03:00
auto par = qobject_cast<QWidget *>(QObject::parent());
if (!par->isVisible()) {
return; // for https://github.com/owncloud/client/issues/2648#issuecomment-71377909
}
auto *f = qobject_cast<Folder *>(sender());
2017-05-17 11:55:42 +03:00
if (!f) {
return;
}
int folderIndex = -1;
for (int i = 0; i < _folders.count(); ++i) {
if (_folders.at(i)._folder == f) {
folderIndex = i;
break;
}
}
2017-05-17 11:55:42 +03:00
if (folderIndex < 0) {
return;
}
auto *pi = &_folders[folderIndex]._progress;
QVector<int> roles;
roles << FolderStatusDelegate::SyncProgressItemString
<< FolderStatusDelegate::WarningCount
<< Qt::ToolTipRole;
if (progress.status() == ProgressInfo::Discovery) {
if (!progress._currentDiscoveredRemoteFolder.isEmpty()) {
pi->_overallSyncString = tr("Checking for changes in remote '%1'").arg(progress._currentDiscoveredRemoteFolder);
emit dataChanged(index(folderIndex), index(folderIndex), roles);
return;
} else if (!progress._currentDiscoveredLocalFolder.isEmpty()) {
pi->_overallSyncString = tr("Checking for changes in local '%1'").arg(progress._currentDiscoveredLocalFolder);
emit dataChanged(index(folderIndex), index(folderIndex), roles);
return;
}
}
if (progress.status() == ProgressInfo::Reconcile) {
pi->_overallSyncString = tr("Reconciling changes");
emit dataChanged(index(folderIndex), index(folderIndex), roles);
return;
}
// Status is Starting, Propagation or Done
2017-05-17 11:55:42 +03:00
if (!progress._lastCompletedItem.isEmpty()
&& Progress::isWarningKind(progress._lastCompletedItem._status)) {
pi->_warningCount++;
}
// find the single item to display: This is going to be the bigger item, or the last completed
// item if no items are in progress.
SyncFileItem curItem = progress._lastCompletedItem;
qint64 curItemProgress = -1; // -1 means finished
qint64 biggerItemSize = 0;
quint64 estimatedUpBw = 0;
quint64 estimatedDownBw = 0;
QString allFilenames;
2017-05-17 11:55:42 +03:00
foreach (const ProgressInfo::ProgressItem &citm, progress._currentItems) {
if (curItemProgress == -1 || (ProgressInfo::isSizeDependent(citm._item)
2017-05-17 11:55:42 +03:00
&& biggerItemSize < citm._item._size)) {
curItemProgress = citm._progress.completed();
curItem = citm._item;
biggerItemSize = citm._item._size;
}
2017-05-17 11:55:42 +03:00
if (citm._item._direction != SyncFileItem::Up) {
estimatedDownBw += progress.fileProgress(citm._item).estimatedBandwidth;
} else {
estimatedUpBw += progress.fileProgress(citm._item).estimatedBandwidth;
}
auto fileName = QFileInfo(citm._item._file).fileName();
if (allFilenames.length() > 0) {
//: Build a list of file names
allFilenames.append(tr(", '%1'").arg(fileName));
} else {
//: Argument is a file name
allFilenames.append(tr("'%1'").arg(fileName));
}
}
if (curItemProgress == -1) {
curItemProgress = curItem._size;
}
QString itemFileName = curItem._file;
QString kindString = Progress::asActionString(curItem);
QString fileProgressString;
if (ProgressInfo::isSizeDependent(curItem)) {
2017-05-17 11:55:42 +03:00
QString s1 = Utility::octetsToString(curItemProgress);
QString s2 = Utility::octetsToString(curItem._size);
//quint64 estimatedBw = progress.fileProgress(curItem).estimatedBandwidth;
if (estimatedUpBw || estimatedDownBw) {
/*
//: Example text: "uploading foobar.png (1MB of 2MB) time left 2 minutes at a rate of 24Kb/s"
fileProgressString = tr("%1 %2 (%3 of %4) %5 left at a rate of %6/s")
.arg(kindString, itemFileName, s1, s2,
Utility::durationToDescriptiveString(progress.fileProgress(curItem).estimatedEta),
Utility::octetsToString(estimatedBw) );
*/
//: Example text: "Syncing 'foo.txt', 'bar.txt'"
fileProgressString = tr("Syncing %1").arg(allFilenames);
if (estimatedDownBw > 0) {
fileProgressString.append(tr(", "));
2015-08-06 19:08:05 +03:00
// ifdefs: https://github.com/owncloud/client/issues/3095#issuecomment-128409294
#ifdef Q_OS_WIN
//: Example text: "download 24Kb/s" (%1 is replaced by 24Kb (translated))
2015-08-06 19:08:05 +03:00
fileProgressString.append(tr("download %1/s").arg(Utility::octetsToString(estimatedDownBw)));
#else
fileProgressString.append(tr("\u2193 %1/s")
2017-05-17 11:55:42 +03:00
.arg(Utility::octetsToString(estimatedDownBw)));
2015-08-06 19:08:05 +03:00
#endif
}
if (estimatedUpBw > 0) {
fileProgressString.append(tr(", "));
2017-05-17 11:55:42 +03:00
#ifdef Q_OS_WIN
//: Example text: "upload 24Kb/s" (%1 is replaced by 24Kb (translated))
2015-08-06 19:08:05 +03:00
fileProgressString.append(tr("upload %1/s").arg(Utility::octetsToString(estimatedUpBw)));
#else
fileProgressString.append(tr("\u2191 %1/s")
2017-05-17 11:55:42 +03:00
.arg(Utility::octetsToString(estimatedUpBw)));
2015-08-06 19:08:05 +03:00
#endif
}
} else {
//: Example text: "uploading foobar.png (2MB of 2MB)"
2017-05-17 11:55:42 +03:00
fileProgressString = tr("%1 %2 (%3 of %4)").arg(kindString, itemFileName, s1, s2);
}
} else if (!kindString.isEmpty()) {
//: Example text: "uploading foobar.png"
fileProgressString = tr("%1 %2").arg(kindString, itemFileName);
}
pi->_progressString = fileProgressString;
// overall progress
qint64 completedSize = progress.completedSize();
qint64 completedFile = progress.completedFiles();
qint64 currentFile = progress.currentFile();
qint64 totalSize = qMax(completedSize, progress.totalSize());
qint64 totalFileCount = qMax(currentFile, progress.totalFiles());
QString overallSyncString;
if (totalSize > 0) {
2017-05-17 11:55:42 +03:00
QString s1 = Utility::octetsToString(completedSize);
QString s2 = Utility::octetsToString(totalSize);
if (progress.trustEta()) {
//: Example text: "5 minutes left, 12 MB of 345 MB, file 6 of 7"
overallSyncString = tr("%5 left, %1 of %2, file %3 of %4")
2017-05-17 11:55:42 +03:00
.arg(s1, s2)
.arg(currentFile)
.arg(totalFileCount)
.arg(Utility::durationToDescriptiveString1(progress.totalProgress().estimatedEta));
} else {
//: Example text: "12 MB of 345 MB, file 6 of 7"
overallSyncString = tr("%1 of %2, file %3 of %4")
2017-05-17 11:55:42 +03:00
.arg(s1, s2)
.arg(currentFile)
.arg(totalFileCount);
}
} else if (totalFileCount > 0) {
// Don't attempt to estimate the time left if there is no kb to transfer.
2017-05-17 11:55:42 +03:00
overallSyncString = tr("file %1 of %2").arg(currentFile).arg(totalFileCount);
}
2017-05-17 11:55:42 +03:00
pi->_overallSyncString = overallSyncString;
int overallPercent = 0;
2017-05-17 11:55:42 +03:00
if (totalFileCount > 0) {
// Add one 'byte' for each file so the percentage is moving when deleting or renaming files
2017-05-17 11:55:42 +03:00
overallPercent = qRound(double(completedSize + completedFile) / double(totalSize + totalFileCount) * 100.0);
}
pi->_overallPercent = qBound(0, overallPercent, 100);
emit dataChanged(index(folderIndex), index(folderIndex), roles);
}
void FolderStatusModel::slotFolderSyncStateChange(Folder *f)
{
2017-05-17 11:55:42 +03:00
if (!f) {
return;
}
int folderIndex = -1;
for (int i = 0; i < _folders.count(); ++i) {
if (_folders.at(i)._folder == f) {
folderIndex = i;
break;
}
}
2017-05-17 11:55:42 +03:00
if (folderIndex < 0) {
return;
}
2017-05-17 11:55:42 +03:00
auto &pi = _folders[folderIndex]._progress;
2016-11-08 15:57:03 +03:00
SyncResult::Status state = f->syncResult().status();
if (!f->canSync() || state == SyncResult::Problem || state == SyncResult::Success || state == SyncResult::Error) {
// Reset progress info.
2016-11-08 15:57:03 +03:00
pi = SubFolderInfo::Progress();
} else if (state == SyncResult::NotYetStarted) {
2017-05-17 11:55:42 +03:00
FolderMan *folderMan = FolderMan::instance();
int pos = folderMan->scheduleQueue().indexOf(f);
for (auto other : folderMan->map()) {
if (other != f && other->isSyncRunning())
pos += 1;
}
QString message;
if (pos <= 0) {
message = tr("Waiting …");
} else {
message = tr("Waiting for %n other folder(s) …", "", pos);
}
2016-11-08 15:57:03 +03:00
pi = SubFolderInfo::Progress();
pi._overallSyncString = message;
} else if (state == SyncResult::SyncPrepare) {
2016-11-08 15:57:03 +03:00
pi = SubFolderInfo::Progress();
pi._overallSyncString = tr("Preparing to sync …");
}
// update the icon etc. now
slotUpdateFolderState(f);
if (f->syncResult().folderStructureWasChanged()
&& (state == SyncResult::Success || state == SyncResult::Problem)) {
// There is a new or a removed folder. reset all data
resetAndFetch(index(folderIndex));
}
}
void FolderStatusModel::slotFolderScheduleQueueChanged()
{
// Update messages on waiting folders.
2017-05-17 11:55:42 +03:00
foreach (Folder *f, FolderMan::instance()->map()) {
slotFolderSyncStateChange(f);
}
}
void FolderStatusModel::resetFolders()
{
setAccountState(_accountState);
}
void FolderStatusModel::slotSyncAllPendingBigFolders()
{
for (int i = 0; i < _folders.count(); ++i) {
if (!_folders[i]._fetched) {
_folders[i]._folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, QStringList());
continue;
}
auto folder = _folders.at(i)._folder;
bool ok = false;
auto undecidedList = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, &ok);
2017-05-17 11:55:42 +03:00
if (!ok) {
qCWarning(lcFolderStatus) << "Could not read selective sync list from db.";
return;
}
// If this folder had no undecided entries, skip it.
if (undecidedList.isEmpty()) {
continue;
}
// Remove all undecided folders from the blacklist
auto blackList = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, &ok);
2017-05-17 11:55:42 +03:00
if (!ok) {
qCWarning(lcFolderStatus) << "Could not read selective sync list from db.";
return;
}
2017-05-17 11:55:42 +03:00
foreach (const auto &undecidedFolder, undecidedList) {
blackList.removeAll(undecidedFolder);
}
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncBlackList, blackList);
// Add all undecided folders to the white list
auto whiteList = folder->journalDb()->getSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, &ok);
2017-05-17 11:55:42 +03:00
if (!ok) {
qCWarning(lcFolderStatus) << "Could not read selective sync list from db.";
return;
}
whiteList += undecidedList;
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncWhiteList, whiteList);
// Clear the undecided list
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, QStringList());
// Trigger a sync
if (folder->isBusy()) {
folder->slotTerminateSync();
}
// The part that changed should not be read from the DB on next sync because there might be new folders
// (the ones that are no longer in the blacklist)
foreach (const auto &it, undecidedList) {
folder->journalDb()->schedulePathForRemoteDiscovery(it);
folder->schedulePathForLocalDiscovery(it);
}
FolderMan::instance()->scheduleFolder(folder);
}
resetFolders();
}
void FolderStatusModel::slotSyncNoPendingBigFolders()
{
for (int i = 0; i < _folders.count(); ++i) {
auto folder = _folders.at(i)._folder;
// clear the undecided list
folder->journalDb()->setSelectiveSyncList(SyncJournalDb::SelectiveSyncUndecidedList, QStringList());
}
resetFolders();
}
void FolderStatusModel::slotNewBigFolder()
{
auto f = qobject_cast<Folder *>(sender());
ASSERT(f);
int folderIndex = -1;
for (int i = 0; i < _folders.count(); ++i) {
if (_folders.at(i)._folder == f) {
folderIndex = i;
break;
}
}
2017-05-17 11:55:42 +03:00
if (folderIndex < 0) {
return;
}
resetAndFetch(index(folderIndex));
emit suggestExpand(index(folderIndex));
emit dirtyChanged();
}
void FolderStatusModel::slotShowFetchProgress()
{
QMutableMapIterator<QPersistentModelIndex, QElapsedTimer> it(_fetchingItems);
while (it.hasNext()) {
it.next();
2017-05-17 11:55:42 +03:00
if (it.value().elapsed() > 800) {
auto idx = it.key();
2017-05-17 11:55:42 +03:00
auto *info = infoForIndex(idx);
if (info && info->_fetchingJob) {
bool add = !info->hasLabel();
if (add) {
beginInsertRows(idx, 0, 0);
}
info->_fetchingLabel = true;
if (add) {
endInsertRows();
}
}
it.remove();
}
}
}
bool FolderStatusModel::SubFolderInfo::hasLabel() const
{
return _hasError || _fetchingLabel;
}
2017-05-17 11:55:42 +03:00
void FolderStatusModel::SubFolderInfo::resetSubs(FolderStatusModel *model, QModelIndex index)
{
_fetched = false;
if (_fetchingJob) {
disconnect(_fetchingJob, nullptr, model, nullptr);
_fetchingJob->deleteLater();
_fetchingJob.clear();
}
if (hasLabel()) {
2017-05-17 11:55:42 +03:00
model->beginRemoveRows(index, 0, 0);
_fetchingLabel = false;
_hasError = false;
model->endRemoveRows();
} else if (!_subs.isEmpty()) {
model->beginRemoveRows(index, 0, _subs.count() - 1);
_subs.clear();
model->endRemoveRows();
}
}
2014-11-10 00:34:07 +03:00
} // namespace OCC