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" ),
90 wxT( "No EMBEDDED_FILES object provided for kicad_embed URI" ) );
91 return false;
92 }
93
94 if( !docname.starts_with( FILEEXT::KiCadUriPrefix + "://" ) )
95 {
96 wxLogTrace( wxT( "KICAD_EMBED" ),
97 wxT( "Invalid kicad_embed URI '%s'" ), docname );
98 return false;
99 }
100
101 docname = docname.Mid( wxString( FILEEXT::KiCadUriPrefix + "://" ).length() );
102
103 wxFileName temp_file = aFilesStack[0]->GetTemporaryFileName( docname );
104 int ii = 1;
105
106 while( !temp_file.IsOk() && ii < (int) aFilesStack.size() )
107 temp_file = aFilesStack[ii++]->GetTemporaryFileName( docname );
108
109 if( !temp_file.IsOk() )
110 {
111 wxLogTrace( wxT( "KICAD_EMBED" ),
112 wxT( "Failed to get temp file '%s' for kicad_embed URI" ), docname );
113 return false;
114 }
115
116 wxLogTrace( wxT( "KICAD_EMBED" ),
117 wxT( "Opening embedded file '%s' as '%s'" ),
118 docname,
119 temp_file.GetFullPath() );
120 docname = temp_file.GetFullPath();
121 }
122 }
123 }
124
125#ifdef __WINDOWS__
126 docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
127#else
128 docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
129#endif
130
131 /* Compute the full file name */
132 if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
133 fullfilename = docname;
134 /* If the file exists, this is a trivial case: return the filename "as this". the name can
135 * be an absolute path, or a relative path like ./filename or ../<filename>.
136 */
137 else if( wxFileName::FileExists( docname ) )
138 fullfilename = docname;
139 else
140 fullfilename = aPaths->FindValidPath( docname );
141
142 wxString extension;
143
144#ifdef __WINDOWS__
145 extension = wxT( ".*" );
146#endif
147
148 if( wxIsWild( fullfilename ) )
149 {
150 fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
151 fullfilename, extension, wxFileSelectorDefaultWildcardStr,
152 wxFD_OPEN, aParent );
153
154 if( fullfilename.IsEmpty() )
155 return false;
156 }
157
158 if( !wxFileExists( fullfilename ) )
159 {
160 msg.Printf( _( "Documentation file '%s' not found." ), docname );
161 DisplayErrorMessage( aParent, msg );
162 return false;
163 }
164
165 wxFileName currentFileName( fullfilename );
166
167 // Use wxWidgets to resolve any "." and ".." in the path
168 fullfilename = currentFileName.GetAbsolutePath();
169
170 wxString file_ext = currentFileName.GetExt();
171
172 if( file_ext.Lower() == wxT( "pdf" ) )
173 {
174 success = OpenPDF( fullfilename );
175 return success;
176 }
177
178 /* Try to launch some browser (useful under linux) */
179 wxFileType* filetype;
180
181 wxString type;
182 filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
183
184 if( !filetype ) // 2nd attempt.
185 {
186 mimeDatabase = new wxMimeTypesManager;
187 mimeDatabase->AddFallbacks( EDAfallbacks );
188 filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
189 delete mimeDatabase;
190 mimeDatabase = nullptr;
191 }
192
193 if( filetype )
194 {
195 wxFileType::MessageParameters params( fullfilename, type );
196
197 success = filetype->GetOpenCommand( &command, params );
198 delete filetype;
199
200 if( success )
201 success = wxExecute( command );
202 }
203
204 if( !success )
205 {
206 msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
207 DisplayErrorMessage( aParent, msg );
208 }
209
210 return success;
211}
Container for project specific data.
Definition project.h:62
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:717
The common library.
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:271
#define WIN_STRING_DIR_SEP
Definition gestfich.h:36
#define UNIX_STRING_DIR_SEP
Definition gestfich.h:35
static const std::string KiCadUriPrefix
see class PGM_BASE