1 // (c) Pierpaolo Di Panfilo 2004
2 // (c) 2005 Isaiah Damron <xepo@trifault.net>
3 // See COPYING file for licensing information
5 #include "covermanager.h"
8 #include "amarokconfig.h"
9 #include "browserToolBar.h"
11 #include "querybuilder.h"
12 #include "config-amarok.h"
13 #include "pixmapviewer.h"
14 #include "playlist/PlaylistModel.h"
16 #include "TheInstances.h"
18 #include <k3listview.h>
19 #include <k3multipledrag.h>
20 #include <k3urldrag.h>
21 #include <KApplication>
24 #include <KFileDialog>
25 #include <KIconLoader>
26 #include <KIO/NetAccess>
29 #include <KMenu> //showCoverMenu()
30 #include <KMessageBox> //showCoverMenu()
31 #include <KPushButton>
32 #include <KSqueezedTextLabel> //status label
33 #include <KStandardDirs> //KGlobal::dirs()
38 #include <KWindowSystem>
40 #include <QDesktopWidget> //ctor: desktop size
42 #include <QFontMetrics> //paintItem()
46 #include <QObject> //used to delete all cover fetchers
47 #include <QPainter> //paintItem()
48 #include <QPalette> //paintItem()
51 #include <QProgressBar>
52 #include <q3progressdialog.h>
54 #include <QStringList>
56 #include <QTimer> //search filter timer
58 #include <Q3ValueList>
61 #include <QToolButton>
64 static QString artistToSelectInInitFunction
;
65 CoverManager
*CoverManager::s_instance
= 0;
67 class ArtistItem
: public K3ListViewItem
70 ArtistItem(Q3ListView
*view
, Q3ListViewItem
*item
, const QString
&text
)
71 : K3ListViewItem(view
, item
, text
) {}
73 int compare( Q3ListViewItem
* i
, int col
, bool ascending
) const
79 QString b
= i
->text(0);
81 if ( a
.startsWith( "the ", Qt::CaseInsensitive
) )
82 Amarok::manipulateThe( a
, true );
83 if ( b
.startsWith( "the ", Qt::CaseInsensitive
) )
84 Amarok::manipulateThe( b
, true );
86 return QString::localeAwareCompare( a
.toLower(), b
.toLower() );
90 CoverManager::CoverManager()
92 , m_timer( new QTimer( this ) ) //search filter timer
94 , m_fetchingCovers( 0 )
95 , m_coversFetched( 0 )
100 setObjectName( "TheCoverManager" );
104 // Sets caption and icon correctly (needed e.g. for GNOME)
105 kapp
->setTopWidget( this );
106 setWindowTitle( KDialog::makeStandardCaption( i18n("Cover Manager") ) );
107 setAttribute( Qt::WA_DeleteOnClose
);
108 setContentsMargins( 4, 4, 4, 4 );
111 m_artistView
= new K3ListView( this );
112 m_artistView
->addColumn(i18n( "Albums By" ));
113 m_artistView
->setFullWidth( true );
114 m_artistView
->setSorting( 0 );
115 m_artistView
->setMinimumWidth( 180 );
116 ArtistItem
*item
= 0;
118 //load artists from the collection db
119 const QStringList artists
= CollectionDB::instance()->artistList( false, false );
120 foreach( QString artist
, artists
)
122 item
= new ArtistItem( m_artistView
, item
, artist
);
123 item
->setPixmap( 0, SmallIcon( Amarok::icon( "artist" ) ) );
125 m_artistView
->sort();
127 m_artistView
->setSorting( -1 );
128 ArtistItem
*last
= static_cast<ArtistItem
*>(m_artistView
->lastItem());
129 item
= new ArtistItem( m_artistView
, 0, i18n( "All Albums" ) );
130 item
->setPixmap( 0, SmallIcon( Amarok::icon( "album" ) ) );
133 qb
.addReturnValue( QueryBuilder::tabAlbum
, QueryBuilder::valName
);
134 qb
.groupBy( QueryBuilder::tabAlbum
, QueryBuilder::valName
);
135 qb
.setOptions( QueryBuilder::optOnlyCompilations
);
137 if ( qb
.run().count() ) {
138 item
= new ArtistItem( m_artistView
, last
, i18n( "Various Artists" ) );
139 item
->setPixmap( 0, SmallIcon("personal") );
142 KVBox
*vbox
= new KVBox( this );
143 KHBox
*hbox
= new KHBox( vbox
);
145 vbox
->setSpacing( 4 );
146 hbox
->setSpacing( 4 );
148 { //<Search LineEdit>
149 KHBox
*searchBox
= new KHBox( hbox
);
150 KToolBar
* searchToolBar
= new Browser::ToolBar( searchBox
);
151 QToolButton
*button
= new QToolButton( searchToolBar
);
152 button
->setIcon( KIcon( "locationbar-erase") );
153 m_searchEdit
= new KLineEdit( searchToolBar
);
154 m_searchEdit
->setClickMessage( i18n( "Enter search terms here" ) );
155 m_searchEdit
->setFrame( QFrame::Sunken
);
157 m_searchEdit
->setSizePolicy(QSizePolicy::Expanding
,QSizePolicy::Minimum
);
158 connect( button
, SIGNAL(clicked()), m_searchEdit
, SLOT(clear()) );
160 button
->setToolTip( i18n( "Clear search field" ) );
161 m_searchEdit
->setToolTip( i18n( "Enter space-separated terms to search in the albums" ) );
163 hbox
->setStretchFactor( searchBox
, 1 );
164 } //</Search LineEdit>
167 m_viewMenu
= new KMenu( this );
168 m_selectAllAlbums
= m_viewMenu
->addAction( i18n("All Albums"), this, SLOT( slotShowAllAlbums() ) );
169 m_selectAlbumsWithCover
= m_viewMenu
->addAction( i18n("Albums With Cover"), this, SLOT( slotShowAlbumsWithCover() ) );
170 m_selectAlbumsWithoutCover
= m_viewMenu
->addAction( i18n("Albums Without Cover"), this, SLOT( slotShowAlbumsWithoutCover() ) );
172 QActionGroup
*viewGroup
= new QActionGroup( this );
173 viewGroup
->setExclusive( true );
174 viewGroup
->addAction( m_selectAllAlbums
);
175 viewGroup
->addAction( m_selectAlbumsWithCover
);
176 viewGroup
->addAction( m_selectAlbumsWithoutCover
);
177 m_selectAllAlbums
->setChecked( true );
179 // amazon locale menu
180 QString locale
= AmarokConfig::amazonLocale();
181 m_currentLocale
= CoverFetcher::localeStringToID( locale
);
184 QActionGroup
*localeGroup
= new QActionGroup( this );
185 localeGroup
->setExclusive( true );
187 m_amazonLocaleMenu
= new KMenu( this );
189 a
= m_amazonLocaleMenu
->addAction( i18n("International"), this, SLOT( slotSetLocaleIntl() ) );
190 if( m_currentLocale
== CoverFetcher::International
) a
->setChecked( true );
191 localeGroup
->addAction( a
);
193 a
= m_amazonLocaleMenu
->addAction( i18n("Canada"), this, SLOT( slotSetLocaleCa() ) );
194 if( m_currentLocale
== CoverFetcher::Canada
) a
->setChecked( true );
195 localeGroup
->addAction( a
);
197 a
= m_amazonLocaleMenu
->addAction( i18n("France"), this, SLOT( slotSetLocaleFr() ) );
198 if( m_currentLocale
== CoverFetcher::France
) a
->setChecked( true );
199 localeGroup
->addAction( a
);
201 a
= m_amazonLocaleMenu
->addAction( i18n("Germany"), this, SLOT( slotSetLocaleDe() ) );
202 if( m_currentLocale
== CoverFetcher::Germany
) a
->setChecked( true );
203 localeGroup
->addAction( a
);
205 a
= m_amazonLocaleMenu
->addAction( i18n("Japan"), this, SLOT( slotSetLocaleJp() ) );
206 if( m_currentLocale
== CoverFetcher::Japan
) a
->setChecked( true );
207 localeGroup
->addAction( a
);
209 a
= m_amazonLocaleMenu
->addAction( i18n("United Kingdom"), this, SLOT( slotSetLocaleUk() ) );
210 if( m_currentLocale
== CoverFetcher::UK
) a
->setChecked( true );
211 localeGroup
->addAction( a
);
213 KToolBar
* toolBar
= new KToolBar( hbox
);
214 toolBar
->setToolButtonStyle( Qt::ToolButtonTextBesideIcon
);
216 // toolBar->setFrameShape( QFrame::NoFrame );
218 QAction
* viewMenuAction
= new QAction( KIcon( "view_choose" ), i18n( "View" ), this );
219 viewMenuAction
->setMenu( m_viewMenu
);
220 toolBar
->addAction( viewMenuAction
);
223 QAction
* localeMenuAction
= new QAction( KIcon( "babelfish" ), i18n( "Amazon Locale" ), this );
224 localeMenuAction
->setMenu( m_amazonLocaleMenu
);
225 toolBar
->addAction( localeMenuAction
);
228 //fetch missing covers button
229 m_fetchButton
= new KPushButton( KGuiItem( i18n("Fetch Missing Covers"), Amarok::icon( "download" ) ), hbox
);
230 connect( m_fetchButton
, SIGNAL(clicked()), SLOT(fetchMissingCovers()) );
234 m_coverView
= new CoverView( vbox
);
237 KStatusBar
*m_statusBar
= new KStatusBar( vbox
);
239 m_statusLabel
= new KSqueezedTextLabel( m_statusBar
);
240 m_statusLabel
->setIndent( 3 );
241 m_progressBox
= new KHBox( m_statusBar
);
243 m_statusBar
->addWidget( m_statusLabel
, 4 );
244 m_statusBar
->addPermanentWidget( m_progressBox
, 1 );
246 KPushButton
*stopButton
= new KPushButton( KGuiItem(i18n("Abort"), "stop"), m_progressBox
);
247 connect( stopButton
, SIGNAL(clicked()), SLOT(stopFetching()) );
248 m_progress
= new QProgressBar( m_progressBox
);
249 m_progress
->setTextVisible( true );
251 const int h
= m_statusLabel
->height() + 3;
252 m_statusLabel
->setFixedHeight( h
);
253 m_progressBox
->setFixedHeight( h
);
254 m_progressBox
->hide();
257 // signals and slots connections
258 connect( m_artistView
, SIGNAL(selectionChanged( Q3ListViewItem
* ) ),
259 SLOT(slotArtistSelected( Q3ListViewItem
* )) );
260 connect( m_coverView
, SIGNAL(contextMenuRequested( Q3IconViewItem
*, const QPoint
& )),
261 SLOT(showCoverMenu( Q3IconViewItem
*, const QPoint
& )) );
262 connect( m_coverView
, SIGNAL(executed( Q3IconViewItem
* )),
263 SLOT(coverItemExecuted( Q3IconViewItem
* )) );
264 connect( m_timer
, SIGNAL(timeout()),
265 SLOT(slotSetFilter()) );
266 connect( m_searchEdit
, SIGNAL(textChanged( const QString
& )),
267 SLOT(slotSetFilterTimeout()) );
269 connect( CollectionDB::instance(), SIGNAL(coverFetched( const QString
&, const QString
& )),
270 SLOT(coverFetched( const QString
&, const QString
& )) );
271 connect( CollectionDB::instance(), SIGNAL(coverRemoved( const QString
&, const QString
& )),
272 SLOT(coverRemoved( const QString
&, const QString
& )) );
273 connect( CollectionDB::instance(), SIGNAL(coverFetcherError( const QString
& )),
274 SLOT(coverFetcherError()) );
276 m_currentView
= AllAlbums
;
278 QSize size
= QApplication::desktop()->screenGeometry( this ).size() / 1.5;
279 QSize sz
= Amarok::config( "Cover Manager" ).readEntry( "Window Size", size
);
280 resize( sz
.width(), sz
.height() );
284 QTimer::singleShot( 0, this, SLOT(init()) );
288 CoverManager::~CoverManager()
292 Amarok::config( "Cover Manager" ).writeEntry( "Window Size", size() );
298 void CoverManager::init()
302 Q3ListViewItem
*item
= 0;
304 if ( !artistToSelectInInitFunction
.isEmpty() )
305 for ( item
= m_artistView
->firstChild(); item
; item
= item
->nextSibling() )
306 if ( item
->text( 0 ) == artistToSelectInInitFunction
)
310 item
= m_artistView
->firstChild();
312 m_artistView
->setSelected( item
, true );
316 CoverViewDialog::CoverViewDialog( const QString
& artist
, const QString
& album
, QWidget
*parent
)
317 : QDialog( parent
, 0, false, Qt::WType_TopLevel
| Qt::WNoAutoErase
)
318 , m_pixmap( CollectionDB::instance()->albumImage( artist
, album
, false, 0 ) )
320 setAttribute( Qt::WA_DeleteOnClose
);
322 KWindowSystem::setType( winId(), NET::Utility
);
324 kapp
->setTopWidget( this );
325 setWindowTitle( KDialog::makeStandardCaption( i18n("%1 - %2", artist
, album
) ) );
327 m_layout
= new QHBoxLayout( this );
328 m_pixmapViewer
= new PixmapViewer( this, m_pixmap
);
329 m_layout
->addWidget( m_pixmapViewer
);
331 setFixedSize( m_pixmapViewer
->maximalSize() );
335 void CoverManager::viewCover( const QString
& artist
, const QString
& album
, QWidget
*parent
) //static
337 //QDialog means "escape" works as expected
338 QDialog
*dialog
= new CoverViewDialog( artist
, album
, parent
);
343 QString
CoverManager::amazonTld() //static
345 if( AmarokConfig::amazonLocale() == "us" )
347 else if( AmarokConfig::amazonLocale()== "jp" )
349 else if( AmarokConfig::amazonLocale() == "uk" )
351 else if( AmarokConfig::amazonLocale() == "ca" )
354 return AmarokConfig::amazonLocale();
358 void CoverManager::fetchMissingCovers() //SLOT
362 for ( Q3IconViewItem
*item
= m_coverView
->firstItem(); item
; item
= item
->nextItem() ) {
363 CoverViewItem
*coverItem
= static_cast<CoverViewItem
*>( item
);
364 if( !coverItem
->hasCover() ) {
365 m_fetchCovers
+= coverItem
->artist() + " @@@ " + coverItem
->album();
370 if( !m_fetchCounter
) //loop isn't started yet
374 m_fetchButton
->setEnabled( false );
379 void CoverManager::fetchCoversLoop() //SLOT
381 if( (int)m_fetchCounter
< m_fetchCovers
.count() )
383 //get artist and album from keyword
384 const QStringList values
= m_fetchCovers
[m_fetchCounter
].split( " @@@ ", QString::KeepEmptyParts
);
386 if( values
.count() > 1 )
387 CollectionDB::instance()->fetchCover( this, values
[0], values
[1], m_fetchCovers
.count() != 1); //edit mode when fetching 1 cover
391 // Wait 1 second, since amazon caps the number of accesses per client
392 QTimer::singleShot( 1000, this, SLOT( fetchCoversLoop() ) );
395 m_fetchCovers
.clear();
402 void CoverManager::showOnce( const QString
&artist
)
405 artistToSelectInInitFunction
= artist
;
406 new CoverManager(); //shows itself
409 s_instance
->activateWindow();
414 void CoverManager::slotArtistSelected( Q3ListViewItem
*item
) //SLOT
416 if( item
->depth() ) //album item
419 QString artist
= item
->text(0);
421 if( artist
.endsWith( ", The" ) )
422 Amarok::manipulateThe( artist
, false );
424 m_coverView
->clear();
425 m_coverItems
.clear();
427 // reset current view mode state to "AllAlbum" which is the default on artist change in left panel
428 m_currentView
= AllAlbums
;
429 m_selectAllAlbums
->trigger();
430 m_selectAllAlbums
->setChecked( true );
432 Q3ProgressDialog
progress( this, 0, true );
433 progress
.setLabelText( i18n("Loading Thumbnails...") );
434 progress
.QDialog::setWindowTitle( i18n("...") );
436 //NOTE we MUST show the dialog, otherwise the closeEvents get processed
437 // in the processEvents() calls below, GRUMBLE! Qt sux0rs
439 progress
.repaint(); //ensures the dialog isn't blank
441 //this is an extra processEvent call for the sake of init() and aesthetics
443 kapp
->processEvents();
445 //this can be a bit slow
446 QApplication::setOverrideCursor( Qt::WaitCursor
);
450 qb
.addReturnValue( QueryBuilder::tabArtist
, QueryBuilder::valName
);
451 qb
.addReturnValue( QueryBuilder::tabAlbum
, QueryBuilder::valName
);
453 qb
.excludeMatch( QueryBuilder::tabAlbum
, i18n( "Unknown" ) );
454 qb
.sortBy( QueryBuilder::tabAlbum
, QueryBuilder::valName
);
455 qb
.setOptions( QueryBuilder::optRemoveDuplicates
);
456 qb
.setOptions( QueryBuilder::optNoCompilations
);
458 if ( item
!= m_artistView
->firstChild() )
459 qb
.addMatch( QueryBuilder::tabArtist
, artist
);
463 //also retrieve compilations when we're showing all items (first treenode) or
464 //"Various Artists" (last treenode)
465 if ( item
== m_artistView
->firstChild() || item
== m_artistView
->lastChild() )
470 qb
.addReturnValue( QueryBuilder::tabAlbum
, QueryBuilder::valName
);
472 qb
.excludeMatch( QueryBuilder::tabAlbum
, i18n( "Unknown" ) );
473 qb
.sortBy( QueryBuilder::tabAlbum
, QueryBuilder::valName
);
474 qb
.setOptions( QueryBuilder::optRemoveDuplicates
);
475 qb
.setOptions( QueryBuilder::optOnlyCompilations
);
478 for( int i
= 0; i
< cl
.count(); i
++ ) {
479 albums
.append( i18n( "Various Artists" ) );
480 albums
.append( cl
[ i
] );
484 QApplication::restoreOverrideCursor();
486 progress
.setTotalSteps( (albums
.count()/2) + (albums
.count()/10) );
488 //insert the covers first because the list view is soooo paint-happy
489 //doing it in the second loop looks really bad, unfortunately
490 //this is the slowest step in the bit that we can't process events
494 const QString artist
= *it
;
495 const QString album
= *(++it
);
496 m_coverItems
.append( new CoverViewItem( m_coverView
, m_coverView
->lastItem(), artist
, album
) );
498 if ( ++x
% 50 == 0 ) {
499 progress
.setProgress( x
/ 5 ); // we do it less often due to bug in Qt, ask Max
500 kapp
->processEvents(); // QProgressDialog also calls this, but not always due to Qt bug!
502 //only worth testing for after processEvents() is called
503 if( progress
.wasCancelled() )
508 //now, load the thumbnails
509 for( Q3IconViewItem
*item
= m_coverView
->firstItem(); item
; item
= item
->nextItem() ) {
510 progress
.setProgress( progress
.progress() + 1 );
511 kapp
->processEvents();
513 if( progress
.wasCancelled() )
516 static_cast<CoverViewItem
*>(item
)->loadCover();
522 void CoverManager::showCoverMenu( Q3IconViewItem
*item
, const QPoint
&p
) //SLOT
524 #define item static_cast<CoverViewItem*>(item)
529 menu
.addTitle( i18n( "Cover Image" ) );
531 Q3PtrList
<CoverViewItem
> selected
= selectedItems();
532 const int nSelected
= selected
.count();
534 QAction
* fetchSelectedAction
= new QAction( KIcon( Amarok::icon( "download" ) )
535 , i18np( "&Fetch From amazon.%1", "&Fetch Selected Covers", nSelected
, CoverManager::amazonTld() )
537 connect( fetchSelectedAction
, SIGNAL( triggered() ), this, SLOT( fetchSelectedCovers() ) );
539 QAction
* setCustomAction
= new QAction( KIcon( Amarok::icon( "files" ) )
540 , i18np( "Set &Custom Cover", "Set &Custom Cover for Selected Albums", nSelected
)
542 connect( setCustomAction
, SIGNAL( triggered() ), this, SLOT( setCustomSelectedCovers() ) );
543 QAction
* unsetAction
= new QAction( KIcon( Amarok::icon( "remove" ) ), i18np( "&Unset Cover", "&Unset Selected Covers", nSelected
), &menu
);
544 connect( unsetAction
, SIGNAL( triggered() ), this, SLOT ( deleteSelectedCovers() ) );
546 QAction
* playAlbumAction
= new QAction( KIcon( Amarok::icon( "add_playlist" ) )
547 , i18np( "&Append to Playlist", "&Append Selected Albums to Playlist", nSelected
)
550 connect( playAlbumAction
, SIGNAL( triggered() ), this, SLOT( playSelectedAlbums() ) );
552 if( nSelected
> 1 ) {
553 menu
.addAction( fetchSelectedAction
);
554 menu
.addAction( setCustomAction
);
555 menu
.addAction( playAlbumAction
);
556 menu
.addAction( unsetAction
);
559 QAction
* viewAction
= new QAction( KIcon( Amarok::icon( "zoom" ) ), i18n( "&Show Fullsize" ), &menu
);
560 connect( viewAction
, SIGNAL( triggered() ), this, SLOT( viewSelectedCover() ) );
561 viewAction
->setEnabled( item
->hasCover() );
562 unsetAction
->setEnabled( item
->canRemoveCover() );
563 menu
.addAction( viewAction
);
564 menu
.addAction( fetchSelectedAction
);
565 menu
.addAction( setCustomAction
);
566 menu
.addAction( playAlbumAction
);
569 menu
.addAction( unsetAction
);
577 void CoverManager::viewSelectedCover()
579 CoverViewItem
* item
= selectedItems().first();
580 viewCover( item
->artist(), item
->album(), this );
583 void CoverManager::coverItemExecuted( Q3IconViewItem
*item
) //SLOT
585 #define item static_cast<CoverViewItem*>(item)
589 item
->setSelected( true );
590 if ( item
->hasCover() )
591 viewCover( item
->artist(), item
->album(), this );
593 fetchSelectedCovers();
599 void CoverManager::slotSetFilter() //SLOT
601 m_filter
= m_searchEdit
->text();
603 m_coverView
->selectAll( false);
604 Q3IconViewItem
*item
= m_coverView
->firstItem();
607 Q3IconViewItem
*tmp
= item
->nextItem();
608 m_coverView
->takeItem( item
);
612 m_coverView
->setAutoArrange( false );
613 for( Q3IconViewItem
*item
= m_coverItems
.first(); item
; item
= m_coverItems
.next() )
615 CoverViewItem
*coverItem
= static_cast<CoverViewItem
*>(item
);
616 if( coverItem
->album().contains( m_filter
, Qt::CaseInsensitive
) || coverItem
->artist().contains( m_filter
, Qt::CaseInsensitive
) )
617 m_coverView
->insertItem( item
, m_coverView
->lastItem() );
619 m_coverView
->setAutoArrange( true );
621 m_coverView
->arrangeItemsInGrid();
626 void CoverManager::slotSetFilterTimeout() //SLOT
628 if ( m_timer
->isActive() ) m_timer
->stop();
629 m_timer
->setSingleShot( true );
630 m_timer
->start( 180 );
633 void CoverManager::changeView( int id
) //SLOT
635 if( m_currentView
== id
) return;
637 //clear the iconview without deleting items
638 m_coverView
->selectAll( false);
639 Q3IconViewItem
*item
= m_coverView
->firstItem();
641 Q3IconViewItem
*tmp
= item
->nextItem();
642 m_coverView
->takeItem( item
);
646 m_coverView
->setAutoArrange(false );
647 for( Q3IconViewItem
*item
= m_coverItems
.first(); item
; item
= m_coverItems
.next() ) {
649 CoverViewItem
*coverItem
= static_cast<CoverViewItem
*>(item
);
650 if( !m_filter
.isEmpty() ) {
651 if( !coverItem
->album().contains( m_filter
, Qt::CaseInsensitive
) && !coverItem
->artist().contains( m_filter
, Qt::CaseInsensitive
) )
655 if( id
== AllAlbums
) //show all albums
657 else if( id
== AlbumsWithCover
&& coverItem
->hasCover() ) //show only albums with cover
659 else if( id
== AlbumsWithoutCover
&& !coverItem
->hasCover() ) //show only albums without cover
662 if( show
) m_coverView
->insertItem( item
, m_coverView
->lastItem() );
664 m_coverView
->setAutoArrange( true );
666 m_coverView
->arrangeItemsInGrid();
670 void CoverManager::changeLocale( int id
) //SLOT
672 QString locale
= CoverFetcher::localeIDToString( id
);
673 AmarokConfig::setAmazonLocale( locale
);
674 m_currentLocale
= id
;
678 void CoverManager::coverFetched( const QString
&artist
, const QString
&album
) //SLOT
680 loadCover( artist
, album
);
686 void CoverManager::coverRemoved( const QString
&artist
, const QString
&album
) //SLOT
688 loadCover( artist
, album
);
694 void CoverManager::coverFetcherError()
703 void CoverManager::stopFetching()
705 Debug::Block
block( __PRETTY_FUNCTION__
);
707 m_fetchCovers
.clear();
710 //delete all cover fetchers
711 QList
<CoverFetcher
*> list
= qFindChildren
<CoverFetcher
*>(this);
712 foreach( CoverFetcher
*cf
, list
)
715 m_fetchingCovers
= 0;
721 void CoverManager::loadCover( const QString
&artist
, const QString
&album
)
723 for( Q3IconViewItem
*item
= m_coverItems
.first(); item
; item
= m_coverItems
.next() )
725 CoverViewItem
*coverItem
= static_cast<CoverViewItem
*>(item
);
726 if ( album
== coverItem
->album() && ( artist
== coverItem
->artist() || ( artist
.isEmpty() && coverItem
->artist().isEmpty() ) ) )
728 coverItem
->loadCover();
734 void CoverManager::setCustomSelectedCovers()
736 //function assumes something is selected
737 Q3PtrList
<CoverViewItem
> selected
= selectedItems();
738 CoverViewItem
* first
= selected
.getFirst();
740 QString artist_id
; artist_id
.setNum( CollectionDB::instance()->artistID( first
->artist() ) );
741 QString album_id
; album_id
.setNum( CollectionDB::instance()->albumID( first
->album() ) );
742 QStringList values
= CollectionDB::instance()->albumTracks( artist_id
, album_id
);
744 QString startPath
= ":homedir";
745 if ( !values
.isEmpty() ) {
747 url
.setPath( values
.first() );
748 startPath
= url
.directory();
750 KUrl file
= KFileDialog::getImageOpenUrl( startPath
, this, i18n( "Select Cover Image File" ) );
751 if ( !file
.isEmpty() ) {
752 kapp
->processEvents(); //it may takes a while so process pending events
754 QImage image
= CollectionDB::fetchImage(file
, tmpFile
);
755 for ( CoverViewItem
* item
= selected
.first(); item
; item
= selected
.next() ) {
756 CollectionDB::instance()->setAlbumImage( item
->artist(), item
->album(), image
);
759 KIO::NetAccess::removeTempFile( tmpFile
);
763 void CoverManager::fetchSelectedCovers()
765 Q3PtrList
<CoverViewItem
> selected
= selectedItems();
766 for ( CoverViewItem
* item
= selected
.first(); item
; item
= selected
.next() )
767 m_fetchCovers
+= item
->artist() + " @@@ " + item
->album();
769 m_fetchingCovers
+= selected
.count();
771 if( !m_fetchCounter
) //loop isn't started yet
778 void CoverManager::deleteSelectedCovers()
780 Q3PtrList
<CoverViewItem
> selected
= selectedItems();
782 int button
= KMessageBox::warningContinueCancel( this,
783 i18np( "Are you sure you want to remove this cover from the Collection?",
784 "Are you sure you want to delete these %1 covers from the Collection?",
787 KStandardGuiItem::del() );
789 if ( button
== KMessageBox::Continue
) {
790 for ( CoverViewItem
* item
= selected
.first(); item
; item
= selected
.next() ) {
791 kapp
->processEvents();
792 if ( CollectionDB::instance()->removeAlbumImage( item
->artist(), item
->album() ) ) //delete selected cover
793 coverRemoved( item
->artist(), item
->album() );
799 void CoverManager::playSelectedAlbums()
801 Q3PtrList
<CoverViewItem
> selected
= selectedItems();
802 QString artist_id
, album_id
;
803 for ( CoverViewItem
* item
= selected
.first(); item
; item
= selected
.next() )
805 artist_id
.setNum( CollectionDB::instance()->artistID( item
->artist() ) );
806 album_id
.setNum( CollectionDB::instance()->albumID( item
->album() ) );
807 The::playlistModel()->insertMedia( CollectionDB::instance()->albumTracks( artist_id
, album_id
), Playlist::Append
);
811 Q3PtrList
<CoverViewItem
> CoverManager::selectedItems()
813 Q3PtrList
<CoverViewItem
> selectedItems
;
814 for ( Q3IconViewItem
* item
= m_coverView
->firstItem(); item
; item
= item
->nextItem() )
815 if ( item
->isSelected() )
816 selectedItems
.append( static_cast<CoverViewItem
*>(item
) );
818 return selectedItems
;
822 void CoverManager::updateStatusBar()
826 //cover fetching info
827 if( m_fetchingCovers
) {
828 //update the progress bar
829 m_progress
->setMaximum( m_fetchingCovers
);
830 m_progress
->setValue( m_coversFetched
+ m_coverErrors
);
831 if( m_progressBox
->isHidden() )
832 m_progressBox
->show();
834 //update the status text
835 if( m_coversFetched
+ m_coverErrors
>= m_progress
->value() ) {
837 text
= i18n( "Finished." );
839 text
+= i18np( " Cover not found", " <b>%1</b> covers not found", m_coverErrors
);
841 m_fetchingCovers
= 0;
844 QTimer::singleShot( 2000, this, SLOT( updateStatusBar() ) );
847 if( m_fetchingCovers
== 1 ) {
848 QStringList values
= m_fetchCovers
[0].split( " @@@ ", QString::KeepEmptyParts
); //get artist and album name
849 if ( values
.count() >= 2 )
851 if( values
[0].isEmpty() )
852 text
= i18n( "Fetching cover for %1..." , values
[1] );
854 text
= i18n( "Fetching cover for %1 - %2...", values
[0], values
[1] );
857 else if( m_fetchingCovers
) {
858 text
= i18np( "Fetching 1 cover: ", "Fetching <b>%1</b> covers... : ", m_fetchingCovers
);
859 if( m_coversFetched
)
860 text
+= i18np( "1 fetched", "%1 fetched", m_coversFetched
);
861 if( m_coverErrors
) {
862 if( m_coversFetched
) text
+= i18n(" - ");
863 text
+= i18np( "1 not found", "%1 not found", m_coverErrors
);
865 if( m_coversFetched
+ m_coverErrors
== 0 )
866 text
+= i18n( "Connecting..." );
873 uint totalCounter
= 0, missingCounter
= 0;
875 if( m_progressBox
->isVisible() )
876 m_progressBox
->hide();
879 for( Q3IconViewItem
*item
= m_coverView
->firstItem(); item
; item
= item
->nextItem() ) {
881 if( !static_cast<CoverViewItem
*>( item
)->hasCover() )
882 missingCounter
++; //counter for albums without cover
885 if( !m_filter
.isEmpty() )
886 text
= i18np( "1 result for \"%1\"", "%1 results for \"%1\"", totalCounter
, m_filter
);
887 else if( m_artistView
->selectedItem() ) {
888 text
= i18np( "1 album", "%1 albums", totalCounter
);
889 if( m_artistView
->selectedItem() != m_artistView
->firstChild() ) //showing albums by an artist
891 QString artist
= m_artistView
->selectedItem()->text(0);
892 if( artist
.endsWith( ", The" ) )
893 Amarok::manipulateThe( artist
, false );
894 text
+= i18n( " by " ) + artist
;
899 text
+= i18n(" - ( <b>%1</b> without cover )", missingCounter
);
901 m_fetchButton
->setEnabled( missingCounter
);
904 m_statusLabel
->setText( text
);
907 void CoverManager::setStatusText( QString text
)
909 m_oldStatusText
= m_statusLabel
->text();
910 m_statusLabel
->setText( text
);
913 //////////////////////////////////////////////////////////////////////
915 /////////////////////////////////////////////////////////////////////
917 CoverView::CoverView( QWidget
*parent
, const char *name
, Qt::WFlags f
)
918 : K3IconView( parent
, name
, f
)
920 Debug::Block
block( __PRETTY_FUNCTION__
);
922 setArrangement( Q3IconView::LeftToRight
);
923 setResizeMode( Q3IconView::Adjust
);
924 setSelectionMode( Q3IconView::Extended
);
925 arrangeItemsInGrid();
926 setAutoArrange( true );
927 setItemsMovable( false );
929 // as long as QIconView only shows tooltips when the cursor is over the
930 // icon (and not the text), we have to create our own tooltips
931 setShowToolTips( false );
933 connect( this, SIGNAL( onItem( Q3IconViewItem
* ) ), SLOT( setStatusText( Q3IconViewItem
* ) ) );
934 connect( this, SIGNAL( onViewport() ), CoverManager::instance(), SLOT( updateStatusBar() ) );
938 Q3DragObject
*CoverView::dragObject()
940 CoverViewItem
*item
= static_cast<CoverViewItem
*>( currentItem() );
944 const QString sql
= "SELECT tags.url FROM tags, album WHERE album.name %1 AND tags.album = album.id ORDER BY tags.track;";
945 const QStringList values
= CollectionDB::instance()->query( sql
.arg( CollectionDB::likeCondition( item
->album() ) ) );
948 for( QStringList::ConstIterator it
= values
.begin(), end
= values
.end(); it
!= end
; ++it
)
951 QString imagePath
= CollectionDB::instance()->albumImage( item
->artist(), item
->album(), false, 1 );
952 K3MultipleDrag
*drag
= new K3MultipleDrag( this );
953 drag
->setPixmap( item
->coverPixmap() );
954 drag
->addDragObject( new Q3IconDrag( this ) );
955 drag
->addDragObject( new Q3ImageDrag( QImage( imagePath
) ) );
956 drag
->addDragObject( new K3URLDrag( urls
) );
961 void CoverView::setStatusText( Q3IconViewItem
*item
)
963 #define item static_cast<CoverViewItem *>( item )
967 bool sampler
= false;
968 //compilations have valDummy for artist. see QueryBuilder::addReturnValue(..) for explanation
969 //FIXME: Don't rely on other independent code, use an sql query
970 if( item
->artist().isEmpty() ) sampler
= true;
972 QString tipContent
= i18n( "%1 - %2", sampler
? i18n("Various Artists") : item
->artist() , item
->album() );
974 CoverManager::instance()->setStatusText( tipContent
);
979 //////////////////////////////////////////////////////////////////////
980 // CLASS CoverViewItem
981 /////////////////////////////////////////////////////////////////////
983 CoverViewItem::CoverViewItem( Q3IconView
*parent
, Q3IconViewItem
*after
, const QString
&artist
, const QString
&album
)
984 : K3IconViewItem( parent
, after
, album
)
987 , m_coverImagePath( CollectionDB::instance()->albumImage( m_artist
, m_album
, false, 0, &m_embedded
) )
990 setDragEnabled( true );
991 setDropEnabled( true );
995 bool CoverViewItem::hasCover() const
997 return !m_coverImagePath
.endsWith( "nocover.png" ) && QFile::exists( m_coverImagePath
);
1000 void CoverViewItem::loadCover()
1002 m_coverImagePath
= CollectionDB::instance()->albumImage( m_artist
, m_album
, false, 1, &m_embedded
);
1003 m_coverPixmap
= QPixmap( m_coverImagePath
); //create the scaled cover
1009 void CoverViewItem::calcRect( const QString
& )
1011 int thumbWidth
= AmarokConfig::coverPreviewSize();
1013 QFontMetrics fm
= iconView()->fontMetrics();
1014 QRect
itemPixmapRect( 5, 1, thumbWidth
, thumbWidth
);
1015 QRect itemRect
= rect();
1016 itemRect
.setWidth( thumbWidth
+ 10 );
1017 itemRect
.setHeight( thumbWidth
+ fm
.lineSpacing() + 2 );
1018 QRect
itemTextRect( 0, thumbWidth
+2, itemRect
.width(), fm
.lineSpacing() );
1020 setPixmapRect( itemPixmapRect
);
1021 setTextRect( itemTextRect
);
1022 setItemRect( itemRect
);
1026 void CoverViewItem::paintItem(QPainter
* p
, const QColorGroup
& cg
)
1028 Q_UNUSED( cg
); // FIXME is this correct (cg replaced by palette() )?
1029 QPalette palette
= KApplication::palette();
1030 QRect itemRect
= rect();
1033 p
->translate( itemRect
.x(), itemRect
.y() );
1036 p
->setPen( palette
.mid().color() );
1037 p
->drawRect( 0, 0, itemRect
.width(), pixmapRect().height()+2 );
1039 // draw the cover image
1040 if( !m_coverPixmap
.isNull() )
1041 p
->drawPixmap( pixmapRect().x() + (pixmapRect().width() - m_coverPixmap
.width())/2,
1042 pixmapRect().y() + (pixmapRect().height() - m_coverPixmap
.height())/2, m_coverPixmap
);
1044 //justify the album name
1045 QString str
= text();
1046 QFontMetrics fm
= p
->fontMetrics();
1047 str
= fm
.elidedText( str
, Qt::ElideRight
, textRect().width() );
1049 p
->setPen( palette
.text().color() );
1050 p
->drawText( textRect(), Qt::AlignCenter
, str
);
1052 if( isSelected() ) {
1053 p
->setPen( palette
.highlight().color() );
1054 p
->drawRect( pixmapRect() );
1055 p
->drawRect( pixmapRect().left()+1, pixmapRect().top()+1, pixmapRect().width()-2, pixmapRect().height()-2);
1056 p
->drawRect( pixmapRect().left()+2, pixmapRect().top()+2, pixmapRect().width()-4, pixmapRect().height()-4);
1063 void CoverViewItem::dropped( QDropEvent
*e
, const Q3ValueList
<Q3IconDragItem
> & )
1065 if( Q3ImageDrag::canDecode( e
) ) {
1067 KGuiItem continueButton
= KStandardGuiItem::cont();
1068 continueButton
.setText( i18n("&Overwrite") );
1069 int button
= KMessageBox::warningContinueCancel( iconView(),
1070 i18n( "Are you sure you want to overwrite this cover?"),
1071 i18n("Overwrite Confirmation"),
1073 if( button
== KMessageBox::Cancel
)
1078 Q3ImageDrag::decode( e
, img
);
1079 CollectionDB::instance()->setAlbumImage( artist(), album(), img
);
1080 m_coverImagePath
= CollectionDB::instance()->albumImage( m_artist
, m_album
, false, 0 );
1086 void CoverViewItem::dragEntered()
1088 setSelected( true );
1092 void CoverViewItem::dragLeft()
1094 setSelected( false );
1097 #include "covermanager.moc"