KiCad PCB EDA Suite
gestfich.h File Reference
#include <wx/filename.h>
#include <wx/process.h>

Go to the source code of this file.

Macros

#define UNIX_STRING_DIR_SEP   wxT( "/" )
 
#define WIN_STRING_DIR_SEP   wxT( "\\" )
 

Functions

bool OpenPDF (const wxString &file)
 Run the PDF viewer and display a PDF file. More...
 
void OpenFile (const wxString &file)
 
void PrintFile (const wxString &file)
 
bool CanPrintFile (const wxString &file)
 
void KiCopyFile (const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
 
int ExecuteFile (const wxString &ExecFile, const wxString &param=wxEmptyString, wxProcess *callback=nullptr)
 Call the executable file ExecFile with the command line parameters param. More...
 
void QuoteString (wxString &string)
 Add un " to the start and the end of string (if not already done). More...
 
wxString FindKicadFile (const wxString &shortname)
 Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin. More...
 
wxString QuoteFullPath (wxFileName &fn, wxPathFormat format=wxPATH_NATIVE)
 Quote return value of wxFileName::GetFullPath(). More...
 

Macro Definition Documentation

◆ UNIX_STRING_DIR_SEP

#define UNIX_STRING_DIR_SEP   wxT( "/" )
Note
Do we really need these defined?

Definition at line 35 of file gestfich.h.

◆ WIN_STRING_DIR_SEP

#define WIN_STRING_DIR_SEP   wxT( "\\" )

Definition at line 36 of file gestfich.h.

Function Documentation

◆ CanPrintFile()

bool CanPrintFile ( const wxString &  file)

Definition at line 328 of file gestfich.cpp.

329 {
330  return doPrintFile( file, true );
331 }
bool doPrintFile(const wxString &file, bool aDryRun)
Definition: gestfich.cpp:247

References doPrintFile().

Referenced by PROJECT_TREE_PANE::onRight().

◆ ExecuteFile()

int ExecuteFile ( const wxString &  ExecFile,
const wxString &  param = wxEmptyString,
wxProcess *  callback = nullptr 
)

Call the executable file ExecFile with the command line parameters param.

Definition at line 115 of file gestfich.cpp.

116 {
117  wxString fullFileName;
118  wxString fullParams;
119  int space = ExecFile.Find( ' ' );
120 
121  if( space > 0 && !ExecFile.Contains( "\"" ) && !ExecFile.Contains( "\'" ) )
122  {
123  fullFileName = FindKicadFile( ExecFile.Mid( 0, space ) );
124  fullParams = ExecFile.Mid( space + 1 ) + wxS( " " ) + param;
125  }
126  else
127  {
128  fullFileName = FindKicadFile( ExecFile );
129  fullParams = param;
130  }
131 
132  if( wxFileExists( fullFileName ) )
133  {
134  if( !fullParams.IsEmpty() )
135  fullFileName += wxS( " " ) + fullParams;
136 
137  return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback );
138  }
139 #ifdef __WXMAC__
140  else
141  {
142  QuoteString( fullFileName );
143 
144  if( !param.IsEmpty() )
145  fullFileName += wxT( " " ) + param;
146 
147  return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback );
148  }
149 #else
150  else
151  {
152  wxString msg;
153  msg.Printf( _( "Command '%s' could not be found." ), fullFileName );
154  DisplayError( nullptr, msg, 20 );
155  return -1;
156  }
157 #endif
158 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:271
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:46
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:42
#define _(s)
wxString FindKicadFile(const wxString &shortname)
Search the executable file shortname in KiCad binary path and return full file name if found or short...
Definition: gestfich.cpp:52

References _, DisplayError(), FindKicadFile(), ProcessExecute(), and QuoteString().

Referenced by KICAD_MANAGER_CONTROL::Execute(), DIALOG_CONFIG_EQUFILES::OnEditEquFile(), DIALOG_BOM::OnEditGenerator(), SCH_EDIT_FRAME::OnOpenPcbnew(), PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor(), PCB_EDIT_FRAME::RunEeschema(), and GERBVIEW_INSPECTION_TOOL::ShowSource().

◆ FindKicadFile()

wxString FindKicadFile ( const wxString &  shortname)

Search the executable file shortname in KiCad binary path and return full file name if found or shortname if the kicad binary path is kicad/bin.

The binary path is found from:

  • binary path.
  • KICAD environment variable.
  • c:\kicad or /usr/local/kicad (the default).
  • default binary path.

Definition at line 52 of file gestfich.cpp.

53 {
54  // Test the presence of the file in the directory shortname of
55  // the KiCad binary path.
56 #ifndef __WXMAC__
57  wxString fullFileName = Pgm().GetExecutablePath() + shortname;
58 #else
59  wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
60 #endif
61  if( wxFileExists( fullFileName ) )
62  return fullFileName;
63 
64  // Test the presence of the file in the directory shortname
65  // defined by the environment variable KiCad.
66  if( Pgm().IsKicadEnvVariableDefined() )
67  {
68  fullFileName = Pgm().GetKicadEnvVariable() + shortname;
69 
70  if( wxFileExists( fullFileName ) )
71  return fullFileName;
72  }
73 
74 #if defined( __WINDOWS__ )
75  // kicad can be installed highly portably on Windows, anywhere and concurrently
76  // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
77  return shortname;
78 #endif
79 
80  // Path list for KiCad binary files
81  const static wxChar* possibilities[] = {
82 #if defined( __WXMAC__ )
83  // all internal paths are relative to main bundle kicad.app
84  wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
85  wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
86  wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
87  wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
88  wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
89  wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
90 #else
91  wxT( "/usr/bin/" ),
92  wxT( "/usr/local/bin/" ),
93  wxT( "/usr/local/kicad/bin/" ),
94 #endif
95  };
96 
97  // find binary file from possibilities list:
98  for( unsigned i=0; i<arrayDim(possibilities); ++i )
99  {
100 #ifndef __WXMAC__
101  fullFileName = possibilities[i] + shortname;
102 #else
103  // make relative paths absolute
104  fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
105 #endif
106 
107  if( wxFileExists( fullFileName ) )
108  return fullFileName;
109  }
110 
111  return shortname;
112 }
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31

References arrayDim(), and Pgm().

Referenced by ExecuteFile().

◆ KiCopyFile()

void KiCopyFile ( const wxString &  aSrcPath,
const wxString &  aDestPath,
wxString &  aErrors 
)
Parameters
aSrcPathis the full filename of the source.
aDestPathis the full filename of the target
aErrorsa wxString to append any errors to

Definition at line 334 of file gestfich.cpp.

335 {
336  if( !wxCopyFile( aSrcPath, aDestPath ) )
337  {
338  wxString msg;
339 
340  if( !aErrors.IsEmpty() )
341  aErrors += "\n";
342 
343  msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
344  aErrors += msg;
345  }
346 }
#define _(s)

References _.

Referenced by CreateWindow(), IfaceOrAddress(), SAVE_AS_TRAVERSER::OnFile(), MIGRATION_TRAVERSER::OnFile(), SCH::IFACE::SaveFileAs(), PCB_EDIT_FRAME::SavePcbCopy(), and PCB_EDIT_FRAME::SavePcbFile().

◆ OpenFile()

void OpenFile ( const wxString &  file)

Definition at line 228 of file gestfich.cpp.

229 {
230  wxFileName fileName( file );
231  wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
232 
233  if( !filetype )
234  return;
235 
236  wxString command;
237  wxFileType::MessageParameters params( file );
238 
239  filetype->GetOpenCommand( &command, params );
240  delete filetype;
241 
242  if( !command.IsEmpty() )
243  ProcessExecute( command );
244 }
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:46

References ProcessExecute().

Referenced by PROJECT_TREE_ITEM::Activate().

◆ OpenPDF()

bool OpenPDF ( const wxString &  file)

Run the PDF viewer and display a PDF file.

Parameters
filethe PDF file to open.
Returns
true is success or false if no PDF viewer found.

Definition at line 161 of file gestfich.cpp.

162 {
163  wxString command;
164  wxString filename = file;
165 
166  Pgm().ReadPdfBrowserInfos();
167 
168  if( !Pgm().UseSystemPdfBrowser() ) // Run the preferred PDF Browser
169  {
170 #ifdef __WXMSW__
171  // Windows requires double quotes around the filename to handle spaces
172  command = Pgm().GetPdfBrowserName() + wxT( " \"" ) + filename + wxT( "\"" );
173 #else
174  command = Pgm().GetPdfBrowserName() + wxT( " '" ) + filename + wxT( "'" );
175 #endif
176  }
177  else
178  {
179  // wxLaunchDefaultApplication on Unix systems is run as an external process passing
180  // the filename to the appropriate application as a command argument.
181  // depending on wxWidgets version, spaces in the path and/or file name will cause
182  // argument parsing issues so always quote the filename and path.
183  // This is applicable to all Unix platforms with wxWidgets version < 3.1.0.
184  // See https://github.com/wxWidgets/wxWidgets/blob/master/src/unix/utilsx11.cpp#L2654
185 #ifdef __WXGTK__
186  #if !wxCHECK_VERSION( 3, 1, 0 )
187  // Quote in case there are spaces in the path.
188  // Not needed on 3.1.4, but needed in 3.0 versions
189  // Moreover, on Linux, on 3.1.4 wx version, adding quotes breaks wxLaunchDefaultApplication
190  QuoteString( filename );
191  #endif
192 #endif
193 
194  if( wxLaunchDefaultApplication( filename ) )
195  return true;
196 
197 #ifdef __WXMAC__
198  command = wxT( "/usr/bin/open -a '" ) + file + wxT( "'" );
199 #endif
200  // If launching the system default PDF viewer fails, fall through with empty command
201  // string so the error message is displayed.
202  }
203 
204  if( !command.IsEmpty() )
205  {
206  if( ProcessExecute( command ) != -1 )
207  {
208  return true;
209  }
210  else
211  {
212  wxString msg;
213  msg.Printf( _( "Problem while running the PDF viewer.\nCommand is '%s'." ), command );
214  DisplayError( nullptr, msg );
215  }
216  }
217  else
218  {
219  wxString msg;
220  msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), file );
221  DisplayError( nullptr, msg );
222  }
223 
224  return false;
225 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:271
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:46
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:42
#define _(s)

References _, DisplayError(), Pgm(), ProcessExecute(), and QuoteString().

Referenced by PROJECT_TREE_ITEM::Activate(), and GetAssociatedDocument().

◆ PrintFile()

void PrintFile ( const wxString &  file)

Definition at line 322 of file gestfich.cpp.

323 {
324  doPrintFile( file, false );
325 }
bool doPrintFile(const wxString &file, bool aDryRun)
Definition: gestfich.cpp:247

References doPrintFile().

Referenced by PROJECT_TREE_ITEM::Print().

◆ QuoteFullPath()

wxString QuoteFullPath ( wxFileName &  fn,
wxPathFormat  format = wxPATH_NATIVE 
)

Quote return value of wxFileName::GetFullPath().

This allows file name paths with spaces to be used as parameters to ProcessExecute function calls.

Parameters
fnis the filename to wrap.
formatif provided, can be used to transform the nature of the wrapped filename to another platform.

Definition at line 349 of file gestfich.cpp.

350 {
351  return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
352 }

Referenced by SCH_EDIT_FRAME::OnOpenPcbnew(), and GERBVIEW_INSPECTION_TOOL::ShowSource().

◆ QuoteString()

void QuoteString ( wxString &  string)

Add un " to the start and the end of string (if not already done).

Parameters
stringstring to modify.

Definition at line 42 of file gestfich.cpp.

43 {
44  if( !string.StartsWith( wxT( "\"" ) ) )
45  {
46  string.Prepend ( wxT( "\"" ) );
47  string.Append ( wxT( "\"" ) );
48  }
49 }

Referenced by KICAD_MANAGER_CONTROL::Execute(), ExecuteFile(), LaunchExternal(), DIALOG_BOM::OnEditGenerator(), PROJECT_TREE_PANE::onOpenDirectory(), PROJECT_TREE_PANE::onOpenSelectedFileWithTextEditor(), and OpenPDF().