summaryrefslogtreecommitdiffstats
path: root/src/k3bemptydiscwaiter.cpp
blob: f763f722440c90c106ba0e7c623dcff1b8528e4e (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
/*
 *
 * $Id: k3bemptydiscwaiter.cpp 642801 2007-03-15 12:39:04Z trueg $
 * Copyright (C) 2003-2007 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 "k3bemptydiscwaiter.h"
#include "k3bmediacache.h"
#include "k3bapplication.h"
#include <k3bdevice.h>
#include <k3bdeviceglobals.h>
#include <k3bdevicehandler.h>
#include <k3bglobals.h>
#include <k3bcore.h>
#include <k3biso9660.h>
#include "k3bblankingjob.h"
#include <k3bbusywidget.h>
#include <k3bprogressdialog.h>
#include <k3bdvdformattingjob.h>

#include <tqtimer.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqtooltip.h>
#include <tqpushbutton.h>
#include <tqapplication.h>
#include <tqeventloop.h>
#include <tqfont.h>

#include <tdelocale.h>
#include <tdeconfig.h>
#include <kiconloader.h>
#include <tdemessagebox.h>
#include <kactivelabel.h>
#include <knotifyclient.h>


class K3bEmptyDiscWaiter::Private
{
public:
  Private()
    : erasingInfoDialog(0) {
    dialogVisible = false;
    inLoop = false;
    mediumChanged = 0;
    blockMediaChange = false;
  }

  K3bDevice::Device* device;

  int wantedMediaType;
  int wantedMediaState;

  TQString wantedMediaTypeString;

  int result;
  int dialogVisible;
  bool inLoop;

  bool blockMediaChange;
  int mediumChanged;

  bool forced;
  bool canceled;

  bool waitingDone;

  TQLabel* labelRequest;
  TQLabel* labelFoundMedia;
  TQLabel* pixLabel;

  K3bProgressDialog* erasingInfoDialog;
};



K3bEmptyDiscWaiter::K3bEmptyDiscWaiter( K3bDevice::Device* device, TQWidget* parent, const char* name )
  : KDialogBase( KDialogBase::Plain, i18n("Waiting for Disk"),
		 KDialogBase::Cancel|KDialogBase::User1|KDialogBase::User2|KDialogBase::User3,
		 KDialogBase::User3, parent, name, true, true, i18n("Force"), i18n("Eject"), i18n("Load") )
{
  d = new Private();
  d->device = device;

  // setup the gui
  // -----------------------------
  d->labelRequest = new TQLabel( plainPage() );
  d->labelRequest->setAlignment( TQt::AlignLeft | TQt::AlignVCenter );
  d->labelFoundMedia = new TQLabel( plainPage() );
  d->pixLabel = new TQLabel( plainPage() );
  d->pixLabel->setAlignment( TQt::AlignHCenter | TQt::AlignTop );

  TQFont f( d->labelFoundMedia->font() );
  f.setBold(true);
  d->labelFoundMedia->setFont( f );

  TQGridLayout* grid = new TQGridLayout( plainPage() );
  grid->setMargin( marginHint() );
  grid->setSpacing( spacingHint() );

  grid->addMultiCellWidget( d->pixLabel, 0, 2, 0, 0 );
  grid->addColSpacing( 1, 20 );
  grid->addWidget( new TQLabel( i18n("Found media:"), plainPage() ), 0, 2 );
  grid->addWidget( d->labelFoundMedia, 0, 3 );
  grid->addMultiCellWidget( d->labelRequest, 1, 1, 2, 3 );
  grid->setRowStretch( 2, 1 );
  grid->setColStretch( 3, 1 );
  // -----------------------------

  connect( k3bappcore->mediaCache(), TQT_SIGNAL(mediumChanged(K3bDevice::Device*)),
	   this, TQT_SLOT(slotMediumChanged(K3bDevice::Device*)) );

  TQToolTip::add( actionButton(KDialogBase::User1),
		 i18n("Force K3b to continue if it seems not to detect your empty CD/DVD.") );
}


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


int K3bEmptyDiscWaiter::waitForDisc( int mediaState, int mediaType, const TQString& message )
{
  if ( d->inLoop ) {
    kdError() << "(K3bEmptyDiscWaiter) Recursive call detected." << endl;
    return -1;
  }

  d->wantedMediaState = mediaState;
  d->wantedMediaType = mediaType;
  d->dialogVisible = false;
  d->forced = false;
  d->canceled = false;
  d->waitingDone = false;
  d->blockMediaChange = false;
  d->mediumChanged = 0;

  //
  // We do not cover every case here but just the ones that really make sense
  //
  if( (d->wantedMediaType & K3bDevice::MEDIA_WRITABLE_DVD) &&
	   (d->wantedMediaType & K3bDevice::MEDIA_WRITABLE_CD) )
    d->wantedMediaTypeString = i18n("CD-R(W) or DVD%1R(W)").arg("�");
  else if( d->wantedMediaType & K3bDevice::MEDIA_WRITABLE_DVD_SL )
    d->wantedMediaTypeString = i18n("DVD%1R(W)").arg("�");
  else if( d->wantedMediaType & K3bDevice::MEDIA_WRITABLE_DVD_DL )
    d->wantedMediaTypeString = i18n("Double Layer DVD%1R").arg("�");
  else
    d->wantedMediaTypeString = i18n("CD-R(W)");

  if( message.isEmpty() ) {
    if( (d->wantedMediaState & K3bDevice::STATE_COMPLETE) && (d->wantedMediaState & K3bDevice::STATE_INCOMPLETE) )
      d->labelRequest->setText( i18n("Please insert a complete or appendable %4 medium "
				     "into drive<p><b>%1 %2 (%3)</b>.")
				.arg(d->device->vendor())
				.arg(d->device->description())
				.arg(d->device->devicename())
				.arg( d->wantedMediaTypeString ) );
    else if( d->wantedMediaState & K3bDevice::STATE_COMPLETE )
      d->labelRequest->setText( i18n("Please insert a complete %4 medium "
				     "into drive<p><b>%1 %2 (%3)</b>.")
				.arg(d->device->vendor())
				.arg(d->device->description())
				.arg(d->device->devicename())
				.arg( d->wantedMediaTypeString ) );
    else if( (d->wantedMediaState & K3bDevice::STATE_INCOMPLETE) && (d->wantedMediaState & K3bDevice::STATE_EMPTY) )
      d->labelRequest->setText( i18n("Please insert an empty or appendable %4 medium "
				     "into drive<p><b>%1 %2 (%3)</b>.")
				.arg(d->device->vendor())
				.arg(d->device->description())
				.arg(d->device->devicename())
				.arg( d->wantedMediaTypeString ) );
    else if( d->wantedMediaState & K3bDevice::STATE_INCOMPLETE )
      d->labelRequest->setText( i18n("Please insert an appendable %4 medium "
				     "into drive<p><b>%1 %2 (%3)</b>.")
				.arg(d->device->vendor())
				.arg(d->device->description())
				.arg(d->device->devicename())
				.arg( d->wantedMediaTypeString ) );
    else if( d->wantedMediaState & K3bDevice::STATE_EMPTY )
      d->labelRequest->setText( i18n("Please insert an empty %4 medium "
				     "into drive<p><b>%1 %2 (%3)</b>.")
				.arg(d->device->vendor())
				.arg(d->device->description())
				.arg(d->device->devicename())
				.arg( d->wantedMediaTypeString ) );
    else // fallback case (this should not happen in K3b)
      d->labelRequest->setText( i18n("Please insert a suitable medium "
				     "into drive<p><b>%1 %2 (%3)</b>.")
				.arg(d->device->vendor())
				.arg(d->device->description())
				.arg(d->device->devicename()) );

  }
  else
    d->labelRequest->setText( message );

  if( d->wantedMediaType & K3bDevice::MEDIA_WRITABLE_DVD )
    d->pixLabel->setPixmap( TDEGlobal::instance()->iconLoader()->loadIcon( "dvd_unmount",
									 TDEIcon::NoGroup, TDEIcon::SizeMedium ) );
  else
    d->pixLabel->setPixmap( TDEGlobal::instance()->iconLoader()->loadIcon( "cdwriter_unmount",
									 TDEIcon::NoGroup, TDEIcon::SizeMedium ) );

  adjustSize();

  slotMediumChanged( d->device );

  //
  // in case we already found a medium and thus the dialog is not shown entering
  // the loop only causes problems (since there is no dialog yet the user could
  // not have forced or canceled yet
  //
  if( !d->waitingDone ) {
    d->inLoop = true;
    TQApplication::eventLoop()->enterLoop();
  }

  return d->result;
}


int K3bEmptyDiscWaiter::exec()
{
  return waitForDisc();
}


void K3bEmptyDiscWaiter::slotMediumChanged( K3bDevice::Device* dev )
{
  kdDebug() << "(K3bEmptyDiscWaiter) slotMediumChanged() " << endl;
  if( d->forced || d->canceled || d->device != dev )
    return;

  //
  // This slot may open dialogs which enter a new event loop and that
  // may result in another call to this slot if a medium changes while
  // a dialog is open
  //
  if( d->blockMediaChange ) {
    d->mediumChanged++;
    return;
  }

  d->blockMediaChange = true;

  TDEConfig* c = k3bcore->config();
  bool formatWithoutAsking = TDEConfigGroup( k3bcore->config(), "General Options" ).readBoolEntry( "auto rewritable erasing", false );

  K3bMedium medium = k3bappcore->mediaCache()->medium( dev );

  d->labelFoundMedia->setText( medium.shortString( false ) );

  if( medium.diskInfo().diskState() == K3bDevice::STATE_NO_MEDIA ) {
    continueWaiting();
    d->blockMediaChange = false;
    return;
  }

//   TQString mediaState;
//   if( medium.diskInfo().diskState() == K3bDevice::STATE_COMPLETE )
//     mediaState = i18n("complete");
//   else if( medium.diskInfo().diskState() == K3bDevice::STATE_INCOMPLETE )
//     mediaState = i18n("appendable");
//   else if( medium.diskInfo().diskState() == K3bDevice::STATE_EMPTY )
//     mediaState = i18n("empty");

//   if( !mediaState.isEmpty() )
//     mediaState = " (" + mediaState +")";


  // /////////////////////////////////////////////////////////////
  //
  // DVD+RW handling
  //
  // /////////////////////////////////////////////////////////////

  // DVD+RW: if empty we need to preformat. Although growisofs does it before writing doing it here
  //         allows better control and a progress bar. If it's not empty we should check if there is
  //         already a filesystem on the media.
  if( (d->wantedMediaType & K3bDevice::MEDIA_DVD_PLUS_RW) &&
      (medium.diskInfo().mediaType() & K3bDevice::MEDIA_DVD_PLUS_RW) ) {

    kdDebug() << "(K3bEmptyDiscWaiter) ------ found DVD+RW as wanted." << endl;

    if( medium.diskInfo().diskState() == K3bDevice::STATE_EMPTY ) {
      if( d->wantedMediaState & K3bDevice::STATE_EMPTY ) {
	// special case for the formatting job which wants to preformat on it's own!
	if( d->wantedMediaState & K3bDevice::STATE_COMPLETE &&
	    d->wantedMediaState & K3bDevice::STATE_EMPTY ) {
	  kdDebug() << "(K3bEmptyDiscWaiter) special case: DVD+RW for the formatting job." << endl;
	  finishWaiting( K3bDevice::MEDIA_DVD_PLUS_RW );
	}
	else {
	  // empty - preformat without asking
	  prepareErasingDialog();

	  K3bDvdFormattingJob job( this );
	  job.setDevice( d->device );
	  job.setQuickFormat( true );
	  job.setForce( false );
	  job.setForceNoEject( true );

	  d->erasingInfoDialog->setText( i18n("Preformatting DVD+RW") );
	  connect( &job, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotErasingFinished(bool)) );
	  connect( &job, TQT_SIGNAL(percent(int)), d->erasingInfoDialog, TQT_SLOT(setProgress(int)) );
	  connect( d->erasingInfoDialog, TQT_SIGNAL(cancelClicked()), &job, TQT_SLOT(cancel()) );
	  job.start( medium.diskInfo() );
	  d->erasingInfoDialog->exec( true );
	}
      }
      else {
	kdDebug() << "(K3bEmptyDiscWaiter) starting devicehandler: empty DVD+RW where a non-empty was requested." << endl;
	continueWaiting();
      }
    }
    else {
      //
      // We have a DVD+RW medium which is already preformatted
      //
      if( d->wantedMediaState == K3bDevice::STATE_EMPTY ) {
	// check if the media contains a filesystem
	K3bIso9660 isoF( d->device );
	bool hasIso = isoF.open();

	if( formatWithoutAsking ||
	    !hasIso ||
	    KMessageBox::warningContinueCancel( parentWidgetToUse(),
						i18n("Found %1 media in %2 - %3. "
						     "Should it be overwritten?")
						.arg("DVD+RW")
						.arg(d->device->vendor())
						.arg(d->device->description()),
						i18n("Found %1").arg("DVD+RW"),i18n("Overwrite") ) == KMessageBox::Continue ) {
	  finishWaiting( K3bDevice::MEDIA_DVD_PLUS_RW );
	}
	else {
	  kdDebug() << "(K3bEmptyDiscWaiter) starting devicehandler: no DVD+RW overwrite" << endl;
	  K3b::unmount( d->device );
	  K3bDevice::eject( d->device );
	  continueWaiting();
	}
      }

      //
      // We want a DVD+RW not nessessarily empty. No problem, just use this one. Becasue incomplete and complete
      // are handled the same everywhere (isofs is grown).
      //
      else {
	finishWaiting( K3bDevice::MEDIA_DVD_PLUS_RW );
      }
    }
  } // --- DVD+RW --------


    // /////////////////////////////////////////////////////////////
    //
    // DVD-RW handling
    //
    // /////////////////////////////////////////////////////////////

    //
    // DVD-RW in sequential mode can be empty. DVD-RW in restricted overwrite mode is always complete.
    //
  else if( (d->wantedMediaType & (K3bDevice::MEDIA_DVD_RW|
				  K3bDevice::MEDIA_DVD_RW_SEQ|
				  K3bDevice::MEDIA_DVD_RW_OVWR) ) &&
	   (medium.diskInfo().mediaType() & (K3bDevice::MEDIA_DVD_RW|
					     K3bDevice::MEDIA_DVD_RW_SEQ|
					     K3bDevice::MEDIA_DVD_RW_OVWR) ) ) {

    kdDebug() << "(K3bEmptyDiscWaiter) ------ found DVD-R(W) as wanted." << endl;

    // we format in the following cases:
    // seq. incr. and not empty and empty requested
    // seq. incr. and restr. overwri. reqested
    // restr. ovw. and seq. incr. requested

    // we have exactly what was requested
    if( (d->wantedMediaType & medium.diskInfo().mediaType()) &&
	(d->wantedMediaState & medium.diskInfo().diskState()) ) {
      finishWaiting( medium.diskInfo().mediaType() );
    }

    // DVD-RW in restr. overwrite may just be overwritten
    else if( (medium.diskInfo().mediaType() & K3bDevice::MEDIA_DVD_RW_OVWR) &&
	     (d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_OVWR) ) {
      if( d->wantedMediaState == K3bDevice::STATE_EMPTY ) {

	kdDebug() << "(K3bEmptyDiscWaiter) ------ DVD-RW restricted overwrite." << endl;

	// check if the media contains a filesystem
	K3bIso9660 isoF( d->device );
	bool hasIso = isoF.open();

	if( formatWithoutAsking ||
	    !hasIso ||
	    KMessageBox::warningContinueCancel( parentWidgetToUse(),
						i18n("Found %1 media in %2 - %3. "
						     "Should it be overwritten?")
						.arg(K3bDevice::mediaTypeString(medium.diskInfo().mediaType()))
						.arg(d->device->vendor())
						.arg(d->device->description()),
						i18n("Found %1").arg("DVD-RW"),i18n("Overwrite") ) == KMessageBox::Continue ) {
	  finishWaiting( K3bDevice::MEDIA_DVD_RW_OVWR );
	}
	else {
	  kdDebug() << "(K3bEmptyDiscWaiter) starting devicehandler: no DVD-RW overwrite." << endl;
	  K3b::unmount( d->device );
	  K3bDevice::eject( d->device );
	  continueWaiting();
	}
      }

      else if( !(d->wantedMediaState & K3bDevice::STATE_EMPTY ) ) {
	// check if the media contains a filesystem
	K3bIso9660 isoF( d->device );
	bool hasIso = isoF.open();

	if( hasIso ) {
	  finishWaiting( K3bDevice::MEDIA_DVD_RW_OVWR );
	}
	else {
	  kdDebug() << "(K3bEmptyDiscWaiter) starting devicehandler: empty DVD-RW where a non-empty was requested." << endl;
	  continueWaiting();
	}
      }

      //
      // We want a DVD-RW overwrite not nessessarily empty. No problem, just use this one. Becasue incomplete and complete
      // are handled the same everywhere (isofs is grown).
      //
      else {
	finishWaiting( K3bDevice::MEDIA_DVD_RW_OVWR );
      }
    }

    // formatting
    else if( ( (d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_OVWR) &&
	       (medium.diskInfo().mediaType() & K3bDevice::MEDIA_DVD_RW_SEQ) &&
	       !(d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_SEQ) ) ||

	     ( (d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_SEQ) &&
	       (medium.diskInfo().mediaType() & K3bDevice::MEDIA_DVD_RW_OVWR) &&
	       !(d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_OVWR) ) ||

	     ( (d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_SEQ) &&
	       (medium.diskInfo().mediaType() & K3bDevice::MEDIA_DVD_RW_SEQ) &&
	       (d->wantedMediaState & K3bDevice::STATE_EMPTY) &&
	       (medium.diskInfo().diskState() != K3bDevice::STATE_EMPTY) ) ) {

      kdDebug() << "(K3bEmptyDiscWaiter) ------ DVD-RW needs to be formated." << endl;

      if( formatWithoutAsking ||
	  KMessageBox::warningContinueCancel( parentWidgetToUse(),
					      i18n("Found %1 media in %2 - %3. "
						   "Should it be formatted?")
					      .arg( K3bDevice::mediaTypeString(medium.diskInfo().mediaType()) )
					      .arg(d->device->vendor())
					      .arg(d->device->description()),
					      i18n("Found %1").arg("DVD-RW"), i18n("Format") ) == KMessageBox::Continue ) {

	kdDebug() << "(K3bEmptyDiscWaiter) ------ formatting DVD-RW." << endl;

	prepareErasingDialog();

	K3bDvdFormattingJob job( this );
	job.setDevice( d->device );
	// we prefere the current mode of the media if no special mode has been requested
	job.setMode( ( (d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_SEQ) &&
		       (d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_OVWR) )
		     ? ( medium.diskInfo().mediaType() == K3bDevice::MEDIA_DVD_RW_OVWR
			 ? K3b::WRITING_MODE_RES_OVWR
			 : K3b::WRITING_MODE_INCR_SEQ )
		     : ( (d->wantedMediaType & K3bDevice::MEDIA_DVD_RW_SEQ)
			 ? K3b::WRITING_MODE_INCR_SEQ
			 : K3b::WRITING_MODE_RES_OVWR ) );
	job.setQuickFormat( true );
	job.setForce( false );
	job.setForceNoEject(true);

	d->erasingInfoDialog->setText( i18n("Formatting DVD-RW") );
	connect( &job, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotErasingFinished(bool)) );
	connect( &job, TQT_SIGNAL(percent(int)), d->erasingInfoDialog, TQT_SLOT(setProgress(int)) );
	connect( d->erasingInfoDialog, TQT_SIGNAL(cancelClicked()), &job, TQT_SLOT(cancel()) );
	job.start( medium.diskInfo() );
	d->erasingInfoDialog->exec( true );
      }
      else {
	kdDebug() << "(K3bEmptyDiscWaiter) starting devicehandler: no DVD-RW formatting." << endl;
	K3b::unmount( d->device );
	K3bDevice::eject( d->device );
	continueWaiting();
      }
    }
    else {
      kdDebug() << "(K3bEmptyDiscWaiter) ------ nothing useful found." << endl;
      continueWaiting();
    }
  } // --- DVD-RW ------


    // /////////////////////////////////////////////////////////////
    //
    // CD handling (and DVD-R and DVD+R)
    //
    // /////////////////////////////////////////////////////////////

    // we have exactly what was requested
  else if( (d->wantedMediaType & medium.diskInfo().mediaType()) &&
	   (d->wantedMediaState & medium.diskInfo().diskState()) )
    finishWaiting( medium.diskInfo().mediaType() );

  else if( (medium.diskInfo().mediaType() != K3bDevice::MEDIA_UNKNOWN) &&
	   (d->wantedMediaType & medium.diskInfo().mediaType()) &&
	   (d->wantedMediaState & medium.diskInfo().diskState()) )
    finishWaiting( medium.diskInfo().mediaType() );

  // this is for CD drives that are not able to determine the state of a disk
  else if( medium.diskInfo().diskState() == K3bDevice::STATE_UNKNOWN &&
	   medium.diskInfo().mediaType() == K3bDevice::MEDIA_CD_ROM &&
	   d->wantedMediaType & K3bDevice::MEDIA_CD_ROM )
    finishWaiting( medium.diskInfo().mediaType() );

  // format CD-RW
  else if( (d->wantedMediaType & medium.diskInfo().mediaType()) &&
	   (d->wantedMediaState & K3bDevice::STATE_EMPTY) &&
	   medium.diskInfo().rewritable() ) {

    if( formatWithoutAsking ||
	KMessageBox::questionYesNo( parentWidgetToUse(),
				    i18n("Found rewritable media in %1 - %2. "
					 "Should it be erased?").arg(d->device->vendor()).arg(d->device->description()),
				    i18n("Found Rewritable Disk"),
				    KGuiItem(i18n("&Erase"), "cdrwblank"),
				    KGuiItem(i18n("E&ject")) ) == KMessageBox::Yes ) {


      prepareErasingDialog();

      // start a k3bblankingjob
      d->erasingInfoDialog->setText( i18n("Erasing CD-RW") );

      // the user may be using cdrdao for erasing as cdrecord does not work
      int erasingApp = K3b::DEFAULT;
      if( TDEConfigGroup( c, "General Options" ).readBoolEntry( "Manual writing app selection", false ) ) {
	erasingApp = K3b::writingAppFromString( TDEConfigGroup( c, "CDRW Erasing" ).readEntry( "writing_app" ) );
      }

      K3bBlankingJob job( this );
      job.setDevice( d->device );
      job.setMode( K3bBlankingJob::Fast );
      job.setForce(true);
      job.setForceNoEject(true);
      job.setSpeed( 0 ); // Auto
      job.setWritingApp( erasingApp );
      connect( &job, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotErasingFinished(bool)) );
      connect( d->erasingInfoDialog, TQT_SIGNAL(cancelClicked()), &job, TQT_SLOT(cancel()) );
      job.start();
      d->erasingInfoDialog->exec( false );
    }
    else {
      kdDebug() << "(K3bEmptyDiscWaiter) starting devicehandler: no CD-RW overwrite." << endl;
      K3b::unmount( d->device );
      K3bDevice::eject( d->device );
      continueWaiting();
    }
  }
  else {
    kdDebug() << "(K3bEmptyDiscWaiter) ------ nothing useful found." << endl;
    continueWaiting();
  }

  // handle queued medium changes
  d->blockMediaChange = false;
  if( d->mediumChanged > 0 ) {
    d->mediumChanged--;
    slotMediumChanged( dev );
  }
}


void K3bEmptyDiscWaiter::showDialog()
{
  // we need to show the dialog if not done already
  if( !d->dialogVisible ) {

    KNotifyClient::event( 0, "WaitingForMedium", i18n("Waiting for Medium") );

    d->dialogVisible = true;
    clearWFlags( WDestructiveClose );
    setWFlags( WShowModal );
    setResult( 0 );
    show();
  }
}


void K3bEmptyDiscWaiter::continueWaiting()
{
  showDialog();
}


void K3bEmptyDiscWaiter::slotCancel()
{
  kdDebug() << "(K3bEmptyDiscWaiter) slotCancel() " << endl;
  d->canceled = true;
  finishWaiting( CANCELED );
}


void K3bEmptyDiscWaiter::slotUser1()
{
  d->forced = true;
  finishWaiting( DISK_READY );
}


void K3bEmptyDiscWaiter::slotUser2()
{
  K3b::unmount( d->device );
  K3bDevice::eject( d->device );
}


void K3bEmptyDiscWaiter::slotUser3()
{
  K3bDevice::load( d->device );
}


void K3bEmptyDiscWaiter::finishWaiting( int code )
{
  kdDebug() << "(K3bEmptyDiscWaiter) finishWaiting() " << endl;

  d->waitingDone = true;
  d->result = code;

  if( d->dialogVisible )
    hide();

  if( d->inLoop ) {
    d->inLoop = false;
    kdDebug() << "(K3bEmptyDiscWaiter) exitLoop " << endl;
    TQApplication::eventLoop()->exitLoop();
  }
}


void K3bEmptyDiscWaiter::slotErasingFinished( bool success )
{
  if( success ) {
    connect( K3bDevice::reload( d->device ),
	     TQT_SIGNAL(finished(K3bDevice::DeviceHandler*)),
	     this,
	     TQT_SLOT(slotReloadingAfterErasingFinished(K3bDevice::DeviceHandler*)) );
  }
  else {
    K3bDevice::eject( d->device );
    KMessageBox::error( d->erasingInfoDialog, i18n("Erasing failed.") );
    d->erasingInfoDialog->hide(); // close the dialog thus ending it's event loop -> back to slotMediumChanged
  }
}


void K3bEmptyDiscWaiter::slotReloadingAfterErasingFinished( K3bDevice::DeviceHandler* )
{
  // close the dialog thus ending it's event loop -> back to slotMediumChanged
  d->erasingInfoDialog->hide();
}


int K3bEmptyDiscWaiter::wait( K3bDevice::Device* device, bool appendable, int mediaType, TQWidget* parent )
{
  K3bEmptyDiscWaiter d( device, parent ? parent : TQT_TQWIDGET(tqApp->activeWindow()) );
  int mediaState = K3bDevice::STATE_EMPTY;
  if( appendable ) mediaState |= K3bDevice::STATE_INCOMPLETE;
  return d.waitForDisc( mediaState, mediaType );
}


int K3bEmptyDiscWaiter::wait( K3bDevice::Device* device,
			      int mediaState,
			      int mediaType,
			      const TQString& message,
			      TQWidget* parent )
{
  K3bEmptyDiscWaiter d( device, parent ? parent : TQT_TQWIDGET(tqApp->activeWindow()) );
  return d.waitForDisc( mediaState, mediaType, message );
}


void K3bEmptyDiscWaiter::prepareErasingDialog()
{
  // we hide the emptydiskwaiter so the info dialog needs to have the same parent
  if( !d->erasingInfoDialog )
    d->erasingInfoDialog = new K3bProgressDialog( TQString(), parentWidget() );

  //
  // hide the dialog
  //
  if( d->dialogVisible ) {
    hide();
    d->dialogVisible = false;
  }
}


TQWidget* K3bEmptyDiscWaiter::parentWidgetToUse()
{
  // we might also show dialogs if the discwaiter widget is not visible yet
  if( d->dialogVisible )
    return this;
  else
    return parentWidget();
}


int K3bEmptyDiscWaiter::waitForMedia( K3bDevice::Device* device,
				      int mediaState,
				      int mediaType,
				      const TQString& message )
{
  // this is only needed for the formatting
  return wait( device, mediaState, mediaType, message, d->erasingInfoDialog );
}


bool K3bEmptyDiscWaiter::questionYesNo( const TQString& text,
					const TQString& caption,
					const TQString& yesText,
					const TQString& noText )
{
  return ( KMessageBox::questionYesNo( parentWidgetToUse(),
				       text,
				       caption,
				       yesText.isEmpty() ? KStdGuiItem::yes() : KGuiItem(yesText),
				       noText.isEmpty() ? KStdGuiItem::no() : KGuiItem(noText) ) == KMessageBox::Yes );
}


void K3bEmptyDiscWaiter::blockingInformation( const TQString& text,
					      const TQString& caption )
{
  KMessageBox::information( this, text, caption );
}

#include "k3bemptydiscwaiter.moc"