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 // ----------------------------------------------------------------------------
21 // for compilers that support precompilation, includes "wx.h".
22 #include "wx/wxprec.h"
31 #include "wx/string.h"
34 #include "wx/module.h"
37 #include "wx/iconloc.h"
39 #include "wx/dynarray.h"
40 #include "wx/confbase.h"
42 #include "wx/mimetype.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 const wxChar
*ext
= va_arg(argptr
, const wxChar
*);
87 // NULL terminates the list
98 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
100 m_mimeType
= sArray
[0u];
101 m_openCmd
= sArray
[1u];
102 m_printCmd
= sArray
[2u];
103 m_desc
= sArray
[3u];
105 size_t count
= sArray
.GetCount();
106 for ( size_t i
= 4; i
< count
; i
++ )
108 m_exts
.Add(sArray
[i
]);
112 #include "wx/arrimpl.cpp"
113 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
)
115 // ============================================================================
116 // implementation of the wrapper classes
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
124 wxString
wxFileType::ExpandCommand(const wxString
& command
,
125 const wxFileType::MessageParameters
& params
)
127 bool hasFilename
= false;
130 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
131 if ( *pc
== wxT('%') ) {
134 // '%s' expands into file name (quoted because it might
135 // contain spaces) - except if there are already quotes
136 // there because otherwise some programs may get confused
137 // by double double quotes
139 if ( *(pc
- 2) == wxT('"') )
140 str
<< params
.GetFileName();
142 str
<< wxT('"') << params
.GetFileName() << wxT('"');
144 str
<< params
.GetFileName();
149 // '%t' expands into MIME type (quote it too just to be
151 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
156 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
157 if ( pEnd
== NULL
) {
159 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
160 params
.GetMimeType().c_str());
164 wxString
param(pc
+ 1, pEnd
- pc
- 1);
165 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
173 // TODO %n is the number of parts, %F is an array containing
174 // the names of temp files these parts were written to
175 // and their mime types.
179 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
180 *pc
, command
.c_str());
189 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
190 // the program will accept the data on stdin so normally we should append
191 // "< %s" to the end of the command in such case, but not all commands
192 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
193 // and appending "< %s" to this command makes the test fail... I don't
194 // know of the correct solution, try to guess what we have to do.
196 // test now carried out on reading file so test should never get here
197 if ( !hasFilename
&& !str
.empty()
199 && !str
.StartsWith(_T("test "))
202 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
208 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
214 wxFileType::wxFileType()
217 m_impl
= new wxFileTypeImpl
;
220 wxFileType::~wxFileType()
226 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
230 extensions
= m_info
->GetExtensions();
234 return m_impl
->GetExtensions(extensions
);
237 bool wxFileType::GetMimeType(wxString
*mimeType
) const
239 wxCHECK_MSG( mimeType
, false, _T("invalid parameter in GetMimeType") );
243 *mimeType
= m_info
->GetMimeType();
248 return m_impl
->GetMimeType(mimeType
);
251 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
256 mimeTypes
.Add(m_info
->GetMimeType());
261 return m_impl
->GetMimeTypes(mimeTypes
);
264 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
270 iconLoc
->SetFileName(m_info
->GetIconFile());
272 iconLoc
->SetIndex(m_info
->GetIconIndex());
279 return m_impl
->GetIcon(iconLoc
);
283 wxFileType::GetIcon(wxIconLocation
*iconloc
,
284 const MessageParameters
& params
) const
286 if ( !GetIcon(iconloc
) )
291 // we may have "%s" in the icon location string, at least under Windows, so
295 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
301 bool wxFileType::GetDescription(wxString
*desc
) const
303 wxCHECK_MSG( desc
, false, _T("invalid parameter in GetDescription") );
307 *desc
= m_info
->GetDescription();
312 return m_impl
->GetDescription(desc
);
316 wxFileType::GetOpenCommand(wxString
*openCmd
,
317 const wxFileType::MessageParameters
& params
) const
319 wxCHECK_MSG( openCmd
, false, _T("invalid parameter in GetOpenCommand") );
323 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
328 return m_impl
->GetOpenCommand(openCmd
, params
);
331 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
334 if ( !GetOpenCommand(&cmd
, filename
) )
336 // return empty string to indicate an error
344 wxFileType::GetPrintCommand(wxString
*printCmd
,
345 const wxFileType::MessageParameters
& params
) const
347 wxCHECK_MSG( printCmd
, false, _T("invalid parameter in GetPrintCommand") );
351 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
356 return m_impl
->GetPrintCommand(printCmd
, params
);
360 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
361 wxArrayString
*commands
,
362 const wxFileType::MessageParameters
& params
) const
369 #if defined (__WXMSW__) || defined(__UNIX__)
370 return m_impl
->GetAllCommands(verbs
, commands
, params
);
371 #else // !__WXMSW__ || Unix
372 // we don't know how to retrieve all commands, so just try the 2 we know
376 if ( GetOpenCommand(&cmd
, params
) )
379 verbs
->Add(_T("Open"));
385 if ( GetPrintCommand(&cmd
, params
) )
388 verbs
->Add(_T("Print"));
396 #endif // __WXMSW__/| __UNIX__
399 bool wxFileType::Unassociate()
401 #if defined(__WXMSW__)
402 return m_impl
->Unassociate();
403 #elif defined(__UNIX__)
404 return m_impl
->Unassociate(this);
406 wxFAIL_MSG( _T("not implemented") ); // TODO
411 bool wxFileType::SetCommand(const wxString
& cmd
,
412 const wxString
& verb
,
413 bool overwriteprompt
)
415 #if defined (__WXMSW__) || defined(__UNIX__)
416 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
420 wxUnusedVar(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
);
441 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
510 wxFAIL_MSG( _T("not implemented") ); // TODO
516 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
519 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
522 // check the fallbacks
524 // TODO linear search is potentially slow, perhaps we should use a
526 size_t count
= m_fallbacks
.GetCount();
527 for ( size_t n
= 0; n
< count
; n
++ ) {
528 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
529 ft
= new wxFileType(m_fallbacks
[n
]);
540 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
543 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
546 // check the fallbacks
548 // TODO linear search is potentially slow, perhaps we should use a
550 size_t count
= m_fallbacks
.GetCount();
551 for ( size_t n
= 0; n
< count
; n
++ ) {
552 if ( wxMimeTypesManager::IsOfType(mimeType
,
553 m_fallbacks
[n
].GetMimeType()) ) {
554 ft
= new wxFileType(m_fallbacks
[n
]);
564 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
567 return m_impl
->ReadMailcap(filename
, fallback
);
570 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
573 return m_impl
->ReadMimeTypes(filename
);
576 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
579 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
584 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
587 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
589 // add the fallback filetypes
590 size_t count
= m_fallbacks
.GetCount();
591 for ( size_t n
= 0; n
< count
; n
++ ) {
592 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
593 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
601 void wxMimeTypesManager::Initialize(int mcapStyle
,
602 const wxString
& sExtraDir
)
604 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
607 m_impl
->Initialize(mcapStyle
, sExtraDir
);
614 // and this function clears all the data from the manager
615 void wxMimeTypesManager::ClearData()
617 #if defined(__UNIX__) && !defined(__CYGWIN__) && !defined(__WINE__)
624 // ----------------------------------------------------------------------------
625 // global data and wxMimeTypeCmnModule
626 // ----------------------------------------------------------------------------
629 static wxMimeTypesManager gs_mimeTypesManager
;
631 // and public pointer
632 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
634 class wxMimeTypeCmnModule
: public wxModule
637 wxMimeTypeCmnModule() : wxModule() { }
638 virtual bool OnInit() { return true; }
639 virtual void OnExit()
641 // this avoids false memory leak allerts:
642 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
644 delete gs_mimeTypesManager
.m_impl
;
645 gs_mimeTypesManager
.m_impl
= NULL
;
646 gs_mimeTypesManager
.m_fallbacks
.Clear();
650 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
653 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
655 #endif // wxUSE_MIMETYPE