eded2d04100bda246e106193d0ad9aeadde05f5c
[staging/xdg-launcher.git] / examples / hybrid-qml / xxxxxx-hybrid-qml-app.qml
1 import QtQuick 2.0
2 import QtQuick.Window 2.0
3 import QtQuick.Controls 1.4
4 import QtWebSockets 1.0
5
6 Window {
7          // VARIABLES
8
9         property string port_str: Qt.application.arguments[1]
10         property string token_str: Qt.application.arguments[2]
11         property string address_str: "ws://localhost:"+port_str+"/api?token="+token_str
12         property string request_str: ""
13         property string status_str: "waiting..."
14         property var msgid_enu: { "call":2, "retok":3, "reterr":4, "event":5 }
15
16          // WINDOW PROPERTIES
17
18         visible: true
19         width: 340
20         height: 160
21
22          // WEBSOCKET WIDGET (MAIN LOGIC)
23
24         WebSocket {
25                 id: websocket
26                 url: address_str
27                 onTextMessageReceived: {
28                          // VERB RESPONSE VALIDATION
29                         var message_json = JSON.parse (message)
30                         var request_json = message_json[2].request
31                         if (message_json[0] != msgid_enu.retok) {
32                                 console.log ("Return value is not ok !")
33                                 status_str = "Bad return value, binding probably not installed"
34                                 return
35                         }
36                          // VERB RESPONSE PARSING AND LOGIC
37                         status_str = request_json.info
38                 }
39                 onStatusChanged: {
40                         if (websocket.status == WebSocket.Error)
41                                 status_str = "WebSocket error: " + websocket.errorString
42                 }
43                 active: true
44         }
45
46          // OTHER WIDGETS
47
48         Rectangle {
49                 anchors.left: parent.left
50                 anchors.top: parent.top
51                 anchors.horizontalCenter: parent.horizontalCenter
52                 anchors.margins: 20
53
54                  // TITLE SECTION
55                 Label {
56                         text: "QML Websocket Sample Application"
57                         font.pixelSize: 18
58                         font.bold: true
59                         anchors.centerIn: parent
60                         y: 0
61                 }
62                 Text {
63                         id: url_notifier
64                         text: "<b>URL:</b> " + websocket.url
65                         y: 20
66                 }
67
68                  // PING BUTTON
69                 Button {
70                         text: "Ping!"
71                         onClicked: {
72                                 request_str = '[' + msgid_enu.call + ',"99999","xxxxxx/ping", null ]';
73                                 websocket.sendTextMessage (request_str)
74                         }
75                         anchors.horizontalCenter: parent.horizontalCenter
76                         y: 60
77                 }
78
79                  // STATUS SECTION
80                 Text {
81                         id: status_notifier
82                         text: "<b>Status</b>: " + status_str
83                         y: 100
84                 }
85         }
86
87 }