1 /* Copyright (C) 2005-2007 Jeff Mitchell <kde-dev@emailgoeshere.com>
2 (c) 2004 Christian Muehlhaeuser <chris@chris.de>
3 (c) 2005-2006 Martin Aumueller <aumuell@reserv.at>
4 (c) 2005 Seb Ruiz <ruiz@kde.org>
5 (c) 2006 T.R.Shashwath <trshash84@gmail.com>
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
24 #include "collectiondb.h"
25 #include "mediabrowser.h"
26 #include "MediaItem.h"
28 #include <kiconloader.h>
32 QPixmap
*MediaItem::s_pixUnknown
= 0;
33 QPixmap
*MediaItem::s_pixArtist
= 0;
34 QPixmap
*MediaItem::s_pixAlbum
= 0;
35 QPixmap
*MediaItem::s_pixFile
= 0;
36 QPixmap
*MediaItem::s_pixTrack
= 0;
37 QPixmap
*MediaItem::s_pixPodcast
= 0;
38 QPixmap
*MediaItem::s_pixPlaylist
= 0;
39 QPixmap
*MediaItem::s_pixInvisible
= 0;
40 QPixmap
*MediaItem::s_pixStale
= 0;
41 QPixmap
*MediaItem::s_pixOrphaned
= 0;
42 QPixmap
*MediaItem::s_pixDirectory
= 0;
43 QPixmap
*MediaItem::s_pixRootItem
= 0;
44 QPixmap
*MediaItem::s_pixTransferFailed
= 0;
45 QPixmap
*MediaItem::s_pixTransferBegin
= 0;
46 QPixmap
*MediaItem::s_pixTransferEnd
= 0;
48 MediaItem::MediaItem( Q3ListView
* parent
)
49 : K3ListViewItem( parent
)
54 MediaItem::MediaItem( Q3ListViewItem
* parent
)
55 : K3ListViewItem( parent
)
60 MediaItem::MediaItem( Q3ListView
* parent
, Q3ListViewItem
* after
)
61 : K3ListViewItem( parent
, after
)
66 MediaItem::MediaItem( Q3ListViewItem
* parent
, Q3ListViewItem
* after
)
67 : K3ListViewItem( parent
, after
)
72 MediaItem::~MediaItem()
80 // preload pixmaps used in browser
81 KIconLoader iconLoader
;
82 MediaItem::s_pixUnknown
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "unknown" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
));
83 MediaItem::s_pixTrack
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "playlist" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
));
84 MediaItem::s_pixFile
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "sound" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
85 MediaItem::s_pixPodcast
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "podcast" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
86 MediaItem::s_pixPlaylist
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "playlist" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
87 MediaItem::s_pixRootItem
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "files2" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
96 MediaItem::s_pixArtist
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "personal" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
97 MediaItem::s_pixAlbum
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cdrom_unmount" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
98 MediaItem::s_pixInvisible
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
99 MediaItem::s_pixStale
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
100 MediaItem::s_pixOrphaned
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
101 MediaItem::s_pixDirectory
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "folder" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
102 MediaItem::s_pixTransferBegin
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "play" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
103 MediaItem::s_pixTransferEnd
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "process-stop" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
104 MediaItem::s_pixTransferFailed
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
109 m_playlistName
.clear();
112 setExpandable( false );
113 setDragEnabled( true );
114 setDropEnabled( true );
118 MediaItem::setBundle( MetaBundle
*bundle
)
120 MediaBrowser::instance()->m_itemMapMutex
.lock();
123 QString itemUrl
= url().url();
124 MediaBrowser::ItemMap::iterator it
= MediaBrowser::instance()->m_itemMap
.find( itemUrl
);
125 if( it
!= MediaBrowser::instance()->m_itemMap
.end() && *it
== this )
126 MediaBrowser::instance()->m_itemMap
.remove( itemUrl
);
133 QString itemUrl
= url().url();
134 MediaBrowser::ItemMap::iterator it
= MediaBrowser::instance()->m_itemMap
.find( itemUrl
);
135 if( it
== MediaBrowser::instance()->m_itemMap
.end() )
136 MediaBrowser::instance()->m_itemMap
[itemUrl
] = this;
138 MediaBrowser::instance()->m_itemMapMutex
.unlock();
142 void MediaItem::paintCell( QPainter
*p
, const QColorGroup
&cg
, int column
, int width
, int align
)
152 QFont
font( p
->font() );
153 font
.setBold( true );
160 K3ListViewItem::paintCell( p
, cg
, column
, width
, align
);
164 MediaItem::bundle() const
170 MediaItem::url() const
173 return bundle()->url();
179 MediaItem::isFileBacked() const
209 MediaItem::syncStatsFromPath( const QString
&url
)
214 // copy Amarok rating, play count and last played time to device
215 int rating
= CollectionDB::instance()->getSongRating( url
)*10;
218 int playcount
= CollectionDB::instance()->getPlayCount( url
);
219 if( playcount
> played() )
220 setPlayCount( playcount
);
221 QDateTime lastplay
= CollectionDB::instance()->getLastPlay( url
);
222 if( lastplay
> playTime() )
223 setLastPlayed( lastplay
.toTime_t() );
227 MediaItem::size() const
229 if( !isFileBacked() )
233 return bundle()->filesize();
239 MediaItem::setType( Type type
)
243 setDragEnabled(true);
244 setDropEnabled(false);
249 setPixmap(0, *s_pixUnknown
);
253 setPixmap(0, *s_pixFile
);
256 setPixmap(0, *s_pixTrack
);
257 setDropEnabled(true);
260 setPixmap(0, *s_pixArtist
);
263 setPixmap(0, *s_pixAlbum
);
266 setPixmap(0, *s_pixRootItem
);
270 setPixmap(0, *s_pixPodcast
);
273 setPixmap(0, *s_pixPlaylist
);
274 setDropEnabled(true);
277 setPixmap(0, *s_pixRootItem
);
278 setDropEnabled( true );
281 setPixmap(0, *s_pixInvisible
);
285 setPixmap(0, *s_pixStale
);
289 setPixmap(0, *s_pixOrphaned
);
292 setExpandable( true );
293 setDropEnabled( true );
294 setPixmap(0, *s_pixDirectory
);
301 MediaItem::setFailed( bool failed
)
305 m_flags
&= ~MediaItem::Transferring
;
306 m_flags
|= MediaItem::Failed
;
307 setPixmap(0, *MediaItem::s_pixTransferFailed
);
311 m_flags
&= ~MediaItem::Failed
;
312 if( m_type
== PODCASTITEM
)
313 setPixmap(0, *s_pixPodcast
);
314 else if( m_type
== PLAYLIST
)
315 setPixmap(0, *s_pixPlaylist
);
317 setPixmap(0, QPixmap() );
322 MediaItem::lastChild() const
324 Q3ListViewItem
*last
= 0;
325 for( Q3ListViewItem
*it
= firstChild();
327 it
= it
->nextSibling() )
332 return dynamic_cast<MediaItem
*>(last
);
336 MediaItem::isLeafItem() const
368 MediaItem::findItem( const QString
&key
, const MediaItem
*after
) const
372 it
= dynamic_cast<MediaItem
*>( after
->nextSibling() );
374 it
= dynamic_cast<MediaItem
*>( firstChild() );
376 for( ; it
; it
= dynamic_cast<MediaItem
*>(it
->nextSibling()))
378 if(key
== it
->text(0))
380 if(key
.isEmpty() && it
->text(0).isEmpty())
387 MediaItem::compare( Q3ListViewItem
*i
, int col
, bool ascending
) const
389 MediaItem
*item
= dynamic_cast<MediaItem
*>(i
);
390 if(item
&& col
==0 && item
->m_order
!= m_order
)
391 return m_order
-item
->m_order
;
392 else if( item
&& item
->type() == MediaItem::ARTIST
)
394 QString key1
= key( col
, ascending
);
395 if( key1
.startsWith( "the ", Qt::CaseInsensitive
) )
396 key1
= key1
.mid( 4 );
397 QString key2
= i
->key( col
, ascending
);
398 if( key2
.startsWith( "the ", Qt::CaseInsensitive
) )
399 key2
= key2
.mid( 4 );
401 return key1
.localeAwareCompare( key2
);
404 return K3ListViewItem::compare(i
, col
, ascending
);
408 MediaItem::createToolTip()
413 case MediaItem::TRACK
:
415 const MetaBundle
*b
= bundle();
419 text
= QString( "%1 - %2 (%3)" )
420 .arg( QString::number(b
->track()), b
->title(), b
->prettyLength() );
421 if( !b
->genre().isEmpty() )
423 if( !text
.isEmpty() )
425 text
+= QString( "<i>Genre: %1</i>" )
431 case MediaItem::PLAYLISTSROOT
:
432 text
= i18n( "Drag items here to create new playlist" );
434 case MediaItem::PLAYLIST
:
435 text
= i18n( "Drag items here to append to this playlist" );
437 case MediaItem::PLAYLISTITEM
:
438 text
= i18n( "Drag items here to insert before this item" );
440 case MediaItem::INVISIBLEROOT
:
441 case MediaItem::INVISIBLE
:
442 text
= i18n( "Not visible on media device" );
444 case MediaItem::STALEROOT
:
445 case MediaItem::STALE
:
446 text
= i18n( "In device database, but file is missing" );
448 case MediaItem::ORPHANEDROOT
:
449 case MediaItem::ORPHANED
:
450 text
= i18n( "File on device, but not in device database" );
456 //if( !text.isEmpty() ) setToolTip( text );