1
/******************************************************************************
2
 * Copyright (C) 2004 by Michael Pyne <michael.pyne@kdemail.net>              *
3
 *           (C) 2004 Pierpaolo Di Panfilo <pippo_dp@libero.it>               *
4
 *                                                                            *
5
 * This program is free software; you can redistribute it and/or              *
6
 * modify it under the terms of the GNU General Public License as             *
7
 * published by the Free Software Foundation; either version 2 of             *
8
 * the License, or (at your option) any later version.                        *
9
 *                                                                            *
10
 * This program is distributed in the hope that it will be useful,            *
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13
 * GNU General Public License for more details.                               *
14
 *                                                                            *
15
 * You should have received a copy of the GNU General Public License          *
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
17
 ******************************************************************************/
18
19
#include "k3bexporter.h"
20
21
#include "Amarok.h"
22
23
#include "AmarokProcess.h"
24
25
#include <KLocale>
26
#include <KMessageBox>
27
#include <KStandardDirs>
28
29
#include <QByteArray>
30
#include <QStringList>
31
#include <Q3ValueList>
32
33
34
K3bExporter *K3bExporter::s_instance = 0;
35
36
// FIXME: implement me!
37
#if 0
38
bool K3bExporter::isAvailable() //static
39
{
40
    return !KStandardDirs::findExe( "k3b" ).isNull();
41
}
42
43
void K3bExporter::exportTracks( const KUrl::List &urls, int openmode )
44
{
45
    if( urls.empty() )
46
        return;
47
48
    DCOPClient *client = DCOPClient::mainClient();
49
    QByteArray appId, appObj;
50
    QByteArray data;
51
52
    if( openmode == -1 )
53
        //ask to open a data or an audio cd project
54
        openmode = openMode();
55
56
    if( !client->findObject( "k3b-*", "K3bInterface", "", data, appId, appObj) )
57
        exportViaCmdLine( urls, openmode );
58
    else {
59
        DCOPRef ref( appId, appObj );
60
        exportViaDCOP( urls, ref, openmode );
61
    }
62
}
63
64
void K3bExporter::exportCurrentPlaylist( int openmode )
65
{
66
    Playlist::instance()->burnPlaylist( openmode );
67
}
68
69
void K3bExporter::exportSelectedTracks( int openmode )
70
{
71
    Playlist::instance()->burnSelectedTracks( openmode );
72
}
73
74
void K3bExporter::exportAlbum( const QString &album, int openmode )
75
{
76
    exportAlbum( QString(), album, openmode );
77
}
78
79
void K3bExporter::exportAlbum( const QString &artist, const QString &album, int openmode )
80
{
81
    QString albumId = QString::number( CollectionDB::instance()->albumID( album, false, false, true ) );
82
    QString artistId;
83
    if( !artist.isNull() )
84
        artistId = QString::number( CollectionDB::instance()->artistID( artist, false, false, true ) );
85
86
    QueryBuilder qb;
87
    qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valURL );
88
    qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valAlbumID, albumId );
89
    if( !artist.isNull() )
90
        qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valArtistID, artistId );
91
    qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valDiscNumber );
92
    qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valTrack );
93
94
    QStringList values( qb.run() );
95
96
    if( !values.isEmpty() )
97
    {
98
        KUrl::List urls;
99
100
        oldForeach( values )
101
            urls << KUrl( *it );
102
103
        exportTracks( urls, openmode );
104
    }
105
}
106
107
void K3bExporter::exportArtist( const QString &artist, int openmode )
108
{
109
    const QString artistId = QString::number( CollectionDB::instance()->artistID( artist, false, false, true ) );
110
111
    QueryBuilder qb;
112
    qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valURL );
113
    qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valArtistID, artistId );
114
    qb.sortBy( QueryBuilder::tabAlbum, QueryBuilder::valName );
115
    qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valDiscNumber );
116
    qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valTrack );
117
118
    QStringList values( qb.run() );
119
120
    if( !values.isEmpty() )
121
    {
122
        KUrl::List urls;
123
124
        oldForeach( values )
125
            urls << KUrl( *it );
126
127
        exportTracks( urls, openmode );
128
    }
129
}
130
131
void K3bExporter::exportComposer( const QString &composer, int openmode )
132
{
133
    const QString composerId = QString::number( CollectionDB::instance()->composerID( composer, false, false, true ) );
134
135
    QueryBuilder qb;
136
    qb.addReturnValue( QueryBuilder::tabSong, QueryBuilder::valURL );
137
    qb.addMatch( QueryBuilder::tabSong, QueryBuilder::valComposerID, composerId );
138
    qb.sortBy( QueryBuilder::tabAlbum, QueryBuilder::valName );
139
    qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valDiscNumber );
140
    qb.sortBy( QueryBuilder::tabSong, QueryBuilder::valTrack );
141
142
    QStringList values( qb.run() );
143
144
    if( !values.isEmpty() )
145
    {
146
        KUrl::List urls;
147
148
        oldForeach( values )
149
            urls << KUrl( *it );
150
151
        exportTracks( urls, openmode );
152
    }
153
}
154
155
void K3bExporter::exportViaCmdLine( const KUrl::List &urls, int openmode )
156
{
157
    QByteArray cmdOption;
158
159
    switch( openmode ) {
160
    case AudioCD:
161
        cmdOption = "--audiocd";
162
        break;
163
164
    case DataCD:
165
        cmdOption = "--datacd";
166
        break;
167
168
    case Abort:
169
        return;
170
    }
171
172
    K3Process *process = new K3Process;
173
174
    *process << "k3b";
175
    *process << cmdOption;
176
177
    KUrl::List::ConstIterator it;
178
    KUrl::List::ConstIterator constEnd( urls.end() );
179
    for( it = urls.constBegin(); it != end; ++it )
180
        *process << ( *it ).path();
181
182
    if( !process->start( K3Process::DontCare ) )
183
        KMessageBox::error( 0, i18n("Unable to start K3b.") );
184
}
185
186
void K3bExporter::exportViaDCOP( const KUrl::List &urls, DCOPRef &ref, int openmode )
187
{
188
    Q3ValueList<DCOPRef> projectList;
189
    DCOPReply projectListReply = ref.call("projects()");
190
191
    if( !projectListReply.get<Q3ValueList<DCOPRef> >(projectList, "QValueList<DCOPRef>") ) {
192
        DCOPErrorMessage();
193
        return;
194
    }
195
196
    if( projectList.count() == 0 && !startNewK3bProject(ref, openmode) )
197
        return;
198
199
    if( !ref.send( "addUrls(KUrl::List)", DCOPArg(urls, "KUrl::List") ) ) {
200
        DCOPErrorMessage();
201
        return;
202
    }
203
}
204
205
void K3bExporter::DCOPErrorMessage()
206
{
207
    KMessageBox::error( 0, i18n("There was a DCOP communication error with K3b."));
208
}
209
210
bool K3bExporter::startNewK3bProject( DCOPRef &ref, int openmode )
211
{
212
    QByteArray request;
213
    //K3bOpenMode mode = openMode();
214
215
    switch( openmode ) {
216
    case AudioCD:
217
        request = "createAudioCDProject()";
218
        break;
219
220
    case DataCD:
221
        request = "createDataCDProject()";
222
        break;
223
224
    case Abort:
225
        return false;
226
    }
227
228
    KMessageBox::sorry(0,request);
229
    if( !ref.send( request ) ) {
230
        DCOPErrorMessage();
231
        return false;
232
    }
233
234
    return true;
235
}
236
237
K3bExporter::K3bOpenMode K3bExporter::openMode()
238
{
239
    int reply = KMessageBox::questionYesNoCancel(
240
        0,
241
        i18n("Create an audio mode CD suitable for CD players, or a data "
242
             "mode CD suitable for computers and other digital music "
243
             "players?"),
244
        i18n("Create K3b Project"),
245
        i18n("Audio Mode"),
246
        i18n("Data Mode")
247
    );
248
249
    switch(reply) {
250
    case KMessageBox::Cancel:
251
        return Abort;
252
253
    case KMessageBox::No:
254
        return DataCD;
255
256
    case KMessageBox::Yes:
257
        return AudioCD;
258
    }
259
260
    return Abort;
261
}
262
#endif