Class: Cups::Printer
- Inherits:
-
Object
- Object
- Cups::Printer
- Defined in:
- lib/ffi-cups/printer.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.get_destination(name, connection = nil) ⇒ Printer
Get a destination by name.
-
.get_destinations(connection = nil) ⇒ Object
Get all destinations (printer devices).
Instance Method Summary collapse
-
#initialize(name, options = {}, connection = nil) ⇒ Printer
constructor
A new instance of Printer.
- #print_file(filename, title, options = {}) ⇒ Object
-
#state ⇒ Symbol
Returns the printer state.
-
#state_reasons ⇒ Array
Returns the reason for the printer state.
Constructor Details
#initialize(name, options = {}, connection = nil) ⇒ Printer
Returns a new instance of Printer.
7 8 9 10 11 |
# File 'lib/ffi-cups/printer.rb', line 7 def initialize(name, ={}, connection=nil) @name = name @options = @connection = connection end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
3 4 5 |
# File 'lib/ffi-cups/printer.rb', line 3 def connection @connection end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/ffi-cups/printer.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/ffi-cups/printer.rb', line 3 def @options end |
Class Method Details
.get_destination(name, connection = nil) ⇒ Printer
Get a destination by name
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ffi-cups/printer.rb', line 90 def self.get_destination(name, connection=nil) http = connection.nil? ? nil : connection.httpConnect2 # Get all destinations with cupsGetDests2 dests = FFI::MemoryPointer.new :pointer num_dests = FFI::Cups.cupsGetDests2(http, dests) # Get the destination from name with cupsGetDest p_dest = FFI::Cups.cupsGetDest(name, nil, num_dests, dests.get_pointer(0)) dest = Cups::Struct::Destination.new(p_dest) raise "Destination with name: #{name} not found!" if dest.null? printer = Cups::Printer.new(dest[:name].dup, (dest), connection) cupsFreeDests(num_dests, dests) Cups::Connection.close(http) if http return printer end |
.get_destinations(connection = nil) ⇒ Object
Get all destinations (printer devices)
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ffi-cups/printer.rb', line 74 def self.get_destinations(connection=nil) pointer = FFI::MemoryPointer.new :pointer dests = cupsGetDests2(pointer, connection) printers = [] dests.each do |d| printer = Cups::Printer.new(d[:name].dup, (d), connection) printers.push(printer) end cupsFreeDests(dests.count, pointer) return printers end |
Instance Method Details
#print_file(filename, title, options = {}) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ffi-cups/printer.rb', line 30 def print_file(filename, title, ={}) raise "File not found: #{filename}" unless File.exists? filename http = @connection.nil? ? nil : @connection.httpConnect2 # Get all destinations with cupsGetDests2 dests = FFI::MemoryPointer.new :pointer num_dests = FFI::Cups.cupsGetDests2(http, dests) # Get the destination from name with cupsGetDest p_dest = FFI::Cups.cupsGetDest(@name, nil, num_dests, dests.get_pointer(0)) dest = Cups::Struct::Destination.new(p_dest) raise "Destination with name: #{@name} not found!" if dest.null? = nil = 0 unless .empty? = FFI::MemoryPointer.new :pointer .each do |k, v| unless self.class.cupsCheckDestSupported(p_dest, k, v, http) raise "Option:#{k} #{v if v} not supported for printer: #{@name}" end = FFI::Cups.cupsAddOption(k, v, , ) end = .get_pointer(0) end job_id = FFI::Cups.cupsPrintFile2(http, @name, filename, title, , ) if job_id.zero? last_error = Cups.cupsLastErrorString() self.class.cupsFreeOptions(, ) unless .empty? raise last_error end #job = Cups::Job.new(job_id, title, @name) job = Cups::Job.get_job(job_id, @name, 0, @connection) self.class.cupsFreeOptions(, ) unless .empty? self.class.cupsFreeDests(num_dests, dests) Cups::Connection.close(http) return job end |
#state ⇒ Symbol
Returns the printer state
15 16 17 18 19 20 21 22 |
# File 'lib/ffi-cups/printer.rb', line 15 def state case ['printer-state'] when '3' then :idle when '4' then :printing when '5' then :stopped else :unkown end end |
#state_reasons ⇒ Array
Returns the reason for the printer state
26 27 28 |
# File 'lib/ffi-cups/printer.rb', line 26 def state_reasons ['printer-state-reasons'].split(/,/) end |