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