29#include <wx/filename.h>
31#include <wx/translation.h>
34#include <wx/datetime.h>
48 default:
return wxString::Format(
_(
"UNKNOWN (%d)" ), aFileType );
56 if( aFileType ==
_(
"KiCad" ) )
68 default:
return nullptr;
85 const wxString& aOldFilePath,
86 const wxString& aNewFilePath )
98 wxArrayString dbNames;
99 wxFileName newFileName( aNewFilePath );
101 if( newFileName.HasExt() )
103 wxString extraDir = newFileName.GetFullName();
104 newFileName.ClearExt();
105 newFileName.SetName(
"" );
106 newFileName.AppendDir( extraDir );
109 if( !newFileName.DirExists() && !wxFileName::Mkdir( aNewFilePath, wxS_DIR_DEFAULT ) )
114 bool bestEfforts =
false;
115 oldFilePI->DesignBlockEnumerate( dbNames, aOldFilePath, bestEfforts, aOldFileProps );
117 for(
const wxString& dbName : dbNames )
119 std::unique_ptr<const DESIGN_BLOCK> db(
120 oldFilePI->GetEnumeratedDesignBlock( aOldFilePath, dbName, aOldFileProps ) );
121 kicadPI->DesignBlockSave( aNewFilePath, db.get() );
142 wxDir libDir( aLibraryPath );
144 if( !libDir.IsOpened() )
150 bool hasMoreFiles = libDir.GetFirst( &filename, wxEmptyString, wxDIR_DIRS );
152 while( hasMoreFiles )
154 wxFileName blockDir( aLibraryPath, filename );
158 ts +=
TimestampDir( blockDir.GetFullPath(), wxT(
"*" ) );
160 hasMoreFiles = libDir.GetNext( &filename );
168 const std::map<std::string, UTF8>* aProperties )
170 if( wxDir::Exists( aLibraryPath ) )
172 THROW_IO_ERROR( wxString::Format(
_(
"Cannot overwrite library path '%s'." ),
173 aLibraryPath.GetData() ) );
177 dir.SetPath( aLibraryPath );
182 wxString::Format(
_(
"Library path '%s' could not be created.\n\n"
183 "Make sure you have write permissions and try again." ),
190 const std::map<std::string, UTF8>* aProperties )
193 fn.SetPath( aLibraryPath );
196 if( !fn.DirExists() )
199 if( !fn.IsDirWritable() )
201 THROW_IO_ERROR( wxString::Format(
_(
"Insufficient permissions to delete folder '%s'." ),
202 aLibraryPath.GetData() ) );
205 wxDir dir( aLibraryPath );
210 THROW_IO_ERROR( wxString::Format(
_(
"Library folder '%s' has unexpected files." ),
211 aLibraryPath.GetData() ) );
215 if( dir.HasSubDirs() )
220 dir.GetAllFiles( aLibraryPath, &dirs, wxEmptyString, wxDIR_DIRS );
222 for(
size_t i = 0; i < dirs.GetCount(); i++ )
224 wxFileName tmp = dirs[i];
228 THROW_IO_ERROR( wxString::Format(
_(
"Unexpected folder '%s' found in library "
230 dirs[i].GetData(), aLibraryPath.GetData() ) );
234 for(
size_t i = 0; i < dirs.GetCount(); i++ )
235 wxRemoveFile( dirs[i] );
239 aLibraryPath.GetData() );
243 if( !wxFileName::Rmdir( aLibraryPath, wxPATH_RMDIR_RECURSIVE ) )
245 THROW_IO_ERROR( wxString::Format(
_(
"Design block library '%s' cannot be deleted." ),
246 aLibraryPath.GetData() ) );
253 wxMilliSleep( 250L );
261 const wxString& aLibraryPath,
bool aBestEfforts,
262 const std::map<std::string, UTF8>* aProperties )
265 wxDir dir( aLibraryPath );
267 if( !dir.IsOpened() )
272 bool cont = dir.GetFirst( &dirname, fileSpec, wxDIR_DIRS );
276 aDesignBlockNames.Add( dirname.Before( wxT(
'.' ) ) );
277 cont = dir.GetNext( &dirname );
283 const wxString& aDesignBlockName,
bool aKeepUUID,
284 const std::map<std::string, UTF8>* aProperties )
286 wxString dbPath = aLibraryPath + wxFileName::GetPathSeparator() + aDesignBlockName + wxT(
"." )
288 wxString dbSchPath = dbPath + aDesignBlockName + wxT(
"." )
292 if( !wxFileExists( dbSchPath ) )
303 if( wxFileExists( dbMetadataPath ) )
307 nlohmann::ordered_json dbMetadata;
308 std::ifstream dbMetadataFile( dbMetadataPath.fn_str() );
310 dbMetadataFile >> dbMetadata;
312 if( dbMetadata.contains(
"description" ) )
315 if( dbMetadata.contains(
"keywords" ) )
318 nlohmann::ordered_map<wxString, wxString> fields;
321 if( dbMetadata.contains(
"fields" ) )
323 for(
auto& item : dbMetadata[
"fields"].items() )
325 wxString
name = wxString::FromUTF8( item.key() );
326 wxString value = wxString::FromUTF8( item.value().get<std::string>() );
328 fields[
name] = value;
337 _(
"Design block metadata file '%s' could not be read." ), dbMetadataPath ) );
348 const std::map<std::string, UTF8>* aProperties )
353 THROW_IO_ERROR(
_(
"Design block does not have a valid library ID." ) );
358 THROW_IO_ERROR( wxString::Format(
_(
"Schematic source file '%s' does not exist." ),
363 wxFileName dbFolder( aLibraryPath + wxFileName::GetPathSeparator()
366 + wxFileName::GetPathSeparator() );
368 if( !dbFolder.DirExists() )
370 if( !dbFolder.Mkdir() )
372 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be created." ),
373 dbFolder.GetFullPath().GetData() ) );
389 _(
"Schematic file '%s' could not be saved as design block at '%s'." ),
399 nlohmann::ordered_json dbMetadata;
401 dbMetadata[
"keywords"] = aDesignBlock->
GetKeywords();
402 dbMetadata[
"fields"] = aDesignBlock->
GetFields();
404 bool success =
false;
408 wxFFile mdFile( dbMetadataFile, wxT(
"wb" ) );
410 if( mdFile.IsOpened() )
411 success = mdFile.Write( dbMetadata.dump( 0 ) );
423 _(
"Design block metadata file '%s' could not be saved." ), dbMetadataFile ) );
429 const std::map<std::string, UTF8>* aProperties )
431 wxFileName dbDir = wxFileName( aLibPath + wxFileName::GetPathSeparator() + aDesignBlockName
434 if( !dbDir.DirExists() )
437 wxString::Format(
_(
"Design block '%s' does not exist." ), dbDir.GetFullName() ) );
441 if( !wxFileName::Rmdir( dbDir.GetFullPath(), wxPATH_RMDIR_RECURSIVE ) )
443 THROW_IO_ERROR( wxString::Format(
_(
"Design block folder '%s' could not be deleted." ),
444 dbDir.GetFullPath().GetData() ) );
451 wxFileName
path( aLibraryPath );
452 return path.IsOk() &&
path.IsDirWritable();
@ KICAD_SEXP
S-expression KiCad file format.
@ DESIGN_BLOCK_FILE_UNKNOWN
0 is not a legal menu id on Mac
static const wxString ShowType(DESIGN_BLOCK_FILE_T aFileType)
static DESIGN_BLOCK_FILE_T GuessPluginTypeFromLibPath(const wxString &aLibPath, int aCtl=0)
static DESIGN_BLOCK_FILE_T EnumFromStr(const wxString &aFileType)
static bool ConvertLibrary(std::map< std::string, UTF8 > *aOldFileProps, const wxString &aOldFilePath, const wxString &aNewFilePath)
Convert a design block library to the latest KiCad format.
static DESIGN_BLOCK_IO * FindPlugin(DESIGN_BLOCK_FILE_T aFileType)
long long GetLibraryTimestamp(const wxString &aLibraryPath) const
void DesignBlockDelete(const wxString &aLibraryPath, const wxString &aDesignBlockName, const std::map< std::string, UTF8 > *aProperties=nullptr)
DESIGN_BLOCK * DesignBlockLoad(const wxString &aLibraryPath, const wxString &aDesignBlockName, bool aKeepUUID=false, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockSave(const wxString &aLibraryPath, const DESIGN_BLOCK *aDesignBlock, const std::map< std::string, UTF8 > *aProperties=nullptr)
void DesignBlockEnumerate(wxArrayString &aDesignBlockNames, const wxString &aLibraryPath, bool aBestEfforts, const std::map< std::string, UTF8 > *aProperties=nullptr)
virtual bool DeleteLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Delete an existing library and returns true, or if library does not exist returns false,...
void CreateLibrary(const wxString &aLibraryPath, const std::map< std::string, UTF8 > *aProperties=nullptr) override
Create a new empty library at aLibraryPath empty.
bool IsLibraryWritable(const wxString &aLibraryPath) override
Return true if the library at aLibraryPath is writable.
const IO_BASE::IO_FILE_DESC GetLibraryDesc() const override
Get the descriptor for the library container that this IO plugin operates on.
void SetLibDescription(const wxString &aDesc)
void SetKeywords(const wxString &aKeywords)
void SetSchematicFile(const wxString &aFile)
const wxString & GetKeywords() const
const wxString & GetLibDescription() const
void SetLibId(const LIB_ID &aName)
const wxString & GetSchematicFile() const
void SetFields(nlohmann::ordered_map< wxString, wxString > &aFields)
const LIB_ID & GetLibId() const
const nlohmann::ordered_map< wxString, wxString > & GetFields() const
A logical library item identifier and consists of various portions much like a URI.
bool IsValid() const
Check if this LID_ID is valid.
const UTF8 & GetLibItemName() const
long long TimestampDir(const wxString &aDirPath, const wxString &aFilespec)
A copy of ConvertFileTimeToWx() because wxWidgets left it as a static function private to src/common/...
static const std::string KiCadDesignBlockLibPathExtension
static const std::string KiCadDesignBlockPathExtension
static const std::string JsonFileExtension
static const std::string KiCadSchematicFileExtension
const wxChar *const traceDesignBlocks
Some functions to handle hotkeys in KiCad.
std::unique_ptr< T > IO_RELEASER
Helper to hold and release an IO_BASE object when exceptions are thrown.
#define THROW_IO_ERROR(msg)
#define KICTL_NONKICAD_ONLY
chosen file is non-KiCad according to user
Container that describes file type info.
wxLogTrace helper definitions.
Definition of file extensions used in Kicad.