aboutsummaryrefslogtreecommitdiff
path: root/State.hs
blob: 76f200eab6451887ba480318155aa1fb8d06eb54 (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
-- This module is part of Lazymail, a Haskell email client.
--
-- Copyright (C) 2013 Raúl Benencia <rul@kalgan.cc>
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.

--
-- | The top level application state, and operations on that value.
--
module State where

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

data Mode = MaildirMode | IndexMode | EmailMode

data MState = MState {
    selectedRowMD   :: Integer -- Selected row in MaildirMode
  , selectedRowIn   :: Integer -- Selected row in IndexMode
  , mode            :: Mode
  , initPath        :: String
  , scrRows         :: Integer
  , scrColumns      :: Integer
  , curRow          :: Integer
  , colPadding      :: Integer
  , selectedColorID :: ColorID
  , statusColorID   :: ColorID
  , selectedEmail   :: Message
  , selectedEmails  :: [(String, [Flag], String)]
  , selectedMD      :: String
  , detectedMDs     :: [String]
  , exitRequested   :: Bool
  , showStatus      :: Bool
}

initState = MState {
    selectedRowMD   = 0
  , selectedRowIn   = 0
  , mode            = MaildirMode
  , initPath        = ""
  , scrRows         = (-1)
  , scrColumns      = (-1)
  , curRow          = 0
  , colPadding      = 0             
  , selectedColorID = defaultColorID
  , statusColorID   = defaultColorID                      
  , selectedEmail   = Message [] "Dummy email"
  , selectedEmails  = []
  , selectedMD      = ""                    
  , detectedMDs     = []
  , exitRequested   = False
  , showStatus      = True
} 

incCurRow st = st { curRow = (curRow st) + 1 }

incSelectedRow st | (selectedRow st) < limit = case (mode st) of
                                                 MaildirMode -> st { selectedRowMD = (selectedRowMD st) + 1 }
                                                 IndexMode   -> st { selectedRowIn = (selectedRowIn st) + 1 }
                  | otherwise = st
  where
    limit' = case (mode st) of
      MaildirMode -> (length $ detectedMDs st ) - 1
      IndexMode   -> (length $ selectedEmails st) - 1
    limit = if (showStatus st) && (limit' == scrRowsAsInt st)
            then fromIntegral $ limit' - 2
            else fromIntegral limit'

decSelectedRow st | (selectedRow st) > 0 = case (mode st) of
                                             MaildirMode -> st { selectedRowMD = (selectedRowMD st) - 1 }
                                             IndexMode   -> st { selectedRowIn = (selectedRowIn st) - 1 }
                  | otherwise = st
                                         
selectedRow st = case (mode st) of
      MaildirMode -> selectedRowMD st
      IndexMode   -> selectedRowIn st

scrColsAsInt st = fromIntegral $ scrColumns st
scrRowsAsInt st = fromIntegral $ scrRows st
nihil fit ex nihilo