2019-03-11 04:45:00 +01:00
|
|
|
package widgets
|
|
|
|
|
|
|
|
import (
|
2019-03-15 02:37:00 +01:00
|
|
|
"log"
|
|
|
|
|
2019-03-15 02:51:29 +01:00
|
|
|
"github.com/emersion/go-imap"
|
2019-03-15 02:37:00 +01:00
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/config"
|
|
|
|
"git.sr.ht/~sircmpwn/aerc2/lib/ui"
|
2019-03-11 04:45:00 +01:00
|
|
|
"git.sr.ht/~sircmpwn/aerc2/worker/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MessageStore struct {
|
|
|
|
DirInfo types.DirectoryInfo
|
2019-03-15 02:51:29 +01:00
|
|
|
Messages map[uint32]*types.MessageInfo
|
2019-03-15 03:19:04 +01:00
|
|
|
// Ordered list of known UIDs
|
|
|
|
Uids []uint32
|
2019-03-15 02:51:29 +01:00
|
|
|
// Map of uids we've asked the worker to fetch
|
|
|
|
onUpdate func(store *MessageStore)
|
|
|
|
pendingBodies map[uint32]interface{}
|
|
|
|
pendingHeaders map[uint32]interface{}
|
|
|
|
worker *types.Worker
|
2019-03-11 04:45:00 +01:00
|
|
|
}
|
|
|
|
|
2019-03-15 02:51:29 +01:00
|
|
|
func NewMessageStore(worker *types.Worker,
|
|
|
|
dirInfo *types.DirectoryInfo) *MessageStore {
|
|
|
|
|
|
|
|
return &MessageStore{
|
|
|
|
DirInfo: *dirInfo,
|
|
|
|
|
|
|
|
pendingBodies: make(map[uint32]interface{}),
|
|
|
|
pendingHeaders: make(map[uint32]interface{}),
|
|
|
|
worker: worker,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MessageStore) FetchHeaders(uids []uint32) {
|
|
|
|
// TODO: this could be optimized by pre-allocating toFetch and trimming it
|
|
|
|
// at the end. In practice we expect to get most messages back in one frame.
|
|
|
|
var toFetch imap.SeqSet
|
|
|
|
for _, uid := range uids {
|
|
|
|
if _, ok := store.pendingHeaders[uid]; !ok {
|
|
|
|
toFetch.AddNum(uint32(uid))
|
|
|
|
store.pendingHeaders[uid] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !toFetch.Empty() {
|
|
|
|
store.worker.PostAction(&types.FetchMessageHeaders{
|
|
|
|
Uids: toFetch,
|
|
|
|
}, nil)
|
|
|
|
}
|
2019-03-11 04:45:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MessageStore) Update(msg types.WorkerMessage) {
|
2019-03-15 02:51:29 +01:00
|
|
|
update := false
|
2019-03-11 04:45:00 +01:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *types.DirectoryInfo:
|
|
|
|
store.DirInfo = *msg
|
2019-03-15 02:51:29 +01:00
|
|
|
update = true
|
2019-03-11 04:45:00 +01:00
|
|
|
break
|
|
|
|
case *types.DirectoryContents:
|
2019-03-15 02:51:29 +01:00
|
|
|
newMap := make(map[uint32]*types.MessageInfo)
|
2019-03-11 04:45:00 +01:00
|
|
|
for _, uid := range msg.Uids {
|
|
|
|
if msg, ok := store.Messages[uid]; ok {
|
|
|
|
newMap[uid] = msg
|
|
|
|
} else {
|
|
|
|
newMap[uid] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
store.Messages = newMap
|
2019-03-15 03:19:04 +01:00
|
|
|
store.Uids = msg.Uids
|
2019-03-15 02:51:29 +01:00
|
|
|
update = true
|
2019-03-11 04:45:00 +01:00
|
|
|
break
|
|
|
|
case *types.MessageInfo:
|
2019-03-15 03:19:04 +01:00
|
|
|
// TODO: merge message info into existing record, if applicable
|
2019-03-11 04:45:00 +01:00
|
|
|
store.Messages[msg.Uid] = msg
|
2019-03-15 02:51:29 +01:00
|
|
|
if _, ok := store.pendingHeaders[msg.Uid]; msg.Envelope != nil && ok {
|
|
|
|
delete(store.pendingHeaders, msg.Uid)
|
|
|
|
}
|
|
|
|
update = true
|
2019-03-11 04:45:00 +01:00
|
|
|
break
|
|
|
|
}
|
2019-03-15 02:51:29 +01:00
|
|
|
if update && store.onUpdate != nil {
|
|
|
|
store.onUpdate(store)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *MessageStore) OnUpdate(fn func(store *MessageStore)) {
|
|
|
|
store.onUpdate = fn
|
2019-03-11 04:45:00 +01:00
|
|
|
}
|
2019-03-15 02:37:00 +01:00
|
|
|
|
|
|
|
type MessageList struct {
|
|
|
|
conf *config.AercConfig
|
|
|
|
logger *log.Logger
|
|
|
|
onInvalidate func(d ui.Drawable)
|
2019-03-15 03:19:04 +01:00
|
|
|
selected int
|
2019-03-15 02:37:00 +01:00
|
|
|
spinner *Spinner
|
|
|
|
store *MessageStore
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: fish in config
|
2019-03-15 02:51:29 +01:00
|
|
|
func NewMessageList(logger *log.Logger) *MessageList {
|
2019-03-15 02:37:00 +01:00
|
|
|
ml := &MessageList{
|
2019-03-15 03:19:04 +01:00
|
|
|
logger: logger,
|
|
|
|
selected: 0,
|
|
|
|
spinner: NewSpinner(),
|
2019-03-15 02:37:00 +01:00
|
|
|
}
|
|
|
|
ml.spinner.OnInvalidate(func(_ ui.Drawable) {
|
|
|
|
ml.Invalidate()
|
|
|
|
})
|
|
|
|
// TODO: stop spinner, probably
|
|
|
|
ml.spinner.Start()
|
|
|
|
return ml
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *MessageList) OnInvalidate(onInvalidate func(d ui.Drawable)) {
|
|
|
|
ml.onInvalidate = onInvalidate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *MessageList) Invalidate() {
|
|
|
|
if ml.onInvalidate != nil {
|
|
|
|
ml.onInvalidate(ml)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *MessageList) Draw(ctx *ui.Context) {
|
|
|
|
ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
|
|
|
|
|
|
|
|
if ml.store == nil {
|
|
|
|
ml.spinner.Draw(ctx)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2019-03-15 02:51:29 +01:00
|
|
|
needsHeaders []uint32
|
2019-03-15 02:37:00 +01:00
|
|
|
row int = 0
|
|
|
|
)
|
|
|
|
|
2019-03-15 03:19:04 +01:00
|
|
|
for i := len(ml.store.Uids) - 1; i >= 0; i-- {
|
|
|
|
uid := ml.store.Uids[i]
|
|
|
|
msg := ml.store.Messages[uid]
|
|
|
|
|
2019-03-15 02:37:00 +01:00
|
|
|
if row >= ctx.Height() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg == nil {
|
|
|
|
needsHeaders = append(needsHeaders, uid)
|
|
|
|
ml.spinner.Draw(ctx.Subcontext(0, row, ctx.Width(), 1))
|
2019-03-15 03:19:04 +01:00
|
|
|
row += 1
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
style := tcell.StyleDefault
|
|
|
|
if row == ml.selected {
|
|
|
|
style = style.Background(tcell.ColorWhite).
|
|
|
|
Foreground(tcell.ColorBlack)
|
2019-03-15 02:37:00 +01:00
|
|
|
}
|
2019-03-15 03:19:04 +01:00
|
|
|
ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
|
|
|
|
ctx.Printf(0, row, style, "%s", msg.Envelope.Subject)
|
2019-03-15 02:37:00 +01:00
|
|
|
|
|
|
|
row += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(needsHeaders) != 0 {
|
2019-03-15 02:51:29 +01:00
|
|
|
ml.store.FetchHeaders(needsHeaders)
|
2019-03-15 02:37:00 +01:00
|
|
|
ml.spinner.Start()
|
|
|
|
} else {
|
|
|
|
ml.spinner.Stop()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *MessageList) SetStore(store *MessageStore) {
|
|
|
|
ml.store = store
|
|
|
|
if store != nil {
|
|
|
|
ml.spinner.Stop()
|
|
|
|
} else {
|
|
|
|
ml.spinner.Start()
|
|
|
|
}
|
|
|
|
ml.Invalidate()
|
|
|
|
}
|
2019-03-15 04:41:25 +01:00
|
|
|
|
|
|
|
func (ml *MessageList) nextPrev(delta int) {
|
|
|
|
ml.selected += delta
|
|
|
|
if ml.selected < 0 {
|
|
|
|
ml.selected = len(ml.store.Uids) - 1
|
|
|
|
}
|
|
|
|
if ml.selected >= len(ml.store.Uids) {
|
|
|
|
ml.selected = 0
|
|
|
|
}
|
|
|
|
// TODO: scrolling
|
|
|
|
ml.Invalidate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *MessageList) Next() {
|
|
|
|
ml.nextPrev(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *MessageList) Prev() {
|
|
|
|
ml.nextPrev(-1)
|
|
|
|
}
|