Amarok – versatile and easy to use audio player
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3824 lines
112 KiB

// (c) 2004 Christian Muehlhaeuser <chris@chris.de>
// (c) 2005-2006 Martin Aumueller <aumuell@reserv.at>
// (c) 2005 Seb Ruiz <me@sebruiz.net>
// (c) 2006 T.R.Shashwath <trshash84@gmail.com>
// See COPYING file for licensing information
#define DEBUG_PREFIX "MediaBrowser"
#include <config.h>
#include "amarok.h"
#include "amarokconfig.h"
#include "app.h"
#include "browserToolBar.h"
#include "clicklineedit.h"
#include "collectiondb.h"
#include "colorgenerator.h"
#include "contextbrowser.h"
#include "debug.h"
#include "editfilterdialog.h"
#include "deviceconfiguredialog.h"
#include "mediadevicemanager.h"
#include "expression.h"
#include "hintlineedit.h"
#include "mediabrowser.h"
#include "medium.h"
#include "mediumpluginmanager.h"
#include "metabundle.h"
#include "mountpointmanager.h"
#include "playlist.h"
#include "playlistbrowser.h"
#include "playlistbrowseritem.h"
#include "playlistloader.h"
#include "pluginmanager.h"
#include "podcastbundle.h"
#include "scriptmanager.h"
#include "scrobbler.h"
#include "statusbar.h"
#include "transferdialog.h"
#include "browserToolBar.h"
#include <tqvbuttongroup.h>
#include <tqcheckbox.h>
#include <tqdatetime.h>
#include <tqdir.h>
#include <tqdom.h>
#include <tqfileinfo.h>
#include <tqgroupbox.h>
#include <tqheader.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqobjectlist.h>
#include <tqpainter.h>
#include <tqradiobutton.h>
#include <tqsimplerichtext.h>
#include <tqtimer.h>
#include <tqtooltip.h> //TQToolTip::add()
#include <tdeapplication.h> //kapp
#include <kcombobox.h>
#include <kdirlister.h>
#include <tdefiledialog.h>
#include <tdeglobal.h>
#include <kiconloader.h>
#include <kinputdialog.h>
#include <tdeio/job.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <tdemultipledrag.h>
#include <tdepopupmenu.h>
#include <kprocess.h>
#include <kprogress.h>
#include <kpushbutton.h>
#include <krun.h>
#include <kstandarddirs.h> //locate file
#include <ktabbar.h>
#include <tdetempfile.h>
#include <tdetoolbarbutton.h> //ctor
#include <kurldrag.h> //dragObject()
#include <tdeactioncollection.h>
MediaBrowser *MediaBrowser::s_instance = 0;
TQPixmap *MediaItem::s_pixUnknown = 0;
TQPixmap *MediaItem::s_pixArtist = 0;
TQPixmap *MediaItem::s_pixAlbum = 0;
TQPixmap *MediaItem::s_pixFile = 0;
TQPixmap *MediaItem::s_pixTrack = 0;
TQPixmap *MediaItem::s_pixPodcast = 0;
TQPixmap *MediaItem::s_pixPlaylist = 0;
TQPixmap *MediaItem::s_pixInvisible = 0;
TQPixmap *MediaItem::s_pixStale = 0;
TQPixmap *MediaItem::s_pixOrphaned = 0;
TQPixmap *MediaItem::s_pixDirectory = 0;
TQPixmap *MediaItem::s_pixRootItem = 0;
TQPixmap *MediaItem::s_pixTransferFailed = 0;
TQPixmap *MediaItem::s_pixTransferBegin = 0;
TQPixmap *MediaItem::s_pixTransferEnd = 0;
bool MediaBrowser::isAvailable() //static
{
if( !MediaBrowser::instance() )
return false;
return true;
//to re-enable hiding, uncomment this and get rid of the return true above:
//return MediaBrowser::instance()->m_haveDevices;
}
class SpaceLabel : public TQLabel {
public:
SpaceLabel(TQWidget *parent)
: TQLabel(parent)
{
m_total = m_used = m_scheduled = 0;
setBackgroundMode(TQt::NoBackground);
}
void paintEvent(TQPaintEvent *e)
{
TQPainter p(this);
p.fillRect(e->rect(), colorGroup().brush(TQColorGroup::Background));
if(m_total > 0)
{
int used = int(float(m_used)/float(m_total)*width());
int scheduled = int(float(m_used + m_scheduled)/float(m_total)*width());
if(m_used > 0)
{
TQColor blueish(70,120,255);
if(e->rect().left() < used)
{
int right = used;
if(e->rect().right() < right)
right = e->rect().right();
p.fillRect(e->rect().left(), e->rect().top(),
used, e->rect().bottom()+1, TQBrush(blueish, TQt::SolidPattern));
}
}
if(m_scheduled > 0)
{
TQColor sched(70, 230, 120);
if(m_used + m_scheduled > m_total - m_total/200)
{
sched.setRgb( 255, 120, 120 );
}
int left = e->rect().left();
if(used > left)
left = used;
int right = e->rect().right();
if(scheduled < right)
right = scheduled;
p.fillRect(left, e->rect().top(), right, e->rect().bottom()+1, TQBrush(sched, TQt::SolidPattern));
}
if(m_used + m_scheduled < m_total)
{
TQColor grey(180, 180, 180);
int left = e->rect().left();
if(scheduled > left)
left = scheduled;
int right = e->rect().right();
p.fillRect(left, e->rect().top(), right, e->rect().bottom()+1, colorGroup().brush(TQColorGroup::Background));
}
}
TQLabel::paintEvent(e);
}
TDEIO::filesize_t m_total;
TDEIO::filesize_t m_used;
TDEIO::filesize_t m_scheduled;
};
class DummyMediaDevice : public MediaDevice
{
public:
DummyMediaDevice() : MediaDevice()
{
m_name = i18n( "No Device Available" );
m_type = "dummy-mediadevice";
m_medium = Medium( "DummyDevice", "DummyDevice" );
}
void init( MediaBrowser *browser ) { MediaDevice::init( browser ); }
virtual ~DummyMediaDevice() {}
virtual bool isConnected() { return false; }
virtual MediaItem* trackExists(const MetaBundle&) { return 0; }
virtual bool lockDevice(bool) { return true; }
virtual void unlockDevice() {}
virtual bool openDevice( bool silent )
{
if( !silent )
{
//TQString msg = i18n( "Sorry, you do not have a supported portable music player." );
//Amarok::StatusBar::instance()->longMessage( msg, KDE::StatusBar::Sorry );
}
return false;
}
virtual bool closeDevice() { return false; }
virtual void synchronizeDevice() {}
virtual MediaItem* copyTrackToDevice(const MetaBundle&) { return 0; }
virtual int deleteItemFromDevice(MediaItem*, int) { return -1; }
};
MediaBrowser::MediaBrowser( const char *name )
: TQVBox( 0, name )
, m_timer( new TQTimer( this ) )
, m_currentDevice( m_devices.end() )
, m_waitForTranscode( false )
, m_quitting( false )
{
s_instance = this;
// preload pixmaps used in browser
TDEIconLoader iconLoader;
MediaItem::s_pixUnknown = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "unknown" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ));
MediaItem::s_pixTrack = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "playlist" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ));
MediaItem::s_pixFile = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "sound" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixPodcast = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "podcast" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixPlaylist = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "playlist" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixRootItem = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "files2" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
// history
// favorites
// collection
// folder
// folder_red
// player_playlist_2
// cancel
// sound
MediaItem::s_pixArtist = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "personal" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixAlbum = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "media-optical-cdrom" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixInvisible = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "cancel" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixStale = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "cancel" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixOrphaned = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "cancel" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixDirectory = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "folder" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixTransferBegin = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "play" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixTransferEnd = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "stop" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
MediaItem::s_pixTransferFailed = new TQPixmap(iconLoader.loadIcon( Amarok::icon( "cancel" ), TDEIcon::Toolbar, TDEIcon::SizeSmall ) );
setSpacing( 4 );
m_toolbar = new Browser::ToolBar( this );
m_toolbar->setIconText( TDEToolBar::IconTextRight, false );
m_toolbar->insertButton( "connect_creating", CONNECT, true, i18n("Connect") );
TQToolTip::add( m_toolbar->getButton(CONNECT), i18n( "Connect media device" ) );
m_toolbar->insertButton( "player_eject", DISCONNECT, true, i18n("Disconnect") );
TQToolTip::add( m_toolbar->getButton(DISCONNECT), i18n( "Disconnect media device" ) );
m_toolbar->insertButton( "rebuild", TRANSFER, true, i18n("Transfer") );
TQToolTip::add( m_toolbar->getButton(TRANSFER), i18n( "Transfer tracks to media device" ) );
m_toolbar->insertLineSeparator();
// m_toolbar->setIconText( TDEToolBar::IconTextRight, true );
m_toolbar->insertButton( Amarok::icon( "add_playlist" ), CUSTOM, TQT_SIGNAL( clicked() ), TQT_TQOBJECT(this), TQT_SLOT( customClicked() ), true, "custom" );
TQToolTip::add( m_toolbar->getButton(TRANSFER), i18n( "Transfer tracks to media device" ) );
m_toolbar->setIconText( TDEToolBar::IconOnly, false );
m_toolbar->insertButton( Amarok::icon( "configure" ), CONFIGURE, true, i18n("Configure") );
TQToolTip::add( m_toolbar->getButton(CONFIGURE), i18n( "Configure device" ) );
m_deviceCombo = new KComboBox( this );
// searching/filtering
{ //<Search LineEdit>
TDEToolBar* searchToolBar = new Browser::ToolBar( this );
TDEToolBarButton *button = new TDEToolBarButton( "locationbar_erase", 0, searchToolBar );
m_searchEdit = new ClickLineEdit( i18n( "Enter search terms here" ), searchToolBar );
KPushButton *filterButton = new KPushButton("...", searchToolBar, "filter");
searchToolBar->setStretchableWidget( m_searchEdit );
m_searchEdit->setFrame( TQFrame::Sunken );
connect( button, TQT_SIGNAL( clicked() ), m_searchEdit, TQT_SLOT( clear() ) );
connect( filterButton, TQT_SIGNAL( clicked() ), TQT_SLOT( slotEditFilter() ) );
TQToolTip::add( button, i18n( "Clear filter" ) );
TQToolTip::add( m_searchEdit, i18n( "Enter space-separated terms to search" ) );
TQToolTip::add( filterButton, i18n( "Click to edit filter" ) );
} //</Search LineEdit>
connect( m_timer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotSetFilter() ) );
connect( m_searchEdit, TQT_SIGNAL( textChanged( const TQString& ) ), TQT_SLOT( slotSetFilterTimeout() ) );
connect( m_searchEdit, TQT_SIGNAL( returnPressed() ), TQT_SLOT( slotSetFilter() ) );
// connect to device manager
connect( MediaDeviceManager::instance(), TQT_SIGNAL( mediumAdded(const Medium *, TQString) ),
TQT_SLOT( mediumAdded(const Medium *, TQString) ) );
connect( MediaDeviceManager::instance(), TQT_SIGNAL( mediumChanged(const Medium *, TQString) ),
TQT_SLOT( mediumChanged(const Medium *, TQString) ) );
connect( MediaDeviceManager::instance(), TQT_SIGNAL( mediumRemoved(const Medium *, TQString) ),
TQT_SLOT( mediumRemoved(const Medium *, TQString) ) );
// we always have a dummy device
m_pluginName[ i18n( "Disable" ) ] = "dummy-mediadevice";
m_pluginAmarokName["dummy-mediadevice"] = i18n( "Disable" );
m_pluginName[ i18n( "Do not handle" ) ] = "ignore";
m_pluginAmarokName["ignore"] = i18n( "Do not handle" );
// query available device plugins
m_plugins = PluginManager::query( "[X-TDE-Amarok-plugintype] == 'mediadevice'" );
for( TDETrader::OfferList::ConstIterator it = m_plugins.begin(); it != m_plugins.end(); ++it ) {
// Save name properties in TQMap for lookup
m_pluginName[(*it)->name()] = (*it)->property( "X-TDE-Amarok-name" ).toString();
m_pluginAmarokName[(*it)->property( "X-TDE-Amarok-name" ).toString()] = (*it)->name();
}
m_views = new TQVBox( this );
m_queue = new MediaQueue( this );
m_progressBox = new TQHBox( this );
m_progress = new KProgress( m_progressBox );
m_cancelButton = new KPushButton( SmallIconSet( Amarok::icon( "cancel" ) ), i18n("Cancel"), m_progressBox );
m_stats = new SpaceLabel(this);
m_progressBox->hide();
MediaDevice *dev = new DummyMediaDevice();
dev->init( this );
addDevice( dev );
activateDevice( 0, false );
queue()->load( Amarok::saveLocation() + "transferlist.xml" );
queue()->computeSize();
setFocusProxy( m_queue );
updateStats();
TQMap<TQString, Medium*> mmap = MediaDeviceManager::instance()->getMediumMap();
bool newflag = false;
//This deals with <strike>auto-detectable</strike> ALL devices!
for( TQMap<TQString, Medium*>::Iterator it = mmap.begin();
it != mmap.end();
it++ )
{
TQString handler = Amarok::config( "MediaBrowser" )->readEntry( (*it)->id() );
//debug() << "[MediaBrowser] (*it)->id() = " << (*it)->id() << ", handler = " << handler << endl;
if( handler.isEmpty() )
{
//this should probably never be the case with a manually added device, unless amarokrc's been messed with
Amarok::config( "MediaBrowser" )->writeEntry( (*it)->id(), "ignore" );
newflag = true;
mediumAdded( *it, (*it)->name(), true );
}
//and this definitely shouldn't!
else if( handler != "deleted" )
mediumAdded( *it, (*it)->name(), true );
}
if ( newflag )
Amarok::StatusBar::instance()->longMessageThreadSafe(
i18n("Amarok has detected new portable media devices.\n"
"Go to the \"Media Devices\" pane of the configuration\n"
"dialog to choose a plugin for these devices.") );
connect( m_toolbar->getButton(CONNECT), TQT_SIGNAL( clicked() ), TQT_SLOT( connectClicked() ) );
connect( m_toolbar->getButton(DISCONNECT), TQT_SIGNAL( clicked() ), TQT_SLOT( disconnectClicked() ) );
connect( m_toolbar->getButton(TRANSFER), TQT_SIGNAL( clicked() ), TQT_SLOT( transferClicked() ) );
connect( m_toolbar->getButton(CONFIGURE), TQT_SIGNAL( clicked() ), TQT_SLOT( config() ) );
connect( m_deviceCombo, TQT_SIGNAL( activated( int ) ), TQT_SLOT( activateDevice( int ) ) );
connect( m_cancelButton, TQT_SIGNAL( clicked() ), TQT_SLOT( cancelClicked() ) );
connect( pApp, TQT_SIGNAL( prepareToQuit() ), TQT_SLOT( prepareToQuit() ) );
connect( CollectionDB::instance(), TQT_SIGNAL( tagsChanged( const MetaBundle& ) ),
TQT_SLOT( tagsChanged( const MetaBundle& ) ) );
m_haveDevices = false;
TQMap<TQString,TQString> savedDevices = Amarok::config( "MediaBrowser" )->entryMap( "MediaBrowser" );
for( TQMap<TQString,TQString>::Iterator it = savedDevices.begin();
it != savedDevices.end();
++it )
{
if( it.data() != "deleted" && it.data() != "ignore" )
{
m_haveDevices = true;
break;
}
}
emit availabilityChanged( m_haveDevices );
}
bool
MediaBrowser::blockQuit() const
{
for( TQValueList<MediaDevice *>::const_iterator it = m_devices.begin();
it != m_devices.end();
++it )
{
if( *it && (*it)->isConnected() )
return true;
}
return false;
}
void
MediaBrowser::tagsChanged( const MetaBundle &bundle )
{
m_itemMapMutex.lock();
debug() << "tags changed for " << bundle.url().url() << endl;
ItemMap::iterator it = m_itemMap.find( bundle.url().url() );
if( it != m_itemMap.end() )
{
MediaItem *item = *it;
m_itemMapMutex.unlock();
if( item->device() )
{
item->device()->tagsChanged( item, bundle );
}
else
{
// it's an item on the transfer queue
item->setBundle( new MetaBundle( bundle ) );
TQString text = item->bundle()->prettyTitle();
if( text.isEmpty() || (!item->bundle()->isValidMedia() && !item->bundle()->podcastBundle()) )
text = item->bundle()->url().prettyURL();
if( !item->m_playlistName.isNull() )
{
text += " (" + item->m_playlistName + ')';
}
item->setText( 0, text);
}
}
else
{
m_itemMapMutex.unlock();
}
}
bool
MediaBrowser::getBundle( const KURL &url, MetaBundle *bundle ) const
{
TQMutexLocker locker( &m_itemMapMutex );
ItemMap::const_iterator it = m_itemMap.find( url.url() );
if( it == m_itemMap.end() )
return false;
if( bundle )
*bundle = TQDeepCopy<MetaBundle>( *(*it)->bundle() );
return true;
}
KURL
MediaBrowser::getProxyUrl( const KURL& daapUrl ) const
{
DEBUG_BLOCK
KURL url;
MediaDevice* dc = dynamic_cast<MediaDevice*>( queryList( "DaapClient" )->getFirst() );
if( dc )
url = dc->getProxyUrl( daapUrl );
return url;
}
MediaDevice *
MediaBrowser::currentDevice() const
{
TQValueList<MediaDevice *>::const_iterator current = m_currentDevice;
if( current != m_devices.constEnd() )
{
return *m_currentDevice;
}
return 0;
}
MediaDevice *
MediaBrowser::deviceFromId( const TQString &id ) const
{
for( TQValueList<MediaDevice *>::const_iterator it = m_devices.constBegin();
it != m_devices.end();
it++ )
{
if( (*it)->uniqueId() == id )
return (*it);
}
return NULL;
}
void
MediaBrowser::activateDevice( const MediaDevice *dev )
{
int index = 0;
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
if( *it == dev )
{
activateDevice( index );
break;
}
index++;
}
}
void
MediaBrowser::activateDevice( int index, bool skipDummy )
{
if( currentDevice() && currentDevice()->customAction() )
{
currentDevice()->customAction()->unplug( m_toolbar );
m_toolbar->hide();
m_toolbar->show();
}
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
(*it)->view()->hide();
}
if( index < 0 )
{
m_currentDevice = m_devices.end();
return;
}
if( skipDummy )
index++;
if( (uint)index >= m_devices.count() )
{
m_currentDevice = m_devices.end();
updateButtons();
queue()->computeSize();
updateStats();
return;
}
m_currentDevice = m_devices.at( index );
if( currentDevice() )
{
currentDevice()->view()->show();
if( currentDevice()->customAction() )
{
m_toolbar->setIconText( TDEToolBar::IconTextRight, false );
currentDevice()->customAction()->plug( m_toolbar );
m_toolbar->hide();
m_toolbar->show();
}
}
m_deviceCombo->setCurrentItem( index-1 );
updateButtons();
queue()->computeSize();
updateStats();
}
void
MediaBrowser::addDevice( MediaDevice *device )
{
m_devices.append( device );
device->loadConfig();
if( device->autoConnect() )
{
device->connectDevice( true );
updateButtons();
}
updateDevices();
}
void
MediaBrowser::removeDevice( MediaDevice *device )
{
DEBUG_BLOCK
debug() << "remove device: type=" << device->deviceType() << endl;
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
if( *it == device )
{
bool current = (it == m_currentDevice);
m_devices.remove( device );
if( current )
activateDevice( 0, false );
break;
}
}
if( device->isConnected() )
{
if( device->disconnectDevice( false /* don't run post-disconnect command */ ) )
unloadDevicePlugin( device );
else
{
debug() << "Cannot remove device because disconnect failed" << endl;
Amarok::StatusBar::instance()->longMessage(
i18n( "Cannot remove device because disconnect failed" ),
KDE::StatusBar::Warning );
}
}
else
unloadDevicePlugin( device );
updateDevices();
}
void
MediaBrowser::updateDevices()
{
m_deviceCombo->clear();
uint i = 0;
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
if( m_devices.count() > 1 && dynamic_cast<DummyMediaDevice *>(*it) )
continue;
TQString name = (*it)->name();
if( !(*it)->deviceNode().isEmpty() )
{
name = i18n( "%1 at %2" ).arg( name, (*it)->deviceNode() );
}
if( (*it)->hasMountPoint() && !(*it)->mountPoint().isEmpty() )
{
name += i18n( " (mounted at %1)" ).arg( (*it)->mountPoint() );
}
m_deviceCombo->insertItem( name, i );
if( it == m_currentDevice )
{
m_deviceCombo->setCurrentItem( i );
}
i++;
}
m_deviceCombo->setEnabled( m_devices.count() > 1 );
m_haveDevices = m_devices.count() > 1;
emit availabilityChanged( m_haveDevices );
}
TQStringList
MediaBrowser::deviceNames() const
{
TQStringList list;
for( TQValueList<MediaDevice *>::const_iterator it = m_devices.constBegin();
it != m_devices.constEnd();
it++ )
{
TQString name = (*it)->name();
list << name;
}
return list;
}
bool
MediaBrowser::deviceSwitch( const TQString &name )
{
int index = 0;
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
if( (*it)->name() == name )
{
activateDevice( index, false );
return true;
}
index++;
}
return false;
}
void
MediaBrowser::transcodingFinished( const TQString &src, const TQString &dst )
{
KURL srcJob = KURL::fromPathOrURL( m_transcodeSrc );
KURL srcResult = KURL::fromPathOrURL( src );
if( srcJob.path() == srcResult.path() )
{
m_transcodedUrl = KURL::fromPathOrURL( dst );
m_waitForTranscode = false;
}
else
{
debug() << "transcoding for " << src << " finished, "
<< "but we are waiting for " << m_transcodeSrc << " -- aborting" << endl;
m_waitForTranscode = false;
}
}
KURL
MediaBrowser::transcode( const KURL &src, const TQString &filetype )
{
const ScriptManager* const sm = ScriptManager::instance();
if( sm->transcodeScriptRunning().isEmpty() )
{
debug() << "cannot transcode with no transcoder registered" << endl;
return KURL();
}
m_waitForTranscode = true;
m_transcodeSrc = src.url();
m_transcodedUrl = KURL();
ScriptManager::instance()->notifyTranscode( src.url(), filetype );
while( m_waitForTranscode && sm->transcodeScriptRunning() != TQString() )
{
usleep( 10000 );
kapp->processEvents( 100 );
}
return m_transcodedUrl;
}
void
MediaBrowser::slotSetFilterTimeout() //SLOT
{
m_timer->start( 280, true ); //stops the timer for us first
}
void
MediaBrowser::slotSetFilter() //SLOT
{
m_timer->stop();
if( currentDevice() )
currentDevice()->view()->setFilter( m_searchEdit->text() );
}
void
MediaBrowser::slotSetFilter( const TQString &text )
{
m_searchEdit->setText( text );
slotSetFilter();
}
void
MediaBrowser::slotEditFilter()
{
EditFilterDialog *fd = new EditFilterDialog( this, true, m_searchEdit->text() );
connect( fd, TQT_SIGNAL(filterChanged(const TQString &)), TQT_SLOT(slotSetFilter(const TQString &)) );
if( fd->exec() )
m_searchEdit->setText( fd->filter() );
delete fd;
}
void
MediaBrowser::prepareToQuit()
{
m_waitForTranscode = false;
m_quitting = true;
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
++it )
{
if( (*it)->isConnected() )
(*it)->disconnectDevice( false /* don't unmount */ );
}
}
MediaBrowser::~MediaBrowser()
{
debug() << "having to remove " << m_devices.count() << " devices" << endl;
while( !m_devices.isEmpty() )
{
removeDevice( m_devices.last() );
}
queue()->save( Amarok::saveLocation() + "transferlist.xml" );
delete m_deviceCombo;
delete m_queue;
}
MediaItem::MediaItem( TQListView* parent )
: TDEListViewItem( parent )
{
init();
}
MediaItem::MediaItem( TQListViewItem* parent )
: TDEListViewItem( parent )
{
init();
}
MediaItem::MediaItem( TQListView* parent, TQListViewItem* after )
: TDEListViewItem( parent, after )
{
init();
}
MediaItem::MediaItem( TQListViewItem* parent, TQListViewItem* after )
: TDEListViewItem( parent, after )
{
init();
}
MediaItem::~MediaItem()
{
setBundle( 0 );
}
void
MediaItem::init()
{
m_bundle=0;
m_order=0;
m_type=UNKNOWN;
m_playlistName = TQString();
m_device=0;
m_flags=0;
setExpandable( false );
setDragEnabled( true );
setDropEnabled( true );
}
void
MediaItem::setBundle( MetaBundle *bundle )
{
MediaBrowser::instance()->m_itemMapMutex.lock();
if( m_bundle )
{
TQString itemUrl = url().url();
MediaBrowser::ItemMap::iterator it = MediaBrowser::instance()->m_itemMap.find( itemUrl );
if( it != MediaBrowser::instance()->m_itemMap.end() && *it == this )
MediaBrowser::instance()->m_itemMap.remove( itemUrl );
}
delete m_bundle;
m_bundle = bundle;
if( m_bundle )
{
TQString itemUrl = url().url();
MediaBrowser::ItemMap::iterator it = MediaBrowser::instance()->m_itemMap.find( itemUrl );
if( it == MediaBrowser::instance()->m_itemMap.end() )
MediaBrowser::instance()->m_itemMap[itemUrl] = this;
}
MediaBrowser::instance()->m_itemMapMutex.unlock();
}
void MediaItem::paintCell( TQPainter *p, const TQColorGroup &cg, int column, int width, int align )
{
switch( type() )
{
case INVISIBLE:
case PODCASTSROOT:
case PLAYLISTSROOT:
case ORPHANEDROOT:
case STALEROOT:
{
TQFont font( p->font() );
font.setBold( true );
p->setFont( font );
}
default:
break;
}
TDEListViewItem::paintCell( p, cg, column, width, align );
}
const MetaBundle *
MediaItem::bundle() const
{
return m_bundle;
}
KURL
MediaItem::url() const
{
if( bundle() )
return bundle()->url();
else
return KURL();
}
bool
MediaItem::isFileBacked() const
{
switch( type() )
{
case ARTIST:
case ALBUM:
case PODCASTSROOT:
case PODCASTCHANNEL:
case PLAYLISTSROOT:
case PLAYLIST:
case PLAYLISTITEM:
case INVISIBLEROOT:
case STALEROOT:
case STALE:
case ORPHANEDROOT:
return false;
case UNKNOWN:
case TRACK:
case ORPHANED:
case INVISIBLE:
case PODCASTITEM:
case DIRECTORY:
return true;
}
return false;
}
long
MediaItem::size() const
{
if( !isFileBacked() )
return 0;
if( bundle() )
return bundle()->filesize();
return 0;
}
void
MediaItem::setType( Type type )
{
m_type=type;
setDragEnabled(true);
setDropEnabled(false);
switch(m_type)
{
case UNKNOWN:
setPixmap(0, *s_pixUnknown);
break;
case INVISIBLE:
case TRACK:
setPixmap(0, *s_pixFile);
break;
case PLAYLISTITEM:
setPixmap(0, *s_pixTrack);
setDropEnabled(true);
break;
case ARTIST:
setPixmap(0, *s_pixArtist);
break;
case ALBUM:
setPixmap(0, *s_pixAlbum);
break;
case PODCASTSROOT:
setPixmap(0, *s_pixRootItem);
break;
case PODCASTITEM:
case PODCASTCHANNEL:
setPixmap(0, *s_pixPodcast);
break;
case PLAYLIST:
setPixmap(0, *s_pixPlaylist);
setDropEnabled(true);
break;
case PLAYLISTSROOT:
setPixmap(0, *s_pixRootItem);
setDropEnabled( true );
break;
case INVISIBLEROOT:
setPixmap(0, *s_pixInvisible);
break;
case STALEROOT:
case STALE:
setPixmap(0, *s_pixStale);
break;
case ORPHANEDROOT:
case ORPHANED:
setPixmap(0, *s_pixOrphaned);
break;
case DIRECTORY:
setExpandable( true );
setDropEnabled( true );
setPixmap(0, *s_pixDirectory);
break;
}
}
void
MediaItem::setFailed( bool failed )
{
if( failed )
{
m_flags &= ~MediaItem::Transferring;
m_flags |= MediaItem::Failed;
setPixmap(0, *MediaItem::s_pixTransferFailed);
}
else
{
m_flags &= ~MediaItem::Failed;
if( m_type == PODCASTITEM )
setPixmap(0, *s_pixPodcast);
else if( m_type == PLAYLIST )
setPixmap(0, *s_pixPlaylist);
else
setPixmap(0, TQPixmap() );
}
}
MediaItem *
MediaItem::lastChild() const
{
TQListViewItem *last = 0;
for( TQListViewItem *it = firstChild();
it;
it = it->nextSibling() )
{
last = it;
}
return dynamic_cast<MediaItem *>(last);
}
bool
MediaItem::isLeafItem() const
{
switch(type())
{
case UNKNOWN:
return false;
case INVISIBLE:
case TRACK:
case PODCASTITEM:
case PLAYLISTITEM:
case STALE:
case ORPHANED:
return true;
case ARTIST:
case ALBUM:
case PODCASTSROOT:
case PODCASTCHANNEL:
case PLAYLISTSROOT:
case PLAYLIST:
case INVISIBLEROOT:
case STALEROOT:
case ORPHANEDROOT:
case DIRECTORY:
return false;
}
return false;
}
MediaItem *
MediaItem::findItem( const TQString &key, const MediaItem *after ) const
{
MediaItem *it = 0;
if( after )
it = dynamic_cast<MediaItem *>( after->nextSibling() );
else
it = dynamic_cast<MediaItem *>( firstChild() );
for( ; it; it = dynamic_cast<MediaItem *>(it->nextSibling()))
{
if(key == it->text(0))
return it;
if(key.isEmpty() && it->text(0).isEmpty())
return it;
}
return 0;
}
int
MediaItem::compare( TQListViewItem *i, int col, bool ascending ) const
{
MediaItem *item = dynamic_cast<MediaItem *>(i);
if(item && col==0 && item->m_order != m_order)
return m_order-item->m_order;
else if( item && item->type() == MediaItem::ARTIST )
{
TQString key1 = key( col, ascending );
if( key1.startsWith( "the ", false ) )
key1 = key1.mid( 4 );
TQString key2 = i->key( col, ascending );
if( key2.startsWith( "the ", false ) )
key2 = key2.mid( 4 );
return key1.localeAwareCompare( key2 );
}
return TDEListViewItem::compare(i, col, ascending);
}
class MediaItemTip : public TQToolTip
{
public:
MediaItemTip( TQListView *listview )
: TQToolTip( listview->viewport() )
, m_view( listview )
{}
virtual ~MediaItemTip() {}
protected:
virtual void maybeTip( const TQPoint &p )
{
MediaItem *i = dynamic_cast<MediaItem *>(m_view->itemAt( m_view->viewportToContents( p ) ) );
if( !i )
return;
TQString text;
switch( i->type() )
{
case MediaItem::TRACK:
{
const MetaBundle *b = i->bundle();
if( b )
{
if( b->track() )
text = TQString( "%1 - %2 (%3)" )
.arg( TQString::number(b->track()), b->title(), b->prettyLength() );
if( !b->genre().isEmpty() )
{
if( !text.isEmpty() )
text += "<br>";
text += TQString( "<i>Genre: %1</i>" )
.arg( b->genre() );
}
}
}
break;
case MediaItem::PLAYLISTSROOT:
text = i18n( "Drag items here to create new playlist" );
break;
case MediaItem::PLAYLIST:
text = i18n( "Drag items here to append to this playlist" );
break;
case MediaItem::PLAYLISTITEM:
text = i18n( "Drag items here to insert before this item" );
break;
case MediaItem::INVISIBLEROOT:
case MediaItem::INVISIBLE:
text = i18n( "Not visible on media device" );
break;
case MediaItem::STALEROOT:
case MediaItem::STALE:
text = i18n( "In device database, but file is missing" );
break;
case MediaItem::ORPHANEDROOT:
case MediaItem::ORPHANED:
text = i18n( "File on device, but not in device database" );
break;
default:
break;
}
if( !text.isEmpty() && !text.isNull() )
tip( m_view->itemRect( i ), text );
}
TQListView *m_view;
};
MediaView::MediaView( TQWidget* parent, MediaDevice *device )
: TDEListView( parent )
, m_parent( parent )
, m_device( device )
{
hide();
setSelectionMode( TQListView::Extended );
setItemsMovable( false );
setShowSortIndicator( true );
setFullWidth( true );
setRootIsDecorated( true );
setDragEnabled( true );
setDropVisualizer( true ); //the visualizer (a line marker) is drawn when dragging over tracks
setDropHighlighter( true ); //and the highligther (a focus rect) is drawn when dragging over playlists
setDropVisualizerWidth( 3 );
setAcceptDrops( true );
header()->hide();
addColumn( i18n( "Remote Media" ) );
TDEActionCollection* ac = new TDEActionCollection( this );
KStdAction::selectAll( TQT_TQOBJECT(this), TQT_SLOT( selectAll() ), ac, "mediabrowser_select_all" );
connect( this, TQT_SIGNAL( contextMenuRequested( TQListViewItem*, const TQPoint&, int ) ),
this, TQT_SLOT( rmbPressed( TQListViewItem*, const TQPoint&, int ) ) );
connect( this, TQT_SIGNAL( itemRenamed( TQListViewItem* ) ),
this, TQT_SLOT( renameItem( TQListViewItem* ) ) );
connect( this, TQT_SIGNAL( expanded( TQListViewItem* ) ),
this, TQT_SLOT( slotExpand( TQListViewItem* ) ) );
connect( this, TQT_SIGNAL( returnPressed( TQListViewItem* ) ),
this, TQT_SLOT( invokeItem( TQListViewItem* ) ) );
connect( this, TQT_SIGNAL( doubleClicked( TQListViewItem*, const TQPoint&, int ) ),
this, TQT_SLOT( invokeItem( TQListViewItem*, const TQPoint &, int ) ) );
m_toolTip = new MediaItemTip( this );
}
void
MediaView::keyPressEvent( TQKeyEvent *e )
{
if( e->key() == Key_Delete )
m_device->deleteFromDevice();
else
TDEListView::keyPressEvent( e );
}
void
MediaView::invokeItem( TQListViewItem* i, const TQPoint& point, int column ) //SLOT
{
if( column == -1 )
return;
TQPoint p = mapFromGlobal( point );
if ( p.x() > header()->sectionPos( header()->mapToIndex( 0 ) ) + treeStepSize() * ( i->depth() + ( rootIsDecorated() ? 1 : 0) ) + itemMargin()
|| p.x() < header()->sectionPos( header()->mapToIndex( 0 ) ) )
invokeItem( i );
}
void
MediaView::invokeItem( TQListViewItem *i )
{
MediaItem *item = dynamic_cast<MediaItem *>( i );
if( !item )
return;
KURL::List urls = nodeBuildDragList( item );
Playlist::instance()->insertMedia( urls, Playlist::DefaultOptions );
}
void
MediaView::renameItem( TQListViewItem *item )
{
m_device->renameItem( item );
}
void
MediaView::slotExpand( TQListViewItem *item )
{
m_device->expandItem( item );
}
MediaView::~MediaView()
{
delete m_toolTip;
}
TQDragObject *
MediaView::dragObject()
{
KURL::List urls = nodeBuildDragList( 0 );
KMultipleDrag *md = new KMultipleDrag( viewport() );
md->addDragObject( TDEListView::dragObject() );
KURLDrag* ud = new KURLDrag( urls, viewport() );
md->addDragObject( ud );
md->setPixmap( CollectionDB::createDragPixmap( urls ),
TQPoint( CollectionDB::DRAGPIXMAP_OFFSET_X, CollectionDB::DRAGPIXMAP_OFFSET_Y ) );
return md;
}
KURL::List
MediaView::nodeBuildDragList( MediaItem* item, int flags )
{
KURL::List items;
MediaItem* fi;
if ( !item )
{
fi = static_cast<MediaItem*>(firstChild());
}
else
fi = item;
while ( fi )
{
if( fi->isVisible() )
{
if ( fi->isSelected() || !(flags & OnlySelected ) )
{
if( fi->isLeafItem() || fi->type() == MediaItem::DIRECTORY )
items += fi->url();
else
{
if(fi->childCount() )
items += nodeBuildDragList( static_cast<MediaItem*>(fi->firstChild()), None );
}
}
else
{
if ( fi->childCount() )
items += nodeBuildDragList( static_cast<MediaItem*>(fi->firstChild()), OnlySelected );
}
}
fi = static_cast<MediaItem*>(fi->nextSibling());
}
return items;
}
int
MediaView::getSelectedLeaves( MediaItem *parent, TQPtrList<MediaItem> *list, int flags )
{
int numFiles = 0;
if( !list )
list = new TQPtrList<MediaItem>;
MediaItem *it;
if( !parent )
it = dynamic_cast<MediaItem *>(firstChild());
else
it = dynamic_cast<MediaItem *>(parent->firstChild());
for( ; it; it = dynamic_cast<MediaItem*>(it->nextSibling()))
{
if( it->isVisible() )
{
if( it->childCount() && !( it->type() == MediaItem::DIRECTORY && it->isSelected() ) )
{
int f = flags;
if( it->isSelected() )
f &= ~OnlySelected;
numFiles += getSelectedLeaves(it, list, f );
}
if( it->isSelected() || !(flags & OnlySelected) )
{
if( it->type() == MediaItem::TRACK ||
it->type() == MediaItem::DIRECTORY ||
it->type() == MediaItem::PODCASTITEM ||
it->type() == MediaItem::PLAYLISTITEM||
it->type() == MediaItem::INVISIBLE ||
it->type() == MediaItem::ORPHANED )
{
if( flags & OnlyPlayed )
{
if( it->played() > 0 )
numFiles++;
}
else
numFiles++;
}
if( ( it->isLeafItem() && (!(flags & OnlyPlayed) || it->played()>0) )
|| it->type() == MediaItem::DIRECTORY )
list->append( it );
}
}
}
return numFiles;
}
bool
MediaView::acceptDrag( TQDropEvent *e ) const
{
if( e->source() == MediaBrowser::queue()->viewport() )
return false;
TQString data;
TQCString subtype;
TQTextDrag::decode( e, data, subtype );
return e->source() == viewport()
|| subtype == "amarok-sql"
|| KURLDrag::canDecode( e );
}
void
MediaView::contentsDropEvent( TQDropEvent *e )
{
cleanDropVisualizer();
cleanItemHighlighter();
if(e->source() == viewport())
{
const TQPoint p = contentsToViewport( e->pos() );
MediaItem *item = dynamic_cast<MediaItem *>(itemAt( p ));
if( !item && MediaBrowser::instance()->currentDevice()->m_type != "generic-mediadevice" )
return;
TQPtrList<MediaItem> items;
if( !item || item->type() == MediaItem::DIRECTORY ||
item->type() == MediaItem::TRACK )
{
TQPtrList<MediaItem> items;
getSelectedLeaves( 0, &items );
m_device->addToDirectory( item, items );
}
else if( item->type() == MediaItem::PLAYLIST )
{
MediaItem *list = item;
MediaItem *after = 0;
for(MediaItem *it = dynamic_cast<MediaItem *>(item->firstChild());
it;
it = dynamic_cast<MediaItem *>(it->nextSibling()))
after = it;
getSelectedLeaves( 0, &items );
m_device->addToPlaylist( list, after, items );
}
else if( item->type() == MediaItem::PLAYLISTITEM )
{
MediaItem *list = dynamic_cast<MediaItem *>(item->parent());
MediaItem *after = 0;
for(MediaItem *it = dynamic_cast<MediaItem*>(item->parent()->firstChild());
it;
it = dynamic_cast<MediaItem *>(it->nextSibling()))
{
if(it == item)
break;
after = it;
}
getSelectedLeaves( 0, &items );
m_device->addToPlaylist( list, after, items );
}
else if( item->type() == MediaItem::PLAYLISTSROOT )
{
TQPtrList<MediaItem> items;
getSelectedLeaves( 0, &items );
TQString base( i18n("New Playlist") );
TQString name = base;
int i=1;
while( item->findItem(name) )
{
TQString num;
num.setNum(i);
name = base + ' ' + num;
i++;
}
MediaItem *pl = m_device->newPlaylist(name, item, items);
ensureItemVisible(pl);
rename(pl, 0);
}
}
else
{
TQString data;
TQCString subtype;
TQTextDrag::decode( e, data, subtype );
KURL::List list;
if( subtype == "amarok-sql" )
{
TQString playlist = data.section( "\n", 0, 0 );
TQString query = data.section( "\n", 1 );
TQStringList values = CollectionDB::instance()->query( query );
list = CollectionDB::instance()->URLsFromSqlDrag( values );
MediaBrowser::queue()->addURLs( list, playlist );
}
else if ( KURLDrag::decode( e, list ) )
{
MediaBrowser::queue()->addURLs( list );
}
}
}
void
MediaView::viewportPaintEvent( TQPaintEvent *e )
{
TDEListView::viewportPaintEvent( e );
// Superimpose bubble help:
if ( !MediaBrowser::instance()->currentDevice() || !MediaBrowser::instance()->currentDevice()->isConnected() )
{
TQPainter p( viewport() );
TQSimpleRichText t( i18n(
"<div align=center>"
"<h3>Media Device Browser</h3>"
"Configure your media device and then "
"click the Connect button to access your media device. "
"Drag and drop files to enqueue them for transfer."
"</div>" ), TQApplication::font() );
t.setWidth( width() - 50 );
const uint w = t.width() + 20;
const uint h = t.height() + 20;
p.setBrush( colorGroup().background() );
p.drawRoundRect( 15, 15, w, h, (8*200)/w, (8*200)/h );
t.draw( &p, 20, 20, TQRect(), colorGroup() );
}
MediaBrowser::instance()->updateButtons();
}
void
MediaView::rmbPressed( TQListViewItem *item, const TQPoint &p, int i )
{
if( m_device->isConnected() )
m_device->rmbPressed( item, p, i );
}
MediaItem *
MediaView::newDirectory( MediaItem *parent )
{
bool ok;
const TQString name = KInputDialog::getText(i18n("Add Directory"), i18n("Directory Name:"), TQString(), &ok, this);
if( ok && !name.isEmpty() )
{
m_device->newDirectory( name, parent );
}
return 0;
}
void
MediaBrowser::mediumAdded( const Medium *medium, TQString /*name*/, bool /*constructing*/ )
{
debug() << "mediumAdded: " << (medium? medium->properties():"null") << endl;
if( medium )
{
TQString handler = Amarok::config( "MediaBrowser" )->readEntry( medium->id() );
if( handler.isEmpty() )
{
//Some people complained about the dialog, boohoo
//Just disable it for now I guess
/*if( !constructing && medium->isAutodetected() )
{
MediumPluginManagerDialog *mpm = new MediumPluginManagerDialog();
mpm->exec();
}*/
}
//debug() << "id=" << medium->id() << ", handler=" << handler << endl;
MediaDevice *device = loadDevicePlugin( handler );
if( device )
{
device->m_medium = *medium;
addDevice( device );
if( m_currentDevice == m_devices.begin() || m_currentDevice == m_devices.end() )
activateDevice( m_devices.count()-1, false );
}
}
}
void
MediaBrowser::pluginSelected( const Medium *medium, const TQString plugin )
{
DEBUG_BLOCK
if( !plugin.isEmpty() )
{
debug() << "Medium id is " << medium->id() << " and plugin selected is: " << plugin << endl;
Amarok::config( "MediaBrowser" )->writeEntry( medium->id(), plugin );
bool success = true;
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
if( (*it)->uniqueId() == medium->id() )
{
debug() << "removing " << medium->deviceNode() << endl;
if( (*it)->isConnected() )
{
if( (*it)->disconnectDevice( false ) )
removeDevice( *it );
else
success = false;
}
else
removeDevice( *it );
break;
}
}
if( success )
{
mediumAdded( medium, "doesntmatter", false );
}
else
{
debug() << "Cannot change plugin while operation is in progress" << endl;
Amarok::StatusBar::instance()->longMessage(
i18n( "Cannot change plugin while operation is in progress" ),
KDE::StatusBar::Warning );
}
}
else
debug() << "Medium id is " << medium->id() << " and you opted not to use a plugin" << endl;
}
void
MediaBrowser::showPluginManager()
{
MediumPluginManagerDialog* mpm = new MediumPluginManagerDialog();
mpm->exec();
delete mpm;
}
void
MediaBrowser::mediumChanged( const Medium *medium, TQString /*name*/ )
{
if( medium )
{
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
if( (*it)->uniqueId() == medium->id() )
{
(*it)->m_medium = const_cast<Medium *>(medium);
if( !(*it)->isConnected() && medium->isMounted() )
(*it)->connectDevice();
#if 0
else if( (*it)->isConnected() && !medium->isMounted() )
{
Amarok::StatusBar::instance()->longMessage(
i18n( "The device %1 was unmounted before it was synchronized. "
"In order to avoid data loss, press the \"Disconnect\" button "
"before unmounting the device." ).arg( name ),
KDE::StatusBar::Warning );
//(*it)->disconnectDevice();
}
#endif
break;
}
}
}
}
void
MediaBrowser::mediumRemoved( const Medium *medium, TQString name )
{
if( medium )
{
for( TQValueList<MediaDevice *>::iterator it = m_devices.begin();
it != m_devices.end();
it++ )
{
if( (*it)->uniqueId() == medium->id() )
{
if( (*it)->isConnected() )
{
if( (*it)->disconnectDevice() )
removeDevice( *it );
Amarok::StatusBar::instance()->longMessage(
i18n( "The device %1 was removed before it was disconnected. "
"In order to avoid possible data loss, press the \"Disconnect\" "
"button before disconnecting the device." ).arg( name ),
KDE::StatusBar::Warning );
}
else
removeDevice( *it );
break;
}
}
}
}
MediaDevice *
MediaBrowser::loadDevicePlugin( const TQString &deviceType )
{
DEBUG_BLOCK
if( deviceType == "ignore" )
return 0;
TQString query = "[X-TDE-Amarok-plugintype] == 'mediadevice' and [X-TDE-Amarok-name] == '%1'";
Amarok::Plugin *plugin = PluginManager::createFromQuery( query.arg( deviceType ) );
if( plugin )
{
debug() << "Returning plugin!" << endl;
MediaDevice *device = static_cast<MediaDevice *>( plugin );
device->init( this );
device->m_type = deviceType;
return device;
}
debug() << "no plugin for " << deviceType << endl;
return 0;
}
void
MediaBrowser::unloadDevicePlugin( MediaDevice *device )
{
DEBUG_BLOCK
if( !device )
return;
disconnect( device ); // disconnect all signals
if( dynamic_cast<DummyMediaDevice *>(device) )
{
delete device;
}
else
{
PluginManager::unload( device );
}
}
bool
MediaBrowser::config()
{
if( m_deviceCombo->currentText() == "No Device Selected" )
{
showPluginManager();
return true;
}
DeviceConfigureDialog* dcd = new DeviceConfigureDialog( currentDevice()->m_medium );
dcd->exec();
bool successful = dcd->successful();
delete dcd;
return successful;
}
void
MediaBrowser::configSelectPlugin( int index )
{
Q_UNUSED( index );
if( m_currentDevice == m_devices.begin() )
{
AmarokConfig::setDeviceType( m_pluginName[m_configPluginCombo->currentText()] );
}
else if( currentDevice() )
{
TDEConfig *config = Amarok::config( "MediaBrowser" );
config->writeEntry( currentDevice()->uniqueId(), m_pluginName[m_configPluginCombo->currentText()] );
}
if( !currentDevice() )
activateDevice( 0, false );
if( !currentDevice() )
return;
if( m_pluginName[m_configPluginCombo->currentText()] != currentDevice()->deviceType() )
{
MediaDevice *dev = currentDevice();
dev->removeConfigElements( m_configBox );
if( dev->isConnected() )
{
dev->disconnectDevice( false );
}
unloadDevicePlugin( dev );
*m_currentDevice = loadDevicePlugin( AmarokConfig::deviceType() );
if( !*m_currentDevice )
{
*m_currentDevice = new DummyMediaDevice();
if( AmarokConfig::deviceType() != "dummy-mediadevice" )
{
TQString msg = i18n( "The requested media device could not be loaded" );
Amarok::StatusBar::instance()->shortMessage( msg );
}
}
dev = currentDevice();
dev->init( this );
dev->loadConfig();
m_configBox->hide();
dev->addConfigElements( m_configBox );
m_configBox->show();
dev->view()->show();
if( dev->autoConnect() )
{
dev->connectDevice( true );
updateButtons();
}
updateDevices();
}
}
void
MediaBrowser::updateButtons()
{
if( !m_toolbar->getButton(CONNECT) ||
!m_toolbar->getButton(DISCONNECT) ||
!m_toolbar->getButton(TRANSFER) ) //TODO add CUSTOM
return;
if( currentDevice() )
{
if( currentDevice()->m_transfer )
m_toolbar->showItem( TRANSFER );
else
m_toolbar->hideItem( TRANSFER );
if( currentDevice()->m_customButton )
m_toolbar->showItem( CUSTOM );
else
m_toolbar->hideItem( CUSTOM );
if( currentDevice()->m_configure )
m_toolbar->showItem( CONFIGURE );
else
m_toolbar->hideItem( CONFIGURE );
m_toolbar->getButton(CONNECT)->setEnabled( !currentDevice()->isConnected() );
m_toolbar->getButton(DISCONNECT)->setEnabled( currentDevice()->isConnected() );
m_toolbar->getButton(TRANSFER)->setEnabled( currentDevice()->isConnected() && m_queue->childCount() > 0 );
m_toolbar->getButton( CUSTOM )->setEnabled( true );
}
else
{
m_toolbar->getButton( CONNECT )->setEnabled( false );
m_toolbar->getButton( DISCONNECT )->setEnabled( false );
m_toolbar->getButton( TRANSFER )->setEnabled( false );
m_toolbar->getButton( CUSTOM )->setEnabled( false );
}
}
void
MediaBrowser::updateStats()
{
if( !m_stats )
return;
TDEIO::filesize_t queued = m_queue->totalSize();
TQString text = i18n( "1 track in queue", "%n tracks in queue", m_queue->childCount() );
if(m_queue->childCount() > 0)
{
text += i18n(" (%1)").arg( TDEIO::convertSize( queued ) );
}
TDEIO::filesize_t total, avail;
if( currentDevice() && currentDevice()->getCapacity(&total, &avail) )
{
text += i18n( " - %1 of %2 available" ).arg( TDEIO::convertSize( avail ) ).arg( TDEIO::convertSize( total ) );
m_stats->m_used = total-avail;
m_stats->m_total = total;
m_stats->m_scheduled = queued;
}
else
{
m_stats->m_used = 0;
m_stats->m_total = 0;
m_stats->m_scheduled = queued;
}
m_stats->setText(text);
TQToolTip::add( m_stats, text );
}
bool
MediaView::setFilter( const TQString &filter, MediaItem *parent )
{
bool advanced = ExpressionParser::isAdvancedExpression( filter );
TQValueList<int> defaultColumns;
defaultColumns << MetaBundle::Album;
defaultColumns << MetaBundle::Title;
defaultColumns << MetaBundle::Artist;
bool root = false;
MediaItem *it;
if( !parent )
{
root = true;
it = dynamic_cast<MediaItem *>(firstChild());
}
else
{
it = dynamic_cast<MediaItem *>(parent->firstChild());
}
bool childrenVisible = false;
for( ; it; it = dynamic_cast<MediaItem *>(it->nextSibling()))
{
bool visible = true;
if(it->isLeafItem())
{
if( advanced )
{
ParsedExpression parsed = ExpressionParser::parse( filter );
visible = it->bundle() && it->bundle()->matchesParsedExpression( parsed, defaultColumns );
}
else
{
visible = it->bundle() && it->bundle()->matchesSimpleExpression( filter, defaultColumns );
}
}
else
{
visible = setFilter(filter, it);
if(it->type()==MediaItem::PLAYLISTSROOT || it->type()==MediaItem::PLAYLIST)
{
visible = true;
}
else if(it->type()==MediaItem::DIRECTORY)
{
bool match = true;
TQStringList list = TQStringList::split( " ", filter );
for( TQStringList::iterator i = list.begin();
i != list.end();
++i )
{
if( !(*it).text(0).contains( *i ) )
{
match = false;
break;
}
}
if( match )
visible = true;
}
}
if( filter.isEmpty() )
visible = true;
it->setVisible( visible );
if(visible)
childrenVisible = true;
}
if( root && m_device )
m_device->updateRootItems();
return childrenVisible;
}
MediaDevice::MediaDevice()
: Amarok::Plugin()
, m_hasMountPoint( true )
, m_autoDeletePodcasts( false )
, m_syncStats( false )
, m_transcode( false )
, m_transcodeAlways( false )
, m_transcodeRemove( false )
, sysProc ( 0 )
, m_parent( 0 )
, m_view( 0 )
, m_wait( false )
, m_requireMount( false )
, m_canceled( false )
, m_transferring( false )
, m_deleting( false )
, m_deferredDisconnect( false )
, m_scheduledDisconnect( false )
, m_transfer( true )
, m_configure( true )
, m_customButton( false )
, m_playlistItem( 0 )
, m_podcastItem( 0 )
, m_invisibleItem( 0 )
, m_staleItem( 0 )
, m_orphanedItem( 0 )
{
sysProc = new KShellProcess(); TQ_CHECK_PTR(sysProc);
}
void MediaDevice::init( MediaBrowser* parent )
{
m_parent = parent;
if( !m_view )
m_view = new MediaView( m_parent->m_views, this );
m_view->hide();
}
MediaDevice::~MediaDevice()
{
delete m_view;
delete sysProc;
}
bool
MediaDevice::isSpecialItem( MediaItem *item )
{
return (item == m_playlistItem) ||
(item == m_podcastItem) ||
(item == m_invisibleItem) ||
(item == m_staleItem) ||
(item == m_orphanedItem);
}
void
MediaDevice::loadConfig()
{
m_transcode = configBool( "Transcode" );
m_transcodeAlways = configBool( "TranscodeAlways" );
m_transcodeRemove = configBool( "TranscodeRemove" );
m_preconnectcmd = configString( "PreConnectCommand" );
if( m_preconnectcmd.isEmpty() )
m_preconnectcmd = configString( "MountCommand" );
m_postdisconnectcmd = configString( "PostDisconnectCommand" );
if( m_postdisconnectcmd.isEmpty() )
m_postdisconnectcmd = configString( "UmountCommand" );
if( m_requireMount && m_postdisconnectcmd.isEmpty() )
m_postdisconnectcmd = "tdeeject -q %d";
}
TQString
MediaDevice::configString( const TQString &name, const TQString &defValue )
{
TQString configName = "MediaDevice";
if( !uniqueId().isEmpty() )
configName += '_' + uniqueId();
TDEConfig *config = Amarok::config( configName );
return config->readEntry( name, defValue );
}
void
MediaDevice::setConfigString( const TQString &name, const TQString &value )
{
TQString configName = "MediaDevice";
if( !uniqueId().isEmpty() )
configName += '_' + uniqueId();
TDEConfig *config = Amarok::config( configName );
config->writeEntry( name, value );
}
bool
MediaDevice::configBool( const TQString &name, bool defValue )
{
TQString configName = "MediaDevice";
if( !uniqueId().isEmpty() )
configName += '_' + uniqueId();
TDEConfig *config = Amarok::config( configName );
return config->readBoolEntry( name, defValue );
}
void
MediaDevice::setConfigBool( const TQString &name, bool value )
{
TQString configName = "MediaDevice";
if( !uniqueId().isEmpty() )
configName += '_' + uniqueId();
TDEConfig *config = Amarok::config( configName );
config->writeEntry( name, value );
}
MediaView *
MediaDevice::view()
{
return m_view;
}
void
MediaDevice::hideProgress()
{
m_parent->m_progressBox->hide();
}
void
MediaDevice::updateRootItems()
{
if(m_podcastItem)
m_podcastItem->setVisible(m_podcastItem->childCount() > 0);
if(m_invisibleItem)
m_invisibleItem->setVisible(m_invisibleItem->childCount() > 0);
if(m_staleItem)
m_staleItem->setVisible(m_staleItem->childCount() > 0);
if(m_orphanedItem)
m_orphanedItem->setVisible(m_orphanedItem->childCount() > 0);
}
void
MediaQueue::syncPlaylist( const TQString &name, const TQString &query, bool loading )
{
MediaItem* item = new MediaItem( this, lastItem() );
item->setType( MediaItem::PLAYLIST );
item->setExpandable( false );
item->setData( query );
item->m_playlistName = name;
item->setText( 0, name );
item->m_flags |= MediaItem::SmartPlaylist;
m_parent->m_progress->setTotalSteps( m_parent->m_progress->totalSteps() + 1 );
itemCountChanged();
if( !loading )
URLsAdded();
}
void
MediaQueue::syncPlaylist( const TQString &name, const KURL &url, bool loading )
{
MediaItem* item = new MediaItem( this, lastItem() );
item->setType( MediaItem::PLAYLIST );
item->setExpandable( false );
item->setData( url.url() );
item->m_playlistName = name;
item->setText( 0, name );
m_parent->m_progress->setTotalSteps( m_parent->m_progress->totalSteps() + 1 );
itemCountChanged();
if( !loading )
URLsAdded();
}
BundleList
MediaDevice::bundlesToSync( const TQString &name, const KURL &url )
{
BundleList bundles;
if( !PlaylistFile::isPlaylistFile( url ) )
{
Amarok::StatusBar::instance()->longMessage( i18n( "Not a playlist file: %1" ).arg( url.path() ),
KDE::StatusBar::Sorry );
return bundles;
}
PlaylistFile playlist( url.path() );
if( playlist.isError() )
{
Amarok::StatusBar::instance()->longMessage( i18n( "Failed to load playlist: %1" ).arg( url.path() ),
KDE::StatusBar::Sorry );
return bundles;
}
for( BundleList::iterator it = playlist.bundles().begin();
it != playlist.bundles().end();
++it )
{
bundles += MetaBundle( (*it).url() );
}
preparePlaylistForSync( name, bundles );
return bundles;
}
BundleList
MediaDevice::bundlesToSync( const TQString &name, const TQString &query )
{
const TQStringList values = CollectionDB::instance()->query( query );
BundleList bundles;
for( for_iterators( TQStringList, values ); it != end; ++it )
bundles += CollectionDB::instance()->bundleFromQuery( &it );
preparePlaylistForSync( name, bundles );
return bundles;
}
void
MediaDevice::preparePlaylistForSync( const TQString &name, const BundleList &bundles )
{
if( ! m_playlistItem ) // might be syncing a new playlist from the playlist browser
return;
MediaItem *pl = m_playlistItem->findItem( name );
if( pl )
{
MediaItem *next = 0;
for( MediaItem *it = static_cast<MediaItem *>(pl->firstChild());
it;
it = next )
{
next = static_cast<MediaItem *>(it->nextSibling());
const MetaBundle *bundle = (*it).bundle();
if( !bundle )
continue;
if( isOnOtherPlaylist( name, *bundle ) )
continue;
if( isInBundleList( bundles, *bundle ) )
continue;
deleteItemFromDevice( it );
}
deleteItemFromDevice( pl, None );
}
purgeEmptyItems();
}
bool
MediaDevice::bundleMatch( const MetaBundle &b1, const MetaBundle &b2 )
{
if( b1.track() != b2.track() )
return false;
if( b1.title() != b2.title() )
return false;
if( b1.album() != b2.album() )
return false;
if( b1.artist() != b2.artist() )
return false;
#if 0
if( b1.discNumber() != b2.discNumber() )
return false;
if( b1.composer() != b2.composer() )
return false;
#endif
return true;
}
bool
MediaDevice::isInBundleList( const BundleList &bundles, const MetaBundle &b )
{
for( BundleList::const_iterator it = bundles.begin();
it != bundles.end();
++it )
{
if( bundleMatch( b, *it ) )
return true;
}
return false;
}
bool
MediaDevice::isOnOtherPlaylist( const TQString &playlistToAvoid, const MetaBundle &bundle )
{
for( MediaItem *it = static_cast<MediaItem *>(m_playlistItem->firstChild());
it;
it = static_cast<MediaItem *>(it->nextSibling()) )
{
if( it->text( 0 ) == playlistToAvoid )
continue;
if( isOnPlaylist( *it, bundle ) )
return true;
}
return false;
}
bool
MediaDevice::isOnPlaylist( const MediaItem &playlist, const MetaBundle &bundle )
{
for( MediaItem *it = static_cast<MediaItem *>(playlist.firstChild());
it;
it = static_cast<MediaItem *>(it->nextSibling()) )
{
const MetaBundle *b = (*it).bundle();
if( !b )
continue;
if( bundleMatch( *b, bundle ) )
return true;
}
return false;
}
void
MediaQueue::addURL( const KURL& url2, MetaBundle *bundle, const TQString &playlistName )
{
KURL url = Amarok::mostLocalURL( url2 );
if( PlaylistFile::isPlaylistFile( url ) )
{
TQString name = TQString(url.path().section( "/", -1 ).section( ".", 0, -2 )).replace( "_", " " );
PlaylistFile playlist( url.path() );
if( playlist.isError() )
{
Amarok::StatusBar::instance()->longMessage( i18n( "Failed to load playlist: %1" ).arg( url.path() ),
KDE::StatusBar::Sorry );
return;
}
for( BundleList::iterator it = playlist.bundles().begin();
it != playlist.bundles().end();
++it )
{
addURL( (*it).url(), 0, name );
}
return;
}
else if( ContextBrowser::hasContextProtocol( url ) )
{
KURL::List urls = ContextBrowser::expandURL( url );
for( KURL::List::iterator it = urls.begin();
it != urls.end();
++it )
{
addURL( *it );
}
return;
}
else if( url.protocol() == "file" && TQFileInfo( url.path() ).isDir() )
{
KURL::List urls = Amarok::recursiveUrlExpand( url );
foreachType( KURL::List, urls )
addURL( *it );
return;
}
if( playlistName.isNull() )
{
for( MediaItem *it = static_cast<MediaItem *>(firstChild());
it;
it = static_cast<MediaItem *>(it->nextSibling()) )
{
if( it->url() == url )
{
Amarok::StatusBar::instance()->shortMessage(
i18n( "Track already queued for transfer: %1" ).arg( url.url() ) );
return;
}
}
}
if(!bundle)
bundle = new MetaBundle( url );
MediaItem* item = new MediaItem( this, lastItem() );
item->setExpandable( false );
item->setDropEnabled( true );
item->setBundle( bundle );
if(bundle->podcastBundle() )
{
item->setType( MediaItem::PODCASTITEM );
}
item->m_playlistName = playlistName;
TQString text = item->bundle()->prettyTitle();
if( text.isEmpty() || (!item->bundle()->isValidMedia() && !item->bundle()->podcastBundle()) )
text = item->bundle()->url().prettyURL();