metabundle.cpp
来自「Amarok是一款在LINUX或其他类UNIX操作系统中运行的音频播放器软件。 」· C++ 代码 · 共 1,884 行 · 第 1/5 页
CPP
1,884 行
}boolMetaBundle::checkExists(){ m_exists = !isFile() || QFile::exists( url().path() ); return m_exists;}boolMetaBundle::operator==( const MetaBundle& bundle ) const{ return uniqueId() == bundle.uniqueId() && //first, since if using IDs will return faster artist() == bundle.artist() && albumArtist() == bundle.albumArtist() && title() == bundle.title() && composer() == bundle.composer() && album() == bundle.album() && year() == bundle.year() && comment() == bundle.comment() && genre() == bundle.genre() && track() == bundle.track() && discNumber() == bundle.discNumber() && bpm() == bundle.bpm() && length() == bundle.length() && bitrate() == bundle.bitrate() && sampleRate() == bundle.sampleRate(); // FIXME: check for size equality?}voidMetaBundle::clear(){ *this = MetaBundle();}voidMetaBundle::init( TagLib::AudioProperties *ap ){ if ( ap ) { m_bitrate = ap->bitrate(); m_length = ap->length(); m_sampleRate = ap->sampleRate(); } else m_bitrate = m_length = m_sampleRate = Undetermined;}voidMetaBundle::init( const KFileMetaInfo& info ){ if( info.isValid() && !info.isEmpty() ) { m_artist = info.item( "Artist" ).string(); m_album = info.item( "Album" ).string(); m_comment = info.item( "Comment" ).string(); m_genre = info.item( "Genre" ).string(); m_year = info.item( "Year" ).string().toInt(); m_track = info.item( "Track" ).string().toInt(); m_bitrate = info.item( "Bitrate" ).value().toInt(); m_length = info.item( "Length" ).value().toInt(); m_sampleRate = info.item( "Sample Rate" ).value().toInt(); // For title, check if it is valid. If not, use prettyTitle. // @see bug:83650 const KFileMetaInfoItem itemtitle = info.item( "Title" ); m_title = itemtitle.isValid() ? itemtitle.string() : prettyTitle( m_url.fileName() ); const KFileMetaInfoItem itemid = info.item( "Unique ID" ); m_uniqueId = itemid.isValid() ? itemid.string() : QString::null; // because whoever designed KMetaInfoItem is a donkey #define makeSane( x ) if( x == "---" ) x = null; QString null; makeSane( m_artist ); makeSane( m_album ); makeSane( m_comment ); makeSane( m_genre ); makeSane( m_title ); #undef makeSane m_isValidMedia = true; } else { m_bitrate = m_length = m_sampleRate = m_filesize = Undetermined; m_isValidMedia = false; }}voidMetaBundle::embeddedImages( MetaBundle::EmbeddedImageList& images ) const{ if ( isFile() ) { TagLib::FileRef fileref = TagLib::FileRef( QFile::encodeName( url().path() ), false ); if ( !fileref.isNull() ) { if ( TagLib::MPEG::File *file = dynamic_cast<TagLib::MPEG::File *>( fileref.file() ) ) { if ( file->ID3v2Tag() ) loadImagesFromTag( *file->ID3v2Tag(), images ); } else if ( TagLib::FLAC::File *file = dynamic_cast<TagLib::FLAC::File *>( fileref.file() ) ) { if ( file->ID3v2Tag() ) loadImagesFromTag( *file->ID3v2Tag(), images ); } else if ( TagLib::MP4::File *file = dynamic_cast<TagLib::MP4::File *>( fileref.file() ) ) { TagLib::MP4::Tag *mp4tag = dynamic_cast<TagLib::MP4::Tag *>( file->tag() ); if( mp4tag && mp4tag->cover().size() ) { images.push_back( EmbeddedImage( mp4tag->cover(), "" ) ); } } } }}voidMetaBundle::readTags( TagLib::AudioProperties::ReadStyle readStyle, EmbeddedImageList* images ){ if( !isFile() ) return; const QString path = url().path(); TagLib::FileRef fileref; TagLib::Tag *tag = 0; fileref = TagLib::FileRef( QFile::encodeName( path ), true, readStyle ); if( !fileref.isNull() ) { setUniqueId( readUniqueId( &fileref ) ); m_filesize = QFile( path ).size(); tag = fileref.tag(); if ( tag ) { #define strip( x ) TStringToQString( x ).stripWhiteSpace() setTitle( strip( tag->title() ) ); setArtist( strip( tag->artist() ) ); setAlbum( strip( tag->album() ) ); setComment( strip( tag->comment() ) ); setGenre( strip( tag->genre() ) ); setYear( tag->year() ); setTrack( tag->track() ); #undef strip m_isValidMedia = true; } /* As mpeg implementation on TagLib uses a Tag class that's not defined on the headers, we have to cast the files, not the tags! */ QString disc; QString compilation; if ( TagLib::MPEG::File *file = dynamic_cast<TagLib::MPEG::File *>( fileref.file() ) ) { m_type = mp3; if ( file->ID3v2Tag() ) { if ( !file->ID3v2Tag()->frameListMap()["TPOS"].isEmpty() ) disc = TStringToQString( file->ID3v2Tag()->frameListMap()["TPOS"].front()->toString() ).stripWhiteSpace(); if ( !file->ID3v2Tag()->frameListMap()["TBPM"].isEmpty() ) setBpm( TStringToQString( file->ID3v2Tag()->frameListMap()["TBPM"].front()->toString() ).stripWhiteSpace().toFloat() ); if ( !file->ID3v2Tag()->frameListMap()["TCOM"].isEmpty() ) setComposer( TStringToQString( file->ID3v2Tag()->frameListMap()["TCOM"].front()->toString() ).stripWhiteSpace() ); if ( !file->ID3v2Tag()->frameListMap()["TPE2"].isEmpty() ) // non-standard: Apple, Microsoft setAlbumArtist( TStringToQString( file->ID3v2Tag()->frameListMap()["TPE2"].front()->toString() ).stripWhiteSpace() ); if ( !file->ID3v2Tag()->frameListMap()["TCMP"].isEmpty() ) compilation = TStringToQString( file->ID3v2Tag()->frameListMap()["TCMP"].front()->toString() ).stripWhiteSpace(); if(images) { loadImagesFromTag( *file->ID3v2Tag(), *images ); } } } else if ( TagLib::Ogg::Vorbis::File *file = dynamic_cast<TagLib::Ogg::Vorbis::File *>( fileref.file() ) ) { m_type = ogg; if ( file->tag() ) { if ( !file->tag()->fieldListMap()[ "COMPOSER" ].isEmpty() ) setComposer( TStringToQString( file->tag()->fieldListMap()["COMPOSER"].front() ).stripWhiteSpace() ); if ( !file->tag()->fieldListMap()[ "BPM" ].isEmpty() ) setBpm( TStringToQString( file->tag()->fieldListMap()["BPM"].front() ).stripWhiteSpace().toFloat() ); if ( !file->tag()->fieldListMap()[ "DISCNUMBER" ].isEmpty() ) disc = TStringToQString( file->tag()->fieldListMap()["DISCNUMBER"].front() ).stripWhiteSpace(); if ( !file->tag()->fieldListMap()[ "COMPILATION" ].isEmpty() ) compilation = TStringToQString( file->tag()->fieldListMap()["COMPILATION"].front() ).stripWhiteSpace(); } } else if ( TagLib::FLAC::File *file = dynamic_cast<TagLib::FLAC::File *>( fileref.file() ) ) { m_type = flac; if ( file->xiphComment() ) { if ( !file->xiphComment()->fieldListMap()[ "COMPOSER" ].isEmpty() ) setComposer( TStringToQString( file->xiphComment()->fieldListMap()["COMPOSER"].front() ).stripWhiteSpace() ); if ( !file->xiphComment()->fieldListMap()[ "BPM" ].isEmpty() ) setBpm( TStringToQString( file->xiphComment()->fieldListMap()["BPM"].front() ).stripWhiteSpace().toFloat() ); if ( !file->xiphComment()->fieldListMap()[ "DISCNUMBER" ].isEmpty() ) disc = TStringToQString( file->xiphComment()->fieldListMap()["DISCNUMBER"].front() ).stripWhiteSpace(); if ( !file->xiphComment()->fieldListMap()[ "COMPILATION" ].isEmpty() ) compilation = TStringToQString( file->xiphComment()->fieldListMap()["COMPILATION"].front() ).stripWhiteSpace(); } if ( images && file->ID3v2Tag() ) { loadImagesFromTag( *file->ID3v2Tag(), *images ); } } else if ( TagLib::MP4::File *file = dynamic_cast<TagLib::MP4::File *>( fileref.file() ) ) { m_type = mp4; TagLib::MP4::Tag *mp4tag = dynamic_cast<TagLib::MP4::Tag *>( file->tag() ); if( mp4tag ) { setComposer( TStringToQString( mp4tag->composer() ) ); setBpm( QString::number( mp4tag->bpm() ).toFloat() ); disc = QString::number( mp4tag->disk() ); compilation = QString::number( mp4tag->compilation() ); if ( images && mp4tag->cover().size() ) { images->push_back( EmbeddedImage( mp4tag->cover(), "" ) ); } } } if ( !disc.isEmpty() ) { int i = disc.find ('/'); if ( i != -1 ) // disc.right( i ).toInt() is total number of discs, we don't use this at the moment setDiscNumber( disc.left( i ).toInt() ); else setDiscNumber( disc.toInt() ); } if ( compilation.isEmpty() ) { // well, it wasn't set, but if the artist is VA assume it's a compilation if ( artist().string() == i18n( "Various Artists" ) ) setCompilation( CompilationYes ); } else { int i = compilation.toInt(); if ( i == CompilationNo ) setCompilation( CompilationNo ); else if ( i == CompilationYes ) setCompilation( CompilationYes ); } init( fileref.audioProperties() ); } //FIXME disabled for beta4 as it's simpler to not got 100 bug reports //else if( KMimeType::findByUrl( m_url )->is( "audio" ) ) // init( KFileMetaInfo( m_url, QString::null, KFileMetaInfo::Everything ) );}void MetaBundle::updateFilesize(){ if( !isFile() ) { m_filesize = Undetermined; return; } const QString path = url().path(); m_filesize = QFile( path ).size();}float MetaBundle::score( bool ensureCached ) const{ if( m_score == Undetermined && !ensureCached ) //const_cast is ugly, but other option was mutable, and then we lose const correctness checking //everywhere else *const_cast<float*>(&m_score) = CollectionDB::instance()->getSongPercentage( m_url.path() ); return m_score;}int MetaBundle::rating( bool ensureCached ) const{ if( m_rating == Undetermined && !ensureCached ) *const_cast<int*>(&m_rating) = CollectionDB::instance()->getSongRating( m_url.path() ); return m_rating;}int MetaBundle::playCount( bool ensureCached ) const{ if( m_playCount == Undetermined && !ensureCached ) *const_cast<int*>(&m_playCount) = CollectionDB::instance()->getPlayCount( m_url.path() ); return m_playCount;}uint MetaBundle::lastPlay( bool ensureCached ) const{ if( (int)m_lastPlay == abs(Undetermined) && !ensureCached ) *const_cast<uint*>(&m_lastPlay) = CollectionDB::instance()->getLastPlay( m_url.path() ).toTime_t(); return m_lastPlay;}void MetaBundle::copyFrom( const MetaBundle &bundle ){ setTitle( bundle.title() ); setArtist( bundle.artist() ); setAlbumArtist( bundle.albumArtist() ); setComposer( bundle.composer() ); setAlbum( bundle.album() ); setYear( bundle.year() ); setDiscNumber( bundle.discNumber() ); setBpm( bundle.bpm() ); setComment( bundle.comment() ); setGenre( bundle.genre() ); setTrack( bundle.track() ); setLength( bundle.length() ); setBitrate( bundle.bitrate() ); setSampleRate( bundle.sampleRate() ); setScore( bundle.score() ); setRating( bundle.rating() ); setPlayCount( bundle.playCount() ); setLastPlay( bundle.lastPlay() ); setFileType( bundle.fileType() ); setFilesize( bundle.filesize() ); if( bundle.m_podcastBundle ) setPodcastBundle( *bundle.m_podcastBundle ); else { delete m_podcastBundle; m_podcastBundle = 0; } if( bundle.m_lastFmBundle ) setLastFmBundle( *bundle.m_lastFmBundle ); else { delete m_lastFmBundle; m_lastFmBundle = 0; }}void MetaBundle::copyFrom( const PodcastEpisodeBundle &peb ){ setPodcastBundle( peb ); setTitle( peb.title() ); setArtist( peb.author() ); PodcastChannelBundle pcb; if( CollectionDB::instance()->getPodcastChannelBundle( peb.parent(), &pcb ) ) { if( !pcb.title().isEmpty() ) setAlbum( pcb.title() ); } setGenre( QString ( "Podcast" ) );}void MetaBundle::setExactText( int column, const QString &newText ){ switch( column ) { case Title: setTitle( newText ); break; case Artist: setArtist( newText ); break; case AlbumArtist: setAlbumArtist( newText ); break; case Composer: setComposer( newText ); break; case Year: setYear( newText.toInt() ); break; case Album: setAlbum( newText ); break; case DiscNumber: setDiscNumber( newText.toInt() ); break; case Track: setTrack( newText.toInt() ); break; case Bpm: setBpm( newText.toFloat() ); break; case Genre: setGenre( newText ); break; case Comment: setComment( newText ); break; case Length: setLength( newText.toInt() ); break;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?