[app] Register OpenRepos app as a file handler

This commit is contained in:
Slava Monich 2021-11-13 03:49:23 +02:00
parent 7378e3982e
commit fdca2d959e
10 changed files with 261 additions and 22 deletions

View file

@ -51,7 +51,7 @@ LIBS += \
OTHER_FILES += \
icons/harbour-books.svg \
harbour-books.desktop \
*.desktop \
qml/*.qml \
qml/*.js \
qml/images/* \
@ -134,16 +134,6 @@ SOURCES += \
src/ZLApplication.cpp \
src/ZLibrary.cpp
# Some libraries are not allowed in harbour
openrepos {
LIBS += -lexpat -lmagic -ludev
} else {
SOURCES += \
stubs/libexpat.c \
stubs/libmagic.c \
stubs/libudev.c
}
HEADERS += \
src/BooksBook.h \
src/BooksBookModel.h \
@ -174,6 +164,23 @@ HEADERS += \
src/BooksTypes.h \
src/BooksUtil.h
# Some libraries are not allowed in harbour
openrepos {
LIBS += -lexpat -lmagic -ludev
} else {
SOURCES += \
stubs/libexpat.c \
stubs/libmagic.c \
stubs/libudev.c
}
# D-Bus handler is only used in OpenRepos build
openrepos {
QT += dbus
HEADERS += src/BooksDBus.h
SOURCES += src/BooksDBus.cpp
}
# harbour-lib
HEADERS += \
@ -245,10 +252,11 @@ settings_images.files = settings/images/*.svg
settings_images.path = /usr/share/$${TARGET}/settings/images/
INSTALLS += settings_images
# Desktop file
equals(PREFIX, "openrepos") {
desktop.extra = sed s/harbour/openrepos/g harbour-$${NAME}.desktop > $${TARGET}.desktop
desktop.CONFIG += no_check_exist
# File handler
openrepos {
service.files = $${TARGET}.service
service.path = /usr/share/dbus-1/services/
INSTALLS += service
}
# Translations

View file

@ -0,0 +1,26 @@
[Desktop Entry]
Type=Application
Name=Books
Name[de]=Bücher
Name[es]=Libros
Name[fi]=Kirjat
Name[hu]=Könyvek
Name[nl]=Boeken
Name[pt_BR]=Livros
Name[ru]=Книги
Name[sv]=Böcker
Comment=E-Book Reader
Icon=openrepos-books
Exec=openrepos-books %f
MimeType=application/epub+zip;application/x-fictionbook+xml;
X-Nemo-Application-Type=silica-qt5
X-Maemo-Service=openrepos.books
X-Maemo-Object-Path=/
X-Maemo-Method=openrepos.books.Open
[X-Sailjail]
Sandboxing=Disabled
[X-HarbourBackup]
BackupPathList=.local/share/openrepos-books/:Documents/Books/
BackupConfigList=/apps/openrepos-books/

View file

@ -0,0 +1,3 @@
[D-BUS Service]
Name=openrepos.books
Exec=/usr/bin/invoker --type=silica-qt5 --desktop-file=openrepos-books.desktop -n -d 5 /usr/bin/openrepos-books

122
app/src/BooksDBus.cpp Normal file
View file

@ -0,0 +1,122 @@
/*
* Copyright (C) 2021 Jolla Ltd.
* Copyright (C) 2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 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
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "BooksDefs.h"
#include "BooksDBus.h"
#include "HarbourDebug.h"
#include <QDBusConnection>
// ==========================================================================
// BooksDBus::Adaptor
// ==========================================================================
class BooksDBus::Adaptor : public QDBusAbstractAdaptor
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", BOOKS_DBUS_INTERFACE)
public:
Adaptor(QObject* aParent);
void open(QString aPathOrUrl);
public Q_SLOTS:
void Open(QString path);
void Open(QStringList args);
};
BooksDBus::Adaptor::Adaptor(QObject* aParent) :
QDBusAbstractAdaptor(aParent)
{
}
void BooksDBus::Adaptor::open(QString aPathOrUrl)
{
BooksDBus* publicObject = qobject_cast<BooksDBus*>(parent());
if (!aPathOrUrl.isEmpty()) {
static const QString fileUrlPrefix("file://");
if (aPathOrUrl.startsWith(fileUrlPrefix)) {
const QString path(aPathOrUrl.right(aPathOrUrl.length() -
fileUrlPrefix.length()));
if (!path.isEmpty()) {
HDEBUG(qPrintable(path));
Q_EMIT publicObject->openBook(path);
}
} else {
// Assume it's a path
HDEBUG(qPrintable(aPathOrUrl));
Q_EMIT publicObject->openBook(aPathOrUrl);
}
}
Q_EMIT publicObject->activate();
}
void BooksDBus::Adaptor::Open(QString aArg)
{
HDEBUG(aArg);
open(aArg);
}
void BooksDBus::Adaptor::Open(QStringList aArgs)
{
HDEBUG(aArgs);
open(aArgs.isEmpty() ? QString() : aArgs.at(0));
}
// ==========================================================================
// BooksDBus
// ==========================================================================
BooksDBus::BooksDBus(QObject* aParent) :
QObject(aParent),
iAdaptor(new Adaptor(this))
{
}
BooksDBus* BooksDBus::create(QObject* aParent)
{
BooksDBus* handler = new BooksDBus(aParent);
QDBusConnection sessionBus(QDBusConnection::sessionBus());
if (sessionBus.registerObject("/", handler) &&
sessionBus.registerService(BOOKS_DBUS_SERVICE)) {
HDEBUG("Registered D-Bus handler");
return handler;
} else {
HDEBUG("Failed to registered D-Bus handler");
delete handler;
return Q_NULLPTR;
}
}
#include "BooksDBus.moc"

56
app/src/BooksDBus.h Normal file
View file

@ -0,0 +1,56 @@
/*
* Copyright (C) 2021 Jolla Ltd.
* Copyright (C) 2021 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of the BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 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
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef BOOKS_DBUS_H
#define BOOKS_DBUS_H
#include <QDBusAbstractAdaptor>
class BooksDBus : public QObject
{
Q_OBJECT
BooksDBus(QObject* aParent);
public:
static BooksDBus* create(QObject* aParent);
Q_SIGNALS:
void openBook(QString aPath);
void activate();
private:
class Adaptor;
Adaptor* iAdaptor;
};
#endif // BOOKS_DBUS_H

View file

@ -37,12 +37,15 @@
#include <QString>
#ifdef OPENREPOS
# define BOOKS_DBUS_INTERFACE "openrepos.books"
# define BOOKS_APP_NAME "openrepos-books"
# define BOOKS_SETTINGS_MENU false
#else
# define BOOKS_APP_NAME "harbour-books"
# define BOOKS_SETTINGS_MENU true
#endif
#define BOOKS_DBUS_SERVICE BOOKS_DBUS_INTERFACE
#define BOOKS_DCONF_ROOT "/apps/" BOOKS_APP_NAME "/"
#define BOOKS_DATA_ROOT "usr/share/" BOOKS_APP_NAME
#define BOOKS_QML_DIR BOOKS_DATA_ROOT "/qml"

View file

@ -837,4 +837,11 @@ BooksSettings::orientation() const
return DEFAULT_ORIENTATION;
}
void
BooksSettings::setCurrentBookPath(QString aPath)
{
HDEBUG(aPath);
iPrivate->iCurrentBookPathConf->set(aPath);
}
#include "BooksSettings.moc"

View file

@ -144,6 +144,9 @@ public:
Orientation orientation() const;
public Q_SLOTS:
void setCurrentBookPath(QString aPath);
Q_SIGNALS:
void fontSizeChanged();
void nightModeBrightnessChanged();

View file

@ -74,6 +74,10 @@
# define TASK_QUEUE_TIMEOUT (10000)
#endif
#ifdef OPENREPOS
# include "BooksDBus.h"
#endif
Q_DECL_EXPORT int main(int argc, char **argv)
{
QGuiApplication* app = SailfishApp::application(argc, argv);
@ -137,15 +141,21 @@ Q_DECL_EXPORT int main(int argc, char **argv)
QVariant::fromValue(BOOKS_SETTINGS_MENU));
root->setContextProperty("Settings", settings.data());
#ifdef BOOKS_DBUS_INTERFACE
BooksDBus* dbusHandler = BooksDBus::create(app);
if (dbusHandler) {
view->connect(dbusHandler, SIGNAL(activate()), SLOT(raise()));
settings->connect(dbusHandler, SIGNAL(openBook(QString)),
SLOT(setCurrentBookPath(QString)));
}
#endif
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();
}
settings->setCurrentBookPath(file);
} else {
HWARN(qPrintable(file) << "doesn't exist");
}
}

View file

@ -55,6 +55,7 @@ desktop-file-install --delete-original \
%{_datadir}/icons/hicolor/*/apps/%{name}.png
%{_datadir}/translations/%{name}*.qm
%{_datadir}/jolla-settings/entries/%{name}.json
%{_datadir}/dbus-1/services/%{name}.service
%check
make -C test test