You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2011-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #include <wx/filename.h>
  25. #include <wx/uri.h>
  26. #include <io_mgr.h>
  27. #include <legacy_plugin.h>
  28. #include <kicad_plugin.h>
  29. #include <eagle_plugin.h>
  30. #include <pcad2kicadpcb_plugin/pcad_plugin.h>
  31. #include <gpcb_plugin.h>
  32. #include <config.h>
  33. #if defined(BUILD_GITHUB_PLUGIN)
  34. #include <github/github_plugin.h>
  35. #endif
  36. #include <wildcards_and_files_ext.h>
  37. #define FMT_UNIMPLEMENTED _( "Plugin \"%s\" does not implement the \"%s\" function." )
  38. #define FMT_NOTFOUND _( "Plugin type \"%s\" is not found." )
  39. // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
  40. // and code size. Until then, use the simplest method:
  41. // This implementation is one of two which could be done.
  42. // The other one would cater to DLL/DSO's. But since it would be nearly
  43. // impossible to link a KICAD type DLL/DSO right now without pulling in all
  44. // ::Draw() functions, I forgo that option temporarily.
  45. // Some day it may be possible to have some built in AND some DLL/DSO
  46. // plugins coexisting.
  47. PLUGIN* IO_MGR::PluginFind( PCB_FILE_T aFileType )
  48. {
  49. // This implementation is subject to change, any magic is allowed here.
  50. // The public IO_MGR API is the only pertinent public information.
  51. return PLUGIN_REGISTRY::Instance()->Create( aFileType );
  52. }
  53. void IO_MGR::PluginRelease( PLUGIN* aPlugin )
  54. {
  55. // This function is a place holder for a future point in time where
  56. // the plugin is a DLL/DSO. It could do reference counting, and then
  57. // unload the DLL/DSO when count goes to zero.
  58. delete aPlugin;
  59. }
  60. const wxString IO_MGR::ShowType( PCB_FILE_T aType )
  61. {
  62. const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
  63. for( const auto& plugin : plugins )
  64. {
  65. if ( plugin.m_type == aType )
  66. {
  67. return plugin.m_name;
  68. }
  69. }
  70. return wxString::Format( _( "UNKNOWN (%d)" ), aType );
  71. }
  72. IO_MGR::PCB_FILE_T IO_MGR::EnumFromStr( const wxString& aType )
  73. {
  74. const auto& plugins = PLUGIN_REGISTRY::Instance()->AllPlugins();
  75. for( const auto& plugin : plugins )
  76. {
  77. if ( plugin.m_name == aType )
  78. {
  79. return plugin.m_type;
  80. }
  81. }
  82. return PCB_FILE_T( -1 );
  83. }
  84. const wxString IO_MGR::GetFileExtension( PCB_FILE_T aFileType )
  85. {
  86. wxString ext = wxEmptyString;
  87. PLUGIN* plugin = PluginFind( aFileType );
  88. if( plugin != NULL )
  89. {
  90. ext = plugin->GetFileExtension();
  91. PluginRelease( plugin );
  92. }
  93. return ext;
  94. }
  95. IO_MGR::PCB_FILE_T IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
  96. {
  97. PCB_FILE_T ret = KICAD_SEXP; // default guess, unless detected otherwise.
  98. wxFileName fn( aLibPath );
  99. if( fn.GetExt() == LegacyFootprintLibPathExtension )
  100. {
  101. ret = LEGACY;
  102. }
  103. else if( fn.GetExt() == GedaPcbFootprintLibFileExtension )
  104. {
  105. ret = GEDA_PCB;
  106. }
  107. else if( fn.GetExt() == EagleFootprintLibPathExtension )
  108. {
  109. ret = EAGLE;
  110. }
  111. // Test this one anyways, even though it's the default guess, to avoid
  112. // the wxURI instantiation below.
  113. // We default ret to KICAD above, because somebody might have
  114. // mistakenly put a pretty library into a directory other than
  115. // *.pretty/ with *.kicad_mod in there., and I don't want to return -1,
  116. // since we only claimed to be guessing.
  117. //
  118. // However libraries on GitHub have names ending by .pretty
  119. // so test also this is not a name starting by http (including https).
  120. else if( fn.GetExt() == KiCadFootprintLibPathExtension &&
  121. !aLibPath.StartsWith( wxT( "http" ) ) )
  122. {
  123. ret = KICAD_SEXP;
  124. }
  125. else
  126. {
  127. #if defined(BUILD_GITHUB_PLUGIN)
  128. // There is no extension for a remote repo, so test the server name.
  129. wxURI uri( aLibPath );
  130. if( uri.HasServer() && uri.GetServer() == wxT( "github.com" ) )
  131. {
  132. ret = GITHUB;
  133. }
  134. #endif
  135. }
  136. return ret;
  137. }
  138. BOARD* IO_MGR::Load( PCB_FILE_T aFileType, const wxString& aFileName,
  139. BOARD* aAppendToMe, const PROPERTIES* aProperties )
  140. {
  141. // release the PLUGIN even if an exception is thrown.
  142. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  143. if( (PLUGIN*) pi ) // test pi->plugin
  144. {
  145. return pi->Load( aFileName, aAppendToMe, aProperties ); // virtual
  146. }
  147. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  148. }
  149. void IO_MGR::Save( PCB_FILE_T aFileType, const wxString& aFileName, BOARD* aBoard, const PROPERTIES* aProperties )
  150. {
  151. // release the PLUGIN even if an exception is thrown.
  152. PLUGIN::RELEASER pi( PluginFind( aFileType ) );
  153. if( (PLUGIN*) pi ) // test pi->plugin
  154. {
  155. pi->Save( aFileName, aBoard, aProperties ); // virtual
  156. return;
  157. }
  158. THROW_IO_ERROR( wxString::Format( FMT_NOTFOUND, ShowType( aFileType ).GetData() ) );
  159. }
  160. // These text strings are "truth" for identifying the plugins. If you change the spellings,
  161. // you will obsolete library tables, so don't do it. Additions are OK.
  162. static IO_MGR::REGISTER_PLUGIN registerEaglePlugin( IO_MGR::EAGLE, wxT("Eagle"), []() -> PLUGIN* { return new EAGLE_PLUGIN; } );
  163. static IO_MGR::REGISTER_PLUGIN registerKicadPlugin( IO_MGR::KICAD_SEXP, wxT("KiCad"), []() -> PLUGIN* { return new PCB_IO; } );
  164. static IO_MGR::REGISTER_PLUGIN registerPcadPlugin( IO_MGR::PCAD, wxT("P-Cad"), []() -> PLUGIN* { return new PCAD_PLUGIN; } );
  165. #ifdef BUILD_GITHUB_PLUGIN
  166. static IO_MGR::REGISTER_PLUGIN registerGithubPlugin( IO_MGR::GITHUB, wxT("Github"), []() -> PLUGIN* { return new GITHUB_PLUGIN; } );
  167. #endif /* BUILD_GITHUB_PLUGIN */
  168. static IO_MGR::REGISTER_PLUGIN registerLegacyPlugin( IO_MGR::LEGACY, wxT("Legacy"), []() -> PLUGIN* { return new LEGACY_PLUGIN; } );
  169. static IO_MGR::REGISTER_PLUGIN registerGPCBPlugin( IO_MGR::GEDA_PCB, wxT("GEDA/Pcb"), []() -> PLUGIN* { return new GPCB_PLUGIN; } );