Skip to content

Commit

Permalink
Various API cleanups and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Mar 15, 2024
1 parent 890a7b7 commit 8ada4c4
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 41 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

func main() {
options := filechooser.OpenOptions{Multiple: true}
options := filechooser.OpenFileOptions{Multiple: true}
files, err := filechooser.OpenFile("", "Select files", &options)
if err != nil {
log.Fatalln(err)
Expand Down
19 changes: 8 additions & 11 deletions filechooser/filechooser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
package filechooser

import (
"errors"

"github.com/godbus/dbus/v5"
"github.com/rymdport/portal"
"github.com/rymdport/portal/internal/apis"
)

const fileChooserCallName = portal.CallBaseName + ".FileChooser"

var errorUnexpectedResponse = errors.New("unexpected response")
const fileChooserCallName = apis.CallBaseName + ".FileChooser"

func readURIFromResponse(conn *dbus.Conn, call *dbus.Call) ([]string, error) {
var responsePath dbus.ObjectPath
Expand All @@ -22,8 +19,8 @@ func readURIFromResponse(conn *dbus.Conn, call *dbus.Call) ([]string, error) {

err = conn.AddMatchSignal(
dbus.WithMatchObjectPath(responsePath),
dbus.WithMatchInterface(portal.RequestInterface),
dbus.WithMatchMember(portal.ResponseMember),
dbus.WithMatchInterface(apis.RequestInterface),
dbus.WithMatchMember(apis.ResponseMember),
)
if err != nil {
return nil, err
Expand All @@ -34,23 +31,23 @@ func readURIFromResponse(conn *dbus.Conn, call *dbus.Call) ([]string, error) {

response := <-dbusChan
if len(response.Body) != 2 {
return nil, errorUnexpectedResponse
return nil, portal.ErrUnexpectedResponse
}

if responseKey, ok := response.Body[0].(uint32); !ok {
return nil, errorUnexpectedResponse
return nil, portal.ErrUnexpectedResponse
} else if responseKey == 1 || responseKey == 2 {
return nil, nil
}

result, ok := response.Body[1].(map[string]dbus.Variant)
if !ok {
return nil, errorUnexpectedResponse
return nil, portal.ErrUnexpectedResponse
}

uris, ok := result["uris"].Value().([]string)
if !ok {
return nil, errorUnexpectedResponse
return nil, portal.ErrUnexpectedResponse
}

return uris, nil
Expand Down
4 changes: 2 additions & 2 deletions filechooser/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package filechooser

import (
"github.com/godbus/dbus/v5"
"github.com/rymdport/portal"
"github.com/rymdport/portal/internal/apis"
"github.com/rymdport/portal/internal/convert"
)

Expand Down Expand Up @@ -39,7 +39,7 @@ func OpenFile(parentWindow, title string, options *OpenFileOptions) ([]string, e
data["current_folder"] = convert.ToNullTerminatedString(options.CurrentFolder)
}

obj := conn.Object(portal.ObjectName, portal.ObjectPath)
obj := conn.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(openFileCallName, 0, parentWindow, title, data)
if call.Err != nil {
return nil, call.Err
Expand Down
6 changes: 3 additions & 3 deletions filechooser/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package filechooser

import (
"github.com/godbus/dbus/v5"
"github.com/rymdport/portal"
"github.com/rymdport/portal/internal/apis"
"github.com/rymdport/portal/internal/convert"
)

Expand Down Expand Up @@ -43,7 +43,7 @@ func SaveFile(parentWindow, title string, options *SaveFileOptions) ([]string, e
data["current_folder"] = convert.ToNullTerminatedString(options.CurrentFolder)
}

obj := conn.Object(portal.ObjectName, portal.ObjectPath)
obj := conn.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(saveFileCallName, 0, parentWindow, title, data)
if call.Err != nil {
return nil, call.Err
Expand Down Expand Up @@ -79,7 +79,7 @@ func SaveFiles(parentWindow, title string, options *SaveFilesOptions) ([]string,
data["current_folder"] = convert.ToNullTerminatedString(options.CurrentFolder)
}

obj := conn.Object(portal.ObjectName, portal.ObjectPath)
obj := conn.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(saveFilesCallName, 0, parentWindow, title, data)
if call.Err != nil {
return nil, call.Err
Expand Down
12 changes: 12 additions & 0 deletions internal/apis/apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Package apis contains dbus paths and call names for various portal apis.
package apis

const (
ObjectName = "org.freedesktop.portal.Desktop"
ObjectPath = "/org/freedesktop/portal/desktop"

CallBaseName = "org.freedesktop.portal"

RequestInterface = "org.freedesktop.portal.Request"
ResponseMember = "Response"
)
8 changes: 4 additions & 4 deletions notification/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strconv"

"github.com/godbus/dbus/v5"
"github.com/rymdport/portal"
"github.com/rymdport/portal/internal/apis"
)

const notificationCallName = portal.CallBaseName + ".Notification"
const notificationCallName = apis.CallBaseName + ".Notification"

// Priority is the priroity of a notification.
type Priority = string
Expand Down Expand Up @@ -47,7 +47,7 @@ func Add(id uint, content *Content) error {
data["priority"] = dbus.MakeVariant(content.Priority)
}

obj := bus.Object(portal.ObjectName, portal.ObjectPath)
obj := bus.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(notificationCallName+".AddNotification", 0, strconv.FormatUint(uint64(id), 10), data)
return call.Err
}
Expand All @@ -59,7 +59,7 @@ func Remove(id uint) error {
return err
}

obj := bus.Object(portal.ObjectName, portal.ObjectPath)
obj := bus.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(notificationCallName+".RemoveNotification", 0, strconv.FormatUint(uint64(id), 10))
return call.Err
}
6 changes: 3 additions & 3 deletions openuri/openuri.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ package openuri

import (
"github.com/godbus/dbus/v5"
"github.com/rymdport/portal"
"github.com/rymdport/portal/internal/apis"
)

const (
openURIBaseName = portal.CallBaseName + ".OpenURI"
openURIBaseName = apis.CallBaseName + ".OpenURI"
openURICallName = openURIBaseName + ".OpenURI"
)

Expand All @@ -21,7 +21,7 @@ func OpenURI(parentWindow, uri string) error {

data := map[string]dbus.Variant{}

obj := conn.Object(portal.ObjectName, portal.ObjectPath)
obj := conn.Object(apis.ObjectName, apis.ObjectPath)
call := obj.Call(openURICallName, 0, parentWindow, uri, data)
return call.Err
}
13 changes: 3 additions & 10 deletions portal.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
package portal

// TODO: Move these internal details to internal/ package.
import "errors"

const (
ObjectName = "org.freedesktop.portal.Desktop"
ObjectPath = "/org/freedesktop/portal/desktop"

CallBaseName = "org.freedesktop.portal"

RequestInterface = "org.freedesktop.portal.Request"
ResponseMember = "Response"
)
// ErrunexpectedResonse is returned when the received dbus data was in an unexpected format.
var ErrUnexpectedResponse = errors.New("unexpected response from dbus")
8 changes: 4 additions & 4 deletions settings/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"image/color"

"github.com/godbus/dbus/v5"
"github.com/rymdport/portal"
"github.com/rymdport/portal/internal/apis"
)

// ErrNotSet indicates that the value is not set.
Expand Down Expand Up @@ -37,7 +37,7 @@ func GetColorScheme() (ColorScheme, error) {
return NoPreference, err
}

dbusObj := dbusConn.Object(portal.ObjectName, portal.ObjectPath)
dbusObj := dbusConn.Object(apis.ObjectName, apis.ObjectPath)
call := dbusObj.Call(
readOneCallPath,
dbus.FlagNoAutoStart,
Expand Down Expand Up @@ -68,7 +68,7 @@ func GetAccentColor() (color.RGBA, error) {
return color.RGBA{}, err
}

dbusObj := dbusConn.Object(portal.ObjectName, portal.ObjectPath)
dbusObj := dbusConn.Object(apis.ObjectName, apis.ObjectPath)
call := dbusObj.Call(
readOneCallPath,
dbus.FlagNoAutoStart,
Expand Down Expand Up @@ -123,7 +123,7 @@ func GetContrast() (Contrast, error) {
return NormalContrast, err
}

dbusObj := dbusConn.Object(portal.ObjectName, portal.ObjectPath)
dbusObj := dbusConn.Object(apis.ObjectName, apis.ObjectPath)
call := dbusObj.Call(
readOneCallPath,
dbus.FlagNoAutoStart,
Expand Down
6 changes: 3 additions & 3 deletions settings/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package settings

import (
"github.com/godbus/dbus/v5"
"github.com/rymdport/portal"
"github.com/rymdport/portal/internal/apis"
)

const settingsCallPath = portal.CallBaseName + ".Settings"
const settingsCallPath = apis.CallBaseName + ".Settings"

// WatchSettingsChange allows setting a function to run each time the portal settings change.
func WatchSettingsChange(callback func(value []any)) error {
Expand All @@ -15,7 +15,7 @@ func WatchSettingsChange(callback func(value []any)) error {
}

if err := conn.AddMatchSignal(
dbus.WithMatchObjectPath(portal.ObjectPath),
dbus.WithMatchObjectPath(apis.ObjectPath),
dbus.WithMatchInterface(settingsCallPath),
dbus.WithMatchMember("SettingChanged"),
); err != nil {
Expand Down

0 comments on commit 8ada4c4

Please sign in to comment.