summaryrefslogtreecommitdiffstats
path: root/libk3b/tools/k3bdevicehandler.cpp
blob: daabe013b6ffb85930b2f148c71679d0f8b36b03 (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
/*
 *
 * $Id: k3bdevicehandler.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 "k3bdevicehandler.h"
#include <k3bprogressinfoevent.h>
#include <k3bthread.h>
#include <k3bdevice.h>
#include <k3bcdtext.h>



class K3bDevice::DeviceHandler::DeviceHandlerThread : public K3bThread
{
public:
  DeviceHandlerThread() 
    : K3bThread(),
      dev(0) {
  }


  void run() {
    success = false;
    m_bCanceled = false;

    // clear data
    toc.clear();
    ngInfo = DiskInfo();
    cdText.clear();
    cdTextRaw.resize(0);

    if( dev ) {
      success = dev->open();
      if( !m_bCanceled && command & DISKINFO ) {
	ngInfo = dev->diskInfo();
	if( !m_bCanceled && !ngInfo.empty() ) {
	  toc = dev->readToc();
	  if( toc.contentType() == AUDIO ||
	      toc.contentType() == MIXED )
	    cdText = dev->readCdText();
	}
      }

      if( !m_bCanceled && command & (NG_DISKINFO|
				     DISKSIZE|
				     REMAININGSIZE|
				     NUMSESSIONS) ) {
	ngInfo = dev->diskInfo();
      }

      if( !m_bCanceled && command & (TOC|TOCTYPE) ) {
	toc = dev->readToc();
      }

      if( !m_bCanceled && command & CD_TEXT ) {
	cdText = dev->readCdText();
	success = (success && !cdText.isEmpty());
      }

      if( !m_bCanceled && command & CD_TEXT_RAW ) {
	unsigned char* data = 0;
	unsigned int dataLen = 0;
	if( dev->readTocPmaAtip( &data, dataLen, 5, false, 0 ) ) {
	  // we need more than the header and a multiple of 18 bytes to have valid CD-TEXT
	  if( dataLen > 4 && dataLen%18 == 4 ) {
	    cdTextRaw.assign( reinterpret_cast<char*>(data), dataLen );
	  }
	  else {
	    kdDebug() << "(K3bDevice::DeviceHandler) invalid CD-TEXT length: " << dataLen << endl;
	    delete [] data;
	    success = false;
	  }
	}
	else
	  success = false;
      }

      if( !m_bCanceled && command & BLOCK )
	success = (success && dev->block( true ));

      if( !m_bCanceled && command & UNBLOCK )
	success = (success && dev->block( false ));

      //
      // It is important that eject is performed before load
      // since the RELOAD command is a combination of both
      //

      if( !m_bCanceled && command & EJECT )
	success = (success && dev->eject());

      if( !m_bCanceled && command & LOAD )
	success = (success && dev->load());

      if( !m_bCanceled && command & BUFFER_CAPACITY )
	success = dev->readBufferCapacity( bufferCapacity, availableBufferCapacity );
	
      dev->close();
    }

    //
    // This thread only gets cancelled if a new request was started.
    // So we don't emit the finished signal for this (old) request.
    //
    if( !m_bCanceled )
      emitFinished(success);
  }

  void cancel() {
    m_bCanceled = true;
  }


  bool success;
  int errorCode;
  int command;
  DiskInfo ngInfo;
  Toc toc;
  CdText cdText;
  TQByteArray cdTextRaw;
  long long bufferCapacity;
  long long availableBufferCapacity;
  Device* dev;

private:
  bool m_bCanceled;
};


K3bDevice::DeviceHandler::DeviceHandler( Device* dev, TQObject* parent, const char* name )
  : K3bThreadJob( 0, parent, name ),
    m_selfDelete(false)
{
  m_thread = new DeviceHandlerThread();
  m_thread->dev = dev;
  setThread( m_thread );
}


K3bDevice::DeviceHandler::DeviceHandler( TQObject* parent, const char* name )
  : K3bThreadJob( 0, parent, name ),
    m_selfDelete(false)
{
  m_thread = new DeviceHandlerThread();
  setThread( m_thread );
}


K3bDevice::DeviceHandler::DeviceHandler( int command, Device* dev, const char* name )
  : K3bThreadJob( 0, 0, name ),
    m_selfDelete(true)
{
  m_thread = new DeviceHandlerThread();
  setThread( m_thread );
  m_thread->dev = dev;
  sendCommand(command);
}

K3bDevice::DeviceHandler::~DeviceHandler()
{
  delete m_thread;
}


int K3bDevice::DeviceHandler::errorCode() const
{
  return m_thread->errorCode;
}

bool K3bDevice::DeviceHandler::success() const
{
  return m_thread->success;
}


const K3bDevice::DiskInfo& K3bDevice::DeviceHandler::diskInfo() const
{
  return m_thread->ngInfo;
}


const K3bDevice::Toc& K3bDevice::DeviceHandler::toc() const
{
  return m_thread->toc;
}

const K3bDevice::CdText& K3bDevice::DeviceHandler::cdText() const
{
  return m_thread->cdText;
}


const TQByteArray& K3bDevice::DeviceHandler::cdTextRaw() const
{
  return m_thread->cdTextRaw;
}


K3b::Msf K3bDevice::DeviceHandler::diskSize() const
{
  return m_thread->ngInfo.capacity();
}

K3b::Msf K3bDevice::DeviceHandler::remainingSize() const
{
  return m_thread->ngInfo.remainingSize();
}

int K3bDevice::DeviceHandler::tocType() const
{
  return m_thread->toc.contentType();
}

int K3bDevice::DeviceHandler::numSessions() const
{
  return m_thread->ngInfo.numSessions();
}

long long K3bDevice::DeviceHandler::bufferCapacity() const
{
  return m_thread->bufferCapacity;
}

long long K3bDevice::DeviceHandler::availableBufferCapacity() const
{
  return m_thread->availableBufferCapacity;
}

void K3bDevice::DeviceHandler::setDevice( Device* dev )
{
  m_thread->dev = dev;
}



void K3bDevice::DeviceHandler::sendCommand( int command )
{
  //
  // We do not want the finished signal emitted in case the devicehandler was cancelled. This is a special case.
  // That's why we do not use K3bThreadJob::start() becasue otherwise we would be registered twice.
  //
  if( m_thread->running() ) {
    kdDebug() << "(K3bDevice::DeviceHandler) thread already running. canceling thread..." << endl;
    m_thread->cancel();
    m_thread->wait();
  }
  else
    jobStarted();

  kdDebug() << "(K3bDevice::DeviceHandler) starting command: " << command << endl;

  m_thread->command = command;
  m_thread->start();
}

void K3bDevice::DeviceHandler::getToc()
{
  sendCommand(DeviceHandler::TOC);
}

void K3bDevice::DeviceHandler::getDiskInfo()
{
  sendCommand(DeviceHandler::DISKINFO);
}

void K3bDevice::DeviceHandler::getDiskSize()
{
  sendCommand(DeviceHandler::DISKSIZE);
}

void K3bDevice::DeviceHandler::getRemainingSize()
{
  sendCommand(DeviceHandler::REMAININGSIZE);
}

void K3bDevice::DeviceHandler::getTocType()
{
  sendCommand(DeviceHandler::TOCTYPE);
}

void K3bDevice::DeviceHandler::getNumSessions()
{
  sendCommand(DeviceHandler::NUMSESSIONS);
}


void K3bDevice::DeviceHandler::block( bool b )
{
  sendCommand(b ? DeviceHandler::BLOCK : DeviceHandler::UNBLOCK);
}

void K3bDevice::DeviceHandler::eject()
{
  sendCommand(DeviceHandler::EJECT);
}

K3bDevice::DeviceHandler* K3bDevice::sendCommand( int command, Device* dev )
{
  return new DeviceHandler( command, dev, "DeviceHandler" );
}

void K3bDevice::DeviceHandler::customEvent( TQCustomEvent* e )
{
  K3bThreadJob::customEvent(e);

  if( (int)e->type() == K3bProgressInfoEvent::Finished ) {
    emit finished( this );
    if( m_selfDelete ) {
      kdDebug() << "(K3bDevice::DeviceHandler) thread emitted finished. Waiting for thread actually finishing" << endl;
      kdDebug() << "(K3bDevice::DeviceHandler) success: " << m_thread->success << endl;
      // wait for the thread to finish
      m_thread->wait();
      kdDebug() << "(K3bDevice::DeviceHandler) deleting thread." << endl;
      deleteLater();
    }
  }
}


#include "k3bdevicehandler.moc"