31#include <wx/mimetype.h>
40#include "wx/tokenzr.h"
44#include <wx/wfstream.h>
46#include <wx/zipstrm.h>
53 if( !
string.StartsWith( wxT(
"\"" ) ) )
55 string.Prepend ( wxT(
"\"" ) );
56 string.Append ( wxT(
"\"" ) );
70 if( wxFileExists( fullFileName ) )
73 if( wxGetEnv( wxT(
"KICAD_RUN_FROM_BUILD_DIR" ),
nullptr ) )
75 wxFileName buildDir(
Pgm().GetExecutablePath(), shortname );
76 buildDir.RemoveLastDir();
78 buildDir.AppendDir( shortname );
80 buildDir.AppendDir( shortname.BeforeLast(
'.' ) );
83 if( buildDir.GetDirs().Last() ==
"pl_editor" )
85 buildDir.RemoveLastDir();
86 buildDir.AppendDir(
"pagelayout_editor" );
89 if( wxFileExists( buildDir.GetFullPath() ) )
90 return buildDir.GetFullPath();
95 if(
Pgm().IsKicadEnvVariableDefined() )
99 if( wxFileExists( fullFileName ) )
103#if defined( __WINDOWS__ )
110 const static wxChar* possibilities[] = {
111#if defined( __WXMAC__ )
113 wxT(
"Contents/Applications/pcbnew.app/Contents/MacOS/" ),
114 wxT(
"Contents/Applications/eeschema.app/Contents/MacOS/" ),
115 wxT(
"Contents/Applications/gerbview.app/Contents/MacOS/" ),
116 wxT(
"Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
117 wxT(
"Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
118 wxT(
"Contents/Applications/pl_editor.app/Contents/MacOS/" ),
121 wxT(
"/usr/local/bin/" ),
122 wxT(
"/usr/local/kicad/bin/" ),
127 for(
unsigned i=0; i<
arrayDim(possibilities); ++i )
130 fullFileName = possibilities[i] + shortname;
136 if( wxFileExists( fullFileName ) )
146int ExecuteFile(
const wxString& aEditorName,
const wxString& aFileName, wxProcess* aCallback,
149 wxString fullEditorName;
150 std::vector<wxString> params;
154 bool inSingleQuotes =
false;
155 bool inDoubleQuotes =
false;
160 if( !param.IsEmpty() )
162 params.push_back( param );
167 for( wxUniChar ch : aEditorName )
174 inSingleQuotes =
false;
182 else if( inDoubleQuotes )
187 inDoubleQuotes =
false;
194 else if( ch ==
'\'' )
197 inSingleQuotes =
true;
202 inDoubleQuotes =
true;
219 fullEditorName = params[0];
221 params.erase( params.begin() );
227 fullEditorName = aEditorName;
230 if( wxFileExists( fullEditorName ) )
232 std::vector<const wchar_t*> args;
234 args.emplace_back( fullEditorName.wc_str() );
236 if( !params.empty() )
238 for(
const wxString& p : params )
239 args.emplace_back( p.wc_str() );
242 if( !aFileName.IsEmpty() )
243 args.emplace_back( aFileName.wc_str() );
245 args.emplace_back(
nullptr );
247 return wxExecute(
const_cast<wchar_t**
>( args.data() ), wxEXEC_ASYNC, aCallback );
251 msg.Printf(
_(
"Command '%s' could not be found." ), fullEditorName );
260 wxString filename = file;
264 if(
Pgm().UseSystemPdfBrowser() )
268 msg.Printf(
_(
"Unable to find a PDF viewer for '%s'." ), filename );
275 const wchar_t* args[3];
278 args[1] = filename.wc_str();
281 if( wxExecute(
const_cast<wchar_t**
>( args ) ) == -1 )
283 msg.Printf(
_(
"Problem while running the PDF viewer '%s'." ), args[0] );
293void KiCopyFile(
const wxString& aSrcPath,
const wxString& aDestPath, wxString& aErrors )
295 if( !wxCopyFile( aSrcPath, aDestPath ) )
299 if( !aErrors.IsEmpty() )
302 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
321 std::function<
bool(
const std::string& token, wxString& value )> aCallback,
324 bool success =
false;
334 if( node->IsList() && node->GetNumberOfChildren() > 1 && node->GetChild( 0 )->IsSymbol() )
336 std::string token = node->GetChild( 0 )->GetSymbol();
337 SEXPR::SEXPR_STRING* pathNode = dynamic_cast<SEXPR::SEXPR_STRING*>( node->GetChild( 1 ) );
338 SEXPR::SEXPR_SYMBOL* symNode = dynamic_cast<SEXPR::SEXPR_SYMBOL*>( node->GetChild( 1 ) );
342 path = pathNode->m_value;
344 path = symNode->m_value;
346 if( aCallback( token, path ) )
349 pathNode->m_value = path;
351 symNode->m_value = path;
356 wxFFile destFile( aDestPath,
"wb" );
358 if( destFile.IsOpened() )
359 success = destFile.Write( sexpr->AsString( 0 ) );
372 if( !aErrors.empty() )
373 aErrors += wxS(
"\n" );
375 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
383 return wxT(
"\"" ) + fn.GetFullPath( format ) + wxT(
"\"" );
389 namespace fs = std::filesystem;
391 std::string rmDir = aFileName.ToStdString();
393 if( rmDir.length() < 3 )
396 *aErrors =
_(
"Invalid directory name, cannot remove root" );
401 if( !fs::exists( rmDir ) )
404 *aErrors = wxString::Format(
_(
"Directory '%s' does not exist" ), aFileName );
409 fs::path
path( rmDir );
411 if( !fs::is_directory(
path ) )
414 *aErrors = wxString::Format(
_(
"'%s' is not a directory" ), aFileName );
421 fs::remove_all(
path );
423 catch(
const fs::filesystem_error& e )
426 *aErrors = wxString::Format(
_(
"Error removing directory '%s': %s" ), aFileName, e.what() );
436 const std::vector<wxString>& aPathsWithOverwriteDisallowed, wxString& aErrors )
438 wxDir dir( aSourceDir );
440 if( !dir.IsOpened() )
442 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
443 aErrors += wxT(
"\n" );
447 if( !wxFileName::Mkdir( aDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
449 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), aDestDir );
450 aErrors += wxT(
"\n" );
455 bool cont = dir.GetFirst( &filename );
459 wxString sourcePath = dir.GetNameWithSep() + filename;
460 wxString destPath = aDestDir + wxFileName::GetPathSeparator() + filename;
462 if( wxFileName::DirExists( sourcePath ) )
465 if( !
CopyDirectory( sourcePath, destPath, aPathsWithOverwriteDisallowed, aErrors ) )
471 if(
alg::contains( aPathsWithOverwriteDisallowed, sourcePath ) && wxFileExists( destPath ) )
475 else if( !wxCopyFile( sourcePath, destPath ) )
477 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), sourcePath, destPath );
482 cont = dir.GetNext( &filename );
490 wxString& aErrors, std::vector<wxString>& aPathsWritten )
493 wxFileName sourceFn( aSourcePath );
494 wxString sourcePath = sourceFn.GetFullPath();
495 bool isSourceDirectory = wxFileName::DirExists( sourcePath );
496 wxString baseDestDir = aDestDir;
499 [&](
const wxString& src,
const wxString& dest ) ->
bool
501 if( wxCopyFile( src, dest, aAllowOverwrites ) )
503 aPathsWritten.push_back( dest );
507 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), src, dest );
508 aErrors += wxT(
"\n" );
512 auto processEntries =
513 [&](
const wxString& srcDir,
const wxString& pattern,
const wxString& destDir ) ->
bool
517 if( !dir.IsOpened() )
519 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), srcDir );
520 aErrors += wxT(
"\n" );
528 bool cont = dir.GetFirst( &filename, pattern, wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN );
532 const wxString entrySrc = srcDir + wxFileName::GetPathSeparator() + filename;
533 const wxString entryDest = destDir + wxFileName::GetPathSeparator() + filename;
535 if( !filename.Matches( wxT(
"~*.lck" ) ) && !filename.Matches( wxT(
"*.lck" ) ) )
537 if( wxFileName::DirExists( entrySrc ) )
542 aErrors += wxString::Format(
_(
"Could not copy directory: %s to %s" ),
543 entrySrc, entryDest );
544 aErrors += wxT(
"\n" );
552 if( !performCopy( entrySrc, entryDest ) )
559 cont = dir.GetNext( &filename );
566 if( isSourceDirectory )
568 wxString sourceDirName = sourceFn.GetFullName();
569 baseDestDir = wxFileName( aDestDir, sourceDirName ).GetFullPath();
573 if( !wxFileName::Mkdir( baseDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
575 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), baseDestDir );
576 aErrors += wxT(
"\n" );
582 if( !isSourceDirectory )
584 const wxString fileName = sourceFn.GetFullName();
587 if( fileName.Contains(
'*' ) || fileName.Contains(
'?' ) )
589 const wxString dirPath = sourceFn.GetPath();
591 if( !wxFileName::DirExists( dirPath ) )
593 aErrors += wxString::Format(
_(
"Source directory does not exist: %s" ), dirPath );
594 aErrors += wxT(
"\n" );
600 return processEntries( dirPath, fileName, baseDestDir );
604 return performCopy( sourcePath, wxFileName( baseDestDir, fileName ).GetFullPath() );
608 return processEntries( sourcePath, wxEmptyString, baseDestDir );
613 const wxString& aParentDir )
615 wxDir dir( aSourceDir );
617 if( !dir.IsOpened() )
619 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
625 bool cont = dir.GetFirst( &filename );
629 wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
630 wxString zipPath = aParentDir + filename;
632 if( wxFileName::DirExists( sourcePath ) )
635 aZip.PutNextDirEntry( zipPath +
"/" );
644 aZip.PutNextEntry( zipPath );
645 wxFFileInputStream fileStream( sourcePath );
647 if( !fileStream.IsOk() )
649 aErrors += wxString::Format(
_(
"Could not read file: %s" ), sourcePath );
653 aZip.Write( fileStream );
656 cont = dir.GetNext( &filename );
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
virtual const wxString & GetKicadEnvVariable() const
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
virtual const wxString & GetPdfBrowserName() const
virtual const wxString & GetExecutablePath() const
std::unique_ptr< SEXPR > ParseFromFile(const std::string &aFilename)
size_t GetNumberOfChildren() const
SEXPR * GetChild(size_t aIndex) const
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
This file is part of the common library.
wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
void CopySexprFile(const wxString &aSrcPath, const wxString &aDestPath, std::function< bool(const std::string &token, wxString &value)> aCallback, wxString &aErrors)
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback, bool aFileForKicad)
Call the executable file aEditorName with the parameter aFileName.
bool RmDirRecursive(const wxString &aFileName, wxString *aErrors)
Remove the directory aDirName and all its contents including subdirectories and their files.
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
bool CopyFilesOrDirectory(const wxString &aSourcePath, const wxString &aDestDir, bool aAllowOverwrites, wxString &aErrors, std::vector< wxString > &aPathsWritten)
static void traverseSEXPR(SEXPR::SEXPR *aNode, const std::function< void(SEXPR::SEXPR *)> &aVisitor)
bool CopyDirectory(const wxString &aSourceDir, const wxString &aDestDir, const std::vector< wxString > &aPathsWithOverwriteDisallowed, wxString &aErrors)
Copy a directory and its contents to another directory.
bool AddDirectoryToZip(wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir)
Add a directory and its contents to a zip file.
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
bool contains(const _Container &__container, _Value __value)
Returns true if the container contains the given value.
PGM_BASE & Pgm()
The global program "get" accessor.
#define TO_UTF8(wxstring)
Convert a wxString to a UTF8 encoded C string for all wxWidgets build modes.