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