PageMessageList.qml 9.53 KB
Newer Older
1
/*
2
 * Copyright (C) 2014-2019 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22 23
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations including
 * the two.
 */

24
import QtQuick 2.7
25
import QtQuick.Controls 2.1
26
import cz.nic.mobileDatovka 1.0
27
import cz.nic.mobileDatovka.messages 1.0
28
import cz.nic.mobileDatovka.models 1.0
29

30 31 32 33 34
/*
 * Roles are defined in MessageListModel::roleNames() and are accessed directly
 * via their names.
 */

35
Component {
36
    id: pageMessageList
37 38
    Item {
        id: msgListPage
39 40

        /* These properties must be set by caller. */
41 42
        property var pageView
        property var statusBar
43 44 45
        property string acntName
        property string userName
        property int msgType
46
        property var accountModel: null
47

48
        Component.onCompleted: {
49
            messages.fillMessageList(messageModel, userName, msgType)
Martin Straka's avatar
Martin Straka committed
50
            if (messageModel.rowCount() === 0) {
51
                emptyList.visible = true
52
                /* Don't use visible property as it hides the element. */
53
                menuButton.visible = false
Martin Straka's avatar
Martin Straka committed
54
                searchButton.visible = false
55
            }
56

57
            proxyMessageModel.setSourceModel(messageModel)
58 59 60

            /* Don't use message settings for sent messages */
            if (msgType == MessageType.TYPE_SENT) {
61
                menuButton.visible = false
62
            }
63 64
        }

65 66 67 68 69
        Component.onDestruction: {
            accounts.updateNewMessageCounter(accountModel, userName)
            statusBar.visible = false
        }

70 71 72 73 74
        MessageListModel {
            id: messageModel

            Component.onCompleted: {
            }
75
        }
76 77 78 79 80

        ListSortFilterProxyModel {
            id: proxyMessageModel

            Component.onCompleted: {
81 82 83
                setFilterRoles([MessageListModel.ROLE_MSG_ID,
                    MessageListModel.ROLE_FROM, MessageListModel.ROLE_TO,
                    MessageListModel.ROLE_ANNOTATION])
84 85
            }
        }
86
        PageHeader {
87
            id: headerBar
Martin Straka's avatar
Martin Straka committed
88
            title: (msgType == MessageType.TYPE_RECEIVED) ? qsTr("Received messages") : qsTr("Sent messages");
89 90
            onBackClicked: {
                pageView.pop()
91
            }
92
            Row {
93
                anchors.verticalCenter: parent.verticalCenter
94
                spacing: defaultMargin
95 96
                anchors.right: parent.right
                anchors.rightMargin: defaultMargin
97
                AccessibleImageButton {
98 99
                    id: searchButton
                    anchors.verticalCenter: parent.verticalCenter
Martin Straka's avatar
Martin Straka committed
100
                    sourceSize.height: imgHeightHeader
101
                    source: "qrc:/ui/magnify.svg"
102 103 104 105
                    accessibleName: qsTr("Filter messages.")
                    onClicked: {
                        messageList.anchors.top = filterBar.bottom
                        filterBar.visible = true
106
                        filterBar.filterField.forceActiveFocus()
107
                        Qt.inputMethod.show()
108 109
                    }
                }
110 111
                AccessibleImageButton {
                    id: menuButton
112
                    anchors.verticalCenter: parent.verticalCenter
Martin Straka's avatar
Martin Straka committed
113
                    sourceSize.height: imgHeightHeader
114
                    source: "qrc:/ui/menu.svg"
115 116 117 118 119 120 121 122 123 124 125 126
                    accessibleName: qsTr("Show menu of available operations.")
                    onClicked: {
                        statusBar.visible = false
                        Qt.inputMethod.hide()
                        pageView.push(pageMenuMessageList, {
                                "pageView": pageView,
                                "statusBar": statusBar,
                                "acntName": acntName,
                                "userName": userName,
                                "msgType": msgType,
                                "messageModel": messageModel
                            }, StackView.Immediate)
127
                    }
128
                }
129
            }
130
        }
131
        FilterBar {
Martin Straka's avatar
Martin Straka committed
132
            id: filterBar
133 134 135
            visible: false
            z: 1
            anchors.top: headerBar.bottom
Martin Straka's avatar
Martin Straka committed
136
            width: parent.width
137 138
            height: filterField.height
            color: (filterField.text.length == 0) ? datovkaPalette.alternateBase :
Martin Straka's avatar
Martin Straka committed
139
                    (messageList.count > 0) ? "#afffaf" : "#ffafaf"
140 141 142 143 144 145 146 147 148
            border.color: filterField.activeFocus ? "#0066ff" : "#bdbebf"

            placeholderText: qsTr("Set filter")
            fontPointSize: defaultTextFont.font.pointSize
            buttonImageHeight: imgHeight
            buttonAccessibleName: qsTr("Clear and hide filter field")

            onTextChanged: {
                proxyMessageModel.setFilterRegExpStr(text)
149
            }
150 151 152 153 154

            onClearClicked: {
                filterBar.visible = false
                messageList.anchors.top = headerBar.bottom
                Qt.inputMethod.hide()
155
            }
156
        }
157
        AccessibleText {
158 159 160 161
            id: emptyList
            visible: false
            color: datovkaPalette.text
            anchors.centerIn: parent
162 163 164
            width: parent.width
            horizontalAlignment: Text.AlignHCenter
            wrapMode: Text.Wrap
165
            text: (filterBar.filterField.text.length == 0) ?
Martin Straka's avatar
Martin Straka committed
166
                qsTr("No messages or have not been downloaded yet.") :
167
                qsTr("No message found that matches filter text '%1'.").arg(filterBar.filterField.text)
168
        }
169
        MessageList {
170
            id: messageList
171
            anchors.top: headerBar.bottom
172 173 174 175 176 177 178
            anchors.bottom: parent.bottom
            clip: true
            spacing: 1
            opacity: 1
            visible: true
            width: parent.width
            interactive: true
179
            model: proxyMessageModel
180

181 182 183
            onCountChanged: {
                if (messageList.count == 0) {
                    emptyList.visible = true
184
                    menuButton.enabled = false
185 186
                } else {
                    emptyList.visible = false
187
                    menuButton.enabled = true
188 189
                }
            }
190 191
            onMsgClicked: {
                statusBar.visible = false
Martin Straka's avatar
Martin Straka committed
192
                Qt.inputMethod.hide()
193 194 195 196 197 198 199
                pageView.push(pageMessageDetail, {
                        "pageView": pageView,
                        "statusBar": statusBar,
                        "fromLocalDb": true,
                        "acntName": acntName,
                        "userName": userName,
                        "msgType": msgType,
200 201
                        "msgId": msgId,
                        "messageModel": messageModel
202
                    })
203 204 205
            }
            onMsgPressAndHold: {
                statusBar.visible = false
Martin Straka's avatar
Martin Straka committed
206
                Qt.inputMethod.hide()
207 208 209 210 211 212 213 214 215 216 217 218 219 220
                /* Use message settings for received message only */
                if (msgType == MessageType.TYPE_RECEIVED) {
                    pageView.push(pageMenuMessage, {
                            "pageView": pageView,
                            "statusBar": statusBar,
                            "acntName": acntName,
                            "userName": userName,
                            "msgType": msgType,
                            "msgId": msgId,
                            "canDeleteMsg": canDelete,
                            "accountModel": accountModel,
                            "messageModel": messageModel
                        }, StackView.Immediate)
                }
221
            }
222 223 224 225 226

            property bool downloadStart: false
            onMovementEnded: {
                if (downloadStart) {
                    downloadStart = false
227
                    if (msgType == MessageType.TYPE_RECEIVED) {
228
                        isds.doIsdsAction("syncSingleAccountReceived", userName)
229
                    } else if (msgType == MessageType.TYPE_SENT) {
230
                        isds.doIsdsAction("syncSingleAccountSent", userName)
231 232 233 234 235 236
                    }
                }
            }
            onDragEnded: {
                downloadStart = contentY < -120
            }
237 238 239 240 241 242 243 244 245 246
            Connections {
                target: isds
                onDownloadMessageListFinishedSig: {
                    if (isMsgReceived) {
                        settings.setLastUpdateToNow()
                        settings.saveAllSettings(accountModel)
                    }
                    messages.fillMessageList(messageModel, userName, msgType)
                }
            }
247 248 249 250 251 252 253 254 255 256 257 258
            Connections {
                target: isds
                onRunSyncSingleAccountReceivedSig: {
                        isds.syncSingleAccountReceived(accountModel, messageModel, userName)
                }
            }
            Connections {
                target: isds
                onRunSyncSingleAccountSentSig: {
                        isds.syncSingleAccountSent(accountModel, messageModel, userName)
                }
            }
259 260 261
        }
    }
}