KiCad PCB EDA Suite
gestfich.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
5  * Copyright (C) 2008 Wayne Stambaugh <stambaughw@gmail.com>
6  * Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
31 #include <wx/mimetype.h>
32 #include <wx/filename.h>
33 #include <wx/dir.h>
34 #include <wx/filedlg.h>
35 
36 #include <pgm_base.h>
37 #include <common.h>
38 #include <confirm.h>
39 #include <core/arraydim.h>
40 #include <gestfich.h>
41 
42 void QuoteString( wxString& string )
43 {
44  if( !string.StartsWith( wxT( "\"" ) ) )
45  {
46  string.Prepend ( wxT( "\"" ) );
47  string.Append ( wxT( "\"" ) );
48  }
49 }
50 
51 
52 wxString FindKicadFile( const wxString& shortname )
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 }
113 
114 
115 int ExecuteFile( const wxString& ExecFile, const wxString& param, wxProcess *callback )
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 }
159 
160 
161 bool OpenPDF( const wxString& file )
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 }
226 
227 
228 void OpenFile( const wxString& file )
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 }
245 
246 
247 bool doPrintFile( const wxString& file, bool aDryRun )
248 {
249  wxFileName fileName( file );
250  wxString ext = fileName.GetExt();
251  wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( ext );
252 
253  if( !filetype )
254  return false;
255 
256  wxString printCommand;
257  wxString openCommand;
258  wxString application;
259 
260  wxFileType::MessageParameters params( file );
261  filetype->GetPrintCommand( &printCommand, params );
262  filetype->GetOpenCommand( &openCommand, params );
263  delete filetype;
264 
265  if( !printCommand.IsEmpty() )
266  {
267  if( !aDryRun )
268  ProcessExecute( printCommand );
269 
270  return true;
271  }
272 
273 #ifdef __WXMAC__
274  if( ext == "ps" || ext == "pdf" )
275  application = "Preview";
276  else if( ext == "csv" )
277  application = "Numbers";
278  else if( ext == "txt" || ext == "rpt" || ext == "pos" || ext == "cmp" || ext == "net" )
279  application = "TextEdit";
280 
281  if( !application.IsEmpty() )
282  {
283  printCommand.Printf( "osascript -e 'tell application \"%s\"' "
284  "-e ' set srcFileRef to (open POSIX file \"%s\")' "
285  "-e ' activate' "
286  "-e ' print srcFileRef print dialog true' "
287  "-e 'end tell' ",
288  application,
289  file );
290 
291  if( !aDryRun )
292  system( printCommand.c_str() );
293 
294  return true;
295  }
296 #endif
297 
298 #ifdef __WXGTK__
299  if( ext == "ps" || ext == "pdf"
300  || ext == "csv"
301  || ext == "txt" || ext == "rpt" || ext == "pos" || ext == "cmp" || ext == "net" )
302  {
303  printCommand.Printf( "lp \"%s\"", file );
304 
305  if( !aDryRun )
306  ProcessExecute( printCommand );
307 
308  return true;
309  }
310 #endif
311 
312  if( !aDryRun )
313  {
314  DisplayError( nullptr, wxString::Format( _( "Cannot print '%s'.\n\nUnknown file type." ),
315  file ) );
316  }
317 
318  return false;
319 }
320 
321 
322 void PrintFile( const wxString& file )
323 {
324  doPrintFile( file, false );
325 }
326 
327 
328 bool CanPrintFile( const wxString& file )
329 {
330  return doPrintFile( file, true );
331 }
332 
333 
334 void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
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 }
347 
348 
349 wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
350 {
351  return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
352 }
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
bool doPrintFile(const wxString &file, bool aDryRun)
Definition: gestfich.cpp:247
This file is part of the common library.
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:334
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
int ExecuteFile(const wxString &ExecFile, const wxString &param, wxProcess *callback)
Call the executable file ExecFile with the command line parameters param.
Definition: gestfich.cpp:115
#define _(s)
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
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
void PrintFile(const wxString &file)
Definition: gestfich.cpp:322
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
see class PGM_BASE
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition: gestfich.cpp:349
The common library.
void OpenFile(const wxString &file)
Definition: gestfich.cpp:228
bool CanPrintFile(const wxString &file)
Definition: gestfich.cpp:328
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:161