WIP: Allow refreshing root in file browser
This commit is contained in:
parent
35d03551c4
commit
92d0db9cd2
|
@ -57,7 +57,7 @@ enum class SyncthingItemType { Unknown, File, Directory };
|
||||||
|
|
||||||
struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingItem {
|
struct LIB_SYNCTHING_CONNECTOR_EXPORT SyncthingItem {
|
||||||
QString name;
|
QString name;
|
||||||
CppUtilities::DateTime modificationTime;
|
CppUtilities::DateTime modificationTime = CppUtilities::DateTime();
|
||||||
std::size_t size = std::size_t();
|
std::size_t size = std::size_t();
|
||||||
SyncthingItemType type = SyncthingItemType::Unknown;
|
SyncthingItemType type = SyncthingItemType::Unknown;
|
||||||
std::vector<SyncthingItem> children;
|
std::vector<SyncthingItem> children;
|
||||||
|
|
|
@ -13,15 +13,17 @@ using namespace CppUtilities;
|
||||||
|
|
||||||
namespace Data {
|
namespace Data {
|
||||||
|
|
||||||
SyncthingFileModel::SyncthingFileModel(SyncthingConnection &connection, const QString &dirId, QObject *parent)
|
SyncthingFileModel::SyncthingFileModel(SyncthingConnection &connection, const SyncthingDir &dir, QObject *parent)
|
||||||
: SyncthingModel(connection, parent)
|
: SyncthingModel(connection, parent)
|
||||||
, m_connection(connection)
|
, 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) {
|
m_connection.browse(m_dirId, QString(), 1, [this](std::vector<SyncthingItem> &&items) {
|
||||||
const auto last = items.size() - 1;
|
const auto last = items.size() - 1;
|
||||||
beginInsertRows(QModelIndex(), 0, last < std::numeric_limits<int>::max() ? static_cast<int>(last) : std::numeric_limits<int>::max());
|
beginInsertRows(index(0, 0), 0, last < std::numeric_limits<int>::max() ? static_cast<int>(last) : std::numeric_limits<int>::max());
|
||||||
m_items = std::move(items);
|
m_root.children = std::move(items);
|
||||||
|
m_root.childrenPopulated = true;
|
||||||
endInsertRows();
|
endInsertRows();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -51,10 +53,7 @@ QModelIndex SyncthingFileModel::index(int row, int column, const QModelIndex &pa
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!parent.isValid()) {
|
if (!parent.isValid()) {
|
||||||
if (static_cast<std::size_t>(row) >= m_items.size()) {
|
return static_cast<std::size_t>(row) ? QModelIndex() : createIndex(row, column, &m_root);
|
||||||
return QModelIndex();
|
|
||||||
}
|
|
||||||
return createIndex(row, column, &m_items[static_cast<std::size_t>(row)]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto *const parentItem = reinterpret_cast<SyncthingItem *>(parent.internalPointer());
|
auto *const parentItem = reinterpret_cast<SyncthingItem *>(parent.internalPointer());
|
||||||
|
@ -77,7 +76,11 @@ QString SyncthingFileModel::path(const QModelIndex &index) const
|
||||||
auto size = QString::size_type();
|
auto size = QString::size_type();
|
||||||
parts.reserve(reinterpret_cast<SyncthingItem *>(index.internalPointer())->level + 1);
|
parts.reserve(reinterpret_cast<SyncthingItem *>(index.internalPointer())->level + 1);
|
||||||
for (auto i = index; i.isValid(); i = i.parent()) {
|
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());
|
res.reserve(size + parts.size());
|
||||||
for (auto i = parts.rbegin(), end = parts.rend(); i != end; ++i) {
|
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();
|
auto res = std::size_t();
|
||||||
if (!parent.isValid()) {
|
if (!parent.isValid()) {
|
||||||
res = m_items.size();
|
res = m_root.children.size();
|
||||||
} else {
|
} else {
|
||||||
auto *const parentItem = reinterpret_cast<SyncthingItem *>(parent.internalPointer());
|
auto *const parentItem = reinterpret_cast<SyncthingItem *>(parent.internalPointer());
|
||||||
res = parentItem->childrenPopulated || parentItem->type != SyncthingItemType::Directory ? parentItem->children.size() : 1;
|
res = parentItem->childrenPopulated || parentItem->type != SyncthingItemType::Directory ? parentItem->children.size() : 1;
|
||||||
|
|
|
@ -3,18 +3,18 @@
|
||||||
|
|
||||||
#include "./syncthingmodel.h"
|
#include "./syncthingmodel.h"
|
||||||
|
|
||||||
|
#include <syncthingconnector/syncthingconnection.h>
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace Data {
|
namespace Data {
|
||||||
|
|
||||||
struct SyncthingItem;
|
|
||||||
|
|
||||||
class LIB_SYNCTHING_MODEL_EXPORT SyncthingFileModel : public SyncthingModel {
|
class LIB_SYNCTHING_MODEL_EXPORT SyncthingFileModel : public SyncthingModel {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum SyncthingFileModelRole { NameRole = SyncthingModelUserRole + 1, SizeRole, ModificationTimeRole, Actions, ActionNames, ActionIcons };
|
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;
|
~SyncthingFileModel() override;
|
||||||
|
|
||||||
public Q_SLOTS:
|
public Q_SLOTS:
|
||||||
|
@ -46,7 +46,7 @@ private:
|
||||||
QString m_dirId;
|
QString m_dirId;
|
||||||
QModelIndexList m_fetchQueue;
|
QModelIndexList m_fetchQueue;
|
||||||
QMetaObject::Connection m_pendingRequest;
|
QMetaObject::Connection m_pendingRequest;
|
||||||
mutable std::vector<SyncthingItem> m_items;
|
SyncthingItem m_root;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Data
|
} // namespace Data
|
||||||
|
|
|
@ -88,7 +88,7 @@ QDialog *browseRemoteFilesDialog(Data::SyncthingConnection &connection, const Da
|
||||||
dlg->setAttribute(Qt::WA_DeleteOnClose);
|
dlg->setAttribute(Qt::WA_DeleteOnClose);
|
||||||
|
|
||||||
// setup model/view
|
// 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);
|
auto view = new QTreeView(dlg);
|
||||||
view->setModel(model);
|
view->setModel(model);
|
||||||
view->setContextMenuPolicy(Qt::CustomContextMenu);
|
view->setContextMenuPolicy(Qt::CustomContextMenu);
|
||||||
|
|
Loading…
Reference in New Issue