2023-05-02 14:55:17 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 by Matthieu Gallien <matthieu.gallien@nextcloud.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; 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 "syncconflictsmodel.h"
|
2023-05-12 16:01:28 +03:00
|
|
|
|
2023-05-02 14:55:17 +03:00
|
|
|
#include "folderman.h"
|
|
|
|
|
|
|
|
#include <QLoggingCategory>
|
|
|
|
|
|
|
|
namespace OCC {
|
|
|
|
|
|
|
|
Q_LOGGING_CATEGORY(lcSyncConflictsModel, "nextcloud.syncconflictsmodel", QtInfoMsg)
|
|
|
|
|
|
|
|
SyncConflictsModel::SyncConflictsModel(QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int SyncConflictsModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
return _data.size();
|
2023-05-02 14:55:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant SyncConflictsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
auto result = QVariant{};
|
|
|
|
|
|
|
|
Q_ASSERT(checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid));
|
|
|
|
|
|
|
|
if (index.parent().isValid()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role >= static_cast<int>(SyncConflictRoles::ExistingFileName) && role <= static_cast<int>(SyncConflictRoles::ConflictPreviewUrl)) {
|
2023-05-16 11:37:40 +03:00
|
|
|
const auto convertedRole = static_cast<SyncConflictRoles>(role);
|
2023-05-02 14:55:17 +03:00
|
|
|
|
|
|
|
switch (convertedRole) {
|
|
|
|
case SyncConflictRoles::ExistingFileName:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mExistingFileName;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingSize:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mExistingSize;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictSize:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mConflictSize;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingDate:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mExistingDate;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictDate:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mConflictDate;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingSelected:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mExistingSelected == ConflictInfo::ConflictSolution::SolutionSelected;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictSelected:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mConflictSelected == ConflictInfo::ConflictSolution::SolutionSelected;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingPreviewUrl:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mExistingPreviewUrl;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictPreviewUrl:
|
2023-05-16 11:37:40 +03:00
|
|
|
result = _conflictData[index.row()].mConflictPreviewUrl;
|
2023-05-02 14:55:17 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:17:38 +03:00
|
|
|
bool SyncConflictsModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
|
|
{
|
|
|
|
auto result = false;
|
|
|
|
|
|
|
|
Q_ASSERT(checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid));
|
|
|
|
|
|
|
|
if (index.parent().isValid()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (role >= static_cast<int>(SyncConflictRoles::ExistingFileName) && role <= static_cast<int>(SyncConflictRoles::ConflictPreviewUrl)) {
|
2023-05-16 11:37:40 +03:00
|
|
|
const auto convertedRole = static_cast<SyncConflictRoles>(role);
|
2023-05-12 14:17:38 +03:00
|
|
|
|
|
|
|
switch(convertedRole) {
|
|
|
|
case SyncConflictRoles::ExistingFileName:
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingSize:
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictSize:
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingDate:
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictDate:
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingSelected:
|
|
|
|
setExistingSelected(value.toBool(), index, role);
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictSelected:
|
|
|
|
setConflictingSelected(value.toBool(), index, role);
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ExistingPreviewUrl:
|
|
|
|
break;
|
|
|
|
case SyncConflictRoles::ConflictPreviewUrl:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-02 14:55:17 +03:00
|
|
|
QHash<int, QByteArray> SyncConflictsModel::roleNames() const
|
|
|
|
{
|
|
|
|
auto result = QAbstractListModel::roleNames();
|
|
|
|
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ExistingFileName)] = "existingFileName";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ExistingSize)] = "existingSize";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ConflictSize)] = "conflictSize";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ExistingDate)] = "existingDate";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ConflictDate)] = "conflictDate";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ExistingSelected)] = "existingSelected";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ConflictSelected)] = "conflictSelected";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ExistingPreviewUrl)] = "existingPreviewUrl";
|
|
|
|
result[static_cast<int>(SyncConflictRoles::ConflictPreviewUrl)] = "conflictPreviewUrl";
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:17:38 +03:00
|
|
|
Qt::ItemFlags SyncConflictsModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
auto result = Qt::ItemFlags{};
|
|
|
|
|
|
|
|
if (!index.parent().isValid()) {
|
|
|
|
result = QAbstractListModel::flags(index);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = Qt::ItemIsSelectable | Qt::ItemIsEditable;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-05-02 14:55:17 +03:00
|
|
|
ActivityList SyncConflictsModel::conflictActivities() const
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
return _data;
|
2023-05-02 14:55:17 +03:00
|
|
|
}
|
|
|
|
|
2023-05-12 14:17:38 +03:00
|
|
|
bool SyncConflictsModel::allExistingsSelected() const
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
return _allExistingsSelected;
|
2023-05-12 14:17:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SyncConflictsModel::allConflictingSelected() const
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
return _allConflictingsSelected;
|
2023-05-12 14:17:38 +03:00
|
|
|
}
|
|
|
|
|
2023-05-02 14:55:17 +03:00
|
|
|
void SyncConflictsModel::setConflictActivities(ActivityList conflicts)
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
if (_data == conflicts) {
|
2023-05-02 14:55:17 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
beginResetModel();
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
_data = conflicts;
|
2023-05-02 14:55:17 +03:00
|
|
|
emit conflictActivitiesChanged();
|
|
|
|
|
|
|
|
updateConflictsData();
|
|
|
|
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:17:38 +03:00
|
|
|
void SyncConflictsModel::selectAllExisting(bool selected)
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
for (auto &singleConflict : _conflictData) {
|
|
|
|
singleConflict.mExistingSelected = selected ? ConflictInfo::ConflictSolution::SolutionSelected : ConflictInfo::ConflictSolution::SolutionDeselected;
|
2023-05-12 14:17:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT dataChanged(index(0), index(rowCount() - 1), {static_cast<int>(SyncConflictRoles::ExistingSelected)});
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
if (selected && !_allExistingsSelected) {
|
|
|
|
_allExistingsSelected = true;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allExistingsSelectedChanged();
|
2023-05-16 11:37:40 +03:00
|
|
|
} else if (!selected && _allExistingsSelected) {
|
|
|
|
_allExistingsSelected = false;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allExistingsSelectedChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncConflictsModel::selectAllConflicting(bool selected)
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
for (auto &singleConflict : _conflictData) {
|
|
|
|
singleConflict.mConflictSelected = selected ? ConflictInfo::ConflictSolution::SolutionSelected : ConflictInfo::ConflictSolution::SolutionDeselected;
|
2023-05-12 14:17:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Q_EMIT dataChanged(index(0), index(rowCount() - 1), {static_cast<int>(SyncConflictRoles::ConflictSelected)});
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
if (selected && !_allConflictingsSelected) {
|
|
|
|
_allConflictingsSelected = true;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allConflictingSelectedChanged();
|
2023-05-16 11:37:40 +03:00
|
|
|
} else if (!selected && _allConflictingsSelected) {
|
|
|
|
_allConflictingsSelected = false;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allConflictingSelectedChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
void SyncConflictsModel::applySolution()
|
2023-05-12 16:01:28 +03:00
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
for(const auto &syncConflict : qAsConst(_conflictData)) {
|
2023-05-12 16:01:28 +03:00
|
|
|
if (syncConflict.isValid()) {
|
|
|
|
qCInfo(lcSyncConflictsModel) << syncConflict.mExistingFilePath << syncConflict.mConflictingFilePath << syncConflict.solution();
|
|
|
|
ConflictSolver solver;
|
|
|
|
solver.setLocalVersionFilename(syncConflict.mConflictingFilePath);
|
|
|
|
solver.setRemoteVersionFilename(syncConflict.mExistingFilePath);
|
|
|
|
solver.exec(syncConflict.solution());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 14:55:17 +03:00
|
|
|
void SyncConflictsModel::updateConflictsData()
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
_conflictData.clear();
|
|
|
|
_conflictData.reserve(_data.size());
|
2023-05-02 14:55:17 +03:00
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
for (const auto &oneConflict : qAsConst(_data)) {
|
2023-05-02 14:55:17 +03:00
|
|
|
const auto folder = FolderMan::instance()->folder(oneConflict._folder);
|
|
|
|
if (!folder) {
|
|
|
|
qCWarning(lcSyncConflictsModel) << "no Folder instance for" << oneConflict._folder;
|
2023-05-16 11:37:40 +03:00
|
|
|
_conflictData.push_back({});
|
2023-05-02 14:55:17 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto conflictedRelativePath = oneConflict._file;
|
2023-05-16 11:37:40 +03:00
|
|
|
const auto baseRelativePath = folder->journalDb() ? folder->journalDb()->conflictFileBaseName(conflictedRelativePath.toUtf8()) : QString{};
|
2023-05-02 14:55:17 +03:00
|
|
|
|
|
|
|
const auto dir = QDir(folder->path());
|
|
|
|
const auto conflictedPath = dir.filePath(conflictedRelativePath);
|
|
|
|
const auto basePath = dir.filePath(baseRelativePath);
|
|
|
|
|
|
|
|
const auto existingFileInfo = QFileInfo(basePath);
|
|
|
|
const auto conflictFileInfo = QFileInfo(conflictedPath);
|
|
|
|
|
|
|
|
auto newConflictData = ConflictInfo{
|
|
|
|
existingFileInfo.fileName(),
|
2023-05-16 11:37:40 +03:00
|
|
|
_locale.formattedDataSize(existingFileInfo.size()),
|
|
|
|
_locale.formattedDataSize(conflictFileInfo.size()),
|
2023-05-02 14:55:17 +03:00
|
|
|
existingFileInfo.lastModified().toString(),
|
|
|
|
conflictFileInfo.lastModified().toString(),
|
2023-05-04 13:30:13 +03:00
|
|
|
QUrl{QStringLiteral("image://tray-image-provider/:/fileicon") + existingFileInfo.filePath()},
|
|
|
|
QUrl{QStringLiteral("image://tray-image-provider/:/fileicon") + conflictFileInfo.filePath()},
|
2023-05-16 11:37:40 +03:00
|
|
|
ConflictInfo::ConflictSolution::SolutionDeselected,
|
|
|
|
ConflictInfo::ConflictSolution::SolutionDeselected,
|
2023-05-12 16:01:28 +03:00
|
|
|
existingFileInfo.filePath(),
|
|
|
|
conflictFileInfo.filePath(),
|
2023-05-02 14:55:17 +03:00
|
|
|
};
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
_conflictData.push_back(std::move(newConflictData));
|
2023-05-02 14:55:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 14:17:38 +03:00
|
|
|
void SyncConflictsModel::setExistingSelected(bool value,
|
|
|
|
const QModelIndex &index,
|
|
|
|
int role)
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
_conflictData[index.row()].mExistingSelected = value ? ConflictInfo::ConflictSolution::SolutionSelected : ConflictInfo::ConflictSolution::SolutionDeselected;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT dataChanged(index, index, {role});
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
if (_conflictData[index.row()].mExistingSelected == ConflictInfo::ConflictSolution::SolutionDeselected && _allExistingsSelected) {
|
|
|
|
_allExistingsSelected = false;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allExistingsSelectedChanged();
|
2023-05-16 11:37:40 +03:00
|
|
|
} else if (_conflictData[index.row()].mExistingSelected == ConflictInfo::ConflictSolution::SolutionSelected && !_allExistingsSelected) {
|
|
|
|
const auto deselectedConflictIt = std::find_if(_conflictData.constBegin(), _conflictData.constEnd(), [] (const auto singleConflict) {
|
|
|
|
return singleConflict.mExistingSelected == ConflictInfo::ConflictSolution::SolutionDeselected;
|
|
|
|
});
|
|
|
|
const auto allSelected = (deselectedConflictIt == _conflictData.constEnd());
|
2023-05-12 14:17:38 +03:00
|
|
|
if (allSelected) {
|
2023-05-16 11:37:40 +03:00
|
|
|
_allExistingsSelected = true;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allExistingsSelectedChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyncConflictsModel::setConflictingSelected(bool value,
|
|
|
|
const QModelIndex &index,
|
|
|
|
int role)
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
_conflictData[index.row()].mConflictSelected = value ? ConflictInfo::ConflictSolution::SolutionSelected : ConflictInfo::ConflictSolution::SolutionDeselected;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT dataChanged(index, index, {role});
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
if (_conflictData[index.row()].mConflictSelected == ConflictInfo::ConflictSolution::SolutionDeselected && _allConflictingsSelected) {
|
|
|
|
_allConflictingsSelected = false;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allConflictingSelectedChanged();
|
2023-05-16 11:37:40 +03:00
|
|
|
} else if (_conflictData[index.row()].mConflictSelected == ConflictInfo::ConflictSolution::SolutionSelected && !_allConflictingsSelected) {
|
|
|
|
const auto deselectedConflictIt = std::find_if(_conflictData.constBegin(), _conflictData.constEnd(), [] (const auto singleConflict) {
|
|
|
|
return singleConflict.mConflictSelected == ConflictInfo::ConflictSolution::SolutionDeselected;
|
|
|
|
});
|
|
|
|
const auto allSelected = (deselectedConflictIt == _conflictData.constEnd());
|
2023-05-12 14:17:38 +03:00
|
|
|
if (allSelected) {
|
2023-05-16 11:37:40 +03:00
|
|
|
_allConflictingsSelected = true;
|
2023-05-12 14:17:38 +03:00
|
|
|
Q_EMIT allConflictingSelectedChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 16:01:28 +03:00
|
|
|
ConflictSolver::Solution SyncConflictsModel::ConflictInfo::solution() const
|
|
|
|
{
|
|
|
|
auto result = ConflictSolver::Solution{};
|
|
|
|
|
2023-05-16 11:37:40 +03:00
|
|
|
if (mConflictSelected == ConflictSolution::SolutionSelected && mExistingSelected == ConflictSolution::SolutionSelected) {
|
2023-05-12 16:01:28 +03:00
|
|
|
result = ConflictSolver::KeepBothVersions;
|
2023-05-16 11:37:40 +03:00
|
|
|
} else if (mConflictSelected == ConflictSolution::SolutionDeselected && mExistingSelected == ConflictSolution::SolutionSelected) {
|
2023-05-12 16:01:28 +03:00
|
|
|
result = ConflictSolver::KeepRemoteVersion;
|
2023-09-11 17:19:42 +03:00
|
|
|
} else if (mConflictSelected == ConflictSolution::SolutionSelected && mExistingSelected == ConflictSolution::SolutionDeselected) {
|
|
|
|
result = ConflictSolver::KeepLocalVersion;
|
2023-05-12 16:01:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SyncConflictsModel::ConflictInfo::isValid() const
|
|
|
|
{
|
2023-05-16 11:37:40 +03:00
|
|
|
return mConflictSelected == ConflictInfo::ConflictSolution::SolutionSelected || mExistingSelected == ConflictInfo::ConflictSolution::SolutionSelected;
|
2023-05-12 16:01:28 +03:00
|
|
|
}
|
|
|
|
|
2023-05-02 14:55:17 +03:00
|
|
|
}
|