func getVendorState(printerTags map[string][]string) *cdd.VendorState {
	reasons, exists := printerTags[attrPrinterStateReasons]
	if !exists || len(reasons) < 1 {
		return nil
	}

	sort.Strings(reasons)
	vendorState := &cdd.VendorState{Item: make([]cdd.VendorStateItem, len(reasons))}
	for i, reason := range reasons {
		vs := cdd.VendorStateItem{DescriptionLocalized: cdd.NewLocalizedString(reason)}
		if strings.HasSuffix(reason, "-error") {
			vs.State = cdd.VendorStateError
		} else if strings.HasSuffix(reason, "-warning") {
			vs.State = cdd.VendorStateWarning
		} else if strings.HasSuffix(reason, "-report") {
			vs.State = cdd.VendorStateInfo
		} else {
			vs.State = cdd.VendorStateError
		}
		vendorState.Item[i] = vs
	}

	return vendorState
}
示例#2
0
// tagsToPrinter converts a map of tags to a Printer.
func tagsToPrinter(printerTags map[string][]string, systemTags map[string]string, infoToDisplayName bool) lib.Printer {
	tags := make(map[string]string)

	for k, v := range printerTags {
		tags[k] = strings.Join(v, ",")
	}
	for k, v := range systemTags {
		tags[k] = v
	}

	var name string
	if n, ok := printerTags[attrPrinterName]; ok {
		name = n[0]
	}
	var uuid string
	if u, ok := printerTags[attrPrinterUUID]; ok {
		uuid = u[0]
	}

	state := cdd.PrinterStateSection{}

	if s, ok := printerTags[attrPrinterState]; ok {
		switch s[0] {
		case "3":
			state.State = cdd.CloudDeviceStateIdle
		case "4":
			state.State = cdd.CloudDeviceStateProcessing
		case "5":
			state.State = cdd.CloudDeviceStateStopped
		default:
			state.State = cdd.CloudDeviceStateIdle
		}
	}

	if reasons, ok := printerTags[attrPrinterStateReasons]; ok && len(reasons) > 0 {
		sort.Strings(reasons)
		state.VendorState = &cdd.VendorState{Item: make([]cdd.VendorStateItem, len(reasons))}
		for i, reason := range reasons {
			vendorState := cdd.VendorStateItem{DescriptionLocalized: cdd.NewLocalizedString(reason)}
			if strings.HasSuffix(reason, "-error") {
				vendorState.State = cdd.VendorStateError
			} else if strings.HasSuffix(reason, "-warning") {
				vendorState.State = cdd.VendorStateWarning
			} else if strings.HasSuffix(reason, "-report") {
				vendorState.State = cdd.VendorStateInfo
			} else {
				vendorState.State = cdd.VendorStateInfo
			}
			state.VendorState.Item[i] = vendorState
		}
	}

	markers, markerState := convertMarkers(printerTags[attrMarkerNames], printerTags[attrMarkerTypes], printerTags[attrMarkerLevels])
	state.MarkerState = markerState
	description := cdd.PrinterDescriptionSection{Marker: markers}

	p := lib.Printer{
		Name:        name,
		UUID:        uuid,
		State:       &state,
		Description: &description,
		Tags:        tags,
	}
	p.SetTagshash()

	if pi, ok := printerTags[attrPrinterInfo]; ok && infoToDisplayName {
		p.DefaultDisplayName = pi[0]
	}

	return p
}