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.

240 lines
6.9 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
18 years ago
18 years ago
18 years ago
18 years ago
  1. /***************/
  2. /* 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. /*****************************************/
  13. void WinEDA_App::ReadPdfBrowserInfos()
  14. /*****************************************/
  15. /* Read from Common config the Pdf browser choice
  16. */
  17. {
  18. wxASSERT( m_EDA_CommonConfig != NULL );
  19. m_PdfBrowserIsDefault = m_EDA_CommonConfig->Read( wxT( "PdfBrowserIsDefault" ),
  20. true );
  21. m_PdfBrowser = m_EDA_CommonConfig->Read( wxT( "PdfBrowserName" ),
  22. wxEmptyString );
  23. if( m_PdfBrowser.IsEmpty() )
  24. m_PdfBrowserIsDefault = true;
  25. }
  26. /*****************************************/
  27. void WinEDA_App::WritePdfBrowserInfos()
  28. /*****************************************/
  29. /* Write into Common config the Pdf browser choice
  30. */
  31. {
  32. wxASSERT( m_EDA_CommonConfig != NULL );
  33. if( m_PdfBrowser.IsEmpty() )
  34. m_PdfBrowserIsDefault = true;
  35. m_EDA_CommonConfig->Write( wxT( "PdfBrowserIsDefault" ),
  36. m_PdfBrowserIsDefault );
  37. m_EDA_CommonConfig->Write( wxT( "PdfBrowserName" ), m_PdfBrowser );
  38. }
  39. // Mime type extensions (PDF files are not considered here)
  40. static wxMimeTypesManager* mimeDatabase;
  41. static 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" ), wxT( "html" ),NULL ),
  48. wxFileTypeInfo( wxT( "application/sch" ),
  49. wxT( "eeschema %s" ),
  50. wxT( "eeschema -p %s" ),
  51. wxT( "sch document (from Kicad)" ),
  52. wxT( "sch" ), wxT( "SCH" ), NULL ),
  53. // must terminate the table with this!
  54. wxFileTypeInfo()
  55. };
  56. /** Function GetAssociatedDocument
  57. * open a document (file) with the suitable browser
  58. * @param aFrame = main frame
  59. * if DocName is starting by http: or ftp: or www. the default internet browser is launched
  60. * @param aDocName = filename of file to open (Full filename or short filename)
  61. * @param aPaths = a wxPathList to explore.
  62. * if NULL or aDocName is a full filename, aPath is not used.
  63. */
  64. bool GetAssociatedDocument( wxFrame* aFrame,
  65. const wxString& aDocName,
  66. const wxPathList* aPaths)
  67. {
  68. wxString docname, fullfilename, file_ext;
  69. wxString msg;
  70. wxString command;
  71. bool success = FALSE;
  72. // Is an internet url
  73. static const wxString url_header[3] = { wxT( "http:" ), wxT( "ftp:" ), wxT( "www." ) };
  74. for( int ii = 0; ii < 3; ii++ )
  75. {
  76. if( aDocName.First( url_header[ii] ) == 0 ) //. seems an internet url
  77. {
  78. wxLaunchDefaultBrowser( aDocName );
  79. return TRUE;
  80. }
  81. }
  82. docname = aDocName;
  83. #ifdef __WINDOWS__
  84. docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
  85. #else
  86. docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
  87. #endif
  88. /* Compute the full file name */
  89. if( wxIsAbsolutePath( aDocName ) || aPaths == NULL)
  90. fullfilename = aDocName;
  91. /* If the file exists, this is a trivial case: return the filename "as this"
  92. * the name can be an absolute path, or a relative path like ./filename or ../<filename>
  93. */
  94. else if( wxFileName::FileExists( aDocName ) )
  95. fullfilename = aDocName;
  96. else
  97. {
  98. fullfilename = aPaths->FindValidPath( aDocName );
  99. }
  100. wxString mask( wxT( "*" ) ), extension;
  101. #ifdef __WINDOWS__
  102. mask += wxT( ".*" );
  103. extension = wxT( ".*" );
  104. #endif
  105. if( wxIsWild( fullfilename ) )
  106. {
  107. fullfilename =
  108. EDA_FileSelector( _( "Doc Files" ), /* Titre de la fenetre */
  109. wxPathOnly( fullfilename ), /* Chemin par defaut */
  110. fullfilename, /* nom fichier par defaut */
  111. extension, /* extension par defaut */
  112. mask, /* Masque d'affichage */
  113. aFrame, /* parent frame */
  114. wxFD_OPEN, /* wxSAVE, wxFD_OPEN ..*/
  115. TRUE, /* true = ne change pas le repertoire courant */
  116. wxPoint( -1, -1 )
  117. );
  118. if( fullfilename.IsEmpty() )
  119. return FALSE;
  120. }
  121. if( !wxFileExists( fullfilename ) )
  122. {
  123. msg = _( "Doc File " );
  124. msg << wxT("\"") << aDocName << wxT("\"") << _( " not found" );
  125. DisplayError( aFrame, msg );
  126. return FALSE;
  127. }
  128. wxFileName CurrentFileName( fullfilename );
  129. file_ext = CurrentFileName.GetExt();
  130. if( file_ext == wxT( "pdf" ) )
  131. {
  132. success = OpenPDF( fullfilename );
  133. return success;
  134. }
  135. /* Try to launch some browser (usefull under linux) */
  136. wxFileType* filetype;
  137. wxString type;
  138. filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
  139. if( !filetype ) // 2ieme tentative
  140. {
  141. mimeDatabase = new wxMimeTypesManager;
  142. mimeDatabase->AddFallbacks( EDAfallbacks );
  143. filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
  144. delete mimeDatabase;
  145. mimeDatabase = NULL;
  146. }
  147. if( filetype )
  148. {
  149. wxFileType::MessageParameters params( fullfilename, type );
  150. success = filetype->GetOpenCommand( &command, params );
  151. delete filetype;
  152. if( success )
  153. success = ProcessExecute( command );
  154. }
  155. if( !success )
  156. {
  157. msg.Printf( _( "Unknown MIME type for doc file <%s>" ),
  158. fullfilename.GetData() );
  159. DisplayError( aFrame, msg );
  160. }
  161. return success;
  162. }
  163. /******************************************************************/
  164. int KeyWordOk( const wxString& KeyList, const wxString& Database )
  165. /******************************************************************/
  166. /* Recherche si dans le texte Database on retrouve tous les mots
  167. * cles donnes dans KeyList ( KeyList = suite de mots cles
  168. * separes par des espaces
  169. * Retourne:
  170. * 0 si aucun mot cle trouv
  171. * 1 si mot cle trouv
  172. */
  173. {
  174. wxString KeysCopy, DataList;
  175. if( KeyList.IsEmpty() )
  176. return 0;
  177. KeysCopy = KeyList; KeysCopy.MakeUpper();
  178. DataList = Database; DataList.MakeUpper();
  179. wxStringTokenizer Token( KeysCopy, wxT( " \n\r" ) );
  180. while( Token.HasMoreTokens() )
  181. {
  182. wxString Key = Token.GetNextToken();
  183. // Search Key in Datalist:
  184. wxStringTokenizer Data( DataList, wxT( " \n\r" ) );
  185. while( Data.HasMoreTokens() )
  186. {
  187. wxString word = Data.GetNextToken();
  188. if( word == Key )
  189. return 1; // Key found !
  190. }
  191. }
  192. // keyword not found
  193. return 0;
  194. }