1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2016 CERN
5  * Copyright (C) 2016-2021 KiCad Developers, see change_log.txt for contributors.
6  *
7  * @author Wayne Stambaugh <stambaughw@gmail.com>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <wx/filename.h>
24 #include <wx/uri.h>
25 
26 #include <sch_io_mgr.h>
27 #include <sch_plugins/legacy/sch_legacy_plugin.h>
28 #include <sch_plugins/eagle/sch_eagle_plugin.h>
29 #include <sch_plugins/kicad/sch_sexpr_plugin.h>
30 
31 #include <sch_plugins/altium/sch_altium_plugin.h>
32 #include <sch_plugins/cadstar/cadstar_sch_archive_plugin.h>
33 #include <wildcards_and_files_ext.h>
34 
35 #define FMT_UNIMPLEMENTED   _( "Plugin \"%s\" does not implement the \"%s\" function." )
36 #define FMT_NOTFOUND        _( "Plugin type \"%s\" is not found." )
37 
38 
39 
40 // Some day plugins might be in separate DLL/DSOs, simply because of numbers of them
41 // and code size.  Until then, use the simplest method:
42 
43 // This implementation is one of two which could be done.
44 // The other one would cater to DLL/DSO's.  But since it would be nearly
45 // impossible to link a KICAD type DLL/DSO right now without pulling in all
46 // ::Draw() functions, I forgo that option temporarily.
47 
48 // Some day it may be possible to have some built in AND some DLL/DSO
49 // plugins coexisting.
50 
51 
FindPlugin(SCH_FILE_T aFileType)52 SCH_PLUGIN* SCH_IO_MGR::FindPlugin( SCH_FILE_T aFileType )
53 {
54     // This implementation is subject to change, any magic is allowed here.
55     // The public SCH_IO_MGR API is the only pertinent public information.
56 
57     switch( aFileType )
58     {
59     case SCH_LEGACY:          return new SCH_LEGACY_PLUGIN();
60     case SCH_KICAD:           return new SCH_SEXPR_PLUGIN();
61     case SCH_ALTIUM:          return new SCH_ALTIUM_PLUGIN();
62     case SCH_CADSTAR_ARCHIVE: return new CADSTAR_SCH_ARCHIVE_PLUGIN();
63     case SCH_EAGLE:           return new SCH_EAGLE_PLUGIN();
64     default:                  return nullptr;
65     }
66 }
67 
68 
ReleasePlugin(SCH_PLUGIN * aPlugin)69 void SCH_IO_MGR::ReleasePlugin( SCH_PLUGIN* aPlugin )
70 {
71     // This function is a place holder for a future point in time where
72     // the plugin is a DLL/DSO.  It could do reference counting, and then
73     // unload the DLL/DSO when count goes to zero.
74 
75     delete aPlugin;
76 }
77 
78 
ShowType(SCH_FILE_T aType)79 const wxString SCH_IO_MGR::ShowType( SCH_FILE_T aType )
80 {
81     // keep this function in sync with EnumFromStr() relative to the
82     // text spellings.  If you change the spellings, you will obsolete
83     // library tables, so don't do change, only additions are ok.
84 
85     switch( aType )
86     {
87     case SCH_LEGACY:          return wxString( wxT( "Legacy" ) );
88     case SCH_KICAD:           return wxString( wxT( "KiCad" ) );
89     case SCH_ALTIUM:          return wxString( wxT( "Altium" ) );
90     case SCH_CADSTAR_ARCHIVE: return wxString( wxT( "CADSTAR Schematic Archive" ) );
91     case SCH_EAGLE:           return wxString( wxT( "EAGLE" ) );
92     default:                  return wxString::Format( _( "Unknown SCH_FILE_T value: %d" ),
93                                                        aType );
94     }
95 }
96 
97 
EnumFromStr(const wxString & aType)98 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::EnumFromStr( const wxString& aType )
99 {
100     // keep this function in sync with ShowType() relative to the
101     // text spellings.  If you change the spellings, you will obsolete
102     // library tables, so don't do change, only additions are ok.
103 
104     if( aType == wxT( "Legacy" ) )
105         return SCH_LEGACY;
106     else if( aType == wxT( "KiCad" ) )
107         return SCH_KICAD;
108     else if( aType == wxT( "Altium" ) )
109         return SCH_ALTIUM;
110     else if( aType == wxT( "CADSTAR Schematic Archive" ) )
111         return SCH_CADSTAR_ARCHIVE;
112     else if( aType == wxT( "EAGLE" ) )
113         return SCH_EAGLE;
114 
115     // wxASSERT( blow up here )
116 
117     return SCH_FILE_T( -1 );
118 }
119 
120 
GetFileExtension(SCH_FILE_T aFileType)121 const wxString SCH_IO_MGR::GetFileExtension( SCH_FILE_T aFileType )
122 {
123     wxString ext = wxEmptyString;
124     SCH_PLUGIN* plugin = FindPlugin( aFileType );
125 
126     if( plugin != nullptr )
127     {
128         ext = plugin->GetFileExtension();
129         ReleasePlugin( plugin );
130     }
131 
132     return ext;
133 }
134 
135 
GetLibraryFileExtension(SCH_FILE_T aFileType)136 const wxString SCH_IO_MGR::GetLibraryFileExtension( SCH_FILE_T aFileType )
137 {
138     wxString ext = wxEmptyString;
139     SCH_PLUGIN* plugin = FindPlugin( aFileType );
140 
141     if( plugin != nullptr )
142     {
143         ext = plugin->GetLibraryFileExtension();
144         ReleasePlugin( plugin );
145     }
146 
147     return ext;
148 }
149 
150 
GuessPluginTypeFromLibPath(const wxString & aLibPath)151 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromLibPath( const wxString& aLibPath )
152 {
153     SCH_FILE_T  ret = SCH_KICAD;        // default guess, unless detected otherwise.
154     wxFileName  fn( aLibPath );
155 
156     if( fn.GetExt() == LegacySymbolLibFileExtension )
157     {
158         ret = SCH_LEGACY;
159     }
160     else if( fn.GetExt() == KiCadSymbolLibFileExtension )
161     {
162         ret = SCH_KICAD;
163     }
164 
165     return ret;
166 }
167 
168 
GuessPluginTypeFromSchPath(const wxString & aSchematicPath)169 SCH_IO_MGR::SCH_FILE_T SCH_IO_MGR::GuessPluginTypeFromSchPath( const wxString& aSchematicPath )
170 {
171     SCH_FILE_T  ret = SCH_KICAD;        // default guess, unless detected otherwise.
172     wxFileName  fn( aSchematicPath );
173 
174     if( fn.GetExt() == LegacySchematicFileExtension )
175     {
176         ret = SCH_LEGACY;
177     }
178     else if( fn.GetExt() == KiCadSchematicFileExtension )
179     {
180         ret = SCH_KICAD;
181     }
182 
183     return ret;
184 }
185 
186 
187 DECLARE_ENUM_VECTOR( SCH_IO_MGR, SCH_FILE_T )
188