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, see <https://www.gnu.org/licenses/>.
19 */
20
21#include <pgm_base.h>
22#include <common.h>
23#include <confirm.h>
24#include <eda_doc.h>
25#include <embedded_files.h>
26#include <gestfich.h>
28
29#include <wx/filedlg.h>
30#include <wx/filename.h>
31#include <wx/log.h>
32#include <wx/mimetype.h>
33#include <wx/uri.h>
34
35
36// Mime type extensions (PDF files are not considered here)
37static wxMimeTypesManager* mimeDatabase;
38static const wxFileTypeInfo EDAfallbacks[] =
39{
40 wxFileTypeInfo( wxT( "text/html" ),
41 wxT( "wxhtml %s" ),
42 wxT( "wxhtml %s" ),
43 wxT( "html document (from KiCad)" ),
44 wxT( "htm" ),
45 wxT( "html" ),wxNullPtr ),
46
47 wxFileTypeInfo( wxT( "application/sch" ),
48 wxT( "eeschema %s" ),
49 wxT( "eeschema -p %s" ),
50 wxT( "sch document (from KiCad)" ),
51 wxT( "sch" ),
52 wxT( "SCH" ), wxNullPtr ),
53
54 // must terminate the table with this!
55 wxFileTypeInfo()
56};
57
58
59bool GetAssociatedDocument( wxWindow* aParent, const wxString& aDocName, PROJECT* aProject,
60 SEARCH_STACK* aPaths, std::vector<EMBEDDED_FILES*> aFilesStack )
61{
62 wxString docname;
63 wxString fullfilename;
64 wxString msg;
65 wxString command;
66 bool success = false;
67
68 // Replace before resolving as we might have a URL in a variable
69 docname = ResolveUriByEnvVars( aDocName, aProject );
70
71 // We don't want the wx error message about not being able to open the URI
72 {
73 wxURI uri( docname );
74 wxLogNull logNo; // Disable log messages
75
76 if( uri.HasScheme() )
77 {
78 wxString scheme = uri.GetScheme().Lower();
79
80 if( scheme != FILEEXT::KiCadUriPrefix )
81 {
82 if( wxLaunchDefaultBrowser( docname ) )
83 return true;
84 }
85 else
86 {
87 if( aFilesStack.empty() )
88 {
89 wxLogTrace( wxT( "KICAD_EMBED" ), wxT( "No EMBEDDED_FILES object provided for kicad_embed URI" ) );
90 return false;
91 }
92
93 if( !docname.starts_with( FILEEXT::KiCadUriPrefix + "://" ) )
94 {
95 wxLogTrace( wxT( "KICAD_EMBED" ), wxT( "Invalid kicad_embed URI '%s'" ), docname );
96 return false;
97 }
98
99 docname = docname.Mid( wxString( FILEEXT::KiCadUriPrefix + "://" ).length() );
100
101 // It's a "stack" stored in a std::vector. Search from the back.
102 int ii = (int) aFilesStack.size() - 1;
103 wxFileName temp_file = aFilesStack[ii--]->GetTemporaryFileName( docname );
104
105 while( !temp_file.IsOk() && ii >= 0 )
106 temp_file = aFilesStack[ii--]->GetTemporaryFileName( docname );
107
108 if( !temp_file.IsOk() )
109 {
110 wxLogTrace( wxT( "KICAD_EMBED" ), wxT( "Failed to get temp file '%s' for kicad_embed URI" ),
111 docname );
112 return false;
113 }
114
115 wxLogTrace( wxT( "KICAD_EMBED" ), wxT( "Opening embedded file '%s' as '%s'" ), docname,
116 temp_file.GetFullPath() );
117 docname = temp_file.GetFullPath();
118 }
119 }
120 }
121
122#ifdef __WINDOWS__
123 docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
124#else
125 docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
126#endif
127
128 /* Compute the full file name */
129 if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
130 fullfilename = docname;
131 /* If the file exists, this is a trivial case: return the filename "as this". the name can
132 * be an absolute path, or a relative path like ./filename or ../<filename>.
133 */
134 else if( wxFileName::FileExists( docname ) )
135 fullfilename = docname;
136 else
137 fullfilename = aPaths->FindValidPath( docname );
138
139 wxString extension;
140
141#ifdef __WINDOWS__
142 extension = wxT( ".*" );
143#endif
144
145 if( wxIsWild( fullfilename ) )
146 {
147 fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
148 fullfilename, extension, wxFileSelectorDefaultWildcardStr,
149 wxFD_OPEN, aParent );
150
151 if( fullfilename.IsEmpty() )
152 return false;
153 }
154
155 if( !wxFileExists( fullfilename ) )
156 {
157 msg.Printf( _( "Documentation file '%s' not found." ), docname );
158 DisplayErrorMessage( aParent, msg );
159 return false;
160 }
161
162 wxFileName currentFileName( fullfilename );
163
164 // Use wxWidgets to resolve any "." and ".." in the path
165 fullfilename = currentFileName.GetAbsolutePath();
166
167 wxString file_ext = currentFileName.GetExt();
168
169 if( file_ext.Lower() == wxT( "pdf" ) )
170 {
171 success = OpenPDF( fullfilename );
172 return success;
173 }
174
175 /* Try to launch some browser (useful under linux) */
176 wxFileType* filetype;
177
178 wxString type;
179 filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
180
181 if( !filetype ) // 2nd attempt.
182 {
183 mimeDatabase = new wxMimeTypesManager;
184 mimeDatabase->AddFallbacks( EDAfallbacks );
185 filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
186 delete mimeDatabase;
187 mimeDatabase = nullptr;
188 }
189
190 if( filetype )
191 {
192 wxFileType::MessageParameters params( fullfilename, type );
193
194 success = filetype->GetOpenCommand( &command, params );
195 delete filetype;
196
197 if( success )
198 success = wxExecute( command );
199 }
200
201 if( !success )
202 {
203 msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
204 DisplayErrorMessage( aParent, msg );
205 }
206
207 return success;
208}
Container for project specific data.
Definition project.h:63
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:790
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:217
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:59
static const wxFileTypeInfo EDAfallbacks[]
Definition eda_doc.cpp:38
static wxMimeTypesManager * mimeDatabase
Definition eda_doc.cpp:37
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:380
#define WIN_STRING_DIR_SEP
Definition gestfich.h:42
#define UNIX_STRING_DIR_SEP
Definition gestfich.h:41
static const std::string KiCadUriPrefix
see class PGM_BASE