diff --git a/app/app_xdg.go b/app/app_xdg.go index 6e1fa7fa8c..ba6d38b25e 100644 --- a/app/app_xdg.go +++ b/app/app_xdg.go @@ -12,10 +12,11 @@ import ( "net/url" "os" "path/filepath" - "strconv" "sync" "github.com/godbus/dbus/v5" + "github.com/rymdport/portal/notification" + "github.com/rymdport/portal/openuri" "golang.org/x/sys/execabs" "fyne.io/fyne/v2" @@ -29,8 +30,10 @@ func defaultVariant() fyne.ThemeVariant { } func (a *fyneApp) OpenURL(url *url.URL) error { - if openURLThroughPortal(url) == nil { + if err := openuri.OpenURI(url.String()); err == nil { return nil + } else { + fyne.LogError("Opening url in portal failed", err) } cmd := execabs.Command("xdg-open", url.String()) @@ -38,27 +41,6 @@ func (a *fyneApp) OpenURL(url *url.URL) error { return cmd.Start() } -func openURLThroughPortal(url *url.URL) error { - conn, err := dbus.SessionBus() // shared connection, don't close - if err != nil { - fyne.LogError("Unable to connect to session D-Bus", err) - return err - } - - parentWindow := "" - uri := url.String() - options := map[string]dbus.Variant{} - - obj := conn.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop") - call := obj.Call("org.freedesktop.portal.OpenURI.OpenURI", 0, parentWindow, uri, options) - if call.Err != nil { - fyne.LogError("Failed to open url with xdg-desktop-portal", call.Err) - return call.Err - } - - return nil -} - // fetch color variant from dbus portal desktop settings. func findFreedestktopColorScheme() fyne.ThemeVariant { dbusConn, err := dbus.SessionBus() @@ -107,8 +89,10 @@ func (a *fyneApp) SendNotification(n *fyne.Notification) { return } - if a.sendNotificationThroughPortal(conn, n) == nil { + if err := a.sendNotificationThroughPortal(conn, n); err == nil { return // No need to use the fallback path. + } else { + fyne.LogError("Sending notification using portal failed", err) } appIcon := a.cachedIconPath() @@ -123,22 +107,19 @@ func (a *fyneApp) SendNotification(n *fyne.Notification) { } // Sending with same ID replaces the old notification. -var notificationID uint64 = 0 +var notificationID uint = 0 // See https://flatpak.github.io/xdg-desktop-portal/docs/#gdbus-org.freedesktop.portal.Notification. func (a *fyneApp) sendNotificationThroughPortal(conn *dbus.Conn, n *fyne.Notification) error { - id := strconv.FormatUint(notificationID, 10) - data := map[string]dbus.Variant{ - "title": dbus.MakeVariant(n.Title), - "body": dbus.MakeVariant(n.Content), - "icon": dbus.MakeVariant(a.uniqueID), - } - - obj := conn.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop") - call := obj.Call("org.freedesktop.portal.Notification.AddNotification", 0, id, data) - if call.Err != nil { - fyne.LogError("Failed to send notification to xdg-desktop-portal", call.Err) - return call.Err + err := notification.Add(notificationID, + ¬ification.Content{ + Title: n.Title, + Body: n.Content, + Icon: a.uniqueID, + }, + ) + if err != nil { + return err } notificationID++ diff --git a/go.mod b/go.mod index 899268e30d..d8d94d146f 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/lucor/goinfo v0.9.0 github.com/mcuadros/go-version v0.0.0-20190830083331-035f6764e8d2 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 + github.com/rymdport/portal v0.0.0-20231119111435-f007cc4c83bb github.com/srwiley/oksvg v0.0.0-20221011165216-be6e8873101c github.com/srwiley/rasterx v0.0.0-20220730225603-2ab79fcdd4ef github.com/stretchr/testify v1.8.4 diff --git a/go.sum b/go.sum index 79b30237b6..1453e84a44 100644 --- a/go.sum +++ b/go.sum @@ -266,6 +266,8 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= +github.com/rymdport/portal v0.0.0-20231119111435-f007cc4c83bb h1:G7vOtmOMTjkjbApmz4Rfa9vSft+8faoe8i9kBqmzBuo= +github.com/rymdport/portal v0.0.0-20231119111435-f007cc4c83bb/go.mod h1:RYYAnv4sssTQ7ceErKl7UD8auER/0yFV7CgmfS/uAD8= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk= github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=