1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-09-28 04:58:50 +00:00
cchat-gtk/internal/ui/service/service.go

208 lines
5.2 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package service
import (
"github.com/diamondburned/cchat"
2020-06-07 07:06:13 +00:00
"github.com/diamondburned/cchat-gtk/internal/gts"
2020-06-07 04:27:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/keyring"
2020-06-07 07:06:13 +00:00
"github.com/diamondburned/cchat-gtk/internal/log"
2020-05-28 19:26:55 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2020-06-06 00:47:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/service/breadcrumb"
2020-05-26 06:51:06 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/service/session"
2020-06-07 04:27:28 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/service/session/server"
2020-05-26 06:51:06 +00:00
"github.com/gotk3/gotk3/gtk"
2020-06-07 07:06:13 +00:00
"github.com/pkg/errors"
2020-05-26 06:51:06 +00:00
)
type View struct {
*gtk.ScrolledWindow
Box *gtk.Box
Services []*Container
}
func NewView() *View {
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
box.Show()
2020-05-28 19:26:55 +00:00
primitives.AddClass(box, "services")
2020-05-26 06:51:06 +00:00
sw, _ := gtk.ScrolledWindowNew(nil, nil)
2020-05-28 19:26:55 +00:00
sw.SetPolicy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
2020-05-26 06:51:06 +00:00
sw.Add(box)
2020-05-28 19:26:55 +00:00
sw.Show()
2020-05-26 06:51:06 +00:00
return &View{
sw,
box,
nil,
}
}
2020-06-04 23:00:41 +00:00
func (v *View) AddService(svc cchat.Service, ctrl Controller) *Container {
s := NewContainer(svc, ctrl)
2020-05-26 06:51:06 +00:00
v.Services = append(v.Services, s)
v.Box.Add(s)
2020-06-07 07:06:13 +00:00
// Try and restore all sessions.
s.restoreAllSessions()
2020-06-04 23:00:41 +00:00
return s
2020-05-26 06:51:06 +00:00
}
2020-06-07 07:06:13 +00:00
type Controller interface {
// MessageRowSelected is wrapped around session's MessageRowSelected.
MessageRowSelected(*session.Row, *server.Row, cchat.ServerMessage)
// AuthenticateSession is called to spawn the authentication dialog.
AuthenticateSession(*Container, cchat.Service)
// RemoveSession is called to remove a session. This should also clear out
// the message view in the parent package.
RemoveSession(id string)
}
2020-05-26 06:51:06 +00:00
type Container struct {
*gtk.Box
2020-06-07 04:27:28 +00:00
Service cchat.Service
2020-05-26 06:51:06 +00:00
header *header
revealer *gtk.Revealer
children *children
2020-06-07 04:27:28 +00:00
// Embed controller and extend it to override RestoreSession.
Controller
2020-05-26 06:51:06 +00:00
}
2020-06-07 07:06:13 +00:00
// Guarantee that our interface is up-to-date with session's controller.
var _ session.Controller = (*Container)(nil)
2020-06-04 23:00:41 +00:00
func NewContainer(svc cchat.Service, ctrl Controller) *Container {
2020-05-26 06:51:06 +00:00
children := newChildren()
2020-05-28 19:26:55 +00:00
2020-05-26 06:51:06 +00:00
chrev, _ := gtk.RevealerNew()
2020-06-04 23:00:41 +00:00
chrev.SetRevealChild(true)
2020-05-26 06:51:06 +00:00
chrev.Add(children)
2020-05-28 19:26:55 +00:00
chrev.Show()
2020-05-26 06:51:06 +00:00
2020-06-04 23:00:41 +00:00
header := newHeader(svc)
header.reveal.SetActive(chrev.GetRevealChild())
2020-05-26 06:51:06 +00:00
box, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
box.Show()
box.PackStart(header, false, false, 0)
box.PackStart(chrev, false, false, 0)
2020-05-28 19:26:55 +00:00
primitives.AddClass(box, "service")
2020-05-26 06:51:06 +00:00
2020-06-07 04:27:28 +00:00
container := &Container{
Box: box,
Service: svc,
header: header,
revealer: chrev,
children: children,
Controller: ctrl,
}
2020-05-28 19:26:55 +00:00
// On click, toggle reveal.
header.reveal.Connect("clicked", func() {
revealed := !chrev.GetRevealChild()
chrev.SetRevealChild(revealed)
header.reveal.SetActive(revealed)
})
// On click, show the auth dialog.
2020-05-26 06:51:06 +00:00
header.add.Connect("clicked", func() {
2020-06-04 23:00:41 +00:00
ctrl.AuthenticateSession(container, svc)
2020-05-26 06:51:06 +00:00
})
2020-06-07 04:27:28 +00:00
// Make menu items.
2020-06-07 07:06:13 +00:00
primitives.AppendMenuItems(header.Menu, []*gtk.MenuItem{
primitives.MenuItem("Save Sessions", func() {
container.SaveAllSessions()
}),
2020-06-07 04:27:28 +00:00
})
2020-05-26 06:51:06 +00:00
return container
}
2020-06-07 04:27:28 +00:00
func (c *Container) AddSession(ses cchat.Session) *session.Row {
srow := session.New(c, ses, c)
c.children.addSessionRow(ses.ID(), srow)
2020-06-07 07:06:13 +00:00
c.SaveAllSessions()
2020-06-07 04:27:28 +00:00
return srow
2020-06-04 23:00:41 +00:00
}
2020-06-07 04:27:28 +00:00
func (c *Container) AddLoadingSession(id, name string) *session.Row {
srow := session.NewLoading(c, name, c)
c.children.addSessionRow(id, srow)
return srow
2020-05-26 06:51:06 +00:00
}
2020-06-07 07:06:13 +00:00
func (c *Container) RemoveSession(id string) {
c.children.removeSessionRow(id)
c.SaveAllSessions()
// Call the parent's method.
c.Controller.RemoveSession(id)
}
// RestoreSession tries to restore sessions asynchronously. This satisfies
// session.Controller.
func (c *Container) RestoreSession(row *session.Row, krs keyring.Session) {
// Can this session be restored? If not, exit.
restorer, ok := c.Service.(cchat.SessionRestorer)
if !ok {
return
}
c.restoreSession(row, restorer, krs)
}
// internal method called on AddService.
func (c *Container) restoreAllSessions() {
// Can this session be restored? If not, exit.
restorer, ok := c.Service.(cchat.SessionRestorer)
if !ok {
return
}
var sessions = keyring.RestoreSessions(c.Service.Name())
for _, krs := range sessions {
// Copy the session to avoid race conditions.
krs := krs
row := c.AddLoadingSession(krs.ID, krs.Name)
c.restoreSession(row, restorer, krs)
}
}
func (c *Container) restoreSession(r *session.Row, res cchat.SessionRestorer, k keyring.Session) {
go func() {
s, err := res.RestoreSession(k.Data)
if err != nil {
err = errors.Wrapf(err, "Failed to restore session %s (%s)", k.ID, k.Name)
log.Error(err)
gts.ExecAsync(func() { r.SetFailed(k, err) })
} else {
gts.ExecAsync(func() { r.SetSession(s) })
}
}()
}
func (c *Container) SaveAllSessions() {
keyring.SaveSessions(c.Service.Name(), c.keyringSessions())
}
// keyringSessions returns all known keyring sessions. Sessions that can't be
2020-06-07 04:27:28 +00:00
// saved will not be in the slice.
2020-06-07 07:06:13 +00:00
func (c *Container) keyringSessions() []keyring.Session {
2020-06-07 04:27:28 +00:00
var ksessions = make([]keyring.Session, 0, len(c.children.Sessions))
for _, s := range c.children.Sessions {
if k := s.KeyringSession(); k != nil {
ksessions = append(ksessions, *k)
2020-06-06 07:44:36 +00:00
}
}
2020-06-07 04:27:28 +00:00
return ksessions
2020-05-26 06:51:06 +00:00
}
2020-06-07 04:27:28 +00:00
func (c *Container) Breadcrumb() breadcrumb.Breadcrumb {
return breadcrumb.Try(nil, c.header.reveal.GetText())
2020-05-26 06:51:06 +00:00
}