You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
5.2 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /**
  2. * @file eda_doc.cpp
  3. */
  4. #include <fctsys.h>
  5. #include <appl_wxstruct.h>
  6. #include <common.h>
  7. #include <confirm.h>
  8. #include <gestfich.h>
  9. #include <wx/mimetype.h>
  10. #include <wx/tokenzr.h>
  11. #include <wx/filename.h>
  12. #include <macros.h>
  13. void EDA_APP::ReadPdfBrowserInfos()
  14. {
  15. wxASSERT( m_commonSettings != NULL );
  16. m_PdfBrowser = m_commonSettings->Read( wxT( "PdfBrowserName" ), wxEmptyString );
  17. }
  18. void EDA_APP::WritePdfBrowserInfos()
  19. {
  20. wxASSERT( m_commonSettings != NULL );
  21. m_commonSettings->Write( wxT( "PdfBrowserName" ), m_PdfBrowser );
  22. }
  23. // Mime type extensions (PDF files are not considered here)
  24. static wxMimeTypesManager* mimeDatabase;
  25. static const wxFileTypeInfo EDAfallbacks[] =
  26. {
  27. wxFileTypeInfo( wxT( "text/html" ),
  28. wxT( "wxhtml %s" ),
  29. wxT( "wxhtml %s" ),
  30. wxT( "html document (from KiCad)" ),
  31. wxT( "htm" ),
  32. wxT( "html" ),wxNullPtr ),
  33. wxFileTypeInfo( wxT( "application/sch" ),
  34. wxT( "eeschema %s" ),
  35. wxT( "eeschema -p %s" ),
  36. wxT( "sch document (from KiCad)" ),
  37. wxT( "sch" ),
  38. wxT( "SCH" ), wxNullPtr ),
  39. // must terminate the table with this!
  40. wxFileTypeInfo()
  41. };
  42. bool GetAssociatedDocument( wxFrame* aFrame,
  43. const wxString& aDocName,
  44. const wxPathList* aPaths)
  45. {
  46. wxString docname, fullfilename, file_ext;
  47. wxString msg;
  48. wxString command;
  49. bool success = false;
  50. // Is an internet url
  51. static const wxString url_header[3] = { wxT( "http:" ), wxT( "ftp:" ), wxT( "www." ) };
  52. for( int ii = 0; ii < 3; ii++ )
  53. {
  54. if( aDocName.First( url_header[ii] ) == 0 ) //. seems an internet url
  55. {
  56. wxLaunchDefaultBrowser( aDocName );
  57. return true;
  58. }
  59. }
  60. docname = aDocName;
  61. #ifdef __WINDOWS__
  62. docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
  63. #else
  64. docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
  65. #endif
  66. /* Compute the full file name */
  67. if( wxIsAbsolutePath( aDocName ) || aPaths == NULL)
  68. fullfilename = aDocName;
  69. /* If the file exists, this is a trivial case: return the filename
  70. * "as this". the name can be an absolute path, or a relative path
  71. * like ./filename or ../<filename>
  72. */
  73. else if( wxFileName::FileExists( aDocName ) )
  74. fullfilename = aDocName;
  75. else
  76. {
  77. fullfilename = aPaths->FindValidPath( aDocName );
  78. }
  79. wxString mask( wxT( "*" ) ), extension;
  80. #ifdef __WINDOWS__
  81. mask += wxT( ".*" );
  82. extension = wxT( ".*" );
  83. #endif
  84. if( wxIsWild( fullfilename ) )
  85. {
  86. fullfilename = EDA_FileSelector( _( "Doc Files" ),
  87. wxPathOnly( fullfilename ),
  88. fullfilename,
  89. extension,
  90. mask,
  91. aFrame,
  92. wxFD_OPEN,
  93. true,
  94. wxPoint( -1, -1 ) );
  95. if( fullfilename.IsEmpty() )
  96. return false;
  97. }
  98. if( !wxFileExists( fullfilename ) )
  99. {
  100. msg.Printf( _( "Doc File <%s> not found" ), GetChars( aDocName ) );
  101. DisplayError( aFrame, msg );
  102. return false;
  103. }
  104. wxFileName CurrentFileName( fullfilename );
  105. file_ext = CurrentFileName.GetExt();
  106. if( file_ext == wxT( "pdf" ) )
  107. {
  108. success = OpenPDF( fullfilename );
  109. return success;
  110. }
  111. /* Try to launch some browser (useful under linux) */
  112. wxFileType* filetype;
  113. wxString type;
  114. filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
  115. if( !filetype ) // 2nd attempt.
  116. {
  117. mimeDatabase = new wxMimeTypesManager;
  118. mimeDatabase->AddFallbacks( EDAfallbacks );
  119. filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
  120. delete mimeDatabase;
  121. mimeDatabase = NULL;
  122. }
  123. if( filetype )
  124. {
  125. wxFileType::MessageParameters params( fullfilename, type );
  126. success = filetype->GetOpenCommand( &command, params );
  127. delete filetype;
  128. if( success )
  129. success = ProcessExecute( command );
  130. }
  131. if( !success )
  132. {
  133. msg.Printf( _( "Unknown MIME type for doc file <%s>" ), GetChars( fullfilename ) );
  134. DisplayError( aFrame, msg );
  135. }
  136. return success;
  137. }
  138. int KeyWordOk( const wxString& KeyList, const wxString& Database )
  139. {
  140. wxString KeysCopy, DataList;
  141. if( KeyList.IsEmpty() )
  142. return 0;
  143. KeysCopy = KeyList; KeysCopy.MakeUpper();
  144. DataList = Database; DataList.MakeUpper();
  145. wxStringTokenizer Token( KeysCopy, wxT( " \n\r" ) );
  146. while( Token.HasMoreTokens() )
  147. {
  148. wxString Key = Token.GetNextToken();
  149. // Search Key in Datalist:
  150. wxStringTokenizer Data( DataList, wxT( " \n\r" ) );
  151. while( Data.HasMoreTokens() )
  152. {
  153. wxString word = Data.GetNextToken();
  154. if( word == Key )
  155. return 1; // Key found !
  156. }
  157. }
  158. // keyword not found
  159. return 0;
  160. }