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/module.h"
37 // this one is needed for MSVC5
38 #include "wx/module.h"
41 #include "wx/string.h"
46 #include "wx/iconloc.h"
48 #include "wx/dynarray.h"
49 #include "wx/confbase.h"
51 #include "wx/mimetype.h"
53 // other standard headers
56 // implementation classes:
57 #if defined(__WXMSW__)
58 #include "wx/msw/mimetype.h"
59 #elif defined(__WXMAC__)
60 #include "wx/mac/mimetype.h"
61 #elif defined(__WXPM__)
62 #include "wx/os2/mimetype.h"
64 #include "wx/unix/mimetype.h"
67 // ============================================================================
69 // ============================================================================
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 wxFileTypeInfo::wxFileTypeInfo(const wxChar
*mimeType
,
76 const wxChar
*openCmd
,
77 const wxChar
*printCmd
,
80 : m_mimeType(mimeType
),
86 va_start(argptr
, desc
);
90 const wxChar
*ext
= va_arg(argptr
, const wxChar
*);
93 // NULL terminates the list
104 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
106 m_mimeType
= sArray
[0u];
107 m_openCmd
= sArray
[1u];
108 m_printCmd
= sArray
[2u];
109 m_desc
= sArray
[3u];
111 size_t count
= sArray
.GetCount();
112 for ( size_t i
= 4; i
< count
; i
++ )
114 m_exts
.Add(sArray
[i
]);
118 #include "wx/arrimpl.cpp"
119 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
121 // ============================================================================
122 // implementation of the wrapper classes
123 // ============================================================================
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
130 wxString
wxFileType::ExpandCommand(const wxString
& command
,
131 const wxFileType::MessageParameters
& params
)
133 bool hasFilename
= FALSE
;
136 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
137 if ( *pc
== wxT('%') ) {
140 // '%s' expands into file name (quoted because it might
141 // contain spaces) - except if there are already quotes
142 // there because otherwise some programs may get confused
143 // by double double quotes
145 if ( *(pc
- 2) == wxT('"') )
146 str
<< params
.GetFileName();
148 str
<< wxT('"') << params
.GetFileName() << wxT('"');
150 str
<< params
.GetFileName();
155 // '%t' expands into MIME type (quote it too just to be
157 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
162 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
163 if ( pEnd
== NULL
) {
165 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
166 params
.GetMimeType().c_str());
170 wxString
param(pc
+ 1, pEnd
- pc
- 1);
171 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
179 // TODO %n is the number of parts, %F is an array containing
180 // the names of temp files these parts were written to
181 // and their mime types.
185 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
186 *pc
, command
.c_str());
195 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
196 // the program will accept the data on stdin so normally we should append
197 // "< %s" to the end of the command in such case, but not all commands
198 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
199 // and appending "< %s" to this command makes the test fail... I don't
200 // know of the correct solution, try to guess what we have to do.
202 // test now carried out on reading file so test should never get here
203 if ( !hasFilename
&& !str
.IsEmpty()
205 && !str
.StartsWith(_T("test "))
208 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
214 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
220 wxFileType::wxFileType()
223 m_impl
= new wxFileTypeImpl
;
226 wxFileType::~wxFileType()
232 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
236 extensions
= m_info
->GetExtensions();
240 return m_impl
->GetExtensions(extensions
);
243 bool wxFileType::GetMimeType(wxString
*mimeType
) const
245 wxCHECK_MSG( mimeType
, FALSE
, _T("invalid parameter in GetMimeType") );
249 *mimeType
= m_info
->GetMimeType();
254 return m_impl
->GetMimeType(mimeType
);
257 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
262 mimeTypes
.Add(m_info
->GetMimeType());
267 return m_impl
->GetMimeTypes(mimeTypes
);
270 bool wxFileType::GetIcon(wxIconLocation
*iconLoc
) const
276 iconLoc
->SetFileName(m_info
->GetIconFile());
278 iconLoc
->SetIndex(m_info
->GetIconIndex());
285 return m_impl
->GetIcon(iconLoc
);
289 wxFileType::GetIcon(wxIconLocation
*iconloc
,
290 const MessageParameters
& params
) const
292 if ( !GetIcon(iconloc
) )
297 // we may have "%s" in the icon location string, at least under Windows, so
301 iconloc
->SetFileName(ExpandCommand(iconloc
->GetFileName(), params
));
307 bool wxFileType::GetDescription(wxString
*desc
) const
309 wxCHECK_MSG( desc
, FALSE
, _T("invalid parameter in GetDescription") );
313 *desc
= m_info
->GetDescription();
318 return m_impl
->GetDescription(desc
);
322 wxFileType::GetOpenCommand(wxString
*openCmd
,
323 const wxFileType::MessageParameters
& params
) const
325 wxCHECK_MSG( openCmd
, FALSE
, _T("invalid parameter in GetOpenCommand") );
329 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
334 return m_impl
->GetOpenCommand(openCmd
, params
);
337 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
340 if ( !GetOpenCommand(&cmd
, filename
) )
342 // return empty string to indicate an error
350 wxFileType::GetPrintCommand(wxString
*printCmd
,
351 const wxFileType::MessageParameters
& params
) const
353 wxCHECK_MSG( printCmd
, FALSE
, _T("invalid parameter in GetPrintCommand") );
357 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
362 return m_impl
->GetPrintCommand(printCmd
, params
);
366 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
367 wxArrayString
*commands
,
368 const wxFileType::MessageParameters
& params
) const
375 #if defined (__WXMSW__) || defined(__UNIX__)
376 return m_impl
->GetAllCommands(verbs
, commands
, params
);
377 #else // !__WXMSW__ || Unix
378 // we don't know how to retrieve all commands, so just try the 2 we know
382 if ( GetOpenCommand(&cmd
, params
) )
385 verbs
->Add(_T("Open"));
391 if ( GetPrintCommand(&cmd
, params
) )
394 verbs
->Add(_T("Print"));
402 #endif // __WXMSW__/| __UNIX__
405 bool wxFileType::Unassociate()
407 #if defined(__WXMSW__)
408 return m_impl
->Unassociate();
409 #elif defined(__UNIX__) && !defined(__WXPM__)
410 return m_impl
->Unassociate(this);
412 wxFAIL_MSG( _T("not implemented") ); // TODO
417 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
418 bool overwriteprompt
)
420 #if defined (__WXMSW__) || defined(__UNIX__)
421 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
423 wxFAIL_MSG(_T("not implemented"));
428 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
432 // VZ: should we do this?
433 // chris elliott : only makes sense in MS windows
435 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxT(""), wxT("")));
437 wxCHECK_MSG( !sTmp
.empty(), FALSE
, _T("need the icon file") );
439 #if defined (__WXMSW__) || defined(__UNIX__)
440 return m_impl
->SetDefaultIcon (cmd
, index
);
442 wxFAIL_MSG(_T("not implemented"));
449 // ----------------------------------------------------------------------------
450 // wxMimeTypesManager
451 // ----------------------------------------------------------------------------
453 void wxMimeTypesManager::EnsureImpl()
456 m_impl
= new wxMimeTypesManagerImpl
;
459 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
460 const wxString
& wildcard
)
462 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
463 wxT("first MIME type can't contain wildcards") );
465 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
466 if ( wildcard
.BeforeFirst(wxT('/')).
467 IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
469 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
471 if ( strSubtype
== wxT("*") ||
472 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
474 // matches (either exactly or it's a wildcard)
482 wxMimeTypesManager::wxMimeTypesManager()
487 wxMimeTypesManager::~wxMimeTypesManager()
493 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
495 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
496 return m_impl
->Unassociate(ft
);
498 return ft
->Unassociate();
504 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
508 #if defined(__WXMSW__) || (defined(__UNIX__) && !defined(__WXPM__))
509 return m_impl
->Associate(ftInfo
);
510 #else // other platforms
511 wxFAIL_MSG( _T("not implemented") ); // TODO
517 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
520 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
523 // check the fallbacks
525 // TODO linear search is potentially slow, perhaps we should use a
527 size_t count
= m_fallbacks
.GetCount();
528 for ( size_t n
= 0; n
< count
; n
++ ) {
529 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
530 ft
= new wxFileType(m_fallbacks
[n
]);
541 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
544 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
547 // check the fallbacks
549 // TODO linear search is potentially slow, perhaps we should use a
551 size_t count
= m_fallbacks
.GetCount();
552 for ( size_t n
= 0; n
< count
; n
++ ) {
553 if ( wxMimeTypesManager::IsOfType(mimeType
,
554 m_fallbacks
[n
].GetMimeType()) ) {
555 ft
= new wxFileType(m_fallbacks
[n
]);
565 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
568 return m_impl
->ReadMailcap(filename
, fallback
);
571 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
574 return m_impl
->ReadMimeTypes(filename
);
577 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
580 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
585 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
588 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
590 // add the fallback filetypes
591 size_t count
= m_fallbacks
.GetCount();
592 for ( size_t n
= 0; n
< count
; n
++ ) {
593 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
594 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
602 void wxMimeTypesManager::Initialize(int mcapStyle
,
603 const wxString
& sExtraDir
)
605 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
608 m_impl
->Initialize(mcapStyle
, sExtraDir
);
615 // and this function clears all the data from the manager
616 void wxMimeTypesManager::ClearData()
618 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
625 // ----------------------------------------------------------------------------
626 // global data and wxMimeTypeCmnModule
627 // ----------------------------------------------------------------------------
630 static wxMimeTypesManager gs_mimeTypesManager
;
632 // and public pointer
633 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
635 class wxMimeTypeCmnModule
: public wxModule
638 wxMimeTypeCmnModule() : wxModule() { }
639 virtual bool OnInit() { return TRUE
; }
640 virtual void OnExit()
642 // this avoids false memory leak allerts:
643 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
645 delete gs_mimeTypesManager
.m_impl
;
646 gs_mimeTypesManager
.m_impl
= NULL
;
647 gs_mimeTypesManager
.m_fallbacks
.Clear();
651 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
654 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
656 #endif // wxUSE_MIMETYPE