Extract library paths methods to it's own struct in yacreader_global.h and use it everywhere

This commit is contained in:
Luis Ángel San Martín 2025-03-29 11:31:53 +01:00
parent 5aa637fdbe
commit d4b7c6dd8a
15 changed files with 133 additions and 90 deletions

View File

@ -1,4 +1,5 @@
#include "create_library_dialog.h"
#include "yacreader_global.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
@ -6,6 +7,8 @@
#include <QSizePolicy>
#include <QMessageBox>
using namespace YACReader;
CreateLibraryDialog::CreateLibraryDialog(QWidget *parent)
: QDialog(parent)
{
@ -87,7 +90,7 @@ void CreateLibraryDialog::create()
QFileInfo f(path->text());
if (f.exists() && f.isDir() && f.isWritable()) {
if (!libraries.contains(nameEdit->text())) {
emit createLibrary(QDir::cleanPath(path->text()), QDir::cleanPath(path->text()) + "/.yacreaderlibrary", nameEdit->text());
emit createLibrary(QDir::cleanPath(path->text()), LibraryPaths::libraryDataPath(QDir::cleanPath(path->text())), nameEdit->text());
close();
} else
emit libraryExists(nameEdit->text());

View File

@ -11,6 +11,7 @@
#include "comic_db.h"
#include "db_helper.h"
#include "reading_list_model.h"
#ifdef use_unarr
#include <unarr.h>
#endif
@ -489,11 +490,10 @@ int ComicModel::rowCount(const QModelIndex &parent) const
QStringList ComicModel::getPaths(const QString &_source)
{
QStringList paths;
QString source = _source + "/.yacreaderlibrary/covers/";
QList<ComicItem *>::ConstIterator itr;
for (itr = _data.constBegin(); itr != _data.constEnd(); itr++) {
QString hash = (*itr)->data(ComicModel::Hash).toString();
paths << source + hash + ".jpg";
paths << LibraryPaths::coverPath(_source, hash);
}
return paths;

View File

@ -4,6 +4,7 @@
#include "initial_comic_info_extractor.h"
#include "check_new_version.h"
#include "db_helper.h"
#include "yacreader_libraries.h"
#include "QsLog.h"
@ -118,15 +119,15 @@ QSqlDatabase DataBaseManagement::createDatabase(QString dest)
return db;
}
QSqlDatabase DataBaseManagement::loadDatabase(QString path)
QSqlDatabase DataBaseManagement::loadDatabase(QString libraryDataPath)
{
if (!QFile::exists(path + "/library.ydb")) {
if (!QFile::exists(libraryDataPath + "/library.ydb")) {
return QSqlDatabase();
}
QString threadId = QString::number((long long)QThread::currentThreadId(), 16);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", path + threadId);
db.setDatabaseName(path + "/library.ydb");
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", libraryDataPath + threadId);
db.setDatabaseName(libraryDataPath + "/library.ydb");
if (!db.open()) {
return QSqlDatabase();
}
@ -667,7 +668,8 @@ bool DataBaseManagement::importComicsInfo(QString source, QString dest)
QString basePath = QString(dest).remove("/.yacreaderlibrary/library.ydb");
QString path = basePath + getComic.record().value("path").toString();
int coverPage = getComic.record().value("coverPage").toInt();
InitialComicInfoExtractor ie(path, basePath + "/.yacreaderlibrary/covers/" + hash + ".jpg", coverPage);
auto coverPath = LibraryPaths::coverPath(basePath, hash);
InitialComicInfoExtractor ie(path, coverPath, coverPage);
ie.extract();
}
}
@ -851,7 +853,7 @@ int DataBaseManagement::compareVersions(const QString &v1, const QString v2)
return 0;
}
bool DataBaseManagement::updateToCurrentVersion(const QString &path)
bool DataBaseManagement::updateToCurrentVersion(const QString &libraryDataPath)
{
bool pre7 = false;
bool pre7_1 = false;
@ -861,7 +863,7 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &path)
bool pre9_13 = false;
bool pre9_14 = false;
QString fullPath = path + "/library.ydb";
QString fullPath = libraryDataPath + "/library.ydb";
if (compareVersions(DataBaseManagement::checkValidDB(fullPath), "7.0.0") < 0)
pre7 = true;
@ -966,7 +968,7 @@ bool DataBaseManagement::updateToCurrentVersion(const QString &path)
QImageReader thumbnail;
while (selectQuery.next()) {
thumbnail.setFileName(path % "/covers/" % selectQuery.value(1).toString() % ".jpg");
thumbnail.setFileName(libraryDataPath % "/covers/" % selectQuery.value(1).toString() % ".jpg");
float coverSizeRatio = static_cast<float>(thumbnail.size().width()) / thumbnail.size().height();
updateCoverInfo.bindValue(":coverSizeRatio", coverSizeRatio);

View File

@ -46,7 +46,7 @@ public:
static QSqlDatabase createDatabase(QString name, QString path);
static QSqlDatabase createDatabase(QString dest);
// carga una base de datos desde la ruta path
static QSqlDatabase loadDatabase(QString path);
static QSqlDatabase loadDatabase(QString libraryDataPath);
static QSqlDatabase loadDatabaseFromFile(QString path);
static bool createTables(QSqlDatabase &database);
static bool createComicInfoTable(QSqlDatabase &database, QString tableName);

View File

@ -21,10 +21,14 @@
#include "data_base_management.h"
#include "folder.h"
#include "yacreader_libraries.h"
#include "yacreader_global.h"
#include "qnaturalsorting.h"
#include "QsLog.h"
using namespace YACReader;
// server
YACReaderLibraries DBHelper::getLibraries()
@ -40,7 +44,7 @@ QList<LibraryItem *> DBHelper::getFolderSubfoldersFromLibrary(qulonglong library
QString connectionName = "";
QList<LibraryItem *> list;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
list = DBHelper::getFoldersFromParent(folderId, db, false);
connectionName = db.connectionName();
@ -82,7 +86,7 @@ QList<LibraryItem *> DBHelper::getFolderComicsFromLibrary(qulonglong libraryId,
QString connectionName = "";
QList<LibraryItem *> list;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
list = DBHelper::getComicsFromParent(folderId, db, sort);
connectionName = db.connectionName();
@ -98,7 +102,7 @@ quint32 DBHelper::getNumChildrenFromFolder(qulonglong libraryId, qulonglong fold
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery(db);
selectQuery.prepare("SELECT count(*) FROM folder WHERE parentId = :parentId and id <> 1");
@ -126,7 +130,7 @@ qulonglong DBHelper::getParentFromComicFolderId(qulonglong libraryId, qulonglong
QString connectionName = "";
Folder f;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
f = DBHelper::loadFolder(id, db);
connectionName = db.connectionName();
@ -141,7 +145,7 @@ ComicDB DBHelper::getComicInfo(qulonglong libraryId, qulonglong id)
QString connectionName = "";
ComicDB comic;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
bool found;
comic = DBHelper::loadComic(id, db, found);
@ -157,7 +161,7 @@ QList<ComicDB> DBHelper::getSiblings(qulonglong libraryId, qulonglong parentId)
QString connectionName = "";
QList<ComicDB> comics;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
comics = DBHelper::getSortedComicsFromParent(parentId, db);
connectionName = db.connectionName();
}
@ -174,7 +178,7 @@ QString DBHelper::getFolderName(qulonglong libraryId, qulonglong id)
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery(db); // TODO check
selectQuery.prepare("SELECT name FROM folder WHERE id = :id");
selectQuery.bindValue(":id", id);
@ -198,7 +202,7 @@ Folder DBHelper::getFolder(qulonglong libraryId, qulonglong id)
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery(db); // TODO check
selectQuery.prepare("SELECT * FROM folder WHERE id = :id");
selectQuery.bindValue(":id", id);
@ -259,7 +263,7 @@ QList<ComicDB> DBHelper::getLabelComics(qulonglong libraryId, qulonglong labelId
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery(db);
selectQuery.prepare("SELECT c.id,c.fileName,ci.title,ci.currentPage,ci.numPages,ci.hash,ci.read,ci.coverSizeRatio "
"FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) "
@ -300,7 +304,7 @@ QList<ComicDB> DBHelper::getFavorites(qulonglong libraryId)
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery(db);
selectQuery.prepare("SELECT c.id,c.fileName,ci.title,ci.currentPage,ci.numPages,ci.hash,ci.read,ci.coverSizeRatio "
"FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) "
@ -341,7 +345,7 @@ QList<ComicDB> DBHelper::getReading(qulonglong libraryId)
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery(db);
selectQuery.prepare("SELECT c.id,c.parentId,c.fileName,ci.title,ci.currentPage,ci.numPages,ci.hash,ci.read,ci.coverSizeRatio,ci.number "
"FROM comic c INNER JOIN comic_info ci ON (c.comicInfoId = ci.id) "
@ -381,7 +385,7 @@ QList<ReadingList> DBHelper::getReadingLists(qulonglong libraryId)
QList<ReadingList> list;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery("SELECT * from reading_list WHERE parentId IS NULL ORDER BY name DESC", db);
@ -420,7 +424,7 @@ QList<ComicDB> DBHelper::getReadingListFullContent(qulonglong libraryId, qulongl
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QList<qulonglong> ids;
ids << readingListId;
@ -618,7 +622,7 @@ void DBHelper::update(qulonglong libraryId, ComicInfo &comicInfo)
QString libraryPath = DBHelper::getLibraries().getPath(libraryId);
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
DBHelper::update(&comicInfo, db);
connectionName = db.connectionName();
}
@ -872,6 +876,10 @@ Folder DBHelper::updateChildrenInfo(qulonglong folderId, QSqlDatabase &db)
}
}
if (folder.numChildren == subfolders.count() + comics.count() && folder.firstChildHash == coverHash) {
return folder;
}
folder.numChildren = subfolders.count() + comics.count();
folder.firstChildHash = coverHash;
@ -894,6 +902,9 @@ Folder DBHelper::updateChildrenInfo(qulonglong folderId, QSqlDatabase &db)
void DBHelper::updateChildrenInfo(QSqlDatabase &db)
{
// measure time
auto start = std::chrono::high_resolution_clock::now();
QSqlQuery selectQuery(db); // TODO check
selectQuery.prepare("SELECT id FROM folder f WHERE f.parentId = 1 AND f.id <> 1");
selectQuery.exec();
@ -901,6 +912,12 @@ void DBHelper::updateChildrenInfo(QSqlDatabase &db)
while (selectQuery.next()) {
DBHelper::updateChildrenInfo(selectQuery.value(0).toULongLong(), db);
}
auto end = std::chrono::high_resolution_clock::now();
QString time = QString::number(std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count());
QString message = "updateChildrenInfo took " + time + "ms";
QLOG_INFO() << message;
}
void DBHelper::updateProgress(qulonglong libraryId, const ComicInfo &comicInfo)
@ -908,7 +925,7 @@ void DBHelper::updateProgress(qulonglong libraryId, const ComicInfo &comicInfo)
QString libraryPath = DBHelper::getLibraries().getPath(libraryId);
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
bool found;
ComicDB comic = DBHelper::loadComic(comicInfo.id, db, found);
@ -929,7 +946,7 @@ void DBHelper::setComicAsReading(qulonglong libraryId, const ComicInfo &comicInf
QString libraryPath = DBHelper::getLibraries().getPath(libraryId);
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
bool found;
ComicDB comic = DBHelper::loadComic(comicInfo.id, db, found);
@ -970,7 +987,7 @@ void DBHelper::updateFromRemoteClient(qulonglong libraryId, const ComicInfo &com
QString libraryPath = DBHelper::getLibraries().getPath(libraryId);
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
bool found;
ComicDB comic = DBHelper::loadComic(comicInfo.id, db, found);
@ -1009,7 +1026,7 @@ QMap<qulonglong, QList<ComicDB>> DBHelper::updateFromRemoteClient(const QMap<qul
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
db.transaction();
@ -1100,7 +1117,7 @@ void DBHelper::updateFromRemoteClientWithHash(const QList<ComicInfo> &comics)
QString libraryPath = DBHelper::getLibraries().getPath(libraries.getId(name));
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
db.transaction();
@ -1595,7 +1612,7 @@ QList<Label> DBHelper::getLabels(qulonglong libraryId)
QString connectionName = "";
QList<Label> labels;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
QSqlQuery selectQuery("SELECT * FROM label ORDER BY ordering,name", db); // TODO add some kind of
QSqlRecord record = selectQuery.record();
@ -2033,7 +2050,7 @@ QString DBHelper::getLibraryInfo(QUuid id)
QString connectionName = "";
QList<LibraryItem *> list;
{
QSqlDatabase db = DataBaseManagement::loadDatabase(libraryPath + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(libraryPath));
connectionName = db.connectionName();
// num folders

View File

@ -5,6 +5,8 @@
#include "yacreader_libraries.h"
#include "yacreader_global.h"
using namespace YACReader;
LibrariesUpdateCoordinator::LibrariesUpdateCoordinator(QSettings *settings, YACReaderLibraries &libraries, const std::function<bool()> &canStartUpdateProvider, QObject *parent)
: QObject(parent), libraries(libraries), canStartUpdateProvider(canStartUpdateProvider)
{
@ -121,7 +123,7 @@ void LibrariesUpdateCoordinator::updateLibrary(const QString &path)
QString cleanPath = QDir::cleanPath(pathDir.absolutePath());
libraryCreator->updateLibrary(cleanPath, QDir::cleanPath(pathDir.absolutePath() + "/.yacreaderlibrary"));
libraryCreator->updateLibrary(cleanPath, LibraryPaths::libraryDataPath(cleanPath));
connect(libraryCreator, &LibraryCreator::finished, &eventLoop, &QEventLoop::quit);

View File

@ -816,10 +816,12 @@ void LibraryWindow::loadLibrary(const QString &name)
historyController->clear();
showRootWidget();
QString path = libraries.getPath(name) + "/.yacreaderlibrary";
QString rootPath = libraries.getPath(name);
QString path = LibraryPaths::libraryDataPath(rootPath);
QString databasePath = LibraryPaths::libraryDatabasePath(rootPath);
QDir d; // TODO change this by static methods (utils class?? with delTree for example)
QString dbVersion;
if (d.exists(path) && d.exists(path + "/library.ydb") && (dbVersion = DataBaseManagement::checkValidDB(path + "/library.ydb")) != "") // si existe en disco la biblioteca seleccionada, y es válida..
if (d.exists(path) && d.exists(databasePath) && (dbVersion = DataBaseManagement::checkValidDB(databasePath)) != "") // si existe en disco la biblioteca seleccionada, y es válida..
{
int comparation = DataBaseManagement::compareVersions(dbVersion, DB_VERSION);
@ -932,11 +934,9 @@ void LibraryWindow::loadLibrary(const QString &name)
QString currentLibrary = selectedLibrary->currentText();
QString path = libraries.getPath(selectedLibrary->currentText());
if (QMessageBox::question(this, tr("Old library"), tr("Library '%1' has been created with an older version of YACReaderLibrary. It must be created again. Do you want to create the library now?").arg(currentLibrary), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes) {
QDir d(path + "/.yacreaderlibrary");
QDir d(LibraryPaths::libraryDataPath(path));
d.removeRecursively();
// d.rmdir(path+"/.yacreaderlibrary");
createLibraryDialog->setDataAndStart(currentLibrary, path);
// create(path,path+"/.yacreaderlibrary",currentLibrary);
}
// será possible renombrar y borrar estas bibliotecas
actions.renameLibraryAction->setEnabled(true);
@ -1070,9 +1070,9 @@ void LibraryWindow::updateFolder(const QModelIndex &miFolder)
showImportingWidget();
QString currentLibrary = selectedLibrary->currentText();
QString path = libraries.getPath(currentLibrary);
QString path = QDir::cleanPath(libraries.getPath(currentLibrary));
_lastAdded = currentLibrary;
libraryCreator->updateFolder(QDir::cleanPath(path), QDir::cleanPath(path + "/.yacreaderlibrary"), QDir::cleanPath(currentPath() + foldersModel->getFolderPath(miFolder)), miFolder);
libraryCreator->updateFolder(path, LibraryPaths::libraryDataPath(path), QDir::cleanPath(currentPath() + foldersModel->getFolderPath(miFolder)), miFolder);
libraryCreator->start();
}
@ -1773,7 +1773,8 @@ void LibraryWindow::openLibrary(QString path, QString name)
// TODO: fix bug, /a/b/c/.yacreaderlibrary/d/e
path.remove("/.yacreaderlibrary");
QDir d; // TODO change this by static methods (utils class?? with delTree for example)
if (d.exists(path + "/.yacreaderlibrary")) {
auto libraryDataPath = LibraryPaths::libraryDataPath(path);
if (d.exists(libraryDataPath)) {
_lastAdded = name;
_sourceLastAdded = path;
openLastCreated();
@ -1805,7 +1806,7 @@ void LibraryWindow::updateLibrary()
QString currentLibrary = selectedLibrary->currentText();
QString path = libraries.getPath(currentLibrary);
_lastAdded = currentLibrary;
libraryCreator->updateLibrary(path, path + "/.yacreaderlibrary");
libraryCreator->updateLibrary(path, LibraryPaths::libraryDataPath(path));
libraryCreator->start();
}
@ -1814,8 +1815,7 @@ void LibraryWindow::deleteCurrentLibrary()
QString path = libraries.getPath(selectedLibrary->currentText());
libraries.remove(selectedLibrary->currentText());
selectedLibrary->removeItem(selectedLibrary->currentIndex());
// selectedLibrary->setCurrentIndex(0);
path = path + "/.yacreaderlibrary";
path = LibraryPaths::libraryDatabasePath(path);
QDir d(path);
d.removeRecursively();
@ -1895,7 +1895,7 @@ void LibraryWindow::rescanLibraryForXMLInfo()
QString path = libraries.getPath(currentLibrary);
_lastAdded = currentLibrary;
xmlInfoLibraryScanner->scanLibrary(path, path + "/.yacreaderlibrary");
xmlInfoLibraryScanner->scanLibrary(path, LibraryPaths::libraryDataPath(path));
}
void LibraryWindow::showLibraryInfo()
@ -1929,7 +1929,7 @@ void LibraryWindow::rescanFolderForXMLInfo(QModelIndex modelIndex)
QString path = libraries.getPath(currentLibrary);
_lastAdded = currentLibrary;
xmlInfoLibraryScanner->scanFolder(path, path + "/.yacreaderlibrary", QDir::cleanPath(currentPath() + foldersModel->getFolderPath(modelIndex)), modelIndex);
xmlInfoLibraryScanner->scanFolder(path, LibraryPaths::libraryDataPath(path), QDir::cleanPath(currentPath() + foldersModel->getFolderPath(modelIndex)), modelIndex);
}
void LibraryWindow::cancelCreating()
@ -1952,7 +1952,7 @@ void LibraryWindow::stopXMLScanning()
void LibraryWindow::setRootIndex()
{
if (!libraries.isEmpty()) {
QString path = libraries.getPath(selectedLibrary->currentText()) + "/.yacreaderlibrary";
QString path = LibraryPaths::libraryDataPath(libraries.getPath(selectedLibrary->currentText()));
QDir d; // TODO change this by static methods (utils class?? with delTree for example)
if (d.exists(path)) {
navigationController->selectedFolder(QModelIndex());
@ -2270,7 +2270,7 @@ void LibraryWindow::setFolderType(FileType type)
void LibraryWindow::exportLibrary(QString destPath)
{
QString currentLibrary = selectedLibrary->currentText();
QString path = libraries.getPath(currentLibrary) + "/.yacreaderlibrary";
QString path = LibraryPaths::libraryDataPath(libraries.getPath(currentLibrary));
packageManager->createPackage(path, destPath + "/" + currentLibrary);
}
@ -2311,13 +2311,13 @@ QString LibraryWindow::currentFolderPath()
void LibraryWindow::showExportComicsInfo()
{
exportComicsInfoDialog->source = currentPath() + "/.yacreaderlibrary/library.ydb";
exportComicsInfoDialog->source = LibraryPaths::libraryDatabasePath(currentPath());
exportComicsInfoDialog->open();
}
void LibraryWindow::showImportComicsInfo()
{
importComicsInfoDialog->dest = currentPath() + "/.yacreaderlibrary/library.ydb";
importComicsInfoDialog->dest = currentPath() + LibraryPaths::libraryDatabasePath(currentPath());
importComicsInfoDialog->open();
}
@ -2609,7 +2609,7 @@ void LibraryWindow::updateViewsOnComicUpdateWithId(quint64 libraryId, quint64 co
}
QString connectionName = "";
{
QSqlDatabase db = DataBaseManagement::loadDatabase(path + "/.yacreaderlibrary");
QSqlDatabase db = DataBaseManagement::loadDatabase(LibraryPaths::libraryDataPath(path));
bool found;
auto comic = DBHelper::loadComic(comicId, db, found);
if (found) {

View File

@ -954,7 +954,8 @@ void PropertiesDialog::save()
if (sequentialEditing) {
if (coverChanged) {
InitialComicInfoExtractor ie(basePath + comics[currentComicIndex].path, basePath + "/.yacreaderlibrary/covers/" + comics[currentComicIndex].info.hash + ".jpg", comics[currentComicIndex].info.coverPage.toInt());
auto coverPath = LibraryPaths::coverPath(basePath, comics[currentComicIndex].info.hash);
InitialComicInfoExtractor ie(basePath + comics[currentComicIndex].path, coverPath, comics[currentComicIndex].info.coverPage.toInt());
ie.extract();
if (ie.getOriginalCoverSize().second > 0) {

View File

@ -1,10 +1,8 @@
#include "covercontroller_v2.h"
#include "db_helper.h" //get libraries
#include <QImage>
#include "yacreader_libraries.h"
#include "yacreader_http_session.h"
#include "template.h"
#include "../static.h"
#include "yacreader_global.h"
using stefanfrings::HttpRequest;
using stefanfrings::HttpResponse;
@ -22,7 +20,7 @@ void CoverControllerV2::service(HttpRequest &request, HttpResponse &response)
QString libraryName = DBHelper::getLibraryName(pathElements.at(3).toInt());
QString fileName = pathElements.at(5);
QImage img(libraries.getPath(libraryName) + "/.yacreaderlibrary/covers/" + fileName);
QImage img(YACReader::LibraryPaths::coverPathWithFileName(libraries.getPath(libraryName), fileName));
if (!img.isNull()) {
QByteArray ba;
QBuffer buffer(&ba);

View File

@ -2,9 +2,11 @@
#include "qnaturalsorting.h"
#include "yacreader_global.h"
using namespace YACReader;
void writeIdToLibraryFolder(const QString &path, const QUuid &id)
{
QFile file(path + "/.yacreaderlibrary/id");
QFile file(LibraryPaths::idPath(path));
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
stream << id.toString(QUuid::WithoutBraces);
@ -14,7 +16,7 @@ void writeIdToLibraryFolder(const QString &path, const QUuid &id)
QUuid readFromLibraryFolder(const QString &path)
{
QFile file(path + "/.yacreaderlibrary/id");
QFile file(LibraryPaths::idPath(path));
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
QString id = stream.readLine();
@ -62,7 +64,7 @@ QString YACReaderLibraries::getPath(const QUuid &id)
QString YACReaderLibraries::getDBPath(int id)
{
return YACReaderLibrary::libraryDataPath(getPath(id));
return LibraryPaths::libraryDataPath(getPath(id));
}
QString YACReaderLibraries::getName(int id)
@ -228,7 +230,7 @@ QString YACReaderLibrary::getPath() const
QString YACReaderLibrary::getDBPath() const
{
return YACReaderLibrary::libraryDataPath(path);
return LibraryPaths::libraryDataPath(path);
}
int YACReaderLibrary::getLegacyId() const
@ -251,21 +253,6 @@ bool YACReaderLibrary::operator!=(const YACReaderLibrary &other) const
return !(*this == other);
}
QString YACReaderLibrary::libraryDataPath(const QString &libraryPath)
{
return QDir(libraryPath).filePath(".yacreaderlibrary");
}
QString YACReaderLibrary::libraryDatabasePath(const QString &libraryPath)
{
return QDir(YACReaderLibrary::libraryDataPath(libraryPath)).filePath("library.ydb");
}
QString YACReaderLibrary::libraryCoversFolderPath(const QString &libraryPath)
{
return QDir(YACReaderLibrary::libraryDataPath(libraryPath)).filePath("covers");
}
QDataStream &operator<<(QDataStream &out, const YACReaderLibrary &library)
{
out << library.name << library.path << library.legacyId << library.id;

View File

@ -55,10 +55,6 @@ public:
friend QDataStream &operator>>(QDataStream &in, YACReaderLibrary &library);
operator QString() const { return QString("%1 [%2, %3, %4]").arg(name, QString::number(legacyId), id.toString(QUuid::WithoutBraces), path); }
static QString libraryDataPath(const QString &libraryPath); // libraryPath + /.yacreaderlibrary
static QString libraryDatabasePath(const QString &libraryPath); // libraryPath + /.yacreaderlibrary/library.ydb
static QString libraryCoversFolderPath(const QString &libraryPath); // libraryPath + /.yacreaderlibrary/covers
private:
QString name;
QString path;

View File

@ -33,7 +33,7 @@ void ConsoleUILibraryCreator::createLibrary(const QString &name, const QString &
return;
}
libraryCreator->createLibrary(cleanPath, YACReaderLibrary::libraryDataPath(cleanPath));
libraryCreator->createLibrary(cleanPath, LibraryPaths::libraryDataPath(cleanPath));
connect(libraryCreator, &LibraryCreator::finished, this, &ConsoleUILibraryCreator::done);
connect(libraryCreator, &LibraryCreator::comicAdded, this, &ConsoleUILibraryCreator::newComic);
@ -62,7 +62,7 @@ void ConsoleUILibraryCreator::updateLibrary(const QString &path)
LibraryCreator *libraryCreator = new LibraryCreator(settings);
QString cleanPath = QDir::cleanPath(pathDir.absolutePath());
libraryCreator->updateLibrary(cleanPath, YACReaderLibrary::libraryDataPath(cleanPath));
libraryCreator->updateLibrary(cleanPath, LibraryPaths::libraryDataPath(cleanPath));
connect(libraryCreator, &LibraryCreator::finished, this, &ConsoleUILibraryCreator::done);
connect(libraryCreator, &LibraryCreator::comicAdded, this, &ConsoleUILibraryCreator::newComic);
@ -85,7 +85,7 @@ void ConsoleUILibraryCreator::addExistingLibrary(const QString &name, const QStr
}
QString cleanPath = QDir::cleanPath(pathDir.absolutePath());
if (!QDir(YACReaderLibrary::libraryDataPath(cleanPath)).exists()) {
if (!QDir(LibraryPaths::libraryDataPath(cleanPath)).exists()) {
std::cout << "No data folder found in path: " << cleanPath.toStdString() << std::endl;
return;
}
@ -135,7 +135,7 @@ void ConsoleUILibraryCreator::rescanXMLInfoLibrary(const QString &path)
connect(scanner, &XMLInfoLibraryScanner::finished, &eventLoop, &QEventLoop::quit);
std::cout << "Scanning comics";
scanner->scanLibrary(cleanPath, YACReaderLibrary::libraryDataPath(cleanPath));
scanner->scanLibrary(cleanPath, LibraryPaths::libraryDataPath(cleanPath));
eventLoop.exec();
}

View File

@ -14,16 +14,18 @@ void LibrariesUpdater::updateIfNeeded()
libraries.load();
foreach (QString name, libraries.getNames()) {
QString path = libraries.getPath(name) + "/.yacreaderlibrary";
QString root = libraries.getPath(name);
QString libraryDataPath = YACReader::LibraryPaths::libraryDataPath(root);
QString databasePath = YACReader::LibraryPaths::libraryDatabasePath(root);
QDir d;
QString dbVersion;
if (d.exists(path) && d.exists(path + "/library.ydb") && (dbVersion = DataBaseManagement::checkValidDB(path + "/library.ydb")) != "") {
if (d.exists(libraryDataPath) && d.exists(databasePath) && (dbVersion = DataBaseManagement::checkValidDB(databasePath)) != "") {
int comparation = DataBaseManagement::compareVersions(dbVersion, DB_VERSION);
if (comparation < 0) {
bool updated = DataBaseManagement::updateToCurrentVersion(path);
bool updated = DataBaseManagement::updateToCurrentVersion(libraryDataPath);
if (!updated) {
// TODO log error
}

View File

@ -405,7 +405,7 @@ ComicInfo &ComicInfo::operator=(const ComicInfo &comicInfo)
QPixmap ComicInfo::getCover(const QString &basePath)
{
if (cover.isNull()) {
cover.load(basePath + "/.yacreaderlibrary/covers/" + hash + ".jpg");
cover.load(YACReader::LibraryPaths::coverPath(basePath, hash));
}
QPixmap c;
c.convertFromImage(cover);

View File

@ -5,6 +5,7 @@
#include <QDataStream>
#include <QMetaType>
#include <QAbstractItemModel>
#include <QDir>
class QLibrary;
@ -108,7 +109,41 @@ void iterate(const QModelIndex &index,
const QAbstractItemModel *model,
const std::function<bool(const QModelIndex &)> &iteration);
}
struct LibraryPaths {
LibraryPaths() = delete; // Prevent instantiation
static QString libraryDataPath(const QString &libraryPath) // libraryPath + /.yacreaderlibrary
{
return QDir(libraryPath).filePath(".yacreaderlibrary");
}
static QString libraryDatabasePath(const QString &libraryPath) // libraryPath + /.yacreaderlibrary/library.ydb
{
return QDir(libraryDataPath(libraryPath)).filePath("library.ydb");
}
static QString libraryCoversFolderPath(const QString &libraryPath) // libraryPath + /.yacreaderlibrary/covers
{
return QDir(libraryDataPath(libraryPath)).filePath("covers");
}
static QString coverPath(const QString &libraryPath, const QString &hash) // libraryPath + /.yacreaderlibrary/covers/hash + .jpg
{
return QDir(libraryCoversFolderPath(libraryPath)).filePath(hash + ".jpg");
}
static QString coverPathWithFileName(const QString &libraryPath, const QString &fileName) // libraryPath + /.yacreaderlibrary/covers/hash + fileName
{
return QDir(libraryCoversFolderPath(libraryPath)).filePath(fileName);
}
static QString idPath(const QString &libraryPath) // libraryPath + /.yacreaderlibrary/id
{
return QDir(libraryDataPath(libraryPath)).filePath("id");
}
};
} // namespace YACReader
Q_DECLARE_METATYPE(YACReader::OpenComicSource::Source)
Q_DECLARE_METATYPE(YACReader::OpenComicSource)