1 /*
2     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 
8 #include "k3bdevicehandler.h"
9 #include "k3bprogressinfoevent.h"
10 #include "k3bthread.h"
11 #include "k3bdevice.h"
12 #include "k3bcdtext.h"
13 #include "k3bcore.h"
14 #include "k3bmediacache.h"
15 
16 
17 class K3b::Device::DeviceHandler::Private
18 {
19 public:
Private(bool _selfDelete)20     Private( bool _selfDelete )
21         : selfDelete( _selfDelete ) {
22     }
23 
24     bool selfDelete;
25 
26     bool success;
27     Commands command;
28     DiskInfo diskInfo;
29     Toc toc;
30     CdText cdText;
31     QByteArray cdTextRaw;
32     long long bufferCapacity;
33     long long availableBufferCapacity;
34     Device* dev;
35     K3b::Msf nextWritableAddress;
36 };
37 
38 
DeviceHandler(Device * dev,QObject * parent)39 K3b::Device::DeviceHandler::DeviceHandler( Device* dev, QObject* parent )
40     : K3b::ThreadJob( 0, parent ),
41       d( new Private( false ) )
42 {
43     d->dev = dev;
44 }
45 
46 
DeviceHandler(QObject * parent)47 K3b::Device::DeviceHandler::DeviceHandler( QObject* parent )
48     : K3b::ThreadJob( 0, parent ),
49       d( new Private( false ) )
50 {
51 }
52 
53 
DeviceHandler(Commands command,Device * dev)54 K3b::Device::DeviceHandler::DeviceHandler( Commands command, Device* dev )
55     : K3b::ThreadJob( 0, 0 ),
56       d( new Private( false ) )
57 {
58     d->dev = dev;
59     sendCommand(command);
60 }
61 
~DeviceHandler()62 K3b::Device::DeviceHandler::~DeviceHandler()
63 {
64     delete d;
65 }
66 
67 
success() const68 bool K3b::Device::DeviceHandler::success() const
69 {
70     return d->success;
71 }
72 
73 
diskInfo() const74 K3b::Device::DiskInfo K3b::Device::DeviceHandler::diskInfo() const
75 {
76     return d->diskInfo;
77 }
78 
79 
toc() const80 K3b::Device::Toc K3b::Device::DeviceHandler::toc() const
81 {
82     return d->toc;
83 }
84 
cdText() const85 K3b::Device::CdText K3b::Device::DeviceHandler::cdText() const
86 {
87     return d->cdText;
88 }
89 
90 
cdTextRaw() const91 QByteArray K3b::Device::DeviceHandler::cdTextRaw() const
92 {
93     return d->cdTextRaw;
94 }
95 
96 
diskSize() const97 K3b::Msf K3b::Device::DeviceHandler::diskSize() const
98 {
99     return d->diskInfo.capacity();
100 }
101 
remainingSize() const102 K3b::Msf K3b::Device::DeviceHandler::remainingSize() const
103 {
104     return d->diskInfo.remainingSize();
105 }
106 
tocType() const107 int K3b::Device::DeviceHandler::tocType() const
108 {
109     return d->toc.contentType();
110 }
111 
numSessions() const112 int K3b::Device::DeviceHandler::numSessions() const
113 {
114     return d->diskInfo.numSessions();
115 }
116 
bufferCapacity() const117 long long K3b::Device::DeviceHandler::bufferCapacity() const
118 {
119     return d->bufferCapacity;
120 }
121 
availableBufferCapacity() const122 long long K3b::Device::DeviceHandler::availableBufferCapacity() const
123 {
124     return d->availableBufferCapacity;
125 }
126 
nextWritableAddress() const127 K3b::Msf K3b::Device::DeviceHandler::nextWritableAddress() const
128 {
129     return d->nextWritableAddress;
130 }
131 
setDevice(Device * dev)132 void K3b::Device::DeviceHandler::setDevice( Device* dev )
133 {
134     d->dev = dev;
135 }
136 
137 
sendCommand(DeviceHandler::Commands command)138 void K3b::Device::DeviceHandler::sendCommand( DeviceHandler::Commands command )
139 {
140     if( active() ) {
141         qDebug() << "thread already running. canceling thread...";
142         cancel();
143         wait();
144     }
145 
146     d->command = command;
147     start();
148 }
149 
getToc()150 void K3b::Device::DeviceHandler::getToc()
151 {
152     sendCommand(DeviceHandler::CommandToc);
153 }
154 
getDiskInfo()155 void K3b::Device::DeviceHandler::getDiskInfo()
156 {
157     sendCommand(DeviceHandler::CommandDiskInfo);
158 }
159 
getDiskSize()160 void K3b::Device::DeviceHandler::getDiskSize()
161 {
162     sendCommand(DeviceHandler::CommandDiskSize);
163 }
164 
getRemainingSize()165 void K3b::Device::DeviceHandler::getRemainingSize()
166 {
167     sendCommand(DeviceHandler::CommandRemainingSize);
168 }
169 
getTocType()170 void K3b::Device::DeviceHandler::getTocType()
171 {
172     sendCommand(DeviceHandler::CommandTocType);
173 }
174 
getNumSessions()175 void K3b::Device::DeviceHandler::getNumSessions()
176 {
177     sendCommand(DeviceHandler::CommandNumSessions);
178 }
179 
180 
block(bool b)181 void K3b::Device::DeviceHandler::block( bool b )
182 {
183     sendCommand(b ? DeviceHandler::CommandBlock : DeviceHandler::CommandUnblock);
184 }
185 
eject()186 void K3b::Device::DeviceHandler::eject()
187 {
188     sendCommand(DeviceHandler::CommandEject);
189 }
190 
sendCommand(DeviceHandler::Commands command,Device * dev)191 K3b::Device::DeviceHandler* K3b::Device::sendCommand( DeviceHandler::Commands command, Device* dev )
192 {
193     return new DeviceHandler( command, dev );
194 }
195 
jobFinished(bool success)196 void K3b::Device::DeviceHandler::jobFinished( bool success )
197 {
198     K3b::ThreadJob::jobFinished( success );
199 
200     emit finished( this );
201 
202     if( d->selfDelete ) {
203         deleteLater();
204     }
205 }
206 
207 
run()208 bool K3b::Device::DeviceHandler::run()
209 {
210     qDebug() << "starting command: " << d->command;
211 
212     d->success = false;
213 
214     // clear data
215     d->toc.clear();
216     d->diskInfo = DiskInfo();
217     d->cdText.clear();
218     d->cdTextRaw.clear();
219 
220     if( d->dev ) {
221         d->success = d->dev->open();
222         if( !canceled() && d->command & CommandBlock )
223             d->success = (d->success && d->dev->block( true ));
224 
225         if( !canceled() && d->command & CommandUnblock )
226             d->success = (d->success && d->dev->block( false ));
227 
228         //
229         // It is important that eject is performed before load
230         // since the CommandReload command is a combination of both
231         //
232 
233         if( !canceled() && d->command & CommandEject ) {
234             d->success = (d->success && d->dev->eject());
235 
236             // to be on the safe side, especially with respect to the EmptyDiscWaiter
237             // we reset the device in the cache.
238             k3bcore->mediaCache()->resetDevice( d->dev );
239         }
240 
241         if( !canceled() && d->command & CommandLoad )
242             d->success = (d->success && d->dev->load());
243 
244         if( !canceled() && d->command & (CommandDiskInfo|
245                                          CommandDiskSize|
246                                          CommandRemainingSize|
247                                          CommandNumSessions) ) {
248             d->diskInfo = d->dev->diskInfo();
249         }
250 
251         if( !canceled() && d->command & (CommandToc|CommandTocType) ) {
252             d->toc = d->dev->readToc();
253         }
254 
255         if( !canceled() &&
256             d->command & CommandCdText &&
257               !( d->command & CommandToc &&
258                  d->toc.contentType() == DATA )
259             ) {
260             d->cdText = d->dev->readCdText();
261             if ( d->command != CommandMediaInfo )
262                 d->success = (d->success && !d->cdText.isEmpty());
263         }
264 
265         if( !canceled() && d->command & CommandCdTextRaw ) {
266             bool cdTextSuccess = true;
267             d->cdTextRaw = d->dev->readRawCdText( &cdTextSuccess );
268             d->success = d->success && cdTextSuccess;
269         }
270 
271         if( !canceled() && d->command & CommandBufferCapacity )
272             d->success = d->dev->readBufferCapacity( d->bufferCapacity, d->availableBufferCapacity );
273 
274         if ( !canceled() && d->command & CommandNextWritableAddress ) {
275             int nwa = d->dev->nextWritableAddress();
276             d->nextWritableAddress = nwa;
277             d->success = ( d->success && ( nwa > 0 ) );
278         }
279 
280         d->dev->close();
281     }
282 
283     qDebug() << "finished command: " << d->command;
284 
285     return d->success;
286 }
287 
288 
operator <<(QDebug dbg,K3b::Device::DeviceHandler::Commands commands)289 QDebug operator<<( QDebug dbg, K3b::Device::DeviceHandler::Commands commands )
290 {
291     QStringList commandStrings;
292     if ( commands & K3b::Device::DeviceHandler::CommandDiskInfo )
293         commandStrings << QLatin1String( "CommandDiskInfo" );
294     if ( commands & K3b::Device::DeviceHandler::CommandToc )
295         commandStrings << QLatin1String( "CommandToc" );
296     if ( commands & K3b::Device::DeviceHandler::CommandCdText )
297         commandStrings << QLatin1String( "CommandCdText" );
298     if ( commands & K3b::Device::DeviceHandler::CommandCdTextRaw )
299         commandStrings << QLatin1String( "CommandCdTextRaw" );
300     if ( commands & K3b::Device::DeviceHandler::CommandDiskSize )
301         commandStrings << QLatin1String( "CommandDiskSize" );
302     if ( commands & K3b::Device::DeviceHandler::CommandRemainingSize )
303         commandStrings << QLatin1String( "CommandRemainingSize" );
304     if ( commands & K3b::Device::DeviceHandler::CommandTocType )
305         commandStrings << QLatin1String( "CommandTocType" );
306     if ( commands & K3b::Device::DeviceHandler::CommandNumSessions )
307         commandStrings << QLatin1String( "CommandNumSessions" );
308     if ( commands & K3b::Device::DeviceHandler::CommandBlock )
309         commandStrings << QLatin1String( "CommandBlock" );
310     if ( commands & K3b::Device::DeviceHandler::CommandUnblock )
311         commandStrings << QLatin1String( "CommandUnblock" );
312     if ( commands & K3b::Device::DeviceHandler::CommandEject )
313         commandStrings << QLatin1String( "CommandEject" );
314     if ( commands & K3b::Device::DeviceHandler::CommandLoad )
315         commandStrings << QLatin1String( "CommandLoad" );
316     if ( commands & K3b::Device::DeviceHandler::CommandBufferCapacity )
317         commandStrings << QLatin1String( "CommandBufferCapacity" );
318     if ( commands & K3b::Device::DeviceHandler::CommandNextWritableAddress )
319         commandStrings << QLatin1String( "CommandNextWritableAddress" );
320     dbg.nospace() << '(' + commandStrings.join( "|" ) + ')';
321     return dbg.space();
322 }
323 
324 
325