Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

playlistwidget.cpp

Go to the documentation of this file.
00001 /*
00002                     This file is part of the Opie Project
00003 
00004                       Copyright (c)  2002,2003 Max Reiss <harlekin@handhelds.org>
00005                              Copyright (c)  2002 L. Potter <ljp@llornkcor.com>
00006                              Copyright (c)  2002 Holger Freyther <zecke@handhelds.org>
00007               =.
00008             .=l.
00009      .>+-=
00010 _;:,   .>  :=|.         This program is free software; you can
00011 .> <`_,  > .  <=          redistribute it and/or  modify it under
00012 :`=1 )Y*s>-.--  :           the terms of the GNU General Public
00013 .="- .-=="i,   .._         License as published by the Free Software
00014 - .  .-<_>   .<>         Foundation; either version 2 of the License,
00015   ._= =}    :          or (at your option) any later version.
00016   .%`+i>    _;_.
00017   .i_,=:_.   -<s.       This program is distributed in the hope that
00018   + . -:.    =       it will be useful,  but WITHOUT ANY WARRANTY;
00019   : ..  .:,   . . .    without even the implied warranty of
00020   =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
00021  _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU
00022 ..}^=.=    =    ;      General Public License for more
00023 ++=  -.   .`   .:       details.
00024 :   = ...= . :.=-
00025 -.  .:....=;==+<;          You should have received a copy of the GNU
00026  -_. . .  )=. =           General Public License along with
00027   --    :-=`           this library; see the file COPYING.LIB.
00028                              If not, write to the Free Software Foundation,
00029                              Inc., 59 Temple Place - Suite 330,
00030                              Boston, MA 02111-1307, USA.
00031 
00032 */
00033 
00034 #include "playlistselection.h"
00035 #include "playlistwidget.h"
00036 #include "mediaplayer.h"
00037 #include "inputDialog.h"
00038 #include "om3u.h"
00039 #include "playlistfileview.h"
00040 
00041 /* OPIE */
00042 #include <opie2/odebug.h>
00043 #include <opie2/ofiledialog.h>
00044 using namespace Opie::Core;
00045 using namespace Opie::Ui;
00046 
00047 /* QT */
00048 #include <qmessagebox.h>
00049 #include <qtoolbar.h>
00050 
00051 /* STD */
00052 #include <assert.h>
00053 
00054 PlayListWidget::PlayListWidget(QWidget* parent, const char* name, WFlags fl )
00055     : PlayListWidgetGui( parent, "playList" ) , currentFileListView( 0 )
00056 {
00057     Global::statusMessage( tr( "Loading of Skin started" ) );
00058     mediaPlayerState = new MediaPlayerState(0, "mediaPlayerState" );
00059     m_mp = new MediaPlayer(*this, *mediaPlayerState, 0, "mediaPlayer");
00060 
00061 
00062 
00063     d->tbAddToList =  new ToolButton( bar, tr( "Add to Playlist" ),
00064                                       "opieplayer2/add_to_playlist",
00065                                       this , SLOT(addSelected() ) );
00066     d->tbRemoveFromList = new ToolButton( bar, tr( "Remove from Playlist" ),
00067                                           "opieplayer2/remove_from_playlist",
00068                                           this , SLOT(removeSelected() ) );
00069     d->tbPlay    = new ToolButton( bar, tr( "Play" ), "opieplayer2/play",
00070                                    this , SLOT( btnPlay(bool) ), TRUE );
00071     d->tbShuffle = new ToolButton( bar, tr( "Randomize" ),"opieplayer2/shuffle",
00072                                    mediaPlayerState, SLOT( setShuffled(bool) ), TRUE );
00073     d->tbLoop    = new ToolButton( bar, tr( "Loop" ), "opieplayer2/loop",
00074                                    mediaPlayerState, SLOT( setLooping(bool) ), TRUE );
00075 
00076     (void)new MenuItem( pmPlayList, tr( "Clear List" ), this, SLOT( clearList() ) );
00077     (void)new MenuItem( pmPlayList, tr( "Add all audio files" ),
00078                         this, SLOT( addAllMusicToList() ) );
00079     (void)new MenuItem( pmPlayList, tr( "Add all video files" ),
00080                         this, SLOT( addAllVideoToList() ) );
00081     (void)new MenuItem( pmPlayList, tr( "Add all files" ),
00082                         this, SLOT( addAllToList() ) );
00083     pmPlayList->insertSeparator(-1);
00084     (void)new MenuItem( pmPlayList, tr( "Add File" ),
00085                         this,SLOT( openFile() ) );
00086     (void)new MenuItem( pmPlayList, tr("Add URL"),
00087                         this,SLOT( openURL() ) );
00088     (void)new MenuItem( pmPlayList, tr( "Add Directory"),
00089                         this, SLOT(openDirectory()));
00090     pmPlayList->insertSeparator(-1);
00091     (void)new MenuItem( pmPlayList, tr( "Save Playlist" ),
00092                         this, SLOT(writem3u() ) );
00093     pmPlayList->insertSeparator(-1);
00094     (void)new MenuItem( pmPlayList, tr( "Rescan for Audio Files" ),
00095                         audioView, SLOT( scanFiles() ) );
00096     (void)new MenuItem( pmPlayList, tr( "Rescan for Video Files" ),
00097                         videoView, SLOT( scanFiles() ) );
00098 
00099     pmView->insertItem(  Opie::Core::OResource::loadPixmap("fullscreen", Opie::Core::OResource::SmallIcon) , tr( "Full Screen"),
00100                          mediaPlayerState, SLOT( toggleFullscreen() ) );
00101 
00102     Config cfg( "OpiePlayer" );
00103     bool b= cfg.readBoolEntry("FullScreen", 0);
00104     mediaPlayerState->setFullscreen(  b );
00105     pmView->setItemChecked( -16, b );
00106 
00107     (void)new ToolButton( vbox1, tr( "Move Up" ), "opieplayer2/up",
00108                           d->selectedFiles, SLOT(moveSelectedUp() ) );
00109     (void)new ToolButton( vbox1, tr( "Remove" ), "opieplayer2/cut",
00110                           d->selectedFiles, SLOT(removeSelected() ) );
00111     (void)new ToolButton( vbox1, tr( "Move Down" ), "opieplayer2/down",
00112                           d->selectedFiles, SLOT(moveSelectedDown() ) );
00113     QVBox *stretch2 = new QVBox( vbox1 );
00114 
00115     connect( tbDeletePlaylist, ( SIGNAL( released() ) ),
00116              SLOT( deletePlaylist() ) );
00117     connect( pmView, SIGNAL( activated(int) ),
00118              this, SLOT( pmViewActivated(int) ) );
00119     connect( skinsMenu, SIGNAL( activated(int) ) ,
00120              this, SLOT( skinsMenuActivated(int) ) );
00121     connect( d->selectedFiles, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ),
00122              this,SLOT( playlistViewPressed(int,QListViewItem*,const QPoint&,int) ) );
00123     connect( audioView, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ),
00124              this,SLOT( viewPressed(int,QListViewItem*,const QPoint&,int) ) );
00125     connect( audioView, SIGNAL( returnPressed(QListViewItem*) ),
00126              this,SLOT( playIt(QListViewItem*) ) );
00127     connect( audioView, SIGNAL( doubleClicked(QListViewItem*) ),
00128              this, SLOT( addToSelection(QListViewItem*) ) );
00129     connect( videoView, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int) ),
00130              this,SLOT( viewPressed(int,QListViewItem*,const QPoint&,int) ) );
00131     connect( videoView, SIGNAL( returnPressed(QListViewItem*) ),
00132              this,SLOT( playIt(QListViewItem*) ) );
00133     connect( videoView, SIGNAL( doubleClicked(QListViewItem*) ),
00134              this, SLOT( addToSelection(QListViewItem*) ) );
00135     connect( playLists, SIGNAL( fileSelected(const DocLnk&) ),
00136              this, SLOT( loadList(const DocLnk&) ) );
00137     connect( tabWidget, SIGNAL ( currentChanged(QWidget*) ),
00138              this, SLOT( tabChanged(QWidget*) ) );
00139     connect( mediaPlayerState, SIGNAL( playingToggled(bool) ),
00140              d->tbPlay,  SLOT( setOn(bool) ) );
00141     connect( mediaPlayerState, SIGNAL( loopingToggled(bool) ),
00142              d->tbLoop, SLOT( setOn(bool) ) );
00143     connect( mediaPlayerState, SIGNAL( shuffledToggled(bool) ),
00144              d->tbShuffle, SLOT( setOn(bool) ) );
00145     connect( d->selectedFiles, SIGNAL( doubleClicked(QListViewItem*) ),
00146              this, SLOT( playIt(QListViewItem*) ) );
00147     connect ( gammaSlider,  SIGNAL( valueChanged(int) ),
00148               mediaPlayerState,  SLOT( setVideoGamma(int) ) );
00149 
00150     connect( this, SIGNAL(skinSelected() ),
00151              m_mp, SLOT( reloadSkins() ) );
00152 
00153     // see which skins are installed
00154     populateSkinsMenu();
00155     initializeStates();
00156 
00157     channel = new QCopChannel( "QPE/Application/opieplayer2", this );
00158     connect( channel, SIGNAL(received(const QCString&,const QByteArray&)),
00159         this, SLOT( qcopReceive(const QCString&,const QByteArray&)) );
00160 
00161 
00162     cfg.setGroup("PlayList");
00163     QString currentPlaylist = cfg.readEntry( "CurrentPlaylist", "default");
00164     loadList(DocLnk(  currentPlaylist ) );
00165 
00166     tabWidget->showPage( playListTab );
00167 }
00168 
00169 
00170 PlayListWidget::~PlayListWidget() {
00171     delete d;
00172     delete m_mp;
00173 }
00174 
00175 
00176 void PlayListWidget::initializeStates() {
00177     d->tbPlay->setOn( mediaPlayerState->isPlaying() );
00178     d->tbLoop->setOn( mediaPlayerState->isLooping() );
00179     d->tbShuffle->setOn( mediaPlayerState->isShuffled() );
00180     d->playListFrame->show();
00181 }
00182 
00183 void PlayListWidget::setButtons(void) {
00184     bool hasitem;
00185 
00186     switch ( currentTab() ) {
00187     case CurrentPlayList:
00188     {
00189         hasitem = !d->selectedFiles->isEmpty();
00190         d->tbPlay->setEnabled( hasitem );
00191         d->tbRemoveFromList->setEnabled( hasitem );
00192     }
00193     break;
00194     case AudioFiles:
00195     {
00196         d->tbPlay->setEnabled( audioView->hasSelection() );
00197         d->tbRemoveFromList->setEnabled( false );
00198     }
00199     break;
00200     case VideoFiles:
00201     {
00202         d->tbPlay->setEnabled( videoView->hasSelection() );
00203         d->tbRemoveFromList->setEnabled( false );
00204     }
00205     break;
00206     case PlayLists:
00207     {
00208         d->tbPlay->setEnabled( false );
00209         d->tbRemoveFromList->setEnabled( false );
00210     }
00211     break;
00212     };
00213 }
00214 
00215 void PlayListWidget::writeDefaultPlaylist() {
00216 
00217     Config config( "OpiePlayer" );
00218     config.setGroup( "PlayList" );
00219     QString filename=QPEApplication::documentDir() + "/default.m3u";
00220     QString currentString = config.readEntry( "CurrentPlaylist", filename);
00221     if( currentString == filename) {
00222         Om3u *m3uList;
00223       if( d->selectedFiles->first() ) {
00224         m3uList = new Om3u(filename, IO_ReadWrite | IO_Truncate);
00225         do {
00226           m3uList->add( d->selectedFiles->current()->file() );
00227         }
00228         while ( d->selectedFiles->next() );
00229 
00230         m3uList->write();
00231         m3uList->close();
00232         delete m3uList;
00233 
00234       }
00235     }
00236 }
00237 
00238 void PlayListWidget::addToSelection( const DocLnk& lnk ) {
00239     d->setDocumentUsed = FALSE;
00240     if( QFileInfo( lnk.file() ).exists() ||
00241         lnk.file().left(4) == "http" ) {
00242         d->selectedFiles->addToSelection( lnk );
00243     }
00244 }
00245 
00246 
00247 void PlayListWidget::clearList() {
00248     while ( first() ) {
00249         d->selectedFiles->removeSelected();
00250     }
00251   Config cfg( "OpiePlayer" );
00252   cfg.setGroup("PlayList");
00253   cfg.writeEntry("CurrentPlaylist","default");
00254   setCaption("OpiePlayer");
00255   setButtons();
00256 }
00257 
00258 void PlayListWidget::viewPressed( int mouse, QListViewItem *, const QPoint& , int) {
00259     switch (mouse) {
00260     case LeftButton:
00261         break;
00262     case RightButton:
00263     {
00264         QPopupMenu  m;
00265         m.insertItem( tr( "Play Selected" ), this, SLOT( playSelected() ));
00266         m.insertItem( tr( "Add to Playlist" ), this, SLOT( addSelected() ));
00267         m.exec( QCursor::pos() );
00268     }
00269         break;
00270     }
00271 }
00272 
00273 
00274 void PlayListWidget::playlistViewPressed( int mouse, QListViewItem *, const QPoint& , int ) {
00275     switch (mouse) {
00276     case LeftButton:
00277         break;
00278     case RightButton:
00279     {
00280         QPopupMenu  m;
00281         m.insertItem( tr( "Play" ), this, SLOT( playSelected() ));
00282         m.insertItem( tr( "Remove" ), this, SLOT( removeSelected() ));
00283         m.exec( QCursor::pos() );
00284     }
00285     break;
00286     }
00287 }
00288 
00289 
00290 void PlayListWidget::addAllToList() {
00291 
00292 
00293   audioView->populateView();
00294 
00295   QListViewItemIterator audioIt( audioView );
00296   DocLnk lnk;
00297   QString filename;
00298   // iterate through all items of the listview
00299   for ( ; audioIt.current(); ++audioIt ) {
00300     filename = audioIt.current()->text(3);
00301     lnk.setName( QFileInfo(filename).baseName() ); //sets name
00302     lnk.setFile( filename ); //sets file name
00303     d->selectedFiles->addToSelection(  lnk);
00304   }
00305 
00306   videoView->populateView();
00307 
00308   QListViewItemIterator videoIt( videoView );
00309   for ( ; videoIt.current(); ++videoIt ) {
00310     filename = videoIt.current()->text(3);
00311     lnk.setName( QFileInfo(filename).baseName() ); //sets name
00312     lnk.setFile( filename ); //sets file name
00313     d->selectedFiles->addToSelection(  lnk);
00314   }
00315 
00316     tabWidget->setCurrentPage(0);
00317 
00318     writeCurrentM3u();
00319     d->selectedFiles->first();
00320 
00321   setButtons();
00322 }
00323 
00324 
00325 void PlayListWidget::addAllMusicToList() {
00326 
00327   audioView->populateView();
00328 
00329   QListViewItemIterator audioIt( audioView );
00330   DocLnk lnk;
00331   QString filename;
00332   // iterate through all items of the listview
00333   for ( ; audioIt.current(); ++audioIt ) {
00334     filename = audioIt.current()->text(3);
00335     lnk.setName( QFileInfo(filename).baseName() ); //sets name
00336     lnk.setFile( filename ); //sets file name
00337     d->selectedFiles->addToSelection(  lnk);
00338   }
00339 
00340     tabWidget->setCurrentPage(0);
00341     writeCurrentM3u();
00342     d->selectedFiles->first();
00343 
00344   setButtons();
00345 }
00346 
00347 
00348 void PlayListWidget::addAllVideoToList() {
00349 
00350   videoView->populateView();
00351 
00352   QListViewItemIterator videoIt( videoView );
00353   DocLnk lnk;
00354   QString filename;
00355   for ( ; videoIt.current(); ++videoIt ) {
00356     filename = videoIt.current()->text(3);
00357     lnk.setName( QFileInfo(filename).baseName() ); //sets name
00358     lnk.setFile( filename ); //sets file name
00359     d->selectedFiles->addToSelection(  lnk);
00360   }
00361     tabWidget->setCurrentPage(0);
00362     writeCurrentM3u();
00363     d->selectedFiles->first();
00364 
00365   setButtons();
00366 }
00367 
00368 
00369 void PlayListWidget::setDocument( const QString& _fileref ) {
00370   //    odebug << "<<<<<<<<set document>>>>>>>>>> "+fileref << oendl;
00371     QString fileref  = _fileref;
00372     fromSetDocument = TRUE;
00373 
00374     DocLnk lnk(_fileref);
00375     if(lnk.isValid())
00376         fileref = lnk.file();
00377 
00378     QFileInfo fileInfo(fileref);
00379 
00380     if ( !fileInfo.exists() ) {
00381         QMessageBox::warning( this, tr( "Invalid File" ),
00382                                tr( "There was a problem in getting the file." ) );
00383         return;
00384     }
00385 
00386     clearList();
00387     QString extension = fileInfo.extension(false);
00388 
00389     if( extension.find( "m3u", 0, false) != -1
00390        || extension.find( "pls", 0, false) != -1 ) {
00391         readListFromFile( fileref );
00392     } else {
00393         clearList();
00394         DocLnk lnk;
00395         lnk.setName( fileInfo.baseName() ); //sets name
00396         lnk.setFile( fileref ); //sets file name
00397         addToSelection( lnk );
00398         writeCurrentM3u();
00399 
00400         d->setDocumentUsed = TRUE;
00401         mediaPlayerState->setPlaying( FALSE );
00402         mediaPlayerState->setPlaying( TRUE );
00403     }
00404 }
00405 
00406 
00407 void PlayListWidget::useSelectedDocument() {
00408     d->setDocumentUsed = FALSE;
00409 }
00410 
00411 
00412 const DocLnk *PlayListWidget::current() const { // this is fugly
00413     assert( currentTab() == CurrentPlayList );
00414 
00415     const DocLnk *lnk = d->selectedFiles->current();
00416     if ( !lnk ) {
00417         d->selectedFiles->first();
00418         lnk = d->selectedFiles->current();
00419     }
00420     assert( lnk );
00421     return lnk;
00422 }
00423 
00424 
00425 bool PlayListWidget::prev() {
00426     if ( mediaPlayerState->isShuffled() ) {
00427         const DocLnk *cur = current();
00428         int j = 1 + (int)(97.0 * rand() / (RAND_MAX + 1.0));
00429         for ( int i = 0; i < j; i++ ) {
00430             if ( !d->selectedFiles->next() )
00431                 d->selectedFiles->first();
00432         }
00433         if ( cur == current() )
00434             if ( !d->selectedFiles->next() ) {
00435                 d->selectedFiles->first();
00436             }
00437         return TRUE;
00438     } else {
00439         if ( !d->selectedFiles->prev() ) {
00440             if ( mediaPlayerState->isLooping() ) {
00441                 return d->selectedFiles->last();
00442             } else {
00443                 return FALSE;
00444             }
00445         }
00446         return TRUE;
00447     }
00448 }
00449 
00450 
00451 bool PlayListWidget::next() {
00452 //odebug << "<<<<<<<<<<<<next()" << oendl;
00453     if ( mediaPlayerState->isShuffled() ) {
00454         return prev();
00455     } else {
00456         if ( !d->selectedFiles->next() ) {
00457             if ( mediaPlayerState->isLooping() ) {
00458                 return d->selectedFiles->first();
00459             } else {
00460                 return FALSE;
00461             }
00462         }
00463         return TRUE;
00464     }
00465 }
00466 
00467 
00468 bool PlayListWidget::first() {
00469     return d->selectedFiles->first();
00470 }
00471 
00472 
00473 bool PlayListWidget::last() {
00474     return d->selectedFiles->last();
00475 }
00476 
00477 
00478  void PlayListWidget::saveList() {
00479    writem3u();
00480  }
00481 
00482 
00483 void PlayListWidget::loadList( const DocLnk & lnk) {
00484     QString name = lnk.name();
00485 
00486     if( name.length()>0) {
00487         setCaption("OpiePlayer: "+name);
00488         clearList();
00489         readListFromFile(lnk.file());
00490         tabWidget->setCurrentPage(0);
00491     }
00492 }
00493 
00494 void PlayListWidget::addSelected() {
00495   assert( inFileListMode() );
00496 
00497   QListViewItemIterator it( currentFileListView );
00498   for ( ; it.current(); ++it )
00499       if ( it.current()->isSelected() ) {
00500           QString filename = it.current()->text(3);
00501 
00502           DocLnk lnk;
00503           lnk.setName( QFileInfo( filename ).baseName() ); //sets name
00504           lnk.setFile( filename ); //sets file name
00505 
00506           d->selectedFiles->addToSelection( lnk );
00507       }
00508 
00509   currentFileListView->clearSelection();
00510 
00511   writeCurrentM3u();
00512 }
00513 
00514 
00515 void PlayListWidget::removeSelected() {
00516     d->selectedFiles->removeSelected( );
00517     writeCurrentM3u();
00518     setButtons();
00519 }
00520 
00521 
00522 void PlayListWidget::playIt( QListViewItem *it) {
00523     if(!it) return;
00524     mediaPlayerState->setPlaying(FALSE);
00525     mediaPlayerState->setPlaying(TRUE);
00526     d->selectedFiles->unSelect();
00527 }
00528 
00529 
00530 void PlayListWidget::addToSelection( QListViewItem *it) {
00531   d->setDocumentUsed = FALSE;
00532 
00533   if(it) {
00534     if ( currentTab() == CurrentPlayList )
00535         return;
00536     DocLnk lnk;
00537     QString filename;
00538 
00539     filename=it->text(3);
00540     lnk.setName( QFileInfo(filename).baseName() ); //sets name
00541     lnk.setFile( filename ); //sets file name
00542     d->selectedFiles->addToSelection(  lnk);
00543 
00544     writeCurrentM3u();
00545 //    tabWidget->setCurrentPage(0);
00546 
00547   }
00548 }
00549 
00550 
00551 void PlayListWidget::tabChanged(QWidget *) {
00552 
00553     disconnect( audioView, SIGNAL( itemsSelected(bool) ),
00554                 d->tbPlay, SLOT( setEnabled(bool) ) );
00555     disconnect( videoView, SIGNAL( itemsSelected(bool) ),
00556                 d->tbPlay, SLOT( setEnabled(bool) ) );
00557 
00558     currentFileListView = 0;
00559 
00560     switch ( currentTab() ) {
00561     case CurrentPlayList:
00562     {
00563         if( !tbDeletePlaylist->isHidden() )  {
00564             tbDeletePlaylist->hide();
00565         }
00566         d->tbAddToList->setEnabled(FALSE);
00567 
00568     }
00569     break;
00570     case AudioFiles:
00571     {
00572         audioView->populateView();
00573 
00574         if( !tbDeletePlaylist->isHidden() ) {
00575             tbDeletePlaylist->hide();
00576         }
00577         d->tbAddToList->setEnabled(TRUE);
00578 
00579         connect( audioView, SIGNAL( itemsSelected(bool) ),
00580                  d->tbPlay, SLOT( setEnabled(bool) ) );
00581 
00582         currentFileListView = audioView;
00583     }
00584     break;
00585     case VideoFiles:
00586     {
00587         videoView->populateView();
00588         if( !tbDeletePlaylist->isHidden() ) {
00589             tbDeletePlaylist->hide();
00590         }
00591         d->tbAddToList->setEnabled(TRUE);
00592 
00593         connect( videoView, SIGNAL( itemsSelected(bool) ),
00594                  d->tbPlay, SLOT( setEnabled(bool) ) );
00595 
00596         currentFileListView = videoView;
00597     }
00598     break;
00599     case PlayLists:
00600     {
00601         if( tbDeletePlaylist->isHidden() ) {
00602             tbDeletePlaylist->show();
00603         }
00604         playLists->reread();
00605         d->tbAddToList->setEnabled(FALSE);
00606 
00607     }
00608     break;
00609     };
00610 
00611     setButtons();
00612 }
00613 
00614 
00615 void PlayListWidget::btnPlay(bool b) {
00616 //    mediaPlayerState->setPlaying(false);
00617     mediaPlayerState->setPlaying(b);
00618     insanityBool=FALSE;
00619 }
00620 
00621 void PlayListWidget::deletePlaylist() {
00622   switch( QMessageBox::information( this, (tr("Remove Playlist?")),
00623                                     (tr("You really want to delete\nthis playlist?")),
00624                                     (tr("Yes")), (tr("No")), 0 )){
00625   case 0: // Yes clicked,
00626     QFile().remove(playLists->selectedDocument().file());
00627     QFile().remove(playLists->selectedDocument().linkFile());
00628     playLists->reread();
00629     break;
00630   case 1: // Cancel
00631     break;
00632   };
00633 }
00634 
00635 
00636 void PlayListWidget::playSelected() {
00637     btnPlay( TRUE);
00638 }
00639 
00640 bool PlayListWidget::inFileListMode() const
00641 {
00642     TabType tab = currentTab();
00643     return tab == AudioFiles || tab == VideoFiles;
00644 }
00645 
00646 void PlayListWidget::openURL() {
00647     // http://66.28.164.33:2080
00648     // http://somafm.com/star0242.m3u
00649     QString filename, name;
00650     InputDialog *fileDlg;
00651     fileDlg = new InputDialog(this,tr("Add  URL"),TRUE, 0);
00652     fileDlg->exec();
00653     if( fileDlg->result() == 1 ) {
00654         filename = fileDlg->text();
00655         odebug << "Selected filename is " + filename << oendl;
00656         //            Om3u *m3uList;
00657             DocLnk lnk;
00658             Config cfg( "OpiePlayer" );
00659             cfg.setGroup("PlayList");
00660 
00661         if(filename.left(4) == "http") {
00662             QString m3uFile, m3uFilePath;
00663             if(filename.find(":",8,TRUE) != -1) { //found a port
00664                 m3uFile = filename.left( filename.find( ":",8,TRUE));
00665                 m3uFile = m3uFile.right(  7);
00666             } else if(filename.left(4) == "http"){
00667                 m3uFile=filename;
00668                 m3uFile = m3uFile.right( m3uFile.length() - 7);
00669             } else{
00670                 m3uFile=filename;
00671             }
00672 
00673             lnk.setName( filename ); //sets name
00674             lnk.setFile( filename ); //sets file name
00675 
00676 //            lnk.setIcon("opieplayer2/musicfile");
00677 
00678             d->selectedFiles->addToSelection(  lnk );
00679             writeCurrentM3u();
00680             d->selectedFiles->setSelectedItem( lnk.name());
00681         }
00682         else if( filename.right( 3) == "m3u" ||  filename.right(3) == "pls" ) {
00683             readListFromFile( filename );
00684         } else {
00685             lnk.setName( QFileInfo(filename).baseName() ); //sets name
00686             lnk.setFile( filename ); //sets file name
00687             d->selectedFiles->addToSelection(  lnk);
00688             writeCurrentM3u();
00689             d->selectedFiles->setSelectedItem( lnk.name());
00690         }
00691     }
00692 
00693 
00694     delete fileDlg;
00695 }
00696 
00697 
00698 static MimeTypes fileSelectorMimeTypes() {
00699     MimeTypes types;
00700     QStringList audio, video, all;
00701     audio << "audio/*";
00702     audio << "playlist/plain";
00703     audio << "audio/x-ogg";
00704     audio << "audio/x-mpegurl";
00705 
00706     video << "video/*";
00707     video << "playlist/plain";
00708 
00709     all += audio;
00710     all += video;
00711     types.insert("All Media Files", all );
00712     types.insert("Audio",  audio );
00713     types.insert("Video", video );
00714 
00715     return types;
00716 }
00717 
00718 void PlayListWidget::openFile() {
00719 
00720     QString filename, name;
00721 
00722     Config cfg( "OpiePlayer" );
00723     cfg.setGroup("Dialog");
00724     MimeTypes types = fileSelectorMimeTypes();
00725 
00726 
00727     QString str = OFileDialog::getOpenFileName( 1, QString::null,
00728                                                 QString::null, types, 0,
00729                                                 tr("Add File to Playlist") );
00730 
00731     if( str.isEmpty() )
00732         return;
00733 
00734     if(str.left(2) == "//") str=str.right(str.length()-1);
00735 
00736 
00737     if( str.right( 3) == "m3u" ||  str.right(3) == "pls" ) {
00738         readListFromFile( str );
00739     } else {
00740         QFileInfo info( str );
00741         DocLnk lnk = addFileToPlaylist( str, info.baseName() );
00742         d->selectedFiles->setSelectedItem( lnk.name() );
00743     }
00744 
00745     setButtons();
00746 }
00747 
00748 void PlayListWidget::openDirectory() {
00749     QString str = OFileDialog::getDirectory( OFileSelector::DirectorySelector,
00750                                              QString::null, 0,
00751                                              tr( "Add Files from Directory"));
00752 
00753     if(str.isEmpty() )
00754         return;
00755 
00756     if(str.left(2) == "//") str=str.right(str.length()-1);
00757     QDir dir( str );
00758     QStringList lst = dir.entryList(QDir::Files|QDir::Readable);
00759 
00760     for ( QStringList::Iterator it = lst.begin(); it != lst.end(); ++it ) {
00761         QString filename = str + "/" + *it;
00762 
00763         if( filename.right( 3) == "m3u" ||  filename.right(3) == "pls" )
00764             readListFromFile( filename );
00765         else {
00766             addFileToPlaylist( filename, QFileInfo(*it).baseName() );
00767         }
00768     }
00769 
00770     if (!d->selectedFiles->isEmpty()) {
00771         d->selectedFiles->first();
00772 
00773         setButtons();
00774     }
00775 }
00776 
00777 void PlayListWidget::readListFromFile( const QString &filename ) {
00778     odebug << "read list filename " + filename << oendl;
00779     QFileInfo fi(filename);
00780     Om3u *m3uList;
00781     QString s, name;
00782     m3uList = new Om3u( filename, IO_ReadOnly );
00783     if(fi.extension(false).find("m3u",0,false) != -1 )
00784         m3uList->readM3u();
00785     else if(fi.extension(false).find("pls",0,false) != -1 )
00786         m3uList->readPls();
00787 
00788     DocLnk lnk;
00789     for ( QStringList::ConstIterator it = m3uList->begin(); it != m3uList->end(); ++it ) {
00790         s = *it;
00791         //        odebug << s << oendl;
00792         if(s.left(4)=="http") {
00793           lnk.setName( s ); //sets file name
00794           lnk.setIcon("opieplayer2/musicfile");
00795           lnk.setFile( s ); //sets file name
00796 
00797         }  else { //is file
00798           lnk.setName( QFileInfo(s).baseName());
00799           if(s.left(1) != "/")  {
00800 
00801             lnk.setFile( QFileInfo(filename).dirPath()+"/"+s);
00802           } else {
00803             lnk.setFile( s);
00804           }
00805         }
00806         d->selectedFiles->addToSelection( lnk );
00807     }
00808     Config config( "OpiePlayer" );
00809     config.setGroup( "PlayList" );
00810 
00811     config.writeEntry("CurrentPlaylist",filename);
00812     config.write();
00813     currentPlayList=filename;
00814 
00815     m3uList->close();
00816     delete m3uList;
00817 
00818     d->selectedFiles->setSelectedItem( s);
00819     setCaption(tr("OpiePlayer: ")+ QFileInfo(filename).baseName());
00820 
00821     setButtons();
00822 }
00823 
00824 //  writes current playlist to current m3u file */
00825  void PlayListWidget::writeCurrentM3u() {
00826    odebug << "writing to current m3u" << oendl;
00827    Config cfg( "OpiePlayer" );
00828    cfg.setGroup("PlayList");
00829    QString currentPlaylist = cfg.readEntry("CurrentPlaylist","default");
00830 
00831    Om3u *m3uList;
00832    m3uList = new Om3u( currentPlaylist, IO_ReadWrite | IO_Truncate );
00833    if( d->selectedFiles->first()) {
00834 
00835        do {
00836            //      odebug << "add writeCurrentM3u " +d->selectedFiles->current()->file() << oendl;
00837            m3uList->add( d->selectedFiles->current()->file() );
00838        }
00839        while ( d->selectedFiles->next() );
00840        //    odebug << "<<<<<<<<<<<<>>>>>>>>>>>>>>>>>" << oendl;
00841        m3uList->write();
00842       m3uList->close();
00843    }
00844    delete m3uList;
00845 
00846  }
00847 
00848   /*
00849  writes current playlist to m3u file */
00850 void PlayListWidget::writem3u() {
00851 
00852     Config cfg( "OpiePlayer" );
00853     cfg.setGroup("Dialog");
00854     MimeTypes types = fileSelectorMimeTypes();
00855 
00856     QString str = OFileDialog::getOpenFileName( 1, QString::null,
00857                                                 QString::null, types, 0,
00858                                                 tr( "Save Playlist" ));
00859 
00860     if(str.left(2) == "//") str=str.right(str.length()-1);
00861 
00862     QString name, filename, list;
00863     Om3u *m3uList;
00864 
00865     if( !str.isEmpty() ) {
00866         name = str;
00867         //       name = fileDlg->text();
00868 //        odebug << filename << oendl;
00869         if( name.find("/",0,true) != -1) {// assume they specify a file path
00870             filename = name;
00871             name = name.right(name.length()- name.findRev("/",-1,true) - 1 );
00872         }
00873         else //otherwise dump it somewhere noticable
00874             filename = QPEApplication::documentDir() + "/" + name;
00875 
00876         if( filename.right( 3 ) != "m3u" ) //needs filename extension
00877             filename += ".m3u";
00878 
00879         if( d->selectedFiles->first()) { //ramble through playlist view
00880             m3uList = new Om3u( filename, IO_ReadWrite | IO_Truncate);
00881 
00882             do {
00883             m3uList->add( d->selectedFiles->current()->file());
00884           }
00885           while ( d->selectedFiles->next() );
00886           //    odebug << list << oendl;
00887           m3uList->write();
00888           m3uList->close();
00889           delete m3uList;
00890 
00891           //delete fileDlg;
00892 
00893           DocLnk lnk;
00894           lnk.setFile( filename);
00895           lnk.setIcon("opieplayer2/playlist2");
00896           lnk.setName( name); //sets file name
00897 
00898           // odebug << filename << oendl;
00899           Config config( "OpiePlayer" );
00900           config.setGroup( "PlayList" );
00901 
00902           config.writeEntry("CurrentPlaylist",filename);
00903           currentPlayList=filename;
00904 
00905           if(!lnk.writeLink()) {
00906             odebug << "Writing doclink did not work" << oendl;
00907           }
00908 
00909           setCaption(tr("OpiePlayer: ") + name);
00910         }
00911     }
00912 }
00913 
00914 void PlayListWidget::keyReleaseEvent( QKeyEvent *e ) {
00915   switch ( e->key() ) {
00917   case Key_F9: //activity
00918     //           if(audioUI->isHidden())
00919     //             audioUI->showMaximized();
00920     break;
00921   case Key_F10: //contacts
00922     //           if( videoUI->isHidden())
00923     //             videoUI->showMaximized();
00924     break;
00925   case Key_F11: //menu
00926     break;
00927   case Key_F12: //home
00928     //          doBlank();
00929     break;
00930   case Key_F13: //mail
00931     //           doUnblank();
00932     break;
00933   case Key_Q: //add to playlist
00934     addSelected();
00935     break;
00936   case Key_R: //remove from playlist
00937     removeSelected();
00938     break;
00939     //       case Key_P: //play
00940     //           odebug << "Play" << oendl;
00941     //           playSelected();
00942     //           break;
00943   case Key_Space:
00944     //          playSelected(); puh
00945     break;
00946   case Key_1:
00947     tabWidget->setCurrentPage( 0 );
00948     break;
00949   case Key_2:
00950     tabWidget->setCurrentPage( 1 );
00951     break;
00952   case Key_3:
00953     tabWidget->setCurrentPage( 2 );
00954     break;
00955   case Key_4:
00956     tabWidget->setCurrentPage( 3 );
00957     break;
00958   case Key_Down:
00959     if ( !d->selectedFiles->next() )
00960       d->selectedFiles->first();
00961     break;
00962   case Key_Up:
00963     if ( !d->selectedFiles->prev() )
00964       //        d->selectedFiles->last();
00965       break;
00966   }
00967 }
00968 
00969 void PlayListWidget::pmViewActivated(int index) {
00970 //  odebug << "" << index << "" << oendl;
00971     switch(index) {
00972     case -16:
00973     {
00974         mediaPlayerState->toggleFullscreen();
00975         bool b=mediaPlayerState->isFullscreen();
00976         pmView->setItemChecked( index, b);
00977         Config cfg( "OpiePlayer" );
00978         cfg.writeEntry( "FullScreen", b );
00979     }
00980     break;
00981     };
00982 }
00983 
00984 void PlayListWidget::populateSkinsMenu() {
00985     int item = 0;
00986     defaultSkinIndex = 0;
00987     QString skinName;
00988     Config cfg( "OpiePlayer" );
00989     cfg.setGroup("Options" );
00990     QString skin = cfg.readEntry( "Skin", "default" );
00991 
00992     QDir skinsDir( QPEApplication::qpeDir() + "pics/opieplayer2/skins" );
00993                 if(!skinsDir.exists()) {
00994                                 QMessageBox::critical( 0,  tr("Opieplayer Error"),
00995                                                                                                                          tr("<p><b>Opieplayer2 skin not found!</b></p><p>Please install an opieplayer2 skin package.</p>") );
00996                                 exit(1) ;
00997                 }               
00998     skinsDir.setFilter( QDir::Dirs );
00999     skinsDir.setSorting(QDir::Name );
01000     const QFileInfoList *skinslist = skinsDir.entryInfoList();
01001     QFileInfoListIterator it( *skinslist );
01002     QFileInfo *fi;
01003     while ( ( fi = it.current() ) ) {
01004         skinName =  fi->fileName();
01005 //        odebug << fi->fileName() << oendl;
01006         if( skinName != "." &&  skinName != ".." && skinName !="CVS" )  {
01007             item = skinsMenu->insertItem( fi->fileName() ) ;
01008         }
01009         if( skinName == "default" ) {
01010             defaultSkinIndex = item;
01011         }
01012         if( skinName == skin ) {
01013             skinsMenu->setItemChecked( item, TRUE );
01014         }
01015         ++it;
01016     }
01017 }
01018 
01019 void PlayListWidget::skinsMenuActivated( int item ) {
01020     for(unsigned int i = defaultSkinIndex; i > defaultSkinIndex - skinsMenu->count(); i-- ) {
01021         skinsMenu->setItemChecked( i, FALSE );
01022     }
01023     skinsMenu->setItemChecked( item, TRUE );
01024 
01025     {
01026         Config cfg( "OpiePlayer" );
01027         cfg.setGroup("Options");
01028         cfg.writeEntry("Skin", skinsMenu->text( item ) );
01029     }
01030 
01031     emit skinSelected();
01032 }
01033 
01034 PlayListWidget::TabType PlayListWidget::currentTab() const
01035 {
01036     static const TabType indexToTabType[ TabTypeCount ] =
01037     { CurrentPlayList, AudioFiles, VideoFiles, PlayLists };
01038 
01039     int index = tabWidget->currentPageIndex();
01040     assert( index < TabTypeCount && index >= 0 );
01041 
01042     return indexToTabType[ index ];
01043 }
01044 
01045 PlayListWidget::Entry PlayListWidget::currentEntry() const
01046 {
01047     if ( currentTab() == CurrentPlayList ) {
01048         const DocLnk *lnk = current();
01049         return Entry( lnk->name(), lnk->file() );
01050     }
01051 
01052     return Entry( currentFileListPathName() );
01053 }
01054 
01055 QString PlayListWidget::currentFileListPathName() const {
01056     return currentFileListView->currentItem()->text( 3 );
01057 }
01058 
01059 
01060 void PlayListWidget::qcopReceive(const QCString &msg, const QByteArray &data) {
01061    QDataStream stream ( data, IO_ReadOnly );
01062    if ( msg == "play()" ) { //plays current selection
01063       btnPlay( true);
01064    } else if ( msg == "stop()" ) {
01065       mediaPlayerState->setPlaying( false);
01066    } else if ( msg == "togglePause()" ) {
01067       mediaPlayerState->togglePaused();
01068    } else if ( msg == "next()" ) { //select next in list
01069       mediaPlayerState->setNext();
01070    } else if ( msg == "prev()" ) { //select previous in list
01071       mediaPlayerState->setPrev();
01072    } else if ( msg == "toggleLooping()" ) { //loop or not loop
01073       mediaPlayerState->toggleLooping();
01074    } else if ( msg == "toggleShuffled()" ) { //shuffled or not shuffled
01075       mediaPlayerState->toggleShuffled();
01076    } else if ( msg == "volUp()" ) { //volume more
01077 //       emit moreClicked();
01078 //       emit moreReleased();
01079    } else if ( msg == "volDown()" ) { //volume less
01080 //       emit lessClicked();
01081 //       emit lessReleased();
01082    } else if ( msg == "play(QString)" ) { //play this now
01083       QString file;
01084       stream >> file;
01085       setDocument( (const QString &) file);
01086    } else if ( msg == "add(QString)" ) { //add to playlist
01087       QString file;
01088       stream >> file;
01089       QFileInfo fileInfo(file);
01090       DocLnk lnk;
01091       lnk.setName( fileInfo.baseName() ); //sets name
01092       lnk.setFile( file ); //sets file name
01093       addToSelection( lnk );
01094    } else if ( msg == "rem(QString)" ) { //remove from playlist
01095       QString file;
01096       stream >> file;
01097 
01098    }
01099 
01100 }
01101 
01102 DocLnk PlayListWidget::addFileToPlaylist( const QString& file,
01103                                         const QString& name ) {
01104     DocLnk lnk;
01105     lnk.setName( name ); //sets name
01106     lnk.setFile( file ); //sets file name
01107     d->selectedFiles->addToSelection(  lnk );
01108     writeCurrentM3u();
01109 
01110     return lnk;
01111 }

Generated on Sat Nov 5 16:15:43 2005 for OPIE by  doxygen 1.4.2