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"
27 #if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__)
30 #include "wx/string.h"
36 // Doesn't compile in WIN16 mode
42 #include "wx/dynarray.h"
43 #include "wx/confbase.h"
46 #include "wx/msw/registry.h"
48 #elif defined(__UNIX__) || defined(__WXPM__)
50 #include "wx/textfile.h"
53 #include "wx/tokenzr.h"
56 #include "wx/mimetype.h"
58 // other standard headers
61 // in case we're compiling in non-GUI mode
62 class WXDLLEXPORT wxIcon
;
65 // implementation classes:
67 #if defined(__WXMSW__)
68 #include "wx/msw/mimetype.h"
69 #elif defined (__WXMAC__)
70 #include "wx/mac/mimetype.h"
71 #elif defined (__WXPM__)
72 #include "wx/os2/mimetype.h"
74 #include "wx/unix/mimetype.h"
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxFileTypeInfo::wxFileTypeInfo(const char *mimeType
,
90 : m_mimeType(mimeType
),
96 va_start(argptr
, desc
);
100 const char *ext
= va_arg(argptr
, const char *);
103 // NULL terminates the list
113 #include "wx/arrimpl.cpp"
114 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
117 // ============================================================================
118 // implementation of the wrapper classes
119 // ============================================================================
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 wxString
wxFileType::ExpandCommand(const wxString
& command
,
126 const wxFileType::MessageParameters
& params
)
128 bool hasFilename
= FALSE
;
131 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
132 if ( *pc
== wxT('%') ) {
135 // '%s' expands into file name (quoted because it might
136 // contain spaces) - except if there are already quotes
137 // there because otherwise some programs may get confused
138 // by double double quotes
140 if ( *(pc
- 2) == wxT('"') )
141 str
<< params
.GetFileName();
143 str
<< wxT('"') << params
.GetFileName() << wxT('"');
145 str
<< params
.GetFileName();
150 // '%t' expands into MIME type (quote it too just to be
152 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
157 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
158 if ( pEnd
== NULL
) {
160 wxLogWarning(_("Unmatched '{' in an entry for "
162 params
.GetMimeType().c_str());
166 wxString
param(pc
+ 1, pEnd
- pc
- 1);
167 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
175 // TODO %n is the number of parts, %F is an array containing
176 // the names of temp files these parts were written to
177 // and their mime types.
181 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
182 *pc
, command
.c_str());
191 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
192 // the program will accept the data on stdin: so give it to it!
193 if ( !hasFilename
&& !str
.IsEmpty() ) {
194 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
200 wxFileType::wxFileType()
202 m_impl
= new wxFileTypeImpl
;
205 wxFileType::~wxFileType()
210 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
212 return m_impl
->GetExtensions(extensions
);
215 bool wxFileType::GetMimeType(wxString
*mimeType
) const
217 return m_impl
->GetMimeType(mimeType
);
220 bool wxFileType::GetIcon(wxIcon
*icon
) const
222 return m_impl
->GetIcon(icon
);
225 bool wxFileType::GetDescription(wxString
*desc
) const
227 return m_impl
->GetDescription(desc
);
231 wxFileType::GetOpenCommand(wxString
*openCmd
,
232 const wxFileType::MessageParameters
& params
) const
234 return m_impl
->GetOpenCommand(openCmd
, params
);
238 wxFileType::GetPrintCommand(wxString
*printCmd
,
239 const wxFileType::MessageParameters
& params
) const
241 return m_impl
->GetPrintCommand(printCmd
, params
);
244 // ----------------------------------------------------------------------------
245 // wxMimeTypesManager
246 // ----------------------------------------------------------------------------
248 void wxMimeTypesManager::EnsureImpl()
251 m_impl
= new wxMimeTypesManagerImpl
;
254 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
255 const wxString
& wildcard
)
257 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
258 wxT("first MIME type can't contain wildcards") );
260 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
261 if ( wildcard
.BeforeFirst(wxT('/')).IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
263 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
265 if ( strSubtype
== wxT("*") ||
266 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
268 // matches (either exactly or it's a wildcard)
276 wxMimeTypesManager::wxMimeTypesManager()
281 wxMimeTypesManager::~wxMimeTypesManager()
288 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
291 return m_impl
->GetFileTypeFromExtension(ext
);
295 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
298 return m_impl
->GetFileTypeFromMimeType(mimeType
);
301 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
304 return m_impl
->ReadMailcap(filename
, fallback
);
307 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
310 return m_impl
->ReadMimeTypes(filename
);
313 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
316 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
->IsValid(); ft
++ ) {
317 m_impl
->AddFallback(*ft
);
321 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
324 return m_impl
->EnumAllFileTypes(mimetypes
);
328 // ----------------------------------------------------------------------------
330 // ----------------------------------------------------------------------------
333 static wxMimeTypesManager gs_mimeTypesManager
;
335 // and public pointer
336 wxMimeTypesManager
* wxTheMimeTypesManager
= &gs_mimeTypesManager
;
340 // wxUSE_FILE && wxUSE_TEXTFILE