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 <gestfich.h>
30
31#include <wx/filedlg.h>
32#include <wx/filename.h>
33#include <wx/log.h>
34#include <wx/mimetype.h>
35#include <wx/uri.h>
36
37
38// Mime type extensions (PDF files are not considered here)
39static wxMimeTypesManager* mimeDatabase;
40static const wxFileTypeInfo EDAfallbacks[] =
41{
42 wxFileTypeInfo( wxT( "text/html" ),
43 wxT( "wxhtml %s" ),
44 wxT( "wxhtml %s" ),
45 wxT( "html document (from KiCad)" ),
46 wxT( "htm" ),
47 wxT( "html" ),wxNullPtr ),
48
49 wxFileTypeInfo( wxT( "application/sch" ),
50 wxT( "eeschema %s" ),
51 wxT( "eeschema -p %s" ),
52 wxT( "sch document (from KiCad)" ),
53 wxT( "sch" ),
54 wxT( "SCH" ), wxNullPtr ),
55
56 // must terminate the table with this!
57 wxFileTypeInfo()
58};
59
60
61bool GetAssociatedDocument( wxWindow* aParent, const wxString& aDocName, PROJECT* aProject, SEARCH_STACK* aPaths )
62{
63 wxString docname;
64 wxString fullfilename;
65 wxString msg;
66 wxString command;
67 bool success = false;
68
69 // Replace before resolving as we might have a URL in a variable
70 docname = ResolveUriByEnvVars( aDocName, aProject );
71
72 // We don't want the wx error message about not being able to open the URI
73 {
74 wxURI uri( docname );
75 wxLogNull logNo; // Disable log messages
76
77 if( uri.HasScheme() && wxLaunchDefaultBrowser( docname ) )
78 return true;
79 }
80
81#ifdef __WINDOWS__
82 docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
83#else
84 docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
85#endif
86
87 /* Compute the full file name */
88 if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
89 fullfilename = docname;
90 /* If the file exists, this is a trivial case: return the filename "as this". the name can
91 * be an absolute path, or a relative path like ./filename or ../<filename>.
92 */
93 else if( wxFileName::FileExists( docname ) )
94 fullfilename = docname;
95 else
96 fullfilename = aPaths->FindValidPath( docname );
97
98 wxString extension;
99
100#ifdef __WINDOWS__
101 extension = wxT( ".*" );
102#endif
103
104 if( wxIsWild( fullfilename ) )
105 {
106 fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
107 fullfilename, extension, wxFileSelectorDefaultWildcardStr,
108 wxFD_OPEN, aParent );
109
110 if( fullfilename.IsEmpty() )
111 return false;
112 }
113
114 if( !wxFileExists( fullfilename ) )
115 {
116 msg.Printf( _( "Documentation file '%s' not found." ), docname );
117 DisplayError( aParent, msg );
118 return false;
119 }
120
121 wxFileName currentFileName( fullfilename );
122
123 // Use wxWidgets to resolve any "." and ".." in the path
124 fullfilename = currentFileName.GetAbsolutePath();
125
126 wxString file_ext = currentFileName.GetExt();
127
128 if( file_ext.Lower() == wxT( "pdf" ) )
129 {
130 success = OpenPDF( fullfilename );
131 return success;
132 }
133
134 /* Try to launch some browser (useful under linux) */
135 wxFileType* filetype;
136
137 wxString type;
138 filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
139
140 if( !filetype ) // 2nd attempt.
141 {
142 mimeDatabase = new wxMimeTypesManager;
143 mimeDatabase->AddFallbacks( EDAfallbacks );
144 filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
145 delete mimeDatabase;
146 mimeDatabase = nullptr;
147 }
148
149 if( filetype )
150 {
151 wxFileType::MessageParameters params( fullfilename, type );
152
153 success = filetype->GetOpenCommand( &command, params );
154 delete filetype;
155
156 if( success )
157 success = wxExecute( command );
158 }
159
160 if( !success )
161 {
162 msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
163 DisplayError( aParent, msg );
164 }
165
166 return success;
167}
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:347
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)
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:40
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:39
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:61
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
see class PGM_BASE