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
);
288 bool wxFileType::GetDescription(wxString
*desc
) const
290 wxCHECK_MSG( desc
, FALSE
, _T("invalid parameter in GetDescription") );
294 *desc
= m_info
->GetDescription();
299 return m_impl
->GetDescription(desc
);
303 wxFileType::GetOpenCommand(wxString
*openCmd
,
304 const wxFileType::MessageParameters
& params
) const
306 wxCHECK_MSG( openCmd
, FALSE
, _T("invalid parameter in GetOpenCommand") );
310 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
315 return m_impl
->GetOpenCommand(openCmd
, params
);
318 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
321 if ( !GetOpenCommand(&cmd
, filename
) )
323 // return empty string to indicate an error
331 wxFileType::GetPrintCommand(wxString
*printCmd
,
332 const wxFileType::MessageParameters
& params
) const
334 wxCHECK_MSG( printCmd
, FALSE
, _T("invalid parameter in GetPrintCommand") );
338 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
343 return m_impl
->GetPrintCommand(printCmd
, params
);
347 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
348 wxArrayString
*commands
,
349 const wxFileType::MessageParameters
& params
) const
356 #if defined (__WXMSW__) || defined(__UNIX__)
357 return m_impl
->GetAllCommands(verbs
, commands
, params
);
358 #else // !__WXMSW__ || Unix
359 // we don't know how to retrieve all commands, so just try the 2 we know
363 if ( GetOpenCommand(&cmd
, params
) )
366 verbs
->Add(_T("Open"));
372 if ( GetPrintCommand(&cmd
, params
) )
375 verbs
->Add(_T("Print"));
383 #endif // __WXMSW__/| __UNIX__
386 bool wxFileType::Unassociate()
388 #if defined(__WXMSW__)
389 return m_impl
->Unassociate();
390 #elif defined(__UNIX__) && !defined(__WXPM__)
391 return m_impl
->Unassociate(this);
393 wxFAIL_MSG( _T("not implemented") ); // TODO
398 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
399 bool overwriteprompt
)
401 #if defined (__WXMSW__) || defined(__UNIX__)
402 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
404 wxFAIL_MSG(_T("not implemented"));
409 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
413 // VZ: should we do this?
414 // chris elliott : only makes sense in MS windows
416 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxT(""), wxT("")));
418 wxCHECK_MSG( !sTmp
.empty(), FALSE
, _T("need the icon file") );
420 #if defined (__WXMSW__) || defined(__UNIX__)
421 return m_impl
->SetDefaultIcon (cmd
, index
);
423 wxFAIL_MSG(_T("not implemented"));
430 // ----------------------------------------------------------------------------
431 // wxMimeTypesManager
432 // ----------------------------------------------------------------------------
434 void wxMimeTypesManager::EnsureImpl()
437 m_impl
= new wxMimeTypesManagerImpl
;
440 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
441 const wxString
& wildcard
)
443 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
444 wxT("first MIME type can't contain wildcards") );
446 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
447 if ( wildcard
.BeforeFirst(wxT('/')).
448 IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
450 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
452 if ( strSubtype
== wxT("*") ||
453 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
455 // matches (either exactly or it's a wildcard)
463 wxMimeTypesManager::wxMimeTypesManager()
468 wxMimeTypesManager::~wxMimeTypesManager()
474 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
476 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
477 return m_impl
->Unassociate(ft
);
479 return ft
->Unassociate();
485 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
489 #if defined(__WXMSW__) || (defined(__UNIX__) && !defined(__WXPM__))
490 return m_impl
->Associate(ftInfo
);
491 #else // other platforms
492 wxFAIL_MSG( _T("not implemented") ); // TODO
498 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
501 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
504 // check the fallbacks
506 // TODO linear search is potentially slow, perhaps we should use a
508 size_t count
= m_fallbacks
.GetCount();
509 for ( size_t n
= 0; n
< count
; n
++ ) {
510 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
511 ft
= new wxFileType(m_fallbacks
[n
]);
522 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
525 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
528 // check the fallbacks
530 // TODO linear search is potentially slow, perhaps we should use a sorted
532 size_t count
= m_fallbacks
.GetCount();
533 for ( size_t n
= 0; n
< count
; n
++ ) {
534 if ( wxMimeTypesManager::IsOfType(mimeType
,
535 m_fallbacks
[n
].GetMimeType()) ) {
536 ft
= new wxFileType(m_fallbacks
[n
]);
546 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
549 return m_impl
->ReadMailcap(filename
, fallback
);
552 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
555 return m_impl
->ReadMimeTypes(filename
);
558 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
561 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
566 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
569 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
571 // add the fallback filetypes
572 size_t count
= m_fallbacks
.GetCount();
573 for ( size_t n
= 0; n
< count
; n
++ ) {
574 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
575 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
583 void wxMimeTypesManager::Initialize(int mcapStyle
,
584 const wxString
& sExtraDir
)
586 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
589 m_impl
->Initialize(mcapStyle
, sExtraDir
);
596 // and this function clears all the data from the manager
597 void wxMimeTypesManager::ClearData()
599 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
606 // ----------------------------------------------------------------------------
607 // global data and wxMimeTypeCmnModule
608 // ----------------------------------------------------------------------------
611 static wxMimeTypesManager gs_mimeTypesManager
;
613 // and public pointer
614 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
616 class wxMimeTypeCmnModule
: public wxModule
619 wxMimeTypeCmnModule() : wxModule() { }
620 virtual bool OnInit() { return TRUE
; }
621 virtual void OnExit()
623 // this avoids false memory leak allerts:
624 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
626 delete gs_mimeTypesManager
.m_impl
;
627 gs_mimeTypesManager
.m_impl
= NULL
;
628 gs_mimeTypesManager
.m_fallbacks
.Clear();
632 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
635 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
637 #endif // wxUSE_MIMETYPE