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