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: "mediaplayer"
28 property var verbs: []
29 property string payloadLength: "9999"
30 property string cover_art: ""
31 property string title: ""
32 property string artist: ""
33 property int duration: 0
34 property int position: 0
35 property int old_index: -1
37 property bool loop_state: false
38 property bool running: false
40 readonly property var msgid: {
47 onTextMessageReceived: {
48 var json = JSON.parse(message)
49 console.debug("Raw response: " + message)
50 var request = json[2].request
51 var response = json[2].response
52 console.debug("response: " + JSON.stringify(response))
57 root.statusString = request.status
58 var verb = verbs.shift()
59 if (verb == "playlist") {
60 console.debug("Media result returned")
61 var media = response.list
63 for (var i = 0; i < media.length; i++) {
65 playlist.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' })
67 playlistview.currentIndex = i
70 } else if (verb == "controls") {
72 root.running = response.playing
74 } else if (verb == "metadata") {
75 root.cover_art = response.image ? response.image : ''
79 root.statusString = "Bad return value, binding probably not installed"
80 var verb = verbs.shift()
83 var payload = JSON.parse(JSON.stringify(json[2]))
84 var event = payload.event
85 if (event == "mediaplayer/playlist") {
86 console.debug("Media playlist is updated")
87 var media = json[2].data.list
95 for (var i = 0; i < media.length; i++) {
98 playlist.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' })
100 playlistview.currentIndex = i
104 } else if (event == "mediaplayer/metadata") {
105 var data = json[2].data
107 if (data.status == "stopped") {
114 root.position = data.position
115 root.duration = data.duration
117 playlistview.currentIndex = data.index
119 if (playlistview.currentIndex != root.old_index) {
120 sendSocketMessage("metadata", 'None')
121 root.old_index = data.index
124 root.title = data.title ? data.title : ''
125 root.artist = data.artist ? data.artist : ''
134 console.debug("onStatusChanged: Open")
135 sendSocketMessage("subscribe", { value: "metadata" })
136 sendSocketMessage("playlist", 'None')
137 sendSocketMessage("subscribe", { value: "playlist" })
139 case WebSocket.Error:
140 root.statusString = "WebSocket error: " + root.errorString
145 function sendSocketMessage(verb, parameter) {
146 var requestJson = [ msgid.call, payloadLength, apiString + '/'
148 console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
150 sendTextMessage(JSON.stringify(requestJson))
153 function loop(value) {
154 sendSocketMessage("controls", { "value": "loop", "state": value })
155 root.loop_state = value
159 sendSocketMessage("controls", { "value": "next" })
162 function previous() {
163 sendSocketMessage("controls", { "value": "previous" })
167 sendSocketMessage("controls", { "value": "play" })
171 sendSocketMessage("controls", { "value": "pause" })
174 function pick_track(index) {
175 sendSocketMessage("controls", { "value": "pick-track", "index": index })
178 function seek(milliseconds) {
179 sendSocketMessage("controls", { "value": "seek", "position": milliseconds })
183 sendSocketMessage("controls", { "value": "stop" })
186 function clearPlaylist() {
193 playlistview.currentIndex = -1
196 function time2str(value) {
197 return Qt.formatTime(new Date(value), 'mm:ss')