fix(MediaScanner.qml): Remove qml M126 warning
[apps/videoplayer.git] / app / api / MediaScanner.qml
1 /*
2  * Copyright (C) 2018 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: "mediascanner"
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     signal added(var media)
39     signal removed(var index)
40
41     property var cache: []
42     function add(files) {
43         for (var i = 0; i < files.length; i++) {
44             var media = files[i]
45
46             if (cache.indexOf(media.path) < 0) {
47                 root.added(media)
48                 cache.push(media.path)
49             }
50         }
51     }
52
53     function remove(prefix) {
54         for (var i = cache.length - 1; i > -1; i--) {
55             var media = cache[i]
56             if (media.substr(0, prefix.length) === prefix) {
57                 root.removed(i)
58                 cache.splice(i, 1)
59             }
60         }
61     }
62
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))
69         switch (json[0]) {
70             case msgid.call:
71                 break
72             case msgid.retok:
73                 root.statusString = request.status
74                 var verb = verbs.shift()
75                 if (verb === "media_result") {
76                     root.add(response.Media)
77                 }
78                 break
79             case msgid.reterr:
80                 root.statusString = "Bad return value, binding probably not installed"
81                 var verb = verbs.shift()
82                 break
83             case msgid.event:
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)
91                 }
92                 break
93         }
94     }
95
96     onStatusChanged: {
97         switch (status) {
98         case WebSocket.Open:
99             console.debug("onStatusChanged: Open")
100             sendSocketMessage("subscribe", { value: "media_added" })
101             sendSocketMessage("subscribe", { value: "media_removed" })
102             sendSocketMessage("media_result", { type: 'video' })
103             break
104         case WebSocket.Error:
105             root.statusString = "WebSocket error: " + root.errorString
106             break
107         }
108     }
109
110     function sendSocketMessage(verb, parameter) {
111         var requestJson = [ msgid.call, payloadLength, apiString + '/'
112         + verb, parameter ]
113         console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
114         verbs.push(verb)
115         sendTextMessage(JSON.stringify(requestJson))
116     }
117 }