1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/mimecmn.cpp
3 // Purpose: classes and functions to manage MIME types
4 // Author: Vadim Zeitlin
6 // Chris Elliott (biol75@york.ac.uk) 5 Dec 00: write support for Win32
9 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
10 // Licence: wxWindows licence (part of wxExtra library)
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
21 // for compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
30 #include "wx/mimetype.h"
33 #include "wx/dynarray.h"
34 #include "wx/string.h"
37 #include "wx/module.h"
41 #include "wx/iconloc.h"
42 #include "wx/confbase.h"
44 // other standard headers
47 // implementation classes:
48 #if defined(__WXMSW__)
49 #include "wx/msw/mimetype.h"
50 #elif defined(__WXMAC__)
51 #include "wx/mac/mimetype.h"
52 #elif defined(__WXPM__) || defined (__EMX__)
53 #include "wx/os2/mimetype.h"
55 #elif defined(__DOS__)
56 #include "wx/msdos/mimetype.h"
58 #include "wx/unix/mimetype.h"
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
70 wxMimeTypeCommands::AddOrReplaceVerb(const wxString
& verb
, const wxString
& cmd
)
72 int n
= m_verbs
.Index(verb
, false /* ignore case */);
73 if ( n
== wxNOT_FOUND
)
85 wxMimeTypeCommands::GetCommandForVerb(const wxString
& verb
, size_t *idx
) const
89 int n
= m_verbs
.Index(verb
);
90 if ( n
!= wxNOT_FOUND
)
92 s
= m_commands
[(size_t)n
];
98 // different from any valid index
105 wxString
wxMimeTypeCommands::GetVerbCmd(size_t n
) const
107 return m_verbs
[n
] + wxT('=') + m_commands
[n
];
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 void wxFileTypeInfo::DoVarArgInit(const wxString
& mimeType
,
115 const wxString
& openCmd
,
116 const wxString
& printCmd
,
117 const wxString
& desc
,
120 m_mimeType
= mimeType
;
122 m_printCmd
= printCmd
;
127 // icc gives this warning in its own va_arg() macro, argh
129 #pragma warning(push)
130 #pragma warning(disable: 1684)
133 wxArgNormalizedString
ext(WX_VA_ARG_STRING(argptr
));
140 // NULL terminates the list
144 m_exts
.Add(ext
.GetString());
148 // NB: DoVarArgInit uses WX_VA_ARG_STRING macro to extract the string and this
149 // macro interprets the argument as char* or wchar_t* depending on build
150 // (and in UTF8 build, on the current locale). Because only one of the
151 // vararg forms below is called and the decision about which one gets
152 // called depends on the same conditions WX_VA_ARG_STRING uses, we can
153 // implement both of them in the exact same way:
155 #if !wxUSE_UTF8_LOCALE_ONLY
156 void wxFileTypeInfo::VarArgInitWchar(const wxChar
*mimeType
,
157 const wxChar
*openCmd
,
158 const wxChar
*printCmd
,
163 va_start(argptr
, desc
);
165 DoVarArgInit(mimeType
, openCmd
, printCmd
, desc
, argptr
);
169 #endif // !wxUSE_UTF8_LOCALE_ONLY
171 #if wxUSE_UNICODE_UTF8
172 void wxFileTypeInfo::VarArgInitUtf8(const char *mimeType
,
174 const char *printCmd
,
179 va_start(argptr
, desc
);
181 DoVarArgInit(mimeType
, openCmd
, printCmd
, desc
, argptr
);
185 #endif // wxUSE_UNICODE_UTF8
188 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
190 m_mimeType
= sArray
[0u];
191 m_openCmd
= sArray
[1u];
192 m_printCmd
= sArray
[2u];
193 m_desc
= sArray
[3u];
195 size_t count
= sArray
.GetCount();
196 for ( size_t i
= 4; i
< count
; i
++ )
198 m_exts
.Add(sArray
[i
]);
202 #include "wx/arrimpl.cpp"
203 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
)
205 // ============================================================================
206 // implementation of the wrapper classes
207 // ============================================================================
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 wxString
wxFileType::ExpandCommand(const wxString
& command
,
215 const wxFileType::MessageParameters
& params
)
217 bool hasFilename
= false;
220 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
221 if ( *pc
== wxT('%') ) {
224 // '%s' expands into file name (quoted because it might
225 // contain spaces) - except if there are already quotes
226 // there because otherwise some programs may get confused
227 // by double double quotes
229 if ( *(pc
- 2) == wxT('"') )
230 str
<< params
.GetFileName();
232 str
<< wxT('"') << params
.GetFileName() << wxT('"');
234 str
<< params
.GetFileName();
239 // '%t' expands into MIME type (quote it too just to be
241 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
246 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
247 if ( pEnd
== NULL
) {
249 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
250 params
.GetMimeType().c_str());
254 wxString
param(pc
+ 1, pEnd
- pc
- 1);
255 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
263 // TODO %n is the number of parts, %F is an array containing
264 // the names of temp files these parts were written to
265 // and their mime types.
269 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
270 *pc
, command
.c_str());
279 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
280 // the program will accept the data on stdin so normally we should append
281 // "< %s" to the end of the command in such case, but not all commands
282 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
283 // and appending "< %s" to this command makes the test fail... I don't
284 // know of the correct solution, try to guess what we have to do.
286 // test now carried out on reading file so test should never get here
287 if ( !hasFilename
&& !str
.empty()
289 && !str
.StartsWith(_T("test "))
292 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
298 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
304 wxFileType::wxFileType()
307 m_impl
= new wxFileTypeImpl
;
310 wxFileType::~wxFileType()
316 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
320 extensions
= m_info
->GetExtensions();
324 return m_impl
->GetExtensions(extensions
);
327 bool wxFileType::GetMimeType(wxString
*mimeType
) const
329 wxCHECK_MSG( mimeType
, false, _T("invalid parameter in GetMimeType") );
333 *mimeType
= m_info
->GetMimeType();
338 return m_impl
->GetMimeType(mimeType
);
341 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
346 mimeTypes
.Add(m_info
->GetMimeType());
351 return m_impl
->GetMimeTypes(mimeTypes
);
354 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
360 iconLoc
->SetFileName(m_info
->GetIconFile());
362 iconLoc
->SetIndex(m_info
->GetIconIndex());
369 return m_impl
->GetIcon(iconLoc
);
373 wxFileType::GetIcon(wxIconLocation
*iconloc
,
374 const MessageParameters
& params
) const
376 if ( !GetIcon(iconloc
) )
381 // we may have "%s" in the icon location string, at least under Windows, so
385 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
391 bool wxFileType::GetDescription(wxString
*desc
) const
393 wxCHECK_MSG( desc
, false, _T("invalid parameter in GetDescription") );
397 *desc
= m_info
->GetDescription();
402 return m_impl
->GetDescription(desc
);
406 wxFileType::GetOpenCommand(wxString
*openCmd
,
407 const wxFileType::MessageParameters
& params
) const
409 wxCHECK_MSG( openCmd
, false, _T("invalid parameter in GetOpenCommand") );
413 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
418 return m_impl
->GetOpenCommand(openCmd
, params
);
421 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
424 if ( !GetOpenCommand(&cmd
, filename
) )
426 // return empty string to indicate an error
434 wxFileType::GetPrintCommand(wxString
*printCmd
,
435 const wxFileType::MessageParameters
& params
) const
437 wxCHECK_MSG( printCmd
, false, _T("invalid parameter in GetPrintCommand") );
441 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
446 return m_impl
->GetPrintCommand(printCmd
, params
);
450 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
451 wxArrayString
*commands
,
452 const wxFileType::MessageParameters
& params
) const
459 #if defined (__WXMSW__) || defined(__UNIX__)
460 return m_impl
->GetAllCommands(verbs
, commands
, params
);
461 #else // !__WXMSW__ || Unix
462 // we don't know how to retrieve all commands, so just try the 2 we know
466 if ( GetOpenCommand(&cmd
, params
) )
469 verbs
->Add(_T("Open"));
475 if ( GetPrintCommand(&cmd
, params
) )
478 verbs
->Add(_T("Print"));
486 #endif // __WXMSW__/| __UNIX__
489 bool wxFileType::Unassociate()
491 #if defined(__WXMSW__)
492 return m_impl
->Unassociate();
493 #elif defined(__UNIX__)
494 return m_impl
->Unassociate(this);
496 wxFAIL_MSG( _T("not implemented") ); // TODO
501 bool wxFileType::SetCommand(const wxString
& cmd
,
502 const wxString
& verb
,
503 bool overwriteprompt
)
505 #if defined (__WXMSW__) || defined(__UNIX__)
506 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
510 wxUnusedVar(overwriteprompt
);
511 wxFAIL_MSG(_T("not implemented"));
516 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
520 // VZ: should we do this?
521 // chris elliott : only makes sense in MS windows
523 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxEmptyString
, wxEmptyString
));
525 wxCHECK_MSG( !sTmp
.empty(), false, _T("need the icon file") );
527 #if defined (__WXMSW__) || defined(__UNIX__)
528 return m_impl
->SetDefaultIcon (cmd
, index
);
531 wxFAIL_MSG(_T("not implemented"));
536 // ----------------------------------------------------------------------------
537 // wxMimeTypesManagerFactory
538 // ----------------------------------------------------------------------------
540 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::m_factory
= NULL
;
543 void wxMimeTypesManagerFactory::Set(wxMimeTypesManagerFactory
*factory
)
551 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::Get()
554 m_factory
= new wxMimeTypesManagerFactory
;
559 wxMimeTypesManagerImpl
*wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl()
561 return new wxMimeTypesManagerImpl
;
564 // ----------------------------------------------------------------------------
565 // wxMimeTypesManager
566 // ----------------------------------------------------------------------------
568 void wxMimeTypesManager::EnsureImpl()
571 m_impl
= wxMimeTypesManagerFactory::Get()->CreateMimeTypesManagerImpl();
574 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
575 const wxString
& wildcard
)
577 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
578 wxT("first MIME type can't contain wildcards") );
580 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
581 if ( wildcard
.BeforeFirst(wxT('/')).
582 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
584 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
586 if ( strSubtype
== wxT("*") ||
587 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
589 // matches (either exactly or it's a wildcard)
597 wxMimeTypesManager::wxMimeTypesManager()
602 wxMimeTypesManager::~wxMimeTypesManager()
608 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
612 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
613 return m_impl
->Unassociate(ft
);
615 return ft
->Unassociate();
621 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
625 #if defined(__WXMSW__) || defined(__UNIX__)
626 return m_impl
->Associate(ftInfo
);
627 #else // other platforms
629 wxFAIL_MSG( _T("not implemented") ); // TODO
635 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
638 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
641 // check the fallbacks
643 // TODO linear search is potentially slow, perhaps we should use a
645 size_t count
= m_fallbacks
.GetCount();
646 for ( size_t n
= 0; n
< count
; n
++ ) {
647 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
648 ft
= new wxFileType(m_fallbacks
[n
]);
659 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
662 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
665 // check the fallbacks
667 // TODO linear search is potentially slow, perhaps we should use a
669 size_t count
= m_fallbacks
.GetCount();
670 for ( size_t n
= 0; n
< count
; n
++ ) {
671 if ( wxMimeTypesManager::IsOfType(mimeType
,
672 m_fallbacks
[n
].GetMimeType()) ) {
673 ft
= new wxFileType(m_fallbacks
[n
]);
683 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
686 return m_impl
->ReadMailcap(filename
, fallback
);
689 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
692 return m_impl
->ReadMimeTypes(filename
);
695 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
698 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
703 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
706 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
708 // add the fallback filetypes
709 size_t count
= m_fallbacks
.GetCount();
710 for ( size_t n
= 0; n
< count
; n
++ ) {
711 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
712 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
720 void wxMimeTypesManager::Initialize(int mcapStyle
,
721 const wxString
& sExtraDir
)
723 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
726 m_impl
->Initialize(mcapStyle
, sExtraDir
);
733 // and this function clears all the data from the manager
734 void wxMimeTypesManager::ClearData()
736 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
743 // ----------------------------------------------------------------------------
744 // global data and wxMimeTypeCmnModule
745 // ----------------------------------------------------------------------------
748 static wxMimeTypesManager gs_mimeTypesManager
;
750 // and public pointer
751 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
753 class wxMimeTypeCmnModule
: public wxModule
756 wxMimeTypeCmnModule() : wxModule() { }
758 virtual bool OnInit() { return true; }
759 virtual void OnExit()
761 wxMimeTypesManagerFactory::Set(NULL
);
763 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
765 delete gs_mimeTypesManager
.m_impl
;
766 gs_mimeTypesManager
.m_impl
= NULL
;
767 gs_mimeTypesManager
.m_fallbacks
.Clear();
771 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
774 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
776 #endif // wxUSE_MIMETYPE