aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/General/notifier/notifier.pro
blob: 50364bb129f97677543f9ac23ee3e630ccdd8436 (plain) (blame)
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
include(../../plugins.pri)

TARGET = $$PLUGINS_PREFIX/General/notifier

HEADERS += notifierfactory.h \
           notifier.h \
           popupwidget.h \
           settingsdialog.h

SOURCES += notifierfactory.cpp \
           notifier.cpp \
           popupwidget.cpp \
           settingsdialog.cpp

FORMS += settingsdialog.ui

RESOURCES = notifier_images.qrc \
            translations/translations.qrc

LIBS += $$QMMPUI_LIB

unix {
  target.path = $$PLUGIN_DIR/General
  INSTALLS += target
  PKGCONFIG += x11
  QT += x11extras
  DEFINES += X11_FOUND
}
ec@90c681e8-e032-0410-971d-27865f9a5e38> 2007-06-23 16:48:01 +0000 moved into qmmp dir' href='/qmmp/commit/src/playlistmodel.cpp?id=2d622fd9bcb8da9dd3f3206e296cd6a701fc9d12'>2d622fd9b
2d622fd9b
2d622fd9b
7a3c89bf2
8ab140dac

9c02a3609
2e689e667

6ba4fc48f
6ba4fc48f
7586a6542
bf50c3440
92f1159a1
90d3aeb64

14ff3ad80
2d622fd9b
90d3aeb64
ae7155e16
2d622fd9b

92f1159a1
2d622fd9b

245d0ac0c
92f1159a1
24af8da3f
92f1159a1









257a322c4
245d0ac0c
3be88d8c0

24af8da3f

2d622fd9b



a65a14049
1d93bac0b
2d622fd9b

1d93bac0b
2d622fd9b

90d3aeb64






e2225dbc1




90d3aeb64

245d0ac0c
2d622fd9b
1d93bac0b
245d0ac0c
2d622fd9b
1d93bac0b
48a0ee1fc

1d93bac0b
48a0ee1fc

92f1159a1
18b1a7a93



8ae997e7c
90d3aeb64
3a965fcd4
90d3aeb64
7e58dddb0
245d0ac0c
90d3aeb64
245d0ac0c
90d3aeb64
90d3aeb64
245d0ac0c
03082b6a3
1d93bac0b
245d0ac0c
1d93bac0b
48a0ee1fc

1d93bac0b
48a0ee1fc

92f1159a1
7b5e9960a



8ae997e7c
03082b6a3
cef2410a9
90d3aeb64
3a965fcd4
2d622fd9b

24af8da3f

15921e6ff
157b69f61
24af8da3f



15921e6ff
24af8da3f

157b69f61
24af8da3f


7b5e9960a




















3be88d8c0




7b5e9960a




























55869c8c5
7b5e9960a



3be88d8c0


7b5e9960a
3be88d8c0
55869c8c5




3be88d8c0
3be88d8c0












7b5e9960a

245d0ac0c
2d622fd9b
1d93bac0b
2d622fd9b

245d0ac0c
2d622fd9b
1d93bac0b
2d622fd9b

245d0ac0c
24af8da3f
1d93bac0b
245d0ac0c



1d93bac0b
40ee4f96d
245d0ac0c
6d1217944
c6e0029ee

e2bc4908a
1d93bac0b
e2bc4908a
1d93bac0b
40ee4f96d

3a965fcd4
e662c2f44
1d93bac0b
e662c2f44

3a965fcd4

1d93bac0b
3a965fcd4

245d0ac0c

1d93bac0b
245d0ac0c

b94095cd8




245d0ac0c
2d622fd9b



3a965fcd4
2d622fd9b
3a965fcd4
11b7655bb
1d93bac0b
245d0ac0c
b94095cd8



3a965fcd4
245d0ac0c
2d622fd9b

013ee07b5
2d622fd9b

245d0ac0c
03082b6a3
1d93bac0b
03082b6a3
1d93bac0b
03082b6a3

664d78f03



1d93bac0b
664d78f03

b94095cd8



70637af74
b94095cd8

2d622fd9b

245d0ac0c
6d1217944
245d0ac0c
6d1217944


4221ec3de


013ee07b5
4221ec3de

24af8da3f
2d622fd9b
2d622fd9b




24af8da3f
2d622fd9b
90d3aeb64
2d622fd9b



24af8da3f
2d622fd9b
245d0ac0c
1d93bac0b
a823ec787
2d622fd9b


3a965fcd4
2d622fd9b



1d93bac0b
2d622fd9b


245d0ac0c
10cf99e34
1d93bac0b
2d622fd9b

245d0ac0c
2d622fd9b
1d93bac0b
2d622fd9b

542c1d2d3

76b31b99f
542c1d2d3
76b31b99f



542c1d2d3
76b31b99f
542c1d2d3


245d0ac0c

1d93bac0b
245d0ac0c

3a965fcd4
2d622fd9b
1d93bac0b
245d0ac0c
2d622fd9b

70637af74













2d622fd9b

ae16fa17d
2d622fd9b



ae16fa17d


245d0ac0c
fedd13511
80298eeab
fedd13511

1d93bac0b
245d0ac0c


80298eeab
245d0ac0c


0400576a8
fedd13511
80298eeab
245d0ac0c
80298eeab


1daf8cdc9
80298eeab
992cbd13d
80298eeab






1daf8cdc9
245d0ac0c
fedd13511
80298eeab









fedd13511
80298eeab



fedd13511
3a965fcd4
fedd13511


0400576a8
43bf74b18
1d93bac0b

43bf74b18

ae16fa17d

2d622fd9b
ae16fa17d
80298eeab
ae16fa17d
1d93bac0b
2d622fd9b
1d93bac0b
245d0ac0c
2d622fd9b
80298eeab


2d622fd9b
1d93bac0b
467be1724

ae16fa17d
2d622fd9b




1d93bac0b

ae16fa17d
eeffcb5f6
1d93bac0b
ae16fa17d
2d622fd9b

80298eeab


2d622fd9b
3a965fcd4
2d622fd9b



1d93bac0b

2d622fd9b




1d93bac0b

2d622fd9b


24af8da3f
2d622fd9b
a2b0cb8e8

1d93bac0b
2d622fd9b
a2b0cb8e8


dc76e6a2b
a2b0cb8e8









2d622fd9b
2d622fd9b

2d622fd9b



















9548b5189




ae7155e16
2d622fd9b

14ff3ad80
2d622fd9b

14ff3ad80



2d622fd9b
14ff3ad80
2d622fd9b
14ff3ad80








1d93bac0b
ef9971268
1d93bac0b
ef9971268

2d622fd9b

ae7155e16
2d622fd9b
ae7155e16
2d622fd9b











ae7155e16
2d622fd9b
245d0ac0c
2d622fd9b

245d0ac0c
2d622fd9b








ae7155e16
2d622fd9b
ae7155e16
ae7155e16
3a965fcd4
2d622fd9b


3a965fcd4
2d622fd9b
245d0ac0c
1d93bac0b
2d622fd9b
1d93bac0b
2d622fd9b






3b41c0698
2d622fd9b
245d0ac0c
1d93bac0b
2d622fd9b
245d0ac0c

2d622fd9b
245d0ac0c
2d622fd9b

9548b5189

1d93bac0b
9548b5189

2d622fd9b

245d0ac0c


2d622fd9b


245d0ac0c
2d622fd9b
d4bb0061a

e6223d1c6
d4bb0061a
e6223d1c6
2d622fd9b

245d0ac0c
2d622fd9b





3a965fcd4
2d622fd9b






245d0ac0c
6d1217944
245d0ac0c
6d1217944








245d0ac0c
6d1217944

2d622fd9b

3cce6c1d8




2d622fd9b



fce0e4d52




2d622fd9b
2d622fd9b


3b41c0698




2d622fd9b



80567cfa3




2d622fd9b

2d622fd9b








2d622fd9b

a5a2707ad














2d622fd9b





cf021281c
2d622fd9b
b67a746b1
15921e6ff
2d622fd9b

b67a746b1
2d622fd9b
b67a746b1

2d622fd9b
b67a746b1
1d93bac0b
883075834
b67a746b1
2d622fd9b

03082b6a3




2d622fd9b



cda0f909b
039a1be23
43bf74b18
039a1be23
0400576a8
80298eeab
43bf74b18
80298eeab
039a1be23
80298eeab

039a1be23


a259c404c
039a1be23
a259c404c
0400576a8
80298eeab
039a1be23
43bf74b18
29c73615f


1d93bac0b
29c73615f
0400576a8

1d93bac0b
d0a6dc46a
29c73615f
1d93bac0b
0400576a8



d0a6dc46a
29c73615f
0400576a8
29c73615f
6d1217944



245d0ac0c
6d1217944




245d0ac0c
6d1217944

245d0ac0c
6d1217944
245d0ac0c
6d1217944
245d0ac0c
6d1217944
245d0ac0c
6d1217944

245d0ac0c
6d1217944




92f1159a1





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
                                                                            
                                                                            




                                                                            
                                                                            








                                                                            
                                                                            

                                                                             
                       
                      
                 
                                 

                           
                                

                               
                         
                        
                          
                         
                           

                          
                        
 
                                                                  
                                         

                    
                                               

                       
                     
                  
                                    









                                                                                               
                                                            
                                                             

                                                                               

                                                                    



                               
                       
                       

                        
                       

 






                                                




                               

 
                                             
 
                                 
                                      
 
                                      

                                
                                                

                              
                                             



                                                          
                           
                       
                        
 
 
                                                      
 
                        
               
 
                                         
     
                                     
                                          
                                          

                                    
                                                    

                                  
                                                 



                                                              
                               
     
                       
                       
                        

 

                                            
                        
                       



                                                 
                         

                               
                          


     




















                                                           




                                                                      




























                                                                                 
                                         



                                                               


                                                  
     
                                                        




                                                            
     












                                                                          

 
                                
 
                                

 
                                     
 
                                     

 
                                                  
 
                                                        



                                               
                                               
                 
                                                      
                 

                                    
                                          
                                                       
                 
                                     

 
                                                    
 
                                      

 

                                                  
                                    

 

                                                    
                                     

 




                                                    
                                       



                     
                                         
 
                                       
                     
                                                  
                       



                                        
                      
                                                          

                          
                

 
                                                    
 
                                     
                     
                                                   

 



                                            
                                                

 



                                            
                                               

 

                          
                                      
     
                         


                           


                             
                    

     
                             
                                




                                
                              
                                
                                    



                           
                       
                  
                     
                         
                           


                               
                        



                                    
                                  


                       
                                                                  
 
                                        

 
                                               
 
                                          

 

                                                
                                                 
     



                                    
                        
     


                 

                                                 
                                             

 
                                                         
 
                                              
                       

 













                                                                             

                                    
                           



                                      


                          
                                       
 
                                 

                                  
                                                     


                                        
                                        


                                          
                                                 
 
                                    
         


                                      
             
                                       
                                                                    






                                                                                   
             
         
 









                                                                                 
                                



                                  
                           
                            


     
                                                     
 

                                                  

 

                                                  
              
                                 
                                                        
 
                                                               
     
                                                  
                                                              
         


                                
 
                                      

                         
                                    




                

                                                       
 
                                 
                                                            
 

                            


                                             
                       
                        



                                     

                                                                 




                               

                                                  


                       
                                                
 

                                           
                                                  
     


                                                     
                                                          









                                                                             
     

 



















                                              




                                      
                                               

                              
                   

               



                                                    
 
                                                        
     








                                                       
                                                     
     
                                                          

                           

 
                                              
 
                 











                                    
                                                 
 
                           

                   
                                           








                          
                                                           
 
                                                
                                                      
                                                       


                       
                                                 
 
                             
                                                  
     
                                               






                                    
                                                            
 
                                          
                                                     
     

                                                                        
     
                           

 

                                                  
                                

 

                                


                                                             


                       
                                                  
 

                                       
        
                                     
                       

 
                                                    





                                        
                                                  






                                        
                                                          
 
                                         








                                                         
                                

 

                                   




                                                      



                                 




                                                      
 


                                           




                                                      



                                  




                                                      

 








                                                      

 














                                                          





                                             
                                                       
 
                                                            
                        

 
                                                       
 

                                                 
     
                      
                                           
     
                                                

 




                                           



                                      
 
                                         
 
                    
 
                                                      
     
                                                    
                     

                                                     


                                                                                                    
            
                                                                     
               
                           
 
     
 


                                      
                                                 
     

                       
                                         
                    
         
                                                      



                                                      
                
         
     
 



                                
                      




                                       
                                                             

                                 
                                                                                         
     
                                         
     
                                                                                         
     
                                        

                     
                                             




                       





                                        
/***************************************************************************
 *   Copyright(C) 2006-2013 by Ilya Kotov                                  *
 *   forkotov02@hotmail.ru                                                 *
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *  (at your option) any later version.                                    *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
 ***************************************************************************/
#include <QWidget>
#include <QtAlgorithms>
#include <QTextStream>
#include <time.h>
#include <qmmp/metadatamanager.h>
#include "playlistparser.h"
#include "playlistformat.h"
#include "playlistcontainer_p.h"
#include "groupedcontainer_p.h"
#include "normalcontainer_p.h"
#include "fileloader_p.h"
#include "playstate_p.h"
#include "detailsdialog.h"
#include "tagupdater_p.h"
#include "qmmpuisettings.h"
#include "playlistmodel.h"

#define INVALID_INDEX -1

PlayListModel::PlayListModel(const QString &name, QObject *parent)
        : QObject(parent) , m_selection()
{
    qsrand(time(0));
    m_ui_settings = QmmpUiSettings::instance();
    m_total_length = 0;
    m_current = 0;
    m_stop_track = 0;
    m_name = name;
    m_loader = new FileLoader(this);
    if(m_ui_settings->isGroupsEnabled())
        m_container = new GroupedContainer;
    else
        m_container = new NormalContainer;
    if(m_ui_settings->isShuffle())
        m_play_state = new ShufflePlayState(this);
    else
        m_play_state = new NormalPlayState(this);
    connect(m_ui_settings, SIGNAL(groupsChanged(bool)), SLOT(prepareGroups(bool)));
    connect(m_ui_settings, SIGNAL(shuffleChanged(bool)), SLOT(prepareForShufflePlaying(bool)));
    connect(m_loader, SIGNAL(newTrackToAdd(PlayListTrack*)),
            SLOT(add(PlayListTrack*)), Qt::QueuedConnection);
    connect(m_loader, SIGNAL(newTrackToInsert(PlayListItem*, PlayListTrack*)),
            SLOT(insert(PlayListItem*, PlayListTrack*)), Qt::QueuedConnection);
    connect(m_loader, SIGNAL(finished()), SLOT(preparePlayState()));
    connect(m_loader, SIGNAL(finished()), SIGNAL(loaderFinished()));
}

PlayListModel::~PlayListModel()
{
    blockSignals(true);
    m_loader->finish();
    clear();
    delete m_play_state;
    delete m_container;
}

QString PlayListModel::name() const
{
    return m_name;
}

void PlayListModel::setName(const QString &name)
{
    if(m_name != name)
    {
        m_name = name;
        emit nameChanged(name);
    }
}

void PlayListModel::add(PlayListTrack *track)
{
    m_container->addTrack(track);
    m_total_length += track->length();

    if(m_container->trackCount() == 1)
    {
        m_current_track = track;
        m_current = m_container->indexOf(track);
        emit currentChanged();
    }
    else if(m_ui_settings->isGroupsEnabled())
    {
        //update current index for grouped container only
        m_current = m_container->indexOf(m_current_track);
    }
    emit trackAdded(track);
    emit listChanged();
    emit countChanged();
}

void PlayListModel::add(QList<PlayListTrack *> tracks)
{
    if(tracks.isEmpty())
        return;

    foreach(PlayListTrack *track, tracks)
    {
        m_container->addTrack(track);
        m_total_length += track->length();
        if(m_container->trackCount() == 1)
        {
            m_current_track = track;
            m_current = m_container->indexOf(track);
            emit currentChanged();
        }
        else if(m_ui_settings->isGroupsEnabled())
        {
            //update current index for grouped container only
            m_current = m_container->indexOf(m_current_track);
        }
        emit trackAdded(track);
    }
    preparePlayState();
    emit listChanged();
    emit countChanged();
}

void PlayListModel::add(const QString &path)
{
    m_loader->add(path);
    loadPlaylist(path);
}

void PlayListModel::add(const QStringList &paths)
{
    m_loader->add(paths);
    foreach(QString str, paths)
    {
        loadPlaylist(str);
    }
}

void PlayListModel::insert(int index, PlayListTrack *track)
{
    m_container->insertTrack(index, track);
    m_total_length += track->length();

    if(m_container->trackCount() == 1)
    {
        m_current_track = track;
        m_current = m_container->indexOf(track);
        emit currentChanged();
    }
    else
    {
        //update current index
        m_current = m_container->indexOf(m_current_track);
    }
    emit trackAdded(track);
    emit listChanged();
    emit countChanged();
}

void PlayListModel::insert(PlayListItem *before, PlayListTrack *track)
{
    insert(m_container->indexOf(before), track);
}

void PlayListModel::insert(int index, QList<PlayListTrack *> tracks)
{
    if(tracks.isEmpty())
        return;

    PlayListItem *prevItem = m_container->item(index);
    foreach(PlayListTrack *track, tracks)
    {
        m_container->insertTrack(index, track);
        index = prevItem ? m_container->indexOf(prevItem) : m_container->count();

        m_total_length += track->length();
        if(m_container->trackCount() == 1)
        {
            m_current_track = track;
            m_current = m_container->indexOf(track);
            emit currentChanged();
        }
        emit trackAdded(track);
    }
    //update current index
    m_current = m_container->indexOf(m_current_track);
    preparePlayState();
    emit listChanged();
    emit countChanged();
}

void PlayListModel::insert(int index, const QString &path)
{
    insert(index, QStringList() << path);
}

void PlayListModel::insert(int index, const QStringList &paths)
{
    if(index < 0 || index >= m_container->count())
        add(paths);
    else
    {
        PlayListItem *before = m_container->item(index);

        QStringList list = paths;
        foreach (QString path, paths)
            list.append(PlayListParser::loadPlaylist(path));
        m_loader->insert(before, list);
    }
}

void PlayListModel::insert(int index, const QList<QUrl> &urls)
{
    QStringList paths;
    foreach (QUrl url, urls)
    {
        if(url.scheme() == "file")
            paths.append(QFileInfo(url.toLocalFile()).absoluteFilePath());
        else
            paths.append(url.toString());
    }
    insert(index, paths);
}

int PlayListModel::count() const
{
    return m_container->count();
}

int PlayListModel::trackCount() const
{
    return m_container->trackCount();
}

PlayListTrack* PlayListModel::currentTrack() const
{
    return m_container->isEmpty() ? 0 : m_current_track;
}

PlayListTrack *PlayListModel::nextTrack() const
{
    if(m_container->isEmpty() || !m_play_state)
        return 0;
    if(m_stop_track && m_stop_track == currentTrack())
        return 0;
    if(!isEmptyQueue())
        return m_queued_songs.at(0);
    int index = m_play_state->nextIndex();
    if(index < 0 || (index + 1 > m_container->count()))
        return 0;
    return m_container->track(index);
}

int PlayListModel::indexOf(PlayListItem* item) const
{
    return m_container->indexOf(item);
}

PlayListItem* PlayListModel::item(int index) const
{
    return m_container->item(index);
}

PlayListTrack* PlayListModel::track(int index) const
{
    return m_container->track(index);
}

PlayListGroup* PlayListModel::group(int index) const
{
    return m_container->group(index);
}

int PlayListModel::currentIndex() const
{
    return m_current;
}

bool PlayListModel::setCurrent(int index)
{
    if (index > count()-1 || index < 0)
        return false;
    PlayListItem *item = m_container->item(index);
    if(item->isGroup())
    {
        index++;
        item = m_container->item(index);
    }
    m_current = index;
    m_current_track = dynamic_cast<PlayListTrack*> (item);
    emit currentChanged();
    emit listChanged();
    return true;
}

bool PlayListModel::setCurrent(PlayListTrack *track)
{
    if(!m_container->contains(track))
        return false;
    return setCurrent(m_container->indexOf(track));
}

bool PlayListModel::isTrack(int index) const
{
    if (index > count()-1 || index < 0)
        return false;
    return !m_container->item(index)->isGroup();
}

bool PlayListModel::isGroup(int index) const
{
    if (index > count()-1 || index < 0)
        return false;
    return m_container->item(index)->isGroup();
}

bool PlayListModel::next()
{
    if(m_stop_track == currentTrack())
    {
        m_stop_track = 0;
        emit listChanged();
        return false;
    }
    if (!isEmptyQueue())
    {
        setCurrentToQueued();
        return true;
    }

    if(m_loader->isRunning())
        m_play_state->prepare();
    return m_play_state->next();
}

bool PlayListModel::previous()
{
    if (m_loader->isRunning())
        m_play_state->prepare();
    return m_play_state->previous();
}

void PlayListModel::clear()
{
    m_loader->finish();
    m_current = 0;
    m_stop_track = 0;
    m_container->clear();
    m_queued_songs.clear();
    m_total_length = 0;
    m_play_state->resetState();
    emit listChanged();
    emit countChanged();
}

void PlayListModel::clearSelection()
{
    m_container->clearSelection();
    emit listChanged();
}

QList<PlayListItem *> PlayListModel::mid(int pos, int count) const
{
    return m_container->mid(pos, count);
}

bool PlayListModel::isSelected(int index) const
{
    return m_container->isSelected(index);
}

bool PlayListModel::contains(const QString &url)
{
    for(int i = 0; i < m_container->count(); ++i)
    {
        PlayListTrack *t = track(i);
        if(!t)
            continue;
        if(t->url() == url)
            return true;
    }
    return false;
}

int PlayListModel::numberOfTrack(int index) const
{
    return m_container->numberOfTrack(index);
}

void PlayListModel::setSelected(int index, bool selected)
{
    m_container->setSelected(index, selected);
    emit listChanged();
}

void PlayListModel::setSelected(QList<PlayListTrack *> tracks, bool selected)
{
    foreach(PlayListTrack *t, tracks)
        t->setSelected(selected);
    emit listChanged();
}

void PlayListModel::setSelected(QList<PlayListItem *> items, bool selected)
{
    foreach(PlayListItem *i, items)
        i->setSelected(selected);
    emit listChanged();
}

void PlayListModel::removeSelected()
{
    removeSelection(false);
}

void PlayListModel::removeUnselected()
{
    removeSelection(true);
}

void PlayListModel::removeTrack (int i)
{
    bool current_changed = false;
    if ((i < count()) && (i >= 0))
    {
        PlayListTrack* track = m_container->track(i);
        if(!track)
            return;
        m_queued_songs.removeAll(track);
        m_container->removeTrack(track);
        if(m_stop_track == track)
            m_stop_track = 0;
        m_total_length -= track->length();
        m_total_length = qMax(0, m_total_length);

        if(m_current_track == track)
        {
            if(m_container->isEmpty())
                m_current_track = 0;
            else
            {
                current_changed = true;
                int current = qMin(i - 1, m_container->count() - 1);
                current = qMax(current, 0);
                m_current_track = m_container->track(current);
                if(!m_current_track)
                {
                    m_current_track = current > 0 ? m_container->track(current-1) :
                                                    m_container->track(1);
                }
            }
        }

        if (track->flag() == PlayListTrack::FREE)
        {
            delete track;
            track = NULL;
        }
        else if (track->flag() == PlayListTrack::EDITING)
            track->setFlag(PlayListTrack::SCHEDULED_FOR_DELETION);


        m_current = m_current_track ? m_container->indexOf(m_current_track) : -1;
        m_play_state->prepare();

        if(current_changed)
            emit currentChanged();

        emit listChanged();
        emit countChanged();
    }
}

void PlayListModel::removeTrack (PlayListItem *track)
{
    if(m_container->contains(track))
        removeTrack (m_container->indexOf(track));
}

void PlayListModel::removeSelection(bool inverted)
{
    int i = 0;
    int select_after_delete = -1;
    PlayListTrack *prev_current_track = m_current_track;

    while (!m_container->isEmpty() && i < m_container->count())
    {
        PlayListItem *item = m_container->item(i);
        if (!item->isGroup() && item->isSelected() ^ inverted)
        {
            blockSignals(true);
            removeTrack(i);
            blockSignals(false);

            if(m_container->isEmpty())
                continue;

            select_after_delete = i;
        }
        else
            i++;
    }

    if (select_after_delete >= m_container->count())
        select_after_delete = m_container->count() - 1;

    if(select_after_delete != -1)
        m_container->setSelected(select_after_delete, true);

    m_play_state->prepare();

    if(prev_current_track != m_current_track)
        emit currentChanged();

    emit listChanged();
    emit countChanged();
}

void PlayListModel::invertSelection()
{
    for (int i = 0; i < m_container->count(); ++i)
        m_container->setSelected(i, !m_container->isSelected(i));
    emit listChanged();
}

void PlayListModel::selectAll()
{
    for (int i = 0; i < m_container->count(); ++i)
        m_container->setSelected(i, true);
    emit listChanged();
}

void PlayListModel::showDetails(QWidget *parent)
{
    QList<PlayListTrack *> selected_tracks;

    for (int i = 0; i < m_container->count(); ++i)
    {
        if(!m_container->isSelected(i))
            continue;
        PlayListTrack *track = m_container->track(i);
        if(!track || track->flag() != PlayListTrack::FREE)
            continue;
        selected_tracks.append(track);
    }

    if(!selected_tracks.isEmpty())
    {
        QDialog *d = new DetailsDialog(selected_tracks, parent);
        TagUpdater *updater = new TagUpdater(d, selected_tracks);
        connect(updater, SIGNAL(destroyed(QObject *)),SIGNAL(listChanged()));
        d->show();
    }
}

int PlayListModel::firstSelectedUpper(int row)
{
    for (int i = row - 1;i >= 0;i--)
    {
        if (isSelected(i))
            return i;
    }
    return -1;
}

int PlayListModel::firstSelectedLower(int row)
{
    for (int i = row + 1;i < count() ;i++)
    {
        if (isSelected(i))
            return i;
    }
    return -1;
}

int PlayListModel::totalLength() const
{
    return m_total_length;
}

void PlayListModel::moveItems(int from, int to)
{
    // Get rid of useless work
    if (from == to)
        return;

    QList<int> selected_indexes = selectedIndexes();

    if(selected_indexes.isEmpty())
        return;

    foreach(int i, selected_indexes) //do no move groups
    {
        if(!isTrack(i))
            return;
    }

    if (bottommostInSelection(from) == INVALID_INDEX ||
            from == INVALID_INDEX ||
            topmostInSelection(from) == INVALID_INDEX)
        return;

    if(m_container->move(selected_indexes, from, to))
    {
        m_current = m_container->indexOf(m_current_track);
        emit listChanged();
    }
}

int PlayListModel::topmostInSelection(int row)
{
    if (row == 0)
        return 0;

    for (int i = row - 1;i >= 0;i--)
    {
        if (isSelected(i))
            continue;
        else
            return i + 1;
    }
    return 0;
}

int PlayListModel::bottommostInSelection(int row)
{
    if (row >= count() - 1)
        return row;

    for (int i = row + 1; i < count(); i++)
    {
        if (isSelected(i))
            continue;
        else
            return i - 1;
    }
    return count() - 1;
}

const SimpleSelection& PlayListModel::getSelection(int row)
{
    m_selection.m_top = topmostInSelection(row);
    m_selection.m_bottom = bottommostInSelection(row);
    m_selection.m_selected_indexes = selectedIndexes();
    return m_selection;
}

QList<int> PlayListModel::selectedIndexes() const
{
    QList<int> selected_rows;
    for (int i = 0; i < m_container->count(); i++)
    {
        if (m_container->item(i)->isSelected())
        {
            selected_rows.append(i);
        }
    }
    return selected_rows;
}

QList<PlayListTrack *> PlayListModel::selectedTracks() const
{
    QList<PlayListTrack*> selected_tracks;
    foreach(PlayListItem *item, m_container->items())
    {
        if(!item->isGroup() && item->isSelected())
            selected_tracks.append(dynamic_cast<PlayListTrack *>(item));
    }
    return selected_tracks;
}

QList<PlayListItem *> PlayListModel::items() const
{
    return m_container->items();
}

void PlayListModel::addToQueue()
{
    QList<PlayListTrack*> selected_tracks = selectedTracks();
    foreach(PlayListTrack* track, selected_tracks)
        setQueued(track);
    emit listChanged();
}

void PlayListModel::setQueued(PlayListTrack *item)
{
    if (isQueued(item))
        m_queued_songs.removeAll(item);
    else
        m_queued_songs.enqueue(item);
    emit listChanged();
}

bool PlayListModel::isQueued(PlayListTrack *f) const
{
    return m_queued_songs.contains(f);
}

void PlayListModel::setCurrentToQueued()
{
    setCurrent(indexOf(m_queued_songs.dequeue()));
}

bool PlayListModel::isEmptyQueue() const
{
    return m_queued_songs.isEmpty();
}

int PlayListModel::queuedIndex(PlayListTrack *track) const
{
    return m_queued_songs.indexOf(track);
}

int PlayListModel::queueSize() const
{
    return m_queued_songs.size();
}

bool PlayListModel::isStopAfter(PlayListItem* item) const
{
    return m_stop_track == item;
}

void PlayListModel::randomizeList()
{
    if(m_container->isEmpty())
        return;
    m_container->randomizeList();
    m_current = m_container->indexOf(m_current_track);
    emit listChanged();
}

void PlayListModel::reverseList()
{
    if(m_container->isEmpty())
        return;
    m_container->reverseList();
    m_current = m_container->indexOf(m_current_track);
    emit listChanged();
}

void PlayListModel::sortSelection(int mode)
{
    if(m_container->isEmpty())
        return;
    m_container->sortSelection(mode);
    m_current = m_container->indexOf(m_current_track);
    emit listChanged();
}

void PlayListModel::sort(int mode)
{
    if(m_container->isEmpty())
        return;
    m_container->sort(mode);
    m_current = m_container->indexOf(m_current_track);
    emit listChanged();
}

void PlayListModel::prepareForShufflePlaying(bool val)
{
    if (m_play_state)
        delete m_play_state;

    if (val)
        m_play_state = new ShufflePlayState(this);
    else
        m_play_state = new NormalPlayState(this);
}

void PlayListModel::prepareGroups(bool enabled)
{
    PlayListContainer *container = 0;
    if(enabled)
        container = new GroupedContainer;
    else
        container = new NormalContainer;
    container->addTracks(m_container->takeAllTracks());
    delete m_container;
    m_container = container;
    if(!m_container->isEmpty())
        m_current = m_container->indexOf(m_current_track);
    emit listChanged();
}

void PlayListModel::doCurrentVisibleRequest()
{
    emit currentChanged();
    emit listChanged();
}

void PlayListModel::loadPlaylist(const QString &f_name)
{
    QStringList list = PlayListParser::loadPlaylist(f_name);
    m_loader->add(list);
}

void PlayListModel::savePlaylist(const QString &f_name)
{
    QList <PlayListTrack *> songs;
    for(int i = 0; i < m_container->count(); ++i)
    {
        if(isTrack(i))
            songs << m_container->track(i);
    }
    PlayListParser::savePlayList(songs, f_name);
}

bool PlayListModel::isLoaderRunning() const
{
    return m_loader->isRunning();
}

void PlayListModel::preparePlayState()
{
    m_play_state->prepare();
}

void PlayListModel::removeInvalidTracks()
{