WIP: Allow refreshing root in file browser

This commit is contained in:
Martchus 2024-04-07 23:29:23 +02:00
parent 35d03551c4
commit 92d0db9cd2
4 changed files with 19 additions and 16 deletions

View File

@ -57,7 +57,7 @@ enum class SyncthingItemType { Unknown, File, Directory };
struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingItem {
QString name;
CppUtilities::DateTime modificationTime;
CppUtilities::DateTime modificationTime = CppUtilities::DateTime();
std::size_t size = std::size_t();
SyncthingItemType type = SyncthingItemType::Unknown;
std::vector<SyncthingItem> children;

View File

@ -13,15 +13,17 @@ using namespace CppUtilities;
namespace Data {
SyncthingFileModel::SyncthingFileModel(SyncthingConnection &connection, const QString &dirId, QObject *parent)
SyncthingFileModel::SyncthingFileModel(SyncthingConnection &connection, const SyncthingDir &dir, QObject *parent)
: SyncthingModel(connection, parent)
, m_connection(connection)
, m_dirId(dirId)
, m_dirId(dir.id)
, m_root({.name = dir.displayName(), .modificationTime = dir.lastFileTime, .size = dir.globalStats.bytes, .type = SyncthingItemType::Directory})
{
m_connection.browse(m_dirId, QString(), 1, [this](std::vector<SyncthingItem> &&items) {
const auto last = items.size() - 1;
beginInsertRows(QModelIndex(), 0, last < std::numeric_limits<int>::max() ? static_cast<int>(last) : std::numeric_limits<int>::max());
m_items = std::move(items);
beginInsertRows(index(0, 0), 0, last < std::numeric_limits<int>::max() ? static_cast<int>(last) : std::numeric_limits<int>::max());
m_root.children = std::move(items);
m_root.childrenPopulated = true;
endInsertRows();
});
}
@ -51,10 +53,7 @@ QModelIndex SyncthingFileModel::index(int row, int column, const QModelIndex &pa
}
if (!parent.isValid()) {
if (static_cast<std::size_t>(row) >= m_items.size()) {
return QModelIndex();
}
return createIndex(row, column, &m_items[static_cast<std::size_t>(row)]);
return static_cast<std::size_t>(row) ? QModelIndex() : createIndex(row, column, &m_root);
}
auto *const parentItem = reinterpret_cast<SyncthingItem *>(parent.internalPointer());
@ -77,7 +76,11 @@ QString SyncthingFileModel::path(const QModelIndex &index) const
auto size = QString::size_type();
parts.reserve(reinterpret_cast<SyncthingItem *>(index.internalPointer())->level + 1);
for (auto i = index; i.isValid(); i = i.parent()) {
size += parts.emplace_back(reinterpret_cast<SyncthingItem *>(i.internalPointer())->name).size();
const auto *const item = reinterpret_cast<SyncthingItem *>(i.internalPointer());
if (item == &m_root) {
break;
}
size += parts.emplace_back(item->name).size();
}
res.reserve(size + parts.size());
for (auto i = parts.rbegin(), end = parts.rend(); i != end; ++i) {
@ -191,7 +194,7 @@ int SyncthingFileModel::rowCount(const QModelIndex &parent) const
{
auto res = std::size_t();
if (!parent.isValid()) {
res = m_items.size();
res = m_root.children.size();
} else {
auto *const parentItem = reinterpret_cast<SyncthingItem *>(parent.internalPointer());
res = parentItem->childrenPopulated || parentItem->type != SyncthingItemType::Directory ? parentItem->children.size() : 1;

View File

@ -3,18 +3,18 @@
#include "./syncthingmodel.h"
#include <syncthingconnector/syncthingconnection.h>
#include <vector>
namespace Data {
struct SyncthingItem;
class LIB_SYNCTHING_MODEL_EXPORT SyncthingFileModel : public SyncthingModel {
Q_OBJECT
public:
enum SyncthingFileModelRole { NameRole = SyncthingModelUserRole + 1, SizeRole, ModificationTimeRole, Actions, ActionNames, ActionIcons };
explicit SyncthingFileModel(SyncthingConnection &connection, const QString &dirId, QObject *parent = nullptr);
explicit SyncthingFileModel(SyncthingConnection &connection, const SyncthingDir &dir, QObject *parent = nullptr);
~SyncthingFileModel() override;
public Q_SLOTS:
@ -46,7 +46,7 @@ private:
QString m_dirId;
QModelIndexList m_fetchQueue;
QMetaObject::Connection m_pendingRequest;
mutable std::vector<SyncthingItem> m_items;
SyncthingItem m_root;
};
} // namespace Data

View File

@ -88,7 +88,7 @@ QDialog *browseRemoteFilesDialog(Data::SyncthingConnection &connection, const Da
dlg->setAttribute(Qt::WA_DeleteOnClose);
// setup model/view
auto model = new Data::SyncthingFileModel(connection, dir.id, &connection);
auto model = new Data::SyncthingFileModel(connection, dir, &connection);
auto view = new QTreeView(dlg);
view->setModel(model);
view->setContextMenuPolicy(Qt::CustomContextMenu);