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
|
|
|
|
// 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 02:51:29 +01:00
|
|
|
update = true
|
2019-03-11 04:45:00 +01:00
|
|
|
break
|
|
|
|
case *types.MessageInfo:
|
|
|
|
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)
|
|
|
|
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{
|
|
|
|
logger: logger,
|
|
|
|
spinner: NewSpinner(),
|
|
|
|
}
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
for uid, msg := range ml.store.Messages {
|
|
|
|
if row >= ctx.Height() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if msg == nil {
|
|
|
|
needsHeaders = append(needsHeaders, uid)
|
|
|
|
ml.spinner.Draw(ctx.Subcontext(0, row, ctx.Width(), 1))
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if ml.store == store {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ml.store = store
|
|
|
|
if store != nil {
|
|
|
|
ml.spinner.Stop()
|
|
|
|
} else {
|
|
|
|
ml.spinner.Start()
|
|
|
|
}
|
|
|
|
ml.Invalidate()
|
|
|
|
}
|