~danielholm/musicapp/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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyleft Daniel Holm.
 *
 * Authors:
 *  Daniel Holm <d.holmen@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 QtQuick 2.0
import Ubuntu.Components 0.1
import Ubuntu.Components.ListItems 0.1
import Ubuntu.Components.Popups 0.1
import Ubuntu.Components.ListItems 0.1 as ListItem
import Qt.labs.folderlistmodel 1.0
import QtMultimedia 5.0
import QtQuick.LocalStorage 2.0
import "settings.js" as Settings
import "meta-database.js" as MetaDatabase

MainView {
    objectName: i18n.tr("mainView")
    applicationName: "Music player"

    width: units.gu(50)
    height: units.gu(75)

    // VARIABLES
    property string musicName: i18n.tr("Music")
    property string musicDir: ""
    property string trackStatus: "stopped"
    property string appVersion: '0.4.9.1'

    // FUNCTIONS

    // digg deeper in the music folder
    function subDirCheck(directory) {
        // populate listmodel with meta data of tracks
    }

    // What is the state of the playback?
    function stateChange() {
        // state was stopped (0)
        if (playMusic.playbackState == "0") {
            console.debug("Debug: Music was stopped. Playing")
            beenStopped() // run stop function
            playMusic.play() // then play
        }

        // if state was playing (1)
        else if (playMusic.playbackState == "1") {
            console.debug("Debug: Track was playing. Pause")
            playMusic.pause() // pause the music then
        }

        // if state is paused (2)
        else if (playMusic.playbackState == "2") {
            console.debug("Debug: Track was paused. Playing")
            playMusic.play() // resume then
        }

        updateUI()
    }

    // stop function
    function beenStopped() {
        console.debug("Debug: has a track been played before or did I just start?")

        // track was just paused or stopped. Resume previous track
        if (playMusic.source != "") {
            console.debug("Debug: Resume previous song: "+playMusic.source)

            updateUI()
        }

        // app just started, play random
        else {
            playMusic.source = "/home/daniel/Musik/Cyndi Lauper - 80s music - Time After Time.mp3"
            //playMusic.source = musicDir+folderModel.get(0).file // this should play a dynamic track
            //console.debug("Debug: First song is"+folderModel.get(0).file)
            console.debug("Debug: I was just started. Play random track: "+playMusic.source)

            updateUI()
        }
    }

    // previous and next track function
    function nextTrack() {
        //check if any queued songs
        // if there are, play them
        if (trackQueue > 1) {
            console.debug() // print next songs filename
            playMusic.source = musicDir+trackQueue.get(0).file
            playMusic.play()
            removedTrackQueue.append({"title": trackQueue.get(0).title, "artist": trackQueue.get(0).artist, "file": trackQueue.get(0).file}) // move the track to a list of preious tracks
            trackQueue.remove(index) // remove the track from queue

            updateUI()
        }

        // if not, play another
        else {

            // if shuffle, play random
            if (settings.shuffle == 1) {
                updateUI()
            }

            // if not shuffle, play next
            else {
                console.debug() // print next songs filename
                playMusic.source = musicDir+trackQueue.get(0).file
                playMusic.play()
                removedTrackQueue.append({"title": trackQueue.get(0).title, "artist": trackQueue.get(0).artist, "file": trackQueue.get(0).file}) // move the track to a list of preious tracks
                trackQueue.remove(index) // remove the track from queue

                updateUI()
            }
        }
    }

    function previousTrack() {
        console.debug("Debug: Previous track was "+musicDir+removedTrackQueue.get(removedTrackQueue.count).file)
        // play the previous track
        playMusic.source = musicDir+removedTrackQueue.get(removedTrackQueue.count).file
        playMusic.play()

        updateUI()
    }

    // Get song title
    function getTrackInfo(source, type) {
        console.debug('Debug: Got it. Trying to get meta data from: '+source) // debug
        musicInfo.source = source
        musicInfo.pause()

        // if title
        if (type == "title") {
            return musicInfo.metaData.title
        }

        // if artist
        else if (type == "artist") {
            return musicInfo.metaData.albumArtist
        }

        // if album
        else if (type == "album") {
            return musicInfo.metaData.albumTitle
        }

        // year
        else if (type == "year") {
            return musicInfo.metaData.year
        }

        // cover
        else if (type == "cover") {
            // download cover art from last.fm
            // save the file in a app dir in HOME
            // return the value of the cover art file
        }

        // tracknr
        else if (type == "tracknr") {
        }

        // lenght
        else if (type == "length") {
        }
    }

    // add track to database
    function addToDatabase(track) {
        // get the needed info of track
        title = getTrackInfo(track, title) // title
        artist = getTrackInfo(track, artist) // artist
        album = getTrackInfo(track, album) // album
        cover = getTrackInfo(track, cover) // cover
        year = getTrackInfo(track, year) // year of album relase
        //tracknr =
        //length =

        // push to database
        MetaDatabase.setMetadata(track, title, artist, album, cover, year, tracknr, length)
    }

    // progressbar
    function setProgressbar() {
        console.debug("Debug: change progressvalue to "+playMusic.duration)
        trackProgress.maximumValue = playMusic.duration
    }

    // update the UI things with meta data from track
    function updateUI() {
        playinTab.title = playMusic.metaData.title // show track title in tab header
        playTrack.iconSource = Qt.resolvedUrl("images/icon_pause@20.png") // change toolbar icon
        playTrack.text = i18n.tr("Pause") // change toolbar text
        trackInfo.text = playMusic.metaData.albumArtist+" - "+playMusic.metaData.title // show track meta data
//        coverArt.source = "HOMEDIR/.musicapp/coverart/"+MetaDatabase.getMetadata(playMusic.source,"cover") // get cover art filename

        setProgressbar() // set progressbar

        console.debug("Debug: UI updated.")
    }

    // Music stuff
    Audio {
        id: playMusic
        source: ""
        /*
        onStatusChanged: {
             if (status === Audio.EndOfMedia || 7 ) {
                 console.log("Debug: Track ended. Play next.") //debug
                 //nextTrack()
             }
             else {
                  console.log("the Music Players status = " + playMusic.status)
            }
        }*/
    }

    // while playing
    Connections {

        target: playMusic
        onPlaying: {
            trackProgress.value = playMusic.position
            console.debug("Debug: change position to "+playMusic.position)
        }     
    }

    // get file meta data
    Audio {
        id: musicInfo
        source: ""
        volume: 0.0
        //Keys.onSpacePressed: stateChange()
    }

    // list of tracks on startup. This is just during development
    ListModel {
        id: trackQueue
        ListElement {
            title: "Dancing in the Moonlight"
            artist: "Thin Lizzy"
            file: "something"
        }
        ListElement {
            title: "Rock and Roll"
            artist: "Led Zeppelin"
            file: "something else"
        }
        ListElement {
            title: "Moonlight Serenade"
            artist: "Frank Sinatra"
            file: "something completely else"
        }
    }

    // list of songs, which has been removed.
    ListModel {
        id: removedTrackQueue
        ListElement {
            title: "Dancing in the Moonlight"
            artist: "Thin Lizzy"
            file: "dancing"
        }
    }

    // list of single tracks
    ListModel {
        id: singleTracks
    }

    // reusable toolbar
    ToolbarActions {
        id: defaultToolbar

        // Share
        Action {
            id: shareTrack
            objectName: "share"

            iconSource: Qt.resolvedUrl("images/icon_share@20.png")
            text: i18n.tr("Share")

            onTriggered: {
                console.debug('Debug: Share pressed')
                // just to debug other stuff for a while
                console.debug("Debug: change progress to "+playMusic.position)
                trackProgress.value = playMusic.position
            }
        }

        // prevous track
        Action {
            id: prevTrack
            objectName: "prev"

            iconSource: Qt.resolvedUrl("images/icon_prev@20.png")
            text: i18n.tr("Previous")

            onTriggered: {
                console.debug('Debug: Prev track pressed')
                //console.debug(musicDir+removedTrackQueue.get(0).file) // print next songs filename
                //playMusic.source = musicDir+removedTrackQueue.get(0).file
                //playMusic.play()
                previousTrack()
            }
        }

        // Play
        Action {
            id: playTrack
            objectName: "play"

            iconSource: Qt.resolvedUrl("images/icon_play@20.png")
            text: i18n.tr("Play")

            onTriggered: {
                console.debug("Debug: "+trackStatus+" pressed in toolbar.")
                console.debug(playMusic.playbackState)
                stateChange()
            }
        }

        // Next track
        Action {
            id: nextTrack
            objectName: "next"

            iconSource: Qt.resolvedUrl("images/icon_next@20.png")
            text: i18n.tr("Next")

            onTriggered: {
                console.debug('Debug: next track pressed')
                console.debug(musicDir+trackQueue.get(0).file) // print next songs filename
                playMusic.source = musicDir+trackQueue.get(0).file
                playMusic.play()

                // move track, which has been played from queue, to old queue so that prev button works
                removedTrackQueue.append({"title": trackQueue.get(0).title, "artist": trackQueue.get(0).artist, "file": trackQueue.get(0).file})
                trackQueue.remove(0)
            }
        }

        // Queue
        Action {
            id: trackQueueAction
            objectName: "queuelist"
            iconSource: Qt.resolvedUrl("images/icon_settings@20.png")
            text: i18n.tr("Queue")
            onTriggered: {
                PopupUtils.open(Qt.resolvedUrl("QueueDialog.qml"), pageLayout,
                            {
                                title: i18n.tr("Queue")
                            } )
            }
        }

        // Settings
        Action {
            id: settingsAction
            objectName: "settings"

            iconSource: Qt.resolvedUrl("images/icon_settings@20.png")
            text: i18n.tr("Settings")

            onTriggered: {
                console.debug('Debug: Settings pressed')
                // show settings page (not yet implemented)
                //dialog
                //Settings.initialize()
                //musicDirField.text = Settings.getSetting("currentfolder")
                PopupUtils.open(Qt.resolvedUrl("MusicSettings.qml"), pageLayout,
                            {
                                title: i18n.tr("Settings")
                            } )
            }
        }
    }

    Tabs {
        id: tabs
        anchors.fill: parent

        // First tab begins here - should be the primary tab
        Tab {
            id: playinTab
            objectName: "Tab1"

            title: musicName

            // Tab content begins here
            page: Page {
                id: playingPage

                // toolbar
                tools: defaultToolbar

                Column {
                        id: pageLayout

                        spacing: units.gu(1)

                        Column {
                            spacing: units.gu(1)
                            // Album cover here
                            UbuntuShape {
                                id: trackCoverArt
                                width: units.gu(50)
                                height: units.gu(50)
                                gradientColor: "blue" // test
                                image: Image {
                                    id: coverArt
                                    source: "images/music.png"
                                }
                            }
                            /*onClicked: {
                                playMusic.pause()
                            }
                            onDoubbleTap: {
                                nextTrack()
                            }
                            onPressAndHold: {
                                playMusic.stop()
                            }*/
                        }

                        // track progress
                        Column {
                            width: units.gu(50)
                            ProgressBar {
                                id: trackProgress
                                minimumValue: 0
                                maximumValue: 100
                                value: 0
                            }
                        }

                        // Track info
                        Column {
                            spacing: units.gu(1)
                            Label {
                                id: trackInfo
                                text: "Stopped. Press play."
                                //subText: "Artist: + Year:"
                            }
                        }
                    }


            }
        }


        // Second tab begins here - artists here
        Tab {
            objectName: "Artists Tab"

            title: i18n.tr("Artists")
            page: Page {
                anchors.margins: units.gu(2)

            // foreach artist:
            ListItem.Standard {
                height: units.gu(4)
                // when pressed on this row, change to albums of artist
                Row {
                    Label {
                        text: i18n.tr("Artist 1")
                    }
                }
            }

            // toolbar
            tools: defaultToolbar

            Column {
                anchors.centerIn: parent
                anchors.fill: parent
            }
            }
        }

        // Third tab begins here - albums here
        Tab {
            objectName: "Albums Tab"

            title: i18n.tr("Albums")
            page: Page {
                anchors.margins: units.gu(2)

                Column {
                    anchors.centerIn: parent
                    anchors.fill: parent
                }

                // toolbar
                tools: defaultToolbar

                Column {
                    anchors.centerIn: parent
                }
            }
        }

        // Fourth tab - tracks in ~/Music
        Tab {
            objectName: "Tracks Tab"

            title: i18n.tr("Tracks")
            page: MusicTracks { id: musicTracksPage }
            /*
            page: Page {
                anchors.margins: units.gu(2)

            // toolbar
            tools: defaultToolbar

                Column {
                    anchors.centerIn: parent
                    anchors.fill: parent
                    ListView {
                        id: musicFolder
                        width: parent.width
                        height: parent.height
                        model: folderModel
                        delegate: ListItem.Subtitled {
                            text: fileName
                            subText: "Artist: "
                            onClicked: {
                                playMusic.source = musicDir+fileName
                                playMusic.play()
                                console.debug('Debug: User pressed '+musicDir+fileName)
                                // run UI changes
                                // cool animation
                            }
                            onPressAndHold: {
                                console.debug('Debug: '+fileName+' added to queue.')
                                trackQueue.append({"title": playMusic.metaData.title, "artist": playMusic.metaData.albumArtist, "file": fileName})
                                // cool animation
                            }
                        }
                    }
                }
            }*/
        } // 4th tab

    } // tabs
} // main view