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::VarArgInit(const wxString
& mimeType
,
115 const wxString
& openCmd
,
116 const wxString
& printCmd
,
117 const wxString
& desc
,
121 va_start(argptr
, desc
);
123 m_mimeType
= mimeType
;
125 m_printCmd
= printCmd
;
130 // icc gives this warning in its own va_arg() macro, argh
132 #pragma warning(push)
133 #pragma warning(disable: 1684)
136 wxArgNormalizedString
ext(WX_VA_ARG_STRING(argptr
));
143 // NULL terminates the list
147 m_exts
.Add(ext
.GetString());
154 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
156 m_mimeType
= sArray
[0u];
157 m_openCmd
= sArray
[1u];
158 m_printCmd
= sArray
[2u];
159 m_desc
= sArray
[3u];
161 size_t count
= sArray
.GetCount();
162 for ( size_t i
= 4; i
< count
; i
++ )
164 m_exts
.Add(sArray
[i
]);
168 #include "wx/arrimpl.cpp"
169 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
)
171 // ============================================================================
172 // implementation of the wrapper classes
173 // ============================================================================
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
180 wxString
wxFileType::ExpandCommand(const wxString
& command
,
181 const wxFileType::MessageParameters
& params
)
183 bool hasFilename
= false;
186 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
187 if ( *pc
== wxT('%') ) {
190 // '%s' expands into file name (quoted because it might
191 // contain spaces) - except if there are already quotes
192 // there because otherwise some programs may get confused
193 // by double double quotes
195 if ( *(pc
- 2) == wxT('"') )
196 str
<< params
.GetFileName();
198 str
<< wxT('"') << params
.GetFileName() << wxT('"');
200 str
<< params
.GetFileName();
205 // '%t' expands into MIME type (quote it too just to be
207 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
212 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
213 if ( pEnd
== NULL
) {
215 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
216 params
.GetMimeType().c_str());
220 wxString
param(pc
+ 1, pEnd
- pc
- 1);
221 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
229 // TODO %n is the number of parts, %F is an array containing
230 // the names of temp files these parts were written to
231 // and their mime types.
235 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
236 *pc
, command
.c_str());
245 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
246 // the program will accept the data on stdin so normally we should append
247 // "< %s" to the end of the command in such case, but not all commands
248 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
249 // and appending "< %s" to this command makes the test fail... I don't
250 // know of the correct solution, try to guess what we have to do.
252 // test now carried out on reading file so test should never get here
253 if ( !hasFilename
&& !str
.empty()
255 && !str
.StartsWith(_T("test "))
258 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
264 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
270 wxFileType::wxFileType()
273 m_impl
= new wxFileTypeImpl
;
276 wxFileType::~wxFileType()
282 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
286 extensions
= m_info
->GetExtensions();
290 return m_impl
->GetExtensions(extensions
);
293 bool wxFileType::GetMimeType(wxString
*mimeType
) const
295 wxCHECK_MSG( mimeType
, false, _T("invalid parameter in GetMimeType") );
299 *mimeType
= m_info
->GetMimeType();
304 return m_impl
->GetMimeType(mimeType
);
307 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
312 mimeTypes
.Add(m_info
->GetMimeType());
317 return m_impl
->GetMimeTypes(mimeTypes
);
320 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
326 iconLoc
->SetFileName(m_info
->GetIconFile());
328 iconLoc
->SetIndex(m_info
->GetIconIndex());
335 return m_impl
->GetIcon(iconLoc
);
339 wxFileType::GetIcon(wxIconLocation
*iconloc
,
340 const MessageParameters
& params
) const
342 if ( !GetIcon(iconloc
) )
347 // we may have "%s" in the icon location string, at least under Windows, so
351 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
357 bool wxFileType::GetDescription(wxString
*desc
) const
359 wxCHECK_MSG( desc
, false, _T("invalid parameter in GetDescription") );
363 *desc
= m_info
->GetDescription();
368 return m_impl
->GetDescription(desc
);
372 wxFileType::GetOpenCommand(wxString
*openCmd
,
373 const wxFileType::MessageParameters
& params
) const
375 wxCHECK_MSG( openCmd
, false, _T("invalid parameter in GetOpenCommand") );
379 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
384 return m_impl
->GetOpenCommand(openCmd
, params
);
387 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
390 if ( !GetOpenCommand(&cmd
, filename
) )
392 // return empty string to indicate an error
400 wxFileType::GetPrintCommand(wxString
*printCmd
,
401 const wxFileType::MessageParameters
& params
) const
403 wxCHECK_MSG( printCmd
, false, _T("invalid parameter in GetPrintCommand") );
407 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
412 return m_impl
->GetPrintCommand(printCmd
, params
);
416 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
417 wxArrayString
*commands
,
418 const wxFileType::MessageParameters
& params
) const
425 #if defined (__WXMSW__) || defined(__UNIX__)
426 return m_impl
->GetAllCommands(verbs
, commands
, params
);
427 #else // !__WXMSW__ || Unix
428 // we don't know how to retrieve all commands, so just try the 2 we know
432 if ( GetOpenCommand(&cmd
, params
) )
435 verbs
->Add(_T("Open"));
441 if ( GetPrintCommand(&cmd
, params
) )
444 verbs
->Add(_T("Print"));
452 #endif // __WXMSW__/| __UNIX__
455 bool wxFileType::Unassociate()
457 #if defined(__WXMSW__)
458 return m_impl
->Unassociate();
459 #elif defined(__UNIX__)
460 return m_impl
->Unassociate(this);
462 wxFAIL_MSG( _T("not implemented") ); // TODO
467 bool wxFileType::SetCommand(const wxString
& cmd
,
468 const wxString
& verb
,
469 bool overwriteprompt
)
471 #if defined (__WXMSW__) || defined(__UNIX__)
472 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
476 wxUnusedVar(overwriteprompt
);
477 wxFAIL_MSG(_T("not implemented"));
482 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
486 // VZ: should we do this?
487 // chris elliott : only makes sense in MS windows
489 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxEmptyString
, wxEmptyString
));
491 wxCHECK_MSG( !sTmp
.empty(), false, _T("need the icon file") );
493 #if defined (__WXMSW__) || defined(__UNIX__)
494 return m_impl
->SetDefaultIcon (cmd
, index
);
497 wxFAIL_MSG(_T("not implemented"));
502 // ----------------------------------------------------------------------------
503 // wxMimeTypesManagerFactory
504 // ----------------------------------------------------------------------------
506 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::m_factory
= NULL
;
509 void wxMimeTypesManagerFactory::Set(wxMimeTypesManagerFactory
*factory
)
517 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::Get()
520 m_factory
= new wxMimeTypesManagerFactory
;
525 wxMimeTypesManagerImpl
*wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl()
527 return new wxMimeTypesManagerImpl
;
530 // ----------------------------------------------------------------------------
531 // wxMimeTypesManager
532 // ----------------------------------------------------------------------------
534 void wxMimeTypesManager::EnsureImpl()
537 m_impl
= wxMimeTypesManagerFactory::Get()->CreateMimeTypesManagerImpl();
540 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
541 const wxString
& wildcard
)
543 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
544 wxT("first MIME type can't contain wildcards") );
546 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
547 if ( wildcard
.BeforeFirst(wxT('/')).
548 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
550 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
552 if ( strSubtype
== wxT("*") ||
553 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
555 // matches (either exactly or it's a wildcard)
563 wxMimeTypesManager::wxMimeTypesManager()
568 wxMimeTypesManager::~wxMimeTypesManager()
574 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
578 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
579 return m_impl
->Unassociate(ft
);
581 return ft
->Unassociate();
587 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
591 #if defined(__WXMSW__) || defined(__UNIX__)
592 return m_impl
->Associate(ftInfo
);
593 #else // other platforms
595 wxFAIL_MSG( _T("not implemented") ); // TODO
601 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
604 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
607 // check the fallbacks
609 // TODO linear search is potentially slow, perhaps we should use a
611 size_t count
= m_fallbacks
.GetCount();
612 for ( size_t n
= 0; n
< count
; n
++ ) {
613 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
614 ft
= new wxFileType(m_fallbacks
[n
]);
625 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
628 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
631 // check the fallbacks
633 // TODO linear search is potentially slow, perhaps we should use a
635 size_t count
= m_fallbacks
.GetCount();
636 for ( size_t n
= 0; n
< count
; n
++ ) {
637 if ( wxMimeTypesManager::IsOfType(mimeType
,
638 m_fallbacks
[n
].GetMimeType()) ) {
639 ft
= new wxFileType(m_fallbacks
[n
]);
649 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
652 return m_impl
->ReadMailcap(filename
, fallback
);
655 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
658 return m_impl
->ReadMimeTypes(filename
);
661 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
664 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
669 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
672 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
674 // add the fallback filetypes
675 size_t count
= m_fallbacks
.GetCount();
676 for ( size_t n
= 0; n
< count
; n
++ ) {
677 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
678 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
686 void wxMimeTypesManager::Initialize(int mcapStyle
,
687 const wxString
& sExtraDir
)
689 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
692 m_impl
->Initialize(mcapStyle
, sExtraDir
);
699 // and this function clears all the data from the manager
700 void wxMimeTypesManager::ClearData()
702 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
709 // ----------------------------------------------------------------------------
710 // global data and wxMimeTypeCmnModule
711 // ----------------------------------------------------------------------------
714 static wxMimeTypesManager gs_mimeTypesManager
;
716 // and public pointer
717 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
719 class wxMimeTypeCmnModule
: public wxModule
722 wxMimeTypeCmnModule() : wxModule() { }
724 virtual bool OnInit() { return true; }
725 virtual void OnExit()
727 wxMimeTypesManagerFactory::Set(NULL
);
729 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
731 delete gs_mimeTypesManager
.m_impl
;
732 gs_mimeTypesManager
.m_impl
= NULL
;
733 gs_mimeTypesManager
.m_fallbacks
.Clear();
737 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
740 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
742 #endif // wxUSE_MIMETYPE