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)


372
373
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
# File 'ext/cups.c', line 372

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

.default_printerString?

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

Returns:

  • (String, nil)


190
191
192
193
194
195
196
197
198
199
200
# File 'ext/cups.c', line 190

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

.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)


421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'ext/cups.c', line 421

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)


170
171
172
173
174
175
176
177
178
179
180
181
182
# 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
  }
  return dest_list;
}