2023-05-03 21:21:04 +00:00
import "pkg:/source/roku_modules/log/LogMixin.brs"
import "pkg:/source/utils/config.brs"
2021-07-09 10:12:19 +00:00
sub init()
2023-05-03 21:21:04 +00:00
m.log = log.Logger("SetServerScreen")
2021-07-09 20:08:32 +00:00
m.top.setFocus(true)
2021-07-09 10:12:19 +00:00
2021-07-09 20:08:32 +00:00
m.spinner = m.top.findNode("spinner")
m.serverPicker = m.top.findNode("serverPicker")
m.serverUrlTextbox = m.top.findNode("serverUrlTextbox")
m.serverUrlContainer = m.top.findNode("serverUrlContainer")
m.serverUrlOutline = m.top.findNode("serverUrlOutline")
m.submit = m.top.findNode("submit")
2021-07-09 10:12:19 +00:00
2021-07-09 20:08:32 +00:00
m.top.observeField("serverUrl", "clearErrorMessage")
2021-07-16 02:32:34 +00:00
2021-07-09 20:08:32 +00:00
ScanForServers()
2021-07-09 10:12:19 +00:00
end sub
function onKeyEvent(key as string, press as boolean) as boolean
2023-05-03 21:21:04 +00:00
m.log.debug("SetServerScreen onKeyEvent", key, press)
2021-07-09 20:08:32 +00:00
if not press then return true
handled = true
if key = "OK" and m.serverPicker.hasFocus()
m.top.serverUrl = m.serverPicker.content.getChild(m.serverPicker.itemFocused).baseUrl
m.submit.setFocus(true)
'if the user pressed the down key and we are already at the last child of server picker, then change focus to the url textbox
else if key = "down" and m.serverPicker.hasFocus() and m.serverPicker.content.getChildCount() > 0 and m.serverPicker.itemFocused = m.serverPicker.content.getChildCount() - 1
m.serverUrlContainer.setFocus(true)
'user navigating up to the server picker from the input box (it's only focusable if it has items)
else if key = "up" and m.serverUrlContainer.hasFocus() and m.servers.Count() > 0
m.serverPicker.setFocus(true)
2022-09-08 19:19:48 +00:00
else if key = "up" and m.serverUrlContainer.hasFocus() and m.servers.Count() = 0
ScanForServers()
else if key = "back" and m.serverUrlContainer.hasFocus() and m.servers.Count() > 0
m.serverPicker.setFocus(true)
2021-07-09 20:08:32 +00:00
else if key = "OK" and m.serverUrlContainer.hasFocus()
ShowKeyboard()
2022-09-08 19:19:48 +00:00
else if key = "back" and m.submit.hasFocus() and m.servers.Count() > 0
m.serverPicker.setFocus(true)
else if key = "back" and m.submit.hasFocus() and m.servers.Count() = 0
m.serverUrlContainer.setFocus(true)
else if key = "back" and m.serverUrlContainer.hasFocus() and m.servers.Count() = 0
ScanForServers()
else if key = "back" and m.serverPicker.hasFocus() and m.servers.Count() > 0
ScanForServers()
' On "back" with or without available local servers, will rescan for servers
2021-07-09 20:08:32 +00:00
else if key = "up" and m.submit.hasFocus()
m.serverUrlContainer.setFocus(true)
'focus the submit button from serverUrl
else if key = "down" and m.serverUrlContainer.hasFocus()
m.submit.setFocus(true)
2021-12-24 04:08:43 +00:00
else if key = "options"
2022-05-07 15:21:37 +00:00
if m.serverPicker.itemFocused >= 0 and m.serverPicker.itemFocused < m.serverPicker.content.getChildCount()
serverName = m.serverPicker.content.getChild(m.serverPicker.itemFocused).name
if m.servers.Count() > 0 and Instr(1, serverName, "Saved") > 0
'Can only delete previously saved servers, not locally discovered ones
'So if we are on a "Saved" item, let the options dialog be shown (handled elsewhere)
handled = false
end if
2021-12-24 04:08:43 +00:00
end if
2021-07-09 20:08:32 +00:00
else
handled = false
end if
'show/hide input box outline
m.serverUrlOutline.visible = m.serverUrlContainer.isInFocusChain()
return handled
2021-07-09 10:12:19 +00:00
end function
2021-07-09 19:16:43 +00:00
sub ScanForServers()
2021-07-09 20:08:32 +00:00
m.ssdpScanner = CreateObject("roSGNode", "ServerDiscoveryTask")
'run the task
m.ssdpScanner.observeField("content", "ScanForServersComplete")
m.ssdpScanner.control = "RUN"
2022-09-08 19:19:48 +00:00
m.spinner.visible = true
2021-07-09 19:16:43 +00:00
end sub
2021-07-09 10:12:19 +00:00
sub ScanForServersComplete(event)
2021-07-09 20:08:32 +00:00
m.servers = event.getData()
items = CreateObject("roSGNode", "ContentNode")
for each server in m.servers
server.subtype = "ContentNode"
'add new fields for every server property onto the ContentNode (rather than making a dedicated component just to hold data...)
items.update([server], true)
end for
2021-12-23 01:00:47 +00:00
'load any previously logged in to servers as well (if they aren't already discovered on the local network)
saved = get_setting("saved_servers")
if saved <> invalid
savedServers = ParseJson(saved)
for each server in savedServers.serverList
alreadyListed = false
for each listed in m.servers
2021-12-26 18:52:43 +00:00
if LCase(listed.baseUrl) = server.baseUrl 'saved server data is always lowercase
2021-12-23 01:00:47 +00:00
alreadyListed = true
exit for
end if
end for
if alreadyListed = false
items.update([server], true)
2021-12-26 18:52:43 +00:00
m.servers.push(server)
2021-12-24 04:08:43 +00:00
end if
2021-12-23 01:00:47 +00:00
end for
end if
2021-07-09 20:08:32 +00:00
m.serverPicker.content = items
m.spinner.visible = false
'if we have at least one server, focus on the server picker
if m.servers.Count() > 0
m.serverPicker.setFocus(true)
'no servers found...focus on the input textbox
else
m.serverUrlContainer.setFocus(true)
'show/hide input box outline
m.serverUrlOutline.visible = true
end if
2021-07-12 02:06:10 +00:00
2021-07-09 10:12:19 +00:00
end sub
2021-07-09 19:16:43 +00:00
sub ShowKeyboard()
2022-09-05 06:50:13 +00:00
dialog = createObject("roSGNode", "StandardKeyboardDialog")
2021-07-09 20:08:32 +00:00
dialog.title = tr("Enter the server name or ip address")
dialog.buttons = [tr("OK"), tr("Cancel")]
dialog.text = m.serverUrlTextbox.text
2022-09-05 06:50:13 +00:00
greenPalette = createObject("roSGNode", "RSGPalette")
greenPalette.colors = { DialogBackgroundColor: "#2A2B2A" }
dialog.palette = greenPalette
2021-07-09 10:12:19 +00:00
2021-07-09 20:08:32 +00:00
m.top.getscene().dialog = dialog
m.dialog = dialog
2021-07-09 10:12:19 +00:00
2021-07-09 20:08:32 +00:00
dialog.observeField("buttonSelected", "onDialogButton")
2021-07-09 19:16:43 +00:00
end sub
2021-07-09 10:12:19 +00:00
function onDialogButton()
2021-07-09 20:08:32 +00:00
d = m.dialog
button_text = d.buttons[d.buttonSelected]
if button_text = tr("OK")
m.serverUrlTextbox.text = d.text
m.dialog.close = true
return true
else if button_text = tr("Cancel")
m.dialog.close = true
return true
else
return false
end if
2021-07-09 10:12:19 +00:00
end function
2021-07-16 02:32:34 +00:00
sub clearErrorMessage()
2021-07-09 20:08:32 +00:00
m.top.errorMessage = ""
2021-07-16 02:32:34 +00:00
end sub