File: DetailsView.hs

package info (click to toggle)
bustle 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 720 kB
  • sloc: haskell: 3,938; ansic: 939; makefile: 110; sh: 8
file content (150 lines) | stat: -rw-r--r-- 5,351 bytes parent folder | download
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
{-
Bustle.UI.DetailsView: displays the bodies of D-Bus messages
Copyright © 2011–2012 Collabora Ltd.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-}
module Bustle.UI.DetailsView
  ( DetailsView
  , detailsViewNew
  , detailsViewGetTop
  , detailsViewUpdate
  )
where

import Data.List (intercalate)
import Graphics.UI.Gtk hiding (Signal)

import qualified DBus as D
import DBus.Internal.Message (MethodError(..))
import DBus.Internal.Types (ErrorName(..))

import Bustle.Types
import Bustle.Marquee
import Bustle.VariantFormatter

type OptionalRow = (Label, Label)

data DetailsView =
    DetailsView { detailsGrid :: Grid
                , detailsType :: Stack
                , detailsSender :: Label
                , detailsDestination :: OptionalRow
                , detailsPath :: Label
                , detailsMember :: Label
                , detailsErrorName :: OptionalRow
                , detailsBodyView :: TextView
                }

detailsViewNew :: Builder
               -> IO DetailsView
detailsViewNew builder = DetailsView
    <$> builderGetObject builder castToGrid "detailsGrid"
    <*> builderGetObject builder castToStack "detailsType"
    <*> builderGetObject builder castToLabel "detailsSender"
    <*> optionalRow "detailsDestination"
    <*> builderGetObject builder castToLabel "detailsPath"
    <*> builderGetObject builder castToLabel "detailsMember"
    <*> optionalRow "detailsErrorName"
    <*> builderGetObject builder castToTextView "detailsArguments"
  where
    optionalRow labelId = (,)
        <$> builderGetObject builder castToLabel (labelId ++ "Caption")
        <*> builderGetObject builder castToLabel labelId


pickType :: Detailed Message -> String
pickType (Detailed _ m _ _) = case m of
    MethodCall {} -> "methodCall"
    MethodReturn {} -> "methodReturn"
    Error {} -> "error"
    Signal { signalDestination = d } ->
        maybe "signal" (const "directedSignal") d

getMemberMarkup :: Member -> String
getMemberMarkup m =
    toPangoMarkup $ formatMember (iface m) (membername m)

getMember :: Detailed Message -> Maybe Member
getMember (Detailed _ m _ _) = case m of
    MethodCall {}   -> Just $ member m
    Signal {}       -> Just $ member m
    MethodReturn {} -> callMember
    Error {}        -> callMember
  where
    callMember = member . deEvent <$> inReplyTo m

getDestination :: Detailed Message -> Maybe TaggedBusName
getDestination (Detailed _ m _ _) = case m of
    Signal { signalDestination = d } -> d
    _                                -> Just (destination m)

getErrorName :: Detailed a -> Maybe String
getErrorName (Detailed _ _ _ rm) = case rm of
    (D.ReceivedMethodError _ (MethodError { methodErrorName = ErrorName en})) -> Just en
    _                                                                         -> Nothing

getErrorMessage :: Detailed a
                -> Maybe String
getErrorMessage (Detailed _ _ _ (D.ReceivedMethodError _ (MethodError _ _ _ _ body))) =
    case D.fromVariant <$> body of
        [message] -> message
        _         -> Nothing
getErrorMessage _ = Nothing

formatMessage :: Detailed Message -> String
formatMessage (Detailed _ _ _ rm) =
    formatArgs $ D.receivedMessageBody rm
  where
    formatArgs = intercalate "\n" . map (format_Variant VariantStyleSignature)
-- TODO: suppress escaping and type sig for errors, which are always (s)

detailsViewGetTop :: DetailsView -> Widget
detailsViewGetTop = toWidget . detailsGrid

setOptionalRow :: OptionalRow
               -> Maybe String
               -> IO ()
setOptionalRow (caption, label) s_ = do
    case s_ of
        Just s -> do
            labelSetText label s
            widgetShow label
            widgetShow caption
        Nothing -> do
            widgetHide label
            widgetHide caption

detailsViewUpdate :: DetailsView
                  -> Detailed Message
                  -> IO ()
detailsViewUpdate d m = do
    buf <- textViewGetBuffer $ detailsBodyView d
    let member_ = getMember m
    stackSetVisibleChildName (detailsType d) (pickType m)

    -- TODO: these would be a lot more useful if we could resolve unique names
    -- to/from well-known names and show both
    labelSetText (detailsSender d) (unBusName . sender . deEvent $ m)
    setOptionalRow (detailsDestination d) (unBusName <$> getDestination m)
    setOptionalRow (detailsErrorName d) (getErrorName m)

    labelSetText (detailsPath d) (maybe unknown (D.formatObjectPath . path) member_)
    labelSetMarkup (detailsMember d) (maybe unknown getMemberMarkup member_)
    textBufferSetText buf $ case getErrorMessage m of
        Just message -> message
        Nothing      -> formatMessage m
  where
    unknown = ""