summaryrefslogtreecommitdiffstats
path: root/libk3b/projects/datadvd/k3bdvdbooktypejob.cpp
blob: f5dea6bb30d8a50f2fd1fab825886e933beca064 (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
/* 
 *
 * $Id: k3bdvdbooktypejob.cpp 619556 2007-01-03 17:38:12Z 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 "k3bdvdbooktypejob.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 <tqvaluelist.h>
#include <tqregexp.h>

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


class K3bDvdBooktypeJob::Private
{
public:
  Private()
    : device(0),
      process(0),
      dvdBooktypeBin(0),
      running(false),
      forceNoEject(false) {
  }

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

  bool success;
  bool canceled;
  bool running;

  bool forceNoEject;

  int foundMediaType;
};


K3bDvdBooktypeJob::K3bDvdBooktypeJob( K3bJobHandler* jh, TQObject* tqparent, const char* name )
  : K3bJob( jh, tqparent, name ),
    m_action(0)
{
  d = new Private;
}


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


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


TQString K3bDvdBooktypeJob::jobDescription() const
{
  return i18n("Changing DVD Booktype"); // Changing DVD±R(W) Booktype
}


TQString K3bDvdBooktypeJob::jobDetails() const
{
  return TQString();
}


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

  jobStarted();

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

  //
  // In case we want to change the writers default we do not need to wait for a media
  //
  if( m_action == SET_MEDIA_DVD_ROM ||
      m_action == SET_MEDIA_DVD_R_W ) {
    emit newSubTask( i18n("Waiting for media") );
    if( waitForMedia( d->device,  
		      K3bDevice::STATE_COMPLETE|K3bDevice::STATE_INCOMPLETE|K3bDevice::STATE_EMPTY,
		      K3bDevice::MEDIA_DVD_PLUS_RW|K3bDevice::MEDIA_DVD_PLUS_R,
		      i18n("Please insert an empty DVD+R or a DVD+RW medium into drive<p><b>%1 %2 (%3)</b>.")
		      .tqarg(d->device->vendor()).tqarg(d->device->description()).tqarg(d->device->devicename()) ) == -1 ) {
      emit canceled();
      jobFinished(false);
      d->running = false;
      return;
    }

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

    connect( K3bDevice::sendCommand( K3bDevice::DeviceHandler::NG_DISKINFO, d->device ), 
	     TQT_SIGNAL(finished(K3bDevice::DeviceHandler*)),
	     this, 
	     TQT_SLOT(slotDeviceHandlerFinished(K3bDevice::DeviceHandler*)) );
  }
  else {
    // change writer defaults
    startBooktypeChange();
  }
}


void K3bDvdBooktypeJob::start( K3bDevice::DeviceHandler* dh )
{
  d->canceled = false;
  d->running = true;

  jobStarted();

  slotDeviceHandlerFinished( dh );
}


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


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


void K3bDvdBooktypeJob::slotStderrLine( const TQString& line )
{
  emit debuggingOutput( "dvd+rw-booktype", line );
  // FIXME
}


void K3bDvdBooktypeJob::slotProcessFinished( KProcess* p )
{
  if( d->canceled ) {
    emit canceled();
    d->success = false;
  }
  else if( p->normalExit() ) {
    if( p->exitStatus() == 0 ) {
      emit infoMessage( i18n("Booktype successfully changed"), K3bJob::SUCCESS );
      d->success = true;
    }
    else {
      emit infoMessage( i18n("%1 returned an unknown error (code %2).").tqarg(d->dvdBooktypeBin->name()).tqarg(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.").tqarg(d->dvdBooktypeBin->name()), 
		      ERROR );
    d->success = false;
  }

  //
  // No need to eject the media if we changed the writer's default
  //
  if( m_action == SET_MEDIA_DVD_ROM ||
      m_action == SET_MEDIA_DVD_R_W ) {

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


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

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


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

  if( dh->success() ) {

    d->foundMediaType = dh->diskInfo().mediaType();
    if( d->foundMediaType == K3bDevice::MEDIA_DVD_PLUS_R ) {
      // the media needs to be empty
      if( dh->diskInfo().empty() )
	startBooktypeChange();
      else {
	emit infoMessage( i18n("Cannot change booktype on non-empty DVD+R media."), ERROR );
	jobFinished(false);
      }
    }
    else if( d->foundMediaType == K3bDevice::MEDIA_DVD_PLUS_RW ) {
      startBooktypeChange();
    }
    else {
      emit infoMessage( i18n("No DVD+R(W) media found."), ERROR );
      jobFinished(false);
    }
  }
  else {
    emit infoMessage( i18n("Unable to determine media state."), ERROR );
    d->running = false;
    jobFinished(false);
  }
}


void K3bDvdBooktypeJob::startBooktypeChange()
{
  delete d->process;
  d->process = new K3bProcess();
  d->process->setRunPrivileged(true);
  d->process->setSuppressEmptyLines(true);
  connect( d->process, TQT_SIGNAL(stderrLine(const TQString&)), this, TQT_SLOT(slotStderrLine(const TQString&)) );
  connect( d->process, TQT_SIGNAL(processExited(KProcess*)), this, TQT_SLOT(slotProcessFinished(KProcess*)) );

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

  *d->process << d->dvdBooktypeBin;

  switch( m_action ) {
  case SET_MEDIA_DVD_ROM:
    *d->process << "-dvd-rom-spec"
		<< "-media";
    break;
  case SET_MEDIA_DVD_R_W:
    if( d->foundMediaType == K3bDevice::MEDIA_DVD_PLUS_RW )
      *d->process << "-dvd+rw-spec";
    else
      *d->process << "-dvd+r-spec";
    *d->process << "-media";
    break;
  case SET_UNIT_DVD_ROM_ON_NEW_DVD_R:
    *d->process << "-dvd-rom-spec"
		<< "-unit+r";
    break;
  case SET_UNIT_DVD_ROM_ON_NEW_DVD_RW:
    *d->process << "-dvd-rom-spec"
		<< "-unit+rw";
    break;
  case SET_UNIT_DVD_R_ON_NEW_DVD_R:
    *d->process << "-dvd+r-spec"
		<< "-unit+r";
    break;
  case SET_UNIT_DVD_RW_ON_NEW_DVD_RW:
    *d->process << "-dvd+rw-spec"
		<< "-unit+rw";
    break;
  }

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

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


  if( !d->process->start( KProcess::NotifyOnExit, KProcess::All ) ) {
    // something went wrong when starting the program
    // it "should" be the executable
    emit infoMessage( i18n("Could not start %1.").tqarg(d->dvdBooktypeBin->name()), K3bJob::ERROR );
    d->running = false;
    jobFinished(false);
  }
  else {
    emit newTask( i18n("Changing Booktype") );
  }
}

#include "k3bdvdbooktypejob.moc"