]> git.saurik.com Git - wxWidgets.git/blame - src/common/mimecmn.cpp
SN: Replace a __WXPM__ by __OS2__ in #if deciding whether to use Unix-
[wxWidgets.git] / src / common / mimecmn.cpp
CommitLineData
7dc3cc31
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: common/mimecmn.cpp
3// Purpose: classes and functions to manage MIME types
4// Author: Vadim Zeitlin
5// Modified by:
6// Created: 23.09.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license (part of wxExtra library)
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "mimetypebase.h"
14#endif
15
16// for compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20 #pragma hdrstop
21#endif
22
23#ifndef WX_PRECOMP
24 #include "wx/defs.h"
25#endif
26
27#if (wxUSE_FILE && wxUSE_TEXTFILE) || defined(__WXMSW__)
28
29#ifndef WX_PRECOMP
30 #include "wx/string.h"
31 #if wxUSE_GUI
32 #include "wx/icon.h"
33 #endif
34#endif //WX_PRECOMP
35
36// Doesn't compile in WIN16 mode
37#ifndef __WIN16__
38
39#include "wx/log.h"
40#include "wx/file.h"
41#include "wx/intl.h"
42#include "wx/dynarray.h"
43#include "wx/confbase.h"
44
45#ifdef __WXMSW__
46 #include "wx/msw/registry.h"
47 #include "windows.h"
48#elif defined(__UNIX__) || defined(__WXPM__)
49 #include "wx/ffile.h"
50 #include "wx/textfile.h"
51 #include "wx/dir.h"
52 #include "wx/utils.h"
53 #include "wx/tokenzr.h"
54#endif // OS
55
56#include "wx/mimetype.h"
57
58// other standard headers
59#include <ctype.h>
60
61// in case we're compiling in non-GUI mode
62class WXDLLEXPORT wxIcon;
63
64
65// implementation classes:
66
67#if defined(__WXMSW__)
68#include "wx/msw/mimetype.h"
69#elif defined (__WXMAC__)
70#include "wx/mac/mimetype.h"
e1c8c2f8
DW
71#elif defined (__WXPM__)
72#include "wx/os2/mimetype.h"
7dc3cc31
VS
73#else
74#include "wx/unix/mimetype.h"
75#endif
76
77// ============================================================================
78// common classes
79// ============================================================================
80
81// ----------------------------------------------------------------------------
82// wxFileTypeInfo
83// ----------------------------------------------------------------------------
84
85wxFileTypeInfo::wxFileTypeInfo(const char *mimeType,
86 const char *openCmd,
87 const char *printCmd,
88 const char *desc,
89 ...)
90 : m_mimeType(mimeType),
91 m_openCmd(openCmd),
92 m_printCmd(printCmd),
93 m_desc(desc)
94{
95 va_list argptr;
96 va_start(argptr, desc);
97
98 for ( ;; )
99 {
100 const char *ext = va_arg(argptr, const char *);
101 if ( !ext )
102 {
103 // NULL terminates the list
104 break;
105 }
106
107 m_exts.Add(ext);
108 }
109
110 va_end(argptr);
111}
112
113#include "wx/arrimpl.cpp"
114WX_DEFINE_OBJARRAY(wxArrayFileTypeInfo);
115
116
117// ============================================================================
118// implementation of the wrapper classes
119// ============================================================================
120
121// ----------------------------------------------------------------------------
122// wxFileType
123// ----------------------------------------------------------------------------
124
125wxString wxFileType::ExpandCommand(const wxString& command,
126 const wxFileType::MessageParameters& params)
127{
128 bool hasFilename = FALSE;
129
130 wxString str;
131 for ( const wxChar *pc = command.c_str(); *pc != wxT('\0'); pc++ ) {
132 if ( *pc == wxT('%') ) {
133 switch ( *++pc ) {
134 case wxT('s'):
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
139#if 0
140 if ( *(pc - 2) == wxT('"') )
141 str << params.GetFileName();
142 else
143 str << wxT('"') << params.GetFileName() << wxT('"');
144#endif
145 str << params.GetFileName();
146 hasFilename = TRUE;
147 break;
148
149 case wxT('t'):
150 // '%t' expands into MIME type (quote it too just to be
151 // consistent)
152 str << wxT('\'') << params.GetMimeType() << wxT('\'');
153 break;
154
155 case wxT('{'):
156 {
157 const wxChar *pEnd = wxStrchr(pc, wxT('}'));
158 if ( pEnd == NULL ) {
159 wxString mimetype;
160 wxLogWarning(_("Unmatched '{' in an entry for "
161 "mime type %s."),
162 params.GetMimeType().c_str());
163 str << wxT("%{");
164 }
165 else {
166 wxString param(pc + 1, pEnd - pc - 1);
167 str << wxT('\'') << params.GetParamValue(param) << wxT('\'');
168 pc = pEnd;
169 }
170 }
171 break;
172
173 case wxT('n'):
174 case wxT('F'):
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.
178 break;
179
180 default:
181 wxLogDebug(wxT("Unknown field %%%c in command '%s'."),
182 *pc, command.c_str());
183 str << *pc;
184 }
185 }
186 else {
187 str << *pc;
188 }
189 }
190
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('\'');
195 }
196
197 return str;
198}
199
200wxFileType::wxFileType()
201{
202 m_impl = new wxFileTypeImpl;
203}
204
205wxFileType::~wxFileType()
206{
207 delete m_impl;
208}
209
210bool wxFileType::GetExtensions(wxArrayString& extensions)
211{
212 return m_impl->GetExtensions(extensions);
213}
214
215bool wxFileType::GetMimeType(wxString *mimeType) const
216{
217 return m_impl->GetMimeType(mimeType);
218}
219
220bool wxFileType::GetIcon(wxIcon *icon) const
221{
222 return m_impl->GetIcon(icon);
223}
224
225bool wxFileType::GetDescription(wxString *desc) const
226{
227 return m_impl->GetDescription(desc);
228}
229
230bool
231wxFileType::GetOpenCommand(wxString *openCmd,
232 const wxFileType::MessageParameters& params) const
233{
234 return m_impl->GetOpenCommand(openCmd, params);
235}
236
237bool
238wxFileType::GetPrintCommand(wxString *printCmd,
239 const wxFileType::MessageParameters& params) const
240{
241 return m_impl->GetPrintCommand(printCmd, params);
242}
243
244// ----------------------------------------------------------------------------
245// wxMimeTypesManager
246// ----------------------------------------------------------------------------
247
248void wxMimeTypesManager::EnsureImpl()
249{
250 if (m_impl == NULL)
251 m_impl = new wxMimeTypesManagerImpl;
252}
253
254bool wxMimeTypesManager::IsOfType(const wxString& mimeType,
255 const wxString& wildcard)
256{
257 wxASSERT_MSG( mimeType.Find(wxT('*')) == wxNOT_FOUND,
258 wxT("first MIME type can't contain wildcards") );
259
260 // all comparaisons are case insensitive (2nd arg of IsSameAs() is FALSE)
261 if ( wildcard.BeforeFirst(wxT('/')).IsSameAs(mimeType.BeforeFirst(wxT('/')), FALSE) )
262 {
263 wxString strSubtype = wildcard.AfterFirst(wxT('/'));
264
265 if ( strSubtype == wxT("*") ||
266 strSubtype.IsSameAs(mimeType.AfterFirst(wxT('/')), FALSE) )
267 {
268 // matches (either exactly or it's a wildcard)
269 return TRUE;
270 }
271 }
272
273 return FALSE;
274}
275
276wxMimeTypesManager::wxMimeTypesManager()
277{
278 m_impl = NULL;
279}
280
281wxMimeTypesManager::~wxMimeTypesManager()
282{
283 if (m_impl != NULL)
284 delete m_impl;
285}
286
287wxFileType *
288wxMimeTypesManager::GetFileTypeFromExtension(const wxString& ext)
289{
290 EnsureImpl();
291 return m_impl->GetFileTypeFromExtension(ext);
292}
293
294wxFileType *
295wxMimeTypesManager::GetFileTypeFromMimeType(const wxString& mimeType)
296{
297 EnsureImpl();
298 return m_impl->GetFileTypeFromMimeType(mimeType);
299}
300
301bool wxMimeTypesManager::ReadMailcap(const wxString& filename, bool fallback)
302{
303 EnsureImpl();
304 return m_impl->ReadMailcap(filename, fallback);
305}
306
307bool wxMimeTypesManager::ReadMimeTypes(const wxString& filename)
308{
309 EnsureImpl();
310 return m_impl->ReadMimeTypes(filename);
311}
312
313void wxMimeTypesManager::AddFallbacks(const wxFileTypeInfo *filetypes)
314{
315 EnsureImpl();
316 for ( const wxFileTypeInfo *ft = filetypes; ft->IsValid(); ft++ ) {
317 m_impl->AddFallback(*ft);
318 }
319}
320
321size_t wxMimeTypesManager::EnumAllFileTypes(wxArrayString& mimetypes)
322{
323 EnsureImpl();
324 return m_impl->EnumAllFileTypes(mimetypes);
325}
326
327
328// ----------------------------------------------------------------------------
329// global data
330// ----------------------------------------------------------------------------
331
332// private object
333static wxMimeTypesManager gs_mimeTypesManager;
334
335// and public pointer
336wxMimeTypesManager * wxTheMimeTypesManager = &gs_mimeTypesManager;
337
338
339#endif
340 // wxUSE_FILE && wxUSE_TEXTFILE
341
342#endif
343 // __WIN16__