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__) || (__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();
390 #if defined(__UNIX__)
391 return m_impl
->Unassociate(this);
394 wxFAIL_MSG( _T("not implemented") ); // TODO
399 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
400 bool overwriteprompt
)
402 #if defined (__WXMSW__) || (__UNIX__)
403 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
405 wxFAIL_MSG(_T("not implemented"));
411 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
415 // VZ: should we do this?
416 // chris elliott : only makes sense in MS windows
418 GetOpenCommand(&sTmp
, wxFileType::MessageParameters("", ""));
420 wxCHECK_MSG( !sTmp
.empty(), FALSE
, _T("need the icon file") );
422 #if defined (__WXMSW__) || (__UNIX__)
423 return m_impl
->SetDefaultIcon (cmd
, index
);
425 wxFAIL_MSG(_T("not implemented"));
432 // ----------------------------------------------------------------------------
433 // wxMimeTypesManager
434 // ----------------------------------------------------------------------------
436 void wxMimeTypesManager::EnsureImpl()
439 m_impl
= new wxMimeTypesManagerImpl
;
442 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
443 const wxString
& wildcard
)
445 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
446 wxT("first MIME type can't contain wildcards") );
448 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
449 if ( wildcard
.BeforeFirst(wxT('/')).
450 IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
452 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
454 if ( strSubtype
== wxT("*") ||
455 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
457 // matches (either exactly or it's a wildcard)
465 wxMimeTypesManager::wxMimeTypesManager()
470 wxMimeTypesManager::~wxMimeTypesManager()
476 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
478 #if defined(__UNIX__)
479 return m_impl
->Unassociate(ft
);
481 return ft
->Unassociate();
487 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
491 #if defined(__WXMSW__) || defined(__UNIX__)
492 return m_impl
->Associate(ftInfo
);
493 #else // other platforms
494 wxFAIL_MSG( _T("not implemented") ); // TODO
500 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
503 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
506 // check the fallbacks
508 // TODO linear search is potentially slow, perhaps we should use a
510 size_t count
= m_fallbacks
.GetCount();
511 for ( size_t n
= 0; n
< count
; n
++ ) {
512 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
513 ft
= new wxFileType(m_fallbacks
[n
]);
524 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
527 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
530 // check the fallbacks
532 // TODO linear search is potentially slow, perhaps we should use a sorted
534 size_t count
= m_fallbacks
.GetCount();
535 for ( size_t n
= 0; n
< count
; n
++ ) {
536 if ( wxMimeTypesManager::IsOfType(mimeType
,
537 m_fallbacks
[n
].GetMimeType()) ) {
538 ft
= new wxFileType(m_fallbacks
[n
]);
548 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
551 return m_impl
->ReadMailcap(filename
, fallback
);
554 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
557 return m_impl
->ReadMimeTypes(filename
);
560 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
563 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
568 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
571 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
573 // add the fallback filetypes
574 size_t count
= m_fallbacks
.GetCount();
575 for ( size_t n
= 0; n
< count
; n
++ ) {
576 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
577 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
585 void wxMimeTypesManager::Initialize(int mcapStyle
,
586 const wxString
& sExtraDir
)
591 m_impl
->Initialize(mcapStyle
, sExtraDir
);
595 // and this function clears all the data from the manager
596 void wxMimeTypesManager::ClearData()
605 // ----------------------------------------------------------------------------
606 // global data and wxMimeTypeCmnModule
607 // ----------------------------------------------------------------------------
610 static wxMimeTypesManager gs_mimeTypesManager
;
612 // and public pointer
613 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
615 class wxMimeTypeCmnModule
: public wxModule
618 wxMimeTypeCmnModule() : wxModule() { }
619 virtual bool OnInit() { return TRUE
; }
620 virtual void OnExit()
622 // this avoids false memory leak allerts:
623 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
625 delete gs_mimeTypesManager
.m_impl
;
626 gs_mimeTypesManager
.m_impl
= NULL
;
630 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
633 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)