Skip to content

Commit

Permalink
Minor cleanup and refactor of filechooser portal
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Mar 15, 2024
1 parent 185650a commit 890a7b7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
8 changes: 5 additions & 3 deletions filechooser/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
18 changes: 11 additions & 7 deletions filechooser/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -26,20 +32,19 @@ 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 != "" {
data["current_name"] = dbus.MakeVariant(options.CurrentName)
}

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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions internal/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Packge convert contains functions for converting to dbus data structures.
package convert
8 changes: 8 additions & 0 deletions internal/convert/nullstr.go
Original file line number Diff line number Diff line change
@@ -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"))
}

0 comments on commit 890a7b7

Please sign in to comment.