2 * Copyright (c) 2007 Maximilian Kossick <maximilian.kossick@googlemail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "AmarokMimeData.h"
23 #include <QCoreApplication>
27 const QString
AmarokMimeData::TRACK_MIME
= "application/x-amarok-tracks";
29 class AmarokMimeData::Private
32 Private() : deleteQueryMakers( true ), completedQueries( 0 )
37 if( deleteQueryMakers
)
38 qDeleteAll( queryMakers
);
41 Meta::TrackList tracks
;
42 QList
<QueryMaker
*> queryMakers
;
43 QMap
<QueryMaker
*, Meta::TrackList
> trackMap
;
44 bool deleteQueryMakers
;
49 AmarokMimeData::AmarokMimeData()
56 AmarokMimeData::~AmarokMimeData()
62 AmarokMimeData::formats() const
64 QStringList
formats( QMimeData::formats() );
65 if( !d
->tracks
.isEmpty() || !d
->queryMakers
.isEmpty())
67 formats
.append( TRACK_MIME
);
68 if( !formats
.contains( "text/uri-list" ) )
69 formats
.append( "text/uri-list" );
70 if( !formats
.contains( "text/plain" ) )
71 formats
.append( "text/plain" );
77 AmarokMimeData::hasFormat( const QString
&mimeType
) const
79 if( mimeType
== TRACK_MIME
|| mimeType
== "text/uri-list" || mimeType
== "text/plain" )
80 return !d
->tracks
.isEmpty() || !d
->queryMakers
.isEmpty();
82 return QMimeData::hasFormat( mimeType
);
86 AmarokMimeData::tracks() const
88 while( d
->completedQueries
< d
->queryMakers
.count() )
90 QCoreApplication::instance()->processEvents( QEventLoop::AllEvents
);
92 Meta::TrackList result
= d
->tracks
;
93 foreach( QueryMaker
*qm
, d
->queryMakers
)
95 if( d
->trackMap
.contains( qm
) )
96 result
<< d
->trackMap
.value( qm
);
102 AmarokMimeData::setTracks( const Meta::TrackList
&tracks
)
108 AmarokMimeData::addTracks( const Meta::TrackList
&tracks
)
114 AmarokMimeData::queryMakers()
116 d
->deleteQueryMakers
= false;
117 return d
->queryMakers
;
121 AmarokMimeData::addQueryMaker( QueryMaker
*queryMaker
)
123 d
->queryMakers
.append( queryMaker
);
127 AmarokMimeData::setQueryMakers( const QList
<QueryMaker
*> &queryMakers
)
129 d
->queryMakers
<< queryMakers
;
133 AmarokMimeData::retrieveData( const QString
&mimeType
, QVariant::Type type
) const
135 Meta::TrackList tracks
= this->tracks();
136 if( !tracks
.isEmpty() )
138 if( mimeType
== "text/uri-list" && type
== QVariant::List
)
140 QList
<QVariant
> list
;
141 foreach( Meta::TrackPtr track
, tracks
)
143 list
.append( QVariant( QUrl( track
->playableUrl().url() ) ) );
145 return QVariant( list
);
147 if( mimeType
== "text/plain" && type
== QVariant::String
)
150 foreach( Meta::TrackPtr track
, tracks
)
152 if( !result
.isEmpty() )
154 result
+= track
->artist()->prettyName();
156 result
+= track
->prettyName();
158 return QVariant( result
);
161 return QMimeData::retrieveData( mimeType
, type
);
165 AmarokMimeData::startQueries()
168 foreach( QueryMaker
*qm
, d
->queryMakers
)
170 qm
->startTrackQuery();
171 connect( qm
, SIGNAL( newResultReady( QString
, Meta::TrackList
) ), this, SLOT( newResultReady( QString
, Meta::TrackList
) ), Qt::QueuedConnection
);
172 connect( qm
, SIGNAL( queryDone() ), this, SLOT( queryDone() ), Qt::QueuedConnection
);
178 AmarokMimeData::newResultReady( const QString
&collectionId
, const Meta::TrackList
&tracks
)
180 Q_UNUSED( collectionId
)
181 QueryMaker
*qm
= dynamic_cast<QueryMaker
*>( sender() );
184 d
->trackMap
.insert( qm
, tracks
);
191 AmarokMimeData::queryDone()
193 d
->completedQueries
++;
196 #include "AmarokMimeData.moc"