playlistbrowser.cpp

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

CPP
1,990
字号
    QDomDocument d;    QDomElement e;    QListViewItem *after = m_dynamicCategory;    if( !file.open( IO_ReadOnly ) || !d.setContent( stream.read() ) )    { /*Couldn't open the file or it had invalid content, so let's create an empty element*/        return new PlaylistCategory( m_listview, after , i18n("Radio Streams") );    }    else {        e = d.namedItem( "category" ).toElement();        if ( e.attribute("formatversion") =="1.1" ) {            PlaylistCategory* p = new PlaylistCategory( m_listview, after, e );            p->setText(0, i18n("Radio Streams") );            return p;        }        else { // Old unversioned format            PlaylistCategory* p = new PlaylistCategory( m_listview, after, i18n("Radio Streams") );            QListViewItem *last = 0;            QDomNode n = d.namedItem( "streambrowser" ).namedItem("stream");            for( ; !n.isNull();  n = n.nextSibling() ) {                last = new StreamEntry( p, last, n.toElement() );            }            return p;        }    }}void PlaylistBrowser::loadCoolStreams(){    QFile file( locate( "data","amarok/data/Cool-Streams.xml" ) );    if( !file.open( IO_ReadOnly ) )        return;    QTextStream stream( &file );    stream.setEncoding( QTextStream::UnicodeUTF8 );    QDomDocument d;    if( !d.setContent( stream.read() ) )    {        error() << "Bad Cool Streams XML file" << endl;        return;    }    m_coolStreams = new PlaylistCategory( m_streamsCategory, 0, i18n("Cool-Streams") );    m_coolStreams->setOpen( m_coolStreamsOpen );    m_coolStreams->setKept( false );    StreamEntry *last = 0;    QDomNode n = d.namedItem( "coolstreams" ).firstChild();    for( ; !n.isNull(); n = n.nextSibling() )    {        QDomElement e = n.toElement();        QString name = e.attribute( "name" );        e = n.namedItem( "url" ).toElement();        KURL url( e.text() );        last = new StreamEntry( m_coolStreams, last, url, name );        last->setKept( false );    }}void PlaylistBrowser::addStream( QListViewItem *parent ){    StreamEditor dialog( this, i18n( "Radio Stream" ), QString::null );    dialog.setCaption( i18n( "Add Radio Stream" ) );    if( !parent ) parent = static_cast<QListViewItem*>(m_streamsCategory);    if( dialog.exec() == QDialog::Accepted )    {        new StreamEntry( parent, 0, dialog.url(), dialog.name() );        parent->sortChildItems( 0, true );        parent->setOpen( true );        saveStreams();    }}void PlaylistBrowser::editStreamURL( StreamEntry *item, const bool readonly ){    StreamEditor dialog( this, item->title(), item->url().prettyURL(), readonly );    dialog.setCaption( readonly ? i18n( "Radio Stream" ) : i18n( "Edit Radio Stream" ) );    if( dialog.exec() == QDialog::Accepted )    {        item->setTitle( dialog.name() );        item->setURL( dialog.url() );        item->setText(0, dialog.name() );    }}void PlaylistBrowser::saveStreams(){    QFile file( streamBrowserCache() );    QDomDocument doc;    QDomElement streamB = m_streamsCategory->xml();    streamB.setAttribute( "product", "Amarok" );    streamB.setAttribute( "version", APP_VERSION );    streamB.setAttribute( "formatversion", "1.1" );    QDomNode streamsNode = doc.importNode( streamB, true );    doc.appendChild( streamsNode );    QString temp( doc.toString() );    // Only open the file after all data is ready. If it crashes, data is not lost!    if ( !file.open( IO_WriteOnly ) ) return;    QTextStream stream( &file );    stream.setEncoding( QTextStream::UnicodeUTF8 );    stream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";    stream << temp;}/** ************************************************************************* *  LAST.FM ************************************************************************* **/void PlaylistBrowser::loadLastfmStreams( const bool subscriber /*false*/ ){    QFile file( Amarok::saveLocation() + "lastfmbrowser_save.xml" );    QTextStream stream( &file );    stream.setEncoding( QTextStream::UnicodeUTF8 );    QDomDocument d;    QDomElement e;    QListViewItem *after = m_streamsCategory;    if( !file.open( IO_ReadOnly ) || !d.setContent( stream.read() ) )    { /*Couldn't open the file or it had invalid content, so let's create an empty element*/        m_lastfmCategory = new PlaylistCategory( m_listview, after , i18n("Last.fm Radio") );    }    else {        e = d.namedItem( "category" ).toElement();        m_lastfmCategory = new PlaylistCategory( m_listview, after, e );        m_lastfmCategory->setText( 0, i18n("Last.fm Radio") );    }    /// Load the default items    QStringList globaltags;    globaltags << "Alternative" << "Ambient" << "Chill Out" << "Classical" << "Dance"            << "Electronica" << "Favorites" << "Heavy Metal" << "Hip Hop" << "Indie Rock"            << "Industrial" << "Japanese" << "Pop" << "Psytrance" << "Rap" << "Rock"            << "Soundtrack" << "Techno" << "Trance";    PlaylistCategory *tagsFolder = new PlaylistCategory( m_lastfmCategory, 0, i18n("Global Tags") );    tagsFolder->setKept( false );    LastFmEntry *last = 0;    foreach( globaltags )    {        const KURL url( "lastfm://globaltags/" + *it );        last = new LastFmEntry( tagsFolder, last, url, *it );        last->setKept( false );    }    QString user = AmarokConfig::scrobblerUsername();    KURL url( QString("lastfm://user/%1/neighbours").arg( user ) );    last = new LastFmEntry( m_lastfmCategory, tagsFolder, url, i18n( "Neighbor Radio" ) );    last->setKept( false );    if( subscriber )    {        url = KURL::fromPathOrURL( QString("lastfm://user/%1/personal").arg( user ) );        last = new LastFmEntry( m_lastfmCategory, last, url, i18n( "Personal Radio" ) );        last->setKept( false );        url = KURL::fromPathOrURL( QString("lastfm://user/%1/loved").arg( user ) );        last = new LastFmEntry( m_lastfmCategory, last, url, i18n( "Loved Radio" ) );        last->setKept( false );    }}void PlaylistBrowser::addLastFmRadio( QListViewItem *parent ){    StreamEditor dialog( this, i18n( "Last.fm Radio" ), QString::null );    dialog.setCaption( i18n( "Add Last.fm Radio" ) );    if( !parent ) parent = static_cast<QListViewItem*>(m_lastfmCategory);    if( dialog.exec() == QDialog::Accepted )    {        new LastFmEntry( parent, 0, dialog.url(), dialog.name() );        parent->sortChildItems( 0, true );        parent->setOpen( true );        saveLastFm();    }}void PlaylistBrowser::addLastFmCustomRadio( QListViewItem *parent ){    QString token = LastFm::Controller::createCustomStation();    if( token.isEmpty() ) return;    token.replace( "/", "%252" );    const QString text = "lastfm://artistnames/" + token;    const KURL url( text );    QString name = LastFm::Controller::stationDescription( text );    name.replace( "%252", "/" );    new LastFmEntry( parent, 0, url, name );    saveLastFm();}void PlaylistBrowser::saveLastFm(){    if ( !m_lastfmCategory )        return;    QFile file( Amarok::saveLocation() + "lastfmbrowser_save.xml" );    QDomDocument doc;    QDomElement lastfmB = m_lastfmCategory->xml();    lastfmB.setAttribute( "product", "Amarok" );    lastfmB.setAttribute( "version", APP_VERSION );    lastfmB.setAttribute( "formatversion", "1.1" );    QDomNode lastfmNode = doc.importNode( lastfmB, true );    doc.appendChild( lastfmNode );    QString temp( doc.toString() );    // Only open the file after all data is ready. If it crashes, data is not lost!    if ( !file.open( IO_WriteOnly ) ) return;    QTextStream stream( &file );    stream.setEncoding( QTextStream::UnicodeUTF8 );    stream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";    stream << temp;}/** ************************************************************************* *  SMART-PLAYLISTS ************************************************************************* **/QString PlaylistBrowser::smartplaylistBrowserCache() const{    return Amarok::saveLocation() + "smartplaylistbrowser_save.xml";}void PlaylistBrowser::addSmartPlaylist( QListViewItem *parent ) //SLOT{    if( CollectionDB::instance()->isEmpty() || !m_smartCategory )        return;    if( !parent ) parent = static_cast<QListViewItem*>(m_smartCategory);    SmartPlaylistEditor dialog( i18n("Untitled"), this );    if( dialog.exec() == QDialog::Accepted ) {        PlaylistCategory *category = dynamic_cast<PlaylistCategory*>(parent);        for( QListViewItem *item = category->firstChild(); item; item = item->nextSibling() ) {            SmartPlaylist *sp = dynamic_cast<SmartPlaylist*>(item);            if ( sp && sp->title() == dialog.name() ) {                if( KMessageBox::warningContinueCancel(                    PlaylistWindow::self(),                    i18n( "A Smart Playlist named \"%1\" already exists. Do you want to overwrite it?" ).arg( dialog.name() ),                    i18n( "Overwrite Playlist?" ), i18n( "Overwrite" ) ) == KMessageBox::Continue )                {                    delete item;                    break;                }                else                    return;            }        }        new SmartPlaylist( parent, 0, dialog.result() );        parent->sortChildItems( 0, true );        parent->setOpen( true );        saveSmartPlaylists();    }}PlaylistCategory* PlaylistBrowser::loadSmartPlaylists(){    QFile file( smartplaylistBrowserCache() );    QTextStream stream( &file );    stream.setEncoding( QTextStream::UnicodeUTF8 );    QListViewItem *after = m_playlistCategory;    QDomDocument d;    QDomElement e;    if( !file.open( IO_ReadOnly ) || !d.setContent( stream.read() ) )    { /*Couldn't open the file or it had invalid content, so let's create an empty element*/        return new PlaylistCategory(m_listview, after, i18n("Smart Playlists") );    }    else {        e = d.namedItem( "category" ).toElement();        QString version = e.attribute("formatversion");        float fversion = e.attribute("formatversion").toFloat();        if ( version == "1.8" )        {            PlaylistCategory* p = new PlaylistCategory(m_listview, after, e );            p->setText( 0, i18n("Smart Playlists") );            return p;        }        else if ( fversion > 1.0f  )        {            PlaylistCategory* p = new PlaylistCategory(m_listview, after, e );            p->setText( 0, i18n("Smart Playlists") );            debug() << "loading old format smart playlists, converted to new format" << endl;            updateSmartPlaylists( p );            saveSmartPlaylists( p );            return p;        }        else { // Old unversioned format            PlaylistCategory* p = new PlaylistCategory(m_listview, after , i18n("Smart Playlists") );            QListViewItem *last = 0;            QDomNode n = d.namedItem( "smartplaylists" ).namedItem("smartplaylist");            for( ; !n.isNull();  n = n.nextSibling() ) {                last = new SmartPlaylist( p, last, n.toElement() );            }            return p;        }    }}void PlaylistBrowser::updateSmartPlaylists( QListViewItem *p ){    if( !p )        return;    for( QListViewItem *it =  p->firstChild();            it;            it = it->nextSibling() )    {        SmartPlaylist *spl = dynamic_cast<SmartPlaylist *>( it );        if( spl )        {            QDomElement xml = spl->xml();            QDomElement query = xml.namedItem( "sqlquery" ).toElement();            QDomElement expandBy = xml.namedItem( "expandby" ).toElement();            updateSmartPlaylistElement( query );            updateSmartPlaylistElement( expandBy );            spl->setXml( xml );        }        else            updateSmartPlaylists( it );    }}void PlaylistBrowser::updateSmartPlaylistElement( QDomElement& query ){    QRegExp limitSearch( "LIMIT.*(\\d+)\\s*,\\s*(\\d+)" );    QRegExp selectFromSearch( "SELECT[^'\"]*FROM" );    for(QDomNode child = query.firstChild();            !child.isNull();            child = child.nextSibling() )    {        if( child.isText() )        {            //HACK this should be refactored to just regenerate the SQL from the <criteria>'s            QDomText text = child.toText();            QString sql = text.data();            if ( selectFromSearch.search( sql ) != -1 )                sql.replace( selectFromSearch, "SELECT (*ListOfFields*) FROM" );            if ( limitSearch.search( sql ) != -1 )                sql.replace( limitSearch,                    QString( "LIMIT %1 OFFSET %2").arg( limitSearch.capturedTexts()[2].toInt() ).arg( limitSearch.capturedTexts()[1].toInt() ) );            text.setData( sql );            break;        }    }}void PlaylistBrowser::loadDefaultSmartPlaylists(){    DEBUG_BLOCK    const QStringList genres  = CollectionDB::instance()->query( "SELECT DISTINCT name FROM genre;" );    const QStringList artists = CollectionDB::instance()->artistList();    SmartPlaylist *item;    QueryBuilder qb;    SmartPlaylist *last = 0;    m_smartDefaults = new PlaylistCategory( m_smartCategory, 0, i18n("Collection") );    m_smartDefaults->setOpen( m_smartDefaultsOpen );    m_smartDefaults->setKept( false );    /********** All Collection **************/    qb.initSQLDrag();    qb.sortBy( QueryBuilder::tabArtist, QueryBuilder::valName );    qb.sortBy( QueryBuilder::tabAlbum, QueryBuilder::valName );    qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valTrack );

⌨️ 快捷键说明

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