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 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 // code added by L. J. Potter Sat 03-02-2002 06:17:54
00021 #define QTOPIA_INTERNAL_FSLP
00022 
00023 #include "playlistselection.h"
00024 #include "playlistwidget.h"
00025 #include "mediaplayerstate.h"
00026 #include "inputDialog.h"
00027 #include "audiowidget.h"
00028 #include "videowidget.h"
00029 #include "rssparser.h"
00030 
00031 #include <qpe/process.h>
00032 
00033 #include <qvector.h>
00034 #include <qxml.h>
00035 /* OPIE */
00036 #include <qpe/qpeapplication.h>
00037 #include <qpe/qpemenubar.h>
00038 #include <qpe/lnkproperties.h>
00039 #include <opie2/odebug.h>
00040 #include <opie2/oresource.h>
00041 
00042 /* QT */
00043 #include <qtoolbar.h>
00044 #include <qaction.h>
00045 #include <qlayout.h>
00046 #include <qmessagebox.h>
00047 //#include <qtimer.h>
00048 
00049 /* STD */
00050 #include <stdlib.h>
00051 
00052 #include <unistd.h>
00053 #include <sys/file.h>
00054 #include <sys/ioctl.h>
00055 #include <sys/soundcard.h>
00056 
00057 // for setBacklight()
00058 #include <linux/fb.h>
00059 #include <sys/types.h>
00060 #include <sys/stat.h>
00061 #include <stdlib.h>
00062 
00063 #define BUTTONS_ON_TOOLBAR
00064 #define SIDE_BUTTONS
00065 #define CAN_SAVE_LOAD_PLAYLISTS
00066 
00067 extern AudioWidget *audioUI;
00068 extern VideoWidget *videoUI;
00069 extern MediaPlayerState *mediaPlayerState;
00070 
00071 static inline QString fullBaseName ( const QFileInfo &fi )
00072 {
00073   QString str = fi. fileName ( );
00074   return str. left ( str. findRev ( '.' ));
00075 }
00076 
00077 
00078 QString audioMimes ="audio/mpeg;audio/x-wav;audio/x-ogg;audio/x-mod";
00079 // class myFileSelector {
00080 
00081 // };
00082 class PlayListWidgetPrivate {
00083 public:
00084     QToolButton *tbPlay, *tbFull, *tbLoop, *tbScale, *tbShuffle, *tbAddToList,  *tbRemoveFromList, *tbMoveUp, *tbMoveDown, *tbRemove;
00085     QFrame *playListFrame;
00086     FileSelector *files;
00087     PlayListSelection *selectedFiles;
00088     bool setDocumentUsed;
00089     DocLnk *current;
00090 };
00091 
00092 
00093 class ToolButton : public QToolButton {
00094 public:
00095     ToolButton( QWidget *parent, const char *name, const QString& icon, QObject *handler, const QString& slot, bool t = FALSE )
00096             : QToolButton( parent, name ) {
00097         setTextLabel( name );
00098         setPixmap( Opie::Core::OResource::loadPixmap( icon, Opie::Core::OResource::SmallIcon ) );
00099         setAutoRaise( TRUE );
00100         setUsesBigPixmap( qApp->desktop()->size().width() > 330 );
00101         setFocusPolicy( QWidget::NoFocus );
00102         setToggleButton( t );
00103         connect( this, t ? SIGNAL( toggled(bool) ) : SIGNAL( clicked() ), handler, slot );
00104         QPEMenuToolFocusManager::manager()->addWidget( this );
00105     }
00106 };
00107 
00108 
00109 class MenuItem : public QAction {
00110 public:
00111     MenuItem( QWidget *parent, const QString& text, QObject *handler, const QString& slot )
00112             : QAction( text, QString::null, 0, 0 ) {
00113         connect( this, SIGNAL( activated() ), handler, slot );
00114         addTo( parent );
00115     }
00116 };
00117 
00118 
00119 PlayListWidget::PlayListWidget( QWidget* parent, const char* name, WFlags fl )
00120         : QMainWindow( parent, name, fl ) {
00121 
00122     d = new PlayListWidgetPrivate;
00123     d->setDocumentUsed = FALSE;
00124     d->current = NULL;
00125     fromSetDocument = FALSE;
00126     insanityBool=FALSE;
00127     audioScan = FALSE;
00128     videoScan = FALSE;
00129 //    menuTimer = new QTimer( this ,"menu timer"),
00130 //     connect( menuTimer, SIGNAL( timeout() ), SLOT( addSelected() ) );
00131     channel = new QCopChannel( "QPE/Application/opieplayer", this );
00132     connect( channel, SIGNAL(received(const QCString&,const QByteArray&)),
00133         this, SLOT( qcopReceive(const QCString&,const QByteArray&)) );
00134 
00135     setBackgroundMode( PaletteButton );
00136 
00137     setCaption( tr("OpiePlayer") );
00138     setIcon( Opie::Core::OResource::loadPixmap( "opieplayer/MPEGPlayer", Opie::Core::OResource::SmallIcon ) );
00139 
00140     setToolBarsMovable( FALSE );
00141 
00142       // Create Toolbar
00143     QToolBar *toolbar = new QToolBar( this );
00144     toolbar->setHorizontalStretchable( TRUE );
00145 
00146       // Create Menubar
00147     QMenuBar *menu = new QMenuBar( toolbar );
00148     menu->setMargin( 0 );
00149 
00150     QToolBar *bar = new QToolBar( this );
00151     bar->setLabel( tr( "Play Operations" ) );
00152 //      d->tbPlayCurList =  new ToolButton( bar, tr( "play List" ), "opieplayer/play_current_list",
00153 //                                        this , SLOT( addSelected()) );
00154     tbDeletePlaylist = new QPushButton( Opie::Core::OResource::loadPixmap("trash", Opie::Core::OResource::SmallIcon),"",bar,"close");
00155     tbDeletePlaylist->setFlat(TRUE);
00156 
00157     tbDeletePlaylist->setFixedSize(20,20);
00158 
00159     d->tbAddToList =  new ToolButton( bar, tr( "Add to Playlist" ), "opieplayer/add_to_playlist",
00160                                       this , SLOT(addSelected()) );
00161     d->tbRemoveFromList = new ToolButton( bar, tr( "Remove from Playlist" ), "opieplayer/remove_from_playlist",
00162                                           this , SLOT(removeSelected()) );
00163 //    d->tbPlay    = new ToolButton( bar, tr( "Play" ), "opieplayer/play", /*this */mediaPlayerState , SLOT(setPlaying(bool)/*btnPlay()*/), TRUE );
00164     d->tbPlay    = new ToolButton( bar, tr( "Play" ), "opieplayer/play",
00165                                    this , SLOT( btnPlay(bool) ), TRUE );
00166     d->tbShuffle = new ToolButton( bar, tr( "Randomize" ),"opieplayer/shuffle",
00167                                    mediaPlayerState, SLOT(setShuffled(bool)), TRUE );
00168     d->tbLoop    = new ToolButton( bar, tr( "Loop" ),"opieplayer/loop",
00169                                    mediaPlayerState, SLOT(setLooping(bool)), TRUE );
00170     tbDeletePlaylist->hide();
00171 
00172     QPopupMenu *pmPlayList = new QPopupMenu( this );
00173     menu->insertItem( tr( "File" ), pmPlayList );
00174     new MenuItem( pmPlayList, tr( "Clear List" ), this, SLOT( clearList() ) );
00175     new MenuItem( pmPlayList, tr( "Add all audio files" ), this, SLOT( addAllMusicToList() ) );
00176     new MenuItem( pmPlayList, tr( "Add all video files" ), this, SLOT( addAllVideoToList() ) );
00177     new MenuItem( pmPlayList, tr( "Add all files" ), this, SLOT( addAllToList() ) );
00178     pmPlayList->insertSeparator(-1);
00179     new MenuItem( pmPlayList, tr( "Save PlayList" ), this, SLOT( saveList() ) );
00180     new MenuItem( pmPlayList, tr( "Open File or URL" ), this,SLOT( openFile() ) );
00181     pmPlayList->insertSeparator(-1);
00182     new MenuItem( pmPlayList, tr( "Rescan for Audio Files" ), this,SLOT( scanForAudio() ) );
00183     new MenuItem( pmPlayList, tr( "Rescan for Video Files" ), this,SLOT( scanForVideo() ) );
00184 
00185     QPopupMenu *pmView = new QPopupMenu( this );
00186     menu->insertItem( tr( "View" ), pmView );
00187 
00188     fullScreenButton = new QAction(tr("Full Screen"), Opie::Core::OResource::loadPixmap("fullscreen", Opie::Core::OResource::SmallIcon),
00189                                    QString::null, 0, this, 0);
00190     fullScreenButton->addTo(pmView);
00191     scaleButton = new QAction(tr("Scale"), Opie::Core::OResource::loadPixmap("opieplayer/scale", Opie::Core::OResource::SmallIcon),
00192                               QString::null, 0, this, 0);
00193     scaleButton->addTo(pmView);
00194 
00195 
00196     skinsMenu = new QPopupMenu( this );
00197     menu->insertItem( tr( "Skins" ), skinsMenu );
00198     skinsMenu->isCheckable();
00199     connect( skinsMenu, SIGNAL( activated(int) ) ,
00200              this, SLOT( skinsMenuActivated(int) ) );
00201     populateSkinsMenu();
00202 
00203     QVBox *vbox5 = new QVBox( this ); vbox5->setBackgroundMode( PaletteButton );
00204     QVBox *vbox4 = new QVBox( vbox5 ); vbox4->setBackgroundMode( PaletteButton );
00205 
00206     QHBox *hbox6 = new QHBox( vbox4 ); hbox6->setBackgroundMode( PaletteButton );
00207 
00208     tabWidget = new QTabWidget( hbox6, "tabWidget" );
00209 //    tabWidget->setTabShape(QTabWidget::Triangular);
00210 
00211     QWidget *pTab;
00212     pTab = new QWidget( tabWidget, "pTab" );
00213 //      playlistView = new QListView( pTab, "playlistview" );
00214 //    playlistView->setMinimumSize(236,260);
00215     tabWidget->insertTab( pTab,"Playlist");
00216 
00217 
00218     // Add the playlist area
00219 
00220     QVBox *vbox3 = new QVBox( pTab ); vbox3->setBackgroundMode( PaletteButton );
00221     d->playListFrame = vbox3;
00222 
00223     QGridLayout *layoutF = new QGridLayout( pTab );
00224     layoutF->setSpacing( 2);
00225     layoutF->setMargin( 2);
00226     layoutF->addMultiCellWidget( d->playListFrame , 0, 0, 0, 1 );
00227 
00228     QHBox *hbox2 = new QHBox( vbox3 ); hbox2->setBackgroundMode( PaletteButton );
00229 
00230     d->selectedFiles = new PlayListSelection( hbox2);
00231     QVBox *vbox1 = new QVBox( hbox2 ); vbox1->setBackgroundMode( PaletteButton );
00232 
00233     QPEApplication::setStylusOperation( d->selectedFiles->viewport(),QPEApplication::RightOnHold);
00234 
00235 
00236 
00237     QVBox *stretch1 = new QVBox( vbox1 ); stretch1->setBackgroundMode( PaletteButton ); // add stretch
00238     new ToolButton( vbox1, tr( "Move Up" ),   "opieplayer/up",   d->selectedFiles, SLOT(moveSelectedUp()) );
00239     new ToolButton( vbox1, tr( "Remove" ),    "opieplayer/cut",  d->selectedFiles, SLOT(removeSelected()) );
00240     new ToolButton( vbox1, tr( "Move Down" ), "opieplayer/down", d->selectedFiles, SLOT(moveSelectedDown()) );
00241     QVBox *stretch2 = new QVBox( vbox1 ); stretch2->setBackgroundMode( PaletteButton ); // add stretch
00242 
00243     QWidget *aTab;
00244     aTab = new QWidget( tabWidget, "aTab" );
00245     audioView = new QListView( aTab, "Audioview" );
00246 
00247     QGridLayout *layoutA = new QGridLayout( aTab );
00248     layoutA->setSpacing( 2);
00249     layoutA->setMargin( 2);
00250     layoutA->addMultiCellWidget( audioView, 0, 0, 0, 1 );
00251 
00252     audioView->addColumn( tr("Title"),-1);
00253     audioView->addColumn(tr("Size"), -1);
00254     audioView->addColumn(tr("Media"),-1);
00255     audioView->addColumn( tr( "Path" ), -1 );
00256 
00257     audioView->setColumnAlignment(1, Qt::AlignRight);
00258     audioView->setColumnAlignment(2, Qt::AlignRight);
00259     audioView->setAllColumnsShowFocus(TRUE);
00260 
00261     audioView->setMultiSelection( TRUE );
00262     audioView->setSelectionMode( QListView::Extended);
00263     audioView->setSorting( 3, TRUE );
00264 
00265     tabWidget->insertTab(aTab,tr("Audio"));
00266 
00267     QPEApplication::setStylusOperation( audioView->viewport(),QPEApplication::RightOnHold);
00268 
00269 //    audioView
00270 //     populateAudioView();
00271 // videowidget
00272 
00273     QWidget *vTab;
00274     vTab = new QWidget( tabWidget, "vTab" );
00275     videoView = new QListView( vTab, "Videoview" );
00276 
00277     QGridLayout *layoutV = new QGridLayout( vTab );
00278     layoutV->setSpacing( 2);
00279     layoutV->setMargin( 2);
00280     layoutV->addMultiCellWidget( videoView, 0, 0, 0, 1 );
00281 
00282     videoView->addColumn(tr("Title"),-1);
00283     videoView->addColumn(tr("Size"),-1);
00284     videoView->addColumn(tr("Media"),-1);
00285     videoView->addColumn(tr( "Path" ), -1 );
00286     videoView->setColumnAlignment(1, Qt::AlignRight);
00287     videoView->setColumnAlignment(2, Qt::AlignRight);
00288     videoView->setAllColumnsShowFocus(TRUE);
00289     videoView->setMultiSelection( TRUE );
00290     videoView->setSelectionMode( QListView::Extended);
00291 
00292     QPEApplication::setStylusOperation( videoView->viewport(),QPEApplication::RightOnHold);
00293 
00294     tabWidget->insertTab( vTab,tr("Video"));
00295 
00296     QWidget *LTab;
00297     LTab = new QWidget( tabWidget, "LTab" );
00298     playLists = new FileSelector( "playlist/plain;audio/x-mpegurl", LTab, "fileselector" , FALSE, FALSE);
00299 
00300     QGridLayout *layoutL = new QGridLayout( LTab );
00301     layoutL->setSpacing( 2);
00302     layoutL->setMargin( 2);
00303     layoutL->addMultiCellWidget( playLists, 0, 0, 0, 1 );
00304 
00305     tabWidget->insertTab(LTab,tr("Lists"));
00306 
00307     connect(tbDeletePlaylist,(SIGNAL(released())),SLOT( deletePlaylist()));
00308     connect( fullScreenButton, SIGNAL(activated()), mediaPlayerState, SLOT(toggleFullscreen()) );
00309     connect( scaleButton, SIGNAL(activated()), mediaPlayerState, SLOT(toggleScaled()) );
00310 
00311     connect( d->selectedFiles, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int)),
00312              this,SLOT( playlistViewPressed(int,QListViewItem*,const QPoint&,int)) );
00313 
00314 
00316     connect( audioView, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int)),
00317              this,SLOT( viewPressed(int,QListViewItem*,const QPoint&,int)) );
00318 
00319     connect( audioView, SIGNAL( returnPressed(QListViewItem*)),
00320              this,SLOT( playIt(QListViewItem*)) );
00321     connect( audioView, SIGNAL( doubleClicked(QListViewItem*) ), this, SLOT( addToSelection(QListViewItem*) ) );
00322 
00323 
00324 //videoView
00325     connect( videoView, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int)),
00326              this,SLOT( viewPressed(int,QListViewItem*,const QPoint&,int)) );
00327     connect( videoView, SIGNAL( returnPressed(QListViewItem*)),
00328              this,SLOT( playIt(QListViewItem*)) );
00329     connect( videoView, SIGNAL( doubleClicked(QListViewItem*) ), this, SLOT( addToSelection(QListViewItem*) ) );
00330 
00331 //playlists
00332     connect( playLists, SIGNAL( fileSelected(const DocLnk&) ), this, SLOT( loadList(const DocLnk&) ) );
00333 
00334     connect( tabWidget, SIGNAL (currentChanged(QWidget*)),this,SLOT(tabChanged(QWidget*)));
00335 
00336     connect( mediaPlayerState, SIGNAL( playingToggled(bool) ),    d->tbPlay,    SLOT( setOn(bool) ) );
00337 
00338     connect( mediaPlayerState, SIGNAL( loopingToggled(bool) ),    d->tbLoop,    SLOT( setOn(bool) ) );
00339     connect( mediaPlayerState, SIGNAL( shuffledToggled(bool) ),   d->tbShuffle, SLOT( setOn(bool) ) );
00340     connect( mediaPlayerState, SIGNAL( playlistToggled(bool) ),   this,         SLOT( setPlaylist(bool) ) );
00341 
00342     connect( d->selectedFiles, SIGNAL( doubleClicked(QListViewItem*) ), this, SLOT( playIt(QListViewItem*) ) );
00343 
00344     setCentralWidget( vbox5 );
00345 
00346     Config cfg( "OpiePlayer" );
00347     readConfig( cfg );
00348 
00349     currentPlayList = cfg.readEntry("CurrentPlaylist","default");
00350     loadList(DocLnk( currentPlayList));
00351     setCaption(tr("OpiePlayer: ")+ fullBaseName ( QFileInfo(currentPlayList)));
00352 
00353     initializeStates();
00354 }
00355 
00356 
00357 PlayListWidget::~PlayListWidget() {
00358     Config cfg( "OpiePlayer" );
00359     writeConfig( cfg );
00360 
00361       if ( d->current )
00362           delete d->current;
00363       if(d) delete d;
00364 }
00365 
00366 
00367 void PlayListWidget::initializeStates() {
00368 
00369     d->tbPlay->setOn( mediaPlayerState->playing() );
00370     d->tbLoop->setOn( mediaPlayerState->looping() );
00371     d->tbShuffle->setOn( mediaPlayerState->shuffled() );
00372     setPlaylist( true);
00373 }
00374 
00375 
00376 void PlayListWidget::readConfig( Config& cfg ) {
00377     cfg.setGroup("PlayList");
00378     QString currentString = cfg.readEntry("current", "" );
00379     int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
00380     for ( int i = 0; i < noOfFiles; i++ ) {
00381         QString entryName;
00382         entryName.sprintf( "File%i", i + 1 );
00383         QString linkFile = cfg.readEntry( entryName );
00384         DocLnk lnk( linkFile );
00385         if ( lnk.isValid() ) {
00386             d->selectedFiles->addToSelection( lnk );
00387         }
00388     }
00389     d->selectedFiles->setSelectedItem( currentString);
00390 }
00391 
00392 
00393 void PlayListWidget::writeConfig( Config& cfg ) const {
00394 
00395     d->selectedFiles->writeCurrent( cfg);
00396     cfg.setGroup("PlayList");
00397     int noOfFiles = 0;
00398     d->selectedFiles->first();
00399     do {
00400         const DocLnk *lnk = d->selectedFiles->current();
00401         if ( lnk ) {
00402             QString entryName;
00403             entryName.sprintf( "File%i", noOfFiles + 1 );
00404 //            odebug << entryName << oendl;
00405             cfg.writeEntry( entryName, lnk->linkFile() );
00406               // if this link does exist, add it so we have the file
00407               // next time...
00408             if ( !QFile::exists( lnk->linkFile() ) ) {
00409                   // the way writing lnks doesn't really check for out
00410                   // of disk space, but check it anyway.
00411 //                  if ( !lnk->writeLink() ) {
00412 //                      QMessageBox::critical( 0, tr("Out of space"),
00413 //                                             tr( "There was a problem saving "
00414 //                                                 "the playlist.\n"
00415 //                                                 "Your playlist "
00416 //                                                 "may be missing some entries\n"
00417 //                                                 "the next time you start it." )
00418 //                                             );
00419 //                  }
00420 
00421             }
00422             noOfFiles++;
00423         }
00424     }
00425     while ( d->selectedFiles->next() );
00426     cfg.writeEntry("NumberOfFiles", noOfFiles );
00427 }
00428 
00429 
00430 void PlayListWidget::addToSelection( const DocLnk& lnk ) {
00431         d->setDocumentUsed = false;
00432         if ( mediaPlayerState->playlist() ) {
00433             if(QFileInfo(lnk.file()).exists() || lnk.file().left(4) == "http" )
00434                 d->selectedFiles->addToSelection( lnk );
00435         }
00436         else
00437             mediaPlayerState->setPlaying( true);
00438 
00439 }
00440 
00441 
00442 void PlayListWidget::clearList() {
00443    while ( first() )
00444       d->selectedFiles->removeSelected();
00445    Config cfg( "OpiePlayer" );
00446    cfg.setGroup("PlayList");
00447    cfg.writeEntry("CurrentPlaylist","");
00448    currentPlayList="";
00449 
00450 }
00451 
00452 
00453 void PlayListWidget::addAllToList() {
00454     DocLnkSet filesAll;
00455     Global::findDocuments(&filesAll, "video/*;audio/*");
00456     QListIterator<DocLnk> Adit( filesAll.children() );
00457     for ( ; Adit.current(); ++Adit )
00458         if(QFileInfo(Adit.current()->file()).exists())
00459             d->selectedFiles->addToSelection( **Adit );
00460     tabWidget->setCurrentPage(0);
00461 
00462     writeCurrentM3u();
00463     d->selectedFiles->first();
00464 }
00465 
00466 
00467 void PlayListWidget::addAllMusicToList() {
00468     QListIterator<DocLnk> dit( files.children() );
00469     for ( ; dit.current(); ++dit )
00470         if(QFileInfo(dit.current()->file()).exists())
00471             d->selectedFiles->addToSelection( **dit );
00472     tabWidget->setCurrentPage(0);
00473 
00474     writeCurrentM3u();
00475     d->selectedFiles->first();
00476 }
00477 
00478 
00479 void PlayListWidget::addAllVideoToList() {
00480     QListIterator<DocLnk> dit( vFiles.children() );
00481     for ( ; dit.current(); ++dit )
00482         if(QFileInfo( dit.current()->file()).exists())
00483             d->selectedFiles->addToSelection( **dit );
00484     tabWidget->setCurrentPage(0);
00485 
00486     writeCurrentM3u();
00487     d->selectedFiles->first();
00488 }
00489 
00490 
00491 void PlayListWidget::setDocument(const QString& fileref) {
00492    fromSetDocument = true;
00493    d->setDocumentUsed = TRUE;
00494    setDocumentEx(fileref);
00495 }
00496 
00497 void PlayListWidget::setDocumentEx(const QString& fileref) {
00498    owarn << "opieplayer receive "+fileref << oendl;
00499    clearList();
00500    DocLnk lnk;
00501    QFileInfo fileInfo(fileref);
00502    if ( !fileInfo.exists() ) {
00503       QMessageBox::critical( 0, tr( "Invalid File" ),
00504                              tr( "There was a problem in getting the file." ) );
00505       return;
00506    }
00507    //   odebug << "<<<<<<<<<<<<<<<<<<<<<<setDocument "+fileref << oendl;
00508    QString extension = fileInfo.extension(false);
00509      if( extension.find( "m3u", 0, false) != -1) { //is m3u
00510         readm3u( fileref);
00511      }
00512   else if( extension.find( "pls", 0, false) != -1 ) { //is pls
00513       readPls( fileref);
00514    }
00515    else if( fileref.find("playlist",0,TRUE) != -1) {//is playlist
00516       clearList();
00517       lnk.setName( fileInfo.baseName() ); //sets name
00518       lnk.setFile( fileref ); //sets file name
00519       lnk.setIcon("Sound");
00520       //addToSelection( lnk );
00521 
00522       loadList( lnk);
00523       d->selectedFiles->first();
00524    } else {
00525       if( fileref.find(".desktop",0,TRUE) != -1) {
00526          lnk = DocLnk(fileref);
00527       } else {
00528          lnk.setName( fileInfo.baseName() ); //sets name
00529          lnk.setFile( fileref ); //sets file name
00530          lnk.setIcon("Sound");
00531       }
00532 
00533       addToSelection( lnk );
00534       //        addToSelection( DocLnk( fileref ) );
00535       lnk.removeLinkFile();
00536       //        qApp->processEvents();
00537    }
00538       setCaption(tr("OpiePlayer"));
00539       d->setDocumentUsed = TRUE;
00540      d->selectedFiles->setSelected(d->selectedFiles->firstChild(),true );
00541       mediaPlayerState->setPlaying( FALSE );
00542       qApp->processEvents();
00543       mediaPlayerState->setPlaying( TRUE );
00544 
00545 }
00546 
00547 
00548 void PlayListWidget::setActiveWindow() {
00549    //        odebug << "SETTING active window" << oendl;
00550       // When we get raised we need to ensure that it switches views
00551     char origView = mediaPlayerState->view();
00552     mediaPlayerState->setView( 'l' ); // invalidate
00553     mediaPlayerState->setView( origView ); // now switch back
00554 }
00555 
00556 
00557 void PlayListWidget::useSelectedDocument() {
00558     d->setDocumentUsed = FALSE;
00559 }
00560 
00561 
00562 const DocLnk *PlayListWidget::current() { // this is fugly
00563    switch (tabWidget->currentPageIndex()) {
00564    case 0: //playlist
00565    {
00566       //          odebug << "playlist" << oendl;
00567       if ( mediaPlayerState->playlist() ) {
00568          return d->selectedFiles->current();
00569       }
00570       else if ( d->setDocumentUsed && d->current ) {
00571          return d->current;
00572       } else {
00573          return &(d->files->selectedDocument());
00574       }
00575    }
00576    break;
00577    case 1://audio
00578    {
00579       //          odebug << "audioView" << oendl;
00580       QListIterator<DocLnk> dit( files.children() );
00581       for ( ; dit.current(); ++dit ) {
00582          if( dit.current()->name() == audioView->currentItem()->text(0) && !insanityBool) {
00583             insanityBool=TRUE;
00584             return dit;
00585          }
00586       }
00587    }
00588    break;
00589    case 2: // video
00590    {
00591       QListIterator<DocLnk> Vdit( vFiles.children() );
00592       for ( ; Vdit.current(); ++Vdit ) {
00593          if( Vdit.current()->name() == videoView->currentItem()->text(0) && !insanityBool) {
00594             insanityBool=TRUE;
00595             return Vdit;
00596          }
00597       }
00598    }
00599    break;
00600    };
00601    return 0;
00602 }
00603 
00604 bool PlayListWidget::prev() {
00605    if ( mediaPlayerState->playlist() ) {
00606       if ( mediaPlayerState->shuffled() ) {
00607          const DocLnk *cur = current();
00608          int j = 1 + (int)(97.0 * rand() / (RAND_MAX + 1.0));
00609          for ( int i = 0; i < j; i++ ) {
00610             if ( !d->selectedFiles->next() )
00611                d->selectedFiles->first();
00612          }
00613          if ( cur == current() )
00614             if ( !d->selectedFiles->next() )
00615                d->selectedFiles->first();
00616          return TRUE;
00617       } else {
00618          if ( !d->selectedFiles->prev() ) {
00619             if ( mediaPlayerState->looping() ) {
00620                return d->selectedFiles->last();
00621             } else {
00622                return FALSE;
00623             }
00624          }
00625          return TRUE;
00626       }
00627    } else {
00628       return mediaPlayerState->looping();
00629    }
00630 }
00631 
00632 
00633 bool PlayListWidget::next() {
00634    if ( mediaPlayerState->playlist() ) {
00635       if ( mediaPlayerState->shuffled() ) {
00636          return prev();
00637       } else {
00638          if ( !d->selectedFiles->next() ) {
00639             if ( mediaPlayerState->looping() ) {
00640                return d->selectedFiles->first();
00641             } else {
00642                return FALSE;
00643             }
00644          }
00645          return TRUE;
00646       }
00647    } else {
00648       return mediaPlayerState->looping();
00649    }
00650 }
00651 
00652 
00653 bool PlayListWidget::first() {
00654    if ( mediaPlayerState->playlist() )
00655       return d->selectedFiles->first();
00656    else
00657       return mediaPlayerState->looping();
00658 }
00659 
00660 
00661 bool PlayListWidget::last() {
00662    if ( mediaPlayerState->playlist() )
00663       return d->selectedFiles->last();
00664    else
00665       return mediaPlayerState->looping();
00666 }
00667 
00668 
00669 void PlayListWidget::saveList() {
00670    writem3u();
00671 }
00672 
00673 void PlayListWidget::loadList( const DocLnk & lnk) {
00674    QString name = lnk.name();
00675    //  odebug << "<<<<<<<<<<<<<<<<<<<<<<<<currentList is "+name << oendl;
00676 
00677    if( name.length()>0) {
00678       setCaption("OpiePlayer: "+name);
00679       //       odebug << "<<<<<<<<<<<<load list "+ lnk.file() << oendl;
00680       clearList();
00681       readm3u(lnk.file());
00682       tabWidget->setCurrentPage(0);
00683    }
00684 }
00685 
00686 void PlayListWidget::setPlaylist( bool shown ) {
00687    if ( shown )
00688       d->playListFrame->show();
00689    else
00690       d->playListFrame->hide();
00691 }
00692 
00693 void PlayListWidget::setView( char view ) {
00694    if ( view == 'l' )
00695       QPEApplication::showWidget( this );
00696    else
00697       hide();
00698 }
00699 
00700 void PlayListWidget::addSelected() {
00701    DocLnk lnk;
00702    QString filename;
00703    switch (tabWidget->currentPageIndex()) {
00704 
00705    case 0: //playlist
00706       return;
00707       break;
00708    case 1: { //audio
00709       QListViewItemIterator it( audioView );
00710       for ( ; it.current(); ++it ) {
00711          if ( it.current()->isSelected() ) {
00712             filename = it.current()->text(3);
00713             lnk.setName(  QFileInfo(filename).baseName() ); //sets name
00714             lnk.setFile( filename ); //sets file name
00715             d->selectedFiles->addToSelection(  lnk);
00716          }
00717       }
00718       audioView->clearSelection();
00719       //    d->selectedFiles->next();
00720    }
00721       break;
00722 
00723    case 2: { // video
00724       QListViewItemIterator it( videoView );
00725       for ( ; it.current(); ++it ) {
00726          if ( it.current()->isSelected() ) {
00727 
00728             filename = it.current()->text(3);
00729             lnk.setName(  QFileInfo(filename).baseName() ); //sets name
00730             lnk.setFile( filename ); //sets file name
00731             d->selectedFiles->addToSelection(  lnk);
00732          }
00733       }
00734       videoView->clearSelection();
00735    }
00736       break;
00737    };
00738    //  tabWidget->setCurrentPage(0);
00739    writeCurrentM3u();
00740 
00741 }
00742 
00743 void PlayListWidget::removeSelected() {
00744     d->selectedFiles->removeSelected( );
00745 }
00746 
00747 void PlayListWidget::playIt( QListViewItem *) {
00748    //   d->setDocumentUsed = FALSE;
00749    //  mediaPlayerState->curPosition =0;
00750    //    mediaPlayerState->setPlaying(FALSE);
00751    mediaPlayerState->setPlaying(TRUE);
00752    d->selectedFiles->unSelect();
00753 }
00754 
00755 void PlayListWidget::addToSelection( QListViewItem *it) {
00756    d->setDocumentUsed = FALSE;
00757 
00758    if(it) {
00759       switch ( tabWidget->currentPageIndex()) {
00760       case 0: //playlist
00761          return;
00762          break;
00763       };
00764       //      case 1: {
00765       DocLnk lnk;
00766       QString filename;
00767 
00768       filename=it->text(3);
00769       lnk.setName( fullBaseName ( QFileInfo(filename)) ); //sets name
00770       lnk.setFile( filename ); //sets file name
00771       d->selectedFiles->addToSelection(  lnk);
00772 
00773       if(tabWidget->currentPageIndex() == 0)
00774          writeCurrentM3u();
00775       //    tabWidget->setCurrentPage(0);
00776 
00777    }
00778 }
00779 
00780 void PlayListWidget::tabChanged(QWidget *) {
00781 
00782    switch ( tabWidget->currentPageIndex()) {
00783    case 0:
00784    {
00785       if( !tbDeletePlaylist->isHidden())
00786          tbDeletePlaylist->hide();
00787       d->tbRemoveFromList->setEnabled(TRUE);
00788       d->tbAddToList->setEnabled(FALSE);
00789    }
00790    break;
00791    case 1:
00792    {
00793       audioView->clear();
00794       populateAudioView();
00795 
00796       if( !tbDeletePlaylist->isHidden())
00797          tbDeletePlaylist->hide();
00798       d->tbRemoveFromList->setEnabled(FALSE);
00799       d->tbAddToList->setEnabled(TRUE);
00800    }
00801    break;
00802    case 2:
00803    {
00804       videoView->clear();
00805       populateVideoView();
00806       if( !tbDeletePlaylist->isHidden())
00807          tbDeletePlaylist->hide();
00808       d->tbRemoveFromList->setEnabled(FALSE);
00809       d->tbAddToList->setEnabled(TRUE);
00810    }
00811    break;
00812    case 3:
00813    {
00814       if( tbDeletePlaylist->isHidden())
00815          tbDeletePlaylist->show();
00816       playLists->reread();
00817    }
00818    break;
00819    };
00820 }
00821 
00822 void PlayListWidget::btnPlay(bool b) {
00823    //    mediaPlayerState->setPlaying(b);
00824    switch ( tabWidget->currentPageIndex()) {
00825    case 0:
00826    {
00827       //            if( d->selectedFiles->current()->file().find(" ",0,TRUE) != -1
00828       //                if( d->selectedFiles->current()->file().find("%20",0,TRUE) != -1) {
00829       //                 QMessageBox::message("Note","You are trying to play\na malformed url.");
00830       //            } else {
00831       mediaPlayerState->setPlaying(b);
00832       insanityBool=FALSE;
00833       odebug << "insanity" << oendl;
00834       //          }
00835    }
00836    break;
00837    case 1:
00838    {
00839       //          d->selectedFiles->unSelect();
00840       addToSelection( audioView->currentItem() );
00841       mediaPlayerState->setPlaying( b);
00842       d->selectedFiles->removeSelected( );
00843       d->selectedFiles->unSelect();
00844       tabWidget->setCurrentPage(1);
00845       insanityBool=FALSE;
00846    }//          audioView->clearSelection();
00847    break;
00848    case 2:
00849    {
00850 
00851       addToSelection( videoView->currentItem() );
00852       mediaPlayerState->setPlaying( b);
00853       //          qApp->processEvents();
00854       d->selectedFiles->removeSelected( );
00855       d->selectedFiles->unSelect();
00856       tabWidget->setCurrentPage(2);
00857       insanityBool=FALSE;
00858    }//          videoView->clearSelection();
00859    break;
00860    };
00861 
00862 }
00863 
00864 void PlayListWidget::deletePlaylist() {
00865    switch( QMessageBox::information( this, (tr("Remove Playlist?")),
00866                                      (tr("You really want to delete\nthis playlist?")),
00867                                      (tr("Yes")), (tr("No")), 0 )){
00868    case 0: // Yes clicked,
00869       QFile().remove(playLists->selectedDocument().file());
00870       QFile().remove(playLists->selectedDocument().linkFile());
00871       playLists->reread();
00872       break;
00873    case 1: // Cancel
00874       break;
00875    };
00876 }
00877 
00878 void PlayListWidget::viewPressed( int mouse, QListViewItem *, const QPoint&, int )
00879 {
00880    switch (mouse) {
00881    case 1:
00882       break;
00883    case 2:{
00884 
00885       QPopupMenu  m;
00886       m.insertItem( tr( "Play" ), this, SLOT( playSelected() ));
00887       m.insertItem( tr( "Add to Playlist" ), this, SLOT( addSelected() ));
00888       m.insertSeparator();
00889       if( QFile(QPEApplication::qpeDir()+"lib/libopie.so").exists() )
00890          m.insertItem( tr( "Properties" ), this, SLOT( listDelete() ));
00891 
00892       m.exec( QCursor::pos() );
00893    }
00894       break;
00895    };
00896 }
00897 
00898 void PlayListWidget::playSelected()
00899 {
00900     btnPlay( true);
00901 //    d->selectedFiles->unSelect();
00902 }
00903 
00904 void PlayListWidget::playlistViewPressed( int mouse, QListViewItem *, const QPoint&, int)
00905 {
00906    switch (mouse) {
00907    case 1:
00908 
00909       break;
00910    case 2:{
00911       QPopupMenu  m;
00912       m.insertItem( tr( "Play Selected" ), this, SLOT( playSelected() ));
00913       m.insertItem( tr( "Remove" ), this, SLOT( removeSelected() ));
00914       //       m.insertSeparator();
00915       //     m.insertItem( tr( "Properties" ), this, SLOT( listDelete() ));
00916       m.exec( QCursor::pos() );
00917    }
00918       break;
00919    };
00920 
00921 }
00922 
00923 void PlayListWidget::listDelete() {
00924    Config cfg( "OpiePlayer" );
00925    cfg.setGroup("PlayList");
00926    currentPlayList = cfg.readEntry("CurrentPlaylist","");
00927    QString file;
00928    //    int noOfFiles = cfg.readNumEntry("NumberOfFiles", 0 );
00929    switch ( tabWidget->currentPageIndex()) {
00930    case 0:
00931       break;
00932    case 1:
00933    {
00934       file = audioView->currentItem()->text(0);
00935       QListIterator<DocLnk> Pdit( files.children() );
00936       for ( ; Pdit.current(); ++Pdit ) {
00937          if( Pdit.current()->name() == file) {
00938             LnkProperties prop( Pdit.current() );
00939             QPEApplication::execDialog( &prop );
00940          }
00941       }
00942       populateAudioView();
00943    }
00944    break;
00945    case 2:
00946    {
00947       //           file = videoView->selectedItem()->text(0);
00948       //           for ( int i = 0; i < noOfFiles; i++ ) {
00949       //               QString entryName;
00950       //               entryName.sprintf( "File%i", i + 1 );
00951       //               QString linkFile = cfg.readEntry( entryName );
00952       //                   AppLnk lnk( AppLnk(linkFile));
00953       //               if( lnk.name() == file ) {
00954       //                   LnkProperties prop( &lnk);
00955       // //  connect(&prop, SIGNAL(select(const AppLnk*)), this, SLOT(externalSelected(const AppLnk*)));
00956       //                   prop.showMaximized();
00957       //                   prop.exec();
00958       //               }
00959       //            }
00960    }
00961    break;
00962    };
00963 }
00964 
00965 void PlayListWidget::scanForAudio() {
00966    //    odebug << "scan for audio" << oendl;
00967    files.detachChildren();
00968    QListIterator<DocLnk> sdit( files.children() );
00969    for ( ; sdit.current(); ++sdit ) {
00970       delete sdit.current();
00971    }
00972    Global::findDocuments( &files, audioMimes);
00973    audioScan = true;
00974 }
00975 void PlayListWidget::scanForVideo() {
00976    //    odebug << "scan for video" << oendl;
00977    vFiles.detachChildren();
00978    QListIterator<DocLnk> sdit( vFiles.children() );
00979    for ( ; sdit.current(); ++sdit ) {
00980       delete sdit.current();
00981    }
00982    Global::findDocuments(&vFiles, "video/*");
00983    videoScan = true;
00984 }
00985 
00986 void PlayListWidget::populateAudioView() {
00987 
00988    audioView->clear();
00989    StorageInfo storageInfo;
00990    const QList<FileSystem> &fs = storageInfo.fileSystems();
00991    if(!audioScan) scanForAudio();
00992 
00993    QListIterator<DocLnk> dit( files.children() );
00994    QListIterator<FileSystem> it ( fs );
00995 
00996    QString storage;
00997    for ( ; dit.current(); ++dit ) {
00998       for( ; it.current(); ++it ){
00999          const QString name = (*it)->name();
01000          const QString path = (*it)->path();
01001          if(dit.current()->file().find(path) != -1 ) storage=name;
01002       }
01003 
01004       QListViewItem * newItem;
01005       if ( QFile( dit.current()->file()).exists() || dit.current()->file().left(4) == "http" ) {
01006          long size;
01007          if( dit.current()->file().left(4) == "http" )
01008             size=0;
01009          else
01010             size = QFile( dit.current()->file() ).size();
01011          //            odebug << dit.current()->name() << oendl;
01012          newItem= /*(void)*/ new QListViewItem( audioView, dit.current()->name(),
01013                              QString::number(size ), storage, dit.current()->file());
01014          newItem->setPixmap(0, Opie::Core::OResource::loadPixmap( "opieplayer/musicfile", Opie::Core::OResource::SmallIcon ));
01015       }
01016    }
01017 
01018 }
01019 
01020 void PlayListWidget::populateVideoView() {
01021     videoView->clear();
01022     StorageInfo storageInfo;
01023     const QList<FileSystem> &fs = storageInfo.fileSystems();
01024 
01025     if(!videoScan ) scanForVideo();
01026 
01027     QListIterator<DocLnk> Vdit( vFiles.children() );
01028     QListIterator<FileSystem> it ( fs );
01029     videoView->clear();
01030     QString storage;
01031     for ( ; Vdit.current(); ++Vdit ) {
01032         for( ; it.current(); ++it ){
01033             const QString name = (*it)->name();
01034             const QString path = (*it)->path();
01035             if( Vdit.current()->file().find(path) != -1 ) storage=name;
01036         }
01037 
01038         QListViewItem * newItem;
01039         if ( QFile( Vdit.current()->file()).exists() ) {
01040             newItem= /*(void)*/ new QListViewItem( videoView, Vdit.current()->name(),
01041                    QString::number( QFile( Vdit.current()->file() ).size() ),
01042                                                    storage, Vdit.current()->file());
01043             newItem->setPixmap(0, Opie::Core::OResource::loadPixmap( "opieplayer/videofile", Opie::Core::OResource::SmallIcon ));
01044         }
01045     }
01046 }
01047 
01048 void PlayListWidget::openFile() {
01049    QString filename, name;
01050    InputDialog *fileDlg;
01051    fileDlg = new InputDialog(this,tr("Open file or URL"),TRUE, 0);
01052    fileDlg->exec();
01053    if( fileDlg->result() == 1 ) {
01054       filename = fileDlg->text();
01055       //        odebug << "Selected filename is " + filename << oendl;
01056       DocLnk lnk;
01057       Config cfg( "OpiePlayer" );
01058       cfg.setGroup("PlayList");
01059 
01060       QString m3uFile;
01061       m3uFile = filename;
01062       if(filename.left(4) == "http") {
01063          if(filename.find(":",8,TRUE) != -1) { //found a port
01064 
01065             m3uFile = filename;
01066             if( m3uFile.right( 1 ).find( '/' ) == -1) {
01067                m3uFile += "/";
01068             }
01069             filename = m3uFile;
01070          }
01071          if( filename.right(3) == "xml" ||
01072                  filename.find("rss" ) !=-1)
01073          {
01074             // readpodcast(filename );
01075             downloadPodcast(filename);
01076          } else {
01077             lnk.setName( m3uFile ); //sets name
01078             lnk.setFile( filename ); //sets file name
01079             lnk.setIcon("opieplayer2/musicfile");
01080             d->selectedFiles->addToSelection(  lnk );
01081             writeCurrentM3u();
01082          }
01083       }
01084       else if( filename.right( 3) == "m3u" ) {
01085          readm3u( filename );
01086 
01087       } else if( filename.right(3) == "pls" ) {
01088          readPls( filename );
01089       } else if( filename.right(3) == "xml" ||
01090                  filename.find("rss" ) !=-1
01091          ) {
01092          readpodcast( filename );
01093       } else {
01094          lnk.setName( fullBaseName ( QFileInfo(filename)) ); //sets name
01095          lnk.setFile( filename ); //sets file name
01096          d->selectedFiles->addToSelection(  lnk);
01097          lnk.removeLinkFile();
01098          writeCurrentM3u();
01099       }
01100    }
01101 
01102    if( fileDlg ) {
01103       delete fileDlg;
01104    }
01105 }
01106 
01107 
01108 /*
01109  writes current playlist to current m3u file */
01110 void PlayListWidget::writeCurrentM3u() {
01111    //  odebug << "writing to current m3u" << oendl;
01112   Config cfg( "OpiePlayer" );
01113   cfg.setGroup("PlayList");
01114   currentPlayList = cfg.readEntry("CurrentPlaylist","");
01115   Om3u *m3uList;
01116   m3uList = new Om3u( currentPlayList, IO_ReadWrite | IO_Truncate );
01117 
01118   if( d->selectedFiles->first()) {
01119   do {
01120      //      odebug << "writeCurrentM3u " +d->selectedFiles->current()->file() << oendl;
01121     m3uList->add( d->selectedFiles->current()->file() );
01122   }
01123   while ( d->selectedFiles->next() );
01124   //    odebug << "<<<<<<<<<<<<>>>>>>>>>>>>>>>>>" << oendl;
01125   m3uList->write();
01126   m3uList->close();
01127 
01128   if(m3uList) delete m3uList;
01129   }
01130 }
01131 
01132   /*
01133  writes current playlist to m3u file */
01134 void PlayListWidget::writem3u() {
01135    InputDialog *fileDlg;
01136    fileDlg = new InputDialog( this, tr( "Save m3u Playlist " ), TRUE, 0);
01137    fileDlg->exec();
01138    QString name, filename, list;
01139    Om3u *m3uList;
01140 
01141    if( fileDlg->result() == 1 ) {
01142       name = fileDlg->text();
01143       //        odebug << filename << oendl;
01144       if( name.find("/",0,true) != -1) {// assume they specify a file path
01145          filename = name;
01146          name = name.right(name.length()- name.findRev("/",-1,true) - 1 );
01147       }
01148       else //otherwise dump it somewhere noticable
01149          filename = QPEApplication::documentDir() + "/" + name;
01150 
01151       if( filename.right( 3 ) != "m3u" ) //needs filename extension
01152          filename += ".m3u";
01153 
01154       if( d->selectedFiles->first()) {
01155          m3uList = new Om3u(filename, IO_ReadWrite | IO_Truncate);
01156 
01157          do {
01158             m3uList->add( d->selectedFiles->current()->file());
01159          }
01160          while ( d->selectedFiles->next() );
01161          //    odebug << list << oendl;
01162          m3uList->write();
01163          m3uList->close();
01164          if(m3uList) delete m3uList;
01165 
01166          if(fileDlg) delete fileDlg;
01167 
01168          DocLnk lnk;
01169          lnk.setFile( filename);
01170          lnk.setIcon("opieplayer2/playlist2");
01171          lnk.setName( name); //sets file name
01172 
01173          // odebug << filename << oendl;
01174          Config config( "OpiePlayer" );
01175          config.setGroup( "PlayList" );
01176 
01177          config.writeEntry("CurrentPlaylist",filename);
01178          currentPlayList=filename;
01179 
01180          if(!lnk.writeLink()) {
01181             //            odebug << "Writing doclink did not work" << oendl;
01182          }
01183 
01184          setCaption(tr("OpiePlayer: ") + name);
01185       }
01186    }
01187 }
01188 
01189 
01190 void PlayListWidget::keyReleaseEvent( QKeyEvent *e)
01191 {
01192     switch ( e->key() ) {
01194       case Key_F9: //activity
01195 //           if(audioUI->isHidden())
01196 //             audioUI->showMaximized();
01197           break;
01198       case Key_F10: //contacts
01199 //           if( videoUI->isHidden())
01200 //             videoUI->showMaximized();
01201           break;
01202       case Key_F11: //menu
01203           break;
01204       case Key_F12: //home
01205 //           doBlank();
01206           break;
01207       case Key_F13: //mail
01208 //           doUnblank();
01209           break;
01210       case Key_Q: //add to playlist
01211           addSelected();
01212           break;
01213       case Key_R: //remove from playlist
01214           removeSelected();
01215           break;
01216 //       case Key_P: //play
01217 //           odebug << "Play" << oendl;
01218 //           playSelected();
01219 //           break;
01220       case Key_Space:
01221 //          playSelected(); puh
01222           break;
01223       case Key_1:
01224           tabWidget->setCurrentPage(0);
01225           break;
01226       case Key_2:
01227           tabWidget->setCurrentPage(1);
01228           break;
01229     case Key_3:
01230       tabWidget->setCurrentPage(2);
01231       break;
01232     case Key_4:
01233       tabWidget->setCurrentPage(3);
01234       break;
01235     case Key_Down:
01236       if ( !d->selectedFiles->next() )
01237         d->selectedFiles->first();
01238 
01239       break;
01240     case Key_Up:
01241       if ( !d->selectedFiles->prev() )
01242         //        d->selectedFiles->last();
01243 
01244       break;
01245 
01246     }
01247 }
01248 
01249 void PlayListWidget::keyPressEvent( QKeyEvent *)
01250 {
01251 //    odebug << "Key press" << oendl;
01252 //     switch ( e->key() ) {
01253 // ////////////////////////////// Zaurus keys
01254 //       case Key_A: //add to playlist
01255 //           odebug << "Add" << oendl;
01256 //           addSelected();
01257 //           break;
01258 //       case Key_R: //remove from playlist
01259 //           removeSelected();
01260 //           break;
01261 //       case Key_P: //play
01262 //           odebug << "Play" << oendl;
01263 //           playSelected();
01264 //           break;
01265 //       case Key_Space:
01266 //           odebug << "Play" << oendl;
01267 //           playSelected();
01268 //           break;
01269 //     }
01270 }
01271 
01272 void PlayListWidget::doBlank() {
01273    //    odebug << "do blanking" << oendl;
01274 #ifdef QT_QWS_DEVFS
01275    fd=open("/dev/fb/0",O_RDWR);
01276 #else
01277    fd=open("/dev/fb0",O_RDWR);
01278 #endif
01279    if (fd != -1) {
01280       ioctl(fd,FBIOBLANK,1);
01281       //            close(fd);
01282    }
01283 }
01284 
01285 void PlayListWidget::doUnblank() {
01286    // this crashes opieplayer with a segfault
01287    //      int fd;
01288    //       fd=open("/dev/fb0",O_RDWR);
01289    //    odebug << "do unblanking" << oendl;
01290    if (fd != -1) {
01291       ioctl(fd,FBIOBLANK,0);
01292       close(fd);
01293    }
01294    QCopEnvelope h("QPE/System", "setBacklight(int)");
01295    h <<-3;// v[1]; // -3 Force on
01296 }
01297 
01298 void PlayListWidget::populateSkinsMenu() {
01299    int item = 0;
01300    defaultSkinIndex = 0;
01301    QString skinName;
01302    Config cfg( "OpiePlayer" );
01303    cfg.setGroup("Options" );
01304    QString skin = cfg.readEntry( "Skin", "default" );
01305 
01306    QDir skinsDir( QPEApplication::qpeDir() + "pics/opieplayer2/skins" );
01307    skinsDir.setFilter( QDir::Dirs );
01308    skinsDir.setSorting(QDir::Name );
01309    const QFileInfoList *skinslist = skinsDir.entryInfoList();
01310    QFileInfoListIterator it( *skinslist );
01311    QFileInfo *fi;
01312    while ( ( fi = it.current() ) ) {
01313       skinName =  fi->fileName();
01314       //        odebug << fi->fileName() << oendl;
01315       if( skinName != "." &&  skinName != ".." && skinName !="CVS" )  {
01316          item = skinsMenu->insertItem( fi->fileName() ) ;
01317       }
01318       if( skinName == "default" ) {
01319          defaultSkinIndex = item;
01320       }
01321       if( skinName == skin ) {
01322          skinsMenu->setItemChecked( item, TRUE );
01323       }
01324       ++it;
01325    }
01326 }
01327 
01328 void PlayListWidget::skinsMenuActivated( int item ) {
01329    for( int i = defaultSkinIndex; i > defaultSkinIndex - static_cast<int>(skinsMenu->count()); i-- ) {
01330       skinsMenu->setItemChecked( i, FALSE );
01331    }
01332    skinsMenu->setItemChecked( item, TRUE );
01333 
01334    Config cfg( "OpiePlayer" );
01335    cfg.setGroup("Options");
01336    cfg.writeEntry("Skin", skinsMenu->text( item ) );
01337 }
01338 
01339 void PlayListWidget::qcopReceive(const QCString &msg, const QByteArray &data) {
01340    //   odebug << "qcop message "+msg << oendl;
01341    QDataStream stream ( data, IO_ReadOnly );
01342    if ( msg == "play()" ) { //plays current selection
01343       btnPlay( true);
01344    } else if ( msg == "stop()" ) {
01345       mediaPlayerState->setPlaying( false);
01346    } else if ( msg == "togglePause()" ) {
01347       mediaPlayerState->togglePaused();
01348    } else if ( msg == "next()" ) { //select next in lis
01349       mediaPlayerState->setNext();
01350    } else if ( msg == "prev()" ) { //select previous in list
01351       mediaPlayerState->setPrev();
01352    } else if ( msg == "toggleLooping()" ) { //loop or not loop
01353       mediaPlayerState->toggleLooping();
01354    } else if ( msg == "toggleShuffled()" ) { //shuffled or not shuffled
01355       mediaPlayerState->toggleShuffled();
01356    } else if ( msg == "volUp()" ) { //volume more
01357       //       emit moreClicked();
01358       //       emit moreReleased();
01359    } else if ( msg == "volDown()" ) { //volume less
01360       //       emit lessClicked();
01361       //       emit lessReleased();
01362    } else if ( msg == "play(QString)" ) { //play this now
01363       QString file;
01364       stream >> file;
01365       setDocumentEx( (const QString &) file);
01366    } else if ( msg == "add(QString)" ) { //add to playlist
01367       QString file;
01368       stream >> file;
01369       QFileInfo fileInfo(file);
01370       DocLnk lnk;
01371       lnk.setName( fileInfo.baseName() ); //sets name
01372       lnk.setFile( file ); //sets file name
01373       addToSelection( lnk );
01374    } else if ( msg == "rem(QString)" ) { //remove from playlist
01375       QString file;
01376       stream >> file;
01377    } else if ( msg == "setDocument(QString)" ) { //loop or not loop
01378       QCopEnvelope h("QPE/Application/opieplayer", "raise()");
01379    }
01380 }
01381 
01382 /*
01383 reads m3u and shows files/urls to playlist widget */
01384 void PlayListWidget::readm3u( const QString &filename ) {
01385    //    odebug << "read m3u filename " + filename << oendl;
01386 
01387    Om3u *m3uList;
01388    QString s, name;
01389    m3uList = new Om3u( filename, IO_ReadOnly );
01390    m3uList->readM3u();
01391    DocLnk lnk;
01392    for ( QStringList::ConstIterator it = m3uList->begin(); it != m3uList->end(); ++it ) {
01393       s = *it;
01394       //          odebug << "reading "+ s << oendl;
01395       if(s.left(4)=="http") {
01396          lnk.setName( s ); //sets file name
01397          lnk.setIcon("opieplayer2/musicfile");
01398 
01399          //          if(s.right(4) != '.' || s.right(5) != '.')
01400          if(s.right(4) != '.' || s.right(5) != '.' )
01401             if( s.right(1) != "/")
01402                lnk.setFile( s+"/"); //if url with no extension
01403             else
01404                lnk.setFile( s ); //sets file name
01405 
01406       }  else {
01407          //               if( QFileInfo( s ).exists() ) {
01408          lnk.setName( fullBaseName ( QFileInfo(s)));
01409          //                 if(s.right(4) == '.')   {//if regular file
01410          if(s.left(1) != "/")  {
01411             //            odebug << "set link "+QFileInfo(filename).dirPath()+"/"+s << oendl;
01412             lnk.setFile( QFileInfo(filename).dirPath()+"/"+s);
01413             lnk.setIcon("SoundPlayer");
01414          } else {
01415             //            odebug << "set link2 "+s << oendl;
01416             lnk.setFile( s);
01417             lnk.setIcon("SoundPlayer");
01418          }
01419       }
01420       d->selectedFiles->addToSelection( lnk );
01421    }
01422    Config config( "OpiePlayer" );
01423    config.setGroup( "PlayList" );
01424 
01425    config.writeEntry("CurrentPlaylist",filename);
01426    config.write();
01427    currentPlayList=filename;
01428 
01429 //    m3uList->write();
01430    m3uList->close();
01431    if(m3uList) delete m3uList;
01432 
01433    d->selectedFiles->setSelectedItem( s);
01434    setCaption(tr("OpiePlayer: ")+ fullBaseName ( QFileInfo(filename)));
01435 
01436 }
01437 
01438 /*
01439 reads pls and adds files/urls to playlist  */
01440 void PlayListWidget::readPls( const QString &filename )
01441 {
01442    //    odebug << "pls filename is " + filename << oendl;
01443    Om3u *m3uList;
01444    QString s, name;
01445    m3uList = new Om3u( filename, IO_ReadOnly );
01446    m3uList->readPls();
01447 
01448    for ( QStringList::ConstIterator it = m3uList->begin(); it != m3uList->end(); ++it ) {
01449       s = *it;
01450       //        s.replace( QRegExp( "%20" )," " );
01451       DocLnk lnk( s );
01452       QFileInfo f( s );
01453       QString name = fullBaseName ( f);
01454 
01455       if( name.left( 4 ) == "http" ) {
01456          name = s.right( s.length() - 7);
01457       }  else {
01458          name = s;
01459       }
01460 
01461       name = name.right( name.length() - name.findRev( "\\", -1, TRUE) - 1 );
01462 
01463       lnk.setName( name );
01464       if( s.at( s.length() - 4) == '.') {// if this is probably a file
01465          lnk.setFile( s );
01466       } else { //if its a url
01467          if( name.right( 1 ).find( '/' ) == -1) {
01468             s += "/";
01469          }
01470          lnk.setFile( s );
01471       }
01472       lnk.setType( "audio/x-mpegurl" );
01473 
01474       lnk.writeLink();
01475       d->selectedFiles->addToSelection( lnk );
01476    }
01477 
01478    m3uList->close();
01479    if(m3uList) delete m3uList;
01480 }
01481 
01482 bool PlayListWidget::readpodcast( const QString &filename )
01483 {
01484    QStringList latestPodCast;
01485    //download url
01486    qWarning("podcast "+filename);
01487    QFileInfo info(filename);
01488    if (info.size() > 0) {
01489       bool result = false;
01490       // qWarning("parseDoc " + feedFile.name() );
01491       QFile file(filename);
01492       QXmlInputSource source( file);
01493       QXmlSimpleReader reader;
01494       //  reader.setFeature("http://xml.org/sax/features/namespaces", true);
01495       //  reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
01496       //  reader.setFeature("http://trolltech.com/xml/features/report-whitespace-only-CharData", false);
01497 
01498       reader.setContentHandler( &rssHandler);
01499       reader.setErrorHandler( &rssHandler);
01500       result = reader.parse( source);
01501       if (!result) {
01502          QMessageBox::critical(0, "Error", tr("<p>Error unable to parse file.</p>"));
01503 //            qWarning("Error unable to parse file\n%s", handler.errorMessage.local8Bit().data());
01504          return false;
01505       } else {
01506          int size = rssHandler.getItems().size();
01507           qWarning( rssHandler.getChannelInfo().join("\n"));
01508          
01509          for(int i = 0; i < size; i++) {
01510             QList<QStringList> attributesList = rssHandler.getItems().at(i)->attributes;
01511             QStringList *sList;
01512             QStringList attList;
01513             for(sList = attributesList.first(); sList !=0; sList = attributesList.next()) {
01514                for( QStringList::Iterator it = sList->begin(); it != sList->end(); ++it ) {
01515                   attList << (*it);
01516                }
01517                if(i == 0) { //this assumes that the latest is the first
01518                   latestPodCast << attList[2]; //this is our mp3 url
01519                   latestPodCast << rssHandler.getItems().at(i)->title;
01520                   latestPodCast << rssHandler.getItems().at(i)->description;   
01521                   latestPodCast << rssHandler.getItems().at(i)->pubdate;
01522                }
01523             }
01524          }
01525          QString s = latestPodCast[0];   //this is our mp3 url
01526          
01527 //            http://www.davesipaq.com/podcast.xml
01528          DocLnk lnk( s );
01529          QFileInfo f( s );
01530          QString name = fullBaseName ( f);
01531 
01532          if( name.left( 4 ) == "http" ) {
01533             name = s.right( s.length() - 7);
01534          }  else {
01535             name = s;
01536          }
01537 
01538          name = name.right( name.length() - name.findRev( "\\", -1, TRUE) - 1 );
01539 
01540          lnk.setName( name );
01541          if( s.at( s.length() - 4) == '.') {// if this is probably a file
01542             lnk.setFile( s );
01543          } else { //if its a url
01544             if( name.right( 1 ).find( '/' ) == -1) {
01545                s += "/";
01546             }
01547             lnk.setFile( s );
01548          }
01549          lnk.setType( "audio/x-mpegurl" );
01550 
01551          lnk.writeLink();
01552          d->selectedFiles->addToSelection( lnk );
01553 
01554       }
01555    } else {
01556       QMessageBox::critical( 0, "Qtopia Rss", tr("<p>Sorry, could not find the requested document.</p>"));
01557       return false;
01558    }
01559 
01560    qWarning( latestPodCast.join("\n"));
01561 
01562    return true;
01563 }
01564 
01565 bool PlayListWidget::downloadPodcast(const QString &url)
01566 {
01567    qWarning("download "+url);
01568    QString localFile;
01569    localFile = url;
01570    localFile = localFile.mid(7, localFile.length()-7);
01571   
01572    localFile = localFile.replace(QRegExp("/"), "_");
01573    localFile = localFile.replace(QRegExp("&"), "_");
01574    localFile = localFile.replace(QRegExp("="), "_");
01575    localFile = localFile.replace(QRegExp("\\?"), "_");
01576    localFile = localFile.replace(QRegExp("@"), "_");
01577    localFile = QDir::homeDirPath()+"/Settings/"+localFile;
01578    
01579 #warning FIXME
01580    QString cmd;
01581    cmd = "wget ";
01582    cmd +=" -O ";
01583    cmd += localFile + " " + url;
01584    qWarning(cmd);
01585    system(cmd.latin1());   
01586 //   Process ipkg_status(QStringList()<< "wget" <<"-O" << localFile << url );
01587    // QString out;
01588 //   bool r = ipkg_status.exec("",out);
01589 //   if(r)
01590 //      qWarning(out);
01591    readpodcast(localFile);   
01592    return true;
01593 }
01594 

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