From f80fefa34fbaf4383d028fdf94c20faf0c16366e Mon Sep 17 00:00:00 2001 From: Agel Date: Fri, 17 Apr 2026 11:14:52 +0200 Subject: [PATCH] fix macos book opening --- src/backend/book/Book.cpp | 6 ++++-- src/backend/formats/TxtReader.cpp | 24 ++++++++++++------------ 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/backend/book/Book.cpp b/src/backend/book/Book.cpp index a8918fa..1fc5363 100644 --- a/src/backend/book/Book.cpp +++ b/src/backend/book/Book.cpp @@ -81,11 +81,12 @@ void Book::openImpl(const QString &filePath) m_openFuture = QtConcurrent::run([this, filePath, generation]() { auto reader = m_factory.create(filePath); if (!reader) { - QMetaObject::invokeMethod(this, [this, generation]() { + QMetaObject::invokeMethod(this, [this, generation, filePath]() { if (generation != m_openGeneration) return; m_loading = false; emit loadingChanged(); m_errorMessage = QStringLiteral("No format reader available for this file type"); + qWarning() << "Book load failed:" << filePath << "—" << m_errorMessage; emit errorMessageChanged(); }, Qt::QueuedConnection); return; @@ -93,11 +94,12 @@ void Book::openImpl(const QString &filePath) if (!reader->open(filePath)) { const QString err = reader->lastError(); - QMetaObject::invokeMethod(this, [this, generation, err]() { + QMetaObject::invokeMethod(this, [this, generation, filePath, err]() { if (generation != m_openGeneration) return; m_loading = false; emit loadingChanged(); m_errorMessage = err; + qWarning() << "Book load failed:" << filePath << "—" << m_errorMessage; emit errorMessageChanged(); }, Qt::QueuedConnection); return; diff --git a/src/backend/formats/TxtReader.cpp b/src/backend/formats/TxtReader.cpp index 82688b8..d65a090 100644 --- a/src/backend/formats/TxtReader.cpp +++ b/src/backend/formats/TxtReader.cpp @@ -8,6 +8,8 @@ #include #include +#include + static constexpr int kChunkSize = 4096; static QString decodeWithIconv(const QByteArray &raw, const char *charset) @@ -98,18 +100,17 @@ static QStringList splitRawText(const QString &content) bool TxtReader::open(const QString &filePath) { - QFile file(filePath); - if (!file.exists()) { - m_error = QStringLiteral("File not found: ") + filePath; + // Use std::ifstream to bypass Qt's CFStringGetFileSystemRepresentation() on macOS, + // which re-normalizes paths to NFD and breaks NFC filenames on SMB/ZFS mounts. + std::ifstream f(filePath.toUtf8().constData(), std::ios::binary | std::ios::ate); + if (!f) { + m_error = QStringLiteral("Cannot open file: ") + filePath; return false; } - if (!file.open(QIODevice::ReadOnly)) { - m_error = QStringLiteral("Cannot open file: ") + file.errorString(); - return false; - } - - QByteArray raw = file.readAll(); - file.close(); + const auto size = f.tellg(); + f.seekg(0); + QByteArray raw(static_cast(size), '\0'); + f.read(raw.data(), size); m_contentHash = QCryptographicHash::hash(raw, QCryptographicHash::Md5); @@ -145,8 +146,7 @@ bool TxtReader::open(const QString &filePath) bool TxtReader::loadMetadata(const QString &filePath) { - QFile file(filePath); - if (!file.exists()) { + if (!std::ifstream(filePath.toUtf8().constData())) { m_error = QStringLiteral("File not found: ") + filePath; return false; }