refactor: reorganize functions
This commit is contained in:
parent
5d6ab3eb5a
commit
0b1130c743
106
filepick.go
106
filepick.go
@ -3,63 +3,95 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/godbus/dbus/v5"
|
"github.com/godbus/dbus/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
busName = "org.freedesktop.portal.Desktop"
|
||||||
|
objectPath = "/org/freedesktop/portal/desktop"
|
||||||
|
methodName = "org.freedesktop.portal.FileChooser.OpenFile"
|
||||||
|
requestIFace = "org.freedesktop.portal.Request"
|
||||||
|
responseSignal = "Response"
|
||||||
|
handleToken = "revelation"
|
||||||
|
dialogTitle = "Choose file"
|
||||||
|
)
|
||||||
|
|
||||||
func SelectFile() string {
|
func SelectFile() string {
|
||||||
conn, err := dbus.ConnectSessionBus()
|
conn := connectDBus()
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
|
|
||||||
token := "revelation"
|
responsePath := openFileDialog(conn)
|
||||||
|
setupSignalHandler(conn, responsePath)
|
||||||
|
|
||||||
|
fmt.Println("File picker triggered, waiting for response...")
|
||||||
|
return processSignal(<-waitForSignal(conn), responsePath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func connectDBus() *dbus.Conn {
|
||||||
|
conn, err := dbus.ConnectSessionBus()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to connect to session bus: %v", err)
|
||||||
|
}
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|
||||||
|
func openFileDialog(conn *dbus.Conn) dbus.ObjectPath {
|
||||||
options := map[string]dbus.Variant{
|
options := map[string]dbus.Variant{
|
||||||
"handle_token": dbus.MakeVariant(token),
|
"handle_token": dbus.MakeVariant(handleToken),
|
||||||
"title": dbus.MakeVariant("Choose file"),
|
"title": dbus.MakeVariant(dialogTitle),
|
||||||
}
|
}
|
||||||
|
|
||||||
obj := conn.Object("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop")
|
call := conn.Object(busName, objectPath).Call(methodName, 0, "", "", options)
|
||||||
call := obj.Call("org.freedesktop.portal.FileChooser.OpenFile", 0, "revelation", "", options)
|
|
||||||
if call.Err != nil {
|
if call.Err != nil {
|
||||||
log.Fatalf("Failed to trigger file picker: %v", call.Err)
|
log.Fatalf("Failed to trigger file picker: %v", call.Err)
|
||||||
}
|
}
|
||||||
|
|
||||||
replyPath := call.Body[0].(dbus.ObjectPath)
|
return call.Body[0].(dbus.ObjectPath)
|
||||||
|
}
|
||||||
|
|
||||||
err = conn.AddMatchSignal(
|
func setupSignalHandler(conn *dbus.Conn, path dbus.ObjectPath) {
|
||||||
dbus.WithMatchOption("interface", "org.freedesktop.portal.Request"),
|
err := conn.AddMatchSignal(
|
||||||
dbus.WithMatchOption("member", "Response"),
|
dbus.WithMatchInterface(requestIFace),
|
||||||
dbus.WithMatchOption("path", string(replyPath)),
|
dbus.WithMatchMember(responseSignal),
|
||||||
|
dbus.WithMatchPathNamespace(path),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to add match signal: %v", err)
|
log.Fatalf("Failed to add signal match: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func waitForSignal(conn *dbus.Conn) <-chan *dbus.Signal {
|
||||||
|
ch := make(chan *dbus.Signal, 1)
|
||||||
|
conn.Signal(ch)
|
||||||
|
return ch
|
||||||
|
}
|
||||||
|
|
||||||
|
func processSignal(signal *dbus.Signal, expectedPath dbus.ObjectPath) string {
|
||||||
|
if signal.Path != expectedPath || signal.Name != requestIFace+"."+responseSignal {
|
||||||
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
c := make(chan *dbus.Signal, 1)
|
if len(signal.Body) < 2 {
|
||||||
conn.Signal(c)
|
// nothing selected
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
for signal := range c {
|
results, ok := signal.Body[1].(map[string]dbus.Variant)
|
||||||
if signal.Path == replyPath && signal.Name == "org.freedesktop.portal.Request.Response" {
|
if !ok {
|
||||||
reply := signal.Body
|
// invalid response
|
||||||
if len(reply) > 1 {
|
return ""
|
||||||
results, ok := reply[1].(map[string]dbus.Variant)
|
}
|
||||||
if ok {
|
|
||||||
if urisVariant, exists := results["uris"]; exists {
|
urisVariant, exists := results["uris"]
|
||||||
uris, ok := urisVariant.Value().([]string)
|
if !exists {
|
||||||
if ok && len(uris) > 0 {
|
// nothing selected
|
||||||
return uris[0]
|
return ""
|
||||||
} else {
|
}
|
||||||
return ""
|
|
||||||
}
|
uris, ok := urisVariant.Value().([]string)
|
||||||
}
|
if ok && len(uris) > 0 {
|
||||||
}
|
return uris[0]
|
||||||
}
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(SelectFile())
|
uploadFile(strings.Split(SelectFile(), "file://")[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
func uploadFile(file string) {
|
||||||
|
fmt.Println(file)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user