summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3baudiocuefilewritingjob.cpp
blob: 65fa3e7ffa51080f93da709b2aa4e753970d84fb (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
/* 
 *
 * $Id: k3baudiocuefilewritingjob.cpp 619556 2007-01-03 17:38:12Z trueg $
 * Copyright (C) 2005 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 "k3baudiocuefilewritingjob.h"

#include <k3baudiodoc.h>
#include <k3baudiojob.h>
#include <k3bdevice.h>
#include <k3baudiodecoder.h>
#include <k3baudiotrack.h>
#include <k3baudiofile.h>
#include <k3bcuefileparser.h>
#include <k3bthread.h>
#include <k3bthreadjob.h>

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


class K3bAudioCueFileWritingJob::AnalyserThread : public K3bThread
{
public:
  AnalyserThread() 
    : K3bThread() {
  }

  void setDecoder( K3bAudioDecoder* dec ) { m_decoder = dec; }

protected:
  void run() {
    emitStarted();
    m_decoder->analyseFile();
    emitFinished(true);
  }

private:
  K3bAudioDecoder* m_decoder;
};


K3bAudioCueFileWritingJob::K3bAudioCueFileWritingJob( K3bJobHandler* jh, TQObject* parent, const char* name )
  : K3bBurnJob( jh, parent, name ),
    m_decoder(0)
{
  m_analyserThread = new AnalyserThread();
  m_analyserJob = new K3bThreadJob( m_analyserThread, this, this );
  connect( m_analyserJob, TQT_SIGNAL(finished(bool)), this, TQT_SLOT(slotAnalyserThreadFinished(bool)) );

  m_audioDoc = new K3bAudioDoc( this );
  m_audioDoc->newDocument();
  m_audioJob = new K3bAudioJob( m_audioDoc, this, this );

  // just loop all through
  connect( m_audioJob, TQT_SIGNAL(newTask(const TQString&)), this, TQT_SIGNAL(newTask(const TQString&)) );
  connect( m_audioJob, TQT_SIGNAL(newSubTask(const TQString&)), this, TQT_SIGNAL(newSubTask(const TQString&)) );
  connect( m_audioJob, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)),
	   this, TQT_SIGNAL(debuggingOutput(const TQString&, const TQString&)) );
  connect( m_audioJob, TQT_SIGNAL(infoMessage(const TQString&, int)),
	   this, TQT_SIGNAL(infoMessage(const TQString&, int)) );
  connect( m_audioJob, TQT_SIGNAL(finished(bool)), this, TQT_SIGNAL(finished(bool)) );
  connect( m_audioJob, TQT_SIGNAL(canceled()), this, TQT_SIGNAL(canceled()) );
  connect( m_audioJob, TQT_SIGNAL(percent(int)), this, TQT_SIGNAL(percent(int)) );
  connect( m_audioJob, TQT_SIGNAL(subPercent(int)), this, TQT_SIGNAL(subPercent(int)) );
  connect( m_audioJob, TQT_SIGNAL(processedSize(int, int)), this, TQT_SIGNAL(processedSubSize(int, int)) );
  connect( m_audioJob, TQT_SIGNAL(processedSubSize(int, int)), this, TQT_SIGNAL(processedSubSize(int, int)) );
  connect( m_audioJob, TQT_SIGNAL(burning(bool)), this, TQT_SIGNAL(burning(bool)) );
  connect( m_audioJob, TQT_SIGNAL(bufferStatus(int)), this, TQT_SIGNAL(bufferStatus(int)) );
  connect( m_audioJob, TQT_SIGNAL(deviceBuffer(int)), this, TQT_SIGNAL(deviceBuffer(int)) );
  connect( m_audioJob, TQT_SIGNAL(writeSpeed(int, int)), this, TQT_SIGNAL(writeSpeed(int, int)) );

  m_canceled = false;
  m_audioJobRunning = false;
}


K3bAudioCueFileWritingJob::~K3bAudioCueFileWritingJob()
{
  // the threadjob does not delete the thread
  delete m_analyserThread;
}


K3bDevice::Device* K3bAudioCueFileWritingJob::writer() const
{
  return m_audioDoc->burner();
}


TQString K3bAudioCueFileWritingJob::jobDescription() const
{
  return i18n("Writing Audio Cue File");
}


TQString K3bAudioCueFileWritingJob::jobDetails() const
{
  return m_cueFile.section( '/', -1 );
}


void K3bAudioCueFileWritingJob::start()
{
  // FIXME: here we trust that a job won't be started twice :(
  jobStarted();
  m_canceled = false;
  m_audioJobRunning = false;
  importCueInProject();
}


void K3bAudioCueFileWritingJob::cancel()
{
  m_canceled = true;

  // the AudioJob cancel method is very stupid. It emits the canceled signal even if it was never running :(
  if( m_audioJobRunning )
    m_audioJob->cancel();
  m_analyserJob->cancel();
}


void K3bAudioCueFileWritingJob::setCueFile( const TQString& s )
{
  m_cueFile = s;
}


void K3bAudioCueFileWritingJob::setOnTheFly( bool b )
{
  m_audioDoc->setOnTheFly( b );
}


void K3bAudioCueFileWritingJob::setSpeed( int s )
{
  m_audioDoc->setSpeed( s );
}


void K3bAudioCueFileWritingJob::setBurnDevice( K3bDevice::Device* dev )
{
  m_audioDoc->setBurner( dev );
}


void K3bAudioCueFileWritingJob::setWritingMode( int mode )
{
  m_audioDoc->setWritingMode( mode );
}


void K3bAudioCueFileWritingJob::setSimulate( bool b )
{
  m_audioDoc->setDummy( b );
}


void K3bAudioCueFileWritingJob::setCopies( int c )
{
  m_audioDoc->setCopies( c );
}


void K3bAudioCueFileWritingJob::setTempDir( const TQString& s )
{
  m_audioDoc->setTempDir( s );
}


void K3bAudioCueFileWritingJob::slotAnalyserThreadFinished( bool )
{
  if( !m_canceled ) {
    if( m_audioDoc->lastTrack()->length() == 0 ) {
      emit infoMessage( i18n("Analysing the audio file failed. Corrupt file?"), ERROR );
      jobFinished(false);
    }
    else {
      // FIXME: m_audioJobRunning is never reset
      m_audioJobRunning = true;
      m_audioJob->start(); // from here on the audio job takes over completely
    }
  }
  else {
    emit canceled();
    jobFinished(false);
  }
}


void K3bAudioCueFileWritingJob::importCueInProject()
{
  // cleanup the project (this wil also delete the decoder)
  // we do not use newDocument as that would overwrite the settings already made
  while( m_audioDoc->firstTrack() )
    delete m_audioDoc->firstTrack()->take();

  m_decoder = 0;

  K3bCueFileParser parser( m_cueFile );
  if( parser.isValid() && parser.toc().contentType() == K3bDevice::AUDIO ) {

    kdDebug() << "(K3bAudioCueFileWritingJob::importCueFile) parsed with image: " << parser.imageFilename() << endl;

    // global cd-text
    m_audioDoc->setTitle( parser.cdText().title() );
    m_audioDoc->setPerformer( parser.cdText().performer() );
    m_audioDoc->writeCdText( !parser.cdText().title().isEmpty() );

    m_decoder = K3bAudioDecoderFactory::createDecoder( parser.imageFilename() );
    if( m_decoder ) {
      m_decoder->setFilename( parser.imageFilename() );

      K3bAudioTrack* after = 0;
      K3bAudioFile* newFile = 0;
      unsigned int i = 0;
      for( K3bDevice::Toc::const_iterator it = parser.toc().begin();
	   it != parser.toc().end(); ++it ) {
	const K3bDevice::Track& track = *it;

	newFile = new K3bAudioFile( m_decoder, m_audioDoc );
	newFile->setStartOffset( track.firstSector() );
	newFile->setEndOffset( track.lastSector()+1 );

	K3bAudioTrack* newTrack = new K3bAudioTrack( m_audioDoc );
	newTrack->addSource( newFile );
	newTrack->moveAfter( after );

	// cd-text
	newTrack->setTitle( parser.cdText()[i].title() );
	newTrack->setPerformer( parser.cdText()[i].performer() );

	// add the next track after this one
	after = newTrack;
	++i;
      }

      // let the last source use the data up to the end of the file
      if( newFile )
	newFile->setEndOffset(0);

      // now analyze the source
      emit newTask( i18n("Analysing the audio file") );
      emit newSubTask( i18n("Analysing %1").tqarg( parser.imageFilename() ) );

      // start the analyser thread
      m_analyserThread->setDecoder( m_decoder );
      m_analyserJob->start();
    }
    else {
      emit infoMessage( i18n("Unable to handle '%1' due to an unsupported format.").tqarg( m_cueFile ), ERROR );
      jobFinished(false);
    }
  }
  else {
    emit infoMessage( i18n("No valid audio cue file: '%1'").tqarg( m_cueFile ), ERROR );
    jobFinished(false);
  }
}

#include "k3baudiocuefilewritingjob.moc"