1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-08 23:26:40 +02:00

Made tag reading safer and easier.

Show error message if we couldn't write tags to a file.
This commit is contained in:
Christian Muehlhaeuser
2014-11-19 16:12:11 +01:00
parent d5e1e0fc70
commit 83861f32cf
2 changed files with 79 additions and 65 deletions

View File

@@ -42,6 +42,7 @@
#include <QDialogButtonBox> #include <QDialogButtonBox>
#include <QFileInfo> #include <QFileInfo>
#include <QFile> #include <QFile>
#include <QMessageBox>
MetadataEditor::MetadataEditor( const Tomahawk::query_ptr& query, const Tomahawk::playlistinterface_ptr& plInterface, QWidget* parent ) MetadataEditor::MetadataEditor( const Tomahawk::query_ptr& query, const Tomahawk::playlistinterface_ptr& plInterface, QWidget* parent )
@@ -85,9 +86,12 @@ MetadataEditor::init( const Tomahawk::playlistinterface_ptr& plInterface )
void void
MetadataEditor::writeMetadata( bool closeDlg ) MetadataEditor::writeMetadata( bool closeDlg )
{ {
bool failed = false;
QFileInfo fi;
if ( m_result ) if ( m_result )
{ {
QFileInfo fi( QUrl( m_result->url() ).toLocalFile() ); fi = QFileInfo( QUrl( m_result->url() ).toLocalFile() );
bool changed = false; bool changed = false;
QByteArray fileName = QFile::encodeName( fi.canonicalFilePath() ); QByteArray fileName = QFile::encodeName( fi.canonicalFilePath() );
@@ -99,14 +103,17 @@ MetadataEditor::writeMetadata( bool closeDlg )
TagLib::FileRef f( encodedName ); TagLib::FileRef f( encodedName );
QSharedPointer<Tomahawk::Tag> tag( Tomahawk::Tag::fromFile( f ) ); QSharedPointer<Tomahawk::Tag> tag( Tomahawk::Tag::fromFile( f ) );
if ( !tag )
{
failed = true;
}
else
{
if ( title() != m_result->track()->track() ) if ( title() != m_result->track()->track() )
{ {
tDebug() << Q_FUNC_INFO << "Track changed" << title() << m_result->track(); tDebug() << Q_FUNC_INFO << "Track changed" << title() << m_result->track();
tag->setTitle( title() ); tag->setTitle( title() );
m_result->track()->setTrack( title() );
changed = true; changed = true;
} }
@@ -116,8 +123,6 @@ MetadataEditor::writeMetadata( bool closeDlg )
tDebug() << Q_FUNC_INFO << "Artist changed" << artist() << m_result->track()->artist(); tDebug() << Q_FUNC_INFO << "Artist changed" << artist() << m_result->track()->artist();
tag->setArtist( artist() ); tag->setArtist( artist() );
m_result->track()->setArtist( artist() );
changed = true; changed = true;
} }
@@ -128,8 +133,6 @@ MetadataEditor::writeMetadata( bool closeDlg )
if ( newAlbum->id() != m_result->track()->albumPtr()->id() ) if ( newAlbum->id() != m_result->track()->albumPtr()->id() )
{ {
tag->setAlbum( album() ); tag->setAlbum( album() );
m_result->track()->setAlbum( album() );
changed = true; changed = true;
} }
} }
@@ -138,7 +141,6 @@ MetadataEditor::writeMetadata( bool closeDlg )
if ( albumPos() != 0 && albumPos() != (int)m_result->track()->albumpos() ) if ( albumPos() != 0 && albumPos() != (int)m_result->track()->albumpos() )
{ {
tag->setTrack( albumPos() ); tag->setTrack( albumPos() );
m_result->track()->setAlbumPos( albumPos() );
tDebug() << Q_FUNC_INFO << "Albumpos changed"; tDebug() << Q_FUNC_INFO << "Albumpos changed";
changed = true; changed = true;
@@ -148,11 +150,6 @@ MetadataEditor::writeMetadata( bool closeDlg )
if ( year() != 1900 && year() != m_result->track()->year() ) if ( year() != 1900 && year() != m_result->track()->year() )
{ {
tag->setYear( year() ); tag->setYear( year() );
{
QVariantMap attr = m_result->track()->attributes();
attr[ "releaseyear" ] = year();
m_result->track()->setAttributes( attr );
}
tDebug() << Q_FUNC_INFO << "Year changed"; tDebug() << Q_FUNC_INFO << "Year changed";
changed = true; changed = true;
@@ -160,16 +157,34 @@ MetadataEditor::writeMetadata( bool closeDlg )
if ( changed ) if ( changed )
{ {
f.save(); if ( f.save() )
{
m_result->track()->setTrack( title() );
m_result->track()->setArtist( artist() );
m_result->track()->setAlbum( album() );
m_result->track()->setAlbumPos( albumPos() );
{
QVariantMap attr = m_result->track()->attributes();
attr[ "releaseyear" ] = year();
m_result->track()->setAttributes( attr );
}
m_editFiles.append( fileName ); m_editFiles.append( fileName );
m_result->doneEditing(); m_result->doneEditing();
tDebug() << Q_FUNC_INFO << m_result->toString() << m_result->track()->toString(); tDebug() << Q_FUNC_INFO << m_result->toString() << m_result->track()->toString();
} }
else
failed = true;
}
}
} }
if ( closeDlg ) if ( failed )
{
QMessageBox::critical( 0, tr( "Error" ), tr( "Could not write tags to file:\n%1" ).arg( fi.canonicalFilePath() ) );
}
else if ( closeDlg )
{ {
if ( m_editFiles.count() ) if ( m_editFiles.count() )
ScanManager::instance()->runFileScan( m_editFiles, false ); ScanManager::instance()->runFileScan( m_editFiles, false );

View File

@@ -408,6 +408,9 @@ MusicScanner::readTags( const QFileInfo& fi )
int bitrate = 0; int bitrate = 0;
int duration = 0; int duration = 0;
QSharedPointer<Tag> tag( Tag::fromFile( f ) ); QSharedPointer<Tag> tag( Tag::fromFile( f ) );
if ( !tag )
return QVariantMap();
if ( f.audioProperties() ) if ( f.audioProperties() )
{ {
TagLib::AudioProperties *properties = f.audioProperties(); TagLib::AudioProperties *properties = f.audioProperties();
@@ -415,18 +418,14 @@ MusicScanner::readTags( const QFileInfo& fi )
bitrate = properties->bitrate(); bitrate = properties->bitrate();
} }
QString artist, album, track; const QString artist = tag->artist().trimmed();
if ( tag ) const QString album = tag->album().trimmed();
{ const QString track = tag->title().trimmed();
artist = tag->artist().trimmed(); if ( artist.isEmpty() || track.isEmpty() )
album = tag->album().trimmed();
track = tag->title().trimmed();
}
if ( !tag || artist.isEmpty() || track.isEmpty() )
return QVariantMap(); return QVariantMap();
QString mimetype = TomahawkUtils::extensionToMimetype( suffix ); const QString mimetype = TomahawkUtils::extensionToMimetype( suffix );
QString url( "file://%1" ); const QString url( "file://%1" );
QVariantMap m; QVariantMap m;
m["url"] = url.arg( fi.canonicalFilePath() ); m["url"] = url.arg( fi.canonicalFilePath() );