Push UnifiedPush come harbour-fmd (0.16.0)

Connector D-Bus org.unifiedpush.Connector.harbour-grumpydove: possiede il nome,
esporta NewEndpoint/Message/Unregistered (Q_CLASSINFO sulla classe, non su un
adaptor: con un adaptor l'interfaccia non viene esportata), si registra col
distributore FogHorn in asincrono e salva token ed endpoint.

Il risveglio si dirama per ambiente (GRUMPYDOVE_PUSH_WAKE=1) prima di creare la
GUI: nessuna finestra, motore in un thread, checkMail, notifica via
org.freedesktop.Notifications, uscita con tetto a 60 s.
preparaPercorsiMotore() sta in un posto solo, usata da app e risveglio.
This commit is contained in:
Carlo Baratto committed 2026-11-17 12:14:23 +01:00
1 parent d7549cedd0
commit d9aa530ffd
8 files changed
+323 -35

No files matched your search

+22 -32
View File
@@ -11,6 +11,7 @@
#include "enginebridge.h"
#include "accountsettings.h"
#include "pushservice.h"
// gd_engine_run: il main di GrumpyDove, esportato da libgrumpydove.so. Entra
// nel motore, che a sua volta chiama gd_run (enginecallbacks.cpp) e da lì il
@@ -20,44 +21,26 @@ extern "C" int gd_engine_run(void);
Q_DECL_EXPORT int main(int argc, char *argv[])
{
// Il risveglio da push si dirama PRIMA di creare la GUI: una push non deve
// poter aprire una finestra. Il desktop file passa questa variabile
// perché SailJail confronta la riga di comando coi suoi template e un
// argomento in più farebbe fallire l'attivazione.
if (qgetenv("GRUMPYDOVE_PUSH_WAKE") == QByteArrayLiteral("1"))
return risveglioPush(argc, argv);
QGuiApplication *app = SailfishApp::application(argc, argv);
app->setOrganizationName(QStringLiteral("harbour"));
app->setApplicationName(QStringLiteral("grumpydove"));
app->setApplicationVersion(QStringLiteral("0.15.0"));
app->setApplicationVersion(QStringLiteral("0.16.0"));
qDebug() << "harbour-grumpydove v0.15.0 build" << __DATE__ << __TIME__;
qDebug() << "harbour-grumpydove v0.16.0 build" << __DATE__ << __TIME__;
// La sandbox di Sailfish (SailJail) non lascia vedere ~/Mail, ma il motore
// legge il percorso del Maildir da GRUMPYDOVE_MAIL: si punta alla cartella
// dati dell'app, che la sandbox rende visibile e persistente. NON si tocca
// HOME: e' anche il posto dove Qt cerca la configurazione, e cambiarla
// faceva perdere il tema del telefono (colori di default).
const QString mailDir = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation)
+ QStringLiteral("/Mail");
QDir().mkpath(mailDir);
qputenv("GRUMPYDOVE_MAIL", QFile::encodeName(mailDir));
// Anche il log del motore va dove la sandbox lascia scrivere, altrimenti
// non resta traccia di niente.
const QString logDir = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation)
+ QStringLiteral("/log");
QDir().mkpath(logDir);
qputenv("GRUMPYDOVE_LOG", QFile::encodeName(logDir));
// E i contatti della rubrica: anche quelli andrebbero sotto HOME.
const QString booksDir = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation)
+ QStringLiteral("/books");
QDir().mkpath(booksDir);
qputenv("GRUMPYDOVE_BOOKS", QFile::encodeName(booksDir));
// E le liste dei filtri che imparano.
const QString filingDir = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation)
+ QStringLiteral("/filing");
QDir().mkpath(filingDir);
qputenv("GRUMPYDOVE_FILING", QFile::encodeName(filingDir));
qDebug() << "Maildir del motore:" << mailDir;
// Maildir, log, rubrica e liste dei filtri nella cartella dati dell'app:
// la sandbox non lascia vedere ~/Mail né scrivere sotto HOME, e cambiare
// HOME faceva perdere il tema del telefono.
preparaPercorsiMotore();
qDebug() << "Maildir del motore:" << qgetenv("GRUMPYDOVE_MAIL");
QTranslator translator;
if (translator.load(QLocale(), QStringLiteral("harbour-grumpydove"),
@@ -70,9 +53,16 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
AccountSettings account;
bridge.setSettings(&account);
// UnifiedPush: l'app possiede il nome del connector e si registra col
// distributore. Se FogHorn non c'è, lo dice nel log e la posta si
// controlla come prima.
PushService push;
push.start();
QQuickView *view = SailfishApp::createView();
view->rootContext()->setContextProperty(QStringLiteral("engine"), &bridge);
view->rootContext()->setContextProperty(QStringLiteral("account"), &account);
view->rootContext()->setContextProperty(QStringLiteral("pushService"), &push);
view->rootContext()->setContextProperty(QStringLiteral("appVersion"),
QCoreApplication::applicationVersion());
view->setSource(SailfishApp::pathTo(QStringLiteral("qml/harbour-grumpydove.qml")));
+213
View File
@@ -0,0 +1,213 @@
#include "pushservice.h"
#include "enginebridge.h"
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QSettings>
#include <QStandardPaths>
#include <QThread>
#include <QTimer>
#include <QUuid>
extern "C" int gd_engine_run(void);
static const char *kServizio = "org.unifiedpush.Connector.harbour-grumpydove";
static const char *kOggetto = "/org/unifiedpush/Connector";
static const char *kDistributore = "org.unifiedpush.Distributor.Foghorn";
static const char *kPercorsoDistributore = "/org/unifiedpush/Distributor";
static const char *kInterfacciaDistributore = "org.unifiedpush.Distributor2";
PushService::PushService(QObject *parent) : QObject(parent)
{
QSettings s;
m_token = s.value(QStringLiteral("push/token")).toString();
m_endpoint = s.value(QStringLiteral("push/endpoint")).toString();
}
void PushService::start()
{
QDBusConnection bus = QDBusConnection::sessionBus();
if (!bus.isConnected()) {
qWarning() << "push: nessun bus di sessione";
return;
}
// Il processo possiede il nome (non si registra): è così che il
// distributore sa a chi consegnare.
if (!bus.registerService(QString::fromLatin1(kServizio)))
qWarning() << "push: nome già posseduto da un altro processo";
bus.registerObject(QString::fromLatin1(kOggetto), this,
QDBusConnection::ExportAllSlots);
// Il token è l'identità verso il distributore e va scritto su disco prima
// della registrazione: NewEndpoint può arrivare prima che Register
// ritorni.
if (m_token.isEmpty()) {
m_token = QUuid::createUuid().toString();
m_token.remove(QLatin1Char('{'));
m_token.remove(QLatin1Char('}'));
QSettings s;
s.setValue(QStringLiteral("push/token"), m_token);
}
registra();
}
void PushService::registra()
{
QDBusInterface distributore(QString::fromLatin1(kDistributore),
QString::fromLatin1(kPercorsoDistributore),
QString::fromLatin1(kInterfacciaDistributore),
QDBusConnection::sessionBus());
if (!distributore.isValid()) {
qWarning() << "push: distributore non disponibile (FogHorn assente?)";
return;
}
QVariantMap args;
args[QStringLiteral("service")] = QString::fromLatin1(kServizio);
args[QStringLiteral("token")] = m_token;
args[QStringLiteral("description")] = QStringLiteral("GrumpyDove");
// Asincrono: la risposta può arrivare dopo NewEndpoint, e bloccarsi qui
// sarebbe un abbraccio mortale.
QDBusPendingCall call = distributore.asyncCallWithArgumentList(
QStringLiteral("Register"), { args });
auto *watcher = new QDBusPendingCallWatcher(call, this);
connect(watcher, &QDBusPendingCallWatcher::finished, this,
[this](QDBusPendingCallWatcher *w) {
QDBusPendingReply<QVariantMap> r = *w;
w->deleteLater();
if (r.isError()) {
qWarning() << "push: Register:" << r.error().message();
return;
}
const QVariantMap m = r.value();
qDebug() << "push: registrazione"
<< m.value(QStringLiteral("success")).toString()
<< m.value(QStringLiteral("reason")).toString();
});
}
QVariantMap PushService::NewEndpoint(const QVariantMap &args)
{
const QString nuovo = args.value(QStringLiteral("endpoint")).toString();
if (!nuovo.isEmpty() && nuovo != m_endpoint) {
m_endpoint = nuovo;
QSettings s;
s.setValue(QStringLiteral("push/endpoint"), nuovo);
qDebug() << "push: endpoint ricevuto";
emit endpointChanged(nuovo);
}
QVariantMap r;
r[QStringLiteral("success")] = true;
return r;
}
QVariantMap PushService::Message(const QVariantMap &args)
{
// Il push non porta dati: dice solo che c'è qualcosa da guardare (il
// servizio Mozilla rifiuta un corpo non cifrato, quindi questa catena
// funziona con ntfy/FogHorn).
emit pushReceived();
QVariantMap r;
r[QStringLiteral("id")] = args.value(QStringLiteral("id"));
return r;
}
QVariantMap PushService::Unregistered(const QVariantMap &args)
{
Q_UNUSED(args)
m_endpoint.clear();
QSettings s;
s.remove(QStringLiteral("push/endpoint"));
emit endpointChanged(QString());
QVariantMap r;
r[QStringLiteral("success")] = true;
return r;
}
void notificaSistema(const QString &titolo, const QString &testo)
{
QDBusInterface n(QStringLiteral("org.freedesktop.Notifications"),
QStringLiteral("/org/freedesktop/Notifications"),
QStringLiteral("org.freedesktop.Notifications"),
QDBusConnection::sessionBus());
if (!n.isValid()) {
qWarning() << "notifica: servizio non disponibile";
return;
}
n.call(QStringLiteral("Notify"),
QStringLiteral("harbour-grumpydove"),
0u,
QStringLiteral("harbour-grumpydove"),
titolo, testo,
QStringList(),
QVariantMap(),
8000);
}
void preparaPercorsiMotore()
{
// Come in main(): la sandbox non lascia vedere ~/Mail né scrivere sotto
// HOME, quindi Maildir, log, rubrica e liste dei filtri stanno nella
// cartella dati dell'app. HOME non si tocca, o si perde il tema.
const QString dati = QStandardPaths::writableLocation(
QStandardPaths::AppDataLocation);
struct { const char *env; const char *sotto; } cartelle[] = {
{ "GRUMPYDOVE_MAIL", "/Mail" },
{ "GRUMPYDOVE_LOG", "/log" },
{ "GRUMPYDOVE_BOOKS", "/books" },
{ "GRUMPYDOVE_FILING", "/filing" },
};
for (const auto &c : cartelle) {
const QString percorso = dati + QString::fromLatin1(c.sotto);
QDir().mkpath(percorso);
qputenv(c.env, QFile::encodeName(percorso));
}
}
int risveglioPush(int argc, char *argv[])
{
static int uno = 1;
QCoreApplication::setOrganizationName(QStringLiteral("harbour"));
QCoreApplication::setApplicationName(QStringLiteral("grumpydove"));
QCoreApplication app(uno, argv);
Q_UNUSED(argc)
preparaPercorsiMotore();
// Il motore entra in gd_engine_run e da lì nel suo ciclo: gira in un
// thread suo, mentre qui si aspetta l'esito.
EngineBridge bridge;
EngineBridge::setInstance(&bridge);
QThread *motore = QThread::create([] { gd_engine_run(); });
motore->start();
// Tetto di sicurezza: un risveglio non deve restare appeso.
QTimer::singleShot(60000, &app, [&app]() {
qWarning() << "push: tempo scaduto";
app.exit(2);
});
// Si aspetta che il motore abbia letto le cartelle, poi si controlla.
QObject::connect(&bridge, &EngineBridge::ready, &app, [&app, &bridge]() {
static bool gia = false;
if (gia) return; // ready() arriva più volte
gia = true;
QObject::connect(&bridge, &EngineBridge::mailChecked, &app, [&app]() {
notificaSistema(QStringLiteral("Posta nuova"),
QStringLiteral("Tocca per leggere i messaggi"));
app.exit(0);
});
bridge.checkMail();
});
return app.exec();
}
+53
View File
@@ -0,0 +1,53 @@
#ifndef PUSHSERVICE_H
#define PUSHSERVICE_H
#include <QObject>
#include <QString>
#include <QVariantMap>
// Connector UnifiedPush, come nelle altre app di casa: l'app possiede il nome
// org.unifiedpush.Connector.<app>, si registra col distributore (FogHorn) e
// riceve i push. Non tiene nessuna connessione verso il servizio push: quella
// è del distributore, una sola per tutte le app.
//
// I metodi D-Bus si chiamano con la maiuscola (NewEndpoint, Message), quindi
// devono stare in C++: in QML le funzioni non possono iniziare in maiuscolo.
class PushService : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "org.unifiedpush.Connector2")
public:
explicit PushService(QObject *parent = nullptr);
void start();
QString endpoint() const { return m_endpoint; }
public slots:
QVariantMap NewEndpoint(const QVariantMap &args);
QVariantMap Message(const QVariantMap &args);
QVariantMap Unregistered(const QVariantMap &args);
signals:
void endpointChanged(const QString &endpoint);
void pushReceived();
private:
void registra();
QString m_token;
QString m_endpoint;
};
// Una notifica di sistema che funziona anche senza GUI: nel risveglio non c'è
// nessuna finestra, quindi niente QML.
void notificaSistema(const QString &titolo, const QString &testo);
// I percorsi del motore nella cartella dati dell'app (Maildir, log, rubrica,
// liste dei filtri). Sta qui perché serve sia all'app sia al risveglio.
void preparaPercorsiMotore();
// Il risveglio da push: apre il motore senza finestre, controlla la posta,
// notifica e esce. Una push non deve poter aprire l'app.
int risveglioPush(int argc, char *argv[]);
#endif // PUSHSERVICE_H