Skip to content

Commit

Permalink
Merge pull request #28 from Cellebyte/master
Browse files Browse the repository at this point in the history
[#27 printer-state] Added support for printer-state in CupsPrinter
  • Loading branch information
harwey committed Nov 14, 2019
2 parents 4b47dad + e910fa8 commit 77f9d00
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 12 deletions.
37 changes: 26 additions & 11 deletions src/main/java/org/cups4j/CupsPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class CupsPrinter {
private URL printerURL = null;
private String name = null;
private PrinterStateEnum state = null;
private String description = null;
private String location = null;
private boolean isDefault = false;
Expand All @@ -51,7 +52,7 @@ public class CupsPrinter {

/**
* Constructor
*
*
* @param printerURL
* @param printerName
* @param isDefault
Expand All @@ -72,7 +73,7 @@ private void updateClassAttribute() {

/**
* Print method
*
*
* @param printJob
* @return PrintRequestResult
* @throws Exception
Expand Down Expand Up @@ -290,7 +291,7 @@ public PrintRequestResult print(PrintJob job, int jobId, boolean lastDocument) {
}

/**
*
*
* @param map
* @param name
* @param value
Expand All @@ -309,7 +310,7 @@ private void addAttribute(Map<String, String> map, String name, String value) {

/**
* Get a list of jobs
*
*
* @param whichJobs
* completed, not completed or all
* @param user
Expand All @@ -329,7 +330,7 @@ public List<PrintJobAttributes> getJobs(WhichJobsEnum whichJobs, String user, bo

/**
* Get current status for the print job with the given ID.
*
*
* @param jobID
* @return job status
* @throws Exception
Expand All @@ -340,7 +341,7 @@ public JobStateEnum getJobStatus(int jobID) throws Exception {

/**
* Get current status for the print job with the given ID
*
*
* @param userName
* @param jobID
* @return job status
Expand All @@ -355,7 +356,7 @@ public JobStateEnum getJobStatus(String userName, int jobID) throws Exception {

/**
* Get the URL for this printer
*
*
* @return printer URL
*/
public URL getPrinterURL() {
Expand All @@ -364,7 +365,7 @@ public URL getPrinterURL() {

/**
* Is this the default printer
*
*
* @return true if this is the default printer false otherwise
*/
public boolean isDefault() {
Expand All @@ -378,7 +379,7 @@ protected void setDefault(boolean isDefault) {
/**
* Get a String representation of this printer consisting of the printer URL
* and the name
*
*
* @return String
*/
public String toString() {
Expand All @@ -391,13 +392,25 @@ public String toString() {
* For a printer http://localhost:631/printers/printername 'printername' will
* be returned.
* </p>
*
*
* @return printer name
*/
public String getName() {
return name;
}

/**
* Get state of this printer.
* <p>
* For a printer http://localhost:631/printers/printername 'printer-state' will
* be returned.
* </p>
*
* @return printer state
*/
public PrinterStateEnum getState() {
return state;
}
/**
* Get location attribute for this printer
*
Expand All @@ -413,7 +426,7 @@ public void setLocation(String location) {

/**
* Get description attribute for this printer
*
*
* @return description
*/
public String getDescription() {
Expand Down Expand Up @@ -457,6 +470,8 @@ public void setName(String name) {
this.name = name;
}

public void setState(PrinterStateEnum state) { this.state = state; }

public void setDescription(String description) {
this.description = description;
}
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/org/cups4j/PrinterStateEnum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.cups4j;

public enum PrinterStateEnum {
IDLE(3, "idle"), PRINTING(4, "printing"), STOPPED(5, "stopped");

private Integer value;
private String stateName;

PrinterStateEnum(Integer value, String stateName) {
this.value = value;
this.stateName = stateName;
}

@Override
public String toString() {
return this.stateName;
}

public Integer getValue() {
return value;
}
public String getStateName() {
return stateName;
}

public static PrinterStateEnum fromInteger(Integer value) {
if (value != null) {
for (PrinterStateEnum printerState : PrinterStateEnum.values()) {
if (value == printerState.getValue()) {
return printerState;
}
}
}
return null;
}

public static PrinterStateEnum fromStringInteger(String value) {
if (value != null) {
for (PrinterStateEnum printerState : PrinterStateEnum.values()) {
if (value.equalsIgnoreCase(printerState.getValue().toString())) {
return printerState;
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;

import org.cups4j.CupsPrinter;
import org.cups4j.PrinterStateEnum;
import org.cups4j.ipp.attributes.Attribute;
import org.cups4j.ipp.attributes.AttributeGroup;
import org.cups4j.ipp.attributes.AttributeValue;
Expand Down Expand Up @@ -49,7 +50,7 @@ public List<CupsPrinter> getPrinters(String hostname, int port) throws Exception
Map<String, String> map = new HashMap<String, String>();
map.put(
"requested-attributes",
"copies-supported page-ranges-supported printer-name printer-info printer-location printer-make-and-model printer-uri-supported media-supported media-default sides-supported sides-default orientation-requested-supported printer-resolution-supported printer printer-resolution-default number-up-default number-up-supported document-format-supported print-color-mode-supported print-color-mode-default");
"copies-supported page-ranges-supported printer-name printer-info printer-state printer-location printer-make-and-model printer-uri-supported media-supported media-default sides-supported sides-default orientation-requested-supported printer-resolution-supported printer printer-resolution-default number-up-default number-up-supported document-format-supported print-color-mode-supported print-color-mode-default");
// map.put("requested-attributes", "all");
this.ippPort = port;

Expand All @@ -64,6 +65,7 @@ public List<CupsPrinter> getPrinters(String hostname, int port) throws Exception
String printerName = null;
String printerLocation = null;
String printerDescription = null;
PrinterStateEnum printerState = null;
List<String> mediaSupportedList = new ArrayList<String>();
String mediaDefault = null;
List<String> printerResolutionSupported = new ArrayList<String>();
Expand Down Expand Up @@ -107,6 +109,8 @@ public List<CupsPrinter> getPrinters(String hostname, int port) throws Exception
sidesSupported = getAttributeValues(attr);
} else if (attr.getName().equals("sides-default")) {
sidesDefault = getAttributeValue(attr);
} else if (attr.getName().equals("printer-state")) {
printerState = PrinterStateEnum.fromStringInteger(getAttributeValue(attr));
}
}
URL printerUrl = null;
Expand All @@ -121,6 +125,7 @@ public List<CupsPrinter> getPrinters(String hostname, int port) throws Exception
}

printer = new CupsPrinter(printerUrl, printerName, false);
printer.setState(printerState);
printer.setLocation(printerLocation);
printer.setDescription(printerDescription);
printer.setMediaDefault(mediaDefault);
Expand Down

0 comments on commit 77f9d00

Please sign in to comment.