Skip to content

Commit

Permalink
model: first step
Browse files Browse the repository at this point in the history
Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
  • Loading branch information
mgallien committed May 3, 2023
1 parent 674c694 commit bf2266a
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ set(client_SRCS
userstatusselectormodel.cpp
emojimodel.h
emojimodel.cpp
syncconflictsmodel.h
syncconflictsmodel.cpp
fileactivitylistmodel.h
fileactivitylistmodel.cpp
filedetails/filedetails.h
Expand Down
135 changes: 135 additions & 0 deletions src/gui/syncconflictsmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* 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"

namespace OCC {

SyncConflictsModel::SyncConflictsModel(QObject *parent)
: QAbstractListModel(parent)
{
}

int SyncConflictsModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
return 0;
}

return mData.size();
}

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)) {
auto convertedRole = static_cast<SyncConflictRoles>(role);

switch (convertedRole) {
case SyncConflictRoles::ExistingFileName:
result = mData[index.row()]._file;
break;
case SyncConflictRoles::ConflictFileName:
break;
case SyncConflictRoles::ExistingSize:
break;
case SyncConflictRoles::ConflictSize:
break;
case SyncConflictRoles::ExistingDate:
break;
case SyncConflictRoles::ConflictDate:
break;
case SyncConflictRoles::ExistingSelected:
break;
case SyncConflictRoles::ConflictSelected:
break;
case SyncConflictRoles::ExistingPreviewUrl:
break;
case SyncConflictRoles::ConflictPreviewUrl:
break;
}
}

return result;
}

QHash<int, QByteArray> SyncConflictsModel::roleNames() const
{
auto result = QAbstractListModel::roleNames();

result[static_cast<int>(SyncConflictRoles::ExistingFileName)] = "existingFileName";
result[static_cast<int>(SyncConflictRoles::ConflictFileName)] = "conflictFileName";
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;
}

ActivityList SyncConflictsModel::conflictActivities() const
{
return mData;
}

void SyncConflictsModel::setConflictActivities(ActivityList conflicts)
{
if (mData == conflicts) {
return;
}

beginResetModel();

mData = conflicts;
emit conflictActivitiesChanged();

updateConflictsData();

endResetModel();
}

void SyncConflictsModel::updateConflictsData()
{
mConflictData.clear();

for (const auto &oneConflict : qAsConst(mData)) {
auto newConflictData = ConflictInfo{};

const auto info = QFileInfo(oneConflict._file);
// mtimeLabel->setText(info.lastModified().toString());
// sizeLabel->setText(locale().formattedDataSize(info.size()));

// const auto mime = mimeDb.mimeTypeForFile(filename);
// if (QIcon::hasThemeIcon(mime.iconName())) {
// button->setIcon(QIcon::fromTheme(mime.iconName()));
// } else {
// button->setIcon(QIcon(":/qt-project.org/styles/commonstyle/images/file-128.png"));
// }

mConflictData.push_back(std::move(newConflictData));
}
}

}
75 changes: 75 additions & 0 deletions src/gui/syncconflictsmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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.
*/

#ifndef SYNCCONFLICTSMODEL_H
#define SYNCCONFLICTSMODEL_H

#include <QAbstractListModel>

#include "tray/activitydata.h"

namespace OCC {

class SyncConflictsModel : public QAbstractListModel
{
Q_OBJECT

Q_PROPERTY(OCC::ActivityList conflictActivities READ conflictActivities WRITE setConflictActivities NOTIFY conflictActivitiesChanged)

struct ConflictInfo {
};

public:
enum class SyncConflictRoles : int {
ExistingFileName = Qt::UserRole,
ConflictFileName,
ExistingSize,
ConflictSize,
ExistingDate,
ConflictDate,
ExistingSelected,
ConflictSelected,
ExistingPreviewUrl,
ConflictPreviewUrl,
};

Q_ENUM(SyncConflictRoles)

explicit SyncConflictsModel(QObject *parent = nullptr);

[[nodiscard]] int rowCount(const QModelIndex &parent = QModelIndex()) const override;

[[nodiscard]] QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;

[[nodiscard]] QHash<int,QByteArray> roleNames() const override;

[[nodiscard]] OCC::ActivityList conflictActivities() const;

public slots:
void setConflictActivities(OCC::ActivityList conflicts);

signals:
void conflictActivitiesChanged();

private:
void updateConflictsData();

OCC::ActivityList mData;

QVector<ConflictInfo> mConflictData;
};

}

#endif // SYNCCONFLICTSMODEL_H
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ nextcloud_add_test(ShareModel)
nextcloud_add_test(ShareeModel)
nextcloud_add_test(SortedShareModel)
nextcloud_add_test(SecureFileDrop)
nextcloud_add_test(SyncConflictsModel)

target_link_libraries(SecureFileDropTest PRIVATE Nextcloud::sync)
configure_file(fake2eelocksucceeded.json "${PROJECT_BINARY_DIR}/bin/fake2eelocksucceeded.json" COPYONLY)
Expand Down
51 changes: 51 additions & 0 deletions test/testsyncconflictsmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) by Claudio Cambra <claudio.cambra@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 "gui/syncconflictsmodel.h"

#include <QTest>
#include <QAbstractItemModelTester>
#include <QSignalSpy>

using namespace OCC;

class TestSyncConflictsModel : public QObject
{
Q_OBJECT

private:

private slots:
void initTestCase()
{
}

void testSuccessfulFetchShares()
{
SyncConflictsModel model;
QAbstractItemModelTester modelTester(&model);

OCC::ActivityList allConflicts;
allConflicts.push_back(OCC::Activity{});
allConflicts.push_back(OCC::Activity{});
allConflicts.push_back(OCC::Activity{});
allConflicts.push_back(OCC::Activity{});

model.setConflictActivities(allConflicts);
}

};

QTEST_MAIN(TestSyncConflictsModel)
#include "testsyncconflictsmodel.moc"

0 comments on commit bf2266a

Please sign in to comment.