From 84fa12fef1736d04ee79e40cebaadadda262f063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Benencia?= Date: Tue, 3 Sep 2013 20:24:51 -0300 Subject: Mark as read functionality --- Email.hs | 2 +- Handlers.hs | 41 +++++++++++++++++++++++++++++++++++++++-- Maildir.hs | 31 +++++++++++++++++++++++++------ State.hs | 12 +++++++----- Types.hs | 2 ++ 5 files changed, 74 insertions(+), 14 deletions(-) diff --git a/Email.hs b/Email.hs index 659d96b..db6f17b 100644 --- a/Email.hs +++ b/Email.hs @@ -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 }) } + diff --git a/Maildir.hs b/Maildir.hs index 7e2941d..752f6f0 100644 --- a/Maildir.hs +++ b/Maildir.hs @@ -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 diff --git a/State.hs b/State.hs index 5d04035..5c3112f 100644 --- a/State.hs +++ b/State.hs @@ -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 { diff --git a/Types.hs b/Types.hs index c5e9428..07c9144 100644 --- a/Types.hs +++ b/Types.hs @@ -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 { -- cgit v1.2.3