Commit 40bc9bed authored by Roman Alifanov's avatar Roman Alifanov

godbus -> gio

parent c73e9a7f
...@@ -5,12 +5,10 @@ go 1.25.0 ...@@ -5,12 +5,10 @@ go 1.25.0
require ( require (
github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20250703085337-e94555b846b6 github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20250703085337-e94555b846b6
github.com/diamondburned/gotk4/pkg v0.3.2-0.20250703063411-16654385f59a github.com/diamondburned/gotk4/pkg v0.3.2-0.20250703063411-16654385f59a
github.com/godbus/dbus/v5 v5.2.0
) )
require ( require (
github.com/KarpelesLab/weak v0.1.1 // indirect github.com/KarpelesLab/weak v0.1.1 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6 // indirect go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6 // indirect
golang.org/x/sync v0.18.0 // indirect golang.org/x/sync v0.18.0 // indirect
golang.org/x/sys v0.38.0 // indirect
) )
...@@ -4,11 +4,7 @@ github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20250703085337-e94555b846b6 h1 ...@@ -4,11 +4,7 @@ github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20250703085337-e94555b846b6 h1
github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20250703085337-e94555b846b6/go.mod h1:ZzYiyPe0TqsukfPHi0sK/WwKzm0wIJdSRylLnuvAZNw= github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20250703085337-e94555b846b6/go.mod h1:ZzYiyPe0TqsukfPHi0sK/WwKzm0wIJdSRylLnuvAZNw=
github.com/diamondburned/gotk4/pkg v0.3.2-0.20250703063411-16654385f59a h1:dN2jYYZ71hFhoKFSn24pQdKWLZb/XDydBt8pEIkFjJo= github.com/diamondburned/gotk4/pkg v0.3.2-0.20250703063411-16654385f59a h1:dN2jYYZ71hFhoKFSn24pQdKWLZb/XDydBt8pEIkFjJo=
github.com/diamondburned/gotk4/pkg v0.3.2-0.20250703063411-16654385f59a/go.mod h1:O9K8+PGNFGJpAu8+u5D2Sn5Wae4hxWzHB+AeZNbV/2Q= github.com/diamondburned/gotk4/pkg v0.3.2-0.20250703063411-16654385f59a/go.mod h1:O9K8+PGNFGJpAu8+u5D2Sn5Wae4hxWzHB+AeZNbV/2Q=
github.com/godbus/dbus/v5 v5.2.0 h1:3WexO+U+yg9T70v9FdHr9kCxYlazaAXUhx2VMkbfax8=
github.com/godbus/dbus/v5 v5.2.0/go.mod h1:3AAv2+hPq5rdnr5txxxRwiGjPXamgoIHgz9FPBfOp3c=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6 h1:lGdhQUN/cnWdSH3291CUuxSEqc+AsGTiDxPP3r2J0l4= go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6 h1:lGdhQUN/cnWdSH3291CUuxSEqc+AsGTiDxPP3r2J0l4=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E= go4.org/unsafe/assume-no-moving-gc v0.0.0-20231121144256-b99613f794b6/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I= golang.org/x/sync v0.18.0 h1:kr88TuHDroi+UVf+0hZnirlk8o8T+4MrK6mr60WkH/I=
golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sync v0.18.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc=
golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
...@@ -2,25 +2,32 @@ package apm ...@@ -2,25 +2,32 @@ package apm
import ( import (
"SystemUpdater/lib/apm/sudbus" "SystemUpdater/lib/apm/sudbus"
"context"
"encoding/json" "encoding/json"
"github.com/diamondburned/gotk4/pkg/gio/v2"
"github.com/diamondburned/gotk4/pkg/glib/v2"
) )
func GetPackageInfo(name string) InfoResponse { func GetPackageInfo(name string) InfoResponse {
conn := sudbus.GetConnection() conn := sudbus.GetConnection()
reply := conn.BusObject.Call("org.altlinux.APM.system.Info", 0, name, "Ximper System Updater") reply, err := conn.Proxy.CallSync(
context.Background(),
"Info",
glib.NewVariantTuple([]*glib.Variant{
glib.NewVariantString(name),
glib.NewVariantString("Ximper System Updater"),
}),
gio.DBusCallFlagsNone,
-1,
)
if reply.Err != nil { if err != nil {
panic("Error during async method call: " + reply.Err.Error()) panic("Error during async method call: " + err.Error())
}
if len(reply.Body) < 1 {
panic("Unexpected reply body")
} }
responseStr, ok := reply.Body[0].(string) responseStr := reply.ChildValue(0).String()
if !ok {
panic("Unexpected response type")
}
var response InfoRootResponse var response InfoRootResponse
if err := json.Unmarshal([]byte(responseStr), &response); err != nil { if err := json.Unmarshal([]byte(responseStr), &response); err != nil {
......
package sudbus package sudbus
import ( import (
"context"
"log" "log"
"github.com/godbus/dbus/v5" "github.com/diamondburned/gotk4/pkg/gio/v2"
) )
type Connection struct { type Connection struct {
Conn *dbus.Conn Conn *gio.DBusConnection
BusObject dbus.BusObject Proxy *gio.DBusProxy
} }
var conn *Connection var conn *Connection
func GetConnection() *Connection { func GetConnection() *Connection {
if conn == nil { if conn == nil {
connection, err := dbus.ConnectSystemBus() c, err := gio.BusGetSync(context.Background(), gio.BusTypeSystem)
if err != nil {
log.Fatal("DBus connect:", err)
}
proxy, err := gio.NewDBusProxyForBusSync(
context.Background(),
gio.BusTypeSystem,
gio.DBusProxyFlagsNone,
nil,
"org.altlinux.APM",
"/org/altlinux/APM",
"org.altlinux.APM.system",
)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal("DBus proxy:", err)
} }
obj := connection.Object("org.altlinux.APM", dbus.ObjectPath("/org/altlinux/APM")) conn = &Connection{
conn = &Connection{connection, obj} Conn: c,
Proxy: proxy,
}
} }
return conn return conn
} }
...@@ -2,13 +2,14 @@ package apm ...@@ -2,13 +2,14 @@ package apm
import ( import (
"SystemUpdater/lib/apm/sudbus" "SystemUpdater/lib/apm/sudbus"
"context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"log" "log"
"sync" "sync"
"github.com/diamondburned/gotk4/pkg/gio/v2"
"github.com/diamondburned/gotk4/pkg/glib/v2" "github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/godbus/dbus/v5"
) )
type UpdaterSource interface { type UpdaterSource interface {
...@@ -42,62 +43,25 @@ type Response struct { ...@@ -42,62 +43,25 @@ type Response struct {
Error bool `json:"error"` Error bool `json:"error"`
} }
func updatesOutputProcessing(reply *dbus.Call) PackageChanges {
if reply.Err != nil {
panic("Error during async method call: " + reply.Err.Error())
}
if len(reply.Body) < 1 {
panic("Unexpected reply body")
}
responseStr, ok := reply.Body[0].(string)
if !ok {
panic("Unexpected response type")
}
var response Response
if err := json.Unmarshal([]byte(responseStr), &response); err != nil {
panic("Failed to parse response: " + err.Error())
}
return response.Data.Info
}
type UpdatesSources map[string]UpdaterSource type UpdatesSources map[string]UpdaterSource
func NewUpdatesSources() UpdatesSources { func NewUpdatesSources() UpdatesSources {
conn := sudbus.GetConnection() conn := sudbus.GetConnection()
return UpdatesSources{ return UpdatesSources{
"System": &SystemUpdatesSource{conn: conn.Conn, obj: conn.BusObject}, "System": &SystemUpdatesSource{Proxy: conn.Proxy, Conn: conn.Conn},
// "Kernel": &KernelUpdatesSource{conn: conn, obj: obj}, // "Kernel": &KernelUpdatesSource{conn: conn, obj: obj},
} }
} }
type SystemUpdatesSource struct { type SystemUpdatesSource struct {
conn *dbus.Conn Proxy *gio.DBusProxy
obj dbus.BusObject Conn *gio.DBusConnection
} }
func (s *SystemUpdatesSource) RunUpgrade( func (s *SystemUpdatesSource) RunUpgrade(
onDone func(), onDone func(),
onLog func(EventData), onLog func(EventData),
) error { ) error {
conn, err := dbus.SystemBus()
if err != nil {
return fmt.Errorf("connect system bus: %w", err)
}
err = conn.AddMatchSignal(
dbus.WithMatchSender("org.altlinux.APM"),
dbus.WithMatchObjectPath("/org/altlinux/APM"),
)
if err != nil {
return fmt.Errorf("add match signal: %w", err)
}
obj := conn.Object("org.altlinux.APM", "/org/altlinux/APM")
stopSignals := make(chan struct{}) stopSignals := make(chan struct{})
wg := sync.WaitGroup{} wg := sync.WaitGroup{}
wg.Add(2) wg.Add(2)
...@@ -105,52 +69,63 @@ func (s *SystemUpdatesSource) RunUpgrade( ...@@ -105,52 +69,63 @@ func (s *SystemUpdatesSource) RunUpgrade(
go func() { go func() {
defer wg.Done() defer wg.Done()
sigCh := make(chan *dbus.Signal, 10) subID := s.Conn.SignalSubscribe(
conn.Signal(sigCh) "org.altlinux.APM",
"org.altlinux.APM",
for { "Notification",
select { "/org/altlinux/APM",
case sig := <-sigCh: "",
if sig == nil { gio.DBusSignalFlagsNone,
func(conn *gio.DBusConnection,
senderName string,
objectPath string,
interfaceName string,
signalName string,
parameters *glib.Variant) {
select {
case <-stopSignals:
return return
default:
} }
fmt.Printf("RAW SIGNAL: %+v\n", sig) raw := parameters.ChildValue(0).String()
if len(sig.Body) > 0 { var data EventData
raw, ok := sig.Body[0].(string) if err := json.Unmarshal([]byte(raw), &data); err == nil {
if ok { glib.IdleAdd(func() { onLog(data) })
var data EventData
if err := json.Unmarshal([]byte(raw), &data); err == nil { } else {
glib.IdleAdd(func() { onLog(data) }) fmt.Println("JSON decode error:", err)
} else {
fmt.Println("JSON decode error:", err)
}
}
} }
},
)
case <-stopSignals: <-stopSignals
return s.Conn.SignalUnsubscribe(subID)
}
}
}() }()
go func() { go func() {
defer wg.Done() defer wg.Done()
call := obj.Call( args := glib.NewVariantTuple([]*glib.Variant{
"org.altlinux.APM.system.Upgrade", glib.NewVariantString("Ximper System Updater"),
0, })
"Ximper System Updater",
_, err := s.Proxy.CallSync(
context.Background(),
"Upgrade",
args,
gio.DBusCallFlagsNone,
-1,
) )
if call.Err != nil { if err != nil {
log.Println("DBus call error:", call.Err) log.Println("Upgrade error:", err)
} else if len(call.Body) > 0 {
log.Println("DBus reply:", call.Body[0])
} }
close(stopSignals) close(stopSignals)
glib.IdleAdd(onDone) glib.IdleAdd(onDone)
}() }()
...@@ -159,16 +134,25 @@ func (s *SystemUpdatesSource) RunUpgrade( ...@@ -159,16 +134,25 @@ func (s *SystemUpdatesSource) RunUpgrade(
} }
func (s *SystemUpdatesSource) GetPackageChanges() PackageChanges { func (s *SystemUpdatesSource) GetPackageChanges() PackageChanges {
reply := s.obj.Call("org.altlinux.APM.system.CheckUpgrade", 0, "Ximper System Updater") args := glib.NewVariantTuple([]*glib.Variant{
return updatesOutputProcessing(reply) glib.NewVariantString("Ximper System Updater"),
} })
reply, err := s.Proxy.CallSync(
context.Background(),
"CheckUpgrade",
args,
gio.DBusCallFlagsNone,
-1,
)
if err != nil {
panic("DBus CheckUpgrade error: " + err.Error())
}
type KernelUpdatesSource struct { var parsed Response
conn *dbus.Conn if err := json.Unmarshal([]byte(reply.ChildValue(0).String()), &parsed); err != nil {
obj dbus.BusObject panic("Failed to parse CheckUpgrade: " + err.Error())
} }
func (k *KernelUpdatesSource) GetPackageChanges() PackageChanges { return parsed.Data.Info
reply := k.obj.Call("org.altlinux.APM.kernel.CheckUpdateKernel", 0, "Ximper System Updater")
return updatesOutputProcessing(reply)
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment