add mp3 and ogg torrent url info to JamendoAlbum
[amarok.git] / src / tagdialog.cpp
blobb863aae847bf7effd7e09167ffd015e22459a8c9
1 // (c) 2004 Mark Kretschmann <markey@web.de>
2 // (c) 2004 Pierpaolo Di Panfilo <pippo_dp@libero.it>
3 // (c) 2005-2006 Alexandre Pereira de Oliveira <aleprj@gmail.com>
4 // See COPYING file for licensing information.
6 #include "tagdialog.h"
8 #include "amarok.h"
9 #include "amarokconfig.h"
10 #include "coverfetcher.h"
11 #include "debug.h"
12 #include "metabundle.h"
13 #include "querybuilder.h"
14 #include "statusbar.h" //for status messages
15 #include "tagguesser.h"
16 #include "trackpickerdialog.h"
17 #include "ui_tagguesserconfigdialog.h"
19 #include <KApplication>
20 #include <KComboBox>
21 #include <KCursor>
22 #include <KGlobal>
23 #include <KHTMLView>
24 #include <KIconLoader>
25 #include <KLineEdit>
26 #include <KMessageBox>
27 #include <KNumInput>
28 #include <KRun>
29 #include <KStandardDirs>
30 #include <KTabWidget>
31 #include <KTextEdit>
32 #include <KVBox>
34 #include <QCheckBox>
35 #include <qdom.h>
36 #include <QFile>
37 #include <QLabel>
38 #include <QLayout>
39 #include <QPair>
40 #include <QPushButton>
41 #include <QToolTip>
43 #include <tfile.h> //TagLib::File::isWritable
45 class TagDialogWriter : public ThreadManager::Job
47 public:
48 TagDialogWriter( const QMap<QString, MetaBundle> tagsToChange );
49 bool doJob();
50 void completeJob();
51 private:
52 QList<bool> m_failed;
53 QList<MetaBundle> m_tags;
54 bool m_updateView;
55 int m_successCount;
56 int m_failCount;
57 QStringList m_failedURLs;
60 TagDialog::TagDialog( const KUrl& url, QWidget* parent )
61 : TagDialogBase( parent )
62 , m_bundle( url, true )
63 , m_playlistItem( 0 )
64 , m_currentCover( 0 )
66 init();
70 TagDialog::TagDialog( const KUrl::List list, QWidget* parent )
71 : TagDialogBase( parent )
72 , m_bundle()
73 , m_playlistItem( 0 )
74 , m_urlList( list )
75 , m_currentCover( 0 )
77 init();
81 // TagDialog::TagDialog( const MetaBundle& mb, PlaylistItem* item, QWidget* parent )
82 // : TagDialogBase( parent )
83 // , m_bundle( mb )
84 // , m_playlistItem( item )
85 // , m_currentCover( 0 )
86 // {
87 // init();
88 // }
90 TagDialog::TagDialog( const Meta::TrackList &tracks, QWidget *parent )
91 :TagDialogBase( parent )
92 , m_currentCover()
93 , m_tracks( tracks )
94 , m_currentTrack( 0 )
96 init();
99 TagDialog::TagDialog( Meta::TrackPtr track, QWidget *parent )
100 :TagDialogBase( parent )
101 , m_currentCover()
102 , m_tracks()
103 , m_currentTrack( track )
105 m_tracks.append( track );
106 init();
110 TagDialog::~TagDialog()
112 DEBUG_BLOCK
114 Amarok::config( "TagDialog" ).writeEntry( "CurrentTab", kTabWidget->currentIndex() );
115 delete m_labelCloud;
118 void
119 TagDialog::setTab( int id )
121 kTabWidget->setCurrentIndex( id );
125 ////////////////////////////////////////////////////////////////////////////////
126 // PRIVATE SLOTS
127 ////////////////////////////////////////////////////////////////////////////////
129 void
130 TagDialog::cancelPressed() //SLOT
132 QApplication::restoreOverrideCursor(); // restore the cursor before closing the dialog
133 reject();
137 void
138 TagDialog::accept() //SLOT
140 pushButton_ok->setEnabled( false ); //visual feedback
141 saveTags();
143 QDialog::accept();
147 inline void
148 TagDialog::openPressed() //SLOT
150 Amarok::invokeBrowser( m_path );
154 inline void
155 TagDialog::previousTrack()
157 //PORT 2.0
158 // if( m_playlistItem )
159 // {
160 // if( !m_playlistItem->itemAbove() ) return;
162 // storeTags();
164 // m_playlistItem = static_cast<PlaylistItem *>( m_playlistItem->itemAbove() );
166 // loadTags( m_playlistItem->url() );
167 // }
168 // else
169 // {
170 storeTags( *m_currentURL );
172 if( m_currentURL != m_urlList.begin() )
173 --m_currentURL;
174 loadTags( *m_currentURL );
175 enableItems();
176 // }
177 readTags();
181 inline void
182 TagDialog::nextTrack()
184 //PORT 2.0
185 // if( m_playlistItem )
186 // {
187 // if( !m_playlistItem->itemBelow() ) return;
189 // storeTags();
191 // m_playlistItem = static_cast<PlaylistItem *>( m_playlistItem->itemBelow() );
193 // loadTags( m_playlistItem->url() );
194 // }
195 // else
196 // {
197 storeTags( *m_currentURL );
199 KUrl::List::iterator next = m_currentURL;
200 ++next;
201 if( next != m_urlList.end() )
202 ++m_currentURL;
203 loadTags( *m_currentURL );
204 enableItems();
205 // }
206 readTags();
209 inline void
210 TagDialog::perTrack()
212 m_perTrack = !m_perTrack;
213 if( m_perTrack )
215 // just switched to per track mode
216 applyToAllTracks();
217 setSingleTrackMode();
218 loadTags( *m_currentURL );
219 readTags();
221 else
223 storeTags( *m_currentURL );
224 setMultipleTracksMode();
225 readMultipleTracks();
228 enableItems();
232 void
233 TagDialog::enableItems()
235 checkBox_perTrack->setChecked( m_perTrack );
236 pushButton_previous->setEnabled( m_perTrack && m_currentURL != m_urlList.begin() );
237 KUrl::List::ConstIterator next = m_currentURL;
238 ++next;
239 pushButton_next->setEnabled( m_perTrack && next != m_urlList.end());
240 if( m_urlList.count() == 1 )
242 checkBox_perTrack->setEnabled( false );
244 else
246 checkBox_perTrack->setEnabled( true );
251 inline void
252 TagDialog::checkModified() //SLOT
254 pushButton_ok->setEnabled( hasChanged() || storedTags.count() > 0 || storedScores.count() > 0
255 || storedLyrics.count() > 0 || storedRatings.count() > 0 || newLabels.count() > 0 );
258 void
259 TagDialog::loadCover( const QString &artist, const QString &album )
261 if ( m_bundle.artist() != artist || m_bundle.album()!=album )
262 return;
264 // draw the album cover on the dialog
265 QString cover = CollectionDB::instance()->albumImage( m_bundle );
267 if( m_currentCover != cover )
269 pixmap_cover->setPixmap( QPixmap( cover, "PNG" ) );
270 m_currentCover = cover;
272 pixmap_cover->setInformation( m_bundle.artist(), m_bundle.album() );
273 const int s = AmarokConfig::coverPreviewSize();
274 pixmap_cover->setMinimumSize( s, s );
275 pixmap_cover->setMaximumSize( s, s );
279 void
280 TagDialog::setFileNameSchemes() //SLOT
282 KDialog *kDialog = new KDialog(this);
283 Ui::TagGuesserConfigDialog* dialog = new Ui::TagGuesserConfigDialog();
284 dialog->setupUi(kDialog);
285 kDialog->exec();
286 delete dialog;
290 void
291 TagDialog::guessFromFilename() //SLOT
293 int cur = 0;
295 TagGuesser guesser( m_bundle.url().path() );
296 if( !guesser.title().isNull() )
297 kLineEdit_title->setText( guesser.title() );
299 if( !guesser.artist().isNull() )
301 cur = kComboBox_artist->currentIndex();
302 kComboBox_artist->setItemText( cur, guesser.artist() );
305 if( !guesser.album().isNull() )
307 cur = kComboBox_album->currentIndex();
308 kComboBox_album->setItemText( cur, guesser.album() );
311 if( !guesser.track().isNull() )
312 qSpinBox_track->setValue( guesser.track().toInt() );
313 if( !guesser.comment().isNull() )
314 kTextEdit_comment->setText( guesser.comment() );
315 if( !guesser.year().isNull() )
316 qSpinBox_year->setValue( guesser.year().toInt() );
318 if( !guesser.composer().isNull() )
320 cur = kComboBox_composer->currentIndex();
321 kComboBox_composer->setItemText( cur, guesser.composer() );
324 if( !guesser.genre().isNull() )
326 cur = kComboBox_genre->currentIndex();
327 kComboBox_genre->setItemText( cur, guesser.genre() );
331 void
332 TagDialog::musicbrainzQuery() //SLOT
334 #if HAVE_TUNEPIMP
335 kDebug() ;
337 m_mbTrack = m_bundle.url();
338 KTRMLookup* ktrm = new KTRMLookup( m_mbTrack.path(), true );
339 connect( ktrm, SIGNAL( sigResult( KTRMResultList, QString ) ), SLOT( queryDone( KTRMResultList, QString ) ) );
340 connect( pushButton_cancel, SIGNAL( clicked() ), ktrm, SLOT( deleteLater() ) );
342 pushButton_musicbrainz->setEnabled( false );
343 pushButton_musicbrainz->setText( i18n( "Generating audio fingerprint..." ) );
344 QApplication::setOverrideCursor( Qt::BusyCursor );
345 #endif
348 void
349 TagDialog::queryDone( KTRMResultList results, QString error ) //SLOT
351 #if HAVE_TUNEPIMP
353 if ( !error.isEmpty() ) {
354 KMessageBox::sorry( this, i18n( "Tunepimp (MusicBrainz tagging library) returned the following error: \"%1\".", error) );
356 else {
357 if ( !results.isEmpty() )
359 TrackPickerDialog* t = new TrackPickerDialog( m_mbTrack.fileName(), results, this );
360 t->show();
361 connect( t, SIGNAL( finished() ), SLOT( resetMusicbrainz() ) ); // clear m_mbTrack
363 else {
364 KMessageBox::sorry( this, i18n( "The track was not found in the MusicBrainz database." ) );
365 resetMusicbrainz(); // clear m_mbTrack
369 QApplication::restoreOverrideCursor();
370 pushButton_musicbrainz->setEnabled( true );
371 pushButton_musicbrainz->setText( m_buttonMbText );
372 #else
373 Q_UNUSED(results);
374 Q_UNUSED(error);
375 #endif
378 void
379 TagDialog::fillSelected( KTRMResult selected ) //SLOT
381 #if HAVE_TUNEPIMP
382 kDebug() ;
385 if ( m_bundle.url() == m_mbTrack ) {
386 if ( !selected.title().isEmpty() ) kLineEdit_title->setText( selected.title() );
387 if ( !selected.artist().isEmpty() ) kComboBox_artist->setCurrentText( selected.artist() );
388 if ( !selected.album().isEmpty() ) kComboBox_album->setCurrentText( selected.album() );
389 if ( selected.track() != 0 ) qSpinBox_track->setValue( selected.track() );
390 if ( selected.year() != 0 ) qSpinBox_year->setValue( selected.year() );
391 } else {
392 MetaBundle mb;
393 mb.setPath( m_mbTrack.path() );
394 if ( !selected.title().isEmpty() ) mb.setTitle( selected.title() );
395 if ( !selected.artist().isEmpty() ) mb.setArtist( selected.artist() );
396 if ( !selected.album().isEmpty() ) mb.setAlbum( selected.album() );
397 if ( selected.track() != 0 ) mb.setTrack( selected.track() );
398 if ( selected.year() != 0 ) mb.setYear( selected.year() );
400 storedTags.replace( m_mbTrack.path(), mb );
402 #else
403 Q_UNUSED(selected);
404 #endif
407 void TagDialog::resetMusicbrainz() //SLOT
409 #if HAVE_TUNEPIMP
410 m_mbTrack = "";
411 #endif
414 ////////////////////////////////////////////////////////////////////////////////
415 // PRIVATE
416 ////////////////////////////////////////////////////////////////////////////////
418 void TagDialog::init()
420 DEBUG_BLOCK
423 // delete itself when closing
424 setAttribute( Qt::WA_DeleteOnClose );
426 KConfigGroup config = Amarok::config( "TagDialog" );
428 kTabWidget->addTab( summaryTab, i18n( "Summary" ) );
429 kTabWidget->addTab( tagsTab, i18n( "Tags" ) );
430 kTabWidget->addTab( lyricsTab, i18n( "Lyrics" ) );
431 kTabWidget->addTab( statisticsTab, i18n( "Statistics" ) );
432 kTabWidget->addTab( labelsTab, i18n( "Labels" ) );
433 kTabWidget->setCurrentIndex( config.readEntry( "CurrentTab", 0 ) );
435 int items = kComboBox_artist->count();
436 const QStringList artists = CollectionDB::instance()->artistList();
437 kComboBox_artist->insertItems( items, artists );
438 kComboBox_artist->completionObject()->insertItems( artists );
439 kComboBox_artist->completionObject()->setIgnoreCase( true );
440 kComboBox_artist->setCompletionMode( KGlobalSettings::CompletionPopup );
442 items = kComboBox_album->count();
443 const QStringList albums = CollectionDB::instance()->albumList();
444 kComboBox_album->insertItems( items, albums );
445 kComboBox_album->completionObject()->insertItems( albums );
446 kComboBox_album->completionObject()->setIgnoreCase( true );
447 kComboBox_album->setCompletionMode( KGlobalSettings::CompletionPopup );
449 items = kComboBox_artist->count();
450 const QStringList composers = CollectionDB::instance()->composerList();
451 kComboBox_composer->insertItems( items, composers );
452 kComboBox_composer->completionObject()->insertItems( composers );
453 kComboBox_composer->completionObject()->setIgnoreCase( true );
454 kComboBox_composer->setCompletionMode( KGlobalSettings::CompletionPopup );
456 items = kComboBox_artist->count();
457 kComboBox_rating->insertItems( items, MetaBundle::ratingList() );
459 // const QStringList genres = MetaBundle::genreList();
460 items = kComboBox_artist->count();
461 const QStringList genres = CollectionDB::instance()->genreList();
462 kComboBox_genre->insertItems( items, genres );
463 kComboBox_genre->completionObject()->insertItems( genres );
464 kComboBox_genre->completionObject()->setIgnoreCase( true );
466 const QStringList labels = CollectionDB::instance()->labelList();
467 //TODO: figure out a way to add auto-completion support to kTestEdit_selectedLabels
469 m_labelCloud = new KHTMLPart( labels_favouriteLabelsFrame );
470 //m_labelCloud = new HTMLView( labels_favouriteLabelsFrame );
471 //m_labelCloud->view()->setSizePolicy( QSizePolicy::Ignored, QSizePolicy::Ignored, false );
472 QSizePolicy policy(QSizePolicy::Ignored, QSizePolicy::Ignored);
473 m_labelCloud->view()->setSizePolicy(policy);
475 //m_labelCloud->view()->setVScrollBarMode( Q3ScrollView::AlwaysOff );
476 //m_labelCloud->view()->setHScrollBarMode( Q3ScrollView::AlwaysOff );
478 new QVBoxLayout( labels_favouriteLabelsFrame );
479 labels_favouriteLabelsFrame->layout()->addWidget( m_labelCloud->view() );
480 const QStringList favoriteLabels = CollectionDB::instance()->favoriteLabels();
481 QString html = generateHTML( favoriteLabels );
482 m_labelCloud->write( html );
483 connect( m_labelCloud->browserExtension(), SIGNAL( openUrlRequest( const KUrl &, const KParts::OpenUrlArguments&, const KParts::BrowserArguments& ) ),
484 this, SLOT( openUrlRequest( const KUrl & ) ) );
486 // looks better to have a blank label than 0, we can't do this in
487 // the UI file due to bug in Designer
488 qSpinBox_track->setSpecialValueText( " " );
489 qSpinBox_year->setSpecialValueText( " " );
490 qSpinBox_score->setSpecialValueText( " " );
491 qSpinBox_discNumber->setSpecialValueText( " " );
493 if( !AmarokConfig::useRatings() )
495 kComboBox_rating->hide();
496 ratingLabel->hide();
498 if( !AmarokConfig::useScores() )
500 qSpinBox_score->hide();
501 scoreLabel->hide();
504 //HACK due to deficiency in Qt that will be addressed in version 4
505 // QSpinBox doesn't emit valueChanged if you edit the value with
506 // the lineEdit until you change the keyboard focus
507 connect( qSpinBox_year, SIGNAL(valueChanged( int )), SLOT(checkModified()) );
508 connect( qSpinBox_track, SIGNAL(valueChanged( int )), SLOT(checkModified()) );
509 connect( qSpinBox_score, SIGNAL(valueChanged( int )), SLOT(checkModified()) );
510 connect( qSpinBox_discNumber, SIGNAL(valueChanged( int )), SLOT(checkModified()) );
512 // Connects for modification check
513 connect( kLineEdit_title, SIGNAL(textChanged( const QString& )), SLOT(checkModified()) );
514 connect( kComboBox_composer,SIGNAL(activated( int )), SLOT(checkModified()) );
515 connect( kComboBox_composer,SIGNAL(textChanged( const QString& )), SLOT(checkModified()) );
516 connect( kComboBox_artist, SIGNAL(activated( int )), SLOT(checkModified()) );
517 connect( kComboBox_artist, SIGNAL(textChanged( const QString& )), SLOT(checkModified()) );
518 connect( kComboBox_album, SIGNAL(activated( int )), SLOT(checkModified()) );
519 connect( kComboBox_album, SIGNAL(textChanged( const QString& )), SLOT(checkModified()) );
520 connect( kComboBox_genre, SIGNAL(activated( int )), SLOT(checkModified()) );
521 connect( kComboBox_genre, SIGNAL(textChanged( const QString& )), SLOT(checkModified()) );
522 connect( kComboBox_rating, SIGNAL(activated( int )), SLOT(checkModified()) );
523 connect( kComboBox_rating, SIGNAL(textChanged( const QString& )), SLOT(checkModified()) );
524 connect( qSpinBox_track, SIGNAL(valueChanged( int )), SLOT(checkModified()) );
525 connect( qSpinBox_year, SIGNAL(valueChanged( int )), SLOT(checkModified()) );
526 connect( qSpinBox_score, SIGNAL(valueChanged( int )), SLOT(checkModified()) );
527 connect( kTextEdit_comment, SIGNAL(textChanged()), SLOT(checkModified()) );
528 connect( kTextEdit_lyrics, SIGNAL(textChanged()), SLOT(checkModified()) );
529 connect( kTextEdit_selectedLabels, SIGNAL(textChanged()), SLOT(checkModified()) );
531 // Remember original button text
532 m_buttonMbText = pushButton_musicbrainz->text();
534 connect( pushButton_cancel, SIGNAL(clicked()), SLOT(cancelPressed()) );
535 connect( pushButton_ok, SIGNAL(clicked()), SLOT(accept()) );
536 connect( pushButton_open, SIGNAL(clicked()), SLOT(openPressed()) );
537 connect( pushButton_previous, SIGNAL(clicked()), SLOT(previousTrack()) );
538 connect( pushButton_next, SIGNAL(clicked()), SLOT(nextTrack()) );
539 connect( checkBox_perTrack, SIGNAL(clicked()), SLOT(perTrack()) );
541 // set an icon for the open-in-konqui button
542 pushButton_open->setIcon( KIcon( Amarok::icon( "files" ) ) );
544 //Update lyrics on Context Browser
545 // connect( this, SIGNAL(lyricsChanged( const QString& )), ContextBrowser::instance(), SLOT( lyricsChanged( const QString& ) ) );
547 //Update cover
548 connect( CollectionDB::instance(), SIGNAL( coverFetched( const QString&, const QString& ) ),
549 this, SLOT( loadCover( const QString&, const QString& ) ) );
550 connect( CollectionDB::instance(), SIGNAL( coverChanged( const QString&, const QString& ) ),
551 this, SLOT( loadCover( const QString&, const QString& ) ) );
555 #if HAVE_TUNEPIMP
556 connect( pushButton_musicbrainz, SIGNAL(clicked()), SLOT(musicbrainzQuery()) );
557 #else
558 pushButton_musicbrainz->setToolTip( i18n("Please install MusicBrainz to enable this functionality") );
559 #endif
561 connect( pushButton_guessTags, SIGNAL(clicked()), SLOT( guessFromFilename() ) );
562 connect( pushButton_setFilenameSchemes, SIGNAL(clicked()), SLOT( setFileNameSchemes() ) );
564 if( m_tracks.count() ) { //editing multiple tracks
565 m_perTrack = false;
566 setMultipleTracksMode();
567 readMultipleTracks();
569 checkBox_perTrack->setChecked( m_perTrack );
570 if( m_urlList.count() == 1 )
572 checkBox_perTrack->setEnabled( false );
573 pushButton_previous->setEnabled( false );
574 pushButton_next->setEnabled( false );
576 else
578 checkBox_perTrack->setEnabled( true );
579 pushButton_previous->setEnabled( m_perTrack );
580 pushButton_next->setEnabled( m_perTrack );
583 else
585 m_perTrack = true;
586 checkBox_perTrack->hide();
588 if( !m_playlistItem ) {
589 //We have already loaded the metadata (from the file) in the constructor
590 pushButton_previous->hide();
591 pushButton_next->hide();
593 else
595 //PORT 2.0
596 //Reload the metadata from the file, to be sure it's accurate
597 // loadTags( m_playlistItem->url() );
600 loadLyrics( m_bundle.url() );
601 loadLabels( m_bundle.url() );
602 readTags();
606 // make it as small as possible
607 resize( sizeHint().width(), minimumSize().height() );
612 inline const QString TagDialog::unknownSafe( QString s ) {
613 return ( s.isNull() || s.isEmpty() || s == "?" || s == "-" )
614 ? i18n ( "Unknown" )
615 : s;
618 const QStringList TagDialog::statisticsData() {
620 QStringList data, values;
621 const uint artist_id = CollectionDB::instance()->artistID( m_bundle.artist() );
622 const uint album_id = CollectionDB::instance()->albumID ( m_bundle.album() );
624 QueryBuilder qb;
626 if ( !m_bundle.artist().isEmpty() ) {
627 // tracks by this artist
628 qb.clear();
629 qb.addReturnFunctionValue( QueryBuilder::funcCount, QueryBuilder::tabSong, QueryBuilder::valTitle );
630 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valArtistID, QString::number( artist_id ) );
631 values = qb.run();
632 data += i18n( "Tracks by this Artist" );
633 data += values[0];
636 // albums by this artist
637 qb.clear();
638 qb.addReturnFunctionValue( QueryBuilder::funcCount, QueryBuilder::tabAlbum, QueryBuilder::valID );
639 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valArtistID, QString::number( artist_id ) );
640 qb.groupBy( QueryBuilder::tabSong, QueryBuilder::valAlbumID );
641 qb.excludeMatch( QueryBuilder::tabAlbum, i18n( "Unknown" ) );
642 qb.setOptions( QueryBuilder::optNoCompilations );
643 values = qb.run();
644 data += i18n( "Albums by this Artist" );
645 data += QString::number( values.count() );
648 // Favorite track by this artist
649 qb.clear();
650 qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valTitle );
651 qb.addReturnValue( QueryBuilder::tabStats, QueryBuilder::valScore );
652 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valArtistID, QString::number( artist_id ) );
653 qb.sortByFavorite();
654 qb.setLimit( 0, 1 );
655 values = qb.run();
656 data += i18n( "Favorite by this Artist" );
657 data += values.isEmpty() ? QString() : values[0];
659 if ( !m_bundle.album().isEmpty() ) {
660 // Favorite track on this album
661 qb.clear();
662 qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valTitle );
663 qb.addReturnValue( QueryBuilder::tabStats, QueryBuilder::valScore );
664 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valAlbumID, QString::number( album_id ) );
665 qb.sortByFavorite();
666 qb.setLimit( 0, 1 );
667 values = qb.run();
668 data += i18n( "Favorite on this Album" );
669 data += values.isEmpty() ? QString() : values[0];
672 // Related Artists
673 const QString sArtists = CollectionDB::instance()->similarArtists( m_bundle.artist(), 4 ).join(", ");
674 if ( !sArtists.isEmpty() ) {
675 data += i18n( "Related Artists" );
676 data += sArtists;
679 return data;
682 void TagDialog::readTags()
684 bool local = m_bundle.url().isLocalFile();
686 setWindowTitle( KDialog::makeStandardCaption( i18n("Track Information: %1 by %2",
687 m_bundle.title(), m_bundle.artist() ) ) );
689 QString niceTitle;
690 if ( m_bundle.album().isEmpty() ) {
691 if( !m_bundle.title().isEmpty() ) {
692 if( !m_bundle.artist().isEmpty() )
693 niceTitle = i18n( "<b>%1</b> by <b>%2</b>", m_bundle.title(), m_bundle.artist() );
694 else
695 niceTitle = QString( "<b>%1</b>").arg( m_bundle.title() );
697 else niceTitle = m_bundle.prettyTitle();
699 else {
700 niceTitle = i18n( "<b>%1</b> by <b>%2</b> on <b>%3</b>" ,
701 m_bundle.title(), m_bundle.artist(), m_bundle.album() );
703 trackArtistAlbumLabel->setText( niceTitle );
704 trackArtistAlbumLabel2->setText( niceTitle );
706 kLineEdit_title->setText( m_bundle.title() );
707 kComboBox_artist->setItemText( kComboBox_artist->currentIndex(), m_bundle.artist() );
708 kComboBox_album->setItemText( kComboBox_album->currentIndex(), m_bundle.album() );
709 kComboBox_genre->setItemText( kComboBox_genre->currentIndex(), m_bundle.genre() );
710 kComboBox_rating->setCurrentIndex( m_bundle.rating() ? m_bundle.rating() - 1 : 0 );
711 qSpinBox_track->setValue( m_bundle.track() );
712 kComboBox_composer->setItemText( kComboBox_composer->currentIndex(), m_bundle.composer() );
713 qSpinBox_year->setValue( m_bundle.year() );
714 qSpinBox_score->setValue( static_cast<int>(m_bundle.score()) );
715 qSpinBox_discNumber->setValue( m_bundle.discNumber() );
716 kTextEdit_comment->setText( m_bundle.comment() );
718 bool extended = m_bundle.hasExtendedMetaInformation();
719 qSpinBox_discNumber->setEnabled( extended );
720 kComboBox_composer->setEnabled( extended );
723 QString summaryText, statisticsText;
724 const QString body2cols = "<tr><td><nobr>%1</nobr></td><td><b>%2</b></td></tr>";
725 const QString body1col = "<tr><td colspan=2>%1</td></td></tr>";
726 const QString emptyLine = "<tr><td colspan=2></td></tr>";
728 summaryText = "<table width=100%><tr><td width=50%><table>";
729 summaryText += body2cols.arg( i18n("Length:"), unknownSafe( m_bundle.prettyLength() ) );
730 summaryText += body2cols.arg( i18n("Bitrate:"), unknownSafe( m_bundle.prettyBitrate() ) );
731 summaryText += body2cols.arg( i18n("Samplerate:"), unknownSafe( m_bundle.prettySampleRate() ) );
732 summaryText += body2cols.arg( i18n("Size:"), unknownSafe( m_bundle.prettyFilesize() ) );
733 summaryText += body2cols.arg( i18n("Format:"), unknownSafe( m_bundle.type() ) );
735 summaryText += "</table></td><td width=50%><table>";
736 if( AmarokConfig::useScores() )
737 summaryText += body2cols.arg( i18n("Score:"), QString::number( static_cast<int>( m_bundle.score() ) ) );
738 if( AmarokConfig::useRatings() )
739 summaryText += body2cols.arg( i18n("Rating:"), m_bundle.prettyRating() );
741 summaryText += body2cols.arg( i18n("Playcount:"), QString::number( m_bundle.playCount() ) );
742 summaryText += body2cols.arg( i18n("First Played:"),
743 m_bundle.playCount() ? KGlobal::locale()->formatDate( CollectionDB::instance()->getFirstPlay( m_bundle.url().path() ).date() , KLocale::ShortDate ) : i18n("Never") );
744 summaryText += body2cols.arg( i18nc("a single item (singular)", "Last Played:"),
745 m_bundle.playCount() ? KGlobal::locale()->formatDate( CollectionDB::instance()->getLastPlay( m_bundle.url().path() ).date() , KLocale::ShortDate ) : i18n("Never") );
747 summaryText += "</table></td></tr></table>";
748 summaryLabel->setText( summaryText );
750 statisticsText = "<table>";
752 QStringList sData = statisticsData();
753 for ( int i = 0; i < sData.count(); i+=2 ) {
754 statisticsText += body2cols.arg( sData[i], sData[i+1] );
757 statisticsText += "</table>";
759 statisticsLabel->setText( statisticsText );
761 kLineEdit_location->setText( local ? m_bundle.url().path() : m_bundle.url().url() );
763 //lyrics
764 kTextEdit_lyrics->setText( m_lyrics );
766 loadCover( m_bundle.artist(), m_bundle.album() );
769 // enable only for local files
770 kLineEdit_title->setReadOnly( !local );
771 kComboBox_artist->setEnabled( local );
772 kComboBox_album->setEnabled( local );
773 kComboBox_genre->setEnabled( local );
774 kComboBox_rating->setEnabled( local );
775 qSpinBox_track->setEnabled( local );
776 qSpinBox_year->setEnabled( local );
777 qSpinBox_score->setEnabled( local );
778 kTextEdit_comment->setEnabled( local );
779 kTextEdit_selectedLabels->setEnabled( local );
780 m_labelCloud->view()->setEnabled( local );
782 if( local )
784 pushButton_musicbrainz->show();
785 pushButton_guessTags->show();
786 pushButton_setFilenameSchemes->show();
788 else
790 pushButton_musicbrainz->hide();
791 pushButton_guessTags->hide();
792 pushButton_setFilenameSchemes->hide();
795 // If it's a local file, write the directory to m_path, else disable the "open in konqui" button
796 if ( local )
797 m_path = m_bundle.url().directory();
798 else
799 pushButton_open->setEnabled( false );
801 pushButton_ok->setEnabled( storedTags.count() > 0 || storedScores.count() > 0
802 || storedLyrics.count() > 0 || storedRatings.count() > 0
803 || newLabels.count() > 0 );
805 #if HAVE_TUNEPIMP
806 // Don't enable button if a query is in progress already (or if the file isn't local)
807 pushButton_musicbrainz->setEnabled( m_bundle.url().isLocalFile() && m_mbTrack.isEmpty() );
808 #else
809 pushButton_musicbrainz->setEnabled( false );
810 #endif
812 //PORT 2.0
813 // if( m_playlistItem ) {
814 // pushButton_previous->setEnabled( m_playlistItem->itemAbove() );
815 // pushButton_next->setEnabled( m_playlistItem->itemBelow() );
816 // }
820 void
821 TagDialog::setMultipleTracksMode()
824 kTabWidget->setTabEnabled( kTabWidget->indexOf(summaryTab), false );
825 kTabWidget->setTabEnabled( kTabWidget->indexOf(lyricsTab), false );
827 kComboBox_artist->setItemText( kComboBox_artist->currentIndex(), "" );
828 kComboBox_album->setItemText( kComboBox_album->currentIndex(), "" );
829 kComboBox_genre->setItemText( kComboBox_genre->currentIndex(), "" );
830 kComboBox_composer->setItemText( kComboBox_composer->currentIndex(), "" );
831 kLineEdit_title->setText( "" );
832 kTextEdit_comment->setText( "" );
833 qSpinBox_track->setValue( qSpinBox_track->minimum() );
834 qSpinBox_discNumber->setValue( qSpinBox_discNumber->minimum() );
835 qSpinBox_year->setValue( qSpinBox_year->minimum() );
837 qSpinBox_score->setValue( qSpinBox_score->minimum() );
838 kComboBox_rating->setCurrentItem( 0 );
840 kLineEdit_title->setEnabled( false );
841 qSpinBox_track->setEnabled( false );
843 pushButton_musicbrainz->hide();
844 pushButton_guessTags->hide();
845 pushButton_setFilenameSchemes->hide();
847 locationLabel->hide();
848 kLineEdit_location->hide();
849 pushButton_open->hide();
850 pixmap_cover->hide();
853 void
854 TagDialog::setSingleTrackMode()
857 kTabWidget->setTabEnabled( kTabWidget->indexOf(summaryTab), true );
858 kTabWidget->setTabEnabled( kTabWidget->indexOf(lyricsTab), true );
860 kLineEdit_title->setEnabled( true );
861 qSpinBox_track->setEnabled( true );
863 pushButton_musicbrainz->show();
864 pushButton_guessTags->show();
865 pushButton_setFilenameSchemes->show();
867 locationLabel->show();
868 kLineEdit_location->show();
869 pushButton_open->show();
870 pixmap_cover->show();
874 void
875 TagDialog::readMultipleTracks()
878 setWindowTitle( KDialog::makeStandardCaption( i18np("1 Track", "Information for %1 Tracks", m_urlList.count()) ) );
880 //Check which fields are the same for all selected tracks
881 const KUrl::List::ConstIterator end = m_urlList.end();
882 KUrl::List::ConstIterator it = m_urlList.begin();
884 m_bundle = MetaBundle();
886 MetaBundle first = bundleForURL( *it );
888 bool artist=true, album=true, genre=true, comment=true, year=true,
889 score=true, rating=true, composer=true, discNumber=true;
890 int songCount=0, ratingCount=0, ratingSum=0, scoreCount=0;
891 float scoreSum = 0.f;
892 for ( ; it != end; ++it ) {
893 MetaBundle mb = bundleForURL( *it );
894 songCount++;
895 if ( mb.rating() ) {
896 ratingCount++;
897 ratingSum+=mb.rating();
899 if ( mb.score() > 0.f ) {
900 scoreCount++;
901 scoreSum+=mb.score();
904 if( !mb.url().isLocalFile() ) {
905 // If we have a non local file, don't even lose more time comparing
906 artist = album = genre = comment = year = false;
907 score = rating = composer = discNumber = false;
908 continue;
910 if ( artist && mb.artist()!=first.artist() )
911 artist=false;
912 if ( album && mb.album()!=first.album() )
913 album=false;
914 if ( genre && mb.genre()!=first.genre() )
915 genre=false;
916 if ( comment && mb.comment()!=first.comment() )
917 comment=false;
918 if ( year && mb.year()!=first.year() )
919 year=false;
920 if ( composer && mb.composer()!=first.composer() )
921 composer=false;
922 if ( discNumber && mb.discNumber()!=first.discNumber() )
923 discNumber=false;
924 if ( score && mb.score()!=first.score() )
925 score = false;
926 if ( rating && mb.rating()!=first.rating() )
927 rating = false;
929 // Set them in the dialog and in m_bundle ( so we don't break hasChanged() )
930 int cur_item;
931 if (artist) {
932 cur_item = kComboBox_artist->currentIndex();
933 m_bundle.setArtist( first.artist() );
934 kComboBox_artist->setItemText( cur_item, first.artist() );
936 if (album) {
937 cur_item = kComboBox_album->currentIndex();
938 m_bundle.setAlbum( first.album() );
939 kComboBox_album->setItemText( cur_item, first.album() );
941 if (genre) {
942 cur_item = kComboBox_genre->currentIndex();
943 m_bundle.setGenre( first.genre() );
944 kComboBox_genre->setItemText( cur_item, first.genre() );
946 if (comment) {
947 m_bundle.setComment( first.comment() );
948 kTextEdit_comment->setText( first.comment() );
950 if (composer) {
951 cur_item = kComboBox_composer->currentIndex();
952 m_bundle.setComposer( first.composer() );
953 kComboBox_composer->setItemText( cur_item, first.composer() );
955 if (year) {
956 m_bundle.setYear( first.year() );
957 qSpinBox_year->setValue( first.year() );
959 if (discNumber) {
960 m_bundle.setDiscNumber( first.discNumber() );
961 qSpinBox_discNumber->setValue( first.discNumber() );
963 if (score) {
964 m_bundle.setScore( first.score() );
965 qSpinBox_score->setValue( static_cast<int>( first.score() ) );
967 if (rating) {
968 m_bundle.setRating( first.rating() );
969 kComboBox_rating->setCurrentIndex( first.rating() ? first.rating() - 1 : 0 );
972 m_currentURL = m_urlList.begin();
974 trackArtistAlbumLabel2->setText( i18np( "Editing 1 file", "Editing %1 files", songCount ) );
976 const QString body = "<tr><td><nobr>%1:</nobr></td><td><b>%2</b></td></tr>";
977 QString statisticsText = "<table>";
979 if( AmarokConfig::useRatings() ) {
980 statisticsText += body.arg( i18n( "Rated Songs:" ) , QString::number( ratingCount ) );
981 if ( ratingCount )
982 statisticsText += body.arg( i18n( "Average Rating:" ) , QString::number( (float)ratingSum / (float)ratingCount/2.0, 'f', 1 ) );
985 if( AmarokConfig::useRatings() ) {
986 statisticsText += body.arg( i18n( "Scored Songs:" ) , QString::number( scoreCount ) );
987 if ( scoreCount )
988 statisticsText += body.arg( i18n( "Average Score:" ) , QString::number( scoreSum / scoreCount, 'f', 1 ) );
992 statisticsText += "</table>";
994 statisticsLabel->setText( statisticsText );
996 QStringList commonLabels = getCommonLabels();
997 QString text;
998 oldForeach ( commonLabels )
1000 if ( !text.isEmpty() )
1001 text.append( ", " );
1002 text.append( *it );
1004 kTextEdit_selectedLabels->setText( text );
1005 m_commaSeparatedLabels = text;
1007 // This will reset a wrongly enabled Ok button
1008 checkModified();
1011 QStringList
1012 TagDialog::getCommonLabels()
1014 DEBUG_BLOCK
1015 QMap<QString, int> counterMap;
1016 const KUrl::List::ConstIterator end = m_urlList.end();
1017 KUrl::List::ConstIterator iter = m_urlList.begin();
1018 for(; iter != end; ++iter )
1020 QStringList labels = labelsForURL( *iter );
1021 oldForeach( labels )
1023 if ( counterMap.contains( *it ) )
1024 counterMap[ *it ] = counterMap[ *it ] +1;
1025 else
1026 counterMap[ *it ] = 1;
1029 int n = m_urlList.count();
1030 QStringList result;
1031 QMap<QString, int>::ConstIterator counterEnd( counterMap.end() );
1032 for(QMap<QString, int>::ConstIterator it = counterMap.begin(); it != counterEnd; ++it )
1034 if ( it.value() == n )
1035 result.append( it.key() );
1037 return result;
1040 inline bool
1041 equalString( const QString &a, const QString &b )
1043 return (a.isEmpty() && b.isEmpty()) ? true : a == b;
1046 bool
1047 TagDialog::hasChanged()
1049 return changes();
1053 TagDialog::changes()
1055 int result=TagDialog::NOCHANGE;
1056 bool modified = false;
1057 modified |= !equalString( kComboBox_artist->lineEdit()->text(), m_bundle.artist() );
1058 modified |= !equalString( kComboBox_album->lineEdit()->text(), m_bundle.album() );
1059 modified |= !equalString( kComboBox_genre->lineEdit()->text(), m_bundle.genre() );
1060 modified |= qSpinBox_year->value() != m_bundle.year();
1061 modified |= qSpinBox_discNumber->value() != m_bundle.discNumber();
1062 modified |= !equalString( kComboBox_composer->lineEdit()->text(), m_bundle.composer() );
1064 modified |= !equalString( kTextEdit_comment->toPlainText(), m_bundle.comment() );
1066 if (!m_urlList.count() || m_perTrack) { //ignore these on MultipleTracksMode
1067 modified |= !equalString( kLineEdit_title->text(), m_bundle.title() );
1068 modified |= qSpinBox_track->value() != m_bundle.track();
1070 if (modified)
1071 result |= TagDialog::TAGSCHANGED;
1073 if (qSpinBox_score->value() != m_bundle.score())
1074 result |= TagDialog::SCORECHANGED;
1075 if (kComboBox_rating->currentIndex() != ( m_bundle.rating() ? m_bundle.rating() - 1 : 0 ) )
1076 result |= TagDialog::RATINGCHANGED;
1078 if (!m_urlList.count() || m_perTrack) { //ignore these on MultipleTracksMode
1079 if ( !equalString( kTextEdit_lyrics->toPlainText(), m_lyrics ) )
1080 result |= TagDialog::LYRICSCHANGED;
1083 if ( !equalString( kTextEdit_selectedLabels->toPlainText(), m_commaSeparatedLabels ) )
1084 result |= TagDialog::LABELSCHANGED;
1086 return result;
1089 void
1090 TagDialog::storeTags()
1092 storeTags( m_bundle.url() );
1095 void
1096 TagDialog::storeTags( const KUrl &kurl )
1098 int result = changes();
1099 QString url = kurl.path();
1100 if( result & TagDialog::TAGSCHANGED ) {
1101 MetaBundle mb( m_bundle );
1103 mb.setTitle( kLineEdit_title->text() );
1104 mb.setComposer( kComboBox_composer->currentText() );
1105 mb.setArtist( kComboBox_artist->currentText() );
1106 mb.setAlbum( kComboBox_album->currentText() );
1107 mb.setComment( kTextEdit_comment->toPlainText() );
1108 mb.setGenre( kComboBox_genre->currentText() );
1109 mb.setTrack( qSpinBox_track->value() );
1110 mb.setYear( qSpinBox_year->value() );
1111 mb.setDiscNumber( qSpinBox_discNumber->value() );
1112 mb.setLength( m_bundle.length() );
1113 mb.setBitrate( m_bundle.bitrate() );
1114 mb.setSampleRate( m_bundle.sampleRate() );
1115 storedTags.remove( url );
1116 storedTags.insert( url, mb );
1118 if( result & TagDialog::SCORECHANGED ) {
1119 storedScores.remove( url );
1120 storedScores.insert( url, qSpinBox_score->value() );
1123 if( result & TagDialog::RATINGCHANGED ) {
1124 storedRatings.remove( url );
1125 storedRatings.insert( url, kComboBox_rating->currentIndex() ? kComboBox_rating->currentIndex() + 1 : 0 );
1128 if( result & TagDialog::LYRICSCHANGED ) {
1129 if ( kTextEdit_lyrics->toPlainText().isEmpty() ) {
1130 storedLyrics.remove( url );
1131 storedLyrics.insert( url, QString() );
1133 else {
1134 QDomDocument doc;
1135 QDomElement e = doc.createElement( "lyrics" );
1136 e.setAttribute( "artist", kComboBox_artist->currentText() );
1137 e.setAttribute( "title", kLineEdit_title->text() );
1138 QDomText t = doc.createTextNode( kTextEdit_lyrics->toPlainText() );
1139 e.appendChild( t );
1140 doc.appendChild( e );
1141 storedLyrics.remove( url );
1142 storedLyrics.insert( url, doc.toString() );
1145 if( result & TagDialog::LABELSCHANGED ) {
1146 generateDeltaForLabelList( labelListFromText( kTextEdit_selectedLabels->toPlainText() ) );
1147 QStringList tmpLabels;
1148 if ( newLabels.find( url ) != newLabels.end() )
1149 tmpLabels = newLabels[ url ];
1150 else
1151 tmpLabels = originalLabels[ url ];
1152 //apply delta
1153 oldForeach( m_removedLabels )
1155 tmpLabels.removeAt( tmpLabels.indexOf(*it) );
1157 oldForeach( m_addedLabels )
1159 // this just feels dirty...
1160 if( tmpLabels.indexOf( *it ) == tmpLabels.indexOf( *tmpLabels.end() ) )
1161 tmpLabels.append( *it );
1163 newLabels.remove( url );
1164 newLabels.insert( url, tmpLabels );
1168 void
1169 TagDialog::storeTags( const KUrl &url, int changes, const MetaBundle &mb )
1171 if ( changes & TagDialog::TAGSCHANGED ) {
1172 storedTags.remove( url.path() );
1173 storedTags.insert( url.path(), mb );
1175 if ( changes & TagDialog::SCORECHANGED ) {
1176 storedScores.remove( url.path() );
1177 storedScores.insert( url.path(), mb.score() );
1179 if ( changes & TagDialog::RATINGCHANGED ) {
1180 storedRatings.remove( url.path() );
1181 storedRatings.insert( url.path(), mb.rating() );
1185 void
1186 TagDialog::storeLabels( const KUrl &url, const QStringList &labels )
1188 newLabels.remove( url.path() );
1189 newLabels.insert( url.path(), labels );
1193 void
1194 TagDialog::loadTags( const KUrl &url )
1196 m_bundle = bundleForURL( url );
1197 loadLyrics( url );
1198 loadLabels( url );
1201 void
1202 TagDialog::loadLyrics( const KUrl &url )
1204 QString xml = lyricsForURL(url.path() );
1206 QDomDocument doc;
1207 if( doc.setContent( xml ) )
1208 m_lyrics = doc.documentElement().text();
1209 else
1210 m_lyrics.clear();
1213 void
1214 TagDialog::loadLabels( const KUrl &url )
1216 DEBUG_BLOCK
1217 m_labels = labelsForURL( url );
1218 originalLabels[ url.path() ] = m_labels;
1219 QString text;
1220 oldForeach( m_labels )
1222 if ( !text.isEmpty() )
1223 text.append( ", " );
1224 text.append( *it );
1226 kTextEdit_selectedLabels->setText( text );
1227 m_commaSeparatedLabels = text;
1230 MetaBundle
1231 TagDialog::bundleForURL( const KUrl &url )
1233 if( storedTags.find( url.path() ) != storedTags.end() )
1234 return storedTags[ url.path() ];
1236 return MetaBundle( url, url.isLocalFile() );
1239 float
1240 TagDialog::scoreForURL( const KUrl &url )
1242 if( storedScores.find( url.path() ) != storedScores.end() )
1243 return storedScores[ url.path() ];
1245 return CollectionDB::instance()->getSongPercentage( url.path() );
1249 TagDialog::ratingForURL( const KUrl &url )
1251 if( storedRatings.find( url.path() ) != storedRatings.end() )
1252 return storedRatings[ url.path() ];
1254 return CollectionDB::instance()->getSongRating( url.path() );
1257 QString
1258 TagDialog::lyricsForURL( const KUrl &url )
1260 if( storedLyrics.find( url.path() ) != storedLyrics.end() )
1261 return storedLyrics[ url.path() ];
1263 return CollectionDB::instance()->getLyrics( url.path() );
1266 QStringList
1267 TagDialog::labelsForURL( const KUrl &url )
1269 if( newLabels.find( url.path() ) != newLabels.end() )
1270 return newLabels[ url.path() ];
1271 if( originalLabels.find( url.path() ) != originalLabels.end() )
1272 return originalLabels[ url.path() ];
1273 QStringList tmp = CollectionDB::instance()->getLabels( url.path(), CollectionDB::typeUser );
1274 originalLabels[ url.path() ] = tmp;
1275 return tmp;
1278 void
1279 TagDialog::saveTags()
1281 if( !m_perTrack )
1283 applyToAllTracks();
1285 else
1287 storeTags();
1290 QMap<QString, float>::ConstIterator endScore( storedScores.end() );
1291 for(QMap<QString, float>::ConstIterator it = storedScores.begin(); it != endScore; ++it ) {
1292 CollectionDB::instance()->setSongPercentage( it.key(), it.value() );
1294 QMap<QString, int>::ConstIterator endRating( storedRatings.end() );
1295 for(QMap<QString, int>::ConstIterator it = storedRatings.begin(); it != endRating; ++it ) {
1296 CollectionDB::instance()->setSongRating( it.key(), it.value() );
1298 QMap<QString, QString>::ConstIterator endLyrics( storedLyrics.end() );
1299 for(QMap<QString, QString>::ConstIterator it = storedLyrics.begin(); it != endLyrics; ++it ) {
1300 CollectionDB::instance()->setLyrics( it.key(), it.value(),
1301 CollectionDB::instance()->uniqueIdFromUrl( KUrl( it.key() ) ) );
1302 emit lyricsChanged( it.key() );
1304 QMap<QString, QStringList>::ConstIterator endLabels( newLabels.end() );
1305 for(QMap<QString, QStringList>::ConstIterator it = newLabels.begin(); it != endLabels; ++it ) {
1306 CollectionDB::instance()->setLabels( it.key(), it.value(),
1307 CollectionDB::instance()->uniqueIdFromUrl( KUrl( it.key() ) ), CollectionDB::typeUser );
1309 CollectionDB::instance()->cleanLabels();
1311 ThreadManager::instance()->queueJob( new TagDialogWriter( storedTags ) );
1315 void
1316 TagDialog::applyToAllTracks()
1318 generateDeltaForLabelList( labelListFromText( kTextEdit_selectedLabels->toPlainText() ) );
1320 const KUrl::List::ConstIterator end = m_urlList.end();
1321 for ( KUrl::List::ConstIterator it = m_urlList.begin(); it != end; ++it ) {
1323 /* we have to update the values if they changed, so:
1324 1) !kLineEdit_field->text().isEmpty() && kLineEdit_field->text() != mb.field
1325 i.e.: The user wrote something on the field, and it's different from
1326 what we have in the tag.
1327 2) !m_bundle.field().isEmpty() && kLineEdit_field->text().isEmpty()
1328 i.e.: The user was shown some value for the field (it was the same
1329 for all selected tracks), and he deliberately emptied it.
1330 TODO: All this mess is because the dialog uses "" to represent what the user
1331 doesn't want to change, maybe we can think of something better?
1334 MetaBundle mb = bundleForURL( *it );
1336 int changed = 0;
1337 if( !kComboBox_artist->currentText().isEmpty() && kComboBox_artist->currentText() != mb.artist() ||
1338 kComboBox_artist->currentText().isEmpty() && !m_bundle.artist().isEmpty() ) {
1339 mb.setArtist( kComboBox_artist->currentText() );
1340 changed |= TagDialog::TAGSCHANGED;
1343 if( !kComboBox_album->currentText().isEmpty() && kComboBox_album->currentText() != mb.album() ||
1344 kComboBox_album->currentText().isEmpty() && !m_bundle.album().isEmpty() ) {
1345 mb.setAlbum( kComboBox_album->currentText() );
1346 changed |= TagDialog::TAGSCHANGED;
1348 if( !kComboBox_genre->currentText().isEmpty() && kComboBox_genre->currentText() != mb.genre() ||
1349 kComboBox_genre->currentText().isEmpty() && !m_bundle.genre().isEmpty() ) {
1350 mb.setGenre( kComboBox_genre->currentText() );
1351 changed |= TagDialog::TAGSCHANGED;
1353 if( !kTextEdit_comment->toPlainText().isEmpty() && kTextEdit_comment->toPlainText() != mb.comment() ||
1354 kTextEdit_comment->toPlainText().isEmpty() && !m_bundle.comment().isEmpty() ) {
1355 mb.setComment( kTextEdit_comment->toPlainText() );
1356 changed |= TagDialog::TAGSCHANGED;
1358 if( !kComboBox_composer->currentText().isEmpty() && kComboBox_composer->currentText() != mb.composer() ||
1359 kComboBox_composer->currentText().isEmpty() && !m_bundle.composer().isEmpty() ) {
1360 mb.setComposer( kComboBox_composer->currentText() );
1361 changed |= TagDialog::TAGSCHANGED;
1364 if( qSpinBox_year->value() && qSpinBox_year->value() != mb.year() ||
1365 !qSpinBox_year->value() && m_bundle.year() ) {
1366 mb.setYear( qSpinBox_year->value() );
1367 changed |= TagDialog::TAGSCHANGED;
1369 if( qSpinBox_discNumber->value() && qSpinBox_discNumber->value() != mb.discNumber() ||
1370 !qSpinBox_discNumber->value() && m_bundle.discNumber() ) {
1371 mb.setDiscNumber( qSpinBox_discNumber->value() );
1372 changed |= TagDialog::TAGSCHANGED;
1375 if( qSpinBox_score->value() && qSpinBox_score->value() != mb.score() ||
1376 !qSpinBox_score->value() && m_bundle.score() )
1378 mb.setScore( qSpinBox_score->value() );
1379 changed |= TagDialog::SCORECHANGED;
1382 if( kComboBox_rating->currentIndex() && kComboBox_rating->currentIndex() != m_bundle.rating() - 1 ||
1383 !kComboBox_rating->currentIndex() && m_bundle.rating() )
1385 mb.setRating( kComboBox_rating->currentIndex() ? kComboBox_rating->currentIndex() + 1 : 0 );
1386 changed |= TagDialog::RATINGCHANGED;
1388 storeTags( *it, changed, mb );
1390 QStringList tmpLabels = labelsForURL( *it );
1391 //apply delta
1392 for( QStringList::Iterator iter = m_removedLabels.begin(); iter != m_removedLabels.end(); ++iter )
1394 tmpLabels.erase( iter );
1396 for( QStringList::Iterator iter = m_addedLabels.begin(); iter != m_addedLabels.end(); ++iter )
1398 if( tmpLabels.indexOf( *iter ) == tmpLabels.indexOf( *tmpLabels.end() ) )
1399 tmpLabels.append( *iter );
1401 storeLabels( *it, tmpLabels );
1405 QStringList
1406 TagDialog::labelListFromText( const QString &text )
1408 QStringList tmp = text.split(',', QString::SkipEmptyParts);
1409 //insert each string into a map to remove duplicates
1410 QMap<QString, int> map;
1411 oldForeach( tmp )
1413 QString tmpString = (*it).trimmed();
1414 if ( !tmpString.isEmpty() ) {
1415 map.remove( tmpString );
1416 map.insert( tmpString, 0 );
1419 QStringList result;
1420 QMap<QString, int>::ConstIterator endMap( map.end() );
1421 for(QMap<QString, int>::ConstIterator it = map.begin(); it != endMap; ++it ) {
1422 result.append( it.key() );
1424 return result;
1427 void
1428 TagDialog::generateDeltaForLabelList( const QStringList &list )
1430 m_addedLabels.clear();
1431 m_removedLabels.clear();
1432 oldForeach( list )
1434 if ( !m_labels.contains( *it ) )
1435 m_addedLabels.append( *it );
1437 oldForeach( m_labels )
1439 if ( !list.contains( *it ) )
1440 m_removedLabels.append( *it );
1442 m_labels = list;
1445 QString
1446 TagDialog::generateHTML( const QStringList &labels )
1448 //the first column of each row is the label name, the second the number of assigned songs
1449 //loop through it to find the highest number of songs, can be removed if somebody figures out a better sql query
1450 QMap<QString, QPair<QString, int> > mapping;
1451 QStringList sortedLabels;
1452 int max = 1;
1453 oldForeach( labels )
1455 QString label = *it;
1456 sortedLabels << label.toLower();
1457 ++it;
1458 int value = ( *it ).toInt();
1459 if ( value > max )
1460 max = value;
1461 mapping[label.toLower()] = QPair<QString, int>( label, value );
1463 sortedLabels.sort();
1464 QString html = "<html><body>";
1465 oldForeach( sortedLabels )
1467 QString key = *it;
1468 //generate a number in the range 1..10 based on how much the label is used
1469 int labelUse = ( mapping[key].second * 10 ) / max;
1470 if ( labelUse == 0 )
1471 labelUse = 1;
1472 html.append( QString( "<span class='label size%1'><a href=\"label:%2\">%3</a></span> " )
1473 .arg( QString::number( labelUse ), mapping[key].first, mapping[key].first ) );
1475 html.append( "</html></body>" );
1476 debug() << "Dumping HTML for label cloud: " << html;
1477 return html;
1480 void
1481 TagDialog::openUrlRequest(const KUrl &url ) //SLOT
1483 DEBUG_BLOCK
1484 if ( url.protocol() == "label" )
1486 QString text = kTextEdit_selectedLabels->toPlainText();
1487 QStringList currentLabels = labelListFromText( text );
1488 if ( currentLabels.contains( url.path() ) )
1489 return;
1490 if ( !text.isEmpty() )
1491 text.append( ", " );
1492 text.append( url.path() );
1493 kTextEdit_selectedLabels->setText( text );
1497 bool
1498 TagDialog::writeTag( MetaBundle &mb, bool updateCB )
1500 QByteArray path = QFile::encodeName( mb.url().path() );
1501 if ( !TagLib::File::isWritable( path ) ) {
1502 Amarok::StatusBar::instance()->longMessage( i18n(
1503 "The file %1 is not writable.", mb.url().fileName() ), KDE::StatusBar::Error );
1504 return false;
1507 //visual feedback
1508 QApplication::setOverrideCursor( Qt::WaitCursor );
1510 bool result = mb.save();
1511 mb.updateFilesize();
1513 if( result )
1514 //update the collection db
1515 CollectionDB::instance()->updateTags( mb.url().path(), mb, updateCB );
1517 QApplication::restoreOverrideCursor();
1519 return result;
1522 TagDialogWriter::TagDialogWriter( const QMap<QString, MetaBundle> tagsToChange )
1523 : ThreadManager::Job( "TagDialogWriter" ),
1524 m_successCount ( 0 ),
1525 m_failCount ( 0 )
1527 QApplication::setOverrideCursor( Qt::WaitCursor );
1528 QMap<QString, MetaBundle>::ConstIterator end = tagsToChange.end();
1529 for(QMap<QString, MetaBundle>::ConstIterator it = tagsToChange.begin(); it != end; ++it ) {
1530 MetaBundle mb = it.value();
1531 m_tags += mb;
1535 bool
1536 TagDialogWriter::doJob()
1538 for( int i = 0, size=m_tags.size(); i<size; ++i ) {
1539 QByteArray path = QFile::encodeName( m_tags[i].url().path() );
1540 if ( !TagLib::File::isWritable( path ) ) {
1541 Amarok::StatusBar::instance()->longMessageThreadSafe( i18n(
1542 "The file %1 is not writable.", m_tags[i].url().fileName() ), KDE::StatusBar::Error );
1543 m_failed += true;
1544 continue;
1547 bool result = m_tags[i].save();
1548 m_tags[i].updateFilesize();
1550 if( result )
1551 m_successCount++;
1552 else {
1553 m_failCount++;
1554 m_failedURLs += m_tags[i].prettyUrl();
1556 m_failed += !result;
1558 return true;
1561 void
1562 TagDialogWriter::completeJob()
1564 for( int i = 0, size=m_tags.size(); i<size; ++i ) {
1565 if ( !m_failed[i] ) {
1566 CollectionDB::instance()->updateTags( m_tags[i].url().path(), m_tags[i], false /* don't update browsers*/ );
1567 // PORT 2.0 Playlist::instance()->updateMetaData( m_tags[i] );
1570 QApplication::restoreOverrideCursor();
1571 //PORT 2.0 if ( m_successCount )
1572 //PORT 2.0 CollectionView::instance()->databaseChanged();
1573 if ( m_failCount )
1574 Amarok::StatusBar::instance()->longMessage( i18n(
1575 "Sorry, the tag for the following files could not be changed:\n", m_failedURLs.join( ";\n" ) ), KDE::StatusBar::Error );
1579 #include "tagdialog.moc"