summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3biso9660imagewritingjob.cpp
blob: cf63da4d1190ed47913629db39e3a202918e368d (plain)
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
 *
 * $Id: k3biso9660imagewritingjob.cpp 690187 2007-07-20 09:18:03Z 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 "k3biso9660imagewritingjob.h"
#include "k3bverificationjob.h"

#include <k3bdevice.h>
#include <k3bdiskinfo.h>
#include <k3bdevicehandler.h>
#include <k3bcdrecordwriter.h>
#include <k3bcdrdaowriter.h>
#include <k3bgrowisofswriter.h>
#include <k3bglobals.h>
#include <k3bcore.h>
#include <k3bversion.h>
#include <k3bexternalbinmanager.h>
#include <k3bchecksumpipe.h>
#include <k3bfilesplitter.h>

#include <kdebug.h>
#include <kconfig.h>
#include <klocale.h>
#include <ktempfile.h>
#include <kio/global.h>

#include <tqstring.h>
#include <tqtextstream.h>
#include <tqfile.h>
#include <tqapplication.h>


class K3bIso9660ImageWritingJob::Private
{
public:
  K3bChecksumPipe checksumPipe;
  K3bFileSplitter imageFile;
};


K3bIso9660ImageWritingJob::K3bIso9660ImageWritingJob( K3bJobHandler* hdl )
  : K3bBurnJob( hdl ),
    m_writingMode(K3b::WRITING_MODE_AUTO),
    m_simulate(false),
    m_device(0),
    m_noFix(false),
    m_speed(2),
    m_dataMode(K3b::DATA_MODE_AUTO),
    m_writer(0),
    m_tocFile(0),
    m_copies(1),
    m_verifyJob(0)
{
  d = new Private;
}

K3bIso9660ImageWritingJob::~K3bIso9660ImageWritingJob()
{
  delete m_tocFile;
  delete d;
}


void K3bIso9660ImageWritingJob::start()
{
  m_canceled = m_finished = false;
  m_currentCopy = 1;

  jobStarted();

  if( m_simulate )
    m_verifyData = false;

  emit newTask( i18n("Preparing data") );

  if( !TQFile::exists( m_imagePath ) ) {
    emit infoMessage( i18n("Could not find image %1").tqarg(m_imagePath), K3bJob::ERROR );
    jobFinished( false );
    return;
  }

  unsigned long gb = K3b::imageFilesize( m_imagePath )/1024/1024;

  // very rough test but since most dvd images are 4,x or 8,x GB it should be enough
  m_dvd = ( gb > 900 );

  startWriting();
}


void K3bIso9660ImageWritingJob::slotWriterJobFinished( bool success )
{
  if( m_canceled ) {
    m_finished = true;
    emit canceled();
    jobFinished(false);
    return;
  }

  d->checksumPipe.close();

  if( success ) {
    if( !m_simulate && m_verifyData ) {
      emit burning(false);

      // allright
      // the writerJob should have emited the "simulation/writing successful" signal

      if( !m_verifyJob ) {
	m_verifyJob = new K3bVerificationJob( this );
	connectSubJob( m_verifyJob,
		       TQT_SLOT(slotVerificationFinished(bool)),
		       true,
		       TQT_SLOT(slotVerificationProgress(int)),
		       TQT_SIGNAL(subPercent(int)) );
      }
      m_verifyJob->setDevice( m_device );
      m_verifyJob->clear();
      m_verifyJob->addTrack( 1, d->checksumPipe.checksum(), K3b::imageFilesize( m_imagePath )/2048 );

      if( m_copies == 1 )
	emit newTask( i18n("Verifying written data") );
      else
	emit newTask( i18n("Verifying written copy %1 of %2").tqarg(m_currentCopy).tqarg(m_copies) );

      m_verifyJob->start();
    }
    else if( m_currentCopy >= m_copies ) {
      m_finished = true;
      jobFinished(true);
    }
    else {
      m_currentCopy++;
      startWriting();
    }
  }
  else {
    m_finished = true;
    jobFinished(false);
  }
}


void K3bIso9660ImageWritingJob::slotVerificationFinished( bool success )
{
  if( m_canceled ) {
    m_finished = true;
    emit canceled();
    jobFinished(false);
    return;
  }

  if( success && m_currentCopy < m_copies ) {
    m_currentCopy++;
    connect( K3bDevice::eject( m_device ), TQT_SIGNAL(finished(bool)),
	     this, TQT_SLOT(startWriting()) );
    return;
  }

  k3bcore->config()->setGroup("General Options");
  if( !k3bcore->config()->readBoolEntry( "No cd eject", false ) )
    K3bDevice::eject( m_device );

  m_finished = true;
  jobFinished( success );
}


void K3bIso9660ImageWritingJob::slotVerificationProgress( int p )
{
  emit percent( (int)(100.0 / (double)m_copies * ( (double)(m_currentCopy-1) + 0.5 + (double)p/200.0 )) );
}


void K3bIso9660ImageWritingJob::slotWriterPercent( int p )
{
  emit subPercent( p );

  if( m_verifyData )
    emit percent( (int)(100.0 / (double)m_copies * ( (double)(m_currentCopy-1) + ((double)p/200.0) )) );
  else
    emit percent( (int)(100.0 / (double)m_copies * ( (double)(m_currentCopy-1) + ((double)p/100.0) )) );
}


void K3bIso9660ImageWritingJob::slotNextTrack( int, int )
{
  if( m_copies == 1 )
    emit newSubTask( i18n("Writing image") );
  else
    emit newSubTask( i18n("Writing copy %1 of %2").tqarg(m_currentCopy).tqarg(m_copies) );
}


void K3bIso9660ImageWritingJob::cancel()
{
  if( !m_finished ) {
    m_canceled = true;

    if( m_writer )
      m_writer->cancel();
    if( m_verifyData && m_verifyJob )
      m_verifyJob->cancel();
  }
}


void K3bIso9660ImageWritingJob::startWriting()
{
  emit newSubTask( i18n("Waiting for medium") );

  // we wait for the following:
  // 1. if writing mode auto and writing app auto: all writable media types
  // 2. if writing mode auto and writing app not growisofs: all writable cd types
  // 3. if writing mode auto and writing app growisofs: all writable dvd types
  // 4. if writing mode TAO or RAW: all writable cd types
  // 5. if writing mode DAO and writing app auto: all writable cd types and DVD-R(W)
  // 6. if writing mode DAO and writing app GROWISOFS: DVD-R(W)
  // 7. if writing mode DAO and writing app CDRDAO or CDRECORD: all writable cd types
  // 8. if writing mode WRITING_MODE_INCR_SEQ: DVD-R(W)
  // 9. if writing mode WRITING_MODE_RES_OVWR: DVD-RW or DVD+RW

  int mt = 0;
  if( m_writingMode == K3b::WRITING_MODE_AUTO ) {
    if( writingApp() == K3b::DEFAULT ) {
      if( m_dvd )
	mt = K3bDevice::MEDIA_WRITABLE_DVD;
      else
	mt = K3bDevice::MEDIA_WRITABLE_CD;
    }
    else if( writingApp() != K3b::GROWISOFS )
      mt = K3bDevice::MEDIA_WRITABLE_CD;
    else
      mt = K3bDevice::MEDIA_WRITABLE_DVD;
  }
  else if( m_writingMode == K3b::TAO || m_writingMode == K3b::RAW )
    mt = K3bDevice::MEDIA_WRITABLE_CD;
  else if( m_writingMode == K3b::DAO ) {
    if( writingApp() == K3b::DEFAULT ) {
      if( m_dvd )
	mt = K3bDevice::MEDIA_WRITABLE_DVD;
      else
	mt = K3bDevice::MEDIA_WRITABLE_CD;
    }
    else if( writingApp() == K3b::GROWISOFS )
      mt = K3bDevice::MEDIA_WRITABLE_DVD;
    else
      mt = K3bDevice::MEDIA_WRITABLE_CD;
  }
  else if( m_writingMode == K3b::WRITING_MODE_RES_OVWR )
    mt = K3bDevice::MEDIA_DVD_PLUS_R|K3bDevice::MEDIA_DVD_PLUS_R_DL|K3bDevice::MEDIA_DVD_PLUS_RW|K3bDevice::MEDIA_DVD_RW_OVWR;
  else
    mt = K3bDevice::MEDIA_WRITABLE_DVD;


  // wait for the media
  int media = waitForMedia( m_device, K3bDevice::STATE_EMPTY, mt );
  if( media < 0 ) {
    m_finished = true;
    emit canceled();
    jobFinished(false);
    return;
  }

  // we simply always calculate the checksum, thus making the code simpler
  d->imageFile.close();
  d->imageFile.setName( m_imagePath );
  d->imageFile.open( IO_ReadOnly );
  d->checksumPipe.close();
  d->checksumPipe.readFromIODevice( &d->imageFile );

  if( prepareWriter( media ) ) {
    emit burning(true);
    m_writer->start();
    d->checksumPipe.writeToFd( m_writer->fd(), true );
    d->checksumPipe.open( K3bChecksumPipe::MD5, true );
  }
  else {
    m_finished = true;
    jobFinished(false);
  }
}


bool K3bIso9660ImageWritingJob::prepareWriter( int mediaType )
{
  if( mediaType == 0 ) { // media forced
    // just to get it going...
    if( writingApp() != K3b::GROWISOFS && !m_dvd )
      mediaType = K3bDevice::MEDIA_CD_R;
    else
      mediaType = K3bDevice::MEDIA_DVD_R;
  }

  delete m_writer;

  if( mediaType == K3bDevice::MEDIA_CD_R || mediaType == K3bDevice::MEDIA_CD_RW ) {
    int usedWritingMode = m_writingMode;
    if( usedWritingMode == K3b::WRITING_MODE_AUTO ) {
      // cdrecord seems to have problems when writing in mode2 in dao mode
      // so with cdrecord we use TAO
      if( m_noFix || m_dataMode == K3b::MODE2 || !m_device->dao() )
	usedWritingMode = K3b::TAO;
      else
	usedWritingMode = K3b::DAO;
    }

    int usedApp = writingApp();
    if( usedApp == K3b::DEFAULT ) {
      if( usedWritingMode == K3b::DAO &&
	  ( m_dataMode == K3b::MODE2 || m_noFix ) )
	usedApp = K3b::CDRDAO;
      else
	usedApp = K3b::CDRECORD;
    }


    if( usedApp == K3b::CDRECORD ) {
      K3bCdrecordWriter* writer = new K3bCdrecordWriter( m_device, this );

      writer->setWritingMode( usedWritingMode );
      writer->setSimulate( m_simulate );
      writer->setBurnSpeed( m_speed );

      if( m_noFix ) {
	writer->addArgument("-multi");
      }

      if( (m_dataMode == K3b::DATA_MODE_AUTO && m_noFix) ||
	  m_dataMode == K3b::MODE2 ) {
	if( k3bcore->externalBinManager()->binObject("cdrecord") &&
	    k3bcore->externalBinManager()->binObject("cdrecord")->hasFeature( "xamix" ) )
	  writer->addArgument( "-xa" );
	else
	  writer->addArgument( "-xa1" );
      }
      else
	writer->addArgument("-data");

      // read from stdin
      writer->addArgument( TQString("-tsize=%1s").tqarg( K3b::imageFilesize( m_imagePath )/2048 ) )->addArgument( "-" );

      m_writer = writer;
    }
    else {
      // create cdrdao job
      K3bCdrdaoWriter* writer = new K3bCdrdaoWriter( m_device, this );
      writer->setCommand( K3bCdrdaoWriter::WRITE );
      writer->setSimulate( m_simulate );
      writer->setBurnSpeed( m_speed );
      // multisession
      writer->setMulti( m_noFix );

      // now write the tocfile
      delete m_tocFile;
      m_tocFile = new KTempFile( TQString(), "toc" );
      m_tocFile->setAutoDelete(true);

      if( TQTextStream* s = m_tocFile->textStream() ) {
	if( (m_dataMode == K3b::DATA_MODE_AUTO && m_noFix) ||
	    m_dataMode == K3b::MODE2 ) {
	  *s << "CD_ROM_XA" << "\n";
	  *s << "\n";
	  *s << "TRACK MODE2_FORM1" << "\n";
	}
	else {
	  *s << "CD_ROM" << "\n";
	  *s << "\n";
	  *s << "TRACK MODE1" << "\n";
	}
	*s << "DATAFILE \"-\" " << TQString::number( K3b::imageFilesize( m_imagePath ) ) << "\n";

	m_tocFile->close();
      }
      else {
	kdDebug() << "(K3bDataJob) could not write tocfile." << endl;
	emit infoMessage( i18n("IO Error"), ERROR );
	return false;
      }

      writer->setTocFile( m_tocFile->name() );

      m_writer = writer;
    }
  }
  else {  // DVD
    if( mediaType & K3bDevice::MEDIA_DVD_PLUS_ALL ) {
      if( m_simulate ) {
	if( questionYesNo( i18n("K3b does not support simulation with DVD+R(W) media. "
				"Do you really want to continue? The media will be written "
				"for real."),
			   i18n("No Simulation with DVD+R(W)") ) ) {
	  return false;
	}
      }

      m_simulate = false;
    }

    K3bGrowisofsWriter* writer = new K3bGrowisofsWriter( m_device, this );
    writer->setSimulate( m_simulate );
    writer->setBurnSpeed( m_speed );
    writer->setWritingMode( m_writingMode == K3b::DAO ? K3b::DAO : 0 );
    writer->setImageToWrite( TQString() ); // read from stdin
    writer->setCloseDvd( !m_noFix );
    writer->setTrackSize( K3b::imageFilesize( m_imagePath )/2048 );

    m_writer = writer;
  }

  connect( m_writer, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
  connect( m_writer, TQT_SIGNAL(nextTrack(int, int)), this, TQT_SLOT(slotNextTrack(int, int)) );
  connect( m_writer, TQT_SIGNAL(percent(int)), this, TQT_SLOT(slotWriterPercent(int)) );
  connect( m_writer, TQT_SIGNAL(processedSize(int, int)), this, TQT_SIGNAL(processedSize(int, int)) );
  connect( m_writer, TQT_SIGNAL(buffer(int)), this, TQT_SIGNAL(buffertqStatus(int)) );
  connect( m_writer, TQT_SIGNAL(deviceBuffer(int)), this, TQT_SIGNAL(deviceBuffer(int)) );
  connect( m_writer, TQT_SIGNAL(writeSpeed(int, int)), this, TQT_SIGNAL(writeSpeed(int, int)) );
  connect( m_writer, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotWriterJobFinished(bool)) );
  connect( m_writer, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newTask(const TQString&)) );
  connect( m_writer, TQT_SIGNAL(newSubTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
  connect( m_writer, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
	   this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );

  return true;
}


TQString K3bIso9660ImageWritingJob::jobDescription() const
{
  if( m_simulate )
    return i18n("Simulating ISO9660 Image");
  else
    return ( i18n("Burning ISO9660 Image")
	     + ( m_copies > 1
		 ? i18n(" - %n Copy", " - %n Copies", m_copies)
		 : TQString() ) );
}


TQString K3bIso9660ImageWritingJob::jobDetails() const
{
  return m_imagePath.section("/", -1) + TQString( " (%1)" ).tqarg(KIO::convertSize(K3b::filesize(KURL::fromPathOrURL(m_imagePath))));
}


#include "k3biso9660imagewritingjob.moc"