File indexing completed on 2024-04-21 04:49:42

0001 /*
0002     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 #include "k3bdeviceglobals.h"
0006 #include "k3bdevice.h"
0007 #include "k3bdevice_i18n.h"
0008 #include "k3bdiskinfo.h"
0009 
0010 #include <QDebug>
0011 #include <QStringList>
0012 
0013 
0014 QString K3b::Device::deviceTypeString( int t )
0015 {
0016     QStringList s;
0017     if( t & K3b::Device::DEVICE_CD_R )
0018         s += i18n("CD-R");
0019     if( t & K3b::Device::DEVICE_CD_RW )
0020         s += i18n("CD-RW");
0021     if( t & K3b::Device::DEVICE_CD_ROM )
0022         s += i18n("CD-ROM");
0023     if( t & K3b::Device::DEVICE_DVD_ROM )
0024         s += i18n("DVD-ROM");
0025     if( t & K3b::Device::DEVICE_DVD_RAM )
0026         s += i18n("DVD-RAM");
0027     if( t & K3b::Device::DEVICE_DVD_R )
0028         s += i18n("DVD-R");
0029     if( t & K3b::Device::DEVICE_DVD_RW )
0030         s += i18n("DVD-RW");
0031     if( t & K3b::Device::DEVICE_DVD_R_DL )
0032         s += i18n("DVD-R DL");
0033     if( t & DEVICE_HD_DVD_ROM )
0034         s += i18n("HD DVD-ROM");
0035     if( t & DEVICE_HD_DVD_R )
0036         s += i18n("HD DVD-R");
0037     if( t & DEVICE_HD_DVD_RAM )
0038         s += i18n("HD DVD-RAM");
0039     if( t & DEVICE_BD_ROM )
0040         s += i18n("BD-ROM");
0041     if( t & DEVICE_BD_R )
0042         s += i18n("BD-R");
0043     if( t & DEVICE_BD_RE )
0044         s += i18n("BD-RE");
0045     if( t & K3b::Device::DEVICE_DVD_PLUS_R )
0046         s += i18n("DVD+R");
0047     if( t & K3b::Device::DEVICE_DVD_PLUS_RW )
0048         s += i18n("DVD+RW");
0049     if( t & K3b::Device::DEVICE_DVD_PLUS_R_DL )
0050         s += i18n("DVD+R DL");
0051 
0052     if( s.isEmpty() )
0053         return i18n("Error");
0054     else
0055         return s.join( ", " );
0056 }
0057 
0058 
0059 QString K3b::Device::writingModeString( int m )
0060 {
0061     QStringList s;
0062     if( m & K3b::Device::WRITINGMODE_SAO )
0063         s += i18n("SAO");
0064     if( m & K3b::Device::WRITINGMODE_TAO )
0065         s += i18n("TAO");
0066     if( m & K3b::Device::WRITINGMODE_RAW )
0067         s += i18n("RAW");
0068     if( m & K3b::Device::WRITINGMODE_SAO_R96P )
0069         s += i18n("SAO/R96P");
0070     if( m & K3b::Device::WRITINGMODE_SAO_R96R )
0071         s += i18n("SAO/R96R");
0072     if( m & K3b::Device::WRITINGMODE_RAW_R16 )
0073         s += i18n("RAW/R16");
0074     if( m & K3b::Device::WRITINGMODE_RAW_R96P )
0075         s += i18n("RAW/R96P");
0076     if( m & K3b::Device::WRITINGMODE_RAW_R96R )
0077         s += i18n("RAW/R96R");
0078     if( m & K3b::Device::WRITINGMODE_INCR_SEQ )
0079         s += i18n("Incremental Sequential");
0080     if( m & K3b::Device::WRITINGMODE_RES_OVWR )
0081         s += i18n("Restricted Overwrite");
0082     if( m & K3b::Device::WRITINGMODE_LAYER_JUMP )
0083         s += i18n("Layer Jump");
0084 
0085     if( m & K3b::Device::WRITINGMODE_RRM )
0086         s += i18n("Random Recording");
0087     if( m & K3b::Device::WRITINGMODE_SRM )
0088         s += i18n("Sequential Recording");
0089     if( m & K3b::Device::WRITINGMODE_SRM_POW )
0090         s += i18n("Sequential Recording + POW");
0091 
0092     if( s.isEmpty() )
0093         return i18nc("no writing mode", "None");
0094     else
0095         return s.join( ", " );
0096 }
0097 
0098 
0099 QString K3b::Device::mediaTypeString( int m, bool simple )
0100 {
0101     if( m == K3b::Device::MEDIA_UNKNOWN )
0102         return i18nc("unknown medium type", "Unknown");
0103 
0104     QStringList s;
0105     if( m & MEDIA_NONE )
0106         s += i18n("No media");
0107     if( m & MEDIA_DVD_ROM )
0108         s += i18n("DVD-ROM");
0109     if( m & MEDIA_DVD_R ||
0110         (simple && (m & MEDIA_DVD_R_SEQ)) )
0111         s += i18n("DVD-R");
0112     if( m & MEDIA_DVD_R_SEQ && !simple )
0113         s += i18n("DVD-R Sequential");
0114     if( m & MEDIA_DVD_R_DL ||
0115         (simple && (m & (MEDIA_DVD_R_DL_SEQ|MEDIA_DVD_R_DL_JUMP))) )
0116         s += i18n("DVD-R Dual Layer");
0117     if( m & MEDIA_DVD_R_DL_SEQ && !simple )
0118         s += i18n("DVD-R Dual Layer Sequential");
0119     if( m & MEDIA_DVD_R_DL_JUMP && !simple )
0120         s += i18n("DVD-R Dual Layer Jump");
0121     if( m & MEDIA_DVD_RAM )
0122         s += i18n("DVD-RAM");
0123     if( m & MEDIA_DVD_RW ||
0124         (simple && (m & (MEDIA_DVD_RW_OVWR|MEDIA_DVD_RW_SEQ))) )
0125         s += i18n("DVD-RW");
0126     if( m & MEDIA_DVD_RW_OVWR && !simple )
0127         s += i18n("DVD-RW Restricted Overwrite");
0128     if( m & MEDIA_DVD_RW_SEQ && !simple )
0129         s += i18n("DVD-RW Sequential");
0130     if( m & MEDIA_DVD_PLUS_RW )
0131         s += i18n("DVD+RW");
0132     if( m & MEDIA_DVD_PLUS_R )
0133         s += i18n("DVD+R");
0134     if( m & MEDIA_DVD_PLUS_RW_DL )
0135         s += i18n("DVD+RW Dual Layer");
0136     if( m & MEDIA_DVD_PLUS_R_DL )
0137         s += i18n("DVD+R Dual Layer");
0138     if( m & MEDIA_CD_ROM )
0139         s += i18n("CD-ROM");
0140     if( m & MEDIA_CD_R )
0141         s += i18n("CD-R");
0142     if( m & MEDIA_CD_RW )
0143         s += i18n("CD-RW");
0144     if( m & MEDIA_HD_DVD_ROM )
0145         s += i18n("HD DVD-ROM");
0146     if( m & MEDIA_HD_DVD_R )
0147         s += i18n("HD DVD-R");
0148     if( m & MEDIA_HD_DVD_RAM )
0149         s += i18n("HD DVD-RAM");
0150     if( m & MEDIA_BD_ROM )
0151         s += i18n("BD-ROM");
0152     if( m & MEDIA_BD_R ||
0153         (simple && (m & (MEDIA_BD_R_SRM|MEDIA_BD_R_RRM))) )
0154         s += i18n("BD-R");
0155     if( m & MEDIA_BD_R_SRM && !simple )
0156         s += i18n("BD-R Sequential (SRM)");
0157     if( m & MEDIA_BD_R_SRM_POW && !simple )
0158         s += i18n("BD-R Sequential Pseudo Overwrite (SRM+POW)");
0159     if( m & MEDIA_BD_R_RRM && !simple )
0160         s += i18n("BD-R Random (RRM)");
0161     if( m & MEDIA_BD_RE )
0162         s += i18n("BD-RE");
0163 
0164     if( s.isEmpty() )
0165         return i18n("Error");
0166     else
0167         return s.join( ", " );
0168 }
0169 
0170 
0171 QString K3b::Device::mediaStateString( int state )
0172 {
0173     if( state == K3b::Device::STATE_UNKNOWN )
0174         return i18nc("unknown medium state", "Unknown");
0175 
0176     QStringList s;
0177     if( state & STATE_NO_MEDIA )
0178         s += i18n("no medium");
0179     if( state & STATE_COMPLETE )
0180         s += i18n("complete medium");
0181     if( state & STATE_INCOMPLETE )
0182         s += i18n("incomplete medium");
0183     if( state & STATE_EMPTY )
0184         s += i18n("empty medium");
0185 
0186     if( s.isEmpty() )
0187         return i18n("Error");
0188     else
0189         return s.join( ", " );
0190 }
0191 
0192 
0193 void K3b::Device::debugBitfield( unsigned char* data, long len )
0194 {
0195     for( int i = 0; i < len; ++i ) {
0196         QString index, bitString;
0197         index = QString::asprintf( "%4i", i );
0198         for( int bp = 7; bp >= 0; --bp )
0199             bitString[7-bp] = ( data[i] & (1<<bp) ? '1' : '0' );
0200         qDebug() << index << " - " << bitString << " - " << (int)data[i];
0201     }
0202 }
0203 
0204 
0205 quint16 K3b::Device::from2Byte(const unsigned char* d)
0206 {
0207     if (d == NULL) {
0208         qWarning() << "Invalid Byte!";
0209         return 0;
0210     }
0211     return ((d[0] << 8 & 0xFF00) |
0212             (d[1]      & 0xFF));
0213 }
0214 
0215 
0216 quint32 K3b::Device::from4Byte(const unsigned char* d)
0217 {
0218     if (d == NULL) {
0219         qWarning() << "Invalid Byte!";
0220         return 0;
0221     }
0222     return ((d[0] << 24 & 0xFF000000) |
0223             (d[1] << 16 & 0xFF0000)   |
0224             (d[2] << 8  & 0xFF00)     |
0225             (d[3]       & 0xFF));
0226 }
0227 
0228 
0229 char K3b::Device::fromBcd( const char& i )
0230 {
0231     return (i & 0x0f) + 10 * ( (i >> 4) & 0x0f );
0232 }
0233 
0234 
0235 char K3b::Device::toBcd( const char& i )
0236 {
0237     return ( i % 10 ) | ( ( (( i / 10 ) % 10) << 4 ) & 0xf0 );
0238 }
0239 
0240 
0241 bool K3b::Device::isValidBcd( const char& i )
0242 {
0243     return ( i & 0x0f ) <= 0x09 && ( i & 0xf0 ) <= 0x90;
0244 }
0245 
0246 
0247 int K3b::Device::determineMaxReadingBufferSize( K3b::Device::Device* dev, const K3b::Msf& firstSector )
0248 {
0249     //
0250     // As long as we do not know how to determine the max read buffer properly we simply determine it
0251     // by trying. :)
0252     //
0253 
0254     int bufferSizeSectors = 128;
0255     unsigned char buffer[2048*128];
0256     while( !dev->read10( buffer, 2048*bufferSizeSectors, firstSector.lba(), bufferSizeSectors ) ) {
0257         qDebug() << "(K3b::DataTrackReader) determine max read sectors: "
0258                  << bufferSizeSectors << " too high." << Qt::endl;
0259         bufferSizeSectors--;
0260     }
0261     qDebug() << "(K3b::DataTrackReader) determine max read sectors: "
0262              << bufferSizeSectors << " is max." << Qt::endl;
0263 
0264     return bufferSizeSectors;
0265 }
0266 
0267 
0268 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaType type )
0269 {
0270     return dbg << mediaTypeString( type );
0271 }
0272 
0273 
0274 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaTypes types )
0275 {
0276     return dbg << mediaTypeString( types );
0277 }
0278 
0279 
0280 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::WritingMode mode )
0281 {
0282     return dbg << writingModeString( mode );
0283 }
0284 
0285 
0286 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::WritingModes modes )
0287 {
0288     return dbg << writingModeString( modes );
0289 }
0290 
0291 
0292 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaState state )
0293 {
0294     return dbg << mediaStateString( state );
0295 }
0296 
0297 
0298 QDebug& K3b::Device::operator<<( QDebug& dbg, K3b::Device::MediaStates states )
0299 {
0300     return dbg << mediaStateString( states );
0301 }