]>
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: |
d98a58c5 | 49 | #if defined(__WINDOWS__) |
c7ce8392 | 50 | #include "wx/msw/mimetype.h" |
bd7bfb00 | 51 | #elif ( defined(__WXMAC__) ) |
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 | 194 | |
9ac34ac9 VZ |
195 | // We consider that only the file names with spaces in them need to be |
196 | // handled specially. This is not perfect, but this can be done easily | |
197 | // under all platforms while handling the file names with quotes in them, | |
198 | // for example, needs to be done differently. | |
199 | const bool needToQuoteFilename = params.GetFileName().find_first_of(" \t") | |
200 | != wxString::npos; | |
201 | ||
7dc3cc31 VS |
202 | wxString str; |
203 | for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) { | |
204 | if ( *pc == wxT('%') ) { | |
205 | switch ( *++pc ) { | |
206 | case wxT('s'): | |
9ac34ac9 VZ |
207 | // don't quote the file name if it's already quoted: notice |
208 | // that we check for a quote following it and not preceding | |
209 | // it as at least under Windows we can have commands | |
210 | // containing "file://%s" (with quotes) in them so the | |
211 | // argument may be quoted even if there is no quote | |
212 | // directly before "%s" itself | |
213 | if ( needToQuoteFilename && pc[1] != '"' ) | |
7dc3cc31 | 214 | str << wxT('"') << params.GetFileName() << wxT('"'); |
9ac34ac9 VZ |
215 | else |
216 | str << params.GetFileName(); | |
4e32eea1 | 217 | hasFilename = true; |
7dc3cc31 VS |
218 | break; |
219 | ||
220 | case wxT('t'): | |
221 | // '%t' expands into MIME type (quote it too just to be | |
222 | // consistent) | |
223 | str << wxT('\'') << params.GetMimeType() << wxT('\''); | |
224 | break; | |
225 | ||
226 | case wxT('{'): | |
227 | { | |
228 | const wxChar *pEnd = wxStrchr(pc, wxT('}')); | |
229 | if ( pEnd == NULL ) { | |
230 | wxString mimetype; | |
f6bcfd97 | 231 | wxLogWarning(_("Unmatched '{' in an entry for mime type %s."), |
7dc3cc31 VS |
232 | params.GetMimeType().c_str()); |
233 | str << wxT("%{"); | |
234 | } | |
235 | else { | |
236 | wxString param(pc + 1, pEnd - pc - 1); | |
237 | str << wxT('\'') << params.GetParamValue(param) << wxT('\''); | |
238 | pc = pEnd; | |
239 | } | |
240 | } | |
241 | break; | |
242 | ||
243 | case wxT('n'): | |
244 | case wxT('F'): | |
245 | // TODO %n is the number of parts, %F is an array containing | |
246 | // the names of temp files these parts were written to | |
247 | // and their mime types. | |
248 | break; | |
249 | ||
250 | default: | |
251 | wxLogDebug(wxT("Unknown field %%%c in command '%s'."), | |
252 | *pc, command.c_str()); | |
253 | str << *pc; | |
254 | } | |
255 | } | |
256 | else { | |
257 | str << *pc; | |
258 | } | |
259 | } | |
260 | ||
261 | // metamail(1) man page states that if the mailcap entry doesn't have '%s' | |
f6bcfd97 BP |
262 | // the program will accept the data on stdin so normally we should append |
263 | // "< %s" to the end of the command in such case, but not all commands | |
264 | // behave like this, in particular a common test is 'test -n "$DISPLAY"' | |
265 | // and appending "< %s" to this command makes the test fail... I don't | |
266 | // know of the correct solution, try to guess what we have to do. | |
2b813b73 VZ |
267 | |
268 | // test now carried out on reading file so test should never get here | |
525d8583 | 269 | if ( !hasFilename && !str.empty() |
f6bcfd97 | 270 | #ifdef __UNIX__ |
9a83f860 | 271 | && !str.StartsWith(wxT("test ")) |
f6bcfd97 | 272 | #endif // Unix |
9ac34ac9 VZ |
273 | ) |
274 | { | |
275 | str << wxT(" < "); | |
276 | if ( needToQuoteFilename ) | |
277 | str << '"'; | |
278 | str << params.GetFileName(); | |
279 | if ( needToQuoteFilename ) | |
280 | str << '"'; | |
7dc3cc31 VS |
281 | } |
282 | ||
283 | return str; | |
284 | } | |
285 | ||
a6c65e88 VZ |
286 | wxFileType::wxFileType(const wxFileTypeInfo& info) |
287 | { | |
288 | m_info = &info; | |
289 | m_impl = NULL; | |
290 | } | |
291 | ||
7dc3cc31 VS |
292 | wxFileType::wxFileType() |
293 | { | |
a6c65e88 | 294 | m_info = NULL; |
7dc3cc31 VS |
295 | m_impl = new wxFileTypeImpl; |
296 | } | |
297 | ||
298 | wxFileType::~wxFileType() | |
299 | { | |
dca2d56f GT |
300 | if ( m_impl ) |
301 | delete m_impl; | |
7dc3cc31 VS |
302 | } |
303 | ||
304 | bool wxFileType::GetExtensions(wxArrayString& extensions) | |
305 | { | |
a6c65e88 VZ |
306 | if ( m_info ) |
307 | { | |
308 | extensions = m_info->GetExtensions(); | |
4e32eea1 | 309 | return true; |
a6c65e88 VZ |
310 | } |
311 | ||
7dc3cc31 VS |
312 | return m_impl->GetExtensions(extensions); |
313 | } | |
314 | ||
315 | bool wxFileType::GetMimeType(wxString *mimeType) const | |
316 | { | |
9a83f860 | 317 | wxCHECK_MSG( mimeType, false, wxT("invalid parameter in GetMimeType") ); |
a6c65e88 VZ |
318 | |
319 | if ( m_info ) | |
320 | { | |
321 | *mimeType = m_info->GetMimeType(); | |
322 | ||
4e32eea1 | 323 | return true; |
a6c65e88 VZ |
324 | } |
325 | ||
7dc3cc31 VS |
326 | return m_impl->GetMimeType(mimeType); |
327 | } | |
328 | ||
4d2976ad VS |
329 | bool wxFileType::GetMimeTypes(wxArrayString& mimeTypes) const |
330 | { | |
a6c65e88 VZ |
331 | if ( m_info ) |
332 | { | |
333 | mimeTypes.Clear(); | |
334 | mimeTypes.Add(m_info->GetMimeType()); | |
335 | ||
4e32eea1 | 336 | return true; |
a6c65e88 VZ |
337 | } |
338 | ||
4d2976ad VS |
339 | return m_impl->GetMimeTypes(mimeTypes); |
340 | } | |
341 | ||
da0766ab | 342 | bool wxFileType::GetIcon(wxIconLocation *iconLoc) const |
7dc3cc31 | 343 | { |
a6c65e88 VZ |
344 | if ( m_info ) |
345 | { | |
da0766ab | 346 | if ( iconLoc ) |
a6c65e88 | 347 | { |
da0766ab | 348 | iconLoc->SetFileName(m_info->GetIconFile()); |
d98a58c5 | 349 | #ifdef __WINDOWS__ |
da0766ab | 350 | iconLoc->SetIndex(m_info->GetIconIndex()); |
d98a58c5 | 351 | #endif // __WINDOWS__ |
a6c65e88 | 352 | } |
a6c65e88 | 353 | |
4e32eea1 | 354 | return true; |
a6c65e88 VZ |
355 | } |
356 | ||
da0766ab | 357 | return m_impl->GetIcon(iconLoc); |
7dc3cc31 VS |
358 | } |
359 | ||
11d395f9 VZ |
360 | bool |
361 | wxFileType::GetIcon(wxIconLocation *iconloc, | |
362 | const MessageParameters& params) const | |
363 | { | |
364 | if ( !GetIcon(iconloc) ) | |
365 | { | |
366 | return false; | |
367 | } | |
368 | ||
369 | // we may have "%s" in the icon location string, at least under Windows, so | |
370 | // expand this | |
371 | if ( iconloc ) | |
372 | { | |
373 | iconloc->SetFileName(ExpandCommand(iconloc->GetFileName(), params)); | |
374 | } | |
375 | ||
376 | return true; | |
377 | } | |
378 | ||
7dc3cc31 VS |
379 | bool wxFileType::GetDescription(wxString *desc) const |
380 | { | |
9a83f860 | 381 | wxCHECK_MSG( desc, false, wxT("invalid parameter in GetDescription") ); |
a6c65e88 VZ |
382 | |
383 | if ( m_info ) | |
384 | { | |
385 | *desc = m_info->GetDescription(); | |
386 | ||
4e32eea1 | 387 | return true; |
a6c65e88 VZ |
388 | } |
389 | ||
7dc3cc31 VS |
390 | return m_impl->GetDescription(desc); |
391 | } | |
392 | ||
393 | bool | |
394 | wxFileType::GetOpenCommand(wxString *openCmd, | |
395 | const wxFileType::MessageParameters& params) const | |
396 | { | |
9a83f860 | 397 | wxCHECK_MSG( openCmd, false, wxT("invalid parameter in GetOpenCommand") ); |
a6c65e88 VZ |
398 | |
399 | if ( m_info ) | |
400 | { | |
401 | *openCmd = ExpandCommand(m_info->GetOpenCommand(), params); | |
402 | ||
4e32eea1 | 403 | return true; |
a6c65e88 VZ |
404 | } |
405 | ||
7dc3cc31 VS |
406 | return m_impl->GetOpenCommand(openCmd, params); |
407 | } | |
408 | ||
0532a258 VZ |
409 | wxString wxFileType::GetOpenCommand(const wxString& filename) const |
410 | { | |
411 | wxString cmd; | |
412 | if ( !GetOpenCommand(&cmd, filename) ) | |
413 | { | |
414 | // return empty string to indicate an error | |
415 | cmd.clear(); | |
416 | } | |
417 | ||
418 | return cmd; | |
419 | } | |
420 | ||
7dc3cc31 VS |
421 | bool |
422 | wxFileType::GetPrintCommand(wxString *printCmd, | |
423 | const wxFileType::MessageParameters& params) const | |
424 | { | |
9a83f860 | 425 | wxCHECK_MSG( printCmd, false, wxT("invalid parameter in GetPrintCommand") ); |
a6c65e88 VZ |
426 | |
427 | if ( m_info ) | |
428 | { | |
429 | *printCmd = ExpandCommand(m_info->GetPrintCommand(), params); | |
430 | ||
4e32eea1 | 431 | return true; |
a6c65e88 VZ |
432 | } |
433 | ||
7dc3cc31 VS |
434 | return m_impl->GetPrintCommand(printCmd, params); |
435 | } | |
436 | ||
c7ce8392 VZ |
437 | |
438 | size_t wxFileType::GetAllCommands(wxArrayString *verbs, | |
439 | wxArrayString *commands, | |
440 | const wxFileType::MessageParameters& params) const | |
441 | { | |
442 | if ( verbs ) | |
443 | verbs->Clear(); | |
444 | if ( commands ) | |
445 | commands->Clear(); | |
446 | ||
d98a58c5 | 447 | #if defined (__WINDOWS__) || defined(__UNIX__) |
c7ce8392 | 448 | return m_impl->GetAllCommands(verbs, commands, params); |
d98a58c5 | 449 | #else // !__WINDOWS__ || __UNIX__ |
c7ce8392 VZ |
450 | // we don't know how to retrieve all commands, so just try the 2 we know |
451 | // about | |
452 | size_t count = 0; | |
453 | wxString cmd; | |
a6c65e88 | 454 | if ( GetOpenCommand(&cmd, params) ) |
c7ce8392 VZ |
455 | { |
456 | if ( verbs ) | |
9a83f860 | 457 | verbs->Add(wxT("Open")); |
c7ce8392 VZ |
458 | if ( commands ) |
459 | commands->Add(cmd); | |
460 | count++; | |
461 | } | |
462 | ||
463 | if ( GetPrintCommand(&cmd, params) ) | |
464 | { | |
465 | if ( verbs ) | |
9a83f860 | 466 | verbs->Add(wxT("Print")); |
c7ce8392 VZ |
467 | if ( commands ) |
468 | commands->Add(cmd); | |
469 | ||
470 | count++; | |
471 | } | |
472 | ||
473 | return count; | |
d98a58c5 | 474 | #endif // __WINDOWS__/| __UNIX__ |
c7ce8392 VZ |
475 | } |
476 | ||
a6c65e88 | 477 | bool wxFileType::Unassociate() |
c7ce8392 | 478 | { |
d98a58c5 | 479 | #if defined(__WINDOWS__) |
a6c65e88 | 480 | return m_impl->Unassociate(); |
6691d737 | 481 | #elif defined(__UNIX__) |
2b813b73 | 482 | return m_impl->Unassociate(this); |
2900bd1c | 483 | #else |
9a83f860 | 484 | wxFAIL_MSG( wxT("not implemented") ); // TODO |
4e32eea1 | 485 | return false; |
2900bd1c | 486 | #endif |
2b813b73 VZ |
487 | } |
488 | ||
7a893a31 WS |
489 | bool wxFileType::SetCommand(const wxString& cmd, |
490 | const wxString& verb, | |
491 | bool overwriteprompt) | |
2b813b73 | 492 | { |
d98a58c5 | 493 | #if defined (__WINDOWS__) || defined(__UNIX__) |
2b813b73 VZ |
494 | return m_impl->SetCommand(cmd, verb, overwriteprompt); |
495 | #else | |
7a893a31 WS |
496 | wxUnusedVar(cmd); |
497 | wxUnusedVar(verb); | |
498 | wxUnusedVar(overwriteprompt); | |
9a83f860 | 499 | wxFAIL_MSG(wxT("not implemented")); |
4e32eea1 | 500 | return false; |
c7ce8392 VZ |
501 | #endif |
502 | } | |
503 | ||
2b813b73 VZ |
504 | bool wxFileType::SetDefaultIcon(const wxString& cmd, int index) |
505 | { | |
506 | wxString sTmp = cmd; | |
d98a58c5 | 507 | #ifdef __WINDOWS__ |
2b813b73 VZ |
508 | // VZ: should we do this? |
509 | // chris elliott : only makes sense in MS windows | |
510 | if ( sTmp.empty() ) | |
525d8583 | 511 | GetOpenCommand(&sTmp, wxFileType::MessageParameters(wxEmptyString, wxEmptyString)); |
2b813b73 | 512 | #endif |
9a83f860 | 513 | wxCHECK_MSG( !sTmp.empty(), false, wxT("need the icon file") ); |
2b813b73 | 514 | |
d98a58c5 | 515 | #if defined (__WINDOWS__) || defined(__UNIX__) |
2b813b73 VZ |
516 | return m_impl->SetDefaultIcon (cmd, index); |
517 | #else | |
7a893a31 | 518 | wxUnusedVar(index); |
9a83f860 | 519 | wxFAIL_MSG(wxT("not implemented")); |
4e32eea1 | 520 | return false; |
2b813b73 VZ |
521 | #endif |
522 | } | |
523 | ||
a893b65e | 524 | // ---------------------------------------------------------------------------- |
2b850ae1 | 525 | // wxMimeTypesManagerFactory |
a893b65e | 526 | // ---------------------------------------------------------------------------- |
2b850ae1 RR |
527 | |
528 | wxMimeTypesManagerFactory *wxMimeTypesManagerFactory::m_factory = NULL; | |
529 | ||
ad9835c9 | 530 | /* static */ |
a893b65e | 531 | void wxMimeTypesManagerFactory::Set(wxMimeTypesManagerFactory *factory) |
2b850ae1 | 532 | { |
a893b65e | 533 | delete m_factory; |
2b850ae1 | 534 | |
a893b65e | 535 | m_factory = factory; |
2b850ae1 RR |
536 | } |
537 | ||
ad9835c9 | 538 | /* static */ |
a893b65e | 539 | wxMimeTypesManagerFactory *wxMimeTypesManagerFactory::Get() |
2b850ae1 | 540 | { |
a893b65e VZ |
541 | if ( !m_factory ) |
542 | m_factory = new wxMimeTypesManagerFactory; | |
2b850ae1 | 543 | |
a893b65e | 544 | return m_factory; |
2b850ae1 RR |
545 | } |
546 | ||
547 | wxMimeTypesManagerImpl *wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl() | |
548 | { | |
549 | return new wxMimeTypesManagerImpl; | |
550 | } | |
2b813b73 | 551 | |
7dc3cc31 VS |
552 | // ---------------------------------------------------------------------------- |
553 | // wxMimeTypesManager | |
554 | // ---------------------------------------------------------------------------- | |
555 | ||
556 | void wxMimeTypesManager::EnsureImpl() | |
557 | { | |
c7ce8392 | 558 | if ( !m_impl ) |
a893b65e | 559 | m_impl = wxMimeTypesManagerFactory::Get()->CreateMimeTypesManagerImpl(); |
7dc3cc31 VS |
560 | } |
561 | ||
562 | bool wxMimeTypesManager::IsOfType(const wxString& mimeType, | |
563 | const wxString& wildcard) | |
564 | { | |
565 | wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND, | |
566 | wxT("first MIME type can't contain wildcards") ); | |
567 | ||
4e32eea1 | 568 | // all comparaisons are case insensitive (2nd arg of IsSameAs() is false) |
a6c65e88 | 569 | if ( wildcard.BeforeFirst(wxT('/')). |
4e32eea1 | 570 | IsSameAs(mimeType.BeforeFirst(wxT('/')), false) ) |
7dc3cc31 VS |
571 | { |
572 | wxString strSubtype = wildcard.AfterFirst(wxT('/')); | |
573 | ||
574 | if ( strSubtype == wxT("*") || | |
4e32eea1 | 575 | strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), false) ) |
7dc3cc31 VS |
576 | { |
577 | // matches (either exactly or it's a wildcard) | |
4e32eea1 | 578 | return true; |
7dc3cc31 VS |
579 | } |
580 | } | |
581 | ||
4e32eea1 | 582 | return false; |
7dc3cc31 VS |
583 | } |
584 | ||
585 | wxMimeTypesManager::wxMimeTypesManager() | |
586 | { | |
587 | m_impl = NULL; | |
588 | } | |
589 | ||
590 | wxMimeTypesManager::~wxMimeTypesManager() | |
591 | { | |
8a16a98e GT |
592 | if ( m_impl ) |
593 | delete m_impl; | |
7dc3cc31 VS |
594 | } |
595 | ||
2b813b73 VZ |
596 | bool wxMimeTypesManager::Unassociate(wxFileType *ft) |
597 | { | |
9413e1e3 VZ |
598 | EnsureImpl(); |
599 | ||
6691d737 | 600 | #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__) |
2b813b73 VZ |
601 | return m_impl->Unassociate(ft); |
602 | #else | |
603 | return ft->Unassociate(); | |
604 | #endif | |
605 | } | |
606 | ||
607 | ||
7dc3cc31 | 608 | wxFileType * |
a6c65e88 | 609 | wxMimeTypesManager::Associate(const wxFileTypeInfo& ftInfo) |
7dc3cc31 VS |
610 | { |
611 | EnsureImpl(); | |
a6c65e88 | 612 | |
d98a58c5 | 613 | #if defined(__WINDOWS__) || defined(__UNIX__) |
a6c65e88 VZ |
614 | return m_impl->Associate(ftInfo); |
615 | #else // other platforms | |
7a893a31 | 616 | wxUnusedVar(ftInfo); |
9a83f860 | 617 | wxFAIL_MSG( wxT("not implemented") ); // TODO |
a6c65e88 VZ |
618 | return NULL; |
619 | #endif // platforms | |
7dc3cc31 VS |
620 | } |
621 | ||
c7ce8392 | 622 | wxFileType * |
a6c65e88 | 623 | wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext) |
c7ce8392 VZ |
624 | { |
625 | EnsureImpl(); | |
e16916ea VZ |
626 | |
627 | wxString::const_iterator i = ext.begin(); | |
628 | const wxString::const_iterator end = ext.end(); | |
629 | wxString extWithoutDot; | |
630 | if ( i != end && *i == '.' ) | |
631 | extWithoutDot.assign(++i, ext.end()); | |
632 | else | |
633 | extWithoutDot = ext; | |
634 | ||
9a83f860 | 635 | wxCHECK_MSG( !ext.empty(), NULL, wxT("extension can't be empty") ); |
e16916ea VZ |
636 | |
637 | wxFileType *ft = m_impl->GetFileTypeFromExtension(extWithoutDot); | |
a6c65e88 VZ |
638 | |
639 | if ( !ft ) { | |
640 | // check the fallbacks | |
641 | // | |
642 | // TODO linear search is potentially slow, perhaps we should use a | |
643 | // sorted array? | |
644 | size_t count = m_fallbacks.GetCount(); | |
645 | for ( size_t n = 0; n < count; n++ ) { | |
646 | if ( m_fallbacks[n].GetExtensions().Index(ext) != wxNOT_FOUND ) { | |
647 | ft = new wxFileType(m_fallbacks[n]); | |
648 | ||
649 | break; | |
650 | } | |
651 | } | |
652 | } | |
c7ce8392 | 653 | |
a6c65e88 | 654 | return ft; |
c7ce8392 VZ |
655 | } |
656 | ||
7dc3cc31 VS |
657 | wxFileType * |
658 | wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType) | |
659 | { | |
660 | EnsureImpl(); | |
a6c65e88 VZ |
661 | wxFileType *ft = m_impl->GetFileTypeFromMimeType(mimeType); |
662 | ||
f627fbee | 663 | if ( !ft ) { |
a6c65e88 VZ |
664 | // check the fallbacks |
665 | // | |
f627fbee VZ |
666 | // TODO linear search is potentially slow, perhaps we should use a |
667 | // sorted array? | |
a6c65e88 VZ |
668 | size_t count = m_fallbacks.GetCount(); |
669 | for ( size_t n = 0; n < count; n++ ) { | |
670 | if ( wxMimeTypesManager::IsOfType(mimeType, | |
671 | m_fallbacks[n].GetMimeType()) ) { | |
672 | ft = new wxFileType(m_fallbacks[n]); | |
673 | ||
674 | break; | |
675 | } | |
676 | } | |
677 | } | |
678 | ||
679 | return ft; | |
7dc3cc31 VS |
680 | } |
681 | ||
7dc3cc31 VS |
682 | void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes) |
683 | { | |
684 | EnsureImpl(); | |
a6c65e88 VZ |
685 | for ( const wxFileTypeInfo *ft = filetypes; ft && ft->IsValid(); ft++ ) { |
686 | AddFallback(*ft); | |
7dc3cc31 VS |
687 | } |
688 | } | |
689 | ||
690 | size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes) | |
691 | { | |
692 | EnsureImpl(); | |
a6c65e88 VZ |
693 | size_t countAll = m_impl->EnumAllFileTypes(mimetypes); |
694 | ||
695 | // add the fallback filetypes | |
696 | size_t count = m_fallbacks.GetCount(); | |
697 | for ( size_t n = 0; n < count; n++ ) { | |
698 | if ( mimetypes.Index(m_fallbacks[n].GetMimeType()) == wxNOT_FOUND ) { | |
699 | mimetypes.Add(m_fallbacks[n].GetMimeType()); | |
700 | countAll++; | |
701 | } | |
702 | } | |
7dc3cc31 | 703 | |
a6c65e88 VZ |
704 | return countAll; |
705 | } | |
7dc3cc31 | 706 | |
2b813b73 VZ |
707 | void wxMimeTypesManager::Initialize(int mcapStyle, |
708 | const wxString& sExtraDir) | |
709 | { | |
6691d737 | 710 | #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__) |
2b813b73 VZ |
711 | EnsureImpl(); |
712 | ||
713 | m_impl->Initialize(mcapStyle, sExtraDir); | |
33ac7e6f | 714 | #else |
4e32eea1 WS |
715 | (void)mcapStyle; |
716 | (void)sExtraDir; | |
2b813b73 VZ |
717 | #endif // Unix |
718 | } | |
719 | ||
720 | // and this function clears all the data from the manager | |
721 | void wxMimeTypesManager::ClearData() | |
722 | { | |
6691d737 | 723 | #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__) |
2b813b73 VZ |
724 | EnsureImpl(); |
725 | ||
726 | m_impl->ClearData(); | |
727 | #endif // Unix | |
728 | } | |
729 | ||
7dc3cc31 | 730 | // ---------------------------------------------------------------------------- |
a6c65e88 | 731 | // global data and wxMimeTypeCmnModule |
7dc3cc31 VS |
732 | // ---------------------------------------------------------------------------- |
733 | ||
734 | // private object | |
735 | static wxMimeTypesManager gs_mimeTypesManager; | |
736 | ||
737 | // and public pointer | |
a6c65e88 | 738 | wxMimeTypesManager *wxTheMimeTypesManager = &gs_mimeTypesManager; |
7dc3cc31 | 739 | |
66806a0b VS |
740 | class wxMimeTypeCmnModule: public wxModule |
741 | { | |
66806a0b | 742 | public: |
c7ce8392 | 743 | wxMimeTypeCmnModule() : wxModule() { } |
a893b65e | 744 | |
4e32eea1 | 745 | virtual bool OnInit() { return true; } |
c7ce8392 VZ |
746 | virtual void OnExit() |
747 | { | |
a893b65e VZ |
748 | wxMimeTypesManagerFactory::Set(NULL); |
749 | ||
c7ce8392 VZ |
750 | if ( gs_mimeTypesManager.m_impl != NULL ) |
751 | { | |
5276b0a5 | 752 | wxDELETE(gs_mimeTypesManager.m_impl); |
65e50848 | 753 | gs_mimeTypesManager.m_fallbacks.Clear(); |
c7ce8392 | 754 | } |
66806a0b | 755 | } |
c7ce8392 VZ |
756 | |
757 | DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule) | |
66806a0b VS |
758 | }; |
759 | ||
760 | IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule, wxModule) | |
1e6feb95 VZ |
761 | |
762 | #endif // wxUSE_MIMETYPE |