Skip to content

Commit

Permalink
Mark offline printers as stopped (google#285)
Browse files Browse the repository at this point in the history
* detect printer offline state on Windows
* detect printer offline state with CUPS
  • Loading branch information
mfly authored and agoode committed Aug 3, 2016
1 parent 84508df commit e7a886a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
11 changes: 11 additions & 0 deletions cups/translate-attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ func getUUID(printerTags map[string][]string) string {
}

func getState(printerTags map[string][]string) cdd.CloudDeviceStateType {
// Some CUPS backends (e.g. usb-darwin) add offline-report
// to printer-state-reasons when the printer is offline/disconnected
reasons, exists := printerTags[attrPrinterStateReasons]
if exists && len(reasons) > 0 {
for _, reason := range reasons {
if reason == "offline-report" {
return cdd.CloudDeviceStateStopped
}
}
}

if s, ok := printerTags[attrPrinterState]; ok {
switch s[0] {
case "3":
Expand Down
22 changes: 22 additions & 0 deletions winspool/win32.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ const (
REG_QWORD_LITTLE_ENDIAN = 11
)

// PRINTER_INFO_2 attribute values
const (
PRINTER_ATTRIBUTE_QUEUED uint32 = 0x00000001
PRINTER_ATTRIBUTE_DIRECT uint32 = 0x00000002
PRINTER_ATTRIBUTE_DEFAULT uint32 = 0x00000004
PRINTER_ATTRIBUTE_SHARED uint32 = 0x00000008
PRINTER_ATTRIBUTE_NETWORK uint32 = 0x00000010
PRINTER_ATTRIBUTE_HIDDEN uint32 = 0x00000020
PRINTER_ATTRIBUTE_LOCAL uint32 = 0x00000040
PRINTER_ATTRIBUTE_ENABLE_DEVQ uint32 = 0x00000080
PRINTER_ATTRIBUTE_KEEPPRINTEDJOBS uint32 = 0x00000100
PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST uint32 = 0x00000200
PRINTER_ATTRIBUTE_WORK_OFFLINE uint32 = 0x00000400
PRINTER_ATTRIBUTE_ENABLE_BIDI uint32 = 0x00000800
PRINTER_ATTRIBUTE_RAW_ONLY uint32 = 0x00001000
PRINTER_ATTRIBUTE_PUBLISHED uint32 = 0x00002000
)

// PRINTER_INFO_2 status values.
const (
PRINTER_STATUS_PAUSED uint32 = 0x00000001
Expand Down Expand Up @@ -175,6 +193,10 @@ func (pi *PrinterInfo2) GetDevMode() *DevMode {
return pi.pDevMode
}

func (pi *PrinterInfo2) GetAttributes() uint32 {
return pi.attributes
}

func (pi *PrinterInfo2) GetStatus() uint32 {
return pi.status
}
Expand Down
11 changes: 8 additions & 3 deletions winspool/winspool.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func getSystemTags() (map[string]string, error) {
return tags, nil
}

func convertPrinterState(wsStatus uint32) *cdd.PrinterStateSection {
func convertPrinterState(wsStatus uint32, wsAttributes uint32) *cdd.PrinterStateSection {
state := cdd.PrinterStateSection{
State: cdd.CloudDeviceStateIdle,
VendorState: &cdd.VendorState{},
Expand Down Expand Up @@ -154,7 +154,12 @@ func convertPrinterState(wsStatus uint32) *cdd.PrinterStateSection {
}
state.VendorState.Item = append(state.VendorState.Item, vs)
}
if wsStatus&PRINTER_STATUS_OFFLINE != 0 {

// If PRINTER_ATTRIBUTE_WORK_OFFLINE is set
// spooler won't despool any jobs to the printer.
// At least for some USB printers, this flag is controlled
// automatically by the system depending on the state of physical connection.
if wsStatus&PRINTER_STATUS_OFFLINE != 0 || wsAttributes&PRINTER_ATTRIBUTE_WORK_OFFLINE != 0 {
state.State = cdd.CloudDeviceStateStopped
vs := cdd.VendorStateItem{
State: cdd.VendorStateError,
Expand Down Expand Up @@ -318,7 +323,7 @@ func (ws *WinSpool) GetPrinters() ([]lib.Printer, error) {
UUID: printerName, // TODO: Add something unique from host.
Manufacturer: manufacturer,
Model: model,
State: convertPrinterState(pi2.GetStatus()),
State: convertPrinterState(pi2.GetStatus(), pi2.GetAttributes()),
Description: &cdd.PrinterDescriptionSection{},
Tags: map[string]string{
"printer-location": pi2.GetLocation(),
Expand Down

0 comments on commit e7a886a

Please sign in to comment.