Skip to content

Commit

Permalink
[musicdb] cosmetic: separate tag scanning out into a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
night199uk committed Apr 9, 2013
1 parent 8983723 commit 84506df
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 50 deletions.
111 changes: 61 additions & 50 deletions xbmc/music/infoscanner/MusicInfoScanner.cpp
Expand Up @@ -458,27 +458,17 @@ bool CMusicInfoScanner::DoScan(const CStdString& strDirectory)
return !m_bStop;
}

int CMusicInfoScanner::RetrieveMusicInfo(CFileItemList& items, const CStdString& strDirectory)
INFO_RET CMusicInfoScanner::ScanTags(const CFileItemList& items, CFileItemList& scannedItems)
{
MAPSONGS songsMap;

// get all information for all files in current directory from database, and remove them
if (m_musicDatabase.RemoveSongsFromPath(strDirectory, songsMap))
m_needsCleanup = true;

VECSONGS songsToAdd;

CStdStringArray regexps = g_advancedSettings.m_audioExcludeFromScanRegExps;

// for every file found, but skip folder
for (int i = 0; i < items.Size(); ++i)
{
CFileItemPtr pItem = items[i];
CStdString strExtension;
URIUtils::GetExtension(pItem->GetPath(), strExtension);

if (m_bStop)
return 0;
return INFO_CANCELLED;

// Discard all excluded files defined by m_musicExcludeRegExps
if (CUtil::ExcludeFileOrFolder(pItem->GetPath(), regexps))
Expand All @@ -488,11 +478,11 @@ int CMusicInfoScanner::RetrieveMusicInfo(CFileItemList& items, const CStdString&
if (!pItem->m_bIsFolder && !pItem->IsPlayList() && !pItem->IsPicture() && !pItem->IsLyrics() )
{
m_currentItem++;
// CLog::Log(LOGDEBUG, "%s - Reading tag for: %s", __FUNCTION__, pItem->GetPath().c_str());

CMusicInfoTag& tag = *pItem->GetMusicInfoTag();
if (!tag.Loaded() )
{ // read the tag from a file
if (!tag.Loaded())
{
// read the tag from a file
auto_ptr<IMusicInfoTagLoader> pLoader (CMusicInfoTagLoaderFactory::CreateLoader(pItem->GetPath()));
if (NULL != pLoader.get())
pLoader->Load(pItem->GetPath(), tag);
Expand All @@ -504,46 +494,67 @@ int CMusicInfoScanner::RetrieveMusicInfo(CFileItemList& items, const CStdString&
m_handle->SetPercentage(m_currentItem/(float)m_itemCount*100);

if (tag.Loaded())
{
CSong song(tag);
scannedItems.Add(pItem);
else
CLog::Log(LOGDEBUG, "%s - No tag found for: %s", __FUNCTION__, pItem->GetPath().c_str());
}
}
return INFO_ADDED;
}

// ensure our song has a valid filename or else it will assert in AddSong()
if (song.strFileName.IsEmpty())
{
// copy filename from path in case UPnP or other tag loaders didn't specify one (FIXME?)
song.strFileName = pItem->GetPath();
int CMusicInfoScanner::RetrieveMusicInfo(CFileItemList& items, const CStdString& strDirectory)
{
MAPSONGS songsMap;

// if we still don't have a valid filename, skip the song
if (song.strFileName.IsEmpty())
{
// this shouldn't ideally happen!
CLog::Log(LOGERROR, "Skipping song since it doesn't seem to have a filename");
continue;
}
}
// get all information for all files in current directory from database, and remove them
if (m_musicDatabase.RemoveSongsFromPath(strDirectory, songsMap))
m_needsCleanup = true;

song.iStartOffset = pItem->m_lStartOffset;
song.iEndOffset = pItem->m_lEndOffset;
song.strThumb = pItem->GetUserMusicThumb(true);

// grab info from the song
MAPSONGS::iterator it = songsMap.find(pItem->GetPath());
if (it != songsMap.end())
{ // keep the db-only fields intact on rescan...
song.iTimesPlayed = it->second.iTimesPlayed;
song.lastPlayed = it->second.lastPlayed;
song.iKaraokeNumber = it->second.iKaraokeNumber;

if (song.rating == '0') song.rating = it->second.rating;
if (song.strThumb.empty())
song.strThumb = it->second.strThumb;
}
songsToAdd.push_back(song);
// CLog::Log(LOGDEBUG, "%s - Tag loaded for: %s", __FUNCTION__, pItem->GetPath().c_str());
CFileItemList scannedItems;
if (ScanTags(items, scannedItems) == INFO_CANCELLED)
return 0;

VECSONGS songsToAdd;
for (int i = 0; i < scannedItems.Size(); ++i)
{
CFileItemPtr pItem = scannedItems[i];
CMusicInfoTag& tag = *pItem->GetMusicInfoTag();
CSong song(tag);

// ensure our song has a valid filename or else it will assert in AddSong()
if (song.strFileName.IsEmpty())
{
// copy filename from path in case UPnP or other tag loaders didn't specify one (FIXME?)
song.strFileName = pItem->GetPath();

// if we still don't have a valid filename, skip the song
if (song.strFileName.IsEmpty())
{
// this shouldn't ideally happen!
CLog::Log(LOGERROR, "Skipping song since it doesn't seem to have a filename");
continue;
}
else
CLog::Log(LOGDEBUG, "%s - No tag found for: %s", __FUNCTION__, pItem->GetPath().c_str());
}

song.iStartOffset = pItem->m_lStartOffset;
song.iEndOffset = pItem->m_lEndOffset;
song.strThumb = pItem->GetUserMusicThumb(true);

// grab info from the song
MAPSONGS::iterator it = songsMap.find(pItem->GetPath());
if (it != songsMap.end())
{
// keep the db-only fields intact on rescan...
song.iTimesPlayed = it->second.iTimesPlayed;
song.lastPlayed = it->second.lastPlayed;
song.iKaraokeNumber = it->second.iKaraokeNumber;

if (song.rating == '0')
song.rating = it->second.rating;
if (song.strThumb.empty())
song.strThumb = it->second.strThumb;
}
songsToAdd.push_back(song);
}

VECALBUMS albums;
Expand Down
21 changes: 21 additions & 0 deletions xbmc/music/infoscanner/MusicInfoScanner.h
Expand Up @@ -28,6 +28,18 @@ class CGUIDialogProgressBarHandle;

namespace MUSIC_INFO
{
/*! \brief return values from the information lookup functions
*/
enum INFO_RET
{
INFO_CANCELLED,
INFO_ERROR,
INFO_NOT_NEEDED,
INFO_HAVE_ALREADY,
INFO_NOT_FOUND,
INFO_ADDED
};

class CMusicInfoScanner : CThread, public IRunnable
{
public:
Expand Down Expand Up @@ -97,6 +109,15 @@ class CMusicInfoScanner : CThread, public IRunnable
protected:
virtual void Process();
int RetrieveMusicInfo(CFileItemList& items, const CStdString& strDirectory);

/*! \brief Scan in the ID3/Ogg/FLAC tags for a bunch of FileItems
Given a list of FileItems, scan in the tags for those FileItems
and populate a new FileItemList with the files that were successfully scanned.
Any files which couldn't be scanned (no/bad tags) are discarded in the process.
\param items [in] list of FileItems to scan
\param scannedItems [in] list to populate with the scannedItems
*/
INFO_RET ScanTags(const CFileItemList& items, CFileItemList& scannedItems);
int GetPathHash(const CFileItemList &items, CStdString &hash);
void GetAlbumArtwork(long id, const CAlbum &artist);

Expand Down

0 comments on commit 84506df

Please sign in to comment.