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

playlist.cpp

Go to the documentation of this file.
00001 /*
00002                             This file is part of the Opie Project
00003 
00004                              Copyright (c)  2002 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 ..}^=.=       =       ;      Library General Public License for more
00023 ++=   -.     .`     .:       details.
00024  :     =  ...= . :.=-
00025  -.   .:....=;==+<;          You should have received a copy of the GNU
00026   -_. . .   )=.  =           Library 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 #include "playlist.h"
00034 #include "../opieplayer2/lib.h"
00035 #include "../opieplayer2/om3u.h"
00036 
00037 #include <opie2/odebug.h>
00038 #include <opie2/oresource.h>
00039 #include <opie2/ofiledialog.h>
00040 
00041 #include <qfileinfo.h>
00042 #include <qmessagebox.h>
00043 #include <qdir.h>
00044 
00045 //#define DEFAULT_FILE_TYPES  "*.mp3;*.ogg;*.ogm;*.wma;*.wav;*.asf;*.au;*.avi;*.mpeg;*.mpg;*.mv1;*.mov;*.wmv;*.pls;*.m3u;*.mp4;*.m4a"
00046 #define DEFAULT_FILE_TYPES  "*.mp3;*.ogg;*.ogm;*.wma;*.wav;*.asf;*.au;*.avi;*.mpeg;*.mpg;*.mv1;*.mov;*.wmv;*.mp4;*.m4a"
00047 
00048 PlaylistItem::PlaylistItem(const DocLnk& aLink,PlaylistView*parent)
00049     :QListViewItem(parent),m_Content(aLink),m_video(false)
00050 {
00051 }
00052 
00053 PlaylistItem::PlaylistItem(const DocLnk&aLink,PlaylistView*parent,PlaylistItem*after)
00054     :QListViewItem(parent,after),m_Content(aLink),m_video(false)
00055 {
00056 }
00057 
00058 void PlaylistItem::Video(bool y)
00059 {
00060     m_video=y;
00061     if (m_video) {
00062         setPixmap(0,Opie::Core::OResource::loadPixmap("opieplayer2/videofile"));
00063     } else {
00064         setPixmap(0,Opie::Core::OResource::loadPixmap("opieplayer2/musicfile"));
00065     }
00066 }
00067 
00068 PlaylistItem::~PlaylistItem()
00069 {
00070 }
00071 
00072 /* PlaylistView Methods */
00073 PlaylistView::PlaylistView(QWidget *parent, const char *name)
00074     : QListView(parent,name)
00075 {
00076 //    columnLabels << tr("FullName");
00077     columnLabels << tr("");                 // icon
00078     columnLabels << tr("File");
00079     columnLabels << tr("Playtime");
00080     columnLabels << tr("Artist");
00081     columnLabels << tr("Album");
00082     columnLabels << tr("Title");
00083     columnLabels << tr("Type");
00084     columnLabels << tr("Size");
00085     for (QStringList::Iterator it = columnLabels.begin(); it != columnLabels.end(); ++it) {
00086         addColumn(*it);
00087     }
00088     m_Infolib=0;
00089     setAllColumnsShowFocus(true);
00090     setSelectionMode(Single);
00091     setSorting(-1);
00092     m_lastItem = 0;
00093     m_lastDir = QDir::homeDirPath();
00094 }
00095 
00096 PlaylistView::~PlaylistView()
00097 {
00098     if (m_Infolib) delete m_Infolib;
00099 }
00100 
00101 void PlaylistView::checkLib()
00102 {
00103     if (!m_Infolib) {
00104         m_Infolib = new XINE::Lib(XINE::Lib::InitializeImmediately);
00105         connect(m_Infolib,SIGNAL(stopped()),this,SLOT(slotDummyStop()));
00106         m_Infolib->ensureInitialized();
00107     }
00108 }
00109 
00110 void PlaylistView::slotDummyStop()
00111 {
00112     odebug << "void PlaylistView::slotDummyStop()" << oendl;
00113 }
00114 
00115 void PlaylistView::slotAddFile(const DocLnk&aLink)
00116 {
00117     QFileInfo f(aLink.file());
00118     if (f.extension(FALSE).lower()=="m3u"||f.extension(FALSE).lower()=="pls") {
00119         readPlayList(aLink.file());
00120     } else {
00121         addFile(aLink.file(),aLink.name());
00122     }
00123     emit contentChanged(childCount());
00124 }
00125 
00126 void PlaylistView::addFile(const QString&aFile,const QString&aName)
00127 {
00128     QFileInfo fileInfo(aFile);
00129     if (!fileInfo.exists()) return;
00130     checkLib();
00131     QString name = aName;
00132     if (name.isEmpty()) {
00133         name = fileInfo.fileName();
00134     }
00135     int i = m_Infolib->setfile(aFile.utf8().data());
00136     /* realy! otherwise we get an "stopped" signal when playing! - I don't know why */
00137     m_Infolib->stop();
00138     odebug << "File set: " << i << " ("<<aFile.utf8().data()<<")"<<oendl;
00139     if (i<1) {
00140         i = m_Infolib->error();
00141         odebug << "Errorcode: " << i << oendl;
00142         QString msg;
00143         msg = QString(tr("Error open file %1: ")).arg(name);
00144         switch (i) {
00145             case 1:
00146                 msg += tr("No input plugin");
00147                 break;
00148             case 2:
00149                 msg += tr("No demuxer plugin");
00150                 break;
00151             case 3:
00152                 msg += tr("Demuxer failed");
00153                 break;
00154             case 4:
00155                 msg+=tr("Malformed mrl");
00156                 break;
00157             default:
00158                 msg += tr("Unknown error");
00159                 break;
00160         }
00161         QMessageBox::warning(0,tr("Error reading fileheader"),msg);
00162         return;
00163     }
00164     if (m_items.count()>0) {
00165         m_lastItem = m_items.last();
00166     } else {
00167         m_lastItem = 0;
00168     }
00169     PlaylistItem*_it = currentItem();
00170 
00171     if (m_lastItem) {
00172         m_lastItem = new PlaylistItem(aFile,this,m_lastItem);
00173     } else {
00174         m_lastItem = new PlaylistItem(aFile,this);
00175     }
00176     m_lastItem->setExpandable(false);
00177     m_lastItem->setText(1,name);
00178 
00179     QString codec = m_Infolib->metaInfo(6);
00180     if (codec.isEmpty()) {
00181         codec = m_Infolib->metaInfo(7);
00182     }
00183     // codec
00184     m_lastItem->setText(COL_TYPE,codec);
00185     // title
00186     m_lastItem->setText(COL_TITLE,m_Infolib->metaInfo(0));
00187     // artist
00188     m_lastItem->setText(COL_ARTIST,m_Infolib->metaInfo(2));
00189     // album
00190     m_lastItem->setText(COL_ALBUM,m_Infolib->metaInfo(4));
00191     int l = m_Infolib->length();
00192     int h = l/3600;
00193     l-=h*3600;
00194     int m = l/60;
00195     l-=m*60;
00196     codec = "";
00197     if (h>0) {
00198         codec.sprintf("%2i:%2i:%2i h",h,m,l);
00199     } else {
00200         codec.sprintf("%02i:%02i m",m,l);
00201     }
00202     // time
00203     m_lastItem->setText(COL_TIME,codec);
00204     m_lastItem->Video(m_Infolib->hasVideo());
00205     m_items.append(m_lastItem);
00206     if (_it==NULL) {
00207         setSelected(m_lastItem,true);
00208     }
00209 }
00210 
00211 void PlaylistView::slotAppendDir()
00212 {
00213     QString _dir = Opie::Ui::OFileDialog::getDirectory(Opie::Ui::OFileSelector::Extended,m_lastDir,0,m_lastDir);
00214     if (_dir.isEmpty()) return;
00215     m_lastDir = _dir;
00216     QDir sDir(_dir);
00217     QStringList list = sDir.entryList(DEFAULT_FILE_TYPES,QDir::Files,QDir::Name | QDir::IgnoreCase);
00218     for (unsigned i = 0; i < list.count();++i) {
00219         addFile(_dir+QString("/")+list[i]);
00220     }
00221     emit contentChanged(childCount());
00222 }
00223 
00224 void PlaylistView::slotOpenM3u()
00225 {
00226     QStringList types;
00227     QMap<QString, QStringList> mimeTypes;
00228     types << "audio/x-mpegurl";
00229     mimeTypes.insert("Playlists",types);
00230 //    mimeTypes.insert("All",types);
00231     QString fileName= Opie::Ui::OFileDialog::getOpenFileName(Opie::Ui::OFileSelector::EXTENDED,
00232                                                  m_lastDir,"playlist.m3u", mimeTypes);
00233     if (fileName.isEmpty()) {
00234         return;
00235     }
00236     readPlayList(fileName);
00237     emit contentChanged(childCount());
00238 }
00239 
00240 void PlaylistView::readPlayList(const QString&fileName)
00241 {
00242     QFileInfo f(fileName);
00243 
00244     Om3u _om3u(fileName, IO_ReadOnly);
00245     if (f.extension(FALSE).lower()=="m3u") {
00246         _om3u.readM3u();
00247     } else if (f.extension(FALSE).lower()=="pls") {
00248         _om3u.readPls();
00249     }
00250     for (unsigned int j=0; j<_om3u.count();++j) {
00251         addFile(_om3u[j]);
00252     }
00253 }
00254 
00255 void PlaylistView::slotSaveAsM3u()
00256 {
00257     QStringList types;
00258     QMap<QString, QStringList> mimeTypes;
00259     types << "audio/x-mpegurl";
00260     mimeTypes.insert("Playlists",types);
00261     QString fileName= Opie::Ui::OFileDialog::getSaveFileName(Opie::Ui::OFileSelector::EXTENDED,
00262                                                  m_lastDir,"playlist.m3u", mimeTypes);
00263     odebug << "Save as " << fileName << oendl;
00264     if (fileName.isEmpty()) {
00265         return;
00266     }
00267     Om3u _om3u(fileName, IO_ReadWrite | IO_Truncate);
00268     for (unsigned j=0; j<m_items.count();++j) {
00269         _om3u.add(m_items[j]->Lnk().file());
00270     }
00271     _om3u.write();
00272 }
00273 
00274 void PlaylistView::removeFromList(PlaylistItem*Item)
00275 {
00276     if (!Item)return;
00277     t_itemlist::Iterator iter,it2;
00278     iter = m_items.find(Item);
00279     it2 = m_items.end();
00280     if (iter!=m_items.end()) {
00281         it2 = iter;
00282         ++it2;
00283         m_items.remove(iter);
00284     }
00285     delete Item;
00286     if (it2!=m_items.end()) {
00287         setSelected((*it2),true);
00288     } else if (m_items.count()) {
00289         QListViewItem * it = m_items.last();
00290         setSelected(it,true);
00291     }
00292     emit contentChanged(childCount());
00293 }
00294 
00295 XINE::Lib*PlaylistView::getXine()
00296 {
00297     checkLib();
00298     return m_Infolib;
00299 }
00300 
00301 void PlaylistView::setCurrentItem(PlaylistItem*aItem)
00302 {
00303     setSelected(aItem,true);
00304 }
00305 
00306 PlaylistItem* PlaylistView::currentItem()const
00307 {
00308     QListViewItem*it = selectedItem();
00309     if (!it) return 0;
00310     return (PlaylistItem*)it;
00311 }
00312 
00313 PlaylistItem* PlaylistView::nextItem(PlaylistItem*parent)const
00314 {
00315     if (m_items.count()==0) return 0;
00316     if (!parent) return m_items[0];
00317     for (unsigned j=0; j<m_items.count()-1;++j) {
00318         if (m_items[j]==parent) {
00319             return m_items[j+1];
00320         }
00321     }
00322     return 0;
00323 }
00324 
00325 PlaylistItem* PlaylistView::prevItem(PlaylistItem*parent)const
00326 {
00327     if (m_items.count()==0) return 0;
00328     if (!parent) return m_items[m_items.count()-1];
00329     for (unsigned j=m_items.count()-1; j>0;--j) {
00330         if (m_items[j]==parent) {
00331             return m_items[j-1];
00332         }
00333     }
00334     return 0;
00335 }

Generated on Sat Nov 5 16:17:34 2005 for OPIE by  doxygen 1.4.2