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"
42 #include "wx/iconloc.h"
43 #include "wx/confbase.h"
45 // other standard headers
48 // implementation classes:
49 #if defined(__WXMSW__)
50 #include "wx/msw/mimetype.h"
51 #elif defined(__WXMAC__)
52 #include "wx/osx/mimetype.h"
53 #elif defined(__WXPM__) || defined (__EMX__)
54 #include "wx/os2/mimetype.h"
56 #elif defined(__DOS__)
57 #include "wx/msdos/mimetype.h"
59 #include "wx/unix/mimetype.h"
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
71 wxMimeTypeCommands::AddOrReplaceVerb(const wxString
& verb
, const wxString
& cmd
)
73 int n
= m_verbs
.Index(verb
, false /* ignore case */);
74 if ( n
== wxNOT_FOUND
)
86 wxMimeTypeCommands::GetCommandForVerb(const wxString
& verb
, size_t *idx
) const
90 int n
= m_verbs
.Index(verb
);
91 if ( n
!= wxNOT_FOUND
)
93 s
= m_commands
[(size_t)n
];
99 // different from any valid index
106 wxString
wxMimeTypeCommands::GetVerbCmd(size_t n
) const
108 return m_verbs
[n
] + wxT('=') + m_commands
[n
];
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 void wxFileTypeInfo::DoVarArgInit(const wxString
& mimeType
,
116 const wxString
& openCmd
,
117 const wxString
& printCmd
,
118 const wxString
& desc
,
121 m_mimeType
= mimeType
;
123 m_printCmd
= printCmd
;
128 // icc gives this warning in its own va_arg() macro, argh
130 #pragma warning(push)
131 #pragma warning(disable: 1684)
134 wxArgNormalizedString
ext(WX_VA_ARG_STRING(argptr
));
141 // NULL terminates the list
145 m_exts
.Add(ext
.GetString());
149 void wxFileTypeInfo::VarArgInit(const wxString
*mimeType
,
150 const wxString
*openCmd
,
151 const wxString
*printCmd
,
152 const wxString
*desc
,
156 va_start(argptr
, desc
);
158 DoVarArgInit(*mimeType
, *openCmd
, *printCmd
, *desc
, argptr
);
164 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
166 m_mimeType
= sArray
[0u];
167 m_openCmd
= sArray
[1u];
168 m_printCmd
= sArray
[2u];
169 m_desc
= sArray
[3u];
171 size_t count
= sArray
.GetCount();
172 for ( size_t i
= 4; i
< count
; i
++ )
174 m_exts
.Add(sArray
[i
]);
178 #include "wx/arrimpl.cpp"
179 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
)
181 // ============================================================================
182 // implementation of the wrapper classes
183 // ============================================================================
185 // ----------------------------------------------------------------------------
187 // ----------------------------------------------------------------------------
190 wxString
wxFileType::ExpandCommand(const wxString
& command
,
191 const wxFileType::MessageParameters
& params
)
193 bool hasFilename
= false;
196 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
197 if ( *pc
== wxT('%') ) {
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
205 if ( *(pc
- 2) == wxT('"') )
206 str
<< params
.GetFileName();
208 str
<< wxT('"') << params
.GetFileName() << wxT('"');
210 str
<< params
.GetFileName();
215 // '%t' expands into MIME type (quote it too just to be
217 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
222 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
223 if ( pEnd
== NULL
) {
225 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
226 params
.GetMimeType().c_str());
230 wxString
param(pc
+ 1, pEnd
- pc
- 1);
231 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
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.
245 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
246 *pc
, command
.c_str());
255 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
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.
262 // test now carried out on reading file so test should never get here
263 if ( !hasFilename
&& !str
.empty()
265 && !str
.StartsWith(_T("test "))
268 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
274 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
280 wxFileType::wxFileType()
283 m_impl
= new wxFileTypeImpl
;
286 wxFileType::~wxFileType()
292 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
296 extensions
= m_info
->GetExtensions();
300 return m_impl
->GetExtensions(extensions
);
303 bool wxFileType::GetMimeType(wxString
*mimeType
) const
305 wxCHECK_MSG( mimeType
, false, _T("invalid parameter in GetMimeType") );
309 *mimeType
= m_info
->GetMimeType();
314 return m_impl
->GetMimeType(mimeType
);
317 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
322 mimeTypes
.Add(m_info
->GetMimeType());
327 return m_impl
->GetMimeTypes(mimeTypes
);
330 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
336 iconLoc
->SetFileName(m_info
->GetIconFile());
338 iconLoc
->SetIndex(m_info
->GetIconIndex());
345 return m_impl
->GetIcon(iconLoc
);
349 wxFileType::GetIcon(wxIconLocation
*iconloc
,
350 const MessageParameters
& params
) const
352 if ( !GetIcon(iconloc
) )
357 // we may have "%s" in the icon location string, at least under Windows, so
361 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
367 bool wxFileType::GetDescription(wxString
*desc
) const
369 wxCHECK_MSG( desc
, false, _T("invalid parameter in GetDescription") );
373 *desc
= m_info
->GetDescription();
378 return m_impl
->GetDescription(desc
);
382 wxFileType::GetOpenCommand(wxString
*openCmd
,
383 const wxFileType::MessageParameters
& params
) const
385 wxCHECK_MSG( openCmd
, false, _T("invalid parameter in GetOpenCommand") );
389 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
394 return m_impl
->GetOpenCommand(openCmd
, params
);
397 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
400 if ( !GetOpenCommand(&cmd
, filename
) )
402 // return empty string to indicate an error
410 wxFileType::GetPrintCommand(wxString
*printCmd
,
411 const wxFileType::MessageParameters
& params
) const
413 wxCHECK_MSG( printCmd
, false, _T("invalid parameter in GetPrintCommand") );
417 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
422 return m_impl
->GetPrintCommand(printCmd
, params
);
426 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
427 wxArrayString
*commands
,
428 const wxFileType::MessageParameters
& params
) const
435 #if defined (__WXMSW__) || defined(__UNIX__)
436 return m_impl
->GetAllCommands(verbs
, commands
, params
);
437 #else // !__WXMSW__ || Unix
438 // we don't know how to retrieve all commands, so just try the 2 we know
442 if ( GetOpenCommand(&cmd
, params
) )
445 verbs
->Add(_T("Open"));
451 if ( GetPrintCommand(&cmd
, params
) )
454 verbs
->Add(_T("Print"));
462 #endif // __WXMSW__/| __UNIX__
465 bool wxFileType::Unassociate()
467 #if defined(__WXMSW__)
468 return m_impl
->Unassociate();
469 #elif defined(__UNIX__)
470 return m_impl
->Unassociate(this);
472 wxFAIL_MSG( _T("not implemented") ); // TODO
477 bool wxFileType::SetCommand(const wxString
& cmd
,
478 const wxString
& verb
,
479 bool overwriteprompt
)
481 #if defined (__WXMSW__) || defined(__UNIX__)
482 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
486 wxUnusedVar(overwriteprompt
);
487 wxFAIL_MSG(_T("not implemented"));
492 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
496 // VZ: should we do this?
497 // chris elliott : only makes sense in MS windows
499 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxEmptyString
, wxEmptyString
));
501 wxCHECK_MSG( !sTmp
.empty(), false, _T("need the icon file") );
503 #if defined (__WXMSW__) || defined(__UNIX__)
504 return m_impl
->SetDefaultIcon (cmd
, index
);
507 wxFAIL_MSG(_T("not implemented"));
512 // ----------------------------------------------------------------------------
513 // wxMimeTypesManagerFactory
514 // ----------------------------------------------------------------------------
516 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::m_factory
= NULL
;
519 void wxMimeTypesManagerFactory::Set(wxMimeTypesManagerFactory
*factory
)
527 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::Get()
530 m_factory
= new wxMimeTypesManagerFactory
;
535 wxMimeTypesManagerImpl
*wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl()
537 return new wxMimeTypesManagerImpl
;
540 // ----------------------------------------------------------------------------
541 // wxMimeTypesManager
542 // ----------------------------------------------------------------------------
544 void wxMimeTypesManager::EnsureImpl()
547 m_impl
= wxMimeTypesManagerFactory::Get()->CreateMimeTypesManagerImpl();
550 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
551 const wxString
& wildcard
)
553 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
554 wxT("first MIME type can't contain wildcards") );
556 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
557 if ( wildcard
.BeforeFirst(wxT('/')).
558 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
560 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
562 if ( strSubtype
== wxT("*") ||
563 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
565 // matches (either exactly or it's a wildcard)
573 wxMimeTypesManager::wxMimeTypesManager()
578 wxMimeTypesManager::~wxMimeTypesManager()
584 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
588 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
589 return m_impl
->Unassociate(ft
);
591 return ft
->Unassociate();
597 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
601 #if defined(__WXMSW__) || defined(__UNIX__)
602 return m_impl
->Associate(ftInfo
);
603 #else // other platforms
605 wxFAIL_MSG( _T("not implemented") ); // TODO
611 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
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());
623 wxCHECK_MSG( !ext
.empty(), NULL
, _T("extension can't be empty") );
625 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(extWithoutDot
);
628 // check the fallbacks
630 // TODO linear search is potentially slow, perhaps we should use a
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
]);
646 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
649 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
652 // check the fallbacks
654 // TODO linear search is potentially slow, perhaps we should use a
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
]);
670 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
673 return m_impl
->ReadMailcap(filename
, fallback
);
676 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
679 return m_impl
->ReadMimeTypes(filename
);
682 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
685 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
690 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
693 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
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());
707 void wxMimeTypesManager::Initialize(int mcapStyle
,
708 const wxString
& sExtraDir
)
710 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
713 m_impl
->Initialize(mcapStyle
, sExtraDir
);
720 // and this function clears all the data from the manager
721 void wxMimeTypesManager::ClearData()
723 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
730 // ----------------------------------------------------------------------------
731 // global data and wxMimeTypeCmnModule
732 // ----------------------------------------------------------------------------
735 static wxMimeTypesManager gs_mimeTypesManager
;
737 // and public pointer
738 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
740 class wxMimeTypeCmnModule
: public wxModule
743 wxMimeTypeCmnModule() : wxModule() { }
745 virtual bool OnInit() { return true; }
746 virtual void OnExit()
748 wxMimeTypesManagerFactory::Set(NULL
);
750 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
752 delete gs_mimeTypesManager
.m_impl
;
753 gs_mimeTypesManager
.m_impl
= NULL
;
754 gs_mimeTypesManager
.m_fallbacks
.Clear();
758 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
761 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
763 #endif // wxUSE_MIMETYPE