Class: ClipboardManager

Inherits:
Object
  • Object
show all
Extended by:
Rafini::Empty
Defined in:
lib/clipboard_manager.rb,
lib/clipboard_manager/config.rb,
lib/clipboard_manager/clipboard_manager.rb

Defined Under Namespace

Classes: CancelOk, Message, NoYes

Constant Summary collapse

HELP =
<<~HELP
  Usage:
    clipboard_manager [:options+]
  Options:
    -h --help
    -v --version
    --minime      \t Real minime
    --notoggle    \t Minime wont toggle decorated and keep above
    --notdecorated\t Dont decorate window
HELP
VERSION =
'4.2.230117'
CONFIG =
{
  Tasks!: { # Note that Ruby's Hash preserves order, and order here is important.
    calculator: [
      '^([\d\.\+\-\*\/\%\(\) ]{3,80})$',
      :reply,
      true, # clears clipboard
    ],
    dictionary: [
      '^(\w+)$',
      :bashit,
      true, # clears clipboard
      "xdg-open 'https://en.wiktionary.org/wiki/$1'",
    ],
    url: ['^https?://\w[\-\+\.\w]*(\.\w+)(:\d+)?(/\S*)?$', :open, true],
    espeak: ['.{40,}', :espeak, true],
  },

  StatusTimeOut: 3,
  Sleep: 750,
  MaxHistory: 13,
  MaxString: 60,

  QrcTimeOut: 12,

  IsPwd: is_pwd,

  # The text-to-speech needs to be able to receive text from stdin
  Espeak: 'espeak -s 180 --stdin',

  Working: "#{UserSpace::XDG['data']}/gtk3app/clipboardmanager/working.png",
  Ok:      "#{UserSpace::XDG['data']}/gtk3app/clipboardmanager/ok.png",
  Nope:    "#{UserSpace::XDG['data']}/gtk3app/clipboardmanager/nope.png",
  Ready:   "#{UserSpace::XDG['data']}/gtk3app/clipboardmanager/ready.png",
  Off:     "#{UserSpace::XDG['data']}/gtk3app/clipboardmanager/off.png",

  HelpFile: "https://github.com/carlosjhr64/clipboard_manager",
  Logo: "#{UserSpace::XDG['data']}/gtk3app/clipboardmanager/logo.png",

  ### Gui Things ###

  about_dialog: {
    set_program_name: 'Clipboard Manager',
    set_version: VERSION.semantic(0..1),
    set_copyright: '(c) 2023 CarlosJHR64',
    set_comments: 'A Ruby Gtk3App Clipboard Manager ',
    set_website: 'https://github.com/carlosjhr64/clipboard_manager',
    set_website_label: 'See it at GitHub!',
  },

  window: {
    set_title: "Clipboard Manager",
    set_window_position: :center,
  },

  VBOX: [:vertical],
  vbox: h0,
  vbox!: [:VBOX, :vbox],

  ASK: ['Ask For Confirmation'],
  ask: h0,
  ask!: [:ASK, :ask],

  RUNNING: ['On/Off'],
  running: h0,
  running!: [:RUNNING, :running],

  TASKS: ['Tasks:'],
  tasks: h0,
  tasks!: [:TASKS, :tasks],

  HISTORY_BUTTON: [label: 'History'],
  history_button: h0,
  history_button!: [:HISTORY_BUTTON, :history_button],

  HISTORY_DIALOG: a0,
  history_dialog: h0,
  history_dialog!: [:HISTORY_DIALOG, :history_dialog],

  HISTORY_COMBO: a0,
  history_combo: h0,
  history_combo!: [:HISTORY_COMBO, :history_combo],

  QRCODE_BUTTON: [label: 'QR-Code'],
  qrcode_button: h0,
  qrcode_button!: [:QRCODE_BUTTON, :qrcode_button],

  QUESTION_DIALOG: a0,
  question_dialog: {
    set_keep_above: true,
  },
  question_dialog!: [:question_dialog, :QUESTION_DIALOG],

  QUESTION_LABEL: a0,
  question_label: h0,
  question_label!: [:question_label, :QUESTION_LABEL],

  REPLY_LABEL: a0,
  reply_label: h0,
  reply_label!: [:reply_label, :REPLY_LABEL],

  REPLY_DIALOG: a0,
  reply_dialog: h0,
  reply_dialog!: [:reply_dialog, :REPLY_DIALOG],

  # Toggle's app-menu item.
  # Application MAY modify :TOGGLE for language.
  TOGGLE: [label: 'Toggle On/Off'],
  toggle: h0,
  toggle!: [:TOGGLE, :toggle, 'activate'],
  app_menu: {
    add_menu_item: [ :toggle!, :minime!, :help!, :about!, :quit!  ],
  },
}
CLIPBOARD =
Gtk::Clipboard.get(Gdk::Selection::CLIPBOARD)

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage, toolbar, options) ⇒ ClipboardManager

Returns a new instance of ClipboardManager.



69
70
71
72
73
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# 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

Class Attribute Details

.do_qrcodeObject

Returns the value of attribute do_qrcode.



2
3
4
# File 'lib/clipboard_manager.rb', line 2

def do_qrcode
  @do_qrcode
end

Class Method Details

.kill_espeakObject



239
240
241
242
# File 'lib/clipboard_manager/clipboard_manager.rb', line 239

def ClipboardManager.kill_espeak
  espeak = CONFIG[:Espeak].split.first
  system "killall --quiet #{espeak}"
end

.runObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# 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

Instance Method Details

#add_history(text) ⇒ Object



201
202
203
204
205
# 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) ⇒ Object



255
256
257
258
259
260
261
# 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!Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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!Object



143
144
145
146
147
148
149
150
151
# 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!Object



165
166
167
168
169
170
# 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) ⇒ Object



244
245
246
247
248
249
# 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) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# 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) ⇒ Object



251
252
253
# File 'lib/clipboard_manager/clipboard_manager.rb', line 251

def open(text)
  Process.detach spawn CONFIG[:Open], text
end

#question?(name) ⇒ Boolean

Returns:

  • (Boolean)


153
154
155
156
157
158
159
# 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) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
# 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_textObject



172
173
174
175
176
177
178
# 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) ⇒ Object



180
181
182
183
# File 'lib/clipboard_manager/clipboard_manager.rb', line 180

def status(type)
  @image.set_pixbuf(type)
  @timer = Time.now
end

#stepObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# 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

#toggledObject



161
162
163
# File 'lib/clipboard_manager/clipboard_manager.rb', line 161

def toggled
  @running.active? ? status(@ready) : status(@off)
end