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-2017 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 1992-2020 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 <fctsys.h>
32 #include <wx/mimetype.h>
33 #include <wx/filename.h>
34 #include <wx/dir.h>
35 #include <pgm_base.h>
36 #include <confirm.h>
37 #include <common.h>
38 #include <macros.h>
39 
40 #include <gestfich.h>
41 
42 void AddDelimiterString( wxString& string )
43 {
44  if( !string.StartsWith( wxT( "\"" ) ) )
45  {
46  string.Prepend ( wxT( "\"" ) );
47  string.Append ( wxT( "\"" ) );
48  }
49 }
50 
51 
52 wxString EDA_FILE_SELECTOR( const wxString& aTitle,
53  const wxString& aPath,
54  const wxString& aFileName,
55  const wxString& aExtension,
56  const wxString& aWildcard,
57  wxWindow* aParent,
58  int aStyle,
59  const bool aKeepWorkingDirectory,
60  const wxPoint& aPosition,
61  wxString* aMruPath )
62 {
63  wxString fullfilename;
64  wxString curr_cwd = wxGetCwd();
65  wxString defaultname = aFileName;
66  wxString defaultpath = aPath;
67  wxString dotted_Ext = wxT(".") + aExtension;
68 
69 #ifdef __WINDOWS__
70  defaultname.Replace( wxT( "/" ), wxT( "\\" ) );
71  defaultpath.Replace( wxT( "/" ), wxT( "\\" ) );
72 #endif
73 
74  if( defaultpath.IsEmpty() )
75  {
76  if( aMruPath == NULL )
77  defaultpath = wxGetCwd();
78  else
79  defaultpath = *aMruPath;
80  }
81 
82  wxSetWorkingDirectory( defaultpath );
83 
84  fullfilename = wxFileSelector( aTitle, defaultpath, defaultname,
85  dotted_Ext, aWildcard,
86  aStyle, // open mode wxFD_OPEN, wxFD_SAVE ..
87  aParent, aPosition.x, aPosition.y );
88 
89  if( aKeepWorkingDirectory )
90  wxSetWorkingDirectory( curr_cwd );
91 
92  if( !fullfilename.IsEmpty() && aMruPath )
93  {
94  wxFileName fn = fullfilename;
95  *aMruPath = fn.GetPath();
96  }
97 
98  return fullfilename;
99 }
100 
101 
102 wxString FindKicadFile( const wxString& shortname )
103 {
104  // Test the presence of the file in the directory shortname of
105  // the KiCad binary path.
106 #ifndef __WXMAC__
107  wxString fullFileName = Pgm().GetExecutablePath() + shortname;
108 #else
109  wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
110 #endif
111  if( wxFileExists( fullFileName ) )
112  return fullFileName;
113 
114  // Test the presence of the file in the directory shortname
115  // defined by the environment variable KiCad.
116  if( Pgm().IsKicadEnvVariableDefined() )
117  {
118  fullFileName = Pgm().GetKicadEnvVariable() + shortname;
119 
120  if( wxFileExists( fullFileName ) )
121  return fullFileName;
122  }
123 
124  // Path list for KiCad binary files
125  const static wxChar* possibilities[] = {
126 #if defined( __WINDOWS__ )
127  wxT( "c:/kicad/bin/" ),
128  wxT( "d:/kicad/bin/" ),
129  wxT( "c:/Program Files/kicad/bin/" ),
130  wxT( "d:/Program Files/kicad/bin/" ),
131 #elif defined( __WXMAC__ )
132  // all internal paths are relative to main bundle kicad.app
133  wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
134  wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
135  wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
136  wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
137  wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
138  wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
139 #else
140  wxT( "/usr/bin/" ),
141  wxT( "/usr/local/bin/" ),
142  wxT( "/usr/local/kicad/bin/" ),
143 #endif
144  };
145 
146  // find binary file from possibilities list:
147  for( unsigned i=0; i<arrayDim(possibilities); ++i )
148  {
149 #ifndef __WXMAC__
150  fullFileName = possibilities[i] + shortname;
151 #else
152  // make relative paths absolute
153  fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
154 #endif
155 
156  if( wxFileExists( fullFileName ) )
157  return fullFileName;
158  }
159 
160  return shortname;
161 }
162 
163 
164 int ExecuteFile( wxWindow* frame, const wxString& ExecFile, const wxString& param,
165  wxProcess *callback )
166 {
167  wxString fullFileName = FindKicadFile( ExecFile );
168 
169  if( wxFileExists( fullFileName ) )
170  {
171  if( !param.IsEmpty() )
172  fullFileName += wxT( " " ) + param;
173 
174  return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback );
175  }
176 #ifdef __WXMAC__
177  else
178  {
179  AddDelimiterString( fullFileName );
180 
181  if( !param.IsEmpty() )
182  fullFileName += wxT( " " ) + param;
183 
184  return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback );
185  }
186 #endif
187 
188  wxString msg;
189  msg.Printf( _( "Command \"%s\" could not found" ), GetChars( fullFileName ) );
190  DisplayError( frame, msg, 20 );
191  return -1;
192 }
193 
194 
195 bool OpenPDF( const wxString& file )
196 {
197  wxString command;
198  const wxString& filename = file;
199 
200  Pgm().ReadPdfBrowserInfos();
201 
202  if( !Pgm().UseSystemPdfBrowser() ) // Run the preferred PDF Browser
203  {
204 #ifdef __WXMSW__
205  // Windows requires double quotes around the filename to handle spaces
206  command = Pgm().GetPdfBrowserName() + wxT( " \"" ) + filename + wxT( "\"" );
207 #else
208  command = Pgm().GetPdfBrowserName() + wxT( " '" ) + filename + wxT( "'" );
209 #endif
210  }
211  else
212  {
213  if( wxLaunchDefaultApplication( filename ) )
214  return true;
215 
216 #ifdef __WXMAC__
217  command = wxT( "/usr/bin/open -a '" ) + filename + wxT( "'" );
218 #endif
219  // If launching the system default PDF viewer fails, fall through with empty command
220  // string so the error message is displayed.
221  }
222 
223  if( !command.IsEmpty() )
224  {
225  if( ProcessExecute( command ) != -1 )
226  {
227  return true;
228  }
229  else
230  {
231  wxString msg;
232  msg.Printf( _( "Problem while running the PDF viewer\nCommand is \"%s\"" ), command );
233  DisplayError( NULL, msg );
234  }
235  }
236  else
237  {
238  wxString msg;
239  msg.Printf( _( "Unable to find a PDF viewer for \"%s\"" ), file );
240  DisplayError( NULL, msg );
241  }
242 
243  return false;
244 }
245 
246 
247 void OpenFile( const wxString& file )
248 {
249  wxFileName fileName( file );
250  wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
251 
252  if( !filetype )
253  return;
254 
255  wxString command;
256  wxFileType::MessageParameters params( file );
257 
258  filetype->GetOpenCommand( &command, params );
259  delete filetype;
260 
261  if( !command.IsEmpty() )
262  ProcessExecute( command );
263 }
264 
265 
266 bool doPrintFile( const wxString& file, bool aDryRun )
267 {
268  wxFileName fileName( file );
269  wxString ext = fileName.GetExt();
270  wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( ext );
271 
272  if( !filetype )
273  return false;
274 
275  wxString printCommand;
276  wxString openCommand;
277  wxString application;
278 
279  wxFileType::MessageParameters params( file );
280  filetype->GetPrintCommand( &printCommand, params );
281  filetype->GetOpenCommand( &openCommand, params );
282  delete filetype;
283 
284  if( !printCommand.IsEmpty() )
285  {
286  if( !aDryRun )
287  ProcessExecute( printCommand );
288 
289  return true;
290  }
291 
292 #ifdef __WXMAC__
293  if( ext == "ps" || ext == "pdf" )
294  application = "Preview";
295  else if( ext == "csv" )
296  application = "Numbers";
297  else if( ext == "txt" || ext == "rpt" || ext == "pos" || ext == "cmp" || ext == "net" )
298  application = "TextEdit";
299 
300  if( !application.IsEmpty() )
301  {
302  printCommand.Printf( "osascript -e 'tell application \"%s\"' "
303  "-e ' set srcFileRef to (open POSIX file \"%s\")' "
304  "-e ' activate' "
305  "-e ' print srcFileRef print dialog true' "
306  "-e 'end tell' ",
307  application,
308  file );
309 
310  if( !aDryRun )
311  system( printCommand.c_str() );
312 
313  return true;
314  }
315 #endif
316 
317 #ifdef __WXGTK__
318  if( ext == "ps" || ext == "pdf"
319  || ext == "csv"
320  || ext == "txt" || ext == "rpt" || ext == "pos" || ext == "cmp" || ext == "net" )
321  {
322  printCommand.Printf( "lp \"%s\"", file );
323 
324  if( !aDryRun )
325  ProcessExecute( printCommand );
326 
327  return true;
328  }
329 #endif
330 
331  if( !aDryRun )
332  {
333  DisplayError( nullptr, wxString::Format( _( "Cannot print '%s'.\n\nUnknown filetype." ),
334  file ) );
335  }
336 
337  return false;
338 }
339 
340 
341 void PrintFile( const wxString& file )
342 {
343  doPrintFile( file, false );
344 }
345 
346 
347 bool CanPrintFile( const wxString& file )
348 {
349  return doPrintFile( file, true );
350 }
351 
352 
353 void CopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
354 {
355  if( !wxCopyFile( aSrcPath, aDestPath ) )
356  {
357  wxString msg;
358 
359  if( !aErrors.IsEmpty() )
360  aErrors += "\n";
361 
362  msg.Printf( _( "Cannot copy file \"%s\"." ), aDestPath );
363  aErrors += msg;
364  }
365 }
366 
367 
368 wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
369 {
370  return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
371 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:366
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:104
bool doPrintFile(const wxString &file, bool aDryRun)
Definition: gestfich.cpp:266
This file is part of the common library TODO brief description.
This file is part of the common library.
void CopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Function CopyFile.
Definition: gestfich.cpp:353
This file contains miscellaneous commonly used macros and functions.
#define NULL
wxString EDA_FILE_SELECTOR(const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition, wxString *aMruPath)
Function EDA_FILE_SELECTOR.
Definition: gestfich.cpp:52
void AddDelimiterString(wxString &string)
Function AddDelimiterString Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:42
wxString FindKicadFile(const wxString &shortname)
Function FindKicadFile searches the executable file shortname in KiCad binary path and return full fi...
Definition: gestfich.cpp:102
void PrintFile(const wxString &file)
Definition: gestfich.cpp:341
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:160
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:153
see class PGM_BASE
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition: gestfich.cpp:368
#define _(s)
Definition: 3d_actions.cpp:33
The common library.
void OpenFile(const wxString &file)
Definition: gestfich.cpp:247
int ExecuteFile(wxWindow *frame, const wxString &ExecFile, const wxString &param, wxProcess *callback)
Function ExecuteFile calls the executable file ExecFile with the command line parameters param.
Definition: gestfich.cpp:164
bool CanPrintFile(const wxString &file)
Definition: gestfich.cpp:347
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:195