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 license (part of wxExtra library)
11 /////////////////////////////////////////////////////////////////////////////
13 // ============================================================================
15 // ============================================================================
17 // ----------------------------------------------------------------------------
19 // ----------------------------------------------------------------------------
22 #pragma implementation "mimetypebase.h"
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
27 #include "wx/module.h"
38 #include "wx/string.h"
47 #include "wx/dynarray.h"
48 #include "wx/confbase.h"
50 #include "wx/mimetype.h"
52 // other standard headers
55 // implementation classes:
56 #if defined(__WXMSW__)
57 #include "wx/msw/mimetype.h"
58 #elif defined (__WXMAC__)
59 #include "wx/mac/mimetype.h"
60 #elif defined (__WXPM__)
61 #include "wx/os2/mimetype.h"
63 #include "wx/unix/mimetype.h"
66 // ============================================================================
68 // ============================================================================
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 wxFileTypeInfo::wxFileTypeInfo(const char *mimeType
,
79 : m_mimeType(mimeType
),
85 va_start(argptr
, desc
);
89 const char *ext
= va_arg(argptr
, const char *);
92 // NULL terminates the list
102 #include "wx/arrimpl.cpp"
103 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
105 // ============================================================================
106 // implementation of the wrapper classes
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
114 wxString
wxFileType::ExpandCommand(const wxString
& command
,
115 const wxFileType::MessageParameters
& params
)
117 bool hasFilename
= FALSE
;
120 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
121 if ( *pc
== wxT('%') ) {
124 // '%s' expands into file name (quoted because it might
125 // contain spaces) - except if there are already quotes
126 // there because otherwise some programs may get confused
127 // by double double quotes
129 if ( *(pc
- 2) == wxT('"') )
130 str
<< params
.GetFileName();
132 str
<< wxT('"') << params
.GetFileName() << wxT('"');
134 str
<< params
.GetFileName();
139 // '%t' expands into MIME type (quote it too just to be
141 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
146 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
147 if ( pEnd
== NULL
) {
149 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
150 params
.GetMimeType().c_str());
154 wxString
param(pc
+ 1, pEnd
- pc
- 1);
155 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
163 // TODO %n is the number of parts, %F is an array containing
164 // the names of temp files these parts were written to
165 // and their mime types.
169 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
170 *pc
, command
.c_str());
179 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
180 // the program will accept the data on stdin so normally we should append
181 // "< %s" to the end of the command in such case, but not all commands
182 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
183 // and appending "< %s" to this command makes the test fail... I don't
184 // know of the correct solution, try to guess what we have to do.
185 if ( !hasFilename
&& !str
.IsEmpty()
187 && !str
.StartsWith(_T("test "))
190 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
196 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
202 wxFileType::wxFileType()
205 m_impl
= new wxFileTypeImpl
;
208 wxFileType::~wxFileType()
214 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
218 extensions
= m_info
->GetExtensions();
222 return m_impl
->GetExtensions(extensions
);
225 bool wxFileType::GetMimeType(wxString
*mimeType
) const
227 wxCHECK_MSG( mimeType
, FALSE
, _T("invalid parameter in GetMimeType") );
231 *mimeType
= m_info
->GetMimeType();
236 return m_impl
->GetMimeType(mimeType
);
239 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
244 mimeTypes
.Add(m_info
->GetMimeType());
249 return m_impl
->GetMimeTypes(mimeTypes
);
252 bool wxFileType::GetIcon(wxIcon
*icon
,
254 int *iconIndex
) const
259 *iconFile
= m_info
->GetIconFile();
261 *iconIndex
= m_info
->GetIconIndex();
264 if ( icon
&& !m_info
->GetIconFile().empty() )
266 // FIXME: what about the index?
267 icon
->LoadFile(m_info
->GetIconFile());
275 return m_impl
->GetIcon(icon
, iconFile
, iconIndex
);
277 return m_impl
->GetIcon(icon
);
281 bool wxFileType::GetDescription(wxString
*desc
) const
283 wxCHECK_MSG( desc
, FALSE
, _T("invalid parameter in GetDescription") );
287 *desc
= m_info
->GetDescription();
292 return m_impl
->GetDescription(desc
);
296 wxFileType::GetOpenCommand(wxString
*openCmd
,
297 const wxFileType::MessageParameters
& params
) const
299 wxCHECK_MSG( openCmd
, FALSE
, _T("invalid parameter in GetOpenCommand") );
303 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
308 return m_impl
->GetOpenCommand(openCmd
, params
);
312 wxFileType::GetPrintCommand(wxString
*printCmd
,
313 const wxFileType::MessageParameters
& params
) const
315 wxCHECK_MSG( printCmd
, FALSE
, _T("invalid parameter in GetPrintCommand") );
319 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
324 return m_impl
->GetPrintCommand(printCmd
, params
);
328 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
329 wxArrayString
*commands
,
330 const wxFileType::MessageParameters
& params
) const
338 return m_impl
->GetAllCommands(verbs
, commands
, params
);
340 // we don't know how to retrieve all commands, so just try the 2 we know
344 if ( GetOpenCommand(&cmd
, params
) )
347 verbs
->Add(_T("Open"));
353 if ( GetPrintCommand(&cmd
, params
) )
356 verbs
->Add(_T("Print"));
364 #endif // __WXMSW__/!__WXMSW__
367 bool wxFileType::Unassociate()
369 #if defined(__WXMSW__) || defined(__UNIX__)
370 return m_impl
->Unassociate();
372 wxFAIL_MSG( _T("not implemented") ); // TODO
377 // ----------------------------------------------------------------------------
378 // wxMimeTypesManager
379 // ----------------------------------------------------------------------------
381 void wxMimeTypesManager::EnsureImpl()
384 m_impl
= new wxMimeTypesManagerImpl
;
387 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
388 const wxString
& wildcard
)
390 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
391 wxT("first MIME type can't contain wildcards") );
393 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
394 if ( wildcard
.BeforeFirst(wxT('/')).
395 IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
397 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
399 if ( strSubtype
== wxT("*") ||
400 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
402 // matches (either exactly or it's a wildcard)
410 wxMimeTypesManager::wxMimeTypesManager()
415 wxMimeTypesManager::~wxMimeTypesManager()
422 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
426 #if defined(__WXMSW__) || defined(__UNIX__)
427 return m_impl
->Associate(ftInfo
);
428 #else // other platforms
429 wxFAIL_MSG( _T("not implemented") ); // TODO
435 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
438 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
441 // check the fallbacks
443 // TODO linear search is potentially slow, perhaps we should use a
445 size_t count
= m_fallbacks
.GetCount();
446 for ( size_t n
= 0; n
< count
; n
++ ) {
447 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
448 ft
= new wxFileType(m_fallbacks
[n
]);
459 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
462 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
465 // check the fallbacks
467 // TODO linear search is potentially slow, perhaps we should use a sorted
469 size_t count
= m_fallbacks
.GetCount();
470 for ( size_t n
= 0; n
< count
; n
++ ) {
471 if ( wxMimeTypesManager::IsOfType(mimeType
,
472 m_fallbacks
[n
].GetMimeType()) ) {
473 ft
= new wxFileType(m_fallbacks
[n
]);
483 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
486 return m_impl
->ReadMailcap(filename
, fallback
);
489 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
492 return m_impl
->ReadMimeTypes(filename
);
495 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
498 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
503 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
506 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
508 // add the fallback filetypes
509 size_t count
= m_fallbacks
.GetCount();
510 for ( size_t n
= 0; n
< count
; n
++ ) {
511 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
512 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
520 // ----------------------------------------------------------------------------
521 // global data and wxMimeTypeCmnModule
522 // ----------------------------------------------------------------------------
525 static wxMimeTypesManager gs_mimeTypesManager
;
527 // and public pointer
528 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
530 class wxMimeTypeCmnModule
: public wxModule
533 wxMimeTypeCmnModule() : wxModule() { }
534 virtual bool OnInit() { return TRUE
; }
535 virtual void OnExit()
537 // this avoids false memory leak allerts:
538 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
540 delete gs_mimeTypesManager
.m_impl
;
541 gs_mimeTypesManager
.m_impl
= NULL
;
545 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
548 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)