Skip to content
Snippets Groups Projects
main.qml 4.39 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kali Kaneko's avatar
    Kali Kaneko committed
    import QtQuick 2.0
    import QtQuick.Controls 2.4
    import QtQuick.Dialogs 1.2
    import QtQuick.Controls.Material 2.1
    import QtQuick.Layouts 1.14
    
    import "./components"
    
    ApplicationWindow {
    
        id: root
        visible: true
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
        property int appHeight: 460
    
        property int appWidth: 280
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        property alias customTheme: themeLoader.item
    
        property bool drawerOn: false
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
        width: appWidth
        minimumWidth: appWidth
        maximumWidth: appWidth
    
        height: appHeight
        minimumHeight: appHeight
        maximumHeight: appHeight
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        title: ctx ? ctx.appName : ""
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        Material.accent: Material.Green
    
        property var ctx
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        property var error: ""
    
    
        // TODO can move properties to some state sub-item to unclutter
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        property bool isDonationService: false
        property bool showDonationReminder: false
    
        property var locationsModel: []
        // TODO get from persistance
        property var selectedGateway: "auto"
    
    Kali Kaneko's avatar
    Kali Kaneko committed
    
    
        // FIXME get svg icons
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        property var icons: {
            "off": "qrc:/assets/icon/png/white/vpn_off.png",
            "on": "qrc:/assets/icon/png/white/vpn_on.png",
            "wait": "qrc:/assets/icon/png/white/vpn_wait_0.png",
            "blocked": "qrc:/assets/icon/png/white/vpn_blocked.png"
        }
    
    
        signal openDonateDialog()
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        FontLoader {
            id: lightFont
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            source: "qrc:/poppins-regular.ttf"
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        }
    
        FontLoader {
            id: boldFont
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            source: "qrc:/poppins-bold.ttf"
        }
    
        FontLoader {
            id: boldFontMonserrat
            source: "qrc:/monserrat-bold.ttf"
    
        FontLoader {
            id: robotoFont
            source: "qrc:/roboto.ttf"
        }
    
        FontLoader {
            id: robotoBoldFont
            source: "qrc:/roboto-bold.ttf"
        }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        Loader {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            id: loader
            asynchronous: true
            anchors.fill: parent
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        Loader {
            id: themeLoader
            source: loadTheme()
        }
    
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        Systray {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            id: systray
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        }
    
        Connections {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            target: jsonModel
            function onDataChanged() {
                let j = jsonModel.getJson()
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                if (qmlDebug) {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                    console.debug(j)
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                }
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                ctx = JSON.parse(j)
    
                if (ctx != undefined) {
    
                    locationsModel = getSortedLocations()
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                if (ctx.errors) {
                    console.debug("errors, setting root.error")
                    root.error = ctx.errors
                } else {
                    root.error = ""
                }
                if (ctx.donateURL) {
    
                    isDonationService = true
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                }
                if (ctx.donateDialog == 'true') {
    
                    showDonationReminder = true
    
    Kali Kaneko's avatar
    Kali Kaneko committed
                }
    
                if (isAutoLocation()) {
                    root.selectedGateway = "auto"
                }
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            }
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        }
    
    
        function getSortedLocations() {
            let obj = ctx.locations
            var arr = []
            for (var prop in obj) {
                if (obj.hasOwnProperty(prop)) {
                    arr.push({
                                 "key": prop,
                                 "value": obj[prop]
                             })
                }
            }
            arr.sort(function (a, b) {
                return a.value - b.value
            }).reverse()
            return Array.from(arr, (k,_) => k.key);
        }
    
    
        function isAutoLocation() {
            // FIXME there's something weird going on with newyork location...
            // it gets marked as auto, which from europe is a bug.
            let best = ctx.locationLabels[ctx.bestLocation]
            if (best == undefined) {
                return false
            }
            return (best[0] == ctx.currentLocation)
        }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        function bringToFront() {
            // FIXME does not work properly, at least on linux 
            if (visibility == 3) {
                showNormal()
            } else {
                show() 
            }
            raise()
            requestActivate()
        }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        function loadTheme() {
            let arr = flavor.split("/")
            var providerFlavor = arr[arr.length-1]
            console.debug("flavor: " + providerFlavor)
            if (providerFlavor == "riseup-vpn") {
                return "themes/Riseup.qml"
            } else if (providerFlavor== "calyx-vpn") {
                return "themes/Calyx.qml"
            } else {
                // we should do a Default theme, with a fallback
                // mechanism
                return "Riseup.qml"
            }
        }
    
    
    Kali Kaneko's avatar
    Kali Kaneko committed
        onSceneGraphError: function (error, msg) {
    
    Kali Kaneko's avatar
    Kali Kaneko committed
            console.debug("ERROR while initializing scene")
            console.debug(msg)
        }
    
        Component.onCompleted: {
            loader.source = "components/Splash.qml"
        }
    }