Skip to content

Commit

Permalink
Make it DBusActivatable
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Feb 19, 2023
1 parent e4038db commit 29c5bbf
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 72 deletions.
5 changes: 3 additions & 2 deletions harbour-seaprint.desktop
Expand Up @@ -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
5 changes: 4 additions & 1 deletion harbour-seaprint.pro
Expand Up @@ -26,14 +26,15 @@ 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
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 \
Expand Down Expand Up @@ -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 \
Expand All @@ -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 \
Expand Down
30 changes: 29 additions & 1 deletion qml/harbour-seaprint.qml
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions qml/pages/BusyPage.qml
Expand Up @@ -3,6 +3,8 @@ import Sailfish.Silica 1.0
import "utils.js" as Utils

Page {
property bool busyPage: true

allowedOrientations: Orientation.All

Component.onCompleted: {
Expand Down
54 changes: 17 additions & 37 deletions qml/pages/FirstPage.qml
Expand Up @@ -12,9 +12,6 @@ Page {
id: page
allowedOrientations: Orientation.All

property string selectedFile: ""
property string selectedFileType

Connections {
target: wifi
onConnectedChanged: {
Expand Down Expand Up @@ -60,11 +57,6 @@ Page {

Component.onCompleted: {
IppDiscovery.discover();
if(selectedFile != "")
{
var type = Mimer.get_type(selectedFile);
selectedFileType = type;
}
}

property bool nagged: false
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand All @@ -199,7 +191,7 @@ Page {
return;
}
debugCountReset.restart();
if(selectedFile == "")
if(appWin.selectedFile == "")
{
noFileSelected();
}
Expand All @@ -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()})
}
}

Expand Down Expand Up @@ -250,21 +242,21 @@ 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 : "")
}

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
}
Expand All @@ -285,15 +277,15 @@ 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 {
height: Theme.itemSizeExtraSmall/2
width: Theme.itemSizeExtraSmall/2
visible: supported_formats.postscript
highlightColor: "red"
highlighted: !(selectedFile == "" || canPrint)
highlighted: !(appWin.selectedFile == "" || canPrint)
source: "image://theme/icon-m-file-other"

}
Expand All @@ -302,31 +294,31 @@ 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 {
height: Theme.itemSizeExtraSmall/2
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 {
height: Theme.itemSizeExtraSmall/2
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 {
height: Theme.itemSizeExtraSmall/2
width: Theme.itemSizeExtraSmall/2
visible: supported_formats.images
highlightColor: "red"
highlighted: !(selectedFile == "" || canPrint)
highlighted: !(appWin.selectedFile == "" || canPrint)
source: "image://theme/icon-m-file-image"
}
}
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -478,8 +459,7 @@ Page {
allowedOrientations: Orientation.All

onSelectedContentPropertiesChanged: {
page.selectedFile = selectedContentProperties.filePath
page.selectedFileType = Mimer.get_type(selectedContentProperties.filePath)
appWin.openFile(selectedContentProperties.filePath);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/dbusadaptor.cpp
@@ -0,0 +1,26 @@
#include "dbusadaptor.h"

#include <QtQuick>

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");
}
26 changes: 26 additions & 0 deletions src/dbusadaptor.h
@@ -0,0 +1,26 @@
#ifndef DBUSADAPTOR_H
#define DBUSADAPTOR_H

#include <QObject>
#include <QtDBus/QDBusAbstractAdaptor>
#include <QQuickView>

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
37 changes: 35 additions & 2 deletions src/harbour-seaprint.cpp
@@ -1,4 +1,6 @@
#include <QtQuick>
#include <QVariant>
#include <QDBusConnection>

#include <sailfishapp.h>
#include <src/ippdiscovery.h>
Expand All @@ -8,6 +10,9 @@
#include <src/convertchecker.h>
#include <src/settings.h>
#include <src/rangelistchecker.h>
#include <src/dbusadaptor.h>
#include "argget.h"
#include <iostream>

Q_DECLARE_METATYPE(CURLcode)
Q_DECLARE_METATYPE(Bytestream)
Expand All @@ -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<bool> 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<CURLcode>();
qRegisterMetaType<Bytestream>();
qRegisterMetaType<PrintParameters>();
Expand All @@ -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();
}

0 comments on commit 29c5bbf

Please sign in to comment.