Ejemplo n.º 1
0
Archivo: cups.c Proyecto: siftd106/cups
/*
 * call-seq:
 *   Cups.options_for(name) -> Hash or nil
 *
 * 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.
 */
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;
  }

}
Ejemplo n.º 2
0
/*
* call-seq:
*   PrintJob.new(filename, printer=nil)
*
* Initializes a new PrintJob object. If no target printer/class is specified, the default is chosen.
* Note the specified file does not have to exist until print is called.
*/
static VALUE job_init(int argc, VALUE* argv, VALUE self)
{
  VALUE filename, printer, job_options;

  rb_scan_args(argc, argv, "12", &filename, &printer, &job_options);

  rb_iv_set(self, "@filename", filename);
  rb_iv_set(self, "@url_path", rb_str_new2(cupsServer()));

  if (NIL_P(job_options)) {
    rb_iv_set(self, "@job_options", rb_hash_new());
  } else {
    rb_iv_set(self, "@job_options", job_options);
  }

  if (NIL_P(printer)) {

    // Fall back to default printer
    VALUE def_p = rb_funcall(rubyCups, rb_intern("default_printer"), 0);

    if (def_p == Qfalse) {
			rb_raise(rb_eRuntimeError, "There is no default printer!");
		} else {
			rb_iv_set(self, "@printer", def_p);
		}

  } else {
    if (printer_exists(printer)) {
      rb_iv_set(self, "@printer", printer);
    } else {
      rb_raise(rb_eRuntimeError, "The printer or destination doesn't exist!");
    }
  }
  return self;
}