diff options
-rw-r--r-- | Email.hs | 2 | ||||
-rw-r--r-- | Handlers.hs | 41 | ||||
-rw-r--r-- | Maildir.hs | 31 | ||||
-rw-r--r-- | State.hs | 12 | ||||
-rw-r--r-- | Types.hs | 2 |
5 files changed, 74 insertions, 14 deletions
@@ -20,7 +20,7 @@ getBody msg = Nothing -> "This email has no displayable content." where unwrapContent (Single c) = c - + -- hackish function for showing the email. In he future the logic of this -- function should be improved. firstTextPart [] = Nothing diff --git a/Handlers.hs b/Handlers.hs index da886b3..5c04f89 100644 --- a/Handlers.hs +++ b/Handlers.hs @@ -26,7 +26,13 @@ import Types previousMode :: Mode -> LazymailCurses () previousMode MaildirMode = (=<<) put $ get >>= \st -> return st { exitRequested = True } -previousMode EmailMode = (=<<) put $ get >>= \st -> return st { mode = IndexMode } +previousMode EmailMode = do + st <- get + if (triggerUpdateIn . indexState $ st) + then do + changeMode MaildirMode + solveIndexUpdate + else put $ st { mode = IndexMode } previousMode IndexMode = do st <- get let ist = (indexState st) { selectedRowIn = 0, scrollRowIn = 0 } @@ -36,7 +42,13 @@ changeMode :: Mode -> LazymailCurses () changeMode EmailMode = return () changeMode IndexMode = do st <- get - msg <- liftIO $ UTF8.readFile (selectedEmailPath . indexState $ st) + let fp = selectedEmailPath . indexState $ st + nfp <- if (isNew fp) + then liftIO $ markAsRead fp + else return fp + when (fp /= nfp) triggerIndexUpdate + st <- get + msg <- liftIO $ UTF8.readFile nfp let email = parseMIMEMessage msg let body = getBody $ email let el = formatBody body $ screenColumns st @@ -193,3 +205,28 @@ formatMaildirModeRows st = mapM formatRow where pad = " " numPads = (length $ filter (== '/') str) + (length $ filter (`elem` imapSep) str) imapSep = ['.'] -- IMAP usually separates its directories with dots + +triggerIndexUpdate :: LazymailCurses () +triggerIndexUpdate = do + st <- get + let ist = indexState st + put $ st { indexState = (ist { triggerUpdateIn = True }) } + +solveIndexUpdate :: LazymailCurses () +solveIndexUpdate = do + st <- get + let ist = indexState st + put $ st { indexState = (ist { triggerUpdateIn = False }) } + +triggerMaildirUpdate :: LazymailCurses () +triggerMaildirUpdate = do + st <- get + let mst = maildirState st + put $ st { maildirState = (mst { triggerUpdateMD = True }) } + +solveMaildirUpdate :: LazymailCurses () +solveMaildirUpdate = do + st <- get + let mst = maildirState st + put $ st { maildirState = (mst { triggerUpdateMD = False }) } + @@ -11,8 +11,8 @@ module Maildir where import Control.Monad.Loops(allM) import Control.Monad (forM, filterM) import Data.List(isPrefixOf) -import System.Directory (doesDirectoryExist, getDirectoryContents) -import System.FilePath ((</>)) +import System.Directory (doesDirectoryExist, getDirectoryContents, renameFile) +import System.FilePath ((</>), takeFileName, takeDirectory, splitDirectories, joinPath) import System.IO(IOMode(..), hGetContents, openFile) import Types(Maildir, Flag(..), Flags) @@ -40,10 +40,29 @@ getMessages :: Maildir -> [FilePath] -> IO [(FilePath, Flags, String)] getMessages mb list = do messages <- getAll mb return $ filter (\(id, f, m) -> id `elem` list) messages - --- --- | Based on getRecursiveContents from Real World Haskell --- + +{- Given a mail in a Maildir, mark it as read -} +markAsRead :: FilePath -> IO FilePath +markAsRead fp = + case newPath of + Nothing -> return fp + Just path -> do + renameFile fp path + return path + where newPath = + if not $ isNew fp + then Just fp + else do + let fil = takeFileName fp + let dir = takeDirectory fp + let spl = splitDirectories dir + case last spl of + "cur" -> Just $ fp ++ "S" + "new" -> Just $ (joinPath . init $ spl) </> ("cur" </> (fil ++ "S")) + _ -> Nothing + + +-- Based on getRecursiveContents from Real World Haskell getMaildirsRecursively :: FilePath -> IO [Maildir] getMaildirsRecursively topdir = do result <- search topdir @@ -33,11 +33,12 @@ initialState = LazymailState { } initialMaildirState = MaildirState { - selectedRowMD = 0 - , selectedMD = "" - , detectedMDs = [] - , scrollRowMD = 0 - , scrollBufferMD = [] + selectedRowMD = 0 + , selectedMD = "" + , detectedMDs = [] + , scrollRowMD = 0 + , scrollBufferMD = [] + , triggerUpdateMD = False } initialIndexState = IndexState { @@ -47,6 +48,7 @@ initialIndexState = IndexState { , scrollRowIn = 0 , currentInLen = 0 , scrollBufferIn = [] + , triggerUpdateIn = False } initialEmailState = EmailState { @@ -88,6 +88,7 @@ data MaildirState = MaildirState { , detectedMDs :: [(FilePath, String)] , scrollRowMD :: Int , scrollBufferMD :: [(FilePath, String)] + , triggerUpdateMD :: Bool } data IndexState = IndexState { @@ -97,6 +98,7 @@ data IndexState = IndexState { , scrollRowIn :: Int , currentInLen :: Int , scrollBufferIn :: [(FilePath, String)] + , triggerUpdateIn :: Bool } data ComposeState = ComposeState { |