summaryrefslogtreecommitdiff
path: root/core/multimedia/opieplayer/playlistwidget.cpp
blob: 63df7156e36d0de1a99fe26eea399eef31d09247 (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
/**********************************************************************
** Copyright (C) 2000 Trolltech AS.  All rights reserved.
**
** This file is part of Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/
// code added by L. J. Potter Sat 03-02-2002 06:17:54
#define QTOPIA_INTERNAL_FSLP

#include <qpe/qpemenubar.h>
#include <qpe/qpetoolbar.h>
#include <qpe/fileselector.h>
#include <qpe/qpeapplication.h>
#include <qpe/lnkproperties.h>
#include <qpe/storage.h>

#include <qpe/applnk.h>
#include <qpe/config.h>
#include <qpe/global.h>
#include <qpe/resource.h>
#include <qaction.h>
#include <qimage.h>
#include <qfile.h>
#include <qdir.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qlist.h>
#include <qlistbox.h>
#include <qmainwindow.h>
#include <qmessagebox.h>
#include <qtoolbutton.h>
#include <qtabwidget.h>
#include <qlistview.h>
#include <qpoint.h>
#include <qlineedit.h>
#include <qpushbutton.h>

//#include <qtimer.h>

#include "playlistselection.h"
#include "playlistwidget.h"
#include "mediaplayerstate.h"

#include "inputDialog.h"

#include <stdlib.h>

#define BUTTONS_ON_TOOLBAR
#define SIDE_BUTTONS
#define CAN_SAVE_LOAD_PLAYLISTS

extern MediaPlayerState *mediaPlayerState;

// class myFileSelector {

// };
class PlayListWidgetPrivate {
public:
    QToolButton *tbPlay, *tbFull, *tbLoop, *tbScale, *tbShuffle, *tbAddToList,  *tbRemoveFromList, *tbMoveUp, *tbMoveDown, *tbRemove;
    QFrame *playListFrame;
    FileSelector *files;
    PlayListSelection *selectedFiles;
    bool setDocumentUsed;
    DocLnk *current;
};


class ToolButton : public QToolButton {
public:
    ToolButton( QWidget *parent, const char *name, const QString& icon, QObject *handler, const QString& slot, bool t = FALSE )
  : QToolButton( parent, name ) {
  setTextLabel( name );
  setPixmap( Resource::loadPixmap( icon ) );
  setAutoRaise( TRUE );
  setFocusPolicy( QWidget::NoFocus );
  setToggleButton( t );
  connect( this, t ? SIGNAL( toggled(bool) ) : SIGNAL( clicked() ), handler, slot );
  QPEMenuToolFocusManager::manager()->addWidget( this );
    }
};


class MenuItem : public QAction {
public:
    MenuItem( QWidget *parent, const QString& text, QObject *handler, const QString& slot )
  : QAction( text, QString::null, 0, 0 ) {
  connect( this, SIGNAL( activated() ), handler, slot );
  addTo( parent );
    }
};


PlayListWidget::PlayListWidget( QWidget* parent, const char* name, WFlags fl )
    : QMainWindow( parent, name, fl ) {

    d = new PlayListWidgetPrivate;
    d->setDocumentUsed = FALSE;
    d->current = NULL;
    fromSetDocument = FALSE;
    insanityBool=FALSE;
//    menuTimer = new QTimer( this ,"menu timer"),
//     connect( menuTimer, SIGNAL( timeout() ), SLOT( addSelected() ) );

    setBackgroundMode( PaletteButton );

    setCaption( tr("OpiePlayer") );
    setIcon( Resource::loadPixmap( "MPEGPlayer" ) );

    setToolBarsMovable( FALSE );

    // Create Toolbar
    QPEToolBar *toolbar = new QPEToolBar( this );
    toolbar->setHorizontalStretchable( TRUE );

    // Create Menubar
    QPEMenuBar *menu = new QPEMenuBar( toolbar );
    menu->setMargin( 0 );

    QPEToolBar *bar = new QPEToolBar( this );
    bar->setLabel( tr( "Play Operations" ) );
//      d->tbPlayCurList =  new ToolButton( bar, tr( "play List" ), "mpegplayer/play_current_list",
//                                        this , SLOT( addSelected()) );
    tbDeletePlaylist = new QPushButton( Resource::loadIconSet("trash"),"",bar,"close");
    tbDeletePlaylist->setFlat(TRUE);
    tbDeletePlaylist->setFixedSize(20,20);
    connect(tbDeletePlaylist,(SIGNAL(released())),SLOT( deletePlaylist()));
    
    d->tbAddToList =  new ToolButton( bar, tr( "Add to Playlist" ), "mpegplayer/add_to_playlist",
                                      this , SLOT(addSelected()) );
    d->tbRemoveFromList = new ToolButton( bar, tr( "Remove from Playlist" ), "mpegplayer/remove_from_playlist",
                                          this , SLOT(removeSelected()) );
//    d->tbPlay    = new ToolButton( bar, tr( "Play" ), "mpegplayer/play", /*this */mediaPlayerState , SLOT(setPlaying(bool) /* btnPlay() */), TRUE );
    d->tbPlay    = new ToolButton( bar, tr( "Play" ), "mpegplayer/play",
                                   this , SLOT( btnPlay(bool) ), TRUE );
    d->tbShuffle = new ToolButton( bar, tr( "Randomize" ),"mpegplayer/shuffle",
                                   mediaPlayerState, SLOT(setShuffled(bool)), TRUE );
    d->tbLoop    = new ToolButton( bar, tr( "Loop" ),"mpegplayer/loop",
                                   mediaPlayerState, SLOT(setLooping(bool)), TRUE );
    tbDeletePlaylist->hide();

    QPopupMenu *pmPlayList = new QPopupMenu( this );
    menu->insertItem( tr( "File" ), pmPlayList );
    new MenuItem( pmPlayList, tr( "Clear List" ),          this,             SLOT( clearList() ) );
    new MenuItem( pmPlayList, tr( "Add all audio files" ), this,             SLOT( addAllMusicToList() ) );
    new MenuItem( pmPlayList, tr( "Add all video files" ), this,             SLOT( addAllVideoToList() ) );
    new MenuItem( pmPlayList, tr( "Add all files" ),       this,             SLOT( addAllToList() ) );
    new MenuItem( pmPlayList, tr( "Save PlayList" ),       this,             SLOT( saveList() ) );
      //   new MenuItem( pmPlayList, tr( "Load PlayList" ),       this,             SLOT( loadList() ) );

    QPopupMenu *pmView = new QPopupMenu( this );
    menu->insertItem( tr( "View" ), pmView );

    fullScreenButton = new QAction(tr("Full Screen"), Resource::loadPixmap("fullscreen"), QString::null, 0, this, 0);
    connect( fullScreenButton, SIGNAL(activated()), mediaPlayerState, SLOT(toggleFullscreen()) );
    fullScreenButton->addTo(pmView);
    scaleButton = new QAction(tr("Scale"), Resource::loadPixmap("mpegplayer/scale"), QString::null, 0, this, 0);
    connect( scaleButton, SIGNAL(activated()), mediaPlayerState, SLOT(toggleScaled()) );
    scaleButton->addTo(pmView);

    QVBox *vbox5 = new QVBox( this ); vbox5->setBackgroundMode( PaletteButton );
    QVBox *vbox4 = new QVBox( vbox5 ); vbox4->setBackgroundMode( PaletteButton );

    QHBox *hbox6 = new QHBox( vbox4 ); hbox6->setBackgroundMode( PaletteButton );
    
    tabWidget = new QTabWidget( hbox6, "tabWidget" );
    tabWidget->setTabShape(QTabWidget::Triangular);
    
    QWidget *pTab;
    pTab = new QWidget( tabWidget, "pTab" );
//      playlistView = new QListView( pTab, "playlistview" );
//    playlistView->setMinimumSize(236,260);
    tabWidget->insertTab( pTab,"Playlist");


    // Add the playlist area

    QVBox *vbox3 = new QVBox( pTab ); vbox3->setBackgroundMode( PaletteButton );
    d->playListFrame = vbox3;
    d->playListFrame ->setMinimumSize(235,260);

    QHBox *hbox2 = new QHBox( vbox3 ); hbox2->setBackgroundMode( PaletteButton );

    d->selectedFiles = new PlayListSelection( hbox2);
    QVBox *vbox1 = new QVBox( hbox2 ); vbox1->setBackgroundMode( PaletteButton );

    QPEApplication::setStylusOperation( d->selectedFiles->viewport(),QPEApplication::RightOnHold);
          connect( d->selectedFiles, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint&, int)),
                   this,SLOT( playlistViewPressed(int, QListViewItem *, const QPoint&, int)) );

          
     QVBox *stretch1 = new QVBox( vbox1 ); stretch1->setBackgroundMode( PaletteButton ); // add stretch
     new ToolButton( vbox1, tr( "Move Up" ),   "mpegplayer/up",   d->selectedFiles, SLOT(moveSelectedUp()) );
     new ToolButton( vbox1, tr( "Remove" ),    "mpegplayer/cut",  d->selectedFiles, SLOT(removeSelected()) );
     new ToolButton( vbox1, tr( "Move Down" ), "mpegplayer/down", d->selectedFiles, SLOT(moveSelectedDown()) );
     QVBox *stretch2 = new QVBox( vbox1 ); stretch2->setBackgroundMode( PaletteButton ); // add stretch

    QWidget *aTab;
    aTab = new QWidget( tabWidget, "aTab" );
    audioView = new QListView( aTab, "Audioview" );
    audioView->setMinimumSize(233,260);
    audioView->addColumn( tr("Title"),140);
    audioView->addColumn(tr("Size"), -1);
    audioView->addColumn(tr("Media"),-1);
    audioView->setColumnAlignment(1, Qt::AlignRight);
    audioView->setColumnAlignment(2, Qt::AlignRight);
    audioView->setAllColumnsShowFocus(TRUE);
//     audioView->setMultiSelection( TRUE );
//     audioView->setSelectionMode( QListView::Extended);
    
    tabWidget->insertTab(aTab,tr("Audio"));

    QPEApplication::setStylusOperation( audioView->viewport(),QPEApplication::RightOnHold);
    connect( audioView, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint&, int)),
             this,SLOT( viewPressed(int, QListViewItem *, const QPoint&, int)) );
    

//    audioView
     populateAudioView();
// videowidget
     
    QWidget *vTab;
    vTab = new QWidget( tabWidget, "vTab" );
    videoView = new QListView( vTab, "Videoview" );
    videoView->setMinimumSize(233,260);
    
    videoView->addColumn(tr("Title"),140);
    videoView->addColumn(tr("Size"),-1);
    videoView->addColumn(tr("Media"),-1);
    videoView->setColumnAlignment(1, Qt::AlignRight);
    videoView->setColumnAlignment(2, Qt::AlignRight);
    videoView->setAllColumnsShowFocus(TRUE);
//     videoView->setMultiSelection( TRUE );
//     videoView->setSelectionMode( QListView::Extended);

    QPEApplication::setStylusOperation( videoView->viewport(),QPEApplication::RightOnHold);
    connect( videoView, SIGNAL( mouseButtonPressed( int, QListViewItem *, const QPoint&, int)),
             this,SLOT( viewPressed(int, QListViewItem *, const QPoint&, int)) );

    tabWidget->insertTab( vTab,tr("Video"));


//playlists list
    QWidget *LTab;
    LTab = new QWidget( tabWidget, "LTab" );
    playLists = new FileSelector( "playlist/plain", LTab, "fileselector" , FALSE, FALSE); //buggy
    playLists->setMinimumSize(233,260);;
    tabWidget->insertTab(LTab,tr("Lists"));

    connect( playLists, SIGNAL( fileSelected( const DocLnk &) ), this, SLOT( loadList( const DocLnk & ) ) );
//      connect( playLists, SIGNAL( newSelected( const DocLnk &) ), this, SLOT( newFile( const DocLnk & ) ) );


// add the library area

//     connect( audioView, SIGNAL( rightButtonClicked( QListViewItem *, const QPoint &, int)),
//              this, SLOT( fauxPlay( QListViewItem *) ) );
//     connect( videoView, SIGNAL( rightButtonClicked( QListViewItem *, const QPoint &, int)),
//              this, SLOT( fauxPlay( QListViewItem *)) );

//    connect( audioView, SIGNAL( clicked( QListViewItem *) ), this, SLOT( fauxPlay( QListViewItem *) ) );
//    connect( videoView, SIGNAL( clicked( QListViewItem *) ), this, SLOT( fauxPlay( QListViewItem *) ) );

   connect( audioView, SIGNAL( doubleClicked( QListViewItem *) ), this, SLOT( addToSelection( QListViewItem *) ) );
   connect( videoView, SIGNAL( doubleClicked( QListViewItem *) ), this, SLOT( addToSelection( QListViewItem *) ) );

   connect( tabWidget, SIGNAL (currentChanged(QWidget*)),this,SLOT(tabChanged(QWidget*)));
   connect( mediaPlayerState, SIGNAL( playingToggled( bool ) ),    d->tbPlay,    SLOT( setOn( bool ) ) );
   connect( mediaPlayerState, SIGNAL( loopingToggled( bool ) ),    d->tbLoop,    SLOT( setOn( bool ) ) );
   connect( mediaPlayerState, SIGNAL( shuffledToggled( bool ) ),   d->tbShuffle, SLOT( setOn( bool ) ) );
   connect( mediaPlayerState, SIGNAL( playlistToggled( bool ) ),   this,         SLOT( setPlaylist( bool ) ) );

   connect( d->selectedFiles, SIGNAL( doubleClicked( QListViewItem *) ), this, SLOT( playIt( QListViewItem *) ) );
//   connect( d->selectedFiles, SIGNAL( fileSelected( const DocLnk & ) ), this, SLOT( addToSelection( const DocLnk & ) ) );

    setCentralWidget( vbox5 );

    Config cfg( "MediaPlayer" );
    readConfig( cfg );
    QString currentPlaylist = cfg.readEntry("CurrentPlaylist","");
//    qDebug("currentList is "+currentPlaylist);
    loadList(DocLnk( currentPlaylist));
    setCaption(tr("OpiePlayer: ")+ currentPlaylist );
    
    initializeStates();
}


PlayListWidget::~PlayListWidget() {
    Config cfg( "MediaPlayer" );
    writeConfig( cfg );
     

    if ( d->current )
  delete d->current;
    delete d;
}


void PlayListWidget::initializeStates() {

    d->tbPlay->setOn( mediaPlayerState->playing() );
    d->tbLoop->setOn( mediaPlayerState->looping() );
    d->tbShuffle->setOn( mediaPlayerState->shuffled() );
//     d->tbFull->setOn( mediaPlayerState->fullscreen() );
//     d->tbScale->setOn( mediaPlayerState->scaled() );
//     d->tbScale->setEnabled( mediaPlayerState->fullscreen() );
//    setPlaylist( mediaPlayerState->playlist() );
     setPlaylist( true);
     d->selectedFiles->first();
}


void PlayListWidget::readConfig( Config& cfg ) {
    cfg.setGroup("PlayList");
    QString currentString = cfg.readEntry("current", "" );
    int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
    for ( int i = 0; i < noOfFiles; i++ ) {
        QString entryName;
        entryName.sprintf( "File%i", i + 1 );
        QString linkFile = cfg.readEntry( entryName );
        DocLnk lnk( linkFile );
        if ( lnk.isValid() ) {
            d->selectedFiles->addToSelection( lnk );
        }
    }
//    d->selectedFiles->setSelectedItem( (const QString &)currentString);
}


void PlayListWidget::writeConfig( Config& cfg ) const {

    d->selectedFiles->writeCurrent( cfg);
    cfg.setGroup("PlayList");
    int noOfFiles = 0;
    d->selectedFiles->first();
    do {
        const DocLnk *lnk = d->selectedFiles->current();
        if ( lnk ) {
            QString entryName;
            entryName.sprintf( "File%i", noOfFiles + 1 );
            qDebug(entryName);
            cfg.writeEntry( entryName, lnk->linkFile() );
              // if this link does exist, add it so we have the file
              // next time...
            if ( !QFile::exists( lnk->linkFile() ) ) {
                  // the way writing lnks doesn't really check for out
                  // of disk space, but check it anyway.
                if ( !lnk->writeLink() ) {
                    QMessageBox::critical( 0, tr("Out of space"),
                                           tr( "There was a problem saving "
                                               "the playlist.\n"
                                               "Your playlist "
                                               "may be missing some entries\n"
                                               "the next time you start it." )
                                           );
                }
            }      
            noOfFiles++;
        }
    }
    while ( d->selectedFiles->next() );
    cfg.writeEntry("NumberOfFiles", noOfFiles );
}


void PlayListWidget::addToSelection( const DocLnk& lnk ) {
//    qDebug("add");
    d->setDocumentUsed = FALSE;
    if ( mediaPlayerState->playlist() )
        d->selectedFiles->addToSelection( lnk );
    else
  mediaPlayerState->setPlaying( TRUE );
}


void PlayListWidget::clearList() {
    while ( first() )
  d->selectedFiles->removeSelected();
}


void PlayListWidget::addAllToList() {
    DocLnkSet filesAll;
    Global::findDocuments(&filesAll, "video/*;audio/*");
    QListIterator<DocLnk> Adit( filesAll.children() );
    for ( ; Adit.current(); ++Adit )
        d->selectedFiles->addToSelection( **Adit );
}


void PlayListWidget::addAllMusicToList() {
//    DocLnkSet files;
//    Global::findDocuments(&files, "audio/*");
    QListIterator<DocLnk> dit( files.children() );
    for ( ; dit.current(); ++dit )
        d->selectedFiles->addToSelection( **dit );
}


void PlayListWidget::addAllVideoToList() {
    QListIterator<DocLnk> dit( vFiles.children() );
    for ( ; dit.current(); ++dit )
        d->selectedFiles->addToSelection( **dit );
}


void PlayListWidget::setDocument(const QString& fileref) {
    fromSetDocument = TRUE;
    if ( fileref.isNull() ) {
        QMessageBox::critical( 0, tr( "Invalid File" ), tr( "There was a problem in getting the file." ) );
        return;
    }
//    qDebug("setDocument "+fileref);
    if(fileref.find("playlist",0,TRUE) == -1) {
        clearList();
        addToSelection( DocLnk( fileref ) );
        d->setDocumentUsed = TRUE;
        mediaPlayerState->setPlaying( FALSE );
        qApp->processEvents();
        mediaPlayerState->setPlaying( TRUE );
        qApp->processEvents();
        setCaption(tr("OpiePlayer"));

    } else { //is playlist
        clearList();
        loadList(DocLnk(fileref));
        d->selectedFiles->first();
    }
}


void PlayListWidget::setActiveWindow() {
    // When we get raised we need to ensure that it switches views
    char origView = mediaPlayerState->view();
    mediaPlayerState->setView( 'l' ); // invalidate
    mediaPlayerState->setView( origView ); // now switch back
}


void PlayListWidget::useSelectedDocument() {
    d->setDocumentUsed = FALSE;
}


const DocLnk *PlayListWidget::current() { // this is fugly

//      if( fromSetDocument) {
//          qDebug("from setDoc");
//          DocLnkSet files;
//          Global::findDocuments(&files, "video/*;audio/*");
//          QListIterator<DocLnk> dit( files.children() );
//          for ( ; dit.current(); ++dit ) {
//              if(dit.current()->linkFile() ==  setDocFileRef) {
//                  qDebug(setDocFileRef);
//                  return dit;
//              }
//          }
//      } else
//    qDebug("current");
//     switch (tabWidget->currentPageIndex()) {
//          case 0: //playlist
//          {
    qDebug("playlist");
    if ( mediaPlayerState->playlist() ) {
        return d->selectedFiles->current();
    }
    else if ( d->setDocumentUsed && d->current ) {
        return d->current;
    } else {
        return d->files->selected();
    }
//        }
//        break;
//        case 1://audio
//        {
//            qDebug("audioView");
//            Global::findDocuments(&files, "audio/*");
//            QListIterator<DocLnk> dit( files.children() );
//            for ( ; dit.current(); ++dit ) {
//                if( dit.current()->name() == audioView->currentItem()->text(0) && !insanityBool) {
//                    qDebug("here");
//                    insanityBool=TRUE;
//                    return dit;
//                }
//            }
//        }           
//        break;
//        case 2: // video
//        {
//            qDebug("videoView");
//            Global::findDocuments(&vFiles, "video/*");
//            QListIterator<DocLnk> Vdit( vFiles.children() );
//            for ( ; Vdit.current(); ++Vdit ) {
//                if( Vdit.current()->name() == videoView->currentItem()->text(0) && !insanityBool) {
//                    insanityBool=TRUE;
//                    return Vdit;
//                }
//            }
//        }
//        break;
//      };
//  return 0;
}

bool PlayListWidget::prev() {
    if ( mediaPlayerState->playlist() ) {
        if ( mediaPlayerState->shuffled() ) {
            const DocLnk *cur = current();
            int j = 1 + (int)(97.0 * rand() / (RAND_MAX + 1.0));
            for ( int i = 0; i < j; i++ ) {
                if ( !d->selectedFiles->next() )
                    d->selectedFiles->first();
            }
            if ( cur == current() )
                if ( !d->selectedFiles->next() )
                    d->selectedFiles->first();
            return TRUE;
        } else {
            if ( !d->selectedFiles->prev() ) {
                if ( mediaPlayerState->looping() ) {
                    return d->selectedFiles->last();
                } else {
                    return FALSE;
                }
            }
            return TRUE;
        }
    } else {
        return mediaPlayerState->looping();
    }
}


bool PlayListWidget::next() {
    if ( mediaPlayerState->playlist() ) {
        if ( mediaPlayerState->shuffled() ) {
            return prev();
        } else {
            if ( !d->selectedFiles->next() ) {
                if ( mediaPlayerState->looping() ) {
                    return d->selectedFiles->first();
                } else {
                    return FALSE;
                }
            }
            return TRUE;
        }
    } else {
        return mediaPlayerState->looping();
    }
}


bool PlayListWidget::first() {
    if ( mediaPlayerState->playlist() )
        return d->selectedFiles->first();
    else
        return mediaPlayerState->looping();
}


bool PlayListWidget::last() {
    if ( mediaPlayerState->playlist() )
        return d->selectedFiles->last();
    else
        return mediaPlayerState->looping();
}


void PlayListWidget::saveList() {

    QString filename;
    InputDialog *fileDlg;
    fileDlg = new InputDialog(this,tr("Save Playlist"),TRUE, 0);
    fileDlg->exec();
    if( fileDlg->result() == 1 ) {
     if ( d->current )
         delete d->current;
        filename = fileDlg->LineEdit1->text();//+".playlist";
//         qDebug("saving playlist "+filename+".playlist");
        Config cfg( filename +".playlist");
        writeConfig( cfg );

//             qDebug("same name so delete lnk??");
//         if( playLists->selected()->name() == filename) {

//             qDebug("same name so delete lnk");
//             QFile().remove(playLists->selected()->file());
//             QFile().remove(playLists->selected()->linkFile());
//             playLists->reread();
//         }
//        qDebug("new doclnk");            
        DocLnk lnk;
//        lnk.setComment( "");
        lnk.setFile(QDir::homeDirPath()+"/Settings/"+filename+".playlist.conf"); //sets File property
        lnk.setType("playlist/plain");// hey is this a REGISTERED mime type?!?!? ;D
        lnk.setIcon("mpegplayer/playlist2");
        lnk.setName( filename); //sets file name
        qDebug(filename);
        if(!lnk.writeLink())
            qDebug("Writing doclink did not work");
      }
        Config config( "MediaPlayer" );
        config.writeEntry("CurrentPlaylist",filename);
        setCaption(tr("OpiePlayer: ")+filename);
        d->selectedFiles->first();
     if(fileDlg)
         delete fileDlg;
}

void PlayListWidget::loadList( const DocLnk & lnk) {
    QString name= lnk.name();
//    qDebug("currentList is "+name);
    if( name.length()>1) {
        setCaption("OpiePlayer: "+name);
//    qDebug("load list "+ name+".playlist");
        clearList();
        Config cfg( name+".playlist");
        readConfig(cfg);

        tabWidget->setCurrentPage(0);

        Config config( "MediaPlayer" );
        config.writeEntry("CurrentPlaylist", name);
//        d->selectedFiles->first();
    }
    
}

void PlayListWidget::setPlaylist( bool shown ) {
    if ( shown ) 
  d->playListFrame->show();
    else
  d->playListFrame->hide();
}

void PlayListWidget::setView( char view ) {
    if ( view == 'l' )
  showMaximized();
    else
  hide();
}

void PlayListWidget::addSelected() {

      Config cfg( "MediaPlayer" );
      cfg.setGroup("PlayList");
      QString currentPlaylist = cfg.readEntry("CurrentPlaylist","");
      int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );

      switch (tabWidget->currentPageIndex()) {
        case 0: //playlist
            break;
        case 1: { //audio
            for ( int i = 0; i < noOfFiles; i++ ) {
                QString entryName;
                entryName.sprintf( "File%i", i + 1 );
                QString linkFile = cfg.readEntry( entryName );
                  if( DocLnk( linkFile).name() == audioView->selectedItem()->text(0) ) {
                      int result= QMessageBox::warning(this,tr("OpiePlayer"),
                                  tr("This is all ready in your playlist.\nContinue?"),
                                  tr("Yes"),tr("No"),0,0,1);
                      if (result !=0)
                          return;
                  }
            }
            addToSelection( audioView->selectedItem() );
            tabWidget->setCurrentPage(1);
        }           
        break;
        case 2: { // video
            for ( int i = 0; i < noOfFiles; i++ ) {
                QString entryName;
                entryName.sprintf( "File%i", i + 1 );
                QString linkFile = cfg.readEntry( entryName );
                  if( DocLnk( linkFile).name() == videoView->selectedItem()->text(0) ) {
                      int result= QMessageBox::warning(this,tr("OpiePlayer"),
                                  tr("This is all ready in your playlist.\nContinue?"),
                                  tr("Yes"),tr("No"),0,0,1);
                      if (result !=0)
                          return;
                  }
            }
            addToSelection( videoView->selectedItem() );
            tabWidget->setCurrentPage(2);
        }
        break;
      };
}

void PlayListWidget::removeSelected() {
    d->selectedFiles->removeSelected( );
}

void PlayListWidget::playIt( QListViewItem *it) {
//   d->setDocumentUsed = FALSE;
        mediaPlayerState->setPlaying(TRUE);
}

void PlayListWidget::addToSelection( QListViewItem *it) {
    d->setDocumentUsed = FALSE;
    
    if(it) {
        switch (tabWidget->currentPageIndex()) {
          case 1: {
              QListIterator<DocLnk> dit( files.children() );
              for ( ; dit.current(); ++dit ) {
                  if( dit.current()->name() == it->text(0)) {
                      d->selectedFiles->addToSelection(  **dit );
                  }
              }
          }
              break;
          case 2: {
              QListIterator<DocLnk> dit( vFiles.children() );
              for ( ; dit.current(); ++dit ) {
                  if( dit.current()->name() == it->text(0)) {
                      d->selectedFiles->addToSelection(  **dit );
                  }
              }
          }
              break;
          case 0:
              break;
        };
        tabWidget->setCurrentPage(0);
    }
}

void PlayListWidget::tabChanged(QWidget *widg) {

    switch ( tabWidget->currentPageIndex()) {
      case 0:
      {
          if( !tbDeletePlaylist->isHidden())
              tbDeletePlaylist->hide();
          d->tbRemoveFromList->setEnabled(TRUE);
          d->tbAddToList->setEnabled(FALSE);
      }
      break;
      case 1:
      {
          if( !tbDeletePlaylist->isHidden())
              tbDeletePlaylist->hide();
          d->tbRemoveFromList->setEnabled(FALSE);
          d->tbAddToList->setEnabled(TRUE);
      }
      break;
      case 2:
      {
          if( !tbDeletePlaylist->isHidden())
              tbDeletePlaylist->hide();
          d->tbRemoveFromList->setEnabled(FALSE);
          d->tbAddToList->setEnabled(TRUE);
      }
      break;
      case 3:
      {
          if( tbDeletePlaylist->isHidden())
              tbDeletePlaylist->show();
          playLists->reread();
      }
      break;
    };
}



void PlayListWidget::btnPlay(bool b) {

//    mediaPlayerState->setPlaying(b);
    switch ( tabWidget->currentPageIndex()) {
      case 0:
      {
          mediaPlayerState->setPlaying(b);
      }
      break;
      case 1:
      {
          addToSelection( audioView->selectedItem() );
          mediaPlayerState->setPlaying(b);
//            qApp->processEvents();
          d->selectedFiles->removeSelected( );
          tabWidget->setCurrentPage(1);
           audioView->clearSelection();
//      d->selectedFiles->unSelect();
//            mediaPlayerState->setPlaying(FALSE);
      }
      break;
      case 2:
      {
          addToSelection( videoView->selectedItem() );
          mediaPlayerState->setPlaying(b);
          qApp->processEvents();
          d->selectedFiles->removeSelected( );
          tabWidget->setCurrentPage(2);
           videoView->clearSelection();
//      d->selectedFiles->unSelect();
//            mediaPlayerState->setPlaying(FALSE);
      }
      break;
    };
}

void PlayListWidget::deletePlaylist() {
    switch( QMessageBox::information( this, (tr("Remove Playlist?")),
                                      (tr("You really want to delete\nthis playlist?")),
                                      (tr("Yes")), (tr("No")), 0 )){
        case 0: // Yes clicked,
            QFile().remove(playLists->selected()->file());
            QFile().remove(playLists->selected()->linkFile());
            playLists->reread();
            break;
        case 1: // Cancel
            break;
    };

}

void PlayListWidget::viewPressed( int mouse, QListViewItem *item, const QPoint& point, int i)
{
    switch (mouse) {
      case 1:
          break;
      case 2:{
    QPopupMenu  m;
    m.insertItem( tr( "Play" ), this, SLOT( playSelected() ));
    m.insertItem( tr( "Add to Playlist" ), this, SLOT( addSelected() ));
    m.insertSeparator();
    m.insertItem( tr( "Properties" ), this, SLOT( listDelete() ));
    m.exec( QCursor::pos() );
      }
    break;
    };
}

void PlayListWidget::playSelected()
{
    btnPlay( TRUE);
}

void PlayListWidget::playlistViewPressed( int mouse, QListViewItem *item, const QPoint& point, int i)
{
 switch (mouse) {
      case 1:
          break;
      case 2:{
    QPopupMenu  m;
    m.insertItem( tr( "Play Selected" ), this, SLOT( playSelected() ));
    m.insertItem( tr( "Remove" ), this, SLOT( removeSelected() ));
//      m.insertSeparator();
    m.exec( QCursor::pos() );
      }
    break;
    };

}

void PlayListWidget::listDelete() {
      Config cfg( "MediaPlayer" );
      cfg.setGroup("PlayList");
      QString currentPlaylist = cfg.readEntry("CurrentPlaylist","");
      QString file;
      int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
    switch ( tabWidget->currentPageIndex()) {
      case 0:
          break;
      case 1:
      {
          file = audioView->selectedItem()->text(0);
//           Global::findDocuments(&files, "audio/*");
//           AppLnkSet appFiles;
          QListIterator<DocLnk> dit( files.children() );
          for ( ; dit.current(); ++dit ) {
              if( dit.current()->name() == file) {
                  qDebug(file);
                  LnkProperties prop( dit.current() );
//  connect(&prop, SIGNAL(select(const AppLnk *)), this, SLOT(externalSelected(const AppLnk *)));
                  prop.showMaximized();
                  prop.exec();
              }
          }
     populateAudioView();
      }
      break;
      case 2:
      {
//           file = videoView->selectedItem()->text(0);
//           for ( int i = 0; i < noOfFiles; i++ ) {
//               QString entryName;
//               entryName.sprintf( "File%i", i + 1 );
//               QString linkFile = cfg.readEntry( entryName );
//                   AppLnk lnk( AppLnk(linkFile));
//               if( lnk.name() == file ) {
//                   LnkProperties prop( &lnk);
// //  connect(&prop, SIGNAL(select(const AppLnk *)), this, SLOT(externalSelected(const AppLnk *)));
//                   prop.showMaximized();
//                   prop.exec();
//               }
//            }
      }
          break;
    };
}

void PlayListWidget::populateAudioView() {
//     if(files)
//         files.~DocLnkSet();
    StorageInfo storageInfo;
    const QList<FileSystem> &fs = storageInfo.fileSystems();

    Global::findDocuments(&files, "audio/*");
    QListIterator<DocLnk> dit( files.children() );
    QListIterator<FileSystem> it ( fs );
    audioView->clear();
    QString storage;
    for ( ; dit.current(); ++dit ) {
        for( ; it.current(); ++it ){
            const QString name = (*it)->name();
            const QString path = (*it)->path();
            if(dit.current()->file().find(path) != -1 ) storage=name;
        }

        QListViewItem * newItem;
        if ( QFile( dit.current()->file()).exists() ) {
            newItem= /*(void)*/ new QListViewItem( audioView, dit.current()->name(), QString::number( QFile( dit.current()->file()).size() ), storage);
            newItem->setPixmap(0, Resource::loadPixmap( "mpegplayer/musicfile" ));
        }
    }
}

void PlayListWidget::populateVideoView() {
    StorageInfo storageInfo;
    const QList<FileSystem> &fs = storageInfo.fileSystems();

    Global::findDocuments(&vFiles, "video/*");
    QListIterator<DocLnk> Vdit( vFiles.children() );
    QListIterator<FileSystem> it ( fs );
    videoView->clear();
    QString storage;
    for ( ; Vdit.current(); ++Vdit ) {
        for( ; it.current(); ++it ){
            const QString name = (*it)->name();
            const QString path = (*it)->path();
            if( Vdit.current()->file().find(path) != -1 ) storage=name;
        }

        QListViewItem * newItem;
        if ( QFile( Vdit.current()->file()).exists() ) {
            newItem= /*(void)*/ new QListViewItem( videoView, Vdit.current()->name(), QString::number( QFile( Vdit.current()->file()).size() ), storage);
            newItem->setPixmap(0, Resource::loadPixmap( "mpegplayer/videofile" ));
        }
    }
}