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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
|
/*
*
* $Id: k3bglobals.cpp 659634 2007-04-30 14:51:32Z 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 <config.h>
#include "k3bglobals.h"
#include <k3bversion.h>
#include <k3bdevice.h>
#include <k3bdevicemanager.h>
#include <k3bdeviceglobals.h>
#include <k3bexternalbinmanager.h>
#include <k3bcore.h>
#include <k3bhalconnection.h>
#include <kdeversion.h>
#include <kglobal.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kio/job.h>
#include <kio/netaccess.h>
#include <kurl.h>
#include <dcopref.h>
#include <kprocess.h>
#include <tqdatastream.h>
#include <tqdir.h>
#include <tqfile.h>
#include <cmath>
#include <sys/utsname.h>
#include <sys/stat.h>
#if defined(__FreeBSD__) || defined(__NetBSD__)
# include <sys/param.h>
# include <sys/mount.h>
# include <sys/endian.h>
# define bswap_16(x) bswap16(x)
# define bswap_32(x) bswap32(x)
# define bswap_64(x) bswap64(x)
#else
# include <byteswap.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
# include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_VFS_H
# include <sys/vfs.h>
#endif
/*
struct Sample {
unsigned char msbLeft;
unsigned char lsbLeft;
unsigned char msbRight;
unsigned char lsbRight;
short left() const {
return ( msbLeft << 8 ) | lsbLeft;
}
short right() const {
return ( msbRight << 8 ) | lsbRight;
}
void left( short d ) {
msbLeft = d >> 8;
lsbLeft = d;
}
void right( short d ) {
msbRight = d >> 8;
lsbRight = d;
}
};
*/
TQString K3b::framesToString( int h, bool showFrames )
{
int m = h / 4500;
int s = (h % 4500) / 75;
int f = h % 75;
TQString str;
if( showFrames ) {
// cdrdao needs the MSF format where 1 second has 75 frames!
str.sprintf( "%.2i:%.2i:%.2i", m, s, f );
}
else
str.sprintf( "%.2i:%.2i", m, s );
return str;
}
/*TQString K3b::sizeToTime(long size)
{
int h = size / sizeof(Sample) / 588;
return framesToString(h, false);
}*/
TQ_INT16 K3b::swapByteOrder( const TQ_INT16& i )
{
return bswap_16( i );
//((i << 8) & 0xff00) | ((i >> 8 ) & 0xff);
}
TQ_INT32 K3b::swapByteOrder( const TQ_INT32& i )
{
//return ((i << 24) & 0xff000000) | ((i << 8) & 0xff0000) | ((i >> 8) & 0xff00) | ((i >> 24) & 0xff );
return bswap_32( i );
}
TQ_INT64 K3b::swapByteOrder( const TQ_INT64& i )
{
return bswap_64( i );
}
int K3b::round( double d )
{
return (int)( floor(d) + 0.5 <= d ? ceil(d) : floor(d) );
}
TQString K3b::findUniqueFilePrefix( const TQString& _prefix, const TQString& path )
{
TQString url;
if( path.isEmpty() || !TQFile::exists(path) )
url = defaultTempPath();
else
url = prepareDir( path );
TQString prefix = _prefix;
if( prefix.isEmpty() )
prefix = "k3b_";
// now create the unique prefix
TQDir dir( url );
TQStringList entries = dir.entryList( TQDir::DefaultFilter, TQDir::Name );
int i = 0;
for( TQStringList::iterator it = entries.begin();
it != entries.end(); ++it ) {
if( (*it).startsWith( prefix + TQString::number(i) ) ) {
i++;
it = entries.begin();
}
}
return url + prefix + TQString::number(i);
}
TQString K3b::findTempFile( const TQString& ending, const TQString& d )
{
return findUniqueFilePrefix( "k3b_", d ) + ( ending.isEmpty() ? TQString() : (TQString::tqfromLatin1(".") + ending) );
}
TQString K3b::defaultTempPath()
{
TQString oldGroup = kapp->config()->group();
kapp->config()->setGroup( "General Options" );
TQString url = kapp->config()->readPathEntry( "Temp Dir", KGlobal::dirs()->resourceDirs( "tmp" ).first() );
kapp->config()->setGroup( oldGroup );
return prepareDir(url);
}
TQString K3b::prepareDir( const TQString& dir )
{
return (dir + (dir[dir.length()-1] != '/' ? "/" : ""));
}
TQString K3b::parentDir( const TQString& path )
{
TQString tqparent = path;
if( path[path.length()-1] == '/' )
tqparent.truncate( tqparent.length()-1 );
int pos = tqparent.tqfindRev( '/' );
if( pos >= 0 )
tqparent.truncate( pos+1 );
else // relative path, do anything...
tqparent = "/";
return tqparent;
}
TQString K3b::fixupPath( const TQString& path )
{
TQString s;
bool lastWasSlash = false;
for( unsigned int i = 0; i < path.length(); ++i ) {
if( path[i] == '/' ) {
if( !lastWasSlash ) {
lastWasSlash = true;
s.append( "/" );
}
}
else {
lastWasSlash = false;
s.append( path[i] );
}
}
return s;
}
K3bVersion K3b::kernelVersion()
{
// initialize kernel version
K3bVersion v;
utsname unameinfo;
if( ::uname(&unameinfo) == 0 ) {
v = TQString::fromLocal8Bit( unameinfo.release );
kdDebug() << "kernel version: " << v << endl;
}
else
kdError() << "could not determine kernel version." << endl;
return v;
}
K3bVersion K3b::simpleKernelVersion()
{
return kernelVersion().simplify();
}
TQString K3b::systemName()
{
TQString v;
utsname unameinfo;
if( ::uname(&unameinfo) == 0 ) {
v = TQString::fromLocal8Bit( unameinfo.sysname );
}
else
kdError() << "could not determine system name." << endl;
return v;
}
bool K3b::kbFreeOnFs( const TQString& path, unsigned long& size, unsigned long& avail )
{
struct statvfs fs;
if( ::statvfs( TQFile::encodeName(path), &fs ) == 0 ) {
unsigned long kBfak = fs.f_frsize/1024;
size = fs.f_blocks*kBfak;
avail = fs.f_bavail*kBfak;
return true;
}
else
return false;
}
KIO::filesize_t K3b::filesize( const KURL& url )
{
if( url.isLocalFile() ) {
k3b_struct_stat buf;
if ( !k3b_stat( TQFile::encodeName( url.path() ), &buf ) ) {
return (KIO::filesize_t)buf.st_size;
}
}
KIO::UDSEntry uds;
KIO::NetAccess::stat( url, uds, 0 );
for( KIO::UDSEntry::const_iterator it = uds.begin(); it != uds.end(); ++it ) {
if( (*it).m_uds == KIO::UDS_SIZE ) {
return (*it).m_long;
}
}
return ( KIO::filesize_t )0;
}
KIO::filesize_t K3b::imageFilesize( const KURL& url )
{
KIO::filesize_t size = K3b::filesize( url );
int cnt = 0;
while( KIO::NetAccess::exists( KURL::fromPathOrURL( url.url() + '.' + TQString::number(cnt).rightJustify( 3, '0' ) ), true ) )
size += K3b::filesize( KURL::fromPathOrURL( url.url() + '.' + TQString::number(cnt++).rightJustify( 3, '0' ) ) );
return size;
}
TQString K3b::cutFilename( const TQString& name, unsigned int len )
{
if( name.length() > len ) {
TQString ret = name;
// determine extension (we think of an extension to be at most 5 chars in length)
int pos = name.tqfind( '.', -6 );
if( pos > 0 )
len -= (name.length() - pos);
ret.truncate( len );
if( pos > 0 )
ret.append( name.mid( pos ) );
return ret;
}
else
return name;
}
TQString K3b::removeFilenameExtension( const TQString& name )
{
TQString v = name;
int dotpos = v.tqfindRev( '.' );
if( dotpos > 0 )
v.truncate( dotpos );
return v;
}
TQString K3b::appendNumberToFilename( const TQString& name, int num, unsigned int maxlen )
{
// determine extension (we think of an extension to be at most 5 chars in length)
TQString result = name;
TQString ext;
int pos = name.tqfind( '.', -6 );
if( pos > 0 ) {
ext = name.mid(pos);
result.truncate( pos );
}
ext.prepend( TQString::number(num) );
result.truncate( maxlen - ext.length() );
return result + ext;
}
bool K3b::plainAtapiSupport()
{
// FIXME: what about BSD?
return ( K3b::simpleKernelVersion() >= K3bVersion( 2, 5, 40 ) );
}
bool K3b::hackedAtapiSupport()
{
// IMPROVEME!!!
// FIXME: since when does the kernel support this?
return ( K3b::simpleKernelVersion() >= K3bVersion( 2, 4, 0 ) );
}
TQString K3b::externalBinDeviceParameter( K3bDevice::Device* dev, const K3bExternalBin* bin )
{
#ifdef Q_OS_LINUX
//
// experimental: always use block devices on 2.6 kernels
//
if( simpleKernelVersion() >= K3bVersion( 2, 6, 0 ) )
return dev->blockDeviceName();
else
#endif
if( dev->interfaceType() == K3bDevice::SCSI )
return dev->busTargetLun();
else if( (plainAtapiSupport() && bin->hasFeature("plain-atapi") ) )
return dev->blockDeviceName();
else
return TQString("ATAPI:%1").tqarg(dev->blockDeviceName());
}
int K3b::writingAppFromString( const TQString& s )
{
if( s.lower() == "cdrdao" )
return K3b::CDRDAO;
else if( s.lower() == "cdrecord" )
return K3b::CDRECORD;
else if( s.lower() == "dvdrecord" )
return K3b::DVDRECORD;
else if( s.lower() == "growisofs" )
return K3b::GROWISOFS;
else if( s.lower() == "dvd+rw-format" )
return K3b::DVD_RW_FORMAT;
else
return K3b::DEFAULT;
}
TQString K3b::writingModeString( int mode )
{
if( mode == WRITING_MODE_AUTO )
return i18n("Auto");
else
return K3bDevice::writingModeString( mode );
}
TQString K3b::resolveLink( const TQString& file )
{
TQFileInfo f( file );
TQStringList steps( f.absFilePath() );
while( f.isSymLink() ) {
TQString p = f.readLink();
if( !p.startsWith( "/" ) )
p.prepend( f.dirPath(true) + "/" );
f.setFile( p );
if( steps.tqcontains( f.absFilePath() ) ) {
kdDebug() << "(K3b) symlink loop detected." << endl;
break;
}
else
steps.append( f.absFilePath() );
}
return f.absFilePath();
}
K3bDevice::Device* K3b::urlToDevice( const KURL& deviceUrl )
{
if( deviceUrl.protocol() == "media" || deviceUrl.protocol() == "system" ) {
kdDebug() << "(K3b) Asking mediamanager for " << deviceUrl.fileName() << endl;
DCOPRef mediamanager("kded", "mediamanager");
DCOPReply reply = mediamanager.call("properties(TQString)", deviceUrl.fileName());
TQStringList properties = reply;
if( !reply.isValid() || properties.count() < 6 ) {
kdError() << "(K3b) Invalid reply from mediamanager" << endl;
return 0;
}
else {
kdDebug() << "(K3b) Reply from mediamanager " << properties[5] << endl;
return k3bcore->deviceManager()->findDevice( properties[5] );
}
}
return k3bcore->deviceManager()->findDevice( deviceUrl.path() );
}
KURL K3b::convertToLocalUrl( const KURL& url )
{
if( !url.isLocalFile() ) {
#if KDE_IS_VERSION(3,4,91)
return KIO::NetAccess::mostLocalURL( url, 0 );
#else
#ifndef UDS_LOCAL_PATH
#define UDS_LOCAL_PATH (72 | KIO::UDS_STRING)
#else
using namespace KIO;
#endif
KIO::UDSEntry e;
if( KIO::NetAccess::stat( url, e, 0 ) ) {
const KIO::UDSEntry::ConstIterator end = e.end();
for( KIO::UDSEntry::ConstIterator it = e.begin(); it != end; ++it ) {
if( (*it).m_uds == UDS_LOCAL_PATH && !(*it).m_str.isEmpty() )
return KURL::fromPathOrURL( (*it).m_str );
}
}
#endif
}
return url;
}
KURL::List K3b::convertToLocalUrls( const KURL::List& urls )
{
KURL::List r;
for( KURL::List::const_iterator it = urls.constBegin(); it != urls.constEnd(); ++it )
r.append( convertToLocalUrl( *it ) );
return r;
}
TQ_INT16 K3b::fromLe16( char* data )
{
#ifdef WORDS_BIGENDIAN // __BYTE_ORDER == __BIG_ENDIAN
return swapByteOrder( *((TQ_INT16*)data) );
#else
return *((TQ_INT16*)data);
#endif
}
TQ_INT32 K3b::fromLe32( char* data )
{
#ifdef WORDS_BIGENDIAN // __BYTE_ORDER == __BIG_ENDIAN
return swapByteOrder( *((TQ_INT32*)data) );
#else
return *((TQ_INT32*)data);
#endif
}
TQ_INT64 K3b::fromLe64( char* data )
{
#ifdef WORDS_BIGENDIAN // __BYTE_ORDER == __BIG_ENDIAN
return swapByteOrder( *((TQ_INT64*)data) );
#else
return *((TQ_INT64*)data);
#endif
}
TQString K3b::findExe( const TQString& name )
{
// first we search the path
TQString bin = KStandardDirs::findExe( name );
// then go on with our own little list
if( bin.isEmpty() )
bin = KStandardDirs::findExe( name, k3bcore->externalBinManager()->searchPath().join(":") );
return bin;
}
bool K3b::isMounted( K3bDevice::Device* dev )
{
if( !dev )
return false;
return !KIO::findDeviceMountPoint( dev->blockDeviceName() ).isEmpty();
}
bool K3b::unmount( K3bDevice::Device* dev )
{
if( !dev )
return false;
TQString mntDev = dev->blockDeviceName();
#if KDE_IS_VERSION(3,4,0)
// first try to unmount it the standard way
if( KIO::NetAccess::synchronousRun( KIO::unmount( mntDev, false ), 0 ) )
return true;
#endif
TQString umountBin = K3b::findExe( "umount" );
if( !umountBin.isEmpty() ) {
KProcess p;
p << umountBin;
p << "-l"; // lazy unmount
p << dev->blockDeviceName();
p.start( KProcess::Block );
if( !p.exitStatus() )
return true;
}
// now try pmount
TQString pumountBin = K3b::findExe( "pumount" );
if( !pumountBin.isEmpty() ) {
KProcess p;
p << pumountBin;
p << "-l"; // lazy unmount
p << dev->blockDeviceName();
p.start( KProcess::Block );
return !p.exitStatus();
}
else {
#ifdef HAVE_HAL
return !K3bDevice::HalConnection::instance()->unmount( dev );
#else
return false;
#endif
}
}
bool K3b::mount( K3bDevice::Device* dev )
{
if( !dev )
return false;
TQString mntDev = dev->blockDeviceName();
#if KDE_IS_VERSION(3,4,0)
// first try to mount it the standard way
if( KIO::NetAccess::synchronousRun( KIO::mount( true, 0, mntDev, false ), 0 ) )
return true;
#endif
#ifdef HAVE_HAL
if( !K3bDevice::HalConnection::instance()->mount( dev ) )
return true;
#endif
// now try pmount
TQString pmountBin = K3b::findExe( "pmount" );
if( !pmountBin.isEmpty() ) {
KProcess p;
p << pmountBin;
p << mntDev;
p.start( KProcess::Block );
return !p.exitStatus();
}
return false;
}
bool K3b::eject( K3bDevice::Device* dev )
{
#ifdef HAVE_HAL
if( !K3bDevice::HalConnection::instance()->eject( dev ) )
return true;
#endif
if( K3b::isMounted( dev ) )
K3b::unmount( dev );
return dev->eject();
}
|