From 5a00ebd360be65cc88561ab89406ce9ae5893cc6 Mon Sep 17 00:00:00 2001 From: Tobias Doerffel Date: Fri, 5 Jun 2009 00:55:25 +0200 Subject: [PATCH] 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 --- include/mmp.h | 11 +- .../audio_file_processor.cpp | 4 +- src/core/mmp.cpp | 131 +++++++----------- src/core/track.cpp | 9 +- src/gui/piano_roll.cpp | 2 +- src/gui/track_container_view.cpp | 4 +- src/gui/widgets/envelope_and_lfo_view.cpp | 2 +- 7 files changed, 69 insertions(+), 94 deletions(-) diff --git a/include/mmp.h b/include/mmp.h index aecee6323..dbd3d47b1 100644 --- a/include/mmp.h +++ b/include/mmp.h @@ -1,7 +1,7 @@ /* * mmp.h - class for reading and writing multimedia-project-files * - * Copyright (c) 2004-2008 Tobias Doerffel + * Copyright (c) 2004-2009 Tobias Doerffel * * 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 { diff --git a/plugins/audio_file_processor/audio_file_processor.cpp b/plugins/audio_file_processor/audio_file_processor.cpp index 1e787979a..72388f594 100644 --- a/plugins/audio_file_processor/audio_file_processor.cpp +++ b/plugins/audio_file_processor/audio_file_processor.cpp @@ -1,7 +1,7 @@ /* * audio_file_processor.cpp - instrument for using audio-files * - * Copyright (c) 2004-2008 Tobias Doerffel + * Copyright (c) 2004-2009 Tobias Doerffel * * 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()->setAudioFile( mmp.content(). firstChild().toElement().attribute( "src" ) ); _de->accept(); diff --git a/src/core/mmp.cpp b/src/core/mmp.cpp index 0b267c05e..f0d97f910 100644 --- a/src/core/mmp.cpp +++ b/src/core/mmp.cpp @@ -1,9 +1,7 @@ -#ifndef SINGLE_SOURCE_COMPILE - /* * mmp.cpp - implementation of class multimediaProject * - * Copyright (c) 2004-2008 Tobias Doerffel + * Copyright (c) 2004-2009 Tobias Doerffel * * This file is part of Linux MultiMedia Studio - http://lmms.sourceforge.net * @@ -29,6 +27,7 @@ #include +#include #include #include #include @@ -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, "" ); } @@ -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(); +} + diff --git a/src/core/track.cpp b/src/core/track.cpp index aa14b7de3..2335d4829 100644 --- a/src/core/track.cpp +++ b/src/core/track.cpp @@ -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(); diff --git a/src/gui/piano_roll.cpp b/src/gui/piano_roll.cpp index 2033231ce..4ca8039b1 100644 --- a/src/gui/piano_roll.cpp +++ b/src/gui/piano_roll.cpp @@ -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() ); diff --git a/src/gui/track_container_view.cpp b/src/gui/track_container_view.cpp index c2e1e2c75..684f36630 100644 --- a/src/gui/track_container_view.cpp +++ b/src/gui/track_container_view.cpp @@ -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(); } diff --git a/src/gui/widgets/envelope_and_lfo_view.cpp b/src/gui/widgets/envelope_and_lfo_view.cpp index 81a732bc3..9825aaf41 100644 --- a/src/gui/widgets/envelope_and_lfo_view.cpp +++ b/src/gui/widgets/envelope_and_lfo_view.cpp @@ -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" ) );