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"
72 #include "wx/unix/mimetype.h"
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 wxFileTypeInfo::wxFileTypeInfo(const char *mimeType
,
88 : m_mimeType(mimeType
),
94 va_start(argptr
, desc
);
98 const char *ext
= va_arg(argptr
, const char *);
101 // NULL terminates the list
111 #include "wx/arrimpl.cpp"
112 WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo
);
115 // ============================================================================
116 // implementation of the wrapper classes
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 wxString
wxFileType::ExpandCommand(const wxString
& command
,
124 const wxFileType::MessageParameters
& params
)
126 bool hasFilename
= FALSE
;
129 for ( const wxChar
*pc
= command
.c_str(); *pc
!= wxT('\0'); pc
++ ) {
130 if ( *pc
== wxT('%') ) {
133 // '%s' expands into file name (quoted because it might
134 // contain spaces) - except if there are already quotes
135 // there because otherwise some programs may get confused
136 // by double double quotes
138 if ( *(pc
- 2) == wxT('"') )
139 str
<< params
.GetFileName();
141 str
<< wxT('"') << params
.GetFileName() << wxT('"');
143 str
<< params
.GetFileName();
148 // '%t' expands into MIME type (quote it too just to be
150 str
<< wxT('\'') << params
.GetMimeType() << wxT('\'');
155 const wxChar
*pEnd
= wxStrchr(pc
, wxT('}'));
156 if ( pEnd
== NULL
) {
158 wxLogWarning(_("Unmatched '{' in an entry for "
160 params
.GetMimeType().c_str());
164 wxString
param(pc
+ 1, pEnd
- pc
- 1);
165 str
<< wxT('\'') << params
.GetParamValue(param
) << wxT('\'');
173 // TODO %n is the number of parts, %F is an array containing
174 // the names of temp files these parts were written to
175 // and their mime types.
179 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
180 *pc
, command
.c_str());
189 // metamail(1) man page states that if the mailcap entry doesn't have '%s'
190 // the program will accept the data on stdin: so give it to it!
191 if ( !hasFilename
&& !str
.IsEmpty() ) {
192 str
<< wxT(" < '") << params
.GetFileName() << wxT('\'');
198 wxFileType::wxFileType()
200 m_impl
= new wxFileTypeImpl
;
203 wxFileType::~wxFileType()
208 bool wxFileType::GetExtensions(wxArrayString
& extensions
)
210 return m_impl
->GetExtensions(extensions
);
213 bool wxFileType::GetMimeType(wxString
*mimeType
) const
215 return m_impl
->GetMimeType(mimeType
);
218 bool wxFileType::GetIcon(wxIcon
*icon
) const
220 return m_impl
->GetIcon(icon
);
223 bool wxFileType::GetDescription(wxString
*desc
) const
225 return m_impl
->GetDescription(desc
);
229 wxFileType::GetOpenCommand(wxString
*openCmd
,
230 const wxFileType::MessageParameters
& params
) const
232 return m_impl
->GetOpenCommand(openCmd
, params
);
236 wxFileType::GetPrintCommand(wxString
*printCmd
,
237 const wxFileType::MessageParameters
& params
) const
239 return m_impl
->GetPrintCommand(printCmd
, params
);
242 // ----------------------------------------------------------------------------
243 // wxMimeTypesManager
244 // ----------------------------------------------------------------------------
246 void wxMimeTypesManager::EnsureImpl()
249 m_impl
= new wxMimeTypesManagerImpl
;
252 bool wxMimeTypesManager::IsOfType(const wxString
& mimeType
,
253 const wxString
& wildcard
)
255 wxASSERT_MSG( mimeType
.Find(wxT('*')) == wxNOT_FOUND
,
256 wxT("first MIME type can't contain wildcards") );
258 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
259 if ( wildcard
.BeforeFirst(wxT('/')).IsSameAs(mimeType
.BeforeFirst(wxT('/')), FALSE
) )
261 wxString strSubtype
= wildcard
.AfterFirst(wxT('/'));
263 if ( strSubtype
== wxT("*") ||
264 strSubtype
.IsSameAs(mimeType
.AfterFirst(wxT('/')), FALSE
) )
266 // matches (either exactly or it's a wildcard)
274 wxMimeTypesManager::wxMimeTypesManager()
279 wxMimeTypesManager::~wxMimeTypesManager()
286 wxMimeTypesManager::GetFileTypeFromExtension(const wxString
& ext
)
289 return m_impl
->GetFileTypeFromExtension(ext
);
293 wxMimeTypesManager::GetFileTypeFromMimeType(const wxString
& mimeType
)
296 return m_impl
->GetFileTypeFromMimeType(mimeType
);
299 bool wxMimeTypesManager::ReadMailcap(const wxString
& filename
, bool fallback
)
302 return m_impl
->ReadMailcap(filename
, fallback
);
305 bool wxMimeTypesManager::ReadMimeTypes(const wxString
& filename
)
308 return m_impl
->ReadMimeTypes(filename
);
311 void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo
*filetypes
)
314 for ( const wxFileTypeInfo
*ft
= filetypes
; ft
->IsValid(); ft
++ ) {
315 m_impl
->AddFallback(*ft
);
319 size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString
& mimetypes
)
322 return m_impl
->EnumAllFileTypes(mimetypes
);
326 // ----------------------------------------------------------------------------
328 // ----------------------------------------------------------------------------
331 static wxMimeTypesManager gs_mimeTypesManager
;
333 // and public pointer
334 wxMimeTypesManager
* wxTheMimeTypesManager
= &gs_mimeTypesManager
;
338 // wxUSE_FILE && wxUSE_TEXTFILE