1 /****************************************************************************************
2  * Copyright (c) 2008 Seb Ruiz <ruiz@kde.org>                                           *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #include "AlbumItem.h"
18 
19 #include "SvgHandler.h"
20 #include "AlbumsDefs.h"
21 #include "core/meta/Meta.h"
22 #include "core/meta/support/MetaUtility.h"
23 
24 #include <KLocalizedString>
25 
26 #include <QCollator>
27 #include <QIcon>
28 #include <QPixmap>
29 
AlbumItem()30 AlbumItem::AlbumItem()
31     : QStandardItem()
32     , m_iconSize( 40 )
33     , m_showArtist( false )
34 {
35     setEditable( false );
36 }
37 
~AlbumItem()38 AlbumItem::~AlbumItem()
39 {
40     if( m_album )
41         unsubscribeFrom( m_album );
42 }
43 
44 void
setAlbum(const Meta::AlbumPtr & albumPtr)45 AlbumItem::setAlbum( const Meta::AlbumPtr &albumPtr )
46 {
47     if( m_album )
48         unsubscribeFrom( m_album );
49     m_album = albumPtr;
50     subscribeTo( m_album );
51     update();
52 }
53 
54 void
setIconSize(const int iconSize)55 AlbumItem::setIconSize( const int iconSize )
56 {
57     static const int padding = 5;
58 
59     m_iconSize = iconSize;
60 
61     QSize size = sizeHint();
62     size.setHeight( iconSize + padding*2 );
63     setSizeHint( size );
64 }
65 
66 void
setShowArtist(const bool showArtist)67 AlbumItem::setShowArtist( const bool showArtist )
68 {
69     if( showArtist != m_showArtist )
70     {
71         m_showArtist = showArtist;
72         metadataChanged( m_album );
73     }
74 }
75 
76 void
metadataChanged(const Meta::AlbumPtr & album)77 AlbumItem::metadataChanged(const Meta::AlbumPtr &album )
78 {
79     Q_UNUSED( album );
80     QMetaObject::invokeMethod(this, "update", Qt::QueuedConnection);
81 }
82 
83 void
update()84 AlbumItem::update()
85 {
86     if( !m_album )
87         return;
88 
89     Meta::TrackList tracks = m_album->tracks();
90     if( !tracks.isEmpty() )
91     {
92         Meta::TrackPtr first = tracks.first();
93         Meta::YearPtr year = first->year();
94         if( year )
95             setData( year->year(), AlbumYearRole );
96     }
97 
98     QString albumName = m_album->name();
99     albumName = albumName.isEmpty() ? i18n("Unknown") : albumName;
100     QString name = ( m_showArtist && m_album->hasAlbumArtist() )
101                  ? QString( "%1 - %2" ).arg( m_album->albumArtist()->name(), albumName )
102                  : albumName;
103     setData( name, NameRole );
104 
105     qint64 totalTime = 0;
106     foreach( Meta::TrackPtr item, tracks )
107         totalTime += item->length();
108 
109     QString trackCount = i18np( "%1 track", "%1 tracks", tracks.size() );
110     QString lengthText = QString( "%1, %2" ).arg( trackCount, Meta::msToPrettyTime( totalTime ) );
111     setData( lengthText, AlbumLengthRole );
112 
113     QPixmap cover = The::svgHandler()->imageWithBorder( m_album, m_iconSize, 3 );
114     setIcon( QIcon( cover ) );
115     setData( cover, AlbumCoverRole );
116 }
117 
118 int
type() const119 AlbumItem::type() const
120 {
121     return AlbumType;
122 }
123 
124 bool
operator <(const QStandardItem & other) const125 AlbumItem::operator<( const QStandardItem &other ) const
126 {
127     // compare the opposite for descending year order
128     int yearA = data( AlbumYearRole ).toInt();
129     int yearB = other.data( AlbumYearRole ).toInt();
130     if( yearA > yearB )
131         return true;
132     else if( yearA == yearB )
133     {
134         const QString nameA = data( NameRole ).toString();
135         const QString nameB = other.data( NameRole ).toString();
136         QCollator collator;
137         collator.setNumericMode( true );
138         collator.setCaseSensitivity( Qt::CaseInsensitive );
139         return collator.compare( nameA, nameB ) < 0;
140     }
141     else
142         return false;
143 }
144 
145