Module: Bitcoin::Gui::Helpers

Included in:
Gui, TreeView
Defined in:
lib/bitcoin/gui/helpers.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



88
89
90
# File 'lib/bitcoin/gui/helpers.rb', line 88

def method_missing name, *args
  @builder.get_object(name.to_s) rescue super(name, *args)
end

Instance Method Details

#add_wallet_filters(dialog) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bitcoin/gui/helpers.rb', line 29

def add_wallet_filters dialog
  filter = Gtk::FileFilter.new
  filter.name = "Wallet Files"
  filter.add_pattern("*.json")
  dialog.add_filter filter

  filter = Gtk::FileFilter.new
  filter.name = "All Files"
  filter.add_pattern("*")
  dialog.add_filter filter
end

#dialog(name, opts = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/bitcoin/gui/helpers.rb', line 41

def dialog name, opts = {}
  @dialog_cb_ids ||= {}
  ids = @dialog_cb_ids[name] || []
  dialog = send("#{name}_dialog")
  send("add_#{opts[:filters]}_filters", dialog)  if opts[:filters]
  opts[:setup].call(dialog)  if opts[:setup]
  while id = ids.shift
    GObject.signal_handler_disconnect(dialog, id)
  end
  ids << GObject.signal_connect(dialog, "response") do |dialog, response, *data|
    yield(dialog, *data)  if response > 0
    dialog.hide
  end
  if dialog.is_a?(Gtk::FileChooserDialog)
    ids << GObject.signal_connect(dialog, "file-activated") do |dialog, *data|
      yield(dialog, *data)
      dialog.hide
    end
  end
  (opts[:callbacks] || {}).each do |name, block|
    ids << GObject.signal_connect(dialog, name) {|*a| block.call(*a) }
  end
  dialog.show
  @dialog_cb_ids[name] = ids
rescue
  p $!
  puts *$@
end

#display_tx(tx_hash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/bitcoin/gui/helpers.rb', line 5

def display_tx tx_hash
  tx = @storage.get_tx(tx_hash)
  dialog(:tx, setup: ->(d) {
      tx_label_hash.text = tx.hash
      tx_label_value.text = format_value(tx.out.map(&:value).inject(:+))
      tx_label_confirmations.text = tx.confirmations.to_s
      txin_view = Bitcoin::Gui::TxInView.new(self, :tx_txin_view)
#        txin_view.update(tx.in)
    })
end

#format_address(address, label = nil) ⇒ Object



96
97
98
# File 'lib/bitcoin/gui/helpers.rb', line 96

def format_address address, label = nil
  "#{label} (#{address})"
end

#format_uptime(started) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/bitcoin/gui/helpers.rb', line 107

def format_uptime started
  uptime = Time.now.to_i - started
  mm, ss = uptime.divmod(60)       #=> [4515, 21]
  hh, mm = mm.divmod(60)           #=> [75, 15]
  dd, hh = hh.divmod(24)           #=> [3, 3]
  "%02d:%02d:%02d:%02d" % [dd, hh, mm, ss]
end

#format_value(val) ⇒ Object



92
93
94
# File 'lib/bitcoin/gui/helpers.rb', line 92

def format_value val
  "%.8f" % (val / 1e8)
end

#format_version(ver) ⇒ Object



100
101
102
103
104
105
# File 'lib/bitcoin/gui/helpers.rb', line 100

def format_version ver
  ver.insert(-3, '.')  if ver.size > 2
  ver.insert(-6, '.')  if ver.size > 5
  ver.insert(0, "0.")  if ver.size <= 7
  ver
end

#message(type, title, text, buttons) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bitcoin/gui/helpers.rb', line 70

def message(type, title, text, buttons)
  dialog(:message, :setup => ->(dialog){
      dialog.message_type = Gtk::MessageType.find(type.to_sym)
      dialog.text = title
      dialog.secondary_text = text
      [:yes, :no, :ok].each do |n|
        b = send("message_dialog_button_#{n}")
        buttons.include?(n) ? b.show : b.hide
      end
    }) do |dialog|
    yield(dialog)  if block_given?
    dialog.show_all
    dialog.hide
  end
rescue
  p $!
end

#wallet_preview(dialog, *args) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bitcoin/gui/helpers.rb', line 16

def wallet_preview(dialog, *args)
  filename = dialog.preview_filename
  file = filename.read_string rescue nil
  if file && File.file?(file)
    keystore = Bitcoin::Wallet::SimpleKeyStore.new(:file => file)
    wallet = Bitcoin::Wallet::Wallet.new(@storage, keystore,
      Bitcoin::Wallet::SimpleCoinSelector)
    preview = Gtk::Label.new "Keys: #{wallet.addrs.size}\n" +
      "Balance:\n%.8f" % (wallet.get_balance / 1e8)
  end
  dialog.preview_widget = preview
end