Module: Cups
- Defined in:
- lib/cups/printer/printer.rb,
lib/cups/print_job/transient.rb,
ext/cups.c
Defined Under Namespace
Class Method Summary collapse
-
.all_jobs(printer) ⇒ Hash
Get all jobs from default CUPS server.
-
.cancel_print(cups_id, printer_name) ⇒ Boolean
Cancel the print job.
-
.default_printer ⇒ String?
Get default printer or class.
-
.device_uri_for(printer_name) ⇒ String
Return uri for requested printer.
-
.device_uri_for(printer_name) ⇒ String
Return uri for requested printer.
-
.options_for(name) ⇒ Hash?
Get all options from CUPS server with name.
-
.show_destinations ⇒ Array
Show all destinations on the default server.
Class Method Details
.all_jobs(printer) ⇒ Hash
Get all jobs from default CUPS server. Takes a single printer/class string argument. Returned hash keys are CUPS job ids, and the values are hashes of job info with keys:
- :title, :submitted_by, :size, :format, :state
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'ext/cups.c', line 374
static VALUE cups_get_jobs(VALUE self, VALUE printer)
{
// Don't have to lift a finger unless the printer exists.
if (!printer_exists(printer)){
rb_raise(rb_eRuntimeError, "The printer or destination doesn't exist!");
}
VALUE job_list, job_info_hash, jid, jtitle, juser, jsize, jformat, jstate;
int job_id;
int num_jobs;
cups_job_t *jobs;
ipp_jstate_t state;
int i;
char *printer_arg = RSTRING_PTR(printer);
num_jobs = cupsGetJobs(&jobs, printer_arg, 1, -1); // Get jobs
job_list = rb_hash_new();
for (i = 0; i < num_jobs; i ++) { // Construct a hash of individual job info
job_info_hash = rb_hash_new();
jid = INT2NUM(jobs[i].id);
jtitle = rb_str_new2(jobs[i].title);
juser = rb_str_new2(jobs[i].user);
jsize = INT2NUM(jobs[i].size);
jformat = rb_str_new2(jobs[i].format);
jstate = ipp_state_to_symbol(jobs[i].state);
rb_hash_aset(job_info_hash, ID2SYM(rb_intern("title")), jtitle);
rb_hash_aset(job_info_hash, ID2SYM(rb_intern("submitted_by")), juser);
rb_hash_aset(job_info_hash, ID2SYM(rb_intern("size")), jsize);
rb_hash_aset(job_info_hash, ID2SYM(rb_intern("format")), jformat);
rb_hash_aset(job_info_hash, ID2SYM(rb_intern("state")), jstate);
rb_hash_aset(job_list, jid, job_info_hash); // And push it all into job_list hash
}
// Free job array
cupsFreeJobs(num_jobs, jobs);
return job_list;
}
|
.cancel_print(cups_id, printer_name) ⇒ Boolean
Cancel the print job. Returns true if successful, false otherwise.
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 |
# File 'ext/cups.c', line 421
static VALUE cups_cancel_print(int argc, VALUE* argv, VALUE self)
{
VALUE printer, job_id;
rb_scan_args(argc, argv, "20", &job_id, &printer);
if (NIL_P(job_id)) {
return Qfalse; // If @job_id is nil
} else { // Otherwise attempt to cancel
int job = NUM2INT(job_id);
char *target = RSTRING_PTR(printer); // Target printer string
int cancellation;
cancellation = cupsCancelJob(target, job);
return Qtrue;
}
}
|
.default_printer ⇒ String?
Get default printer or class. Returns a string or false if there is no default
192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'ext/cups.c', line 192
static VALUE cups_get_default(VALUE self)
{
const char *default_printer;
default_printer = cupsGetDefault();
if (default_printer != NULL) {
VALUE def_p = rb_str_new2(default_printer);
return def_p;
}
// should return nil if no default printer is found!
}
|
.device_uri_for(printer_name) ⇒ String
Return uri for requested printer.
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'ext/cups.c', line 444
static VALUE cups_get_device_uri(VALUE self, VALUE printer)
{
if (!printer_exists(printer))
{
rb_raise(rb_eRuntimeError, "The printer or destination doesn't exist!");
}
VALUE options_list;
http_t *http;
ipp_t *request;
ipp_t *response;
ipp_attribute_t *attr;
char uri[1024];
char *location;
char *name = RSTRING_PTR(printer);
request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if((attr = ippFindAttribute(response, "device-uri", IPP_TAG_URI)) != NULL)
{
return rb_str_new2(attr->values[0].string.text);
}
ippDelete(response);
}
return Qtrue;
}
|
.device_uri_for(printer_name) ⇒ String
Return uri for requested printer.
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
# File 'ext/cups.c', line 444
static VALUE cups_get_device_uri(VALUE self, VALUE printer)
{
if (!printer_exists(printer))
{
rb_raise(rb_eRuntimeError, "The printer or destination doesn't exist!");
}
VALUE options_list;
http_t *http;
ipp_t *request;
ipp_t *response;
ipp_attribute_t *attr;
char uri[1024];
char *location;
char *name = RSTRING_PTR(printer);
request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if((attr = ippFindAttribute(response, "device-uri", IPP_TAG_URI)) != NULL)
{
return rb_str_new2(attr->values[0].string.text);
}
ippDelete(response);
}
return Qtrue;
}
|
.options_for(name) ⇒ Hash?
Get all options from CUPS server with name. Returns a hash with key/value pairs based on server options, or nil if no server with name.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 |
# File 'ext/cups.c', line 482
static VALUE cups_get_options(VALUE self, VALUE printer)
{
// Don't have to lift a finger unless the printer exists.
if (!printer_exists(printer)){
rb_raise(rb_eRuntimeError, "The printer or destination doesn't exist!");
}
VALUE options_list;
int i;
char *printer_arg = RSTRING_PTR(printer);
options_list = rb_hash_new();
cups_dest_t *dests;
int num_dests = cupsGetDests(&dests);
cups_dest_t *dest = cupsGetDest(printer_arg, NULL, num_dests, dests);
if (dest == NULL) {
cupsFreeDests(num_dests, dests);
return Qnil;
} else {
for(i =0; i< dest->num_options; i++) {
rb_hash_aset(options_list, rb_str_new2(dest->options[i].name), rb_str_new2(dest->options[i].value));
}
cupsFreeDests(num_dests, dests);
return options_list;
}
}
|
.show_destinations ⇒ Array
Show all destinations on the default server
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'ext/cups.c', line 170
static VALUE cups_show_dests(VALUE self)
{
VALUE dest_list;
int i;
int num_dests = cupsGetDests(&dests); // Size of dest_list array
dest_list = rb_ary_new2(num_dests);
for (i = num_dests, dest = dests; i > 0; i --, dest ++) {
VALUE destination = rb_str_new2(dest->name);
rb_ary_push(dest_list, destination); // Add this testination name to dest_list string
}
cupsFreeDests(num_dests, dests);
return dest_list;
}
|