1 /////////////////////////////////////////////////////////////////////////////
2 // Name: common/mimecmn.cpp
3 // Purpose: classes and functions to manage MIME types
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license (part of wxExtra library)
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "mimetypebase.h"
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18 #include "wx/module.h"
28 #if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__)
31 #include "wx/string.h"
37 // Doesn't compile in WIN16 mode
43 #include "wx/dynarray.h"
44 #include "wx/confbase.h"
47 #include "wx/msw/registry.h"
49 #elif defined(__UNIX__) || defined(__WXPM__)
51 #include "wx/textfile.h"
54 #include "wx/tokenzr.h"
57 #include "wx/mimetype.h"
59 // other standard headers
62 // in case we're compiling in non-GUI mode
63 class WXDLLEXPORT wxIcon
;
66 // implementation classes:
68 #if defined(__WXMSW__)
69 #include "wx/msw/mimetype.h"
70 #elif defined (__WXMAC__)
71 #include "wx/mac/mimetype.h"
72 #elif defined (__WXPM__)
73 #include "wx/os2/mimetype.h"
75 #include "wx/unix/mimetype.h"
78 // ============================================================================
80 // ============================================================================
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 wxFileTypeInfo::wxFileTypeInfo(const char *mimeType
,
91 : m_mimeType(mimeType
),
97 va_start(argptr
, desc
);
101 const char *ext
= va_arg(argptr
, const char *);
104 // NULL terminates the list
114 #include "wx/arrimpl.cpp"
115 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
118 // ============================================================================
119 // implementation of the wrapper classes
120 // ============================================================================
122 // ----------------------------------------------------------------------------
124 // ----------------------------------------------------------------------------
126 wxString
wxFileType::ExpandCommand(const wxString
& command
,
127 const wxFileType::MessageParameters
& params
)
129 bool hasFilename
= FALSE
;
132 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
133 if ( *pc
== wxT('%') ) {
136 // '%s' expands into file name (quoted because it might
137 // contain spaces) - except if there are already quotes
138 // there because otherwise some programs may get confused
139 // by double double quotes
141 if ( *(pc
- 2) == wxT('"') )
142 str
<< params
.GetFileName();
144 str
<< wxT('"') << params
.GetFileName() << wxT('"');
146 str
<< params
.GetFileName();
151 // '%t' expands into MIME type (quote it too just to be
153 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
158 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
159 if ( pEnd
== NULL
) {
161 wxLogWarning(_("Unmatched '{' in an entry for "
163 params
.GetMimeType().c_str());
167 wxString
param(pc
+ 1, pEnd
- pc
- 1);
168 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
176 // TODO %n is the number of parts, %F is an array containing
177 // the names of temp files these parts were written to
178 // and their mime types.
182 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
183 *pc
, command
.c_str());
192 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
193 // the program will accept the data on stdin: so give it to it!
194 if ( !hasFilename
&& !str
.IsEmpty() ) {
195 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
201 wxFileType::wxFileType()
203 m_impl
= new wxFileTypeImpl
;
206 wxFileType::~wxFileType()
211 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
213 return m_impl
->GetExtensions(extensions
);
216 bool wxFileType::GetMimeType(wxString
*mimeType
) const
218 return m_impl
->GetMimeType(mimeType
);
221 bool wxFileType::GetMimeTypes(wxArrayString
& mimeTypes
) const
223 return m_impl
->GetMimeTypes(mimeTypes
);
226 bool wxFileType::GetIcon(wxIcon
*icon
) const
228 return m_impl
->GetIcon(icon
);
231 bool wxFileType::GetDescription(wxString
*desc
) const
233 return m_impl
->GetDescription(desc
);
237 wxFileType::GetOpenCommand(wxString
*openCmd
,
238 const wxFileType::MessageParameters
& params
) const
240 return m_impl
->GetOpenCommand(openCmd
, params
);
244 wxFileType::GetPrintCommand(wxString
*printCmd
,
245 const wxFileType::MessageParameters
& params
) const
247 return m_impl
->GetPrintCommand(printCmd
, params
);
250 // ----------------------------------------------------------------------------
251 // wxMimeTypesManager
252 // ----------------------------------------------------------------------------
254 void wxMimeTypesManager::EnsureImpl()
257 m_impl
= new wxMimeTypesManagerImpl
;
260 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
261 const wxString
& wildcard
)
263 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
264 wxT("first MIME type can't contain wildcards") );
266 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
267 if ( wildcard
.BeforeFirst(wxT('/')).IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
269 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
271 if ( strSubtype
== wxT("*") ||
272 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
274 // matches (either exactly or it's a wildcard)
282 wxMimeTypesManager::wxMimeTypesManager()
287 wxMimeTypesManager::~wxMimeTypesManager()
294 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
297 return m_impl
->GetFileTypeFromExtension(ext
);
301 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
304 return m_impl
->GetFileTypeFromMimeType(mimeType
);
307 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
310 return m_impl
->ReadMailcap(filename
, fallback
);
313 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
316 return m_impl
->ReadMimeTypes(filename
);
319 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
322 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
->IsValid(); ft
++ ) {
323 m_impl
->AddFallback(*ft
);
327 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
330 return m_impl
->EnumAllFileTypes(mimetypes
);
334 // ----------------------------------------------------------------------------
336 // ----------------------------------------------------------------------------
339 static wxMimeTypesManager gs_mimeTypesManager
;
341 // and public pointer
342 wxMimeTypesManager
* wxTheMimeTypesManager
= &gs_mimeTypesManager
;
348 class wxMimeTypeCmnModule
: public wxModule
350 DECLARE_DYNAMIC_CLASS(wxMimeTypeCmnModule
)
352 wxMimeTypeCmnModule() : wxModule() {}
353 bool OnInit() { return TRUE
; }
355 { // this avoids false memory leak allerts:
356 if (gs_mimeTypesManager
.m_impl
!= NULL
)
358 delete gs_mimeTypesManager
.m_impl
;
359 gs_mimeTypesManager
.m_impl
= NULL
;
364 IMPLEMENT_DYNAMIC_CLASS(wxMimeTypeCmnModule
, wxModule
)
369 // wxUSE_FILE && wxUSE_TEXTFILE