MultimediaProject: splitted constructor for loading either file or data

The old constructor treated the string argument either as filename or
as raw XML data, depending on 2nd parameter. This is a mess and quickly
leads to confusion.

Now we have two constructors taking either a filename as string or a
QByteArray with XML data. Loading actual data has been separated into
MultimediaProject::loadData( const QByteArray & ).

Signed-off-by: Tobias Doerffel <tobias.doerffel@gmail.com>
This commit is contained in:
Tobias Doerffel
2009-06-05 00:55:25 +02:00
parent eb77b99220
commit 5a00ebd360
7 changed files with 69 additions and 94 deletions

View File

@@ -1,7 +1,7 @@
/*
* mmp.h - class for reading and writing multimedia-project-files
*
* Copyright (c) 2004-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
@@ -53,9 +53,8 @@ public:
} ;
multimediaProject( const QString & _in_file_name,
bool _is_filename = TRUE,
bool _upgrade = TRUE );
multimediaProject( const QString & _fileName );
multimediaProject( const QByteArray & _data );
multimediaProject( ProjectTypes _project_type );
virtual ~multimediaProject();
@@ -77,8 +76,6 @@ public:
return m_type;
}
static ProjectTypes typeOfFile( const QString & _fn );
private:
static ProjectTypes type( const QString & _type_name );
@@ -88,6 +85,8 @@ private:
void upgrade( void );
void loadData( const QByteArray & _data, const QString & _sourceFile );
struct EXPORT typeDescStruct
{

View File

@@ -1,7 +1,7 @@
/*
* audio_file_processor.cpp - instrument for using audio-files
*
* Copyright (c) 2004-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
@@ -424,7 +424,7 @@ void audioFileProcessorView::dropEvent( QDropEvent * _de )
}
else if( type == QString( "tco_%1" ).arg( track::SampleTrack ) )
{
multimediaProject mmp( value, FALSE );
multimediaProject mmp( value.toUtf8() );
castModel<audioFileProcessor>()->setAudioFile( mmp.content().
firstChild().toElement().attribute( "src" ) );
_de->accept();

View File

@@ -1,9 +1,7 @@
#ifndef SINGLE_SOURCE_COMPILE
/*
* mmp.cpp - implementation of class multimediaProject
*
* Copyright (c) 2004-2008 Tobias Doerffel <tobydox/at/users.sourceforge.net>
* Copyright (c) 2004-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
*
* This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net
*
@@ -29,6 +27,7 @@
#include <math.h>
#include <QtCore/QDebug>
#include <QtCore/QFile>
#include <QtCore/QTextStream>
#include <QtGui/QMessageBox>
@@ -86,85 +85,43 @@ multimediaProject::multimediaProject( ProjectTypes _project_type ) :
multimediaProject::multimediaProject( const QString & _in_file_name,
bool _is_filename,
bool _upgrade ) :
multimediaProject::multimediaProject( const QString & _fileName ) :
QDomDocument(),
m_content(),
m_head()
{
QFile in_file( _in_file_name );
if( _is_filename == true )
QFile inFile( _fileName );
if( !inFile.open( QIODevice::ReadOnly ) )
{
if( !in_file.open( QIODevice::ReadOnly ) )
{
QMessageBox::critical( NULL,
songEditor::tr( "Could not open file" ),
songEditor::tr( "Could not open "
"file %1. You probably "
"have no rights to "
"read this file.\n"
"Please make sure you "
"have at least read-"
"access to the file "
"and try again."
).arg( _in_file_name ) );
QMessageBox::critical( NULL,
songEditor::tr( "Could not open file" ),
songEditor::tr( "Could not open file %1. You probably "
"have no permissions to read this "
"file.\n Please make sure to have at "
"least read permissions to the file "
"and try again." ).arg( _fileName ) );
return;
}
}
QString error_msg;
int line;
int col;
if( _is_filename == true )
if( _fileName.section( '.', -1 ) == "mmpz" )
{
bool error = false;
if( _in_file_name.section( '.', -1 ) == "mmpz" )
{
QString data = qUncompress( in_file.readAll() );
error = !setContent( data, &error_msg, &line, &col );
}
else
{
error = !setContent( &in_file, &error_msg, &line,
&col );
}
if( error )
{
printf( "at line %d column %d: %s\n", line, col,
error_msg.toAscii().constData() );
QMessageBox::critical( NULL, songEditor::tr( "Error in "
"multimedia-project" ),
songEditor::tr( "The multimedia-"
"project %1 seems to contain errors. LMMS "
"will try its best to recover as much "
"data as possible data from this file."
).arg( _in_file_name ) );
return;
}
in_file.close();
loadData( qUncompress( inFile.readAll() ), _fileName );
}
else
{
if( !setContent( _in_file_name, &error_msg, &line, &col ) )
{
printf( "multimediaProject: error parsing XML-data "
"directly given to constructor!\n" );
return;
}
loadData( inFile.readAll(), _fileName );
}
}
QDomElement root = documentElement();
m_type = type( root.attribute( "type" ) );
m_head = root.elementsByTagName( "head" ).item( 0 ).toElement();
if( _upgrade && root.hasAttribute( "creatorversion" ) &&
root.attribute( "creatorversion" ) != LMMS_VERSION )
{
upgrade();
}
m_content = root.elementsByTagName( typeName( m_type ) ).
item( 0 ).toElement();
multimediaProject::multimediaProject( const QByteArray & _data ) :
QDomDocument(),
m_content(),
m_head()
{
loadData( _data, "<internal data>" );
}
@@ -257,16 +214,6 @@ bool multimediaProject::writeFile( const QString & _fn )
multimediaProject::ProjectTypes multimediaProject::typeOfFile(
const QString & _fn )
{
multimediaProject m( _fn, true, false );
return m.type();
}
multimediaProject::ProjectTypes multimediaProject::type(
const QString & _type_name )
{
@@ -736,4 +683,32 @@ void multimediaProject::upgrade( void )
#endif
void multimediaProject::loadData( const QByteArray & _data,
const QString & _sourceFile )
{
QString errorMsg;
int line, col;
if( !setContent( _data, &errorMsg, &line, &col ) )
{
qWarning() << "at line" << line << "column" << errorMsg;
QMessageBox::critical( NULL, songEditor::tr( "Error in file" ),
songEditor::tr( "The file %1 seems to contain errors "
"and therefore can't be loaded." ).
arg( _sourceFile ) );
return;
}
QDomElement root = documentElement();
m_type = type( root.attribute( "type" ) );
m_head = root.elementsByTagName( "head" ).item( 0 ).toElement();
if( root.hasAttribute( "creatorversion" ) &&
root.attribute( "creatorversion" ) != LMMS_VERSION )
{
upgrade();
}
m_content = root.elementsByTagName( typeName( m_type ) ).
item( 0 ).toElement();
}

View File

@@ -488,7 +488,7 @@ void trackContentObjectView::dropEvent( QDropEvent * _de )
{
// value contains our XML-data so simply create a
// multimediaProject which does the rest for us...
multimediaProject mmp( value, false );
multimediaProject mmp( value.toUtf8() );
// at least save position before getting to moved to somewhere
// the user doesn't expect...
midiTime pos = m_tco->startPosition();
@@ -1084,7 +1084,7 @@ void trackContentWidget::dropEvent( QDropEvent * _de )
// value contains our XML-data so simply create a
// multimediaProject which does the rest for us...
multimediaProject mmp( value, false );
multimediaProject mmp( value.toUtf8() );
// at least save position before getting moved to somewhere
// the user doesn't expect...
tco->restoreState( mmp.content().firstChild().toElement() );
@@ -1219,7 +1219,8 @@ void trackContentWidget::undoStep( journalEntry & _je )
trackContentObject * tco = getTrack()->createTCO(
midiTime( 0 ) );
multimediaProject mmp(
_je.data().toMap()["state"].toString(), false );
_je.data().toMap()["state"].
toString().toUtf8() );
tco->restoreState(
mmp.content().firstChild().toElement() );
break;
@@ -2255,7 +2256,7 @@ void trackView::dropEvent( QDropEvent * _de )
{
// value contains our XML-data so simply create a
// multimediaProject which does the rest for us...
multimediaProject mmp( value, false );
multimediaProject mmp( value.toUtf8() );
engine::getMixer()->lock();
m_track->restoreState( mmp.content().firstChild().toElement() );
engine::getMixer()->unlock();

View File

@@ -3386,7 +3386,7 @@ void pianoRoll::pasteNotes( void )
if( !value.isEmpty() )
{
multimediaProject mmp( value, false );
multimediaProject mmp( value.toUtf8() );
QDomNodeList list = mmp.elementsByTagName(
note::classNodeName() );

View File

@@ -327,7 +327,7 @@ void trackContainerView::undoStep( journalEntry & _je )
case RemoveTrack:
{
multimediaProject mmp(
_je.data().toMap()["state"].toString(), false );
_je.data().toMap()["state"].toString().utf8() );
track::create( mmp.content().firstChild().toElement(),
m_tc );
break;
@@ -415,7 +415,7 @@ void trackContainerView::dropEvent( QDropEvent * _de )
}
else if( type.left( 6 ) == "track_" )
{
multimediaProject mmp( value, false );
multimediaProject mmp( value.toUtf8() );
track::create( mmp.content().firstChild().toElement(), m_tc );
_de->accept();
}

View File

@@ -397,7 +397,7 @@ void envelopeAndLFOView::dropEvent( QDropEvent * _de )
}
else if( type == QString( "tco_%1" ).arg( track::SampleTrack ) )
{
multimediaProject mmp( value, FALSE );
multimediaProject mmp( value.toUtf8() );
m_params->m_userWave.setAudioFile(
mmp.content().firstChild().toElement().
attribute( "src" ) );