Пример #1
0
// GetJobState gets the current state of the job indicated by jobID.
func (c *CUPS) GetJobState(jobID uint32) (cdd.PrintJobStateDiff, error) {
	ja := C.newArrayOfStrings(C.int(len(jobAttributes)))
	defer C.freeStringArrayAndStrings(ja, C.int(len(jobAttributes)))
	for i, attribute := range jobAttributes {
		C.setStringArrayValue(ja, C.int(i), C.CString(attribute))
	}

	response, err := c.cc.getJobAttributes(C.int(jobID), ja)
	if err != nil {
		return cdd.PrintJobStateDiff{}, err
	}

	// cupsDoRequest() returned ipp_t pointer needs explicit free.
	defer C.ippDelete(response)

	s := C.ippFindAttribute(response, C.JOB_STATE, C.IPP_TAG_ENUM)
	state := int32(C.getAttributeIntegerValue(s, C.int(0)))

	return convertJobState(state), nil
}
Пример #2
0
// attributesToMap converts a slice of C.ipp_attribute_t to a
// string:string "tag" map.
func attributesToMap(attributes []*C.ipp_attribute_t) map[string][]string {
	m := make(map[string][]string)

	for _, a := range attributes {
		key := C.GoString(a.name)
		count := int(a.num_values)
		values := make([]string, count)

		switch a.value_tag {
		case C.IPP_TAG_NOVALUE, C.IPP_TAG_NOTSETTABLE:
			// No value means no value.

		case C.IPP_TAG_INTEGER, C.IPP_TAG_ENUM:
			for i := 0; i < count; i++ {
				values[i] = strconv.FormatInt(int64(C.getAttributeIntegerValue(a, C.int(i))), 10)
			}

		case C.IPP_TAG_BOOLEAN:
			for i := 0; i < count; i++ {
				if int(C.getAttributeIntegerValue(a, C.int(i))) == 0 {
					values[i] = "false"
				} else {
					values[i] = "true"
				}
			}

		case C.IPP_TAG_TEXTLANG, C.IPP_TAG_NAMELANG, C.IPP_TAG_TEXT, C.IPP_TAG_NAME, C.IPP_TAG_KEYWORD, C.IPP_TAG_URI, C.IPP_TAG_URISCHEME, C.IPP_TAG_CHARSET, C.IPP_TAG_LANGUAGE, C.IPP_TAG_MIMETYPE:
			for i := 0; i < count; i++ {
				values[i] = C.GoString(C.getAttributeStringValue(a, C.int(i)))
			}

		case C.IPP_TAG_DATE:
			for i := 0; i < count; i++ {
				date := C.getAttributeDateValue(a, C.int(i))
				t := convertIPPDateToTime(date)
				values[i] = strconv.FormatInt(t.Unix(), 10)
			}

		case C.IPP_TAG_RESOLUTION:
			for i := 0; i < count; i++ {
				xres, yres := C.int(0), C.int(0)
				C.getAttributeValueResolution(a, C.int(i), &xres, &yres)
				values[i] = fmt.Sprintf("%dx%dppi", int(xres), int(yres))
			}

		case C.IPP_TAG_RANGE:
			for i := 0; i < count; i++ {
				upper, lower := C.int(0), C.int(0)
				C.getAttributeValueRange(a, C.int(i), &lower, &upper)
				values[i] = fmt.Sprintf("%d~%d", int(lower), int(upper))
			}

		default:
			if count > 0 {
				values = []string{"unknown or unsupported type"}
			}
		}

		if len(values) == 1 && (values[0] == "none" || len(values[0]) == 0) {
			values = []string{}
		}
		m[key] = values
	}

	return m
}