31#include <wx/mimetype.h>
40#include "wx/tokenzr.h"
46 if( !
string.StartsWith( wxT(
"\"" ) ) )
48 string.Prepend ( wxT(
"\"" ) );
49 string.Append ( wxT(
"\"" ) );
63 if( wxFileExists( fullFileName ) )
66 if( wxGetEnv( wxT(
"KICAD_RUN_FROM_BUILD_DIR" ),
nullptr ) )
68 wxFileName buildDir(
Pgm().GetExecutablePath(), shortname );
69 buildDir.RemoveLastDir();
71 buildDir.AppendDir( shortname );
73 buildDir.AppendDir( shortname.BeforeLast(
'.' ) );
76 if( buildDir.GetDirs().Last() ==
"pl_editor" )
78 buildDir.RemoveLastDir();
79 buildDir.AppendDir(
"pagelayout_editor" );
82 if( wxFileExists( buildDir.GetFullPath() ) )
83 return buildDir.GetFullPath();
88 if(
Pgm().IsKicadEnvVariableDefined() )
92 if( wxFileExists( fullFileName ) )
96#if defined( __WINDOWS__ )
103 const static wxChar* possibilities[] = {
104#if defined( __WXMAC__ )
106 wxT(
"Contents/Applications/pcbnew.app/Contents/MacOS/" ),
107 wxT(
"Contents/Applications/eeschema.app/Contents/MacOS/" ),
108 wxT(
"Contents/Applications/gerbview.app/Contents/MacOS/" ),
109 wxT(
"Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
110 wxT(
"Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
111 wxT(
"Contents/Applications/pl_editor.app/Contents/MacOS/" ),
114 wxT(
"/usr/local/bin/" ),
115 wxT(
"/usr/local/kicad/bin/" ),
120 for(
unsigned i=0; i<
arrayDim(possibilities); ++i )
123 fullFileName = possibilities[i] + shortname;
129 if( wxFileExists( fullFileName ) )
139int ExecuteFile(
const wxString& aEditorName,
const wxString& aFileName, wxProcess* aCallback,
142 wxString fullEditorName;
143 std::vector<wxString> params;
147 bool inSingleQuotes =
false;
148 bool inDoubleQuotes =
false;
153 if( !param.IsEmpty() )
155 params.push_back( param );
160 for( wxUniChar ch : aEditorName )
167 inSingleQuotes =
false;
175 else if( inDoubleQuotes )
180 inDoubleQuotes =
false;
187 else if( ch ==
'\'' )
190 inSingleQuotes =
true;
195 inDoubleQuotes =
true;
212 fullEditorName = params[0];
214 params.erase( params.begin() );
220 fullEditorName = aEditorName;
223 if( wxFileExists( fullEditorName ) )
225 std::vector<const wchar_t*> args;
227 args.emplace_back( fullEditorName.wc_str() );
229 if( !params.empty() )
231 for(
const wxString& p : params )
232 args.emplace_back( p.wc_str() );
235 if( !aFileName.IsEmpty() )
236 args.emplace_back( aFileName.wc_str() );
238 args.emplace_back(
nullptr );
240 return wxExecute(
const_cast<wchar_t**
>( args.data() ), wxEXEC_ASYNC, aCallback );
244 msg.Printf(
_(
"Command '%s' could not be found." ), fullEditorName );
253 wxString filename = file;
257 if(
Pgm().UseSystemPdfBrowser() )
261 msg.Printf(
_(
"Unable to find a PDF viewer for '%s'." ), filename );
268 const wchar_t* args[3];
271 args[1] = filename.wc_str();
274 if( wxExecute(
const_cast<wchar_t**
>( args ) ) == -1 )
276 msg.Printf(
_(
"Problem while running the PDF viewer '%s'." ), args[0] );
288 wxFileName fileName( file );
289 wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
295 wxFileType::MessageParameters params( file );
297 filetype->GetOpenCommand( &command, params );
300 if( !command.IsEmpty() )
301 wxExecute( command );
305void KiCopyFile(
const wxString& aSrcPath,
const wxString& aDestPath, wxString& aErrors )
307 if( !wxCopyFile( aSrcPath, aDestPath ) )
311 if( !aErrors.IsEmpty() )
314 msg.Printf(
_(
"Cannot copy file '%s'." ), aDestPath );
322 return wxT(
"\"" ) + fn.GetFullPath( format ) + wxT(
"\"" );
328 namespace fs = std::filesystem;
330 std::string rmDir = aFileName.ToStdString();
332 if( rmDir.length() < 3 )
335 *aErrors =
_(
"Invalid directory name, cannot remove root" );
340 if( !fs::exists( rmDir ) )
343 *aErrors = wxString::Format(
_(
"Directory '%s' does not exist" ), aFileName );
348 fs::path
path( rmDir );
350 if( !fs::is_directory(
path ) )
353 *aErrors = wxString::Format(
_(
"'%s' is not a directory" ), aFileName );
360 fs::remove_all(
path );
362 catch(
const fs::filesystem_error& e )
365 *aErrors = wxString::Format(
_(
"Error removing directory '%s': %s" ), aFileName, e.what() );
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
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box 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().
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)
Removes the directory aDirName and all its contents including subdirectories and their files.
void OpenFile(const wxString &file)
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
PGM_BASE & Pgm()
The global Program "get" accessor.