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)


400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'ext/cups.c', line 400

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)


447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'ext/cups.c', line 447

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);
    
    if(target != NULL)
      free(target);
      
    return Qtrue;
  }
}

.default_printerString?

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

Returns:

  • (String, nil)


205
206
207
208
209
210
211
212
213
214
215
216
# File 'ext/cups.c', line 205

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.

Returns:

  • (String)


473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'ext/cups.c', line 473

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)


473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'ext/cups.c', line 473

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)


511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'ext/cups.c', line 511

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)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'ext/cups.c', line 183

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