added txt support
This commit is contained in:
1 parent
3c0037cfc9
commit
64a70bc4cf
22 files changed
+799
-185
No files matched your search
@@ -8,7 +8,7 @@ Asteria Qt is a cross-platform desktop book/document reader built with Qt6/C++.
|
|||||||
|
|
||||||
**Target platforms:** Windows and Linux (both required from day one); macOS planned
|
**Target platforms:** Windows and Linux (both required from day one); macOS planned
|
||||||
**Current dev environment:** Linux
|
**Current dev environment:** Linux
|
||||||
**Feature tier:** Minimal reader — open, read, bookmarks
|
**Feature tier:** Minimal reader — open, read
|
||||||
|
|
||||||
### Format Support
|
### Format Support
|
||||||
|
|
||||||
@@ -101,7 +101,7 @@ ctest --test-dir build --output-on-failure
|
|||||||
- Tests live in `tests/` mirroring the `src/backend/` structure (e.g., `tests/formats/TxtReaderTest.cpp`)
|
- Tests live in `tests/` mirroring the `src/backend/` structure (e.g., `tests/formats/TxtReaderTest.cpp`)
|
||||||
- Use `Qt Test` (`QTest`) framework — include `find_package(Qt6 REQUIRED COMPONENTS Test)` in CMakeLists
|
- Use `Qt Test` (`QTest`) framework — include `find_package(Qt6 REQUIRED COMPONENTS Test)` in CMakeLists
|
||||||
- Each format reader must have a test that loads a real sample file from `tests/fixtures/`
|
- Each format reader must have a test that loads a real sample file from `tests/fixtures/`
|
||||||
- `BookController`, `ReadingModel`, `BookmarkModel`, `BookmarkStore`, and `AppState` must each have unit tests
|
- `BookController`, `ReadingModel`, and `AppState` must each have unit tests
|
||||||
- QML-only UI code is exempt from TDD; backend logic extracted to C++ is not
|
- QML-only UI code is exempt from TDD; backend logic extracted to C++ is not
|
||||||
- Run `ctest --test-dir build --output-on-failure` before considering any task complete
|
- Run `ctest --test-dir build --output-on-failure` before considering any task complete
|
||||||
|
|
||||||
@@ -115,7 +115,6 @@ ctest --test-dir build --output-on-failure
|
|||||||
|---|---|
|
|---|---|
|
||||||
| `BookController` | Open/close book, owns reader (via `ReaderFactory`), exposes metadata (title, author), TOC |
|
| `BookController` | Open/close book, owns reader (via `ReaderFactory`), exposes metadata (title, author), TOC |
|
||||||
| `ReadingModel` | Current chapter content, position, next/prev navigation, progress % |
|
| `ReadingModel` | Current chapter content, position, next/prev navigation, progress % |
|
||||||
| `BookmarkModel` | Bookmark CRUD for the current book (wraps `BookmarkStore`) |
|
|
||||||
| `AppState` | Window geometry, last file, preferences (`QSettings`) |
|
| `AppState` | Window geometry, last file, preferences (`QSettings`) |
|
||||||
|
|
||||||
Sidebar state, header state, etc. stay as pure QML properties — no C++ needed.
|
Sidebar state, header state, etc. stay as pure QML properties — no C++ needed.
|
||||||
@@ -170,8 +169,6 @@ src/
|
|||||||
backend/
|
backend/
|
||||||
BookController.h/cpp # Open/close, metadata, TOC — exposed to QML
|
BookController.h/cpp # Open/close, metadata, TOC — exposed to QML
|
||||||
ReadingModel.h/cpp # Chapter content, position, navigation — exposed to QML
|
ReadingModel.h/cpp # Chapter content, position, navigation — exposed to QML
|
||||||
BookmarkModel.h/cpp # Bookmark CRUD — exposed to QML
|
|
||||||
BookmarkStore.h/cpp # Persistence layer (QSettings)
|
|
||||||
AppState.h/cpp # Window/session state (QSettings) — exposed to QML
|
AppState.h/cpp # Window/session state (QSettings) — exposed to QML
|
||||||
FormatReader.h # Abstract interface + Chapter/TocEntry structs
|
FormatReader.h # Abstract interface + Chapter/TocEntry structs
|
||||||
ReaderFactory.h/cpp # Extension → reader mapping
|
ReaderFactory.h/cpp # Extension → reader mapping
|
||||||
@@ -207,8 +204,6 @@ tests/
|
|||||||
EpubReaderTest.cpp
|
EpubReaderTest.cpp
|
||||||
BookControllerTest.cpp
|
BookControllerTest.cpp
|
||||||
ReadingModelTest.cpp
|
ReadingModelTest.cpp
|
||||||
BookmarkModelTest.cpp
|
|
||||||
BookmarkStoreTest.cpp
|
|
||||||
AppStateTest.cpp
|
AppStateTest.cpp
|
||||||
fixtures/
|
fixtures/
|
||||||
sample.txt
|
sample.txt
|
||||||
|
|||||||
+38
-16
@@ -11,18 +11,17 @@ find_package(Qt6 REQUIRED COMPONENTS
|
|||||||
Gui
|
Gui
|
||||||
Qml
|
Qml
|
||||||
Quick
|
Quick
|
||||||
|
Widgets
|
||||||
Concurrent
|
Concurrent
|
||||||
Svg
|
Svg
|
||||||
QuickControls2
|
QuickControls2
|
||||||
Test
|
Test
|
||||||
)
|
)
|
||||||
find_package(Qt6 OPTIONAL_COMPONENTS Pdf)
|
find_package(Qt6 OPTIONAL_COMPONENTS Pdf DBus)
|
||||||
|
|
||||||
# Third-party libraries
|
# Third-party libraries
|
||||||
find_package(PkgConfig QUIET)
|
find_package(PkgConfig REQUIRED)
|
||||||
if(PkgConfig_FOUND)
|
pkg_check_modules(UCHARDET REQUIRED uchardet)
|
||||||
pkg_check_modules(UCHARDET uchardet)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Source files
|
# Source files
|
||||||
set(SOURCES
|
set(SOURCES
|
||||||
@@ -30,16 +29,14 @@ set(SOURCES
|
|||||||
src/backend/ReaderFactory.h
|
src/backend/ReaderFactory.h
|
||||||
src/backend/ReaderFactory.cpp
|
src/backend/ReaderFactory.cpp
|
||||||
src/backend/FormatReader.h
|
src/backend/FormatReader.h
|
||||||
src/backend/BookmarkStore.h
|
|
||||||
src/backend/BookmarkStore.cpp
|
|
||||||
src/backend/AppState.h
|
src/backend/AppState.h
|
||||||
src/backend/AppState.cpp
|
src/backend/AppState.cpp
|
||||||
src/backend/ReadingModel.h
|
src/backend/ReadingModel.h
|
||||||
src/backend/ReadingModel.cpp
|
src/backend/ReadingModel.cpp
|
||||||
src/backend/BookmarkModel.h
|
|
||||||
src/backend/BookmarkModel.cpp
|
|
||||||
src/backend/BookController.h
|
src/backend/BookController.h
|
||||||
src/backend/BookController.cpp
|
src/backend/BookController.cpp
|
||||||
|
src/backend/formats/TxtReader.h
|
||||||
|
src/backend/formats/TxtReader.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
# Executable
|
# Executable
|
||||||
@@ -52,6 +49,7 @@ target_link_libraries(asteria-qt PRIVATE
|
|||||||
Qt6::Gui
|
Qt6::Gui
|
||||||
Qt6::Qml
|
Qt6::Qml
|
||||||
Qt6::Quick
|
Qt6::Quick
|
||||||
|
Qt6::Widgets
|
||||||
Qt6::Concurrent
|
Qt6::Concurrent
|
||||||
Qt6::Svg
|
Qt6::Svg
|
||||||
Qt6::QuickControls2
|
Qt6::QuickControls2
|
||||||
@@ -62,11 +60,13 @@ if(Qt6Pdf_FOUND)
|
|||||||
target_compile_definitions(asteria-qt PRIVATE HAS_QT_PDF)
|
target_compile_definitions(asteria-qt PRIVATE HAS_QT_PDF)
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
if(UCHARDET_FOUND)
|
if(Qt6DBus_FOUND)
|
||||||
|
target_link_libraries(asteria-qt PRIVATE Qt6::DBus)
|
||||||
|
target_compile_definitions(asteria-qt PRIVATE HAS_QT_DBUS)
|
||||||
|
endif()
|
||||||
|
|
||||||
target_include_directories(asteria-qt PRIVATE ${UCHARDET_INCLUDE_DIRS})
|
target_include_directories(asteria-qt PRIVATE ${UCHARDET_INCLUDE_DIRS})
|
||||||
target_link_libraries(asteria-qt PRIVATE ${UCHARDET_LIBRARIES})
|
target_link_libraries(asteria-qt PRIVATE ${UCHARDET_LIBRARIES})
|
||||||
target_compile_definitions(asteria-qt PRIVATE HAS_UCHARDET)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
# Set resource aliases so src/qml/* maps to qrc:/Asteria/*
|
# Set resource aliases so src/qml/* maps to qrc:/Asteria/*
|
||||||
set_source_files_properties(src/qml/Main.qml PROPERTIES QT_RESOURCE_ALIAS Main.qml)
|
set_source_files_properties(src/qml/Main.qml PROPERTIES QT_RESOURCE_ALIAS Main.qml)
|
||||||
@@ -109,15 +109,37 @@ enable_testing()
|
|||||||
|
|
||||||
# Helper function to add tests
|
# Helper function to add tests
|
||||||
function(add_asteria_test TEST_NAME TEST_SOURCE)
|
function(add_asteria_test TEST_NAME TEST_SOURCE)
|
||||||
qt_add_executable(${TEST_NAME} ${TEST_SOURCE})
|
qt_add_executable(${TEST_NAME} ${TEST_SOURCE} ${ARGN})
|
||||||
target_include_directories(${TEST_NAME} PRIVATE src)
|
target_include_directories(${TEST_NAME} PRIVATE src)
|
||||||
target_link_libraries(${TEST_NAME} PRIVATE
|
target_link_libraries(${TEST_NAME} PRIVATE
|
||||||
Qt6::Core
|
Qt6::Core
|
||||||
Qt6::Test
|
Qt6::Test
|
||||||
)
|
)
|
||||||
|
target_compile_definitions(${TEST_NAME} PRIVATE
|
||||||
|
FIXTURES_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures"
|
||||||
|
)
|
||||||
|
target_include_directories(${TEST_NAME} PRIVATE ${UCHARDET_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(${TEST_NAME} PRIVATE ${UCHARDET_LIBRARIES})
|
||||||
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
|
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
|
||||||
endfunction()
|
endfunction()
|
||||||
|
|
||||||
# Tests will be added here as they are created, e.g.:
|
add_asteria_test(TxtReaderTest tests/formats/TxtReaderTest.cpp
|
||||||
# add_asteria_test(TxtReaderTest tests/formats/TxtReaderTest.cpp)
|
src/backend/formats/TxtReader.cpp)
|
||||||
# add_asteria_test(BookControllerTest tests/BookControllerTest.cpp)
|
|
||||||
|
add_asteria_test(ReaderFactoryTest tests/ReaderFactoryTest.cpp
|
||||||
|
src/backend/ReaderFactory.cpp
|
||||||
|
src/backend/formats/TxtReader.cpp)
|
||||||
|
|
||||||
|
add_asteria_test(ReadingModelTest tests/ReadingModelTest.cpp
|
||||||
|
src/backend/ReadingModel.cpp)
|
||||||
|
|
||||||
|
add_asteria_test(BookControllerTest tests/BookControllerTest.cpp
|
||||||
|
src/backend/BookController.cpp
|
||||||
|
src/backend/ReaderFactory.cpp
|
||||||
|
src/backend/ReadingModel.cpp
|
||||||
|
src/backend/formats/TxtReader.cpp)
|
||||||
|
target_link_libraries(BookControllerTest PRIVATE Qt6::Widgets)
|
||||||
|
if(Qt6DBus_FOUND)
|
||||||
|
target_link_libraries(BookControllerTest PRIVATE Qt6::DBus)
|
||||||
|
target_compile_definitions(BookControllerTest PRIVATE HAS_QT_DBUS)
|
||||||
|
endif()
|
||||||
@@ -1,7 +1,71 @@
|
|||||||
#include "BookController.h"
|
#include "BookController.h"
|
||||||
#include "ReaderFactory.h"
|
#include "ReaderFactory.h"
|
||||||
#include "ReadingModel.h"
|
#include "ReadingModel.h"
|
||||||
#include "BookmarkModel.h"
|
#include <QFileDialog>
|
||||||
|
|
||||||
|
#ifdef HAS_QT_DBUS
|
||||||
|
#include <QDBusConnection>
|
||||||
|
#include <QDBusMessage>
|
||||||
|
#include <QDBusArgument>
|
||||||
|
#include <QDBusMetaType>
|
||||||
|
#include <QRandomGenerator>
|
||||||
|
|
||||||
|
// Typed structs so Qt can derive D-Bus signatures without reading a
|
||||||
|
// write-only QDBusArgument (which causes a SIGSEGV).
|
||||||
|
|
||||||
|
struct PortalGlob {
|
||||||
|
uint type; // 0 = glob pattern
|
||||||
|
QString pattern;
|
||||||
|
};
|
||||||
|
Q_DECLARE_METATYPE(PortalGlob)
|
||||||
|
|
||||||
|
QDBusArgument &operator<<(QDBusArgument &a, const PortalGlob &g)
|
||||||
|
{
|
||||||
|
a.beginStructure();
|
||||||
|
a << g.type << g.pattern;
|
||||||
|
a.endStructure();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
const QDBusArgument &operator>>(const QDBusArgument &a, PortalGlob &g)
|
||||||
|
{
|
||||||
|
a.beginStructure();
|
||||||
|
a >> g.type >> g.pattern;
|
||||||
|
a.endStructure();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PortalFilter {
|
||||||
|
QString name;
|
||||||
|
QList<PortalGlob> globs;
|
||||||
|
};
|
||||||
|
Q_DECLARE_METATYPE(PortalFilter)
|
||||||
|
|
||||||
|
QDBusArgument &operator<<(QDBusArgument &a, const PortalFilter &f)
|
||||||
|
{
|
||||||
|
a.beginStructure();
|
||||||
|
a << f.name << f.globs;
|
||||||
|
a.endStructure();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
const QDBusArgument &operator>>(const QDBusArgument &a, PortalFilter &f)
|
||||||
|
{
|
||||||
|
a.beginStructure();
|
||||||
|
a >> f.name >> f.globs;
|
||||||
|
a.endStructure();
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void registerPortalTypes()
|
||||||
|
{
|
||||||
|
static bool done = false;
|
||||||
|
if (done) return;
|
||||||
|
done = true;
|
||||||
|
qDBusRegisterMetaType<PortalGlob>();
|
||||||
|
qDBusRegisterMetaType<QList<PortalGlob>>();
|
||||||
|
qDBusRegisterMetaType<PortalFilter>();
|
||||||
|
qDBusRegisterMetaType<QList<PortalFilter>>();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
BookController::BookController(QObject *parent)
|
BookController::BookController(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
@@ -45,11 +109,6 @@ void BookController::setReadingModel(ReadingModel *model)
|
|||||||
m_readingModel = model;
|
m_readingModel = model;
|
||||||
}
|
}
|
||||||
|
|
||||||
void BookController::setBookmarkModel(BookmarkModel *model)
|
|
||||||
{
|
|
||||||
m_bookmarkModel = model;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookController::openBook(const QUrl &fileUrl)
|
void BookController::openBook(const QUrl &fileUrl)
|
||||||
{
|
{
|
||||||
QString filePath = fileUrl.toLocalFile();
|
QString filePath = fileUrl.toLocalFile();
|
||||||
@@ -86,15 +145,131 @@ void BookController::openBook(const QUrl &fileUrl)
|
|||||||
if (m_readingModel)
|
if (m_readingModel)
|
||||||
m_readingModel->setReader(m_reader.get());
|
m_readingModel->setReader(m_reader.get());
|
||||||
|
|
||||||
if (m_bookmarkModel)
|
|
||||||
m_bookmarkModel->setBookId(filePath);
|
|
||||||
|
|
||||||
m_loading = false;
|
m_loading = false;
|
||||||
emit loadingChanged();
|
emit loadingChanged();
|
||||||
emit bookChanged();
|
emit bookChanged();
|
||||||
emit bookReady();
|
emit bookReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BookController::browseAndOpen()
|
||||||
|
{
|
||||||
|
#ifdef HAS_QT_DBUS
|
||||||
|
auto bus = QDBusConnection::sessionBus();
|
||||||
|
if (!bus.isConnected()) {
|
||||||
|
browseAndOpenFallback();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the FileChooser portal is available
|
||||||
|
QDBusMessage ping = QDBusMessage::createMethodCall(
|
||||||
|
QStringLiteral("org.freedesktop.portal.Desktop"),
|
||||||
|
QStringLiteral("/org/freedesktop/portal/desktop"),
|
||||||
|
QStringLiteral("org.freedesktop.DBus.Properties"),
|
||||||
|
QStringLiteral("Get"));
|
||||||
|
ping << QStringLiteral("org.freedesktop.portal.FileChooser")
|
||||||
|
<< QStringLiteral("version");
|
||||||
|
QDBusMessage pong = bus.call(ping, QDBus::Block, 500);
|
||||||
|
if (pong.type() == QDBusMessage::ErrorMessage) {
|
||||||
|
browseAndOpenFallback();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a unique handle token and compute the expected response path
|
||||||
|
QString token = QStringLiteral("asteria_")
|
||||||
|
+ QString::number(QRandomGenerator::global()->generate());
|
||||||
|
QString sender = bus.baseService();
|
||||||
|
sender.remove(0, 1); // strip leading ':'
|
||||||
|
sender.replace(QLatin1Char('.'), QLatin1Char('_'));
|
||||||
|
m_portalHandlePath = QStringLiteral("/org/freedesktop/portal/desktop/request/%1/%2")
|
||||||
|
.arg(sender, token);
|
||||||
|
|
||||||
|
// Connect to the Response signal BEFORE making the call (avoids race)
|
||||||
|
bus.connect(QStringLiteral("org.freedesktop.portal.Desktop"),
|
||||||
|
m_portalHandlePath,
|
||||||
|
QStringLiteral("org.freedesktop.portal.Request"),
|
||||||
|
QStringLiteral("Response"),
|
||||||
|
this,
|
||||||
|
SLOT(handlePortalResponse(uint,QVariantMap)));
|
||||||
|
|
||||||
|
registerPortalTypes();
|
||||||
|
|
||||||
|
// Build file filters using registered types (no raw QDBusArgument in QVariant)
|
||||||
|
PortalFilter bookFilter;
|
||||||
|
bookFilter.name = QStringLiteral("Book files");
|
||||||
|
for (const char *ext : {"*.txt", "*.fb2", "*.epub", "*.pdf", "*.doc",
|
||||||
|
"*.rtf", "*.html", "*.htm", "*.docx",
|
||||||
|
"*.djvu", "*.odt", "*.chm"}) {
|
||||||
|
bookFilter.globs.append({0, QString::fromLatin1(ext)});
|
||||||
|
}
|
||||||
|
|
||||||
|
PortalFilter allFilter;
|
||||||
|
allFilter.name = QStringLiteral("All files");
|
||||||
|
allFilter.globs.append({0, QStringLiteral("*")});
|
||||||
|
|
||||||
|
QList<PortalFilter> filters{bookFilter, allFilter};
|
||||||
|
|
||||||
|
QVariantMap options;
|
||||||
|
options[QStringLiteral("handle_token")] = token;
|
||||||
|
options[QStringLiteral("filters")] = QVariant::fromValue(filters);
|
||||||
|
|
||||||
|
// Build the OpenFile call
|
||||||
|
QDBusMessage msg = QDBusMessage::createMethodCall(
|
||||||
|
QStringLiteral("org.freedesktop.portal.Desktop"),
|
||||||
|
QStringLiteral("/org/freedesktop/portal/desktop"),
|
||||||
|
QStringLiteral("org.freedesktop.portal.FileChooser"),
|
||||||
|
QStringLiteral("OpenFile"));
|
||||||
|
|
||||||
|
msg << QString(); // parent_window (empty)
|
||||||
|
msg << QStringLiteral("Open Book"); // title
|
||||||
|
msg << options;
|
||||||
|
|
||||||
|
QDBusMessage reply = bus.call(msg);
|
||||||
|
if (reply.type() == QDBusMessage::ErrorMessage) {
|
||||||
|
bus.disconnect(QStringLiteral("org.freedesktop.portal.Desktop"),
|
||||||
|
m_portalHandlePath,
|
||||||
|
QStringLiteral("org.freedesktop.portal.Request"),
|
||||||
|
QStringLiteral("Response"),
|
||||||
|
this,
|
||||||
|
SLOT(handlePortalResponse(uint,QVariantMap)));
|
||||||
|
browseAndOpenFallback();
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
browseAndOpenFallback();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef HAS_QT_DBUS
|
||||||
|
void BookController::handlePortalResponse(uint response,
|
||||||
|
const QVariantMap &results)
|
||||||
|
{
|
||||||
|
QDBusConnection::sessionBus().disconnect(
|
||||||
|
QStringLiteral("org.freedesktop.portal.Desktop"),
|
||||||
|
m_portalHandlePath,
|
||||||
|
QStringLiteral("org.freedesktop.portal.Request"),
|
||||||
|
QStringLiteral("Response"),
|
||||||
|
this,
|
||||||
|
SLOT(handlePortalResponse(uint,QVariantMap)));
|
||||||
|
|
||||||
|
if (response != 0)
|
||||||
|
return; // user cancelled or error
|
||||||
|
|
||||||
|
QStringList uris = results.value(QStringLiteral("uris")).toStringList();
|
||||||
|
if (!uris.isEmpty())
|
||||||
|
openBook(QUrl(uris.first()));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void BookController::browseAndOpenFallback()
|
||||||
|
{
|
||||||
|
QString path = QFileDialog::getOpenFileName(
|
||||||
|
nullptr,
|
||||||
|
QStringLiteral("Open Book"),
|
||||||
|
QString(),
|
||||||
|
QStringLiteral("Book files (*.txt *.fb2 *.epub *.pdf *.doc *.rtf *.html *.htm *.docx *.djvu *.odt *.chm);;All files (*)"));
|
||||||
|
if (!path.isEmpty())
|
||||||
|
openBook(QUrl::fromLocalFile(path));
|
||||||
|
}
|
||||||
|
|
||||||
void BookController::closeBook()
|
void BookController::closeBook()
|
||||||
{
|
{
|
||||||
m_reader.reset();
|
m_reader.reset();
|
||||||
|
|||||||
@@ -10,7 +10,6 @@
|
|||||||
#include "FormatReader.h"
|
#include "FormatReader.h"
|
||||||
|
|
||||||
class ReadingModel;
|
class ReadingModel;
|
||||||
class BookmarkModel;
|
|
||||||
|
|
||||||
class BookController : public QObject {
|
class BookController : public QObject {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
@@ -32,9 +31,9 @@ public:
|
|||||||
QList<TocEntry> toc() const;
|
QList<TocEntry> toc() const;
|
||||||
|
|
||||||
void setReadingModel(ReadingModel *model);
|
void setReadingModel(ReadingModel *model);
|
||||||
void setBookmarkModel(BookmarkModel *model);
|
|
||||||
|
|
||||||
Q_INVOKABLE void openBook(const QUrl &fileUrl);
|
Q_INVOKABLE void openBook(const QUrl &fileUrl);
|
||||||
|
Q_INVOKABLE void browseAndOpen();
|
||||||
Q_INVOKABLE void closeBook();
|
Q_INVOKABLE void closeBook();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
@@ -44,14 +43,23 @@ signals:
|
|||||||
void loadingChanged();
|
void loadingChanged();
|
||||||
void errorMessageChanged();
|
void errorMessageChanged();
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
#ifdef HAS_QT_DBUS
|
||||||
|
void handlePortalResponse(uint response, const QVariantMap &results);
|
||||||
|
#endif
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void browseAndOpenFallback();
|
||||||
|
|
||||||
std::unique_ptr<FormatReader> m_reader;
|
std::unique_ptr<FormatReader> m_reader;
|
||||||
ReadingModel *m_readingModel = nullptr;
|
ReadingModel *m_readingModel = nullptr;
|
||||||
BookmarkModel *m_bookmarkModel = nullptr;
|
|
||||||
QString m_bookTitle;
|
QString m_bookTitle;
|
||||||
QString m_bookAuthor;
|
QString m_bookAuthor;
|
||||||
bool m_loading = false;
|
bool m_loading = false;
|
||||||
QString m_errorMessage;
|
QString m_errorMessage;
|
||||||
|
#ifdef HAS_QT_DBUS
|
||||||
|
QString m_portalHandlePath;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // BOOKCONTROLLER_H
|
#endif // BOOKCONTROLLER_H
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
#include "BookmarkModel.h"
|
|
||||||
#include "BookmarkStore.h"
|
|
||||||
|
|
||||||
BookmarkModel::BookmarkModel(QObject *parent)
|
|
||||||
: QObject(parent)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList BookmarkModel::bookmarks() const
|
|
||||||
{
|
|
||||||
return m_bookmarks;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookmarkModel::setBookId(const QString &bookId)
|
|
||||||
{
|
|
||||||
m_bookId = bookId;
|
|
||||||
m_bookmarks.clear();
|
|
||||||
if (m_store) {
|
|
||||||
m_bookmarks = m_store->bookmarksForBook(m_bookId);
|
|
||||||
}
|
|
||||||
emit bookmarksChanged();
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookmarkModel::setStore(BookmarkStore *store)
|
|
||||||
{
|
|
||||||
m_store = store;
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookmarkModel::addBookmark(const QString &bookmark)
|
|
||||||
{
|
|
||||||
if (m_store) {
|
|
||||||
m_store->addBookmark(m_bookId, bookmark);
|
|
||||||
m_bookmarks = m_store->bookmarksForBook(m_bookId);
|
|
||||||
emit bookmarksChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookmarkModel::removeBookmark(const QString &bookmark)
|
|
||||||
{
|
|
||||||
if (m_store) {
|
|
||||||
m_store->removeBookmark(m_bookId, bookmark);
|
|
||||||
m_bookmarks = m_store->bookmarksForBook(m_bookId);
|
|
||||||
emit bookmarksChanged();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
#ifndef BOOKMARKMODEL_H
|
|
||||||
#define BOOKMARKMODEL_H
|
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QStringList>
|
|
||||||
|
|
||||||
class BookmarkStore;
|
|
||||||
|
|
||||||
class BookmarkModel : public QObject {
|
|
||||||
Q_OBJECT
|
|
||||||
Q_PROPERTY(QStringList bookmarks READ bookmarks NOTIFY bookmarksChanged)
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit BookmarkModel(QObject *parent = nullptr);
|
|
||||||
|
|
||||||
QStringList bookmarks() const;
|
|
||||||
|
|
||||||
void setBookId(const QString &bookId);
|
|
||||||
void setStore(BookmarkStore *store);
|
|
||||||
|
|
||||||
Q_INVOKABLE void addBookmark(const QString &bookmark);
|
|
||||||
Q_INVOKABLE void removeBookmark(const QString &bookmark);
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void bookmarksChanged();
|
|
||||||
|
|
||||||
private:
|
|
||||||
BookmarkStore *m_store = nullptr;
|
|
||||||
QString m_bookId;
|
|
||||||
QStringList m_bookmarks;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // BOOKMARKMODEL_H
|
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#include "BookmarkStore.h"
|
|
||||||
|
|
||||||
BookmarkStore::BookmarkStore(QObject *parent)
|
|
||||||
: QObject(parent)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
QStringList BookmarkStore::bookmarksForBook(const QString &bookId) const
|
|
||||||
{
|
|
||||||
Q_UNUSED(bookId)
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookmarkStore::addBookmark(const QString &bookId, const QString &bookmark)
|
|
||||||
{
|
|
||||||
Q_UNUSED(bookId)
|
|
||||||
Q_UNUSED(bookmark)
|
|
||||||
}
|
|
||||||
|
|
||||||
void BookmarkStore::removeBookmark(const QString &bookId, const QString &bookmark)
|
|
||||||
{
|
|
||||||
Q_UNUSED(bookId)
|
|
||||||
Q_UNUSED(bookmark)
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
#ifndef BOOKMARKSTORE_H
|
|
||||||
#define BOOKMARKSTORE_H
|
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QStringList>
|
|
||||||
|
|
||||||
class BookmarkStore : public QObject {
|
|
||||||
Q_OBJECT
|
|
||||||
|
|
||||||
public:
|
|
||||||
explicit BookmarkStore(QObject *parent = nullptr);
|
|
||||||
|
|
||||||
QStringList bookmarksForBook(const QString &bookId) const;
|
|
||||||
void addBookmark(const QString &bookId, const QString &bookmark);
|
|
||||||
void removeBookmark(const QString &bookId, const QString &bookmark);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // BOOKMARKSTORE_H
|
|
||||||
@@ -1,8 +1,15 @@
|
|||||||
#include "ReaderFactory.h"
|
#include "ReaderFactory.h"
|
||||||
#include "FormatReader.h"
|
#include "FormatReader.h"
|
||||||
|
#include "formats/TxtReader.h"
|
||||||
|
|
||||||
|
#include <QFileInfo>
|
||||||
|
|
||||||
std::unique_ptr<FormatReader> ReaderFactory::create(const QString &filePath)
|
std::unique_ptr<FormatReader> ReaderFactory::create(const QString &filePath)
|
||||||
{
|
{
|
||||||
Q_UNUSED(filePath)
|
const QString ext = QFileInfo(filePath).suffix().toLower();
|
||||||
|
|
||||||
|
if (ext == QLatin1String("txt"))
|
||||||
|
return std::make_unique<TxtReader>();
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,119 @@
|
|||||||
|
#include "TxtReader.h"
|
||||||
|
|
||||||
|
#include <QFile>
|
||||||
|
#include <QFileInfo>
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QTextStream>
|
||||||
|
|
||||||
|
#include <uchardet/uchardet.h>
|
||||||
|
#include <iconv.h>
|
||||||
|
|
||||||
|
static QString decodeWithIconv(const QByteArray &raw, const char *charset)
|
||||||
|
{
|
||||||
|
iconv_t cd = iconv_open("UTF-8", charset);
|
||||||
|
if (cd == reinterpret_cast<iconv_t>(-1))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// Worst case: each byte becomes 4 UTF-8 bytes
|
||||||
|
QByteArray out(raw.size() * 4, '\0');
|
||||||
|
char *inBuf = const_cast<char *>(raw.constData());
|
||||||
|
size_t inLeft = raw.size();
|
||||||
|
char *outBuf = out.data();
|
||||||
|
size_t outLeft = out.size();
|
||||||
|
|
||||||
|
size_t result = iconv(cd, &inBuf, &inLeft, &outBuf, &outLeft);
|
||||||
|
iconv_close(cd);
|
||||||
|
|
||||||
|
if (result == static_cast<size_t>(-1))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
out.resize(out.size() - outLeft);
|
||||||
|
return QString::fromUtf8(out);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TxtReader::open(const QString &filePath)
|
||||||
|
{
|
||||||
|
QFile file(filePath);
|
||||||
|
if (!file.exists()) {
|
||||||
|
m_error = QStringLiteral("File not found: ") + filePath;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!file.open(QIODevice::ReadOnly)) {
|
||||||
|
m_error = QStringLiteral("Cannot open file: ") + file.errorString();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray raw = file.readAll();
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
uchardet_t ud = uchardet_new();
|
||||||
|
if (uchardet_handle_data(ud, raw.constData(), raw.size()) == 0) {
|
||||||
|
uchardet_data_end(ud);
|
||||||
|
const char *charset = uchardet_get_charset(ud);
|
||||||
|
if (charset && charset[0] != '\0') {
|
||||||
|
auto qtEncoding = QStringConverter::encodingForName(charset);
|
||||||
|
if (qtEncoding) {
|
||||||
|
QTextStream stream(raw);
|
||||||
|
stream.setEncoding(qtEncoding.value());
|
||||||
|
m_content = stream.readAll();
|
||||||
|
} else {
|
||||||
|
m_content = decodeWithIconv(raw, charset);
|
||||||
|
}
|
||||||
|
uchardet_delete(ud);
|
||||||
|
if (!m_content.isEmpty()) {
|
||||||
|
m_title = QFileInfo(filePath).completeBaseName();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
uchardet_delete(ud);
|
||||||
|
|
||||||
|
// Default: UTF-8
|
||||||
|
m_content = QString::fromUtf8(raw);
|
||||||
|
m_title = QFileInfo(filePath).completeBaseName();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString TxtReader::lastError()
|
||||||
|
{
|
||||||
|
return m_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TxtReader::chapterCount()
|
||||||
|
{
|
||||||
|
return m_content.isEmpty() ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Chapter TxtReader::loadChapter(int index)
|
||||||
|
{
|
||||||
|
if (index != 0 || m_content.isEmpty())
|
||||||
|
return Chapter{index, {}, {}};
|
||||||
|
|
||||||
|
// Split on blank lines to create paragraphs; single newlines become <br>
|
||||||
|
QString html;
|
||||||
|
const QStringList blocks = m_content.split(QRegularExpression("\\n\\n+"));
|
||||||
|
for (const QString &block : blocks) {
|
||||||
|
QString escaped = block.toHtmlEscaped().trimmed();
|
||||||
|
if (escaped.isEmpty())
|
||||||
|
continue;
|
||||||
|
escaped.replace('\n', QStringLiteral("<br>\n"));
|
||||||
|
html += QStringLiteral("<p>") + escaped + QStringLiteral("</p>\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Chapter{0, m_title, html};
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<TocEntry> TxtReader::tableOfContents()
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
QString TxtReader::title()
|
||||||
|
{
|
||||||
|
return m_title;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString TxtReader::author()
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#ifndef TXTREADER_H
|
||||||
|
#define TXTREADER_H
|
||||||
|
|
||||||
|
#include "backend/FormatReader.h"
|
||||||
|
|
||||||
|
class TxtReader : public FormatReader {
|
||||||
|
public:
|
||||||
|
bool open(const QString &filePath) override;
|
||||||
|
QString lastError() override;
|
||||||
|
int chapterCount() override;
|
||||||
|
Chapter loadChapter(int index) override;
|
||||||
|
QList<TocEntry> tableOfContents() override;
|
||||||
|
QString title() override;
|
||||||
|
QString author() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString m_content;
|
||||||
|
QString m_title;
|
||||||
|
QString m_error;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TXTREADER_H
|
||||||
+2
-9
@@ -1,27 +1,21 @@
|
|||||||
#include <QGuiApplication>
|
#include <QApplication>
|
||||||
#include <QQmlApplicationEngine>
|
#include <QQmlApplicationEngine>
|
||||||
#include <QQmlContext>
|
#include <QQmlContext>
|
||||||
|
|
||||||
#include "backend/BookController.h"
|
#include "backend/BookController.h"
|
||||||
#include "backend/ReadingModel.h"
|
#include "backend/ReadingModel.h"
|
||||||
#include "backend/BookmarkModel.h"
|
|
||||||
#include "backend/BookmarkStore.h"
|
|
||||||
#include "backend/AppState.h"
|
#include "backend/AppState.h"
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
QGuiApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
app.setApplicationName(QStringLiteral("Asteria"));
|
app.setApplicationName(QStringLiteral("Asteria"));
|
||||||
app.setOrganizationName(QStringLiteral("Asteria"));
|
app.setOrganizationName(QStringLiteral("Asteria"));
|
||||||
|
|
||||||
BookmarkStore bookmarkStore;
|
|
||||||
ReadingModel readingModel;
|
ReadingModel readingModel;
|
||||||
BookmarkModel bookmarkModel;
|
|
||||||
bookmarkModel.setStore(&bookmarkStore);
|
|
||||||
|
|
||||||
BookController bookController;
|
BookController bookController;
|
||||||
bookController.setReadingModel(&readingModel);
|
bookController.setReadingModel(&readingModel);
|
||||||
bookController.setBookmarkModel(&bookmarkModel);
|
|
||||||
|
|
||||||
AppState appState;
|
AppState appState;
|
||||||
|
|
||||||
@@ -29,7 +23,6 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("bookController"), &bookController);
|
engine.rootContext()->setContextProperty(QStringLiteral("bookController"), &bookController);
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
|
engine.rootContext()->setContextProperty(QStringLiteral("readingModel"), &readingModel);
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("bookmarkModel"), &bookmarkModel);
|
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("appState"), &appState);
|
engine.rootContext()->setContextProperty(QStringLiteral("appState"), &appState);
|
||||||
|
|
||||||
const QUrl url(QStringLiteral("qrc:/Asteria/Main.qml"));
|
const QUrl url(QStringLiteral("qrc:/Asteria/Main.qml"));
|
||||||
|
|||||||
+1
-9
@@ -1,6 +1,5 @@
|
|||||||
import QtQuick
|
import QtQuick
|
||||||
import QtQuick.Controls
|
import QtQuick.Controls
|
||||||
import QtQuick.Dialogs
|
|
||||||
import Asteria
|
import Asteria
|
||||||
|
|
||||||
ApplicationWindow {
|
ApplicationWindow {
|
||||||
@@ -32,7 +31,7 @@ ApplicationWindow {
|
|||||||
window.sidebarOpen = false
|
window.sidebarOpen = false
|
||||||
}
|
}
|
||||||
onOpenFile: {
|
onOpenFile: {
|
||||||
fileDialog.open()
|
bookController.browseAndOpen()
|
||||||
window.sidebarOpen = false
|
window.sidebarOpen = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -91,13 +90,6 @@ ApplicationWindow {
|
|||||||
z: 3000
|
z: 3000
|
||||||
}
|
}
|
||||||
|
|
||||||
FileDialog {
|
|
||||||
id: fileDialog
|
|
||||||
title: "Open Book"
|
|
||||||
nameFilters: ["Book files (*.txt *.fb2 *.epub *.pdf *.doc *.rtf *.html *.htm *.docx *.djvu *.odt *.chm)", "All files (*)"]
|
|
||||||
onAccepted: bookController.openBook(selectedFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
Connections {
|
Connections {
|
||||||
target: bookController
|
target: bookController
|
||||||
function onBookReady() {
|
function onBookReady() {
|
||||||
|
|||||||
@@ -22,4 +22,17 @@ Rectangle {
|
|||||||
font.bold: true
|
font.bold: true
|
||||||
color: Theme.dimText
|
color: Theme.dimText
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Text {
|
||||||
|
anchors.right: parent.right
|
||||||
|
anchors.rightMargin: 16
|
||||||
|
anchors.verticalCenter: parent.verticalCenter
|
||||||
|
visible: bookController.hasBook
|
||||||
|
text: bookController.hasBook
|
||||||
|
? (readingModel.currentChapter + 1) + "/" + readingModel.totalChapters
|
||||||
|
: ""
|
||||||
|
font.pixelSize: 11
|
||||||
|
font.bold: true
|
||||||
|
color: Theme.counterText
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -71,15 +71,5 @@ Rectangle {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Text {
|
|
||||||
anchors.verticalCenter: parent.verticalCenter
|
|
||||||
font.pixelSize: 12
|
|
||||||
font.bold: true
|
|
||||||
color: Theme.counterText
|
|
||||||
visible: header.currentPage === "reader" && bookController.hasBook
|
|
||||||
text: bookController.hasBook
|
|
||||||
? (readingModel.currentChapter + 1) + "/" + readingModel.totalChapters
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -10,12 +10,16 @@ Rectangle {
|
|||||||
|
|
||||||
Flickable {
|
Flickable {
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
anchors.margins: 12
|
anchors.topMargin: 0
|
||||||
|
anchors.bottomMargin: 0
|
||||||
|
anchors.leftMargin: 12
|
||||||
|
anchors.rightMargin: 0
|
||||||
contentHeight: contentText.implicitHeight
|
contentHeight: contentText.implicitHeight
|
||||||
clip: true
|
clip: true
|
||||||
|
|
||||||
ScrollBar.vertical: ScrollBar {
|
ScrollBar.vertical: ScrollBar {
|
||||||
policy: ScrollBar.AsNeeded
|
policy: ScrollBar.AsNeeded
|
||||||
|
padding: 0
|
||||||
contentItem: Rectangle {
|
contentItem: Rectangle {
|
||||||
implicitWidth: 18
|
implicitWidth: 18
|
||||||
radius: 3
|
radius: 3
|
||||||
@@ -30,6 +34,7 @@ Rectangle {
|
|||||||
Text {
|
Text {
|
||||||
id: contentText
|
id: contentText
|
||||||
width: parent.width
|
width: parent.width
|
||||||
|
rightPadding: 20
|
||||||
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
|
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
|
||||||
textFormat: Text.RichText
|
textFormat: Text.RichText
|
||||||
font.pixelSize: 16
|
font.pixelSize: 16
|
||||||
|
|||||||
@@ -0,0 +1,94 @@
|
|||||||
|
#include <QtTest>
|
||||||
|
#include <QSignalSpy>
|
||||||
|
#include <QUrl>
|
||||||
|
#include "backend/BookController.h"
|
||||||
|
#include "backend/ReadingModel.h"
|
||||||
|
|
||||||
|
class BookControllerTest : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString fixturePath(const QString &filename) const
|
||||||
|
{
|
||||||
|
return QStringLiteral(FIXTURES_DIR) + "/" + filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void openValidBook()
|
||||||
|
{
|
||||||
|
BookController ctrl;
|
||||||
|
QSignalSpy readySpy(&ctrl, &BookController::bookReady);
|
||||||
|
|
||||||
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
||||||
|
|
||||||
|
QCOMPARE(readySpy.count(), 1);
|
||||||
|
QVERIFY(ctrl.hasBook());
|
||||||
|
QCOMPARE(ctrl.bookTitle(), QStringLiteral("sample"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void openInvalidPath()
|
||||||
|
{
|
||||||
|
BookController ctrl;
|
||||||
|
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
|
||||||
|
|
||||||
|
ctrl.openBook(QUrl::fromLocalFile("/tmp/nonexistent_asteria_file.txt"));
|
||||||
|
|
||||||
|
QCOMPARE(errorSpy.count(), 1);
|
||||||
|
QVERIFY(!ctrl.errorMessage().isEmpty());
|
||||||
|
QVERIFY(!ctrl.hasBook());
|
||||||
|
}
|
||||||
|
|
||||||
|
void openUnsupportedFormat()
|
||||||
|
{
|
||||||
|
BookController ctrl;
|
||||||
|
QSignalSpy errorSpy(&ctrl, &BookController::bookError);
|
||||||
|
|
||||||
|
ctrl.openBook(QUrl::fromLocalFile("/tmp/file.xyz"));
|
||||||
|
|
||||||
|
QCOMPARE(errorSpy.count(), 1);
|
||||||
|
QCOMPARE(ctrl.errorMessage(), QStringLiteral("No format readers available"));
|
||||||
|
QVERIFY(!ctrl.hasBook());
|
||||||
|
}
|
||||||
|
|
||||||
|
void closeBook()
|
||||||
|
{
|
||||||
|
BookController ctrl;
|
||||||
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
||||||
|
QVERIFY(ctrl.hasBook());
|
||||||
|
|
||||||
|
QSignalSpy changedSpy(&ctrl, &BookController::bookChanged);
|
||||||
|
ctrl.closeBook();
|
||||||
|
|
||||||
|
QVERIFY(!ctrl.hasBook());
|
||||||
|
QVERIFY(ctrl.bookTitle().isEmpty());
|
||||||
|
QVERIFY(ctrl.bookAuthor().isEmpty());
|
||||||
|
QCOMPARE(changedSpy.count(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void openBookSetsReadingModel()
|
||||||
|
{
|
||||||
|
BookController ctrl;
|
||||||
|
ReadingModel model;
|
||||||
|
ctrl.setReadingModel(&model);
|
||||||
|
|
||||||
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
||||||
|
|
||||||
|
QVERIFY(model.totalChapters() > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadingTransitions()
|
||||||
|
{
|
||||||
|
BookController ctrl;
|
||||||
|
QSignalSpy loadingSpy(&ctrl, &BookController::loadingChanged);
|
||||||
|
|
||||||
|
ctrl.openBook(QUrl::fromLocalFile(fixturePath("sample.txt")));
|
||||||
|
|
||||||
|
// loading goes true then false → 2 emissions
|
||||||
|
QCOMPARE(loadingSpy.count(), 2);
|
||||||
|
// After completion, loading should be false
|
||||||
|
QVERIFY(!ctrl.loading());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(BookControllerTest)
|
||||||
|
#include "BookControllerTest.moc"
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
#include <QtTest>
|
||||||
|
#include "backend/ReaderFactory.h"
|
||||||
|
#include "backend/FormatReader.h"
|
||||||
|
|
||||||
|
class ReaderFactoryTest : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void createTxtReader()
|
||||||
|
{
|
||||||
|
auto reader = ReaderFactory::create("file.txt");
|
||||||
|
QVERIFY(reader != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void createTxtReaderUpperCase()
|
||||||
|
{
|
||||||
|
auto reader = ReaderFactory::create("file.TXT");
|
||||||
|
QVERIFY(reader != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void createUnknownFormat()
|
||||||
|
{
|
||||||
|
auto reader = ReaderFactory::create("file.xyz");
|
||||||
|
QVERIFY(reader == nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void createNoExtension()
|
||||||
|
{
|
||||||
|
auto reader = ReaderFactory::create("noext");
|
||||||
|
QVERIFY(reader == nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
void createEmptyPath()
|
||||||
|
{
|
||||||
|
auto reader = ReaderFactory::create("");
|
||||||
|
QVERIFY(reader == nullptr);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(ReaderFactoryTest)
|
||||||
|
#include "ReaderFactoryTest.moc"
|
||||||
@@ -0,0 +1,163 @@
|
|||||||
|
#include <QtTest>
|
||||||
|
#include <QSignalSpy>
|
||||||
|
#include "backend/ReadingModel.h"
|
||||||
|
#include "backend/FormatReader.h"
|
||||||
|
|
||||||
|
class FakeReader : public FormatReader {
|
||||||
|
public:
|
||||||
|
bool open(const QString &) override { return true; }
|
||||||
|
QString lastError() override { return {}; }
|
||||||
|
int chapterCount() override { return 3; }
|
||||||
|
|
||||||
|
Chapter loadChapter(int index) override
|
||||||
|
{
|
||||||
|
if (index < 0 || index >= 3)
|
||||||
|
return {index, {}, {}};
|
||||||
|
return {index,
|
||||||
|
QStringLiteral("Chapter %1").arg(index),
|
||||||
|
QStringLiteral("<p>Content %1</p>").arg(index)};
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<TocEntry> tableOfContents() override { return {}; }
|
||||||
|
QString title() override { return QStringLiteral("Fake Book"); }
|
||||||
|
QString author() override { return QStringLiteral("Fake Author"); }
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReadingModelTest : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void initialState()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
QCOMPARE(model.totalChapters(), 0);
|
||||||
|
QCOMPARE(model.progress(), 0.0);
|
||||||
|
QVERIFY(model.chapterContent().isEmpty());
|
||||||
|
QVERIFY(model.chapterTitle().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void setReaderLoadsFirstChapter()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
|
||||||
|
QCOMPARE(model.currentChapter(), 0);
|
||||||
|
QCOMPARE(model.totalChapters(), 3);
|
||||||
|
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
|
||||||
|
QCOMPARE(model.chapterContent(), QStringLiteral("<p>Content 0</p>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void nextChapter()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
|
||||||
|
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
|
||||||
|
model.nextChapter();
|
||||||
|
|
||||||
|
QCOMPARE(model.currentChapter(), 1);
|
||||||
|
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 1"));
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void nextChapterAtEnd()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
model.goToChapter(2);
|
||||||
|
|
||||||
|
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
|
||||||
|
model.nextChapter();
|
||||||
|
|
||||||
|
QCOMPARE(model.currentChapter(), 2);
|
||||||
|
QCOMPARE(spy.count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void previousChapter()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
model.goToChapter(1);
|
||||||
|
|
||||||
|
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
|
||||||
|
model.previousChapter();
|
||||||
|
|
||||||
|
QCOMPARE(model.currentChapter(), 0);
|
||||||
|
QCOMPARE(model.chapterTitle(), QStringLiteral("Chapter 0"));
|
||||||
|
QCOMPARE(spy.count(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void previousChapterAtStart()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
|
||||||
|
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
|
||||||
|
model.previousChapter();
|
||||||
|
|
||||||
|
QCOMPARE(model.currentChapter(), 0);
|
||||||
|
QCOMPARE(spy.count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void goToChapter()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
|
||||||
|
model.goToChapter(2);
|
||||||
|
QCOMPARE(model.currentChapter(), 2);
|
||||||
|
QCOMPARE(model.chapterContent(), QStringLiteral("<p>Content 2</p>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void goToChapterOutOfRange()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
|
||||||
|
QSignalSpy spy(&model, &ReadingModel::chapterChanged);
|
||||||
|
model.goToChapter(-1);
|
||||||
|
QCOMPARE(model.currentChapter(), 0);
|
||||||
|
QCOMPARE(spy.count(), 0);
|
||||||
|
|
||||||
|
model.goToChapter(99);
|
||||||
|
QCOMPARE(model.currentChapter(), 0);
|
||||||
|
QCOMPARE(spy.count(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void progressCalculation()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
|
||||||
|
// Chapter 0 of 3 → 1/3
|
||||||
|
QCOMPARE(model.progress(), 1.0 / 3.0);
|
||||||
|
|
||||||
|
model.goToChapter(2);
|
||||||
|
// Chapter 2 of 3 → 3/3 = 1.0
|
||||||
|
QCOMPARE(model.progress(), 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setReaderNull()
|
||||||
|
{
|
||||||
|
ReadingModel model;
|
||||||
|
FakeReader reader;
|
||||||
|
model.setReader(&reader);
|
||||||
|
model.setReader(nullptr);
|
||||||
|
|
||||||
|
QCOMPARE(model.totalChapters(), 0);
|
||||||
|
QCOMPARE(model.progress(), 0.0);
|
||||||
|
QVERIFY(model.chapterContent().isEmpty());
|
||||||
|
QVERIFY(model.chapterTitle().isEmpty());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(ReadingModelTest)
|
||||||
|
#include "ReadingModelTest.moc"
|
||||||
Vendored
+13
@@ -0,0 +1,13 @@
|
|||||||
|
The Art of Reading
|
||||||
|
|
||||||
|
Books are a uniquely portable magic. They transport us to worlds unknown,
|
||||||
|
introduce us to people we would never otherwise meet, and allow us to
|
||||||
|
experience lives vastly different from our own.
|
||||||
|
|
||||||
|
Reading is not just a pastime — it is a journey of the mind.
|
||||||
|
|
||||||
|
Искусство чтения
|
||||||
|
|
||||||
|
Книги — это уникальная портативная магия. Они переносят нас в неизведанные
|
||||||
|
миры, знакомят с людьми, которых мы никогда бы не встретили, и позволяют
|
||||||
|
пережить жизни, совершенно отличные от нашей собственной.
|
||||||
Vendored
+2
@@ -0,0 +1,2 @@
|
|||||||
|
Привет, мир! Это тестовый файл в кодировке Windows-1251.
|
||||||
|
Русская литература — великое наследие.
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
#include <QtTest>
|
||||||
|
#include "backend/formats/TxtReader.h"
|
||||||
|
|
||||||
|
class TxtReaderTest : public QObject {
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString fixturePath(const QString &filename) const
|
||||||
|
{
|
||||||
|
return QStringLiteral(FIXTURES_DIR) + "/" + filename;
|
||||||
|
}
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void openValidFile()
|
||||||
|
{
|
||||||
|
TxtReader reader;
|
||||||
|
QVERIFY(reader.open(fixturePath("sample.txt")));
|
||||||
|
QCOMPARE(reader.chapterCount(), 1);
|
||||||
|
QCOMPARE(reader.title(), QStringLiteral("sample"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadChapter()
|
||||||
|
{
|
||||||
|
TxtReader reader;
|
||||||
|
reader.open(fixturePath("sample.txt"));
|
||||||
|
|
||||||
|
Chapter ch = reader.loadChapter(0);
|
||||||
|
QCOMPARE(ch.index, 0);
|
||||||
|
QVERIFY(!ch.contentHtml.isEmpty());
|
||||||
|
QVERIFY(ch.contentHtml.contains("Books are a uniquely portable magic"));
|
||||||
|
// Verify Cyrillic content survived
|
||||||
|
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Искусство чтения")));
|
||||||
|
// Verify HTML paragraph structure
|
||||||
|
QVERIFY(ch.contentHtml.contains("<p>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
void tableOfContentsEmpty()
|
||||||
|
{
|
||||||
|
TxtReader reader;
|
||||||
|
reader.open(fixturePath("sample.txt"));
|
||||||
|
QVERIFY(reader.tableOfContents().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void authorEmpty()
|
||||||
|
{
|
||||||
|
TxtReader reader;
|
||||||
|
reader.open(fixturePath("sample.txt"));
|
||||||
|
QVERIFY(reader.author().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void openNonexistentFile()
|
||||||
|
{
|
||||||
|
TxtReader reader;
|
||||||
|
QVERIFY(!reader.open("/tmp/does_not_exist_asteria.txt"));
|
||||||
|
QVERIFY(!reader.lastError().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void loadChapterOutOfRange()
|
||||||
|
{
|
||||||
|
TxtReader reader;
|
||||||
|
reader.open(fixturePath("sample.txt"));
|
||||||
|
|
||||||
|
Chapter ch = reader.loadChapter(1);
|
||||||
|
QVERIFY(ch.contentHtml.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
void openCp1251File()
|
||||||
|
{
|
||||||
|
TxtReader reader;
|
||||||
|
QVERIFY(reader.open(fixturePath("sample_cp1251.txt")));
|
||||||
|
QCOMPARE(reader.chapterCount(), 1);
|
||||||
|
|
||||||
|
Chapter ch = reader.loadChapter(0);
|
||||||
|
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Привет, мир!")));
|
||||||
|
QVERIFY(ch.contentHtml.contains(QString::fromUtf8("Русская литература")));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
QTEST_MAIN(TxtReaderTest)
|
||||||
|
#include "TxtReaderTest.moc"
|
||||||
Reference in new issue
Block a user