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(__WINDOWS__)
50 #include "wx/msw/mimetype.h"
51 #elif ( defined(__DARWIN__) )
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;
195 // We consider that only the file names with spaces in them need to be
196 // handled specially. This is not perfect, but this can be done easily
197 // under all platforms while handling the file names with quotes in them,
198 // for example, needs to be done differently.
199 const bool needToQuoteFilename
= params
.GetFileName().find_first_of(" \t")
203 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
204 if ( *pc
== wxT('%') ) {
207 // don't quote the file name if it's already quoted: notice
208 // that we check for a quote following it and not preceding
209 // it as at least under Windows we can have commands
210 // containing "file://%s" (with quotes) in them so the
211 // argument may be quoted even if there is no quote
212 // directly before "%s" itself
213 if ( needToQuoteFilename
&& pc
[1] != '"' )
214 str
<< wxT('"') << params
.GetFileName() << wxT('"');
216 str
<< params
.GetFileName();
221 // '%t' expands into MIME type (quote it too just to be
223 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
228 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
229 if ( pEnd
== NULL
) {
231 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
232 params
.GetMimeType().c_str());
236 wxString
param(pc
+ 1, pEnd
- pc
- 1);
237 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
245 // TODO %n is the number of parts, %F is an array containing
246 // the names of temp files these parts were written to
247 // and their mime types.
251 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
252 *pc
, command
.c_str());
261 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
262 // the program will accept the data on stdin so normally we should append
263 // "< %s" to the end of the command in such case, but not all commands
264 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
265 // and appending "< %s" to this command makes the test fail... I don't
266 // know of the correct solution, try to guess what we have to do.
268 // test now carried out on reading file so test should never get here
269 if ( !hasFilename
&& !str
.empty()
271 && !str
.StartsWith(wxT("test "))
276 if ( needToQuoteFilename
)
278 str
<< params
.GetFileName();
279 if ( needToQuoteFilename
)
286 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
292 wxFileType::wxFileType()
295 m_impl
= new wxFileTypeImpl
;
298 wxFileType::~wxFileType()
304 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
308 extensions
= m_info
->GetExtensions();
312 return m_impl
->GetExtensions(extensions
);
315 bool wxFileType::GetMimeType(wxString
*mimeType
) const
317 wxCHECK_MSG( mimeType
, false, wxT("invalid parameter in GetMimeType") );
321 *mimeType
= m_info
->GetMimeType();
326 return m_impl
->GetMimeType(mimeType
);
329 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
334 mimeTypes
.Add(m_info
->GetMimeType());
339 return m_impl
->GetMimeTypes(mimeTypes
);
342 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
348 iconLoc
->SetFileName(m_info
->GetIconFile());
350 iconLoc
->SetIndex(m_info
->GetIconIndex());
351 #endif // __WINDOWS__
357 return m_impl
->GetIcon(iconLoc
);
361 wxFileType::GetIcon(wxIconLocation
*iconloc
,
362 const MessageParameters
& params
) const
364 if ( !GetIcon(iconloc
) )
369 // we may have "%s" in the icon location string, at least under Windows, so
373 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
379 bool wxFileType::GetDescription(wxString
*desc
) const
381 wxCHECK_MSG( desc
, false, wxT("invalid parameter in GetDescription") );
385 *desc
= m_info
->GetDescription();
390 return m_impl
->GetDescription(desc
);
394 wxFileType::GetOpenCommand(wxString
*openCmd
,
395 const wxFileType::MessageParameters
& params
) const
397 wxCHECK_MSG( openCmd
, false, wxT("invalid parameter in GetOpenCommand") );
401 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
406 return m_impl
->GetOpenCommand(openCmd
, params
);
409 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
412 if ( !GetOpenCommand(&cmd
, filename
) )
414 // return empty string to indicate an error
422 wxFileType::GetPrintCommand(wxString
*printCmd
,
423 const wxFileType::MessageParameters
& params
) const
425 wxCHECK_MSG( printCmd
, false, wxT("invalid parameter in GetPrintCommand") );
429 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
434 return m_impl
->GetPrintCommand(printCmd
, params
);
438 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
439 wxArrayString
*commands
,
440 const wxFileType::MessageParameters
& params
) const
447 #if defined (__WINDOWS__) || defined(__UNIX__)
448 return m_impl
->GetAllCommands(verbs
, commands
, params
);
449 #else // !__WINDOWS__ || __UNIX__
450 // we don't know how to retrieve all commands, so just try the 2 we know
454 if ( GetOpenCommand(&cmd
, params
) )
457 verbs
->Add(wxT("Open"));
463 if ( GetPrintCommand(&cmd
, params
) )
466 verbs
->Add(wxT("Print"));
474 #endif // __WINDOWS__/| __UNIX__
477 bool wxFileType::Unassociate()
479 #if defined(__WINDOWS__)
480 return m_impl
->Unassociate();
481 #elif defined(__UNIX__)
482 return m_impl
->Unassociate(this);
484 wxFAIL_MSG( wxT("not implemented") ); // TODO
489 bool wxFileType::SetCommand(const wxString
& cmd
,
490 const wxString
& verb
,
491 bool overwriteprompt
)
493 #if defined (__WINDOWS__) || defined(__UNIX__)
494 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
498 wxUnusedVar(overwriteprompt
);
499 wxFAIL_MSG(wxT("not implemented"));
504 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
508 // VZ: should we do this?
509 // chris elliott : only makes sense in MS windows
511 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxEmptyString
, wxEmptyString
));
513 wxCHECK_MSG( !sTmp
.empty(), false, wxT("need the icon file") );
515 #if defined (__WINDOWS__) || defined(__UNIX__)
516 return m_impl
->SetDefaultIcon (cmd
, index
);
519 wxFAIL_MSG(wxT("not implemented"));
524 // ----------------------------------------------------------------------------
525 // wxMimeTypesManagerFactory
526 // ----------------------------------------------------------------------------
528 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::m_factory
= NULL
;
531 void wxMimeTypesManagerFactory::Set(wxMimeTypesManagerFactory
*factory
)
539 wxMimeTypesManagerFactory
*wxMimeTypesManagerFactory::Get()
542 m_factory
= new wxMimeTypesManagerFactory
;
547 wxMimeTypesManagerImpl
*wxMimeTypesManagerFactory::CreateMimeTypesManagerImpl()
549 return new wxMimeTypesManagerImpl
;
552 // ----------------------------------------------------------------------------
553 // wxMimeTypesManager
554 // ----------------------------------------------------------------------------
556 void wxMimeTypesManager::EnsureImpl()
559 m_impl
= wxMimeTypesManagerFactory::Get()->CreateMimeTypesManagerImpl();
562 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
563 const wxString
& wildcard
)
565 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
566 wxT("first MIME type can't contain wildcards") );
568 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
569 if ( wildcard
.BeforeFirst(wxT('/')).
570 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
572 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
574 if ( strSubtype
== wxT("*") ||
575 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
577 // matches (either exactly or it's a wildcard)
585 wxMimeTypesManager::wxMimeTypesManager()
590 wxMimeTypesManager::~wxMimeTypesManager()
596 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
600 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
601 return m_impl
->Unassociate(ft
);
603 return ft
->Unassociate();
609 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
613 #if defined(__WINDOWS__) || defined(__UNIX__)
614 return m_impl
->Associate(ftInfo
);
615 #else // other platforms
617 wxFAIL_MSG( wxT("not implemented") ); // TODO
623 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
627 wxString::const_iterator i
= ext
.begin();
628 const wxString::const_iterator end
= ext
.end();
629 wxString extWithoutDot
;
630 if ( i
!= end
&& *i
== '.' )
631 extWithoutDot
.assign(++i
, ext
.end());
635 wxCHECK_MSG( !ext
.empty(), NULL
, wxT("extension can't be empty") );
637 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(extWithoutDot
);
640 // check the fallbacks
642 // TODO linear search is potentially slow, perhaps we should use a
644 size_t count
= m_fallbacks
.GetCount();
645 for ( size_t n
= 0; n
< count
; n
++ ) {
646 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
647 ft
= new wxFileType(m_fallbacks
[n
]);
658 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
661 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
664 // check the fallbacks
666 // TODO linear search is potentially slow, perhaps we should use a
668 size_t count
= m_fallbacks
.GetCount();
669 for ( size_t n
= 0; n
< count
; n
++ ) {
670 if ( wxMimeTypesManager::IsOfType(mimeType
,
671 m_fallbacks
[n
].GetMimeType()) ) {
672 ft
= new wxFileType(m_fallbacks
[n
]);
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 wxDELETE(gs_mimeTypesManager
.m_impl
);
753 gs_mimeTypesManager
.m_fallbacks
.Clear();
757 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
760 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
762 #endif // wxUSE_MIMETYPE