Module: Cups

Defined in:
lib/cups/printer/printer.rb,
lib/cups/print_job/transient.rb,
ext/cups.c

Defined Under Namespace

Classes: PrintJob, Printer

Class Method Summary collapse

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

Returns:

  • (Hash)

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
414
415
# File 'ext/cups.c', line 376

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.

Returns:

  • (Boolean)

423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
# File 'ext/cups.c', line 423

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_printerString?

Get default printer or class. Returns a string or false if there is no default

Returns:

  • (String, nil)

193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'ext/cups.c', line 193

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;
  } else {
    return Qnil;
  }
}

.device_uri_for(printer_name) ⇒ String

Return uri for requested printer.

Returns:

  • (String)

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
474
475
# File 'ext/cups.c', line 446

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.

Returns:

  • (String)

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
474
475
# File 'ext/cups.c', line 446

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.

Returns:

  • (Hash, nil)

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
511
512
# File 'ext/cups.c', line 484

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_destinationsArray

Show all destinations on the default server

Returns:

  • (Array)

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'ext/cups.c', line 171

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;
}