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 "mediabrowser.h"
25 #include "MediaItem.h"
27 #include <kiconloader.h>
31 QPixmap
*MediaItem::s_pixUnknown
= 0;
32 QPixmap
*MediaItem::s_pixArtist
= 0;
33 QPixmap
*MediaItem::s_pixAlbum
= 0;
34 QPixmap
*MediaItem::s_pixFile
= 0;
35 QPixmap
*MediaItem::s_pixTrack
= 0;
36 QPixmap
*MediaItem::s_pixPodcast
= 0;
37 QPixmap
*MediaItem::s_pixPlaylist
= 0;
38 QPixmap
*MediaItem::s_pixInvisible
= 0;
39 QPixmap
*MediaItem::s_pixStale
= 0;
40 QPixmap
*MediaItem::s_pixOrphaned
= 0;
41 QPixmap
*MediaItem::s_pixDirectory
= 0;
42 QPixmap
*MediaItem::s_pixRootItem
= 0;
43 QPixmap
*MediaItem::s_pixTransferFailed
= 0;
44 QPixmap
*MediaItem::s_pixTransferBegin
= 0;
45 QPixmap
*MediaItem::s_pixTransferEnd
= 0;
47 MediaItem::MediaItem( Q3ListView
* parent
)
48 : K3ListViewItem( parent
)
53 MediaItem::MediaItem( Q3ListViewItem
* parent
)
54 : K3ListViewItem( parent
)
59 MediaItem::MediaItem( Q3ListView
* parent
, Q3ListViewItem
* after
)
60 : K3ListViewItem( parent
, after
)
65 MediaItem::MediaItem( Q3ListViewItem
* parent
, Q3ListViewItem
* after
)
66 : K3ListViewItem( parent
, after
)
71 MediaItem::~MediaItem()
79 // preload pixmaps used in browser
80 KIconLoader iconLoader
;
81 MediaItem::s_pixUnknown
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "unknown" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
));
82 MediaItem::s_pixTrack
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "playlist" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
));
83 MediaItem::s_pixFile
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "sound" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
84 MediaItem::s_pixPodcast
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "podcast" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
85 MediaItem::s_pixPlaylist
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "playlist" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
86 MediaItem::s_pixRootItem
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "files2" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
95 MediaItem::s_pixArtist
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "personal" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
96 MediaItem::s_pixAlbum
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cdrom_unmount" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
97 MediaItem::s_pixInvisible
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
98 MediaItem::s_pixStale
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
99 MediaItem::s_pixOrphaned
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
100 MediaItem::s_pixDirectory
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "folder" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
101 MediaItem::s_pixTransferBegin
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "play" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
102 MediaItem::s_pixTransferEnd
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "process-stop" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
103 MediaItem::s_pixTransferFailed
= new QPixmap(iconLoader
.loadIcon( Amarok::icon( "cancel" ), KIconLoader::Toolbar
, KIconLoader::SizeSmall
) );
108 m_playlistName
.clear();
111 setExpandable( false );
112 setDragEnabled( true );
113 setDropEnabled( true );
117 MediaItem::setBundle( MetaBundle
*bundle
)
119 MediaBrowser::instance()->m_itemMapMutex
.lock();
122 QString itemUrl
= url().url();
123 MediaBrowser::ItemMap::iterator it
= MediaBrowser::instance()->m_itemMap
.find( itemUrl
);
124 if( it
!= MediaBrowser::instance()->m_itemMap
.end() && *it
== this )
125 MediaBrowser::instance()->m_itemMap
.remove( itemUrl
);
132 QString itemUrl
= url().url();
133 MediaBrowser::ItemMap::iterator it
= MediaBrowser::instance()->m_itemMap
.find( itemUrl
);
134 if( it
== MediaBrowser::instance()->m_itemMap
.end() )
135 MediaBrowser::instance()->m_itemMap
[itemUrl
] = this;
137 MediaBrowser::instance()->m_itemMapMutex
.unlock();
141 void MediaItem::paintCell( QPainter
*p
, const QColorGroup
&cg
, int column
, int width
, int align
)
151 QFont
font( p
->font() );
152 font
.setBold( true );
159 K3ListViewItem::paintCell( p
, cg
, column
, width
, align
);
163 MediaItem::bundle() const
169 MediaItem::url() const
172 return bundle()->url();
178 MediaItem::isFileBacked() const
208 MediaItem::size() const
210 if( !isFileBacked() )
214 return bundle()->filesize();
220 MediaItem::setType( Type type
)
224 setDragEnabled(true);
225 setDropEnabled(false);
230 setPixmap(0, *s_pixUnknown
);
234 setPixmap(0, *s_pixFile
);
237 setPixmap(0, *s_pixTrack
);
238 setDropEnabled(true);
241 setPixmap(0, *s_pixArtist
);
244 setPixmap(0, *s_pixAlbum
);
247 setPixmap(0, *s_pixRootItem
);
251 setPixmap(0, *s_pixPodcast
);
254 setPixmap(0, *s_pixPlaylist
);
255 setDropEnabled(true);
258 setPixmap(0, *s_pixRootItem
);
259 setDropEnabled( true );
262 setPixmap(0, *s_pixInvisible
);
266 setPixmap(0, *s_pixStale
);
270 setPixmap(0, *s_pixOrphaned
);
273 setExpandable( true );
274 setDropEnabled( true );
275 setPixmap(0, *s_pixDirectory
);
282 MediaItem::setFailed( bool failed
)
286 m_flags
&= ~MediaItem::Transferring
;
287 m_flags
|= MediaItem::Failed
;
288 setPixmap(0, *MediaItem::s_pixTransferFailed
);
292 m_flags
&= ~MediaItem::Failed
;
293 if( m_type
== PODCASTITEM
)
294 setPixmap(0, *s_pixPodcast
);
295 else if( m_type
== PLAYLIST
)
296 setPixmap(0, *s_pixPlaylist
);
298 setPixmap(0, QPixmap() );
303 MediaItem::lastChild() const
305 Q3ListViewItem
*last
= 0;
306 for( Q3ListViewItem
*it
= firstChild();
308 it
= it
->nextSibling() )
313 return dynamic_cast<MediaItem
*>(last
);
317 MediaItem::isLeafItem() const
349 MediaItem::findItem( const QString
&key
, const MediaItem
*after
) const
353 it
= dynamic_cast<MediaItem
*>( after
->nextSibling() );
355 it
= dynamic_cast<MediaItem
*>( firstChild() );
357 for( ; it
; it
= dynamic_cast<MediaItem
*>(it
->nextSibling()))
359 if(key
== it
->text(0))
361 if(key
.isEmpty() && it
->text(0).isEmpty())
368 MediaItem::compare( Q3ListViewItem
*i
, int col
, bool ascending
) const
370 MediaItem
*item
= dynamic_cast<MediaItem
*>(i
);
371 if(item
&& col
==0 && item
->m_order
!= m_order
)
372 return m_order
-item
->m_order
;
373 else if( item
&& item
->type() == MediaItem::ARTIST
)
375 QString key1
= key( col
, ascending
);
376 if( key1
.startsWith( "the ", Qt::CaseInsensitive
) )
377 key1
= key1
.mid( 4 );
378 QString key2
= i
->key( col
, ascending
);
379 if( key2
.startsWith( "the ", Qt::CaseInsensitive
) )
380 key2
= key2
.mid( 4 );
382 return key1
.localeAwareCompare( key2
);
385 return K3ListViewItem::compare(i
, col
, ascending
);
389 MediaItem::createToolTip()
394 case MediaItem::TRACK
:
396 const MetaBundle
*b
= bundle();
400 text
= QString( "%1 - %2 (%3)" )
401 .arg( QString::number(b
->track()), b
->title(), b
->prettyLength() );
402 if( !b
->genre().isEmpty() )
404 if( !text
.isEmpty() )
406 text
+= QString( "<i>Genre: %1</i>" )
412 case MediaItem::PLAYLISTSROOT
:
413 text
= i18n( "Drag items here to create new playlist" );
415 case MediaItem::PLAYLIST
:
416 text
= i18n( "Drag items here to append to this playlist" );
418 case MediaItem::PLAYLISTITEM
:
419 text
= i18n( "Drag items here to insert before this item" );
421 case MediaItem::INVISIBLEROOT
:
422 case MediaItem::INVISIBLE
:
423 text
= i18n( "Not visible on media device" );
425 case MediaItem::STALEROOT
:
426 case MediaItem::STALE
:
427 text
= i18n( "In device database, but file is missing" );
429 case MediaItem::ORPHANEDROOT
:
430 case MediaItem::ORPHANED
:
431 text
= i18n( "File on device, but not in device database" );
437 //if( !text.isEmpty() ) setToolTip( text );