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
)
532 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
533 return m_impl
->Unassociate(ft
);
535 return ft
->Unassociate();
541 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
545 #if defined(__WXMSW__) || defined(__UNIX__)
546 return m_impl
->Associate(ftInfo
);
547 #else // other platforms
549 wxFAIL_MSG( _T("not implemented") ); // TODO
555 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
558 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
561 // check the fallbacks
563 // TODO linear search is potentially slow, perhaps we should use a
565 size_t count
= m_fallbacks
.GetCount();
566 for ( size_t n
= 0; n
< count
; n
++ ) {
567 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
568 ft
= new wxFileType(m_fallbacks
[n
]);
579 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
582 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
585 // check the fallbacks
587 // TODO linear search is potentially slow, perhaps we should use a
589 size_t count
= m_fallbacks
.GetCount();
590 for ( size_t n
= 0; n
< count
; n
++ ) {
591 if ( wxMimeTypesManager::IsOfType(mimeType
,
592 m_fallbacks
[n
].GetMimeType()) ) {
593 ft
= new wxFileType(m_fallbacks
[n
]);
603 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
606 return m_impl
->ReadMailcap(filename
, fallback
);
609 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
612 return m_impl
->ReadMimeTypes(filename
);
615 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
618 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
623 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
626 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
628 // add the fallback filetypes
629 size_t count
= m_fallbacks
.GetCount();
630 for ( size_t n
= 0; n
< count
; n
++ ) {
631 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
632 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
640 void wxMimeTypesManager::Initialize(int mcapStyle
,
641 const wxString
& sExtraDir
)
643 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
646 m_impl
->Initialize(mcapStyle
, sExtraDir
);
653 // and this function clears all the data from the manager
654 void wxMimeTypesManager::ClearData()
656 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
663 // ----------------------------------------------------------------------------
664 // global data and wxMimeTypeCmnModule
665 // ----------------------------------------------------------------------------
668 static wxMimeTypesManager gs_mimeTypesManager
;
670 // and public pointer
671 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
673 class wxMimeTypeCmnModule
: public wxModule
676 wxMimeTypeCmnModule() : wxModule() { }
678 virtual bool OnInit() { return true; }
679 virtual void OnExit()
681 wxMimeTypesManagerFactory::Set(NULL
);
683 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
685 delete gs_mimeTypesManager
.m_impl
;
686 gs_mimeTypesManager
.m_impl
= NULL
;
687 gs_mimeTypesManager
.m_fallbacks
.Clear();
691 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
694 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
696 #endif // wxUSE_MIMETYPE