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"
35 #include "wx/module.h"
37 // this one is needed for MSVC5
38 #include "wx/module.h"
41 #include "wx/string.h"
50 #include "wx/dynarray.h"
51 #include "wx/confbase.h"
53 #include "wx/mimetype.h"
55 // other standard headers
58 // implementation classes:
59 #if defined(__WXMSW__)
60 #include "wx/msw/mimetype.h"
61 #elif defined(__WXMAC__)
62 #include "wx/mac/mimetype.h"
63 #elif defined(__WXPM__)
64 #include "wx/os2/mimetype.h"
66 #include "wx/unix/mimetype.h"
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 wxFileTypeInfo::wxFileTypeInfo(const wxChar
*mimeType
,
78 const wxChar
*openCmd
,
79 const wxChar
*printCmd
,
82 : m_mimeType(mimeType
),
88 va_start(argptr
, desc
);
92 const wxChar
*ext
= va_arg(argptr
, const wxChar
*);
95 // NULL terminates the list
106 wxFileTypeInfo::wxFileTypeInfo(const wxArrayString
& sArray
)
108 m_mimeType
= sArray
[0u];
109 m_openCmd
= sArray
[1u];
110 m_printCmd
= sArray
[2u];
111 m_desc
= sArray
[3u];
113 size_t count
= sArray
.GetCount();
114 for ( size_t i
= 4; i
< count
; i
++ )
116 m_exts
.Add(sArray
[i
]);
120 #include "wx/arrimpl.cpp"
121 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
123 // ============================================================================
124 // implementation of the wrapper classes
125 // ============================================================================
127 // ----------------------------------------------------------------------------
129 // ----------------------------------------------------------------------------
132 wxString
wxFileType::ExpandCommand(const wxString
& command
,
133 const wxFileType::MessageParameters
& params
)
135 bool hasFilename
= FALSE
;
138 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
139 if ( *pc
== wxT('%') ) {
142 // '%s' expands into file name (quoted because it might
143 // contain spaces) - except if there are already quotes
144 // there because otherwise some programs may get confused
145 // by double double quotes
147 if ( *(pc
- 2) == wxT('"') )
148 str
<< params
.GetFileName();
150 str
<< wxT('"') << params
.GetFileName() << wxT('"');
152 str
<< params
.GetFileName();
157 // '%t' expands into MIME type (quote it too just to be
159 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
164 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
165 if ( pEnd
== NULL
) {
167 wxLogWarning(_("Unmatched '{' in an entry for mime type %s."),
168 params
.GetMimeType().c_str());
172 wxString
param(pc
+ 1, pEnd
- pc
- 1);
173 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
181 // TODO %n is the number of parts, %F is an array containing
182 // the names of temp files these parts were written to
183 // and their mime types.
187 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
188 *pc
, command
.c_str());
197 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
198 // the program will accept the data on stdin so normally we should append
199 // "< %s" to the end of the command in such case, but not all commands
200 // behave like this, in particular a common test is 'test -n "$DISPLAY"'
201 // and appending "< %s" to this command makes the test fail... I don't
202 // know of the correct solution, try to guess what we have to do.
204 // test now carried out on reading file so test should never get here
205 if ( !hasFilename
&& !str
.IsEmpty()
207 && !str
.StartsWith(_T("test "))
210 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
216 wxFileType::wxFileType(const wxFileTypeInfo
& info
)
222 wxFileType::wxFileType()
225 m_impl
= new wxFileTypeImpl
;
228 wxFileType::~wxFileType()
234 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
238 extensions
= m_info
->GetExtensions();
242 return m_impl
->GetExtensions(extensions
);
245 bool wxFileType::GetMimeType(wxString
*mimeType
) const
247 wxCHECK_MSG( mimeType
, FALSE
, _T("invalid parameter in GetMimeType") );
251 *mimeType
= m_info
->GetMimeType();
256 return m_impl
->GetMimeType(mimeType
);
259 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
264 mimeTypes
.Add(m_info
->GetMimeType());
269 return m_impl
->GetMimeTypes(mimeTypes
);
272 bool wxFileType::GetIcon(wxIcon
*icon
,
274 int *iconIndex
) const
279 *iconFile
= m_info
->GetIconFile();
281 *iconIndex
= m_info
->GetIconIndex();
284 if ( icon
&& !m_info
->GetIconFile().empty() )
286 // FIXME: what about the index?
287 icon
->LoadFile(m_info
->GetIconFile());
294 #if defined(__WXMSW__) || defined(__UNIX__)
295 return m_impl
->GetIcon(icon
, iconFile
, iconIndex
);
297 return m_impl
->GetIcon(icon
);
301 bool wxFileType::GetDescription(wxString
*desc
) const
303 wxCHECK_MSG( desc
, FALSE
, _T("invalid parameter in GetDescription") );
307 *desc
= m_info
->GetDescription();
312 return m_impl
->GetDescription(desc
);
316 wxFileType::GetOpenCommand(wxString
*openCmd
,
317 const wxFileType::MessageParameters
& params
) const
319 wxCHECK_MSG( openCmd
, FALSE
, _T("invalid parameter in GetOpenCommand") );
323 *openCmd
= ExpandCommand(m_info
->GetOpenCommand(), params
);
328 return m_impl
->GetOpenCommand(openCmd
, params
);
331 wxString
wxFileType::GetOpenCommand(const wxString
& filename
) const
334 if ( !GetOpenCommand(&cmd
, filename
) )
336 // return empty string to indicate an error
344 wxFileType::GetPrintCommand(wxString
*printCmd
,
345 const wxFileType::MessageParameters
& params
) const
347 wxCHECK_MSG( printCmd
, FALSE
, _T("invalid parameter in GetPrintCommand") );
351 *printCmd
= ExpandCommand(m_info
->GetPrintCommand(), params
);
356 return m_impl
->GetPrintCommand(printCmd
, params
);
360 size_t wxFileType::GetAllCommands(wxArrayString
*verbs
,
361 wxArrayString
*commands
,
362 const wxFileType::MessageParameters
& params
) const
369 #if defined (__WXMSW__) || defined(__UNIX__)
370 return m_impl
->GetAllCommands(verbs
, commands
, params
);
371 #else // !__WXMSW__ || Unix
372 // we don't know how to retrieve all commands, so just try the 2 we know
376 if ( GetOpenCommand(&cmd
, params
) )
379 verbs
->Add(_T("Open"));
385 if ( GetPrintCommand(&cmd
, params
) )
388 verbs
->Add(_T("Print"));
396 #endif // __WXMSW__/| __UNIX__
399 bool wxFileType::Unassociate()
401 #if defined(__WXMSW__)
402 return m_impl
->Unassociate();
403 #elif defined(__UNIX__) && !defined(__WXPM__)
404 return m_impl
->Unassociate(this);
406 wxFAIL_MSG( _T("not implemented") ); // TODO
411 bool wxFileType::SetCommand(const wxString
& cmd
, const wxString
& verb
,
412 bool overwriteprompt
)
414 #if defined (__WXMSW__) || defined(__UNIX__)
415 return m_impl
->SetCommand(cmd
, verb
, overwriteprompt
);
417 wxFAIL_MSG(_T("not implemented"));
422 bool wxFileType::SetDefaultIcon(const wxString
& cmd
, int index
)
426 // VZ: should we do this?
427 // chris elliott : only makes sense in MS windows
429 GetOpenCommand(&sTmp
, wxFileType::MessageParameters(wxT(""), wxT("")));
431 wxCHECK_MSG( !sTmp
.empty(), FALSE
, _T("need the icon file") );
433 #if defined (__WXMSW__) || defined(__UNIX__)
434 return m_impl
->SetDefaultIcon (cmd
, index
);
436 wxFAIL_MSG(_T("not implemented"));
443 // ----------------------------------------------------------------------------
444 // wxMimeTypesManager
445 // ----------------------------------------------------------------------------
447 void wxMimeTypesManager::EnsureImpl()
450 m_impl
= new wxMimeTypesManagerImpl
;
453 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
454 const wxString
& wildcard
)
456 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
457 wxT("first MIME type can't contain wildcards") );
459 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
460 if ( wildcard
.BeforeFirst(wxT('/')).
461 IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
463 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
465 if ( strSubtype
== wxT("*") ||
466 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
468 // matches (either exactly or it's a wildcard)
476 wxMimeTypesManager::wxMimeTypesManager()
481 wxMimeTypesManager::~wxMimeTypesManager()
487 bool wxMimeTypesManager::Unassociate(wxFileType
*ft
)
489 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
490 return m_impl
->Unassociate(ft
);
492 return ft
->Unassociate();
498 wxMimeTypesManager::Associate(const wxFileTypeInfo
& ftInfo
)
502 #if defined(__WXMSW__) || (defined(__UNIX__) && !defined(__WXPM__))
503 return m_impl
->Associate(ftInfo
);
504 #else // other platforms
505 wxFAIL_MSG( _T("not implemented") ); // TODO
511 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
514 wxFileType
*ft
= m_impl
->GetFileTypeFromExtension(ext
);
517 // check the fallbacks
519 // TODO linear search is potentially slow, perhaps we should use a
521 size_t count
= m_fallbacks
.GetCount();
522 for ( size_t n
= 0; n
< count
; n
++ ) {
523 if ( m_fallbacks
[n
].GetExtensions().Index(ext
) != wxNOT_FOUND
) {
524 ft
= new wxFileType(m_fallbacks
[n
]);
535 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
538 wxFileType
*ft
= m_impl
->GetFileTypeFromMimeType(mimeType
);
541 // check the fallbacks
543 // TODO linear search is potentially slow, perhaps we should use a sorted
545 size_t count
= m_fallbacks
.GetCount();
546 for ( size_t n
= 0; n
< count
; n
++ ) {
547 if ( wxMimeTypesManager::IsOfType(mimeType
,
548 m_fallbacks
[n
].GetMimeType()) ) {
549 ft
= new wxFileType(m_fallbacks
[n
]);
559 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
562 return m_impl
->ReadMailcap(filename
, fallback
);
565 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
568 return m_impl
->ReadMimeTypes(filename
);
571 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
574 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
&& ft
->IsValid(); ft
++ ) {
579 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
582 size_t countAll
= m_impl
->EnumAllFileTypes(mimetypes
);
584 // add the fallback filetypes
585 size_t count
= m_fallbacks
.GetCount();
586 for ( size_t n
= 0; n
< count
; n
++ ) {
587 if ( mimetypes
.Index(m_fallbacks
[n
].GetMimeType()) == wxNOT_FOUND
) {
588 mimetypes
.Add(m_fallbacks
[n
].GetMimeType());
596 void wxMimeTypesManager::Initialize(int mcapStyle
,
597 const wxString
& sExtraDir
)
599 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
602 m_impl
->Initialize(mcapStyle
, sExtraDir
);
609 // and this function clears all the data from the manager
610 void wxMimeTypesManager::ClearData()
612 #if defined(__UNIX__) && !defined(__WXPM__) && !defined(__CYGWIN__) && !defined(__WINE__)
619 // ----------------------------------------------------------------------------
620 // global data and wxMimeTypeCmnModule
621 // ----------------------------------------------------------------------------
624 static wxMimeTypesManager gs_mimeTypesManager
;
626 // and public pointer
627 wxMimeTypesManager
*wxTheMimeTypesManager
= &gs_mimeTypesManager
;
629 class wxMimeTypeCmnModule
: public wxModule
632 wxMimeTypeCmnModule() : wxModule() { }
633 virtual bool OnInit() { return TRUE
; }
634 virtual void OnExit()
636 // this avoids false memory leak allerts:
637 if ( gs_mimeTypesManager
.m_impl
!= NULL
)
639 delete gs_mimeTypesManager
.m_impl
;
640 gs_mimeTypesManager
.m_impl
= NULL
;
641 gs_mimeTypesManager
.m_fallbacks
.Clear();
645 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
648 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
650 #endif // wxUSE_MIMETYPE