Class: Cups::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi-cups/printer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, connection = nil) ⇒ Printer

Returns a new instance of Printer.

Parameters:

  • name (String)

    printer’s name

  • options (Hash) (defaults to: {})

    printer’s options



7
8
9
10
11
# File 'lib/ffi-cups/printer.rb', line 7

def initialize(name, options={}, connection=nil)
  @name = name
  @options = options
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



3
4
5
# File 'lib/ffi-cups/printer.rb', line 3

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/ffi-cups/printer.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/ffi-cups/printer.rb', line 3

def options
  @options
end

Class Method Details

.get_destination(name, connection = nil) ⇒ Printer

Get a destination by name

Parameters:

  • name (String)

    name of the printer

  • connection (Pointer) (defaults to: nil)

    http pointer from Connection#httpConnect2

Returns:



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 = Cups.cupsGetDests2(http, dests)

  # Get the destination from name with cupsGetDest
  p_dest = 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) ⇒ Object

Get all destinations (printer devices)

Parameters:



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, printer_options(d), connection)
    printers.push(printer)
  end
  cupsFreeDests(dests.count, pointer)
  return printers
end

Instance Method Details



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, options={})
  raise "File not found: #{filename}" unless File.exist? filename
  
  http = @connection.nil? ? nil : @connection.httpConnect2
  # Get all destinations with cupsGetDests2
  dests = FFI::MemoryPointer.new :pointer
  num_dests = Cups.cupsGetDests2(http, dests)

  # Get the destination from name with cupsGetDest
  p_dest = 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?

  p_options = nil
  num_options = 0
  unless options.empty?
    p_options = FFI::MemoryPointer.new :pointer
    options.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
      num_options = Cups.cupsAddOption(k, v, num_options, p_options)
    end
    p_options = p_options.get_pointer(0)
  end

  job_id = Cups.cupsPrintFile2(http, @name, filename, title, num_options, p_options)

  if job_id.zero?
    last_error = Cups.cupsLastErrorString()
    self.class.cupsFreeOptions(num_options, p_options) unless options.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(num_options, p_options) unless options.empty?
  self.class.cupsFreeDests(num_dests, dests)
  Cups::Connection.close(http)
  return job
end

#stateSymbol

Returns the printer state

Returns:

  • (Symbol)

    printer state from options



15
16
17
18
19
20
21
22
# 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_reasonsArray

Returns the reason for the printer state

Returns:

  • (Array)

    array of reasons in string format



26
27
28
# File 'lib/ffi-cups/printer.rb', line 26

def state_reasons
  options['printer-state-reasons'].split(/,/)
end