c7f4ff25d98204cb28ddc40834b2b52c78b10ea6
[apps/mediaplayer.git] / app / api / MediaPlayer.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: "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
36
37     property bool loop_state: false
38     property bool running: false
39
40     readonly property var msgid: {
41         "call": 2,
42         "retok": 3,
43         "reterr": 4,
44         "event": 5
45     }
46
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))
53         switch (json[0]) {
54             case msgid.call:
55                 break
56             case msgid.retok:
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
62
63                     for (var i = 0; i < media.length; i++) {
64                         var item = media[i]
65                         playlist.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' })
66                         if (item.selected) {
67                             playlistview.currentIndex = i
68                         }
69                     }
70                 } else if (verb == "controls") {
71                     if (response) {
72                         root.running = response.playing
73                     }
74                 } else if (verb == "metadata") {
75                     root.title = response.title ? response.title : ''
76                     root.artist = response.artist ? response.artist : ''
77
78                     root.cover_art = response.image ? response.image : ''
79                 }
80                 break
81             case msgid.reterr:
82                 root.statusString = "Bad return value, binding probably not installed"
83                 var verb = verbs.shift()
84                 break
85             case msgid.event:
86                 var payload = JSON.parse(JSON.stringify(json[2]))
87                 var event = payload.event
88                 if (event == "mediaplayer/playlist") {
89                     console.debug("Media playlist is updated")
90                     var media = json[2].data.list
91
92                     if (!root.running) {
93                         root.clearPlaylist()
94                     }
95
96                     playlist.clear()
97
98                     for (var i = 0; i < media.length; i++) {
99                         var item = media[i]
100
101                         playlist.append({ "index": item.index, "artist": item.artist ? item.artist : '', "title": item.title ? item.title : '' })
102                         if (item.selected) {
103                             playlistview.currentIndex = i
104                         }
105                     }
106
107                 } else if (event == "mediaplayer/metadata") {
108                     var data = json[2].data
109
110                     if (data.status == "stopped") {
111                         root.running = false
112                         root.clearPlaylist()
113                         break
114                     }
115
116                     root.running = true
117                     root.position = data.position
118                     root.duration = data.duration
119
120                     playlistview.currentIndex = data.index
121
122                     if (playlistview.currentIndex != root.old_index) {
123                         sendSocketMessage("metadata", 'None')
124                         root.old_index = data.index
125                     }
126
127                     root.title = data.title ? data.title : ''
128                     root.artist = data.artist ? data.artist : ''
129                 }
130                 break
131         }
132     }
133
134     onStatusChanged: {
135         switch (status) {
136             case WebSocket.Open:
137             console.debug("onStatusChanged: Open")
138             sendSocketMessage("subscribe", { value: "metadata" })
139             sendSocketMessage("playlist", 'None')
140             sendSocketMessage("subscribe", { value: "playlist" })
141             sendSocketMessage("metadata", 'None')
142             break
143             case WebSocket.Error:
144             root.statusString = "WebSocket error: " + root.errorString
145             break
146         }
147     }
148
149     function sendSocketMessage(verb, parameter) {
150         var requestJson = [ msgid.call, payloadLength, apiString + '/'
151         + verb, parameter ]
152         console.debug("sendSocketMessage: " + JSON.stringify(requestJson))
153         verbs.push(verb)
154         sendTextMessage(JSON.stringify(requestJson))
155     }
156
157     function loop(value) {
158         sendSocketMessage("controls", { "value": "loop", "state": value })
159         root.loop_state = value
160     }
161
162     function next() {
163         sendSocketMessage("controls", { "value": "next" })
164     }
165
166     function previous() {
167         sendSocketMessage("controls", { "value": "previous" })
168     }
169
170     function play() {
171         sendSocketMessage("controls", { "value": "play" })
172     }
173
174     function pause() {
175         sendSocketMessage("controls", { "value": "pause" })
176     }
177
178     function pick_track(index) {
179         sendSocketMessage("controls", { "value": "pick-track", "index": index })
180     }
181
182     function seek(milliseconds) {
183         sendSocketMessage("controls", { "value": "seek", "position": milliseconds })
184     }
185
186     function stop() {
187         sendSocketMessage("controls", { "value": "stop" })
188     }
189
190     function clearPlaylist() {
191         root.position = ''
192         root.duration = ''
193         root.title = ''
194         root.artist = ''
195         root.cover_art = ''
196         root.old_index = -1
197         playlistview.currentIndex = -1
198     }
199
200     function time2str(value) {
201         return Qt.formatTime(new Date(value), 'mm:ss')
202     }
203 }