]>
Commit | Line | Data |
---|---|---|
7dc3cc31 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: common/mimecmn.cpp | |
3 | // Purpose: classes and functions to manage MIME types | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
c7ce8392 | 6 | // Chris Elliott (biol75@york.ac.uk) 5 Dec 00: write support for Win32 |
7dc3cc31 VS |
7 | // Created: 23.09.98 |
8 | // RCS-ID: $Id$ | |
9 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 10 | // Licence: wxWindows licence (part of wxExtra library) |
7dc3cc31 VS |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
a6c65e88 VZ |
13 | // ============================================================================ |
14 | // declarations | |
15 | // ============================================================================ | |
16 | ||
17 | // ---------------------------------------------------------------------------- | |
18 | // headers | |
19 | // ---------------------------------------------------------------------------- | |
20 | ||
7dc3cc31 VS |
21 | // for compilers that support precompilation, includes "wx.h". |
22 | #include "wx/wxprec.h" | |
23 | ||
24 | #ifdef __BORLANDC__ | |
1e6feb95 | 25 | #pragma hdrstop |
7dc3cc31 VS |
26 | #endif |
27 | ||
1e6feb95 VZ |
28 | #if wxUSE_MIMETYPE |
29 | ||
7dc3cc31 VS |
30 | #ifndef WX_PRECOMP |
31 | #include "wx/string.h" | |
7dc3cc31 VS |
32 | #endif //WX_PRECOMP |
33 | ||
2db1bd22 | 34 | #include "wx/module.h" |
7dc3cc31 VS |
35 | #include "wx/log.h" |
36 | #include "wx/file.h" | |
da0766ab | 37 | #include "wx/iconloc.h" |
7dc3cc31 VS |
38 | #include "wx/intl.h" |
39 | #include "wx/dynarray.h" | |
40 | #include "wx/confbase.h" | |
41 | ||
7dc3cc31 VS |
42 | #include "wx/mimetype.h" |
43 | ||
44 | // other standard headers | |
45 | #include <ctype.h> | |
46 | ||
7dc3cc31 | 47 | // implementation classes: |
7dc3cc31 | 48 | #if defined(__WXMSW__) |
c7ce8392 | 49 | #include "wx/msw/mimetype.h" |
5fde6fcc | 50 | #elif defined(__WXMAC__) |
c7ce8392 | 51 | #include "wx/mac/mimetype.h" |
64621b07 | 52 | #elif defined(__WXPM__) || defined (__EMX__) |
c7ce8392 | 53 | #include "wx/os2/mimetype.h" |
6691d737 | 54 | #undef __UNIX__ |
83d8eb47 MW |
55 | #elif defined(__DOS__) |
56 | #include "wx/msdos/mimetype.h" | |
c7ce8392 VZ |
57 | #else // Unix |
58 | #include "wx/unix/mimetype.h" | |
7dc3cc31 VS |
59 | #endif |
60 | ||
61 | // ============================================================================ | |
62 | // common classes | |
63 | // ============================================================================ | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // wxFileTypeInfo | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
57e67135 VZ |
69 | wxFileTypeInfo::wxFileTypeInfo(const wxChar *mimeType, |
70 | const wxChar *openCmd, | |
71 | const wxChar *printCmd, | |
72 | const wxChar *desc, | |
7dc3cc31 VS |
73 | ...) |
74 | : m_mimeType(mimeType), | |
75 | m_openCmd(openCmd), | |
76 | m_printCmd(printCmd), | |
77 | m_desc(desc) | |
78 | { | |
79 | va_list argptr; | |
80 | va_start(argptr, desc); | |
81 | ||
82 | for ( ;; ) | |
83 | { | |
2cfcf22d VZ |
84 | // icc gives this warning in its own va_arg() macro, argh |
85 | #ifdef __INTELC__ | |
86 | #pragma warning(push) | |
87 | #pragma warning(disable: 1684) | |
88 | #endif | |
89 | ||
57e67135 | 90 | const wxChar *ext = va_arg(argptr, const wxChar *); |
2cfcf22d VZ |
91 | |
92 | #ifdef __INTELC__ | |
93 | #pragma warning(pop) | |
94 | #endif | |
7dc3cc31 VS |
95 | if ( !ext ) |
96 | { | |
97 | // NULL terminates the list | |
98 | break; | |
99 | } | |
100 | ||
101 | m_exts.Add(ext); | |
102 | } | |
103 | ||
104 | va_end(argptr); | |
105 | } | |
106 | ||
2b813b73 VZ |
107 | |
108 | wxFileTypeInfo::wxFileTypeInfo(const wxArrayString& sArray) | |
109 | { | |
110 | m_mimeType = sArray [0u]; | |
111 | m_openCmd = sArray [1u]; | |
112 | m_printCmd = sArray [2u]; | |
113 | m_desc = sArray [3u]; | |
114 | ||
115 | size_t count = sArray.GetCount(); | |
116 | for ( size_t i = 4; i < count; i++ ) | |
117 | { | |
118 | m_exts.Add(sArray[i]); | |
119 | } | |
120 | } | |
121 | ||
7dc3cc31 | 122 | #include "wx/arrimpl.cpp" |
4115960d | 123 | WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo) |
7dc3cc31 | 124 | |
7dc3cc31 VS |
125 | // ============================================================================ |
126 | // implementation of the wrapper classes | |
127 | // ============================================================================ | |
128 | ||
129 | // ---------------------------------------------------------------------------- | |
130 | // wxFileType | |
131 | // ---------------------------------------------------------------------------- | |
132 | ||
a6c65e88 | 133 | /* static */ |
7dc3cc31 VS |
134 | wxString wxFileType::ExpandCommand(const wxString& command, |
135 | const wxFileType::MessageParameters& params) | |
136 | { | |
4e32eea1 | 137 | bool hasFilename = false; |
7dc3cc31 VS |
138 | |
139 | wxString str; | |
140 | for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) { | |
141 | if ( *pc == wxT('%') ) { | |
142 | switch ( *++pc ) { | |
143 | case wxT('s'): | |
144 | // '%s' expands into file name (quoted because it might | |
145 | // contain spaces) - except if there are already quotes | |
146 | // there because otherwise some programs may get confused | |
147 | // by double double quotes | |
148 | #if 0 | |
149 | if ( *(pc - 2) == wxT('"') ) | |
150 | str << params.GetFileName(); | |
151 | else | |
152 | str << wxT('"') << params.GetFileName() << wxT('"'); | |
153 | #endif | |
154 | str << params.GetFileName(); | |
4e32eea1 | 155 | hasFilename = true; |
7dc3cc31 VS |
156 | break; |
157 | ||
158 | case wxT('t'): | |
159 | // '%t' expands into MIME type (quote it too just to be | |
160 | // consistent) | |
161 | str << wxT('\'') << params.GetMimeType() << wxT('\''); | |
162 | break; | |
163 | ||
164 | case wxT('{'): | |
165 | { | |
166 | const wxChar *pEnd = wxStrchr(pc, wxT('}')); | |
167 | if ( pEnd == NULL ) { | |
168 | wxString mimetype; | |
f6bcfd97 | 169 | wxLogWarning(_("Unmatched '{' in an entry for mime type %s."), |
7dc3cc31 VS |
170 | params.GetMimeType().c_str()); |
171 | str << wxT("%{"); | |
172 | } | |
173 | else { | |
174 | wxString param(pc + 1, pEnd - pc - 1); | |
175 | str << wxT('\'') << params.GetParamValue(param) << wxT('\''); | |
176 | pc = pEnd; | |
177 | } | |
178 | } | |
179 | break; | |
180 | ||
181 | case wxT('n'): | |
182 | case wxT('F'): | |
183 | // TODO %n is the number of parts, %F is an array containing | |
184 | // the names of temp files these parts were written to | |
185 | // and their mime types. | |
186 | break; | |
187 | ||
188 | default: | |
189 | wxLogDebug(wxT("Unknown field %%%c in command '%s'."), | |
190 | *pc, command.c_str()); | |
191 | str << *pc; | |
192 | } | |
193 | } | |
194 | else { | |
195 | str << *pc; | |
196 | } | |
197 | } | |
198 | ||
199 | // metamail(1) man page states that if the mailcap entry doesn't have '%s' | |
f6bcfd97 BP |
200 | // the program will accept the data on stdin so normally we should append |
201 | // "< %s" to the end of the command in such case, but not all commands | |
202 | // behave like this, in particular a common test is 'test -n "$DISPLAY"' | |
203 | // and appending "< %s" to this command makes the test fail... I don't | |
204 | // know of the correct solution, try to guess what we have to do. | |
2b813b73 VZ |
205 | |
206 | // test now carried out on reading file so test should never get here | |
525d8583 | 207 | if ( !hasFilename && !str.empty() |
f6bcfd97 BP |
208 | #ifdef __UNIX__ |
209 | && !str.StartsWith(_T("test ")) | |
210 | #endif // Unix | |
211 | ) { | |
7dc3cc31 VS |
212 | str << wxT(" < '") << params.GetFileName() << wxT('\''); |
213 | } | |
214 | ||
215 | return str; | |
216 | } | |
217 | ||
a6c65e88 VZ |
218 | wxFileType::wxFileType(const wxFileTypeInfo& info) |
219 | { | |
220 | m_info = &info; | |
221 | m_impl = NULL; | |
222 | } | |
223 | ||
7dc3cc31 VS |
224 | wxFileType::wxFileType() |
225 | { | |
a6c65e88 | 226 | m_info = NULL; |
7dc3cc31 VS |
227 | m_impl = new wxFileTypeImpl; |
228 | } | |
229 | ||
230 | wxFileType::~wxFileType() | |
231 | { | |
dca2d56f GT |
232 | if ( m_impl ) |
233 | delete m_impl; | |
7dc3cc31 VS |
234 | } |
235 | ||
236 | bool wxFileType::GetExtensions(wxArrayString& extensions) | |
237 | { | |
a6c65e88 VZ |
238 | if ( m_info ) |
239 | { | |
240 | extensions = m_info->GetExtensions(); | |
4e32eea1 | 241 | return true; |
a6c65e88 VZ |
242 | } |
243 | ||
7dc3cc31 VS |
244 | return m_impl->GetExtensions(extensions); |
245 | } | |
246 | ||
247 | bool wxFileType::GetMimeType(wxString *mimeType) const | |
248 | { | |
4e32eea1 | 249 | wxCHECK_MSG( mimeType, false, _T("invalid parameter in GetMimeType") ); |
a6c65e88 VZ |
250 | |
251 | if ( m_info ) | |
252 | { | |
253 | *mimeType = m_info->GetMimeType(); | |
254 | ||
4e32eea1 | 255 | return true; |
a6c65e88 VZ |
256 | } |
257 | ||
7dc3cc31 VS |
258 | return m_impl->GetMimeType(mimeType); |
259 | } | |
260 | ||
4d2976ad VS |
261 | bool wxFileType::GetMimeTypes(wxArrayString& mimeTypes) const |
262 | { | |
a6c65e88 VZ |
263 | if ( m_info ) |
264 | { | |
265 | mimeTypes.Clear(); | |
266 | mimeTypes.Add(m_info->GetMimeType()); | |
267 | ||
4e32eea1 | 268 | return true; |
a6c65e88 VZ |
269 | } |
270 | ||
4d2976ad VS |
271 | return m_impl->GetMimeTypes(mimeTypes); |
272 | } | |
273 | ||
da0766ab | 274 | bool wxFileType::GetIcon(wxIconLocation *iconLoc) const |
7dc3cc31 | 275 | { |
a6c65e88 VZ |
276 | if ( m_info ) |
277 | { | |
da0766ab | 278 | if ( iconLoc ) |
a6c65e88 | 279 | { |
da0766ab VZ |
280 | iconLoc->SetFileName(m_info->GetIconFile()); |
281 | #ifdef __WXMSW__ | |
282 | iconLoc->SetIndex(m_info->GetIconIndex()); | |
283 | #endif // __WXMSW__ | |
a6c65e88 | 284 | } |
a6c65e88 | 285 | |
4e32eea1 | 286 | return true; |
a6c65e88 VZ |
287 | } |
288 | ||
da0766ab | 289 | return m_impl->GetIcon(iconLoc); |
7dc3cc31 VS |
290 | } |
291 | ||
11d395f9 VZ |
292 | bool |
293 | wxFileType::GetIcon(wxIconLocation *iconloc, | |
294 | const MessageParameters& params) const | |
295 | { | |
296 | if ( !GetIcon(iconloc) ) | |
297 | { | |
298 | return false; | |
299 | } | |
300 | ||
301 | // we may have "%s" in the icon location string, at least under Windows, so | |
302 | // expand this | |
303 | if ( iconloc ) | |
304 | { | |
305 | iconloc->SetFileName(ExpandCommand(iconloc->GetFileName(), params)); | |
306 | } | |
307 | ||
308 | return true; | |
309 | } | |
310 | ||
7dc3cc31 VS |
311 | bool wxFileType::GetDescription(wxString *desc) const |
312 | { | |
4e32eea1 | 313 | wxCHECK_MSG( desc, false, _T("invalid parameter in GetDescription") ); |
a6c65e88 VZ |
314 | |
315 | if ( m_info ) | |
316 | { | |
317 | *desc = m_info->GetDescription(); | |
318 | ||
4e32eea1 | 319 | return true; |
a6c65e88 VZ |
320 | } |
321 | ||
7dc3cc31 VS |
322 | return m_impl->GetDescription(desc); |
323 | } | |
324 | ||
325 | bool | |
326 | wxFileType::GetOpenCommand(wxString *openCmd, | |
327 | const wxFileType::MessageParameters& params) const | |
328 | { | |
4e32eea1 | 329 | wxCHECK_MSG( openCmd, false, _T("invalid parameter in GetOpenCommand") ); |
a6c65e88 VZ |
330 | |
331 | if ( m_info ) | |
332 | { | |
333 | *openCmd = ExpandCommand(m_info->GetOpenCommand(), params); | |
334 | ||
4e32eea1 | 335 | return true; |
a6c65e88 VZ |
336 | } |
337 | ||
7dc3cc31 VS |
338 | return m_impl->GetOpenCommand(openCmd, params); |
339 | } | |
340 | ||
0532a258 VZ |
341 | wxString wxFileType::GetOpenCommand(const wxString& filename) const |
342 | { | |
343 | wxString cmd; | |
344 | if ( !GetOpenCommand(&cmd, filename) ) | |
345 | { | |
346 | // return empty string to indicate an error | |
347 | cmd.clear(); | |
348 | } | |
349 | ||
350 | return cmd; | |
351 | } | |
352 | ||
7dc3cc31 VS |
353 | bool |
354 | wxFileType::GetPrintCommand(wxString *printCmd, | |
355 | const wxFileType::MessageParameters& params) const | |
356 | { | |
4e32eea1 | 357 | wxCHECK_MSG( printCmd, false, _T("invalid parameter in GetPrintCommand") ); |
a6c65e88 VZ |
358 | |
359 | if ( m_info ) | |
360 | { | |
361 | *printCmd = ExpandCommand(m_info->GetPrintCommand(), params); | |
362 | ||
4e32eea1 | 363 | return true; |
a6c65e88 VZ |
364 | } |
365 | ||
7dc3cc31 VS |
366 | return m_impl->GetPrintCommand(printCmd, params); |
367 | } | |
368 | ||
c7ce8392 VZ |
369 | |
370 | size_t wxFileType::GetAllCommands(wxArrayString *verbs, | |
371 | wxArrayString *commands, | |
372 | const wxFileType::MessageParameters& params) const | |
373 | { | |
374 | if ( verbs ) | |
375 | verbs->Clear(); | |
376 | if ( commands ) | |
377 | commands->Clear(); | |
378 | ||
2900bd1c | 379 | #if defined (__WXMSW__) || defined(__UNIX__) |
c7ce8392 | 380 | return m_impl->GetAllCommands(verbs, commands, params); |
2b813b73 | 381 | #else // !__WXMSW__ || Unix |
c7ce8392 VZ |
382 | // we don't know how to retrieve all commands, so just try the 2 we know |
383 | // about | |
384 | size_t count = 0; | |
385 | wxString cmd; | |
a6c65e88 | 386 | if ( GetOpenCommand(&cmd, params) ) |
c7ce8392 VZ |
387 | { |
388 | if ( verbs ) | |
389 | verbs->Add(_T("Open")); | |
390 | if ( commands ) | |
391 | commands->Add(cmd); | |
392 | count++; | |
393 | } | |
394 | ||
395 | if ( GetPrintCommand(&cmd, params) ) | |
396 | { | |
397 | if ( verbs ) | |
398 | verbs->Add(_T("Print")); | |
399 | if ( commands ) | |
400 | commands->Add(cmd); | |
401 | ||
402 | count++; | |
403 | } | |
404 | ||
405 | return count; | |
2b813b73 | 406 | #endif // __WXMSW__/| __UNIX__ |
c7ce8392 VZ |
407 | } |
408 | ||
a6c65e88 | 409 | bool wxFileType::Unassociate() |
c7ce8392 | 410 | { |
2b813b73 | 411 | #if defined(__WXMSW__) |
a6c65e88 | 412 | return m_impl->Unassociate(); |
6691d737 | 413 | #elif defined(__UNIX__) |
2b813b73 | 414 | return m_impl->Unassociate(this); |
2900bd1c | 415 | #else |
a6c65e88 | 416 | wxFAIL_MSG( _T("not implemented") ); // TODO |
4e32eea1 | 417 | return false; |
2900bd1c | 418 | #endif |
2b813b73 VZ |
419 | } |
420 | ||
7a893a31 WS |
421 | bool wxFileType::SetCommand(const wxString& cmd, |
422 | const wxString& verb, | |
423 | bool overwriteprompt) | |
2b813b73 | 424 | { |
2900bd1c | 425 | #if defined (__WXMSW__) || defined(__UNIX__) |
2b813b73 VZ |
426 | return m_impl->SetCommand(cmd, verb, overwriteprompt); |
427 | #else | |
7a893a31 WS |
428 | wxUnusedVar(cmd); |
429 | wxUnusedVar(verb); | |
430 | wxUnusedVar(overwriteprompt); | |
2b813b73 | 431 | wxFAIL_MSG(_T("not implemented")); |
4e32eea1 | 432 | return false; |
c7ce8392 VZ |
433 | #endif |
434 | } | |
435 | ||
2b813b73 VZ |
436 | bool wxFileType::SetDefaultIcon(const wxString& cmd, int index) |
437 | { | |
438 | wxString sTmp = cmd; | |
439 | #ifdef __WXMSW__ | |
440 | // VZ: should we do this? | |
441 | // chris elliott : only makes sense in MS windows | |
442 | if ( sTmp.empty() ) | |
525d8583 | 443 | GetOpenCommand(&sTmp, wxFileType::MessageParameters(wxEmptyString, wxEmptyString)); |
2b813b73 | 444 | #endif |
4e32eea1 | 445 | wxCHECK_MSG( !sTmp.empty(), false, _T("need the icon file") ); |
2b813b73 | 446 | |
2900bd1c | 447 | #if defined (__WXMSW__) || defined(__UNIX__) |
2b813b73 VZ |
448 | return m_impl->SetDefaultIcon (cmd, index); |
449 | #else | |
7a893a31 | 450 | wxUnusedVar(index); |
2b813b73 | 451 | wxFAIL_MSG(_T("not implemented")); |
4e32eea1 | 452 | return false; |
2b813b73 VZ |
453 | #endif |
454 | } | |
455 | ||
2b850ae1 RR |
456 | //---------------------------------------------------------------------------- |
457 | // wxMimeTypesManagerFactory | |
458 | //---------------------------------------------------------------------------- | |
459 | ||
460 | wxMimeTypesManagerFactory *wxMimeTypesManagerFactory::m_factory = NULL; | |
461 | ||
462 | /* static */ | |
463 | void wxMimeTypesManagerFactory::SetFactory( wxMimeTypesManagerFactory *factory ) | |
464 | { | |
465 | if (wxMimeTypesManagerFactory::m_factory) | |
466 | delete wxMimeTypesManagerFactory::m_factory; | |
467 | ||
468 | wxMimeTypesManagerFactory::m_factory = factory; | |
469 | } | |
470 | ||
471 | /* static */ | |
472 | wxMimeTypesManagerFactory *wxMimeTypesManagerFactory::GetFactory() | |
473 | { | |
474 | if (!wxMimeTypesManagerFactory::m_factory) | |
475 | wxMimeTypesManagerFactory::m_factory = new wxMimeTypesManagerFactory; | |
476 | ||
477 | return wxMimeTypesManagerFactory::m_factory; | |
478 | } | |
479 | ||
480 | wxMimeTypesManagerImpl *wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl() | |
481 | { | |
482 | return new wxMimeTypesManagerImpl; | |
483 | } | |
2b813b73 | 484 | |
7dc3cc31 VS |
485 | // ---------------------------------------------------------------------------- |
486 | // wxMimeTypesManager | |
487 | // ---------------------------------------------------------------------------- | |
488 | ||
489 | void wxMimeTypesManager::EnsureImpl() | |
490 | { | |
c7ce8392 | 491 | if ( !m_impl ) |
2b850ae1 | 492 | m_impl = wxMimeTypesManagerFactory::GetFactory()->CreateMimeTypesManagerImpl(); |
7dc3cc31 VS |
493 | } |
494 | ||
495 | bool wxMimeTypesManager::IsOfType(const wxString& mimeType, | |
496 | const wxString& wildcard) | |
497 | { | |
498 | wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND, | |
499 | wxT("first MIME type can't contain wildcards") ); | |
500 | ||
4e32eea1 | 501 | // all comparaisons are case insensitive (2nd arg of IsSameAs() is false) |
a6c65e88 | 502 | if ( wildcard.BeforeFirst(wxT('/')). |
4e32eea1 | 503 | IsSameAs(mimeType.BeforeFirst(wxT('/')), false) ) |
7dc3cc31 VS |
504 | { |
505 | wxString strSubtype = wildcard.AfterFirst(wxT('/')); | |
506 | ||
507 | if ( strSubtype == wxT("*") || | |
4e32eea1 | 508 | strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), false) ) |
7dc3cc31 VS |
509 | { |
510 | // matches (either exactly or it's a wildcard) | |
4e32eea1 | 511 | return true; |
7dc3cc31 VS |
512 | } |
513 | } | |
514 | ||
4e32eea1 | 515 | return false; |
7dc3cc31 VS |
516 | } |
517 | ||
518 | wxMimeTypesManager::wxMimeTypesManager() | |
519 | { | |
520 | m_impl = NULL; | |
521 | } | |
522 | ||
523 | wxMimeTypesManager::~wxMimeTypesManager() | |
524 | { | |
8a16a98e GT |
525 | if ( m_impl ) |
526 | delete m_impl; | |
7dc3cc31 VS |
527 | } |
528 | ||
2b813b73 VZ |
529 | bool wxMimeTypesManager::Unassociate(wxFileType *ft) |
530 | { | |
6691d737 | 531 | #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__) |
2b813b73 VZ |
532 | return m_impl->Unassociate(ft); |
533 | #else | |
534 | return ft->Unassociate(); | |
535 | #endif | |
536 | } | |
537 | ||
538 | ||
7dc3cc31 | 539 | wxFileType * |
a6c65e88 | 540 | wxMimeTypesManager::Associate(const wxFileTypeInfo& ftInfo) |
7dc3cc31 VS |
541 | { |
542 | EnsureImpl(); | |
a6c65e88 | 543 | |
6691d737 | 544 | #if defined(__WXMSW__) || defined(__UNIX__) |
a6c65e88 VZ |
545 | return m_impl->Associate(ftInfo); |
546 | #else // other platforms | |
7a893a31 | 547 | wxUnusedVar(ftInfo); |
a6c65e88 VZ |
548 | wxFAIL_MSG( _T("not implemented") ); // TODO |
549 | return NULL; | |
550 | #endif // platforms | |
7dc3cc31 VS |
551 | } |
552 | ||
c7ce8392 | 553 | wxFileType * |
a6c65e88 | 554 | wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext) |
c7ce8392 VZ |
555 | { |
556 | EnsureImpl(); | |
a6c65e88 VZ |
557 | wxFileType *ft = m_impl->GetFileTypeFromExtension(ext); |
558 | ||
559 | if ( !ft ) { | |
560 | // check the fallbacks | |
561 | // | |
562 | // TODO linear search is potentially slow, perhaps we should use a | |
563 | // sorted array? | |
564 | size_t count = m_fallbacks.GetCount(); | |
565 | for ( size_t n = 0; n < count; n++ ) { | |
566 | if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) { | |
567 | ft = new wxFileType(m_fallbacks[n]); | |
568 | ||
569 | break; | |
570 | } | |
571 | } | |
572 | } | |
c7ce8392 | 573 | |
a6c65e88 | 574 | return ft; |
c7ce8392 VZ |
575 | } |
576 | ||
7dc3cc31 VS |
577 | wxFileType * |
578 | wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType) | |
579 | { | |
580 | EnsureImpl(); | |
a6c65e88 VZ |
581 | wxFileType *ft = m_impl->GetFileTypeFromMimeType(mimeType); |
582 | ||
f627fbee | 583 | if ( !ft ) { |
a6c65e88 VZ |
584 | // check the fallbacks |
585 | // | |
f627fbee VZ |
586 | // TODO linear search is potentially slow, perhaps we should use a |
587 | // sorted array? | |
a6c65e88 VZ |
588 | size_t count = m_fallbacks.GetCount(); |
589 | for ( size_t n = 0; n < count; n++ ) { | |
590 | if ( wxMimeTypesManager::IsOfType(mimeType, | |
591 | m_fallbacks[n].GetMimeType()) ) { | |
592 | ft = new wxFileType(m_fallbacks[n]); | |
593 | ||
594 | break; | |
595 | } | |
596 | } | |
597 | } | |
598 | ||
599 | return ft; | |
7dc3cc31 VS |
600 | } |
601 | ||
602 | bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback) | |
603 | { | |
604 | EnsureImpl(); | |
605 | return m_impl->ReadMailcap(filename, fallback); | |
606 | } | |
607 | ||
608 | bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename) | |
609 | { | |
610 | EnsureImpl(); | |
611 | return m_impl->ReadMimeTypes(filename); | |
612 | } | |
613 | ||
614 | void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes) | |
615 | { | |
616 | EnsureImpl(); | |
a6c65e88 VZ |
617 | for ( const wxFileTypeInfo *ft = filetypes; ft && ft->IsValid(); ft++ ) { |
618 | AddFallback(*ft); | |
7dc3cc31 VS |
619 | } |
620 | } | |
621 | ||
622 | size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes) | |
623 | { | |
624 | EnsureImpl(); | |
a6c65e88 VZ |
625 | size_t countAll = m_impl->EnumAllFileTypes(mimetypes); |
626 | ||
627 | // add the fallback filetypes | |
628 | size_t count = m_fallbacks.GetCount(); | |
629 | for ( size_t n = 0; n < count; n++ ) { | |
630 | if ( mimetypes.Index(m_fallbacks[n].GetMimeType()) == wxNOT_FOUND ) { | |
631 | mimetypes.Add(m_fallbacks[n].GetMimeType()); | |
632 | countAll++; | |
633 | } | |
634 | } | |
7dc3cc31 | 635 | |
a6c65e88 VZ |
636 | return countAll; |
637 | } | |
7dc3cc31 | 638 | |
2b813b73 VZ |
639 | void wxMimeTypesManager::Initialize(int mcapStyle, |
640 | const wxString& sExtraDir) | |
641 | { | |
6691d737 | 642 | #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__) |
2b813b73 VZ |
643 | EnsureImpl(); |
644 | ||
645 | m_impl->Initialize(mcapStyle, sExtraDir); | |
33ac7e6f | 646 | #else |
4e32eea1 WS |
647 | (void)mcapStyle; |
648 | (void)sExtraDir; | |
2b813b73 VZ |
649 | #endif // Unix |
650 | } | |
651 | ||
652 | // and this function clears all the data from the manager | |
653 | void wxMimeTypesManager::ClearData() | |
654 | { | |
6691d737 | 655 | #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__) |
2b813b73 VZ |
656 | EnsureImpl(); |
657 | ||
658 | m_impl->ClearData(); | |
659 | #endif // Unix | |
660 | } | |
661 | ||
7dc3cc31 | 662 | // ---------------------------------------------------------------------------- |
a6c65e88 | 663 | // global data and wxMimeTypeCmnModule |
7dc3cc31 VS |
664 | // ---------------------------------------------------------------------------- |
665 | ||
666 | // private object | |
667 | static wxMimeTypesManager gs_mimeTypesManager; | |
668 | ||
669 | // and public pointer | |
a6c65e88 | 670 | wxMimeTypesManager *wxTheMimeTypesManager = &gs_mimeTypesManager; |
7dc3cc31 | 671 | |
66806a0b VS |
672 | class wxMimeTypeCmnModule: public wxModule |
673 | { | |
66806a0b | 674 | public: |
c7ce8392 | 675 | wxMimeTypeCmnModule() : wxModule() { } |
4e32eea1 | 676 | virtual bool OnInit() { return true; } |
c7ce8392 VZ |
677 | virtual void OnExit() |
678 | { | |
679 | // this avoids false memory leak allerts: | |
680 | if ( gs_mimeTypesManager.m_impl != NULL ) | |
681 | { | |
682 | delete gs_mimeTypesManager.m_impl; | |
683 | gs_mimeTypesManager.m_impl = NULL; | |
65e50848 | 684 | gs_mimeTypesManager.m_fallbacks.Clear(); |
c7ce8392 | 685 | } |
66806a0b | 686 | } |
c7ce8392 VZ |
687 | |
688 | DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule) | |
66806a0b VS |
689 | }; |
690 | ||
691 | IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule, wxModule) | |
1e6feb95 VZ |
692 | |
693 | #endif // wxUSE_MIMETYPE |