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::GetMimeTypes(wxArrayString
& mimeTypes
) const
222 return m_impl
->GetMimeTypes(mimeTypes
);
225 bool wxFileType::GetIcon(wxIcon
*icon
) const
227 return m_impl
->GetIcon(icon
);
230 bool wxFileType::GetDescription(wxString
*desc
) const
232 return m_impl
->GetDescription(desc
);
236 wxFileType::GetOpenCommand(wxString
*openCmd
,
237 const wxFileType::MessageParameters
& params
) const
239 return m_impl
->GetOpenCommand(openCmd
, params
);
243 wxFileType::GetPrintCommand(wxString
*printCmd
,
244 const wxFileType::MessageParameters
& params
) const
246 return m_impl
->GetPrintCommand(printCmd
, params
);
249 // ----------------------------------------------------------------------------
250 // wxMimeTypesManager
251 // ----------------------------------------------------------------------------
253 void wxMimeTypesManager::EnsureImpl()
256 m_impl
= new wxMimeTypesManagerImpl
;
259 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
260 const wxString
& wildcard
)
262 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
263 wxT("first MIME type can't contain wildcards") );
265 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
266 if ( wildcard
.BeforeFirst(wxT('/')).IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
268 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
270 if ( strSubtype
== wxT("*") ||
271 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
273 // matches (either exactly or it's a wildcard)
281 wxMimeTypesManager::wxMimeTypesManager()
286 wxMimeTypesManager::~wxMimeTypesManager()
293 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
296 return m_impl
->GetFileTypeFromExtension(ext
);
300 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
303 return m_impl
->GetFileTypeFromMimeType(mimeType
);
306 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
309 return m_impl
->ReadMailcap(filename
, fallback
);
312 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
315 return m_impl
->ReadMimeTypes(filename
);
318 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
321 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
->IsValid(); ft
++ ) {
322 m_impl
->AddFallback(*ft
);
326 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
329 return m_impl
->EnumAllFileTypes(mimetypes
);
333 // ----------------------------------------------------------------------------
335 // ----------------------------------------------------------------------------
338 static wxMimeTypesManager gs_mimeTypesManager
;
340 // and public pointer
341 wxMimeTypesManager
* wxTheMimeTypesManager
= &gs_mimeTypesManager
;
345 // wxUSE_FILE && wxUSE_TEXTFILE