blob: 4edf9b351ceccb327a865602f23ac49f6a48fc5b (
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
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
module TreeView
( TreeView (..)
, getMessage
, isTVSearchResult
, fromSearchResults
, fromMessageForest
, fromMessageTree
) where
import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import Data.Monoid
import Data.Tree
import Notmuch.Message
import Notmuch.SearchResult
type LineNr = Int
data TreeView
= TVMessage Message
| TVMessageHeaderField Message (CI.CI T.Text)
| TVMessagePart Message MessagePart
| TVMessageLine Message MessagePart LineNr String
| TVSearch String
| TVSearchResult SearchResult
deriving (Show)
instance Eq TreeView where
TVMessage m1 == TVMessage m2 =
m1 == m2
TVMessageHeaderField m1 mhf1 == TVMessageHeaderField m2 mhf2 =
m1 == m2 && mhf1 == mhf2
TVMessagePart m1 mp1 == TVMessagePart m2 mp2 =
m1 == m2 && mp1 == mp2
TVMessageLine m1 mp1 ln1 _s1 == TVMessageLine m2 mp2 ln2 _s2 =
m1 == m2 && mp1 == mp2 && ln1 == ln2
TVSearch s1 == TVSearch s2 =
s1 == s2
TVSearchResult s1 == TVSearchResult s2 =
s1 == s2
_ == _ = False
getMessage :: TreeView -> Maybe Message
getMessage = \case
TVMessage m -> Just m
TVMessageHeaderField m _ -> Just m
TVMessagePart m _ -> Just m
TVMessageLine m _ _ _ -> Just m
_ -> Nothing
isTVSearchResult :: TreeView -> Bool
isTVSearchResult (TVSearchResult _) = True
isTVSearchResult _ = False
fromSearchResults :: String -> [SearchResult] -> Tree TreeView
fromSearchResults query =
Node (TVSearch query) . map (\r -> Node (TVSearchResult r) [])
fromMessageForest :: Forest Message -> Forest TreeView
fromMessageForest = map fromMessageTree
fromMessageTree :: Tree Message -> Tree TreeView
fromMessageTree (Node m ms) =
Node m' ms'
where
m' :: TreeView
m' = TVMessage m
ms' :: Forest TreeView
ms' = if isOpen m
then xconvHead m <> xconvBody m <> map fromMessageTree ms
else map fromMessageTree ms
xconvHead :: Message -> Forest TreeView
xconvHead m =
map conv [ "From", "To" ]
-- TODO add Subject if it differs from thread subject
where
conv k =
Node (TVMessageHeaderField m k) []
xconvBody :: Message -> Forest TreeView
xconvBody m = mconcat $ map (xconvPart m) (messageBody m)
xconvPart :: Message -> MessagePart -> Forest TreeView
xconvPart m p = xconvPartContent m p $ partContent p
xconvPartContent
:: Message -> MessagePart -> MessageContent -> Forest TreeView
xconvPartContent m p = \case
ContentText t ->
map (xconvLine m p) $ zip [0..] (T.lines t)
ContentMultipart parts ->
map (xconvPart2 m) parts
-- [Node (TVMessageLine m p 0 "ContentMultipart") []]
ContentMsgRFC822 _ ->
[Node (TVMessageLine m p 0 "ContentMsgRFC822") []]
xconvPart2 :: Message -> MessagePart -> Tree TreeView
xconvPart2 m p =
Node (TVMessagePart m p) $ xconvPartContent m p (partContent p)
xconvLine
:: Message -> MessagePart -> (LineNr, T.Text) -> Tree TreeView
xconvLine m p (i, s) =
Node (TVMessageLine m p i $ T.unpack s) []
|