summaryrefslogtreecommitdiffstats
path: root/libk3b/projects/k3bcdrecordwriter.cpp
blob: 1b0229010ffa9ae81b6ef97e9d4d317177d6aa8c (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
/*
 *
 * $Id: k3bcdrecordwriter.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 <config.h>


#include "k3bcdrecordwriter.h"

#include <k3bcore.h>
#include <k3bexternalbinmanager.h>
#include <k3bprocess.h>
#include <k3bdevice.h>
#include <k3bdevicemanager.h>
#include <k3bdevicehandler.h>
#include <k3bglobals.h>
#include <k3bthroughputestimator.h>
#include <k3bglobalsettings.h>
#include <k3btempfile.h>

#include <tqstring.h>
#include <tqstringlist.h>
#include <tqurl.h>
#include <tqvaluelist.h>
#include <tqregexp.h>
#include <tqfile.h>

#include <klocale.h>
#include <kdebug.h>
#include <kglobal.h>



class K3bCdrecordWriter::Private
{
public:
  Private()
    : cdTextFile(0) {
  }

  K3bThroughputEstimator* speedEst;
  bool canceled;
  bool usingBurnfree;
  int usedSpeed;

  struct Track {
    int size;
    bool audio;
  };

  TQValueList<Track> tracks;

  KTempFile* cdTextFile;
};


K3bCdrecordWriter::K3bCdrecordWriter( K3bDevice::Device* dev, K3bJobHandler* hdl,
				      TQObject* tqparent, const char* name )
  : K3bAbstractWriter( dev, hdl, tqparent, name ),
    m_clone(false),
    m_cue(false),
    m_forceNoEject(false)
{
  d = new Private();
  d->speedEst = new K3bThroughputEstimator( this );
  connect( d->speedEst, TQT_SIGNAL(throughput(int)),
	   this, TQT_SLOT(slotThroughput(int)) );

  m_process = 0;
  m_writingMode = K3b::TAO;
}


K3bCdrecordWriter::~K3bCdrecordWriter()
{
  delete d->cdTextFile;
  delete d;
  delete m_process;
}


bool K3bCdrecordWriter::active() const
{
  return ( m_process && m_process->isRunning() );
}


int K3bCdrecordWriter::fd() const
{
  if( m_process )
    return m_process->stdinFd();
  else
    return -1;
}


void K3bCdrecordWriter::setDao( bool b )
{
  m_writingMode = ( b ? K3b::DAO : K3b::TAO );
}

void K3bCdrecordWriter::setCueFile( const TQString& s)
{
  m_cue = true;
  m_cueFile = s;

  // cuefile only works in DAO mode
  setWritingMode( K3b::DAO );
}

void K3bCdrecordWriter::setClone( bool b )
{
  m_clone = b;
}


void K3bCdrecordWriter::setWritingMode( int mode )
{
  if( mode == K3b::DAO ||
      mode == K3b::TAO ||
      mode == K3b::RAW )
    m_writingMode = mode;
  else
    kdError() << "(K3bCdrecordWriter) wrong writing mode: " << mode << endl;
}


void K3bCdrecordWriter::prepareProcess()
{
  if( m_process ) delete m_process;  // kdelibs want this!
  m_process = new K3bProcess();
  m_process->setRunPrivileged(true);
  //  m_process->setPriority( KProcess::PrioHighest );
  m_process->setSplitStdout(true);
  m_process->setSuppressEmptyLines(true);
  m_process->setRawStdin(true);  // we only use stdin when writing on-the-fly
  connect( m_process, TQT_SIGNAL(stdoutLine(const TQString&)), this, TQT_SLOT(slotStdLine(const TQString&)) );
  connect( m_process, TQT_SIGNAL(stderrLine(const TQString&)), this, TQT_SLOT(slotStdLine(const TQString&)) );
  connect( m_process, TQT_SIGNAL(processExited(KProcess*)), this, TQT_SLOT(slotProcessExited(KProcess*)) );

  m_cdrecordBinObject = k3bcore->externalBinManager()->binObject("cdrecord");

  if( !m_cdrecordBinObject )
    return;

  *m_process << m_cdrecordBinObject;

  // display progress
  *m_process << "-v";

  if( m_cdrecordBinObject->hasFeature( "gracetime") )
    *m_process << "gracetime=2";  // 2 is the lowest allowed value (Joerg, why do you do this to us?)

  // Again we assume the device to be set!
  *m_process << TQString("dev=%1").tqarg(K3b::externalBinDeviceParameter(burnDevice(), m_cdrecordBinObject));

  d->usedSpeed = burnSpeed();
  if( d->usedSpeed == 0 ) {
    // try to determine the writeSpeed
    // if it fails determineMaximalWriteSpeed() will return 0 and
    // the choice is left to cdrecord
    d->usedSpeed = burnDevice()->determineMaximalWriteSpeed();
  }
  d->usedSpeed /= 175;
  if( d->usedSpeed != 0 )
    *m_process << TQString("speed=%1").tqarg(d->usedSpeed);

  if( m_writingMode == K3b::DAO || m_cue ) {
    if( burnDevice()->dao() )
      *m_process << "-dao";
    else {
      if( m_cdrecordBinObject->hasFeature( "tao" ) )
	*m_process << "-tao";
      emit infoMessage( i18n("Writer does not support disk at once (DAO) recording"), WARNING );
    }
  }
  else if( m_writingMode == K3b::RAW ) {
    if( burnDevice()->supportsWritingMode( K3bDevice::RAW_R96R ) )
      *m_process << "-raw96r";
    else if( burnDevice()->supportsWritingMode( K3bDevice::RAW_R16 ) )
      *m_process << "-raw16";
    else if( burnDevice()->supportsWritingMode( K3bDevice::RAW_R96P ) )
      *m_process << "-raw96p";
    else {
      emit infoMessage( i18n("Writer does not support raw writing."), WARNING );
      if( m_cdrecordBinObject->hasFeature( "tao" ) )
	*m_process << "-tao";
    }
  }
  else if( m_cdrecordBinObject->hasFeature( "tao" ) )
    *m_process << "-tao";

  if( simulate() )
    *m_process << "-dummy";

  d->usingBurnfree = false;
  if( k3bcore->globalSettings()->burnfree() ) {
    if( burnDevice()->burnproof() ) {

      d->usingBurnfree = true;

      // with cdrecord 1.11a02 burnproof was renamed to burnfree
      if( m_cdrecordBinObject->hasFeature( "burnproof" ) )
	*m_process << "driveropts=burnproof";
      else
	*m_process << "driveropts=burnfree";
    }
    else
      emit infoMessage( i18n("Writer does not support buffer underrun free recording (Burnfree)"), WARNING );
  }

  if( k3bcore->globalSettings()->force() ) {
    *m_process << "-force";
    emit infoMessage( i18n("'Force unsafe operations' enabled."), WARNING );
  }

  if( m_cue ) {
    m_process->setWorkingDirectory(TQUrl(m_cueFile).dirPath());
    *m_process << TQString("cuefile=%1").tqarg( m_cueFile );
  }

  if( m_clone )
    *m_process << "-clone";

  if( m_rawCdText.size() > 0 ) {
    delete d->cdTextFile;
    d->cdTextFile = new K3bTempFile( TQString(), ".dat" );
    d->cdTextFile->setAutoDelete(true);
    d->cdTextFile->file()->writeBlock( m_rawCdText );
    d->cdTextFile->close();

    *m_process << "textfile=" + d->cdTextFile->name();
  }

  if( k3bcore->globalSettings()->ejectMedia() &&
      !m_forceNoEject )
    *m_process << "-eject";

  bool manualBufferSize = k3bcore->globalSettings()->useManualBufferSize();
  if( manualBufferSize ) {
    *m_process << TQString("fs=%1m").tqarg( k3bcore->globalSettings()->bufferSize() );
  }

  bool overburn = k3bcore->globalSettings()->overburn();
  if( overburn )
    if( m_cdrecordBinObject->hasFeature("overburn") )
      *m_process << "-overburn";
    else
      emit infoMessage( i18n("Cdrecord %1 does not support overburning.").tqarg(m_cdrecordBinObject->version), WARNING );

  // additional user parameters from config
  const TQStringList& params = m_cdrecordBinObject->userParameters();
  for( TQStringList::const_iterator it = params.begin(); it != params.end(); ++it )
    *m_process << *it;

  // add the user parameters
  for( TQStringList::const_iterator it = m_arguments.begin(); it != m_arguments.end(); ++it )
    *m_process << *it;
}


K3bCdrecordWriter* K3bCdrecordWriter::addArgument( const TQString& arg )
{
  m_arguments.append( arg );
  return this;
}


void K3bCdrecordWriter::clearArguments()
{
  m_arguments.clear();
}


void K3bCdrecordWriter::start()
{
  jobStarted();

  d->canceled = false;
  d->speedEst->reset();

  prepareProcess();

  if( !m_cdrecordBinObject ) {
    emit infoMessage( i18n("Could not tqfind %1 executable.").tqarg("cdrecord"), ERROR );
    jobFinished(false);
    return;
  }

  emit debuggingOutput( "Used versions", "cdrecord: " + m_cdrecordBinObject->version );

  if( !m_cdrecordBinObject->copyright.isEmpty() )
    emit infoMessage( i18n("Using %1 %2 - Copyright (C) %3")
		      .tqarg(m_cdrecordBinObject->hasFeature( "wodim" ) ? "Wodim" : "Cdrecord" )
		      .tqarg(m_cdrecordBinObject->version)
		      .tqarg(m_cdrecordBinObject->copyright), INFO );


  kdDebug() << "***** " << m_cdrecordBinObject->name() << " parameters:\n";
  const TQValueList<TQCString>& args = m_process->args();
  TQString s;
  for( TQValueList<TQCString>::const_iterator it = args.begin(); it != args.end(); ++it ) {
    s += *it + " ";
  }
  kdDebug() << s << flush << endl;
  emit debuggingOutput( m_cdrecordBinObject->name() + " command:", s);

  m_currentTrack = 0;
  m_cdrecordError = UNKNOWN;
  m_totalTracksParsed = false;
  m_alreadyWritten = 0;
  d->tracks.clear();
  m_totalSize = 0;

  emit newSubTask( i18n("Preparing write process...") );

  // FIXME: check the return value
  if( K3b::isMounted( burnDevice() ) ) {
    emit infoMessage( i18n("Unmounting medium"), INFO );
    K3b::unmount( burnDevice() );
  }

  // block the device (including certain checks)
  k3bcore->blockDevice( burnDevice() );

  // lock the device for good in this process since it will
  // be opened in the growisofs process
  burnDevice()->close();
  burnDevice()->usageLock();

  if( !m_process->start( KProcess::NotifyOnExit, KProcess::All ) ) {
    // something went wrong when starting the program
    // it "should" be the executable
    kdDebug() << "(K3bCdrecordWriter) could not start " << m_cdrecordBinObject->name() << endl;
    emit infoMessage( i18n("Could not start %1.").tqarg(m_cdrecordBinObject->name()), K3bJob::ERROR );
    jobFinished(false);
  }
  else {
    if( simulate() ) {
      emit newTask( i18n("Simulating") );
      emit infoMessage( i18n("Starting %1 simulation at %2x speed...")
			.tqarg(K3b::writingModeString(m_writingMode))
			.tqarg(d->usedSpeed),
			K3bJob::INFO );
    }
    else {
      emit newTask( i18n("Writing") );
      emit infoMessage( i18n("Starting %1 writing at %2x speed...")
			.tqarg(K3b::writingModeString(m_writingMode))
			.tqarg(d->usedSpeed),
			K3bJob::INFO );
    }
  }
}


void K3bCdrecordWriter::cancel()
{
  if( active() ) {
    d->canceled = true;
    if( m_process && m_process->isRunning() )
      m_process->kill();
  }
}


void K3bCdrecordWriter::slotStdLine( const TQString& line )
{
  static TQRegExp s_burnfreeCounterRx( "^BURN\\-Free\\swas\\s(\\d+)\\stimes\\sused" );
  static TQRegExp s_burnfreeCounterRxPredict( "^Total\\sof\\s(\\d+)\\s\\spossible\\sbuffer\\sunderruns\\spredicted" );

  // tracknumber: cap(1)
  // done: cap(2)
  // complete: cap(3)
  // fifo: cap(4)  (it seems as if some patched cdrecord versions do not emit the fifo info but only the buf... :(
  // buffer: cap(5)
  static TQRegExp s_progressRx( "Track\\s(\\d\\d)\\:\\s*(\\d*)\\sof\\s*(\\d*)\\sMB\\swritten\\s(?:\\(fifo\\s*(\\d*)\\%\\)\\s*)?(?:\\[buf\\s*(\\d*)\\%\\])?.*" );

  emit debuggingOutput( m_cdrecordBinObject->name(), line );

  //
  // Progress and toc parsing
  //

  if( line.startsWith( "Track " ) ) {
    if( !m_totalTracksParsed ) {
      // this is not the progress display but the list of tracks that will get written
      // we always extract the tracknumber to get the highest at last
      bool ok;
      int tt = line.mid( 6, 2 ).toInt(&ok);

      if( ok ) {
	struct Private::Track track;
	track.audio  = ( line.mid( 10, 5 ) == "audio" );

	m_totalTracks = tt;

	int sizeStart = line.tqfind( TQRegExp("\\d"), 10 );
	int sizeEnd = line.tqfind( "MB", sizeStart );
	track.size = line.mid( sizeStart, sizeEnd-sizeStart ).toInt(&ok);

	if( ok ) {
	  d->tracks.append(track);
	  m_totalSize += track.size;
	}
	else
	  kdDebug() << "(K3bCdrecordWriter) track number parse error: "
		    << line.mid( sizeStart, sizeEnd-sizeStart ) << endl;
      }
      else
	kdDebug() << "(K3bCdrecordWriter) track number parse error: "
		  << line.mid( 6, 2 ) << endl;
    }

    else if( s_progressRx.exactMatch( line ) ) {
      //      int num = s_progressRx.cap(1).toInt();
      int made = s_progressRx.cap(2).toInt();
      int size = s_progressRx.cap(3).toInt();
      int fifo = s_progressRx.cap(4).toInt();

      emit buffer( fifo );
      m_lastFifoValue = fifo;

      if( s_progressRx.numCaptures() > 4 )
	emit deviceBuffer( s_progressRx.cap(5).toInt() );

      //
      // cdrecord's output sucks a bit.
      // we get track sizes that differ from the sizes in the progress
      // info since these are dependant on the writing mode.
      // so we just use the track sizes and do a bit of math...
      //

      if( d->tracks.count() > m_currentTrack-1 && size > 0 ) {
	double convV = (double)d->tracks[m_currentTrack-1].size/(double)size;
	made = (int)((double)made * convV);
	size = d->tracks[m_currentTrack-1].size;
      }
      else {
	kdError() << "(K3bCdrecordWriter) Did not parse all tracks sizes!" << endl;
      }

      if( size > 0 ) {
	emit processedSubSize( made, size );
	emit subPercent( 100*made/size );
      }

      if( m_totalSize > 0 ) {
	emit processedSize( m_alreadyWritten+made, m_totalSize );
	emit percent( 100*(m_alreadyWritten+made)/m_totalSize );
      }

      d->speedEst->dataWritten( (m_alreadyWritten+made)*1024 );
    }
  }

  //
  // Cdrecord starts all error and warning messages with it's path
  // With Debian's script it starts with cdrecord (or /usr/bin/cdrecord or whatever! I hate this script!)
  //

  else if( line.startsWith( "cdrecord" ) ||
	   line.startsWith( m_cdrecordBinObject->path ) ||
	   line.startsWith( m_cdrecordBinObject->path.left(m_cdrecordBinObject->path.length()-5) ) ) {
    // get rid of the path and the following colon and space
    TQString errStr = line.mid( line.tqfind(':') + 2 );

    if( errStr.startsWith( "Drive does not support SAO" ) ) {
      emit infoMessage( i18n("DAO (Disk At Once) recording not supported with this writer"), K3bJob::ERROR );
      emit infoMessage( i18n("Please choose TAO (Track At Once) and try again"), K3bJob::ERROR );
    }
    else if( errStr.startsWith( "Drive does not support RAW" ) ) {
      emit infoMessage( i18n("RAW recording not supported with this writer"), K3bJob::ERROR );
    }
    else if( errStr.startsWith("Input/output error.") ) {
      emit infoMessage( i18n("Input/output error. Not necessarily serious."), WARNING );
    }
    else if( errStr.startsWith("shmget failed") ) {
      m_cdrecordError = SHMGET_FAILED;
    }
    else if( errStr.startsWith("OPC failed") ) {
      m_cdrecordError = OPC_FAILED;
    }
    else if( errStr.startsWith( "Drive needs to reload the media" ) ) {
      emit infoMessage( i18n("Reloading of medium required"), K3bJob::INFO );
    }
    else if( errStr.startsWith( "The current problem looks like a buffer underrun" ) ) {
      if( m_cdrecordError == UNKNOWN ) // it is almost never a buffer underrun these days.
	m_cdrecordError = BUFFER_UNDERRUN;
    }
    else if( errStr.startsWith("WARNING: Data may not fit") ) {
      bool overburn = k3bcore->globalSettings()->overburn();
      if( overburn && m_cdrecordBinObject->hasFeature("overburn") )
	emit infoMessage( i18n("Trying to write more than the official disk capacity"), K3bJob::WARNING );
      m_cdrecordError = OVERSIZE;
    }
    else if( errStr.startsWith("Bad Option") ) {
      m_cdrecordError = BAD_OPTION;
      // parse option
      int pos = line.tqfind( "Bad Option" ) + 13;
      int len = line.length() - pos - 1;
      emit infoMessage( i18n("No valid %1 option: %2").tqarg(m_cdrecordBinObject->name()).tqarg(line.mid(pos, len)),
			ERROR );
    }
    else if( errStr.startsWith("Cannot set speed/dummy") ) {
      m_cdrecordError = CANNOT_SET_SPEED;
    }
    else if( errStr.startsWith("Cannot open new session") ) {
      m_cdrecordError = CANNOT_OPEN_NEW_SESSION;
    }
    else if( errStr.startsWith("Cannot send CUE sheet") ) {
      m_cdrecordError = CANNOT_SEND_CUE_SHEET;
    }
    else if( errStr.startsWith( "Trying to use ultra high speed" ) ||
	     errStr.startsWith( "Trying to use high speed" ) ||
	     errStr.startsWith( "Probably trying to use ultra high speed" ) ||
	     errStr.startsWith( "You did use a high speed medium on an improper writer" ) ||
	     errStr.startsWith( "You did use a ultra high speed medium on an improper writer" ) ) {
      m_cdrecordError = HIGH_SPEED_MEDIUM;
    }
    else if( errStr.startsWith( "You may have used an ultra low speed medium" ) ) {
      m_cdrecordError = LOW_SPEED_MEDIUM;
    }
    else if( errStr.startsWith( "Permission denied. Cannot open" ) ||
             errStr.startsWith( "Operation not permitted." ) ) {
      m_cdrecordError = PERMISSION_DENIED;
    }
    else if( errStr.startsWith( "Can only copy session # 1") ) {
      emit infoMessage( i18n("Only session 1 will be cloned."), WARNING );
    }
    else if( errStr == "Cannot fixate disk." ) {
      emit infoMessage( i18n("Unable to fixate the disk."), ERROR );
      if( m_cdrecordError == UNKNOWN )
	m_cdrecordError = CANNOT_FIXATE_DISK;
    }
    else if( errStr == "A write error occurred." ) {
      m_cdrecordError = WRITE_ERROR;
    }
    else if( errStr.startsWith( "Try again with cdrecord blank=all." ) ) {
      m_cdrecordError = BLANK_FAILED;
    }
  }

  //
  // All other messages
  //

  else if( line.tqcontains( "at speed" ) ) {
    // parse the speed and inform the user if cdrdao switched it down
    int pos = line.tqfind( "at speed" );
    int pos2 = line.tqfind( "in", pos+9 );
    int speed = static_cast<int>( line.mid( pos+9, pos2-pos-10 ).toDouble() );  // cdrecord-dvd >= 2.01a25 uses 8.0 and stuff
    if( speed != d->usedSpeed ) {
      emit infoMessage( i18n("Medium or burner do not support writing at %1x speed").tqarg(d->usedSpeed), K3bJob::WARNING );
      if( speed > d->usedSpeed )
	emit infoMessage( i18n("Switching burn speed up to %1x").tqarg(speed), K3bJob::WARNING );
      else
	emit infoMessage( i18n("Switching burn speed down to %1x").tqarg(speed), K3bJob::WARNING );
    }
  }
  else if( line.startsWith( "Starting new" ) ) {
    m_totalTracksParsed = true;
    if( m_currentTrack > 0 ) {// nothing has been written at the start of track 1
      if( d->tracks.count() > m_currentTrack-1 )
	m_alreadyWritten += d->tracks[m_currentTrack-1].size;
      else
	kdError() << "(K3bCdrecordWriter) Did not parse all tracks sizes!" << endl;
    }
    else
      emit infoMessage( i18n("Starting disc write"), INFO );

    m_currentTrack++;

    if( m_currentTrack > d->tracks.count() ) {
      kdDebug() << "(K3bCdrecordWriter) need to add dummy track struct." << endl;
      struct Private::Track t;
      t.size = 1;
      t.audio = false;
      d->tracks.append(t);
    }

    kdDebug() << "(K3bCdrecordWriter) writing track " << m_currentTrack << " of " << m_totalTracks << " tracks." << endl;
    emit nextTrack( m_currentTrack, m_totalTracks );
  }
  else if( line.startsWith( "Fixating" ) ) {
    emit newSubTask( i18n("Closing Session") );
  }
  else if( line.startsWith( "Writing lead-in" ) ) {
    m_totalTracksParsed = true;
    emit newSubTask( i18n("Writing Leadin") );
  }
  else if( line.startsWith( "Writing Leadout") ) {
    emit newSubTask( i18n("Writing Leadout") );
  }
  else if( line.startsWith( "Writing pregap" ) ) {
    emit newSubTask( i18n("Writing pregap") );
  }
  else if( line.startsWith( "Performing OPC" ) ) {
    emit infoMessage( i18n("Performing Optimum Power Calibration"), K3bJob::INFO );
  }
  else if( line.startsWith( "Sending" ) ) {
    emit infoMessage( i18n("Sending CUE sheet"), K3bJob::INFO );
  }
  else if( line.startsWith( "Turning BURN-Free on" ) || line.startsWith( "BURN-Free is ON") ) {
    emit infoMessage( i18n("Enabled Burnfree"), K3bJob::INFO );
  }
  else if( line.startsWith( "Turning BURN-Free off" ) ) {
    emit infoMessage( i18n("Disabled Burnfree"), K3bJob::WARNING );
  }
  else if( line.startsWith( "Re-load disk and hit" ) ) {
    // this happens on some notebooks where cdrecord is not able to close the
    // tray itself, so we need to ask the user to do so
    blockingInformation( i18n("Please reload the medium and press 'ok'"),
			 i18n("Unable to close the tray") );

    // now send a <CR> to cdrecord
    // hopefully this will do it since I have no possibility to test it!
    ::write( fd(), "\n", 1 );
  }
  else if( s_burnfreeCounterRx.search( line ) ) {
    bool ok;
    int num = s_burnfreeCounterRx.cap(1).toInt(&ok);
    if( ok )
      emit infoMessage( i18n("Burnfree was used 1 time.", "Burnfree was used %n times.", num), INFO );
  }
  else if( s_burnfreeCounterRxPredict.search( line ) ) {
    bool ok;
    int num = s_burnfreeCounterRxPredict.cap(1).toInt(&ok);
    if( ok )
      emit infoMessage( i18n("Buffer was low 1 time.", "Buffer was low %n times.", num), INFO );
  }
  else if( line.tqcontains("Medium Error") ) {
    m_cdrecordError = MEDIUM_ERROR;
  }
  else if( line.startsWith( "Error trying to open" ) && line.tqcontains( "(Device or resource busy)" ) ) {
    m_cdrecordError = DEVICE_BUSY;
  }
  else {
    // debugging
    kdDebug() << "(" << m_cdrecordBinObject->name() << ") " << line << endl;
  }
}


void K3bCdrecordWriter::slotProcessExited( KProcess* p )
{
  // remove temporary cdtext file
  delete d->cdTextFile;
  d->cdTextFile = 0;

  // release the device within this process
  burnDevice()->usageUnlock();

  // unblock the device
  k3bcore->unblockDevice( burnDevice() );

  if( d->canceled ) {
    // this will unblock and eject the drive and emit the finished/canceled signals
    K3bAbstractWriter::cancel();
    return;
  }


  if( p->normalExit() ) {
    switch( p->exitStatus() ) {
    case 0:
      {
	if( simulate() )
	  emit infoMessage( i18n("Simulation successfully completed"), K3bJob::SUCCESS );
	else
	  emit infoMessage( i18n("Writing successfully completed"), K3bJob::SUCCESS );

	int s = d->speedEst->average();
	emit infoMessage( i18n("Average overall write speed: %1 KB/s (%2x)").tqarg(s).tqarg(KGlobal::locale()->formatNumber((double)s/150.0), 2), INFO );

	jobFinished( true );
      }
      break;

    default:
      kdDebug() << "(K3bCdrecordWriter) error: " << p->exitStatus() << endl;

      if( m_cdrecordError == UNKNOWN && m_lastFifoValue <= 3 )
	m_cdrecordError = BUFFER_UNDERRUN;

      switch( m_cdrecordError ) {
      case OVERSIZE:
	if( k3bcore->globalSettings()->overburn() &&
	    m_cdrecordBinObject->hasFeature("overburn") )
	  emit infoMessage( i18n("Data did not fit on disk."), ERROR );
	else {
	  emit infoMessage( i18n("Data does not fit on disk."), ERROR );
	  if( m_cdrecordBinObject->hasFeature("overburn") )
	    emit infoMessage( i18n("Enable overburning in the advanced K3b settings to burn anyway."), INFO );
	}
	break;
      case BAD_OPTION:
	// error message has already been emited earlier since we needed the actual line
	break;
      case SHMGET_FAILED:
	emit infoMessage( i18n("%1 could not reserve shared memory segment of requested size.").tqarg(m_cdrecordBinObject->name()), ERROR );
	emit infoMessage( i18n("Probably you chose a too large buffer size."), ERROR );
	break;
      case OPC_FAILED:
	emit infoMessage( i18n("OPC failed. Probably the writer does not like the medium."), ERROR );
	break;
      case CANNOT_SET_SPEED:
	emit infoMessage( i18n("Unable to set write speed to %1.").tqarg(d->usedSpeed), ERROR );
	emit infoMessage( i18n("Probably this is lower than your writer's lowest writing speed."), ERROR );
	break;
      case CANNOT_SEND_CUE_SHEET:
	emit infoMessage( i18n("Unable to send CUE sheet."), ERROR );
	if( m_writingMode == K3b::DAO )
	  emit infoMessage( i18n("Sometimes using TAO writing mode solves this issue."), ERROR );
	break;
      case CANNOT_OPEN_NEW_SESSION:
	emit infoMessage( i18n("Unable to open new session."), ERROR );
	emit infoMessage( i18n("Probably a problem with the medium."), ERROR );
	break;
      case CANNOT_FIXATE_DISK:
	emit infoMessage( i18n("The disk might still be readable."), ERROR );
	if( m_writingMode == K3b::TAO && burnDevice()->dao() )
	  emit infoMessage( i18n("Try DAO writing mode."), ERROR );
	break;
      case PERMISSION_DENIED:
	emit infoMessage( i18n("%1 has no permission to open the device.").tqarg("Cdrecord"), ERROR );
#ifdef HAVE_K3BSETUP
	emit infoMessage( i18n("You may use K3bsetup2 to solve this problem."), ERROR );
#endif
	break;
      case BUFFER_UNDERRUN:
	  emit infoMessage( i18n("Probably a buffer underrun occurred."), ERROR );
	  if( !d->usingBurnfree && burnDevice()->burnproof() )
	    emit infoMessage( i18n("Please enable Burnfree or choose a lower burning speed."), ERROR );
	  else
	    emit infoMessage( i18n("Please choose a lower burning speed."), ERROR );
	break;
      case HIGH_SPEED_MEDIUM:
	emit infoMessage( i18n("Found a high-speed medium not suitable for the writer being used."), ERROR );
	emit infoMessage( i18n("Use the 'force unsafe operations' option to ignore this."), ERROR );
	break;
      case LOW_SPEED_MEDIUM:
	emit infoMessage( i18n("Found a low-speed medium not suitable for the writer being used."), ERROR );
	emit infoMessage( i18n("Use the 'force unsafe operations' option to ignore this."), ERROR );
	break;
      case MEDIUM_ERROR:
	emit infoMessage( i18n("Most likely the burning failed due to low-quality media."), ERROR );
	break;
      case DEVICE_BUSY:
	emit infoMessage( i18n("Another application is blocking the device (most likely automounting)."), ERROR );
	break;
      case WRITE_ERROR:
	emit infoMessage( i18n("A write error occurred."), ERROR );
	if( m_writingMode == K3b::DAO )
	  emit infoMessage( i18n("Sometimes using TAO writing mode solves this issue."), ERROR );
	break;
      case BLANK_FAILED:
	emit infoMessage( i18n("Some drives do not support all erase types."), ERROR );
	emit infoMessage( i18n("Try again using 'Complete' erasing."), ERROR );
	break;
      case UNKNOWN:
	if( p->exitStatus() == 12 && K3b::kernelVersion() >= K3bVersion( 2, 6, 8 ) && m_cdrecordBinObject->hasFeature( "suidroot" ) ) {
	  emit infoMessage( i18n("Since kernel version 2.6.8 cdrecord cannot use SCSI transport when running suid root anymore."), ERROR );
	  emit infoMessage( i18n("You may use K3bSetup to solve this problem or remove the suid bit manually."), ERROR );
	}
	else if( !wasSourceUnreadable() ) {
	  emit infoMessage( i18n("%1 returned an unknown error (code %2).")
			    .tqarg(m_cdrecordBinObject->name()).tqarg(p->exitStatus()),
			    K3bJob::ERROR );

	  if( p->exitStatus() >= 254 && m_writingMode == K3b::DAO ) {
	    emit infoMessage( i18n("Sometimes using TAO writing mode solves this issue."), ERROR );
	  }
	  else {
	    emit infoMessage( i18n("If you are running an unpatched cdrecord version..."), ERROR );
	    emit infoMessage( i18n("...and this error also occurs with high quality media..."), ERROR );
	    emit infoMessage( i18n("...and the K3b FAQ does not help you..."), ERROR );
	    emit infoMessage( i18n("...please include the debugging output in your problem report."), ERROR );
	  }
	}
	break;
      }
      jobFinished( false );
    }
  }
  else {
    emit infoMessage( i18n("%1 did not exit cleanly.").tqarg(m_cdrecordBinObject->name()),
		      ERROR );
    jobFinished( false );
  }
}


void K3bCdrecordWriter::slotThroughput( int t )
{
  emit writeSpeed( t, d->tracks[m_currentTrack-1].audio ? 175 : 150 );
}

#include "k3bcdrecordwriter.moc"