2 * Copyright (C) 2018 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 string payloadLength: "9999"
31 readonly property var msgid: {
38 signal added(var media)
39 signal removed(var index)
41 property var cache: []
43 for (var i = 0; i < files.length; i++) {
46 if (cache.indexOf(media.path) < 0) {
48 cache.push(media.path)
53 function remove(prefix) {
54 for (var i = cache.length - 1; i > -1; i--) {
56 if (media.substr(0, prefix.length) === prefix) {
63 onTextMessageReceived: {
64 console.debug("Raw response: " + message)
65 var json = JSON.parse(message)
66 var request = json[2].request
67 var response = json[2].response
68 // console.debug("response: " + JSON.stringify(response))
73 root.statusString = request.status
74 var verb = verbs.shift()
75 if (verb === "media_result") {
76 root.add(response.Media)
80 root.statusString = "Bad return value, binding probably not installed"
81 var verb = verbs.shift()
84 var payload = JSON.parse(JSON.stringify(json[2]))
85 var event = payload.event
86 if (event === "mediascanner/media_added") {
87 console.debug("Media playlist is updated")
88 root.add(json[2].data.Media)
89 } else if (event === "mediascanner/media_removed") {
90 root.remove(json[2].data.Path)
99 console.debug("onStatusChanged: Open")
100 sendSocketMessage("subscribe", { value: "media_added" })
101 sendSocketMessage("subscribe", { value: "media_removed" })
102 sendSocketMessage("media_result", { type: 'video' })
104 case WebSocket.Error:
105 root.statusString = "WebSocket error: " + root.errorString
110 function sendSocketMessage(verb, parameter) {
111 var requestJson = [ msgid.call, payloadLength, apiString + '/'
113 console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
115 sendTextMessage(JSON.stringify(requestJson))