/*This file is automatically generated by pkg.deepin.io/dbus-generator. Don't edit it*/
package dbus

import "pkg.deepin.io/lib/dbus"
import "pkg.deepin.io/lib/dbus/property"
import "reflect"
import "sync"
import "runtime"
import "fmt"
import "errors"

/*prevent compile error*/
var _ = fmt.Println
var _ = runtime.SetFinalizer
var _ = sync.NewCond
var _ = reflect.TypeOf
var _ = property.BaseObserver{}

type DBusDaemon struct {
	Path     dbus.ObjectPath
	DestName string
	core     *dbus.Object

	signals       map[<-chan *dbus.Signal]struct{}
	signalsLocker sync.Mutex
}

func (obj *DBusDaemon) _createSignalChan() <-chan *dbus.Signal {
	obj.signalsLocker.Lock()
	ch := getBus().Signal()
	obj.signals[ch] = struct{}{}
	obj.signalsLocker.Unlock()
	return ch
}
func (obj *DBusDaemon) _deleteSignalChan(ch <-chan *dbus.Signal) {
	obj.signalsLocker.Lock()
	delete(obj.signals, ch)
	getBus().DetachSignal(ch)
	obj.signalsLocker.Unlock()
}
func DestroyDBusDaemon(obj *DBusDaemon) {
	obj.signalsLocker.Lock()
	defer obj.signalsLocker.Unlock()
	if obj.signals == nil {
		return
	}
	for ch, _ := range obj.signals {
		getBus().DetachSignal(ch)
	}
	obj.signals = nil

	runtime.SetFinalizer(obj, nil)

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus.Properties',sender='" + obj.DestName + "',member='PropertiesChanged'")
	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus',sender='" + obj.DestName + "',member='PropertiesChanged'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus',sender='" + obj.DestName + "',member='NameOwnerChanged'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus',sender='" + obj.DestName + "',member='NameLost'")

	dbusRemoveMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus',sender='" + obj.DestName + "',member='NameAcquired'")

}

func (obj *DBusDaemon) Hello() (arg0 string, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.Hello", 0).Store(&arg0)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) RequestName(arg0 string, arg1 uint32) (arg2 uint32, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.RequestName", 0, arg0, arg1).Store(&arg2)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) ReleaseName(arg0 string) (arg1 uint32, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.ReleaseName", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) StartServiceByName(arg0 string, arg1 uint32) (arg2 uint32, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.StartServiceByName", 0, arg0, arg1).Store(&arg2)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) UpdateActivationEnvironment(arg0 map[string]string) (_err error) {
	_err = obj.core.Call("org.freedesktop.DBus.UpdateActivationEnvironment", 0, arg0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) NameHasOwner(arg0 string) (arg1 bool, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.NameHasOwner", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) ListNames() (arg0 []string, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.ListNames", 0).Store(&arg0)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) ListActivatableNames() (arg0 []string, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.ListActivatableNames", 0).Store(&arg0)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) AddMatch(arg0 string) (_err error) {
	_err = obj.core.Call("org.freedesktop.DBus.AddMatch", 0, arg0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) RemoveMatch(arg0 string) (_err error) {
	_err = obj.core.Call("org.freedesktop.DBus.RemoveMatch", 0, arg0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) GetNameOwner(arg0 string) (arg1 string, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.GetNameOwner", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) ListQueuedOwners(arg0 string) (arg1 []string, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.ListQueuedOwners", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) GetConnectionUnixUser(arg0 string) (arg1 uint32, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.GetConnectionUnixUser", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) GetConnectionUnixProcessID(arg0 string) (arg1 uint32, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.GetConnectionUnixProcessID", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) GetAdtAuditSessionData(arg0 string) (arg1 []uint8, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.GetAdtAuditSessionData", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) GetConnectionSELinuxSecurityContext(arg0 string) (arg1 []uint8, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.GetConnectionSELinuxSecurityContext", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) GetConnectionAppArmorSecurityContext(arg0 string) (arg1 string, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.GetConnectionAppArmorSecurityContext", 0, arg0).Store(&arg1)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) ReloadConfig() (_err error) {
	_err = obj.core.Call("org.freedesktop.DBus.ReloadConfig", 0).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) GetId() (arg0 string, _err error) {
	_err = obj.core.Call("org.freedesktop.DBus.GetId", 0).Store(&arg0)
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *DBusDaemon) ConnectNameOwnerChanged(callback func(arg0 string, arg1 string, arg2 string)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.DBus.NameOwnerChanged" || 3 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*string)(nil)).Elem() {
				continue
			}
			if reflect.TypeOf(v.Body[1]) != reflect.TypeOf((*string)(nil)).Elem() {
				continue
			}
			if reflect.TypeOf(v.Body[2]) != reflect.TypeOf((*string)(nil)).Elem() {
				continue
			}

			callback(v.Body[0].(string), v.Body[1].(string), v.Body[2].(string))
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

func (obj *DBusDaemon) ConnectNameLost(callback func(arg0 string)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.DBus.NameLost" || 1 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*string)(nil)).Elem() {
				continue
			}

			callback(v.Body[0].(string))
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

func (obj *DBusDaemon) ConnectNameAcquired(callback func(arg0 string)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.DBus.NameAcquired" || 1 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*string)(nil)).Elem() {
				continue
			}

			callback(v.Body[0].(string))
		}
	}()
	return func() {
		obj._deleteSignalChan(sigChan)
	}
}

func NewDBusDaemon(destName string, path dbus.ObjectPath) (*DBusDaemon, error) {
	if !path.IsValid() {
		return nil, errors.New("The path of '" + string(path) + "' is invalid.")
	}

	core := getBus().Object(destName, path)

	obj := &DBusDaemon{Path: path, DestName: destName, core: core, signals: make(map[<-chan *dbus.Signal]struct{})}

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus',sender='" + obj.DestName + "',member='NameOwnerChanged'")

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus',sender='" + obj.DestName + "',member='NameLost'")

	dbusAddMatch("type='signal',path='" + string(obj.Path) + "',interface='org.freedesktop.DBus',sender='" + obj.DestName + "',member='NameAcquired'")

	runtime.SetFinalizer(obj, func(_obj *DBusDaemon) { DestroyDBusDaemon(_obj) })
	return obj, nil
}
