1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2012, Christopher Reichert <creichert07@gmail.com>
4
* Copyright 2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
6
* Tomahawk is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Tomahawk is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
20
#include "MetadataEditor.h"
21
#include "ui_MetadataEditor.h"
23
#include <QtGui/QDialog>
24
#include <QtGui/QDialogButtonBox>
25
#include <QtCore/QFileInfo>
26
#include <QtCore/QFile>
33
#include "ScanManager.h"
34
#include "PlaylistInterface.h"
35
#include "AlbumPlaylistInterface.h"
37
#include "taglib/fileref.h"
38
#include "filemetadata/taghandlers/tag.h"
39
#include "utils/TomahawkUtils.h"
40
#include "utils/Closure.h"
41
#include "utils/Logger.h"
44
MetadataEditor::MetadataEditor( const Tomahawk::query_ptr& query, const Tomahawk::playlistinterface_ptr& interface, QWidget* parent )
53
MetadataEditor::MetadataEditor( const Tomahawk::result_ptr& result, const Tomahawk::playlistinterface_ptr& interface, QWidget* parent )
63
MetadataEditor::init( const Tomahawk::playlistinterface_ptr& interface )
65
ui = new Ui::MetadataEditor();
68
setAttribute( Qt::WA_DeleteOnClose );
70
m_interface = interface;
74
NewClosure( ui->buttonBox, SIGNAL( accepted() ), this, SLOT( writeMetadata( bool ) ), true )->setAutoDelete( false );
76
connect( ui->buttonBox, SIGNAL( rejected() ), SLOT( close() ) );
77
connect( ui->forwardPushButton, SIGNAL( clicked() ), SLOT( loadNextQuery() ) );
78
connect( ui->previousPushButton, SIGNAL( clicked() ), SLOT( loadPreviousQuery() ) );
83
MetadataEditor::writeMetadata( bool closeDlg )
87
QFileInfo fi( QUrl( m_result->url() ).toLocalFile() );
90
QByteArray fileName = QFile::encodeName( fi.canonicalFilePath() );
91
const char *encodedName = fileName.constData();
93
TagLib::FileRef f( encodedName );
94
Tomahawk::Tag* tag = Tomahawk::Tag::fromFile( f );
96
if ( title() != m_result->track() )
98
tDebug() << Q_FUNC_INFO << "Track changed" << title() << m_result->track();
100
tag->setTitle( title() );
101
m_result->setTrack( title() );
106
Tomahawk::artist_ptr newArtist = Tomahawk::Artist::get( artist(), true );
107
if ( newArtist != m_result->artist() )
109
tDebug() << Q_FUNC_INFO << "Artist changed" << artist() << m_result->artist();
111
tag->setArtist( artist() );
112
m_result->setArtist( newArtist );
117
Tomahawk::album_ptr newAlbum = Tomahawk::Album::get( newArtist, album(), true );
118
if ( newAlbum != m_result->album() )
120
tDebug() << Q_FUNC_INFO << "Album changed" << album() << newAlbum->id() << m_result->album()->name() << m_result->album()->id() << newAlbum.data() << m_result->album().data();
121
if ( newAlbum->id() != m_result->album()->id() )
123
tag->setAlbum( album() );
124
m_result->setAlbum( newAlbum );
132
// FIXME: Ugly workaround for the min value of 0
133
if ( albumPos() != 0 && albumPos() != (int)m_result->albumpos() )
135
tag->setTrack( albumPos() );
136
m_result->setAlbumPos( albumPos() );
138
tDebug() << Q_FUNC_INFO << "Albumpos changed";
142
// FIXME: Ugly workaround for the min value of 1900
143
if ( year() != 1900 && year() != m_result->year() )
145
tag->setYear( year() );
146
m_result->setYear( year() );
148
tDebug() << Q_FUNC_INFO << "Year changed";
156
m_editFiles.append( fileName );
157
m_result->doneEditing();
159
tDebug() << Q_FUNC_INFO << m_result->toString();
160
tDebug() << Q_FUNC_INFO << m_result->toQuery()->toString();
166
if ( m_editFiles.count() )
167
ScanManager::instance()->runFileScan( m_editFiles, false );
175
MetadataEditor::loadQuery( const Tomahawk::query_ptr& query )
177
if ( query.isNull() )
180
if ( query->numResults() )
182
loadResult( query->results().first() );
186
m_result = Tomahawk::result_ptr();
188
setEditable( false );
190
setTitle( query->displayQuery()->track() );
191
setArtist( query->displayQuery()->artist() );
192
setAlbum( query->displayQuery()->album() );
193
setAlbumPos( query->displayQuery()->albumpos() );
194
setDuration( query->displayQuery()->duration() );
198
setFileName( QString() );
201
setWindowTitle( query->track() );
205
m_index = m_interface->indexOfQuery( query );
214
MetadataEditor::loadResult( const Tomahawk::result_ptr& result )
216
if ( result.isNull() )
220
setEditable( result->collection() && result->collection()->source()->isLocal() );
222
setTitle( result->track() );
223
setArtist( result->artist()->name() );
224
setAlbum( result->album()->name() );
225
setAlbumPos( result->albumpos() );
226
setDuration( result->duration() );
227
setYear( result->year() );
228
setBitrate( result->bitrate() );
230
if ( result->collection() && result->collection()->source()->isLocal() )
232
QFileInfo fi( QUrl( m_result->url() ).toLocalFile() );
233
setFileName( fi.absoluteFilePath() );
234
setFileSize( TomahawkUtils::filesizeToString( fi.size() ) );
237
setWindowTitle( result->track() );
241
m_index = m_interface->indexOfResult( result );
250
MetadataEditor::enablePushButtons()
252
if ( m_interface->siblingIndex( 1, m_index ) > 0 )
253
ui->forwardPushButton->setEnabled( true );
255
ui->forwardPushButton->setEnabled( false );
257
if ( m_interface->siblingIndex( -1, m_index ) > 0 )
258
ui->previousPushButton->setEnabled( true );
260
ui->previousPushButton->setEnabled( false );
265
MetadataEditor::loadNextQuery()
269
if ( m_interface->siblingIndex( 1, m_index ) > 0 )
271
m_index = m_interface->siblingIndex( 1, m_index );
272
loadQuery( m_interface->queryAt( m_index ) );
278
MetadataEditor::loadPreviousQuery()
282
if ( m_interface->siblingIndex( -1, m_index ) > 0 )
284
m_index = m_interface->siblingIndex( -1, m_index );
285
loadQuery( m_interface->queryAt( m_index ) );
291
MetadataEditor::setTitle( const QString& title )
293
ui->titleLineEdit->setText( title );
298
MetadataEditor::setArtist( const QString& artist )
300
ui->artistLineEdit->setText( artist );
305
MetadataEditor::setAlbum( const QString& album )
307
ui->albumLineEdit->setText( album );
312
MetadataEditor::setAlbumPos( unsigned int num )
314
ui->albumPosSpinBox->setValue( num );
319
MetadataEditor::setDuration( unsigned int duration )
321
ui->durationLineEdit->setText( TomahawkUtils::timeToString( duration ) );
326
MetadataEditor::setYear( int year )
328
ui->yearSpinBox->setValue( year );
333
MetadataEditor::setBitrate( unsigned int bitrate )
335
ui->bitrateSpinBox->setValue( bitrate );
340
MetadataEditor::setFileName( const QString& fn )
342
ui->fileNameLineEdit->setText( fn );
347
MetadataEditor::setFileSize( const QString& size )
349
ui->fileSizeLineEdit->setText( size );
354
MetadataEditor::setEditable( bool editable )
356
ui->artistLineEdit->setReadOnly( !editable );
357
ui->albumLineEdit->setReadOnly( !editable );
358
ui->titleLineEdit->setReadOnly( !editable );
359
ui->albumPosSpinBox->setReadOnly( !editable );
360
ui->yearSpinBox->setReadOnly( !editable );
362
m_editable = editable;
367
MetadataEditor::setWindowTitle( const QString& title )
369
QDialog::setWindowTitle( title + " - " + tr( "Properties" ) );