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 // ----------------------------------------------------------------------------
22 #pragma implementation "mimetypebase.h"
25 // for compilers that support precompilation, includes "wx.h".
26 #include "wx/wxprec.h"
35 #include "wx/string.h"
38 #include "wx/module.h"
41 #include "wx/iconloc.h"
43 #include "wx/dynarray.h"
44 #include "wx/confbase.h"
46 #include "wx/mimetype.h"
48 // other standard headers
51 // implementation classes:
52 #if defined(__WXMSW__)
53 #include "wx/msw/mimetype.h"
54 #elif defined(__WXMAC__)
55 #include "wx/mac/mimetype.h"
56 #elif defined(__WXPM__) || defined (__EMX__)
57 #include "wx/os2/mimetype.h"
60 #include "wx/unix/mimetype.h"
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 wxFileTypeInfo::wxFileTypeInfo(const wxChar
*mimeType
,
72 const wxChar
*openCmd
,
73 const wxChar
*printCmd
,
76 : m_mimeType(mimeType
),
82 va_start(argptr
, desc
);
86 const wxChar
*ext
= va_arg(argptr
, const wxChar
*);
89 // NULL terminates the list
100 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
102 m_mimeType
= sArray
[0u];
103 m_openCmd
= sArray
[1u];
104 m_printCmd
= sArray
[2u];
105 m_desc
= sArray
[3u];
107 size_t count
= sArray
.GetCount();
108 for ( size_t i
= 4; i
< count
; i
++ )
110 m_exts
.Add(sArray
[i
]);
114 #include "wx/arrimpl.cpp"
115 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
117 // ============================================================================
118 // implementation of the wrapper classes
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
126 wxString
wxFileType::ExpandCommand(const wxString
& command
,
127 const wxFileType::MessageParameters
& params
)
129 bool hasFilename
= false;
132 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
133 if ( *pc
== wxT('%') ) {
136 // '%s' expands into file name (quoted because it might
137 // contain spaces) - except if there are already quotes
138 // there because otherwise some programs may get confused
139 // by double double quotes
141 if ( *(pc
- 2) == wxT('"') )
142 str
<< params
.GetFileName();
144 str
<< wxT('"') << params
.GetFileName() << wxT('"');
146 str
<< params
.GetFileName();
151 // '%t' expands into MIME type (quote it too just to be
153 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
158 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
159 if ( pEnd
== NULL
) {
161 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
162 params
.GetMimeType().c_str());
166 wxString
param(pc
+ 1, pEnd
- pc
- 1);
167 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
175 // TODO %n is the number of parts, %F is an array containing
176 // the names of temp files these parts were written to
177 // and their mime types.
181 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
182 *pc
, command
.c_str());
191 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
192 // the program will accept the data on stdin so normally we should append
193 // "< %s" to the end of the command in such case, but not all commands
194 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
195 // and appending "< %s" to this command makes the test fail... I don't
196 // know of the correct solution, try to guess what we have to do.
198 // test now carried out on reading file so test should never get here
199 if ( !hasFilename
&& !str
.IsEmpty()
201 && !str
.StartsWith(_T("test "))
204 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
210 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
216 wxFileType::wxFileType()
219 m_impl
= new wxFileTypeImpl
;
222 wxFileType::~wxFileType()
228 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
232 extensions
= m_info
->GetExtensions();
236 return m_impl
->GetExtensions(extensions
);
239 bool wxFileType::GetMimeType(wxString
*mimeType
) const
241 wxCHECK_MSG( mimeType
, false, _T("invalid parameter in GetMimeType") );
245 *mimeType
= m_info
->GetMimeType();
250 return m_impl
->GetMimeType(mimeType
);
253 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
258 mimeTypes
.Add(m_info
->GetMimeType());
263 return m_impl
->GetMimeTypes(mimeTypes
);
266 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
272 iconLoc
->SetFileName(m_info
->GetIconFile());
274 iconLoc
->SetIndex(m_info
->GetIconIndex());
281 return m_impl
->GetIcon(iconLoc
);
285 wxFileType::GetIcon(wxIconLocation
*iconloc
,
286 const MessageParameters
& params
) const
288 if ( !GetIcon(iconloc
) )
293 // we may have "%s" in the icon location string, at least under Windows, so
297 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
303 bool wxFileType::GetDescription(wxString
*desc
) const
305 wxCHECK_MSG( desc
, false, _T("invalid parameter in GetDescription") );
309 *desc
= m_info
->GetDescription();
314 return m_impl
->GetDescription(desc
);
318 wxFileType::GetOpenCommand(wxString
*openCmd
,
319 const wxFileType::MessageParameters
& params
) const
321 wxCHECK_MSG( openCmd
, false, _T("invalid parameter in GetOpenCommand") );
325 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
330 return m_impl
->GetOpenCommand(openCmd
, params
);
333 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
336 if ( !GetOpenCommand(&cmd
, filename
) )
338 // return empty string to indicate an error
346 wxFileType::GetPrintCommand(wxString
*printCmd
,
347 const wxFileType::MessageParameters
& params
) const
349 wxCHECK_MSG( printCmd
, false, _T("invalid parameter in GetPrintCommand") );
353 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
358 return m_impl
->GetPrintCommand(printCmd
, params
);
362 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
363 wxArrayString
*commands
,
364 const wxFileType::MessageParameters
& params
) const
371 #if defined (__WXMSW__) || defined(__UNIX__)
372 return m_impl
->GetAllCommands(verbs
, commands
, params
);
373 #else // !__WXMSW__ || Unix
374 // we don't know how to retrieve all commands, so just try the 2 we know
378 if ( GetOpenCommand(&cmd
, params
) )
381 verbs
->Add(_T("Open"));
387 if ( GetPrintCommand(&cmd
, params
) )
390 verbs
->Add(_T("Print"));
398 #endif // __WXMSW__/| __UNIX__
401 bool wxFileType::Unassociate()
403 #if defined(__WXMSW__)
404 return m_impl
->Unassociate();
405 #elif defined(__UNIX__)
406 return m_impl
->Unassociate(this);
408 wxFAIL_MSG( _T("not implemented") ); // TODO
413 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
414 bool overwriteprompt
)
416 #if defined (__WXMSW__) || defined(__UNIX__)
417 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
419 wxFAIL_MSG(_T("not implemented"));
424 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
428 // VZ: should we do this?
429 // chris elliott : only makes sense in MS windows
431 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxT(""), wxT("")));
433 wxCHECK_MSG( !sTmp
.empty(), false, _T("need the icon file") );
435 #if defined (__WXMSW__) || defined(__UNIX__)
436 return m_impl
->SetDefaultIcon (cmd
, index
);
438 wxFAIL_MSG(_T("not implemented"));
445 // ----------------------------------------------------------------------------
446 // wxMimeTypesManager
447 // ----------------------------------------------------------------------------
449 void wxMimeTypesManager::EnsureImpl()
452 m_impl
= new wxMimeTypesManagerImpl
;
455 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
456 const wxString
& wildcard
)
458 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
459 wxT("first MIME type can't contain wildcards") );
461 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
462 if ( wildcard
.BeforeFirst(wxT('/')).
463 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
465 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
467 if ( strSubtype
== wxT("*") ||
468 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
470 // matches (either exactly or it's a wildcard)
478 wxMimeTypesManager::wxMimeTypesManager()
483 wxMimeTypesManager::~wxMimeTypesManager()
489 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
491 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
492 return m_impl
->Unassociate(ft
);
494 return ft
->Unassociate();
500 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
504 #if defined(__WXMSW__) || defined(__UNIX__)
505 return m_impl
->Associate(ftInfo
);
506 #else // other platforms
507 wxFAIL_MSG( _T("not implemented") ); // TODO
513 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
516 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
519 // check the fallbacks
521 // TODO linear search is potentially slow, perhaps we should use a
523 size_t count
= m_fallbacks
.GetCount();
524 for ( size_t n
= 0; n
< count
; n
++ ) {
525 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
526 ft
= new wxFileType(m_fallbacks
[n
]);
537 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
540 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
543 // check the fallbacks
545 // TODO linear search is potentially slow, perhaps we should use a
547 size_t count
= m_fallbacks
.GetCount();
548 for ( size_t n
= 0; n
< count
; n
++ ) {
549 if ( wxMimeTypesManager::IsOfType(mimeType
,
550 m_fallbacks
[n
].GetMimeType()) ) {
551 ft
= new wxFileType(m_fallbacks
[n
]);
561 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
564 return m_impl
->ReadMailcap(filename
, fallback
);
567 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
570 return m_impl
->ReadMimeTypes(filename
);
573 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
576 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
581 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
584 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
586 // add the fallback filetypes
587 size_t count
= m_fallbacks
.GetCount();
588 for ( size_t n
= 0; n
< count
; n
++ ) {
589 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
590 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
598 void wxMimeTypesManager::Initialize(int mcapStyle
,
599 const wxString
& sExtraDir
)
601 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
604 m_impl
->Initialize(mcapStyle
, sExtraDir
);
611 // and this function clears all the data from the manager
612 void wxMimeTypesManager::ClearData()
614 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
621 // ----------------------------------------------------------------------------
622 // global data and wxMimeTypeCmnModule
623 // ----------------------------------------------------------------------------
626 static wxMimeTypesManager gs_mimeTypesManager
;
628 // and public pointer
629 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
631 class wxMimeTypeCmnModule
: public wxModule
634 wxMimeTypeCmnModule() : wxModule() { }
635 virtual bool OnInit() { return true; }
636 virtual void OnExit()
638 // this avoids false memory leak allerts:
639 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
641 delete gs_mimeTypesManager
.m_impl
;
642 gs_mimeTypesManager
.m_impl
= NULL
;
643 gs_mimeTypesManager
.m_fallbacks
.Clear();
647 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
650 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
652 #endif // wxUSE_MIMETYPE