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
103 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
105 m_mimeType
= sArray
[0u];
106 m_openCmd
= sArray
[1u];
107 m_printCmd
= sArray
[2u];
108 m_desc
= sArray
[3u];
110 size_t count
= sArray
.GetCount();
111 for ( size_t i
= 4; i
< count
; i
++ )
113 m_exts
.Add(sArray
[i
]);
117 #include "wx/arrimpl.cpp"
118 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
120 // ============================================================================
121 // implementation of the wrapper classes
122 // ============================================================================
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
129 wxString
wxFileType::ExpandCommand(const wxString
& command
,
130 const wxFileType::MessageParameters
& params
)
132 bool hasFilename
= FALSE
;
135 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
136 if ( *pc
== wxT('%') ) {
139 // '%s' expands into file name (quoted because it might
140 // contain spaces) - except if there are already quotes
141 // there because otherwise some programs may get confused
142 // by double double quotes
144 if ( *(pc
- 2) == wxT('"') )
145 str
<< params
.GetFileName();
147 str
<< wxT('"') << params
.GetFileName() << wxT('"');
149 str
<< params
.GetFileName();
154 // '%t' expands into MIME type (quote it too just to be
156 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
161 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
162 if ( pEnd
== NULL
) {
164 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
165 params
.GetMimeType().c_str());
169 wxString
param(pc
+ 1, pEnd
- pc
- 1);
170 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
178 // TODO %n is the number of parts, %F is an array containing
179 // the names of temp files these parts were written to
180 // and their mime types.
184 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
185 *pc
, command
.c_str());
194 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
195 // the program will accept the data on stdin so normally we should append
196 // "< %s" to the end of the command in such case, but not all commands
197 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
198 // and appending "< %s" to this command makes the test fail... I don't
199 // know of the correct solution, try to guess what we have to do.
201 // test now carried out on reading file so test should never get here
202 if ( !hasFilename
&& !str
.IsEmpty()
204 && !str
.StartsWith(_T("test "))
207 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
213 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
219 wxFileType::wxFileType()
222 m_impl
= new wxFileTypeImpl
;
225 wxFileType::~wxFileType()
231 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
235 extensions
= m_info
->GetExtensions();
239 return m_impl
->GetExtensions(extensions
);
242 bool wxFileType::GetMimeType(wxString
*mimeType
) const
244 wxCHECK_MSG( mimeType
, FALSE
, _T("invalid parameter in GetMimeType") );
248 *mimeType
= m_info
->GetMimeType();
253 return m_impl
->GetMimeType(mimeType
);
256 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
261 mimeTypes
.Add(m_info
->GetMimeType());
266 return m_impl
->GetMimeTypes(mimeTypes
);
269 bool wxFileType::GetIcon(wxIcon
*icon
,
271 int *iconIndex
) const
276 *iconFile
= m_info
->GetIconFile();
278 *iconIndex
= m_info
->GetIconIndex();
281 if ( icon
&& !m_info
->GetIconFile().empty() )
283 // FIXME: what about the index?
284 icon
->LoadFile(m_info
->GetIconFile());
291 #if defined(__WXMSW__) || defined(__UNIX__)
292 return m_impl
->GetIcon(icon
, iconFile
, iconIndex
);
294 return m_impl
->GetIcon(icon
);
298 bool wxFileType::GetDescription(wxString
*desc
) const
300 wxCHECK_MSG( desc
, FALSE
, _T("invalid parameter in GetDescription") );
304 *desc
= m_info
->GetDescription();
309 return m_impl
->GetDescription(desc
);
313 wxFileType::GetOpenCommand(wxString
*openCmd
,
314 const wxFileType::MessageParameters
& params
) const
316 wxCHECK_MSG( openCmd
, FALSE
, _T("invalid parameter in GetOpenCommand") );
320 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
325 return m_impl
->GetOpenCommand(openCmd
, params
);
329 wxFileType::GetPrintCommand(wxString
*printCmd
,
330 const wxFileType::MessageParameters
& params
) const
332 wxCHECK_MSG( printCmd
, FALSE
, _T("invalid parameter in GetPrintCommand") );
336 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
341 return m_impl
->GetPrintCommand(printCmd
, params
);
345 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
346 wxArrayString
*commands
,
347 const wxFileType::MessageParameters
& params
) const
354 #if defined (__WXMSW__) || defined(__UNIX__)
355 return m_impl
->GetAllCommands(verbs
, commands
, params
);
356 #else // !__WXMSW__ || Unix
357 // we don't know how to retrieve all commands, so just try the 2 we know
361 if ( GetOpenCommand(&cmd
, params
) )
364 verbs
->Add(_T("Open"));
370 if ( GetPrintCommand(&cmd
, params
) )
373 verbs
->Add(_T("Print"));
381 #endif // __WXMSW__/| __UNIX__
384 bool wxFileType::Unassociate()
386 #if defined(__WXMSW__)
387 return m_impl
->Unassociate();
388 #elif defined(__UNIX__)
389 return m_impl
->Unassociate(this);
391 wxFAIL_MSG( _T("not implemented") ); // TODO
396 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
397 bool overwriteprompt
)
399 #if defined (__WXMSW__) || defined(__UNIX__)
400 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
402 wxFAIL_MSG(_T("not implemented"));
407 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
411 // VZ: should we do this?
412 // chris elliott : only makes sense in MS windows
414 GetOpenCommand(&sTmp
, wxFileType::MessageParameters("", ""));
416 wxCHECK_MSG( !sTmp
.empty(), FALSE
, _T("need the icon file") );
418 #if defined (__WXMSW__) || defined(__UNIX__)
419 return m_impl
->SetDefaultIcon (cmd
, index
);
421 wxFAIL_MSG(_T("not implemented"));
428 // ----------------------------------------------------------------------------
429 // wxMimeTypesManager
430 // ----------------------------------------------------------------------------
432 void wxMimeTypesManager::EnsureImpl()
435 m_impl
= new wxMimeTypesManagerImpl
;
438 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
439 const wxString
& wildcard
)
441 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
442 wxT("first MIME type can't contain wildcards") );
444 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
445 if ( wildcard
.BeforeFirst(wxT('/')).
446 IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
448 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
450 if ( strSubtype
== wxT("*") ||
451 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
453 // matches (either exactly or it's a wildcard)
461 wxMimeTypesManager::wxMimeTypesManager()
466 wxMimeTypesManager::~wxMimeTypesManager()
472 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
474 #if defined(__UNIX__)
475 return m_impl
->Unassociate(ft
);
477 return ft
->Unassociate();
483 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
487 #if defined(__WXMSW__) || defined(__UNIX__)
488 return m_impl
->Associate(ftInfo
);
489 #else // other platforms
490 wxFAIL_MSG( _T("not implemented") ); // TODO
496 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
499 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
502 // check the fallbacks
504 // TODO linear search is potentially slow, perhaps we should use a
506 size_t count
= m_fallbacks
.GetCount();
507 for ( size_t n
= 0; n
< count
; n
++ ) {
508 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
509 ft
= new wxFileType(m_fallbacks
[n
]);
520 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
523 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
526 // check the fallbacks
528 // TODO linear search is potentially slow, perhaps we should use a sorted
530 size_t count
= m_fallbacks
.GetCount();
531 for ( size_t n
= 0; n
< count
; n
++ ) {
532 if ( wxMimeTypesManager::IsOfType(mimeType
,
533 m_fallbacks
[n
].GetMimeType()) ) {
534 ft
= new wxFileType(m_fallbacks
[n
]);
544 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
547 return m_impl
->ReadMailcap(filename
, fallback
);
550 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
553 return m_impl
->ReadMimeTypes(filename
);
556 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
559 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
564 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
567 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
569 // add the fallback filetypes
570 size_t count
= m_fallbacks
.GetCount();
571 for ( size_t n
= 0; n
< count
; n
++ ) {
572 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
573 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
581 void wxMimeTypesManager::Initialize(int mcapStyle
,
582 const wxString
& sExtraDir
)
587 m_impl
->Initialize(mcapStyle
, sExtraDir
);
594 // and this function clears all the data from the manager
595 void wxMimeTypesManager::ClearData()
604 // ----------------------------------------------------------------------------
605 // global data and wxMimeTypeCmnModule
606 // ----------------------------------------------------------------------------
609 static wxMimeTypesManager gs_mimeTypesManager
;
611 // and public pointer
612 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
614 class wxMimeTypeCmnModule
: public wxModule
617 wxMimeTypeCmnModule() : wxModule() { }
618 virtual bool OnInit() { return TRUE
; }
619 virtual void OnExit()
621 // this avoids false memory leak allerts:
622 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
624 delete gs_mimeTypesManager
.m_impl
;
625 gs_mimeTypesManager
.m_impl
= NULL
;
629 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
632 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)