Debian Codegraph @

src/context/applets/currenttrack/plugin/CurrentEngine.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2007 Leo Franchi <[email protected]> *
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 #define DEBUG_PREFIX "CurrentEngine"
18
19 #include "CurrentEngine.h"
20
21 #include "EngineController.h"
22 #include "core/support/Debug.h"
23 #include "core/meta/support/MetaUtility.h"
24 #include "core/meta/Statistics.h"
25 #include "core/support/Amarok.h"
26 #include "covermanager/CoverCache.h"
27 #include "covermanager/CoverFetchingActions.h"
28
29
30 #include <KFormat>
31 #include <KLocalizedString>
32
33
34 CurrentEngine::CurrentEngine( QObject* parent )
35 : QObject( parent )
36 {
37 EngineController* engine = The::engineController();
38
39 // Connect queued to reduce interface stuttering.
40 connect( engine, &EngineController::trackPlaying,
41 this, &CurrentEngine::slotTrackChanged, Qt::QueuedConnection );
42 connect( engine, &EngineController::stopped,
43 this, &CurrentEngine::stopped, Qt::QueuedConnection );
44 connect( engine, &EngineController::trackMetadataChanged,
45 this, &CurrentEngine::slotTrackMetadataChanged, Qt::QueuedConnection );
46 connect( engine, &EngineController::albumMetadataChanged,
47 this, &CurrentEngine::slotAlbumMetadataChanged, Qt::QueuedConnection );
48 }
49
50 CurrentEngine::~CurrentEngine()
51 {
52 }
53
54 void
55 CurrentEngine::slotAlbumMetadataChanged( const Meta::AlbumPtr &album )
56 {
57 DEBUG_BLOCK
58
59 // disregard changes for other albums (BR: 306735)
60 if( !m_currentTrack || m_currentTrack->album() != album )
61 return;
62
63 QPixmap cover;
64
65 if( album )
66 cover = The::coverCache()->getCover( album, 1 );
67
68 if( m_cover.cacheKey() != cover.cacheKey() )
69 {
70 m_cover = cover;
71 Q_EMIT albumChanged();
72 }
73 }
74
75 void
76 CurrentEngine::slotTrackMetadataChanged( Meta::TrackPtr track )
77 {
78 if( !track )
79 return;
80
81 update( track->album() );
82 Q_EMIT trackChanged();
83 }
84
85 void
86 CurrentEngine::slotTrackChanged(const Meta::TrackPtr &track)
87 {
88 DEBUG_BLOCK
89
90 if( !track || track == m_currentTrack )
91 return;
92
93 m_currentTrack = track;
94 slotTrackMetadataChanged( track );
95 }
96
97
98 void
99 CurrentEngine::stopped()
100 {
101 m_currentTrack.reset();
102 Q_EMIT trackChanged();
103
104 m_cover = QPixmap();
105 }
106
107 void
108 CurrentEngine::update( Meta::AlbumPtr album )
109 {
110
111 if( !album )
112 return;
113
114 slotAlbumMetadataChanged( album );
115
116 }
117
118 QString
119 CurrentEngine::artist() const
120 {
121 if( !m_currentTrack || !m_currentTrack->artist() )
122 return QString();
123
124 return m_currentTrack->artist()->prettyName();
125 }
126
127 QString
128 CurrentEngine::track() const
129 {
130 if( !m_currentTrack )
131 return QString();
132
133 return m_currentTrack->prettyName();
134 }
135
136 QString
137 CurrentEngine::album() const
138 {
139 if( !m_currentTrack || !m_currentTrack->album() )
140 return QString();
141
142 return m_currentTrack->album()->prettyName();
143 }
144
145 int
146 CurrentEngine::rating() const
147 {
148 if( !m_currentTrack )
149 return 0;
150
151 return m_currentTrack->statistics()->rating();
152 }
153
154 void
155 CurrentEngine::setRating(int rating)
156 {
157 DEBUG_BLOCK
158
159 debug() << "New rating:" << rating;
160
161 if( !m_currentTrack )
162 return;
163
164 if( rating == m_currentTrack->statistics()->rating() )
165 return;
166
167 m_currentTrack->statistics()->setRating( rating );
168 Q_EMIT trackChanged();
169 }
170
171 int
172 CurrentEngine::score() const
173 {
174 if( !m_currentTrack )
175 return 0;
176
177 return m_currentTrack->statistics()->score();
178 }
179
180 int
181 CurrentEngine::length() const
182 {
183 if( !m_currentTrack )
184 return 0;
185
186 return m_currentTrack->length();
187 }
188
189 QString
190 CurrentEngine::lastPlayed() const
191 {
192 if( !m_currentTrack )
193 return QString();
194
195 auto lastPlayed = m_currentTrack->statistics()->lastPlayed();
196 QString lastPlayedString;
197 if( lastPlayed.isValid() )
198 lastPlayedString = KFormat().formatRelativeDateTime( lastPlayed, QLocale::ShortFormat );
199 else
200 lastPlayedString = i18n( "Never" );
201
202 return lastPlayedString;
203 }
204
205 int
206 CurrentEngine::timesPlayed() const
207 {
208 if( !m_currentTrack )
209 return 0;
210
211 return m_currentTrack->statistics()->playCount();
212 }
213
214 void
215 CurrentEngine::displayCover()
216 {
217 if( m_currentTrack && m_currentTrack->album() )
218 {
219 auto dc = new DisplayCoverAction( this, m_currentTrack->album() );
220 dc->trigger();
221 delete dc;
222 }
223 }