1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
/*
*
* $Id: k3bclonejob.cpp 619556 2007-01-03 17:38:12Z trueg $
* Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* See the file "COPYING" for the exact licensing terms.
*/
#include "k3bclonejob.h"
#include <k3breadcdreader.h>
#include <k3bcdrecordwriter.h>
#include <k3bexternalbinmanager.h>
#include <k3bdevice.h>
#include <k3bdevicehandler.h>
#include <k3bglobals.h>
#include <k3bcore.h>
#include <k3bclonetocreader.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tqfile.h>
#include <tqfileinfo.h>
class K3bCloneJob::Private
{
public:
Private()
: doneCopies(0) {
}
int doneCopies;
};
K3bCloneJob::K3bCloneJob( K3bJobHandler* hdl, TQObject* parent, const char* name )
: K3bBurnJob( hdl, parent, name ),
m_writerDevice(0),
m_readerDevice(0),
m_writerJob(0),
m_readcdReader(0),
m_removeImageFiles(false),
m_canceled(false),
m_running(false),
m_simulate(false),
m_speed(1),
m_copies(1),
m_onlyCreateImage(false),
m_onlyBurnExistingImage(false),
m_readRetries(128)
{
d = new Private;
}
K3bCloneJob::~K3bCloneJob()
{
delete d;
}
void K3bCloneJob::start()
{
jobStarted();
m_canceled = false;
m_running = true;
// TODO: check the cd size and warn the user if not enough space
//
// We first check if cdrecord has clone support
// The readcdReader will check the same for readcd
//
const K3bExternalBin* cdrecordBin = k3bcore->externalBinManager()->binObject( "cdrecord" );
if( !cdrecordBin ) {
emit infoMessage( i18n("Could not find %1 executable.").arg("cdrecord"), ERROR );
jobFinished(false);
m_running = false;
return;
}
else if( !cdrecordBin->hasFeature( "clone" ) ) {
emit infoMessage( i18n("Cdrecord version %1 does not have cloning support.").arg(cdrecordBin->version), ERROR );
jobFinished(false);
m_running = false;
return;
}
if( (!m_onlyCreateImage && !writer()) ||
(!m_onlyBurnExistingImage && !readingDevice()) ) {
emit infoMessage( i18n("No device set."), ERROR );
jobFinished(false);
m_running = false;
return;
}
if( !m_onlyCreateImage ) {
if( !writer()->supportsWritingMode( K3bDevice::RAW_R96R ) &&
!writer()->supportsWritingMode( K3bDevice::RAW_R16 ) ) {
emit infoMessage( i18n("CD writer %1 does not support cloning.")
.arg(writer()->vendor())
.arg(writer()->description()), ERROR );
m_running = false;
jobFinished(false);
return;
}
}
if( m_imagePath.isEmpty() ) {
m_imagePath = K3b::findTempFile( "img" );
}
else if( TQFileInfo(m_imagePath).isDir() ) {
m_imagePath = K3b::findTempFile( "img", m_imagePath );
}
if( m_onlyBurnExistingImage ) {
startWriting();
}
else {
emit burning( false );
prepareReader();
if( waitForMedia( readingDevice(),
K3bDevice::STATE_COMPLETE,
K3bDevice::MEDIA_WRITABLE_CD|K3bDevice::MEDIA_CD_ROM ) < 0 ) {
m_running = false;
emit canceled();
jobFinished(false);
return;
}
emit newTask( i18n("Reading clone image") );
m_readcdReader->start();
}
}
void K3bCloneJob::prepareReader()
{
if( !m_readcdReader ) {
m_readcdReader = new K3bReadcdReader( this, this );
connect( m_readcdReader, TQT_SIGNAL(percent(int)), this, TQT_SLOT(slotReadingPercent(int)) );
connect( m_readcdReader, TQT_SIGNAL(percent(int)), this, TQT_SIGNAL(subPercent(int)) );
connect( m_readcdReader, TQT_SIGNAL(processedSize(int, int)), this, TQT_SIGNAL(processedSubSize(int, int)) );
connect( m_readcdReader, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotReadingFinished(bool)) );
connect( m_readcdReader, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
connect( m_readcdReader, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
connect( m_readcdReader, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );
}
m_readcdReader->setReadDevice( readingDevice() );
m_readcdReader->setReadSpeed( 0 ); // MAX
m_readcdReader->setDisableCorrection( m_noCorrection );
m_readcdReader->setImagePath( m_imagePath );
m_readcdReader->setClone( true );
m_readcdReader->setRetries( m_readRetries );
}
void K3bCloneJob::prepareWriter()
{
if( !m_writerJob ) {
m_writerJob = new K3bCdrecordWriter( writer(), this, this );
connect( m_writerJob, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
connect( m_writerJob, TQT_SIGNAL(percent(int)), this, TQT_SLOT(slotWriterPercent(int)) );
connect( m_writerJob, TQT_SIGNAL(percent(int)), this, TQT_SIGNAL(subPercent(int)) );
connect( m_writerJob, TQT_SIGNAL(nextTrack(int, int)), this, TQT_SLOT(slotWriterNextTrack(int, int)) );
connect( m_writerJob, TQT_SIGNAL(processedSize(int, int)), this, TQT_SIGNAL(processedSubSize(int, int)) );
connect( m_writerJob, TQT_SIGNAL(buffer(int)), this, TQT_SIGNAL(bufferStatus(int)) );
connect( m_writerJob, TQT_SIGNAL(deviceBuffer(int)), this, TQT_SIGNAL(deviceBuffer(int)) );
connect( m_writerJob, TQT_SIGNAL(writeSpeed(int, int)), this, TQT_SIGNAL(writeSpeed(int, int)) );
connect( m_writerJob, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotWriterFinished(bool)) );
// connect( m_writerJob, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newTask(const TQString&)) );
connect( m_writerJob, TQT_SIGNAL(newSubTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
connect( m_writerJob, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );
}
m_writerJob->clearArguments();
m_writerJob->setWritingMode( K3b::RAW );
m_writerJob->setClone( true );
m_writerJob->setSimulate( m_simulate );
m_writerJob->setBurnSpeed( m_speed );
m_writerJob->addArgument( m_imagePath );
}
void K3bCloneJob::cancel()
{
if( m_running ) {
m_canceled = true;
if( m_readcdReader )
m_readcdReader->cancel();
if( m_writerJob )
m_writerJob->cancel();
}
}
void K3bCloneJob::slotWriterPercent( int p )
{
if( m_onlyBurnExistingImage )
emit percent( (int)((double)(d->doneCopies)*100.0/(double)(m_copies) + (double)p/(double)(m_copies)) );
else
emit percent( (int)((double)(1+d->doneCopies)*100.0/(double)(1+m_copies) + (double)p/(double)(1+m_copies)) );
}
void K3bCloneJob::slotWriterNextTrack( int t, int tt )
{
emit newSubTask( i18n("Writing Track %1 of %2").arg(t).arg(tt) );
}
void K3bCloneJob::slotWriterFinished( bool success )
{
if( m_canceled ) {
removeImageFiles();
m_running = false;
emit canceled();
jobFinished(false);
return;
}
if( success ) {
d->doneCopies++;
emit infoMessage( i18n("Successfully written clone copy %1.").arg(d->doneCopies), INFO );
if( d->doneCopies < m_copies ) {
K3bDevice::eject( writer() );
startWriting();
}
else {
if( m_removeImageFiles )
removeImageFiles();
m_running = false;
jobFinished(true);
}
}
else {
removeImageFiles();
m_running = false;
jobFinished(false);
}
}
void K3bCloneJob::slotReadingPercent( int p )
{
emit percent( m_onlyCreateImage ? p : (int)((double)p/(double)(1+m_copies)) );
}
void K3bCloneJob::slotReadingFinished( bool success )
{
if( m_canceled ) {
removeImageFiles();
m_running = false;
emit canceled();
jobFinished(false);
return;
}
if( success ) {
//
// Make a quick test if the image is really valid.
// Readcd does not seem to have proper exit codes
//
K3bCloneTocReader ctr( m_imagePath );
if( ctr.isValid() ) {
emit infoMessage( i18n("Successfully read disk."), INFO );
if( m_onlyCreateImage ) {
m_running = false;
jobFinished(true);
}
else {
if( writer() == readingDevice() )
K3bDevice::eject( writer() );
startWriting();
}
}
else {
emit infoMessage( i18n("Failed to read disk completely in clone mode."), ERROR );
removeImageFiles();
m_running = false;
jobFinished(false);
}
}
else {
emit infoMessage( i18n("Error while reading disk."), ERROR );
removeImageFiles();
m_running = false;
jobFinished(false);
}
}
void K3bCloneJob::startWriting()
{
emit burning( true );
// start writing
prepareWriter();
if( waitForMedia( writer(),
K3bDevice::STATE_EMPTY,
K3bDevice::MEDIA_WRITABLE_CD ) < 0 ) {
removeImageFiles();
m_running = false;
emit canceled();
jobFinished(false);
return;
}
if( m_simulate )
emit newTask( i18n("Simulating clone copy") );
else
emit newTask( i18n("Writing clone copy %1").arg(d->doneCopies+1) );
m_writerJob->start();
}
void K3bCloneJob::removeImageFiles()
{
if( !m_onlyBurnExistingImage ) {
emit infoMessage( i18n("Removing image files."), INFO );
if( TQFile::exists( m_imagePath ) )
TQFile::remove( m_imagePath );
if( TQFile::exists( m_imagePath + ".toc" ) )
TQFile::remove( m_imagePath + ".toc" );
}
}
TQString K3bCloneJob::jobDescription() const
{
if( m_onlyCreateImage )
return i18n("Creating Clone Image");
else if( m_onlyBurnExistingImage ) {
if( m_simulate )
return i18n("Simulating Clone Image");
else
return i18n("Burning Clone Image");
}
else if( m_simulate )
return i18n("Simulating CD Cloning");
else
return i18n("Cloning CD");
}
TQString K3bCloneJob::jobDetails() const
{
return i18n("Creating 1 clone copy",
"Creating %n clone copies",
(m_simulate||m_onlyCreateImage) ? 1 : m_copies );
}
#include "k3bclonejob.moc"
|