File indexing completed on 2024-04-28 04:50:21

0001 /*
0002     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "k3bcuefilewriter.h"
0007 
0008 #include "k3btrack.h"
0009 #include "k3bmsf.h"
0010 #include "k3bcore.h"
0011 #include "k3bversion.h"
0012 
0013 #include <QDateTime>
0014 #include <QFile>
0015 
0016 
0017 K3b::CueFileWriter::CueFileWriter()
0018 {
0019 }
0020 
0021 
0022 bool K3b::CueFileWriter::save( const QString& filename )
0023 {
0024     QFile f( filename );
0025 
0026     if( !f.open( QIODevice::WriteOnly ) ) {
0027         qDebug() << "(K3b::CueFileWriter) could not open file " << f.fileName();
0028         return false;
0029     }
0030 
0031     QTextStream s( &f );
0032 
0033     return save( s );
0034 }
0035 
0036 
0037 bool K3b::CueFileWriter::save( QTextStream& t )
0038 {
0039     t << "REM Cue file written by K3b " << k3bcore->version() << Qt::endl
0040       << Qt::endl;
0041 
0042     if( !m_cdText.isEmpty() ) {
0043         t << "PERFORMER \"" << m_cdText.performer() << "\"" << Qt::endl;
0044         t << "TITLE \"" << m_cdText.title() << "\"" << Qt::endl;
0045     }
0046 
0047     t << "FILE \"" << m_image << "\" " << m_dataType.toUpper() << Qt::endl;
0048 
0049     // the tracks
0050     int i = 0;
0051     for( K3b::Device::Toc::const_iterator it = m_toc.constBegin();
0052          it != m_toc.constEnd(); ++it ) {
0053 
0054         const K3b::Device::Track& track = *it;
0055 
0056         t << "  TRACK " << QString::number(i+1).rightJustified( 2, '0' ) << " AUDIO" << Qt::endl;
0057 
0058         if( m_cdText.count() > i && !m_cdText[i].isEmpty() ) {
0059             t << "    PERFORMER \"" << m_cdText[i].performer() << "\"" << Qt::endl;
0060             t << "    TITLE \"" << m_cdText[i].title() << "\"" << Qt::endl;
0061         }
0062 
0063         //
0064         // the pregap is part of the current track like in toc files
0065         // and not part of the last track as on the CD
0066         //
0067         if( i > 0 ) {
0068             --it;
0069             if( (*it).index0() > 0 )
0070                 t << "    INDEX 00 " << ((*it).firstSector() + (*it).index0()).toString() << Qt::endl;
0071             ++it;
0072         }
0073         t << "    INDEX 01 " << track.firstSector().toString() << Qt::endl;
0074         // TODO: add additional indices
0075 
0076         i++;
0077     }
0078 
0079     return ( t.status() == QTextStream::Ok );
0080 }