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

godbus -> gio

parent c73e9a7f
......@@ -5,12 +5,10 @@ go 1.25.0
require (
github.com/diamondburned/gotk4-adwaita/pkg v0.0.0-20250703085337-e94555b846b6
github.com/diamondburned/gotk4/pkg v0.3.2-0.20250703063411-16654385f59a
github.com/godbus/dbus/v5 v5.2.0
)
require (
github.com/KarpelesLab/weak v0.1.1 // 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/sys v0.38.0 // indirect
)
......@@ -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/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/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/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/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
import (
"SystemUpdater/lib/apm/sudbus"
"context"
"encoding/json"
"github.com/diamondburned/gotk4/pkg/gio/v2"
"github.com/diamondburned/gotk4/pkg/glib/v2"
)
func GetPackageInfo(name string) InfoResponse {
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 {
panic("Error during async method call: " + reply.Err.Error())
}
if len(reply.Body) < 1 {
panic("Unexpected reply body")
if err != nil {
panic("Error during async method call: " + err.Error())
}
responseStr, ok := reply.Body[0].(string)
if !ok {
panic("Unexpected response type")
}
responseStr := reply.ChildValue(0).String()
var response InfoRootResponse
if err := json.Unmarshal([]byte(responseStr), &response); err != nil {
......
package sudbus
import (
"context"
"log"
"github.com/godbus/dbus/v5"
"github.com/diamondburned/gotk4/pkg/gio/v2"
)
type Connection struct {
Conn *dbus.Conn
BusObject dbus.BusObject
Conn *gio.DBusConnection
Proxy *gio.DBusProxy
}
var conn *Connection
func GetConnection() *Connection {
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 {
log.Fatal(err)
log.Fatal("DBus proxy:", err)
}
obj := connection.Object("org.altlinux.APM", dbus.ObjectPath("/org/altlinux/APM"))
conn = &Connection{connection, obj}
conn = &Connection{
Conn: c,
Proxy: proxy,
}
}
return conn
}
......@@ -2,13 +2,14 @@ package apm
import (
"SystemUpdater/lib/apm/sudbus"
"context"
"encoding/json"
"fmt"
"log"
"sync"
"github.com/diamondburned/gotk4/pkg/gio/v2"
"github.com/diamondburned/gotk4/pkg/glib/v2"
"github.com/godbus/dbus/v5"
)
type UpdaterSource interface {
......@@ -42,62 +43,25 @@ type Response struct {
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
func NewUpdatesSources() UpdatesSources {
conn := sudbus.GetConnection()
return UpdatesSources{
"System": &SystemUpdatesSource{conn: conn.Conn, obj: conn.BusObject},
"System": &SystemUpdatesSource{Proxy: conn.Proxy, Conn: conn.Conn},
// "Kernel": &KernelUpdatesSource{conn: conn, obj: obj},
}
}
type SystemUpdatesSource struct {
conn *dbus.Conn
obj dbus.BusObject
Proxy *gio.DBusProxy
Conn *gio.DBusConnection
}
func (s *SystemUpdatesSource) RunUpgrade(
onDone func(),
onLog func(EventData),
) 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{})
wg := sync.WaitGroup{}
wg.Add(2)
......@@ -105,52 +69,63 @@ func (s *SystemUpdatesSource) RunUpgrade(
go func() {
defer wg.Done()
sigCh := make(chan *dbus.Signal, 10)
conn.Signal(sigCh)
for {
select {
case sig := <-sigCh:
if sig == nil {
subID := s.Conn.SignalSubscribe(
"org.altlinux.APM",
"org.altlinux.APM",
"Notification",
"/org/altlinux/APM",
"",
gio.DBusSignalFlagsNone,
func(conn *gio.DBusConnection,
senderName string,
objectPath string,
interfaceName string,
signalName string,
parameters *glib.Variant) {
select {
case <-stopSignals:
return
default:
}
fmt.Printf("RAW SIGNAL: %+v\n", sig)
if len(sig.Body) > 0 {
raw, ok := sig.Body[0].(string)
if ok {
var data EventData
if err := json.Unmarshal([]byte(raw), &data); err == nil {
glib.IdleAdd(func() { onLog(data) })
} else {
fmt.Println("JSON decode error:", err)
}
}
raw := parameters.ChildValue(0).String()
var data EventData
if err := json.Unmarshal([]byte(raw), &data); err == nil {
glib.IdleAdd(func() { onLog(data) })
} else {
fmt.Println("JSON decode error:", err)
}
},
)
case <-stopSignals:
return
}
}
<-stopSignals
s.Conn.SignalUnsubscribe(subID)
}()
go func() {
defer wg.Done()
call := obj.Call(
"org.altlinux.APM.system.Upgrade",
0,
"Ximper System Updater",
args := glib.NewVariantTuple([]*glib.Variant{
glib.NewVariantString("Ximper System Updater"),
})
_, err := s.Proxy.CallSync(
context.Background(),
"Upgrade",
args,
gio.DBusCallFlagsNone,
-1,
)
if call.Err != nil {
log.Println("DBus call error:", call.Err)
} else if len(call.Body) > 0 {
log.Println("DBus reply:", call.Body[0])
if err != nil {
log.Println("Upgrade error:", err)
}
close(stopSignals)
glib.IdleAdd(onDone)
}()
......@@ -159,16 +134,25 @@ func (s *SystemUpdatesSource) RunUpgrade(
}
func (s *SystemUpdatesSource) GetPackageChanges() PackageChanges {
reply := s.obj.Call("org.altlinux.APM.system.CheckUpgrade", 0, "Ximper System Updater")
return updatesOutputProcessing(reply)
}
args := glib.NewVariantTuple([]*glib.Variant{
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 {
conn *dbus.Conn
obj dbus.BusObject
}
var parsed Response
if err := json.Unmarshal([]byte(reply.ChildValue(0).String()), &parsed); err != nil {
panic("Failed to parse CheckUpgrade: " + err.Error())
}
func (k *KernelUpdatesSource) GetPackageChanges() PackageChanges {
reply := k.obj.Call("org.altlinux.APM.kernel.CheckUpdateKernel", 0, "Ximper System Updater")
return updatesOutputProcessing(reply)
return parsed.Data.Info
}
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