Module: PrintButton

Defined in:
lib/qooxview/helpers/open_print.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#printer_buttonsObject (readonly)

Returns the value of attribute printer_buttons.



151
152
153
# File 'lib/qooxview/helpers/open_print.rb', line 151

def printer_buttons
  @printer_buttons
end

Instance Method Details

#call_lpstat(ip) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/qooxview/helpers/open_print.rb', line 153

def call_lpstat(ip)
  if ConfigBase.use_printing.to_s == 'true'
    %x( which lpstat >/dev/null 2>&1 && lpstat -h #{ip}:631 -a 2>/dev/null |
        sed -e "s/ .*//" ).split
  else
    []
  end
end

#cmd_printer(session, button) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/qooxview/helpers/open_print.rb', line 211

def cmd_printer(session, button)
  cmd = nil
  pn = stat_printer(session, button).data_str
  remote = session.client_ip
  dputs(3) { "Found printer #{pn} with remote #{remote}" }
  if pn != 'PDF'
    if get_server_printers.index(pn)
      cmd = "lp -o media=a4 -o fitplot -d #{pn.sub(/^server /, '')}"
    elsif !(dp(session.web_req.header['user_agent']) =~ /Windows/) &&
        get_remote_printers(remote).index(pn)
      cmd = "lp -o media=a4 -o fitplot -h #{remote}:631 -d #{pn}"
    end
  end
  dputs(3) { "Command will be #{cmd}" }
  cmd
end

#get_remote_printers(ip) ⇒ Object



162
163
164
165
166
167
168
169
170
# File 'lib/qooxview/helpers/open_print.rb', line 162

def get_remote_printers(ip)
  if ip.match(ConfigBase.openprint_search)
    dputs(2) { "Getting printers for #{ip}" }
    call_lpstat(ip)
  else
    dputs(3) { "Not getting remote for #{ip}" }
    []
  end
end

#get_server_printersObject



172
173
174
175
176
# File 'lib/qooxview/helpers/open_print.rb', line 172

def get_server_printers
  call_lpstat('localhost').collect { |p|
    "server #{p}"
  } + %w( PDF )
end

#reply_print(session) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/qooxview/helpers/open_print.rb', line 237

def reply_print(session)
  #dputs_func
  ret = []
  @printer_buttons.each { |pb|
    p = stat_printer(session, pb)
    dputs(4) { "#{pb}-#{p.inspect}" }
    value = "#{GetText._(pb.to_s)} #{p.data_str}"
    if session.web_req && ip = session.client_ip
      # We're not looking for CUPS on the localhost, only on Macintosh and
      # non-android Linuxes
      ua = session.web_req.header['user-agent']
      ua &&= ua.first
      if ip =~ /(::1|localhost|127.0.0.1)/
        dputs(3) { "Not looking for cups on #{ip} - #{session.web_req.header['user-agent']}" }
      elsif ua =~ /(Macintosh|Linux)/ && !(ua =~ /Android/)
        value = [value] + get_server_printers + get_remote_printers(ip)
      end
    end
    ret += reply(:update, pb => value)
  }
  dputs(4) { "#{ret.inspect}" }
  ret
end

#rpc_print(session, name, data) ⇒ Object



261
262
263
264
265
266
267
268
# File 'lib/qooxview/helpers/open_print.rb', line 261

def rpc_print(session, name, data)
  #dputs_func
  dputs(4) { "Printing button #{name} with #{data.inspect}" }
  if data and data['menu'] and data['menu'].length > 0
    stat_printer(session, name).data_str = data['menu']
  end
  reply_print(session)
end

#send_printer(session, button, file) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/qooxview/helpers/open_print.rb', line 228

def send_printer(session, button, file)
  if cmd = cmd_printer(session, button)
    %x[ #{cmd} #{file} ]
    stat_printer(session, button).data_str
  else
    return nil
  end
end

#send_printer_reply(session, button, data, file) ⇒ Object



270
271
272
273
274
275
276
277
278
279
# File 'lib/qooxview/helpers/open_print.rb', line 270

def send_printer_reply(session, button, data, file)
  rpc_print(session, button, data) +
      reply(:window_show, :print_status) +
      reply(:update, :status =>
                       if printer = send_printer(session, button, file)
                         "Printed to #{printer}"
                       else
                         "<a href='#{file}' target='other'>#{file}</a>"
                       end)
end

#show_print(*buttons) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/qooxview/helpers/open_print.rb', line 178

def show_print(*buttons)
  dputs(3) { "show_print with #{buttons.inspect}" }
  if not instance_variable_defined? :@printer_buttons
    @printer_buttons = []
  end
  print_name = nil
  buttons.to_a.each { |b|
    dputs(4) { "Doing #{b.inspect}" }
    if b.to_s =~ /^print/
      show_split_button b, get_server_printers
      print_name = b.to_sym
    else
      show_button b
    end
  }
  if not print_name
    show_split_button :print, get_server_printers
    print_name = :print
  end
  @printer_buttons.push print_name
end

#stat_printer(session, button) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/qooxview/helpers/open_print.rb', line 200

def stat_printer(session, button)
  user = session.owner ? session.owner. : 'anonymous'
  stat_name = "#{self.name}:#{button}:#{user}"
  stat = Statics.get(stat_name)
  dputs(3) { "Getting printer #{stat_name} == #{stat.data_str}" }
  if stat.data_str == ''
    stat.data_str = get_server_printers.first
  end
  stat
end

#window_print_statusObject



281
282
283
284
285
286
287
288
# File 'lib/qooxview/helpers/open_print.rb', line 281

def window_print_status
  gui_window :print_status do
    gui_vbox :nogroup do
      show_html :status
      show_button :close
    end
  end
end