diff --git a/syncthingconnector/syncthingconnection.h b/syncthingconnector/syncthingconnection.h index f5aec99..203b1f5 100644 --- a/syncthingconnector/syncthingconnection.h +++ b/syncthingconnector/syncthingconnection.h @@ -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 children; diff --git a/syncthingmodel/syncthingfilemodel.cpp b/syncthingmodel/syncthingfilemodel.cpp index 8c02f9f..208d501 100644 --- a/syncthingmodel/syncthingfilemodel.cpp +++ b/syncthingmodel/syncthingfilemodel.cpp @@ -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 &&items) { const auto last = items.size() - 1; - beginInsertRows(QModelIndex(), 0, last < std::numeric_limits::max() ? static_cast(last) : std::numeric_limits::max()); - m_items = std::move(items); + beginInsertRows(index(0, 0), 0, last < std::numeric_limits::max() ? static_cast(last) : std::numeric_limits::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(row) >= m_items.size()) { - return QModelIndex(); - } - return createIndex(row, column, &m_items[static_cast(row)]); + return static_cast(row) ? QModelIndex() : createIndex(row, column, &m_root); } auto *const parentItem = reinterpret_cast(parent.internalPointer()); @@ -77,7 +76,11 @@ QString SyncthingFileModel::path(const QModelIndex &index) const auto size = QString::size_type(); parts.reserve(reinterpret_cast(index.internalPointer())->level + 1); for (auto i = index; i.isValid(); i = i.parent()) { - size += parts.emplace_back(reinterpret_cast(i.internalPointer())->name).size(); + const auto *const item = reinterpret_cast(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(parent.internalPointer()); res = parentItem->childrenPopulated || parentItem->type != SyncthingItemType::Directory ? parentItem->children.size() : 1; diff --git a/syncthingmodel/syncthingfilemodel.h b/syncthingmodel/syncthingfilemodel.h index d1931cf..39321e8 100644 --- a/syncthingmodel/syncthingfilemodel.h +++ b/syncthingmodel/syncthingfilemodel.h @@ -3,18 +3,18 @@ #include "./syncthingmodel.h" +#include + #include 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 m_items; + SyncthingItem m_root; }; } // namespace Data diff --git a/syncthingwidgets/misc/otherdialogs.cpp b/syncthingwidgets/misc/otherdialogs.cpp index d167ab2..1a0a8f1 100644 --- a/syncthingwidgets/misc/otherdialogs.cpp +++ b/syncthingwidgets/misc/otherdialogs.cpp @@ -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);