From 694ff39f53bdbd0b65e4144fe939a9d81328541e Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Wed, 15 Jul 2015 14:08:50 +0200 Subject: [PATCH] Footprint Library Wizard accepts any folder name as .pretty library. --- pcbnew/dialogs/wizard_add_fplib.cpp | 69 ++++++++++++++++++----------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/pcbnew/dialogs/wizard_add_fplib.cpp b/pcbnew/dialogs/wizard_add_fplib.cpp index 3df649ba6b..882c8f2b01 100644 --- a/pcbnew/dialogs/wizard_add_fplib.cpp +++ b/pcbnew/dialogs/wizard_add_fplib.cpp @@ -34,6 +34,7 @@ #include #include +#include #include #include @@ -56,16 +57,16 @@ static const int FILTER_COUNT = 4; static const struct { - wxString m_Description; - wxString m_Extension; - bool m_IsFile; + wxString m_Description; ///< Description shown in the file picker dialog + wxString m_Extension; ///< In case of folders it stands for extensions of files stored inside + bool m_IsFile; ///< Whether it is a folder or a file IO_MGR::PCB_FILE_T m_Plugin; } fileFilters[FILTER_COUNT] = { - { "KiCad (*.pretty folders)", ".pretty", false, IO_MGR::KICAD }, - { "Eagle 6.x (*.lbr)", ".lbr", true, IO_MGR::EAGLE }, - { "KiCad legacy (*.mod)", ".mod", true, IO_MGR::LEGACY }, - { "Geda (folder with *.fp files)", "", false, IO_MGR::GEDA_PCB }, + { "KiCad (folder with .kicad_mod files)", "kicad_mod", false, IO_MGR::KICAD }, + { "Eagle 6.x (*.lbr)", "lbr", true, IO_MGR::EAGLE }, + { "KiCad legacy (*.mod)", "mod", true, IO_MGR::LEGACY }, + { "Geda (folder with *.fp files)", "fp", false, IO_MGR::GEDA_PCB }, }; @@ -81,10 +82,11 @@ static wxString getFilterString() if( i != 0 ) filterInit += ";"; - filterInit += "*" + fileFilters[i].m_Extension; + filterInit += "*." + fileFilters[i].m_Extension; // Rest of the filter string - filter += "|" + fileFilters[i].m_Description + "|*" + fileFilters[i].m_Extension; + filter += "|" + fileFilters[i].m_Description + + "|" + ( fileFilters[i].m_IsFile ? "*." + fileFilters[i].m_Extension : "" ); } return filterInit + filter; @@ -94,19 +96,39 @@ static wxString getFilterString() // Tries to guess the plugin type basing on the path static boost::optional getPluginType( const wxString& aPath ) { - if( ( aPath.StartsWith( "http://" ) || aPath.StartsWith( "https://" ) ) && aPath.EndsWith( ".pretty" ) ) + if( ( aPath.StartsWith( "http://" ) || aPath.StartsWith( "https://" ) ) ) return boost::optional( IO_MGR::GITHUB ); - wxFileName file( aPath ); + wxFileName path( aPath ); for( int i = 0; i < FILTER_COUNT; ++i ) { - if( aPath.EndsWith( fileFilters[i].m_Extension ) && - file.FileExists() == fileFilters[i].m_IsFile ) + bool ok = false; + + if( fileFilters[i].m_IsFile ) + { + ok = path.IsFileReadable() && path.GetExt() == fileFilters[i].m_Extension; + } + else if( path.IsDirReadable() ) + { + // Plugin expects a directory containing files with a specific extension + wxDir dir( aPath ); + + if( dir.IsOpened() ) + { + wxString filename; + + dir.GetFirst( &filename, "*." + fileFilters[i].m_Extension, wxDIR_FILES ); + + ok = !filename.IsEmpty(); + } + } + + if( ok ) return boost::optional( fileFilters[i].m_Plugin ); } - return boost::optional(); + return boost::none; } @@ -115,20 +137,15 @@ static bool passesFilter( const wxString& aFileName, int aFilterIndex ) { wxASSERT( aFilterIndex <= FILTER_COUNT ); wxFileName file( aFileName ); + boost::optional result = getPluginType( aFileName ); - if( aFilterIndex == 0 ) // any supported library format - { - boost::optional result = getPluginType( aFileName ); - return ( result ? true : false ); - } - else - { - if( aFileName.EndsWith( fileFilters[aFilterIndex - 1].m_Extension ) && - file.FileExists() == fileFilters[aFilterIndex - 1].m_IsFile ) - return true; - } + if( !result ) // does not match any supported plugin + return false; - return false; + if( aFilterIndex == 0 ) // any plugin will do + return true; + + return ( fileFilters[aFilterIndex - 1].m_Plugin == *result ); }