summaryrefslogtreecommitdiffstats
path: root/playerwidget.cpp
blob: 2411f0189ffe6cf1cc6f2130b14790582f47258e (plain)
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
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
#include <QGroupBox>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QTextEdit>
#include <QTextBrowser>
#include <QSlider>
#include <QTreeView>
#include <QSplitter>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QAction>
#include <QToolBar>
#include <QDir>
#include <QMimeDatabase>
#include <QSettings>
#include <QMenu>
#include <QMessageBox>
#include <QInputDialog>
#include <QTimer>
#include <QStackedLayout>
#include <QApplication>
#include <QDateTime>
#include <QRegularExpression>

#include <algorithm>
#include <taglib/tag.h>
#include <taglib/audioproperties.h>
#include <kglobalaccel.h>

#include "playerwidget.h"
#include "beetview.h"
#include "indexerdialog.h"
#include "globals.h"
#include "helper.h"
#include "webdownloader.h"
#include "webradiodialog.h"

PlayerWidget::PlayerWidget(QWidget *parent) : QWidget(parent), mDurSecs(0), mPlayListLength(0), mIsStream(false){
    mStarting = true;
    setupGui();
    createActions();
    mWebDownloader = new WebDownloader(this);
    connect(mWebDownloader, &WebDownloader::done, this, &PlayerWidget::webDlDone);
    mStarting = false;
}

PlayerWidget::~PlayerWidget(){
    writeSettings();
}

void PlayerWidget::setupGui(){
    //the Player
    mPlayer = new QMediaPlayer(this);
    connect(mPlayer, &QMediaPlayer::positionChanged, this, &PlayerWidget::setPosition);
    connect(mPlayer, &QMediaPlayer::mediaStatusChanged, this, &PlayerWidget::continuePlaying);

    //tray icon
    mTrayIcon = new QSystemTrayIcon(this);
    mTrayIcon->setIcon(QIcon(":/stop.png"));
    mTrayIcon->show();
    mVolumeTimer = new QTimer(this);
    mVolumeTimer->setSingleShot(true);
    connect(mVolumeTimer, &QTimer::timeout, this, &PlayerWidget::showVolume);

    //THE view
    mView = new BeetView;
    mView->setAlternatingRowColors(true);
    mViewModel = new QStandardItemModel;
    mView->setModel(mViewModel);
    mSearchModel = new QStandardItemModel;
    mView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    connect(mView, &BeetView::doubleClicked, this, &PlayerWidget::viewDoubleClicked);
    mFolderModel = new QStandardItemModel;
    mWebRadioModel = new QStandardItemModel;
    mCurrentModel = mViewModel;
    QToolBar *viewTB = new QToolBar;
    QActionGroup *viewAG = new QActionGroup(this);
    viewAG->setExclusive(true);
    mViewByArtistA = new QAction(QIcon(":/artist.png"), tr("View by artist"), this);
    mViewByArtistA->setCheckable(true);
    viewAG->addAction(mViewByArtistA);
    connect(mViewByArtistA, &QAction::triggered, this, &PlayerWidget::doPopulateByArtist);
    QAction *viewByAlbumA = new QAction(QIcon(":/album.png"), tr("View by album"), this);
    viewByAlbumA->setCheckable(true);
    viewAG->addAction(viewByAlbumA);
    connect(viewByAlbumA, &QAction::triggered, this, &PlayerWidget::doPopulateByAlbum);
    QAction *viewBySongA = new QAction(QIcon(":/song.png"), tr("View by song"), this);
    viewBySongA->setCheckable(true);
    viewAG->addAction(viewBySongA);
    connect(viewBySongA, &QAction::triggered, this, &PlayerWidget::doPopulateBySong);
    QAction *viewByGenreA = new QAction(QIcon(":/genre.png"), tr("View by genre"), this);
    viewByGenreA->setCheckable(true);
    viewAG->addAction(viewByGenreA);
    connect(viewByGenreA, &QAction::triggered, this, &PlayerWidget::doPopulateByGenre);
    QAction *viewByDateA = new QAction(QIcon(":/sissyd.png"), tr("View by date"), this);
    viewByDateA->setCheckable(true);
    viewAG->addAction(viewByDateA);
    connect(viewByDateA, &QAction::triggered, this, &PlayerWidget::doPopulateByDate);
    viewAG->addAction(Helper::createSeparator(this));
    QAction *viewByFolderA = new QAction(QIcon(":/folder.png"), tr("View by folder"), this);
    viewByFolderA->setCheckable(true);
    viewAG->addAction(viewByFolderA);
    connect(viewByFolderA, &QAction::triggered, this, &PlayerWidget::doPopulateByFolder);
    viewAG->addAction(Helper::createSeparator(this));
    mSearchA = new QAction(QIcon(":/gaping_ass.png"), tr("View search"), this);
    mSearchA->setCheckable(true);
    viewAG->addAction(mSearchA);
    connect(mSearchA, &QAction::triggered, this, &PlayerWidget::doFilter);
    QAction *viewByWebradioA = new QAction(QIcon(":/dog_hood.png"), tr("Webradio"), this);
    viewByWebradioA->setCheckable(true);
    viewAG->addAction(Helper::createSeparator(this));
    viewAG->addAction(viewByWebradioA);
    connect(viewByWebradioA, &QAction::triggered, this, &PlayerWidget::doPopulateByWebradio);
    viewTB->addActions(viewAG->actions());
    mSelectFilesA = new QAction(QIcon(":/bizarre_amputee.png"), tr("Select files..."), this);
    mSelectFilesA->setShortcut(tr("CTRL++"));
    connect(mSelectFilesA, &QAction::triggered, this, &PlayerWidget::doSelectFiles);
    mDeselectAllA = new QAction(QIcon(":/gaping_ass.png"), tr("Clear Selection"), this);
    mDeselectAllA->setShortcut(tr("CTRL+-"));
    connect(mDeselectAllA, &QAction::triggered, this, &PlayerWidget::doDeleteFiles);
    mDeleteFilesA = new QAction(QIcon(":/delete.png"), tr("Delete files..."), this);
    mDeleteFilesA->setShortcut(QKeySequence::Delete);
    connect(mDeleteFilesA, &QAction::triggered, this, &PlayerWidget::doDeleteFiles);
    mRefreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh"), this);
    connect(mRefreshA, &QAction::triggered, this, &PlayerWidget::doPopulateByFolder);

    //filter
    QGroupBox *filterGB = new QGroupBox(tr("Search"));
    mSearch = new QLineEdit;
    connect(mSearch, &QLineEdit::returnPressed, this, &PlayerWidget::doFilter);
    QToolBar *searchTB = new QToolBar;
    QAction *clearSearchA = new QAction(QIcon(":/clean_tampon.png"), tr("Clear search"), this);
    connect(clearSearchA, &QAction::triggered, this, &PlayerWidget::clearFilter);
    searchTB->addAction(clearSearchA);
    QAction *doSearchA = new QAction(QIcon(":/stomp.png"), tr("Go searching!"), this);
    connect(doSearchA, &QAction::triggered, this, &PlayerWidget::doFilter);
    searchTB->addAction(doSearchA);
    QHBoxLayout *filterLayout = new QHBoxLayout;
    filterLayout->addWidget(mSearch);
    filterLayout->addWidget(searchTB);
    filterGB->setLayout(filterLayout);

    //directories
    QGroupBox *dirGB = new QGroupBox(QString(tr("Current Directory")));
    mDir = new QLineEdit;
    mDir->setEnabled(false);
    QToolBar *dirTB = new QToolBar;
    QImage upImg(":/stomp.png");
    upImg = upImg.mirrored();
    QIcon upDirIcon = QPixmap::fromImage(upImg);
    QAction *upDirA = new QAction(upDirIcon, tr("Up directory"), this);
    connect(upDirA, &QAction::triggered, this, &PlayerWidget::dirUp);
    dirTB->addAction(upDirA);
    QAction *homeDirA = new QAction(QIcon(":/home.png"), tr("Go home"), this);
    connect(homeDirA, &QAction::triggered, this, &PlayerWidget::dirHome);
    dirTB->addAction(homeDirA);
    dirTB->addAction(mRefreshA);
    QHBoxLayout *dirLayout = new QHBoxLayout;
    dirLayout->addWidget(mDir);
    dirLayout->addWidget(dirTB);
    dirGB->setLayout(dirLayout);

    //stack it up!
    mSearchDirStack = new QStackedLayout;
    mSearchDirStack->addWidget(filterGB);
    mSearchDirStack->addWidget(dirGB);

    //left widget
    QWidget *leftWidget = new QWidget;
    QVBoxLayout *leftWidgetL = new QVBoxLayout;
    leftWidgetL->addLayout(mSearchDirStack);
    leftWidgetL->addWidget(mView, 999);
    QHBoxLayout *selViewL = new QHBoxLayout;
    selViewL->addWidget(new QLabel(tr("View by:")));
    selViewL->addStretch();
    selViewL->addWidget(viewTB);
    selViewL->addStretch();
    leftWidgetL->addLayout(selViewL);
    leftWidget->setLayout(leftWidgetL);
    connect(this, &PlayerWidget::modelChanged, this, &PlayerWidget::doModelChanged);

    //now playing label
    mNowPlayingL = new QLabel;
    mNowPlayingL->setAlignment(Qt::AlignCenter);
    mNowPlayingL->setFont(QFont("courier new", 20, QFont::Bold));
    mNowPlayingL->setText(tr("(none)"));

    //song slider
    QLabel *l1 = new QLabel(tr("Song"));
    mSongSlider = new QSlider;
    mSongSlider->setOrientation(Qt::Horizontal);
    connect(mSongSlider, &QSlider::sliderMoved, this, &PlayerWidget::slide);
    mPos = new QLabel(tr("00:00"));
    mPos->setFont(QFont("courier"));
    QHBoxLayout *songSliderL = new QHBoxLayout;
    songSliderL->addWidget(l1);
    songSliderL->addWidget(mSongSlider);
    songSliderL->addWidget(mPos);

    //now playing info
    QString currentInfoGBS = QString(tr("Now Playing %1")).arg(QChar(0x26A5));
    QGroupBox *currentInfoGB = new QGroupBox(currentInfoGBS);
    mCurrentTE = new QTextEdit;
    mCurrentTE->setFont(QFont("courier"));
    mCurrentTE->setReadOnly(true);
    QVBoxLayout *currentInfoL = new QVBoxLayout;
    currentInfoL->addWidget(mCurrentTE);
    currentInfoGB->setLayout(currentInfoL);

    //misc info
    QString leftInfoGBS = QString(tr("%1 Misc. %1")).arg(QChar(0x26A5));
    QGroupBox *leftInfoGB = new QGroupBox(leftInfoGBS);
    mLeftTE = new QTextBrowser;
    mLeftTE->setOpenExternalLinks(true);
    mLeftTE->setFont(QFont("courier"));
    mLeftTE->setReadOnly(true);
    QVBoxLayout *leftInfoL = new QVBoxLayout;
    leftInfoL->addWidget(mLeftTE);
    leftInfoGB->setLayout(leftInfoL);

    //current right info
    QString rightInfoGBS = QString(tr("Selected %1")).arg(QChar(0x26A5));
    QGroupBox *rightInfoGB = new QGroupBox(rightInfoGBS);
    mRightTE = new QTextEdit;
    mRightTE->setFont(QFont("courier"));
    mRightTE->setReadOnly(true);
    QVBoxLayout *rightInfoL = new QVBoxLayout;
    rightInfoL->addWidget(mRightTE);
    rightInfoGB->setLayout(rightInfoL);

    //volume slider
    QLabel *l2 = new QLabel(tr("Volume"));
    mVolumeSlider = new QSlider;
    mVolumeSlider->setOrientation(Qt::Horizontal);
    mVolumeSlider->setMinimum(0);
    mVolumeSlider->setMaximum(100);
    mVolumePos = new QLabel(tr("000 %"));
    mVolumePos->setFont(QFont("courier"));
    connect(mVolumeSlider, &QSlider::valueChanged, mPlayer, &QMediaPlayer::setVolume);
    connect(mVolumeSlider, &QSlider::valueChanged, this, &PlayerWidget::volumeChanged);
    mVolumeSlider->setValue(33);
    QHBoxLayout *volumeL = new QHBoxLayout;
    volumeL->addWidget(l2);
    volumeL->addWidget(mVolumeSlider);
    volumeL->addWidget(mVolumePos);

    //center widget
    QWidget *centerWidget = new QWidget;
    QVBoxLayout *centerWidgetL = new QVBoxLayout;
    QSplitter *centerSplitter = new QSplitter;
    centerSplitter->setOrientation(Qt::Vertical);
    centerSplitter->addWidget(currentInfoGB);
    centerSplitter->addWidget(leftInfoGB);
    centerSplitter->addWidget(rightInfoGB);
    mToolBar = new QToolBar;
    centerWidgetL->addWidget(mToolBar);
    centerWidgetL->addWidget(mNowPlayingL);
    centerWidgetL->addLayout(songSliderL);
    centerWidgetL->addWidget(centerSplitter);
    centerWidgetL->addLayout(volumeL);
    centerWidget->setLayout(centerWidgetL);

    //playlist
    mPlayListModel = new QStandardItemModel;
    mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title");
    mPlayListView = new BeetView;
    mPlayListView->setAlternatingRowColors(true);
    mPlayListView->setModel(mPlayListModel);
    mPlayListView->setRootIsDecorated(false);
    mPlayListView->setSelectionMode(QAbstractItemView::ExtendedSelection);
    connect(mPlayListView, &BeetView::doubleClicked, this, &PlayerWidget::playCurrent);
    connect(mPlayListView->selectionModel(), &QItemSelectionModel::currentChanged, this, &PlayerWidget::rightCurrentChanged);
    QGroupBox *playListGB = new QGroupBox(tr("Playlist"));
    QVBoxLayout *playListL = new QVBoxLayout;
    playListL->addWidget(mPlayListView);
    playListGB->setLayout(playListL);

    //right widget
    QWidget *rightWidget = new QWidget;
    QVBoxLayout *rightWidgetL = new QVBoxLayout;
    rightWidgetL->addWidget(playListGB);
    rightWidget->setLayout(rightWidgetL);

    //stream data
    connect(this, &PlayerWidget::streamDataNeedsUpdate, this, &PlayerWidget::updateStreamData);

    //put it all together
    QSplitter *splitter = new QSplitter;
    splitter->addWidget(leftWidget);
    splitter->addWidget(centerWidget);
    splitter->addWidget(rightWidget);
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(splitter);
    setLayout(mainLayout);
}

void PlayerWidget::createActions(){
    QActionGroup *playAG = new QActionGroup(this);
    playAG->setExclusive(true);
    mPlayA = new QAction(QIcon(":/play.png"), tr("Play"), this);
    mPlayA->setCheckable(true);
    playAG->addAction(mPlayA);
    connect(mPlayA, &QAction::triggered, this, &PlayerWidget::doPlay);
    mPauseA = new QAction(QIcon(":/pause.png"), tr("Pause"), this);
    mPauseA->setCheckable(true);
    playAG->addAction(mPauseA);
    connect(mPauseA, &QAction::triggered, this, &PlayerWidget::doPause);
    mStopA = new QAction(QIcon(":/stop.png"), tr("Stop"), this);
    mStopA->setCheckable(true);
    playAG->addAction(mStopA);
    mStopA->setChecked(true);
    connect(mStopA, &QAction::triggered, this, &PlayerWidget::doStop);
    QAction *playOrPauseA = new QAction(tr("Play or Pause"), this);
    playOrPauseA->setObjectName("beetPlayerDoPlayOrPause");
    connect(playOrPauseA, &QAction::triggered, this, &PlayerWidget::doPlayOrPause);
    KGlobalAccel::self()->setShortcut(playOrPauseA, QList<QKeySequence>() << QKeySequence(Qt::Key_MediaPlay), KGlobalAccel::Autoloading);
    QAction *previousA = new QAction(QIcon(":/previous.png"), tr("Previous"), this);
    previousA->setObjectName("beetPlayerPrevious");
    connect(previousA, &QAction::triggered, this, &PlayerWidget::previous);
    KGlobalAccel::self()->setShortcut(previousA, QList<QKeySequence>() << QKeySequence(Qt::Key_Launch2), KGlobalAccel::Autoloading);
    QAction *nextA = new QAction(QIcon(":/next.png"), tr("Next"), this);
    nextA->setObjectName("beetPlayerNext");
    connect(nextA, &QAction::triggered, this, &PlayerWidget::next);
    KGlobalAccel::self()->setShortcut(nextA, QList<QKeySequence>() << QKeySequence(Qt::Key_Launch3), KGlobalAccel::Autoloading);
    QAction *addToPlayListA = new QAction(QIcon(":/belly_right.png"), tr("Add to playlist"), this);
    connect(addToPlayListA, &QAction::triggered, this, &PlayerWidget::addToPlayList);
    QAction *addToPlayListAndClearA = new QAction(QIcon(":/belly_right_and_clear.png"), tr("Clear and add"), this);
    connect(addToPlayListAndClearA, &QAction::triggered, this, &PlayerWidget::addToPlayListAndClear);
    QAction *expandA = new QAction(Helper::iconFromQChar(QChar(0x2640), 90), tr("Expand"), this);
    connect(expandA, &QAction::triggered, this, &PlayerWidget::expand);
    QAction *collapseAllA = new QAction(Helper::iconFromQChar(QChar(0x2642), 120), tr("Collapse all"), this);
    connect(collapseAllA, &QAction::triggered, mView, &BeetView::collapseAll);
    QAction *removeFromPlayListA = new QAction(QIcon(":/belly_left.png"), tr("Remove from playlist"), this);
    connect(removeFromPlayListA, &QAction::triggered, this, &PlayerWidget::removeFromPlayList);
    QAction *clearPlayListA = new QAction(QIcon(":/delete.png"), tr("Clear Playlist"), this);
    connect(clearPlayListA, &QAction::triggered, this, &PlayerWidget::clearPlayList);
    QAction *refreshA = new QAction(QIcon(":/refresh.png"), tr("Refresh..."), this);
    connect(refreshA, &QAction::triggered, this, &PlayerWidget::reindex);
    QAction *shufflePlayistA = new QAction(QIcon(":/shuffle.png"), tr("Shuffle playlist"), this);
    connect(shufflePlayistA, &QAction::triggered, this, &PlayerWidget::shufflePlayList);
    QAction *randomPlayA = new QAction(QIcon(":/dice.png"), tr("Play random"), this);
    connect(randomPlayA, &QAction::triggered, this, &PlayerWidget::randomPlay);
    QAction *muteA = new QAction(QIcon(":/mute.png"), tr("Mute"), this);
    muteA->setObjectName("beetPlayerMute");
    muteA->setCheckable(true);
    connect(muteA, &QAction::triggered, this, &PlayerWidget::mute);
    KGlobalAccel::self()->setShortcut(muteA, QList<QKeySequence>() << QKeySequence(Qt::Key_VolumeMute), KGlobalAccel::Autoloading);
    QAction *volumeUpA = new QAction(tr("Increase volume"), this);
    volumeUpA->setObjectName("beetPlayerIncVolume");
    connect(volumeUpA, &QAction::triggered, this, &PlayerWidget::volumeUp);
    KGlobalAccel::self()->setShortcut(volumeUpA, QList<QKeySequence>() << QKeySequence(Qt::Key_VolumeUp), KGlobalAccel::Autoloading);
    QAction *volumeDownA = new QAction(tr("Decrease volume"), this);
    volumeDownA->setObjectName("beetPlayerDecVolume");
    connect(volumeDownA, &QAction::triggered, this, &PlayerWidget::volumeDown);
    KGlobalAccel::self()->setShortcut(volumeDownA, QList<QKeySequence>() << QKeySequence(Qt::Key_VolumeDown), KGlobalAccel::Autoloading);
    QAction *configA = Globals::instance()->action(Globals::ConfigAction);
    QAction *helpA = new QAction(Helper::iconFromQChar(QChar(0x00BF), 50), tr("Help"), this);
    QMenu *helpM = new QMenu;
    QAction *aboutThisA = new QAction(tr("About..."), this);
    connect(aboutThisA, &QAction::triggered, this, &PlayerWidget::aboutDlg);;
    helpM->addAction(aboutThisA);
    QAction *aboutQtA = new QAction(tr("About Qt..."), this);
    connect(aboutQtA, &QAction::triggered, qApp, &QApplication::aboutQt);
    helpM->addAction(aboutQtA);
    helpA->setMenu(helpM);
    QAction *miscPrintA = new QAction(QIcon(":/clean_tampon.png"), tr("List selected"), this);
    connect(miscPrintA, &QAction::triggered, this, &PlayerWidget::printList);
    QAction *miscMusicBrainzRightA = new QAction(QIcon(":/bizarre_amputee.png"), tr("Search Musicbrainz"), this);
    connect(miscMusicBrainzRightA, &QAction::triggered, this, &PlayerWidget::searchMusicbrainzRight);
    QAction *miscMusicBrainzLeftA = new QAction(QIcon(":/bizarre_amputee.png"), tr("Search Musicbrainz"), this);
    connect(miscMusicBrainzLeftA, &QAction::triggered, this, &PlayerWidget::searchMusicbrainzLeft);
    QAction *miscFilterFromPlaylistA = new QAction(QIcon(":/chastity_belt.png"), tr("Filter artist"), this);
    connect(miscFilterFromPlaylistA, &QAction::triggered, this, &PlayerWidget::filterFromPlaylist);
    QAction *addToWebRadioA = new QAction(QIcon(":/dog_hood.png"), tr("Add Webradio"), this);
    connect(addToWebRadioA, &QAction::triggered, this, &PlayerWidget::addWebRadio);
    mView->addAction(addToPlayListA);
    mView->addAction(addToPlayListAndClearA);
    mView->addAction(Helper::createSeparator(this));
    mView->addAction(expandA);
    mView->addAction(collapseAllA);
    mView->addAction(Helper::createSeparator(this));
    mView->addAction(mSelectFilesA);
    mView->addAction(mDeselectAllA);
    mView->addAction(mDeleteFilesA);
    mView->addAction(Helper::createSeparator(this));
    mView->addAction(miscPrintA);
    mView->addAction(miscMusicBrainzLeftA);
    mView->addAction(Helper::createSeparator(this));
    mView->addAction(mRefreshA);
    mView->addAction(Helper::createSeparator(this));
    mView->addAction(addToWebRadioA);
    mView->addAction(Helper::createSeparator(this));
    mView->addAction(randomPlayA);
    mPlayListView->addAction(removeFromPlayListA);
    mPlayListView->addAction(Helper::createSeparator(this));
    mPlayListView->addAction(miscMusicBrainzRightA);
    mPlayListView->addAction(miscFilterFromPlaylistA);
    mPlayListView->addAction(Helper::createSeparator(this));
    mPlayListView->addAction(shufflePlayistA);
    mPlayListView->addAction(clearPlayListA);
    QWidget* spacer1 = new QWidget();
    spacer1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    mToolBar->addWidget(spacer1);
    mToolBar->addAction(mPlayA);
    mToolBar->addAction(mPauseA);
    mToolBar->addAction(mStopA);
    mToolBar->addSeparator();
    mToolBar->addAction(previousA);
    mToolBar->addAction(nextA);
    mToolBar->addSeparator();
    mToolBar->addAction(addToPlayListA);
    mToolBar->addAction(removeFromPlayListA);
    mToolBar->addAction(clearPlayListA);
    mToolBar->addAction(shufflePlayistA);
    mToolBar->addAction(randomPlayA);
    mToolBar->addSeparator();
    mToolBar->addAction(refreshA);
    mToolBar->addSeparator();
    mToolBar->addAction(muteA);
    mToolBar->addSeparator();
    mToolBar->addAction(configA);
    mToolBar->addSeparator();
    mToolBar->addAction(helpA);
    QWidget* spacer2 = new QWidget();
    spacer2->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    mToolBar->addWidget(spacer2);

    QMenu *trayMenu = new QMenu;
    trayMenu->addAction(mPlayA);
    trayMenu->addAction(mStopA);
    trayMenu->addAction(mPauseA);
    trayMenu->addSeparator();
    trayMenu->addAction(nextA);
    trayMenu->addAction(previousA);
    trayMenu->addSeparator();
    QAction *quitA = new QAction(tr("Quit"), this);
    connect(quitA, &QAction::triggered, qApp, &QApplication::quit);
    trayMenu->addAction(quitA);
    mTrayIcon->setContextMenu(trayMenu);
}

void PlayerWidget::populateByArtist(QStandardItem *parent, const QString &filter){
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QStandardItem *root = parent;
    QSqlQuery artistsQ(db);
    if(!filter.isEmpty()){
        artistsQ.prepare("SELECT iartists_id, tartists_name FROM artists WHERE tartists_name ~ :f ORDER BY tartists_name ASC");
        artistsQ.bindValue(":f", filter);
    }else{
        artistsQ.prepare("SELECT iartists_id, tartists_name FROM artists ORDER BY tartists_name ASC");
    }
    QSqlQuery albumQ(db);
    albumQ.prepare("SELECT DISTINCT(songs.ialbums_id), talbum_name, siyear FROM songs, albums WHERE songs.iartists_id = :artistid AND songs.ialbums_id = albums.ialbums_id ORDER BY siyear ASC");
    QSqlQuery songQ(db);
    songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, ilength FROM songs WHERE ialbums_id = :alid AND iartists_id = :arid ORDER BY sipos ASC");
    QIcon songIcon(":/song.png");
    QIcon albumIcon(":/album.png");
    QIcon artistIcon(":/artist.png");

    //read data
    artistsQ.exec();
    while(artistsQ.next()){
        QStandardItem *curArtist = new QStandardItem;
        curArtist->setEditable(false);
        curArtist->setFont(QFont("courier"));
        curArtist->setText(artistsQ.value(1).toString());
        curArtist->setIcon(artistIcon);
        curArtist->setData(Artist, TypeRole);
        curArtist->setData(artistsQ.value(0).toInt(), IdRole);
        curArtist->setData(artistsQ.value(1), ArtistRole);
        root->appendRow(curArtist);
        albumQ.bindValue(":artistid", artistsQ.value(0));
        albumQ.exec();
        while(albumQ.next()){
            QStandardItem *curAlbum = new QStandardItem;
            curAlbum->setEditable(false);
            curAlbum->setFont(QFont("courier"));
            QString albumText = QString(tr("%1 - %2")).arg(QString::number(albumQ.value(2).toInt())).arg(albumQ.value(1).toString());
            curAlbum->setText(albumText);
            curAlbum->setIcon(albumIcon);
            curAlbum->setData(Album, TypeRole);
            curAlbum->setData(albumQ.value(0), IdRole);
            curAlbum->setData(artistsQ.value(1), ArtistRole);
            curAlbum->setData(albumQ.value(1), AlbumRole);
            curArtist->appendRow(curAlbum);
            songQ.bindValue(":alid", albumQ.value(0));
            songQ.bindValue(":arid", artistsQ.value(0));
            songQ.exec();
            while(songQ.next()){
                QStandardItem *curSong = new QStandardItem;
                curSong->setEditable(false);
                curSong->setFont(QFont("courier"));
                QString songText = QString(tr("%1 - %2")).arg(songQ.value(0).toInt(), 3, 10, QChar('0')).arg(songQ.value(1).toString());
                curSong->setText(songText);
                curSong->setIcon(songIcon);
                curSong->setData(Song, TypeRole);
                curSong->setData(songQ.value(0), IdRole);
                curSong->setData(songQ.value(2), FullPathRole);
                curSong->setData(songQ.value(3), GenreRole);
                curSong->setData(artistsQ.value(1), ArtistRole);
                curSong->setData(songQ.value(1), TitleRole);
                curSong->setData(songQ.value(4), LengthRole);
                curSong->setData(albumQ.value(1), AlbumRole);
                curAlbum->appendRow(curSong);
            }
        }
    }
}

void PlayerWidget::populateByAlbum(QStandardItem *parent, const QVariant &filter, int type){
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QIcon albumIcon(":/album.png");
    QIcon songIcon(":/song.png");
    QSqlQuery albumQ(db);
    if(type == EmptyType){
        albumQ.prepare("SELECT DISTINCT(ialbums_id), talbum_name, siyear FROM albums ORDER BY talbum_name");
    }else if(type == FilterType){
        albumQ.prepare("SELECT DISTINCT(songs.ialbums_id), talbum_name, siyear FROM songs, albums WHERE talbum_name ~ :album AND songs.ialbums_id = albums.ialbums_id ORDER BY siyear ASC");
        albumQ.bindValue(":album", filter);
    }
    albumQ.exec();
    while(albumQ.next()){
        QHash<QString, int> artistcount;
        QStandardItem *curAlbum = new QStandardItem;
        curAlbum->setEditable(false);
        curAlbum->setFont(QFont("courier"));

        curAlbum->setIcon(albumIcon);
        curAlbum->setData(Album, TypeRole);
        curAlbum->setData(albumQ.value(0), IdRole);
        parent->appendRow(curAlbum);
        QSqlQuery songQ = QSqlQuery(db);
        songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name, ilength FROM songs, artists, albums WHERE albums.ialbums_id = :id AND songs.iartists_id = artists.iartists_id and songs.ialbums_id = albums.ialbums_id ORDER BY sipos");
        songQ.bindValue(":id", albumQ.value(0));
        songQ.exec();
        while(songQ.next()){
            QStandardItem *curSong = new QStandardItem;
            curSong->setEditable(false);
            curSong->setFont(QFont("courier"));
            QString songText = QString(tr("%1 - %2 - %3")).arg(songQ.value(0).toInt(), 3, 10, QChar('0')).arg(songQ.value(1).toString()).arg(songQ.value(4).toString());
            curSong->setText(songText);
            curSong->setIcon(songIcon);
            curSong->setData(Song, TypeRole);
            curSong->setData(songQ.value(0), IdRole);
            curSong->setData(songQ.value(2), FullPathRole);
            curSong->setData(songQ.value(3), GenreRole);
            curSong->setData(songQ.value(4), ArtistRole);
            ++artistcount[songQ.value(4).toString()];
            curSong->setData(songQ.value(1), TitleRole);
            curSong->setData(songQ.value(5), AlbumRole);
            curSong->setData(songQ.value(6), LengthRole);
            curAlbum->appendRow(curSong);
        }
        QString albumText;
        if(artistcount.keys().count() > 1){
            albumText = QString(tr("%1 - VA - (%2)")).arg(albumQ.value(1).toString()).arg(QString::number(albumQ.value(2).toInt()));
        }else{
            albumText = QString(tr("%1 - %2 - (%3)")).arg(albumQ.value(1).toString()).arg(artistcount.keys().first()). arg(QString::number(albumQ.value(2).toInt()));
        }
        curAlbum->setText(albumText);
    }
}

void PlayerWidget::populateBySong(QStandardItem *parent, const QVariant &filter, int type){
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QStandardItem *root = parent;
    QIcon songIcon(":/song.png");
    QSqlQuery songQ = QSqlQuery(db);
    if(type == EmptyType){
        songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name, ilength FROM songs, artists, albums WHERE songs.iartists_id = artists.iartists_id and songs.ialbums_id = albums.ialbums_id ORDER BY ttitle ASC");
    }else if(type == FilterType){
        songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name, ilength FROM songs, artists, albums WHERE ttitle ~ :f AND songs.iartists_id = artists.iartists_id and songs.ialbums_id = albums.ialbums_id ORDER BY ttitle ASC");
        songQ.bindValue(":f", filter);
    }else if(type == IdType){
        songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name FROM songs, artists, albums WHERE albums.ialbums_id = :id AND songs.iartists_id = artists.iartists_id and songs.ialbums_id = albums.ialbums_id ORDER BY sipos");
        songQ.bindValue(":id", filter);
    }
    songQ.exec();
    while(songQ.next()){
        QStandardItem *curSong = new QStandardItem;
        curSong->setEditable(false);
        curSong->setFont(QFont("courier"));
        QString songText;
        if(type == IdType){
            songText = QString(tr("%1 - %2")).arg(songQ.value(0).toInt(), 3, 10, QChar('0')).arg(songQ.value(1).toString());
        }else{
            songText = QString(tr("%1 (%2)")).arg(songQ.value(1).toString()).arg(songQ.value(4).toString());
        }
        curSong->setText(songText);
        curSong->setIcon(songIcon);
        curSong->setData(Song, TypeRole);
        curSong->setData(songQ.value(0), IdRole);
        curSong->setData(songQ.value(2), FullPathRole);
        curSong->setData(songQ.value(3), GenreRole);
        curSong->setData(songQ.value(4), ArtistRole);
        curSong->setData(songQ.value(1), TitleRole);
        curSong->setData(songQ.value(5), AlbumRole);
        curSong->setData(songQ.value(6), LengthRole);
        root->appendRow(curSong);
    }
}

void PlayerWidget::populateByDate(QStandardItem *parent){
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QStandardItem *root = parent;
    QIcon dateIcon(":/bizarre_amputee.png");
    root->setIcon(dateIcon);
    QIcon albumIcon(":/album.png");
    QIcon songIcon(":/song.png");
    QSqlQuery q1("SELECT DISTINCT(albums.ialbums_id), talbum_name, dadded, tartists_name FROM albums, artists, songs WHERE albums.ialbums_id = songs.ialbums_id AND songs.iartists_id = artists.iartists_id ORDER BY dadded DESC", db);
    QSqlQuery songQ(db);
    songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name, ilength FROM songs, artists, albums WHERE albums.ialbums_id = :id AND songs.iartists_id = artists.iartists_id and songs.ialbums_id = albums.ialbums_id ORDER BY sipos");
    QHash<int, QStandardItem*> years;
    QHash<QString, QStandardItem*> yearMonths;
    while(q1.next()){
        QDate added = q1.value(2).toDate();
        int year = added.year();
        QStandardItem *yearItem = nullptr;
        QStandardItem *ymItem = nullptr;
        if(years.contains(year)){
            yearItem = years.value(year);
        }else{
            yearItem = new QStandardItem;
            yearItem->setText(QString::number(year));
            years.insert(year, yearItem);
            yearItem->setIcon(dateIcon);
            root->appendRow(yearItem);
        }
        QString yearMonth = QString("%1-%2").arg(QString::number(added.year())).arg(added.month(), 2, 10, QChar('0'));
        if(yearMonths.contains(yearMonth)){
            ymItem = yearMonths.value(yearMonth);
        }else{
            ymItem = new QStandardItem;
            ymItem->setText(yearMonth);
            ymItem->setIcon(dateIcon);
            yearMonths.insert(yearMonth, ymItem);
            yearItem->appendRow(ymItem);
        }
        QStandardItem *albumItem = new QStandardItem;
        QString albumText = QString("%1 - %2").arg(q1.value(3).toString()).arg(q1.value(1).toString());
        albumItem->setText(albumText);
        albumItem->setIcon(albumIcon);
        ymItem->appendRow(albumItem);
        songQ.bindValue(":id", q1.value(0));
        songQ.exec();
        while(songQ.next()){
            QStandardItem *curSong = new QStandardItem;
            curSong->setEditable(false);
            curSong->setFont(QFont("courier"));
            QString songText = QString(tr("%1 - %2 - %3")).arg(songQ.value(0).toInt(), 3, 10, QChar('0')).arg(songQ.value(1).toString()).arg(songQ.value(4).toString());
            curSong->setText(songText);
            curSong->setIcon(songIcon);
            curSong->setData(Song, TypeRole);
            curSong->setData(songQ.value(0), IdRole);
            curSong->setData(songQ.value(2), FullPathRole);
            curSong->setData(songQ.value(3), GenreRole);
            curSong->setData(songQ.value(4), ArtistRole);
            curSong->setData(songQ.value(1), TitleRole);
            curSong->setData(songQ.value(5), AlbumRole);
            curSong->setData(songQ.value(6), LengthRole);
            albumItem->appendRow(curSong);
        }
    }
}

void PlayerWidget::populateByGenre(QStandardItem *parent, const QString &filter){
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QStandardItem *root = parent;
    QIcon songIcon(":/song.png");
    QIcon genreIcon(":/genre.png");
    QSqlQuery genreQ(db);
    if(filter.isEmpty()){
        genreQ.prepare("SELECT igenres_id, tgenres_name FROM genres ORDER BY tgenres_name");
    }else{
        genreQ.prepare("SELECT igenres_id, tgenres_name FROM genres WHERE tgenres_name ~ :f");
        genreQ.bindValue(":f", filter);
    }
    genreQ.exec();
    while(genreQ.next()){
        QStandardItem *curGenre = new QStandardItem;
        curGenre->setEditable(false);
        curGenre->setFont(QFont("courier"));
        curGenre->setText(genreQ.value(1).toString());
        curGenre->setIcon(genreIcon);
        curGenre->setData(Genre, TypeRole);
        curGenre->setData(genreQ.value(0), IdRole);
        root->appendRow(curGenre);
        QSqlQuery songQ = QSqlQuery(db);
        songQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name, ilength FROM songs, artists, albums WHERE igenres_id = :id AND songs.iartists_id = artists.iartists_id AND songs.ialbums_id = albums.ialbums_id ORDER BY ttitle ASC");
        songQ.bindValue(":id", genreQ.value(0));
        songQ.exec();
        while(songQ.next()){
            QStandardItem *curSong = new QStandardItem;
            curSong->setEditable(false);
            curSong->setFont(QFont("courier"));
            QString songText = QString(tr("%1 (%2)")).arg(songQ.value(1).toString()).arg(songQ.value(4).toString());
            curSong->setText(songText);
            curSong->setIcon(songIcon);
            curSong->setData(Song, TypeRole);
            curSong->setData(songQ.value(0), IdRole);
            curSong->setData(songQ.value(2), FullPathRole);
            curSong->setData(songQ.value(3), GenreRole);
            curSong->setData(songQ.value(4), ArtistRole);
            curSong->setData(songQ.value(1), TitleRole);
            curSong->setData(songQ.value(5), AlbumRole);
            curSong->setData(songQ.value(6), LengthRole);
            curGenre->appendRow(curSong);
        }
    }
}

void PlayerWidget::populateByWebradio(QStandardItem *parent){
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QStandardItem *root = parent;
    QIcon wrIcon(":/dog_hood.png");
    QSqlQuery wrQ = QSqlQuery(db);
    wrQ.prepare("SELECT tdescription, turl FROM webradio ORDER BY tdescription DESC");
    wrQ.exec();
    while(wrQ.next()){
        QStandardItem *curWr = new QStandardItem;
        curWr->setEditable(false);
        curWr->setFont(QFont("courier"));
        curWr->setText(wrQ.value(0).toString());
        curWr->setIcon(wrIcon);
        curWr->setData(wrQ.value(1), UrlRole);
        root->appendRow(curWr);
    }
}

void PlayerWidget::doPopulateByFolder(){
    mCurrentModel = mFolderModel;
    mFolderModel->clear();
    mFolderModel->setHorizontalHeaderLabels(QStringList() << tr("Name"));
    QDir d(mCurDir);
    if(!d.exists()){
        d = QDir(QDir::homePath());
        mCurDir = d.absolutePath();
    }
    QMimeDatabase db;
    QStandardItem *root = mFolderModel->invisibleRootItem();
    QIcon songIcon(":/song.png");
    QIcon otherIcon(":/belly_right_and_clear.png");
    QIcon dirIcon(":/folder.png");
    QFileInfoList fl = d.entryInfoList(QStringList() << "*", QDir::Files | QDir::Dirs | QDir::NoDot, QDir::Name | QDir::DirsFirst);
    foreach(QFileInfo fi, fl){
        QStandardItem *cur = new QStandardItem;
        cur->setEditable(false);
        cur->setText(fi.fileName());
        if(fi.isDir()){
            cur->setIcon(dirIcon);
        }else if(fi.isFile()){
            QMimeType mt = db.mimeTypeForFile(fi);
            if(mt.name().startsWith("audio")){
                cur->setIcon(songIcon);
                cur->setData(Song, TypeRole);
                cur->setData(-1, IdRole);
                TagLib::FileRef file(fi.absoluteFilePath().toUtf8());
                if(!file.isNull()){
                    QString artist = QString::fromStdWString(file.tag()->artist().toWString());
                    cur->setData(artist.toLower(), ArtistRole);
                    QString album = QString::fromStdWString(file.tag()->album().toWString());
                    cur->setData(album.toLower(), AlbumRole);
                    QString genre = QString::fromStdWString(file.tag()->genre().toWString());
                    cur->setData(genre.toLower(), GenreRole);
                    QString title = QString::fromStdWString(file.tag()->title().toWString());
                    cur->setData(title.toLower(), TitleRole);
                }
            }else{
                cur->setIcon(otherIcon);
            }
        }
        cur->setData(fi.absoluteFilePath(), FullPathRole);
        root->appendRow(cur);
    }
    mView->setModel(mFolderModel);
    emit viewModeChanged(tr("Folder"));
    emit modelChanged();
}

void PlayerWidget::doPopulateByWebradio(){
    mCurrentModel = mWebRadioModel;
    mWebRadioModel->clear();
    mWebRadioModel->setHorizontalHeaderLabels(QStringList() << tr("Description"));
    QStandardItem *root = mWebRadioModel->invisibleRootItem();
    populateByWebradio(root);
    mView->setModel(mWebRadioModel);
    emit viewModeChanged("WebRadio");
    emit modelChanged();
}

void PlayerWidget::doModelChanged(){
    if(mCurrentModel == mFolderModel){
        mSelectFilesA->setEnabled(true);
        mDeselectAllA->setEnabled(true);
        mDeleteFilesA->setEnabled(true);
        mRefreshA->setEnabled(true);
        mSearchDirStack->setCurrentIndex(1);
    }else{
        mSelectFilesA->setEnabled(false);
        mDeselectAllA->setEnabled(false);
        mDeleteFilesA->setEnabled(false);
        mRefreshA->setEnabled(false);
        mSearchDirStack->setCurrentIndex(0);
    }
}

void PlayerWidget::viewDoubleClicked(const QModelIndex &idx){
    mPlayer->stop();
    const QStandardItemModel *model = static_cast<const QStandardItemModel*>(idx.model());
    if(model == mViewModel || model == mSearchModel){
        addToPlayList();
        return;
    }
    if(model == mWebRadioModel){
        QString url(idx.data(UrlRole).toString());
        mPlayListModel->clear();
        mPlayListModel->setHorizontalHeaderLabels(QStringList() << tr("Title"));
        mRightTE->clear();
        playUrl(url);
        return;
    }
    QString fp(idx.data(FullPathRole).toString());
    QFileInfo fi(fp);
    if(fi.isDir()){
        mCurDir = fp;
        mDir->setText(fp);
        doPopulateByFolder();
    }else{
        addToPlayList();
    }
}

void PlayerWidget::rightCurrentChanged(const QModelIndex &cur, const QModelIndex &prev){
    Q_UNUSED(prev)
    QString fullPath(cur.data(FullPathRole).toString());
    TagLib::FileRef file(QString(fullPath).toUtf8());
    fillWithText(mRightTE, file);
}

void PlayerWidget::doPlay(){
    if(mPlayer->state() == QMediaPlayer::PausedState || mIsStream){
        mPlayer->play();
        mPlayA->setChecked(true);
        emit playModeChanged(tr("Playing"));
        emit setWinTitle(mCurWinTitle);
        mTrayIcon->setIcon(QIcon(":/play.png"));
        mTrayIcon->setToolTip(mCurToolTip);
        return;
    }
    int playListCount = mPlayListModel->rowCount();
    if(playListCount == 0){
        emit message(tr("Playlist is empty! Dazed and confused, but trying to continue..."));
        doStop();
        return;
    }
    QModelIndexList sel = mPlayListView->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        mPlayListView->selectionModel()->select(mPlayListModel->index(0, 0), QItemSelectionModel::SelectCurrent);
        sel = mPlayListView->selectionModel()->selectedRows();
    }
    playCurrent(sel.first());
}

void PlayerWidget::doStop(){
    mPlayer->stop();
    mStopA->setChecked(true);
    emit playModeChanged(tr("Stopped"));
    QString winTitle = QString(tr("%1 [Stopped]")).arg(qApp->applicationName());
    emit setWinTitle(winTitle);
    mTrayIcon->setIcon(QIcon(":/stop.png"));
    mTrayIcon->setToolTip(tr("[Stopped]"));
}

void PlayerWidget::doPause(){
    mPlayer->pause();
    mPauseA->setChecked(true);
    emit playModeChanged(tr("Paused"));
    QString winTitle = QString(tr("%1 [Paused]")).arg(qApp->applicationName());
    emit setWinTitle(winTitle);
    mTrayIcon->setIcon(QIcon(":/pause.png"));
    mTrayIcon->setToolTip(tr("[Paused]"));
}

void PlayerWidget::doPlayOrPause(){
    if(mPlayA->isChecked()){
        doPause();
    }else if(mPauseA->isChecked() || mStopA->isChecked()){
        doPlay();
    }
}

void PlayerWidget::doSelectFiles(){
    bool ok;
    QString pattern = QInputDialog::getText(this, tr("Select files..."), tr("Pattern:"), QLineEdit::Normal, QString(), &ok);
    if(ok && !pattern.isEmpty()){
        QList<QStandardItem*> items = mFolderModel->findItems(pattern, Qt::MatchWildcard);
        mView->selectionModel()->clear();
        foreach(QStandardItem *i, items){
            QModelIndex idx = mFolderModel->indexFromItem(i);
            mView->selectionModel()->select(idx, QItemSelectionModel::Rows | QItemSelectionModel::Toggle);
        }
    }
}

void PlayerWidget::doDeselect(){
    mView->selectionModel()->clearSelection();
}

void PlayerWidget::doDeleteFiles(){
    QModelIndexList sel = mView->selectionModel()->selectedRows();
    if(!sel.isEmpty()){
        QString msg = QString(tr("Really delete %1 file(s)?")).arg(QString::number(sel.count()));
        int r = QMessageBox::question(this, tr("Delete files..."), msg);
        if(r == QMessageBox::Yes){
            foreach(QModelIndex i, sel){
                QString cur = i.data(FullPathRole).toString();
                QFileInfo file(cur);
                if(file.isFile()){
                    QFile::remove(cur);
                    continue;
                }
                if(file.isDir()){
                    QString d = file.absoluteFilePath();
                    QDir toDel(d);
                    toDel.removeRecursively();
                }
            }
            doPopulateByFolder();
        }
    }
}

void PlayerWidget::volumeUp(){
    adjustVolume(2);
}

void PlayerWidget::volumeDown(){
    adjustVolume(-2);
}

void PlayerWidget::dirUp(){
    QDir d(mCurDir);
    d.cdUp();
    mDir->setText(d.absolutePath());
    mCurDir = d.absolutePath();
    doPopulateByFolder();
}

void PlayerWidget::dirHome(){
    mCurDir = QDir::homePath();
    mDir->setText(mCurDir);
    doPopulateByFolder();
}

void PlayerWidget::showVolume(){
    int volume = mVolumeSlider->value();
    QString msg = QString(tr("Volume: %1%")).arg(volume, 3, 10, QChar('0'));
    mTrayIcon->showMessage(msg, QString(), QSystemTrayIcon::Information, 2000);
}

void PlayerWidget::adjustVolume(int by){
    int curVol = mVolumeSlider->value();
    int newVol = curVol + by;
    if(newVol > 100){
        newVol = 100;
    }
    if(newVol < 0){
        newVol = 0;
    }
    mVolumeSlider->setValue(newVol);
}

void PlayerWidget::fillWithText(QTextEdit *te, const TagLib::FileRef &fr){
    if(fr.isNull()){
        return;
    }
    QString artist = QString::fromStdWString(fr.tag()->artist().toWString());
    QString album = QString::fromStdWString(fr.tag()->album().toWString());
    QString title = QString::fromStdWString(fr.tag()->title().toWString());
    QString genre = QString::fromStdWString(fr.tag()->genre().toWString());
    quint16 track = fr.tag()->track();
    quint16 year = fr.tag()->year();
    te->clear();
    te->append(QString("%1 %2").arg(tr("Artist:"), -20).arg(artist));
    te->append(QString("%1 %2").arg(tr("Album:"), -20).arg(album));
    te->append(QString("%1 %2").arg(tr("Title:"), -20).arg(title));
    te->append(QString("%1 %2").arg(tr("Genre:"), -20).arg(genre));
    te->append(QString("%1 %2").arg(tr("Track:"), -20).arg(track, 3, 10, QChar('0')));
    te->append(QString("%1 %2").arg(tr("Year:"), -20).arg(year, 4, 10));
    te->append(QString("%1 %2 kb/s").arg(tr("Bitrate:"), -20).arg(fr.audioProperties()->bitrate(), 4, 10, QChar('0')));
}

void PlayerWidget::setNowPlaying(const QString &what){
    QFont f = mNowPlayingL->font();
    int width = mNowPlayingL->width();
    QFontMetrics fm(f);
    QString newText = fm.elidedText(what, Qt::ElideRight, width);
    mNowPlayingL->setText(newText);
}

void PlayerWidget::recurse(const QModelIndex &parent){
    for(int i = 0; i < mCurrentModel->rowCount(parent); ++i){
        QModelIndex cur = mCurrentModel->index(i, 0, parent);
        int type = cur.data(TypeRole).toInt();
        if(type == Song){
            addSong(cur);
        }else{
            recurse(cur);
        }
    }
}

void PlayerWidget::addSong(const QModelIndex &idx){
    QStandardItem *root = mPlayListModel->invisibleRootItem();
    QString title = idx.data(TitleRole).toString();
    QString artist = idx.data(ArtistRole).toString();
    QString album = idx.data(AlbumRole).toString();
    QStringList args = QStringList() << title << artist << album;
    foreach(QString s, args){
        if(s.isEmpty()){
            s = QString(tr("n/a"));
        }
    }
    QString display = QString(tr("%1 - %2 - %3")).arg(artist).arg(title).arg(album);
    QStandardItem *item = new QStandardItem;
    item->setEditable(false);
    item->setFont(QFont("courier"));
    item->setText(display);
    item->setIcon(QIcon(":/song.png"));
    item->setData(idx.data(FullPathRole), FullPathRole);
    int len = idx.data(LengthRole).toInt();
    if(len == 0){
        len = Helper::lengthInSeconds(idx.data(FullPathRole).toString());
    }
    item->setData(len, LengthRole);
    item->setData(artist, ArtistRole);
    item->setData(album, AlbumRole);
    root->appendRow(item);
    mPlayListLength += len;
}

void PlayerWidget::doPopulateByArtist(){
    qApp->setOverrideCursor(Qt::BusyCursor);
    mView->setModel(mViewModel);
    mCurrentModel = mViewModel;
    mViewModel->clear();
    mViewModel->setHorizontalHeaderLabels(QStringList() << tr("Artist name"));
    QStandardItem *root = mViewModel->invisibleRootItem();
    emit message(QString(tr("Populating by artist... Please wait!")));
    qApp->processEvents();
    populateByArtist(root, QString());
    qApp->restoreOverrideCursor();
    mViewByArtistA->setChecked(true);
    emit viewModeChanged(tr("Artist"));
    emit message(QString(tr("Done!")));
    emit modelChanged();
}

void PlayerWidget::doPopulateByAlbum(){
    qApp->setOverrideCursor(Qt::BusyCursor);
    mView->setModel(mViewModel);
    mCurrentModel = mViewModel;
    mViewModel->clear();
    mViewModel->setHorizontalHeaderLabels(QStringList() << tr("Album name"));
    QStandardItem *root = mViewModel->invisibleRootItem();
    emit message(QString(tr("Populating by album... Please wait!")));
    qApp->processEvents();
    populateByAlbum(root, QString(), EmptyType);
    qApp->restoreOverrideCursor();
    emit viewModeChanged(tr("Album"));
    emit message(QString(tr("Done!")));
    emit modelChanged();
}

void PlayerWidget::doPopulateByGenre(){
    qApp->setOverrideCursor(Qt::BusyCursor);
    mView->setModel(mViewModel);
    mCurrentModel = mViewModel;
    mViewModel->clear();
    mViewModel->setHorizontalHeaderLabels(QStringList() << tr("Genre name"));
    QStandardItem *root = mViewModel->invisibleRootItem();
    emit message(QString(tr("Populating by genre... Please wait!")));
    qApp->processEvents();
    populateByGenre(root, QString());
    qApp->restoreOverrideCursor();
    emit viewModeChanged(tr("Genre"));
    emit message(QString(tr("Done!")));
    emit modelChanged();
}

void PlayerWidget::doPopulateBySong(){
    qApp->setOverrideCursor(Qt::BusyCursor);
    mView->setModel(mViewModel);
    mCurrentModel = mViewModel;
    mViewModel->clear();
    mViewModel->setHorizontalHeaderLabels(QStringList() << tr("Genre name"));
    QStandardItem *root = mViewModel->invisibleRootItem();
    emit message(QString(tr("Populating by song... Please wait!")));
    qApp->processEvents();
    populateBySong(root, QString(), EmptyType);
    qApp->restoreOverrideCursor();
    emit viewModeChanged(tr("Song"));
    emit message(QString(tr("Done!")));
    emit modelChanged();
}

void PlayerWidget::doPopulateByDate(){
    qApp->setOverrideCursor(Qt::BusyCursor);
    mView->setModel(mViewModel);
    mCurrentModel = mViewModel;
    mViewModel->clear();
    mViewModel->setHorizontalHeaderLabels(QStringList() << tr("Date added"));
    QStandardItem *root = mViewModel->invisibleRootItem();
    emit message(QString(tr("Populating by date... Please wait!")));
    qApp->processEvents();
    populateByDate(root);
    qApp->restoreOverrideCursor();
    emit viewModeChanged(tr("Date"));
    emit message(QString(tr("Done!")));
    emit modelChanged();
}

void PlayerWidget::doFilter(){
    mSearchDirStack->setCurrentIndex(0);
    QString filter = mSearch->text();
    mSearchModel->clear();
    mSearchModel->setHorizontalHeaderLabels(QStringList() << tr("Name"));
    mView->setModel(mSearchModel);
    mCurrentModel = mSearchModel;
    emit viewModeChanged(tr("Search"));
    mSearchA->setChecked(true);
    if(filter.isEmpty()){
        emit message(QString(tr("No search term set! Cowardly refusing to do anything!")));
        return;
    }
    QString msg = QString(tr("Searching for \"%1\"")).arg(filter);
    emit message(msg);
    qApp->setOverrideCursor(Qt::BusyCursor);
    QStandardItem *root = mSearchModel->invisibleRootItem();
    QStandardItem *artistI = new QStandardItem(QIcon(":/quadd.png"), tr("Artists"));
    artistI->setFont(QFont("courier", -1, QFont::Bold));
    root->appendRow(artistI);
    populateByArtist(artistI, filter);
    QStandardItem *albumI = new QStandardItem(QIcon(":/quadd.png"), tr("Albums"));
    albumI->setFont(QFont("courier", -1, QFont::Bold));
    root->appendRow(albumI);
    populateByAlbum(albumI, filter, FilterType);
    QStandardItem *genreI = new QStandardItem(QIcon(":/quadd.png"), tr("Genres"));
    genreI->setFont(QFont("courier", -1, QFont::Bold));
    root->appendRow(genreI);
    populateByGenre(genreI, filter);
    QStandardItem *songI = new QStandardItem(QIcon(":/quadd.png"), tr("Songs"));
    songI->setFont(QFont("courier", -1, QFont::Bold));
    root->appendRow(songI);
    populateBySong(songI, filter, FilterType);
    for(int i = 0; i < mCurrentModel->rowCount(); ++i){
        QModelIndex exp = mCurrentModel->index(i, 0, QModelIndex());
        if(exp.isValid()){
            mView->expand(exp);
            QModelIndex next = mCurrentModel->index(0, 0, exp);
            if(next.isValid()){
                mView->expand(next);
            }
        }
    }
    qApp->restoreOverrideCursor();
    emit viewModeChanged(tr("Search"));
    emit modelChanged();
}

void PlayerWidget::clearFilter(){
    mSearch->clear();
    mView->setModel(mViewModel);
    mCurrentModel = mViewModel;
}

void PlayerWidget::reindex(){
    IndexerDialog dlg(this);
    dlg.exec();
}

void PlayerWidget::addToPlayList(){
    QModelIndexList sel = mView->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return;
    }
    foreach(QModelIndex i, sel){
        int type = i.data(TypeRole).toInt();
        if(type == Song){
            addSong(i);
        }else{
            recurse(i);
        }
    }
    QStandardItem *playListRoot = mPlayListModel->invisibleRootItem();
    emit numFilesChanged(playListRoot->rowCount());
    emit playListLengthChanged(mPlayListLength);
}

void PlayerWidget::addToPlayListAndClear(){
    clearPlayList();
    addToPlayList();
}

void PlayerWidget::removeFromPlayList(){
    QModelIndexList sel = mPlayListView->selectionModel()->selectedRows();
    QList<QPersistentModelIndex> persistent;
    int subSecs = 0;
    foreach(QModelIndex i, sel){
        subSecs = i.data(LengthRole).toInt();
        persistent << QPersistentModelIndex(i);
    }
    foreach(QPersistentModelIndex i, persistent){
        mPlayListModel->removeRow(i.row());
    }
    QStandardItem *root = mPlayListModel->invisibleRootItem();
    emit numFilesChanged(root->rowCount());
    mPlayListLength -= subSecs;
    emit playListLengthChanged(mPlayListLength);
}

void PlayerWidget::clearPlayList(){
    mPlayListModel->clear();
    mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title");
    mPlayListLength = 0;
    emit numFilesChanged(0);
    emit playListLengthChanged(0);
}

void PlayerWidget::shufflePlayList(){
    QVector<QStandardItem*> items;
    for(int i = 0; i < mPlayListModel->rowCount(); ++i){
        QStandardItem *cur = mPlayListModel->item(i, 0)->clone();
        items << cur;
    }
    std::random_shuffle(items.begin(), items.end());
    mPlayListModel->clear();
    mPlayListModel->setHorizontalHeaderLabels(QStringList() << "Title");
    QStandardItem *root = mPlayListModel->invisibleRootItem();
    foreach(QStandardItem *i, items){
        root->appendRow(i);
    }
}

void PlayerWidget::randomPlay(){
    clearPlayList();
    QStandardItem *root = mPlayListModel->invisibleRootItem();
    QIcon songIcon(":/song.png");
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QSqlQuery randomQ(db);
    randomQ.prepare("SELECT sipos, ttitle, tfullpath, igenres_id, artists.tartists_name, albums.talbum_name, ilength FROM songs, artists, albums WHERE songs.iartists_id = artists.iartists_id AND songs.ialbums_id = albums.ialbums_id ORDER BY random() LIMIT 1000");
    randomQ.exec();
    while(randomQ.next()){
        QString display = QString(tr("%1 - %2 - %3")).arg(randomQ.value(4).toString()).arg(randomQ.value(1).toString()).arg(randomQ.value(5).toString());
        QStandardItem *item = new QStandardItem;
        item->setEditable(false);
        item->setFont(QFont("courier"));
        item->setText(display);
        item->setIcon(songIcon);
        item->setData(randomQ.value(2), FullPathRole);
        item->setData(randomQ.value(6), LengthRole);
        item->setData(randomQ.value(4), ArtistRole);
        item->setData(randomQ.value(5), AlbumRole);
        mPlayListLength += randomQ.value(6).toInt();
        root->appendRow(item);
    }
    emit numFilesChanged(root->rowCount());
    emit playListLengthChanged(mPlayListLength);
}

void PlayerWidget::playCurrent(const QModelIndex &index){
    int isRemote = index.data(RemoteRole).toInt();
    if(isRemote){
        return;
    }
    mPlayer->stop();
    QString fullPath = index.data(FullPathRole).toString();
    play(fullPath);
    mTrayIcon->setIcon(QIcon(":/play.png"));
    mTrayIcon->setToolTip(mCurToolTip);
}

void PlayerWidget::printList(){
    QModelIndexList sel = mView->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        return;
    }
    QString output;
    foreach(QModelIndex i, sel){
        QString fp = i.data(FullPathRole).toString();
        TagLib::FileRef fr(fp.toUtf8());
        if(!fr.isNull()){
            quint16 track = fr.tag()->track();
            QString title = QString::fromStdWString(fr.tag()->title().toWString());
            int length = fr.audioProperties()->lengthInSeconds();
            int minutes = length / 60;
            int seconds = length % 60;
            QString t = QString("%1 - %2 (%3:%4)\n").arg(track, 2, 10, QChar('0')).arg(title).arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'));
            output.append(t);
        }
    }
    mLeftTE->clear();
    mLeftTE->setPlainText(output);
}

void PlayerWidget::searchMusicbrainzRight(){
    QModelIndex idx = mPlayListView->selectionModel()->currentIndex();
    if(!idx.isValid()){
        return;
    }
    QString artist = idx.data(ArtistRole).toString();
    QString album = idx.data(AlbumRole).toString();
    mWebDownloader->fetchData(artist, album);
}

void PlayerWidget::searchMusicbrainzLeft(){
    QModelIndex idx = mView->selectionModel()->currentIndex();
    if(!idx.isValid()){
        return;
    }
    QString artist = idx.data(ArtistRole).toString().toLower();
    QString album = idx.data(AlbumRole).toString().toLower();
    mWebDownloader->fetchData(artist, album);
}


void PlayerWidget::webDlDone(){
    QString aId = mWebDownloader->artistId();
    QString text;
    if(!aId.isEmpty()){
        text.append(QString(tr("<b>Musicbrainz:</b><table style=\"margin-bottom: 20px\">")));
        text.append(QString(tr("<tr><td style=\"padding-left: 30px\">Artist</td><td style=\"padding-left: 30px\"><a href=\"https://musicbrainz.org/artist/%1\">%2</a></td></tr>")).arg(aId).arg(mWebDownloader->artist()));
        foreach(QString alId, mWebDownloader->data()){
            text.append(QString("<tr><td style=\"padding-left: 30px\">Album</td><td style=\"padding-left: 30px\"><a href=\"https://musicbrainz.org/release-group/%1\">%2</a></td></tr>").arg(alId).arg(mWebDownloader->album()));
        }
        text.append("</table>");
        const QMap<QString, QString> other = mWebDownloader->otherData();
        text.append(QString(tr("<b>All Albums (%1):</b><ul>")).arg(QString::number(other.count())));
        QStringList albums = other.values();
        std::sort(albums.begin(), albums.end());
        foreach(QString a, albums){
            QString id = other.key(a);
            text.append(QString("<li><a href=\"https://musicbrainz.org/release-group/%1\">%2</a></li>").arg(id).arg(a));
        }
        text.append("</ul>");
    }else{
        text.append(QString(tr("<b>Musicbrainz: No match!</b><br/>")));
        const QMap<QString, QString> alt = mWebDownloader->alternateArtists();
        if(!alt.isEmpty()){
            text.append(QString(tr("<b>Guesses:</b><ul>")));
            for(QMap<QString, QString>::const_iterator it = alt.constBegin(); it != alt.constEnd(); ++it){
                text.append(QString("<li><a href=\"https://musicbrainz.org/artist/%1\">%2</a></li>").arg(it.key()).arg(it.value()));
            }
            text.append("</ul>");
        }
    }
    mLeftTE->setText(text);
}

void PlayerWidget::filterFromPlaylist(){
    QModelIndex idx = mPlayListView->selectionModel()->currentIndex();
    if(!idx.isValid()){
        return;
    }
    QString artist = idx.data(ArtistRole).toString();
    mSearch->setText(artist);
    doFilter();
}

void PlayerWidget::addWebRadio(){
    WebRadioDialog dlg(this);
    dlg.exec();
    QString desc = dlg.description();
    QString url = dlg.url();
    QSqlDatabase db = QSqlDatabase::database("beetplayerdb");
    QSqlQuery wrQ(db);
    wrQ.prepare("INSERT INTO webradio (tdescription, turl) VALUES(:d, :u)");
    wrQ.bindValue(":d", desc);
    wrQ.bindValue(":u", url);
    wrQ.exec();
}

void PlayerWidget::doMetadataChange(const QString &key, const QVariant &value){
    if(key == "Title"){
        QString np = value.toString();
        QRegularExpression re("\\s?-\\s?");
        QStringList titleParts = np.split(re);
        QString artist(tr("n/a")), title(tr("n/a"));
        if(titleParts.count() >= 2){
            artist = titleParts.at(0);
            title = titleParts.at(1);
        }
        QStandardItem *plRoot = mPlayListModel->invisibleRootItem();
        QStandardItem *nowP = new QStandardItem;
        nowP->setFont(QFont("courier"));
        nowP->setEditable(false);
        nowP->setIcon(QIcon(":/dog_hood.png"));
        nowP->setData(1, RemoteRole);
        nowP->setData(artist, ArtistRole);
        nowP->setData(title, TitleRole);
        nowP->setText(np);
        plRoot->appendRow(nowP);
        mCurrentTE->clear();
        mCurrentTE->append(QString("%1 %2").arg(tr("Artist:"), -20).arg(artist));
        mCurrentTE->append(QString("%1 %2").arg(tr("Title:"), -20).arg(title));
        setNowPlaying(np);
        mCurWinTitle = QString(tr("%1 - [%2] - [%3]")).arg(qApp->applicationName()).arg(artist).arg(title);
        mCurToolTip = QString(tr("%1: [%2]")).arg(artist).arg(title);
        mTrayIcon->setToolTip(mCurToolTip);
        mTrayIcon->showMessage(QString(tr("Now Playing:")), mCurToolTip, QSystemTrayIcon::Information, 5000);
        emit setWinTitle(mCurWinTitle);
    }else{
        if(mOtherMeta[key] != value){
            mOtherMeta[key] = value;
            emit streamDataNeedsUpdate();
        }
    }
}

void PlayerWidget::updateStreamData(){
    QString retval;
    retval.append("<table>");
    retval.append(QString(tr("<tr><td>Genre</td><td style=\"padding-left: 30px\">%1</td></tr>")).arg(mOtherMeta.value("Genre").toString()));
    retval.append(QString(tr("<tr><td>Publisher</td><td style=\"padding-left: 30px\">%1</td></tr>")).arg(mOtherMeta.value("Publisher").toString()));
    retval.append(QString(tr("<tr><td>Location</td><td style=\"padding-left: 30px\">%1</td></tr>")).arg(mOtherMeta.value("location").toString()));
    retval.append(QString(tr("<tr><td>Audio Codec</td><td style=\"padding-left: 30px\">%1</td></tr>")).arg(mOtherMeta.value("AudioCodec").toString()));
    retval.append("</table>");
    mLeftTE->setText(retval);
}

void PlayerWidget::play(const QString &fullPath){
    disconnect(mPlayer, static_cast<void(QMediaObject::*)(const QString &, const QVariant &)>(&QMediaObject::metaDataChanged), this, &PlayerWidget::doMetadataChange);
    mIsStream = false;
    mSongSlider->setEnabled(true);
    mLeftTE->clear();
    mPlayer->setMedia(QUrl::fromLocalFile(fullPath));
    TagLib::FileRef file(QString(fullPath).toUtf8());
    fillWithText(mCurrentTE, file);
    if(file.isNull()){
        return;
    }
    QString artist = QString::fromStdWString(file.tag()->artist().toWString());
    QString album = QString::fromStdWString(file.tag()->album().toWString());
    QString title = QString::fromStdWString(file.tag()->title().toWString());
    setNowPlaying(title);
    int length = file.audioProperties()->lengthInSeconds();
    mDurSecs = length;
    mSongSlider->setMinimum(0);
    mSongSlider->setMaximum(mDurSecs);
    int minutes = mDurSecs / 60;
    int seconds = mDurSecs % 60;
    mCurrentTE->append(QString("%1 %2:%3").arg(tr("Length:"), -20).arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0')));
    QString msg = QString("File: %1").arg(fullPath);
    emit message(msg);
    mCurWinTitle = QString(tr("%1 - [%2] - [%3] - [%4]")).arg(qApp->applicationName()).arg(artist).arg(album).arg(title);
    mCurToolTip = QString(tr("%1: [%2] - [%3]")).arg(artist).arg(album).arg(title);
    mTrayIcon->setToolTip(mCurToolTip);
    mTrayIcon->showMessage(QString(tr("Now Playing:")), mCurToolTip, QSystemTrayIcon::Information, 5000);
    emit setWinTitle(mCurWinTitle);

    mPlayer->play();
    mPlayA->setChecked(true);
    emit playModeChanged(tr("Playing"));
}

void PlayerWidget::playUrl(const QString &url){
    disconnect(mPlayer, static_cast<void(QMediaObject::*)(const QString &, const QVariant &)>(&QMediaObject::metaDataChanged), this, &PlayerWidget::doMetadataChange);
    connect(mPlayer, static_cast<void(QMediaObject::*)(const QString &, const QVariant &)>(&QMediaObject::metaDataChanged), this, &PlayerWidget::doMetadataChange);
    mIsStream = true;
    mSongSlider->setValue(0);
    mSongSlider->setEnabled(false);
    mPlayer->setMedia(QUrl(url));
    mPlayer->play();
    mPlayA->setChecked(true);
    emit playModeChanged(tr("Playing"));
}

void PlayerWidget::volumeChanged(int volume){
    QString s = QString("%1 %").arg(volume, 3, 10, QChar('0'));
    mVolumePos->setText(s);
    if(!mStarting){
        if(!mVolumeTimer->isActive()){
            mVolumeTimer->start(500);
        }
    }
}

void PlayerWidget::mute(bool triggered){
    mPlayer->setMuted(triggered);
}

void PlayerWidget::next(){
    advance(1);
}

void PlayerWidget::previous(){
    advance(-1);
}

void PlayerWidget::advance(int numSongs){
    mPlayer->stop();
    QModelIndexList sel = mPlayListView->selectionModel()->selectedRows();
    if(sel.isEmpty()){
        QStandardItem *root = mPlayListModel->invisibleRootItem();
        if(root->rowCount() > 0){
            QModelIndex first = mPlayListModel->index(0, 0);
            mPlayListView->selectionModel()->setCurrentIndex(first, QItemSelectionModel::ClearAndSelect);
            playCurrent(first);
            return;
        }
        return;
    }
    QModelIndex cur = sel.first();
    QModelIndex nextIdx = mPlayListModel->index(cur.row() + numSongs, 0);
    if(nextIdx.isValid()){
        mPlayListView->selectionModel()->setCurrentIndex(nextIdx, QItemSelectionModel::ClearAndSelect);
        playCurrent(nextIdx);
    }
}

void PlayerWidget::slide(int value){
    qint64 newValue = value * 1000;
    mPlayer->setPosition(newValue);
}

void PlayerWidget::setPosition(qint64 pos){
    int curPos = pos / 1000;
    if(!mIsStream){
        mSongSlider->setValue(curPos);
    }
    int minutes = curPos / 60;
    int seconds = curPos % 60;
    QString posString = QString("%1:%2").arg(minutes, 2, 10, QChar('0')).arg(seconds, 2, 10, QChar('0'));
    mPos->setText(posString);
}

void PlayerWidget::continuePlaying(QMediaPlayer::MediaStatus state){
    if(state == QMediaPlayer::EndOfMedia){
        next();
    }
}

void PlayerWidget::expand(){
    QModelIndexList sel = mView->selectionModel()->selectedRows();
    foreach(QModelIndex i, sel){
        mView->expand(i);
        expandRecursive(i);
    }
}

void PlayerWidget::expandRecursive(const QModelIndex &idx){
    const QStandardItemModel *model = static_cast<const QStandardItemModel*>(idx.model());
    QStandardItem *item = model->itemFromIndex(idx);
    for(int i = 0; i < item->rowCount(); ++i){
        QModelIndex cur = model->indexFromItem(item->child(i, 0));
        if(cur.isValid()){
            mView->expand(cur);
        }
    }
}

void PlayerWidget::readSettings(){
    mStarting = true;
    QSettings s;
    int vol = s.value("volume").toInt();
    mVolumeSlider->setValue(vol);
    QString dir = s.value("folderdir", QDir::homePath()).toString();
    mCurDir = dir;
    mDir->setText(mCurDir);
    mStarting = false;
}

void PlayerWidget::writeSettings(){
    QSettings s;
    s.setValue("volume", mVolumeSlider->value());
    s.setValue("folderdir", mCurDir);
}

void PlayerWidget::aboutDlg(){
    QString title = QString(tr("About %1")).arg(qApp->applicationName());
    QString aboutText = QString(tr("<p>%1: A basic music player without all the fancy stuff implemented in eg. Amarok</p>")).arg(qApp->applicationName());
    aboutText.append("<table>");
    aboutText.append(QString(tr("<tr><td>Author</td><td style=\"padding-left: 30px\">Sissy herself %1</td></tr>")).arg(QChar(0x26A4)));
    aboutText.append(QString(tr("<tr><td>Organization</td><td style=\"padding-left: 30px\">%1</td></tr>")).arg(qApp->organizationName()));
    aboutText.append(QString(tr("<tr><td>Version</td><td style=\"padding-left: 30px\">%1</td></tr>")).arg(qApp->applicationVersion()));
    aboutText.append(QString(tr("<tr><td>Build:</td><td style=\"padding-left: 30px\">/* __debug build__ */</td></tr>")));
    aboutText.append(tr("<tr><td>Depends</td><td style=\"padding-left: 30px\">Qt PostgreSQL TagLib</td></tr>"));
    aboutText.append(tr("<tr><td>License</td><td style=\"padding-left: 30px\">GPL 2 or later</td></tr></table>"));
    QMessageBox::about(this, title, aboutText);
}