Class: CupsPrinter

Inherits:
Object
  • Object
show all
Defined in:
lib/cupsffi/printer.rb

Overview

The MIT License

Copyright © 2011 Nathan Ehresman

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.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, args = {}) ⇒ CupsPrinter

Returns a new instance of CupsPrinter.



40
41
42
43
44
# File 'lib/cupsffi/printer.rb', line 40

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

#connectionObject (readonly)

Returns the value of attribute connection.



24
25
26
# File 'lib/cupsffi/printer.rb', line 24

def connection
  @connection
end

#nameObject (readonly)

Returns the value of attribute name.



24
25
26
# File 'lib/cupsffi/printer.rb', line 24

def name
  @name
end

Class Method Details

.get_all_printer_names(args = {}) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/cupsffi/printer.rb', line 46

def self.get_all_printer_names(args = {})
  connection = get_connection(args)

  p = FFI::MemoryPointer.new :pointer
  dest_count = CupsFFI::cupsGetDests2(connection, p)
  ary = []
  dest_count.times do |i|
    d = CupsFFI::CupsDestS.new(p.get_pointer(0) + (CupsFFI::CupsDestS.size * i))
    ary.push(d[:name].dup)
  end
  CupsFFI::cupsFreeDests(dest_count, p.get_pointer(0))
  ary
end

.get_connection(args = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cupsffi/printer.rb', line 26

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

Instance Method Details

#attributesObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cupsffi/printer.rb', line 60

def attributes
  p = FFI::MemoryPointer.new :pointer
  dest_count = CupsFFI::cupsGetDests2(@connection, p)
  hash = {}
  dest_count.times do |i|
    dest = CupsFFI::CupsDestS.new(p.get_pointer(0) + (CupsFFI::CupsDestS.size * i))
    next unless dest[:name] == @name
    dest[:num_options].times do |j|
      options = CupsFFI::CupsOptionS.new(dest[:options] + (CupsFFI::CupsOptionS.size * j))
      hash[options[:name].dup] = options[:value].dup
    end
  end
  CupsFFI::cupsFreeDests(dest_count, p.get_pointer(0))
  hash
end

#cancel_all_jobsObject



152
153
154
155
# File 'lib/cupsffi/printer.rb', line 152

def cancel_all_jobs
  r = CupsFFI::cupsCancelJob2(@connection, @name, CupsFFI::CUPS_JOBID_ALL)
  raise CupsFFI::cupsLastErrorString() if r == 0
end


117
118
119
120
121
122
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
148
149
150
# File 'lib/cupsffi/printer.rb', line 117

def print_data(data, mime_type, options = {})
  options_pointer = nil
  num_options = 0
  unless options.empty?
    validate_options(options)
    options_pointer = FFI::MemoryPointer.new :pointer
    options.map do |key,value|
      num_options = CupsFFI::cupsAddOption(key.to_s, value.to_s, num_options, options_pointer)
    end
    options_pointer = options_pointer.get_pointer(0)
  end

  job_id = CupsFFI::cupsCreateJob(@connection, @name, 'data job', num_options, options_pointer)
  if job_id == 0
    last_error = CupsFFI::cupsLastErrorString()
    CupsFFI::cupsFreeOptions(num_options, options_pointer) unless options_pointer.nil?
    raise last_error
  end

  http_status = CupsFFI::cupsStartDocument(@connection, @name,
                                           job_id, 'my doc', mime_type, 1)

  http_status = CupsFFI::cupsWriteRequestData(@connection, data, data.length)

  ipp_status = CupsFFI::cupsFinishDocument(@connection, @name)

  unless ipp_status == :ipp_ok
    CupsFFI::cupsFreeOptions(num_options, options_pointer) unless options_pointer.nil?
    raise ipp_status.to_s
  end

  CupsFFI::cupsFreeOptions(num_options, options_pointer) unless options_pointer.nil?
  CupsJob.new(job_id, self)
end


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/cupsffi/printer.rb', line 91

def print_file(file_name, options = {})
  raise "File not found: #{file_name}" unless File.exists? file_name

  options_pointer = nil
  num_options = 0
  unless options.empty?
    validate_options(options)
    options_pointer = FFI::MemoryPointer.new :pointer
    options.map do |key,value|
      num_options = CupsFFI::cupsAddOption(key.to_s, value.to_s, num_options, options_pointer)
    end
    options_pointer = options_pointer.get_pointer(0)
  end

  job_id = CupsFFI::cupsPrintFile2(@connection, @name, file_name, file_name, num_options, options_pointer)

  if job_id == 0
    last_error = CupsFFI::cupsLastErrorString()
    CupsFFI::cupsFreeOptions(num_options, options_pointer) unless options_pointer.nil?
    raise last_error
  end

  CupsFFI::cupsFreeOptions(num_options, options_pointer) unless options_pointer.nil?
  CupsJob.new(job_id, self)
end

#stateObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/cupsffi/printer.rb', line 76

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