about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJeffas <dev@jeffas.io>2019-09-16 19:22:50 +0100
committerDrew DeVault <sir@cmpwn.com>2019-09-18 19:34:30 -0400
commit43435ba06cd0820a83f14630881981b338473cb8 (patch)
treeaea29c73a6c047452eb242c8eb6309bf089a6ae3
parent0ce1d42bda406a1d7c83df462206f435a841a809 (diff)
downloadaerc-43435ba06cd0820a83f14630881981b338473cb8.tar.gz
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.
-rw-r--r--worker/maildir/worker.go71
1 files changed, 55 insertions, 16 deletions
diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go
index 3e59da6..597e0d2 100644
--- a/worker/maildir/worker.go
+++ b/worker/maildir/worker.go
@@ -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)