class Cups::Printer

Attributes

connection[R]
name[R]
options[R]

Public Class Methods

get_destination(name, connection=nil) click to toggle source

Get a destination by name @param name [String] name of the printer @param connection [Pointer] http pointer from {Cups::Connection#httpConnect2} @return [Printer] a printer object

# 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, printer_options(dest), connection)
  cupsFreeDests(num_dests, dests)
  Cups::Connection.close(http) if http
  return printer
end
get_destinations(connection=nil) click to toggle source

Get all destinations (printer devices) @param connection [Pointer] http pointer from {Cups::Connection#httpConnect2}

# 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, printer_options(d), connection)
    printers.push(printer)
  end
  cupsFreeDests(dests.count, pointer)
  return printers
end
new(name, options={}, connection=nil) click to toggle source

@param name [String] printer's name @param options [Hash] printer's options

# File lib/ffi-cups/printer.rb, line 7
def initialize(name, options={}, connection=nil)
  @name = name
  @options = options
  @connection = connection
end

Private Class Methods

cupsCheckDestSupported(dest, option, value, connection=nil) click to toggle source

Wrapper around {::FFI::Cups#cupsCheckDestSupported} @param dest [Pointer] pointer to the destination @param option [String] @param value [String] @param connection [Pointer] http pointer from {Cups::Connection#httpConnect2} @return [Boolean] true if supported, false otherwise

# File lib/ffi-cups/printer.rb, line 131
def self.cupsCheckDestSupported(dest, option, value, connection=nil)
  info = FFI::Cups.cupsCopyDestInfo(connection, dest)
  i = FFI::Cups.cupsCheckDestSupported(connection, dest, info, option, value)
  return !i.zero?
end
cupsFreeDests(num_dests, pointer) click to toggle source

Wrapper around {::FFI::Cups#cupsFreeDests} @param num_dests [Integer] @param pointer [Pointer] pointer to the destinations

# File lib/ffi-cups/printer.rb, line 166
def self.cupsFreeDests(num_dests, pointer)
  FFI::Cups.cupsFreeDests(num_dests, pointer.get_pointer(0))
end
cupsFreeOptions(num_opts, pointer) click to toggle source

Wrapper around {::FFI::Cups#cupsFreeOptions} @param num_opts [Integer] @param pointer [Pointer] pointer to the options

# File lib/ffi-cups/printer.rb, line 173
def self.cupsFreeOptions(num_opts, pointer)
  FFI::Cups.cupsFreeOptions(num_opts, pointer)
end
cupsGetDests2(pointer, connection=nil) click to toggle source

Wrapper around {::FFI::Cups#cupsGetDests2} @param connection [Pointer] http pointer from {Cups::Connection#httpConnect2} @param pointer [Pointer] pointer to the destinations @return [Hash] hashmap of destination structs

# File lib/ffi-cups/printer.rb, line 112
def self.cupsGetDests2(pointer, connection=nil)
  http = connection.nil? ? nil : connection.httpConnect2
  num_dests = FFI::Cups.cupsGetDests2(http, pointer)
  size = Cups::Struct::Destination.size
  destinations = []
  num_dests.times do |i|
    destination = Cups::Struct::Destination.new(pointer.get_pointer(0) + (size * i))
    destinations.push(destination)
  end
  Cups::Connection.close(http) if http
  return destinations
end
cups_options(dest) click to toggle source

Returns a destination's options @param dest [Object] {Cups::Struct::Destination} object @return [Hash] hash of destination' options as {Cups::Struct::Option}

# File lib/ffi-cups/printer.rb, line 140
def self.cups_options(dest)
  size = Cups::Struct::Option.size
  options = []

  dest[:num_options].times do |i|
    option = Cups::Struct::Option.new(dest[:options] + (size * i))
    options.push(option)
  end
  return options
end
printer_options(dest) click to toggle source

Returns a destination's options as hash @param dest [Object] {Cups::Struct::Destination} object @return [Hash] hash of destination' options

# File lib/ffi-cups/printer.rb, line 154
def self.printer_options(dest)
  dest_opts = cups_options(dest)
  options = {}
  dest_opts.each do |o|
    options[o[:name].dup] = o[:value].dup
  end
  return options
end

Public Instance Methods

print_file(filename, title, options={}) click to toggle source
state() click to toggle source

Returns the printer state @return [Symbol] printer state from options

# File lib/ffi-cups/printer.rb, line 15
def state
  case options['printer-state']
  when '3' then :idle
  when '4' then :printing
  when '5' then :stopped
  else :unknown
  end
end
state_reasons() click to toggle source

Returns the reason for the printer state @return [Array] array of reasons in string format

# File lib/ffi-cups/printer.rb, line 26
def state_reasons
  options['printer-state-reasons'].split(/,/)
end