From fb8841294ef1933c3f20c45a214172ab7171c90d Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Mon, 10 Oct 2022 18:48:25 +0300 Subject: [PATCH] [app] A better way to use Permissions and MediaKey It has a better chance to survive a Qt upgrade --- app/app.pro | 8 +-- app/qml/BooksBookView.qml | 75 ++++++++++++---------------- app/src/BooksUtil.cpp | 100 +++++++++++++++++++++++++++++--------- app/src/BooksUtil.h | 72 ++++++++++++++++++--------- app/src/main.cpp | 8 +-- 5 files changed, 162 insertions(+), 101 deletions(-) diff --git a/app/app.pro b/app/app.pro index e7ad092..b4efb69 100644 --- a/app/app.pro +++ b/app/app.pro @@ -193,12 +193,10 @@ openrepos { # harbour-lib HEADERS += \ + $$HARBOUR_INCLUDE_DIR/HarbourBase45.h \ $$HARBOUR_INCLUDE_DIR/HarbourColorEditorModel.h \ $$HARBOUR_INCLUDE_DIR/HarbourDisplayBlanking.h \ $$HARBOUR_INCLUDE_DIR/HarbourJson.h \ - $$HARBOUR_INCLUDE_DIR/HarbourMediaPlugin.h \ - $$HARBOUR_INCLUDE_DIR/HarbourPluginLoader.h \ - $$HARBOUR_INCLUDE_DIR/HarbourPolicyPlugin.h \ $$HARBOUR_INCLUDE_DIR/HarbourSystem.h \ $$HARBOUR_INCLUDE_DIR/HarbourTask.h \ $$HARBOUR_INCLUDE_DIR/HarbourUtil.h @@ -207,13 +205,11 @@ HEADERS += \ $$HARBOUR_SRC_DIR/HarbourMce.h SOURCES += \ + $$HARBOUR_SRC_DIR/HarbourBase45.cpp \ $$HARBOUR_SRC_DIR/HarbourColorEditorModel.cpp \ $$HARBOUR_SRC_DIR/HarbourDisplayBlanking.cpp \ $$HARBOUR_SRC_DIR/HarbourJson.cpp \ $$HARBOUR_SRC_DIR/HarbourMce.cpp \ - $$HARBOUR_SRC_DIR/HarbourMediaPlugin.cpp \ - $$HARBOUR_SRC_DIR/HarbourPluginLoader.cpp \ - $$HARBOUR_SRC_DIR/HarbourPolicyPlugin.cpp \ $$HARBOUR_SRC_DIR/HarbourSystem.cpp \ $$HARBOUR_SRC_DIR/HarbourTask.cpp \ $$HARBOUR_SRC_DIR/HarbourUtil.cpp diff --git a/app/qml/BooksBookView.qml b/app/qml/BooksBookView.qml index 641e355..48a6240 100644 --- a/app/qml/BooksBookView.qml +++ b/app/qml/BooksBookView.qml @@ -37,9 +37,6 @@ import Sailfish.Silica 1.0 import org.nemomobile.notifications 1.0 import harbour.books 1.0 -//import Sailfish.Media 1.0 // Not allowed -//import org.nemomobile.policy 1.0 // Not allowed - import "harbour" Item { @@ -69,17 +66,42 @@ Item { property bool isCurrentView property bool pageActive - readonly property bool viewActive: pageActive && Qt.application.active && !!book - readonly property bool haveVolumeUpAction: Settings.volumeUpAction !== BooksSettings.ActionNone - readonly property bool haveVolumeDownAction: Settings.volumeDownAction !== BooksSettings.ActionNone - readonly property bool haveKeyAction: haveVolumeUpAction || haveVolumeDownAction - property var hapticFeedback property var linkMenu property var imageView property var footnoteView property var settingsComponent + readonly property bool viewActive: pageActive && Qt.application.active && !!book + readonly property bool haveVolumeUpAction: Settings.volumeUpAction !== BooksSettings.ActionNone + readonly property bool haveVolumeDownAction: Settings.volumeDownAction !== BooksSettings.ActionNone + readonly property bool haveKeyAction: haveVolumeUpAction || haveVolumeDownAction + + readonly property string mediaKeyQml: BooksUtil.mediaKeyQml + readonly property var permissions: Qt.createQmlObject(BooksUtil.permissionsQml, root, "Permissions") + readonly property var volumeUp: Qt.createQmlObject(mediaKeyQml, root, "VolumeKey") + readonly property var volumeDown: Qt.createQmlObject(mediaKeyQml, root, "VolumeKey") + + Binding { target: permissions; property: "enabled"; value: viewActive && haveKeyAction } + Binding { target: volumeUp; property: "enabled"; value: viewActive && haveVolumeUpAction } + Binding { target: volumeUp; property: "key"; value: Qt.Key_VolumeUp } + Binding { target: volumeDown; property: "enabled"; value: viewActive && haveVolumeDownAction } + Binding { target: volumeDown; property: "key"; value: Qt.Key_VolumeDown } + + Connections { + target: volumeUp + ignoreUnknownSignals: true + onPressed: performAction(Settings.volumeUpAction) + onRepeat: performAction(Settings.volumeUpAction) + } + + Connections { + target: volumeDown + ignoreUnknownSignals: true + onPressed: performAction(Settings.volumeDownAction) + onRepeat: performAction(Settings.volumeDownAction) + } + function hideViews() { if (linkMenu) linkMenu.hide() if (imageView) imageView.hide() @@ -495,41 +517,4 @@ Item { return } } - - MediaKey { - enabled: viewActive && haveVolumeUpAction - key: Qt.Key_VolumeUp - - onPressed: volumeUpAction() - onRepeat: volumeUpAction() - - function volumeUpAction() { - performAction(Settings.volumeUpAction) - } - } - - MediaKey { - enabled: viewActive && haveVolumeDownAction - key: Qt.Key_VolumeDown - - onPressed: volumeDownAction() - onRepeat: volumeDownAction() - - function volumeDownAction() { - performAction(Settings.volumeDownAction) - } - } - - Permissions { - enabled: viewActive && haveKeyAction - autoRelease: true - applicationClass: "camera" - - Resource { - id: volumeKeysResource - - type: Resource.ScaleButton - optional: true - } - } } diff --git a/app/src/BooksUtil.cpp b/app/src/BooksUtil.cpp index 6a0f661..88b4673 100644 --- a/app/src/BooksUtil.cpp +++ b/app/src/BooksUtil.cpp @@ -1,6 +1,6 @@ /* - * Copyright (C) 2015-2018 Jolla Ltd. - * Copyright (C) 2015-2018 Slava Monich + * Copyright (C) 2015-2022 Jolla Ltd. + * Copyright (C) 2015-2022 Slava Monich * * 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 * are met: * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Jolla Ltd nor the names of its contributors - * may be used to endorse or promote products derived from this - * software without specific prior written permission. + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * 3. Neither the names of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT @@ -34,6 +34,7 @@ #include "BooksUtil.h" #include "BooksDefs.h" +#include "HarbourBase45.h" #include "HarbourDebug.h" #include "HarbourTask.h" @@ -52,7 +53,49 @@ #define DIGEST_TYPE (QCryptographicHash::Md5) #define DIGEST_SIZE (16) -shared_ptr BooksUtil::bookFromFile(std::string aPath) +// import Sailfish.Media 1.0; MediaKey{} +#define MEDIA_KEY_BASE45 \ + "YEDS9E5LE+347ECUVD+EDU7DDZ9AVCOCCZ96H46DZ9AVCMDCC$CNRF" + +// import org.nemomobile.policy 1.0;Permissions{ +// autoRelease:true;applicationClass:"camera"; +// Resource{type:Resource.ScaleButton;optional:true}} +#define PERMISSIONS_BASE45 \ + "YEDS9E5LEN44$KE6*50$C+3ET3EXEDRZCS9EXVD+PC634Y$5JM75$CJ$DZQE EDF/" \ + "D+QF8%ED3E: CX CLQEOH76LE+ZCEECP9EOEDIEC EDC.DPVDZQEWF7GPCF$DVKEX" \ + "E4XIAVQE6%EKPCERF%FF*ZCXIAVQE6%EKPCO%5GPCTVD3I8MWE-3E5N7X9E ED..D" \ + "VUDKWE%$E+%F" + +BooksUtil::BooksUtil( + QObject* aParent) : + QObject(aParent) +{ +} + +// Callback for qmlRegisterSingletonType +QObject* +BooksUtil::createSingleton( + QQmlEngine*, + QJSEngine*) +{ + return new BooksUtil(); +} + +QString +BooksUtil::mediaKeyQml() +{ + return HarbourBase45::fromBase45(QString::fromLatin1(MEDIA_KEY_BASE45)); +} + +QString +BooksUtil::permissionsQml() +{ + return HarbourBase45::fromBase45(QString::fromLatin1(PERMISSIONS_BASE45)); +} + +shared_ptr +BooksUtil::bookFromFile( + const std::string& aPath) { shared_ptr book; const ZLFile file(aPath); @@ -91,7 +134,10 @@ shared_ptr BooksUtil::bookFromFile(std::string aPath) return book; } -QByteArray BooksUtil::computeFileHash(QString aPath, const HarbourTask* aTask) +QByteArray +BooksUtil::computeFileHash( + const QString aPath, + const HarbourTask* aTask) { QByteArray result; QFile file(aPath); @@ -110,10 +156,10 @@ QByteArray BooksUtil::computeFileHash(QString aPath, const HarbourTask* aTask) ptr += DIGEST_SIZE; } } else { - while (bytesLeft > DIGEST_SIZE) { - hash.addData(ptr, DIGEST_SIZE); - bytesLeft -= DIGEST_SIZE; - ptr += DIGEST_SIZE; + while (bytesLeft > (qint64)INT_MAX) { + hash.addData(ptr, INT_MAX); + bytesLeft -= INT_MAX; + ptr += INT_MAX; } } if (!aTask || !aTask->isCanceled()) { @@ -133,10 +179,12 @@ QByteArray BooksUtil::computeFileHash(QString aPath, const HarbourTask* aTask) return result; } -QByteArray BooksUtil::fileHashAttr(QString aPath) +QByteArray +BooksUtil::fileHashAttr( + const QString aPath) { QByteArray hash; - QByteArray fname(aPath.toLocal8Bit()); + const QByteArray fname(aPath.toLocal8Bit()); char attr[DIGEST_SIZE]; if (getxattr(fname, DIGEST_XATTR, attr, sizeof(attr)) == DIGEST_SIZE) { hash = QByteArray(attr, sizeof(attr)); @@ -145,7 +193,10 @@ QByteArray BooksUtil::fileHashAttr(QString aPath) return hash; } -bool BooksUtil::setFileHashAttr(QString aPath, QByteArray aHash) +bool +BooksUtil::setFileHashAttr( + const QString aPath, + const QByteArray aHash) { if (aHash.size() == DIGEST_SIZE) { QByteArray fname(aPath.toLocal8Bit()); @@ -158,7 +209,10 @@ bool BooksUtil::setFileHashAttr(QString aPath, QByteArray aHash) return false; } -QByteArray BooksUtil::computeFileHashAndSetAttr(QString aPath, const HarbourTask* aTask) +QByteArray +BooksUtil::computeFileHashAndSetAttr( + const QString aPath, + const HarbourTask* aTask) { QByteArray hash = computeFileHash(aPath, aTask); if (!hash.isEmpty()) { @@ -167,7 +221,9 @@ QByteArray BooksUtil::computeFileHashAndSetAttr(QString aPath, const HarbourTask return hash; } -bool BooksUtil::isValidFileName(QString aName) +bool +BooksUtil::isValidFileName( + const QString aName) { return !aName.isEmpty() && aName != QStringLiteral(".") && diff --git a/app/src/BooksUtil.h b/app/src/BooksUtil.h index a785b4e..df01038 100644 --- a/app/src/BooksUtil.h +++ b/app/src/BooksUtil.h @@ -1,6 +1,6 @@ /* - * Copyright (C) 2015-2018 Jolla Ltd. - * Copyright (C) 2015-2018 Slava Monich + * Copyright (C) 2015-2022 Jolla Ltd. + * Copyright (C) 2015-2022 Slava Monich * * 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 * are met: * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Jolla Ltd nor the names of its contributors - * may be used to endorse or promote products derived from this - * software without specific prior written permission. + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * 3. Neither the names of the copyright holders nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT @@ -38,21 +38,49 @@ #include "library/Book.h" +#include #include +#include class HarbourTask; +class QQmlEngine; +class QJSEngine; -namespace BooksUtil { - shared_ptr bookFromFile(std::string aPath); - shared_ptr bookFromFile(QString aPath); - bool isValidFileName(QString aName); - QByteArray fileHashAttr(QString aPath); - bool setFileHashAttr(QString aPath, QByteArray aHash); - QByteArray computeFileHash(QString aPath, const HarbourTask* aTask = NULL); - QByteArray computeFileHashAndSetAttr(QString aPath, const HarbourTask* aTask = NULL); -} +class BooksUtil : public QObject +{ + Q_OBJECT + Q_DISABLE_COPY(BooksUtil) + Q_PROPERTY(QString mediaKeyQml READ mediaKeyQml CONSTANT) + Q_PROPERTY(QString permissionsQml READ permissionsQml CONSTANT) + Q_PROPERTY(qreal opacityFaint READ opacityFaint CONSTANT) + Q_PROPERTY(qreal opacityLow READ opacityLow CONSTANT) + Q_PROPERTY(qreal opacityHigh READ opacityHigh CONSTANT) + Q_PROPERTY(qreal opacityOverlay READ opacityOverlay CONSTANT) + class Private; -inline shared_ptr BooksUtil::bookFromFile(QString aPath) - { return BooksUtil::bookFromFile(aPath.toStdString()); } +public: + explicit BooksUtil(QObject* aParent = Q_NULLPTR); + + // Callback for qmlRegisterSingletonType + static QObject* createSingleton(QQmlEngine*, QJSEngine*); + + // Getters + static QString mediaKeyQml(); + static QString permissionsQml(); + static qreal opacityFaint() { return 0.2; } + static qreal opacityLow() { return 0.4; } + static qreal opacityHigh() { return 0.6; } + static qreal opacityOverlay() { return 0.8; } + + // Static utilities + static shared_ptr bookFromFile(const std::string&); + static shared_ptr bookFromFile(const QString aPath) + { return BooksUtil::bookFromFile(aPath.toStdString()); } + static bool isValidFileName(const QString); + static QByteArray fileHashAttr(const QString); + static bool setFileHashAttr(const QString, const QByteArray); + static QByteArray computeFileHash(const QString, const HarbourTask* aTask = NULL); + static QByteArray computeFileHashAndSetAttr(const QString, const HarbourTask* aTask = NULL); +}; #endif // BOOKS_UTIL_H diff --git a/app/src/main.cpp b/app/src/main.cpp index 2e24e8b..2593dce 100644 --- a/app/src/main.cpp +++ b/app/src/main.cpp @@ -50,12 +50,11 @@ #include "BooksCoverWidget.h" #include "BooksTaskQueue.h" #include "BooksHints.h" +#include "BooksUtil.h" #include "HarbourColorEditorModel.h" #include "HarbourDisplayBlanking.h" #include "HarbourDebug.h" -#include "HarbourMediaPlugin.h" -#include "HarbourPolicyPlugin.h" #include "HarbourUtil.h" #include "ZLibrary.h" @@ -107,6 +106,7 @@ Q_DECL_EXPORT int main(int argc, char **argv) BOOKS_QML_REGISTER(BooksPageStack); BOOKS_QML_REGISTER_SINGLETON(HarbourUtil); BOOKS_QML_REGISTER_SINGLETON(BooksHints); + BOOKS_QML_REGISTER_SINGLETON(BooksUtil); BOOKS_QML_REGISTER_UNCREATABLE(BooksSettings); QLocale locale; @@ -139,10 +139,6 @@ Q_DECL_EXPORT int main(int argc, char **argv) QQmlContext* root = view->rootContext(); QQmlEngine* engine = root->engine(); QSharedPointer 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));