Revert previous commit, was incorrect
[amarok.git] / src / AmarokMimeData.cpp
blob9ca0a32cce25906a0f131439a5c8d3e0cecbb96b
1 /*
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"
21 #include "debug.h"
23 #include <QCoreApplication>
24 #include <QList>
25 #include <QUrl>
27 const QString AmarokMimeData::TRACK_MIME = "application/x-amarok-tracks";
29 class AmarokMimeData::Private
31 public:
32 Private() : deleteQueryMakers( true ), completedQueries( 0 )
35 ~Private()
37 if( deleteQueryMakers )
38 qDeleteAll( queryMakers );
41 Meta::TrackList tracks;
42 QList<QueryMaker*> queryMakers;
43 QMap<QueryMaker*, Meta::TrackList> trackMap;
44 bool deleteQueryMakers;
45 int completedQueries;
49 AmarokMimeData::AmarokMimeData()
50 : QMimeData()
51 , d( new Private() )
53 //nothing to do
56 AmarokMimeData::~AmarokMimeData()
58 delete d;
61 QStringList
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" );
73 return formats;
76 bool
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();
81 else
82 return QMimeData::hasFormat( mimeType );
85 Meta::TrackList
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 );
98 return result;
101 void
102 AmarokMimeData::setTracks( const Meta::TrackList &tracks )
104 d->tracks = tracks;
107 void
108 AmarokMimeData::addTracks( const Meta::TrackList &tracks )
110 d->tracks << tracks;
113 QList<QueryMaker*>
114 AmarokMimeData::queryMakers()
116 d->deleteQueryMakers = false;
117 return d->queryMakers;
120 void
121 AmarokMimeData::addQueryMaker( QueryMaker *queryMaker )
123 d->queryMakers.append( queryMaker );
126 void
127 AmarokMimeData::setQueryMakers( const QList<QueryMaker*> &queryMakers )
129 d->queryMakers << queryMakers;
132 QVariant
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 )
149 QString result;
150 foreach( Meta::TrackPtr track, tracks )
152 if( !result.isEmpty() )
153 result += '\n';
154 result += track->artist()->prettyName();
155 result += " - ";
156 result += track->prettyName();
158 return QVariant( result );
161 return QMimeData::retrieveData( mimeType, type );
164 void
165 AmarokMimeData::startQueries()
167 DEBUG_BLOCK
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 );
173 qm->run();
177 void
178 AmarokMimeData::newResultReady( const QString &collectionId, const Meta::TrackList &tracks )
180 Q_UNUSED( collectionId )
181 QueryMaker *qm = dynamic_cast<QueryMaker*>( sender() );
182 if( qm )
184 d->trackMap.insert( qm, tracks );
186 else
187 d->tracks << tracks;
190 void
191 AmarokMimeData::queryDone()
193 d->completedQueries++;
196 #include "AmarokMimeData.moc"