Skip to content

Commit

Permalink
Get SaveFiles working and input API coverage to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Mar 20, 2024
1 parent fab70c9 commit a0b0dc5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
2 changes: 1 addition & 1 deletion filechooser/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func OpenFile(parentWindow, title string, options *OpenFileOptions) ([]string, e
}

if options.CurrentFolder != "" {
data["current_folder"] = convert.ToNullTerminated(options.CurrentFolder)
data["current_folder"] = convert.ToNullTerminatedValue(options.CurrentFolder)
}
}

Expand Down
23 changes: 11 additions & 12 deletions filechooser/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func SaveFile(parentWindow, title string, options *SaveFileOptions) ([]string, e
}

if options.CurrentFolder != "" {
data["current_folder"] = convert.ToNullTerminated(options.CurrentFolder)
data["current_folder"] = convert.ToNullTerminatedValue(options.CurrentFolder)
}
}

Expand All @@ -81,10 +81,9 @@ type SaveFilesOptions struct {
HandleToken string // A string that will be used as the last element of the handle. Must be a valid object path element.
AcceptLabel string // Label for the accept button. Mnemonic underlines are allowed.
NotModal bool // Whether the dialog should be modal.
Filters []*Filter // Each item specifies a single filter to offer to the user.
CurrentFilter *Filter // Request that this filter be set by default at dialog creation.
Choices []*ComboBox // List of serialized combo boxes to add to the file chooser.
CurrentFolder string // Suggested folder in which the file should be saved.
Files []string // An array of file names to be saved.
}

// SaveFiles opens a filechooser for selecting where to save one or more files.
Expand All @@ -110,20 +109,20 @@ func SaveFiles(parentWindow, title string, options *SaveFilesOptions) ([]string,
data["accept_label"] = convert.FromString(options.AcceptLabel)
}

if len(options.Filters) > 0 {
data["filters"] = dbus.MakeVariant(options.Filters)
}

if options.CurrentFilter != nil {
data["current_filter"] = dbus.MakeVariant(options.CurrentFilter)
}

if len(options.Choices) > 0 {
data["choices"] = dbus.MakeVariant(options.Choices)
}

if options.CurrentFolder != "" {
data["current_folder"] = convert.ToNullTerminated(options.CurrentFolder)
data["current_folder"] = convert.ToNullTerminatedValue(options.CurrentFolder)
}

if len(options.Files) > 0 {
files := make([][]byte, len(options.Files))
for i, file := range options.Files {
files[i] = convert.FromStringToNullTerminated(file)
}
data["files"] = dbus.MakeVariant(files)
}
}

Expand Down
11 changes: 8 additions & 3 deletions internal/convert/nullstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ import (

var bytesSignature = dbus.SignatureOfType(reflect.TypeOf([]byte{}))

// ToNullTerminated connverts a regular string into a null terminated byte string.
func ToNullTerminated(input string) dbus.Variant {
// ToNullTerminatedValue converts a regular string into a null terminated byte string.
func ToNullTerminatedValue(input string) dbus.Variant {
return dbus.MakeVariantWithSignature(FromStringToNullTerminated(input), bytesSignature)
}

// FromStringToNullTerminated converts a regular string into a null terminated byte string
func FromStringToNullTerminated(input string) []byte {
terminated := make([]byte, len(input)+1)
copy(terminated, input)
return dbus.MakeVariantWithSignature(terminated, bytesSignature)
return terminated
}
4 changes: 2 additions & 2 deletions internal/convert/nullstr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
func TestToNullTerminated(t *testing.T) {
input := "test"

actual := ToNullTerminated(input)
actual := ToNullTerminatedValue(input)
expect := dbus.MakeVariant([]byte{'t', 'e', 's', 't', '\000'})
if actual.Signature() != expect.Signature() {
t.Fatalf("Expected %v, got %v", expect, actual)
Expand All @@ -20,7 +20,7 @@ func BenchmarkToNullTerminated(b *testing.B) {
var variant dbus.Variant

for i := 0; i < b.N; i++ {
variant = ToNullTerminated("long_input_string")
variant = ToNullTerminatedValue("long_input_string")
}

benchVariant = variant
Expand Down

0 comments on commit a0b0dc5

Please sign in to comment.