2 * Copyright (C) 2016 The Qt Company Ltd.
3 * Copyright (C) 2017 Konsulko Group
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import QtWebSockets 1.0
26 property string statusString: "waiting..."
27 property string apiString: "mediascanner"
28 property var verbs: []
29 property var items: []
30 property string payloadLength: "9999"
32 readonly property var msgid: {
39 function validateItem(media) {
40 for (var i = 0; i < media.length; i++) {
42 if (root.items.indexOf(item) < 0) {
43 playlist.addItem(item)
49 onTextMessageReceived: {
50 var json = JSON.parse(message)
51 console.debug("Raw response: " + message)
52 var request = json[2].request
53 var response = json[2].response
54 console.debug("response: " + JSON.stringify(response))
59 root.statusString = request.status
60 var verb = verbs.shift()
61 if (verb == "media_result") {
62 console.debug("Media result returned")
63 validateItem(response.Media)
67 root.statusString = "Bad return value, binding probably not installed"
70 var payload = JSON.parse(JSON.stringify(json[2]))
71 var event = payload.event
72 if (event == "mediascanner/media_added") {
73 console.debug("Media is inserted")
74 validateItem(json[2].data.Media)
75 } else if (event == "mediascanner/media_removed") {
77 console.debug("Media is removed")
80 for (var i = 0; i < root.items.length; i++) {
81 if (root.items[i].startsWith(json[2].data.Path)) {
82 playlist.removeItem(i - removed++)
85 root.items = root.items.filter(function (item) { return !item.startsWith(json[2].data.Path) })
94 console.debug("onStatusChanged: Open")
95 sendSocketMessage("subscribe", { value: "media_added" })
96 sendSocketMessage("subscribe", { value: "media_removed" })
97 root.populateMediaPlaylist()
100 root.statusString = "WebSocket error: " + root.errorString
105 function sendSocketMessage(verb, parameter) {
106 var requestJson = [ msgid.call, payloadLength, apiString + '/'
108 console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
110 sendTextMessage(JSON.stringify(requestJson))
113 function populateMediaPlaylist() {
114 sendSocketMessage("media_result", 'None')