aboutsummaryrefslogtreecommitdiff
path: root/State.hs
blob: 57b62582893854278720617c460777eba0cc9488 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
{- Lazymail state, and operations on it.
 -
 - Copyright 2013 Raúl Benencia <rul@kalgan.cc>
 -
 - Licensed under the GNU GPL version 3 or higher
 -
 -}

module State where

import Text.ParserCombinators.Parsec.Rfc2822(Message, GenericMessage(..))
import UI.NCurses(ColorID(..), defaultColorID)
import Network.Email.Mailbox(Flag(..), Flags)
import System.FilePath

data Mode = MaildirMode | IndexMode | EmailMode | ComposeMode

data LazymailState = LazymailState {
    mode            :: Mode
  , basePath        :: FilePath
  , screenRows      :: Int
  , screenColumns   :: Int
  , currentRow      :: Int
  , columnPadding   :: Int
  , exitRequested   :: Bool
  , statusBar       :: Bool
  , maildirState    :: MaildirState
  , indexState      :: IndexState
  , composeState    :: ComposeState
  , colorStyle      :: ColorStyle
}

data MaildirState = MaildirState {
    selectedRowMD   :: Int
  , selectedMD      :: String
  , detectedMDs     :: [String]
}

data IndexState = IndexState {
    selectedRowIn   :: Int
  , selectedEmail   :: Message
  , selectedEmails  :: [(String, [Flag], String)]
}

data ComposeState = ComposeState {
    composition     :: Maybe String
}

data ColorStyle = ColorStyle {
    baseColorID      :: ColorID
  , selectionColorID :: ColorID
  , statusBarColorID :: ColorID
}

initialState = LazymailState {
    mode          = MaildirMode
  , basePath      = ""
  , screenRows    = 0
  , screenColumns = 0
  , currentRow    = 0
  , columnPadding = 0
  , exitRequested = False
  , statusBar     = True
  , maildirState  = initialMaildirState
  , indexState    = initialIndexState
  , composeState  = initialComposeState
  , colorStyle    = initialColorStyle
}

initialMaildirState = MaildirState {
    selectedRowMD = 0
  , selectedMD    = ""
  , detectedMDs   = []
}

initialIndexState = IndexState {
    selectedRowIn  = 0
  , selectedEmail  = Message [] "Dummy email"
  , selectedEmails = []
}

initialComposeState = ComposeState {
    composition = Nothing
}

initialColorStyle = ColorStyle {
    baseColorID      = defaultColorID
  , selectionColorID = defaultColorID
  , statusBarColorID = defaultColorID
}

scrColsAsInteger st = toInteger $ screenColumns st
scrRowsAsInteger st = toInteger $ screenRows st
curRowAsInteger  st = toInteger $ currentRow st
colPadAsInteger  st = toInteger $ columnPadding st


incrementSelectedRow st | (selectedRow st) < limit = case (mode st) of
                                               MaildirMode ->
                                                 let
                                                   sr = (selectedRowMD . maildirState) st
                                                   maildirState' = (maildirState st) { selectedRowMD = sr + 1 }
                                                 in
                                                  st { maildirState = maildirState' }
                                               IndexMode ->
                                                 let
                                                   sr = (selectedRowIn . indexState) st
                                                   indexState' = (indexState st) { selectedRowIn = sr + 1 }
                                                 in
                                                  st { indexState = indexState' }
                                               _ -> st
                        | otherwise = st
  where
    limit' = case (mode st) of
      MaildirMode -> (length $ detectedMDs . maildirState $ st ) - 1
      IndexMode   -> (length $ selectedEmails . indexState $ st) - 1
    limit = if (statusBar st) && (limit' == screenRows st)
            then fromIntegral $ limit' - 2
            else fromIntegral limit'

decrementSelectedRow st | (selectedRow st) > 0 = case (mode st) of
                                             MaildirMode ->
                                               let
                                                 sr = (selectedRowMD . maildirState) st
                                                 maildirState' = (maildirState st) { selectedRowMD = sr - 1 }
                                               in
                                                st { maildirState = maildirState' }
                                             IndexMode ->
                                               let
                                                 sr = (selectedRowIn . indexState) st
                                                 indexState' = (indexState st) { selectedRowIn = sr - 1 }
                                               in
                                                st { indexState = indexState' }
                                             _ -> st
                        | otherwise = st

selectedRow st = case (mode st) of
      MaildirMode -> selectedRowMD . maildirState $ st
      IndexMode   -> selectedRowIn . indexState   $ st
nihil fit ex nihilo