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"
39 #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 // ----------------------------------------------------------------------------
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::Set(wxMimeTypesManagerFactory
*factory
)
471 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::Get()
474 m_factory
= new wxMimeTypesManagerFactory
;
479 wxMimeTypesManagerImpl
*wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl()
481 return new wxMimeTypesManagerImpl
;
484 // ----------------------------------------------------------------------------
485 // wxMimeTypesManager
486 // ----------------------------------------------------------------------------
488 void wxMimeTypesManager::EnsureImpl()
491 m_impl
= wxMimeTypesManagerFactory::Get()->CreateMimeTypesManagerImpl();
494 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
495 const wxString
& wildcard
)
497 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
498 wxT("first MIME type can't contain wildcards") );
500 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
501 if ( wildcard
.BeforeFirst(wxT('/')).
502 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
504 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
506 if ( strSubtype
== wxT("*") ||
507 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
509 // matches (either exactly or it's a wildcard)
517 wxMimeTypesManager::wxMimeTypesManager()
522 wxMimeTypesManager::~wxMimeTypesManager()
528 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
530 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
531 return m_impl
->Unassociate(ft
);
533 return ft
->Unassociate();
539 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
543 #if defined(__WXMSW__) || defined(__UNIX__)
544 return m_impl
->Associate(ftInfo
);
545 #else // other platforms
547 wxFAIL_MSG( _T("not implemented") ); // TODO
553 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
556 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
559 // check the fallbacks
561 // TODO linear search is potentially slow, perhaps we should use a
563 size_t count
= m_fallbacks
.GetCount();
564 for ( size_t n
= 0; n
< count
; n
++ ) {
565 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
566 ft
= new wxFileType(m_fallbacks
[n
]);
577 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
580 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
583 // check the fallbacks
585 // TODO linear search is potentially slow, perhaps we should use a
587 size_t count
= m_fallbacks
.GetCount();
588 for ( size_t n
= 0; n
< count
; n
++ ) {
589 if ( wxMimeTypesManager::IsOfType(mimeType
,
590 m_fallbacks
[n
].GetMimeType()) ) {
591 ft
= new wxFileType(m_fallbacks
[n
]);
601 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
604 return m_impl
->ReadMailcap(filename
, fallback
);
607 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
610 return m_impl
->ReadMimeTypes(filename
);
613 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
616 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
621 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
624 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
626 // add the fallback filetypes
627 size_t count
= m_fallbacks
.GetCount();
628 for ( size_t n
= 0; n
< count
; n
++ ) {
629 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
630 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
638 void wxMimeTypesManager::Initialize(int mcapStyle
,
639 const wxString
& sExtraDir
)
641 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
644 m_impl
->Initialize(mcapStyle
, sExtraDir
);
651 // and this function clears all the data from the manager
652 void wxMimeTypesManager::ClearData()
654 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
661 // ----------------------------------------------------------------------------
662 // global data and wxMimeTypeCmnModule
663 // ----------------------------------------------------------------------------
666 static wxMimeTypesManager gs_mimeTypesManager
;
668 // and public pointer
669 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
671 class wxMimeTypeCmnModule
: public wxModule
674 wxMimeTypeCmnModule() : wxModule() { }
676 virtual bool OnInit() { return true; }
677 virtual void OnExit()
679 wxMimeTypesManagerFactory::Set(NULL
);
681 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
683 delete gs_mimeTypesManager
.m_impl
;
684 gs_mimeTypesManager
.m_impl
= NULL
;
685 gs_mimeTypesManager
.m_fallbacks
.Clear();
689 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
692 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
694 #endif // wxUSE_MIMETYPE