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) ⇒ CupsPrinter

Returns a new instance of CupsPrinter.



26
27
28
29
# File 'lib/cupsffi/printer.rb', line 26

def initialize(name)
  raise "Printer not found" unless CupsPrinter.get_all_printer_names.include? name
  @name = name
end

Instance Attribute Details

#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_namesObject



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cupsffi/printer.rb', line 31

def self.get_all_printer_names
  p = FFI::MemoryPointer.new :pointer
  dest_count = CupsFFI::cupsGetDests(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

Instance Method Details

#attributesObject



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

def attributes
  p = FFI::MemoryPointer.new :pointer
  dest_count = CupsFFI::cupsGetDests(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



135
136
137
138
# File 'lib/cupsffi/printer.rb', line 135

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


100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cupsffi/printer.rb', line 100

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(CupsFFI::CUPS_HTTP_DEFAULT, @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(CupsFFI::CUPS_HTTP_DEFAULT, @name,
                                           job_id, 'my doc', mime_type, 1)

  http_status = CupsFFI::cupsWriteRequestData(CupsFFI::CUPS_HTTP_DEFAULT, data, data.length)

  ipp_status = CupsFFI::cupsFinishDocument(CupsFFI::CUPS_HTTP_DEFAULT, @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


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cupsffi/printer.rb', line 74

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::cupsPrintFile(@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



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

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