summaryrefslogtreecommitdiffstats
path: root/src/k3bwriterselectionwidget.cpp
blob: 29a11f9cddc35877b28fddfed0f03f5014117b80 (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
/*
 *
 * $Id: k3bwriterselectionwidget.cpp 690635 2007-07-21 16:47:29Z 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 "k3bwriterselectionwidget.h"
#include "k3bapplication.h"
#include "k3bmediacache.h"

#include <k3bmediaselectioncombobox.h>
#include <k3bdevice.h>
#include <k3bdevicemanager.h>
#include <k3bglobals.h>
#include <k3bcore.h>

#include <klocale.h>
#include <kdialog.h>
#include <kconfig.h>
#include <kcombobox.h>
#include <kmessagebox.h>
#include <kiconloader.h>
#include <kinputdialog.h>

#include <qlabel.h>
#include <qlayout.h>
#include <qgroupbox.h>
#include <qtooltip.h>
#include <qtoolbutton.h>
#include <qwhatsthis.h>
#include <qmap.h>
#include <qptrvector.h>
#include <qcursor.h>
#include <qapplication.h>


class K3bWriterSelectionWidget::MediaSelectionComboBox : public K3bMediaSelectionComboBox
{
public:
  MediaSelectionComboBox( QWidget* parent )
    : K3bMediaSelectionComboBox( parent ),
      m_overrideDevice( 0 ) {
  }

  void setOverrideDevice( K3bDevice::Device* dev, const QString& s, const QString& t ) {
    m_overrideDevice = dev;
    m_overrideString = s;
    m_overrideToolTip = t;
    updateMedia();
  }

  K3bDevice::Device* overrideDevice() const {
    return m_overrideDevice;
  }

 protected:
  bool showMedium( const K3bMedium& m ) const {
    return ( m.device() == m_overrideDevice ||
	     K3bMediaSelectionComboBox::showMedium( m ) );
  }

  QString mediumString( const K3bMedium& m ) const {
    if( m.device() == m_overrideDevice )
      return m_overrideString;
    else
      return K3bMediaSelectionComboBox::mediumString( m );
  }

  QString mediumToolTip( const K3bMedium& m ) const {
    if( m.device() == m_overrideDevice )
      return m_overrideToolTip;
    else {
      QString s = K3bMediaSelectionComboBox::mediumToolTip( m );
      if( !m.diskInfo().empty() && !(wantedMediumState() & m.diskInfo().diskState()) )
	s.append( "<p><i>" + i18n("Medium will be overwritten.") + "</i>" );
      return s;
    }
  }

private:
  K3bDevice::Device* m_overrideDevice;
  QString m_overrideString;
  QString m_overrideToolTip;
};


class K3bWriterSelectionWidget::Private
{
public:
  bool forceAutoSpeed;
  bool haveIgnoreSpeed;
  bool haveManualSpeed;

  int supportedWritingApps;

  int lastSetSpeed;

  QMap<int, int> indexSpeedMap;
  QMap<int, int> speedIndexMap;
};


K3bWriterSelectionWidget::K3bWriterSelectionWidget( QWidget *parent, const char *name )
  : QWidget( parent, name )
{
  d = new Private;
  d->forceAutoSpeed = false;
  d->supportedWritingApps = K3b::CDRECORD|K3b::CDRDAO|K3b::GROWISOFS;
  d->lastSetSpeed = -1;

  QGroupBox* groupWriter = new QGroupBox( this );
  groupWriter->setTitle( i18n( "Burn Medium" ) );
  groupWriter->setColumnLayout(0, Qt::Vertical );
  groupWriter->layout()->setSpacing( 0 );
  groupWriter->layout()->setMargin( 0 );

  QGridLayout* groupWriterLayout = new QGridLayout( groupWriter->layout() );
  groupWriterLayout->setAlignment( Qt::AlignTop );
  groupWriterLayout->setSpacing( KDialog::spacingHint() );
  groupWriterLayout->setMargin( KDialog::marginHint() );

  QLabel* labelSpeed = new QLabel( groupWriter, "TextLabel1" );
  labelSpeed->setText( i18n( "Speed:" ) );

  m_comboSpeed = new KComboBox( false, groupWriter, "m_comboSpeed" );
  m_comboSpeed->setAutoMask( false );
  m_comboSpeed->setDuplicatesEnabled( false );

  m_comboMedium = new MediaSelectionComboBox( groupWriter );

  m_writingAppLabel = new QLabel( i18n("Writing app:"), groupWriter );
  m_comboWritingApp = new KComboBox( groupWriter );

  groupWriterLayout->addWidget( m_comboMedium, 0, 0 );
  groupWriterLayout->addWidget( labelSpeed, 0, 1 );
  groupWriterLayout->addWidget( m_comboSpeed, 0, 2 );
  groupWriterLayout->addWidget( m_writingAppLabel, 0, 3 );
  groupWriterLayout->addWidget( m_comboWritingApp, 0, 4 );
  groupWriterLayout->setColStretch( 0, 1 );


  QGridLayout* mainLayout = new QGridLayout( this );
  mainLayout->setAlignment( Qt::AlignTop );
  mainLayout->setSpacing( KDialog::spacingHint() );
  mainLayout->setMargin( 0 );

  mainLayout->addWidget( groupWriter, 0, 0 );

  // tab order
  setTabOrder( m_comboMedium, m_comboSpeed );
  setTabOrder( m_comboSpeed, m_comboWritingApp );

  connect( m_comboMedium, SIGNAL(selectionChanged(K3bDevice::Device*)), this, SIGNAL(writerChanged()) );
  connect( m_comboMedium, SIGNAL(selectionChanged(K3bDevice::Device*)),
	   this, SIGNAL(writerChanged(K3bDevice::Device*)) );
  connect( m_comboMedium, SIGNAL(newMedia()), this, SIGNAL(newMedia()) );
  connect( m_comboMedium, SIGNAL(newMedium(K3bDevice::Device*)), this, SIGNAL(newMedium(K3bDevice::Device*)) );
  connect( m_comboMedium, SIGNAL(newMedium(K3bDevice::Device*)), this, SLOT(slotNewBurnMedium(K3bDevice::Device*)) );
  connect( m_comboWritingApp, SIGNAL(activated(int)), this, SLOT(slotWritingAppSelected(int)) );
  connect( this, SIGNAL(writerChanged()), SLOT(slotWriterChanged()) );
  connect( m_comboSpeed, SIGNAL(activated(int)), this, SLOT(slotSpeedChanged(int)) );


  QToolTip::add( m_comboMedium, i18n("The medium that will be used for burning") );
  QToolTip::add( m_comboSpeed, i18n("The speed at which to burn the medium") );
  QToolTip::add( m_comboWritingApp, i18n("The external application to actually burn the medium") );

  QWhatsThis::add( m_comboMedium, i18n("<p>Select the medium that you want to use for burning."
				       "<p>In most cases there will only be one medium available which "
				       "does not leave much choice.") );
  QWhatsThis::add( m_comboSpeed, i18n("<p>Select the speed with which you want to burn."
				      "<p><b>Auto</b><br>"
				      "This will choose the maximum writing speed possible with the used "
				      "medium. "
				      "This is the recommended selection for most media.</p>"
				      "<p><b>Ignore</b> (DVD only)<br>"
				      "This will leave the speed selection to the writer device. "
				      "Use this if K3b is unable to set the writing speed."
				      "<p>1x refers to 1385 KB/s for DVD and 175 KB/s for CD.</p>"
				      "<p><b>Caution:</b> Make sure your system is able to send the data "
				      "fast enough to prevent buffer underruns.") );
  QWhatsThis::add( m_comboWritingApp, i18n("<p>K3b uses the command line tools cdrecord, growisofs, and cdrdao "
					   "to actually write a CD or DVD."
					   "<p>Normally K3b chooses the best "
					   "suited application for every task automatically but in some cases it "
					   "may be possible that one of the applications does not work as intended "
					   "with a certain writer. In this case one may select the "
					   "application manually.") );

  clearSpeedCombo();

  slotConfigChanged(k3bcore->config());
  slotWriterChanged();
}


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


void K3bWriterSelectionWidget::setWantedMediumType( int type )
{
  m_comboMedium->setWantedMediumType( type );
}


void K3bWriterSelectionWidget::setWantedMediumState( int state )
{
  m_comboMedium->setWantedMediumState( state );
}


int K3bWriterSelectionWidget::wantedMediumType() const
{
  return m_comboMedium->wantedMediumType();
}


int K3bWriterSelectionWidget::wantedMediumState() const
{
  return m_comboMedium->wantedMediumState();
}


void K3bWriterSelectionWidget::slotConfigChanged( KConfigBase* c )
{
  KConfigGroup g( c, "General Options" );
  if( g.readBoolEntry( "Manual writing app selection", false ) ) {
    m_comboWritingApp->show();
    m_writingAppLabel->show();
  }
  else {
    m_comboWritingApp->hide();
    m_writingAppLabel->hide();
  }
}


void K3bWriterSelectionWidget::slotRefreshWriterSpeeds()
{
  if( writerDevice() ) {
    QValueList<int> speeds = k3bappcore->mediaCache()->writingSpeeds( writerDevice() );

    int lastSpeed = writerSpeed();

    clearSpeedCombo();

    m_comboSpeed->insertItem( i18n("Auto") );
    if( k3bappcore->mediaCache()->diskInfo( writerDevice() ).isDvdMedia() ) {
      m_comboSpeed->insertItem( i18n("Ignore") );
      d->haveIgnoreSpeed = true;
    }
    else
      d->haveIgnoreSpeed = false;

    if( !d->forceAutoSpeed ) {
      if( speeds.isEmpty() || writerDevice() == m_comboMedium->overrideDevice() ) {
	//
	// In case of the override device we do not know which medium will actually be used
	// So this is the only case in which we need to use the device's max writing speed
	//
	// But we need to know if it will be a CD or DVD medium. Since the override device
	// is only used for CD/DVD copy anyway we simply reply on the inserted medium's type.
	//
	int i = 1;
	int speed = ( k3bappcore->mediaCache()->diskInfo( writerDevice() ).isDvdMedia() ? 1385 : 175 );
	int max = writerDevice()->maxWriteSpeed();
	while( i*speed <= max ) {
	  insertSpeedItem( i*speed );
	  // a little hack to handle the stupid 2.4x DVD speed
	  if( i == 2 && speed == 1385 )
	    insertSpeedItem( (int)(2.4*1385.0) );
	  i = ( i == 1 ? 2 : i+2 );
	}

	//
	// Since we do not know the exact max writing speed if an override device is set (we can't becasue
	// the writer always returns the speed relative to the inserted medium) we allow the user to specify
	// the speed manually
	//
	m_comboSpeed->insertItem( i18n("More...") );
	d->haveManualSpeed = true;
      }
      else {
	for( QValueList<int>::iterator it = speeds.begin(); it != speeds.end(); ++it )
	  insertSpeedItem( *it );
      }
    }

    // try to reload last set speed
    if( d->lastSetSpeed == -1 )
      setSpeed( lastSpeed );
    else
      setSpeed( d->lastSetSpeed );
  }

  m_comboSpeed->setEnabled( writerDevice() != 0 );
}


void K3bWriterSelectionWidget::clearSpeedCombo()
{
  m_comboSpeed->clear();
  d->indexSpeedMap.clear();
  d->speedIndexMap.clear();
  d->haveManualSpeed = false;
  d->haveIgnoreSpeed = false;
}


void K3bWriterSelectionWidget::insertSpeedItem( int speed )
{
  if( !d->speedIndexMap.contains( speed ) ) {
    d->indexSpeedMap[m_comboSpeed->count()] = speed;
    d->speedIndexMap[speed] = m_comboSpeed->count();

    if( k3bappcore->mediaCache()->diskInfo( writerDevice() ).isDvdMedia() )
      m_comboSpeed->insertItem( ( speed%1385 > 0
				? QString::number( (float)speed/1385.0, 'f', 1 )  // example: DVD+R(W): 2.4x
				: QString::number( speed/1385 ) )
			      + "x" );
    else
      m_comboSpeed->insertItem( QString("%1x").arg(speed/175) );
  }
}


void K3bWriterSelectionWidget::slotWritingAppSelected( int )
{
  emit writingAppChanged( selectedWritingApp() );
}


K3bDevice::Device* K3bWriterSelectionWidget::writerDevice() const
{
  return m_comboMedium->selectedDevice();
}


QValueList<K3bDevice::Device*> K3bWriterSelectionWidget::allDevices() const
{
  return m_comboMedium->allDevices();
}


void K3bWriterSelectionWidget::setWriterDevice( K3bDevice::Device* dev )
{
  m_comboMedium->setSelectedDevice( dev );
}


void K3bWriterSelectionWidget::setSpeed( int s )
{
  d->lastSetSpeed = -1;

  if( d->haveIgnoreSpeed && s < 0 )
    m_comboSpeed->setCurrentItem( 1 ); // Ignore
  else if( d->speedIndexMap.contains( s ) )
    m_comboSpeed->setCurrentItem( d->speedIndexMap[s] );
  else {
    m_comboSpeed->setCurrentItem( 0 ); // Auto
    d->lastSetSpeed = s; // remember last set speed
  }
}


void K3bWriterSelectionWidget::setWritingApp( int app )
{
  switch( app ) {
  case K3b::CDRECORD:
    m_comboWritingApp->setCurrentItem( "cdrecord" );
    break;
  case K3b::CDRDAO:
    m_comboWritingApp->setCurrentItem( "cdrdao" );
    break;
  case K3b::DVDRECORD:
    m_comboWritingApp->setCurrentItem( "dvdrecord" );
    break;
  case K3b::GROWISOFS:
    m_comboWritingApp->setCurrentItem( "growisofs" );
    break;
  case K3b::DVD_RW_FORMAT:
    m_comboWritingApp->setCurrentItem( "dvd+rw-format" );
    break;
  default:
    m_comboWritingApp->setCurrentItem( 0 );  // Auto
    break;
  }
}


int K3bWriterSelectionWidget::writerSpeed() const
{
  if( m_comboSpeed->currentItem() == 0 )
    return 0; // Auto
  else if( d->haveIgnoreSpeed && m_comboSpeed->currentItem() == 1 )
    return -1; // Ignore
  else
    return d->indexSpeedMap[m_comboSpeed->currentItem()];
}


int K3bWriterSelectionWidget::writingApp() const
{
  KConfigGroup g( k3bcore->config(), "General Options" );
  if( g.readBoolEntry( "Manual writing app selection", false ) ) {
    return selectedWritingApp();
  }
  else
    return K3b::DEFAULT;
}


int K3bWriterSelectionWidget::selectedWritingApp() const
{
  return K3b::writingAppFromString( m_comboWritingApp->currentText() );
}


void K3bWriterSelectionWidget::slotSpeedChanged( int s )
{
  // the last item is the manual speed selection item
  if( d->haveManualSpeed && s == m_comboSpeed->count() - 1 ) {
    slotManualSpeed();
  }
  else {
    d->lastSetSpeed = d->indexSpeedMap[s];

    if( K3bDevice::Device* dev = writerDevice() )
      dev->setCurrentWriteSpeed( writerSpeed() );
  }
}


void K3bWriterSelectionWidget::slotWriterChanged()
{
  slotRefreshWriterSpeeds();
  slotRefreshWritingApps();

  // save last selected writer
  if( K3bDevice::Device* dev = writerDevice() ) {
    KConfigGroup g( k3bcore->config(), "General Options" );
    g.writeEntry( "current_writer", dev->devicename() );
  }
}


void K3bWriterSelectionWidget::setSupportedWritingApps( int i )
{
  int oldApp = writingApp();

  d->supportedWritingApps = i;

  slotRefreshWritingApps();

  setWritingApp( oldApp );
}


void K3bWriterSelectionWidget::slotRefreshWritingApps()
{
  int i = 0;

  // select the ones that make sense
  if( k3bappcore->mediaCache()->diskInfo( writerDevice() ).isDvdMedia() )
    i = K3b::GROWISOFS|K3b::DVD_RW_FORMAT|K3b::DVDRECORD;
  else
    i = K3b::CDRDAO|K3b::CDRECORD;

  // now strip it down to the ones we support
  i &= d->supportedWritingApps;

  m_comboWritingApp->clear();
  m_comboWritingApp->insertItem( i18n("Auto") );

  if( i & K3b::CDRDAO )
    m_comboWritingApp->insertItem( "cdrdao" );
  if( i & K3b::CDRECORD )
    m_comboWritingApp->insertItem( "cdrecord" );
  if( i & K3b::DVDRECORD )
    m_comboWritingApp->insertItem( "dvdrecord" );
  if( i & K3b::GROWISOFS )
    m_comboWritingApp->insertItem( "growisofs" );
  if( i & K3b::DVD_RW_FORMAT )
    m_comboWritingApp->insertItem( "dvd+rw-format" );

  m_comboWritingApp->setEnabled( writerDevice() != 0 );
}


void K3bWriterSelectionWidget::loadConfig( KConfigBase* c )
{
  setWriterDevice( k3bcore->deviceManager()->findDevice( c->readEntry( "writer_device" ) ) );
  setSpeed( c->readNumEntry( "writing_speed",  0 ) );
  setWritingApp( K3b::writingAppFromString( c->readEntry( "writing_app" ) ) );
}


void K3bWriterSelectionWidget::saveConfig( KConfigBase* c )
{
  c->writeEntry( "writing_speed", writerSpeed() );
  c->writeEntry( "writer_device", writerDevice() ? writerDevice()->devicename() : QString::null );
  c->writeEntry( "writing_app", m_comboWritingApp->currentText() );
}

void K3bWriterSelectionWidget::loadDefaults()
{
  // ignore the writer
  m_comboSpeed->setCurrentItem( 0 ); // Auto
  setWritingApp( K3b::DEFAULT );
}


void K3bWriterSelectionWidget::setForceAutoSpeed( bool b )
{
  d->forceAutoSpeed = b;
  slotRefreshWriterSpeeds();
}


void K3bWriterSelectionWidget::setOverrideDevice( K3bDevice::Device* dev, const QString& overrideString, const QString& tooltip )
{
  m_comboMedium->setOverrideDevice( dev, overrideString, tooltip );
}


void K3bWriterSelectionWidget::slotNewBurnMedium( K3bDevice::Device* dev )
{
  //
  // Try to select a medium that is better suited than the current one
  //
  if( dev && dev != writerDevice() ) {
    K3bMedium medium = k3bappcore->mediaCache()->medium( dev );

    //
    // Always prefer newly inserted media over the override device
    //
    if( writerDevice() == m_comboMedium->overrideDevice() ) {
      setWriterDevice( dev );
    }

    //
    // Prefer an empty medium over one that has to be erased
    //
    else if( wantedMediumState() & K3bDevice::STATE_EMPTY &&
	     !k3bappcore->mediaCache()->diskInfo( writerDevice() ).empty() &&
	     medium.diskInfo().empty() ) {
      setWriterDevice( dev );
    }
  }
}


void K3bWriterSelectionWidget::slotManualSpeed()
{
  //
  // We need to know if it will be a CD or DVD medium. Since the override device
  // is only used for CD/DVD copy anyway we simply reply on the inserted medium's type.
  //
  int speedFactor = ( k3bappcore->mediaCache()->diskInfo( writerDevice() ).isDvdMedia() ? 1385 : 175 );

  bool ok = true;
  int newSpeed = KInputDialog::getInteger( i18n("Set writing speed manually"),
					   i18n("<p>K3b is not able to perfectly determine the maximum "
						"writing speed of an optical writer. Writing speed is always "
						"reported subject to the inserted medium."
						"<p>Please enter the writing speed here and K3b will remember it "
						"for future sessions (Example: 16x)."),
					   writerDevice()->maxWriteSpeed()/speedFactor,
					   1,
					   10000,
					   1,
					   10,
					   &ok,
					   this ) * speedFactor;
  if( ok ) {
    writerDevice()->setMaxWriteSpeed( QMAX( newSpeed, writerDevice()->maxWriteSpeed() ) );
    slotRefreshWriterSpeeds();
    setSpeed( newSpeed );
  }
  else {
    if( d->lastSetSpeed == -1 )
      m_comboSpeed->setCurrentItem( 0 ); // Auto
    else
      setSpeed( d->lastSetSpeed );
  }
}


void K3bWriterSelectionWidget::setIgnoreDevice( K3bDevice::Device* dev )
{
    m_comboMedium->setIgnoreDevice( dev );
}

#include "k3bwriterselectionwidget.moc"