]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: common/mimecmn.cpp | |
3 | // Purpose: classes and functions to manage MIME types | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Chris Elliott (biol75@york.ac.uk) 5 Dec 00: write support for Win32 | |
7 | // Created: 23.09.98 | |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
10 | // Licence: wxWindows license (part of wxExtra library) | |
11 | ///////////////////////////////////////////////////////////////////////////// | |
12 | ||
13 | // ============================================================================ | |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
21 | #ifdef __GNUG__ | |
22 | #pragma implementation "mimetypebase.h" | |
23 | #endif | |
24 | ||
25 | // for compilers that support precompilation, includes "wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | #include "wx/module.h" | |
28 | ||
29 | #ifdef __BORLANDC__ | |
30 | #pragma hdrstop | |
31 | #endif | |
32 | ||
33 | #ifndef WX_PRECOMP | |
34 | #include "wx/defs.h" | |
35 | #endif | |
36 | ||
37 | #ifndef WX_PRECOMP | |
38 | #include "wx/string.h" | |
39 | #if wxUSE_GUI | |
40 | #include "wx/icon.h" | |
41 | #endif | |
42 | #endif //WX_PRECOMP | |
43 | ||
44 | #include "wx/log.h" | |
45 | #include "wx/file.h" | |
46 | #include "wx/intl.h" | |
47 | #include "wx/dynarray.h" | |
48 | #include "wx/confbase.h" | |
49 | ||
50 | #include "wx/mimetype.h" | |
51 | ||
52 | // other standard headers | |
53 | #include <ctype.h> | |
54 | ||
55 | // implementation classes: | |
56 | #if defined(__WXMSW__) | |
57 | #include "wx/msw/mimetype.h" | |
58 | #elif defined (__WXMAC__) | |
59 | #include "wx/mac/mimetype.h" | |
60 | #elif defined (__WXPM__) | |
61 | #include "wx/os2/mimetype.h" | |
62 | #else // Unix | |
63 | #include "wx/unix/mimetype.h" | |
64 | #endif | |
65 | ||
66 | // ============================================================================ | |
67 | // common classes | |
68 | // ============================================================================ | |
69 | ||
70 | // ---------------------------------------------------------------------------- | |
71 | // wxFileTypeInfo | |
72 | // ---------------------------------------------------------------------------- | |
73 | ||
74 | wxFileTypeInfo::wxFileTypeInfo(const char *mimeType, | |
75 | const char *openCmd, | |
76 | const char *printCmd, | |
77 | const char *desc, | |
78 | ...) | |
79 | : m_mimeType(mimeType), | |
80 | m_openCmd(openCmd), | |
81 | m_printCmd(printCmd), | |
82 | m_desc(desc) | |
83 | { | |
84 | va_list argptr; | |
85 | va_start(argptr, desc); | |
86 | ||
87 | for ( ;; ) | |
88 | { | |
89 | const char *ext = va_arg(argptr, const char *); | |
90 | if ( !ext ) | |
91 | { | |
92 | // NULL terminates the list | |
93 | break; | |
94 | } | |
95 | ||
96 | m_exts.Add(ext); | |
97 | } | |
98 | ||
99 | va_end(argptr); | |
100 | } | |
101 | ||
102 | #include "wx/arrimpl.cpp" | |
103 | WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo); | |
104 | ||
105 | // ============================================================================ | |
106 | // implementation of the wrapper classes | |
107 | // ============================================================================ | |
108 | ||
109 | // ---------------------------------------------------------------------------- | |
110 | // wxFileType | |
111 | // ---------------------------------------------------------------------------- | |
112 | ||
113 | /* static */ | |
114 | wxString wxFileType::ExpandCommand(const wxString& command, | |
115 | const wxFileType::MessageParameters& params) | |
116 | { | |
117 | bool hasFilename = FALSE; | |
118 | ||
119 | wxString str; | |
120 | for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) { | |
121 | if ( *pc == wxT('%') ) { | |
122 | switch ( *++pc ) { | |
123 | case wxT('s'): | |
124 | // '%s' expands into file name (quoted because it might | |
125 | // contain spaces) - except if there are already quotes | |
126 | // there because otherwise some programs may get confused | |
127 | // by double double quotes | |
128 | #if 0 | |
129 | if ( *(pc - 2) == wxT('"') ) | |
130 | str << params.GetFileName(); | |
131 | else | |
132 | str << wxT('"') << params.GetFileName() << wxT('"'); | |
133 | #endif | |
134 | str << params.GetFileName(); | |
135 | hasFilename = TRUE; | |
136 | break; | |
137 | ||
138 | case wxT('t'): | |
139 | // '%t' expands into MIME type (quote it too just to be | |
140 | // consistent) | |
141 | str << wxT('\'') << params.GetMimeType() << wxT('\''); | |
142 | break; | |
143 | ||
144 | case wxT('{'): | |
145 | { | |
146 | const wxChar *pEnd = wxStrchr(pc, wxT('}')); | |
147 | if ( pEnd == NULL ) { | |
148 | wxString mimetype; | |
149 | wxLogWarning(_("Unmatched '{' in an entry for mime type %s."), | |
150 | params.GetMimeType().c_str()); | |
151 | str << wxT("%{"); | |
152 | } | |
153 | else { | |
154 | wxString param(pc + 1, pEnd - pc - 1); | |
155 | str << wxT('\'') << params.GetParamValue(param) << wxT('\''); | |
156 | pc = pEnd; | |
157 | } | |
158 | } | |
159 | break; | |
160 | ||
161 | case wxT('n'): | |
162 | case wxT('F'): | |
163 | // TODO %n is the number of parts, %F is an array containing | |
164 | // the names of temp files these parts were written to | |
165 | // and their mime types. | |
166 | break; | |
167 | ||
168 | default: | |
169 | wxLogDebug(wxT("Unknown field %%%c in command '%s'."), | |
170 | *pc, command.c_str()); | |
171 | str << *pc; | |
172 | } | |
173 | } | |
174 | else { | |
175 | str << *pc; | |
176 | } | |
177 | } | |
178 | ||
179 | // metamail(1) man page states that if the mailcap entry doesn't have '%s' | |
180 | // the program will accept the data on stdin so normally we should append | |
181 | // "< %s" to the end of the command in such case, but not all commands | |
182 | // behave like this, in particular a common test is 'test -n "$DISPLAY"' | |
183 | // and appending "< %s" to this command makes the test fail... I don't | |
184 | // know of the correct solution, try to guess what we have to do. | |
185 | if ( !hasFilename && !str.IsEmpty() | |
186 | #ifdef __UNIX__ | |
187 | && !str.StartsWith(_T("test ")) | |
188 | #endif // Unix | |
189 | ) { | |
190 | str << wxT(" < '") << params.GetFileName() << wxT('\''); | |
191 | } | |
192 | ||
193 | return str; | |
194 | } | |
195 | ||
196 | wxFileType::wxFileType(const wxFileTypeInfo& info) | |
197 | { | |
198 | m_info = &info; | |
199 | m_impl = NULL; | |
200 | } | |
201 | ||
202 | wxFileType::wxFileType() | |
203 | { | |
204 | m_info = NULL; | |
205 | m_impl = new wxFileTypeImpl; | |
206 | } | |
207 | ||
208 | wxFileType::~wxFileType() | |
209 | { | |
210 | if ( m_impl ) | |
211 | delete m_impl; | |
212 | } | |
213 | ||
214 | bool wxFileType::GetExtensions(wxArrayString& extensions) | |
215 | { | |
216 | if ( m_info ) | |
217 | { | |
218 | extensions = m_info->GetExtensions(); | |
219 | return TRUE; | |
220 | } | |
221 | ||
222 | return m_impl->GetExtensions(extensions); | |
223 | } | |
224 | ||
225 | bool wxFileType::GetMimeType(wxString *mimeType) const | |
226 | { | |
227 | wxCHECK_MSG( mimeType, FALSE, _T("invalid parameter in GetMimeType") ); | |
228 | ||
229 | if ( m_info ) | |
230 | { | |
231 | *mimeType = m_info->GetMimeType(); | |
232 | ||
233 | return TRUE; | |
234 | } | |
235 | ||
236 | return m_impl->GetMimeType(mimeType); | |
237 | } | |
238 | ||
239 | bool wxFileType::GetMimeTypes(wxArrayString& mimeTypes) const | |
240 | { | |
241 | if ( m_info ) | |
242 | { | |
243 | mimeTypes.Clear(); | |
244 | mimeTypes.Add(m_info->GetMimeType()); | |
245 | ||
246 | return TRUE; | |
247 | } | |
248 | ||
249 | return m_impl->GetMimeTypes(mimeTypes); | |
250 | } | |
251 | ||
252 | bool wxFileType::GetIcon(wxIcon *icon, | |
253 | wxString *iconFile, | |
254 | int *iconIndex) const | |
255 | { | |
256 | if ( m_info ) | |
257 | { | |
258 | if ( iconFile ) | |
259 | *iconFile = m_info->GetIconFile(); | |
260 | if ( iconIndex ) | |
261 | *iconIndex = m_info->GetIconIndex(); | |
262 | ||
263 | #if wxUSE_GUI | |
264 | if ( icon && !m_info->GetIconFile().empty() ) | |
265 | { | |
266 | // FIXME: what about the index? | |
267 | icon->LoadFile(m_info->GetIconFile()); | |
268 | } | |
269 | #endif // wxUSE_GUI | |
270 | ||
271 | return TRUE; | |
272 | } | |
273 | ||
274 | #ifdef __WXMSW__ | |
275 | return m_impl->GetIcon(icon, iconFile, iconIndex); | |
276 | #else | |
277 | return m_impl->GetIcon(icon); | |
278 | #endif | |
279 | } | |
280 | ||
281 | bool wxFileType::GetDescription(wxString *desc) const | |
282 | { | |
283 | wxCHECK_MSG( desc, FALSE, _T("invalid parameter in GetDescription") ); | |
284 | ||
285 | if ( m_info ) | |
286 | { | |
287 | *desc = m_info->GetDescription(); | |
288 | ||
289 | return TRUE; | |
290 | } | |
291 | ||
292 | return m_impl->GetDescription(desc); | |
293 | } | |
294 | ||
295 | bool | |
296 | wxFileType::GetOpenCommand(wxString *openCmd, | |
297 | const wxFileType::MessageParameters& params) const | |
298 | { | |
299 | wxCHECK_MSG( openCmd, FALSE, _T("invalid parameter in GetOpenCommand") ); | |
300 | ||
301 | if ( m_info ) | |
302 | { | |
303 | *openCmd = ExpandCommand(m_info->GetOpenCommand(), params); | |
304 | ||
305 | return TRUE; | |
306 | } | |
307 | ||
308 | return m_impl->GetOpenCommand(openCmd, params); | |
309 | } | |
310 | ||
311 | bool | |
312 | wxFileType::GetPrintCommand(wxString *printCmd, | |
313 | const wxFileType::MessageParameters& params) const | |
314 | { | |
315 | wxCHECK_MSG( printCmd, FALSE, _T("invalid parameter in GetPrintCommand") ); | |
316 | ||
317 | if ( m_info ) | |
318 | { | |
319 | *printCmd = ExpandCommand(m_info->GetPrintCommand(), params); | |
320 | ||
321 | return TRUE; | |
322 | } | |
323 | ||
324 | return m_impl->GetPrintCommand(printCmd, params); | |
325 | } | |
326 | ||
327 | ||
328 | size_t wxFileType::GetAllCommands(wxArrayString *verbs, | |
329 | wxArrayString *commands, | |
330 | const wxFileType::MessageParameters& params) const | |
331 | { | |
332 | if ( verbs ) | |
333 | verbs->Clear(); | |
334 | if ( commands ) | |
335 | commands->Clear(); | |
336 | ||
337 | #ifdef __WXMSW__ | |
338 | return m_impl->GetAllCommands(verbs, commands, params); | |
339 | #else // !__WXMSW__ | |
340 | // we don't know how to retrieve all commands, so just try the 2 we know | |
341 | // about | |
342 | size_t count = 0; | |
343 | wxString cmd; | |
344 | if ( GetOpenCommand(&cmd, params) ) | |
345 | { | |
346 | if ( verbs ) | |
347 | verbs->Add(_T("Open")); | |
348 | if ( commands ) | |
349 | commands->Add(cmd); | |
350 | count++; | |
351 | } | |
352 | ||
353 | if ( GetPrintCommand(&cmd, params) ) | |
354 | { | |
355 | if ( verbs ) | |
356 | verbs->Add(_T("Print")); | |
357 | if ( commands ) | |
358 | commands->Add(cmd); | |
359 | ||
360 | count++; | |
361 | } | |
362 | ||
363 | return count; | |
364 | #endif // __WXMSW__/!__WXMSW__ | |
365 | } | |
366 | ||
367 | bool wxFileType::Unassociate() | |
368 | { | |
369 | #if defined(__WXMSW__) || defined(__UNIX__) | |
370 | return m_impl->Unassociate(); | |
371 | #else | |
372 | wxFAIL_MSG( _T("not implemented") ); // TODO | |
373 | return FALSE; | |
374 | #endif | |
375 | } | |
376 | ||
377 | // ---------------------------------------------------------------------------- | |
378 | // wxMimeTypesManager | |
379 | // ---------------------------------------------------------------------------- | |
380 | ||
381 | void wxMimeTypesManager::EnsureImpl() | |
382 | { | |
383 | if ( !m_impl ) | |
384 | m_impl = new wxMimeTypesManagerImpl; | |
385 | } | |
386 | ||
387 | bool wxMimeTypesManager::IsOfType(const wxString& mimeType, | |
388 | const wxString& wildcard) | |
389 | { | |
390 | wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND, | |
391 | wxT("first MIME type can't contain wildcards") ); | |
392 | ||
393 | // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE) | |
394 | if ( wildcard.BeforeFirst(wxT('/')). | |
395 | IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) ) | |
396 | { | |
397 | wxString strSubtype = wildcard.AfterFirst(wxT('/')); | |
398 | ||
399 | if ( strSubtype == wxT("*") || | |
400 | strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), FALSE) ) | |
401 | { | |
402 | // matches (either exactly or it's a wildcard) | |
403 | return TRUE; | |
404 | } | |
405 | } | |
406 | ||
407 | return FALSE; | |
408 | } | |
409 | ||
410 | wxMimeTypesManager::wxMimeTypesManager() | |
411 | { | |
412 | m_impl = NULL; | |
413 | } | |
414 | ||
415 | wxMimeTypesManager::~wxMimeTypesManager() | |
416 | { | |
417 | if ( m_impl ) | |
418 | delete m_impl; | |
419 | } | |
420 | ||
421 | wxFileType * | |
422 | wxMimeTypesManager::Associate(const wxFileTypeInfo& ftInfo) | |
423 | { | |
424 | EnsureImpl(); | |
425 | ||
426 | #if defined(__WXMSW__) || defined(__UNIX__) | |
427 | return m_impl->Associate(ftInfo); | |
428 | #else // other platforms | |
429 | wxFAIL_MSG( _T("not implemented") ); // TODO | |
430 | return NULL; | |
431 | #endif // platforms | |
432 | } | |
433 | ||
434 | wxFileType * | |
435 | wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext) | |
436 | { | |
437 | EnsureImpl(); | |
438 | wxFileType *ft = m_impl->GetFileTypeFromExtension(ext); | |
439 | ||
440 | if ( !ft ) { | |
441 | // check the fallbacks | |
442 | // | |
443 | // TODO linear search is potentially slow, perhaps we should use a | |
444 | // sorted array? | |
445 | size_t count = m_fallbacks.GetCount(); | |
446 | for ( size_t n = 0; n < count; n++ ) { | |
447 | if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) { | |
448 | ft = new wxFileType(m_fallbacks[n]); | |
449 | ||
450 | break; | |
451 | } | |
452 | } | |
453 | } | |
454 | ||
455 | return ft; | |
456 | } | |
457 | ||
458 | wxFileType * | |
459 | wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType) | |
460 | { | |
461 | EnsureImpl(); | |
462 | wxFileType *ft = m_impl->GetFileTypeFromMimeType(mimeType); | |
463 | ||
464 | if ( ft ) { | |
465 | // check the fallbacks | |
466 | // | |
467 | // TODO linear search is potentially slow, perhaps we should use a sorted | |
468 | // array? | |
469 | size_t count = m_fallbacks.GetCount(); | |
470 | for ( size_t n = 0; n < count; n++ ) { | |
471 | if ( wxMimeTypesManager::IsOfType(mimeType, | |
472 | m_fallbacks[n].GetMimeType()) ) { | |
473 | ft = new wxFileType(m_fallbacks[n]); | |
474 | ||
475 | break; | |
476 | } | |
477 | } | |
478 | } | |
479 | ||
480 | return ft; | |
481 | } | |
482 | ||
483 | bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback) | |
484 | { | |
485 | EnsureImpl(); | |
486 | return m_impl->ReadMailcap(filename, fallback); | |
487 | } | |
488 | ||
489 | bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename) | |
490 | { | |
491 | EnsureImpl(); | |
492 | return m_impl->ReadMimeTypes(filename); | |
493 | } | |
494 | ||
495 | void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes) | |
496 | { | |
497 | EnsureImpl(); | |
498 | for ( const wxFileTypeInfo *ft = filetypes; ft && ft->IsValid(); ft++ ) { | |
499 | AddFallback(*ft); | |
500 | } | |
501 | } | |
502 | ||
503 | size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes) | |
504 | { | |
505 | EnsureImpl(); | |
506 | size_t countAll = m_impl->EnumAllFileTypes(mimetypes); | |
507 | ||
508 | // add the fallback filetypes | |
509 | size_t count = m_fallbacks.GetCount(); | |
510 | for ( size_t n = 0; n < count; n++ ) { | |
511 | if ( mimetypes.Index(m_fallbacks[n].GetMimeType()) == wxNOT_FOUND ) { | |
512 | mimetypes.Add(m_fallbacks[n].GetMimeType()); | |
513 | countAll++; | |
514 | } | |
515 | } | |
516 | ||
517 | return countAll; | |
518 | } | |
519 | ||
520 | // ---------------------------------------------------------------------------- | |
521 | // global data and wxMimeTypeCmnModule | |
522 | // ---------------------------------------------------------------------------- | |
523 | ||
524 | // private object | |
525 | static wxMimeTypesManager gs_mimeTypesManager; | |
526 | ||
527 | // and public pointer | |
528 | wxMimeTypesManager *wxTheMimeTypesManager = &gs_mimeTypesManager; | |
529 | ||
530 | class wxMimeTypeCmnModule: public wxModule | |
531 | { | |
532 | public: | |
533 | wxMimeTypeCmnModule() : wxModule() { } | |
534 | virtual bool OnInit() { return TRUE; } | |
535 | virtual void OnExit() | |
536 | { | |
537 | // this avoids false memory leak allerts: | |
538 | if ( gs_mimeTypesManager.m_impl != NULL ) | |
539 | { | |
540 | delete gs_mimeTypesManager.m_impl; | |
541 | gs_mimeTypesManager.m_impl = NULL; | |
542 | } | |
543 | } | |
544 | ||
545 | DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule) | |
546 | }; | |
547 | ||
548 | IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule, wxModule) | |
549 |