Add directory info messages

This populates the directory info model properly when requested,
allowing the fields to be relied upon elsewhere.

This also sends the dirinfo when new messages come in.
This commit is contained in:
Jeffas 2019-09-16 19:22:50 +01:00 committed by Drew DeVault
parent 0ce1d42bda
commit 43435ba06c
1 changed files with 55 additions and 16 deletions

View File

@ -23,10 +23,11 @@ var errUnsupported = fmt.Errorf("unsupported command")
// A Worker handles interfacing between aerc's UI and a group of maildirs.
type Worker struct {
c *Container
selected *maildir.Dir
worker *types.Worker
watcher *fsnotify.Watcher
c *Container
selected *maildir.Dir
selectedName string
worker *types.Worker
watcher *fsnotify.Watcher
}
// NewWorker creates a new maildir worker with the provided worker.
@ -75,7 +76,7 @@ func (w *Worker) handleFSEvent(ev fsnotify.Event) {
if w.selected == nil {
return
}
_, err := w.selected.Unseen()
newUnseen, err := w.selected.Unseen()
if err != nil {
w.worker.Logger.Printf("could not move new to cur : %v", err)
return
@ -88,6 +89,11 @@ func (w *Worker) handleFSEvent(ev fsnotify.Event) {
w.worker.PostMessage(&types.DirectoryContents{
Uids: uids,
}, nil)
dirInfo := w.getDirectoryInfo()
dirInfo.Recent = len(newUnseen)
w.worker.PostMessage(&types.DirectoryInfo{
Info: dirInfo,
}, nil)
}
func (w *Worker) done(msg types.WorkerMessage) {
@ -101,6 +107,48 @@ func (w *Worker) err(msg types.WorkerMessage, err error) {
}, nil)
}
func (w *Worker) getDirectoryInfo() *models.DirectoryInfo {
dirInfo := &models.DirectoryInfo{
Name: w.selectedName,
Flags: []string{},
ReadOnly: false,
// total messages
Exists: 0,
// new messages since mailbox was last opened
Recent: 0,
// total unread
Unseen: 0,
}
uids, err := w.c.UIDs(*w.selected)
if err != nil {
w.worker.Logger.Printf("could not get uids: %v", err)
return dirInfo
}
dirInfo.Exists = len(uids)
for _, uid := range uids {
message, err := w.c.Message(*w.selected, uid)
if err != nil {
w.worker.Logger.Printf("could not get message: %v", err)
continue
}
flags, err := message.Flags()
if err != nil {
w.worker.Logger.Printf("could not get flags: %v", err)
continue
}
seen := false
for _, flag := range flags {
if flag == maildir.FlagSeen {
seen = true
}
}
if !seen {
dirInfo.Unseen++
}
}
return dirInfo
}
func (w *Worker) handleMessage(msg types.WorkerMessage) error {
switch msg := msg.(type) {
case *types.Unsupported:
@ -195,6 +243,7 @@ func (w *Worker) handleOpenDirectory(msg *types.OpenDirectory) error {
return err
}
w.selected = &dir
w.selectedName = msg.Directory
// add watch path
newDir := filepath.Join(string(*w.selected), "new")
@ -208,17 +257,7 @@ func (w *Worker) handleOpenDirectory(msg *types.OpenDirectory) error {
// TODO: why does this need to be sent twice??
info := &types.DirectoryInfo{
Info: &models.DirectoryInfo{
Name: msg.Directory,
Flags: []string{},
ReadOnly: false,
// total messages
Exists: 0,
// new messages since mailbox was last opened
Recent: 0,
// total unread
Unseen: 0,
},
Info: w.getDirectoryInfo(),
}
w.worker.PostMessage(info, nil)
w.worker.PostMessage(info, nil)