AppLink sample code: Servermain.qml

//
// This program is protected under international and U.S. copyright laws as an unpublished work. 
//
// This program is confidential and proprietary to the copyright owners. 
// Reproduction or disclosure, in whole or in part, or the production of derivative works therefrom
// without the express permission of the copyright owners is prohibited.
//
// Copyright (C) 2016 by Dolby Laboratories.
// All rights reserved.
//
import QtQuick 1.1
import com.dolby.dcp.components 1.0
import com.dolby.dcp.engine 1.0

UIApplication {
    id: appLinkApp

    autoLaunchAfterLoad: true

    property string consoleText: ""
    property alias pageUiComponent: uiComponents

    //////////////////////////////
    //App
    //////////////////////////////

    Connections {
        target: app

        onLaunchApplication: {
            Logger.syslog(LogLevel.INFO, "AppLink test app launched");
            app.loadHomePageOnIdleTimeout = false;
            app.loadMainViewFromComponent(page);
        }
    }

    //////////////////////////////
    //Hub Connection
    //////////////////////////////

    Item {

        id: hubConnection

        property bool active : false
        property AppLinkSession session : null

        signal connected()
        signal disconnected()
        signal messageReceived(string message)

        function sendMessage(msg) {
            if(hubConnection.active) {
                hubConnection.session.sendTextMessage(msg);
            }
        }

        Connections {
            target: Hub.appLink

            onNewSession: {
                Logger.syslog(LogLevel.INFO, "Received new AppLink session " + session.name + ", " + (session.reachable ? "reachable" : "unreachable"));

                hubConnection.session = session;
                hubConnection.active = true;

                hubConnection.connected();
            }
        }

        Connections {
            target: hubConnection.active ? hubConnection.session : null

            onTextMessageReceived: {
                Logger.syslog(LogLevel.INFO, "AppLinkSession " + hubConnection.session.name + " received message '" + msg + "'");

                hubConnection.messageReceived(msg);
            }

            onSessionEnded: {
                Logger.syslog(LogLevel.INFO, "AppLinkSession " + hubConnection.session.name + " disconnected");
                hubConnection.active = false;

                hubConnection.disconnected();
            }

            onReachableChanged: {
                Logger.syslog(LogLevel.INFO, "AppLinkSession " + (hubConnection.session.reachable ? "reachable" : "unreachable"));
            }
        }
    }

    Connections {
        target: hubConnection

        onMessageReceived: {
            handleMessageReceived(message);
        }

        onConnected: {
            handleConnected();
        }

        onDisconnected: {
            handleDisconnected();
        }
    }

    function handleConnected() {
        consoleText = consoleText + "New session " + hubConnection.session.name + " connected\n";
    }

    function handleDisconnected() {
        consoleText = consoleText + "Session " + hubConnection.session.name + " disconnected\n";
    }

    function handleMessageReceived(message) {
        consoleText = consoleText + "Received '" + message + "'\n";
    }

    //////////////////////////////
    //UI Components
    //////////////////////////////

    Component {

        id: page

        UIPage {

            navigationBarVisible: true
            navigationBarModel: menuModel
            navigationBarPosition: 1

            tabLoaderSourceComponent: pageUiComponent

            ListModel {
                id: menuModel
                ListElement {
                    enable: 1
                    iconName: "btn_home_sm"
                    action: "home"
                }
                ListElement {
                    enable: 1
                    iconName: "btn_conf_sm"
                    action: "none"
                }
            }

            onNavigationBarSelectionChanged: {

                switch(action) {
                    case "home":
                        app.showHomeScreen();
                        break;

                    case "none":
                        break;
                }
            }
        }
    }

    Component {
        id : uiComponents

        Item {

            anchors.fill: parent

            UIText {
                id: title
                text: "AppLink Test App (" + (hubConnection.active ? "connected" : " awaiting connection") + ")"
                font.pixelSize: Theme.fontSizeSmall
                anchors { left: parent.left; right: parent.right; top: parent.top; margins: 10 }
                horizontalAlignment: Text.AlignLeft
            }

            Row {
                id: row

                anchors {
                    left: parent.left;
                    right: parent.right;
                    top: title.bottom;
                    margins: 10
                }

                height: 30

                spacing: 5

                UITextField {
                    id:  sendText
                    width: 240
                    anchors.verticalCenter: parent.verticalCenter
                }

                UITextButton {

                    id: sendButton

                    text : "Send"
                    font.pixelSize: Theme.fontSizeSmall

                    enabled : hubConnection.active

                    horizontalMargins : 10
                    verticalMargins : 5

                    anchors.verticalCenter: parent.verticalCenter

                    onClicked: {
                        if (hubConnection.active) {
                            var msg = sendText.textComponent.text;
                            appLinkApp.consoleText = appLinkApp.consoleText + "Sent '" + msg + "'\n";
                            hubConnection.sendMessage(msg);
                        }
                    }
                }

                UITextButton {

                    id: clearButton
                    text : "Clear"
                    font.pixelSize: Theme.fontSizeSmall

                    enabled : hubConnection.active

                    horizontalMargins : 10
                    verticalMargins : 5

                    anchors.verticalCenter: parent.verticalCenter

                    onClicked: {
                        appLinkApp.consoleText = "";
                        sendText.inputText = "";
                    }
                }
            }

            Flickable {

                id: flickable

                anchors.margins : 10

                anchors.top : parent.top
                anchors.topMargin : title.height + row.height + 10 + (10 * 2)

                anchors.bottom : parent.bottom
                anchors.left : parent.left
                anchors.right : parent.right

                contentWidth: contentItem.childrenRect.width
                contentHeight: contentItem.childrenRect.height

                clip: true

                flickableDirection : Flickable.VerticalFlick

                Rectangle {
                    color: Theme.textFieldBackgroundColor
                    width : Screen.tabWidth
                    height : Screen.tabHeight - anchors.topMargin
                }

                UIText {
                    text: consoleText
                    font.pixelSize: Theme.fontSizeMicro

                    anchors.left: parent.left
                    anchors.right: parent.right
                    width: parent.width - 20
                    horizontalAlignment: Text.AlignLeft
                    color: Theme.defaultTextColor
                }
            }
        }
    }
}