From 945b24a8f8454088a8004aa13ac8d5e9b6fe53d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Thu, 27 May 2021 18:48:52 +0200 Subject: [PATCH] Extract opening a comic from YACReaderLibrary to its own file And send a new param `--readingListId` to tell YACReader that the comic is opened from a reading list. --- YACReaderLibrary/YACReaderLibrary.pro | 2 + YACReaderLibrary/db/comic_model.cpp | 2 + YACReaderLibrary/db/comic_model.h | 1 + YACReaderLibrary/library_comic_opener.cpp | 47 ++++++++++++++++++++++ YACReaderLibrary/library_comic_opener.h | 18 +++++++++ YACReaderLibrary/library_window.cpp | 48 ++++++++--------------- YACReaderLibrary/library_window.h | 1 - 7 files changed, 86 insertions(+), 33 deletions(-) create mode 100644 YACReaderLibrary/library_comic_opener.cpp create mode 100644 YACReaderLibrary/library_comic_opener.h diff --git a/YACReaderLibrary/YACReaderLibrary.pro b/YACReaderLibrary/YACReaderLibrary.pro index 9a0e4bf2..ee810eca 100644 --- a/YACReaderLibrary/YACReaderLibrary.pro +++ b/YACReaderLibrary/YACReaderLibrary.pro @@ -78,6 +78,7 @@ HEADERS += comic_flow.h \ db/comic_query_result_processor.h \ db/folder_query_result_processor.h \ db/query_lexer.h \ + library_comic_opener.h \ library_creator.h \ library_window.h \ add_library_dialog.h \ @@ -156,6 +157,7 @@ SOURCES += comic_flow.cpp \ db/comic_query_result_processor.cpp \ db/folder_query_result_processor.cpp \ db/query_lexer.cpp \ + library_comic_opener.cpp \ library_creator.cpp \ library_window.cpp \ main.cpp \ diff --git a/YACReaderLibrary/db/comic_model.cpp b/YACReaderLibrary/db/comic_model.cpp index 842922be..0a925ff6 100644 --- a/YACReaderLibrary/db/comic_model.cpp +++ b/YACReaderLibrary/db/comic_model.cpp @@ -546,6 +546,7 @@ void ComicModel::setupFavoritesModelData(const QString &databasePath) { enableResorting = true; mode = Favorites; + sourceId = -1; beginResetModel(); qDeleteAll(_data); @@ -574,6 +575,7 @@ void ComicModel::setupReadingModelData(const QString &databasePath) { enableResorting = false; mode = Reading; + sourceId = -1; beginResetModel(); qDeleteAll(_data); diff --git a/YACReaderLibrary/db/comic_model.h b/YACReaderLibrary/db/comic_model.h index cbe49d45..55063f3e 100644 --- a/YACReaderLibrary/db/comic_model.h +++ b/YACReaderLibrary/db/comic_model.h @@ -129,6 +129,7 @@ public: bool isFavorite(const QModelIndex &index); ComicModel::Mode getMode() { return mode; } + unsigned long long int getSourceId() { return sourceId; } QHash roleNames() const override; diff --git a/YACReaderLibrary/library_comic_opener.cpp b/YACReaderLibrary/library_comic_opener.cpp new file mode 100644 index 00000000..36ebf747 --- /dev/null +++ b/YACReaderLibrary/library_comic_opener.cpp @@ -0,0 +1,47 @@ +#include "library_comic_opener.h" + +#include "comic_db.h" + +#include +#include + +bool YACReader::openComic(const ComicDB &comic, + unsigned long long libraryId, + const QString &path, + OpenComicSource source) +{ + bool yacreaderFound = false; + + QString labelParam; + + if (source.source == OpenComicSource::ReadingList) { + labelParam = QString("--readingListId=%1").arg(source.sourceId); + } + +#ifdef Q_OS_MACOS + QStringList possiblePaths { QDir::cleanPath(QCoreApplication::applicationDirPath() + "/../../../") }; + possiblePaths += QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation); + + for (auto &&ypath : possiblePaths) { + QString yacreaderPath = QDir::cleanPath(ypath + "/YACReader.app"); + if (QFileInfo(yacreaderPath).exists()) { + yacreaderFound = true; + QStringList parameters { "-n", yacreaderPath, "--args", path, QString("--comicId=%1").arg(comic.id), QString("--libraryId=%1").arg(libraryId), labelParam }; + QProcess::startDetached("open", parameters); + break; + } + } +#endif + +#ifdef Q_OS_WIN + QStringList parameters { path, QString("--comicId=%1").arg(comic.id), QString("--libraryId=%1").arg(libraryId), labelParam }; + yacreaderFound = QProcess::startDetached(QDir::cleanPath(QCoreApplication::applicationDirPath() + "/YACReader.exe"), parameters); +#endif + +#if defined Q_OS_UNIX && !defined Q_OS_MAC + QStringList parameters { path, QString("--comicId=%1").arg(comic.id), QString("--libraryId=%1").arg(libraryId), labelParam }; + yacreaderFound = QProcess::startDetached(QStringLiteral("YACReader"), parameters); +#endif + + return yacreaderFound; +} diff --git a/YACReaderLibrary/library_comic_opener.h b/YACReaderLibrary/library_comic_opener.h new file mode 100644 index 00000000..b68cb225 --- /dev/null +++ b/YACReaderLibrary/library_comic_opener.h @@ -0,0 +1,18 @@ +#ifndef LIBRARYCOMICOPENER_H +#define LIBRARYCOMICOPENER_H + +#include "yacreader_global.h" + +class ComicDB; +class QString; + +namespace YACReader { + +bool openComic(const ComicDB &comic, + unsigned long long libraryId, + const QString &path, + OpenComicSource source); + +} + +#endif // LIBRARYCOMICOPENER_H diff --git a/YACReaderLibrary/library_window.cpp b/YACReaderLibrary/library_window.cpp index 1b7824d1..e2c17ca2 100644 --- a/YACReaderLibrary/library_window.cpp +++ b/YACReaderLibrary/library_window.cpp @@ -86,6 +86,8 @@ #include "whats_new_controller.h" +#include "library_comic_opener.h" + #include "QsLog.h" #ifdef Q_OS_WIN @@ -1825,37 +1827,27 @@ void LibraryWindow::checkEmptyFolder() } } -void LibraryWindow::openComic(const ComicDB &comic) +void LibraryWindow::openComic() { if (!importedCovers) { - //TODO generate IDS for libraries... - quint64 libraryId = libraries.getId(selectedLibrary->currentText()); - bool yacreaderFound = false; + auto libraryId = libraries.getId(selectedLibrary->currentText()); -#ifdef Q_OS_MACOS - QStringList possiblePaths { QDir::cleanPath(QCoreApplication::applicationDirPath() + "/../../../") }; - possiblePaths += QStandardPaths::standardLocations(QStandardPaths::ApplicationsLocation); + auto comic = comicsModel->getComic(comicsViewsManager->comicsView->currentIndex()); + auto mode = comicsModel->getMode(); - for (auto &&ypath : possiblePaths) { - QString yacreaderPath = QDir::cleanPath(ypath + "/YACReader.app"); - if (QFileInfo(yacreaderPath).exists()) { - yacreaderFound = true; - QStringList parameters { "-n", yacreaderPath, "--args", currentPath(), QString("--comicId=%1").arg(comic.id), QString("--libraryId=%1").arg(libraryId) }; - QProcess::startDetached("open", parameters); - break; - } + OpenComicSource::Source source; + + if (mode == ComicModel::ReadingList) { + source = OpenComicSource::Source::ReadingList; + } else if (mode == ComicModel::Reading) { + //TODO check where the comic was opened from the last time it was read + source = OpenComicSource::Source::Folder; + } else { + source = OpenComicSource::Source::Folder; } -#endif -#ifdef Q_OS_WIN - QStringList parameters { currentPath(), QString("--comicId=%1").arg(comic.id), QString("--libraryId=%1").arg(libraryId) }; - yacreaderFound = QProcess::startDetached(QDir::cleanPath(QCoreApplication::applicationDirPath() + "/YACReader.exe"), parameters); -#endif + auto yacreaderFound = YACReader::openComic(comic, libraryId, currentPath(), OpenComicSource { source, comicsModel->getSourceId() }); -#if defined Q_OS_UNIX && !defined Q_OS_MAC - QStringList parameters { currentPath(), QString("--comicId=%1").arg(comic.id), QString("--libraryId=%1").arg(libraryId) }; - yacreaderFound = QProcess::startDetached(QStringLiteral("YACReader"), parameters); -#endif if (!yacreaderFound) { #ifdef Q_OS_WIN QMessageBox::critical(this, tr("YACReader not found"), tr("YACReader not found. YACReader should be installed in the same folder as YACReaderLibrary.")); @@ -1866,14 +1858,6 @@ void LibraryWindow::openComic(const ComicDB &comic) } } -void LibraryWindow::openComic() -{ - if (!importedCovers) { - ComicDB comic = comicsModel->getComic(comicsViewsManager->comicsView->currentIndex()); - openComic(comic); - } -} - void LibraryWindow::setCurrentComicsStatusReaded(YACReaderComicReadStatus readStatus) { comicsModel->setComicsRead(getSelectedComics(), readStatus); diff --git a/YACReaderLibrary/library_window.h b/YACReaderLibrary/library_window.h index 8dad315f..969c10a8 100644 --- a/YACReaderLibrary/library_window.h +++ b/YACReaderLibrary/library_window.h @@ -313,7 +313,6 @@ public slots: void selectSubfolder(const QModelIndex &mi, int child); void checkEmptyFolder(); void openComic(); - void openComic(const ComicDB &comic); void createLibrary(); void create(QString source, QString dest, QString name); void showAddLibrary();