Compare commits
No commits in common. "0b1130c743ce22f98b1dbf6e4a61a3850690ec5c" and "5a0e5e2ea3f13c6190da7aff0929de29591ee763" have entirely different histories.
0b1130c743
...
5a0e5e2ea3
97
filepick.go
97
filepick.go
@ -1,97 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
|
|
||||||
"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 {
|
|
||||||
conn := connectDBus()
|
|
||||||
defer conn.Close()
|
|
||||||
|
|
||||||
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{
|
|
||||||
"handle_token": dbus.MakeVariant(handleToken),
|
|
||||||
"title": dbus.MakeVariant(dialogTitle),
|
|
||||||
}
|
|
||||||
|
|
||||||
call := conn.Object(busName, objectPath).Call(methodName, 0, "", "", options)
|
|
||||||
if call.Err != nil {
|
|
||||||
log.Fatalf("Failed to trigger file picker: %v", call.Err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return call.Body[0].(dbus.ObjectPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
func setupSignalHandler(conn *dbus.Conn, path dbus.ObjectPath) {
|
|
||||||
err := conn.AddMatchSignal(
|
|
||||||
dbus.WithMatchInterface(requestIFace),
|
|
||||||
dbus.WithMatchMember(responseSignal),
|
|
||||||
dbus.WithMatchPathNamespace(path),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
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 ""
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(signal.Body) < 2 {
|
|
||||||
// nothing selected
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
results, ok := signal.Body[1].(map[string]dbus.Variant)
|
|
||||||
if !ok {
|
|
||||||
// invalid response
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
urisVariant, exists := results["uris"]
|
|
||||||
if !exists {
|
|
||||||
// nothing selected
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
uris, ok := urisVariant.Value().([]string)
|
|
||||||
if ok && len(uris) > 0 {
|
|
||||||
return uris[0]
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
2
go.mod
2
go.mod
@ -1,5 +1,3 @@
|
|||||||
module github.com/jabuxas/revelation
|
module github.com/jabuxas/revelation
|
||||||
|
|
||||||
go 1.23.4
|
go 1.23.4
|
||||||
|
|
||||||
require github.com/godbus/dbus/v5 v5.1.0 // indirect
|
|
||||||
|
2
go.sum
2
go.sum
@ -1,2 +0,0 @@
|
|||||||
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
|
|
||||||
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
|
@ -2,13 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
uploadFile(strings.Split(SelectFile(), "file://")[1])
|
fmt.Println("Hello world")
|
||||||
}
|
|
||||||
|
|
||||||
func uploadFile(file string) {
|
|
||||||
fmt.Println(file)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user