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, [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
42void QuoteString( wxString& string )
43{
44 if( !string.StartsWith( wxT( "\"" ) ) )
45 {
46 string.Prepend ( wxT( "\"" ) );
47 string.Append ( wxT( "\"" ) );
48 }
49}
50
51
52wxString 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
115int ExecuteFile( const wxString& aEditorName, const wxString& aFileName, wxProcess *aCallback )
116{
117 wxString fullEditorName;
118 wxString param;
119
120#ifdef __UNIX__
121 int space = aEditorName.Find( ' ' );
122
123 if( space > 0 && !aEditorName.Contains( "\"" ) && !aEditorName.Contains( "\'" ) )
124 {
125 fullEditorName = FindKicadFile( aEditorName.Mid( 0, space ) );
126 param = aEditorName.Mid( space + 1 );
127 }
128 else
129#endif
130 {
131 fullEditorName = FindKicadFile( aEditorName );
132 }
133
134 if( wxFileExists( fullEditorName ) )
135 {
136 int i = 0;
137 const wchar_t* args[4];
138
139 args[i++] = fullEditorName.wc_str();
140
141 if( !param.IsEmpty() )
142 args[i++] = param.wc_str();
143
144 if( !aFileName.IsEmpty() )
145 args[i++] = aFileName.wc_str();
146
147 args[i] = nullptr;
148
149 return wxExecute( const_cast<wchar_t**>( args ), wxEXEC_ASYNC, aCallback );
150 }
151
152 wxString msg;
153 msg.Printf( _( "Command '%s' could not be found." ), fullEditorName );
154 DisplayError( nullptr, msg, 20 );
155 return -1;
156}
157
158
159bool OpenPDF( const wxString& file )
160{
161 wxString msg;
162 wxString filename = file;
163
164 Pgm().ReadPdfBrowserInfos();
165
166 if( Pgm().UseSystemPdfBrowser() )
167 {
168 if( !LaunchExternal( filename ) )
169 {
170 msg.Printf( _( "Unable to find a PDF viewer for '%s'." ), filename );
171 DisplayError( nullptr, msg );
172 return false;
173 }
174 }
175 else
176 {
177 const wchar_t* args[3];
178
179 args[0] = Pgm().GetPdfBrowserName().wc_str();
180 args[1] = filename.wc_str();
181 args[2] = nullptr;
182
183 if( wxExecute( const_cast<wchar_t**>( args ) ) == -1 )
184 {
185 msg.Printf( _( "Problem while running the PDF viewer '%s'." ), args[0] );
186 DisplayError( nullptr, msg );
187 return false;
188 }
189 }
190
191 return true;
192}
193
194
195void OpenFile( const wxString& file )
196{
197 wxFileName fileName( file );
198 wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( fileName.GetExt() );
199
200 if( !filetype )
201 return;
202
203 wxString command;
204 wxFileType::MessageParameters params( file );
205
206 filetype->GetOpenCommand( &command, params );
207 delete filetype;
208
209 if( !command.IsEmpty() )
210 wxExecute( command );
211}
212
213
214void KiCopyFile( const wxString& aSrcPath, const wxString& aDestPath, wxString& aErrors )
215{
216 if( !wxCopyFile( aSrcPath, aDestPath ) )
217 {
218 wxString msg;
219
220 if( !aErrors.IsEmpty() )
221 aErrors += "\n";
222
223 msg.Printf( _( "Cannot copy file '%s'." ), aDestPath );
224 aErrors += msg;
225 }
226}
227
228
229wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
230{
231 return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
232}
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:300
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:52
wxString QuoteFullPath(wxFileName &fn, wxPathFormat format)
Quote return value of wxFileName::GetFullPath().
Definition: gestfich.cpp:229
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:159
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition: gestfich.cpp:214
void OpenFile(const wxString &file)
Definition: gestfich.cpp:195
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 &aEditorName, const wxString &aFileName, wxProcess *aCallback)
Call the executable file aEditorName with the parameter aFileName.
Definition: gestfich.cpp:115
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:111