Fix crashes when operating on empty folder (#216)

This commit is contained in:
Daniel Bridges 2019-07-09 17:04:21 -07:00 committed by Drew DeVault
parent 3f30c27bb3
commit 217e85a55d
11 changed files with 45 additions and 15 deletions

View file

@ -41,7 +41,10 @@ func (_ Archive) Execute(aerc *widgets.Aerc, args []string) error {
if acct == nil {
return errors.New("No account selected")
}
msg := widget.SelectedMessage()
msg, err := widget.SelectedMessage()
if err != nil {
return err
}
store := widget.Store()
archiveDir := acct.AccountConfig().Archive
store.Next()

View file

@ -44,7 +44,10 @@ func (_ Copy) Execute(aerc *widgets.Aerc, args []string) error {
}
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
msg := widget.SelectedMessage()
msg, err := widget.SelectedMessage()
if err != nil {
return err
}
store := widget.Store()
store.Copy([]uint32{msg.Uid}, args[optind], createParents, func(
msg types.WorkerMessage) {

View file

@ -35,10 +35,10 @@ func (_ Delete) Execute(aerc *widgets.Aerc, args []string) error {
return errors.New("No account selected")
}
store := widget.Store()
if len(store.Uids) == 0 {
return errors.New("No message selected")
msg, err := widget.SelectedMessage()
if err != nil {
return err
}
msg := widget.SelectedMessage()
_, isMsgView := widget.(*widgets.MessageViewer)
if isMsgView {
aerc.RemoveTab(widget)

View file

@ -49,7 +49,10 @@ func (_ Move) Execute(aerc *widgets.Aerc, args []string) error {
if acct == nil {
return errors.New("No account selected")
}
msg := widget.SelectedMessage()
msg, err := widget.SelectedMessage()
if err != nil {
return err
}
store := widget.Store()
_, isMsgView := widget.(*widgets.MessageViewer)
if isMsgView {

View file

@ -112,7 +112,10 @@ func (_ Pipe) Execute(aerc *widgets.Aerc, args []string) error {
if pipeFull {
store := provider.Store()
msg := provider.SelectedMessage()
msg, err := provider.SelectedMessage()
if err != nil {
return err
}
store.FetchFull([]uint32{msg.Uid}, func(reader io.Reader) {
if background {
doExec(reader)

View file

@ -30,7 +30,10 @@ func (_ Read) Execute(aerc *widgets.Aerc, args []string) error {
}
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
msg := widget.SelectedMessage()
msg, err := widget.SelectedMessage()
if err != nil {
return err
}
store := widget.Store()
store.Read([]uint32{msg.Uid}, args[0] == "read", func(
msg types.WorkerMessage) {

View file

@ -60,7 +60,10 @@ func (_ reply) Execute(aerc *widgets.Aerc, args []string) error {
conf := acct.AccountConfig()
us, _ := gomail.ParseAddress(conf.From)
store := widget.Store()
msg := widget.SelectedMessage()
msg, err := widget.SelectedMessage()
if err != nil {
return err
}
acct.Logger().Println("Replying to email " + msg.Envelope.MessageId)
var (

View file

@ -34,7 +34,11 @@ func (Unsubscribe) Execute(aerc *widgets.Aerc, args []string) error {
return errors.New("Usage: unsubscribe")
}
widget := aerc.SelectedTab().(widgets.ProvidesMessage)
headers := widget.SelectedMessage().RFC822Headers
msg, err := widget.SelectedMessage()
if err != nil {
return err
}
headers := msg.RFC822Headers
if !headers.Has("list-unsubscribe") {
return errors.New("No List-Unsubscribe header found")
}

View file

@ -1,6 +1,7 @@
package widgets
import (
"errors"
"fmt"
"log"
@ -170,8 +171,11 @@ func (acct *AccountView) SelectedAccount() *AccountView {
return acct
}
func (acct *AccountView) SelectedMessage() *models.MessageInfo {
return acct.msglist.Selected()
func (acct *AccountView) SelectedMessage() (*models.MessageInfo, error) {
if len(acct.msglist.Store().Uids) == 0 {
return nil, errors.New("no message selected")
}
return acct.msglist.Selected(), nil
}
func (acct *AccountView) SelectedMessagePart() *PartInfo {

View file

@ -2,6 +2,7 @@ package widgets
import (
"bufio"
"errors"
"fmt"
"io"
"os/exec"
@ -211,8 +212,11 @@ func (mv *MessageViewer) SelectedAccount() *AccountView {
return mv.acct
}
func (mv *MessageViewer) SelectedMessage() *models.MessageInfo {
return mv.msg
func (mv *MessageViewer) SelectedMessage() (*models.MessageInfo, error) {
if mv.msg == nil {
return nil, errors.New("no message selected")
}
return mv.msg, nil
}
func (mv *MessageViewer) ToggleHeaders() {

View file

@ -17,6 +17,6 @@ type ProvidesMessage interface {
ui.Drawable
Store() *lib.MessageStore
SelectedAccount() *AccountView
SelectedMessage() *models.MessageInfo
SelectedMessage() (*models.MessageInfo, error)
SelectedMessagePart() *PartInfo
}