neko/internal/websocket/filechooserdialog.go

68 lines
1.7 KiB
Go
Raw Normal View History

2021-01-20 23:48:50 +01:00
package websocket
import (
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
2021-02-14 17:11:21 +01:00
func (manager *WebSocketManagerCtx) fileChooserDialogEvents() {
2021-03-13 23:27:28 +01:00
var activeSession types.Session
2021-01-20 23:48:50 +01:00
// when dialog opens, everyone should be notified.
2021-02-14 17:11:21 +01:00
manager.desktop.OnFileChooserDialogOpened(func() {
manager.logger.Info().Msg("FileChooserDialog opened")
2021-01-20 23:48:50 +01:00
2021-02-14 17:11:21 +01:00
host := manager.sessions.GetHost()
2021-01-20 23:48:50 +01:00
if host == nil {
2021-02-14 17:11:21 +01:00
manager.logger.Warn().Msg("no host for FileChooserDialog found, closing")
go manager.desktop.CloseFileChooserDialog()
2021-01-20 23:48:50 +01:00
return
}
2021-03-13 23:27:28 +01:00
activeSession = host
2021-01-20 23:48:50 +01:00
2021-03-14 00:05:31 +01:00
go manager.sessions.Broadcast(message.SessionID{
2021-02-14 14:40:17 +01:00
Event: event.FILE_CHOOSER_DIALOG_OPENED,
ID: host.ID(),
2021-01-20 23:48:50 +01:00
}, nil)
})
// when dialog closes, everyone should be notified.
2021-02-14 17:11:21 +01:00
manager.desktop.OnFileChooserDialogClosed(func() {
manager.logger.Info().Msg("FileChooserDialog closed")
2021-01-20 23:48:50 +01:00
2021-03-13 23:27:28 +01:00
activeSession = nil
2021-01-20 23:48:50 +01:00
2021-03-14 00:05:31 +01:00
go manager.sessions.Broadcast(message.SessionID{
2021-02-14 14:40:17 +01:00
Event: event.FILE_CHOOSER_DIALOG_CLOSED,
2021-01-20 23:48:50 +01:00
}, nil)
})
// when new user joins, and someone holds dialog, he shouldd be notified about it.
2021-02-14 17:11:21 +01:00
manager.sessions.OnConnected(func(session types.Session) {
2021-03-13 23:27:28 +01:00
if activeSession == nil {
2021-01-20 23:48:50 +01:00
return
}
2021-03-14 00:05:31 +01:00
if err := session.Send(message.SessionID{
2021-02-14 14:40:17 +01:00
Event: event.FILE_CHOOSER_DIALOG_OPENED,
2021-03-13 23:27:28 +01:00
ID: activeSession.ID(),
2021-01-20 23:48:50 +01:00
}); err != nil {
2021-02-14 17:11:21 +01:00
manager.logger.Warn().
2021-01-20 23:48:50 +01:00
Err(err).
2021-03-16 10:53:03 +01:00
Str("session_id", session.ID()).
2021-01-20 23:48:50 +01:00
Msgf("could not send event `%s` to session", event.FILE_CHOOSER_DIALOG_OPENED)
}
})
// when user, that holds dialog, disconnects, it should be closed.
2021-02-14 17:11:21 +01:00
manager.sessions.OnDisconnected(func(session types.Session) {
2021-03-13 23:27:28 +01:00
if activeSession == nil || activeSession != session {
2021-01-20 23:48:50 +01:00
return
}
2021-02-14 17:11:21 +01:00
manager.desktop.CloseFileChooserDialog()
2021-01-20 23:48:50 +01:00
})
}