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