~ralsina/+junk/hello-v1

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
29
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
105
106
107
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
import QtQuick 2.0
import Ubuntu.Components 0.1
import hello 1.0

/*!
    \brief MainView with Tabs element.
           First Tab has a single Label and
           second Tab has a single ToolbarAction.
*/

MainView {
    // objectName for functional testing purposes (autopilot-qt5)
    objectName: "mainView"

    // Note! applicationName needs to match the "name" field of the click manifest
    applicationName: "com.ubuntu.developer.ralsina.hello"

    /*
     This property enables the application to change orientation
     when the device is rotated. The default is false.
    */
    //automaticOrientation: true

    width: units.gu(100)
    height: units.gu(75)
    ChatClient {
        id: chatClient
        Component.onCompleted: {
            error.connect(messageList.handle_error)
            registered.connect(nick.registered)
        }
    }

    PushClient {
        id: pushClient
        Component.onCompleted: {
            new_notifications.connect(messageList.handle_notifications)
            error.connect(messageList.handle_error)
        }
        appid: "com.ubuntu.developer.ralsina.hello_hello"

    }

    TextField {
        id: nick
        focus: true
        placeholderText: "Your nickname"
        anchors.left: parent.left
        anchors.right: parent.right
        function registered() {
            nick.readOnly = true
            nick.text = "Your nick is " + chatClient.nick
            message.focus = true
        }
        onAccepted: {
            chatClient.registerNick(nick.text, pushClient.token)
        }
    }
    TextField {
        id: message
        anchors.right: annoyingSwitch.left
        anchors.left: parent.left
        anchors.top: nick.bottom
        anchors.topMargin: units.gu(1)
        anchors.rightMargin: units.gu(1)
        placeholderText: "Your message"
        onAccepted: {
            console.log("sending " + text)
            messagesModel.append({
                "direction" :  "◀ ",
                "nick" :  "",
                "message" : text
            })
            chatClient.sendMessage(message.text, annoyingSwitch.checked)
            message.text = ""
        }
    }
    Switch {
        id: annoyingSwitch
        anchors.right: parent.right
        anchors.top: nick.bottom
        anchors.topMargin: units.gu(1)
    }

    ListModel {
        id: messagesModel
        ListElement {
            nick: ""
            direction: "*** "
            message: "Register by typing your nick and pressing enter."
        }
        ListElement {
            nick: ""
            direction: "*** "
            message: "Send messages in the form \"destination: hello\""
        }
        ListElement {
            nick: ""
            direction: "*** "
            message: "The switch on the right makes the message more annoying for the recipient."
        }
    }

    UbuntuShape {
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.top: message.bottom
        anchors.topMargin: units.gu(1)
        ListView {
            id: messageList
            model: messagesModel
            anchors.fill: parent
            delegate: Text {
                text: direction + nick + ": " + message
            }
            function handle_error(error) {
                messagesModel.append({
                    "direction" :  "*** ",
                    "nick" :  "ERROR",
                    "message" : error
                })
            }

            function handle_notifications(list) {
                list.forEach(function(notification) {
                    var item = {}
                    item["nick"] = notification.split(": ")[0]
                    item["message"] = notification.split(": ")[1]
                    item["direction"] =  "▶ "
                    messagesModel.append(item)
                })
            }
        }
    }
}