1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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"
31 #include "wx/string.h"
34 #include "wx/module.h"
37 #include "wx/iconloc.h"
39 #include "wx/dynarray.h"
40 #include "wx/confbase.h"
42 #include "wx/mimetype.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 // ----------------------------------------------------------------------------
69 wxFileTypeInfo::wxFileTypeInfo(const wxChar
*mimeType
,
70 const wxChar
*openCmd
,
71 const wxChar
*printCmd
,
74 : m_mimeType(mimeType
),
80 va_start(argptr
, desc
);
84 // icc gives this warning in its own va_arg() macro, argh
87 #pragma warning(disable: 1684)
90 const wxChar
*ext
= va_arg(argptr
, const wxChar
*);
97 // NULL terminates the list
108 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
110 m_mimeType
= sArray
[0u];
111 m_openCmd
= sArray
[1u];
112 m_printCmd
= sArray
[2u];
113 m_desc
= sArray
[3u];
115 size_t count
= sArray
.GetCount();
116 for ( size_t i
= 4; i
< count
; i
++ )
118 m_exts
.Add(sArray
[i
]);
122 #include "wx/arrimpl.cpp"
123 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
)
125 // ============================================================================
126 // implementation of the wrapper classes
127 // ============================================================================
129 // ----------------------------------------------------------------------------
131 // ----------------------------------------------------------------------------
134 wxString
wxFileType::ExpandCommand(const wxString
& command
,
135 const wxFileType::MessageParameters
& params
)
137 bool hasFilename
= false;
140 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
141 if ( *pc
== wxT('%') ) {
144 // '%s' expands into file name (quoted because it might
145 // contain spaces) - except if there are already quotes
146 // there because otherwise some programs may get confused
147 // by double double quotes
149 if ( *(pc
- 2) == wxT('"') )
150 str
<< params
.GetFileName();
152 str
<< wxT('"') << params
.GetFileName() << wxT('"');
154 str
<< params
.GetFileName();
159 // '%t' expands into MIME type (quote it too just to be
161 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
166 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
167 if ( pEnd
== NULL
) {
169 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
170 params
.GetMimeType().c_str());
174 wxString
param(pc
+ 1, pEnd
- pc
- 1);
175 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
183 // TODO %n is the number of parts, %F is an array containing
184 // the names of temp files these parts were written to
185 // and their mime types.
189 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
190 *pc
, command
.c_str());
199 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
200 // the program will accept the data on stdin so normally we should append
201 // "< %s" to the end of the command in such case, but not all commands
202 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
203 // and appending "< %s" to this command makes the test fail... I don't
204 // know of the correct solution, try to guess what we have to do.
206 // test now carried out on reading file so test should never get here
207 if ( !hasFilename
&& !str
.empty()
209 && !str
.StartsWith(_T("test "))
212 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
218 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
224 wxFileType::wxFileType()
227 m_impl
= new wxFileTypeImpl
;
230 wxFileType::~wxFileType()
236 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
240 extensions
= m_info
->GetExtensions();
244 return m_impl
->GetExtensions(extensions
);
247 bool wxFileType::GetMimeType(wxString
*mimeType
) const
249 wxCHECK_MSG( mimeType
, false, _T("invalid parameter in GetMimeType") );
253 *mimeType
= m_info
->GetMimeType();
258 return m_impl
->GetMimeType(mimeType
);
261 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
266 mimeTypes
.Add(m_info
->GetMimeType());
271 return m_impl
->GetMimeTypes(mimeTypes
);
274 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
280 iconLoc
->SetFileName(m_info
->GetIconFile());
282 iconLoc
->SetIndex(m_info
->GetIconIndex());
289 return m_impl
->GetIcon(iconLoc
);
293 wxFileType::GetIcon(wxIconLocation
*iconloc
,
294 const MessageParameters
& params
) const
296 if ( !GetIcon(iconloc
) )
301 // we may have "%s" in the icon location string, at least under Windows, so
305 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
311 bool wxFileType::GetDescription(wxString
*desc
) const
313 wxCHECK_MSG( desc
, false, _T("invalid parameter in GetDescription") );
317 *desc
= m_info
->GetDescription();
322 return m_impl
->GetDescription(desc
);
326 wxFileType::GetOpenCommand(wxString
*openCmd
,
327 const wxFileType::MessageParameters
& params
) const
329 wxCHECK_MSG( openCmd
, false, _T("invalid parameter in GetOpenCommand") );
333 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
338 return m_impl
->GetOpenCommand(openCmd
, params
);
341 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
344 if ( !GetOpenCommand(&cmd
, filename
) )
346 // return empty string to indicate an error
354 wxFileType::GetPrintCommand(wxString
*printCmd
,
355 const wxFileType::MessageParameters
& params
) const
357 wxCHECK_MSG( printCmd
, false, _T("invalid parameter in GetPrintCommand") );
361 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
366 return m_impl
->GetPrintCommand(printCmd
, params
);
370 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
371 wxArrayString
*commands
,
372 const wxFileType::MessageParameters
& params
) const
379 #if defined (__WXMSW__) || defined(__UNIX__)
380 return m_impl
->GetAllCommands(verbs
, commands
, params
);
381 #else // !__WXMSW__ || Unix
382 // we don't know how to retrieve all commands, so just try the 2 we know
386 if ( GetOpenCommand(&cmd
, params
) )
389 verbs
->Add(_T("Open"));
395 if ( GetPrintCommand(&cmd
, params
) )
398 verbs
->Add(_T("Print"));
406 #endif // __WXMSW__/| __UNIX__
409 bool wxFileType::Unassociate()
411 #if defined(__WXMSW__)
412 return m_impl
->Unassociate();
413 #elif defined(__UNIX__)
414 return m_impl
->Unassociate(this);
416 wxFAIL_MSG( _T("not implemented") ); // TODO
421 bool wxFileType::SetCommand(const wxString
& cmd
,
422 const wxString
& verb
,
423 bool overwriteprompt
)
425 #if defined (__WXMSW__) || defined(__UNIX__)
426 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
430 wxUnusedVar(overwriteprompt
);
431 wxFAIL_MSG(_T("not implemented"));
436 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
440 // VZ: should we do this?
441 // chris elliott : only makes sense in MS windows
443 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxEmptyString
, wxEmptyString
));
445 wxCHECK_MSG( !sTmp
.empty(), false, _T("need the icon file") );
447 #if defined (__WXMSW__) || defined(__UNIX__)
448 return m_impl
->SetDefaultIcon (cmd
, index
);
451 wxFAIL_MSG(_T("not implemented"));
456 //----------------------------------------------------------------------------
457 // wxMimeTypesManagerFactory
458 //----------------------------------------------------------------------------
460 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::m_factory
= NULL
;
463 void wxMimeTypesManagerFactory::SetFactory( wxMimeTypesManagerFactory
*factory
)
465 if (wxMimeTypesManagerFactory::m_factory
)
466 delete wxMimeTypesManagerFactory::m_factory
;
468 wxMimeTypesManagerFactory::m_factory
= factory
;
472 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::GetFactory()
474 if (!wxMimeTypesManagerFactory::m_factory
)
475 wxMimeTypesManagerFactory::m_factory
= new wxMimeTypesManagerFactory
;
477 return wxMimeTypesManagerFactory::m_factory
;
480 wxMimeTypesManagerImpl
*wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl()
482 return new wxMimeTypesManagerImpl
;
485 // ----------------------------------------------------------------------------
486 // wxMimeTypesManager
487 // ----------------------------------------------------------------------------
489 void wxMimeTypesManager::EnsureImpl()
492 m_impl
= wxMimeTypesManagerFactory::GetFactory()->CreateMimeTypesManagerImpl();
495 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
496 const wxString
& wildcard
)
498 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
499 wxT("first MIME type can't contain wildcards") );
501 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
502 if ( wildcard
.BeforeFirst(wxT('/')).
503 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
505 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
507 if ( strSubtype
== wxT("*") ||
508 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
510 // matches (either exactly or it's a wildcard)
518 wxMimeTypesManager::wxMimeTypesManager()
523 wxMimeTypesManager::~wxMimeTypesManager()
529 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
531 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
532 return m_impl
->Unassociate(ft
);
534 return ft
->Unassociate();
540 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
544 #if defined(__WXMSW__) || defined(__UNIX__)
545 return m_impl
->Associate(ftInfo
);
546 #else // other platforms
548 wxFAIL_MSG( _T("not implemented") ); // TODO
554 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
557 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
560 // check the fallbacks
562 // TODO linear search is potentially slow, perhaps we should use a
564 size_t count
= m_fallbacks
.GetCount();
565 for ( size_t n
= 0; n
< count
; n
++ ) {
566 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
567 ft
= new wxFileType(m_fallbacks
[n
]);
578 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
581 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
584 // check the fallbacks
586 // TODO linear search is potentially slow, perhaps we should use a
588 size_t count
= m_fallbacks
.GetCount();
589 for ( size_t n
= 0; n
< count
; n
++ ) {
590 if ( wxMimeTypesManager::IsOfType(mimeType
,
591 m_fallbacks
[n
].GetMimeType()) ) {
592 ft
= new wxFileType(m_fallbacks
[n
]);
602 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
605 return m_impl
->ReadMailcap(filename
, fallback
);
608 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
611 return m_impl
->ReadMimeTypes(filename
);
614 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
617 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
622 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
625 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
627 // add the fallback filetypes
628 size_t count
= m_fallbacks
.GetCount();
629 for ( size_t n
= 0; n
< count
; n
++ ) {
630 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
631 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
639 void wxMimeTypesManager::Initialize(int mcapStyle
,
640 const wxString
& sExtraDir
)
642 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
645 m_impl
->Initialize(mcapStyle
, sExtraDir
);
652 // and this function clears all the data from the manager
653 void wxMimeTypesManager::ClearData()
655 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
662 // ----------------------------------------------------------------------------
663 // global data and wxMimeTypeCmnModule
664 // ----------------------------------------------------------------------------
667 static wxMimeTypesManager gs_mimeTypesManager
;
669 // and public pointer
670 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
672 class wxMimeTypeCmnModule
: public wxModule
675 wxMimeTypeCmnModule() : wxModule() { }
676 virtual bool OnInit() { return true; }
677 virtual void OnExit()
679 // this avoids false memory leak allerts:
680 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
682 delete gs_mimeTypesManager
.m_impl
;
683 gs_mimeTypesManager
.m_impl
= NULL
;
684 gs_mimeTypesManager
.m_fallbacks
.Clear();
688 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
691 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
693 #endif // wxUSE_MIMETYPE