1 /*
2     SPDX-FileCopyrightText: 1998-2009 Sebastian Trueg <trueg@k3b.org>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "k3btoc.h"
7 #include <QDebug>
8 #include <QString>
9 
10 
11 
Toc()12 K3b::Device::Toc::Toc()
13     : QList<K3b::Device::Track>()
14 {
15 }
16 
17 
Toc(const Toc & toc)18 K3b::Device::Toc::Toc( const Toc& toc )
19     : QList<K3b::Device::Track>( toc )
20 {
21     m_mcn = toc.m_mcn;
22 }
23 
24 
~Toc()25 K3b::Device::Toc::~Toc()
26 {
27 }
28 
29 
operator =(const Toc & toc)30 K3b::Device::Toc& K3b::Device::Toc::operator=( const Toc& toc )
31 {
32     if( &toc == this ) return *this;
33 
34     m_mcn = toc.m_mcn;
35 
36     QList<K3b::Device::Track>::operator=( toc );
37 
38     return *this;
39 }
40 
41 
firstSector() const42 K3b::Msf K3b::Device::Toc::firstSector() const
43 {
44     return isEmpty() ? K3b::Msf() : first().firstSector();
45 }
46 
47 
lastSector() const48 K3b::Msf K3b::Device::Toc::lastSector() const
49 {
50     if( isEmpty() )
51         return 0;
52     // the last track's last sector should be the last sector of the entire cd
53     return last().lastSector();
54 }
55 
56 
length() const57 K3b::Msf K3b::Device::Toc::length() const
58 {
59     // +1 since the last sector is included
60     return lastSector() - firstSector() + 1;
61 }
62 
63 
discId() const64 unsigned int K3b::Device::Toc::discId() const
65 {
66     // calculate cddb-id
67     unsigned int id = 0;
68     for( Toc::const_iterator it = constBegin(); it != constEnd(); ++it ) {
69         unsigned int n = (*it).firstSector().lba() + 150;
70         n /= 75;
71         while( n > 0 ) {
72             id += n % 10;
73             n /= 10;
74         }
75     }
76     unsigned int l = length().lba();
77     if ( !empty() )
78         l -= first().firstSector().lba();
79     l /= 75;
80     id = ( ( id % 0xff ) << 24 ) | ( l << 8 ) | count();
81 
82     return id;
83 }
84 
85 
contentType() const86 K3b::Device::ContentsType K3b::Device::Toc::contentType() const
87 {
88     int audioCnt = 0, dataCnt = 0;
89     for( Toc::const_iterator it = constBegin(); it != constEnd(); ++it ) {
90         if( (*it).type() == K3b::Device::Track::TYPE_AUDIO )
91             audioCnt++;
92         else
93             dataCnt++;
94     }
95 
96     if( audioCnt + dataCnt == 0 )
97         return K3b::Device::NONE;
98     if( audioCnt == 0 )
99         return K3b::Device::DATA;
100     if( dataCnt == 0 )
101         return K3b::Device::AUDIO;
102     return K3b::Device::MIXED;
103 }
104 
105 
sessions() const106 int K3b::Device::Toc::sessions() const
107 {
108     if( isEmpty() )
109         return 0;
110     else if( last().session() == 0 )
111         return 1; // default if unknown
112     else
113         return last().session();
114 }
115 
116 
mcn() const117 QByteArray K3b::Device::Toc::mcn() const
118 {
119     return m_mcn;
120 }
121 
122 
setMcn(const QByteArray & mcn)123 void K3b::Device::Toc::setMcn( const QByteArray& mcn )
124 {
125     m_mcn = mcn;
126 }
127 
128 
clear()129 void K3b::Device::Toc::clear()
130 {
131     QList<Track>::clear();
132     m_mcn.resize( 0 );
133 }
134 
135 
operator ==(const Toc & other) const136 bool K3b::Device::Toc::operator==( const Toc& other ) const
137 {
138     return( QList<Track>::operator==( other ) );
139 }
140 
141 
operator !=(const Toc & other) const142 bool K3b::Device::Toc::operator!=( const Toc& other ) const
143 {
144     return( QList<Track>::operator!=( other ) );
145 }
146 
147 
operator <<(QDebug s,const K3b::Device::Toc & toc)148 QDebug operator<<( QDebug s, const K3b::Device::Toc& toc )
149 {
150     s.nospace() << toc.count() << " in " << toc.sessions() << " sessions";
151     int sessionN = 0;
152     int trackN = 0;
153     for( K3b::Device::Toc::const_iterator it = toc.constBegin(); it != toc.constEnd(); ++it ) {
154         ++trackN;
155         if( sessionN != it->session() ) {
156             sessionN = it->session();
157             s.nospace() << "Session Number " << sessionN;
158         }
159         s.nospace() << "  Track " << trackN << *it;
160     }
161     return s;
162 }
163