aboutsummaryrefslogtreecommitdiff
path: root/qml/Message.qml
blob: c7daf071925aadbfefb9ec77361c72a78c470683 (plain) (blame)
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
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQml.Models 2.1
import QtQuick.Dialogs 1.1
import QtMultimedia 5.8
import org.kde.kirigami 2.12 as Kirigami

import DeltaChat 1.0

RowLayout {
    id: messageObject

    property DcMessage message
    property DcContext context

    readonly property DcContact from: context.getContact(message.fromId)
    readonly property DcMessage quoteMessage: message.quotedMessage
    readonly property DcContact quoteFrom: quoteMessage ? context.getContact(quoteMessage.fromId) : null

    layoutDirection: message.fromId == 1 ? Qt.RightToLeft : Qt.LeftToRight

    readonly property string overrideName: message.getOverrideSenderName()
    readonly property string displayName: overrideName != "" ? ("~" + overrideName)
                                                             : messageObject.message.fromId > 0 ? messageObject.from.displayName
                                                                                                : ""

    Component.onCompleted: {
        // Only try to mark fresh and noticed messages as seen to
        // avoid unnecessary database calls when viewing an already read chat.
        if ([10, 13].includes(messageObject.message.state)) {
            // Do not mark DC_CHAT_ID_DEADDROP messages as seen to
            // avoid contact request chat disappearing from chatlist.
            if (messageObject.chatId != 1) {
                messageObject.context.markseenMsgs([messageObject.message.id])
            }
        }
    }

    Rectangle {
        Layout.leftMargin: Kirigami.Units.largeSpacing
        Layout.preferredWidth: messageContents.width
        Layout.preferredHeight: messageContents.height
        color: Kirigami.Theme.backgroundColor
        radius: 5

        Component {
            id: imageMessageView

            ColumnLayout {
                Image {
                    source: "file:" + messageObject.message.file
                    sourceSize.width: messageObject.message.width
                    sourceSize.height: messageObject.message.height
                    fillMode: Image.PreserveAspectCrop
                    Layout.preferredWidth: messageObject.width
                    Layout.maximumWidth: Kirigami.Units.gridUnit * 30
                    Layout.maximumHeight: Kirigami.Units.gridUnit * 20
                    asynchronous: true
                }
                Label {
                    font.bold: true
                    color: messageObject.message.fromId > 0 ? messageObject.from.color : "black"
                    text: messageObject.displayName
                    textFormat: Text.PlainText
                }
            }
        }

        Component {
            id: audioMessageView

            ColumnLayout {
                MediaPlayer {
                    id: player
                    source: Qt.resolvedUrl("file:" + messageObject.message.file)
                    onError: console.log("Audio MediaPlayer error: " + errorString)
                }
                Label {
                    font.bold: true
                    text: "Audio"
                    textFormat: Text.PlainText
                }
                Button {
                    text: "play"
                    onPressed: player.play()
                }
            }
        }

        Component {
            id: videoMessageView

            ColumnLayout {
                MediaPlayer {
                    id: videoplayer
                    source: Qt.resolvedUrl("file:" + messageObject.message.file)
                    onError: console.log("Video MediaPlayer error: " + errorString)
                }
                VideoOutput {
                    source: videoplayer
                }
                Label {
                    font.bold: true
                    text: "Video"
                    textFormat: Text.PlainText
                }
                Button {
                    text: "play"
                    onPressed: videoplayer.play()
                }
            }
        }

        Component {
            id: textMessageView

            Label {
                font.bold: true
                color: messageObject.message.fromId > 0 ? messageObject.from.color : "black"
                text: messageObject.displayName
                textFormat: Text.PlainText
            }
        }

        MouseArea {
            anchors.fill: parent

            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onClicked: function(mouse) {
                if (mouse.button === Qt.RightButton)
                    contextMenu.popup()
            }
            onPressAndHold: function(mouse) {
                if (mouse.source === Qt.MouseEventNotSynthesized)
                    contextMenu.popup()
            }

            MessageDialog {
                id: messageDialog
                title: "Message info"
                text: messageObject.context.getMessageInfo(messageObject.message.id)
                onAccepted: { }
            }

            Menu {
                id: contextMenu
                Action {
                    text: "Info"
                    onTriggered: messageDialog.open()
                }
            }
        }

        ColumnLayout {
            id: messageContents

            Loader {
                sourceComponent: [20, 21, 23].includes(messageObject.message.viewtype) ? imageMessageView
                : [40, 41].includes(messageObject.message.viewtype) ? audioMessageView
                : [50].includes(messageObject.message.viewtype) ? videoMessageView
                : textMessageView
            }

            // Quote
            RowLayout {
                Layout.leftMargin: Kirigami.Units.smallSpacing
                visible: messageObject.message.quotedText
                implicitHeight: quoteTextEdit.height
                spacing: Kirigami.Units.smallSpacing
                Rectangle {
                    width: Kirigami.Units.smallSpacing
                    color: messageObject.quoteFrom ? messageObject.quoteFrom.color : "black"
                    Layout.fillHeight: true
                }
                TextEdit {
                    id: quoteTextEdit
                    Layout.maximumWidth: Kirigami.Units.gridUnit * 30
                    text: messageObject.message.quotedText ? messageObject.message.quotedText : ""
                    textFormat: Text.PlainText
                    selectByMouse: true
                    readOnly: true
                    color: "grey"
                    wrapMode: Text.Wrap
                    font.pixelSize: 14
                }
            }

            // Message
            TextEdit {
                Layout.maximumWidth: Math.min(messageObject.width, Kirigami.Units.gridUnit * 30)
                textFormat: Text.PlainText
                selectByMouse: true
                readOnly: true
                color: "black"
                wrapMode: Text.Wrap
                font.pixelSize: 14

                Component.onCompleted: {
                    text = messageObject.message.text
                }
            }
            Row {
                HtmlViewSheet {
                    id: htmlSheet
                    subject: ""
                    html: ""
                }

                Button {
                    text: "Show full message"
                    visible: messageObject.message.hasHtml
                    onPressed: {
                        htmlSheet.subject = messageObject.message.subject
                        htmlSheet.html = messageObject.context.getMessageHtml(messageObject.message.id)
                        htmlSheet.open()
                    }
                }
                Label {
                    Layout.fillWidth: true
                    text: Qt.formatDateTime(messageObject.message.timestamp, "dd. MMM yyyy, hh:mm")
                        + (messageObject.message.state == 26 ? "✓"
                        : messageObject.message.state == 28 ? "✓✓"
                        : messageObject.message.state == 24 ? "✗"
                        : "");
                }
            }
        }
    }
}