aerc/ui/account.go

91 lines
1.9 KiB
Go
Raw Normal View History

2018-01-11 03:41:15 +00:00
package ui
import (
2018-02-01 02:18:21 +00:00
"log"
2018-01-11 14:04:18 +00:00
2018-01-11 03:41:15 +00:00
tb "github.com/nsf/termbox-go"
"git.sr.ht/~sircmpwn/aerc2/config"
"git.sr.ht/~sircmpwn/aerc2/worker"
2018-01-11 14:04:18 +00:00
"git.sr.ht/~sircmpwn/aerc2/worker/types"
2018-01-11 03:41:15 +00:00
)
type AccountTab struct {
2018-02-01 23:42:03 +00:00
Config *config.AccountConfig
Worker *types.Worker
Parent *UIState
logger *log.Logger
counter int
2018-01-11 03:41:15 +00:00
}
2018-02-01 02:18:21 +00:00
func NewAccountTab(conf *config.AccountConfig,
logger *log.Logger) (*AccountTab, error) {
work, err := worker.NewWorker(conf.Source, logger)
2018-01-11 14:04:18 +00:00
if err != nil {
return nil, err
}
2018-02-01 23:42:03 +00:00
go work.Backend.Run()
2018-02-01 13:48:33 +00:00
acc := &AccountTab{
2018-02-01 23:42:03 +00:00
Config: conf,
Worker: work,
logger: logger,
2018-02-01 13:48:33 +00:00
}
2018-02-01 23:42:03 +00:00
acc.Worker.PostAction(types.Configure{Config: conf}, nil)
acc.Worker.PostAction(types.Connect{}, func(msg types.WorkerMessage) {
2018-02-01 13:48:33 +00:00
if _, ok := msg.(types.Ack); ok {
acc.logger.Println("Connected.")
2018-02-02 00:34:08 +00:00
acc.Worker.PostAction(types.ListDirectories{}, nil)
2018-02-01 13:48:33 +00:00
} else {
acc.logger.Println("Connection failed.")
}
})
return acc, nil
2018-01-11 03:41:15 +00:00
}
func (acc *AccountTab) Name() string {
return acc.Config.Name
}
func (acc *AccountTab) SetParent(parent *UIState) {
acc.Parent = parent
}
func (acc *AccountTab) Render(at Geometry) {
cell := tb.Cell{
2018-01-11 14:04:18 +00:00
Ch: ' ',
2018-01-11 03:41:15 +00:00
Fg: tb.ColorDefault,
Bg: tb.ColorDefault,
}
2018-01-11 14:04:18 +00:00
TFill(at, cell)
TPrintf(&at, cell, "%s %d\n", acc.Name(), acc.counter)
2018-01-11 03:54:55 +00:00
acc.counter++
if acc.counter%10000 == 0 {
acc.counter = 0
}
acc.Parent.InvalidateFrom(acc)
2018-01-11 03:41:15 +00:00
}
2018-01-11 14:04:18 +00:00
func (acc *AccountTab) GetChannel() chan types.WorkerMessage {
2018-02-01 23:42:03 +00:00
return acc.Worker.Messages
2018-02-01 02:54:52 +00:00
}
2018-01-11 14:04:18 +00:00
func (acc *AccountTab) HandleMessage(msg types.WorkerMessage) {
2018-02-01 23:42:03 +00:00
msg = acc.Worker.ProcessMessage(msg)
2018-02-01 02:54:52 +00:00
switch msg.(type) {
case types.Ack:
2018-02-02 00:34:08 +00:00
case types.Unsupported:
2018-02-01 02:54:52 +00:00
// no-op
case types.ApproveCertificate:
// TODO: Ask the user
acc.logger.Println("Approving certificate")
2018-02-01 23:42:03 +00:00
acc.Worker.PostAction(types.Ack{
2018-02-01 02:54:52 +00:00
Message: types.RespondTo(msg),
2018-02-01 13:48:33 +00:00
}, nil)
2018-02-01 02:18:21 +00:00
default:
2018-02-01 23:42:03 +00:00
acc.Worker.PostAction(types.Unsupported{
2018-02-01 02:54:52 +00:00
Message: types.RespondTo(msg),
2018-02-01 13:48:33 +00:00
}, nil)
2018-02-01 02:18:21 +00:00
}
2018-01-11 14:04:18 +00:00
}