module VirtP2V::UI::Connect

Constants

EV_ACTIVATION
EV_BUTTON
EV_HOSTNAME
EV_PASSWORD
EV_USERNAME
UI_STATE_ACTIVATING
UI_STATE_COMPLETE
UI_STATE_INVALID
UI_STATE_VALID

Public Class Methods

connect_button_clicked() click to toggle source
# File lib/virt-p2v/ui/connect.rb, line 139
def self.connect_button_clicked
    event(EV_BUTTON, true)

    hostname = @hostname_ui.text.strip
    username = @username_ui.text.strip
    password = @password_ui.text

    connection = VirtP2V::Connection.new(hostname, username, password)
    @converter.connection = connection
    connection.connect { |result|
        case result
        when true
            event(EV_ACTIVATION, true)
        when VirtP2V::Connection::RemoteError
            @connect_error.text = 'Failed to start ' +
                                  'virt-p2v-server on remote ' +
                                  'server'
            event(EV_ACTIVATION, false)
        when VirtP2V::Connection::InvalidHostnameError
            @connect_error.text = "Unable to connect to #{hostname}"
            event(EV_ACTIVATION, false)
        when VirtP2V::Connection::InvalidCredentialsError
            @connect_error.text = "Invalid username/password"
            event(EV_ACTIVATION, false)
        else
            @connect_error.text = result.message
            event(EV_ACTIVATION, false)
        end
    }
end
event(event, status) click to toggle source
# File lib/virt-p2v/ui/connect.rb, line 33
def self.event(event, status)
    case event
    when EV_HOSTNAME
        @hostname = status
    when EV_USERNAME
        @username = status
    when EV_PASSWORD
        @password = status
    when EV_BUTTON, EV_ACTIVATION
        # Persistent state not required
    else
        raise "Unexpected event: #{event}"
    end

    valid = @hostname && @username && @password

    case @state
    when UI_STATE_INVALID
        set_state(UI_STATE_VALID) if valid
    when UI_STATE_VALID
        if !valid then
            set_state(UI_STATE_INVALID)
        elsif event == EV_BUTTON
            set_state(UI_STATE_ACTIVATING)
        end
    when UI_STATE_ACTIVATING
        # UI is disabled, so we shouldn't be getting any events other than
        # EV_ACTIVATION
        raise "Unexpected event: #{event}" unless event == EV_ACTIVATION

        set_state(status ? UI_STATE_COMPLETE : UI_STATE_VALID)
    else
        raise "Unexpected UI state: #{@state}"
    end
end
init(ui, converter) click to toggle source
# File lib/virt-p2v/ui/connect.rb, line 69
def self.init(ui, converter)
    @hostname_ui    = ui.get_object('server_hostname')
    @username_ui    = ui.get_object('server_username')
    @password_ui    = ui.get_object('server_password')
    @connect_frame  = ui.get_object('connect_frame')
    @connect_button = ui.get_object('connect_button')
    @connect_error  = ui.get_object('connect_error')

    ui.register_handler('server_hostname_changed',
                        method(:server_hostname_changed))
    ui.register_handler('server_username_changed',
                        method(:server_username_changed))
    ui.register_handler('server_password_changed',
                        method(:server_password_changed))
    ui.register_handler('connect_button_clicked',
                        method(:connect_button_clicked))

    @hostname = @hostname_ui.text.strip.length > 0
    @username = @username_ui.text.strip.length > 0
    @password = @password_ui.text.length > 0 # Allow spaces in passwords
    @state = UI_STATE_INVALID

    @ui = ui
    @converter = converter
end
server_hostname_changed() click to toggle source
# File lib/virt-p2v/ui/connect.rb, line 127
def self.server_hostname_changed
    event(EV_HOSTNAME, @hostname_ui.text.strip.length > 0)
end
server_password_changed() click to toggle source
# File lib/virt-p2v/ui/connect.rb, line 135
def self.server_password_changed
    event(EV_PASSWORD, @password_ui.text.length > 0)
end
server_username_changed() click to toggle source
# File lib/virt-p2v/ui/connect.rb, line 131
def self.server_username_changed
    event(EV_USERNAME, @username_ui.text.strip.length > 0)
end
set_state(state) click to toggle source
# File lib/virt-p2v/ui/connect.rb, line 95
def self.set_state(state)
    # Don't do anything if state hasn't changed
    return if state == @state

    case state
    when UI_STATE_INVALID
        @connect_frame.sensitive = true
        @connect_button.sensitive = false

        @state = UI_STATE_INVALID
    when UI_STATE_VALID
        @connect_frame.sensitive = true
        @connect_button.sensitive = true

        @state = UI_STATE_VALID
    when UI_STATE_ACTIVATING
        @connect_frame.sensitive = false
        @connect_button.sensitive = false
        @connect_error.text = ''

        @state = UI_STATE_ACTIVATING
    when UI_STATE_COMPLETE
        # Activate the next page
        @ui.active_page = 'conversion_win'

        # ... then leave this one as we hope to find it if we come back here
        set_state(UI_STATE_VALID)
    else
        raise "Attempt to set unexpected UI state: #{@state}"
    end
end