aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Utility/EventCache.hs
blob: 1ad07a52a1404320ee1f4faf6166c1e236a1c613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module Utility.EventCache ( purgeOldEventCaches
                          , appendManualEventCache
                          , readEmacsEventCache
                          , readManualEventCache
                          ) where

import           Control.Applicative ((<$>))
import           Control.Exception   (IOException, catch)
import           Control.Monad       (filterM, forM_, when)
import qualified Control.SremConfig  as SremConfig
import           Data.List.Split     (splitOn, splitOneOf)
import           Data.Maybe.Read
import           Data.Time.Calendar
import           Data.Time.Clock
import           System.Directory    (doesFileExist, getDirectoryContents,
                                      removeFile)
import           System.FilePath     ((</>))
import           Types.Reminder
import           Utility.Emacs

-- TODO: lockfiles?

-- #### User interface functions

purgeOldEventCaches :: IO ()
purgeOldEventCaches = do
    files <- (SremConfig.getCacheDirectory
             >>= getDirectoryContents)
             `catch` ((\_ -> return []) :: IOException -> IO [FilePath])
    today <- utctDay <$> getCurrentTime
    forM_ files $ \file ->
        when (fileIsOldCache today file) $ removeFile file

appendManualEventCache   :: Reminder -> Day -> IO ()
appendManualEventCache r d = do
    dir <- SremConfig.getCacheDirectory
    let path = dir </> "manual_" ++ (showGregorian d) ++ ".csv"
    appendFile path $ makeEventsCSV [r]

readEmacsEventCache :: IO [Reminder]
readEmacsEventCache = do
    date <- todaysCacheFileDateString
    dir <- SremConfig.getCacheDirectory
    let path = dir </> "emacs_" ++ date ++ ".csv"
    doesFileExist path >>= \alreadyThere ->
        if   alreadyThere
        then readEventsCSV path
        else do
            rems <- parseEmacsOutput <$> getEmacsOutput
            writeFile path $ makeEventsCSV rems
            return rems

readManualEventCache :: IO [Reminder]
readManualEventCache = do
    date <- todaysCacheFileDateString
    dir <- SremConfig.getCacheDirectory
    let path = dir </> "manual_" ++ date ++ ".csv"
    doesFileExist path >>= \alreadyThere ->
        if   alreadyThere
        then readEventsCSV path
        else return []

-- #### Internal functions

readEventsCSV   :: FilePath -> IO [Reminder]
readEventsCSV f = readFile f >>= return . parseEventsCSV

parseEventsCSV :: String -> [Reminder]
parseEventsCSV = foldr step [] . lines
  where
    step r rs = maybe rs (: rs) $ parseEventCSV r

parseEventCSV      :: String -> Maybe Reminder
parseEventCSV line = do
    hourString:minuteString:text:[] <- return $ splitOn "," line
    hour <- readMaybe hourString
    minute <- readMaybe minuteString
    makeReminder' hour minute text

makeEventsCSV :: [Reminder] -> String
makeEventsCSV = unlines . foldr ((:) . makeEventCSV) []

makeEventCSV   :: Reminder -> String
makeEventCSV r = (show . getReminderHour $ r) ++ "," ++
                 (show . getReminderMinute $ r) ++ ","
                 ++ getReminderText r

todaysCacheFileDateString :: IO String
todaysCacheFileDateString = showGregorian . utctDay <$> getCurrentTime

fileIsOldCache            :: Day -> FilePath -> Bool
fileIsOldCache today file = length splitFile == 3
                            && splitFile !! 1 `elem` ["manual", "emacs"]
                            && (maybe False (< today) $ readDay $ splitFile !! 2)
                            && splitFile !! 3 == "csv"
  where
    splitFile             = splitOneOf "_." file

readDay   :: String -> Maybe Day
readDay s = do
    yearString:monthString:dayString:[] <- return $ splitOn "-" s
    year  <- readMaybe yearString
    month <- readMaybe monthString
    day   <- readMaybe dayString
    return $ fromGregorian year month day