File: eda_doc.cpp

package info (click to toggle)
kicad 0.0.20060829-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 56,544 kB
  • ctags: 9,194
  • sloc: cpp: 88,990; ansic: 4,790; makefile: 88; sh: 39
file content (256 lines) | stat: -rw-r--r-- 6,975 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
	/***************/
	/* eda_doc.cpp */
	/***************/

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

// for all others, include the necessary headers (this file is usually all you
// need because it includes almost all "standard" wxWindows headers
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <wx/mimetype.h>
#include <wx/tokenzr.h>

#include "fctsys.h"
#include "wxstruct.h"
#include "common.h"

/*****************************************/
void WinEDA_App::ReadPdfBrowserInfos(void)
/*****************************************/
/* Read from Common config the Pdf browser choice
*/
{
	if ( m_EDA_CommonConfig )
	{
		m_PdfBrowserIsDefault = m_EDA_CommonConfig->Read(wxT("PdfBrowserIsDefault"), TRUE);
		m_PdfBrowser = m_EDA_CommonConfig->Read(wxT("PdfBrowserName"), wxEmptyString);
	}
	if ( m_PdfBrowser.IsEmpty() ) m_PdfBrowserIsDefault = TRUE;
}

/*****************************************/
void WinEDA_App::WritePdfBrowserInfos(void)
/*****************************************/
/* Write into Common config the Pdf browser choice
*/
{
	if ( ! m_EDA_CommonConfig ) return;
	if ( m_PdfBrowser.IsEmpty() ) m_PdfBrowserIsDefault = TRUE;
	m_EDA_CommonConfig->Write(wxT("PdfBrowserIsDefault"), m_PdfBrowserIsDefault);
	m_EDA_CommonConfig->Write(wxT("PdfBrowserName"), m_PdfBrowser);
}


//  Mime type extensions
static wxMimeTypesManager * mimeDatabase;
static const wxFileTypeInfo EDAfallbacks[] =
	{
	wxFileTypeInfo(wxT("text/pdf"),
					wxT("xpdf %s"),
					wxT("xpdf -p %s"),
                    wxT("pdf document (from Kicad)"),
					wxT("pdf"), wxT("PDF"), NULL),

	wxFileTypeInfo(wxT("text/html"),
                   wxT("wxhtml %s"),
					wxT("wxhtml %s"),
                    wxT("html document (from Kicad)"),
                    wxT("htm"), wxT("html"), NULL),

	wxFileTypeInfo(wxT("application/sch"),
                    wxT("eeschema %s"),
					wxT("eeschema -p %s"),
                    wxT("sch document (from Kicad)"),
                    wxT("sch"), wxT("SCH"), NULL),
	// must terminate the table with this!
	wxFileTypeInfo()
};


/********************************************************************/
bool GetAssociatedDocument(wxFrame * frame, const wxString & LibPath,
							const wxString & DocName)
/*********************************************************************/
/* Launch the viewer for the doc file  DocName (mime type)
	LibPath is the doc file search path:
	(kicad/library)
	DocName is the short filename with ext. Wildcarts are allowed
	Seach file is made in LibPath/doc/DocName

	if DocName is starting by http: or ftp: or www. the default internet browser is launched
*/
{
wxString docpath, fullfilename;
wxString Line;
wxString command;
bool success = FALSE;

	// Is an internet url
	wxString url_header[3] = {wxT("http:"), wxT("ftp:"),wxT("www.") };
	for ( int ii = 0; ii < 3; ii ++ )
	{
		if ( DocName.First(url_header[ii]) == 0 ) 	//. seems an internet url
		{
			wxLaunchDefaultBrowser(DocName);
			return TRUE;
		}
	}

	/* Compute the full file name */
	if ( wxIsAbsolutePath(DocName) ) fullfilename = DocName;
	else
	{
		docpath = LibPath + wxT("doc/");
		fullfilename = docpath + DocName;
	}

#ifdef __WINDOWS__
	fullfilename.Replace(UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP);
#else
	fullfilename.Replace(WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP);
#endif

	if ( wxIsWild(fullfilename) )
	{
		fullfilename =
			EDA_FileSelector(_("Doc Files"),	/* Titre de la fenetre */
					wxPathOnly(fullfilename),		/* Chemin par defaut */
					fullfilename,				/* nom fichier par defaut */
					wxEmptyString,				/* extension par defaut */
					wxEmptyString,				/* Masque d'affichage */
					frame,					/* parent frame */
					wxOPEN,					/* wxSAVE, wxOPEN ..*/
					TRUE,	/* true = ne change pas le repertoire courant */
					wxPoint(-1,-1)
					);
		if ( fullfilename.IsEmpty() ) return FALSE;
	}

	if ( ! wxFileExists(fullfilename) )
	{
		Line = _("Doc File ") + fullfilename + _(" not found");
		DisplayError(frame, Line);
		return FALSE;
	}
		
	/* Try to launch some browser (usefull under linux) */
	EDA_Appl->ReadPdfBrowserInfos();
	if ( EDA_Appl->m_PdfBrowserIsDefault )
	{
	wxFileType * filetype;
	wxFileName CurrentFileName(fullfilename);
	wxString ext, type;
		ext = CurrentFileName.GetExt();
		filetype = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
	
		if ( ! filetype )	// 2ieme tentative
		{
			mimeDatabase = new wxMimeTypesManager;
			mimeDatabase->AddFallbacks(EDAfallbacks);
			filetype = mimeDatabase->GetFileTypeFromExtension(ext);
			delete mimeDatabase;
			mimeDatabase = NULL;
		}
	
		if ( filetype )
		{
			wxFileType::MessageParameters params(fullfilename, type);
			success = filetype->GetOpenCommand( &command, params);
			delete filetype;
			if ( success ) wxExecute(command);
		}
	
		if ( ! success)
		{
	#ifdef __LINUX__
			if ( ext == wxT("pdf") )
			{
				success = TRUE; command.Empty();
				if ( wxFileExists( wxT("/usr/bin/xpdf")) )
					command = wxT("xpdf ") + fullfilename;
				else if ( wxFileExists( wxT("/usr/bin/konqueror") ))
					command = wxT("konqueror ") + fullfilename;
				else if ( wxFileExists( wxT("/usr/bin/gpdf") ))
					command = wxT("gpdf ") + fullfilename;
				if ( command.IsEmpty() ) // not started
				{
					DisplayError(frame,
						_(" Cannot find the PDF viewer (xpdf, gpdf or konqueror) in /usr/bin/") );
					success = FALSE;
				}
				else wxExecute(command);
			}
			else
	#endif
			{
				Line.Printf( _("Unknown MIME type for Doc File [%s] (%s)"),
					fullfilename.GetData(), ext.GetData());
				DisplayError(frame, Line);
			}
		}
	}
	
	else
	{
		command = EDA_Appl->m_PdfBrowser;
		if ( wxFileExists(command) )
		{
			success = TRUE;
			AddDelimiterString(fullfilename);
			command += wxT(" ") + fullfilename;
			wxExecute(command);
		}
		
		else
		{
			Line.Printf( _("Cannot find Pdf viewer %s"), command.GetData());
			DisplayError(frame, Line);
		}
	}

	return success;
}

/******************************************************************/
int KeyWordOk(const wxString & KeyList, const wxString & Database )
/******************************************************************/
/* Recherche si dans le texte Database on retrouve tous les mots
	cles donnes dans KeyList ( KeyList = suite de mots cles
	separes par des espaces
	Retourne:
		0 si aucun mot cle trouv
		1 si mot cle trouv
*/
{
wxString KeysCopy, DataList;
	
	if( KeyList.IsEmpty() ) return(0);

	KeysCopy = KeyList; KeysCopy.MakeUpper();
	DataList = Database; DataList.MakeUpper();

wxStringTokenizer Token(KeysCopy, wxT(" \n\r"));
	while ( Token.HasMoreTokens() ) 
	{
		wxString Key = Token.GetNextToken(); 
		// Search Key in Datalist:
		wxStringTokenizer Data(DataList, wxT(" \n\r"));
		while ( Data.HasMoreTokens() ) 
		{
			wxString word = Data.GetNextToken(); 
			if ( word == Key ) return 1;	// Key found !
		}
	}

	// keyword not found
	return(0);
}