Class: CupsPrinter
- Inherits:
-
Object
- Object
- CupsPrinter
- Defined in:
- lib/cupsffi/printer.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .get_all_printer_attrs(args = {}) ⇒ Object
- .get_all_printer_names(args = {}) ⇒ Object
- .get_connection(args = {}) ⇒ Object
- .release_connection(connection) ⇒ Object
- .walk_attributes(connection) ⇒ Object
- .walk_sub_attributes(dest) ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #cancel_all_jobs ⇒ Object
- #close ⇒ Object
- #get_all_jobs(which = CupsFFI::CUPS_WHICHJOBS_ACTIVE) ⇒ Object
-
#initialize(name, args = {}) ⇒ CupsPrinter
constructor
A new instance of CupsPrinter.
- #print_data(data, mime_type, options = {}) ⇒ Object
- #print_file(file_name, options = {}) ⇒ Object
- #state ⇒ Object
Constructor Details
#initialize(name, args = {}) ⇒ CupsPrinter
Returns a new instance of CupsPrinter.
63 64 65 66 67 |
# File 'lib/cupsffi/printer.rb', line 63 def initialize(name, args = {}) raise "Printer not found" unless CupsPrinter.get_all_printer_names(args).include? name @name = name @connection = CupsPrinter.get_connection(args) end |
Instance Attribute Details
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
26 27 28 |
# File 'lib/cupsffi/printer.rb', line 26 def connection @connection end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
26 27 28 |
# File 'lib/cupsffi/printer.rb', line 26 def name @name end |
Class Method Details
.get_all_printer_attrs(args = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/cupsffi/printer.rb', line 83 def self.get_all_printer_attrs(args = {}) connection = get_connection(args) hash = {} walk_attributes(connection) do |dest| pname = dest[:name].dup hash[pname] ||= {} walk_sub_attributes(dest) do || hash[pname][[:name].dup] = [:value].dup end end release_connection(connection) hash end |
.get_all_printer_names(args = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/cupsffi/printer.rb', line 73 def self.get_all_printer_names(args = {}) connection = get_connection(args) ary = [] walk_attributes(connection) do |dest| ary.push(dest[:name].dup) end release_connection(connection) ary end |
.get_connection(args = {}) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cupsffi/printer.rb', line 28 def self.get_connection(args = {}) hostname = args[:hostname] port = args[:port] || 631 if hostname.nil? return CupsFFI::CUPS_HTTP_DEFAULT else connection = CupsFFI::httpConnectEncrypt(hostname, port.to_i, CupsFFI::cupsEncryption()) raise "Printserver at #{hostname}:#{port} not available" if connection.null? return connection end end |
.release_connection(connection) ⇒ Object
59 60 61 |
# File 'lib/cupsffi/printer.rb', line 59 def self.release_connection(connection) CupsFFI::httpClose(connection) end |
.walk_attributes(connection) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/cupsffi/printer.rb', line 42 def self.walk_attributes(connection) p = FFI::MemoryPointer.new :pointer dest_count = CupsFFI::cupsGetDests2(connection, p) dest_count.times do |i| dest = CupsFFI::CupsDestS.new(p.get_pointer(0) + (CupsFFI::CupsDestS.size * i)) yield dest end CupsFFI::cupsFreeDests(dest_count, p.get_pointer(0)) end |
.walk_sub_attributes(dest) ⇒ Object
52 53 54 55 56 57 |
# File 'lib/cupsffi/printer.rb', line 52 def self.walk_sub_attributes(dest) dest[:num_options].times do |j| = CupsFFI::CupsOptionS.new(dest[:options] + (CupsFFI::CupsOptionS.size * j)) yield end end |
Instance Method Details
#attributes ⇒ Object
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/cupsffi/printer.rb', line 97 def attributes hash = {} CupsPrinter.walk_attributes(@connection) do |dest| next unless dest[:name] == @name CupsPrinter.walk_sub_attributes(dest) do || hash[[:name].dup] = [:value].dup end end hash end |
#cancel_all_jobs ⇒ Object
190 191 192 193 |
# File 'lib/cupsffi/printer.rb', line 190 def cancel_all_jobs r = CupsFFI::cupsCancelJob2(@connection, @name, CupsFFI::CUPS_JOBID_ALL) raise CupsFFI::cupsLastErrorString() if r == 0 end |
#close ⇒ Object
69 70 71 |
# File 'lib/cupsffi/printer.rb', line 69 def close CupsPrinter.release_connection(@connection) end |
#get_all_jobs(which = CupsFFI::CUPS_WHICHJOBS_ACTIVE) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/cupsffi/printer.rb', line 195 def get_all_jobs(which = CupsFFI::CUPS_WHICHJOBS_ACTIVE) pointer = FFI::MemoryPointer.new :pointer job_count = CupsFFI::cupsGetJobs(pointer, @name, 0, which) jobs = [] job_count.times do |i| job = CupsFFI::CupsJobS.new(pointer.get_pointer(0) + (CupsFFI::CupsJobS.size * i)) jobs << { :completed_time => job[:completed_time].nil? ? nil : DateTime.strptime(job[:completed_time].to_s, "%s"), :creation_time => job[:creation_time].nil? ? nil : DateTime.strptime(job[:creation_time].to_s, "%s"), :processing_time => job[:processing_time].nil? ? nil : DateTime.strptime(job[:processing_time].to_s, "%s"), :dest => job[:dest].nil? ? nil : job[:dest].dup, :format => job[:format].nil? ? nil : job[:format].dup, :id => job[:id], :priority => job[:priority], :size => job[:size], :title => job[:title].nil? ? nil : job[:title].dup, :user => job[:user].nil? ? nil : job[:user].dup, :state => job[:state].nil? ? nil : job[:state] } end CupsFFI::cupsFreeJobs(job_count, pointer.get_pointer(0)) jobs end |
#print_data(data, mime_type, options = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/cupsffi/printer.rb', line 149 def print_data(data, mime_type, = {}) = nil = 0 unless .empty? () = FFI::MemoryPointer.new :pointer .map do |key,value| = CupsFFI::cupsAddOption(key.to_s, value.to_s, , ) end = .get_pointer(0) end job_id = CupsFFI::cupsCreateJob(@connection, @name, 'data job', , ) if job_id == 0 last_error = CupsFFI::cupsLastErrorString() CupsFFI::cupsFreeOptions(, ) unless .nil? raise last_error end # Copy the data into a char pointer to allow null bytes data_pointer = FFI::MemoryPointer.new(:char, data.bytesize) data_pointer.put_bytes(0, data) http_status = CupsFFI::cupsStartDocument(@connection, @name, job_id, 'my doc', mime_type, 1) http_status = CupsFFI::cupsWriteRequestData(@connection, data_pointer, data.bytesize) ipp_status = CupsFFI::cupsFinishDocument(@connection, @name) data_pointer.free unless ipp_status == :ipp_ok CupsFFI::cupsFreeOptions(, ) unless .nil? raise ipp_status.to_s end CupsFFI::cupsFreeOptions(, ) unless .nil? CupsJob.new(job_id, self) end |
#print_file(file_name, options = {}) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cupsffi/printer.rb', line 123 def print_file(file_name, = {}) raise "File not found: #{file_name}" unless File.exists? file_name = nil = 0 unless .empty? () = FFI::MemoryPointer.new :pointer .map do |key,value| = CupsFFI::cupsAddOption(key.to_s, value.to_s, , ) end = .get_pointer(0) end job_id = CupsFFI::cupsPrintFile2(@connection, @name, file_name, file_name, , ) if job_id == 0 last_error = CupsFFI::cupsLastErrorString() CupsFFI::cupsFreeOptions(, ) unless .nil? raise last_error end CupsFFI::cupsFreeOptions(, ) unless .nil? CupsJob.new(job_id, self) end |
#state ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cupsffi/printer.rb', line 108 def state o = attributes { :state => case o['printer-state'] when "3" then :idle when "4" then :printing when "5" then :stopped else :unknown end, :reasons => o['printer-state-reasons'].split(/,/) } end |