From 29c5bbf62bd08410cb3c8b7d1c3b132a9ce2e79c Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Fri, 17 Feb 2023 21:29:12 +0100 Subject: [PATCH] Make it DBusActivatable --- harbour-seaprint.desktop | 5 ++- harbour-seaprint.pro | 5 ++- qml/harbour-seaprint.qml | 30 +++++++++++++- qml/pages/BusyPage.qml | 2 + qml/pages/FirstPage.qml | 54 ++++++++------------------ src/dbusadaptor.cpp | 26 +++++++++++++ src/dbusadaptor.h | 26 +++++++++++++ src/harbour-seaprint.cpp | 37 +++++++++++++++++- src/mimer.cpp | 9 ++++- translations/harbour-seaprint-de.ts | 12 ++++-- translations/harbour-seaprint-es.ts | 12 ++++-- translations/harbour-seaprint-fr.ts | 12 ++++-- translations/harbour-seaprint-nl.ts | 12 ++++-- translations/harbour-seaprint-pl.ts | 12 ++++-- translations/harbour-seaprint-zh_CN.ts | 12 ++++-- translations/harbour-seaprint.ts | 12 ++++-- 16 files changed, 206 insertions(+), 72 deletions(-) create mode 100644 src/dbusadaptor.cpp create mode 100644 src/dbusadaptor.h diff --git a/harbour-seaprint.desktop b/harbour-seaprint.desktop index be54be7..85b0f21 100644 --- a/harbour-seaprint.desktop +++ b/harbour-seaprint.desktop @@ -2,10 +2,11 @@ Type=Application Icon=harbour-seaprint X-Nemo-Application-Type=silica-qt5 -Exec=harbour-seaprint +Exec=/usr/bin/harbour-seaprint +X-DBusActivatable=true Name=SeaPrint [X-Sailjail] Permissions=Internet;Pictures;Documents;Downloads;RemovableMedia;MediaIndexing;Compatibility OrganizationName=net.attah ApplicationName=seaprint - +ExecDBus=/usr/bin/harbour-seaprint --prestart diff --git a/harbour-seaprint.pro b/harbour-seaprint.pro index a2e4c58..f5a4757 100644 --- a/harbour-seaprint.pro +++ b/harbour-seaprint.pro @@ -26,7 +26,7 @@ INSTALLS += i18n system(lrelease $$PWD/translations/*.ts) CONFIG += sailfishapp -QT += svg +QT += svg dbus PKGCONFIG += mlite5 libcurl poppler-glib glib-2.0 cairo libjpeg LIBS += -lcurl -lglib-2.0 -lgobject-2.0 -ldl DEFINES += MADNESS=1 @@ -34,6 +34,7 @@ DEFINES += PDF_CREATOR='\\"SeaPrint\ $$VERSION\\"' DEFINES += SEAPRINT_VERSION='\\"$$VERSION\\"' SOURCES += src/harbour-seaprint.cpp \ + src/dbusadaptor.cpp \ src/rangelistchecker.cpp \ src/convertchecker.cpp \ src/curlrequester.cpp \ @@ -82,6 +83,7 @@ TRANSLATIONS += translations/harbour-seaprint-de.ts \ translations/harbour-seaprint-pl.ts HEADERS += \ + src/dbusadaptor.h \ src/rangelistchecker.h \ src/convertchecker.h \ src/curlrequester.h \ @@ -101,6 +103,7 @@ HEADERS += \ ppm2pwg/UrfPgHdr.codable \ ppm2pwg/bytestream/bytestream.h \ ppm2pwg/bytestream/codable.h \ + ppm2pwg/argget.h \ src/overrider.h \ src/papersizes.h \ src/printerworker.h \ diff --git a/qml/harbour-seaprint.qml b/qml/harbour-seaprint.qml index 1976b6c..c63a67e 100644 --- a/qml/harbour-seaprint.qml +++ b/qml/harbour-seaprint.qml @@ -13,9 +13,37 @@ ApplicationWindow { id: appWin + property Page _mainPage + property bool expectCalligra: true - initialPage: Component { FirstPage { selectedFile: Qt.application.arguments[1] ? Qt.application.arguments[1] : "" } } + + function openFile(file) { + selectedFile = file + selectedFileType = Mimer.get_type(selectedFile); + if(selectedFileType == "") + { + selectedFile = "" + notifier.notify(qsTr("Unsupported document format")) + } + if(pageStack.currentPage.busyPage) + { + notifier.notify(qsTr("New file selected")) + } + else + { + pageStack.pop(appWin._mainPage, PageStackAction.Immediate) + } + } + + property string selectedFile: "" + property string selectedFileType + + initialPage: Component { + FirstPage { + Component.onCompleted: appWin._mainPage = this + } + } cover: Qt.resolvedUrl("cover/CoverPage.qml") allowedOrientations: Orientation.All diff --git a/qml/pages/BusyPage.qml b/qml/pages/BusyPage.qml index 0ac5252..c178ad8 100644 --- a/qml/pages/BusyPage.qml +++ b/qml/pages/BusyPage.qml @@ -3,6 +3,8 @@ import Sailfish.Silica 1.0 import "utils.js" as Utils Page { + property bool busyPage: true + allowedOrientations: Orientation.All Component.onCompleted: { diff --git a/qml/pages/FirstPage.qml b/qml/pages/FirstPage.qml index d20e5dc..5365f7d 100644 --- a/qml/pages/FirstPage.qml +++ b/qml/pages/FirstPage.qml @@ -12,9 +12,6 @@ Page { id: page allowedOrientations: Orientation.All - property string selectedFile: "" - property string selectedFileType - Connections { target: wifi onConnectedChanged: { @@ -60,11 +57,6 @@ Page { Component.onCompleted: { IppDiscovery.discover(); - if(selectedFile != "") - { - var type = Mimer.get_type(selectedFile); - selectedFileType = type; - } } property bool nagged: false @@ -143,7 +135,7 @@ Page { property string name: Utils.unknownForEmptyString(printer.attrs["printer-name"].value) property var supported_formats: Utils.supported_formats(printer, considerAdditionalFormatsSetting.value) - property bool canPrint: supported_formats.mimetypes.indexOf(selectedFileType) != -1 + property bool canPrint: supported_formats.mimetypes.indexOf(appWin.selectedFileType) != -1 Connections { target: printer @@ -182,7 +174,7 @@ Page { { if(printer.attrs.hasOwnProperty("printer-uuid")) { - return JSON.parse(db.getJobSettings(printer.attrs["printer-uuid"].value, selectedFileType)); + return JSON.parse(db.getJobSettings(printer.attrs["printer-uuid"].value, appWin.selectedFileType)); } else { @@ -199,7 +191,7 @@ Page { return; } debugCountReset.restart(); - if(selectedFile == "") + if(appWin.selectedFile == "") { noFileSelected(); } @@ -209,7 +201,7 @@ Page { } else { - pageStack.push(Qt.resolvedUrl("PrinterPage.qml"), {printer: printer, selectedFile: selectedFile, jobParams: maybeGetParams()}) + pageStack.push(Qt.resolvedUrl("PrinterPage.qml"), {printer: printer, selectedFile: appWin.selectedFile, jobParams: maybeGetParams()}) } } @@ -250,13 +242,13 @@ Page { Label { id: name_label - color: canPrint || selectedFile == "" ? Theme.primaryColor : Theme.secondaryColor + color: canPrint || appWin.selectedFile == "" ? Theme.primaryColor : Theme.secondaryColor text: name } Label { id: mm_label - color: canPrint || selectedFile == "" ? Theme.primaryColor : Theme.secondaryColor + color: canPrint || appWin.selectedFile == "" ? Theme.primaryColor : Theme.secondaryColor font.pixelSize: Theme.fontSizeExtraSmall text: Utils.unknownForEmptyString(printer.attrs["printer-make-and-model"].value) + (Utils.existsAndNotEmpty("printer-location", printer) ? " • "+printer.attrs["printer-location"].value : "") @@ -264,7 +256,7 @@ Page { Label { id: uri_label - color: canPrint || selectedFile == "" ? Theme.highlightColor : Theme.secondaryColor + color: canPrint || appWin.selectedFile == "" ? Theme.highlightColor : Theme.secondaryColor font.pixelSize: Theme.fontSizeTiny text: printer.url } @@ -285,7 +277,7 @@ Page { width: Theme.itemSizeExtraSmall/2 visible: supported_formats.pdf highlightColor: "red" - highlighted: !(selectedFile == "" || canPrint) + highlighted: !(appWin.selectedFile == "" || canPrint) source: "image://theme/icon-m-file-pdf" } HighlightImage { @@ -293,7 +285,7 @@ Page { width: Theme.itemSizeExtraSmall/2 visible: supported_formats.postscript highlightColor: "red" - highlighted: !(selectedFile == "" || canPrint) + highlighted: !(appWin.selectedFile == "" || canPrint) source: "image://theme/icon-m-file-other" } @@ -302,7 +294,7 @@ Page { width: Theme.itemSizeExtraSmall/2 visible: supported_formats.plaintext highlightColor: "red" - highlighted: !(selectedFile == "" || canPrint) + highlighted: !(appWin.selectedFile == "" || canPrint) source: "image://theme/icon-m-file-document" } HighlightImage { @@ -310,7 +302,7 @@ Page { width: Theme.itemSizeExtraSmall/2 visible: supported_formats.office highlightColor: "red" - highlighted: !(selectedFile == "" || canPrint) + highlighted: !(appWin.selectedFile == "" || canPrint) source: "image://theme/icon-m-file-formatted" } HighlightImage { @@ -318,7 +310,7 @@ Page { width: Theme.itemSizeExtraSmall/2 visible: supported_formats.office highlightColor: "red" - highlighted: !(selectedFile == "" || canPrint) + highlighted: !(appWin.selectedFile == "" || canPrint) source: "image://theme/icon-m-file-presentation" } HighlightImage { @@ -326,7 +318,7 @@ Page { width: Theme.itemSizeExtraSmall/2 visible: supported_formats.images highlightColor: "red" - highlighted: !(selectedFile == "" || canPrint) + highlighted: !(appWin.selectedFile == "" || canPrint) source: "image://theme/icon-m-file-image" } } @@ -381,7 +373,7 @@ Page { Row { id: warningRow anchors.horizontalCenter: parent.horizontalCenter - visible: Mimer.isOffice(page.selectedFileType) + visible: Mimer.isOffice(appWin.selectedFileType) HighlightImage { source: "image://theme/icon-s-warning" @@ -404,7 +396,7 @@ Page { horizontalAlignment: contentWidth > width ? Text.AlignRight : Text.AlignHCenter truncationMode: TruncationMode.Fade - text: selectedFile != "" ? selectedFile : qsTr("No file selected") + text: appWin.selectedFile != "" ? appWin.selectedFile : qsTr("No file selected") SequentialAnimation { id: noFileSelectedAnimation @@ -457,18 +449,7 @@ Page { title: qsTr("Choose file") onSelectedContentPropertiesChanged: { - var mimeType = Mimer.get_type(selectedContentProperties.filePath) - if(mimeType == Mimer.PDF || mimeType == Mimer.Postscript || mimeType == Mimer.Plaintext || Mimer.isOffice(mimeType)) - { - page.selectedFile = selectedContentProperties.filePath - page.selectedFileType = mimeType - } - else - { - notifier.notify(qsTr("Unsupported document format")) - page.selectedFile = "" - page.selectedFileType = "" - } + appWin.openFile(selectedContentProperties.filePath); } } } @@ -478,8 +459,7 @@ Page { allowedOrientations: Orientation.All onSelectedContentPropertiesChanged: { - page.selectedFile = selectedContentProperties.filePath - page.selectedFileType = Mimer.get_type(selectedContentProperties.filePath) + appWin.openFile(selectedContentProperties.filePath); } } } diff --git a/src/dbusadaptor.cpp b/src/dbusadaptor.cpp new file mode 100644 index 0000000..e4d5564 --- /dev/null +++ b/src/dbusadaptor.cpp @@ -0,0 +1,26 @@ +#include "dbusadaptor.h" + +#include + +DBusAdaptor::DBusAdaptor(QQuickView *view) + : QDBusAbstractAdaptor(view) + , _view(view) +{ +} + +DBusAdaptor::~DBusAdaptor() +{ +} + +void DBusAdaptor::Open(const QStringList &uris, const QVariantMap &) +{ + if (!uris.isEmpty()) { + QMetaObject::invokeMethod(_view->rootObject(), "openFile", Q_ARG(QVariant, uris.at(0))); + } + QMetaObject::invokeMethod(_view->rootObject(), "activate"); +} + +void DBusAdaptor::Activate(const QVariantMap&) +{ + QMetaObject::invokeMethod(_view->rootObject(), "activate"); +} diff --git a/src/dbusadaptor.h b/src/dbusadaptor.h new file mode 100644 index 0000000..1cb708f --- /dev/null +++ b/src/dbusadaptor.h @@ -0,0 +1,26 @@ +#ifndef DBUSADAPTOR_H +#define DBUSADAPTOR_H + +#include +#include +#include + +class DBusAdaptor : public QDBusAbstractAdaptor +{ + Q_OBJECT + Q_CLASSINFO("D-Bus Interface", "org.freedesktop.Application") + +public: + DBusAdaptor(QQuickView *view); + ~DBusAdaptor(); + +public Q_SLOTS: + Q_NOREPLY void Open(const QStringList &uris, const QVariantMap &platformData); + Q_NOREPLY void Activate(const QVariantMap &platformData); + +private: + QQuickView* _view; + +}; + +#endif // DBUSADAPTOR_H diff --git a/src/harbour-seaprint.cpp b/src/harbour-seaprint.cpp index e9b24e5..ca7766d 100644 --- a/src/harbour-seaprint.cpp +++ b/src/harbour-seaprint.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include #include @@ -8,6 +10,9 @@ #include #include #include +#include +#include "argget.h" +#include Q_DECLARE_METATYPE(CURLcode) Q_DECLARE_METATYPE(Bytestream) @@ -26,6 +31,17 @@ static QObject* singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngi int main(int argc, char *argv[]) { + bool prestart = false; + std::string FileName; + SwitchArg pretsartOpt(prestart, {"--prestart"}, "Run prestart"); + + PosArg fileArg(FileName, "File to print", true); + ArgGet args({&pretsartOpt}, {&fileArg}); + if(!args.get_args(argc, argv)) + { + return 1; + } + qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType(); @@ -52,8 +68,25 @@ int main(int argc, char *argv[]) QQuickView* view = SailfishApp::createView(); view->engine()->addImportPath(SailfishApp::pathTo("qml/pages").toString()); - view->setSource(SailfishApp::pathToMainQml()); - view->show(); + + DBusAdaptor dbus(view); + + if (!QDBusConnection::sessionBus().registerObject("/net/attah/seaprint", view)) + qWarning() << "Could not register /net/attah/seaprint D-Bus object."; + + if (!QDBusConnection::sessionBus().registerService("net.attah.seaprint")) + qWarning() << "Could not register net.attah.seaprint D-Bus service."; + + if(!FileName.empty()) + { + qDebug() << "Opening" << FileName.c_str(); + QVariant fileNameVariant(FileName.c_str()); + QMetaObject::invokeMethod(view->rootObject(), "openFile", Q_ARG(QVariant, fileNameVariant)); + } + if(!prestart) + { + view->show(); + } return app->exec(); } diff --git a/src/mimer.cpp b/src/mimer.cpp index a8bf915..a216b93 100644 --- a/src/mimer.cpp +++ b/src/mimer.cpp @@ -55,5 +55,12 @@ Mimer* Mimer::instance() QString Mimer::get_type(QString filename) { QString type = _db.mimeTypeForFile(filename).name(); qDebug() << "MimeType:" << type; - return type; + if(type == PDF || type == Postscript || type == Plaintext || isImage(type) || isOffice(type)) + { + return type; + } + else + { + return ""; + } } diff --git a/translations/harbour-seaprint-de.ts b/translations/harbour-seaprint-de.ts index 0fb8218..fc53b3d 100644 --- a/translations/harbour-seaprint-de.ts +++ b/translations/harbour-seaprint-de.ts @@ -276,10 +276,6 @@ Not on WiFi Nicht mit einem WLAN verbunden - - Unsupported document format - Dieses Dokumentformat wird nicht unterstützt - This format may not render correctly Dieses Format wird möglicherweise nicht richtig dargestellt @@ -623,6 +619,14 @@ auf diesem Drucker SVGs SVGs + + Unsupported document format + Dieses Dokumentformat wird nicht unterstützt + + + New file selected + + strings diff --git a/translations/harbour-seaprint-es.ts b/translations/harbour-seaprint-es.ts index cad8b28..eca6272 100644 --- a/translations/harbour-seaprint-es.ts +++ b/translations/harbour-seaprint-es.ts @@ -276,10 +276,6 @@ Not on WiFi No hay WiFi - - Unsupported document format - Formato de documento no soportado - This format may not render correctly Este formato puede no renderizarse correctamente @@ -622,6 +618,14 @@ SVGs + + Unsupported document format + Formato de documento no soportado + + + New file selected + + strings diff --git a/translations/harbour-seaprint-fr.ts b/translations/harbour-seaprint-fr.ts index eb16333..e4c1fa3 100644 --- a/translations/harbour-seaprint-fr.ts +++ b/translations/harbour-seaprint-fr.ts @@ -276,10 +276,6 @@ Not on WiFi Pas en WiFi - - Unsupported document format - Format de document non supporté - This format may not render correctly Ce format peut ne pas être rendu correctement @@ -623,6 +619,14 @@ sur cette imprimante SVGs + + Unsupported document format + Format de document non supporté + + + New file selected + + strings diff --git a/translations/harbour-seaprint-nl.ts b/translations/harbour-seaprint-nl.ts index 1f92e08..9fed467 100644 --- a/translations/harbour-seaprint-nl.ts +++ b/translations/harbour-seaprint-nl.ts @@ -276,10 +276,6 @@ Not on WiFi Niet met WiFi netwerk verbonden - - Unsupported document format - Niet-ondersteunde documentindeling - This format may not render correctly Dit formaat wordt mogelijk niet correct weergegeven @@ -622,6 +618,14 @@ SVGs + + Unsupported document format + Niet-ondersteunde documentindeling + + + New file selected + + strings diff --git a/translations/harbour-seaprint-pl.ts b/translations/harbour-seaprint-pl.ts index 6d28d24..c572f63 100644 --- a/translations/harbour-seaprint-pl.ts +++ b/translations/harbour-seaprint-pl.ts @@ -276,10 +276,6 @@ Not on WiFi Brak połączenia WiFi - - Unsupported document format - Nieobsługiwany rodzaj dokumentu - This format may not render correctly Ten format może zostać przetworzony niepoprawnie @@ -622,6 +618,14 @@ SVGs SVG + + Unsupported document format + Nieobsługiwany rodzaj dokumentu + + + New file selected + + strings diff --git a/translations/harbour-seaprint-zh_CN.ts b/translations/harbour-seaprint-zh_CN.ts index 7412e97..ea9a2b3 100644 --- a/translations/harbour-seaprint-zh_CN.ts +++ b/translations/harbour-seaprint-zh_CN.ts @@ -276,10 +276,6 @@ Not on WiFi 未连接WiFi - - Unsupported document format - 不受支持的格式 - This format may not render correctly 格式可能无法正确呈现 @@ -622,6 +618,14 @@ SVGs + + Unsupported document format + 不受支持的格式 + + + New file selected + + strings diff --git a/translations/harbour-seaprint.ts b/translations/harbour-seaprint.ts index 6793b23..673d532 100644 --- a/translations/harbour-seaprint.ts +++ b/translations/harbour-seaprint.ts @@ -276,10 +276,6 @@ Not on WiFi - - Unsupported document format - - This format may not render correctly @@ -622,6 +618,14 @@ plaintext + + Unsupported document format + + + + New file selected + + strings