~ahayzen/+junk/use-sdk-slider-label-by-var

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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
/*
 * Copyright (C) 2013 Andrew Hayzen <ahayzen@gmail.com>
 *                    Daniel Holm <d.holmen@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 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 Ubuntu.Unity.Action 1.0 as UnityActions
import org.nemomobile.grilo 0.1
import QtMultimedia 5.0
import QtQuick.LocalStorage 2.0
import QtQuick.XmlListModel 2.0
import QtGraphicalEffects 1.0
import UserMetrics 0.1
import "settings.js" as Settings
import "meta-database.js" as Library
import "scrobble.js" as Scrobble
import "playlists.js" as Playlists
import "common"

MainView {
    objectName: "music"
    applicationName: "com.ubuntu.music"
    id: mainView
    useDeprecatedToolbar: false

    // Use toolbar color for header
    headerColor: styleMusic.toolbar.fullBackgroundColor
    backgroundColor: styleMusic.toolbar.fullBackgroundColor

    // Global keyboard shortcuts
    focus: true
    Keys.onPressed: {
        if (event.key === Qt.Key_Alt) {
            // On alt key press show toolbar and start autohide timer
            musicToolbar.showToolbar();
        }
        else if(event.key === Qt.Key_Escape) {
            musicToolbar.goBack();  // Esc      Go back
        }
        else if(event.modifiers === Qt.AltModifier) {
            var position;

            switch (event.key) {
            case Qt.Key_Right:  //  Alt+Right   Seek forward +10secs
                position = player.position + 10000 < player.duration
                        ? player.position + 10000 : player.duration;
                player.seek(position);
                break;
            case Qt.Key_Left:  //   Alt+Left    Seek backwards -10secs
                position = player.position - 10000 > 0
                        ? player.position - 10000 : 0;
                player.seek(position);
                break;
            }
        }
        else if(event.modifiers === Qt.ControlModifier) {
            switch (event.key) {
            case Qt.Key_Left:   //  Ctrl+Left   Previous Song
                player.previousSong(true);
                break;
            case Qt.Key_Right:  //  Ctrl+Right  Next Song
                player.nextSong(true, true);
                break;
            case Qt.Key_Up:  //     Ctrl+Up     Volume up
                player.volume = player.volume + .1 > 1 ? 1 : player.volume + .1
                break;
            case Qt.Key_Down:  //   Ctrl+Down   Volume down
                player.volume = player.volume - .1 < 0 ? 0 : player.volume - .1
                break;
            case Qt.Key_R:  //      Ctrl+R      Repeat toggle
                player.repeat = !player.repeat
                break;
            case Qt.Key_F:  //      Ctrl+F      Show Search popup
                if (!searchSheet.sheetVisible) {
                    PopupUtils.open(searchSheet.sheet, mainView,
                                    { title: i18n.tr("Search") })
                }
                break;
            case Qt.Key_J:  //      Ctrl+J      Jump to playing song
                nowPlaying.visible = true;
                nowPlaying.positionAt(player.currentIndex);
                musicToolbar.showToolbar();
                break;
            case Qt.Key_N:  //      Ctrl+N      Show now playing
                nowPlaying.visible = true;
                musicToolbar.showToolbar();
                break;
            case Qt.Key_P:  //      Ctrl+P      Toggle playing state
                player.toggle();
                break;
            case Qt.Key_Q:  //      Ctrl+Q      Quit the app
                Qt.quit();
                break;
            case Qt.Key_U:  //      Ctrl+U      Shuffle toggle
                player.shuffle = !player.shuffle
                break;
            }
        }
    }

    // Arguments during startup
    Arguments {
        id: args
        //defaultArgument.help: "Expects URI of the track to play." // should be used when bug is resolved
        //defaultArgument.valueNames: ["URI"] // should be used when bug is resolved
        // grab a file
        Argument {
            name: "url"
            help: "URI for track to run at start."
            required: false
            valueNames: ["track"]
        }
        // Debug/development mode
        Argument {
            name: "debug"
            help: "Start Music in a debug mode. Will show more output."
            required: false
        }
    }

    // HUD Actions
    Action {
        id: searchAction
        text: i18n.tr("Search")
        keywords: i18n.tr("Search Track")
        onTriggered: {
            if (!searchSheet.sheetVisible) {
                PopupUtils.open(searchSheet.sheet, mainView,
                     {
                                         title: i18n.tr("Search")
                     } )
            }
        }
    }
    Action {
        id: nextAction
        text: i18n.tr("Next")
        keywords: i18n.tr("Next Track")
        onTriggered: player.nextSong()
    }
    Action {
        id: playsAction
        text: player.playbackState === MediaPlayer.PlayingState ?
                  i18n.tr("Pause") : i18n.tr("Play")
        keywords: player.playbackState === MediaPlayer.PlayingState ?
                      i18n.tr("Pause Playback") : i18n.tr("Continue or start playback")
        onTriggered: player.toggle()
    }
    Action {
        id: backAction
        text: i18n.tr("Back")
        keywords: i18n.tr("Go back to last page")
        onTriggered: musicToolbar.goBack();
    }

    // With a default Quit action only the first 4 actions are displayed
    // until the user searches for them within the HUD
    Action {
        id: prevAction
        text: i18n.tr("Previous")
        keywords: i18n.tr("Previous Track")
        onTriggered: player.previousSong()
    }
    Action {
        id: stopAction
        text: i18n.tr("Stop")
        keywords: i18n.tr("Stop Playback")
        onTriggered: player.stop()
    }

    // TODO: Currently there are no settings, so do not display the Action
    Action {
        id: settingsAction
        text: i18n.tr("Settings")
        keywords: i18n.tr("Music Settings")
        onTriggered: {
            customdebug('Show settings')
            musicSettings.visible = true
        }
    }

    actions: [searchAction, nextAction, playsAction, prevAction, stopAction, backAction]

    // signal to open new URIs
    // TODO currently this only allows playing file:// URIs of known files
    // (already in the database), not e.g. http:// URIs or files in directories
    // not picked up by Grilo
    Connections {
        id: uriHandler
        target: UriHandler

        function processAlbum(uri) {
            var split = uri.split("/");

            if (split.length < 2) {
                console.debug("Unknown artist-album " + uri + ", skipping")
                return;
            }

            // Get tracks
            var tracks = Library.getArtistAlbumTracks(decodeURIComponent(split[0]), decodeURIComponent(split[1]));

            if (tracks.length === 0) {
                console.debug("Unknown artist-album " + uri + ", skipping")
                return;
            }

            // Enqueue
            for (var track in tracks) {
                trackQueue.append(tracks[track]);
            }

            // Play first track
            trackClicked(trackQueue, 0, true);
        }

        function processFile(uri, play) {
            uri = decodeURIComponent(uri);

            // search for path in library
            var library = Library.getAll();
            var track = false;

            for (var item in library) {
                if (decodeURIComponent(library[item].file) === uri) {
                    track = library[item];
                    break;
                }
            }

            if (!track) {
                console.debug("Unknown file " + uri + ", skipping")
                return;
            }

            // enqueue
            trackQueue.append(track);

            // play first URI
            if (play) {
                trackClicked(trackQueue, 0, true)
            }
        }

        function process(uri, play) {
            if (uri.indexOf("album:///") === 0) {
                uriHandler.processAlbum(uri.substring(9));
            }
            else if (uri.indexOf("file://") === 0) {
                uriHandler.processFile(uri.substring(7), play);
            }
            else if (uri.indexOf("music://") === 0) {
                uriHandler.processFile(uri.substring(8), play);
            }

            else {
                console.debug("Unsupported URI " + uri + ", skipping")
            }
        }

        onOpened: {
            // clear play queue
            trackQueue.model.clear()
            for (var i=0; i < uris.length; i++) {
                console.debug("URI=" + uris[i])

                uriHandler.process(uris[i], i === 0);
            }
        }
    }

    // UserMetrics to show Music stuff on welcome screen
    Metric {
        id: songsMetric
        name: "music-metrics"
        // TRANSLATORS: this refers to a number of songs greater than one. The actual number will be prepended to the string automatically (plural forms are not yet fully supported in usermetrics, the library that displays that string)
        format: "<b>%1</b> " + i18n.tr("songs played today")
        emptyFormat: i18n.tr("No songs played today")
        domain: "com.ubuntu.music"
    }

    // Connections for usermetrics
    Connections {
        id: userMetricPlayerConnection
        target: player
        property bool songCounted: false

        onSourceChanged: {
            songCounted = false
        }

        onPositionChanged: {
            // Increment song count on Welcome screen if song has been
            // playing for over 10 seconds.
            if (player.position > 10000 && !songCounted) {
                songCounted = true
                songsMetric.increment()
                console.debug("Increment UserMetrics")
            }
        }
    }

    // Design stuff
    Style { id: styleMusic }
    width: units.gu(100)
    height: units.gu(80)

    // Run on startup
    Component.onCompleted: {
        customdebug("Version "+appVersion) // print the curren version
        customdebug("Arguments on startup: Debug: "+args.values.debug)

        customdebug("Arguments on startup: Debug: "+args.values.debug+ " and file: ")

        Settings.initialize()
        console.debug("INITIALIZED in tracks")
        if (Settings.getSetting("initialized") !== "true") {
            // initialize settings
            console.debug("reset settings")
            Settings.setSetting("initialized", "true") // setting to make sure the DB is there
            Settings.setSetting("snaptrack", "1") // default state of snaptrack
            Settings.setSetting("shuffle", "0") // default state of shuffle
            Settings.setSetting("repeat", "0") // default state of repeat
            //Settings.setSetting("scrobble", "0") // default state of scrobble
        }
        Library.reset()
        //Library.initialize();

        // initialize playlists
        Playlists.initializePlaylists()
        Playlists.initializePlaylist()
        // everything else
        loading.visible = true
        scrobble = Settings.getSetting("scrobble") == "1" // scrobble state
        lastfmusername = Settings.getSetting("lastfmusername") // lastfm username
        lastfmpassword = Settings.getSetting("lastfmpassword") // lastfm password

        // push the page to view
        pageStack.push(tabs)

        // TODO: Switch tabs back and forth to get the background color in the
        //       header to work properly.
        tabs.selectedTabIndex = 1
        tabs.selectedTabIndex = 0
    }


    // VARIABLES
    property string musicName: i18n.tr("Music")
    property string appVersion: '1.2'
    property bool hasRecent: !Library.isRecentEmpty()
    property bool scrobble: false
    property string lastfmusername
    property string lastfmpassword
    property string timestamp // used to scrobble
    property var chosenElement: null
    property LibraryListModel currentModel: null  // Current model being used
    property var currentQuery: null
    property var currentParam: null
    property bool queueChanged: false
    property bool toolbarShown: musicToolbar.shown
    signal collapseExpand();
    signal collapseSwipeDelete(int index);
    signal onToolbarShownChanged(bool shown, var currentPage, var currentTab)

    property bool wideAspect: width >= units.gu(70)

    // FUNCTIONS

    // Custom debug funtion that's easier to shut off
    function customdebug(text) {
        var debug = true; // set to "0" for not debugging
        //if (args.values.debug) { // *USE LATER*
        if (debug) {
            console.debug(i18n.tr("Debug: ")+text);
        }
    }

    // Add items from a stored query in libraryModel into the queue
    function addQueueFromModel(libraryModel)
    {
        var items;

        if (libraryModel.query === null)
        {
            return
        }

        if (libraryModel.param === null)
        {
            items = libraryModel.query()
        }
        else
        {
            items = libraryModel.query(libraryModel.param)
        }

        for (var key in items)
        {
            trackQueue.append(items[key])
        }
    }

    // Converts an duration in ms to a formated string ("minutes:seconds")
    function durationToString(duration) {
        var minutes = Math.floor((duration/1000) / 60);
        var seconds = Math.floor((duration/1000)) % 60;
        // Make sure that we never see "NaN:NaN"
        if (minutes.toString() == 'NaN')
            minutes = 0;
        if (seconds.toString() == 'NaN')
            seconds = 0;
        return minutes + ":" + (seconds<10 ? "0"+seconds : seconds);
    }

    function trackClicked(libraryModel, index, play)
    {
        play = play === undefined ? true : play  // default play to true

        if (index > libraryModel.model.count - 1 || index < 0) {
            customdebug("Incorrect index given to trackClicked.")
            return;
        }

        var file = Qt.resolvedUrl(libraryModel.model.get(index).file)

        // Clear the play queue and load the new tracks - if not trackQueue
        // Don't reload queue if model, query and parameters are the same
        // Same file different pages is treated as a new session
        if (libraryModel !== trackQueue &&
                (currentModel !== libraryModel ||
                 currentQuery !== libraryModel.query ||
                 currentParam !== libraryModel.param ||
                 queueChanged === true))
        {
            trackQueue.model.clear()
            addQueueFromModel(libraryModel)
        }
        else if (player.source == file &&
                    player.currentIndex === index)
        {
            // Same track so just toggle playing state
            if (play === true) {
                console.log("Is current track: "+player.playbackState)

                // Show the Now Playing page and make sure the track is visible
                tabs.pushNowPlaying();
                nowPlaying.ensureVisibleIndex = index;

                musicToolbar.showToolbar();

                if (musicToolbar.currentPage == nowPlaying) {
                    player.toggle()
                }
            }

            return
        }

        // Current index must be updated before player.source
        currentModel = libraryModel
        currentQuery = libraryModel.query
        currentParam = libraryModel.param

        if (Qt.resolvedUrl(trackQueue.model.get(index).file) != file) {
            index = trackQueue.indexOf(file)  // pick given index first
        }
        queueChanged = false

        console.log("Click of fileName: " + file)

        if (play === true) {
            player.playSong(file, index)

            // Show the Now Playing page and make sure the track is visible
            tabs.pushNowPlaying();
            nowPlaying.ensureVisibleIndex = index;

            musicToolbar.showToolbar();
        }
        else {
            player.source = file
        }

        collapseExpand();  // collapse all expands if track clicked

        return file
    }

    function playRandomSong(shuffle)
    {
        trackQueue.model.clear();

        var items = Library.getAll();

        for (var key in items) {
            trackQueue.append(items[key]);
        }

        var now = new Date();
        var seed = now.getSeconds();
        var index = Math.floor(trackQueue.model.count * Math.random(seed));

        console.debug("THIS", index);

        player.shuffle = shuffle === undefined ? true : shuffle;
        trackClicked(trackQueue,
                     index,
                     true);
    }

    // WHERE THE MAGIC HAPPENS
    Player {
        id: player
    }

    // Model to send the data
    XmlListModel {
        id: scrobblemodel
        query: "/"

        function rpcRequest(request,handler) {
            var http = new XMLHttpRequest()

            http.open("POST",scrobble_url,true)
            http.setRequestHeader("User-Agent", "Music-App/"+appVersion)
            http.setRequestHeader("Content-type", "text/xml")
            http.setRequestHeader("Content-length", request.length)
            if (root.authenticate) {
                http.setRequestHeader("Authorization", "Basic " + Qt.btoa(lastfmusername+":"+lastfmusername))
            }
            http.setRequestHeader("Connection", "close")
            http.onreadystatechange = function() {
                if(http.readyState == 4 && http.status == 200) {
                    console.debug("Debug: XmlRpc::rpcRequest.onreadystatechange()")
                    handler(http.responseText)
                }
            }
            http.send(request)
        }

        function callHandler(response) {
            xml = response
        }

        function call(cmd,params) {
            console.debug("Debug: XmlRpc.call(",cmd,params,")")
            var request = ""
            request += "<?xml version='1.0'?>"
            request += "<methodCall>"
            request += "<methodName>" + cmd + "</methodName>"
            request += "<params>"
            for (var i=0; i<params.length; i++) {
                request += "<param><value>"
                if (typeof(params[i])=="string") {
                    request += "<string>" + params[i] + "</string>"
                }
                if (typeof(params[i])=="number") {
                    request += "<int>" + params[i] + "</int>"
                }
                request += "</value></param>"
            }
            request += "</params>"
            request += "</methodCall>"
            rpcRequest(request,callHandler)
        }
    }

    GriloModel {
        id: griloModel
        property bool loaded: false

        source: GriloBrowse {
            id: browser
            source: "grl-mediascanner"
            registry: registry
            metadataKeys: [GriloBrowse.Title]
            typeFilter: [GriloBrowse.Audio]
            Component.onCompleted: {
                console.log(browser.supportedKeys);
                console.log(browser.slowKeys);
                refresh();
                console.log("Refreshing");
            }

            onAvailableChanged: {
                console.log("Available ? " + available);
                if (available === true) {
                    console.log("griloModel.count " + griloModel.count)
                }
            }
            onBaseMediaChanged: refresh();

            /* Check if the file (needle) exists in the library (haystack)
             * Searches the the haystack using a binary search
             *
             * false if the file in in grilo but not in the haystack
             * positive if the file is the same (number is the actual index)
             * negative if the file has changed, actual index is -(i + 1)
             */
            function exists(haystack, needle)
            {
                var keyToFind = needle["file"];

                var upper = haystack.length - 1;
                var lower = 0;
                var i = Math.floor(haystack.length / 2);

                while (upper >= lower)
                {
                    var key = haystack[i]["file"];

                    if (keyToFind < key)
                    {
                        upper = i - 1;
                    }
                    else if (keyToFind > key)
                    {
                        lower = i + 1;
                    }
                    else
                    {
                        var found = false;

                        for (var k in haystack[i])
                        {
                            if (haystack[i][k] === needle[k])
                            {
                                found = true;
                            }
                            else
                            {
                                found = false;
                                break;
                            }
                        }

                        if (found === true)
                        {
                            return i;  // in grilo and lib - same
                        }
                        else
                        {
                            return -i - 1;  // in grilo and lib - different
                        }
                    }

                    i = Math.floor((upper + lower) / 2);
                }

                return false;  // in grilo not in lib
            }

            onFinished: {
                // FIXME: remove when grilo is fixed
                var files = [];
                var duplicates = 0;

                for (var i = 0; i < griloModel.count; i++)
                {
                    var media = griloModel.get(i)
                    var file = media.url.toString()
                    if (file.indexOf("file://") === 0)
                    {
                        file = file.slice(7, file.length)
                    }

                    // FIXME: grilo can supply duplicates
                    if (files.indexOf(file) > -1)
                    {
                        duplicates++;
                        continue;
                    }
                    files.push(file);

                    var record = {
                        artist: media.artist || i18n.tr("Unknown Artist"),
                        album: media.album || i18n.tr("Unknown Album"),
                        title: media.title || file,
                        file: file,
                        cover: media.thumbnail.toString() || "",
                        length: media.duration.toString(),
                        number: media.trackNumber,
                        year: media.year.toString() !== "0" ? media.year.toString(): i18n.tr("Unknown Year"),
                        genre: media.genre || i18n.tr("Unknown Genre")
                    };

                    //console.log("Artist:"+ media.artist + ", Album:"+media.album + ", Title:"+media.title + ", File:"+file + ", Cover:"+media.thumbnail + ", Number:"+media.trackNumber + ", Genre:"+media.genre);
                    Library.setMetadata(record)
                }

                Library.writeDb()

                console.debug("Grilo duplicates:", duplicates);  // FIXME: remove when grilo is fixed
                griloModel.loaded = true

                // Show toolbar and start timer if there is music
                if (!emptyPage.noMusic || wideAspect) {
                    musicToolbar.showToolbar(); 
                    musicToolbar.startAutohideTimer(); 
                }

                tabs.ensurePopulated(tabs.selectedTab);

                if (args.values.url) {
                    uriHandler.process(args.values.url, true);
                }
            }
        }
    }

    GriloRegistry {
        id: registry

        Component.onCompleted: {
            console.log("Registry is ready");
            loadAll();
        }
    }

    LibraryListModel {
        id: libraryModel
        onPreLoadCompleteChanged: {
            if (preLoadComplete)
            {
                loading.visible = false
                tracksTab.loading = false
                tracksTab.populated = true
            }
        }
    }

    LibraryListModel {
        id: artistModel
        onPreLoadCompleteChanged: {
            if (preLoadComplete)
            {
                loading.visible = false
                artistsTab.loading = false
                artistsTab.populated = true
            }
        }
    }
    LibraryListModel {
        id: artistTracksModel
    }
    LibraryListModel {
        id: artistAlbumsModel
    }

    LibraryListModel {
        id: albumModel
        onPreLoadCompleteChanged: {
            if (preLoadComplete)
            {
                loading.visible = false
                albumsTab.loading = false
                albumsTab.populated = true
            }
        }
    }
    LibraryListModel {
        id: albumTracksModel
    }

    LibraryListModel {
        id: recentModel
        property bool complete: false
        onPreLoadCompleteChanged: {
            complete = true;

            if (preLoadComplete && (genreModel.complete ||
                                    genreModel.query().length === 0))
            {
                loading.visible = false
                startTab.loading = false
                startTab.populated = true
            }
        }
    }
    LibraryListModel {
        id: recentAlbumTracksModel
    }
    LibraryListModel {
        id: recentPlaylistTracksModel
    }

    LibraryListModel {
        id: genreModel
        property bool complete: false
        onPreLoadCompleteChanged: {
            complete = true;

            if (preLoadComplete && (recentModel.complete ||
                                    recentModel.query().length === 0))
            {
                loading.visible = false
                startTab.loading = false
                startTab.populated = true
            }
        }
    }

    LibraryListModel {
        id: genreTracksModel
    }

    // list of tracks on startup. This is just during development
    LibraryListModel {
        id: trackQueue
        Connections {
            target: trackQueue.model
            onCountChanged: queueChanged = true
        }

        function append(listElement)
        {
            model.append({
                             "album": listElement.album,
                             "artist": listElement.artist,
                             "cover": listElement.cover,
                             "file": listElement.file,
                             "title": listElement.title
                         })
        }
    }

    // list of songs, which has been removed.
    ListModel {
        id: removedTrackQueue
    }

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

    // create the listmodel to use for playlists
    LibraryListModel {
        id: playlistModel

        onPreLoadCompleteChanged: {
            if (preLoadComplete)
            {
                loading.visible = false
                playlistTab.loading = false
                playlistTab.populated = true
            }
        }
    }

    // search model
    LibraryListModel {
        id: searchModel
    }

    // load sheets (after model)
    SongsSheet {
        id: songsSheet
    }

    AlbumsSheet {
        id: artistSheet
    }

    MusicSearch {
        id: searchSheet
    }

    // Blurred background
    BlurredBackground {
    }

    LoadingSpinnerComponent {
        id:loading
    }

    // Popover for tracks, queue and add to playlist, for example
    Component {
        id: trackPopoverComponent
        Popover {
            id: trackPopover
            Column {
                id: containerLayout
                anchors {
                    left: parent.left
                    top: parent.top
                    right: parent.right
                }
                ListItem.Standard {
                    Label {
                        text: i18n.tr("Add to queue")
                        color: styleMusic.popover.labelColor
                        fontSize: "large"
                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    onClicked: {
                        console.debug("Debug: Add track to queue: " + JSON.stringify(chosenElement))
                        PopupUtils.close(trackPopover)
                        trackQueue.append(chosenElement)
                    }
                }
                ListItem.Standard {
                    Label {
                        text: i18n.tr("Add to playlist")
                        color: styleMusic.popover.labelColor
                        fontSize: "large"
                        anchors.horizontalCenter: parent.horizontalCenter
                        anchors.verticalCenter: parent.verticalCenter
                    }
                    onClicked: {
                        console.debug("Debug: Add track to playlist")
                        PopupUtils.close(trackPopover)
                        PopupUtils.open(Qt.resolvedUrl("MusicaddtoPlaylist.qml"), mainView,
                                        {
                                            title: i18n.tr("Select playlist")
                                        } )
                    }
                }
            }
        }
    }

    // New playlist dialog
    Component {
        id: newPlaylistDialog
        Dialog {
            id: dialogueNewPlaylist
            title: i18n.tr("New Playlist")
            text: i18n.tr("Name your playlist.")
            TextField {
                id: playlistName
                objectName: "playlistnameTextfield"
                placeholderText: i18n.tr("Name")
            }
            ListItem.Standard {
                id: newplaylistoutput
                visible: false // should only be visible when an error is made.
            }

            Button {
                text: i18n.tr("Create")
                objectName: "newPlaylistDialog_createButton"
                onClicked: {
                    newplaylistoutput.visible = false // make sure its hidden now if there was an error last time
                    if (playlistName.text.length > 0) { // make sure something is acually inputed
                        var newList = Playlists.addPlaylist(playlistName.text)
                        if (newList === "OK") {
                            console.debug("Debug: User created a new playlist named: "+playlistName.text)
                            // add the new playlist to the tab
                            var index = Playlists.getID(); // get the latest ID
                            playlistModel.model.append({"id": index, "name": playlistName.text, "count": "0"})
                            PopupUtils.close(dialogueNewPlaylist)
                        }
                        else {
                            console.debug("Debug: Something went wrong: "+newList)
                            newplaylistoutput.visible = true
                            newplaylistoutput.text = i18n.tr("Error: "+newList)
                        }
                    }
                    else {
                        newplaylistoutput.visible = true
                        newplaylistoutput.text = i18n.tr("Error: You didn't type a name.")
                    }
                }
            }

            Button {
                text: i18n.tr("Cancel")
                color: styleMusic.dialog.buttonColor
                onClicked: PopupUtils.close(dialogueNewPlaylist)
            }
        }
    }

    MusicToolbar {
        id: musicToolbar
        objectName: "musicToolbarObject"
        z: 200  // put on top of everything else
    }

    Page {
        id: emptyPage
        title: i18n.tr("Music")
        visible: false

        property bool noMusic: griloModel.count === 0 && griloModel.loaded === true

        onNoMusicChanged: {
            if (noMusic)
                pageStack.push(emptyPage)
            else if (pageStack.currentPage == emptyPage)
                pageStack.pop()
        }

        tools: ToolbarItems {
            back: null
            locked: true
            opened: false
        }

        // Overlay to show when no tracks detected on the device
        Rectangle {
            id: libraryEmpty
            anchors.fill: parent
            anchors.topMargin: -emptyPage.header.height
            color: styleMusic.libraryEmpty.backgroundColor

            Column {
                anchors.centerIn: parent


                Label {
                    anchors.horizontalCenter: parent.horizontalCenter
                    color: styleMusic.libraryEmpty.labelColor
                    fontSize: "large"
                    font.bold: true
                    text: "No music found"
                }

                Label {
                    anchors.horizontalCenter: parent.horizontalCenter
                    color: styleMusic.libraryEmpty.labelColor
                    fontSize: "medium"
                    text: "Please import music and restart the app"
                }
            }
        }
    }

    PageStack {
        id: pageStack

        Tabs {
            id: tabs
            anchors {
                bottomMargin: wideAspect ? musicToolbar.fullHeight : undefined
                fill: parent
            }

            // First tab is all music
            Tab {
                property bool populated: false
                property var loader: [recentModel.filterRecent, genreModel.filterGenres, albumModel.filterAlbums]
                property bool loading: false
                property var model: [recentModel, genreModel, albumTracksModel]
                id: startTab
                objectName: "starttab"
                anchors.fill: parent
                title: page.title

                // Tab content begins here
                page: MusicStart {
                    id: musicStartPage
                }
            }

            // Second tab is arists
            Tab {
                property bool populated: false
                property var loader: [artistModel.filterArtists]
                property bool loading: false
                property var model: [artistModel, artistAlbumsModel, albumTracksModel]
                id: artistsTab
                objectName: "artiststab"
                anchors.fill: parent
                title: page.title

                // tab content
                page: MusicArtists {
                    id: musicArtistsPage
                }
            }

            // third tab is albums
            Tab {
                property bool populated: false
                property var loader: [albumModel.filterAlbums]
                property bool loading: false
                property var model: [albumModel, albumTracksModel]
                id: albumsTab
                objectName: "albumstab"
                anchors.fill: parent
                title: page.title

                // Tab content begins here
                page: MusicAlbums {
                    id: musicAlbumsPage
                }
            }

            // fourth tab is all songs
            Tab {
                property bool populated: false
                property var loader: [libraryModel.populate]
                property bool loading: false
                property var model: [libraryModel]
                id: tracksTab
                objectName: "trackstab"
                anchors.fill: parent
                title: page.title

                // Tab content begins here
                page: MusicTracks {
                    id: musicTracksPage
                }
            }


            // fifth tab is the playlists
            Tab {
                property bool populated: false
                property var loader: [playlistModel.filterPlaylists]
                property bool loading: false
                property var model: [playlistModel, albumTracksModel]
                id: playlistTab
                objectName: "playlisttab"
                anchors.fill: parent
                title: page.title

                // Tab content begins here
                page: MusicPlaylists {
                    id: musicPlaylistPage
                }
            }

            // Set the models in the tab to allow/disallow loading
            function allowLoading(tabToLoad, state)
            {
                if (tabToLoad !== undefined && tabToLoad.model !== undefined)
                {
                    for (var i=0; i < tabToLoad.model.length; i++)
                    {
                        tabToLoad.model[i].canLoad = state;
                    }
                }
            }

            function ensurePopulated(selectedTab)
            {
                allowLoading(selectedTab, true);  // allow loading of the models

                if (!selectedTab.populated && !selectedTab.loading && griloModel.loaded) {
                    loading.visible = true
                    selectedTab.loading = true

                    if (selectedTab.loader !== undefined)
                    {
                        for (var i=0; i < selectedTab.loader.length; i++)
                        {
                            selectedTab.loader[i]();
                        }
                    }
                }
                loading.visible = selectedTab.loading || !selectedTab.populated
            }

            function pushNowPlaying()
            {
                // only push if on a different page
                if (pageStack.currentPage !== nowPlaying) {
                    pageStack.push(nowPlaying);
                }
            }

            Component.onCompleted: musicToolbar.currentTab = selectedTab

            onSelectedTabChanged: {
                // pause loading of the models in the old tab
                if (musicToolbar.currentTab !== selectedTab &&
                        musicToolbar.currentTab !== null)
                {
                    allowLoading(musicToolbar.currentTab, false);
                }

                musicToolbar.currentTab = selectedTab;

                ensurePopulated(selectedTab);
            }
        } // end of tabs
    }

    MusicNowPlaying {
        id: nowPlaying
    }

    MusicaddtoPlaylist {
        id: addtoPlaylist
    }

} // end of main view