Module: Cups
- Defined in:
- ext/cups.c
Defined Under Namespace
Classes: PrintJob
Class Method Summary collapse
-
.all_jobs(printer) ⇒ Hash
Get all jobs from default CUPS server.
-
.default_printer ⇒ String?
Get default printer or class.
-
.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
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 |
# File 'ext/cups.c', line 301
static VALUE cups_get_jobs(VALUE self, VALUE printer)
{
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);
// Let's elaborate on that job state...
switch (jobs[i].state) {
case IPP_JOB_PENDING :
jstate = rb_str_new2("Pending...");
break;
case IPP_JOB_HELD :
jstate = rb_str_new2("Held");
break;
case IPP_JOB_PROCESSING :
jstate = rb_str_new2("Processing...");
break;
case IPP_JOB_STOPPED :
jstate = rb_str_new2("Stopped");
break;
case IPP_JOB_CANCELED :
jstate = rb_str_new2("Cancelled");
break;
case IPP_JOB_ABORTED :
jstate = rb_str_new2("Aborted");
break;
case IPP_JOB_COMPLETED :
jstate = rb_str_new2("Completed");
}
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_printer ⇒ String?
Get default printer or class. Returns a string or false if there is no default
98 99 100 101 102 103 104 105 106 107 |
# File 'ext/cups.c', line 98
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;
}
}
|
.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.
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'ext/cups.c', line 368
static VALUE cups_get_options(VALUE self, VALUE printer)
{
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
78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'ext/cups.c', line 78
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;
}
|