summaryrefslogtreecommitdiffstats
path: root/libk3b/jobs/k3bcdda2wavreader.cpp
blob: 393a13c0abec9564903c23993f1748786af8ef83 (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
/* 
 *
 * $Id: k3bcdda2wavreader.cpp 619556 2007-01-03 17:38:12Z 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 "k3bcdda2wavreader.h"

#include <k3bexternalbinmanager.h>
#include <k3bdevice.h>
#include <k3bdevicemanager.h>
#include <k3bcore.h>
#include <k3bprocess.h>

#include <kdebug.h>
#include <tdelocale.h>

#include <tqvaluevector.h>
#include <tqregexp.h>


class K3bCdda2wavReader::Private
{
public:
  Private() 
    : cdda2wavBin(0),
      process(0),
      cancaled(false),
      running(false),
      fdToWriteTo(-1) {
  }

  const K3bExternalBin* cdda2wavBin;
  K3bProcess* process;

  bool cancaled;
  bool running;

  int fdToWriteTo;

  int currentTrack;
  TQValueVector<int> trackOffsets;
};


K3bCdda2wavReader::K3bCdda2wavReader( TQObject* parent, const char* name )
  : K3bJob( parent, name )
{
  d = new Private();
}


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


bool K3bCdda2wavReader::active() const
{
  return d->running;
}


void K3bCdda2wavReader::writeToFd( int fd )
{
  d->fdToWriteTo = fd;
}


void K3bCdda2wavReader::start()
{
  start( false );
}


void K3bCdda2wavReader::start( bool onlyInfo )
{
  d->running = true;
  d->cancaled = false;
  d->currentTrack = 1;
  d->trackOffsets.clear();

  jobStarted();

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

  // prepare the process
  delete d->process;
  d->process = new K3bProcess();
  d->process->setSplitStdout(true);
  d->process->setSuppressEmptyLines(true);
  d->process->setWorkingDirectory( m_imagePath );
  connect( d->process, TQT_SIGNAL(stdoutLine(const TQString&)), this, TQT_SLOT(slotProcessLine(const TQString&)) );
  connect( d->process, TQT_SIGNAL(stderrLine(const TQString&)), this, TQT_SLOT(slotProcessLine(const TQString&)) );
  connect( d->process, TQT_SIGNAL(processExited(TDEProcess*)), this, TQT_SLOT(slotProcessExited(TDEProcess*)) );

  // create the command line
  *d->process << d->cdda2wavBin->path;
  *d->process << "-vall" << ( d->cdda2wavBin->hasFeature( "gui" ) ? "-gui" : "-g" );
  if( d->cdda2wavBin->hasFeature( "dev" ) )
    *d->process << TQString("dev=%1").arg(K3bDevice::externalBinDeviceParameter(m_device, d->cdda2wavBin));
  else
    *d->process << "-D" << K3bDevice::externalBinDeviceParameter(m_device, d->cdda2wavBin);
  *d->process << ( d->cdda2wavBin->hasFeature( "bulk" ) ? "-bulk" : "-B" );
  if( onlyInfo )
    *d->process << ( d->cdda2wavBin->hasFeature( "info-only" ) ? "-info-only" : "-J" );
  else if( d->fdToWriteTo != -1 )
    *d->process << ( d->cdda2wavBin->hasFeature( "no-infofile" ) ? "-no-infofile" : "-H" );

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

  // start the thing
  if( !d->process->start( TDEProcess::NotifyOnExit, TDEProcess::All ) ) {
    // something went wrong when starting the program
    // it "should" be the executable
    kdDebug() << "(K3bCdda2wavReader) could not start cdda2wav" << endl;
    emit infoMessage( i18n("Could not start %1.").arg("cdda2wav"), K3bJob::ERROR );
    d->running = false;
    jobFinished(false);
  }
}


void K3bCdda2wavReader::cancel()
{
  if( d->running ) {
    d->cancaled = true;
    if( d->process )
      if( d->process->isRunning() )
	d->process->kill();
  }
}


void K3bCdda2wavReader::slotProcessLine( const TQString& line )
{
  // Tracks:11 44:37.30
  // CDINDEX discid: ZvzBXv614ACgzn1bWWy107cs0nA-
  // CDDB discid: 0x8a0a730b
  // CD-Text: not detected
  // CD-Extra: not detected
  // Album title: '' from ''
  // T01:       0  3:39.70 audio linear copydenied stereo title '' from ''
  // T02:   16495  3:10.47 audio linear copydenied stereo title '' from ''
  // T03:   30792  3:30.00 audio linear copydenied stereo title '' from ''
  // T04:   46542  4:05.05 audio linear copydenied stereo title '' from ''
  // T05:   64922  3:44.35 audio linear copydenied stereo title '' from ''
  // T06:   81757  4:36.45 audio linear copydenied stereo title '' from ''
  // T07:  102502  3:59.30 audio linear copydenied stereo title '' from ''
  // T08:  120457  5:24.30 audio linear copydenied stereo title '' from ''
  // T09:  144787  3:26.28 audio linear copydenied stereo title '' from ''
  // T10:  160265  4:07.20 audio linear copydenied stereo title '' from ''
  // T11:  178810  4:51.20 audio linear copydenied stereo title '' from ''

  // percent_done:
  // 100%  track  1 successfully recorded
  // 100%  track  2 successfully recorded
  // 100%  track  3 successfully recorded



  static TQRegExp rx( "T\\d\\d:" );
  if( rx.exactMatch( line.left(4) ) || line.startsWith( "Leadout" ) ) {
    int pos = line.find( " " );
    int endpos = line.find( TQRegExp( "\\d" ), pos );
    endpos = line.find( " ", endpos );
    bool ok;
    int offset = line.mid( pos, endpos-pos ).toInt(&ok);
    if( ok )
      d->trackOffsets.append( offset );
    else
      kdDebug() << "(K3bCdda2wavReader) track offset parsing error: '" << line.mid( pos, endpos-pos ) << "'" << endl;
  }

  else if( line.startsWith( "percent_done" ) ) {
    // the reading starts
    d->currentTrack = 1;
    emit nextTrack( d->currentTrack, d->trackOffsets.count() );
  }

  else if( line.contains("successfully recorded") ) {
    d->currentTrack++;
    emit nextTrack( d->currentTrack, d->trackOffsets.count() );
  }

  else if( line.contains("%") ) {
    // parse progress
    bool ok;
    int p = line.left(3).toInt(&ok);
    if( ok ) {
      emit subPercent( p );

      int overall = d->trackOffsets[d->currentTrack-1];
      int tSize = d->trackOffsets[d->currentTrack] - d->trackOffsets[d->currentTrack-1];
      overall += (tSize*p/100);

      emit percent( overall*100/d->trackOffsets[d->trackOffsets.count()-1] );
    }
    else
      kdDebug() << "(K3bCdda2wavReader) track progress parsing error: '" << line.left(3) << "'" << endl;
  }
}


void K3bCdda2wavReader::slotProcessExited( TDEProcess* p )
{
  d->running = false;

  if( d->cancaled ) {
    emit canceled();
    jobFinished(false);
    return;
  }

  if( p->normalExit() ) {
    // TODO: improve this

    if( p->exitStatus() == 0 ) {
      jobFinished( true );
    }
    else {
      emit infoMessage( i18n("%1 returned an unknown error (code %2).")
			.arg("Cdda2wav").arg(p->exitStatus()), ERROR );
      jobFinished( false );
    }
  }
  else {
    emit infoMessage( i18n("%1 did not exit cleanly.").arg("Cdda2wav"), 
		      ERROR );
    jobFinished( false );
  }
}

#include "k3bcdda2wavreader.moc"