kicad/common/eda_doc.cpp

207 lines
5.3 KiB
C++
Raw Normal View History

/**
* @file eda_doc.cpp
*/
2007-05-06 16:03:28 +00:00
#include <fctsys.h>
#include <appl_wxstruct.h>
#include <common.h>
#include <confirm.h>
#include <gestfich.h>
2007-05-06 16:03:28 +00:00
#include <wx/mimetype.h>
#include <wx/tokenzr.h>
#include <wx/filename.h>
#include <macros.h>
void EDA_APP::ReadPdfBrowserInfos()
{
2011-12-16 20:12:49 +00:00
wxASSERT( m_commonSettings != NULL );
2011-12-16 20:12:49 +00:00
m_PdfBrowser = m_commonSettings->Read( wxT( "PdfBrowserName" ), wxEmptyString );
}
2007-05-06 16:03:28 +00:00
2008-04-24 16:55:35 +00:00
void EDA_APP::WritePdfBrowserInfos()
2007-05-06 16:03:28 +00:00
{
2011-12-16 20:12:49 +00:00
wxASSERT( m_commonSettings != NULL );
2011-12-16 20:12:49 +00:00
m_commonSettings->Write( wxT( "PdfBrowserName" ), m_PdfBrowser );
}
2008-06-29 18:51:38 +00:00
// Mime type extensions (PDF files are not considered here)
2008-04-24 16:55:35 +00:00
static wxMimeTypesManager* mimeDatabase;
2007-05-06 16:03:28 +00:00
static const wxFileTypeInfo EDAfallbacks[] =
2008-04-24 16:55:35 +00:00
{
wxFileTypeInfo( wxT( "text/html" ),
wxT( "wxhtml %s" ),
wxT( "wxhtml %s" ),
wxT( "html document (from KiCad)" ),
wxT( "htm" ),
2010-11-20 19:53:00 +00:00
wxT( "html" ),wxNullPtr ),
2008-04-24 16:55:35 +00:00
wxFileTypeInfo( wxT( "application/sch" ),
wxT( "eeschema %s" ),
wxT( "eeschema -p %s" ),
wxT( "sch document (from KiCad)" ),
wxT( "sch" ),
2010-11-20 19:53:00 +00:00
wxT( "SCH" ), wxNullPtr ),
2008-04-24 16:55:35 +00:00
// must terminate the table with this!
wxFileTypeInfo()
2007-05-06 16:03:28 +00:00
};
2007-05-06 16:03:28 +00:00
bool GetAssociatedDocument( wxFrame* aFrame,
const wxString& aDocName,
const wxPathList* aPaths)
2007-05-06 16:03:28 +00:00
{
2009-04-12 14:39:54 +00:00
wxString docname, fullfilename, file_ext;
wxString msg;
2008-04-24 16:55:35 +00:00
wxString command;
bool success = false;
2008-04-24 16:55:35 +00:00
// Is an internet url
static const wxString url_header[3] = { wxT( "http:" ), wxT( "ftp:" ), wxT( "www." ) };
2008-04-24 16:55:35 +00:00
for( int ii = 0; ii < 3; ii++ )
{
if( aDocName.First( url_header[ii] ) == 0 ) //. seems an internet url
2008-04-24 16:55:35 +00:00
{
wxLaunchDefaultBrowser( aDocName );
return true;
2008-04-24 16:55:35 +00:00
}
}
2009-04-12 14:39:54 +00:00
docname = aDocName;
#ifdef __WINDOWS__
2009-04-12 14:39:54 +00:00
docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
#else
docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
#endif
2008-04-24 16:55:35 +00:00
/* Compute the full file name */
if( wxIsAbsolutePath( aDocName ) || aPaths == NULL)
fullfilename = aDocName;
/* If the file exists, this is a trivial case: return the filename
* "as this". the name can be an absolute path, or a relative path
* like ./filename or ../<filename>
*/
else if( wxFileName::FileExists( aDocName ) )
fullfilename = aDocName;
2008-04-24 16:55:35 +00:00
else
{
fullfilename = aPaths->FindValidPath( aDocName );
2008-04-24 16:55:35 +00:00
}
2007-05-06 16:03:28 +00:00
2008-04-24 16:55:35 +00:00
wxString mask( wxT( "*" ) ), extension;
#ifdef __WINDOWS__
2008-04-24 16:55:35 +00:00
mask += wxT( ".*" );
extension = wxT( ".*" );
#endif
2008-04-24 16:55:35 +00:00
if( wxIsWild( fullfilename ) )
{
fullfilename = EDA_FileSelector( _( "Doc Files" ),
wxPathOnly( fullfilename ),
fullfilename,
extension,
mask,
aFrame,
wxFD_OPEN,
true,
wxPoint( -1, -1 ) );
2008-04-24 16:55:35 +00:00
if( fullfilename.IsEmpty() )
return false;
2008-04-24 16:55:35 +00:00
}
if( !wxFileExists( fullfilename ) )
{
msg = _( "Doc File " );
msg << wxT("\"") << aDocName << wxT("\"") << _( " not found" );
DisplayError( aFrame, msg );
return false;
2008-04-24 16:55:35 +00:00
}
2008-06-29 18:51:38 +00:00
wxFileName CurrentFileName( fullfilename );
file_ext = CurrentFileName.GetExt();
2008-06-29 18:51:38 +00:00
if( file_ext == wxT( "pdf" ) )
2008-04-24 16:55:35 +00:00
{
2008-06-29 18:51:38 +00:00
success = OpenPDF( fullfilename );
return success;
}
2008-04-24 16:55:35 +00:00
/* Try to launch some browser (useful under linux) */
2008-06-29 18:51:38 +00:00
wxFileType* filetype;
2008-04-24 16:55:35 +00:00
2008-06-29 18:51:38 +00:00
wxString type;
filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
2008-04-24 16:55:35 +00:00
if( !filetype ) // 2nd attempt.
2008-06-29 18:51:38 +00:00
{
mimeDatabase = new wxMimeTypesManager;
mimeDatabase->AddFallbacks( EDAfallbacks );
filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
delete mimeDatabase;
mimeDatabase = NULL;
}
2008-04-24 16:55:35 +00:00
2008-06-29 18:51:38 +00:00
if( filetype )
{
wxFileType::MessageParameters params( fullfilename, type );
success = filetype->GetOpenCommand( &command, params );
delete filetype;
2008-06-29 18:51:38 +00:00
if( success )
success = ProcessExecute( command );
2008-04-24 16:55:35 +00:00
}
2008-06-29 18:51:38 +00:00
if( !success )
2008-04-24 16:55:35 +00:00
{
msg.Printf( _( "Unknown MIME type for doc file <%s>" ), GetChars( fullfilename ) );
DisplayError( aFrame, msg );
2008-04-24 16:55:35 +00:00
}
return success;
2007-05-06 16:03:28 +00:00
}
2008-04-24 16:55:35 +00:00
int KeyWordOk( const wxString& KeyList, const wxString& Database )
2007-05-06 16:03:28 +00:00
{
2008-04-24 16:55:35 +00:00
wxString KeysCopy, DataList;
if( KeyList.IsEmpty() )
return 0;
KeysCopy = KeyList; KeysCopy.MakeUpper();
DataList = Database; DataList.MakeUpper();
wxStringTokenizer Token( KeysCopy, wxT( " \n\r" ) );
2007-05-06 16:03:28 +00:00
2008-04-24 16:55:35 +00:00
while( Token.HasMoreTokens() )
{
2008-06-29 18:51:38 +00:00
wxString Key = Token.GetNextToken();
2007-05-06 16:03:28 +00:00
2008-04-24 16:55:35 +00:00
// Search Key in Datalist:
wxStringTokenizer Data( DataList, wxT( " \n\r" ) );
while( Data.HasMoreTokens() )
{
wxString word = Data.GetNextToken();
2008-04-24 16:55:35 +00:00
if( word == Key )
2008-06-29 18:51:38 +00:00
return 1; // Key found !
2008-04-24 16:55:35 +00:00
}
}
// keyword not found
return 0;
}