summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3bdvdcopyjob.cpp
blob: 69eb8d54dc61164671ad12122c5905fe2b664c4e (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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
/*
 *
 * $Id: k3bdvdcopyjob.cpp 690529 2007-07-21 10:51:47Z 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 "k3bdvdcopyjob.h"
#include "k3blibdvdcss.h"

#include <k3breadcdreader.h>
#include <k3bdatatrackreader.h>
#include <k3bexternalbinmanager.h>
#include <k3bdevice.h>
#include <k3bdeviceglobals.h>
#include <k3bdevicehandler.h>
#include <k3bdiskinfo.h>
#include <k3bglobals.h>
#include <k3bcore.h>
#include <k3bgrowisofswriter.h>
#include <k3bversion.h>
#include <k3biso9660.h>
#include <k3bfilesplitter.h>
#include <k3bchecksumpipe.h>
#include <k3bverificationjob.h>

#include <kdebug.h>
#include <tdelocale.h>
#include <tdeio/global.h>

#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqapplication.h>


class K3bDvdCopyJob::Private
{
public:
  Private()
    : doneCopies(0),
      running(false),
      canceled(false),
      writerJob(0),
      readcdReader(0),
      dataTrackReader(0),
      verificationJob(0),
      usedWritingMode(0),
      verifyData(false) {
    outPipe.readFromIODevice( &imageFile );
  }

  int doneCopies;

  bool running;
  bool readerRunning;
  bool writerRunning;
  bool canceled;

  K3bGrowisofsWriter* writerJob;
  K3bReadcdReader* readcdReader;
  K3bDataTrackReader* dataTrackReader;
  K3bVerificationJob* verificationJob;

  K3bDevice::DiskInfo sourceDiskInfo;

  K3b::Msf lastSector;

  int usedWritingMode;

  K3bFileSplitter imageFile;
  K3bChecksumPipe inPipe;
  K3bActivePipe outPipe;

  bool verifyData;
};


K3bDvdCopyJob::K3bDvdCopyJob( K3bJobHandler* hdl, TQObject* parent, const char* name )
  : K3bBurnJob( hdl, parent, name ),
    m_writerDevice(0),
    m_readerDevice(0),
    m_onTheFly(false),
    m_removeImageFiles(false),
    m_simulate(false),
    m_speed(1),
    m_copies(1),
    m_onlyCreateImage(false),
    m_ignoreReadErrors(false),
    m_readRetries(128),
    m_writingMode( K3b::WRITING_MODE_AUTO )
{
  d = new Private();
}


K3bDvdCopyJob::~K3bDvdCopyJob()
{
  delete d;
}


void K3bDvdCopyJob::start()
{
  jobStarted();
  emit burning(false);

  d->canceled = false;
  d->running = true;
  d->readerRunning = d->writerRunning = false;

  emit newTask( i18n("Checking Source Medium") );

  if( m_onTheFly &&
      k3bcore->externalBinManager()->binObject( "growisofs" )->version < K3bVersion( 5, 12 ) ) {
    m_onTheFly = false;
    emit infoMessage( i18n("K3b does not support writing on-the-fly with growisofs %1.")
		      .arg(k3bcore->externalBinManager()->binObject( "growisofs" )->version), ERROR );
    emit infoMessage( i18n("Disabling on-the-fly writing."), INFO );
  }

  emit newSubTask( i18n("Waiting for source medium") );

  // wait for a source disk
  if( waitForMedia( m_readerDevice,
		    K3bDevice::STATE_COMPLETE|K3bDevice::STATE_INCOMPLETE,
		    K3bDevice::MEDIA_WRITABLE_DVD|K3bDevice::MEDIA_DVD_ROM ) < 0 ) {
    emit canceled();
    d->running = false;
    jobFinished( false );
    return;
  }

  emit newSubTask( i18n("Checking source medium") );

  connect( K3bDevice::sendCommand( K3bDevice::DeviceHandler::DISKINFO, m_readerDevice ),
           TQT_SIGNAL(finished(K3bDevice::DeviceHandler*)),
           this,
           TQT_SLOT(slotDiskInfoReady(K3bDevice::DeviceHandler*)) );
}


void K3bDvdCopyJob::slotDiskInfoReady( K3bDevice::DeviceHandler* dh )
{
  if( d->canceled ) {
    emit canceled();
    jobFinished(false);
    d->running = false;
  }

  d->sourceDiskInfo = dh->diskInfo();

  if( dh->diskInfo().empty() || dh->diskInfo().diskState() == K3bDevice::STATE_NO_MEDIA ) {
    emit infoMessage( i18n("No source medium found."), ERROR );
    jobFinished(false);
    d->running = false;
  }
  else {
    if( m_readerDevice->copyrightProtectionSystemType() == 1 ) {
      emit infoMessage( i18n("Found encrypted DVD."), WARNING );
      // check for libdvdcss
      bool haveLibdvdcss = false;
      kdDebug() << "(K3bDvdCopyJob) trying to open libdvdcss." << endl;
      if( K3bLibDvdCss* libcss = K3bLibDvdCss::create() ) {
	kdDebug() << "(K3bDvdCopyJob) succeeded." << endl;
	kdDebug() << "(K3bDvdCopyJob) dvdcss_open(" << m_readerDevice->blockDeviceName() << ") = "
		  << libcss->open(m_readerDevice) << endl;
	haveLibdvdcss = true;

	delete libcss;
      }
      else
	kdDebug() << "(K3bDvdCopyJob) failed." << endl;

      if( !haveLibdvdcss ) {
	emit infoMessage( i18n("Cannot copy encrypted DVDs."), ERROR );
	d->running = false;
	jobFinished( false );
	return;
      }
    }


    //
    // We cannot rely on the kernel to determine the size of the DVD for some reason
    // On the other hand it is not always a good idea to rely on the size from the ISO9660
    // header since that may be wrong due to some buggy encoder or some boot code appended
    // after creating the image.
    // That is why we try our best to determine the size of the DVD. For DVD-ROM this is very
    // easy since it has only one track. The same goes for single session DVD-R(W) and DVD+R.
    // Multisession DVDs we will simply not copy. ;)
    // For DVD+RW and DVD-RW in restricted overwrite mode we are left with no other choice but
    // to use the ISO9660 header.
    //
    // On the other hand: in on-the-fly mode growisofs determines the size of the data to be written
    //                    by looking at the ISO9660 header when writing in DAO mode. So in this case
    //                    it would be best for us to do the same....
    //
    // With growisofs 5.15 we have the option to specify the size of the image to be written in DAO mode.
    //

    switch( dh->diskInfo().mediaType() ) {
    case K3bDevice::MEDIA_DVD_ROM:
    case K3bDevice::MEDIA_DVD_PLUS_R_DL:
    case K3bDevice::MEDIA_DVD_R_DL:
    case K3bDevice::MEDIA_DVD_R_DL_SEQ:
    case K3bDevice::MEDIA_DVD_R_DL_JUMP:
      if( !m_onlyCreateImage ) {
	if( dh->diskInfo().numLayers() > 1 &&
	    dh->diskInfo().size().mode1Bytes() > 4700372992LL ) {
	  if( !(m_writerDevice->type() & (K3bDevice::DEVICE_DVD_R_DL|K3bDevice::DEVICE_DVD_PLUS_R_DL)) ) {
	    emit infoMessage( i18n("The writer does not support writing Double Layer DVD."), ERROR );
	    d->running = false;
	    jobFinished(false);
	    return;
	  }
	  // FIXME: check for growisofs 5.22 (or whatever version is needed) for DVD-R DL
	  else if( k3bcore->externalBinManager()->binObject( "growisofs" ) &&
		   k3bcore->externalBinManager()->binObject( "growisofs" )->version < K3bVersion( 5, 20 ) ) {
	    emit infoMessage( i18n("Growisofs >= 5.20 is needed to write Double Layer DVD+R."), ERROR );
	    d->running = false;
	    jobFinished(false);
	    return;
	  }
	}
      }
    case K3bDevice::MEDIA_DVD_R:
    case K3bDevice::MEDIA_DVD_R_SEQ:
    case K3bDevice::MEDIA_DVD_RW:
    case K3bDevice::MEDIA_DVD_RW_SEQ:
    case K3bDevice::MEDIA_DVD_PLUS_R:

      if( dh->diskInfo().numSessions() > 1 ) {
	emit infoMessage( i18n("K3b does not support copying multi-session DVDs."), ERROR );
	d->running = false;
	jobFinished(false);
	return;
      }

      // growisofs only uses the size from the PVD for reserving
      // writable space in DAO mode
      // with version >= 5.15 growisofs supports specifying the size of the track
      if( m_writingMode != K3b::DAO || !m_onTheFly || m_onlyCreateImage ||
	  ( k3bcore->externalBinManager()->binObject( "growisofs" ) &&
	    k3bcore->externalBinManager()->binObject( "growisofs" )->version >= K3bVersion( 5, 15, -1 ) ) ) {
	d->lastSector = dh->toc().lastSector();
	break;
      }

      // fallthrough

    case K3bDevice::MEDIA_DVD_PLUS_RW:
    case K3bDevice::MEDIA_DVD_RW_OVWR:
      {
	emit infoMessage( i18n("K3b relies on the size saved in the ISO9660 header."), WARNING );
	emit infoMessage( i18n("This might result in a corrupt copy if the source was mastered with buggy software."), WARNING );

	K3bIso9660 isoF( m_readerDevice, 0 );
	if( isoF.open() ) {
	  d->lastSector = ((long long)isoF.primaryDescriptor().logicalBlockSize*isoF.primaryDescriptor().volumeSpaceSize)/2048LL - 1;
	}
	else {
	  emit infoMessage( i18n("Unable to determine the ISO9660 filesystem size."), ERROR );
	  jobFinished(false);
	  d->running = false;
	  return;
	}
      }
      break;

    case K3bDevice::MEDIA_DVD_RAM:
      emit infoMessage( i18n("K3b does not support copying DVD-RAM."), ERROR );
      jobFinished(false);
      d->running = false;
      return;

    default:
      emit infoMessage( i18n("Unable to determine DVD media type."), ERROR );
      jobFinished(false);
      d->running = false;
      return;
    }


    if( !m_onTheFly ) {
      //
      // Check the image path
      //
      TQFileInfo fi( m_imagePath );
      if( !fi.isFile() ||
	  questionYesNo( i18n("Do you want to overwrite %1?").arg(m_imagePath),
			 i18n("File Exists") ) ) {
	if( fi.isDir() )
	  m_imagePath = K3b::findTempFile( "iso", m_imagePath );
	else if( !TQFileInfo( m_imagePath.section( '/', 0, -2 ) ).isDir() ) {
	  emit infoMessage( i18n("Specified an unusable temporary path. Using default."), WARNING );
	  m_imagePath = K3b::findTempFile( "iso" );
	}
	// else the user specified a file in an existing dir

	emit infoMessage( i18n("Writing image file to %1.").arg(m_imagePath), INFO );
	emit newSubTask( i18n("Reading source medium.") );
      }

      //
      // check free temp space
      //
      TDEIO::filesize_t imageSpaceNeeded = (TDEIO::filesize_t)(d->lastSector.lba()+1)*2048;
      unsigned long avail, size;
      TQString pathToTest = m_imagePath.left( m_imagePath.findRev( '/' ) );
      if( !K3b::kbFreeOnFs( pathToTest, size, avail ) ) {
	emit infoMessage( i18n("Unable to determine free space in temporary directory '%1'.").arg(pathToTest), ERROR );
	jobFinished(false);
	d->running = false;
	return;
      }
      else {
	if( avail < imageSpaceNeeded/1024 ) {
	  emit infoMessage( i18n("Not enough space left in temporary directory."), ERROR );
	  jobFinished(false);
	  d->running = false;
	  return;
	}
      }

      d->imageFile.setName( m_imagePath );
      if( !d->imageFile.open( IO_WriteOnly ) ) {
	emit infoMessage( i18n("Unable to open '%1' for writing.").arg(m_imagePath), ERROR );
	jobFinished( false );
	d->running = false;
	return;
      }
    }

    if( K3b::isMounted( m_readerDevice ) ) {
      emit infoMessage( i18n("Unmounting source medium"), INFO );
      K3b::unmount( m_readerDevice );
    }

    if( m_onlyCreateImage || !m_onTheFly ) {
      emit newTask( i18n("Creating DVD image") );
    }
    else if( m_onTheFly && !m_onlyCreateImage ) {
      if( waitForDvd() ) {
	prepareWriter();
	if( m_simulate )
	  emit newTask( i18n("Simulating DVD copy") );
	else if( m_copies > 1 )
	  emit newTask( i18n("Writing DVD copy %1").arg(d->doneCopies+1) );
	else
	  emit newTask( i18n("Writing DVD copy") );

	emit burning(true);
	d->writerRunning = true;
	d->writerJob->start();
      }
      else {
	if( d->canceled )
	  emit canceled();
	jobFinished(false);
	d->running = false;
	return;
      }
    }

    prepareReader();
    d->readerRunning = true;
    d->dataTrackReader->start();
  }
}


void K3bDvdCopyJob::cancel()
{
  if( d->running ) {
    d->canceled = true;
    if( d->readerRunning  )
      d->dataTrackReader->cancel();
    if( d->writerRunning )
      d->writerJob->cancel();
    d->inPipe.close();
    d->outPipe.close();
    d->imageFile.close();
  }
  else {
    kdDebug() << "(K3bDvdCopyJob) not running." << endl;
  }
}


void K3bDvdCopyJob::prepareReader()
{
  if( !d->dataTrackReader ) {
    d->dataTrackReader = new K3bDataTrackReader( this );
    connect( d->dataTrackReader, TQT_SIGNAL(percent(int)), this, TQT_SLOT(slotReaderProgress(int)) );
    connect( d->dataTrackReader, TQT_SIGNAL(processedSize(int, int)), this, TQT_SLOT(slotReaderProcessedSize(int, int)) );
    connect( d->dataTrackReader, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotReaderFinished(bool)) );
    connect( d->dataTrackReader, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
    connect( d->dataTrackReader, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
    connect( d->dataTrackReader, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
             this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );
  }

  d->dataTrackReader->setDevice( m_readerDevice );
  d->dataTrackReader->setIgnoreErrors( m_ignoreReadErrors );
  d->dataTrackReader->setRetries( m_readRetries );
  d->dataTrackReader->setSectorRange( 0, d->lastSector );

  if( m_onTheFly && !m_onlyCreateImage )
    d->inPipe.writeToFd( d->writerJob->fd(), true );
  else
    d->inPipe.writeToIODevice( &d->imageFile );

  d->inPipe.open( true );
  d->dataTrackReader->writeToFd( d->inPipe.in() );
}


// ALWAYS CALL WAITFORDVD BEFORE PREPAREWRITER!
void K3bDvdCopyJob::prepareWriter()
{
  delete d->writerJob;

  d->writerJob = new K3bGrowisofsWriter( m_writerDevice, this );

  connect( d->writerJob, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
  connect( d->writerJob, TQT_SIGNAL(percent(int)), this, TQT_SLOT(slotWriterProgress(int)) );
  connect( d->writerJob, TQT_SIGNAL(processedSize(int, int)), this, TQT_SIGNAL(processedSize(int, int)) );
  connect( d->writerJob, TQT_SIGNAL(processedSubSize(int, int)), this, TQT_SIGNAL(processedSubSize(int, int)) );
  connect( d->writerJob, TQT_SIGNAL(buffer(int)), this, TQT_SIGNAL(bufferStatus(int)) );
  connect( d->writerJob, TQT_SIGNAL(deviceBuffer(int)), this, TQT_SIGNAL(deviceBuffer(int)) );
  connect( d->writerJob, TQT_SIGNAL(writeSpeed(int, int)), this, TQT_SIGNAL(writeSpeed(int, int)) );
  connect( d->writerJob, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotWriterFinished(bool)) );
  //  connect( d->writerJob, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newTask(const TQString&)) );
  connect( d->writerJob, TQT_SIGNAL(newSubTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
  connect( d->writerJob, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
	   this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );

  // these do only make sense with DVD-R(W)
  d->writerJob->setSimulate( m_simulate );
  d->writerJob->setBurnSpeed( m_speed );
  d->writerJob->setWritingMode( d->usedWritingMode );
  d->writerJob->setCloseDvd( true );

  //
  // In case the first layer size is not known let the
  // split be determined by growisofs
  //
  if( d->sourceDiskInfo.numLayers() > 1 &&
      d->sourceDiskInfo.firstLayerSize() > 0 ) {
    d->writerJob->setLayerBreak( d->sourceDiskInfo.firstLayerSize().lba() );
  }
  else {
    // this is only used in DAO mode with growisofs >= 5.15
    d->writerJob->setTrackSize( d->lastSector.lba()+1 );
  }

  d->writerJob->setImageToWrite( TQString() ); // write to stdin
}


void K3bDvdCopyJob::slotReaderProgress( int p )
{
  if( !m_onTheFly || m_onlyCreateImage ) {
    emit subPercent( p );

    int bigParts = ( m_onlyCreateImage ? 1 : (m_simulate ? 2 : ( d->verifyData ? m_copies*2 : m_copies ) + 1 ) );
    emit percent( p/bigParts );
  }
}


void K3bDvdCopyJob::slotReaderProcessedSize( int p, int c )
{
  if( !m_onTheFly || m_onlyCreateImage )
    emit processedSubSize( p, c );

  if( m_onlyCreateImage )
    emit processedSize( p, c );
}


void K3bDvdCopyJob::slotWriterProgress( int p )
{
  int bigParts = ( m_simulate ? 1 : ( d->verifyData ? m_copies*2 : m_copies ) ) + ( m_onTheFly ? 0 : 1 );
  int doneParts = ( m_simulate ? 0 : ( d->verifyData ? d->doneCopies*2 : d->doneCopies ) ) + ( m_onTheFly ? 0 : 1 );
  emit percent( 100*doneParts/bigParts + p/bigParts );

  emit subPercent( p );
}


void K3bDvdCopyJob::slotVerificationProgress( int p )
{
  int bigParts = ( m_simulate ? 1 : ( d->verifyData ? m_copies*2 : m_copies ) ) + ( m_onTheFly ? 0 : 1 );
  int doneParts = ( m_simulate ? 0 : ( d->verifyData ? d->doneCopies*2 : d->doneCopies ) ) + ( m_onTheFly ? 0 : 1 ) + 1;
  emit percent( 100*doneParts/bigParts + p/bigParts );
}


void K3bDvdCopyJob::slotReaderFinished( bool success )
{
  d->readerRunning = false;

  d->inPipe.close();

  // close the socket
  // otherwise growisofs will never quit.
  // FIXME: is it posiible to do this in a generic manner?
  if( d->writerJob )
    d->writerJob->closeFd();

  // already finished?
  if( !d->running )
    return;

  if( d->canceled ) {
    removeImageFiles();
    emit canceled();
    jobFinished(false);
    d->running = false;
  }

  if( success ) {
    emit infoMessage( i18n("Successfully read source DVD."), SUCCESS );
    if( m_onlyCreateImage ) {
      jobFinished(true);
      d->running = false;
    }
    else {
      if( m_writerDevice == m_readerDevice ) {
	// eject the media (we do this blocking to know if it worked
	// because if it did not it might happen that k3b overwrites a CD-RW
	// source)
	if( !m_readerDevice->eject() ) {
	  blockingInformation( i18n("K3b was unable to eject the source disk. Please do so manually.") );
	}
      }

      if( !m_onTheFly ) {
	if( waitForDvd() ) {
	  prepareWriter();
	  if( m_copies > 1 )
	    emit newTask( i18n("Writing DVD copy %1").arg(d->doneCopies+1) );
	  else
	    emit newTask( i18n("Writing DVD copy") );

	  emit burning(true);

	  d->writerRunning = true;
	  d->writerJob->start();
	  d->outPipe.writeToFd( d->writerJob->fd(), true );
	  d->outPipe.open( true );
	}
	else {
	  if( m_removeImageFiles )
	    removeImageFiles();
	  if( d->canceled )
	    emit canceled();
	  jobFinished(false);
	  d->running = false;
	}
      }
    }
  }
  else {
    removeImageFiles();
    jobFinished(false);
    d->running = false;
  }
}


void K3bDvdCopyJob::slotWriterFinished( bool success )
{
  d->writerRunning = false;

  d->outPipe.close();

  // already finished?
  if( !d->running )
    return;

  if( d->canceled ) {
    if( m_removeImageFiles )
      removeImageFiles();
    emit canceled();
    jobFinished(false);
    d->running = false;
  }

  if( success ) {
    emit infoMessage( i18n("Successfully written DVD copy %1.").arg(d->doneCopies+1), INFO );

    if( d->verifyData && !m_simulate ) {
      if( !d->verificationJob ) {
	d->verificationJob = new K3bVerificationJob( this, this );
	connect( d->verificationJob, TQT_SIGNAL(infoMessage(const TQString&, int)),
		 this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
	connect( d->verificationJob, TQT_SIGNAL(newTask(const TQString&)),
		 this, TQT_SIGNAL(newSubTask(const TQString&)) );
	connect( d->verificationJob, TQT_SIGNAL(percent(int)),
		 this, TQT_SLOT(slotVerificationProgress(int)) );
	connect( d->verificationJob, TQT_SIGNAL(percent(int)),
		 this, TQT_SIGNAL(subPercent(int)) );
	connect( d->verificationJob, TQT_SIGNAL(finished(bool)),
		 this, TQT_SLOT(slotVerificationFinished(bool)) );
	connect( d->verificationJob, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
		 this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );

      }
      d->verificationJob->setDevice( m_writerDevice );
      d->verificationJob->addTrack( 1, d->inPipe.checksum(), d->lastSector+1 );

      if( m_copies > 1 )
	emit newTask( i18n("Verifying DVD copy %1").arg(d->doneCopies+1) );
      else
	emit newTask( i18n("Verifying DVD copy") );

      emit burning( false );

      d->verificationJob->start();
    }

    else if( ++d->doneCopies < m_copies ) {

        if ( !m_writerDevice->eject() ) {
            blockingInformation( i18n("K3b was unable to eject the written disk. Please do so manually.") );
        }

      if( waitForDvd() ) {
	prepareWriter();
	emit newTask( i18n("Writing DVD copy %1").arg(d->doneCopies+1) );

	emit burning(true);

	d->writerRunning = true;
	d->writerJob->start();
      }
      else {
	if( d->canceled )
	  emit canceled();
	jobFinished(false);
	d->running = false;
	return;
      }

      if( m_onTheFly ) {
	prepareReader();
	d->readerRunning = true;
	d->dataTrackReader->start();
      }
      else {
	d->outPipe.writeToFd( d->writerJob->fd(), true );
	d->outPipe.open( true );
      }
    }
    else {
      if( m_removeImageFiles )
	removeImageFiles();
      d->running = false;
      jobFinished(true);
    }
  }
  else {
    if( m_removeImageFiles )
      removeImageFiles();
    d->running = false;
    jobFinished(false);
  }
}


void K3bDvdCopyJob::slotVerificationFinished( bool success )
{
  // we simply ignore the results from the verification, the verification
  // job already emits a message
  if( ++d->doneCopies < m_copies ) {

    if( waitForDvd() ) {
      prepareWriter();
      emit newTask( i18n("Writing DVD copy %1").arg(d->doneCopies+1) );

      emit burning(true);

      d->writerRunning = true;
      d->writerJob->start();
    }
    else {
      if( d->canceled )
	emit canceled();
      jobFinished(false);
      d->running = false;
      return;
    }

    if( m_onTheFly ) {
      prepareReader();
      d->readerRunning = true;
      d->dataTrackReader->start();
    }
    else {
      d->outPipe.writeToFd( d->writerJob->fd(), true );
      d->outPipe.open( true );
    }
  }
  else {
    if( m_removeImageFiles )
      removeImageFiles();
    d->running = false;
    jobFinished( success );
  }
}


// this is basically the same code as in K3bDvdJob... :(
// perhaps this should be moved to some K3bGrowisofsHandler which also parses the growisofs output?
bool K3bDvdCopyJob::waitForDvd()
{
  int mt = 0;
  if( m_writingMode == K3b::WRITING_MODE_RES_OVWR ) // we treat DVD+R(W) as restricted overwrite media
    mt = K3bDevice::MEDIA_DVD_RW_OVWR|K3bDevice::MEDIA_DVD_PLUS_RW|K3bDevice::MEDIA_DVD_PLUS_R;
  else
    mt = K3bDevice::MEDIA_WRITABLE_DVD_SL;

  //
  // in case the source is a double layer DVD we made sure above that the writer
  // is capable of writing DVD+R-DL or DVD-R DL and here we wait for a DL DVD
  //
  if( d->sourceDiskInfo.numLayers() > 1 &&
      d->sourceDiskInfo.size().mode1Bytes() > 4700372992LL ) {
    mt = K3bDevice::MEDIA_WRITABLE_DVD_DL;
  }

  int m = waitForMedia( m_writerDevice, K3bDevice::STATE_EMPTY, mt );

  if( m < 0 ) {
    cancel();
    return false;
  }

  if( m == 0 ) {
    emit infoMessage( i18n("Forced by user. Growisofs will be called without further tests."), INFO );
  }

  else {
    // -------------------------------
    // DVD Plus
    // -------------------------------
    if( m & K3bDevice::MEDIA_DVD_PLUS_ALL ) {

      d->usedWritingMode = K3b::WRITING_MODE_RES_OVWR;

      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 actually be "
				 "written to."),
			    i18n("No Simulation with DVD+R(W)") ) ) {
	  cancel();
	  return false;
	}

//	m_simulate = false;
	emit newTask( i18n("Writing DVD copy") );
      }

      if( m_writingMode != K3b::WRITING_MODE_AUTO && m_writingMode != K3b::WRITING_MODE_RES_OVWR )
	emit infoMessage( i18n("Writing mode ignored when writing DVD+R(W) media."), INFO );

      if( m & K3bDevice::MEDIA_DVD_PLUS_RW )
	emit infoMessage( i18n("Writing DVD+RW."), INFO );
      else if( m & K3bDevice::MEDIA_DVD_PLUS_R_DL )
	emit infoMessage( i18n("Writing Double Layer DVD+R."), INFO );
      else
	emit infoMessage( i18n("Writing DVD+R."), INFO );
    }

    // -------------------------------
    // DVD Minus
    // -------------------------------
    else {
      if( m_simulate && !m_writerDevice->dvdMinusTestwrite() ) {
	if( !questionYesNo( i18n("Your writer (%1 %2) does not support simulation with DVD-R(W) media. "
				 "Do you really want to continue? The media will be written "
				 "for real.")
			   .arg(m_writerDevice->vendor())
			   .arg(m_writerDevice->description()),
			   i18n("No Simulation with DVD-R(W)") ) ) {
	  cancel();
	  return false;
	}

//	m_simulate = false;
      }

      //
      // We do not default to DAO in onthefly mode since otherwise growisofs would
      // use the size from the PVD to reserve space on the DVD and that can be bad
      // if this size is wrong
      // With growisofs 5.15 we have the option to specify the size of the image to be written in DAO mode.
      //
//       bool sizeWithDao = ( k3bcore->externalBinManager()->binObject( "growisofs" ) &&
// 			   k3bcore->externalBinManager()->binObject( "growisofs" )->version >= K3bVersion( 5, 15, -1 ) );


      // TODO: check for feature 0x21

      if( m & K3bDevice::MEDIA_DVD_RW_OVWR ) {
	emit infoMessage( i18n("Writing DVD-RW in restricted overwrite mode."), INFO );
	d->usedWritingMode = K3b::WRITING_MODE_RES_OVWR;
      }
      else if( m & (K3bDevice::MEDIA_DVD_RW_SEQ|
		    K3bDevice::MEDIA_DVD_RW) ) {
	if( m_writingMode == K3b::DAO ) {
// 	    ( m_writingMode ==  K3b::WRITING_MODE_AUTO &&
// 	      ( sizeWithDao || !m_onTheFly ) ) ) {
	  emit infoMessage( i18n("Writing DVD-RW in DAO mode."), INFO );
	  d->usedWritingMode = K3b::DAO;
	}
	else {
	  emit infoMessage( i18n("Writing DVD-RW in incremental mode."), INFO );
	  d->usedWritingMode = K3b::WRITING_MODE_INCR_SEQ;
	}
      }
      else {

	// FIXME: DVD-R DL jump and stuff

	if( m_writingMode == K3b::WRITING_MODE_RES_OVWR )
	  emit infoMessage( i18n("Restricted Overwrite is not possible with DVD-R media."), INFO );

	if( m_writingMode == K3b::DAO ) {
// 	    ( m_writingMode ==  K3b::WRITING_MODE_AUTO &&
// 	      ( sizeWithDao || !m_onTheFly ) ) ) {
	  emit infoMessage( i18n("Writing %1 in DAO mode.").arg( K3bDevice::mediaTypeString(m, true) ), INFO );
	  d->usedWritingMode = K3b::DAO;
	}
	else {
	  emit infoMessage( i18n("Writing %1 in incremental mode.").arg( K3bDevice::mediaTypeString(m, true) ), INFO );
	  d->usedWritingMode = K3b::WRITING_MODE_INCR_SEQ;
	}
      }
    }
  }

  return true;
}



void K3bDvdCopyJob::removeImageFiles()
{
  if( TQFile::exists( m_imagePath ) ) {
    d->imageFile.remove();
    emit infoMessage( i18n("Removed image file %1").arg(m_imagePath), K3bJob::SUCCESS );
  }
}


TQString K3bDvdCopyJob::jobDescription() const
{
  if( m_onlyCreateImage ) {
    return i18n("Creating DVD Image");
  }
  else {
    if( m_onTheFly )
      return i18n("Copying DVD On-The-Fly");
    else
      return i18n("Copying DVD");
  }
}


TQString K3bDvdCopyJob::jobDetails() const
{
  return i18n("Creating 1 copy",
	      "Creating %n copies",
	      (m_simulate||m_onlyCreateImage) ? 1 : m_copies );
}


void K3bDvdCopyJob::setVerifyData( bool b )
{
  d->verifyData = b;
}

#include "k3bdvdcopyjob.moc"