Skip to content

Commit

Permalink
Cups version
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Sep 21, 2023
1 parent 4f916f8 commit cc3572e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/main/kotlin/de/gmuth/ipp/client/CupsClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package de.gmuth.ipp.client
*/

import de.gmuth.ipp.client.IppExchangeException.ClientErrorNotFoundException
import de.gmuth.ipp.client.IppWhichJobs.All
import de.gmuth.ipp.core.IppOperation
import de.gmuth.ipp.core.IppOperation.*
import de.gmuth.ipp.core.IppRequest
Expand Down Expand Up @@ -67,6 +68,13 @@ open class CupsClient(
cupsPrinterRequest(CupsSetDefault, printerName)
)

val version: String by lazy {
getPrinters().run {
if (isNotEmpty()) last()
else IppPrinter(getJobs(All).last().printerUri)
}.cupsVersion
}

fun cupsPrinterUri(printerName: String) = with(cupsUri) {
val optionalPort = if (port > 0) ":$port" else ""
URI("$scheme://$host$optionalPort/printers/$printerName")
Expand Down Expand Up @@ -164,7 +172,11 @@ open class CupsClient(
.apply { workDirectory = cupsClientWorkDirectory }
}

fun getJobs(whichJobs: IppWhichJobs? = null, limit: Int? = null, requestedAttributes: List<String>? = null) =
fun getJobs(
whichJobs: IppWhichJobs? = null,
limit: Int? = null,
requestedAttributes: List<String>? = ippPrinter.getJobsRequestedAttributes
) =
ippPrinter.getJobs(whichJobs = whichJobs, limit = limit, requestedAttributes = requestedAttributes)

//----------------------------
Expand Down Expand Up @@ -243,7 +255,7 @@ open class CupsClient(
private val jobOwners = mutableSetOf<String>()

fun getJobsAndSaveDocuments(
whichJobs: IppWhichJobs = IppWhichJobs.All,
whichJobs: IppWhichJobs = All,
updateJobAttributes: Boolean = false,
commandToHandleSavedFile: String? = null
): Collection<IppJob> {
Expand All @@ -253,7 +265,8 @@ open class CupsClient(
whichJobs,
requestedAttributes = listOf(
"job-id", "job-uri", "job-printer-uri", "job-originating-user-name",
"job-name", "job-state", "job-state-reasons", "number-of-documents"
"job-name", "job-state", "job-state-reasons",
if(version < "1.6.0") "document-count" else "number-of-documents"
)
// wired: do not modify above set
// job-originating-user-name is missing when document-count or job-originating-host-name ist requested
Expand Down Expand Up @@ -308,12 +321,12 @@ open class CupsClient(
}

// ------------------------------
// get and save documents for job
// Get and save documents for job
// ------------------------------

private fun getAndSaveDocuments(
job: IppJob,
onSuccessUpdateJobAttributes: Boolean = true,
onSuccessUpdateJobAttributes: Boolean = false,
optionalCommandToHandleFile: String? = null
): Collection<File> {
var documents: Collection<IppDocument> = emptyList()
Expand All @@ -326,15 +339,16 @@ open class CupsClient(
ippExchangeException.httpStatus!! != 401
}

val configuredUserName = config.userName
if (configuredUserName != null) getDocuments()
val jobOwnersIterator = jobOwners.iterator()
while (jobOwnersIterator.hasNext()) {
config.userName = jobOwnersIterator.next()
log.fine { "set userName '${config.userName}'" }
if (getDocuments()) break
if(!getDocuments()) {
val configuredUserName = config.userName
jobOwners.forEach {
config.userName = it
log.fine { "set userName '${config.userName}'" }
if (getDocuments()) return@forEach
}
config.userName = configuredUserName
}
config.userName = configuredUserName

documents.onEach { document ->
document.save(job.printerDirectory(), overwrite = true)
optionalCommandToHandleFile?.let { document.runCommand(it) }
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/de/gmuth/ipp/client/IppPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ open class IppPrinter(

fun marker(color: CupsMarker.Color) = markers.single { it.color == color }

val cupsVersion: String
get() = attributes.getTextValue("cups-version")

//-----------------

fun isIdle() = state == Idle
Expand Down Expand Up @@ -394,6 +397,7 @@ open class IppPrinter(
limit: Int? = null,
requestedAttributes: List<String>? = getJobsRequestedAttributes
): Collection<IppJob> {
log.fine { "getJobs(whichJobs=$whichJobs, requestedAttributes=$requestedAttributes)"}
val request = ippRequest(GetJobs, requestedAttributes = requestedAttributes).apply {
operationGroup.run {
whichJobs?.keyword?.let {
Expand Down
7 changes: 7 additions & 0 deletions src/test/kotlin/de/gmuth/ipp/client/CupsClientTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,16 @@ class CupsClientTests {
assertEquals(5, markers.size)
assertTrue(isAcceptingJobs)
assertTrue(isCups())
assertEquals("2.2.5", cupsVersion)
}
}

@Test
fun getCupsVersion() {
ippClientMock.mockResponse("Get-Printer-Attributes.ipp", "printers/CUPS_HP_LaserJet_100_color_MFP_M175")
assertEquals("2.2.5", cupsClient.version)
}

@Test
fun getPrinterFails() {
assertFailsWith<IppException> { // no such cups printer
Expand Down

0 comments on commit cc3572e

Please sign in to comment.