[app] Open the book passed in from the command line

This allows to use this app as a viewer for arbitrary ebooks,
not necessarily managed by this app.
This commit is contained in:
Slava Monich 2021-11-07 03:11:55 +02:00
parent e07cea347a
commit 9644aaddf9
5 changed files with 96 additions and 69 deletions

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2018 Jolla Ltd. * Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2018 Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -8,15 +8,15 @@
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* * Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer
* the documentation and/or other materials provided with the * in the documentation and/or other materials provided with the
* distribution. * distribution.
* * Neither the name of Jolla Ltd nor the names of its contributors * 3. Neither the names of the copyright holders nor the names of its
* may be used to endorse or promote products derived from this * contributors may be used to endorse or promote products derived
* software without specific prior written permission. * from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@ -333,7 +333,7 @@ BooksBook::BooksBook(QObject* aParent) :
BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath, BooksBook::BooksBook(const BooksStorage& aStorage, QString aRelativePath,
shared_ptr<Book> aBook) : shared_ptr<Book> aBook) :
QObject(NULL), QObject(Q_NULLPTR),
iRef(1), iRef(1),
iStorage(aStorage), iStorage(aStorage),
iBook(aBook), iBook(aBook),
@ -656,6 +656,17 @@ void BooksBook::deleteFiles()
} }
} }
BooksBook* BooksBook::newBook(QString aFullPath)
{
shared_ptr<Book> ref = BooksUtil::bookFromFile(aFullPath);
if (!ref.isNull()) {
return new BooksBook(BooksStorage::tmpStorage(),
QFileInfo(aFullPath).dir().absolutePath(), ref);
} else {
return NULL;
}
}
BooksBook* BooksBook::newBook(const BooksStorage& aStorage, QString aRelPath, BooksBook* BooksBook::newBook(const BooksStorage& aStorage, QString aRelPath,
QString aFileName) QString aFileName)
{ {

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2018 Jolla Ltd. * Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2018 Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -8,15 +8,15 @@
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* * Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer
* the documentation and/or other materials provided with the * in the documentation and/or other materials provided with the
* distribution. * distribution.
* * Neither the name of Jolla Ltd nor the names of its contributors * 3. Neither the names of the copyright holders nor the names of its
* may be used to endorse or promote products derived from this * contributors may be used to endorse or promote products derived
* software without specific prior written permission. * from this software without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
@ -67,11 +67,12 @@ class BooksBook : public QObject, public BooksItem
Q_PROPERTY(bool fontSizeAdjust READ fontSizeAdjust WRITE setFontSizeAdjust NOTIFY fontSizeAdjustChanged) Q_PROPERTY(bool fontSizeAdjust READ fontSizeAdjust WRITE setFontSizeAdjust NOTIFY fontSizeAdjustChanged)
public: public:
explicit BooksBook(QObject* aParent = NULL); explicit BooksBook(QObject* aParent = Q_NULLPTR);
BooksBook(const BooksStorage& aStorage, QString aRelativePath, BooksBook(const BooksStorage& aStorage, QString aRelativePath,
shared_ptr<Book> aBook); shared_ptr<Book> aBook);
~BooksBook(); ~BooksBook();
static BooksBook* newBook(QString aFullPath);
static BooksBook* newBook(const BooksStorage& aStorage, QString aRelPath, static BooksBook* newBook(const BooksStorage& aStorage, QString aRelPath,
QString aFileName); QString aFileName);

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2020 Jolla Ltd. * Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2020 Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -453,7 +453,7 @@ BooksSettings::Private::updateCurrentBook()
if (path.isEmpty()) { if (path.isEmpty()) {
if (iCurrentBook) { if (iCurrentBook) {
iCurrentBook->release(); iCurrentBook->release();
iCurrentBook = NULL; iCurrentBook = Q_NULLPTR;
return true; return true;
} }
} else if (!iCurrentBook || iCurrentBook->path() != path) { } else if (!iCurrentBook || iCurrentBook->path() != path) {
@ -463,16 +463,17 @@ BooksSettings::Private::updateCurrentBook()
QFileInfo info(path); QFileInfo info(path);
BooksStorageManager* mgr = BooksStorageManager::instance(); BooksStorageManager* mgr = BooksStorageManager::instance();
BooksStorage storage = mgr->storageForPath(info.path(), &rel); BooksStorage storage = mgr->storageForPath(info.path(), &rel);
if (storage.isValid()) {
if (iCurrentBook) iCurrentBook->release(); if (iCurrentBook) iCurrentBook->release();
iCurrentBook = new BooksBook(storage, rel, book); iCurrentBook = storage.isValid() ?
new BooksBook(storage, rel, book) :
new BooksBook(BooksStorage::tmpStorage(),
info.dir().absolutePath(), book);
iCurrentBook->requestCoverImage(); iCurrentBook->requestCoverImage();
return true; return true;
} }
}
if (iCurrentBook) { if (iCurrentBook) {
iCurrentBook->release(); iCurrentBook->release();
iCurrentBook = NULL; iCurrentBook = Q_NULLPTR;
return true; return true;
} }
} }

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2020 Jolla Ltd. * Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2020 Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -35,16 +35,12 @@
#include "BooksStorage.h" #include "BooksStorage.h"
#include "BooksPaintContext.h" #include "BooksPaintContext.h"
#include "BooksDialogManager.h" #include "BooksDialogManager.h"
#include "BooksImageProvider.h"
#include "BooksSettings.h" #include "BooksSettings.h"
#include "HarbourDebug.h" #include "HarbourDebug.h"
#include "HarbourMediaPlugin.h"
#include "HarbourPolicyPlugin.h"
#include "ZLibrary.h" #include "ZLibrary.h"
#include "ZLApplication.h" #include "ZLApplication.h"
#include "ZLLanguageUtil.h"
#include "ZLLogger.h" #include "ZLLogger.h"
#include "filesystem/ZLQtFSManager.h" #include "filesystem/ZLQtFSManager.h"
@ -232,33 +228,6 @@ bool ZLibrary::init(int& aArgc, char** &aArgv)
void ZLibrary::run(ZLApplication* aApp) void ZLibrary::run(ZLApplication* aApp)
{ {
if (ZLLanguageUtil::isRTLLanguage(ZLibrary::Language())) {
qApp->setLayoutDirection(Qt::RightToLeft);
}
QString qml(QString::fromStdString(BaseDirectory + BOOKS_QML_FILE));
HDEBUG("qml file" << qPrintable(qml));
QQuickView* view = SailfishApp::createView();
QQmlContext* root = view->rootContext();
QQmlEngine* engine = root->engine();
QSharedPointer<BooksSettings> settings = BooksSettings::sharedInstance();
HarbourPolicyPlugin::registerTypes(engine, BOOKS_QML_PLUGIN,
BOOKS_QML_PLUGIN_V1, BOOKS_QML_PLUGIN_V2);
HarbourMediaPlugin::registerTypes(engine, BOOKS_QML_PLUGIN,
BOOKS_QML_PLUGIN_V1, BOOKS_QML_PLUGIN_V2);
engine->addImageProvider(BooksImageProvider::PROVIDER_ID,
new BooksImageProvider(root));
root->setContextProperty("PointsPerInch", booksPPI);
root->setContextProperty("MaximumHintCount", 1);
root->setContextProperty("BooksSettingsMenu",
QVariant::fromValue(BOOKS_SETTINGS_MENU));
root->setContextProperty("Settings", settings.data());
view->setTitle(qtTrId("harbour-books-app-name"));
view->setSource(QUrl::fromLocalFile(qml));
view->show();
HDEBUG("started"); HDEBUG("started");
qApp->exec(); qApp->exec();
HDEBUG("exiting..."); HDEBUG("exiting...");

View file

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2015-2020 Jolla Ltd. * Copyright (C) 2015-2021 Jolla Ltd.
* Copyright (C) 2015-2020 Slava Monich <slava.monich@jolla.com> * Copyright (C) 2015-2021 Slava Monich <slava.monich@jolla.com>
* *
* You may use this file under the terms of the BSD license as follows: * You may use this file under the terms of the BSD license as follows:
* *
@ -38,6 +38,7 @@
#include "BooksBookModel.h" #include "BooksBookModel.h"
#include "BooksCoverModel.h" #include "BooksCoverModel.h"
#include "BooksConfig.h" #include "BooksConfig.h"
#include "BooksImageProvider.h"
#include "BooksImportModel.h" #include "BooksImportModel.h"
#include "BooksPathModel.h" #include "BooksPathModel.h"
#include "BooksPageStack.h" #include "BooksPageStack.h"
@ -50,9 +51,12 @@
#include "HarbourDisplayBlanking.h" #include "HarbourDisplayBlanking.h"
#include "HarbourDebug.h" #include "HarbourDebug.h"
#include "HarbourMediaPlugin.h"
#include "HarbourPolicyPlugin.h"
#include "HarbourTheme.h" #include "HarbourTheme.h"
#include "ZLibrary.h" #include "ZLibrary.h"
#include "ZLLanguageUtil.h"
#include <sailfishapp.h> #include <sailfishapp.h>
@ -106,6 +110,47 @@ Q_DECL_EXPORT int main(int argc, char **argv)
BooksConfigManager configManager; BooksConfigManager configManager;
if (ZLibrary::init(argc, argv)) { if (ZLibrary::init(argc, argv)) {
if (ZLLanguageUtil::isRTLLanguage(ZLibrary::Language())) {
qApp->setLayoutDirection(Qt::RightToLeft);
}
const QString qml(QString::fromStdString(ZLibrary::BaseDirectory +
BOOKS_QML_FILE));
HDEBUG("qml file" << qPrintable(qml));
QQuickView* view = SailfishApp::createView();
QQmlContext* root = view->rootContext();
QQmlEngine* engine = root->engine();
QSharedPointer<BooksSettings> settings = BooksSettings::sharedInstance();
HarbourPolicyPlugin::registerTypes(engine, BOOKS_QML_PLUGIN,
BOOKS_QML_PLUGIN_V1, BOOKS_QML_PLUGIN_V2);
HarbourMediaPlugin::registerTypes(engine, BOOKS_QML_PLUGIN,
BOOKS_QML_PLUGIN_V1, BOOKS_QML_PLUGIN_V2);
engine->addImageProvider(BooksImageProvider::PROVIDER_ID,
new BooksImageProvider(root));
root->setContextProperty("PointsPerInch", booksPPI);
root->setContextProperty("MaximumHintCount", 1);
root->setContextProperty("BooksSettingsMenu",
QVariant::fromValue(BOOKS_SETTINGS_MENU));
root->setContextProperty("Settings", settings.data());
if (argc > 1) {
const QString file(QString::fromLocal8Bit(argv[1]));
if (QFile::exists(file)) {
BooksBook* book = BooksBook::newBook(file);
if (book) {
settings->setCurrentBook(book);
book->requestCoverImage();
book->release();
}
}
}
view->setTitle(qtTrId("harbour-books-app-name"));
view->setSource(QUrl::fromLocalFile(qml));
view->show();
ZLibrary::run(NULL); ZLibrary::run(NULL);
BooksTaskQueue::waitForDone(TASK_QUEUE_TIMEOUT); BooksTaskQueue::waitForDone(TASK_QUEUE_TIMEOUT);
ZLibrary::shutdown(); ZLibrary::shutdown();