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

0001 /*
0002     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3btoc.h"
0007 #include <QDebug>
0008 #include <QString>
0009 
0010 
0011 
0012 K3b::Device::Toc::Toc()
0013     : QList<K3b::Device::Track>()
0014 {
0015 }
0016 
0017 
0018 K3b::Device::Toc::Toc( const Toc& toc )
0019     : QList<K3b::Device::Track>( toc )
0020 {
0021     m_mcn = toc.m_mcn;
0022 }
0023 
0024 
0025 K3b::Device::Toc::~Toc()
0026 {
0027 }
0028 
0029 
0030 K3b::Device::Toc& K3b::Device::Toc::operator=( const Toc& toc )
0031 {
0032     if( &toc == this ) return *this;
0033 
0034     m_mcn = toc.m_mcn;
0035 
0036     QList<K3b::Device::Track>::operator=( toc );
0037 
0038     return *this;
0039 }
0040 
0041 
0042 K3b::Msf K3b::Device::Toc::firstSector() const
0043 {
0044     return isEmpty() ? K3b::Msf() : first().firstSector();
0045 }
0046 
0047 
0048 K3b::Msf K3b::Device::Toc::lastSector() const
0049 {
0050     if( isEmpty() )
0051         return 0;
0052     // the last track's last sector should be the last sector of the entire cd
0053     return last().lastSector();
0054 }
0055 
0056 
0057 K3b::Msf K3b::Device::Toc::length() const
0058 {
0059     // +1 since the last sector is included
0060     return lastSector() - firstSector() + 1;
0061 }
0062 
0063 
0064 unsigned int K3b::Device::Toc::discId() const
0065 {
0066     // calculate cddb-id
0067     unsigned int id = 0;
0068     for( Toc::const_iterator it = constBegin(); it != constEnd(); ++it ) {
0069         unsigned int n = (*it).firstSector().lba() + 150;
0070         n /= 75;
0071         while( n > 0 ) {
0072             id += n % 10;
0073             n /= 10;
0074         }
0075     }
0076     unsigned int l = length().lba();
0077     if ( !empty() )
0078         l -= first().firstSector().lba();
0079     l /= 75;
0080     id = ( ( id % 0xff ) << 24 ) | ( l << 8 ) | count();
0081 
0082     return id;
0083 }
0084 
0085 
0086 K3b::Device::ContentsType K3b::Device::Toc::contentType() const
0087 {
0088     int audioCnt = 0, dataCnt = 0;
0089     for( Toc::const_iterator it = constBegin(); it != constEnd(); ++it ) {
0090         if( (*it).type() == K3b::Device::Track::TYPE_AUDIO )
0091             audioCnt++;
0092         else
0093             dataCnt++;
0094     }
0095 
0096     if( audioCnt + dataCnt == 0 )
0097         return K3b::Device::NONE;
0098     if( audioCnt == 0 )
0099         return K3b::Device::DATA;
0100     if( dataCnt == 0 )
0101         return K3b::Device::AUDIO;
0102     return K3b::Device::MIXED;
0103 }
0104 
0105 
0106 int K3b::Device::Toc::sessions() const
0107 {
0108     if( isEmpty() )
0109         return 0;
0110     else if( last().session() == 0 )
0111         return 1; // default if unknown
0112     else
0113         return last().session();
0114 }
0115 
0116 
0117 QByteArray K3b::Device::Toc::mcn() const
0118 {
0119     return m_mcn;
0120 }
0121 
0122 
0123 void K3b::Device::Toc::setMcn( const QByteArray& mcn )
0124 {
0125     m_mcn = mcn;
0126 }
0127 
0128 
0129 void K3b::Device::Toc::clear()
0130 {
0131     QList<Track>::clear();
0132     m_mcn.resize( 0 );
0133 }
0134 
0135 
0136 bool K3b::Device::Toc::operator==( const Toc& other ) const
0137 {
0138     return( QList<Track>::operator==( other ) );
0139 }
0140 
0141 
0142 bool K3b::Device::Toc::operator!=( const Toc& other ) const
0143 {
0144     return( QList<Track>::operator!=( other ) );
0145 }
0146 
0147 
0148 QDebug operator<<( QDebug s, const K3b::Device::Toc& toc )
0149 {
0150     s.nospace() << toc.count() << " in " << toc.sessions() << " sessions";
0151     int sessionN = 0;
0152     int trackN = 0;
0153     for( K3b::Device::Toc::const_iterator it = toc.constBegin(); it != toc.constEnd(); ++it ) {
0154         ++trackN;
0155         if( sessionN != it->session() ) {
0156             sessionN = it->session();
0157             s.nospace() << "Session Number " << sessionN;
0158         }
0159         s.nospace() << "  Track " << trackN << *it;
0160     }
0161     return s;
0162 }