binding: remove non-binding lightmediascanner detection
[apps/mediaplayer.git] / app / api / LightMediaScanner.qml
1 /*
2  * Copyright (C) 2016 The Qt Company Ltd.
3  * Copyright (C) 2017 Konsulko Group
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 import QtQuick 2.6
19 import QtWebSockets 1.0
20
21 WebSocket {
22     id: root
23     active: true
24     url: bindingAddress
25
26     property string statusString: "waiting..."
27     property string apiString: "media-manager"
28     property var verbs: []
29     property string payloadLength: "9999"
30
31     readonly property var msgid: {
32         "call": 2,
33         "retok": 3,
34         "reterr": 4,
35         "event": 5
36     }
37
38     onTextMessageReceived: {
39         var json = JSON.parse(message)
40         console.debug("Raw response: " + message)
41         var request = json[2].request
42         var response = json[2].response
43         console.debug("response: " + JSON.stringify(response))
44         switch (json[0]) {
45             case msgid.call:
46                 break
47             case msgid.retok:
48                 root.statusString = request.status
49                 var verb = verbs.shift()
50                 if (verb == "media_result") {
51                     console.debug("Media result returned")
52                     playlist.addItems(response.Media)
53                 }
54                 break
55             case msgid.reterr:
56                 root.statusString = "Bad return value, binding probably not installed"
57                 break
58             case msgid.event:
59                 var payload = JSON.parse(JSON.stringify(json[2]))
60                 var event = payload.event
61                 if (event == "media-manager/media_added") {
62                     console.debug("Media is inserted")
63                     playlist.addItems(json[2].data.Media)
64                 } else if (event == "media-manager/media_removed") {
65                     console.debug("Media is removed")
66                     player.stop()
67                     playlist.clear()
68                 }
69                 break
70         }
71     }
72
73     onStatusChanged: {
74         switch (status) {
75             case WebSocket.Open:
76             console.debug("onStatusChanged: Open")
77             sendSocketMessage("subscribe", { value: "media_added" })
78             sendSocketMessage("subscribe", { value: "media_removed" })
79             root.populateMediaPlaylist()
80             break
81             case WebSocket.Error:
82             root.statusString = "WebSocket error: " + root.errorString
83             break
84         }
85     }
86
87     function sendSocketMessage(verb, parameter) {
88         var requestJson = [ msgid.call, payloadLength, apiString + '/'
89         + verb, parameter ]
90         console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
91         verbs.push(verb)
92         sendTextMessage(JSON.stringify(requestJson))
93     }
94
95     function populateMediaPlaylist() {
96         sendSocketMessage("media_result", 'None')
97     }
98 }