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