summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3bbinimagewritingjob.cpp
blob: 1ba15cecf0083dcf4075264fc9b10635f4756c70 (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
/*
 *
 * $Id: k3bbinimagewritingjob.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2003 Klaus-Dieter Krannich <[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 "k3bbinimagewritingjob.h"
#include <k3bcdrecordwriter.h>
#include <k3bcdrdaowriter.h>
#include <k3bcore.h>
#include <k3bdevice.h>
#include <k3bglobals.h>
#include <k3bexternalbinmanager.h>

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

#include <tqfile.h>
#include <tqtextstream.h>



K3bBinImageWritingJob::K3bBinImageWritingJob( K3bJobHandler* hdl, TQObject* parent )
  : K3bBurnJob( hdl, parent ),
    m_device(0),
    m_simulate(false),
    m_force(false),
    m_noFix(false),
    m_tocFile(0),
    m_speed(2),
    m_copies(1),
    m_writer(0)
{
}

K3bBinImageWritingJob::~K3bBinImageWritingJob()
{
}

void K3bBinImageWritingJob::start()
{
  m_canceled =  false;

  if( m_copies < 1 )
    m_copies = 1;
  m_finishedCopies = 0;

  jobStarted();
  emit newTask( i18n("Write Binary Image") );

  if( prepareWriter() )
    writerStart();
  else
    cancel();

}

void K3bBinImageWritingJob::cancel()
{
  m_canceled = true;
  m_writer->cancel();
  emit canceled();
  jobFinished( false );
}

bool K3bBinImageWritingJob::prepareWriter()
{
  if( m_writer )
    delete m_writer;

  int usedWritingApp = writingApp();
  const K3bExternalBin* cdrecordBin = k3bcore->externalBinManager()->binObject("cdrecord");
  if( usedWritingApp == K3b::CDRECORD || 
      ( usedWritingApp == K3b::DEFAULT && cdrecordBin && cdrecordBin->hasFeature("cuefile") && m_device->dao() ) ) {
    usedWritingApp = K3b::CDRECORD;

    // IMPROVEME: check if it's a cdrdao toc-file
    if( m_tocFile.right(4) == ".toc" ) {
      kdDebug() << "(K3bBinImageWritingJob) imagefile has ending toc." << endl;
      usedWritingApp = K3b::CDRDAO;
    }
    else {
      // TODO: put this into K3bCueFileParser
      // TODO: check K3bCueFileParser::imageFilenameInCue()
      // let's see if cdrecord can handle the cue file
      TQFile f( m_tocFile );
      if( f.open( IO_ReadOnly ) ) {
	TQTextStream fStr( &f );
	if( fStr.read().contains( "MODE1/2352" ) ) {
	  kdDebug() << "(K3bBinImageWritingJob) cuefile contains MODE1/2352 track. using cdrdao." << endl;
	  usedWritingApp = K3b::CDRDAO;
	}
	f.close();
      }
      else
	kdDebug() << "(K3bBinImageWritingJob) could not open file " << m_tocFile << endl;
    }
  }
  else
    usedWritingApp = K3b::CDRDAO;

  if( usedWritingApp == K3b::CDRECORD ) {
    // create cdrecord job
    K3bCdrecordWriter* writer = new K3bCdrecordWriter( m_device, this );

    writer->setDao( true );
    writer->setSimulate( m_simulate );
    writer->setBurnSpeed( m_speed );
    writer->setCueFile ( m_tocFile );

    if( m_noFix ) {
      writer->addArgument("-multi");
    }

    if( m_force ) {
      writer->addArgument("-force");
    }

    m_writer = writer;
  }
  else {
    // create cdrdao job
    K3bCdrdaoWriter* writer = new K3bCdrdaoWriter( m_device, this );
    writer->setCommand( K3bCdrdaoWriter::WRITE );
    writer->setSimulate( m_simulate );
    writer->setBurnSpeed( m_speed );
    writer->setForce( m_force );

    // multisession
    writer->setMulti( m_noFix );

    writer->setTocFile( m_tocFile );

    m_writer = writer;
  }

  connect( m_writer, TQT_SIGNAL(infoMessage(const TQString&, int)), this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
  connect( m_writer, TQT_SIGNAL(percent(int)), this, TQT_SLOT(copyPercent(int)) );
  connect( m_writer, TQT_SIGNAL(subPercent(int)), this, TQT_SLOT(copySubPercent(int)) );
  connect( m_writer, TQT_SIGNAL(processedSize(int, int)), this, TQT_SIGNAL(processedSize(int, int)) );
  connect( m_writer, TQT_SIGNAL(buffer(int)), this, TQT_SIGNAL(bufferStatus(int)) );
  connect( m_writer, TQT_SIGNAL(deviceBuffer(int)), this, TQT_SIGNAL(deviceBuffer(int)) );
  connect( m_writer, TQT_SIGNAL(writeSpeed(int, int)), this, TQT_SIGNAL(writeSpeed(int, int)) );
  connect( m_writer, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(writerFinished(bool)) );
  connect( m_writer, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newTask(const TQString&)) );
  connect( m_writer, TQT_SIGNAL(newSubTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
  connect( m_writer, TQT_SIGNAL(nextTrack(int, int)), this, TQT_SLOT(slotNextTrack(int, int)) );
  connect( m_writer, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)), this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );

  return true;
}


void K3bBinImageWritingJob::writerStart()
{

  if( waitForMedia( m_device ) < 0 ) {
    cancel();
  }
  // just to be sure we did not get canceled during the async discWaiting
  else if( !m_canceled ) {
    emit burning(true);
    m_writer->start();
  }
}

void K3bBinImageWritingJob::copyPercent(int p)
{
  emit percent( (100*m_finishedCopies + p)/m_copies );
}

void K3bBinImageWritingJob::copySubPercent(int p)
{
  emit subPercent(p);
}

void K3bBinImageWritingJob::writerFinished(bool ok)
{
  if( m_canceled )
    return;

  if (ok) {
    m_finishedCopies++;
    if ( m_finishedCopies == m_copies ) {
      emit infoMessage( i18n("%n copy successfully created", "%n copies successfully created", m_copies),K3bJob::INFO );
      jobFinished( true );
    }
    else {
      writerStart();
    }
  }
  else {
    jobFinished(false);
  }
}


void K3bBinImageWritingJob::slotNextTrack( int t, int tt )
{
  emit newSubTask( i18n("Writing track %1 of %2").arg(t).arg(tt) );
}


TQString K3bBinImageWritingJob::jobDescription() const
{
  return ( i18n("Writing cue/bin Image")
	   + ( m_copies > 1 
	       ? i18n(" - %n Copy", " - %n Copies", m_copies) 
	       : TQString() ) );
}


TQString K3bBinImageWritingJob::jobDetails() const
{
  return m_tocFile.section("/", -1);
}


void K3bBinImageWritingJob::setTocFile(const TQString& s)
{
  m_tocFile = s;
}

#include "k3bbinimagewritingjob.moc"