-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
228 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ Makefile | |
*.o | ||
*.bundle | ||
*.so | ||
*.out | ||
pkg | ||
doc | ||
.DS_Store | ||
test.rb | ||
*.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
ext/cups.c | ||
ext/extconf.c | ||
Manifest.txt | ||
Rakefile | ||
README | ||
test/cups_test.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,26 @@ | ||
Assumptions: | ||
== Cups | ||
|
||
* Setting up CUPS & requisite print drivers is already part of your sys-admin repertoire | ||
* You're only looking for destinations which have been set up locally | ||
Ello | ||
|
||
(The MIT License) | ||
|
||
Copyright (c) 2009 Chris Mowforth | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
'Software'), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | ||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | ||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
#!/bin/bash | ||
make clean | ||
ruby extconf.rb | ||
make | ||
sudo make install | ||
gem uninstall cups | ||
gem install cups |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Gem::Specification.new do |s| | ||
s.name = %q{cups} | ||
s.version = "0.0.1" | ||
s.authors = ["Chris Mowforth"] | ||
s.email = ["[email protected]"] | ||
s.summary = "Print stuff through the Common Unix Printing System using Ruby" | ||
s.files = Dir.glob("{ext,lib,test}/**/*") | ||
s.extensions << "ext/extconf.rb" | ||
s.homepage = "https://cups.99th.st/" | ||
s.has_rdoc = false | ||
s.extra_rdoc_files = ["README"] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
#include <cups/cups.h> | ||
#include <ruby/ruby.h> | ||
|
||
#ifndef MAXOPTS | ||
#define MAXOPTS 100 | ||
#endif | ||
|
||
static int num_options; | ||
static cups_option_t *options; | ||
cups_dest_t *dests, *dest; | ||
|
||
static VALUE job_init(VALUE self, VALUE filename, VALUE printer) { | ||
rb_iv_set(self, "@filename", filename); | ||
rb_iv_set(self, "@printer", printer); | ||
return self; | ||
} | ||
|
||
// Submit a print job to the selected printer or class | ||
static VALUE cups_print(VALUE self, VALUE file, VALUE printer) { | ||
int job_id; | ||
file = rb_iv_get(self, "@filename"); | ||
printer = rb_iv_get(self, "@printer"); | ||
|
||
char *fname = RSTRING_PTR(file); // Filename | ||
char *target = RSTRING_PTR(printer); // Target printer string | ||
|
||
FILE *fp = fopen(fname,"r"); | ||
// Check @filename actually exists... | ||
if( fp ) { | ||
fclose(fp); | ||
job_id = cupsPrintFile(target, fname, "rCUPS", num_options, options); // Do it. | ||
rb_iv_set(self, "@job_id", INT2NUM(job_id)); | ||
return job_id; | ||
} else { | ||
// and if it doesn't... | ||
rb_raise(rb_eRuntimeError, "Couldn't find file"); | ||
return self; | ||
} | ||
} | ||
|
||
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_new(); | ||
|
||
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; | ||
} | ||
|
||
// Get default printer or class. Returns a string or false if there is no default | ||
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; | ||
} | ||
} | ||
|
||
// Cancel the current job. Returns true if successful, false otherwise. | ||
static VALUE cups_cancel(VALUE self) { | ||
VALUE printer, job_id; | ||
printer = rb_iv_get(self, "@printer"); | ||
job_id = rb_iv_get(self, "@job_id"); | ||
|
||
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; | ||
} | ||
} | ||
|
||
// Convenience method for CUPS job id. Returns nil if job hasn't been submitted. | ||
static VALUE cups_job_id(VALUE self) { | ||
VALUE job_id = rb_iv_get(self, "@job_id"); | ||
return self; | ||
} | ||
|
||
// Get all jobs | ||
static VALUE cups_get_jobs(VALUE self, VALUE printer) { | ||
VALUE job_list, job_info_ary, jid, jtitle, juser, jsize, jformat; | ||
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_ary = rb_ary_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); | ||
|
||
rb_ary_push(job_info_ary, jtitle); | ||
rb_ary_push(job_info_ary, juser); | ||
rb_ary_push(job_info_ary, jsize); | ||
rb_ary_push(job_info_ary, jformat); | ||
|
||
rb_hash_aset(job_list, jid, job_info_ary); // And push it all into job_list hash | ||
} | ||
return job_list; | ||
} | ||
|
||
VALUE rubyCups, printJobs; | ||
|
||
void Init_cups() { | ||
rubyCups = rb_define_module("Cups"); | ||
printJobs = rb_define_class_under(rubyCups, "PrintJob", rb_cObject); | ||
|
||
// Cups::PrintJob Methods | ||
rb_define_method(printJobs, "initialize", job_init, 2); | ||
rb_define_method(printJobs, "print", cups_print, 0); | ||
rb_define_method(printJobs, "cancel", cups_cancel, 0); | ||
rb_define_method(printJobs, "job_id", cups_job_id, 0); | ||
|
||
// Cups Module Methods | ||
rb_define_singleton_method(rubyCups, "show_destinations", cups_show_dests, 0); | ||
rb_define_singleton_method(rubyCups, "default_printer", cups_get_default, 0); | ||
rb_define_singleton_method(rubyCups, "jobs_on", cups_get_jobs, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require "mkmf" | ||
|
||
cups_cflags = `cups-config --cflags`.chomp | ||
cups_libs = `cups-config --libs`.chomp | ||
|
||
with_cflags(cups_cflags) { | ||
with_ldflags(cups_libs) { | ||
create_makefile("cups") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,19 @@ | ||
require "cups" | ||
|
||
class Cups | ||
class << self | ||
def active_jobs(printer=default_printer) | ||
jobs_on(printer, 1, 0) | ||
end | ||
end | ||
end | ||
|
||
# pj = PrintJob.new("/Users/chris/Documents/CV.pdf", "soft_class") | ||
# pj.print | ||
# p pj.cancel | ||
# p pj.inspect | ||
|
||
# p Cups.show_destinations | ||
p Cups.all_jobs_on(Cups.default_printer) | ||
p Cups | ||
# p Cups.active_jobs | ||
# p Cups.default_printer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require "cups" | ||
require "test/unit" | ||
|
||
class CupsTest < Test::Unit::TestCase | ||
def test_same_printers_returned | ||
lplist = `lpstat -a`.split("\n").map { |pr| pr.split(' ').first } | ||
cups_destinations = Cups.show_destinations | ||
assert cups_destinations.is_a?(Array) | ||
assert_equal cups_destinations, lplist | ||
end | ||
|
||
def test_can_instantiate_print_job_object_with_correct_args | ||
assert_raises(ArgumentError) do | ||
Cups::PrintJob.new | ||
end | ||
|
||
assert_nothing_raised do | ||
Cups::PrintJob.new("test_printer", "/path") | ||
end | ||
end | ||
|
||
def test_we_cant_print_to_nonexistant_printers | ||
true | ||
end | ||
end |