From fdca2d959ebddc8bcc81ade7b8c4c61ba7df6496 Mon Sep 17 00:00:00 2001 From: Slava Monich Date: Sat, 13 Nov 2021 03:49:23 +0200 Subject: [PATCH] [app] Register OpenRepos app as a file handler --- app/app.pro | 38 ++++++----- app/openrepos-books.desktop | 26 ++++++++ app/openrepos-books.service | 3 + app/src/BooksDBus.cpp | 122 ++++++++++++++++++++++++++++++++++++ app/src/BooksDBus.h | 56 +++++++++++++++++ app/src/BooksDefs.h | 5 +- app/src/BooksSettings.cpp | 7 +++ app/src/BooksSettings.h | 3 + app/src/main.cpp | 22 +++++-- rpm/openrepos-books.spec | 1 + 10 files changed, 261 insertions(+), 22 deletions(-) create mode 100644 app/openrepos-books.desktop create mode 100644 app/openrepos-books.service create mode 100644 app/src/BooksDBus.cpp create mode 100644 app/src/BooksDBus.h diff --git a/app/app.pro b/app/app.pro index 9f38c2a..6177333 100644 --- a/app/app.pro +++ b/app/app.pro @@ -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 diff --git a/app/openrepos-books.desktop b/app/openrepos-books.desktop new file mode 100644 index 0000000..a880288 --- /dev/null +++ b/app/openrepos-books.desktop @@ -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/ diff --git a/app/openrepos-books.service b/app/openrepos-books.service new file mode 100644 index 0000000..57cf0a3 --- /dev/null +++ b/app/openrepos-books.service @@ -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 diff --git a/app/src/BooksDBus.cpp b/app/src/BooksDBus.cpp new file mode 100644 index 0000000..5700e41 --- /dev/null +++ b/app/src/BooksDBus.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (C) 2021 Jolla Ltd. + * Copyright (C) 2021 Slava Monich + * + * 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 + +// ========================================================================== +// 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(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" diff --git a/app/src/BooksDBus.h b/app/src/BooksDBus.h new file mode 100644 index 0000000..18d780c --- /dev/null +++ b/app/src/BooksDBus.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2021 Jolla Ltd. + * Copyright (C) 2021 Slava Monich + * + * 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 + +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 diff --git a/app/src/BooksDefs.h b/app/src/BooksDefs.h index 41734b9..75bdc62 100644 --- a/app/src/BooksDefs.h +++ b/app/src/BooksDefs.h @@ -37,12 +37,15 @@ #include #ifdef OPENREPOS +# define BOOKS_DBUS_INTERFACE "openrepos.books" # define BOOKS_APP_NAME "openrepos-books" -# define BOOKS_SETTINGS_MENU false +# 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" diff --git a/app/src/BooksSettings.cpp b/app/src/BooksSettings.cpp index 3a06718..b577094 100644 --- a/app/src/BooksSettings.cpp +++ b/app/src/BooksSettings.cpp @@ -837,4 +837,11 @@ BooksSettings::orientation() const return DEFAULT_ORIENTATION; } +void +BooksSettings::setCurrentBookPath(QString aPath) +{ + HDEBUG(aPath); + iPrivate->iCurrentBookPathConf->set(aPath); +} + #include "BooksSettings.moc" diff --git a/app/src/BooksSettings.h b/app/src/BooksSettings.h index 04a4759..f2ad4e4 100644 --- a/app/src/BooksSettings.h +++ b/app/src/BooksSettings.h @@ -144,6 +144,9 @@ public: Orientation orientation() const; +public Q_SLOTS: + void setCurrentBookPath(QString aPath); + Q_SIGNALS: void fontSizeChanged(); void nightModeBrightnessChanged(); diff --git a/app/src/main.cpp b/app/src/main.cpp index c5967c1..de8efb5 100644 --- a/app/src/main.cpp +++ b/app/src/main.cpp @@ -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"); } } diff --git a/rpm/openrepos-books.spec b/rpm/openrepos-books.spec index e87d336..552dc58 100644 --- a/rpm/openrepos-books.spec +++ b/rpm/openrepos-books.spec @@ -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