~vthompson/music-app/remix-songs-page

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
/*
 * Copyright (C) 2013 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 QtMultimedia 5.0
import QtQuick.LocalStorage 2.0
import "settings.js" as Settings
import "meta-database.js" as Library
import "scrobble.js" as Scrobble
import "playlists.js" as Playlists
import "common"

PageStack {
    id: pageStack
    anchors.fill: parent

    property string playlistTracks: ""
    property string oldPlaylistName: ""
    property string oldPlaylistIndex: ""
    property string oldPlaylistID: ""
    property string inPlaylist: ""

    // function that adds each playlist in the listmodel to show it in the app
    function addtoPlaylistModel(element,index,array) {
        customdebug("Playlist #" + element.id + " = " + element.name);
        playlistModel.append({"id": element.id, "name": element.name, "count": element.count, "cover0": element.cover0, "cover1": element.cover1, "cover2": element.cover2, "cover3": element.cover3 });
    }

    // Remove playlist dialog
    Component {
         id: removePlaylistDialog
         Dialog {
             id: dialogueRemovePlaylist
             // TRANSLATORS: this is a title of a dialog with a prompt to delete a playlist
             title: i18n.tr("Are you sure?")
             text: i18n.tr("This will delete your playlist.")

             Button {
                 text: i18n.tr("Remove")
                 onClicked: {
                     // removing playlist
                     Playlists.removePlaylist(oldPlaylistID, oldPlaylistName) // remove using both ID and name, if playlists has similair names
                     playlistModel.remove(oldPlaylistIndex)
                     PopupUtils.close(dialogueRemovePlaylist)
                     if (inPlaylist) {
                         customdebug("Back to playlists")
                         pageStack.pop()
                     }
                }
             }
             Button {
                 text: i18n.tr("Cancel")
                 color: styleMusic.dialog.buttonColor
                 onClicked: PopupUtils.close(dialogueRemovePlaylist)
             }
         }
    }

    // Edit name of playlist dialog
    Component {
         id: editPlaylistDialog
         Dialog {
             id: dialogueEditPlaylist
             // TRANSLATORS: this is a title of a dialog with a prompt to rename a playlist
             title: i18n.tr("Change name")
             text: i18n.tr("Enter the new name of the playlist.")
             TextField {
                 id: playlistName
                 placeholderText: oldPlaylistName
             }
             ListItem.Standard {
                 id: editplaylistoutput
                 visible: false
             }

             Button {
                 text: i18n.tr("Change")
                 onClicked: {
                     editplaylistoutput.visible = true
                     if (playlistName.text.length > 0) { // make sure something is acually inputed
                         var editList = Playlists.namechangePlaylist(oldPlaylistName,playlistName.text) // change the name of the playlist in DB
                         console.debug("Debug: User changed name from "+oldPlaylistName+" to "+playlistName.text)
                         playlistModel.set(oldPlaylistIndex, {"name": playlistName.text})
                         PopupUtils.close(dialogueEditPlaylist)
                         if (inPlaylist) {
                             playlistInfoLabel.text = playlistName.text
                         }
                     }
                     else {
                        editplaylistoutput.text = i18n.tr("You didn't type in a name.")
                     }
                }
             }
             Button {
                 text: i18n.tr("Cancel")
                 color: styleMusic.dialog.buttonColor
                 onClicked: PopupUtils.close(dialogueEditPlaylist)
             }
         }
    }

    Component.onCompleted: {
        pageStack.push(listspage)
        // fix pageStack bug the ugly way
        pageStack.push(playlistpage)
        pageStack.pop()

        random = Settings.getSetting("shuffle") == "1" // shuffle state
        scrobble = Settings.getSetting("scrobble") == "1" // scrobble state
        lastfmusername = Settings.getSetting("lastfmusername") // lastfm username
        lastfmpassword = Settings.getSetting("lastfmpassword") // lastfm password

        // get playlists in an array
        var playlist = Playlists.getPlaylists(); // get the playlist from the database
        playlist.forEach(addtoPlaylistModel) // send each item on playlist array to the model to show it
    }

    MusicSettings {
        id: musicSettings
    }

    // page for the playlists
    Page {
        id: listspage
	// TRANSLATORS: this is the name of the playlists page shown in the tab header.
	// Remember to keep the translation short to fit the screen width
        title: i18n.tr("Playlists")

        onVisibleChanged: {
            if (visible === true)
            {
                musicToolbar.setPage(listspage);
            }
        }

        ListView {
            id: playlistslist
            anchors.fill: parent
            anchors.bottomMargin: musicToolbar.mouseAreaOffset + musicToolbar.minimizedHeight
            model: playlistModel
            delegate: playlistDelegate
            onCountChanged: {
                customdebug("onCountChanged: " + playlistslist.count)
            }
            onCurrentIndexChanged: {
                customdebug("tracklist.currentIndex = " + playlistslist.currentIndex)
            }

            Component {
                id: playlistDelegate
                ListItem.Standard {
                       id: playlist
                       property string name: model.name
                       property string count: model.count
                       property string cover0: model.cover0 || ""
                       property string cover1: model.cover1 || ""
                       property string cover2: model.cover2 || ""
                       property string cover3: model.cover3 || ""
                       iconFrame: false
                       height: styleMusic.playlist.playlistItemHeight

                       UbuntuShape {
                           id: cover0
                           anchors.left: parent.left
                           anchors.leftMargin: units.gu(4)
                           anchors.top: parent.top
                           anchors.topMargin: units.gu(1)
                           height: styleMusic.playlist.playlistAlbumSize
                           width: styleMusic.playlist.playlistAlbumSize
                           visible: playlist.count > 3
                           image: Image {
                               source: playlist.cover3 !== "" ? playlist.cover3 :  Qt.resolvedUrl("images/cover_default_icon.png")
                           }
                       }
                       UbuntuShape {
                           id: cover1
                           anchors.left: parent.left
                           anchors.leftMargin: units.gu(3)
                           anchors.top: parent.top
                           anchors.topMargin: units.gu(1)
                           height: styleMusic.playlist.playlistAlbumSize
                           width: styleMusic.playlist.playlistAlbumSize
                           visible: playlist.count > 2
                           image: Image {
                               source: playlist.cover2 !== "" ? playlist.cover2 :  Qt.resolvedUrl("images/cover_default_icon.png")
                           }
                       }
                       UbuntuShape {
                           id: cover2
                           anchors.left: parent.left
                           anchors.leftMargin: units.gu(2)
                           anchors.top: parent.top
                           anchors.topMargin: units.gu(1)
                           height: styleMusic.playlist.playlistAlbumSize
                           width: styleMusic.playlist.playlistAlbumSize
                           visible: playlist.count > 1
                           image: Image {
                               source: playlist.cover1 !== "" ? playlist.cover1 :  Qt.resolvedUrl("images/cover_default_icon.png")
                           }
                       }
                       UbuntuShape {
                           id: cover3
                           anchors.left: parent.left
                           anchors.leftMargin: units.gu(1)
                           anchors.top: parent.top
                           anchors.topMargin: units.gu(1)
                           height: styleMusic.playlist.playlistAlbumSize
                           width: styleMusic.playlist.playlistAlbumSize
                           image: Image {
                               source: playlist.cover0 !== "" ? playlist.cover0 :  Qt.resolvedUrl("images/cover_default_icon.png")
                           }
                       }
                       // songs count
                       Label {
                           id: playlistCount
                           anchors.left: cover3.right
                           anchors.leftMargin: units.gu(4)
                           anchors.top: parent.top
                           anchors.topMargin: units.gu(2)
                           anchors.right: expandItem.left
                           anchors.rightMargin: units.gu(1.5)
                           elide: Text.ElideRight
                           fontSize: "x-small"
                           height: units.gu(1)
                           text: i18n.tr("%1 song", "%1 songs", playlist.count).arg(playlist.count)
                       }
                       // playlist name
                       Label {
                           id: playlistName
                           wrapMode: Text.NoWrap
                           maximumLineCount: 1
                           fontSize: "medium"
                           color: styleMusic.common.music
                           anchors.left: cover3.right
                           anchors.leftMargin: units.gu(4)
                           anchors.top: playlistCount.bottom
                           anchors.topMargin: units.gu(1)
                           anchors.right: expandItem.left
                           anchors.rightMargin: units.gu(1.5)
                           elide: Text.ElideRight
                           text: playlist.name
                       }

                       //Icon {
                       Image {
                           id: expandItem
                         //  name: "dropdown-menu"
                           source: expandable.visible ? "images/dropdown-menu-up.svg" : "images/dropdown-menu.svg"
                           anchors.right: parent.right
                           anchors.rightMargin: units.gu(2)
                           height: styleMusic.common.expandedItem
                           width: styleMusic.common.expandedItem
                           y: parent.y + (styleMusic.playlist.playlistItemHeight / 2) - (height / 2)
                       }

                       MouseArea {
                           anchors.bottom: parent.bottom
                           anchors.right: parent.right
                           anchors.top: parent.top
                           width: styleMusic.common.expandedItem * 3
                           onClicked: {
                              if(expandable.visible) {
                                  customdebug("clicked collapse")
                                  expandable.visible = false
                                  playlist.height = styleMusic.playlist.playlistItemHeight
                              }
                              else {
                                  customdebug("clicked expand")
                                  collapseExpand(-1);  // collapse all others
                                  expandable.visible = true
                                  playlist.height = styleMusic.playlists.expandedHeight
                              }
                           }
                       }

                       Rectangle {
                           id: expandable
                           anchors.fill: parent
                           color: "transparent"
                           height: styleMusic.common.expandHeight
                           visible: false

                           Component.onCompleted: {
                               collapseExpand.connect(onCollapseExpand);
                           }

                           function onCollapseExpand(indexCol)
                           {
                               if ((indexCol === index || indexCol === -1) && expandable !== undefined && expandable.visible === true)
                               {
                                   customdebug("auto collapse")
                                   expandable.visible = false
                                   playlist.height = styleMusic.playlist.playlistItemHeight
                               }
                           }

                           // background for expander
                           Rectangle {
                               anchors.top: parent.top
                               anchors.topMargin: styleMusic.playlist.playlistItemHeight
                               color: styleMusic.common.black
                               height: styleMusic.playlists.expandedHeight - styleMusic.playlist.playlistItemHeight
                               width: playlist.width
                               opacity: 0.4
                           }

                           Rectangle {
                               id: editColumn
                               anchors.top: parent.top
                               anchors.topMargin: ((styleMusic.playlists.expandedHeight - styleMusic.playlist.playlistItemHeight) / 2)
                                                  + styleMusic.playlist.playlistItemHeight
                                                  - (height / 2)
                               anchors.left: parent.left
                               anchors.leftMargin: styleMusic.common.expandedLeftMargin
                               height: styleMusic.common.expandedItem
                               Rectangle {
                                   color: "transparent"
                                   height: styleMusic.common.expandedItem
                                   width: units.gu(15)
                                   Icon {
                                       id: editPlaylist
                                       color: styleMusic.common.white
                                       name: "edit"
                                       height: styleMusic.common.expandedItem
                                       width: styleMusic.common.expandedItem
                                   }
                                   Label {
                                       anchors.left: editPlaylist.right
                                       anchors.leftMargin: units.gu(0.5)
                                       color: styleMusic.common.white
                                       fontSize: "small"
				       // TRANSLATORS: this refers to editing a playlist
                                       text: i18n.tr("Edit")
                                   }
                                   MouseArea {
                                      anchors.fill: parent
                                      onClicked: {
                                          expandable.visible = false
                                          playlist.height = styleMusic.playlist.playlistItemHeight
                                          customdebug("Edit playlist")
                                          oldPlaylistName = name
                                          oldPlaylistID = id
                                          oldPlaylistIndex = index
                                          PopupUtils.open(editPlaylistDialog, mainView)
                                      }
                                   }
                                }
                           }

                           Rectangle {
                               id: deleteColumn
                               anchors.top: parent.top
                               anchors.topMargin: ((styleMusic.playlists.expandedHeight - styleMusic.playlist.playlistItemHeight) / 2)
                                                  + styleMusic.playlist.playlistItemHeight
                                                  - (height / 2)
                               anchors.horizontalCenter: parent.horizontalCenter
                               height: styleMusic.common.expandedItem
                               Rectangle {
                                   color: "transparent"
                                   height: styleMusic.common.expandedItem
                                   width: units.gu(15)
                                   Icon {
                                       id: deletePlaylist
                                       color: styleMusic.common.white
                                       name: "delete"
                                       height: styleMusic.common.expandedItem
                                       width: styleMusic.common.expandedItem
                                   }
                                   Label {
                                       anchors.left: deletePlaylist.right
                                       anchors.leftMargin: units.gu(0.5)
                                       color: styleMusic.common.white
                                       fontSize: "small"
				       // TRANSLATORS: this refers to deleting a playlist
                                       text: i18n.tr("Delete")
                                   }
                                   MouseArea {
                                      anchors.fill: parent
                                      onClicked: {
                                          expandable.visible = false
                                          playlist.height = styleMusic.playlist.playlistItemHeight
                                          customdebug("Delete")
                                          oldPlaylistName = name
                                          oldPlaylistID = id
                                          oldPlaylistIndex = index
                                          PopupUtils.open(removePlaylistDialog, mainView)
                                      }
                                   }
                                }
                            }
                           // share
                           Rectangle {
                               id: shareColumn
                               anchors.top: parent.top
                               anchors.topMargin: ((styleMusic.playlists.expandedHeight - styleMusic.playlist.playlistItemHeight) / 2)
                                                  + styleMusic.playlist.playlistItemHeight
                                                  - (height / 2)
                               anchors.left: deleteColumn.right
                               anchors.leftMargin: units.gu(2)
                               anchors.right: parent.right
                               visible: false
                               Rectangle {
                                   color: "transparent"
                                   height: styleMusic.common.expandedItem
                                   width: units.gu(15)
                                   Icon {
                                       id: sharePlaylist
                                       color: styleMusic.common.white
                                       name: "share"
                                       height: styleMusic.common.expandedItem
                                       width: styleMusic.common.expandedItem
                                   }
                                   Label {
                                       anchors.left: sharePlaylist.right
                                       anchors.leftMargin: units.gu(0.5)
                                       color: styleMusic.common.white
                                       fontSize: "small"
				       // TRANSLATORS: this refers to sharing a playlist
                                       text: i18n.tr("Share")
                                   }
                                   MouseArea {
                                      anchors.fill: parent
                                      onClicked: {
                                          expandable.visible = false
                                          playlist.height = styleMusic.playlist.playlistItemHeight
                                          customdebug("Share")
                                          inPlaylist = true
                                      }
                                   }
                                }
                            }
                       }

                    onClicked: {
                        customdebug("Playlist chosen: " + name)
                        expandable.visible = false
                        playlist.height = styleMusic.playlist.playlistItemHeight
                        playlisttracksModel.filterPlaylistTracks(name)
                        playlistlist.playlistName = name
                        pageStack.push(playlistpage) // show the chosen playlists content
                        playlistpage.title = name + " " + "("+ count +")" // change name of the tab
                        // for removal or edit in playlist
                        oldPlaylistName = name
                        oldPlaylistID = id
                        oldPlaylistIndex = index
                        expandable.visible = false
                        playlistInfo.count = playlist.count
                        playlistInfo.cover0 = playlist.cover0
                        playlistInfo.cover1 = playlist.cover1
                        playlistInfo.cover2 = playlist.cover2
                        playlistInfo.cover3 = playlist.cover3
                    }
                }
            }
        }
    }

    // page for the tracks in the playlist
    Page {
        id: playlistpage
        title: i18n.tr("Playlist")
        tools: null
        visible: false

        onVisibleChanged: {
            if (visible === true)
            {
                musicToolbar.setPage(playlistpage, listspage, pageStack);
            }
        }

        // playlist name and info
        Rectangle {
            id: playlistInfo
            anchors.top: parent.top
            width: parent.width
            height: styleMusic.playlist.infoHeight
            color: styleMusic.playerControls.backgroundColor
            //opacity: 0.7

            property int count: 0
            property string cover0: ""
            property string cover1: ""
            property string cover2: ""
            property string cover3: ""

            UbuntuShape {
                id: cover0
                anchors.left: parent.left
                anchors.leftMargin: units.gu(5)
                anchors.top: parent.top
                anchors.topMargin: units.gu(2)
                width: styleMusic.common.albumSize
                height: styleMusic.common.albumSize
                visible: playlistInfo.count > 3
                image: Image {
                    source: playlistInfo.cover3 !== "" ? playlistInfo.cover3 : Qt.resolvedUrl("images/cover_default_icon.png")
                }
            }
            UbuntuShape {
                id: cover1
                anchors.left: parent.left
                anchors.leftMargin: units.gu(4)
                anchors.top: parent.top
                anchors.topMargin: units.gu(2)
                width: styleMusic.common.albumSize
                height: styleMusic.common.albumSize
                visible: playlistInfo.count > 2
                image: Image {
                    source: playlistInfo.cover2 !== "" ? playlistInfo.cover2 :  Qt.resolvedUrl("images/cover_default_icon.png")
                }
            }
            UbuntuShape {
                id: cover2
                anchors.left: parent.left
                anchors.leftMargin: units.gu(3)
                anchors.top: parent.top
                anchors.topMargin: units.gu(2)
                width: styleMusic.common.albumSize
                height: styleMusic.common.albumSize
                visible: playlistInfo.count > 1
                image: Image {
                    source: playlistInfo.cover1 !== "" ? playlistInfo.cover1 :  Qt.resolvedUrl("images/cover_default_icon.png")
                }
            }
            UbuntuShape {
                id: cover3
                anchors.left: parent.left
                anchors.leftMargin: units.gu(2)
                anchors.top: parent.top
                anchors.topMargin: units.gu(2)
                width: styleMusic.common.albumSize
                height: styleMusic.common.albumSize
                image: Image {
                    source: playlistInfo.cover0 !== "" ? playlistInfo.cover0 :  Qt.resolvedUrl("images/cover_default_icon.png")
                }
            }

            Label {
                id: playlistInfoLabel
                text: playlistlist.playlistName
                color: styleMusic.common.white
                fontSize: "large"
                anchors.left: parent.left
                anchors.leftMargin: units.gu(16)
                anchors.top: parent.top
                anchors.topMargin: units.gu(2.5)
                anchors.right: expandInfoItem.left
                anchors.rightMargin: units.gu(1.5)
                elide: Text.ElideRight
            }

            Label {
                id: playlistInfoCount
		text: i18n.tr("%1 song", "%1 songs", playlist.count).arg(playlist.count)
                color: styleMusic.common.white
                fontSize: "medium"
                anchors.left: parent.left
                anchors.leftMargin: units.gu(16)
                anchors.top: parent.top
                anchors.topMargin: units.gu(5)
                anchors.right: expandInfoItem.left
                anchors.rightMargin: units.gu(1.5)
                elide: Text.ElideRight
            }

            //Icon { use for 1.0
            Image {
                id: expandInfoItem
                anchors.right: parent.right
                anchors.rightMargin: units.gu(2)
                //name: "dropdown-menu" use for 1.0
                source: expandableInfo.visible ? "images/dropdown-menu-up.svg" : "images/dropdown-menu.svg"
                height: styleMusic.common.expandedItem
                width: styleMusic.common.expandedItem
                y: parent.y + (styleMusic.playlist.infoHeight / 2) - (height / 2)
            }

            MouseArea {
                anchors.bottom: parent.bottom
                anchors.right: parent.right
                anchors.top: parent.top
                width: styleMusic.common.expandedItem * 3
                onClicked: {
                   if(expandableInfo.visible) {
                       customdebug("clicked collapse")
                       expandableInfo.visible = false
                       playlistInfo.height = styleMusic.playlist.infoHeight

                   }
                   else {
                       customdebug("clicked expand")
                       expandableInfo.visible = true
                       playlistInfo.height = styleMusic.playlist.expandedHeight
                   }
               }
           }

            Rectangle {
                id: expandableInfo
                anchors.fill: parent
                color: "transparent"
                height: styleMusic.common.expandHeight
                visible: false
                Rectangle {
                    id: editColumn
                    anchors.top: parent.top
                    anchors.topMargin: styleMusic.common.expandedTopMargin
                    anchors.left: parent.left
                    anchors.leftMargin: styleMusic.common.expandedLeftMargin
                    color: "transparent"
                    Rectangle {
                        id: editRow
                        color: "transparent"
                        height: styleMusic.common.expandedItem
                        width: units.gu(15)
                        Icon {
                            id: editPlaylist
                            name: "edit"
                            height: styleMusic.common.expandedItem
                            width: styleMusic.common.expandedItem
                        }
                        Label {
                            text: i18n.tr("Edit")
                            fontSize: "small"
                            wrapMode: Text.WordWrap
                            anchors.left: editPlaylist.right
                            anchors.leftMargin: units.gu(0.5)
                        }
                        MouseArea {
                           anchors.fill: parent
                           onClicked: {
                               expandableInfo.visible = false
                               playlistInfo.height = styleMusic.playlist.infoHeight
                               customdebug("Edit playlist")
                               inPlaylist = true
                               PopupUtils.open(editPlaylistDialog, mainView)
                         }
                       }
                    }
                }

                Rectangle {
                    id: deleteColumn
                    anchors.horizontalCenter: parent.horizontalCenter
                    anchors.top: parent.top
                    anchors.topMargin: styleMusic.common.expandedTopMargin
                    color: "transparent"
                    Rectangle {
                        id: deleteRow
                        color: "transparent"
                        height: styleMusic.common.expandedItem
                        width: units.gu(15)
                        Icon {
                            id: deletePlaylist
                            name: "delete"
                            height: styleMusic.common.expandedItem
                            width: styleMusic.common.expandedItem
                        }
                        Label {
                            text: i18n.tr("Delete")
                            fontSize: "small"
                            anchors.left: deletePlaylist.right
                            anchors.leftMargin: units.gu(0.5)
                        }
                        MouseArea {
                           anchors.fill: parent
                           onClicked: {
                               expandableInfo.visible = false
                               playlistInfo.height = styleMusic.playlist.infoHeight
                               customdebug("Delete")
                               inPlaylist = true
                               PopupUtils.open(removePlaylistDialog, mainView)
                         }
                       }
                    }
                 }
                // share
                Rectangle {
                    id: shareColumn
                    anchors.top: parent.top
                    anchors.topMargin: styleMusic.common.expandedTopMargin
                    anchors.left: deleteColumn.right
                    anchors.leftMargin: units.gu(2)
                    anchors.right: parent.right
                    color: "transparent"
                    visible: false
                    Rectangle {
                        id: shareRow
                        color: "transparent"
                        height: styleMusic.common.expandedItem
                        width: units.gu(15)
                        Icon {
                            id: sharePlaylist
                            name: "share"
                            height: styleMusic.common.expandedItem
                            width: styleMusic.common.expandedItem
                        }
                        Label {
                            text: i18n.tr("Share")
                            fontSize: "small"
                            anchors.left: sharePlaylist.right
                            anchors.leftMargin: units.gu(0.5)
                        }
                        MouseArea {
                           anchors.fill: parent
                           onClicked: {
                               expandableInfo.visible = false
                               playlistInfo.height = styleMusic.playlist.infoHeight
                               customdebug("Share")
                               inPlaylist = true
                         }
                       }
                    }
                 }
            }
        }

        ListView {
            id: playlistlist
            anchors.bottom: parent.bottom
            anchors.top: playlistInfo.bottom
            width: parent.width
            anchors.bottomMargin: musicToolbar.mouseAreaOffset + musicToolbar.minimizedHeight
            highlightFollowsCurrentItem: false
            model: playlisttracksModel.model
            delegate: playlisttrackDelegate
            state: "normal"
            z: -1
            states: [
                State {
                    name: "normal"
                    PropertyChanges {
                        target: playlistlist
                        interactive: true
                    }
                },
                State {
                    name: "reorder"
                    PropertyChanges {
                        target: playlistlist
                        interactive: false
                    }
                }
            ]

            onCountChanged: {
                console.log("Tracks in playlist onCountChanged: " + playlistlist.count)
                playlistlist.currentIndex = playlisttracksModel.indexOf(currentFile)
            }
            onCurrentIndexChanged: {
                console.log("Tracks in playlist tracklist.currentIndex = " + playlistlist.currentIndex)
            }

            property int normalHeight: styleMusic.common.itemHeight
            property string playlistName: ""
            property int transitionDuration: 250

            Component {
                id: playlisttrackDelegate
                ListItem.Standard {
                    id: playlistTracks
                    property string artist: model.artist
                    property string album: model.album
                    property string title: model.title
                    property string cover: model.cover
                    property string length: model.length
                    property string file: model.file
                    height: playlistlist.normalHeight

                    SwipeDelete {
                        id: swipeBackground
                        duration: playlistlist.transitionDuration

                        onDeleteStateChanged: {
                            if (deleteState === true)
                            {
                                console.debug("Remove from playlist: " + playlistlist.playlistName + " file: " + file);

                                var realID = Playlists.getRealID(playlistlist.playlistName, index);
                                Playlists.removeFromPlaylist(playlistlist.playlistName, realID);

                                playlistlist.model.remove(index);
                                playlistModel.get(oldPlaylistIndex).count -= 1;
                                queueChanged = true;
                            }
                        }
                    }

                    MouseArea {
                        id: playlistTrackArea
                        anchors.fill: parent

                        property int startX: playlistTracks.x
                        property int startY: playlistTracks.y
                        property int startMouseY: -1

                        // Allow dragging on the X axis for swipeDelete if not reordering
                        drag.target: playlistTracks
                        drag.axis: Drag.XAxis
                        drag.minimumX: playlistlist.state == "reorder" ? 0 : -playlistTracks.width
                        drag.maximumX: playlistlist.state == "reorder" ? 0 : playlistTracks.width

                        /* Get the mouse and item difference from the starting positions */
                        function getDiff(mouseY)
                        {
                            return (mouseY - startMouseY) + (playlistTracks.y - startY);
                        }

                        function getNewIndex(mouseY, index)
                        {
                            var diff = getDiff(mouseY);
                            var negPos = diff < 0 ? -1 : 1;

                            return index + (Math.round(diff / playlistlist.normalHeight));
                        }

                        onClicked: {
                            customdebug("File: " + file) // debugger
                            trackClicked(playlisttracksModel, index) // play track
                            Library.addRecent(oldPlaylistName, "Playlist", cover, oldPlaylistName, "playlist")
                            mainView.hasRecent = true
                            recentModel.filterRecent()
                        }

                        onMouseXChanged: {
                            // Only allow XChange if not in reorder state
                            if (playlistlist.state == "reorder")
                            {
                                return;
                            }

                            // New X is less than start so swiping left
                            if (playlistTracks.x < startX)
                            {
                                swipeBackground.state = "swipingLeft";
                            }
                            // New X is greater sow swiping right
                            else if (playlistTracks.x > startX)
                            {
                                swipeBackground.state = "swipingRight";
                            }
                            // Same so reset state back to normal
                            else
                            {
                                swipeBackground.state = "normal";
                                playlistlist.state = "normal";
                            }
                        }

                        onMouseYChanged: {
                            // Y change only affects when in reorder mode
                            if (playlistlist.state == "reorder")
                            {
                                /* update the listitem y position so that the
                                 * listitem horizontalCenter is under the mouse.y */
                                playlistTracks.y += mouse.y - (playlistTracks.height / 2);
                            }
                        }

                        onPressed: {
                            startX = playlistTracks.x;
                            startY = playlistTracks.y;
                            startMouseY = mouse.y;
                        }

                        onPressAndHold: {
                            customdebug("Pressed and held track playlist "+file)
                            playlistlist.state = "reorder";  // enable reordering state
                            trackContainerReorderAnimation.start();
                            //PopupUtils.open(playlistPopoverComponent, mainView)
                        }

                        onReleased: {
                            // Get current state to determine what to do
                            if (playlistlist.state == "reorder")
                            {
                                var newIndex = getNewIndex(mouse.y + (playlistTracks.height / 2), index);  // get new index

                                // Indexes larger than current need -1 because when it is moved the current is removed
                                if (newIndex > index)
                                {
                                    newIndex -= 1;
                                }

                                if (newIndex === index)
                                {
                                    playlistTracksResetAnimation.start();  // reset item position
                                    trackContainerResetAnimation.start();  // reset the trackContainer
                                }
                                else
                                {
                                    playlistTracks.x = startX;  // ensure X position is correct
                                    trackContainerResetAnimation.start();  // reset the trackContainer

                                    // Check that the newIndex is within the range
                                    if (newIndex < 0)
                                    {
                                        newIndex = 0;
                                    }
                                    else if (newIndex > playlistlist.count - 1)
                                    {
                                        newIndex = playlistlist.count - 1;
                                    }

                                    console.debug("Move: " + index + " To: " + newIndex);

                                    // get the real IDs and update the database
                                    var realID = Playlists.getRealID(playlistlist.playlistName, index);
                                    var realNewID = Playlists.getRealID(playlistlist.playlistName, newIndex);
                                    Playlists.move(playlistlist.playlistName, realID, realNewID);

                                    playlistlist.model.move(index, newIndex, 1);  // update the model
                                    queueChanged = true;
                                }
                            }
                            else if (swipeBackground.state == "swipingLeft" || swipeBackground.state == "swipingRight")
                            {
                                // Remove if moved > 10 units otherwise reset
                                if (Math.abs(playlistTracks.x - startX) > units.gu(10))
                                {
                                    /*
                                     * Remove the listitem
                                     *
                                     * Remove the listitem to relevant side (playlistTracksRemoveAnimation)
                                     * Reduce height of listitem and remove the item
                                     *   (swipeDeleteAnimation [called on playlistTracksRemoveAnimation complete])
                                     */
                                    swipeBackground.runSwipeDeletePrepareAnimation();  // fade out the clear text
                                    playlistTracksRemoveAnimation.start();  // remove item from listview
                                }
                                else
                                {
                                    /*
                                     * Reset the listitem
                                     *
                                     * Remove the swipeDelete to relevant side (swipeResetAnimation)
                                     * Reset the listitem to the centre (playlistTracksResetAnimation)
                                     */
                                    playlistTracksResetAnimation.start();  // reset item position
                                }
                            }

                            // ensure states are normal
                            swipeBackground.state = "normal";
                            playlistlist.state = "normal";
                        }

                        // Animation to reset the x, y of the item
                        ParallelAnimation {
                            id: playlistTracksResetAnimation
                            running: false
                            NumberAnimation {  // reset X
                                target: playlistTracks
                                property: "x"
                                to: playlistTrackArea.startX
                                duration: playlistlist.transitionDuration
                            }
                            NumberAnimation {  // reset Y
                                target: playlistTracks
                                property: "y"
                                to: playlistTrackArea.startY
                                duration: playlistlist.transitionDuration
                            }
                        }

                        /*
                         * Animation to remove an item from the list
                         * - Removes listitem to relevant side
                         * - Calls swipeDeleteAnimation to delete the listitem
                         */
                        NumberAnimation {
                            id: playlistTracksRemoveAnimation
                            target: playlistTracks
                            property: "x"
                            to: swipeBackground.state == "swipingRight" ? playlistTracks.width : 0 - playlistTracks.width
                            duration: playlistlist.transitionDuration

                            onRunningChanged: {
                                // Remove from queue once animation has finished
                                if (running == false)
                                {
                                    swipeBackground.runSwipeDeleteAnimation();
                                }
                            }
                        }
                    }
                    Rectangle {
                        id: trackContainer;
                        anchors.fill: parent
                        anchors.margins: units.gu(1)
                        color: "transparent"

                        NumberAnimation {
                            id: trackContainerReorderAnimation
                            target: trackContainer;
                            property: "anchors.leftMargin";
                            duration: playlistlist.transitionDuration;
                            to: units.gu(2)
                        }

                        NumberAnimation {
                            id: trackContainerResetAnimation
                            target: trackContainer;
                            property: "anchors.leftMargin";
                            duration: playlistlist.transitionDuration;
                            to: units.gu(0.5)
                        }

                        UbuntuShape {
                            id: trackCover
                            anchors.left: parent.left
                            anchors.leftMargin: units.gu(1)
                            anchors.top: parent.top
                            anchors.verticalCenter: parent.verticalCenter
                            width: styleMusic.common.albumSize
                            height: styleMusic.common.albumSize
                            image: Image {
                                source: cover !== "" ? cover :  Qt.resolvedUrl("images/cover_default_icon.png")
                            }
                            UbuntuShape {  // Background so can see text in current state
                                id: trackBg
                                anchors.top: parent.top
                                color: styleMusic.common.black
                                width: styleMusic.common.albumSize
                                height: styleMusic.common.albumSize
                                opacity: 0
                            }
                        }

                        Label {
                            id: trackArtist
                            wrapMode: Text.NoWrap
                            maximumLineCount: 2
                            fontSize: "x-small"
                            anchors.left: trackCover.left
                            anchors.leftMargin: units.gu(11)
                            anchors.top: parent.top
                            anchors.topMargin: units.gu(1)
                            anchors.right: parent.right
                            anchors.rightMargin: units.gu(1.5)
                            elide: Text.ElideRight
                            text: playlistTracks.artist == "" ? "" : playlistTracks.artist
                        }
                        Label {
                            id: trackTitle
                            wrapMode: Text.NoWrap
                            maximumLineCount: 1
                            fontSize: "small"
                            color: styleMusic.common.music
                            anchors.left: trackCover.left
                            anchors.leftMargin: units.gu(11)
                            anchors.top: trackArtist.bottom
                            anchors.topMargin: units.gu(1)
                            anchors.right: parent.right
                            anchors.rightMargin: units.gu(1.5)
                            elide: Text.ElideRight
                            text: playlistTracks.title == "" ? playlistTracks.file : playlistTracks.title
                        }
                        Label {
                            id: trackAlbum
                            wrapMode: Text.NoWrap
                            maximumLineCount: 2
                            fontSize: "xx-small"
                            anchors.left: trackCover.left
                            anchors.leftMargin: units.gu(11)
                            anchors.top: trackTitle.bottom
                            anchors.topMargin: units.gu(2)
                            anchors.right: parent.right
                            anchors.rightMargin: units.gu(1.5)
                            elide: Text.ElideRight
                            text: playlistTracks.album
                        }
                        Label {
                            id: trackDuration
                            wrapMode: Text.NoWrap
                            maximumLineCount: 2
                            fontSize: "small"
                            color: styleMusic.common.music
                            anchors.left: trackCover.left
                            anchors.leftMargin: units.gu(12)
                            anchors.top: trackAlbum.bottom
                            anchors.right: parent.right
                            anchors.rightMargin: units.gu(1.5)
                            elide: Text.ElideRight
                            visible: false
                            text: ""
                        }
                        states: State {
                            name: "Current"
                            when: playlistTracks.ListView.isCurrentItem
                        }
                        Image {
                            visible: false // activate when cover art stops expanding togehter with the row
                            id: expandItem
                            anchors.right: parent.right
                            anchors.rightMargin: units.gu(2)
                            anchors.top: parent.top
                            anchors.topMargin: units.gu(4)
                            source: "images/select.png"
                            height: styleMusic.common.expandedItem
                            width: styleMusic.common.expandedItem

                            MouseArea {
                               anchors.fill: parent
                               onClicked: {
                                   if(expandable.visible) {
                                       customdebug("clicked collapse")
                                       expandable.visible = false
                                       playlistTracks.height = styleMusic.common.itemHeight

                                   }
                                   else {
                                       customdebug("clicked expand")
                                       expandable.visible = true
                                       playlistTracks.height = styleMusic.common.expandedHeight
                                   }
                               }
                           }
                        }

                        Rectangle {
                            id: expandable
                            visible: false
                            width: parent.fill
                            height: styleMusic.common.expandHeight
                            MouseArea {
                               anchors.fill: parent
                               onClicked: {
                                   customdebug("User pressed outside the playlist item and expanded items.")
                             }
                           }
                        }
                    }
                }
            }
        }
    }
}