From 890a7b7819ad2314561b7935c62ec0c6a9d8552d Mon Sep 17 00:00:00 2001 From: Jacob Date: Fri, 15 Mar 2024 19:05:31 +0100 Subject: [PATCH] Minor cleanup and refactor of filechooser portal --- filechooser/open.go | 8 +++++--- filechooser/save.go | 18 +++++++++++------- internal/convert/convert.go | 2 ++ internal/convert/nullstr.go | 8 ++++++++ 4 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 internal/convert/convert.go create mode 100644 internal/convert/nullstr.go diff --git a/filechooser/open.go b/filechooser/open.go index 70cb58a..5abcc66 100644 --- a/filechooser/open.go +++ b/filechooser/open.go @@ -3,8 +3,11 @@ package filechooser import ( "github.com/godbus/dbus/v5" "github.com/rymdport/portal" + "github.com/rymdport/portal/internal/convert" ) +const openFileCallName = fileChooserCallName + ".OpenFile" + // OpenFileOptions contains the options for how files are to be selected. type OpenFileOptions struct { AcceptLabel string // Label for the accept button. Mnemonic underlines are allowed. @@ -33,12 +36,11 @@ func OpenFile(parentWindow, title string, options *OpenFileOptions) ([]string, e } if options.CurrentFolder != "" { - nullTerminatedByteString := []byte(options.CurrentFolder + "\000") - data["current_folder"] = dbus.MakeVariant(nullTerminatedByteString) + data["current_folder"] = convert.ToNullTerminatedString(options.CurrentFolder) } obj := conn.Object(portal.ObjectName, portal.ObjectPath) - call := obj.Call(fileChooserCallName+".OpenFile", 0, parentWindow, title, data) + call := obj.Call(openFileCallName, 0, parentWindow, title, data) if call.Err != nil { return nil, call.Err } diff --git a/filechooser/save.go b/filechooser/save.go index 5c2836f..da5ccd6 100644 --- a/filechooser/save.go +++ b/filechooser/save.go @@ -3,6 +3,12 @@ package filechooser import ( "github.com/godbus/dbus/v5" "github.com/rymdport/portal" + "github.com/rymdport/portal/internal/convert" +) + +const ( + saveFileCallName = fileChooserCallName + ".SaveFile" + saveFilesCallName = fileChooserCallName + ".SaveFiles" ) // SaveFileOptions contains the options for how a file is saved. @@ -26,7 +32,7 @@ func SaveFile(parentWindow, title string, options *SaveFileOptions) ([]string, e } if options.AcceptLabel != "" { - data["accept_label"] = dbus.MakeVariant("") // dbus.MakeVariant(options.AcceptLabel) + data["accept_label"] = dbus.MakeVariant(options.AcceptLabel) } if options.CurrentName != "" { @@ -34,12 +40,11 @@ func SaveFile(parentWindow, title string, options *SaveFileOptions) ([]string, e } if options.CurrentFolder != "" { - nullTerminatedByteString := []byte(options.CurrentFolder + "\000") - data["current_folder"] = dbus.MakeVariant(nullTerminatedByteString) + data["current_folder"] = convert.ToNullTerminatedString(options.CurrentFolder) } obj := conn.Object(portal.ObjectName, portal.ObjectPath) - call := obj.Call(fileChooserCallName+".SaveFile", 0, parentWindow, title, data) + call := obj.Call(saveFileCallName, 0, parentWindow, title, data) if call.Err != nil { return nil, call.Err } @@ -71,12 +76,11 @@ func SaveFiles(parentWindow, title string, options *SaveFilesOptions) ([]string, } if options.CurrentFolder != "" { - nullTerminatedByteString := []byte(options.CurrentFolder + "\000") - data["current_folder"] = dbus.MakeVariant(nullTerminatedByteString) + data["current_folder"] = convert.ToNullTerminatedString(options.CurrentFolder) } obj := conn.Object(portal.ObjectName, portal.ObjectPath) - call := obj.Call(fileChooserCallName+".SaveFiles", 0, parentWindow, title, data) + call := obj.Call(saveFilesCallName, 0, parentWindow, title, data) if call.Err != nil { return nil, call.Err } diff --git a/internal/convert/convert.go b/internal/convert/convert.go new file mode 100644 index 0000000..7478104 --- /dev/null +++ b/internal/convert/convert.go @@ -0,0 +1,2 @@ +// Packge convert contains functions for converting to dbus data structures. +package convert diff --git a/internal/convert/nullstr.go b/internal/convert/nullstr.go new file mode 100644 index 0000000..f020554 --- /dev/null +++ b/internal/convert/nullstr.go @@ -0,0 +1,8 @@ +package convert + +import "github.com/godbus/dbus/v5" + +// ToNullTerminatedString connverts a regular string into a null terminated dbus variant string. +func ToNullTerminatedString(input string) dbus.Variant { + return dbus.MakeVariant([]byte(input + "\000")) +}