func updateHostnameProperty(object dbus.BusObject, expectedValue, property, setterName string, apply bool) (checkOK bool, err error) { propertyObject, err := object.GetProperty("org.freedesktop.hostname1." + property) if err != nil { return false, errwrap.Wrapf(err, "failed to get org.freedesktop.hostname1.%s", property) } if propertyObject.Value() == nil { return false, errwrap.Errorf("Unexpected nil value received when reading property %s", property) } propertyValue, ok := propertyObject.Value().(string) if !ok { return false, fmt.Errorf("Received unexpected type as %s value, expected string got '%T'", property, propertyValue) } // expected value and actual value match => checkOk if propertyValue == expectedValue { return true, nil } // nothing to do anymore if !apply { return false, nil } // attempting to apply the changes log.Printf("Changing %s: %s => %s", property, propertyValue, expectedValue) if err := object.Call("org.freedesktop.hostname1."+setterName, 0, expectedValue, false).Err; err != nil { return false, errwrap.Wrapf(err, "failed to call org.freedesktop.hostname1.%s", setterName) } // all good changes should now be applied again return false, nil }
func inhibit(o dbus.BusObject, appId, reason string, flags uint32) (cookie uint32) { c := o.Call("org.gnome.SessionManager.Inhibit", 0, appId, uint32(0), reason, flags) err := c.Store(&cookie) if err != nil { log.Print(err) return 0 } return }
// Call calls org.freedesktop.Introspectable.Introspect on a remote object // and returns the introspection data. func Call(o dbus.BusObject) (*Node, error) { var xmldata string var node Node err := o.Call("org.freedesktop.DBus.Introspectable.Introspect", 0).Store(&xmldata) if err != nil { return nil, err } err = xml.NewDecoder(strings.NewReader(xmldata)).Decode(&node) if err != nil { return nil, err } if node.Name == "" { node.Name = string(o.Path()) } return &node, nil }
func PrintList(obj dbus.BusObject) { files, err := obj.GetProperty(bus_interface + ".Tracks") if err != nil { log.Fatalf("Could not retrieve property: %s", err) } current, _ := obj.GetProperty(bus_interface + ".TrackPath") for i, file := range files.Value().([]string) { var prefix string if current.Value() != nil && file == current.Value().(string) { prefix = "*" } else { prefix = " " } log.Printf("%s %3d:%s\n", prefix, i, file) } }
func uninhibit(o dbus.BusObject, cookie uint32) { c := o.Go("org.gnome.SessionManager.Uninhibit", dbus.FlagNoReplyExpected, nil, cookie) if c.Err != nil { log.Print(c.Err) } }
func PrintStatus(obj dbus.BusObject) { title, err := obj.GetProperty(bus_interface + ".TrackTitle") if err != nil { log.Fatalf("Could not retrieve property: %s", err) } state, err := obj.GetProperty(bus_interface + ".PlaybackStatus") if err != nil { log.Fatalf("Could not retrieve property: %s", err) } metadata, err := obj.GetProperty(bus_interface + ".TrackMetadata") if err != nil { log.Fatalf("Could not retrieve property: %s", err) } length, err := obj.GetProperty(bus_interface + ".TrackLength") if err != nil { log.Fatalf("Could not retrieve property: %s", err) } var pos, percent float64 obj.Call(bus_interface+".TrackPosition", 0).Store(&pos, &percent) loop, err := obj.GetProperty(bus_interface + ".LoopStatus") if err != nil { log.Fatalf("Could not retrieve property: %s", err) } log.Printf("Title: %s\n", title.Value().(string)) log.Printf("Tags:\n") for key, val := range metadata.Value().(map[string]string) { log.Printf(" %s: %s\n", key, val) } pos_time := time.Duration(int64(pos)) * time.Second len_time := time.Duration(int64(length.Value().(float64))) * time.Second log.Printf( "[%s] %s/%s (%.f%%)\n", state.Value().(string), pos_time.String(), len_time.String(), percent, ) log.Printf("loop: %s\n", loop.Value().(string)) }