Module | VirtP2V::UI::Connect |
In: |
lib/virt-p2v/ui/connect.rb
lib/virt-p2v/ui/connect.rb |
UI_STATE_INVALID | = | 0 |
UI_STATE_VALID | = | 1 |
UI_STATE_ACTIVATING | = | 2 |
UI_STATE_COMPLETE | = | 3 |
EV_HOSTNAME | = | 0 |
EV_USERNAME | = | 1 |
EV_PASSWORD | = | 2 |
EV_BUTTON | = | 3 |
EV_ACTIVATION | = | 4 |
UI_STATE_INVALID | = | 0 |
UI_STATE_VALID | = | 1 |
UI_STATE_ACTIVATING | = | 2 |
UI_STATE_COMPLETE | = | 3 |
EV_HOSTNAME | = | 0 |
EV_USERNAME | = | 1 |
EV_PASSWORD | = | 2 |
EV_BUTTON | = | 3 |
EV_ACTIVATION | = | 4 |
# File lib/virt-p2v/ui/connect.rb, line 139 139: def self.connect_button_clicked 140: event(EV_BUTTON, true) 141: 142: hostname = @hostname_ui.text.strip 143: username = @username_ui.text.strip 144: password = @password_ui.text 145: 146: connection = VirtP2V::Connection.new(hostname, username, password) 147: @converter.connection = connection 148: connection.connect { |result| 149: case result 150: when true 151: event(EV_ACTIVATION, true) 152: when VirtP2V::Connection::RemoteError 153: @connect_error.text = 'Failed to start ' + 154: 'virt-p2v-server on remote ' + 155: 'server' 156: event(EV_ACTIVATION, false) 157: when VirtP2V::Connection::InvalidHostnameError 158: @connect_error.text = "Unable to connect to #{hostname}" 159: event(EV_ACTIVATION, false) 160: when VirtP2V::Connection::InvalidCredentialsError 161: @connect_error.text = "Invalid username/password" 162: event(EV_ACTIVATION, false) 163: else 164: @connect_error.text = result.message 165: event(EV_ACTIVATION, false) 166: end 167: } 168: end
# File lib/virt-p2v/ui/connect.rb, line 139 139: def self.connect_button_clicked 140: event(EV_BUTTON, true) 141: 142: hostname = @hostname_ui.text.strip 143: username = @username_ui.text.strip 144: password = @password_ui.text 145: 146: connection = VirtP2V::Connection.new(hostname, username, password) 147: @converter.connection = connection 148: connection.connect { |result| 149: case result 150: when true 151: event(EV_ACTIVATION, true) 152: when VirtP2V::Connection::RemoteError 153: @connect_error.text = 'Failed to start ' + 154: 'virt-p2v-server on remote ' + 155: 'server' 156: event(EV_ACTIVATION, false) 157: when VirtP2V::Connection::InvalidHostnameError 158: @connect_error.text = "Unable to connect to #{hostname}" 159: event(EV_ACTIVATION, false) 160: when VirtP2V::Connection::InvalidCredentialsError 161: @connect_error.text = "Invalid username/password" 162: event(EV_ACTIVATION, false) 163: else 164: @connect_error.text = result.message 165: event(EV_ACTIVATION, false) 166: end 167: } 168: end
# File lib/virt-p2v/ui/connect.rb, line 33 33: def self.event(event, status) 34: case event 35: when EV_HOSTNAME 36: @hostname = status 37: when EV_USERNAME 38: @username = status 39: when EV_PASSWORD 40: @password = status 41: when EV_BUTTON, EV_ACTIVATION 42: # Persistent state not required 43: else 44: raise "Unexpected event: #{event}" 45: end 46: 47: valid = @hostname && @username && @password 48: 49: case @state 50: when UI_STATE_INVALID 51: set_state(UI_STATE_VALID) if valid 52: when UI_STATE_VALID 53: if !valid then 54: set_state(UI_STATE_INVALID) 55: elsif event == EV_BUTTON 56: set_state(UI_STATE_ACTIVATING) 57: end 58: when UI_STATE_ACTIVATING 59: # UI is disabled, so we shouldn't be getting any events other than 60: # EV_ACTIVATION 61: raise "Unexpected event: #{event}" unless event == EV_ACTIVATION 62: 63: set_state(status ? UI_STATE_COMPLETE : UI_STATE_VALID) 64: else 65: raise "Unexpected UI state: #{@state}" 66: end 67: end
# File lib/virt-p2v/ui/connect.rb, line 33 33: def self.event(event, status) 34: case event 35: when EV_HOSTNAME 36: @hostname = status 37: when EV_USERNAME 38: @username = status 39: when EV_PASSWORD 40: @password = status 41: when EV_BUTTON, EV_ACTIVATION 42: # Persistent state not required 43: else 44: raise "Unexpected event: #{event}" 45: end 46: 47: valid = @hostname && @username && @password 48: 49: case @state 50: when UI_STATE_INVALID 51: set_state(UI_STATE_VALID) if valid 52: when UI_STATE_VALID 53: if !valid then 54: set_state(UI_STATE_INVALID) 55: elsif event == EV_BUTTON 56: set_state(UI_STATE_ACTIVATING) 57: end 58: when UI_STATE_ACTIVATING 59: # UI is disabled, so we shouldn't be getting any events other than 60: # EV_ACTIVATION 61: raise "Unexpected event: #{event}" unless event == EV_ACTIVATION 62: 63: set_state(status ? UI_STATE_COMPLETE : UI_STATE_VALID) 64: else 65: raise "Unexpected UI state: #{@state}" 66: end 67: end
# File lib/virt-p2v/ui/connect.rb, line 69 69: def self.init(ui, converter) 70: @hostname_ui = ui.get_object('server_hostname') 71: @username_ui = ui.get_object('server_username') 72: @password_ui = ui.get_object('server_password') 73: @connect_frame = ui.get_object('connect_frame') 74: @connect_button = ui.get_object('connect_button') 75: @connect_error = ui.get_object('connect_error') 76: 77: ui.register_handler('server_hostname_changed', 78: method(:server_hostname_changed)) 79: ui.register_handler('server_username_changed', 80: method(:server_username_changed)) 81: ui.register_handler('server_password_changed', 82: method(:server_password_changed)) 83: ui.register_handler('connect_button_clicked', 84: method(:connect_button_clicked)) 85: 86: @hostname = @hostname_ui.text.strip.length > 0 87: @username = @username_ui.text.strip.length > 0 88: @password = @password_ui.text.length > 0 # Allow spaces in passwords 89: @state = UI_STATE_INVALID 90: 91: @ui = ui 92: @converter = converter 93: end
# File lib/virt-p2v/ui/connect.rb, line 69 69: def self.init(ui, converter) 70: @hostname_ui = ui.get_object('server_hostname') 71: @username_ui = ui.get_object('server_username') 72: @password_ui = ui.get_object('server_password') 73: @connect_frame = ui.get_object('connect_frame') 74: @connect_button = ui.get_object('connect_button') 75: @connect_error = ui.get_object('connect_error') 76: 77: ui.register_handler('server_hostname_changed', 78: method(:server_hostname_changed)) 79: ui.register_handler('server_username_changed', 80: method(:server_username_changed)) 81: ui.register_handler('server_password_changed', 82: method(:server_password_changed)) 83: ui.register_handler('connect_button_clicked', 84: method(:connect_button_clicked)) 85: 86: @hostname = @hostname_ui.text.strip.length > 0 87: @username = @username_ui.text.strip.length > 0 88: @password = @password_ui.text.length > 0 # Allow spaces in passwords 89: @state = UI_STATE_INVALID 90: 91: @ui = ui 92: @converter = converter 93: end
# File lib/virt-p2v/ui/connect.rb, line 127 127: def self.server_hostname_changed 128: event(EV_HOSTNAME, @hostname_ui.text.strip.length > 0) 129: end
# File lib/virt-p2v/ui/connect.rb, line 127 127: def self.server_hostname_changed 128: event(EV_HOSTNAME, @hostname_ui.text.strip.length > 0) 129: end
# File lib/virt-p2v/ui/connect.rb, line 135 135: def self.server_password_changed 136: event(EV_PASSWORD, @password_ui.text.length > 0) 137: end
# File lib/virt-p2v/ui/connect.rb, line 135 135: def self.server_password_changed 136: event(EV_PASSWORD, @password_ui.text.length > 0) 137: end
# File lib/virt-p2v/ui/connect.rb, line 131 131: def self.server_username_changed 132: event(EV_USERNAME, @username_ui.text.strip.length > 0) 133: end
# File lib/virt-p2v/ui/connect.rb, line 131 131: def self.server_username_changed 132: event(EV_USERNAME, @username_ui.text.strip.length > 0) 133: end
# File lib/virt-p2v/ui/connect.rb, line 95 95: def self.set_state(state) 96: # Don't do anything if state hasn't changed 97: return if state == @state 98: 99: case state 100: when UI_STATE_INVALID 101: @connect_frame.sensitive = true 102: @connect_button.sensitive = false 103: 104: @state = UI_STATE_INVALID 105: when UI_STATE_VALID 106: @connect_frame.sensitive = true 107: @connect_button.sensitive = true 108: 109: @state = UI_STATE_VALID 110: when UI_STATE_ACTIVATING 111: @connect_frame.sensitive = false 112: @connect_button.sensitive = false 113: @connect_error.text = '' 114: 115: @state = UI_STATE_ACTIVATING 116: when UI_STATE_COMPLETE 117: # Activate the next page 118: @ui.active_page = 'conversion_win' 119: 120: # ... then leave this one as we hope to find it if we come back here 121: set_state(UI_STATE_VALID) 122: else 123: raise "Attempt to set unexpected UI state: #{@state}" 124: end 125: end
# File lib/virt-p2v/ui/connect.rb, line 95 95: def self.set_state(state) 96: # Don't do anything if state hasn't changed 97: return if state == @state 98: 99: case state 100: when UI_STATE_INVALID 101: @connect_frame.sensitive = true 102: @connect_button.sensitive = false 103: 104: @state = UI_STATE_INVALID 105: when UI_STATE_VALID 106: @connect_frame.sensitive = true 107: @connect_button.sensitive = true 108: 109: @state = UI_STATE_VALID 110: when UI_STATE_ACTIVATING 111: @connect_frame.sensitive = false 112: @connect_button.sensitive = false 113: @connect_error.text = '' 114: 115: @state = UI_STATE_ACTIVATING 116: when UI_STATE_COMPLETE 117: # Activate the next page 118: @ui.active_page = 'conversion_win' 119: 120: # ... then leave this one as we hope to find it if we come back here 121: set_state(UI_STATE_VALID) 122: else 123: raise "Attempt to set unexpected UI state: #{@state}" 124: end 125: end