From 7037faedd35eedc969284eb3c990c955bdb761c6 Mon Sep 17 00:00:00 2001 From: Dave French Date: Mon, 23 Feb 2015 21:16:38 +0000 Subject: [PATCH] Added Checking of filetypes from the xml. Added a static function fileTypeFromData to the DataFile class. This opens the given file and checks the xml for its file type, as oposed to relying on the file extension --- include/DataFile.h | 5 +++++ src/core/DataFile.cpp | 39 +++++++++++++++++++++++++++++++++++++++ src/gui/FileBrowser.cpp | 3 ++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/include/DataFile.h b/include/DataFile.h index 1d5f71f78..86dee8675 100644 --- a/include/DataFile.h +++ b/include/DataFile.h @@ -56,6 +56,11 @@ public: DataFile( const QByteArray& data ); DataFile( Type type ); + /// \brief fileTypeFromData + /// Reads the given file and checks the xml for the type + /// returns UnknownType if the file is not reconised + static DataFile::Type fileTypeFromData( const QString fileName); + virtual ~DataFile(); QString nameWithExtension( const QString& fn ) const; diff --git a/src/core/DataFile.cpp b/src/core/DataFile.cpp index 1e79b0c08..2be3f0377 100644 --- a/src/core/DataFile.cpp +++ b/src/core/DataFile.cpp @@ -114,6 +114,45 @@ DataFile::DataFile( Type type ) : } +DataFile::Type DataFile::fileTypeFromData(const QString fileName) +{ + QString errorMsg; + DataFile::Type t; + QDomDocument doc; + + QFile inFile( fileName ); + if( !inFile.open( QIODevice::ReadOnly ) ) + { + return DataFile::Type::UnknownType; + } + + QByteArray data = inFile.readAll(); + inFile.close(); + + int line = -1, col = -1; + if( !doc.setContent( data, &errorMsg, &line, &col ) ) + { + // parsing failed? then try to uncompress data + QByteArray uncompressed = qUncompress( data ); + if( !uncompressed.isEmpty() ) + { + if( doc.setContent( uncompressed, &errorMsg, &line, &col ) ) + { + line = col = -1; + } + } + if( line >= 0 && col >= 0 ) + { + return DataFile::Type::UnknownType; + } + } + + QDomElement root = doc.documentElement(); + t = type( root.attribute( "type" ) ); + + return t; +} + diff --git a/src/gui/FileBrowser.cpp b/src/gui/FileBrowser.cpp index 0d19ec6aa..4253ce55d 100644 --- a/src/gui/FileBrowser.cpp +++ b/src/gui/FileBrowser.cpp @@ -459,7 +459,8 @@ void FileBrowserTreeWidget::mousePressEvent(QMouseEvent * me ) } else if( f->type() != FileItem::VstPluginFile && ( f->handling() == FileItem::LoadAsPreset || - f->handling() == FileItem::LoadByPlugin ) ) + f->handling() == FileItem::LoadByPlugin ) + && DataFile::fileTypeFromData( f->fullName() ) == DataFile::Type::InstrumentTrackSettings ) { m_previewPlayHandle = new PresetPreviewPlayHandle( f->fullName(), f->handling() == FileItem::LoadByPlugin ); }