Skip to content
Snippets Groups Projects
main.qml 4.85 KiB
Newer Older
akiraohgaki's avatar
akiraohgaki committed
import QtQuick 2.3
akiraohgaki's avatar
akiraohgaki committed
import QtQuick.Window 2.0
akiraohgaki's avatar
akiraohgaki committed
import QtQuick.Controls 1.2
akiraohgaki's avatar
akiraohgaki committed
import QtQuick.Dialogs 1.2
akiraohgaki's avatar
akiraohgaki committed

akiraohgaki's avatar
akiraohgaki committed
import "scripts/Utility.js" as Utility
akiraohgaki's avatar
akiraohgaki committed

akiraohgaki's avatar
akiraohgaki committed
Window {
akiraohgaki's avatar
akiraohgaki committed
    id: app
akiraohgaki's avatar
akiraohgaki committed
    title: Qt.application.name
    width: 400
    height: 200
    minimumWidth: 400
    minimumHeight: 200
    maximumWidth: 800
    maximumHeight: 400
akiraohgaki's avatar
akiraohgaki committed

akiraohgaki's avatar
akiraohgaki committed
    MessageDialog {
        id: confirmDialog
akiraohgaki's avatar
akiraohgaki committed
        title: app.title
akiraohgaki's avatar
akiraohgaki committed
        icon: StandardIcon.Question
akiraohgaki's avatar
akiraohgaki committed
        text: ""
        informativeText: ""
        detailedText: ""
akiraohgaki's avatar
akiraohgaki committed
        standardButtons: StandardButton.Ok | StandardButton.Cancel
        onAccepted: xdgUrlHandler.process()
        onRejected: Qt.quit()
    }

    MessageDialog {
        id: infoDialog
akiraohgaki's avatar
akiraohgaki committed
        title: app.title
akiraohgaki's avatar
akiraohgaki committed
        icon: StandardIcon.Information
akiraohgaki's avatar
akiraohgaki committed
        text: ""
        informativeText: ""
        detailedText: ""
akiraohgaki's avatar
akiraohgaki committed
        standardButtons: StandardButton.Open | StandardButton.Close
        onAccepted: {
            xdgUrlHandler.openDestination();
            Qt.quit();
        }
        onRejected: Qt.quit()
akiraohgaki's avatar
akiraohgaki committed
    }

    MessageDialog {
        id: errorDialog
akiraohgaki's avatar
akiraohgaki committed
        title: app.title
akiraohgaki's avatar
akiraohgaki committed
        icon: StandardIcon.Warning
akiraohgaki's avatar
akiraohgaki committed
        text: ""
        informativeText: ""
        detailedText: ""
akiraohgaki's avatar
akiraohgaki committed
        standardButtons: StandardButton.Close
        onRejected: Qt.quit()
akiraohgaki's avatar
akiraohgaki committed
    }

akiraohgaki's avatar
akiraohgaki committed
    Dialog {
akiraohgaki's avatar
akiraohgaki committed
        id: progressDialog
akiraohgaki's avatar
akiraohgaki committed
        title: app.title
        property alias primaryLabel: primaryLabel
        property alias informativeLabel: informativeLabel
        property alias progressBar: progressBar
        property alias progressLabel: progressLabel
akiraohgaki's avatar
akiraohgaki committed
        contentItem: Item {
akiraohgaki's avatar
akiraohgaki committed
            implicitWidth: 400
akiraohgaki's avatar
akiraohgaki committed
            implicitHeight: 150
akiraohgaki's avatar
akiraohgaki committed
            Column {
                anchors.fill: parent
                anchors.margins: 12
                spacing: 8
                Label {
                    id: primaryLabel
                    text: " "
akiraohgaki's avatar
akiraohgaki committed
                    font.bold: true
                }
akiraohgaki's avatar
akiraohgaki committed
                Label {
                    id: informativeLabel
                    text: " "
akiraohgaki's avatar
akiraohgaki committed
                }
akiraohgaki's avatar
akiraohgaki committed
                ProgressBar {
                    id: progressBar
                    maximumValue: 1
                    minimumValue: 0
                    value: 0
                    anchors.left: parent.left
                    anchors.right: parent.right
                }
akiraohgaki's avatar
akiraohgaki committed
                Label {
                    id: progressLabel
                    text: " "
akiraohgaki's avatar
akiraohgaki committed
                    anchors.right: parent.right
                }
akiraohgaki's avatar
akiraohgaki committed
                Button {
                    id: cancelButton
akiraohgaki's avatar
akiraohgaki committed
                    text: "Cancel"
akiraohgaki's avatar
akiraohgaki committed
                    anchors.right: parent.right
                    onClicked: Qt.quit()
                }
            }
akiraohgaki's avatar
akiraohgaki committed
    Component.onCompleted: {
akiraohgaki's avatar
akiraohgaki committed
        var metadata = xdgUrlHandler.metadata();
        var primaryMessages = {
akiraohgaki's avatar
akiraohgaki committed
            "success_download": "Download successfull",
            "success_install": "Installation successfull",
            "error_validation": "Validation error",
            "error_network": "Network error",
            "error_save": "Saving file failed",
            "error_install": "Installation failed"
        };

        xdgUrlHandler.started.connect(function() {
            progressDialog.open();
        });
akiraohgaki's avatar
akiraohgaki committed

akiraohgaki's avatar
akiraohgaki committed
        xdgUrlHandler.finishedWithSuccess.connect(function(result) {
            progressDialog.close();
            infoDialog.text = primaryMessages[result.status];
            infoDialog.informativeText = metadata.filename;
            infoDialog.detailedText = result.message;
            infoDialog.open();
        });
akiraohgaki's avatar
akiraohgaki committed

akiraohgaki's avatar
akiraohgaki committed
        xdgUrlHandler.finishedWithError.connect(function(result) {
            progressDialog.close();
            errorDialog.text = primaryMessages[result.status];
            errorDialog.informativeText = metadata.filename;
            errorDialog.detailedText = result.message;
            errorDialog.open();
akiraohgaki's avatar
akiraohgaki committed
        });

akiraohgaki's avatar
akiraohgaki committed
        xdgUrlHandler.downloadProgress.connect(function(id, bytesReceived, bytesTotal) {
akiraohgaki's avatar
akiraohgaki committed
            progressDialog.primaryLabel.text = "Downloading... ";
akiraohgaki's avatar
akiraohgaki committed
            progressDialog.informativeLabel.text = metadata.filename;
akiraohgaki's avatar
akiraohgaki committed
            progressDialog.progressBar.value = bytesReceived / bytesTotal;
            progressDialog.progressLabel.text = Utility.convertByteToHumanReadable(bytesReceived)
akiraohgaki's avatar
akiraohgaki committed
                    + " / " + Utility.convertByteToHumanReadable(bytesTotal)
akiraohgaki's avatar
akiraohgaki committed
        if (xdgUrlHandler.isValid()) {
akiraohgaki's avatar
akiraohgaki committed
            confirmDialog.text = "Do you want to " + metadata.command + "?";
akiraohgaki's avatar
akiraohgaki committed
            confirmDialog.informativeText = metadata.filename;
akiraohgaki's avatar
akiraohgaki committed
            confirmDialog.detailedText = "URL: " + metadata.url + "\n\n"
                    + "File: " + metadata.filename + "\n\n"
                    + "Type: " + metadata.type;
akiraohgaki's avatar
akiraohgaki committed
            confirmDialog.open();
        }
        else {
akiraohgaki's avatar
akiraohgaki committed
            errorDialog.text = "Validation error";
            errorDialog.detailedText = "Invalid XDG-URL " + xdgUrlHandler.xdgUrl();
akiraohgaki's avatar
akiraohgaki committed
            errorDialog.open();
        }
    }
akiraohgaki's avatar
akiraohgaki committed
}