UserDetailsPage.qml 17.2 KB
Newer Older
Jan Blackquill's avatar
Jan Blackquill committed
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
/*
    Copyright 2019  Nicolas Fella <nicolas.fella@gmx.de>
    Copyright 2020  Carson Black <uhhadd@gmail.com>

    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) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    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, see <http://www.gnu.org/licenses/>.
*/

import QtQuick 2.6
import QtQuick.Layouts 1.3
import QtGraphicalEffects 1.12
import QtQuick.Controls 2.5 as QQC2
import QtQuick.Dialogs 1.3

import org.kde.kcm 1.2
29
import org.kde.kirigami 2.13 as Kirigami
Jan Blackquill's avatar
Jan Blackquill committed
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

SimpleKCM {
    id: usersDetailPage

    title: user.realName

    property QtObject user
    property bool overrideImage: false
    property url oldImage

    Connections {
        target: kcm

        onApply: {
            usersDetailPage.user.realName = realNametextField.text
            usersDetailPage.user.email = emailTextField.text
            usersDetailPage.user.name = userNameField.text
            usersDetailPage.user.administrator = (usertypeBox.model[usertypeBox.currentIndex]["type"] == "administrator")
            user.apply()
            usersDetailPage.overrideImage = false
            usersDetailPage.oldImage = ""
        }

        onReset: {
            realNametextField.text = usersDetailPage.user.realName
            emailTextField.text = usersDetailPage.user.email
            userNameField.text = usersDetailPage.user.name
            usertypeBox.currentIndex = usersDetailPage.user.administrator ? 1 : 0
            if (usersDetailPage.oldImage != "") {
                usersDetailPage.overrideImage = false
                usersDetailPage.user.face = usersDetailPage.oldImage
            }
        }
    }

    function resolvePending() {
        let pending = false
        let user = usersDetailPage.user
        pending = pending || user.realName != realNametextField.text
        pending = pending || user.email != emailTextField.text
        pending = pending || user.name != userNameField.text
        pending = pending || user.administrator != (usertypeBox.model[usertypeBox.currentIndex]["type"] == "administrator")
        pending = pending || usersDetailPage.overrideImage
        return pending
    }

    Component.onCompleted: {
        kcm.needsSave = Qt.binding(resolvePending)
    }

    FileDialog {
        id: fileDialog
        title: i18n("Choose a picture")
        folder: shortcuts.pictures
        onAccepted: {
            picturesSheet.close()
            usersDetailPage.oldImage = usersDetailPage.user.face
            usersDetailPage.user.face = fileDialog.fileUrl
            usersDetailPage.overrideImage = true
        }
    }

    ColumnLayout {

        RowLayout {
            Layout.alignment: Qt.AlignHCenter
            QQC2.RoundButton {
                id: userPfp

                readonly property int size: 6 * Kirigami.Units.gridUnit

                implicitWidth: size
                implicitHeight: size
                flat: true

105
                Kirigami.Avatar {
Jan Blackquill's avatar
Jan Blackquill committed
106
                    source: usersDetailPage.user.face
107
                    name: user.realName
Jan Blackquill's avatar
Jan Blackquill committed
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
289
290
291
292
293
294
295
296
297
                    anchors {
                        fill: parent
                        margins: Kirigami.Units.smallSpacing
                    }
                }

                Layout.alignment: Qt.AlignHCenter
                Layout.maximumHeight: size + Kirigami.smallSpacing
                Layout.minimumHeight: size + Kirigami.smallSpacing
                Layout.maximumWidth: size + Kirigami.smallSpacing
                Layout.minimumWidth: size + Kirigami.smallSpacing

                onClicked: {
                    picturesSheet.open()
                    stackSwitcher.forceActiveFocus()
                }
            }
            QQC2.TextField  {
                id: realNametextField
                focus: true
                text: user.realName
                placeholderText: i18nc("Example name", "John Doe")
            }
        }
        Kirigami.FormLayout {
            QQC2.TextField {
                id: userNameField
                focus: true
                text: user.name
                Kirigami.FormData.label: i18n("Username:")
            }

            QQC2.ComboBox {
                id: usertypeBox

                textRole: "label"
                model: [
                    { "type": "standard", "label": i18n("Standard") },
                    { "type": "administrator", "label": i18n("Administrator") },
                ]

                Kirigami.FormData.label: i18n("Account type:")

                currentIndex: user.administrator ? 1 : 0
            }

            QQC2.TextField {
                id: emailTextField
                focus: true
                text: user.email
                placeholderText: i18nc("Example email address", "john.doe@kde.org")
                Kirigami.FormData.label: i18n("Email address:")
            }

            QQC2.Button {
                text: i18n("Change Password")
                onClicked: {
                    changePassword.account = user
                    changePassword.openAndClear()
                }
            }

            Item {
                Layout.preferredHeight: deleteUser.height
            }

            QQC2.Button {
                id: deleteUser

                enabled: !usersDetailPage.user.loggedIn && (!kcm.userModel.rowCount() < 2)

                QQC2.Menu {
                    id: deleteMenu
                    modal: true
                    QQC2.MenuItem {
                        text: i18n("Delete files")
                        icon.name: "edit-delete-shred"
                        onClicked: {
                            if (kcm.deleteUser(usersDetailPage.user.uid, true)) {
                                kcm.pop()
                            }
                        }
                    }
                    QQC2.MenuItem {
                        text: i18n("Keep files")
                        icon.name: "document-multiple"
                        onClicked: {
                            if (kcm.deleteUser(usersDetailPage.user.uid, false)) {
                                kcm.pop()
                            }
                        }
                    }
                }
                text: i18n("Delete User...")
                icon.name: "edit-delete"
                onClicked: deleteMenu.open()
            }
        }
    }

    Kirigami.OverlaySheet {
        id: picturesSheet
        header: RowLayout {
            Kirigami.Heading {
                text: i18n("Change Avatar")
            }
        }
        StackLayout {
            id: stackSwitcher
            Layout.preferredWidth: usersDetailPage.width - (Kirigami.Units.largeSpacing*4)
            Keys.onEscapePressed: {
                picturesSheet.close()
                event.accepted = true
            }
            ColumnLayout {
                id: cols
                GridLayout {
                    id: picturesColumn

                    rowSpacing: Kirigami.Units.smallSpacing
                    columnSpacing: Kirigami.Units.smallSpacing
                    columns: {
                        return Math.floor((stackSwitcher.width) / (Kirigami.Units.gridUnit * 6))
                    }

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignHCenter

                    QQC2.Button {
                        Layout.preferredHeight: Kirigami.Units.gridUnit * 6
                        Layout.preferredWidth: Layout.preferredHeight

                        ColumnLayout {
                            anchors.centerIn: parent

                            Kirigami.Icon {
                                width: Kirigami.Units.gridUnit * 4
                                height: Kirigami.Units.gridUnit * 4
                                source: "document-open"

                                Layout.alignment: Qt.AlignHCenter
                            }
                            QQC2.Label {
                                text: i18n("Choose File...")

                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignBottom
                                fontSizeMode: Text.HorizontalFit
                                wrapMode: Text.Wrap
                                Layout.fillWidth: true
                                Layout.maximumWidth: Kirigami.Units.gridUnit * 5
                            }
                        }

                        onClicked: fileDialog.open()
                    }

                    QQC2.Button {
                        Layout.preferredHeight: Kirigami.Units.gridUnit * 6
                        Layout.preferredWidth: Layout.preferredHeight

                        Kirigami.Heading {
                            anchors.centerIn: parent
                            font.pixelSize: Kirigami.Units.gridUnit * 4
                            text: kcm.initializeString(user.realName)
                        }

                        onClicked: stackSwitcher.currentIndex = 1
                        // https://bugs.kde.org/show_bug.cgi?id=420439
                        visible: false
                    }

                    Repeater {
                        model: [
                            "Artist Konqi.png",
                            "Bookworm Konqi.png",
                            "Boss Konqi.png",
                            "Bug Catcher Konqi.png",
                            "Card Shark Konqi.png",
                            "Hacker Konqi.png",
                            "Journalist Konqi.png",
                            "Katie.png",
                            "Konqi.png",
                            "Mechanic Konqi.png",
                            "Messenger Konqi.png",
                            "Musician Konqi.png",
                            "Office Worker Konqi.png",
                            "PC Builder Konqi.png",
                            "Scientist Konqi.png",
                            "Teacher Konqi.png",
Jan Blackquill's avatar
Jan Blackquill committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
                            "Virtual Reality Konqi.png",
                            "photos/Air Balloon.png",
                            "photos/Astronaut.png",
                            "photos/Books.png",
                            "photos/Brushes.png",
                            "photos/Bulb.png",
                            "photos/Car.png",
                            "photos/Cat.png",
                            "photos/Chameleon.png",
                            "photos/Cocktail.png",
                            "photos/Dog.png",
                            "photos/Fish.png",
                            "photos/Gamepad.png",
                            "photos/Owl.png",
                            "photos/Pancakes.png",
                            "photos/Parrot.png",
                            "photos/Pencils.png",
                            "photos/Shuttle.png",
                            "photos/Soccer.png",
                            "photos/Sunflower.png",
                            "photos/Sushi.png"
Jan Blackquill's avatar
Jan Blackquill committed
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
                        ]
                        QQC2.Button {
                            Layout.preferredHeight: Kirigami.Units.gridUnit * 6
                            Layout.preferredWidth: Layout.preferredHeight

                            QQC2.ToolTip.delay: Kirigami.Units.toolTipDelay
                            QQC2.ToolTip.text: modelData
                            Accessible.name: modelData

                            Image {
                                id: imgDelegate
                                smooth: true
                                mipmap: true

                                anchors.fill: parent
                                source: "../img/%1".arg(modelData)

                                Accessible.ignored: true
                            }

                            onClicked: {
                                picturesSheet.close()
                                usersDetailPage.oldImage = usersDetailPage.user.face
                                usersDetailPage.user.face = imgDelegate.source
                                usersDetailPage.overrideImage = true
                            }
                        }
                    }
                }
            }
            ColumnLayout {
                id: initialPictures
                GridLayout {
                    id: initialsColumn

                    rowSpacing: Kirigami.Units.smallSpacing
                    columnSpacing: Kirigami.Units.smallSpacing
                    columns: {
                        return Math.floor((stackSwitcher.width) / (Kirigami.Units.gridUnit * 6))
                    }

                    Layout.fillWidth: true
                    Layout.alignment: Qt.AlignHCenter

                    QQC2.Button {
                        Layout.preferredHeight: Kirigami.Units.gridUnit * 6
                        Layout.preferredWidth: Layout.preferredHeight

                        ColumnLayout {
                            anchors.centerIn: parent

                            Kirigami.Icon {
                                width: Kirigami.Units.gridUnit * 4
                                height: Kirigami.Units.gridUnit * 4
                                source: "go-previous"

                                Layout.alignment: Qt.AlignHCenter
                            }
                        }

                        onClicked: stackSwitcher.currentIndex = 0
                    }

                    Repeater {
                        model: [
                            {"name": i18n("Feisty Flamingo"),  "color": "#E93A9A", "dark": true},
                            {"name": i18n("Dragon's Fruit"),   "color": "#E93D58", "dark": true},
                            {"name": i18n("Sweet Potato"),     "color": "#E9643A", "dark": true},
                            {"name": i18n("Ambient Amber"),    "color": "#EF973C", "dark": false},
                            {"name": i18n("Sparkle Sunbeam"),  "color": "#E8CB2D", "dark": false},
                            {"name": i18n("Lemon-Lime"),       "color": "#B6E521", "dark": false},
                            {"name": i18n("Verdant Charm"),    "color": "#3DD425", "dark": false},
                            {"name": i18n("Mellow Meadow"),    "color": "#00D485", "dark": false},
                            {"name": i18n("Tepid Teal"),       "color": "#00D3B8", "dark": false},
                            {"name": i18n("Plasma Blue"),      "color": "#3DAEE9", "dark": true},
                            {"name": i18n("Pon Purple"),       "color": "#B875DC", "dark": true},
                            {"name": i18n("Bajo Purple"),      "color": "#926EE4", "dark": true},
                            {"name": i18n("Burnt Charcoal"),   "color": "#232629", "dark": true},
                            {"name": i18n("Paper Perfection"), "color": "#EEF1F5", "dark": false},
                            {"name": i18n("Cafétera Brown"),   "color": "#CB775A", "dark": false},
                            {"name": i18n("Rich Hardwood"),    "color": "#6A250E", "dark": true}
                        ]
                        delegate: QQC2.Button {
                            Layout.preferredHeight: Kirigami.Units.gridUnit * 6
                            Layout.preferredWidth: Layout.preferredHeight

                            Rectangle {
                                id: colourRectangle

                                anchors.fill: parent
                                anchors.margins: Kirigami.Units.smallSpacing
                                color: modelData.color
                                Kirigami.Heading {
                                    anchors.centerIn: parent
                                    color: modelData.dark ? "white" : "black"
                                    font.pixelSize: Kirigami.Units.gridUnit * 4
                                    text: kcm.initializeString(user.realName)
                                }
                            }

                            onClicked: {
                                colourRectangle.grabToImage(function(result) {
                                    picturesSheet.close()
                                    let uri = kcm.plonkImageInTempfile(result.image)
                                    if (uri != "") {
                                        usersDetailPage.oldImage = usersDetailPage.user.face
                                        usersDetailPage.user.face = uri
                                        usersDetailPage.overrideImage = true
                                    }
                                })
                            }
                        }
                    }
                }
            }
        }
    }

    ChangePassword { id: changePassword; account: user }
}