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__)
57 #include "wx/os2/mimetype.h"
59 #include "wx/unix/mimetype.h"
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 wxFileTypeInfo::wxFileTypeInfo(const wxChar
*mimeType
,
71 const wxChar
*openCmd
,
72 const wxChar
*printCmd
,
75 : m_mimeType(mimeType
),
81 va_start(argptr
, desc
);
85 const wxChar
*ext
= va_arg(argptr
, const wxChar
*);
88 // NULL terminates the list
99 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
101 m_mimeType
= sArray
[0u];
102 m_openCmd
= sArray
[1u];
103 m_printCmd
= sArray
[2u];
104 m_desc
= sArray
[3u];
106 size_t count
= sArray
.GetCount();
107 for ( size_t i
= 4; i
< count
; i
++ )
109 m_exts
.Add(sArray
[i
]);
113 #include "wx/arrimpl.cpp"
114 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
116 // ============================================================================
117 // implementation of the wrapper classes
118 // ============================================================================
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
125 wxString
wxFileType::ExpandCommand(const wxString
& command
,
126 const wxFileType::MessageParameters
& params
)
128 bool hasFilename
= FALSE
;
131 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
132 if ( *pc
== wxT('%') ) {
135 // '%s' expands into file name (quoted because it might
136 // contain spaces) - except if there are already quotes
137 // there because otherwise some programs may get confused
138 // by double double quotes
140 if ( *(pc
- 2) == wxT('"') )
141 str
<< params
.GetFileName();
143 str
<< wxT('"') << params
.GetFileName() << wxT('"');
145 str
<< params
.GetFileName();
150 // '%t' expands into MIME type (quote it too just to be
152 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
157 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
158 if ( pEnd
== NULL
) {
160 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
161 params
.GetMimeType().c_str());
165 wxString
param(pc
+ 1, pEnd
- pc
- 1);
166 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
174 // TODO %n is the number of parts, %F is an array containing
175 // the names of temp files these parts were written to
176 // and their mime types.
180 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
181 *pc
, command
.c_str());
190 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
191 // the program will accept the data on stdin so normally we should append
192 // "< %s" to the end of the command in such case, but not all commands
193 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
194 // and appending "< %s" to this command makes the test fail... I don't
195 // know of the correct solution, try to guess what we have to do.
197 // test now carried out on reading file so test should never get here
198 if ( !hasFilename
&& !str
.IsEmpty()
200 && !str
.StartsWith(_T("test "))
203 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
209 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
215 wxFileType::wxFileType()
218 m_impl
= new wxFileTypeImpl
;
221 wxFileType::~wxFileType()
227 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
231 extensions
= m_info
->GetExtensions();
235 return m_impl
->GetExtensions(extensions
);
238 bool wxFileType::GetMimeType(wxString
*mimeType
) const
240 wxCHECK_MSG( mimeType
, FALSE
, _T("invalid parameter in GetMimeType") );
244 *mimeType
= m_info
->GetMimeType();
249 return m_impl
->GetMimeType(mimeType
);
252 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
257 mimeTypes
.Add(m_info
->GetMimeType());
262 return m_impl
->GetMimeTypes(mimeTypes
);
265 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
271 iconLoc
->SetFileName(m_info
->GetIconFile());
273 iconLoc
->SetIndex(m_info
->GetIconIndex());
280 return m_impl
->GetIcon(iconLoc
);
284 wxFileType::GetIcon(wxIconLocation
*iconloc
,
285 const MessageParameters
& params
) const
287 if ( !GetIcon(iconloc
) )
292 // we may have "%s" in the icon location string, at least under Windows, so
296 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
302 bool wxFileType::GetDescription(wxString
*desc
) const
304 wxCHECK_MSG( desc
, FALSE
, _T("invalid parameter in GetDescription") );
308 *desc
= m_info
->GetDescription();
313 return m_impl
->GetDescription(desc
);
317 wxFileType::GetOpenCommand(wxString
*openCmd
,
318 const wxFileType::MessageParameters
& params
) const
320 wxCHECK_MSG( openCmd
, FALSE
, _T("invalid parameter in GetOpenCommand") );
324 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
329 return m_impl
->GetOpenCommand(openCmd
, params
);
332 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
335 if ( !GetOpenCommand(&cmd
, filename
) )
337 // return empty string to indicate an error
345 wxFileType::GetPrintCommand(wxString
*printCmd
,
346 const wxFileType::MessageParameters
& params
) const
348 wxCHECK_MSG( printCmd
, FALSE
, _T("invalid parameter in GetPrintCommand") );
352 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
357 return m_impl
->GetPrintCommand(printCmd
, params
);
361 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
362 wxArrayString
*commands
,
363 const wxFileType::MessageParameters
& params
) const
370 #if defined (__WXMSW__) || defined(__UNIX__)
371 return m_impl
->GetAllCommands(verbs
, commands
, params
);
372 #else // !__WXMSW__ || Unix
373 // we don't know how to retrieve all commands, so just try the 2 we know
377 if ( GetOpenCommand(&cmd
, params
) )
380 verbs
->Add(_T("Open"));
386 if ( GetPrintCommand(&cmd
, params
) )
389 verbs
->Add(_T("Print"));
397 #endif // __WXMSW__/| __UNIX__
400 bool wxFileType::Unassociate()
402 #if defined(__WXMSW__)
403 return m_impl
->Unassociate();
404 #elif defined(__UNIX__) && !defined(__WXPM__)
405 return m_impl
->Unassociate(this);
407 wxFAIL_MSG( _T("not implemented") ); // TODO
412 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
413 bool overwriteprompt
)
415 #if defined (__WXMSW__) || defined(__UNIX__)
416 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
418 wxFAIL_MSG(_T("not implemented"));
423 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
427 // VZ: should we do this?
428 // chris elliott : only makes sense in MS windows
430 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxT(""), wxT("")));
432 wxCHECK_MSG( !sTmp
.empty(), FALSE
, _T("need the icon file") );
434 #if defined (__WXMSW__) || defined(__UNIX__)
435 return m_impl
->SetDefaultIcon (cmd
, index
);
437 wxFAIL_MSG(_T("not implemented"));
444 // ----------------------------------------------------------------------------
445 // wxMimeTypesManager
446 // ----------------------------------------------------------------------------
448 void wxMimeTypesManager::EnsureImpl()
451 m_impl
= new wxMimeTypesManagerImpl
;
454 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
455 const wxString
& wildcard
)
457 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
458 wxT("first MIME type can't contain wildcards") );
460 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
461 if ( wildcard
.BeforeFirst(wxT('/')).
462 IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
464 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
466 if ( strSubtype
== wxT("*") ||
467 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
469 // matches (either exactly or it's a wildcard)
477 wxMimeTypesManager::wxMimeTypesManager()
482 wxMimeTypesManager::~wxMimeTypesManager()
488 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
490 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
491 return m_impl
->Unassociate(ft
);
493 return ft
->Unassociate();
499 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
503 #if defined(__WXMSW__) || (defined(__UNIX__) && !defined(__WXPM__))
504 return m_impl
->Associate(ftInfo
);
505 #else // other platforms
506 wxFAIL_MSG( _T("not implemented") ); // TODO
512 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
515 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
518 // check the fallbacks
520 // TODO linear search is potentially slow, perhaps we should use a
522 size_t count
= m_fallbacks
.GetCount();
523 for ( size_t n
= 0; n
< count
; n
++ ) {
524 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
525 ft
= new wxFileType(m_fallbacks
[n
]);
536 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
539 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
542 // check the fallbacks
544 // TODO linear search is potentially slow, perhaps we should use a
546 size_t count
= m_fallbacks
.GetCount();
547 for ( size_t n
= 0; n
< count
; n
++ ) {
548 if ( wxMimeTypesManager::IsOfType(mimeType
,
549 m_fallbacks
[n
].GetMimeType()) ) {
550 ft
= new wxFileType(m_fallbacks
[n
]);
560 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
563 return m_impl
->ReadMailcap(filename
, fallback
);
566 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
569 return m_impl
->ReadMimeTypes(filename
);
572 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
575 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
580 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
583 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
585 // add the fallback filetypes
586 size_t count
= m_fallbacks
.GetCount();
587 for ( size_t n
= 0; n
< count
; n
++ ) {
588 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
589 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
597 void wxMimeTypesManager::Initialize(int mcapStyle
,
598 const wxString
& sExtraDir
)
600 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
603 m_impl
->Initialize(mcapStyle
, sExtraDir
);
610 // and this function clears all the data from the manager
611 void wxMimeTypesManager::ClearData()
613 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
620 // ----------------------------------------------------------------------------
621 // global data and wxMimeTypeCmnModule
622 // ----------------------------------------------------------------------------
625 static wxMimeTypesManager gs_mimeTypesManager
;
627 // and public pointer
628 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
630 class wxMimeTypeCmnModule
: public wxModule
633 wxMimeTypeCmnModule() : wxModule() { }
634 virtual bool OnInit() { return TRUE
; }
635 virtual void OnExit()
637 // this avoids false memory leak allerts:
638 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
640 delete gs_mimeTypesManager
.m_impl
;
641 gs_mimeTypesManager
.m_impl
= NULL
;
642 gs_mimeTypesManager
.m_fallbacks
.Clear();
646 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
649 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
651 #endif // wxUSE_MIMETYPE