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 );
79 if( !buildDir.GetDirs().IsEmpty()
80 && buildDir.GetDirs().Last().Lower().EndsWith( wxT(
".app" ) ) )
82 buildDir.RemoveLastDir();
85 buildDir.RemoveLastDir();
87 buildDir.AppendDir( shortname );
89 buildDir.AppendDir( shortname.BeforeLast(
'.' ) );
92 if( buildDir.GetDirs().Last() ==
"pl_editor" )
94 buildDir.RemoveLastDir();
95 buildDir.AppendDir(
"pagelayout_editor" );
99 buildDir.AppendDir( shortname + wxT(
".app" ) );
100 buildDir.AppendDir( wxT(
"Contents" ) );
101 buildDir.AppendDir( wxT(
"MacOS" ) );
104 if( wxFileExists( buildDir.GetFullPath() ) )
105 return buildDir.GetFullPath();
110 if(
Pgm().IsKicadEnvVariableDefined() )
114 if( wxFileExists( fullFileName ) )
118#if defined( __WINDOWS__ )
125 const static wxChar* possibilities[] = {
126#if defined( __WXMAC__ )
128 wxT(
"Contents/Applications/pcbnew.app/Contents/MacOS/" ),
129 wxT(
"Contents/Applications/eeschema.app/Contents/MacOS/" ),
130 wxT(
"Contents/Applications/gerbview.app/Contents/MacOS/" ),
131 wxT(
"Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
132 wxT(
"Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
133 wxT(
"Contents/Applications/pl_editor.app/Contents/MacOS/" ),
136 wxT(
"/usr/local/bin/" ),
137 wxT(
"/usr/local/kicad/bin/" ),
142 for(
unsigned i=0; i<
arrayDim(possibilities); ++i )
145 fullFileName = possibilities[i] + shortname;
151 if( wxFileExists( fullFileName ) )
161int ExecuteFile(
const wxString& aEditorName,
const wxString& aFileName, wxProcess* aCallback,
164 wxString fullEditorName;
165 std::vector<wxString> params;
169 bool inSingleQuotes =
false;
170 bool inDoubleQuotes =
false;
175 if( !param.IsEmpty() )
177 params.push_back( param );
182 for( wxUniChar ch : aEditorName )
189 inSingleQuotes =
false;
197 else if( inDoubleQuotes )
202 inDoubleQuotes =
false;
209 else if( ch ==
'\'' )
212 inSingleQuotes =
true;
217 inDoubleQuotes =
true;
234 fullEditorName = params[0];
236 params.erase( params.begin() );
242 fullEditorName = aEditorName;
245 if( wxFileExists( fullEditorName ) )
247 std::vector<const wchar_t*> args;
249 args.emplace_back( fullEditorName.wc_str() );
251 if( !params.empty() )
253 for(
const wxString& p : params )
254 args.emplace_back( p.wc_str() );
257 if( !aFileName.IsEmpty() )
258 args.emplace_back( aFileName.wc_str() );
260 args.emplace_back(
nullptr );
262 return wxExecute(
const_cast<wchar_t**
>( args.data() ), wxEXEC_ASYNC, aCallback );
266 msg.Printf(
_(
"Command '%s' could not be found." ), fullEditorName );
275 wxString filename = file;
279 if(
Pgm().UseSystemPdfBrowser() )
283 msg.Printf(
_(
"Unable to find a PDF viewer for '%s'." ), filename );
290 const wchar_t* args[3];
293 args[1] = filename.wc_str();
296 if( wxExecute(
const_cast<wchar_t**
>( args ) ) == -1 )
298 msg.Printf(
_(
"Problem while running the PDF viewer '%s'." ), args[0] );
308void KiCopyFile(
const wxString& aSrcPath,
const wxString& aDestPath, wxString& aErrors )
310 if( !wxCopyFile( aSrcPath, aDestPath ) )
314 if( !aErrors.IsEmpty() )
317 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
336 std::function<
bool(
const std::string& token, wxString& value )> aCallback,
339 bool success =
false;
349 if( node->IsList() && node->GetNumberOfChildren() > 1 && node->GetChild( 0 )->IsSymbol() )
351 std::string token = node->GetChild( 0 )->GetSymbol();
352 SEXPR::SEXPR_STRING* pathNode = dynamic_cast<SEXPR::SEXPR_STRING*>( node->GetChild( 1 ) );
353 SEXPR::SEXPR_SYMBOL* symNode = dynamic_cast<SEXPR::SEXPR_SYMBOL*>( node->GetChild( 1 ) );
357 path = pathNode->m_value;
359 path = symNode->m_value;
361 if( aCallback( token, path ) )
364 pathNode->m_value = path;
366 symNode->m_value = path;
376 formatter.
Print(
"%s", sexpr->AsString( 0 ).c_str() );
378 success = formatter.
Finish();
389 if( !aErrors.empty() )
390 aErrors += wxS(
"\n" );
392 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
400 return wxT(
"\"" ) + fn.GetFullPath( format ) + wxT(
"\"" );
406 namespace fs = std::filesystem;
408 std::string rmDir = aFileName.ToStdString();
410 if( rmDir.length() < 3 )
413 *aErrors =
_(
"Invalid directory name, cannot remove root" );
418 if( !fs::exists( rmDir ) )
421 *aErrors = wxString::Format(
_(
"Directory '%s' does not exist" ), aFileName );
426 fs::path
path( rmDir );
428 if( !fs::is_directory(
path ) )
431 *aErrors = wxString::Format(
_(
"'%s' is not a directory" ), aFileName );
438 fs::remove_all(
path );
440 catch(
const fs::filesystem_error& e )
443 *aErrors = wxString::Format(
_(
"Error removing directory '%s': %s" ), aFileName, e.what() );
453 const std::vector<wxString>& aPathsWithOverwriteDisallowed, wxString& aErrors )
455 wxDir dir( aSourceDir );
457 if( !dir.IsOpened() )
459 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
460 aErrors += wxT(
"\n" );
464 if( !wxFileName::Mkdir( aDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
466 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), aDestDir );
467 aErrors += wxT(
"\n" );
472 bool cont = dir.GetFirst( &filename );
476 wxString sourcePath = dir.GetNameWithSep() + filename;
477 wxString destPath = aDestDir + wxFileName::GetPathSeparator() + filename;
479 if( wxFileName::DirExists( sourcePath ) )
482 if( !
CopyDirectory( sourcePath, destPath, aPathsWithOverwriteDisallowed, aErrors ) )
488 if(
alg::contains( aPathsWithOverwriteDisallowed, sourcePath ) && wxFileExists( destPath ) )
492 else if( !wxCopyFile( sourcePath, destPath ) )
494 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), sourcePath, destPath );
499 cont = dir.GetNext( &filename );
507 wxString& aErrors, std::vector<wxString>& aPathsWritten )
510 wxFileName sourceFn( aSourcePath );
511 wxString sourcePath = sourceFn.GetFullPath();
512 bool isSourceDirectory = wxFileName::DirExists( sourcePath );
513 wxString baseDestDir = aDestDir;
516 [&](
const wxString& src,
const wxString& dest ) ->
bool
518 if( wxCopyFile( src, dest, aAllowOverwrites ) )
520 aPathsWritten.push_back( dest );
524 aErrors += wxString::Format(
_(
"Could not copy file: %s to %s" ), src, dest );
525 aErrors += wxT(
"\n" );
529 auto processEntries =
530 [&](
const wxString& srcDir,
const wxString& pattern,
const wxString& destDir ) ->
bool
534 if( !dir.IsOpened() )
536 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), srcDir );
537 aErrors += wxT(
"\n" );
545 bool cont = dir.GetFirst( &filename, pattern, wxDIR_FILES | wxDIR_DIRS | wxDIR_HIDDEN );
549 const wxString entrySrc = srcDir + wxFileName::GetPathSeparator() + filename;
550 const wxString entryDest = destDir + wxFileName::GetPathSeparator() + filename;
552 if( !filename.Matches( wxT(
"~*.lck" ) ) && !filename.Matches( wxT(
"*.lck" ) ) )
554 if( wxFileName::DirExists( entrySrc ) )
559 aErrors += wxString::Format(
_(
"Could not copy directory: %s to %s" ),
560 entrySrc, entryDest );
561 aErrors += wxT(
"\n" );
569 if( !performCopy( entrySrc, entryDest ) )
576 cont = dir.GetNext( &filename );
583 if( isSourceDirectory )
585 wxString sourceDirName = sourceFn.GetFullName();
586 baseDestDir = wxFileName( aDestDir, sourceDirName ).GetFullPath();
590 if( !wxFileName::Mkdir( baseDestDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
592 aErrors += wxString::Format(
_(
"Could not create destination directory: %s" ), baseDestDir );
593 aErrors += wxT(
"\n" );
599 if( !isSourceDirectory )
601 const wxString fileName = sourceFn.GetFullName();
604 if( fileName.Contains(
'*' ) || fileName.Contains(
'?' ) )
606 const wxString dirPath = sourceFn.GetPath();
608 if( !wxFileName::DirExists( dirPath ) )
610 aErrors += wxString::Format(
_(
"Source directory does not exist: %s" ), dirPath );
611 aErrors += wxT(
"\n" );
617 return processEntries( dirPath, fileName, baseDestDir );
621 return performCopy( sourcePath, wxFileName( baseDestDir, fileName ).GetFullPath() );
625 return processEntries( sourcePath, wxEmptyString, baseDestDir );
630 const wxString& aParentDir )
632 wxDir dir( aSourceDir );
634 if( !dir.IsOpened() )
636 aErrors += wxString::Format(
_(
"Could not open source directory: %s" ), aSourceDir );
642 bool cont = dir.GetFirst( &filename );
646 wxString sourcePath = aSourceDir + wxFileName::GetPathSeparator() + filename;
647 wxString zipPath = aParentDir + filename;
649 if( wxFileName::DirExists( sourcePath ) )
652 aZip.PutNextDirEntry( zipPath +
"/" );
661 aZip.PutNextEntry( zipPath );
662 wxFFileInputStream fileStream( sourcePath );
664 if( !fileStream.IsOk() )
666 aErrors += wxString::Format(
_(
"Could not read file: %s" ), sourcePath );
670 aZip.Write( fileStream );
673 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.