main.qml 20.1 KB
Newer Older
1
/*
Karel Slaný's avatar
Karel Slaný committed
2
 * Copyright (C) 2014-2017 CZ.NIC
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * 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 QtQuick.Window 2.1
27
import QtQuick.Layouts 1.3
28
import QtQuick.Dialogs 1.2
29
import QtQuick.Controls.Material 2.1
30
import cz.nic.mobileDatovka 1.0
31 32 33 34 35 36 37 38 39 40 41 42

ApplicationWindow {

    // Get deafult color theme and palette from system
    SystemPalette { id: datovkaPalette; colorGroup: SystemPalette.Active }
    id: mainWindow
    visible: true
    title: "Datovka"
    width: 800
    height: 600
    color: datovkaPalette.window

43 44
    // This element set default font (size) and must be hidden. Font is used
    // accross application as default font size. Do not remove it.
45 46
    Text {
        id: defaultTextFont
47 48
        visible: false
        text: "Note: set default font"
49 50
    }

51 52
    // Set default material design from system palette
    Material.theme: Material.System
53 54 55 56 57 58 59
    Material.background: datovkaPalette.window
    Material.foreground: datovkaPalette.windowText
    Material.primary: Material.Blue
    Material.accent: Material.Blue
    /* Some color saturation */
    property color readBgColor: Qt.darker(datovkaPalette.alternateBase, 1.08)
    property color headerColor: Material.color(Material.Blue)
60

61
    // define all pages for stackview
Karel Slaný's avatar
Karel Slaný committed
62 63
    property Component pageAboutApp: PageAboutApp {}
    property Component pageAccountDetail: PageAccountDetail {}
64
    property Component pageChangePassword: PageChangePassword {}
Karel Slaný's avatar
Karel Slaný committed
65
    property Component pageMenuAccount: PageMenuAccount {}
Karel Slaný's avatar
Karel Slaný committed
66 67 68 69 70 71
    property Component pageMenuDatovkaSettings: PageMenuDatovkaSettings {}
    property Component pageMenuMessage: PageMenuMessage {}
    property Component pageMenuMessageDetail: PageMenuMessageDetail {}
    property Component pageMenuMessageList: PageMenuMessageList {}
    property Component pageMessageDetail: PageMessageDetail {}
    property Component pageMessageList: PageMessageList {}
72
    property Component pageMessageSearch: PageMessageSearch {}
Karel Slaný's avatar
Karel Slaný committed
73 74 75 76 77
    property Component pageSettingsAccount: PageSettingsAccount {}
    property Component pageSettingsGeneral: PageSettingsGeneral {}
    property Component pageSettingsPin: PageSettingsPin {}
    property Component pageSettingsStorage: PageSettingsStorage {}
    property Component pageSettingsSync: PageSettingsSync {}
78

79 80 81 82
    // header background color
    property string mainHeaderBgColor: "#00539b"
    property int statusBarTimer: 5000
    // dimension and style based on font pixel size and screen dpi
83 84 85 86
    property int textFontSizeInPixels: defaultTextFont.font.pixelSize
    property int textPointSmall: Math.round(defaultTextFont.font.pointSize * 0.7)
    property int textFontSizeSmall: if (textPointSmall > 0) {textPointSmall} else {8}
    property int headerHeight: textFontSizeInPixels * 3
87
    property int imgHeight: headerHeight * 0.8
88
    // inputItemHeight holds height of controls elements computed from font size
Martin Straka's avatar
Martin Straka committed
89
    property int inputItemHeight: headerHeight
90 91 92 93
    property int navImgHeight: headerHeight * 0.3
    property int listItemHeight: headerHeight * 1.5
    property int defaultMargin: Math.round(Screen.pixelDensity)
    property int acntListSpacing: defaultMargin * 2
94
    property int formItemVerticalSpacing: defaultMargin
95
    property int formButtonHorizontalSpacing: defaultMargin * 5
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    /* compare message delivery date with current date-90days */
    property int deleteAfterDays: 90
    function compareMsgDate(msgDeliveryTime) {
        if (msgDeliveryTime == "") {
            // message has virus or delivery time missing
            return true
        } else {
            // convert qml date format to ISO date format
            var inputDateFormat = /(\d{2})\.(\d{2})\.(\d{4})/;
            var msgDate = new Date(msgDeliveryTime.replace(inputDateFormat,'$3-$2-$1'));
            var today = new Date()
            today.setDate(today.getDate()-deleteAfterDays)
            // compare both dates in milliseconds
            return (today.getTime() > msgDate.getTime())
        }
    }

114 115 116
    /* Exposes nested stack to code outside the page component. */
    property var nestedStack: null

117
    /* Password/OTP input dialog, emitted from C++ */
Martin Straka's avatar
Martin Straka committed
118 119 120
    InputDialogue {
        id: inputDialog
        onFinished: {
121
            isds.returnInputDialogText(isdsAction, pwdType, userName, pwd)
Martin Straka's avatar
Martin Straka committed
122 123 124
        }
        Connections {
            target: isds
125
            onOpenDialogRequest: {
126
                 inputDialog.openInputDialog(isdsAction, pwdType, userName, title, text, placeholderText, hidePwd)
Martin Straka's avatar
Martin Straka committed
127 128 129 130
            }
        }
    }

131 132
    StackView { // Page area.
        id: mainStack
133
        anchors.fill: parent
134 135
        initialItem: appArea

136
        Item {
137
            id: appArea
138 139
            objectName: "appArea"
            anchors.fill: parent
140

141 142 143
            StackView.onActivated: {
                mainStack.forceActiveFocus()
            }
144

145 146 147 148 149 150
            Item { /* Application page stack. */
                id: mainPage
                anchors.fill: parent
                visible: true
                StackView {
                    id: pageView
151 152
                    anchors.fill: parent
                    visible: true
153 154 155 156
                    initialItem: PageAccountList {}

                    Component.onCompleted: {
                        nestedStack = pageView
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
            }
            Rectangle {
                id: statusBar
                visible: false
                anchors.left: parent.left
                anchors.bottom: parent.bottom
                anchors.right: parent.right
                height: headerHeight * 0.5
                color: datovkaPalette.alternateBase
                BusyIndicator {
                    id: busyuIndicator
                    width: parent.height
                    height: parent.height
                    anchors.centerIn: parent
                    running: false
                }
                Text {
                    id: statusBarText
                    color: datovkaPalette.text
                    anchors.centerIn: parent
                    text: mainWindow.width + " x "+ mainWindow.height
                    Connections {
                    target: isds
                        onStatusBarTextChanged: {
                            statusBar.visible = isVisible
                            statusBarText.text = txt
                            busyuIndicator.running = busy
                            timerId.running = !busy
187
                        }
188 189 190 191 192 193 194 195
                    }
                    Connections {
                        target: files
                        onStatusBarTextChanged: {
                            statusBar.visible = true
                            statusBarText.text = txt
                            busyuIndicator.running = busy
                            timerId.running = !busy
196 197
                        }
                    }
198 199 200 201 202 203 204
                    Connections {
                        target: settings
                        onStatusBarTextChanged: {
                            statusBar.visible = true
                            statusBarText.text = txt
                            busyuIndicator.running = busy
                            timerId.running = !busy
205 206
                        }
                    }
207
                }
208 209 210 211 212 213 214 215 216 217 218
                Timer {
                    id: timerId
                    interval: statusBarTimer;
                    running: false;
                    repeat: false
                    onTriggered: {
                        timerId.running = false
                        statusBar.visible = false
                        statusBarText.text = ""
                    }
                }
219
            }
220
        }
221

222 223 224 225 226
        Component { /* Pin lock screen. */
            id: lockScreen
            Rectangle  {
                objectName: "lockScreen"
                anchors.fill: parent
227
                color: datovkaPalette.base
228
                Column {
229
                    anchors.centerIn: parent
230
                    spacing: formItemVerticalSpacing
231 232
                    TextField {
                        id: pinCodeInput
233
                        height: inputItemHeight
234
                        font.pointSize: defaultTextFont.font.pointSize
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
                        //focus: true // Forcing focus here causes troubles on mobile devices.
                        echoMode: TextInput.Password
                        passwordMaskDelay: 500 // milliseconds
                        inputMethodHints: Qt.ImhDigitsOnly
                        placeholderText: qsTr("Enter PIN code")
                        anchors.horizontalCenter: parent.horizontalCenter
                        horizontalAlignment: TextInput.AlignHCenter
                        function verifyPin() {
                            settings.verifyPin(pinCodeInput.text.toString())
                        }
                        onEditingFinished: {
                            // This function is called repeatedly when switching
                            // windows. The condition should reduce PIN verification
                            // calls.
                            if (pinCodeInput.text.length > 0) {
                                verifyPin()
                            }
                        }
Karel Slaný's avatar
Karel Slaný committed
253
                    }
254 255 256 257 258 259 260 261
                    Text {
                        id: wrongPin
                        font.bold: true
                        visible: false
                        color: datovkaPalette.text
                        anchors.horizontalCenter: parent.horizontalCenter
                        text: qsTr("Wrong PIN code!")
                    }
262 263
                    Button {
                        text: qsTr("Enter")
264
                        anchors.horizontalCenter: parent.horizontalCenter
265 266 267 268
                        font.pointSize: defaultTextFont.font.pointSize
                        height: inputItemHeight
                        onClicked: {
                            pinCodeInput.verifyPin()
269 270
                        }
                    }
271
                    Item {
272
                        id: blankField
273
                        width: parent.width
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
                        height: headerHeight
                        anchors.horizontalCenter: parent.horizontalCenter
                    }
                    Image{
                        id: datovkaLogo
                        anchors.horizontalCenter: parent.horizontalCenter
                        width: imgHeight * 1.4
                        height: imgHeight * 1.4
                        source: "qrc:/datovka.png"
                    }
                    Text {
                        id: versionLabel
                        anchors.horizontalCenter: parent.horizontalCenter
                        color: datovkaPalette.text
                        text: qsTr("Version") + ": " + settings.appVersion()
289
                    }
290
                } // Column
291 292 293 294 295 296 297 298 299 300 301 302
                Connections {
                    target: settings
                    onSendPinReply: {
                        if (success) {
                            Qt.inputMethod.hide()
                            if (mainStack.currentItem.objectName == "lockScreen") {
                                mainStack.pop(StackView.Immediate)
                            }
                        }
                        wrongPin.visible = !success
                        pinCodeInput.text = ""
                    }
303 304 305
                } // Connections
            } // Rectangle
        } // Component
306

307 308 309
        Connections {
            target: locker
            onLockApp: {
310 311 312 313
                if (mainStack.currentItem.objectName == "appArea") {
                    /* Lock application area. */
                    mainStack.push(lockScreen, StackView.Immediate)
                }
314 315
            }
        }
316 317 318 319 320 321 322 323
        Connections {
            target: interactionZfoFile
            onDisplayZfoFileContent: {
                /*
                 * The app should be locked from within C++ code when PIN is
                 * enabled.
                 */
                console.log("Showing zfo file: " + filePath)
324
                var fileContent = files.rawFileContent(filePath)
Karel Slaný's avatar
Karel Slaný committed
325
                mainStack.push(pageMessageDetail, {
326
                        "pageView": mainStack,
327
                        "fromLocalDb": false,
328
                        "rawZfoContent": fileContent
329
                    }, StackView.Immediate)
330 331 332 333 334
                /*
                 * Next function has effect only for iOS.
                 * Detail info is in the header file.
                 */
                files.deleteTmpFileFromStorage(filePath)
335 336
            }
        }
337

338 339 340 341 342 343 344 345 346 347 348 349 350
        Connections {
            target: dlgEmitter
            onDlgGetText: {
                /*
                 * Create a dynamic dialogue object set content and connect
                 * functionality.
                 */

                var dlgComponent = Qt.createComponent("qrc:/qml/dialogues/PasteInputDialogue.qml", Component.PreferSynchronous);
                var dlgObj;
                if (dlgComponent.status == Component.Ready) {
                    finishCreation();
                } else {
351
                    console.log("Failed loading dlgComponent:", dlgComponent.errorString());
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
                    dlgComponent.statusChanged.connect(finishCreation);
                }

                function finishCreation() {
                    if (dlgComponent.status == Component.Error) {
                        // Error handling.
                        console.log("Error loading dlgComponent:", dlgComponent.errorString());
                        return;
                    } else if (dlgComponent.status != Component.Ready) {
                        return;
                    }

                    dlgObj = dlgComponent.createObject(mainWindow, {"objectName": "textInputDialogue"});
                    if (dlgObj == null) {
                        // Error handling.
                        console.log("Error creating dialogue object");
                        return;
                    }

                    dlgObj.dialogue.title = title
                    dlgObj.dialogue.content.messageText.text = message
                    dlgObj.dialogue.content.textInput.inputMethodHints = inputMethodHints /* Must be set before setting echoMode. */
                    dlgObj.dialogue.dlgEchoMode = echoMode
                    dlgObj.dialogue.content.textInput.text = text
                    dlgObj.dialogue.content.textInput.placeholderText = placeholderText
                    dlgObj.dialogue.explicitPasteMenu = explicitPasteMenu

                    /* See PasteInputDialogue.qml for explanation. */
                    dlgObj.dialogue.standardButtons = StandardButton.Ok | StandardButton.Cancel

                    /* Connect signals. */
                    dlgObj.dialogueClosed.connect(deleteObject);
                    dlgObj.dialogue.accepted.connect(emitAccepted);
                    dlgObj.dialogue.rejected.connect(emitRejected);

                    dlgObj.dialogue.open();
                }

                function deleteObject() {
                    console.log("Destroying dialogue.");
                    dlgObj.destroy();

                    mainStack.forceActiveFocus(); /* Dialogue stole focus. */
                }

                function emitAccepted() {
                    console.log("Accepted " + dlgObj.dialogue.content.textInput.text);
                    dlgEmitter.emitAccepted(dlgObj.dialogue.content.textInput.text);
                }

                function emitRejected() {
                    console.log("Rejected");
                    dlgEmitter.emitRejected();
                }
            }
407 408 409 410 411 412 413 414 415 416 417 418

            onDlgMessage: {
                /*
                 * Create a dynamic dialogue object set content and connect
                 * functionality.
                 */

                var dlgComponent = Qt.createComponent("qrc:/qml/dialogues/MessageDialogue.qml", Component.PreferSynchronous);
                var dlgObj;
                if (dlgComponent.status == Component.Ready) {
                    finishCreation();
                } else {
419
                    console.log("Failed loading dlgComponent:", dlgComponent.errorString());
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
                    dlgComponent.statusChanged.connect(finishCreation);
                }

                function finishCreation() {
                    if (dlgComponent.status == Component.Error) {
                        // Error handling.
                        console.log("Error loading dlgComponent:", dlgComponent.errorString());
                        return;
                    } else if (dlgComponent.status != Component.Ready) {
                        return;
                    }

                    dlgObj = dlgComponent.createObject(mainWindow, {"objectName": "messageDialogue"});
                    if (dlgObj == null) {
                        // Error handling.
                        console.log("Error creating dialogue object");
                        return;
                    }

439
                    dlgObj.dialogue.icon = icon
440 441 442
                    dlgObj.dialogue.title = title
                    dlgObj.dialogue.content.messageText.text = message
                    dlgObj.dialogue.content.infoMessageText.text = infoMessage
443
                    dlgObj.dialogue.dlgButtons = buttons
444 445 446 447 448 449 450

                    /* Connect signals. */
                    dlgObj.dialogueClosed.connect(gatherDataAndDeleteObject);

                    dlgObj.dialogue.open();
                }

451
                function gatherDataAndDeleteObject(button, customButton) {
452
                    /* TODO -- Gather pressed button value. */
453
                    dlgEmitter.emitClosed(button, customButton)
454 455 456 457 458 459 460 461 462 463 464 465

                    console.log("Destroying dialogue.");
                    dlgObj.destroy();

                    mainStack.forceActiveFocus(); /* Dialogue stole focus. */
                }

                function emitClosed() {
                    console.log("Closed");
                    dlgEmitter.emitClosed(1, -1);
                }
            }
466 467
        }

468
        function navigateBack(mainStack, nestedStack, event) {
469 470
            if (event.key === Qt.Key_Back) {
                event.accepted = true
471 472 473 474 475 476 477 478 479 480

                if (nestedStack == null) {
                    return
                }

                if (mainStack.currentItem.objectName == "lockScreen") {
                    console.log("Ignoring back button on lock screen.")
                } else if (mainStack.depth > 1) {
                    mainStack.pop(StackView.Immediate)
                } else if (nestedStack.depth > 1) {
Martin Straka's avatar
Martin Straka committed
481
                    nestedStack.pop()
482 483 484 485 486 487
                } else {
                    event.accepted = false
                    Qt.quit()
                }
            }
        }
488 489 490 491 492 493

        /* Android back button. */
        focus: true
        Keys.onReleased: {
            navigateBack(mainStack, nestedStack, event)
        }
494
    }
495

496 497 498 499 500
    /* We can loose the focus to StackView if any Menu or Popup is opened */
    function resetFocus() {
        pageView.focus = true
    }

501 502 503 504 505 506 507 508
    Text {
        id: dummyText
        text: ""
        visible: false
        wrapMode: Text.NoWrap
        elide: Text.ElideNone
    }
    function computeMenuWidth(menu) {
509
        var w = 0.0
510
        for (var i = 0; i < menu.contentData.length; i++) {
511
            dummyText.text = menu.contentData[i].text + "www"
512 513 514 515 516
            if (w < dummyText.width) {
                w = dummyText.width
            }
        }
        dummyText.text = ""
517
        return Math.round(w)
518
    }
519
}