---
p2v/client/lib/virt-p2v/ui/gtk.rb | 139 +++++++++++++++++++++++++++++++++++++
p2v/client/lib/virt-p2v/ui/main.rb | 122 +-------------------------------
2 files changed, 141 insertions(+), 120 deletions(-)
diff --git a/p2v/client/lib/virt-p2v/ui/gtk.rb b/p2v/client/lib/virt-p2v/ui/gtk.rb
index 3984b7f..a321c3f 100644
--- a/p2v/client/lib/virt-p2v/ui/gtk.rb
+++ b/p2v/client/lib/virt-p2v/ui/gtk.rb
@@ -50,4 +50,143 @@ if Kernel.const_defined?(:NOGUI) && NOGUI == true
Cursor::Type = VirtP2V::UI::NeverMind
Cursor::Type::X_CURSOR = VirtP2V::UI::NeverMind
end
+
+ module VirtP2V
+ module UI
+ class SomeListW < NeverMind
+ def append(*args)
+ @items ||= []
+ @items << []
+ self
+ end
+
+ def _items
+ @items ||= []
+ @items
+ end
+
+ def _select(idx)
+ @idx = idx
+ end
+
+ def _selection
+ @idx ||= 0
+ @idx
+ end
+
+ def []=(idx, item)
+ @items.last[idx] = item
+ self
+ end
+
+ def clear
+ @items.clear
+ end
+
+ def each(&block)
+ @items.each do |item|
+ # block accepts three params:
+ # model, path, iter
+ # since we know nothing about model and path
+ # we'll fill only item as iter
+ block.call(nil, nil, item)
+ end
+ end
+ end
+
+ class ConvertProfileW < NeverMind
+ def active_iter
+ cpl = @main.get_object("convert_profile_list")
+ cpl._items[cpl._selection]
+ end
+ end
+
+ class NetworkDeviceListViewW < NeverMind
+ def selection
+ self
+ end
+
+ def selected
+ # TODO: this needs a proper selection
+ # now it's only for a testing purpouse
+ # but later we need to reflect reality
+ # and not blindly return first device
+ @main.get_object("network_device_list")._items.first
+ end
+ end
+
+ class ConnectErrorW < NeverMind
+ def text
+ @text || ''
+ end
+
+ def text=(str)
+ @text = str
+ unless str == ''
+ puts "Error connecting: '#{str}'"
+ puts "Giving up."
+ exit(3)
+ end
+ end
+ end
+
+ class ConvertStatusW < NeverMind
+ def text
+ @text || ''
+ end
+
+ def text=(str)
+ @text = str
+ puts "conversion status changed to: '#{str}'"
+ STDOUT.flush
+ if str =~ /failure|error/i
+ puts "Giving up."
+ exit(4)
+ end
+ end
+ end
+
+ class SomeTextFieldW < NeverMind
+ def text
+ @text || ""
+ end
+
+ def text=(str)
+ @text = str
+ end
+
+ def secondary_icon_tooltip_text=(str)
+ puts "Error in '#{@name}': '#{str}'"
+ end
+ end
+
+ def self.widget_class_factory(widget_name)
+ widgets = Hash[ *(["network_device_list",
+ "convert_profile_list", "convert_network_list",
+ "convert_fixed_list", "convert_removable_list"].flat_map
do |_l|
+ [_l, SomeListW]
+ end) ]
+ .merge(
+ Hash[ *(["server_hostname", "server_username",
"server_password",
+ "convert_name", "convert_cpus",
"convert_memory",
+ "ip_manual", "ip_address", "ip_prefix",
"ip_gateway",
+ "ip_dns", "server_hostname",
"server_username",
+ "server_password"].flat_map do |_l|
+ [_l, SomeTextFieldW]
+ end) ]
+ ).merge({
+ "convert_profile" => ConvertProfileW,
+ "network_device_list_view" => NetworkDeviceListViewW,
+ "convert_status" => ConvertStatusW,
+ "connect_error" => ConnectErrorW
+ })
+
+ if widgets.has_key? widget_name
+ widgets[widget_name]
+ else
+ NeverMind
+ end
+ end
+ end
+ end
end
diff --git a/p2v/client/lib/virt-p2v/ui/main.rb b/p2v/client/lib/virt-p2v/ui/main.rb
index af96c4a..ac2ef27 100644
--- a/p2v/client/lib/virt-p2v/ui/main.rb
+++ b/p2v/client/lib/virt-p2v/ui/main.rb
@@ -131,127 +131,9 @@ class NewMain < Main
# this is the entry point for returning our mocked
# versions of gtk2 objects, very similar to what we'd
# need to write tests
- puts "GET_OBJ #{name}"
- @gui_objects ||= {}
- @gui_objects[name] ||= if ["network_device_list",
- "convert_profile_list", "convert_network_list",
- "convert_fixed_list", "convert_removable_list"].any?{|m|
m==name}
- n = NeverMind.new self, name
- n.eigen.send(:define_method, :append) do |*args|
-# p "append on #{self.name} #{args.inspect}"
- @items ||= []
- @items << []
- self
- end
- n.eigen.send(:define_method, :_items) do
- @items ||= []
- @items
- end
- n.eigen.send(:define_method, :_select) do |idx|
- @idx = idx
- end
- n.eigen.send(:define_method, :_selection) do
- @idx ||= 0
- @idx
- end
- n.eigen.send(:define_method, :"[]=") do |idx, item|
-# p "called []= on #{name} with #{args}"
- @items.last[idx] = item
- #p @items
- self
- end
- n.eigen.send(:define_method, :clear) do
- @items.clear
- end
- n.eigen.send(:define_method, :each) do |&block|
- @items.each do |item|
- # block accepts three params:
- # model, path, iter
- # since we know nothing about model and path
- # we'll fill only item as iter
- block.call(nil, nil, item)
- end
- end
- n
- elsif name == "convert_profile"
- n = NeverMind.new self, name
- n.eigen.send(:define_method, :active_iter) do |*args|
-# p "convert_profile.active_iter"
- cpl = @main.get_object("convert_profile_list")
- _items = cpl._items
- _sel = _items[cpl._selection]
-# p _sel
-
- end
- n
- elsif name == "network_device_list_view"
- n = NeverMind.new self, name
- n.eigen.send(:define_method, :selection) do
- self
- end
- n.eigen.send(:define_method, :selected) do
- # TODO: this needs a proper selection
- # now it's only for a testing purpouse
- # but later we need to reflect reality
- # and not blindly return first device
- nejm = @main.get_object("network_device_list")._items.first
-# p nejm
- end
- n
- elsif name == 'convert_status'
- n = NeverMind.new self, name
- n.eigen.send(:define_method, :text) do
- @text || ""
- end
- n.eigen.send(:define_method, :"text=") do |str|
- @text = str
- puts "conversion status changed to: '#{str}'"
- STDOUT.flush
- if str =~ /failure|error/i
- #puts "Error connecting: '#{str}'"
- puts "Giving up."
- exit(4)
- end
- end
- n
- elsif name == 'connect_error'
- n = NeverMind.new self, name
- n.eigen.send(:define_method, :text) do
- @text || ""
- end
- n.eigen.send(:define_method, :"text=") do |str|
- @text = str
- unless str == ''
- puts "Error connecting: '#{str}'"
- puts "Giving up."
- exit(3)
- end
- end
- n
- elsif ["server_hostname", "server_username",
"server_password",
- "convert_name", "convert_cpus",
"convert_memory",
- "ip_manual", "ip_address", "ip_prefix",
"ip_gateway",
- "ip_dns", "server_hostname",
"server_username",
- "server_password"].any?{|_m| _m==name}
- # N.B. some of these are set by user
- n = NeverMind.new self, name
- n.eigen.send(:define_method, :text) do
-# p "called text on #{@name}"
- @text || ""
- end
- n.eigen.send(:define_method, :"text=") do |str|
- @text = str
-# p "called text= #{str} on #{@name}"
- end
- n.eigen.send(:define_method, :"secondary_icon_tooltip_text=") do |str|
- puts "Error in '#{@name}': '#{str}'"
- end
- n
- else
- NeverMind.new self, name
- end
- p "I'LL RETURN YOU #{@gui_objects[name].class}:#{@gui_objects[name].__id__}
for #{name}"
+ @gui_objects ||= {}
+ @gui_objects[name] ||= VirtP2V::UI::widget_class_factory(name).new self, name
@gui_objects[name]
end
--
1.8.3.1