[app] A better way to use Permissions and MediaKey

It has a better chance to survive a Qt upgrade
This commit is contained in:
Slava Monich 2022-10-10 18:48:25 +03:00
parent 6c75817c55
commit fb8841294e
5 changed files with 162 additions and 101 deletions

View file

@ -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

View file

@ -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
}
}
}

View file

@ -1,6 +1,6 @@
/*
* Copyright (C) 2015-2018 Jolla Ltd.
* Copyright (C) 2015-2018 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2015-2022 Jolla Ltd.
* Copyright (C) 2015-2022 Slava Monich <slava.monich@jolla.com>
*
* 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<Book> 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<BooksUtil>
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<Book>
BooksUtil::bookFromFile(
const std::string& aPath)
{
shared_ptr<Book> book;
const ZLFile file(aPath);
@ -91,7 +134,10 @@ shared_ptr<Book> 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(".") &&

View file

@ -1,6 +1,6 @@
/*
* Copyright (C) 2015-2018 Jolla Ltd.
* Copyright (C) 2015-2018 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2015-2022 Jolla Ltd.
* Copyright (C) 2015-2022 Slava Monich <slava.monich@jolla.com>
*
* 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 <QByteArray>
#include <QString>
#include <QObject>
class HarbourTask;
class QQmlEngine;
class QJSEngine;
namespace BooksUtil {
shared_ptr<Book> bookFromFile(std::string aPath);
shared_ptr<Book> 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<Book> BooksUtil::bookFromFile(QString aPath)
{ return BooksUtil::bookFromFile(aPath.toStdString()); }
public:
explicit BooksUtil(QObject* aParent = Q_NULLPTR);
// Callback for qmlRegisterSingletonType<BooksUtil>
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<Book> bookFromFile(const std::string&);
static shared_ptr<Book> 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

View file

@ -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<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));