aboutsummaryrefslogtreecommitdiff
path: root/qml/ChatlistPage.qml
blob: 7a9877b93df6a5ddb9b9ac791a7fa70ee1b69a4a (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
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
import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import QtQml.Models 2.1
import org.kde.kirigami 2.13 as Kirigami

import DeltaChat 1.0

Kirigami.ScrollablePage {
    title: qsTr("Chats")
    id: chatlistPage

    required property DcContext context
    required property DcAccountsEventEmitter eventEmitter

    property bool archivedOnly: false

    Connections {
        target: chatlistPage.eventEmitter

        function onMessagesChanged() {
            // Reload chatlist
            updateChatlist();
        }
        function onMessagesNoticed() {
            // Reload chatlist
            updateChatlist();
        }
        function onChatModified() {
            // Reload chatlist
            updateChatlist();
        }
    }

    Component.onCompleted: {
        updateChatlist()
    }

    mainAction: Kirigami.Action {
        text: "New chat"
        iconName: "list-add"
        onTriggered: {
            let newChatPageComponent = Qt.createComponent("qrc:/qml/NewChatPage.qml")
            if (newChatPageComponent.status == Component.Ready) {
                let newChatPage = newChatPageComponent.createObject(pageStack, {context: chatlistPage.context})
                pageStack.layers.push(newChatPage)
            } else if (newChatPageComponent.status == Component.Error) {
                console.log("Error loading new chat page: " + newChatPageComponent.errorString())
            }
        }
    }

    contextualActions: [
        Kirigami.Action {
            text: "Settings"
            iconName: "configure"
            onTriggered: {
                let settingsPageComponent = Qt.createComponent("qrc:/qml/SettingsPage.qml")
                if (settingsPageComponent.status == Component.Ready) {
                    let settingsPage = settingsPageComponent.createObject(pageStack, {context: chatlistPage.context})
                    pageStack.layers.push(settingsPage)
                } else {
                    console.log("Can't open Settings page")
                }
            }
        }
    ]

    ListModel {
        id: chatlistModel
    }

    function loadChat(chatId) {
        chatlistPage.context.marknoticedChat(chatId)

        console.log("Selected chat " + chatId)

        console.log("Depth is " + pageStack.depth)
        let chatPageComponent = Qt.createComponent("qrc:/qml/ChatPage.qml")
        if (chatPageComponent.status == Component.Ready) {
            let myPage = chatPageComponent.createObject(pageStack, {chatId: chatId, context: chatlistPage.context, eventEmitter: chatlistPage.eventEmitter})
            if (pageStack.depth == 1) {
                pageStack.push(myPage)
            } else if (pageStack.depth == 2) {
                pageStack.currentIndex = 1
                pageStack.replace(myPage)
            }
        } else if (chatPageComponent.status == Component.Error) {
            console.log("Error loading chat page: " + chatPageComponent.errorString())
        }
    }

    function updateChatlist() {
        let chatlist = chatlistPage.context.getChatlist(chatlistPage.archivedOnly ? 1 : 0)

        // Merge new chatlist with existing one.
        // To preserve selected item, we do not simply clear and fill
        // the model from scratch.

        for (let i = 0; i < chatlist.getChatCount(); i++) {
            const summary = chatlist.getSummary(i)
            const chatId = chatlist.getChatId(i)
            const chat = chatlistPage.context.getChat(chatId)
            const profileImage = chat.getProfileImage()

            const item = {
                chatId: chatId,
                msgId: chatlist.getMsgId(i),
                username: (summary.text1 != "" ? summary.text1 + ": " : "") + summary.text2,
                avatarSource: profileImage ? "file:" + profileImage : "",
                chatName: chat.name,
                freshMsgCnt: chatlistPage.context.getFreshMsgCnt(chatId),
                isContactRequest: chat.isContactRequest,
                visibility: chat.visibility
            }

            let j;
            for (j = i; j < chatlistModel.count; j++) {
                if (chatlistModel.get(j).chatId == chatId) {
                    // This chat was already in the chatlist,
                    // move it to the new place and update.
                    chatlistModel.move(j, i, 1)
                    chatlistModel.set(i, item)
                    break
                }
            }

            // This chat is new, insert it.
            if (j == chatlistModel.count) {
                chatlistModel.insert(i, item)
            }
        }

        // Remove any chats that are not present in the new chatlist.
        if (chatlistModel.count > chatlist.getChatCount()) {
            chatlistModel.remove(chatlist.getChatCount(),
                                 chatlistModel.count - chatlist.getChatCount())
        }
    }

    ListView {
        id: chatlist

        anchors.fill: parent
        model: chatlistModel
        currentIndex: -1

        onCurrentItemChanged: {
            console.log("Current index is " + chatlist.currentIndex)
            if (currentIndex == -1) {
                return;
            }

            var chatId = chatlistModel.get(chatlist.currentIndex).chatId

            if (chatId > 9) {
                // chatId > DC_CHAT_ID_LAST_SPECIAL
                loadChat(chatId)

            } else if (chatId == 6) {
                chatlistPage.archivedOnly = true
                chatlist.currentIndex = -1
                updateChatlist();
            }
        }

        delegate: Kirigami.AbstractListItem {
            width: chatlist.width

            RowLayout {
                Kirigami.Avatar {
                    source: model.avatarSource
                    name: model.chatName
                    color: chatlistPage.context.getChat(model.chatId).getColor()
                    MouseArea {
                        anchors.fill: parent
                        acceptedButtons: Qt.RightButton
                        onClicked: {
                            if (mouse.button === Qt.RightButton)
                                contextMenu.popup()
                        }

                        Menu {
                            id: contextMenu

                            Action {
                                icon.name: "pin"
                                text: "Pin chat"
                                onTriggered: chatlistPage.context.setChatVisibility(model.chatId, 2)
                            }
                            Action {
                                text: "Unpin chat"
                                onTriggered: chatlistPage.context.setChatVisibility(model.chatId, 0)
                            }
                            Action {
                                text: "Archive chat"
                                onTriggered: chatlistPage.context.setChatVisibility(model.chatId, 1)
                            }
                            Action {
                                text: "Unarchive chat"
                                onTriggered: chatlistPage.context.setChatVisibility(model.chatId, 0)
                            }
                            Action {
                                icon.name: "delete"
                                text: "Delete chat"
                                onTriggered: chatlistPage.context.deleteChat(model.chatId)
                            }
                        }
                    }
                }

                ColumnLayout {
                    Layout.fillWidth: true

                    Label {
                        text: chatlistPage.context.getChat(model.chatId).getName()
                        font.weight: Font.Bold
                        Layout.fillWidth: true
                    }
                    Label {
                        text: model.username
                        font: Kirigami.Theme.smallFont
                        Layout.fillWidth: true
                    }
                }


                Label {
                    text: model.isContactRequest ? "NEW" : model.freshMsgCnt
                    visible: model.freshMsgCnt > 0 || model.isContactRequest

                    // Align label in the center of a badge.
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter

                    // Make sure badge is not too narrow.
                    Layout.minimumWidth: height

                    background: Rectangle {
                        color: Kirigami.Theme.alternateBackgroundColor
                        radius: 0.25 * height
                    }
                }

                // "Pinned" badge
                Rectangle {
                    visible: model.visibility == 2
                    color: Kirigami.Theme.alternateBackgroundColor
                    width: Kirigami.Units.gridUnit
                    height: Kirigami.Units.gridUnit
                    radius: 0.25 * height
                    Kirigami.Icon {
                        source: "pin"
                        height: Kirigami.Units.gridUnit
                        width: Kirigami.Units.gridUnit
                    }
                }
            }
        }
    }
}