summaryrefslogtreecommitdiffstats
path: root/TreeView.hs
blob: 470913bd49181a11983947b117c83b1530144beb (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RecordWildCards #-}


module TreeView
    ( TreeView (..)
    , getMessage
    , getSearchTerm
    , isTVMessage
    , isTVSearchResult
    , fromSearchResults
    , fromMessageForest
    , fromMessageTree
    , treeViewId
    , loadSubForest
    , unloadSubForest
    , hasUnloadedSubForest
    ) where


import qualified Data.CaseInsensitive as CI
import qualified Data.Text as T
import Control.Applicative
import Data.Monoid
import Data.Tree
import Notmuch
import Notmuch.Message
import Notmuch.SearchResult


type LineNr = Int


data TreeView
    = TVMessage Message
    | TVMessageHeaderField Message (CI.CI T.Text)
    | TVMessagePart Message MessagePart
    | TVMessageQuoteLine Message MessagePart LineNr String
    | TVMessageLine Message MessagePart LineNr String
    | TVSearch String
    | TVSearchResult SearchResult
  deriving (Show)


instance Eq TreeView where
    x1 == x2 = treeViewId x1 == treeViewId x2


data TreeViewId
    = TVIDMessage T.Text
    | TVIDMessageHeaderField T.Text T.Text
    | TVIDMessagePart T.Text Int
    | TVIDMessageLine T.Text Int Int
    | TVIDSearch T.Text
    | TVIDSearchResult T.Text
  deriving (Eq,Show)


treeViewId :: TreeView -> TreeViewId
treeViewId = \case
    TVMessage m ->
        TVIDMessage (fromMessage m)

    TVMessageHeaderField m mhf ->
        TVIDMessageHeaderField (fromMessage m) (CI.foldedCase mhf)

    TVMessagePart m mp ->
        TVIDMessagePart (fromMessage m) (partID mp)

    TVMessageLine m mp lineNr _ ->
        TVIDMessageLine (fromMessage m) (partID mp) lineNr

    TVMessageQuoteLine m mp lineNr _ ->
        TVIDMessageLine (fromMessage m) (partID mp) lineNr

    TVSearch s ->
        TVIDSearch (T.pack s)

    TVSearchResult sr ->
        TVIDSearchResult (T.pack $ unThreadID $ searchThread sr)

  where
    fromMessage = T.pack . unMessageID . messageId


getMessage :: TreeView -> Maybe Message
getMessage = \case
    TVMessage m -> Just m
    TVMessageHeaderField m _ -> Just m
    TVMessagePart m _ -> Just m
    TVMessageQuoteLine m _ _ _ -> Just m
    TVMessageLine m _ _ _ -> Just m
    _ -> Nothing


getSearchTerm :: TreeView -> Maybe String
getSearchTerm = \case
    TVSearch term -> Just term
    _ -> Nothing


isTVMessage :: TreeView -> Bool
isTVMessage = \case
    TVMessage _ -> True
    _ -> False


isTVMessagePart :: TreeView -> Bool
isTVMessagePart = \case
    TVMessagePart _ _ -> True
    _ -> False


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 (TVMessage m)
         (xconvHead m <> xconvBody m <> 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 = map (xconvPart m) (messageBody m)


xconvPart :: Message -> MessagePart -> Tree TreeView
xconvPart m p =
    Node (TVMessagePart m p) contents
  where
    contents = case partContent p of
        ContentText t ->
            map (xconvLine m p) $ zip [0..] (T.lines t)
        ContentMultipart parts ->
            map (xconvPart m) parts
        ContentMsgRFC822 _ ->
            []


xconvLine
  :: Message -> MessagePart -> (LineNr, T.Text) -> Tree TreeView
xconvLine m p (i, s) =
    Node (ctor m p i $ T.unpack s) []
  where
    ctor =
        if isQuoteLine s
            then TVMessageQuoteLine
            else TVMessageLine


isQuoteLine :: T.Text -> Bool
isQuoteLine s0 = do
    let s = T.stripStart s0

    -- /^\s*>/
    not (T.null s) && T.head s == '>'


--
-- Loading / Unloading
--


loadSubForest :: TreeView -> IO (Either String (Forest TreeView))
loadSubForest = \case
    TVMessage m ->
        Right
            . unloadPartsWithFilename
            . map unloadReadSubForests
            . concatMap subForest
            . fromMessageForest
            . findFirsts messageMatch
            <$> notmuchShow (termFromMessage m)

    TVMessagePart m mp ->
        -- TODO parse --format=raw
        notmuchShowPart (termFromMessage m) (partID mp) >>= return . \case
            Left e -> Left $ show e
            Right mp' ->
                Right
                    . unloadPartsWithFilename
                    . subForest
                    $ xconvPart m mp'

    TVSearchResult sr -> do
        Right
            . unloadPartsWithFilename
            . map unloadReadSubForests
            . fromMessageForest
            <$> notmuchShow (termFromSearchResult sr)

    TVSearch s -> do
        Right
            . subForest
            . fromSearchResults s
            . either error id
            <$> Notmuch.search s

    _ ->
        return $ Right []

  where
    termFromMessage = unMessageID . messageId
    termFromSearchResult = unThreadID . searchThread


unloadSubForest :: Tree TreeView -> Forest TreeView
unloadSubForest t = case rootLabel t of
    TVMessage _ ->
        filter (isTVMessage . rootLabel) $ subForest t
    TVMessagePart _ _ ->
        filter (isTVMessagePart . rootLabel) $ subForest t
    _ ->
        []


hasUnloadedSubForest :: Tree TreeView -> Bool
hasUnloadedSubForest t = case rootLabel t of
    TVMessage _ ->
        null $ filter (not . isTVMessage . rootLabel) $ subForest t
    TVMessagePart _ _ ->
        null $ filter (not . isTVMessagePart . rootLabel) $ subForest t
    _ ->
        null $ subForest t


unloadReadSubForests :: Tree TreeView -> Tree TreeView
unloadReadSubForests t = case rootLabel t of
    TVMessage m | "unread" `notElem` messageTags m ->
        t { subForest =
                map unloadReadSubForests $
                filter (isTVMessage . rootLabel) $
                subForest t
          }
    _ ->
        t { subForest =
                map unloadReadSubForests $
                subForest t
          }


unloadPartsWithFilename :: Forest TreeView -> Forest TreeView
unloadPartsWithFilename =
    map (rewriteTree f)
  where
    f x@Node{..} = case rootLabel of
      TVMessagePart _ mp ->
          case partContentFilename mp of
              Nothing -> x
              Just _ ->
                  x { subForest = [] }
      _ -> x

    rewriteTree :: (Tree a -> Tree a) -> Tree a -> Tree a
    rewriteTree f x =
        let x' = f x
        in x' { subForest = map (rewriteTree f) $ subForest x' }


findFirsts :: (a -> Bool) -> Forest a -> Forest a
findFirsts p =
    concatMap rec
  where
    rec t@Node{..} =
      if p rootLabel
          then [t]
          else concatMap rec subForest