31#include <wx/mimetype.h>
40#include "wx/tokenzr.h"
45#include <wx/wfstream.h>
47#include <wx/zipstrm.h>
54 if( !
string.StartsWith( wxT(
"\"" ) ) )
56 string.Prepend ( wxT(
"\"" ) );
57 string.Append ( wxT(
"\"" ) );
71 if( wxFileExists( fullFileName ) )
74 if( wxGetEnv( wxT(
"KICAD_RUN_FROM_BUILD_DIR" ),
nullptr ) )
76 wxFileName buildDir(
Pgm().GetExecutablePath(), shortname );
77 buildDir.RemoveLastDir();
79 buildDir.AppendDir( shortname );
81 buildDir.AppendDir( shortname.BeforeLast(
'.' ) );
84 if( buildDir.GetDirs().Last() ==
"pl_editor" )
86 buildDir.RemoveLastDir();
87 buildDir.AppendDir(
"pagelayout_editor" );
90 if( wxFileExists( buildDir.GetFullPath() ) )
91 return buildDir.GetFullPath();
96 if(
Pgm().IsKicadEnvVariableDefined() )
100 if( wxFileExists( fullFileName ) )
104#if defined( __WINDOWS__ )
111 const static wxChar* possibilities[] = {
112#if defined( __WXMAC__ )
114 wxT(
"Contents/Applications/pcbnew.app/Contents/MacOS/" ),
115 wxT(
"Contents/Applications/eeschema.app/Contents/MacOS/" ),
116 wxT(
"Contents/Applications/gerbview.app/Contents/MacOS/" ),
117 wxT(
"Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
118 wxT(
"Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
119 wxT(
"Contents/Applications/pl_editor.app/Contents/MacOS/" ),
122 wxT(
"/usr/local/bin/" ),
123 wxT(
"/usr/local/kicad/bin/" ),
128 for(
unsigned i=0; i<
arrayDim(possibilities); ++i )
131 fullFileName = possibilities[i] + shortname;
137 if( wxFileExists( fullFileName ) )
147int ExecuteFile(
const wxString& aEditorName,
const wxString& aFileName, wxProcess* aCallback,
150 wxString fullEditorName;
151 std::vector<wxString> params;
155 bool inSingleQuotes =
false;
156 bool inDoubleQuotes =
false;
161 if( !param.IsEmpty() )
163 params.push_back( param );
168 for( wxUniChar ch : aEditorName )
175 inSingleQuotes =
false;
183 else if( inDoubleQuotes )
188 inDoubleQuotes =
false;
195 else if( ch ==
'\'' )
198 inSingleQuotes =
true;
203 inDoubleQuotes =
true;
220 fullEditorName = params[0];
222 params.erase( params.begin() );
228 fullEditorName = aEditorName;
231 if( wxFileExists( fullEditorName ) )
233 std::vector<const wchar_t*> args;
235 args.emplace_back( fullEditorName.wc_str() );
237 if( !params.empty() )
239 for(
const wxString& p : params )
240 args.emplace_back( p.wc_str() );
243 if( !aFileName.IsEmpty() )
244 args.emplace_back( aFileName.wc_str() );
246 args.emplace_back(
nullptr );
248 return wxExecute(
const_cast<wchar_t**
>( args.data() ), wxEXEC_ASYNC, aCallback );
252 msg.Printf(
_(
"Command '%s' could not be found." ), fullEditorName );
261 wxString filename = file;
265 if(
Pgm().UseSystemPdfBrowser() )
269 msg.Printf(
_(
"Unable to find a PDF viewer for '%s'." ), filename );
276 const wchar_t* args[3];
279 args[1] = filename.wc_str();
282 if( wxExecute(
const_cast<wchar_t**
>( args ) ) == -1 )
284 msg.Printf(
_(
"Problem while running the PDF viewer '%s'." ), args[0] );
294void KiCopyFile(
const wxString& aSrcPath,
const wxString& aDestPath, wxString& aErrors )
296 if( !wxCopyFile( aSrcPath, aDestPath ) )
300 if( !aErrors.IsEmpty() )
303 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
322 std::function<
bool(
const std::string& token, wxString& value )> aCallback,
325 bool success =
false;
335 if( node->IsList() && node->GetNumberOfChildren() > 1 && node->GetChild( 0 )->IsSymbol() )
337 std::string token = node->GetChild( 0 )->GetSymbol();
338 SEXPR::SEXPR_STRING* pathNode = dynamic_cast<SEXPR::SEXPR_STRING*>( node->GetChild( 1 ) );
339 SEXPR::SEXPR_SYMBOL* symNode = dynamic_cast<SEXPR::SEXPR_SYMBOL*>( node->GetChild( 1 ) );
343 path = pathNode->m_value;
345 path = symNode->m_value;
347 if( aCallback( token, path ) )
350 pathNode->m_value = path;
352 symNode->m_value = path;
362 formatter.
Print(
"%s", sexpr->AsString( 0 ).c_str() );
364 success = formatter.
Finish();
375 if( !aErrors.empty() )
376 aErrors += wxS(
"\n" );
378 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
386 return wxT(
"\"" ) + fn.GetFullPath( format ) + wxT(
"\"" );
392 namespace fs = std::filesystem;
394 std::string rmDir = aFileName.ToStdString();
396 if( rmDir.length() < 3 )
399 *aErrors =
_(
"Invalid directory name, cannot remove root" );
404 if( !fs::exists( rmDir ) )
407 *aErrors = wxString::Format(
_(
"Directory '%s' does not exist" ), aFileName );
412 fs::path
path( rmDir );
414 if( !fs::is_directory(
path ) )
417 *aErrors = wxString::Format(
_(
"'%s' is not a directory" ), aFileName );
424 fs::remove_all(
path );
426 catch(
const fs::filesystem_error& e )
429 *aErrors = wxString::Format(
_(
"Error removing directory '%s': %s" ), aFileName, e.what() );
439 const std::vector<wxString>& aPathsWithOverwriteDisallowed, wxString& aErrors )
441 wxDir dir( aSourceDir );
443 if( !dir.IsOpened() )
445 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
446 aErrors += wxT(
"\n" );
450 if( !wxFileName::Mkdir( aDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
452 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), aDestDir );
453 aErrors += wxT(
"\n" );
458 bool cont = dir.GetFirst( &filename );
462 wxString sourcePath = dir.GetNameWithSep() + filename;
463 wxString destPath = aDestDir + wxFileName::GetPathSeparator() + filename;
465 if( wxFileName::DirExists( sourcePath ) )
468 if( !
CopyDirectory( sourcePath, destPath, aPathsWithOverwriteDisallowed, aErrors ) )
474 if(
alg::contains( aPathsWithOverwriteDisallowed, sourcePath ) && wxFileExists( destPath ) )
478 else if( !wxCopyFile( sourcePath, destPath ) )
480 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), sourcePath, destPath );
485 cont = dir.GetNext( &filename );
493 wxString& aErrors, std::vector<wxString>& aPathsWritten )
496 wxFileName sourceFn( aSourcePath );
497 wxString sourcePath = sourceFn.GetFullPath();
498 bool isSourceDirectory = wxFileName::DirExists( sourcePath );
499 wxString baseDestDir = aDestDir;
502 [&](
const wxString& src,
const wxString& dest ) ->
bool
504 if( wxCopyFile( src, dest, aAllowOverwrites ) )
506 aPathsWritten.push_back( dest );
510 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), src, dest );
511 aErrors += wxT(
"\n" );
515 auto processEntries =
516 [&](
const wxString& srcDir,
const wxString& pattern,
const wxString& destDir ) ->
bool
520 if( !dir.IsOpened() )
522 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), srcDir );
523 aErrors += wxT(
"\n" );
531 bool cont = dir.GetFirst( &filename, pattern, wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN );
535 const wxString entrySrc = srcDir + wxFileName::GetPathSeparator() + filename;
536 const wxString entryDest = destDir + wxFileName::GetPathSeparator() + filename;
538 if( !filename.Matches( wxT(
"~*.lck" ) ) && !filename.Matches( wxT(
"*.lck" ) ) )
540 if( wxFileName::DirExists( entrySrc ) )
545 aErrors += wxString::Format(
_(
"Could not copy directory: %s to %s" ),
546 entrySrc, entryDest );
547 aErrors += wxT(
"\n" );
555 if( !performCopy( entrySrc, entryDest ) )
562 cont = dir.GetNext( &filename );
569 if( isSourceDirectory )
571 wxString sourceDirName = sourceFn.GetFullName();
572 baseDestDir = wxFileName( aDestDir, sourceDirName ).GetFullPath();
576 if( !wxFileName::Mkdir( baseDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
578 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), baseDestDir );
579 aErrors += wxT(
"\n" );
585 if( !isSourceDirectory )
587 const wxString fileName = sourceFn.GetFullName();
590 if( fileName.Contains(
'*' ) || fileName.Contains(
'?' ) )
592 const wxString dirPath = sourceFn.GetPath();
594 if( !wxFileName::DirExists( dirPath ) )
596 aErrors += wxString::Format(
_(
"Source directory does not exist: %s" ), dirPath );
597 aErrors += wxT(
"\n" );
603 return processEntries( dirPath, fileName, baseDestDir );
607 return performCopy( sourcePath, wxFileName( baseDestDir, fileName ).GetFullPath() );
611 return processEntries( sourcePath, wxEmptyString, baseDestDir );
616 const wxString& aParentDir )
618 wxDir dir( aSourceDir );
620 if( !dir.IsOpened() )
622 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
628 bool cont = dir.GetFirst( &filename );
632 wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
633 wxString zipPath = aParentDir + filename;
635 if( wxFileName::DirExists( sourcePath ) )
638 aZip.PutNextDirEntry( zipPath +
"/" );
647 aZip.PutNextEntry( zipPath );
648 wxFFileInputStream fileStream( sourcePath );
650 if( !fileStream.IsOk() )
652 aErrors += wxString::Format(
_(
"Could not read file: %s" ), sourcePath );
656 aZip.Write( fileStream );
659 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.