Add an implementation of the CustomActionsCapability interface that spans all service...
[amarok.git] / src / k3bexporter.cpp
blob33b5644c10b3cc917819177adb75163854da7580
1 /***************************************************************************
2 begin : Mon May 31 2004
3 copyright : (C) 2004 by Michael Pyne
4 (c) 2004 by Pierpaolo Di Panfilo
5 email : michael.pyne@kdemail.net
6 ***************************************************************************/
8 /***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17 #include "k3bexporter.h"
19 #include "amarok.h"
21 #include "querybuilder.h"
23 #include <k3process.h>
24 #include <KLocale>
25 #include <KMessageBox>
26 #include <KStandardDirs>
28 #include <QByteArray>
29 #include <QStringList>
30 #include <Q3ValueList>
33 K3bExporter *K3bExporter::s_instance = 0;
35 // FIXME: implement me!
36 #if 0
37 bool K3bExporter::isAvailable() //static
39 return !KStandardDirs::findExe( "k3b" ).isNull();
42 void K3bExporter::exportTracks( const KUrl::List &urls, int openmode )
44 if( urls.empty() )
45 return;
47 DCOPClient *client = DCOPClient::mainClient();
48 QByteArray appId, appObj;
49 QByteArray data;
51 if( openmode == -1 )
52 //ask to open a data or an audio cd project
53 openmode = openMode();
55 if( !client->findObject( "k3b-*", "K3bInterface", "", data, appId, appObj) )
56 exportViaCmdLine( urls, openmode );
57 else {
58 DCOPRef ref( appId, appObj );
59 exportViaDCOP( urls, ref, openmode );
63 void K3bExporter::exportCurrentPlaylist( int openmode )
65 Playlist::instance()->burnPlaylist( openmode );
68 void K3bExporter::exportSelectedTracks( int openmode )
70 Playlist::instance()->burnSelectedTracks( openmode );
73 void K3bExporter::exportAlbum( const QString &album, int openmode )
75 exportAlbum( QString(), album, openmode );
78 void K3bExporter::exportAlbum( const QString &artist, const QString &album, int openmode )
80 QString albumId = QString::number( CollectionDB::instance()->albumID( album, false, false, true ) );
81 QString artistId;
82 if( !artist.isNull() )
83 artistId = QString::number( CollectionDB::instance()->artistID( artist, false, false, true ) );
85 QueryBuilder qb;
86 qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valURL );
87 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valAlbumID, albumId );
88 if( !artist.isNull() )
89 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valArtistID, artistId );
90 qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valDiscNumber );
91 qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valTrack );
93 QStringList values( qb.run() );
95 if( !values.isEmpty() )
97 KUrl::List urls;
99 oldForeach( values )
100 urls << KUrl( *it );
102 exportTracks( urls, openmode );
106 void K3bExporter::exportArtist( const QString &artist, int openmode )
108 const QString artistId = QString::number( CollectionDB::instance()->artistID( artist, false, false, true ) );
110 QueryBuilder qb;
111 qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valURL );
112 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valArtistID, artistId );
113 qb.sortBy( QueryBuilder::tabAlbum, QueryBuilder::valName );
114 qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valDiscNumber );
115 qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valTrack );
117 QStringList values( qb.run() );
119 if( !values.isEmpty() )
121 KUrl::List urls;
123 oldForeach( values )
124 urls << KUrl( *it );
126 exportTracks( urls, openmode );
130 void K3bExporter::exportComposer( const QString &composer, int openmode )
132 const QString composerId = QString::number( CollectionDB::instance()->composerID( composer, false, false, true ) );
134 QueryBuilder qb;
135 qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valURL );
136 qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valComposerID, composerId );
137 qb.sortBy( QueryBuilder::tabAlbum, QueryBuilder::valName );
138 qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valDiscNumber );
139 qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valTrack );
141 QStringList values( qb.run() );
143 if( !values.isEmpty() )
145 KUrl::List urls;
147 oldForeach( values )
148 urls << KUrl( *it );
150 exportTracks( urls, openmode );
154 void K3bExporter::exportViaCmdLine( const KUrl::List &urls, int openmode )
156 QByteArray cmdOption;
158 switch( openmode ) {
159 case AudioCD:
160 cmdOption = "--audiocd";
161 break;
163 case DataCD:
164 cmdOption = "--datacd";
165 break;
167 case Abort:
168 return;
171 K3Process *process = new K3Process;
173 *process << "k3b";
174 *process << cmdOption;
176 KUrl::List::ConstIterator it;
177 KUrl::List::ConstIterator end( urls.end() );
178 for( it = urls.begin(); it != end; ++it )
179 *process << ( *it ).path();
181 if( !process->start( K3Process::DontCare ) )
182 KMessageBox::error( 0, i18n("Unable to start K3b.") );
185 void K3bExporter::exportViaDCOP( const KUrl::List &urls, DCOPRef &ref, int openmode )
187 Q3ValueList<DCOPRef> projectList;
188 DCOPReply projectListReply = ref.call("projects()");
190 if( !projectListReply.get<Q3ValueList<DCOPRef> >(projectList, "QValueList<DCOPRef>") ) {
191 DCOPErrorMessage();
192 return;
195 if( projectList.count() == 0 && !startNewK3bProject(ref, openmode) )
196 return;
198 if( !ref.send( "addUrls(KUrl::List)", DCOPArg(urls, "KUrl::List") ) ) {
199 DCOPErrorMessage();
200 return;
204 void K3bExporter::DCOPErrorMessage()
206 KMessageBox::error( 0, i18n("There was a DCOP communication error with K3b."));
209 bool K3bExporter::startNewK3bProject( DCOPRef &ref, int openmode )
211 QByteArray request;
212 //K3bOpenMode mode = openMode();
214 switch( openmode ) {
215 case AudioCD:
216 request = "createAudioCDProject()";
217 break;
219 case DataCD:
220 request = "createDataCDProject()";
221 break;
223 case Abort:
224 return false;
227 KMessageBox::sorry(0,request);
228 if( !ref.send( request ) ) {
229 DCOPErrorMessage();
230 return false;
233 return true;
236 K3bExporter::K3bOpenMode K3bExporter::openMode()
238 int reply = KMessageBox::questionYesNoCancel(
240 i18n("Create an audio mode CD suitable for CD players, or a data "
241 "mode CD suitable for computers and other digital music "
242 "players?"),
243 i18n("Create K3b Project"),
244 i18n("Audio Mode"),
245 i18n("Data Mode")
248 switch(reply) {
249 case KMessageBox::Cancel:
250 return Abort;
252 case KMessageBox::No:
253 return DataCD;
255 case KMessageBox::Yes:
256 return AudioCD;
259 return Abort;
261 #endif