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