summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3bdvdformattingjob.cpp
blob: 732e4041be6cf515f1bec57842dc4e4cd631741f (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
/*
 *
 * $Id: k3bdvdformattingjob.cpp 696897 2007-08-06 07:14:14Z trueg $
 * Copyright (C) 2003 Sebastian Trueg <[email protected]>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2007 Sebastian Trueg <[email protected]>
 *
 * 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 "k3bdvdformattingjob.h"

#include <k3bglobals.h>
#include <k3bprocess.h>
#include <k3bdevice.h>
#include <k3bdeviceglobals.h>
#include <k3bdevicehandler.h>
#include <k3bdiskinfo.h>
#include <k3bexternalbinmanager.h>
#include <k3bcore.h>
#include <k3bversion.h>
#include <k3bglobalsettings.h>

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

#include <qvaluelist.h>
#include <qregexp.h>

#include <errno.h>
#include <string.h>


class K3bDvdFormattingJob::Private
{
public:
  Private()
    : quick(false),
      force(false),
      mode(K3b::WRITING_MODE_AUTO),
      device(0),
      process(0),
      dvdFormatBin(0),
      lastProgressValue(0),
      running(false),
      forceNoEject(false) {
  }

  bool quick;
  bool force;
  int mode;

  K3bDevice::Device* device;
  K3bProcess* process;
  const K3bExternalBin* dvdFormatBin;

  int lastProgressValue;

  bool success;
  bool canceled;
  bool running;

  bool forceNoEject;

  bool error;
};


K3bDvdFormattingJob::K3bDvdFormattingJob( K3bJobHandler* jh, QObject* parent, const char* name )
  : K3bBurnJob( jh, parent, name )
{
  d = new Private;
}


K3bDvdFormattingJob::~K3bDvdFormattingJob()
{
  delete d->process;
  delete d;
}


K3bDevice::Device* K3bDvdFormattingJob::writer() const
{
  return d->device;
}


void K3bDvdFormattingJob::setForceNoEject( bool b )
{
  d->forceNoEject = b;
}


QString K3bDvdFormattingJob::jobDescription() const
{
  return i18n("Formatting DVD"); // Formatting DVD±RW
}


QString K3bDvdFormattingJob::jobDetails() const
{
  if( d->quick )
    return i18n("Quick Format");
  else
    return QString::null;
}


void K3bDvdFormattingJob::start()
{
  d->canceled = false;
  d->running = true;
  d->error = false;

  jobStarted();

  if( !d->device ) {
    emit infoMessage( i18n("No device set"), ERROR );
    d->running = false;
    jobFinished(false);
    return;
  }

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

  //
  // first wait for a dvd+rw or dvd-rw
  // Be aware that an empty DVD-RW might be reformatted to another writing mode
  // so we also wait for empty dvds
  //
  if( waitForMedia( d->device,
		    K3bDevice::STATE_COMPLETE|K3bDevice::STATE_INCOMPLETE|K3bDevice::STATE_EMPTY,
		    K3bDevice::MEDIA_WRITABLE_DVD,
		    i18n("Please insert a rewritable DVD medium into drive<p><b>%1 %2 (%3)</b>.")
		    .arg(d->device->vendor()).arg(d->device->description()).arg(d->device->devicename()) ) == -1 ) {
    emit canceled();
    d->running = false;
    jobFinished(false);
    return;
  }

  emit infoMessage( i18n("Checking media..."), INFO );
  emit newTask( i18n("Checking media") );

  connect( K3bDevice::sendCommand( K3bDevice::DeviceHandler::NG_DISKINFO, d->device ),
	   SIGNAL(finished(K3bDevice::DeviceHandler*)),
	   this,
	   SLOT(slotDeviceHandlerFinished(K3bDevice::DeviceHandler*)) );
}


void K3bDvdFormattingJob::start( const K3bDevice::DiskInfo& di )
{
  d->canceled = false;
  d->running = true;

  jobStarted();

  startFormatting( di );
}


void K3bDvdFormattingJob::cancel()
{
  if( d->running ) {
    d->canceled = true;
    if( d->process )
      d->process->kill();
  }
  else {
    kdDebug() << "(K3bDvdFormattingJob) not running." << endl;
  }
}


void K3bDvdFormattingJob::setDevice( K3bDevice::Device* dev )
{
  d->device = dev;
}


void K3bDvdFormattingJob::setMode( int m )
{
  d->mode = m;
}


void K3bDvdFormattingJob::setQuickFormat( bool b )
{
  d->quick = b;
}


void K3bDvdFormattingJob::setForce( bool b )
{
  d->force = b;
}


void K3bDvdFormattingJob::slotStderrLine( const QString& line )
{
// * DVD�RW format utility by <[email protected]>, version 4.4.
// * 4.7GB DVD-RW media in Sequential mode detected.
// * blanking 100.0|

// * formatting 100.0|

  emit debuggingOutput( "dvd+rw-format", line );

  // parsing for the -gui mode (since dvd+rw-format 4.6)
  int pos = line.find( "blanking" );
  if( pos < 0 )
    pos = line.find( "formatting" );
  if( pos >= 0 ) {
    pos = line.find( QRegExp( "\\d" ), pos );
  }
  // parsing for \b\b... stuff
  else if( !line.startsWith("*") ) {
    pos = line.find( QRegExp( "\\d" ) );
  }
  else if( line.startsWith( ":-(" ) ) {
    if( line.startsWith( ":-( unable to proceed with format" ) ) {
      d->error = true;
    }
  }

  if( pos >= 0 ) {
    int endPos = line.find( QRegExp("[^\\d\\.]"), pos ) - 1;
    bool ok;
    int progress = (int)(line.mid( pos, endPos - pos ).toDouble(&ok));
    if( ok ) {
      d->lastProgressValue = progress;
      emit percent( progress );
    }
    else {
      kdDebug() << "(K3bDvdFormattingJob) parsing error: '" << line.mid( pos, endPos - pos ) << "'" << endl;
    }
  }
}


void K3bDvdFormattingJob::slotProcessFinished( KProcess* p )
{
  if( d->canceled ) {
    emit canceled();
    d->success = false;
  }
  else if( p->normalExit() ) {
    if( !d->error && p->exitStatus() == 0 ) {
      emit infoMessage( i18n("Formatting successfully completed"), K3bJob::SUCCESS );

      if( d->lastProgressValue < 100 ) {
	emit infoMessage( i18n("Do not be concerned with the progress stopping before 100%."), INFO );
	emit infoMessage( i18n("The formatting will continue in the background while writing."), INFO );
      }

      d->success = true;
    }
    else {
      emit infoMessage( i18n("%1 returned an unknown error (code %2).").arg(d->dvdFormatBin->name()).arg(p->exitStatus()),
			K3bJob::ERROR );
      emit infoMessage( i18n("Please send me an email with the last output."), K3bJob::ERROR );

      d->success = false;
    }
  }
  else {
    emit infoMessage( i18n("%1 did not exit cleanly.").arg(d->dvdFormatBin->name()),
		      ERROR );
    d->success = false;
  }

  if( d->forceNoEject ||
      !k3bcore->globalSettings()->ejectMedia() ) {
    d->running = false;
    jobFinished(d->success);
  }
  else {
    emit infoMessage( i18n("Ejecting DVD..."), INFO );
    connect( K3bDevice::eject( d->device ),
	     SIGNAL(finished(K3bDevice::DeviceHandler*)),
	     this,
	     SLOT(slotEjectingFinished(K3bDevice::DeviceHandler*)) );
  }
}


void K3bDvdFormattingJob::slotEjectingFinished( K3bDevice::DeviceHandler* dh )
{
  if( !dh->success() )
    emit infoMessage( i18n("Unable to eject media."), ERROR );

  d->running = false;
  jobFinished(d->success);
}


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

  if( dh->success() ) {
    startFormatting( dh->diskInfo() );
  }
  else {
    emit infoMessage( i18n("Unable to determine media state."), ERROR );
    d->running = false;
    jobFinished(false);
  }
}


void K3bDvdFormattingJob::startFormatting( const K3bDevice::DiskInfo& diskInfo )
{
  //
  // Now check the media type:
  // if DVD-RW: use d->mode
  //            emit warning if formatting is full and stuff
  //
  // in overwrite mode: emit info that progress might stop before 100% since formatting will continue
  //                    in the background once the media gets rewritten (only DVD+RW?)
  //

  // emit info about what kind of media has been found

  if( !(diskInfo.mediaType() & (K3bDevice::MEDIA_DVD_RW|
				K3bDevice::MEDIA_DVD_RW_SEQ|
				K3bDevice::MEDIA_DVD_RW_OVWR|
				K3bDevice::MEDIA_DVD_PLUS_RW)) ) {
    emit infoMessage( i18n("No rewritable DVD media found. Unable to format."), ERROR );
    d->running = false;
    jobFinished(false);
    return;
  }


  bool format = true;  // do we need to format
  bool blank = false;  // blank is for DVD-RW sequential incremental
  // DVD-RW restricted overwrite and DVD+RW uses the force option (or no option at all)



  //
  // DVD+RW is quite easy to handle. There is only one possible mode and it is always recommended to not
  // format it more than once but to overwrite it once it is formatted
  // Once the initial formatting has been done it's always "appendable" (or "complete"???)
  //


  if( diskInfo.mediaType() == K3bDevice::MEDIA_DVD_PLUS_RW ) {
    emit infoMessage( i18n("Found %1 media.").arg(K3bDevice::mediaTypeString(K3bDevice::MEDIA_DVD_PLUS_RW)),
		      INFO );

    // mode is ignored

    if( diskInfo.empty() ) {
      //
      // The DVD+RW is blank and needs to be initially formatted
      //
      blank = false;
    }
    else {
      emit infoMessage( i18n("No need to format %1 media more than once.")
			.arg(K3bDevice::mediaTypeString(K3bDevice::MEDIA_DVD_PLUS_RW)), INFO );
      emit infoMessage( i18n("It may simply be overwritten."), INFO );

      if( d->force ) {
	emit infoMessage( i18n("Forcing formatting anyway."), INFO );
	emit infoMessage( i18n("It is not recommended to force formatting of DVD+RW media."), INFO );
	emit infoMessage( i18n("Already after 10-20 reformats the media might be unusable."), INFO );
	blank = false;
      }
      else {
	format = false;
      }
    }

    if( format )
      emit newSubTask( i18n("Formatting DVD+RW") );
  }



  //
  // DVD-RW has two modes: incremental sequential (the default which is also needed for DAO writing)
  // and restricted overwrite which compares to the DVD+RW mode.
  //

  else {  // MEDIA_DVD_RW
    emit infoMessage( i18n("Found %1 media.").arg(K3bDevice::mediaTypeString(K3bDevice::MEDIA_DVD_RW)),
		      INFO );

    if( diskInfo.currentProfile() != K3bDevice::MEDIA_UNKNOWN ) {
      emit infoMessage( i18n("Formatted in %1 mode.").arg(K3bDevice::mediaTypeString(diskInfo.currentProfile())), INFO );


      //
      // Is it possible to have an empty DVD-RW in restricted overwrite mode???? I don't think so.
      //

      if( diskInfo.empty() &&
	  (d->mode == K3b::WRITING_MODE_AUTO ||
	   (d->mode == K3b::WRITING_MODE_INCR_SEQ &&
	    diskInfo.currentProfile() == K3bDevice::MEDIA_DVD_RW_SEQ) ||
	   (d->mode == K3b::WRITING_MODE_RES_OVWR &&
	    diskInfo.currentProfile() == K3bDevice::MEDIA_DVD_RW_OVWR) )
	  ) {
	emit infoMessage( i18n("Media is already empty."), INFO );
	if( d->force )
	  emit infoMessage( i18n("Forcing formatting anyway."), INFO );
	else
	  format = false;
      }
      else if( diskInfo.currentProfile() == K3bDevice::MEDIA_DVD_RW_OVWR &&
	       d->mode != K3b::WRITING_MODE_INCR_SEQ ) {
	emit infoMessage( i18n("No need to format %1 media more than once.")
			  .arg(K3bDevice::mediaTypeString(diskInfo.currentProfile())), INFO );
	emit infoMessage( i18n("It may simply be overwritten."), INFO );

	if( d->force )
	  emit infoMessage( i18n("Forcing formatting anyway."), INFO );
	else
	  format = false;
      }


      if( format ) {
	if( d->mode == K3b::WRITING_MODE_AUTO ) {
	  // just format in the same mode as the media is currently formatted
	  blank = (diskInfo.currentProfile() == K3bDevice::MEDIA_DVD_RW_SEQ);
	}
	else {
	  blank = (d->mode == K3b::WRITING_MODE_INCR_SEQ);
	}

	emit newSubTask( i18n("Formatting"
			      " DVD-RW in %1 mode.").arg(K3bDevice::mediaTypeString( blank ?
										     K3bDevice::MEDIA_DVD_RW_SEQ :
										     K3bDevice::MEDIA_DVD_RW_OVWR )) );
      }
    }
    else {
      emit infoMessage( i18n("Unable to determine the current formatting state of the DVD-RW media."), ERROR );
      d->running = false;
      jobFinished(false);
      return;
    }
  }


  if( format ) {
    delete d->process;
    d->process = new K3bProcess();
    d->process->setRunPrivileged(true);
    //      d->process->setSuppressEmptyLines(false);
    connect( d->process, SIGNAL(stderrLine(const QString&)), this, SLOT(slotStderrLine(const QString&)) );
    connect( d->process, SIGNAL(processExited(KProcess*)), this, SLOT(slotProcessFinished(KProcess*)) );

    d->dvdFormatBin = k3bcore->externalBinManager()->binObject( "dvd+rw-format" );
    if( !d->dvdFormatBin ) {
      emit infoMessage( i18n("Could not find %1 executable.").arg("dvd+rw-format"), ERROR );
      d->running = false;
      jobFinished(false);
      return;
    }

    if( !d->dvdFormatBin->copyright.isEmpty() )
      emit infoMessage( i18n("Using %1 %2 - Copyright (C) %3").arg(d->dvdFormatBin->name()).arg(d->dvdFormatBin->version).arg(d->dvdFormatBin->copyright), INFO );


    *d->process << d->dvdFormatBin;

    if( d->dvdFormatBin->version >= K3bVersion( 4, 6 ) )
      *d->process << "-gui";

    QString p;
    if( blank )
      p = "-blank";
    else
      p = "-force";
    if( !d->quick )
      p += "=full";

    *d->process << p;

    *d->process << d->device->blockDeviceName();

    // additional user parameters from config
    const QStringList& params = d->dvdFormatBin->userParameters();
    for( QStringList::const_iterator it = params.begin(); it != params.end(); ++it )
      *d->process << *it;

    kdDebug() << "***** dvd+rw-format parameters:\n";
    const QValueList<QCString>& args = d->process->args();
    QString s;
    for( QValueList<QCString>::const_iterator it = args.begin(); it != args.end(); ++it ) {
      s += *it + " ";
    }
    kdDebug() << s << endl << flush;
    emit debuggingOutput( "dvd+rw-format command:", s );

    if( !d->process->start( KProcess::NotifyOnExit, KProcess::All ) ) {
      // something went wrong when starting the program
      // it "should" be the executable
      kdDebug() << "(K3bDvdFormattingJob) could not start " << d->dvdFormatBin->path << endl;
      emit infoMessage( i18n("Could not start %1.").arg(d->dvdFormatBin->name()), K3bJob::ERROR );
      d->running = false;
      jobFinished(false);
    }
    else {
      emit newTask( i18n("Formatting") );
    }
  }
  else {
    // already formatted :)
    d->running = false;
    jobFinished(true);
  }
}


#include "k3bdvdformattingjob.moc"