class ClipboardManager

Constants

CLIPBOARD
CONFIG
HELP
VERSION

Attributes

do_qrcode[RW]

Public Class Methods

kill_espeak() click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 239
def ClipboardManager.kill_espeak
  espeak = CONFIG[:Espeak].split.first
  system "killall --quiet #{espeak}"
end
new(stage, toolbar, options) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 69
def initialize(stage, toolbar, options)
  @image = toolbar.parent.children[0].child # Expander:hbox:EventImage:Image
  @timer = nil

  @working = GdkPixbuf::Pixbuf.new(file: CONFIG[:Working])
  @ok      = GdkPixbuf::Pixbuf.new(file: CONFIG[:Ok])
  @nope    = GdkPixbuf::Pixbuf.new(file: CONFIG[:Nope])
  @ready   = GdkPixbuf::Pixbuf.new(file: CONFIG[:Ready])
  @off     = GdkPixbuf::Pixbuf.new(file: CONFIG[:Off])

  @is_pwd  = Regexp.new(CONFIG[:IsPwd], Regexp::EXTENDED)

  vbox = Such::Box.new(stage, :vbox!)
  @running = Such::CheckButton.new(vbox, :running!, 'toggled'){toggled}
  @running.active = true

  @ask = Such::CheckButton.new vbox, :ask!
  @ask.active = true

  Such::Label.new vbox, :tasks!

  @checks = {}
  CONFIG[:Tasks!].keys.each do |key|
    @checks[key] = Such::CheckButton.new(vbox, [key.to_s.capitalize], {set_active: true})
  end

  Such::Button.new(vbox, :history_button!){do_history!}
  Such::Button.new(vbox, :qrcode_button!){do_qrcode!} if ClipboardManager.do_qrcode

  @history, @previous = [], nil
  text = request_text
  if text
    add_history text
    @previous = text
  end
  GLib::Timeout.add(CONFIG[:Sleep]) do
    step if @running.active?
    true # repeat
  end

  Gtk3App.clipboard_manager_hook(self)
  Gtk3App.logo_press_event do |button|
    case button
    when 1
      Gtk3App.minime!
    when 2
      Gtk3App.toggle!
    # When 3 gets captured by Gtk3App's main menu
    end
  end

  status(@ready)
end
run() click to toggle source
# File lib/clipboard_manager.rb, line 18
def self.run

  # Gems
  require 'gtk3app'
  begin
    require 'helpema'
    ::Helpema::ZBar   # autoload
    require 'timeout' # needed to timeout zbarcam
  rescue
    # no ZBar? OK, nevermind.
    ClipboardManager.do_qrcode = false
  end

  # This Gem
  require_relative 'clipboard_manager/config.rb'
  require_relative 'clipboard_manager/clipboard_manager.rb'

  # Run
  Gtk3App.run(klass:ClipboardManager)
end

Public Instance Methods

add_history(text) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 201
def add_history(text)
  @history.unshift text
  @history.uniq!
  @history.pop if @history.length > CONFIG[:MaxHistory]
end
bashit(md, str) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 255
def bashit(md, str)
  (md.length-1).downto(0) do |i|
    str = str.gsub(/\$#{i}/, md[i] || '')
  end
  $stderr.puts str
  Process.detach spawn str
end
do_history!() click to toggle source

github.com/ruby-gnome2/ruby-gnome2/blob/master/gtk3/sample/misc/dialog.rb

# File lib/clipboard_manager/clipboard_manager.rb, line 124
def do_history!
  dialog = CancelOk.new(:history_dialog!)
  Gtk3App.transient dialog
  combo = dialog.combo :history_combo!
  @history.each do |str|
    if str.length > CONFIG[:MaxString]
      n = CONFIG[:MaxString]/2 - 1
      str = "#{str[0..n]}...#{str[-n..-1]}"
    end
    combo.append_text(str)
  end
  dialog.runs do
    if combo.active_text
      @previous = nil
      CLIPBOARD.text = @history[combo.active]
    end
  end
end
do_qrcode!() click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 143
def do_qrcode!
  qrcode = Timeout::timeout(CONFIG[:QrcTimeOut]){ Helpema::ZBar.cam() }
  CLIPBOARD.text = qrcode
  status(@ok)
rescue
  $!.puts
  CLIPBOARD.clear
  status(@nope)
end
do_toggle!() click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 165
def do_toggle!
  text = request_text
  @previous = text
  @running.active = !@running.active?
  ClipboardManager.kill_espeak unless @running.active?
end
espeak(text) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 244
def espeak(text)
  Rafini.thread_bang! do
    ClipboardManager.kill_espeak
    IO.popen(CONFIG[:Espeak], 'w'){_1.puts text.strip}
  end
end
manage(text) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 207
def manage(text)
  add_history text
  CONFIG[:Tasks!].each do |name, _|
    next unless @checks[name].active?
    rgx, mth, clr, str = _
    rgx = Regexp.new(rgx, Regexp::EXTENDED | Regexp::MULTILINE)
    if md=rgx.match(text) and question?(name)
      CLIPBOARD.clear if clr
      begin
        case mth
        when :espeak
          espeak(text)
        when :open
          open(text)
        when :bashit
          bashit(md, str)
        when :reply
          reply(text)
        else
          raise "Method #{mth} not implemented."
        end
        status(@ok)
      rescue RuntimeError
        $!.puts
        status(@nope)
      end
      return
    end
  end
  status(@nope)
end
open(text) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 251
def open(text)
  Process.detach spawn CONFIG[:Open], text
end
question?(name) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 153
def question?(name)
  return true unless @ask.active?
  dialog = NoYes.new :question_dialog!
  Gtk3App.transient dialog
  dialog.label(:question_label!).text = "Run #{name}?"
  dialog.ok?
end
reply(text) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 263
def reply(text)
  dialog = Message.new(:reply_dialog!)
  Gtk3App.transient dialog
  begin
    dialog.label(:reply_label!).text = text
    dialog.label(:reply_label!).text = "#{eval text}"
  rescue
    dialog.label(:reply_label!).text = $!.message
  end
  dialog.runs
end
request_text() click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 172
def request_text
  if text = CLIPBOARD.wait_for_text
    text.strip!
    return text unless text.empty? or @is_pwd.match?(text)
  end
  nil
end
status(type) click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 180
def status(type)
  @image.set_pixbuf(type)
  @timer = Time.now
end
step() click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 185
def step
  if @timer and Time.now - @timer > CONFIG[:StatusTimeOut]
    @timer = nil
    status @ready
  end
  text = request_text
  unless text.nil? or @previous == text
    @previous = text
    status @working
    GLib::Timeout.add(0) do
      manage(text)
      false # don't repeat
    end
  end
end
toggled() click to toggle source
# File lib/clipboard_manager/clipboard_manager.rb, line 161
def toggled
  @running.active? ? status(@ready) : status(@off)
end