Avoid hardcoding the path separator in `SyncthingFileModel`

This commit is contained in:
Martchus 2024-05-25 19:21:45 +02:00
parent 22cc048e33
commit 6c388abac1
2 changed files with 11 additions and 9 deletions

View File

@ -22,15 +22,15 @@ using namespace CppUtilities;
namespace Data { namespace Data {
/// \cond /// \cond
static void populatePath(const QString &root, std::vector<std::unique_ptr<SyncthingItem>> &items) static void populatePath(const QString &root, QChar pathSeparator, std::vector<std::unique_ptr<SyncthingItem>> &items)
{ {
if (root.isEmpty()) { if (root.isEmpty()) {
for (auto &item : items) { for (auto &item : items) {
populatePath(item->path = item->name, item->children); populatePath(item->path = item->name, pathSeparator, item->children);
} }
} else { } else {
for (auto &item : items) { for (auto &item : items) {
populatePath(item->path = root % QChar('/') % item->name, item->children); populatePath(item->path = root % pathSeparator % item->name, pathSeparator, item->children);
} }
} }
} }
@ -70,6 +70,7 @@ SyncthingFileModel::SyncthingFileModel(SyncthingConnection &connection, const Sy
m_localPath = dir.pathWithoutTrailingSlash().toString(); m_localPath = dir.pathWithoutTrailingSlash().toString();
connect(&m_localItemLookup, &QFutureWatcherBase::finished, this, &SyncthingFileModel::handleLocalLookupFinished); connect(&m_localItemLookup, &QFutureWatcherBase::finished, this, &SyncthingFileModel::handleLocalLookupFinished);
} }
m_pathSeparator = m_connection.pathSeparator().size() == 1 ? m_connection.pathSeparator().front() : QDir::separator();
m_root->name = dir.displayName(); m_root->name = dir.displayName();
m_root->modificationTime = dir.lastFileTime; m_root->modificationTime = dir.lastFileTime;
m_root->size = dir.globalStats.bytes; m_root->size = dir.globalStats.bytes;
@ -125,7 +126,7 @@ QModelIndex SyncthingFileModel::index(int row, int column, const QModelIndex &pa
QModelIndex SyncthingFileModel::index(const QString &path) const QModelIndex SyncthingFileModel::index(const QString &path) const
{ {
auto parts = path.split(QChar('/'), Qt::SkipEmptyParts); auto parts = path.split(m_pathSeparator, Qt::SkipEmptyParts);
auto *parent = m_root.get(); auto *parent = m_root.get();
auto res = createIndex(0, 0, parent); auto res = createIndex(0, 0, parent);
for (const auto &part : parts) { for (const auto &part : parts) {
@ -463,7 +464,7 @@ void SyncthingFileModel::triggerAction(const QString &action, const QModelIndex
return; return;
} }
const auto relPath = index.data(PathRole).toString(); const auto relPath = index.data(PathRole).toString();
const auto path = relPath.isEmpty() ? m_localPath : QString(m_localPath % QChar('/') % relPath); const auto path = relPath.isEmpty() ? m_localPath : QString(m_localPath % m_pathSeparator % relPath);
if (action == QLatin1String("open")) { if (action == QLatin1String("open")) {
QtUtilities::openLocalFileOrDir(path); QtUtilities::openLocalFileOrDir(path);
} else if (action == QLatin1String("copy-path")) { } else if (action == QLatin1String("copy-path")) {
@ -563,7 +564,7 @@ void SyncthingFileModel::processFetchQueue(const QString &lastItemPath)
for (auto &item : items) { for (auto &item : items) {
item->parent = refreshedItem; item->parent = refreshedItem;
} }
populatePath(refreshedItem->path, items); populatePath(refreshedItem->path, m_pathSeparator, items);
beginInsertRows( beginInsertRows(
refreshedIndex, 0, last < std::numeric_limits<int>::max() ? static_cast<int>(last) : std::numeric_limits<int>::max()); refreshedIndex, 0, last < std::numeric_limits<int>::max() ? static_cast<int>(last) : std::numeric_limits<int>::max());
refreshedItem->children = std::move(items); refreshedItem->children = std::move(items);
@ -593,7 +594,7 @@ void SyncthingFileModel::processFetchQueue(const QString &lastItemPath)
if (m_localPath.isEmpty()) { if (m_localPath.isEmpty()) {
return; return;
} }
m_pendingRequest.localLookup = QtConcurrent::run([dir = QDir(m_localPath % QChar('/') % path)] { m_pendingRequest.localLookup = QtConcurrent::run([dir = QDir(m_localPath % m_pathSeparator % path)] {
auto items = std::make_shared<std::map<QString, SyncthingItem>>(); auto items = std::make_shared<std::map<QString, SyncthingItem>>();
auto entries = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot); auto entries = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
for (const auto &entry : entries) { for (const auto &entry : entries) {
@ -649,7 +650,7 @@ void SyncthingFileModel::matchItemAgainstIgnorePatterns(SyncthingItem &item) con
} }
auto index = std::size_t(); auto index = std::size_t();
for (const auto &ignorePattern : m_presentIgnorePatterns) { for (const auto &ignorePattern : m_presentIgnorePatterns) {
if (ignorePattern.matches(item.path)) { if (ignorePattern.matches(item.path, m_pathSeparator)) {
item.ignorePattern = index; item.ignorePattern = index;
break; break;
} else { } else {
@ -711,7 +712,7 @@ void SyncthingFileModel::handleLocalLookupFinished()
if (refreshedItem->checked == Qt::Checked) { if (refreshedItem->checked == Qt::Checked) {
setChildrenChecked(item.get(), item->checked = Qt::Checked); setChildrenChecked(item.get(), item->checked = Qt::Checked);
} }
populatePath(item->path = refreshedItem->path % QChar('/') % item->name, item->children); populatePath(item->path = refreshedItem->path % m_pathSeparator % item->name, m_pathSeparator, item->children);
endInsertRows(); endInsertRows();
} }
if (refreshedItem->children.size() != previousChildCount) { if (refreshedItem->children.size() != previousChildCount) {

View File

@ -91,6 +91,7 @@ private:
QueryResult m_pendingRequest; QueryResult m_pendingRequest;
QFutureWatcher<LocalLookupRes> m_localItemLookup; QFutureWatcher<LocalLookupRes> m_localItemLookup;
std::unique_ptr<SyncthingItem> m_root; std::unique_ptr<SyncthingItem> m_root;
QChar m_pathSeparator;
bool m_selectionMode; bool m_selectionMode;
bool m_hasIgnorePatterns; bool m_hasIgnorePatterns;
}; };