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

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 Job struct {
	Path     dbus.ObjectPath
	DestName string
	core     *dbus.Object

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

	Operation       *dbusPropertyJobOperation
	Progress        *dbusPropertyJobProgress
	ProgressValid   *dbusPropertyJobProgressValid
	Bytes           *dbusPropertyJobBytes
	Rate            *dbusPropertyJobRate
	StartTime       *dbusPropertyJobStartTime
	ExpectedEndTime *dbusPropertyJobExpectedEndTime
	Objects         *dbusPropertyJobObjects
	StartedByUID    *dbusPropertyJobStartedByUID
	Cancelable      *dbusPropertyJobCancelable
}

func (obj *Job) _createSignalChan() <-chan *dbus.Signal {
	obj.signalsLocker.Lock()
	ch := getBus().Signal()
	obj.signals[ch] = struct{}{}
	obj.signalsLocker.Unlock()
	return ch
}
func (obj *Job) _deleteSignalChan(ch <-chan *dbus.Signal) {
	obj.signalsLocker.Lock()
	delete(obj.signals, ch)
	getBus().DetachSignal(ch)
	obj.signalsLocker.Unlock()
}
func DestroyJob(obj *Job) {
	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.UDisks2.Job',sender='" + obj.DestName + "',member='PropertiesChanged'")

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

	obj.Operation.Reset()
	obj.Progress.Reset()
	obj.ProgressValid.Reset()
	obj.Bytes.Reset()
	obj.Rate.Reset()
	obj.StartTime.Reset()
	obj.ExpectedEndTime.Reset()
	obj.Objects.Reset()
	obj.StartedByUID.Reset()
	obj.Cancelable.Reset()
}

func (obj *Job) Cancel(options map[string]dbus.Variant) (_err error) {
	_err = obj.core.Call("org.freedesktop.UDisks2.Job.Cancel", 0, options).Store()
	if _err != nil {
		fmt.Println(_err)
	}
	return
}

func (obj *Job) ConnectCompleted(callback func(success bool, message string)) func() {
	sigChan := obj._createSignalChan()
	go func() {
		for v := range sigChan {
			if v.Path != obj.Path || v.Name != "org.freedesktop.UDisks2.Job.Completed" || 2 != len(v.Body) {
				continue
			}
			if reflect.TypeOf(v.Body[0]) != reflect.TypeOf((*bool)(nil)).Elem() {
				continue
			}
			if reflect.TypeOf(v.Body[1]) != reflect.TypeOf((*string)(nil)).Elem() {
				continue
			}

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

type dbusPropertyJobOperation struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobOperation) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.Operation is not writable")
}

func (this *dbusPropertyJobOperation) Get() string {
	v, _ := this.GetValue()
	return v.(string)
}
func (this *dbusPropertyJobOperation) GetValue() (interface{} /*string*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "Operation").Store(&r)
	if err == nil && r.Signature().String() == "s" {
		return r.Value().(string), nil
	}
	return *new(string), err
}
func (this *dbusPropertyJobOperation) GetType() reflect.Type {
	return reflect.TypeOf((*string)(nil)).Elem()
}

type dbusPropertyJobProgress struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobProgress) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.Progress is not writable")
}

func (this *dbusPropertyJobProgress) Get() float64 {
	v, _ := this.GetValue()
	return v.(float64)
}
func (this *dbusPropertyJobProgress) GetValue() (interface{} /*float64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "Progress").Store(&r)
	if err == nil && r.Signature().String() == "d" {
		return r.Value().(float64), nil
	}
	return *new(float64), err
}
func (this *dbusPropertyJobProgress) GetType() reflect.Type {
	return reflect.TypeOf((*float64)(nil)).Elem()
}

type dbusPropertyJobProgressValid struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobProgressValid) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.ProgressValid is not writable")
}

func (this *dbusPropertyJobProgressValid) Get() bool {
	v, _ := this.GetValue()
	return v.(bool)
}
func (this *dbusPropertyJobProgressValid) GetValue() (interface{} /*bool*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "ProgressValid").Store(&r)
	if err == nil && r.Signature().String() == "b" {
		return r.Value().(bool), nil
	}
	return *new(bool), err
}
func (this *dbusPropertyJobProgressValid) GetType() reflect.Type {
	return reflect.TypeOf((*bool)(nil)).Elem()
}

type dbusPropertyJobBytes struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobBytes) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.Bytes is not writable")
}

func (this *dbusPropertyJobBytes) Get() uint64 {
	v, _ := this.GetValue()
	return v.(uint64)
}
func (this *dbusPropertyJobBytes) GetValue() (interface{} /*uint64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "Bytes").Store(&r)
	if err == nil && r.Signature().String() == "t" {
		return r.Value().(uint64), nil
	}
	return *new(uint64), err
}
func (this *dbusPropertyJobBytes) GetType() reflect.Type {
	return reflect.TypeOf((*uint64)(nil)).Elem()
}

type dbusPropertyJobRate struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobRate) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.Rate is not writable")
}

func (this *dbusPropertyJobRate) Get() uint64 {
	v, _ := this.GetValue()
	return v.(uint64)
}
func (this *dbusPropertyJobRate) GetValue() (interface{} /*uint64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "Rate").Store(&r)
	if err == nil && r.Signature().String() == "t" {
		return r.Value().(uint64), nil
	}
	return *new(uint64), err
}
func (this *dbusPropertyJobRate) GetType() reflect.Type {
	return reflect.TypeOf((*uint64)(nil)).Elem()
}

type dbusPropertyJobStartTime struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobStartTime) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.StartTime is not writable")
}

func (this *dbusPropertyJobStartTime) Get() uint64 {
	v, _ := this.GetValue()
	return v.(uint64)
}
func (this *dbusPropertyJobStartTime) GetValue() (interface{} /*uint64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "StartTime").Store(&r)
	if err == nil && r.Signature().String() == "t" {
		return r.Value().(uint64), nil
	}
	return *new(uint64), err
}
func (this *dbusPropertyJobStartTime) GetType() reflect.Type {
	return reflect.TypeOf((*uint64)(nil)).Elem()
}

type dbusPropertyJobExpectedEndTime struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobExpectedEndTime) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.ExpectedEndTime is not writable")
}

func (this *dbusPropertyJobExpectedEndTime) Get() uint64 {
	v, _ := this.GetValue()
	return v.(uint64)
}
func (this *dbusPropertyJobExpectedEndTime) GetValue() (interface{} /*uint64*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "ExpectedEndTime").Store(&r)
	if err == nil && r.Signature().String() == "t" {
		return r.Value().(uint64), nil
	}
	return *new(uint64), err
}
func (this *dbusPropertyJobExpectedEndTime) GetType() reflect.Type {
	return reflect.TypeOf((*uint64)(nil)).Elem()
}

type dbusPropertyJobObjects struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobObjects) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.Objects is not writable")
}

func (this *dbusPropertyJobObjects) Get() []dbus.ObjectPath {
	v, _ := this.GetValue()
	return v.([]dbus.ObjectPath)
}
func (this *dbusPropertyJobObjects) GetValue() (interface{} /*[]dbus.ObjectPath*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "Objects").Store(&r)
	if err == nil && r.Signature().String() == "ao" {
		return r.Value().([]dbus.ObjectPath), nil
	}
	return *new([]dbus.ObjectPath), err
}
func (this *dbusPropertyJobObjects) GetType() reflect.Type {
	return reflect.TypeOf((*[]dbus.ObjectPath)(nil)).Elem()
}

type dbusPropertyJobStartedByUID struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobStartedByUID) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.StartedByUID is not writable")
}

func (this *dbusPropertyJobStartedByUID) Get() uint32 {
	v, _ := this.GetValue()
	return v.(uint32)
}
func (this *dbusPropertyJobStartedByUID) GetValue() (interface{} /*uint32*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "StartedByUID").Store(&r)
	if err == nil && r.Signature().String() == "u" {
		return r.Value().(uint32), nil
	}
	return *new(uint32), err
}
func (this *dbusPropertyJobStartedByUID) GetType() reflect.Type {
	return reflect.TypeOf((*uint32)(nil)).Elem()
}

type dbusPropertyJobCancelable struct {
	*property.BaseObserver
	core *dbus.Object
}

func (this *dbusPropertyJobCancelable) SetValue(notwritable interface{}) {
	fmt.Println("org.freedesktop.UDisks2.Job.Cancelable is not writable")
}

func (this *dbusPropertyJobCancelable) Get() bool {
	v, _ := this.GetValue()
	return v.(bool)
}
func (this *dbusPropertyJobCancelable) GetValue() (interface{} /*bool*/, error) {
	var r dbus.Variant
	err := this.core.Call("org.freedesktop.DBus.Properties.Get", 0, "org.freedesktop.UDisks2.Job", "Cancelable").Store(&r)
	if err == nil && r.Signature().String() == "b" {
		return r.Value().(bool), nil
	}
	return *new(bool), err
}
func (this *dbusPropertyJobCancelable) GetType() reflect.Type {
	return reflect.TypeOf((*bool)(nil)).Elem()
}

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

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

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

	obj.Operation = &dbusPropertyJobOperation{&property.BaseObserver{}, core}
	obj.Progress = &dbusPropertyJobProgress{&property.BaseObserver{}, core}
	obj.ProgressValid = &dbusPropertyJobProgressValid{&property.BaseObserver{}, core}
	obj.Bytes = &dbusPropertyJobBytes{&property.BaseObserver{}, core}
	obj.Rate = &dbusPropertyJobRate{&property.BaseObserver{}, core}
	obj.StartTime = &dbusPropertyJobStartTime{&property.BaseObserver{}, core}
	obj.ExpectedEndTime = &dbusPropertyJobExpectedEndTime{&property.BaseObserver{}, core}
	obj.Objects = &dbusPropertyJobObjects{&property.BaseObserver{}, core}
	obj.StartedByUID = &dbusPropertyJobStartedByUID{&property.BaseObserver{}, core}
	obj.Cancelable = &dbusPropertyJobCancelable{&property.BaseObserver{}, core}

	dbusAddMatch("type='signal',path='" + string(path) + "',interface='org.freedesktop.DBus.Properties',sender='" + destName + "',member='PropertiesChanged'")
	dbusAddMatch("type='signal',path='" + string(path) + "',interface='org.freedesktop.UDisks2.Job',sender='" + destName + "',member='PropertiesChanged'")
	sigChan := obj._createSignalChan()
	go func() {
		typeString := reflect.TypeOf("")
		typeKeyValues := reflect.TypeOf(map[string]dbus.Variant{})
		typeArrayValues := reflect.TypeOf([]string{})
		for v := range sigChan {
			if v.Name == "org.freedesktop.DBus.Properties.PropertiesChanged" &&
				len(v.Body) == 3 &&
				reflect.TypeOf(v.Body[0]) == typeString &&
				reflect.TypeOf(v.Body[1]) == typeKeyValues &&
				reflect.TypeOf(v.Body[2]) == typeArrayValues &&
				v.Body[0].(string) == "org.freedesktop.UDisks2.Job" {
				props := v.Body[1].(map[string]dbus.Variant)
				for key, _ := range props {
					if false {
					} else if key == "Operation" {
						obj.Operation.Notify()

					} else if key == "Progress" {
						obj.Progress.Notify()

					} else if key == "ProgressValid" {
						obj.ProgressValid.Notify()

					} else if key == "Bytes" {
						obj.Bytes.Notify()

					} else if key == "Rate" {
						obj.Rate.Notify()

					} else if key == "StartTime" {
						obj.StartTime.Notify()

					} else if key == "ExpectedEndTime" {
						obj.ExpectedEndTime.Notify()

					} else if key == "Objects" {
						obj.Objects.Notify()

					} else if key == "StartedByUID" {
						obj.StartedByUID.Notify()

					} else if key == "Cancelable" {
						obj.Cancelable.Notify()
					}
				}
			} else if v.Name == "org.freedesktop.UDisks2.Job.PropertiesChanged" && len(v.Body) == 1 && reflect.TypeOf(v.Body[0]) == typeKeyValues {
				for key, _ := range v.Body[0].(map[string]dbus.Variant) {
					if false {
					} else if key == "Operation" {
						obj.Operation.Notify()

					} else if key == "Progress" {
						obj.Progress.Notify()

					} else if key == "ProgressValid" {
						obj.ProgressValid.Notify()

					} else if key == "Bytes" {
						obj.Bytes.Notify()

					} else if key == "Rate" {
						obj.Rate.Notify()

					} else if key == "StartTime" {
						obj.StartTime.Notify()

					} else if key == "ExpectedEndTime" {
						obj.ExpectedEndTime.Notify()

					} else if key == "Objects" {
						obj.Objects.Notify()

					} else if key == "StartedByUID" {
						obj.StartedByUID.Notify()

					} else if key == "Cancelable" {
						obj.Cancelable.Notify()
					}
				}
			}
		}
	}()

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

	runtime.SetFinalizer(obj, func(_obj *Job) { DestroyJob(_obj) })
	return obj, nil
}
