KiCad PCB EDA Suite
Loading...
Searching...
No Matches
eda_doc.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) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#include <pgm_base.h>
26#include <common.h>
27#include <confirm.h>
28#include <eda_doc.h>
29#include <embedded_files.h>
30#include <gestfich.h>
32
33#include <wx/filedlg.h>
34#include <wx/filename.h>
35#include <wx/log.h>
36#include <wx/mimetype.h>
37#include <wx/uri.h>
38
39
40// Mime type extensions (PDF files are not considered here)
41static wxMimeTypesManager* mimeDatabase;
42static const wxFileTypeInfo EDAfallbacks[] =
43{
44 wxFileTypeInfo( wxT( "text/html" ),
45 wxT( "wxhtml %s" ),
46 wxT( "wxhtml %s" ),
47 wxT( "html document (from KiCad)" ),
48 wxT( "htm" ),
49 wxT( "html" ),wxNullPtr ),
50
51 wxFileTypeInfo( wxT( "application/sch" ),
52 wxT( "eeschema %s" ),
53 wxT( "eeschema -p %s" ),
54 wxT( "sch document (from KiCad)" ),
55 wxT( "sch" ),
56 wxT( "SCH" ), wxNullPtr ),
57
58 // must terminate the table with this!
59 wxFileTypeInfo()
60};
61
62
63bool GetAssociatedDocument( wxWindow* aParent, const wxString& aDocName, PROJECT* aProject,
64 SEARCH_STACK* aPaths, std::vector<EMBEDDED_FILES*> aFilesStack )
65{
66 wxString docname;
67 wxString fullfilename;
68 wxString msg;
69 wxString command;
70 bool success = false;
71
72 // Replace before resolving as we might have a URL in a variable
73 docname = ResolveUriByEnvVars( aDocName, aProject );
74
75 // We don't want the wx error message about not being able to open the URI
76 {
77 wxURI uri( docname );
78 wxLogNull logNo; // Disable log messages
79
80 if( uri.HasScheme() )
81 {
82 wxString scheme = uri.GetScheme().Lower();
83
84 if( scheme != FILEEXT::KiCadUriPrefix )
85 {
86 if( wxLaunchDefaultBrowser( docname ) )
87 return true;
88 }
89 else
90 {
91 if( aFilesStack.empty() )
92 {
93 wxLogTrace( wxT( "KICAD_EMBED" ),
94 wxT( "No EMBEDDED_FILES object provided for kicad_embed URI" ) );
95 return false;
96 }
97
98 if( !docname.starts_with( FILEEXT::KiCadUriPrefix + "://" ) )
99 {
100 wxLogTrace( wxT( "KICAD_EMBED" ),
101 wxT( "Invalid kicad_embed URI '%s'" ), docname );
102 return false;
103 }
104
105 docname = docname.Mid( wxString( FILEEXT::KiCadUriPrefix + "://" ).length() );
106
107 wxFileName temp_file = aFilesStack[0]->GetTemporaryFileName( docname );
108 int ii = 1;
109
110 while( !temp_file.IsOk() && ii < (int) aFilesStack.size() )
111 temp_file = aFilesStack[ii++]->GetTemporaryFileName( docname );
112
113 if( !temp_file.IsOk() )
114 {
115 wxLogTrace( wxT( "KICAD_EMBED" ),
116 wxT( "Failed to get temp file '%s' for kicad_embed URI" ), docname );
117 return false;
118 }
119
120 wxLogTrace( wxT( "KICAD_EMBED" ),
121 wxT( "Opening embedded file '%s' as '%s'" ),
122 docname,
123 temp_file.GetFullPath() );
124 docname = temp_file.GetFullPath();
125 }
126 }
127 }
128
129#ifdef __WINDOWS__
130 docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
131#else
132 docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
133#endif
134
135 /* Compute the full file name */
136 if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
137 fullfilename = docname;
138 /* If the file exists, this is a trivial case: return the filename "as this". the name can
139 * be an absolute path, or a relative path like ./filename or ../<filename>.
140 */
141 else if( wxFileName::FileExists( docname ) )
142 fullfilename = docname;
143 else
144 fullfilename = aPaths->FindValidPath( docname );
145
146 wxString extension;
147
148#ifdef __WINDOWS__
149 extension = wxT( ".*" );
150#endif
151
152 if( wxIsWild( fullfilename ) )
153 {
154 fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
155 fullfilename, extension, wxFileSelectorDefaultWildcardStr,
156 wxFD_OPEN, aParent );
157
158 if( fullfilename.IsEmpty() )
159 return false;
160 }
161
162 if( !wxFileExists( fullfilename ) )
163 {
164 msg.Printf( _( "Documentation file '%s' not found." ), docname );
165 DisplayErrorMessage( aParent, msg );
166 return false;
167 }
168
169 wxFileName currentFileName( fullfilename );
170
171 // Use wxWidgets to resolve any "." and ".." in the path
172 fullfilename = currentFileName.GetAbsolutePath();
173
174 wxString file_ext = currentFileName.GetExt();
175
176 if( file_ext.Lower() == wxT( "pdf" ) )
177 {
178 success = OpenPDF( fullfilename );
179 return success;
180 }
181
182 /* Try to launch some browser (useful under linux) */
183 wxFileType* filetype;
184
185 wxString type;
186 filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
187
188 if( !filetype ) // 2nd attempt.
189 {
190 mimeDatabase = new wxMimeTypesManager;
191 mimeDatabase->AddFallbacks( EDAfallbacks );
192 filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
193 delete mimeDatabase;
194 mimeDatabase = nullptr;
195 }
196
197 if( filetype )
198 {
199 wxFileType::MessageParameters params( fullfilename, type );
200
201 success = filetype->GetOpenCommand( &command, params );
202 delete filetype;
203
204 if( success )
205 success = wxExecute( command );
206 }
207
208 if( !success )
209 {
210 msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
211 DisplayErrorMessage( aParent, msg );
212 }
213
214 return success;
215}
Container for project specific data.
Definition project.h:65
Look for files in a number of paths.
const wxString ResolveUriByEnvVars(const wxString &aUri, const PROJECT *aProject)
Replace any environment and/or text variables in URIs.
Definition common.cpp:522
The common library.
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:202
This file is part of the common library.
#define _(s)
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths, std::vector< EMBEDDED_FILES * > aFilesStack)
Open a document (file) with the suitable browser.
Definition eda_doc.cpp:63
static const wxFileTypeInfo EDAfallbacks[]
Definition eda_doc.cpp:42
static wxMimeTypesManager * mimeDatabase
Definition eda_doc.cpp:41
This file is part of the common library.
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition gestfich.cpp:256
#define WIN_STRING_DIR_SEP
Definition gestfich.h:38
#define UNIX_STRING_DIR_SEP
Definition gestfich.h:37
static const std::string KiCadUriPrefix
see class PGM_BASE