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