playlistbrowser.cpp

来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 1,990 行 · 第 1/5 页

CPP
1,990
字号
    QString sql = "SELECT * FROM podcastfolders ORDER BY parent ASC;";    QStringList values = CollectionDB::instance()->query( sql );    // store the folder and IDs so finding a parent is fast    QMap<int,PlaylistCategory*> folderMap;    PlaylistCategory *folder = 0;    foreach( values )    {        const int     id       =     (*it).toInt();        const QString t        =    *++it;        const int     parentId =   (*++it).toInt();        const bool    isOpen   = ( (*++it) == CollectionDB::instance()->boolT() ? true : false );        PlaylistCategory *parent = p;        if( parentId > 0 && folderMap.find( parentId ) != folderMap.end() )            parent = folderMap[parentId];        folder = new PlaylistCategory( parent, folder, t, id );        folder->setOpen( isOpen );        folderMap[id] = folder;    }    // check if the base folder exists    p->setOpen( Amarok::config( "PlaylistBrowser" )->readBoolEntry( "Podcast Folder Open", true ) );    return folderMap;}void PlaylistBrowser::savePodcastFolderStates( PlaylistCategory *folder ){    if( !folder ) return;    PlaylistCategory *child = static_cast<PlaylistCategory*>(folder->firstChild());    while( child )    {        if( isCategory( child ) )            savePodcastFolderStates( child );        else            break;        child = static_cast<PlaylistCategory*>(child->nextSibling());    }    if( folder != m_podcastCategory )    {        if( folder->id() < 0 ) // probably due to a 1.3->1.4 migration        {                      // we add the folder to the db, set the id and then update all the children            int parentId = static_cast<PlaylistCategory*>(folder->parent())->id();            int newId = CollectionDB::instance()->addPodcastFolder( folder->text(0), parentId, folder->isOpen() );            folder->setId( newId );            PodcastChannel *chan = static_cast<PodcastChannel*>(folder->firstChild());            while( chan )            {                if( isPodcastChannel( chan ) )                    // will update the database so child has correct parentId.                    chan->setParent( folder );                chan = static_cast<PodcastChannel*>(chan->nextSibling());            }        }        else        {            CollectionDB::instance()->updatePodcastFolder( folder->id(), folder->text(0),                              static_cast<PlaylistCategory*>(folder->parent())->id(), folder->isOpen() );        }    }}void PlaylistBrowser::scanPodcasts(){    //don't want to restart timer unnecessarily.  addPodcast will start it if it is necessary    if( m_podcastItemsToScan.isEmpty() ) return;    for( uint i=0; i < m_podcastItemsToScan.count(); i++ )    {        QListViewItem  *item = m_podcastItemsToScan.at( i );        PodcastChannel *pc   = static_cast<PodcastChannel*>(item);        pc->rescan();    }    //restart timer    m_podcastTimer->start( m_podcastTimerInterval );}void PlaylistBrowser::refreshPodcasts( QListViewItem *parent ){    for( QListViewItem *child = parent->firstChild();            child;            child = child->nextSibling() )    {        if( isPodcastChannel( child ) )            static_cast<PodcastChannel*>( child )->rescan();        else if( isCategory( child ) )            refreshPodcasts( child );    }}void PlaylistBrowser::addPodcast( QListViewItem *parent ){    bool ok;    const QString name = KInputDialog::getText(i18n("Add Podcast"), i18n("Enter Podcast URL:"), QString::null, &ok, this);    if( ok && !name.isEmpty() )    {        addPodcast( KURL::fromPathOrURL( name ), parent );    }}void PlaylistBrowser::configurePodcasts( QListViewItem *parent ){    QPtrList<PodcastChannel> podcastChannelList;    for( QListViewItem *child = parent->firstChild();         child;         child = child->nextSibling() )    {        if( isPodcastChannel( child ) )        {            podcastChannelList.append( static_cast<PodcastChannel*>( child ) );        }    }    if( !podcastChannelList.isEmpty() )        configurePodcasts( podcastChannelList, i18n( "Podcasts contained in %1", "All in %1").arg( parent->text( 0 ) ) );}void PlaylistBrowser::configureSelectedPodcasts(){    QPtrList<PodcastChannel> selected;    QListViewItemIterator it( m_listview, QListViewItemIterator::Selected);    for( ; it.current(); ++it )    {        if( isPodcastChannel( (*it) ) )            selected.append( static_cast<PodcastChannel*>(*it) );    }    if (selected.isEmpty() )        return; //shouldn't happen    if( selected.count() == 1 )        selected.getFirst()->configure();    else        configurePodcasts( selected, i18n("1 Podcast", "%n Podcasts", selected.count() ) );    if( m_podcastItemsToScan.isEmpty() )        m_podcastTimer->stop();    else if( m_podcastItemsToScan.count() == 1 )        m_podcastTimer->start( m_podcastTimerInterval );                    // else timer is already running}void PlaylistBrowser::configurePodcasts( QPtrList<PodcastChannel> &podcastChannelList,                                         const QString &caption ){    if( podcastChannelList.isEmpty() )    {        debug() << "BUG: podcastChannelList is empty" << endl;        return;    }    QPtrList<PodcastSettings> podcastSettingsList;    foreachType( QPtrList<PodcastChannel>, podcastChannelList)    {        podcastSettingsList.append( (*it)->getSettings() );    }    PodcastSettingsDialog *dialog = new PodcastSettingsDialog( podcastSettingsList, caption );    if( dialog->configure() )    {        PodcastChannel *channel = podcastChannelList.first();        foreachType( QPtrList<PodcastSettings>, podcastSettingsList )        {            if ( (*it)->title() ==  channel->title() )            {                channel->setSettings( *it );            }            else                debug() << " BUG in playlistbrowser.cpp:configurePodcasts( )" << endl;            channel = podcastChannelList.next();        }    }}PodcastChannel *PlaylistBrowser::findPodcastChannel( const KURL &feed, QListViewItem *parent ) const{    if( !parent ) parent = static_cast<QListViewItem*>(m_podcastCategory);    for( QListViewItem *it = parent->firstChild();            it;            it = it->nextSibling() )    {        if( isPodcastChannel( it ) )        {            PodcastChannel *channel = static_cast<PodcastChannel *>( it );            if( channel->url().prettyURL() == feed.prettyURL() )            {                return channel;            }        }        else if( isCategory( it ) )        {            PodcastChannel *channel = findPodcastChannel( feed, it );            if( channel )                return channel;        }    }    return 0;}PodcastEpisode *PlaylistBrowser::findPodcastEpisode( const KURL &episode, const KURL &feed ) const{    PodcastChannel *channel = findPodcastChannel( feed );    if( !channel )        return 0;    if( !channel->isPolished() )        channel->load();    QListViewItem *child = channel->firstChild();    while( child )    {        #define child static_cast<PodcastEpisode*>(child)        if( child->url() == episode )            return child;        #undef  child        child = child->nextSibling();    }    return 0;}void PlaylistBrowser::addPodcast( const KURL& origUrl, QListViewItem *parent ){    if( !parent ) parent = static_cast<QListViewItem*>(m_podcastCategory);    KURL url( origUrl );    if( url.protocol() == "itpc" || url.protocol() == "pcast" )        url.setProtocol( "http" );    PodcastChannel *channel = findPodcastChannel( url );    if( channel )    {        Amarok::StatusBar::instance()->longMessage(                i18n( "Already subscribed to feed %1 as %2" )                .arg( url.prettyURL(), channel->title() ),                KDE::StatusBar::Sorry );        return;    }    PodcastChannel *pc = new PodcastChannel( parent, 0, url );    if( m_podcastItemsToScan.isEmpty() )    {        m_podcastItemsToScan.append( pc );        m_podcastTimer->start( m_podcastTimerInterval );    }    else    {        m_podcastItemsToScan.append( pc );    }    parent->sortChildItems( 0, true );    parent->setOpen( true );}void PlaylistBrowser::changePodcastInterval(){    double time = static_cast<double>(m_podcastTimerInterval / ( 60 * 60 * 1000 ));    bool ok;    double interval = KInputDialog::getDouble( i18n("Download Interval"),                                            i18n("Scan interval (hours):"), time,                                            0.5, 100.0, .5, 1, // min, max, step, base                                            &ok, this);    int milliseconds = static_cast<int>(interval*60.0*60.0*1000.0);    if( ok )    {        if( milliseconds != m_podcastTimerInterval )        {            m_podcastTimerInterval = milliseconds;            m_podcastTimer->changeInterval( m_podcastTimerInterval );        }    }}bool PlaylistBrowser::deleteSelectedPodcastItems( const bool removeItem, const bool silent ){    KURL::List urls;    QListViewItemIterator it( m_podcastCategory, QListViewItemIterator::Selected );    QPtrList<PodcastEpisode> erasedItems;    for( ; it.current(); ++it )    {        if( isPodcastEpisode( *it ) )        {            #define item static_cast<PodcastEpisode*>(*it)            if( item->isOnDisk() ) {                urls.append( item->localUrl() );                erasedItems.append( item );            }            #undef  item        }    }    if( urls.isEmpty() ) return false;    int button;    if( !silent )        button = KMessageBox::warningContinueCancel( this,                    i18n( "<p>You have selected 1 podcast episode to be <b>irreversibly</b> deleted. ",                          "<p>You have selected %n podcast episodes to be <b>irreversibly</b> deleted. ",                           urls.count() ), QString::null, KStdGuiItem::del() );    if( silent || button != KMessageBox::Continue )        return false;    KIO::Job *job = KIO::del( urls );    PodcastEpisode *item;    for ( item = erasedItems.first(); item; item = erasedItems.next() )    {        if( removeItem )        {            CollectionDB::instance()->removePodcastEpisode( item->dBId() );            delete item;        }        else            connect( job, SIGNAL( result( KIO::Job* ) ), item, SLOT( isOnDisk() ) );;    }    return true;}bool PlaylistBrowser::deletePodcasts( QPtrList<PodcastChannel> items ){    if( items.isEmpty() ) return false;    KURL::List urls;    foreachType( QPtrList<PodcastChannel>, items )    {        for( QListViewItem *ch = (*it)->firstChild(); ch; ch = ch->nextSibling() )        {            #define ch static_cast<PodcastEpisode*>(ch)            if( ch->isOnDisk() )            {                //delete downloaded media                urls.append( ch->localUrl() );            }            #undef  ch            /// we don't need to delete from the database, because removing the channel from the database            /// automatically removes the children as well.            m_podcastItemsToScan.remove( static_cast<PodcastChannel*>(*it) );        }        CollectionDB::instance()->removePodcastChannel( static_cast<PodcastChannel*>(*it)->url() );    }    // TODO We need to check which files have been deleted successfully    if ( urls.count() )        KIO::del( urls );    return true;}void PlaylistBrowser::downloadSelectedPodcasts(){    QListViewItemIterator it( m_listview, QListViewItemIterator::Selected );    for( ; it.current(); ++it )    {        if( isPodcastEpisode( *it ) )        {            #define item static_cast<PodcastEpisode*>(*it)            if( !item->isOnDisk() )                m_podcastDownloadQueue.append( item );            #undef  item        }    }    downloadPodcastQueue();}void PlaylistBrowser::downloadPodcastQueue() //SLOT{    if( m_podcastDownloadQueue.isEmpty() ) return;    PodcastEpisode *first = m_podcastDownloadQueue.first();    first->downloadMedia();    m_podcastDownloadQueue.removeFirst();    connect( first, SIGNAL( downloadFinished() ), this, SLOT( downloadPodcastQueue() ) );    connect( first, SIGNAL( downloadAborted() ),  this, SLOT( abortPodcastQueue()  ) );}void PlaylistBrowser::abortPodcastQueue() //SLOT{    m_podcastDownloadQueue.clear();}void PlaylistBrowser::registerPodcastSettings( const QString &title, const PodcastSettings *settings ){    m_podcastSettings.insert( title, settings );}/** ************************************************************************* *  PLAYLISTS

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?