File indexing completed on 2024-06-16 04:33:42

0001 /*
0002     SPDX-FileCopyrightText: 2010 Michal Malek <michalm@jabster.pl>
0003     SPDX-FileCopyrightText: 1998-2008 Sebastian Trueg <trueg@k3b.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "k3baudioencoder.h"
0009 #include "k3b_i18n.h"
0010 
0011 #include <QDebug>
0012 #include <QFile>
0013 
0014 
0015 class K3b::AudioEncoder::Private
0016 {
0017 public:
0018     Private()
0019         : outputFile(0) {
0020     }
0021 
0022     QFile* outputFile;
0023     QString outputFilename;
0024 
0025     QString lastErrorString;
0026 };
0027 
0028 
0029 K3b::AudioEncoder::AudioEncoder( QObject* parent )
0030     : K3b::Plugin( parent )
0031 {
0032     d = new Private();
0033 }
0034 
0035 
0036 K3b::AudioEncoder::~AudioEncoder()
0037 {
0038     closeFile();
0039     delete d;
0040 }
0041 
0042 
0043 QString K3b::AudioEncoder::categoryName() const
0044 {
0045     return i18nc( "plugin type", "Audio Encoder" );
0046 }
0047 
0048 
0049 bool K3b::AudioEncoder::openFile( const QString& extension, const QString& filename, const K3b::Msf& length, const MetaData& metaData )
0050 {
0051     closeFile();
0052 
0053     d->outputFile = new QFile( filename );
0054     if( d->outputFile->open( QIODevice::WriteOnly ) ) {
0055         return initEncoder( extension, length, metaData );
0056     }
0057     else {
0058         qDebug() << "(K3b::AudioEncoder) unable to open file " << filename;
0059         closeFile();
0060         return false;
0061     }
0062 }
0063 
0064 
0065 bool K3b::AudioEncoder::isOpen() const
0066 {
0067     if( d->outputFile )
0068         return d->outputFile->isOpen();
0069     else
0070         return false;
0071 }
0072 
0073 
0074 void K3b::AudioEncoder::closeFile()
0075 {
0076     if( d->outputFile ) {
0077         finishEncoder();
0078         if( d->outputFile->isOpen() )
0079             d->outputFile->close();
0080         delete d->outputFile;
0081         d->outputFile = 0;
0082         d->outputFilename = QString();
0083     }
0084 }
0085 
0086 
0087 QString K3b::AudioEncoder::filename() const
0088 {
0089     if( d->outputFile )
0090         return d->outputFilename;
0091     else
0092         return QString();
0093 }
0094 
0095 
0096 qint64 K3b::AudioEncoder::encode( const char* data, qint64 len )
0097 {
0098     return encodeInternal( data, len );
0099 }
0100 
0101 
0102 bool K3b::AudioEncoder::initEncoder( const QString& extension, const K3b::Msf& length, const MetaData& metaData )
0103 {
0104     if( !isOpen() ) {
0105         qDebug() << "(K3b::AudioEncoder) call to initEncoder without openFile!";
0106         return false;
0107     }
0108 
0109     return initEncoderInternal( extension, length, metaData );
0110 }
0111 
0112 
0113 qint64 K3b::AudioEncoder::writeData( const char* data, qint64 len )
0114 {
0115     if( d->outputFile ) {
0116         return d->outputFile->write( data, len );
0117     }
0118     else {
0119         qDebug() << "(K3b::AudioEncoder) call to writeData without opening a file first.";
0120         return -1;
0121     }
0122 }
0123 
0124 
0125 bool K3b::AudioEncoder::initEncoderInternal( const QString& /*extension*/, const K3b::Msf& /*length*/, const MetaData& /*metaData*/ )
0126 {
0127     // do nothing
0128     return true;
0129 }
0130 
0131 
0132 void K3b::AudioEncoder::finishEncoder()
0133 {
0134     if( isOpen() )
0135         finishEncoderInternal();
0136 }
0137 
0138 
0139 void K3b::AudioEncoder::finishEncoderInternal()
0140 {
0141     // do nothing
0142 }
0143 
0144 
0145 void K3b::AudioEncoder::setLastError( const QString& e )
0146 {
0147     d->lastErrorString = e;
0148 }
0149 
0150 
0151 QString K3b::AudioEncoder::lastErrorString() const
0152 {
0153     if( d->lastErrorString.isEmpty() )
0154         return i18n("An unknown error occurred.");
0155     else
0156         return d->lastErrorString;
0157 }
0158 
0159 #include "moc_k3baudioencoder.cpp"