~rjarry/aerc

43435ba06cd0820a83f14630881981b338473cb8 — Jeffas 2 years ago 0ce1d42
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.
1 files changed, 55 insertions(+), 16 deletions(-)

M worker/maildir/worker.go
M worker/maildir/worker.go => worker/maildir/worker.go +55 -16
@@ 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)