KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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, [email protected]
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
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
35#include <pgm_base.h>
36#include <confirm.h>
37#include <core/arraydim.h>
38#include <gestfich.h>
39#include <string_utils.h>
40#include <launch_ext.h>
41#include "wx/tokenzr.h"
42
43void QuoteString( wxString& string )
44{
45 if( !string.StartsWith( wxT( "\"" ) ) )
46 {
47 string.Prepend ( wxT( "\"" ) );
48 string.Append ( wxT( "\"" ) );
49 }
50}
51
52
53wxString FindKicadFile( const wxString& shortname )
54{
55 // Test the presence of the file in the directory shortname of
56 // the KiCad binary path.
57#ifndef __WXMAC__
58 wxString fullFileName = Pgm().GetExecutablePath() + shortname;
59#else
60 wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
61#endif
62 if( wxFileExists( fullFileName ) )
63 return fullFileName;
64
65 // Test the presence of the file in the directory shortname
66 // defined by the environment variable KiCad.
67 if( Pgm().IsKicadEnvVariableDefined() )
68 {
69 fullFileName = Pgm().GetKicadEnvVariable() + shortname;
70
71 if( wxFileExists( fullFileName ) )
72 return fullFileName;
73 }
74
75#if defined( __WINDOWS__ )
76 // kicad can be installed highly portably on Windows, anywhere and concurrently
77 // either the "kicad file" is immediately adjacent to the exe or it's not a valid install
78 return shortname;
79#else
80
81 // Path list for KiCad binary files
82 const static wxChar* possibilities[] = {
83#if defined( __WXMAC__ )
84 // all internal paths are relative to main bundle kicad.app
85 wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
86 wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
87 wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
88 wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
89 wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
90 wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
91#else
92 wxT( "/usr/bin/" ),
93 wxT( "/usr/local/bin/" ),
94 wxT( "/usr/local/kicad/bin/" ),
95#endif
96 };
97
98 // find binary file from possibilities list:
99 for( unsigned i=0; i<arrayDim(possibilities); ++i )
100 {
101#ifndef __WXMAC__
102 fullFileName = possibilities[i] + shortname;
103#else
104 // make relative paths absolute
105 fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
106#endif
107
108 if( wxFileExists( fullFileName ) )
109 return fullFileName;
110 }
111
112 return shortname;
113
114#endif
115}
116
117
118int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess *aCallback )
119{
120 wxString fullEditorName;
121 std::vector<wxString> params;
122
123#ifdef __UNIX__
124 wxString param;
125 bool inSingleQuotes = false;
126 bool inDoubleQuotes = false;
127
128 auto pushParam =
129 [&]()
130 {
131 if( !param.IsEmpty() )
132 {
133 params.push_back( param );
134 param.clear();
135 }
136 };
137
138 for( wxUniChar ch : aEditorName )
139 {
140 if( inSingleQuotes )
141 {
142 if( ch == '\'' )
143 {
144 pushParam();
145 inSingleQuotes = false;
146 continue;
147 }
148 else
149 {
150 param += ch;
151 }
152 }
153 else if( inDoubleQuotes )
154 {
155 if( ch == '"' )
156 {
157 pushParam();
158 inDoubleQuotes = false;
159 }
160 else
161 {
162 param += ch;
163 }
164 }
165 else if( ch == '\'' )
166 {
167 pushParam();
168 inSingleQuotes = true;
169 }
170 else if( ch == '"' )
171 {
172 pushParam();
173 inDoubleQuotes = true;
174 }
175 else if( ch == ' ' )
176 {
177 pushParam();
178 }
179 else
180 {
181 param += ch;
182 }
183 }
184
185 pushParam();
186
187 fullEditorName = FindKicadFile( params[0] );
188 params.erase( params.begin() );
189#else
190 fullEditorName = FindKicadFile( aEditorName );
191#endif
192
193 if( wxFileExists( fullEditorName ) )
194 {
195 int i = 0;
196 const wchar_t* args[4];
197
198 args[i++] = fullEditorName.wc_str();
199
200 if( !params.empty() )
201 {
202 for( const wxString& p : params )
203 args[i++] = p.wc_str();
204 }
205
206 if( !aFileName.IsEmpty() )
207 args[i++] = aFileName.wc_str();
208
209 args[i] = nullptr;
210
211 return wxExecute( const_cast<wchar_t**>( args ), wxEXEC_ASYNC, aCallback );
212 }
213
214 wxString msg;
215 msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
216 DisplayError( nullptr, msg, 20 );
217 return -1;
218}
219
220
221bool OpenPDF( const wxString& file )
222{
223 wxString msg;
224 wxString filename = file;
225
226 Pgm().ReadPdfBrowserInfos();
227
228 if( Pgm().UseSystemPdfBrowser() )
229 {
230 if( !LaunchExternal( filename ) )
231 {
232 msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
233 DisplayError( nullptr, msg );
234 return false;
235 }
236 }
237 else
238 {
239 const wchar_t* args[3];
240
241 args[0] = Pgm().GetPdfBrowserName().wc_str();
242 args[1] = filename.wc_str();
243 args[2] = nullptr;
244
245 if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
246 {
247 msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
248 DisplayError( nullptr, msg );
249 return false;
250 }
251 }
252
253 return true;
254}
255
256
257void OpenFile( const wxString& file )
258{
259 wxFileName fileName( file );
260 wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
261
262 if( !filetype )
263 return;
264
265 wxString command;
266 wxFileType::MessageParameters params( file );
267
268 filetype->GetOpenCommand( &command, params );
269 delete filetype;
270
271 if( !command.IsEmpty() )
272 wxExecute( command );
273}
274
275
276void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
277{
278 if( !wxCopyFile( aSrcPath, aDestPath ) )
279 {
280 wxString msg;
281
282 if( !aErrors.IsEmpty() )
283 aErrors += "\n";
284
285 msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
286 aErrors += msg;
287 }
288}
289
290
291wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
292{
293 return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
294}
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:280
This file is part of the common library.
#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:53
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition: gestfich.cpp:291
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:221
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:276
void OpenFile(const wxString &file)
Definition: gestfich.cpp:257
void QuoteString(wxString &string)
Add un " to the start and the end of string (if not already done).
Definition: gestfich.cpp:43
int ExecuteFile(const wxString &aEditorName, const wxString &aFileName, wxProcess *aCallback)
Call the executable file aEditorName with the parameter aFileName.
Definition: gestfich.cpp:118
bool LaunchExternal(const wxString &aPath)
Launches the given file or folder in the host OS.
Definition: launch_ext.cpp:25
see class PGM_BASE
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:115