summaryrefslogtreecommitdiffstats
path: root/libk3b/projects/datadvd/k3bdvdjob.cpp
blob: 4a4adcb92ab56551925f53b24cf29fba50579c56 (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
/*
 *
 * $Id: k3bdvdjob.cpp 690187 2007-07-20 09:18:03Z 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 "k3bdvdjob.h"
#include "k3bdvddoc.h"

#include <k3bcore.h>
#include <k3bisoimager.h>
#include <k3bgrowisofswriter.h>
#include <k3bglobals.h>
#include <k3bdevice.h>
#include <k3bdevicehandler.h>
#include <k3bdiskinfo.h>
#include <k3bdeviceglobals.h>
#include <k3bglobalsettings.h>
#include <k3biso9660.h>
#include <k3bmsinfofetcher.h>

#include <klocale.h>
#include <kapplication.h>


class K3bDvdJob::Private
{
public:
};


K3bDvdJob::K3bDvdJob( K3bDataDoc* doc, K3bJobHandler* hdl, TQObject* parent )
  : K3bDataJob( doc, hdl, parent ),
    m_doc( doc )
{
  d = new Private();
}


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


void K3bDvdJob::prepareData()
{
}


bool K3bDvdJob::prepareWriterJob()
{
  K3bGrowisofsWriter* writer = new K3bGrowisofsWriter( m_doc->burner(), this, this );

  // these do only make sense with DVD-R(W)
  writer->setSimulate( m_doc->dummy() );
  writer->setBurnSpeed( m_doc->speed() );

  // Andy said incremental sequential is the default mode and it seems uses have more problems with DAO anyway
  // BUT: I also had a report that incremental sequential produced unreadable media!
  if( m_doc->writingMode() == K3b::DAO )
//     || ( m_doc->writingMode() == K3b::WRITING_MODE_AUTO &&
// 	 usedMultiSessionMode() == K3bDataDoc::NONE ) )
    writer->setWritingMode( K3b::DAO );

  writer->setMultiSession( usedMultiSessionMode() == K3bDataDoc::CONTINUE ||
  		           usedMultiSessionMode() == K3bDataDoc::FINISH );

  writer->setCloseDvd( usedMultiSessionMode() == K3bDataDoc::NONE ||
		       usedMultiSessionMode() == K3bDataDoc::FINISH );

  writer->setImageToWrite( TQString() );  // read from stdin
  writer->setTrackSize( m_isoImager->size() );

  if( usedMultiSessionMode() != K3bDataDoc::NONE ) {
    //
    // growisofs wants a valid -C parameter for multisession, so we get it from the
    // K3bMsInfoFetcher (see K3bDataJob::slotMsInfoFetched)
    //
    writer->setMultiSessionInfo( m_msInfoFetcher->msInfo() );
  }

  setWriterJob( writer );

  return true;
}


void K3bDvdJob::determineMultiSessionMode()
{
  int m = requestMedia( K3bDevice::STATE_INCOMPLETE|K3bDevice::STATE_EMPTY );

  if( m < 0 ) {
    cancel();
  }
  else {
     connect( K3bDevice::sendCommand( K3bDevice::DeviceHandler::NG_DISKINFO, m_doc->burner() ),
	     TQT_SIGNAL(finished(K3bDevice::DeviceHandler*)),
	     this,
	     TQT_SLOT(slotDetermineMultiSessionMode(K3bDevice::DeviceHandler*)) );
  }
}


K3bDataDoc::MultiSessionMode K3bDvdJob::getMultiSessionMode( const K3bDevice::DiskInfo& info )
{
  K3bDataDoc::MultiSessionMode mode = K3bDataDoc::NONE;

  if( info.mediaType() & (K3bDevice::MEDIA_DVD_PLUS_RW|K3bDevice::MEDIA_DVD_RW_OVWR) ) {
    //
    // we need to handle DVD+RW and DVD-RW overwrite media differently since remainingSize() is not valid
    // in both cases
    // Since one never closes a DVD+RW we only differ between CONTINUE and START
    //

    // try to check the filesystem size
    K3bIso9660 iso( m_doc->burner() );
    if( iso.open() && info.capacity() - iso.primaryDescriptor().volumeSpaceSize >= m_doc->burningLength() ) {
      mode = K3bDataDoc::CONTINUE;
    }
    else {
      mode = K3bDataDoc::START;
    }
  }
  else if( info.appendable() ) {
    //
    // 3 cases:
    //  1. the project does not fit -> no multisession (resulting in asking for another media above)
    //  2. the project does fit and fills up the CD (No new sessions after the 4GB boundary) -> finish multisession
    //  3. the project does fit and does not fill up the CD -> continue multisession
    //
    if( m_doc->size() > info.remainingSize().mode1Bytes() && !m_doc->sessionImported() )
      mode = K3bDataDoc::NONE;
    else if( info.size() + m_doc->burningLength() + 11400 /* used size + project size + session gap */ > 2097152 /* 4 GB */ )
      mode = K3bDataDoc::FINISH;
    else
      mode = K3bDataDoc::CONTINUE;
  }
  else {
    //
    // We only close the DVD if the project fills it beyond the 4GB boundary
    //
    if( info.size() + m_doc->burningLength() + 11400 /* used size + project size + session gap */ > 2097152 /* 4 GB */ ||
	m_doc->writingMode() == K3b::DAO )
      mode = K3bDataDoc::NONE;
    else
      mode = K3bDataDoc::START;
  }

  return mode;
}


int K3bDvdJob::requestMedia( int state )
{
  int mt = 0;
  if( m_doc->writingMode() == K3b::WRITING_MODE_RES_OVWR ) // we treat DVD+R(W) as restricted overwrite media
    mt = K3bDevice::MEDIA_DVD_RW_OVWR|K3bDevice::MEDIA_DVD_PLUS_RW|K3bDevice::MEDIA_DVD_PLUS_R;
  else
    mt = K3bDevice::MEDIA_WRITABLE_DVD;

  // double layer media
  // in case overburn is enabled we allow some made up max size
  // before we force a DL medium
  if( m_doc->size() > 4700372992LL ) {
    if( !k3bcore->globalSettings()->overburn() ||
	m_doc->size() > 4900000000LL ) {
      mt = K3bDevice::MEDIA_WRITABLE_DVD_DL;
    }
  }

  return waitForMedia( m_doc->burner(),
		       state,
		       mt );
}


bool K3bDvdJob::waitForMedium()
{
  emit infoMessage( i18n("Waiting for media") + "...", INFO );

  int foundMedium = requestMedia( usedMultiSessionMode() == K3bDataDoc::CONTINUE ||
				  usedMultiSessionMode() == K3bDataDoc::FINISH ?
				  K3bDevice::STATE_INCOMPLETE :
				  K3bDevice::STATE_EMPTY );

  if( foundMedium < 0 || hasBeenCanceled() ) {
    return false;
  }

  if( foundMedium == 0 ) {
    emit infoMessage( i18n("Forced by user. Growisofs will be called without further tests."), INFO );
  }

  else {
    // -------------------------------
    // DVD Plus
    // -------------------------------
    if( foundMedium & K3bDevice::MEDIA_DVD_PLUS_ALL ) {
      if( m_doc->dummy() ) {
	if( !questionYesNo( i18n("K3b does not support simulation with DVD+R(W) media. "
				 "Do you really want to continue? The media will be written "
				 "for real."),
			    i18n("No Simulation with DVD+R(W)") ) ) {
	  return false;
	}

	m_doc->setDummy( false );
	emit newTask( i18n("Writing") );
      }

      if( m_doc->writingMode() != K3b::WRITING_MODE_AUTO && m_doc->writingMode() != K3b::WRITING_MODE_RES_OVWR )
	emit infoMessage( i18n("Writing mode ignored when writing DVD+R(W) media."), INFO );

      if( foundMedium & K3bDevice::MEDIA_DVD_PLUS_RW ) {
	  if( usedMultiSessionMode() == K3bDataDoc::NONE ||
	      usedMultiSessionMode() == K3bDataDoc::START )
	    emit infoMessage( i18n("Writing DVD+RW."), INFO );
	  else
	    emit infoMessage( i18n("Growing ISO9660 filesystem on DVD+RW."), INFO );
      }
      else if( foundMedium & K3bDevice::MEDIA_DVD_PLUS_R_DL )
	emit infoMessage( i18n("Writing Double Layer DVD+R."), INFO );
      else
	emit infoMessage( i18n("Writing DVD+R."), INFO );
    }

    // -------------------------------
    // DVD Minus
    // -------------------------------
    else {
      if( m_doc->dummy() && !m_doc->burner()->dvdMinusTestwrite() ) {
	if( !questionYesNo( i18n("Your writer (%1 %2) does not support simulation with DVD-R(W) media. "
				 "Do you really want to continue? The media will be written "
				 "for real.")
			    .tqarg(m_doc->burner()->vendor())
			    .tqarg(m_doc->burner()->description()),
			    i18n("No Simulation with DVD-R(W)") ) ) {
	  return false;
	}

	m_doc->setDummy( false );
      }

      // RESTRICTED OVERWRITE
      // --------------------
      if( foundMedium & K3bDevice::MEDIA_DVD_RW_OVWR ) {
	if( usedMultiSessionMode() == K3bDataDoc::NONE ||
	    usedMultiSessionMode() == K3bDataDoc::START )
	  emit infoMessage( i18n("Writing DVD-RW in restricted overwrite mode."), INFO );
	else
	  emit infoMessage( i18n("Growing ISO9660 filesystem on DVD-RW in restricted overwrite mode."), INFO );
      }

      // NORMAL
      // ------
      else {

	// FIXME: DVD-R DL jump and stuff

	if( m_doc->writingMode() == K3b::DAO )
	    // || ( m_doc->writingMode() == K3b::WRITING_MODE_AUTO &&
// 	      usedMultiSessionMode() == K3bDataDoc::NONE ) )
	  emit infoMessage( i18n("Writing %1 in DAO mode.").tqarg( K3bDevice::mediaTypeString(foundMedium, true) ), INFO );

	else {
	  // check if the writer supports writing sequential and thus multisession (on -1 the burner cannot handle
	  // features and we simply ignore it and hope for the best)
	  if( m_doc->burner()->featureCurrent( K3bDevice::FEATURE_INCREMENTAL_STREAMING_WRITABLE ) == 0 ) {
	    if( !questionYesNo( i18n("Your writer (%1 %2) does not support Incremental Streaming with %3 "
				     "media. Multisession will not be possible. Continue anyway?")
				.tqarg(m_doc->burner()->vendor())
				.tqarg(m_doc->burner()->description())
				.tqarg( K3bDevice::mediaTypeString(foundMedium, true) ),
				i18n("No Incremental Streaming") ) ) {
	      return false;
	    }
	    else {
	      emit infoMessage( i18n("Writing %1 in DAO mode.").tqarg( K3bDevice::mediaTypeString(foundMedium, true) ), INFO );
	    }
	  }
	  else {
	    if( !(foundMedium & (K3bDevice::MEDIA_DVD_RW|K3bDevice::MEDIA_DVD_RW_OVWR|K3bDevice::MEDIA_DVD_RW_SEQ)) &&
		m_doc->writingMode() == K3b::WRITING_MODE_RES_OVWR )
	      emit infoMessage( i18n("Restricted Overwrite is not possible with DVD-R media."), INFO );

	    emit infoMessage( i18n("Writing %1 in incremental mode.").tqarg( K3bDevice::mediaTypeString(foundMedium, true) ), INFO );
	  }
	}
      }
    }
  }

  return true;
}


TQString K3bDvdJob::jobDescription() const
{
  if( m_doc->onlyCreateImages() ) {
    return i18n("Creating Data Image File");
  }
  else if( m_doc->multiSessionMode() == K3bDataDoc::NONE ||
	   m_doc->multiSessionMode() == K3bDataDoc::AUTO ) {
    return i18n("Writing Data DVD")
      + ( m_doc->isoOptions().volumeID().isEmpty()
	  ? TQString()
	  : TQString( " (%1)" ).tqarg(m_doc->isoOptions().volumeID()) );
  }
  else {
    return i18n("Writing Multisession DVD")
      + ( m_doc->isoOptions().volumeID().isEmpty()
	  ? TQString()
	  : TQString( " (%1)" ).tqarg(m_doc->isoOptions().volumeID()) );
  }
}


TQString K3bDvdJob::jobDetails() const
{
  if( m_doc->copies() > 1 &&
      !m_doc->dummy() &&
      !(m_doc->multiSessionMode() == K3bDataDoc::CONTINUE ||
	m_doc->multiSessionMode() == K3bDataDoc::FINISH) )
    return i18n("ISO9660 Filesystem (Size: %1) - %n copy",
		"ISO9660 Filesystem (Size: %1) - %n copies",
		m_doc->copies())
      .tqarg(KIO::convertSize( m_doc->size() ));
  else
    return i18n("ISO9660 Filesystem (Size: %1)")
      .tqarg(KIO::convertSize( m_doc->size() ));
}

#include "k3bdvdjob.moc"