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"
59 #elif defined(__DOS__)
60 #include "wx/msdos/mimetype.h"
62 #include "wx/unix/mimetype.h"
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 wxFileTypeInfo::wxFileTypeInfo(const wxChar
*mimeType
,
74 const wxChar
*openCmd
,
75 const wxChar
*printCmd
,
78 : m_mimeType(mimeType
),
84 va_start(argptr
, desc
);
88 const wxChar
*ext
= va_arg(argptr
, const wxChar
*);
91 // NULL terminates the list
102 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
104 m_mimeType
= sArray
[0u];
105 m_openCmd
= sArray
[1u];
106 m_printCmd
= sArray
[2u];
107 m_desc
= sArray
[3u];
109 size_t count
= sArray
.GetCount();
110 for ( size_t i
= 4; i
< count
; i
++ )
112 m_exts
.Add(sArray
[i
]);
116 #include "wx/arrimpl.cpp"
117 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
119 // ============================================================================
120 // implementation of the wrapper classes
121 // ============================================================================
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
128 wxString
wxFileType::ExpandCommand(const wxString
& command
,
129 const wxFileType::MessageParameters
& params
)
131 bool hasFilename
= false;
134 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
135 if ( *pc
== wxT('%') ) {
138 // '%s' expands into file name (quoted because it might
139 // contain spaces) - except if there are already quotes
140 // there because otherwise some programs may get confused
141 // by double double quotes
143 if ( *(pc
- 2) == wxT('"') )
144 str
<< params
.GetFileName();
146 str
<< wxT('"') << params
.GetFileName() << wxT('"');
148 str
<< params
.GetFileName();
153 // '%t' expands into MIME type (quote it too just to be
155 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
160 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
161 if ( pEnd
== NULL
) {
163 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
164 params
.GetMimeType().c_str());
168 wxString
param(pc
+ 1, pEnd
- pc
- 1);
169 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
177 // TODO %n is the number of parts, %F is an array containing
178 // the names of temp files these parts were written to
179 // and their mime types.
183 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
184 *pc
, command
.c_str());
193 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
194 // the program will accept the data on stdin so normally we should append
195 // "< %s" to the end of the command in such case, but not all commands
196 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
197 // and appending "< %s" to this command makes the test fail... I don't
198 // know of the correct solution, try to guess what we have to do.
200 // test now carried out on reading file so test should never get here
201 if ( !hasFilename
&& !str
.empty()
203 && !str
.StartsWith(_T("test "))
206 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
212 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
218 wxFileType::wxFileType()
221 m_impl
= new wxFileTypeImpl
;
224 wxFileType::~wxFileType()
230 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
234 extensions
= m_info
->GetExtensions();
238 return m_impl
->GetExtensions(extensions
);
241 bool wxFileType::GetMimeType(wxString
*mimeType
) const
243 wxCHECK_MSG( mimeType
, false, _T("invalid parameter in GetMimeType") );
247 *mimeType
= m_info
->GetMimeType();
252 return m_impl
->GetMimeType(mimeType
);
255 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
260 mimeTypes
.Add(m_info
->GetMimeType());
265 return m_impl
->GetMimeTypes(mimeTypes
);
268 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
274 iconLoc
->SetFileName(m_info
->GetIconFile());
276 iconLoc
->SetIndex(m_info
->GetIconIndex());
283 return m_impl
->GetIcon(iconLoc
);
287 wxFileType::GetIcon(wxIconLocation
*iconloc
,
288 const MessageParameters
& params
) const
290 if ( !GetIcon(iconloc
) )
295 // we may have "%s" in the icon location string, at least under Windows, so
299 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
305 bool wxFileType::GetDescription(wxString
*desc
) const
307 wxCHECK_MSG( desc
, false, _T("invalid parameter in GetDescription") );
311 *desc
= m_info
->GetDescription();
316 return m_impl
->GetDescription(desc
);
320 wxFileType::GetOpenCommand(wxString
*openCmd
,
321 const wxFileType::MessageParameters
& params
) const
323 wxCHECK_MSG( openCmd
, false, _T("invalid parameter in GetOpenCommand") );
327 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
332 return m_impl
->GetOpenCommand(openCmd
, params
);
335 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
338 if ( !GetOpenCommand(&cmd
, filename
) )
340 // return empty string to indicate an error
348 wxFileType::GetPrintCommand(wxString
*printCmd
,
349 const wxFileType::MessageParameters
& params
) const
351 wxCHECK_MSG( printCmd
, false, _T("invalid parameter in GetPrintCommand") );
355 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
360 return m_impl
->GetPrintCommand(printCmd
, params
);
364 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
365 wxArrayString
*commands
,
366 const wxFileType::MessageParameters
& params
) const
373 #if defined (__WXMSW__) || defined(__UNIX__)
374 return m_impl
->GetAllCommands(verbs
, commands
, params
);
375 #else // !__WXMSW__ || Unix
376 // we don't know how to retrieve all commands, so just try the 2 we know
380 if ( GetOpenCommand(&cmd
, params
) )
383 verbs
->Add(_T("Open"));
389 if ( GetPrintCommand(&cmd
, params
) )
392 verbs
->Add(_T("Print"));
400 #endif // __WXMSW__/| __UNIX__
403 bool wxFileType::Unassociate()
405 #if defined(__WXMSW__)
406 return m_impl
->Unassociate();
407 #elif defined(__UNIX__)
408 return m_impl
->Unassociate(this);
410 wxFAIL_MSG( _T("not implemented") ); // TODO
415 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
416 bool overwriteprompt
)
418 #if defined (__WXMSW__) || defined(__UNIX__)
419 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
421 wxFAIL_MSG(_T("not implemented"));
426 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
430 // VZ: should we do this?
431 // chris elliott : only makes sense in MS windows
433 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxEmptyString
, wxEmptyString
));
435 wxCHECK_MSG( !sTmp
.empty(), false, _T("need the icon file") );
437 #if defined (__WXMSW__) || defined(__UNIX__)
438 return m_impl
->SetDefaultIcon (cmd
, index
);
440 wxFAIL_MSG(_T("not implemented"));
447 // ----------------------------------------------------------------------------
448 // wxMimeTypesManager
449 // ----------------------------------------------------------------------------
451 void wxMimeTypesManager::EnsureImpl()
454 m_impl
= new wxMimeTypesManagerImpl
;
457 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
458 const wxString
& wildcard
)
460 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
461 wxT("first MIME type can't contain wildcards") );
463 // all comparaisons are case insensitive (2nd arg of IsSameAs() is false)
464 if ( wildcard
.BeforeFirst(wxT('/')).
465 IsSameAs(mimeType
.BeforeFirst(wxT('/')), false) )
467 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
469 if ( strSubtype
== wxT("*") ||
470 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), false) )
472 // matches (either exactly or it's a wildcard)
480 wxMimeTypesManager::wxMimeTypesManager()
485 wxMimeTypesManager::~wxMimeTypesManager()
491 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
493 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
494 return m_impl
->Unassociate(ft
);
496 return ft
->Unassociate();
502 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
506 #if defined(__WXMSW__) || defined(__UNIX__)
507 return m_impl
->Associate(ftInfo
);
508 #else // other platforms
509 wxFAIL_MSG( _T("not implemented") ); // TODO
515 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
518 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
521 // check the fallbacks
523 // TODO linear search is potentially slow, perhaps we should use a
525 size_t count
= m_fallbacks
.GetCount();
526 for ( size_t n
= 0; n
< count
; n
++ ) {
527 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
528 ft
= new wxFileType(m_fallbacks
[n
]);
539 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
542 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
545 // check the fallbacks
547 // TODO linear search is potentially slow, perhaps we should use a
549 size_t count
= m_fallbacks
.GetCount();
550 for ( size_t n
= 0; n
< count
; n
++ ) {
551 if ( wxMimeTypesManager::IsOfType(mimeType
,
552 m_fallbacks
[n
].GetMimeType()) ) {
553 ft
= new wxFileType(m_fallbacks
[n
]);
563 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
566 return m_impl
->ReadMailcap(filename
, fallback
);
569 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
572 return m_impl
->ReadMimeTypes(filename
);
575 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
578 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
583 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
586 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
588 // add the fallback filetypes
589 size_t count
= m_fallbacks
.GetCount();
590 for ( size_t n
= 0; n
< count
; n
++ ) {
591 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
592 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
600 void wxMimeTypesManager::Initialize(int mcapStyle
,
601 const wxString
& sExtraDir
)
603 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
606 m_impl
->Initialize(mcapStyle
, sExtraDir
);
613 // and this function clears all the data from the manager
614 void wxMimeTypesManager::ClearData()
616 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
623 // ----------------------------------------------------------------------------
624 // global data and wxMimeTypeCmnModule
625 // ----------------------------------------------------------------------------
628 static wxMimeTypesManager gs_mimeTypesManager
;
630 // and public pointer
631 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
633 class wxMimeTypeCmnModule
: public wxModule
636 wxMimeTypeCmnModule() : wxModule() { }
637 virtual bool OnInit() { return true; }
638 virtual void OnExit()
640 // this avoids false memory leak allerts:
641 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
643 delete gs_mimeTypesManager
.m_impl
;
644 gs_mimeTypesManager
.m_impl
= NULL
;
645 gs_mimeTypesManager
.m_fallbacks
.Clear();
649 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
652 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
654 #endif // wxUSE_MIMETYPE