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