~music-app-dev/music-app/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Copyright (C) 2015, 2016
 *      Andrew Hayzen <ahayzen@gmail.com>
 *      Victor Thompson <victor.thompson@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtMultimedia 5.6
import QtQuick 2.4
import Qt.labs.settings 1.0

import QtQuick.LocalStorage 2.0
import "../logic/meta-database.js" as Library

Item {
    objectName: "player"

    // For autopilot as we can't access the MediaPlayer object pad.lv/1269578
    readonly property bool isPlaying: mediaPlayerObject.playbackState === MediaPlayer.PlayingState
    readonly property alias count: mediaPlayerPlaylist.itemCount
    readonly property alias currentIndex: mediaPlayerPlaylist.currentIndex
    readonly property alias currentItemSource: mediaPlayerPlaylist.currentItemSource
    readonly property alias position: mediaPlayerObject.position

    // FIXME: pad.lv/1269578 use Item as autopilot cannot 'see' a var/QtObject
    property alias currentMeta: currentMetaItem

    property alias mediaPlayer: mediaPlayerObject
    property alias repeat: settings.repeat
    property alias shuffle: settings.shuffle

    Item {
        id: currentMetaItem
        objectName: "currentMeta"

        property string album: ""
        property string art: ""
        property string author: ""
        property string filename: ""
        property string title: ""
        property bool useFallbackArt: false
    }

    // Return the metadata for the source given from mediascanner2
    function metaForSource(source) {
        var blankMeta = {
            album: "",
            art: "",
            author: "",
            filename: "",
            title: ""
        };

        source = source.toString();

        if (source.indexOf("file://") === 0) {
            source = source.substring(7);
        }

        return musicStore.lookup(decodeFileURI(source)) || blankMeta;
    }

    Settings {
        id: settings
        category: "PlayerSettings"

        property bool repeat: true
        property bool shuffle: false
    }

    MediaPlayer {
        id: mediaPlayerObject
        playlist: Playlist {
            id: mediaPlayerPlaylist
            playbackMode: {
                if (settings.shuffle) {
                    Playlist.Random
                } else if (settings.repeat) {
                    Playlist.Loop
                } else {
                    Playlist.Sequential
                }
            }

            // as that doesn't emit changes
            readonly property bool canGoPrevious: {  // FIXME: pad.lv/1517580 use previousIndex() > -1 after mh implements it
                currentIndex !== 0 ||
                settings.repeat ||
                settings.shuffle ||  // FIXME: pad.lv/1517580 no way to know when we are at the end of a shuffle yet
                mediaPlayerObject.position > 5000
            }
            readonly property bool canGoNext: {  // FIXME: pad.lv/1517580 use nextIndex() > -1 after mh implements it
                currentIndex !== (itemCount - 1) ||
                settings.repeat ||
                settings.shuffle  // FIXME: pad.lv/1517580 no way to know when we are at the end of a shuffle yet
            }
            readonly property int count: itemCount  // header actions etc depend on the model having 'count'
            readonly property bool empty: itemCount === 0
            property int pendingCurrentIndex: -1
            property var pendingCurrentState: null
            property int pendingShuffle: -1

            onCurrentItemSourceChanged: {
                var meta = metaForSource(currentItemSource);

                currentMeta.album = meta.album;
                currentMeta.art = meta.art;
                currentMeta.author = meta.author;
                currentMeta.filename = meta.filename;
                currentMeta.title = meta.title;
                currentMeta.useFallbackArt = false;

                mediaPlayerObject._calcProgress();
            }
            onItemChanged: {
                console.debug("*** Saving play queue in onItemChanged");
                saveQueue()
            }
            onItemInserted: {
                // When add to queue is done on an empty list currentIndex needs to be set
                if (start === 0 && currentIndex === -1 && pendingCurrentIndex < 1 && pendingShuffle === -1) {
                    currentIndex = 0;

                    pendingCurrentIndex = -1;
                    processPendingCurrentState();
                }

                // Check if the pendingCurrentIndex is now valid
                if (pendingCurrentIndex !== -1 && pendingCurrentIndex < itemCount) {
                    currentIndex = pendingCurrentIndex;

                    pendingCurrentIndex = -1;
                    processPendingCurrentState();
                }

                // Check if there is pending shuffle
                // pendingShuffle holds the expected size of the model
                if (pendingShuffle > -1 && pendingShuffle <= itemCount) {
                    pendingShuffle = -1;

                    nextWrapper();  // find a random track
                    mediaPlayerObject.play();  // next does not enforce play
                }

                console.debug("*** Saving play queue in onItemInserted");
                saveQueue()
            }
            onItemRemoved: {
                console.debug("*** Saving play queue in onItemRemoved");
                saveQueue()
            }

            function addItemsFromModel(model) {
                var items = []

                // TODO: remove once playlists uses U1DB
                if (model.hasOwnProperty("linkLibraryListModel")) {
                    model = model.linkLibraryListModel;
                }

                for (var i=0; i < model.rowCount; i++) {
                    items.push(Qt.resolvedUrl(model.get(i, model.RoleModelData).filename));
                }

                addItems(items);
            }

            // Wrap the clear() method because we need to call stop first
            function clearWrapper() {
                // Stop the current playback (this ensures that play is run later)
                if (mediaPlayerObject.playbackState === MediaPlayer.PlayingState) {
                    mediaPlayerObject.stop();
                }

                clear();
            }

            // Replicates a model.get() on a ms2 model
            function get(index, role) {
                return metaForSource(itemSource(index));
            }

            // Wrap the next() method so we can check canGoNext
            function nextWrapper() {
                if (canGoNext) {
                    next();
                }
            }

            // Wrap the previous() method so we can check canGoPrevious
            function previousWrapper() {
                if (canGoPrevious) {
                    previous();
                }
            }

            // Process the pending current PlaybackState
            function processPendingCurrentState() {
                if (pendingCurrentState === MediaPlayer.PlayingState) {
                    console.debug("Loading pending state play()");
                    mediaPlayerObject.play();
                } else if (pendingCurrentState === MediaPlayer.PausedState) {
                    console.debug("Loading pending state pause()");
                    mediaPlayerObject.pause();
                } else if (pendingCurrentState === MediaPlayer.StoppedState) {
                    console.debug("Loading pending state stop()");
                    mediaPlayerObject.stop();
                }

                pendingCurrentState = null;
            }

            // Wrapper for removeItems(from, to) so that we can use removeItems(list) until it is implemented upstream
            function removeItemsWrapper(items) {
                var previous = -1, end = -1;

                // Sort indexes backwards so we don't have to deal with offsets when removing
                items.sort(function(a,b) { return b-a; });

                console.debug("To Remove", JSON.stringify(items));

                // Merge ranges of indexes into sets of start, end points
                // and call removeItems as we go along
                for (var i=0; i < items.length; i++) {
                    if (end == -1) {  // first value found set to first
                        end = items[i];
                    } else if (previous - 1 !== items[i]) {  // set has ended (next is not 1 lower)
                        console.debug("RemoveItems", previous, end);
                        player.mediaPlayer.playlist.removeItems(previous, end);

                        end = items[i];  // set new high value for the next set
                    }

                    previous = items[i];  // last value to check if next is 1 lower
                }

                // Remove last set in list as well
                if (items.length > 0) {
                    console.debug("RemoveItems", items[items.length - 1], end);
                    player.mediaPlayer.playlist.removeItems(items[items.length - 1], end);
                }
            }

            function saveQueue(start, end) {
                // FIXME: load and save do not work yet pad.lv/1510225
                // so use our localstorage method for now
                // save("/home/phablet/.local/share/com.ubuntu.music/queue.m3u");
                if (mainView.loadedUI) {
                    // Don't be intelligent, just clear and rebuild the queue for now
                    Library.clearQueue();

                    var sources = [];

                    for (var i=0; i < mediaPlayerPlaylist.itemCount; i++) {
                        sources.push(mediaPlayerPlaylist.itemSource(i));
                    }

                    if (sources.length > 0) {
                        Library.addQueueList(sources);
                    }
                }
            }

            function setCurrentIndex(index) {
                // Set the currentIndex but if the itemCount is too low then wait
                if (index < mediaPlayerPlaylist.itemCount) {
                    mediaPlayerPlaylist.currentIndex = index;
                } else {
                    pendingCurrentIndex = index;
                }
            }

            function setPendingCurrentState(pendingState) {
                // Set the PlaybackState to set once pendingCurrentIndex is set
                pendingCurrentState = pendingState;

                if (pendingCurrentIndex === -1) {
                    processPendingCurrentState();
                }
            }

            function setPendingShuffle(modelSize) {
                // Run next() and play() when the modelSize is reached
                if (modelSize <= itemCount) {
                    mediaPlayerPlaylist.nextWrapper();  // find a random track
                    mediaPlayerObject.play();  // next does not enforce play
                } else {
                    pendingShuffle = modelSize;
                }
            }
        }

        property bool endOfMedia: false
        property double progress: 0

        onDurationChanged: _calcProgress()
        onPositionChanged: _calcProgress()

        onStatusChanged: {
            if (status == MediaPlayer.EndOfMedia && !settings.repeat) {
                console.debug("End of media, stopping.")

                // Tells the onStopped to set the curentIndex = 0
                endOfMedia = true;

                stop();
            }
        }

        onStopped: {  // hit when pressing next() on last track with repeat off
            console.debug("onStopped.")

            // FIXME: Workaround for pad.lv/1494031 in the stopped state
            // we do not get position/duration info so if there are items in
            // the queue and we have stopped instead pause
            if (playlist.itemCount > 0) {
                // We have just ended media so jump to start of playlist
                if (endOfMedia) {
                    playlist.currentIndex = 0;

                    // Play then pause otherwise when we come from EndOfMedia
                    // if calls next() until EndOfMedia again
                    play();
                }

                pause();
            }

            endOfMedia = false;  // always reset endOfMedia
            _calcProgress();  // ensures progress bar has reset
        }

        function _calcProgress() {
            if (duration > 0) {
                progress = position / duration;
            } else if (position >= duration) {
                progress = 0;
            } else {
                progress = 0;
            }
        }

        function toggle() {
            if (playbackState === MediaPlayer.PlayingState) {
                pause();
            } else {
                play();
            }
        }
    }
}