1 /*
2  * ImportFilter.cpp - base-class for all import-filters
3  *
4  * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
5  *
6  * This file is part of LMMS - https://lmms.io
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public
19  * License along with this program (see COPYING); if not, write to the
20  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301 USA.
22  *
23  */
24 
25 
26 #include <QMessageBox>
27 
28 #include "ImportFilter.h"
29 #include "Engine.h"
30 #include "TrackContainer.h"
31 #include "PluginFactory.h"
32 #include "ProjectJournal.h"
33 
34 
ImportFilter(const QString & _file_name,const Descriptor * _descriptor)35 ImportFilter::ImportFilter( const QString & _file_name,
36 							const Descriptor * _descriptor ) :
37 	Plugin( _descriptor, NULL ),
38 	m_file( _file_name )
39 {
40 }
41 
42 
43 
44 
~ImportFilter()45 ImportFilter::~ImportFilter()
46 {
47 }
48 
49 
50 
51 
import(const QString & _file_to_import,TrackContainer * tc)52 void ImportFilter::import( const QString & _file_to_import,
53 							TrackContainer* tc )
54 {
55 	bool successful = false;
56 
57 	char * s = qstrdup( _file_to_import.toUtf8().constData() );
58 
59 	// do not record changes while importing files
60 	const bool j = Engine::projectJournal()->isJournalling();
61 	Engine::projectJournal()->setJournalling( false );
62 
63 	for (const Plugin::Descriptor* desc : pluginFactory->descriptors(Plugin::ImportFilter))
64 	{
65 		Plugin * p = Plugin::instantiate( desc->name, NULL, s );
66 		if( dynamic_cast<ImportFilter *>( p ) != NULL &&
67 			dynamic_cast<ImportFilter *>( p )->tryImport( tc ) == true )
68 		{
69 			delete p;
70 			successful = true;
71 			break;
72 		}
73 		delete p;
74 	}
75 
76 	Engine::projectJournal()->setJournalling( j );
77 
78 	delete[] s;
79 
80 	if( successful == false )
81 	{
82 		QMessageBox::information( NULL,
83 			TrackContainer::tr( "Couldn't import file" ),
84 			TrackContainer::tr( "Couldn't find a filter for "
85 						"importing file %1.\n"
86 						"You should convert this file "
87 						"into a format supported by "
88 						"LMMS using another software."
89 						).arg( _file_to_import ),
90 					QMessageBox::Ok,
91 					QMessageBox::NoButton );
92 	}
93 }
94 
95 
96 
97 
openFile()98 bool ImportFilter::openFile()
99 {
100 	if( m_file.open( QFile::ReadOnly ) == false )
101 	{
102 		QMessageBox::critical( NULL,
103 			TrackContainer::tr( "Couldn't open file" ),
104 			TrackContainer::tr( "Couldn't open file %1 "
105 						"for reading.\nPlease make "
106 						"sure you have read-"
107 						"permission to the file and "
108 						"the directory containing the "
109 						"file and try again!" ).arg(
110 							m_file.fileName() ),
111 					QMessageBox::Ok,
112 					QMessageBox::NoButton );
113 		return false;
114 	}
115 	return true;
116 }
117 
118 
119 
120